From 4595e6872d7f9ee5e8720929a3315932b18dae73 Mon Sep 17 00:00:00 2001 From: Michael Schuster Date: Sat, 1 Jun 2024 21:00:33 +0200 Subject: [PATCH] Move print_buf into mbedtls_test_print_buf helper function in sample programs Reduce code duplication and fix missing-prototype error for print_buf Signed-off-by: Michael Schuster --- programs/cipher/cipher_aead_demo.c | 14 +++----------- programs/hash/md_hmac_demo.c | 16 ++++------------ programs/psa/aead_demo.c | 14 +++----------- programs/psa/hmac_demo.c | 16 ++++------------ tests/include/test/helpers.h | 3 +++ tests/src/helpers.c | 9 +++++++++ 6 files changed, 26 insertions(+), 46 deletions(-) diff --git a/programs/cipher/cipher_aead_demo.c b/programs/cipher/cipher_aead_demo.c index 853ec202c6..60a5ea280e 100644 --- a/programs/cipher/cipher_aead_demo.c +++ b/programs/cipher/cipher_aead_demo.c @@ -35,6 +35,8 @@ #include "mbedtls/cipher.h" +#include + #include #include #include @@ -78,16 +80,6 @@ const unsigned char msg2_part2[] = { 0x15, 0x16, 0x17 }; * 32-byte is enough to all the key size supported by this program. */ const unsigned char key_bytes[32] = { 0x2a }; -/* Print the contents of a buffer in hex */ -void print_buf(const char *title, unsigned char *buf, size_t len) -{ - printf("%s:", title); - for (size_t i = 0; i < len; i++) { - printf(" %02x", buf[i]); - } - printf("\n"); -} - /* Run an Mbed TLS function and bail out if it fails. * A string description of the error code can be recovered with: * programs/util/strerror */ @@ -198,7 +190,7 @@ static int aead_encrypt(mbedtls_cipher_context_t *ctx, size_t tag_len, p += tag_len; olen = p - out; - print_buf("out", out, olen); + mbedtls_test_print_buf("out", out, olen); exit: return ret; diff --git a/programs/hash/md_hmac_demo.c b/programs/hash/md_hmac_demo.c index 581816a1d9..a0127ed6b7 100644 --- a/programs/hash/md_hmac_demo.c +++ b/programs/hash/md_hmac_demo.c @@ -32,6 +32,8 @@ #include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize +#include + #include #include @@ -56,16 +58,6 @@ const unsigned char msg2_part2[] = { 0x06, 0x06 }; * This example program uses SHA-256, so a 32-byte key makes sense. */ const unsigned char key_bytes[32] = { 0 }; -/* Print the contents of a buffer in hex */ -void print_buf(const char *title, unsigned char *buf, size_t len) -{ - printf("%s:", title); - for (size_t i = 0; i < len; i++) { - printf(" %02x", buf[i]); - } - printf("\n"); -} - /* Run an Mbed TLS function and bail out if it fails. * A string description of the error code can be recovered with: * programs/util/strerror */ @@ -107,14 +99,14 @@ int hmac_demo(void) CHK(mbedtls_md_hmac_update(&ctx, msg1_part1, sizeof(msg1_part1))); CHK(mbedtls_md_hmac_update(&ctx, msg1_part2, sizeof(msg1_part2))); CHK(mbedtls_md_hmac_finish(&ctx, out)); - print_buf("msg1", out, mbedtls_md_get_size(info)); + mbedtls_test_print_buf("msg1", out, mbedtls_md_get_size(info)); /* compute HMAC(key, msg2_part1 | msg2_part2) */ CHK(mbedtls_md_hmac_reset(&ctx)); // prepare for new operation CHK(mbedtls_md_hmac_update(&ctx, msg2_part1, sizeof(msg2_part1))); CHK(mbedtls_md_hmac_update(&ctx, msg2_part2, sizeof(msg2_part2))); CHK(mbedtls_md_hmac_finish(&ctx, out)); - print_buf("msg2", out, mbedtls_md_get_size(info)); + mbedtls_test_print_buf("msg2", out, mbedtls_md_get_size(info)); exit: mbedtls_md_free(&ctx); diff --git a/programs/psa/aead_demo.c b/programs/psa/aead_demo.c index 619166dba4..b300e3619c 100644 --- a/programs/psa/aead_demo.c +++ b/programs/psa/aead_demo.c @@ -36,6 +36,8 @@ #include "psa/crypto.h" +#include + #include #include #include @@ -81,16 +83,6 @@ const unsigned char msg2_part2[] = { 0x15, 0x16, 0x17 }; * 32-byte is enough to all the key size supported by this program. */ const unsigned char key_bytes[32] = { 0x2a }; -/* Print the contents of a buffer in hex */ -void print_buf(const char *title, uint8_t *buf, size_t len) -{ - printf("%s:", title); - for (size_t i = 0; i < len; i++) { - printf(" %02x", buf[i]); - } - printf("\n"); -} - /* Run a PSA function and bail out if it fails. * The symbolic name of the error code can be recovered using: * programs/psa/psa_constant_name status */ @@ -216,7 +208,7 @@ static int aead_encrypt(psa_key_id_t key, psa_algorithm_t alg, p += olen_tag; olen = p - out; - print_buf("out", out, olen); + mbedtls_test_print_buf("out", out, olen); exit: psa_aead_abort(&op); // required on errors, harmless on success diff --git a/programs/psa/hmac_demo.c b/programs/psa/hmac_demo.c index 205505407f..6ed82989b0 100644 --- a/programs/psa/hmac_demo.c +++ b/programs/psa/hmac_demo.c @@ -32,6 +32,8 @@ #include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize +#include + #include #include @@ -58,16 +60,6 @@ const unsigned char msg2_part2[] = { 0x06, 0x06 }; * This example program uses SHA-256, so a 32-byte key makes sense. */ const unsigned char key_bytes[32] = { 0 }; -/* Print the contents of a buffer in hex */ -void print_buf(const char *title, uint8_t *buf, size_t len) -{ - printf("%s:", title); - for (size_t i = 0; i < len; i++) { - printf(" %02x", buf[i]); - } - printf("\n"); -} - /* Run a PSA function and bail out if it fails. * The symbolic name of the error code can be recovered using: * programs/psa/psa_constant_name status */ @@ -122,14 +114,14 @@ psa_status_t hmac_demo(void) PSA_CHECK(psa_mac_update(&op, msg1_part1, sizeof(msg1_part1))); PSA_CHECK(psa_mac_update(&op, msg1_part2, sizeof(msg1_part2))); PSA_CHECK(psa_mac_sign_finish(&op, out, sizeof(out), &out_len)); - print_buf("msg1", out, out_len); + mbedtls_test_print_buf("msg1", out, out_len); /* compute HMAC(key, msg2_part1 | msg2_part2) */ PSA_CHECK(psa_mac_sign_setup(&op, key, alg)); PSA_CHECK(psa_mac_update(&op, msg2_part1, sizeof(msg2_part1))); PSA_CHECK(psa_mac_update(&op, msg2_part2, sizeof(msg2_part2))); PSA_CHECK(psa_mac_sign_finish(&op, out, sizeof(out), &out_len)); - print_buf("msg2", out, out_len); + mbedtls_test_print_buf("msg2", out, out_len); exit: psa_mac_abort(&op); // needed on error, harmless on success diff --git a/tests/include/test/helpers.h b/tests/include/test/helpers.h index d08100f158..10b321d781 100644 --- a/tests/include/test/helpers.h +++ b/tests/include/test/helpers.h @@ -381,6 +381,9 @@ unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen); int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b, uint32_t a_len, uint32_t b_len); +/* Print the contents of a buffer in hex */ +void mbedtls_test_print_buf(const char *title, unsigned char *buf, size_t len); + #if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) #include "test/fake_external_rng_for_test.h" #endif diff --git a/tests/src/helpers.c b/tests/src/helpers.c index 065d17d3e0..29b2df515d 100644 --- a/tests/src/helpers.c +++ b/tests/src/helpers.c @@ -660,6 +660,15 @@ int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b, return ret; } +void mbedtls_test_print_buf(const char *title, unsigned char *buf, size_t len) +{ + printf("%s:", title); + for (size_t i = 0; i < len; i++) { + printf(" %02x", buf[i]); + } + printf("\n"); +} + #if defined(MBEDTLS_TEST_HOOKS) void mbedtls_test_err_add_check(int high, int low, const char *file, int line)