From 4ba98f5350912ac3da75487e2a4f49fa9b87d666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Fri, 3 Feb 2023 12:25:53 +0100 Subject: [PATCH] Use MBEDTLS_MD_MAX_SIZE in test_suite_md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Not only was the size of 100 arbitrary, it's also not great for testing: using MBEDTLS_MD_MAX_SIZE will get us an ASan error if it ever is too small. Signed-off-by: Manuel Pégourié-Gonnard --- tests/suites/test_suite_md.function | 32 +++++++++-------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/tests/suites/test_suite_md.function b/tests/suites/test_suite_md.function index 988cd39176..e3428a350e 100644 --- a/tests/suites/test_suite_md.function +++ b/tests/suites/test_suite_md.function @@ -134,11 +134,10 @@ void md_info(int md_type, char *md_name, int md_size) void md_text(int md_type, char *text_src_string, data_t *hash) { unsigned char src_str[1000]; - unsigned char output[100]; + unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; const mbedtls_md_info_t *md_info = NULL; memset(src_str, 0x00, 1000); - memset(output, 0x00, 100); strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1); md_info = mbedtls_md_info_from_type(md_type); @@ -155,11 +154,9 @@ void md_text(int md_type, char *text_src_string, data_t *hash) /* BEGIN_CASE */ void md_hex(int md_type, data_t *src_str, data_t *hash) { - unsigned char output[100]; + unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; const mbedtls_md_info_t *md_info = NULL; - memset(output, 0x00, 100); - md_info = mbedtls_md_info_from_type(md_type); TEST_ASSERT(md_info != NULL); @@ -177,7 +174,7 @@ void md_text_multi(int md_type, char *text_src_string, data_t *hash) { unsigned char src_str[1000]; - unsigned char output[100]; + unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; int halfway, len; const mbedtls_md_info_t *md_info = NULL; @@ -187,7 +184,6 @@ void md_text_multi(int md_type, char *text_src_string, mbedtls_md_init(&ctx_copy); memset(src_str, 0x00, 1000); - memset(output, 0x00, 100); strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1); len = strlen((char *) src_str); @@ -212,7 +208,7 @@ void md_text_multi(int md_type, char *text_src_string, hash->len) == 0); /* Test clone */ - memset(output, 0x00, 100); + memset(output, 0x00, sizeof(output)); TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src_str + halfway, len - halfway)); TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output)); @@ -229,7 +225,7 @@ exit: /* BEGIN_CASE */ void md_hex_multi(int md_type, data_t *src_str, data_t *hash) { - unsigned char output[100]; + unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; const mbedtls_md_info_t *md_info = NULL; mbedtls_md_context_t ctx, ctx_copy; int halfway; @@ -237,8 +233,6 @@ void md_hex_multi(int md_type, data_t *src_str, data_t *hash) mbedtls_md_init(&ctx); mbedtls_md_init(&ctx_copy); - memset(output, 0x00, 100); - md_info = mbedtls_md_info_from_type(md_type); TEST_ASSERT(md_info != NULL); TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 0)); @@ -260,7 +254,7 @@ void md_hex_multi(int md_type, data_t *src_str, data_t *hash) hash->len) == 0); /* Test clone */ - memset(output, 0x00, 100); + memset(output, 0x00, sizeof(output)); TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src_str->x + halfway, src_str->len - halfway)); TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output)); @@ -279,11 +273,9 @@ void mbedtls_md_hmac(int md_type, int trunc_size, data_t *key_str, data_t *src_str, data_t *hash) { - unsigned char output[100]; + unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; const mbedtls_md_info_t *md_info = NULL; - memset(output, 0x00, 100); - md_info = mbedtls_md_info_from_type(md_type); TEST_ASSERT(md_info != NULL); @@ -300,15 +292,13 @@ void mbedtls_md_hmac(int md_type, int trunc_size, void md_hmac_multi(int md_type, int trunc_size, data_t *key_str, data_t *src_str, data_t *hash) { - unsigned char output[100]; + unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; const mbedtls_md_info_t *md_info = NULL; mbedtls_md_context_t ctx; int halfway; mbedtls_md_init(&ctx); - memset(output, 0x00, 100); - md_info = mbedtls_md_info_from_type(md_type); TEST_ASSERT(md_info != NULL); TEST_ASSERT(0 == mbedtls_md_setup(&ctx, md_info, 1)); @@ -326,7 +316,7 @@ void md_hmac_multi(int md_type, int trunc_size, data_t *key_str, trunc_size, hash->len) == 0); /* Test again, for reset() */ - memset(output, 0x00, 100); + memset(output, 0x00, sizeof(output)); TEST_ASSERT(0 == mbedtls_md_hmac_reset(&ctx)); TEST_ASSERT(0 == mbedtls_md_hmac_update(&ctx, src_str->x, halfway)); @@ -345,11 +335,9 @@ exit: void mbedtls_md_file(int md_type, char *filename, data_t *hash) { - unsigned char output[100]; + unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0}; const mbedtls_md_info_t *md_info = NULL; - memset(output, 0x00, 100); - md_info = mbedtls_md_info_from_type(md_type); TEST_ASSERT(md_info != NULL);