Skip to content

Commit

Permalink
WIP: also output ascii in hexdump
Browse files Browse the repository at this point in the history
Change-Id: I3c4bf1937d904598d0e8d71a9eabc1a292199def
Signed-off-by: Arne Schwabe <[email protected]>
  • Loading branch information
schwabe committed Nov 10, 2024
1 parent 6c79c7c commit 79b7c1a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
31 changes: 23 additions & 8 deletions src/openvpn/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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...]");
Expand Down
2 changes: 2 additions & 0 deletions src/openvpn/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions tests/unit_tests/openvpn/test_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 79b7c1a

Please sign in to comment.