From 2a5e213f2d3d2296c4ed3a171ff9d5b6b0da8f24 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 9fc0130ea..d0f08e159 100644 --- a/tests/suites/test_suite_md.function +++ b/tests/suites/test_suite_md.function @@ -132,11 +132,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); @@ -153,11 +152,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); @@ -175,7 +172,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; @@ -185,7 +182,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); @@ -208,7 +204,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)); @@ -225,7 +221,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; @@ -233,8 +229,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)); @@ -254,7 +248,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)); @@ -273,11 +267,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); @@ -294,15 +286,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)); @@ -319,7 +309,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)); @@ -338,11 +328,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);