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>
diff --git a/tests/suites/test_suite_md.function b/tests/suites/test_suite_md.function
index e3428a3..6b4d09a 100644
--- a/tests/suites/test_suite_md.function
+++ b/tests/suites/test_suite_md.function
@@ -133,17 +133,15 @@
/* 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_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 @@
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 @@
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 @@
/* 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),