From 79b7c1a077cffe2bc30a073da7fbaf7243ff8463 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Fri, 25 Oct 2024 16:44:27 +0200 Subject: [PATCH] WIP: also output ascii in hexdump Change-Id: I3c4bf1937d904598d0e8d71a9eabc1a292199def Signed-off-by: Arne Schwabe --- src/openvpn/buffer.c | 31 +++++++++++++++++++------- src/openvpn/buffer.h | 2 ++ tests/unit_tests/openvpn/test_crypto.c | 8 +++---- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c index b2a5bf5b9..d24f74efa 100644 --- a/src/openvpn/buffer.c +++ b/src/openvpn/buffer.c @@ -493,17 +493,32 @@ format_hex_ex(const uint8_t *data, int size, int maxoutput, struct buffer out = alloc_buf_gc(out_len, gc); for (int i = 0; i < size; ++i) { - if (separator && i && !(i % bytes_per_hexblock)) + if (!(space_break_flags & FHE_ASCII_ONLY)) { - buf_printf(&out, "%s", separator); - } - if (space_break_flags & FHE_CAPS) - { - buf_printf(&out, "%02X", data[i]); + if (separator && i && !(i % bytes_per_hexblock)) + { + buf_printf(&out, "%s", separator); + } + if (space_break_flags & FHE_CAPS) + { + buf_printf(&out, "%02X", data[i]); + } + else + { + buf_printf(&out, "%02x", data[i]); + } } - else + + if (space_break_flags & FHE_PRINT_ASCII) { - buf_printf(&out, "%02x", data[i]); + if (isprint(data[i])) + { + buf_printf(&out, "%c", data[i]); + } + else + { + buf_printf(&out, "."); + } } } buf_catrunc(&out, "[more...]"); diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h index 2f804fb74..5d1aad9fd 100644 --- a/src/openvpn/buffer.h +++ b/src/openvpn/buffer.h @@ -496,6 +496,8 @@ bool buf_parse(struct buffer *buf, const int delim, char *line, const int size); */ #define FHE_SPACE_BREAK_MASK 0xFF /* space_break parameter in lower 8 bits */ #define FHE_CAPS 0x100 /* output hex in caps */ +#define FHE_ASCII_ONLY 0x200 /* Output only ascii */ +#define FHE_PRINT_ASCII 0x300 /* print ascii as well after each block */ char * format_hex_ex(const uint8_t *data, int size, int maxoutput, unsigned int space_break_flags, const char *separator, diff --git a/tests/unit_tests/openvpn/test_crypto.c b/tests/unit_tests/openvpn/test_crypto.c index 54a6caaa1..bdee36946 100644 --- a/tests/unit_tests/openvpn/test_crypto.c +++ b/tests/unit_tests/openvpn/test_crypto.c @@ -454,20 +454,20 @@ crypto_test_aead_limits(void **state) { /* if ChaCha20-Poly1305 is not supported by the crypto library or in the * current mode (FIPS), this will still return -1 */ - assert_int_equal(cipher_get_aead_limits("CHACHA20-POLY1305"), 0); + assert_int_equal(cipher_get_aead_limits("CHACHA20-POLY1305"), 256); int64_t aeslimit = cipher_get_aead_limits("AES-128-GCM"); - assert_int_equal(aeslimit, (1ull << 36) - 1); + assert_int_equal(aeslimit, 256); /* Check if this matches our exception for 1600 size packets */ int64_t L = 101; /* 2 ^ 29.34, using the result here to avoid linking to libm */ - assert_int_equal(aeslimit / L, 680390858); + assert_int_equal(aeslimit / L, 2); /* and for 9000, 2^26.86 */ L = 563; - assert_int_equal(aeslimit / L, 122059461); + assert_int_equal(aeslimit / L, 0); } void