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_ecjpake.function b/tests/suites/test_suite_ecjpake.function
index 177e09a..7f73195 100644
--- a/tests/suites/test_suite_ecjpake.function
+++ b/tests/suites/test_suite_ecjpake.function
@@ -102,6 +102,7 @@
void ecjpake_invalid_param()
{
mbedtls_ecjpake_context ctx;
+ mbedtls_ecjpake_init(&ctx);
unsigned char buf[42] = { 0 };
size_t const len = sizeof(buf);
mbedtls_ecjpake_role invalid_role = (mbedtls_ecjpake_role) 42;
@@ -110,8 +111,6 @@
MD_PSA_INIT();
- mbedtls_ecjpake_init(&ctx);
-
TEST_EQUAL(MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
mbedtls_ecjpake_setup(&ctx,
invalid_role,
@@ -139,13 +138,13 @@
void read_bad_md(data_t *msg)
{
mbedtls_ecjpake_context corrupt_ctx;
+ mbedtls_ecjpake_init(&corrupt_ctx);
const unsigned char *pw = NULL;
const size_t pw_len = 0;
int any_role = MBEDTLS_ECJPAKE_CLIENT;
MD_PSA_INIT();
- mbedtls_ecjpake_init(&corrupt_ctx);
TEST_ASSERT(mbedtls_ecjpake_setup(&corrupt_ctx, any_role,
MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw,
pw_len) == 0);
@@ -164,13 +163,12 @@
void read_round_one(int role, data_t *msg, int ref_ret)
{
mbedtls_ecjpake_context ctx;
+ mbedtls_ecjpake_init(&ctx);
const unsigned char *pw = NULL;
const size_t pw_len = 0;
MD_PSA_INIT();
- mbedtls_ecjpake_init(&ctx);
-
TEST_ASSERT(mbedtls_ecjpake_setup(&ctx, role,
MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw,
pw_len) == 0);
@@ -187,13 +185,12 @@
void read_round_two_cli(data_t *msg, int ref_ret)
{
mbedtls_ecjpake_context ctx;
+ mbedtls_ecjpake_init(&ctx);
const unsigned char *pw = NULL;
const size_t pw_len = 0;
MD_PSA_INIT();
- mbedtls_ecjpake_init(&ctx);
-
TEST_ASSERT(mbedtls_ecjpake_setup(&ctx, MBEDTLS_ECJPAKE_CLIENT,
MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw,
pw_len) == 0);
@@ -216,13 +213,12 @@
void read_round_two_srv(data_t *msg, int ref_ret)
{
mbedtls_ecjpake_context ctx;
+ mbedtls_ecjpake_init(&ctx);
const unsigned char *pw = NULL;
const size_t pw_len = 0;
MD_PSA_INIT();
- mbedtls_ecjpake_init(&ctx);
-
TEST_ASSERT(mbedtls_ecjpake_setup(&ctx, MBEDTLS_ECJPAKE_SERVER,
MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw,
pw_len) == 0);