Systematically call PSA_INIT for MD tests

All tests that call md_setup() or compute a hash of a HMAC may now need
it in some builds.

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 bf875f3..64a4171 100644
--- a/tests/suites/test_suite_md.function
+++ b/tests/suites/test_suite_md.function
@@ -1,5 +1,13 @@
 /* BEGIN_HEADER */
 #include "mbedtls/md.h"
+
+#if defined(MBEDTLS_MD_SOME_PSA)
+#define MD_PSA_INIT()   PSA_INIT()
+#define MD_PSA_DONE()   PSA_DONE()
+#else /* MBEDTLS_MD_SOME_PSA */
+#define MD_PSA_INIT() ((void) 0)
+#define MD_PSA_DONE() ((void) 0)
+#endif /* MBEDTLS_MD_SOME_PSA */
 /* END_HEADER */
 
 /* BEGIN_DEPENDENCIES
@@ -15,10 +23,8 @@
     mbedtls_md_context_t ctx;
     unsigned char out[MBEDTLS_MD_MAX_SIZE] = { 0 };
 
+    MD_PSA_INIT();
     mbedtls_md_init(&ctx);
-#if defined(MBEDTLS_MD_SOME_PSA)
-    PSA_INIT();
-#endif
 
     /*
      * Test that mbedtls_md_list() only returns valid MDs.
@@ -34,9 +40,7 @@
 
 exit:
     mbedtls_md_free(&ctx);
-#if defined(MBEDTLS_MD_SOME_PSA)
-    PSA_DONE();
-#endif
+    MD_PSA_DONE();
 }
 /* END_CASE */
 
@@ -49,6 +53,7 @@
 #endif
     unsigned char buf[1] = { 0 };
 
+    MD_PSA_INIT();
     mbedtls_md_init(&ctx);
 
     TEST_EQUAL(0, mbedtls_md_get_size(NULL));
@@ -107,6 +112,9 @@
 #if defined(MBEDTLS_MD_C)
     TEST_ASSERT(mbedtls_md_info_from_string("no such md") == NULL);
 #endif
+
+exit:
+    MD_PSA_DONE();
 }
 /* END_CASE */
 
@@ -120,6 +128,8 @@
     (void) md_name;
 #endif
 
+    /* Note: PSA Crypto init not needed to info functions */
+
     md_info = mbedtls_md_info_from_type(md_type);
     TEST_ASSERT(md_info != NULL);
 #if defined(MBEDTLS_MD_C)
@@ -150,12 +160,17 @@
     unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
     const mbedtls_md_info_t *md_info = NULL;
 
+    MD_PSA_INIT();
+
     md_info = mbedtls_md_info_from_type(md_type);
     TEST_ASSERT(md_info != NULL);
 
     TEST_EQUAL(0, mbedtls_md(md_info, src, src_len, output));
 
     ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
+
+exit:
+    MD_PSA_DONE();
 }
 /* END_CASE */
 
@@ -165,6 +180,8 @@
     unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
     const mbedtls_md_info_t *md_info = NULL;
 
+    MD_PSA_INIT();
+
     md_info = mbedtls_md_info_from_type(md_type);
     TEST_ASSERT(md_info != NULL);
 
@@ -172,6 +189,9 @@
 
 
     ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
+
+exit:
+    MD_PSA_DONE();
 }
 /* END_CASE */
 
@@ -187,6 +207,8 @@
     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);
 
@@ -220,6 +242,7 @@
 exit:
     mbedtls_md_free(&ctx);
     mbedtls_md_free(&ctx_copy);
+    MD_PSA_DONE();
 }
 /* END_CASE */
 
@@ -231,6 +254,8 @@
     mbedtls_md_context_t ctx, ctx_copy;
     int halfway;
 
+    MD_PSA_INIT();
+
     mbedtls_md_init(&ctx);
     mbedtls_md_init(&ctx_copy);
 
@@ -264,6 +289,7 @@
 exit:
     mbedtls_md_free(&ctx);
     mbedtls_md_free(&ctx_copy);
+    MD_PSA_DONE();
 }
 /* END_CASE */
 
@@ -275,6 +301,8 @@
     unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
     const mbedtls_md_info_t *md_info = NULL;
 
+    MD_PSA_INIT();
+
     md_info = mbedtls_md_info_from_type(md_type);
     TEST_ASSERT(md_info != NULL);
 
@@ -283,6 +311,9 @@
                                   src_str->x, src_str->len, output));
 
     ASSERT_COMPARE(output, trunc_size, hash->x, hash->len);
+
+exit:
+    MD_PSA_DONE();
 }
 /* END_CASE */
 
@@ -295,6 +326,8 @@
     mbedtls_md_context_t ctx;
     int halfway;
 
+    MD_PSA_INIT();
+
     mbedtls_md_init(&ctx);
 
     md_info = mbedtls_md_info_from_type(md_type);
@@ -326,6 +359,7 @@
 
 exit:
     mbedtls_md_free(&ctx);
+    MD_PSA_DONE();
 }
 /* END_CASE */
 
@@ -336,12 +370,17 @@
     unsigned char output[MBEDTLS_MD_MAX_SIZE] = { 0 };
     const mbedtls_md_info_t *md_info = NULL;
 
+    MD_PSA_INIT();
+
     md_info = mbedtls_md_info_from_type(md_type);
     TEST_ASSERT(md_info != NULL);
 
     TEST_EQUAL(0, mbedtls_md_file(md_info, filename, output));
 
     ASSERT_COMPARE(output, mbedtls_md_get_size(md_info), hash->x, hash->len);
+
+exit:
+    MD_PSA_DONE();
 }
 /* END_CASE */
 
@@ -352,6 +391,8 @@
     TEST_ASSERT(md_info != NULL);
     mbedtls_md_context_t ctx1, ctx2;
 
+    /* Intentionally no PSA init here! (Will be done later.) */
+
     mbedtls_md_init(&ctx1);
     mbedtls_md_init(&ctx2);
 
@@ -368,8 +409,10 @@
     mbedtls_md_free(&ctx1);
     mbedtls_md_init(&ctx1);
 
+    /* Now initilize PSA Crypto */
+    MD_PSA_INIT();
+
     /* After PSA Crypto init */
-    PSA_INIT();
     TEST_EQUAL(0, mbedtls_md_setup(&ctx1, md_info, 0));
 #if defined(MBEDTLS_MD_SOME_PSA)
     TEST_EQUAL(ctx1.engine, post_psa_engine);
@@ -386,6 +429,6 @@
 exit:
     mbedtls_md_free(&ctx1);
     mbedtls_md_free(&ctx2);
-    PSA_DONE();
+    MD_PSA_DONE();
 }
 /* END_CASE */