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_hmac_drbg.function b/tests/suites/test_suite_hmac_drbg.function
index 0a50c6c..fbe1b03 100644
--- a/tests/suites/test_suite_hmac_drbg.function
+++ b/tests/suites/test_suite_hmac_drbg.function
@@ -41,8 +41,6 @@
size_t default_entropy_len;
size_t expected_consumed_entropy = 0;
- MD_PSA_INIT();
-
mbedtls_hmac_drbg_init(&ctx);
memset(buf, 0, sizeof(buf));
memset(out, 0, sizeof(out));
@@ -50,6 +48,8 @@
entropy.len = sizeof(buf);
entropy.p = buf;
+ MD_PSA_INIT();
+
md_info = mbedtls_md_info_from_type(md_alg);
TEST_ASSERT(md_info != NULL);
if (mbedtls_md_get_size(md_info) <= 20) {
@@ -129,11 +129,10 @@
{
const mbedtls_md_info_t *md_info;
mbedtls_hmac_drbg_context ctx;
+ mbedtls_hmac_drbg_init(&ctx);
MD_PSA_INIT();
- mbedtls_hmac_drbg_init(&ctx);
-
md_info = mbedtls_md_info_from_type(md_alg);
TEST_ASSERT(md_info != NULL);
@@ -159,12 +158,12 @@
mbedtls_hmac_drbg_context ctx;
size_t i;
- MD_PSA_INIT();
-
mbedtls_hmac_drbg_init(&ctx);
memset(buf, 0, sizeof(buf));
memset(out, 0, sizeof(out));
+ MD_PSA_INIT();
+
md_info = mbedtls_md_info_from_type(md_alg);
TEST_ASSERT(md_info != NULL);
TEST_ASSERT(mbedtls_hmac_drbg_seed_buf(&ctx, md_info, buf, sizeof(buf)) == 0);
@@ -194,13 +193,13 @@
const mbedtls_md_info_t *md_info;
mbedtls_hmac_drbg_context ctx;
- MD_PSA_INIT();
-
mbedtls_hmac_drbg_init(&ctx);
p_entropy.p = entropy->x;
p_entropy.len = entropy->len;
+ MD_PSA_INIT();
+
md_info = mbedtls_md_info_from_type(md_alg);
TEST_ASSERT(md_info != NULL);
@@ -244,13 +243,13 @@
const mbedtls_md_info_t *md_info;
mbedtls_hmac_drbg_context ctx;
- MD_PSA_INIT();
-
mbedtls_hmac_drbg_init(&ctx);
p_entropy.p = entropy->x;
p_entropy.len = entropy->len;
+ MD_PSA_INIT();
+
md_info = mbedtls_md_info_from_type(md_alg);
TEST_ASSERT(md_info != NULL);
@@ -279,13 +278,13 @@
const mbedtls_md_info_t *md_info;
mbedtls_hmac_drbg_context ctx;
- MD_PSA_INIT();
-
mbedtls_hmac_drbg_init(&ctx);
p_entropy.p = entropy->x;
p_entropy.len = entropy->len;
+ MD_PSA_INIT();
+
md_info = mbedtls_md_info_from_type(md_alg);
TEST_ASSERT(md_info != NULL);