Avoid unnecessary copy in test_suite_md

Also avoids buffer with an arbitrary size while at it.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
This commit is contained in:
Manuel Pégourié-Gonnard 2023-02-03 12:32:41 +01:00
parent 4ba98f5350
commit b707bedca4

View File

@ -133,17 +133,15 @@ void md_info(int md_type, char *md_name, int md_size)
/* BEGIN_CASE */
void md_text(int md_type, char *text_src_string, data_t *hash)
{
unsigned char src_str[1000];
unsigned char *src = (unsigned char *) text_src_string;
size_t src_len = strlen(text_src_string);
unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
const mbedtls_md_info_t *md_info = NULL;
memset(src_str, 0x00, 1000);
strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1);
md_info = mbedtls_md_info_from_type(md_type);
TEST_ASSERT(md_info != NULL);
TEST_ASSERT(0 == mbedtls_md(md_info, src_str, strlen((char *) src_str), output));
TEST_ASSERT(0 == mbedtls_md(md_info, src, src_len, output));
TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
mbedtls_md_get_size(md_info),
@ -173,9 +171,10 @@ void md_hex(int md_type, data_t *src_str, data_t *hash)
void md_text_multi(int md_type, char *text_src_string,
data_t *hash)
{
unsigned char src_str[1000];
unsigned char *src = (unsigned char *) text_src_string;
size_t src_len = strlen(text_src_string);
unsigned char output[MBEDTLS_MD_MAX_SIZE] = {0};
int halfway, len;
size_t halfway;
const mbedtls_md_info_t *md_info = NULL;
mbedtls_md_context_t ctx, ctx_copy;
@ -183,11 +182,7 @@ void md_text_multi(int md_type, char *text_src_string,
mbedtls_md_init(&ctx);
mbedtls_md_init(&ctx_copy);
memset(src_str, 0x00, 1000);
strncpy((char *) src_str, text_src_string, sizeof(src_str) - 1);
len = strlen((char *) src_str);
halfway = len / 2;
halfway = src_len / 2;
md_info = mbedtls_md_info_from_type(md_type);
TEST_ASSERT(md_info != NULL);
@ -198,10 +193,10 @@ void md_text_multi(int md_type, char *text_src_string,
TEST_ASSERT(0 == mbedtls_md_starts(&ctx));
TEST_ASSERT(ctx.md_ctx != NULL);
TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str, halfway));
TEST_ASSERT(0 == mbedtls_md_update(&ctx, src, halfway));
TEST_ASSERT(0 == mbedtls_md_clone(&ctx_copy, &ctx));
TEST_ASSERT(0 == mbedtls_md_update(&ctx, src_str + halfway, len - halfway));
TEST_ASSERT(0 == mbedtls_md_update(&ctx, src + halfway, src_len - halfway));
TEST_ASSERT(0 == mbedtls_md_finish(&ctx, output));
TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
mbedtls_md_get_size(md_info),
@ -210,7 +205,7 @@ void md_text_multi(int md_type, char *text_src_string,
/* Test clone */
memset(output, 0x00, sizeof(output));
TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src_str + halfway, len - halfway));
TEST_ASSERT(0 == mbedtls_md_update(&ctx_copy, src + halfway, src_len - halfway));
TEST_ASSERT(0 == mbedtls_md_finish(&ctx_copy, output));
TEST_ASSERT(mbedtls_test_hexcmp(output, hash->x,
mbedtls_md_get_size(md_info),