Fix MD_PSA_INIT called before initializing some data structures
This fixes accesses to uninitialized memory in test code if
`psa_crypto_init()` fails.
A lot of those were pointed out by Coverity. I quickly reviewed all calls to
`MD_PSA_INIT()` manually, rather than follow any particular list.
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/tests/suites/test_suite_md.function b/tests/suites/test_suite_md.function
index 2a885e2..4e62154 100644
--- a/tests/suites/test_suite_md.function
+++ b/tests/suites/test_suite_md.function
@@ -21,10 +21,10 @@
const int *md_type_ptr;
const mbedtls_md_info_t *info;
mbedtls_md_context_t ctx;
+ mbedtls_md_init(&ctx);
unsigned char out[MBEDTLS_MD_MAX_SIZE] = { 0 };
MD_PSA_INIT();
- mbedtls_md_init(&ctx);
/*
* Test that mbedtls_md_list() only returns valid MDs.
@@ -87,13 +87,13 @@
void md_null_args()
{
mbedtls_md_context_t ctx;
+ mbedtls_md_init(&ctx);
#if defined(MBEDTLS_MD_C)
const mbedtls_md_info_t *info = mbedtls_md_info_from_type(*(mbedtls_md_list()));
#endif
unsigned char buf[1] = { 0 };
MD_PSA_INIT();
- mbedtls_md_init(&ctx);
TEST_EQUAL(0, mbedtls_md_get_size(NULL));
#if defined(MBEDTLS_MD_C)
@@ -245,12 +245,11 @@
const mbedtls_md_info_t *md_info = NULL;
mbedtls_md_context_t ctx, ctx_copy;
-
- MD_PSA_INIT();
-
mbedtls_md_init(&ctx);
mbedtls_md_init(&ctx_copy);
+ MD_PSA_INIT();
+
halfway = src_len / 2;
md_info = mbedtls_md_info_from_type(md_type);
@@ -291,13 +290,12 @@
unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
const mbedtls_md_info_t *md_info = NULL;
mbedtls_md_context_t ctx, ctx_copy;
+ mbedtls_md_init(&ctx);
+ mbedtls_md_init(&ctx_copy);
int halfway;
MD_PSA_INIT();
- mbedtls_md_init(&ctx);
- mbedtls_md_init(&ctx_copy);
-
md_info = mbedtls_md_info_from_type(md_type);
TEST_ASSERT(md_info != NULL);
TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 0));
@@ -363,12 +361,11 @@
unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
const mbedtls_md_info_t *md_info = NULL;
mbedtls_md_context_t ctx;
+ mbedtls_md_init(&ctx);
int halfway;
MD_PSA_INIT();
- mbedtls_md_init(&ctx);
-
md_info = mbedtls_md_info_from_type(md_type);
TEST_ASSERT(md_info != NULL);
TEST_EQUAL(0, mbedtls_md_setup(&ctx, md_info, 1));