Move psa_crypto_init() after other init calls

Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
diff --git a/programs/x509/cert_app.c b/programs/x509/cert_app.c
index b212ac3..13d96ea 100644
--- a/programs/x509/cert_app.c
+++ b/programs/x509/cert_app.c
@@ -145,15 +145,6 @@
     char *p, *q;
     const char *pers = "cert_app";
 
-#if defined(MBEDTLS_USE_PSA_CRYPTO)
-    psa_status_t status = psa_crypto_init();
-    if (status != PSA_SUCCESS) {
-        mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
-                        (int) status);
-        goto exit;
-    }
-#endif /* MBEDTLS_USE_PSA_CRYPTO */
-
     /*
      * Set to sane values
      */
@@ -171,6 +162,15 @@
     memset(&cacrl, 0, sizeof(mbedtls_x509_crl));
 #endif
 
+#if defined(MBEDTLS_USE_PSA_CRYPTO)
+    psa_status_t status = psa_crypto_init();
+    if (status != PSA_SUCCESS) {
+        mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
+                        (int) status);
+        goto exit;
+    }
+#endif /* MBEDTLS_USE_PSA_CRYPTO */
+
     if (argc < 2) {
 usage:
         mbedtls_printf(USAGE);
diff --git a/programs/x509/cert_req.c b/programs/x509/cert_req.c
index 0d71f4d..a3eafff 100644
--- a/programs/x509/cert_req.c
+++ b/programs/x509/cert_req.c
@@ -172,6 +172,15 @@
     const char *pers = "csr example app";
     mbedtls_x509_san_list *cur, *prev;
 
+    /*
+     * Set to sane values
+     */
+    mbedtls_x509write_csr_init(&req);
+    mbedtls_pk_init(&key);
+    mbedtls_ctr_drbg_init(&ctr_drbg);
+    memset(buf, 0, sizeof(buf));
+    mbedtls_entropy_init(&entropy);
+
 #if defined(MBEDTLS_USE_PSA_CRYPTO)
     psa_status_t status = psa_crypto_init();
     if (status != PSA_SUCCESS) {
@@ -181,14 +190,6 @@
     }
 #endif /* MBEDTLS_USE_PSA_CRYPTO */
 
-    /*
-     * Set to sane values
-     */
-    mbedtls_x509write_csr_init(&req);
-    mbedtls_pk_init(&key);
-    mbedtls_ctr_drbg_init(&ctr_drbg);
-    memset(buf, 0, sizeof(buf));
-
     if (argc < 2) {
 usage:
         mbedtls_printf(USAGE);
@@ -397,7 +398,6 @@
     mbedtls_printf("  . Seeding the random number generator...");
     fflush(stdout);
 
-    mbedtls_entropy_init(&entropy);
     if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
                                      (const unsigned char *) pers,
                                      strlen(pers))) != 0) {
diff --git a/programs/x509/cert_write.c b/programs/x509/cert_write.c
index bdcae9e..7b47e54 100644
--- a/programs/x509/cert_write.c
+++ b/programs/x509/cert_write.c
@@ -315,15 +315,6 @@
     mbedtls_ctr_drbg_context ctr_drbg;
     const char *pers = "crt example app";
 
-#if defined(MBEDTLS_USE_PSA_CRYPTO)
-    psa_status_t status = psa_crypto_init();
-    if (status != PSA_SUCCESS) {
-        mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
-                        (int) status);
-        goto exit;
-    }
-#endif /* MBEDTLS_USE_PSA_CRYPTO */
-
     /*
      * Set to sane values
      */
@@ -339,6 +330,15 @@
     memset(buf, 0, sizeof(buf));
     memset(serial, 0, sizeof(serial));
 
+#if defined(MBEDTLS_USE_PSA_CRYPTO)
+    psa_status_t status = psa_crypto_init();
+    if (status != PSA_SUCCESS) {
+        mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
+                        (int) status);
+        goto exit;
+    }
+#endif /* MBEDTLS_USE_PSA_CRYPTO */
+
     if (argc < 2) {
 usage:
         mbedtls_printf(USAGE);
diff --git a/programs/x509/crl_app.c b/programs/x509/crl_app.c
index 840f74e..f45d0b8 100644
--- a/programs/x509/crl_app.c
+++ b/programs/x509/crl_app.c
@@ -65,6 +65,11 @@
     int i;
     char *p, *q;
 
+    /*
+     * Set to sane values
+     */
+    mbedtls_x509_crl_init(&crl);
+
 #if defined(MBEDTLS_USE_PSA_CRYPTO)
     psa_status_t status = psa_crypto_init();
     if (status != PSA_SUCCESS) {
@@ -74,11 +79,6 @@
     }
 #endif /* MBEDTLS_USE_PSA_CRYPTO */
 
-    /*
-     * Set to sane values
-     */
-    mbedtls_x509_crl_init(&crl);
-
     if (argc < 2) {
 usage:
         mbedtls_printf(USAGE);
diff --git a/programs/x509/req_app.c b/programs/x509/req_app.c
index b866c8e..c63f896 100644
--- a/programs/x509/req_app.c
+++ b/programs/x509/req_app.c
@@ -65,6 +65,11 @@
     int i;
     char *p, *q;
 
+    /*
+     * Set to sane values
+     */
+    mbedtls_x509_csr_init(&csr);
+
 #if defined(MBEDTLS_USE_PSA_CRYPTO)
     psa_status_t status = psa_crypto_init();
     if (status != PSA_SUCCESS) {
@@ -74,11 +79,6 @@
     }
 #endif /* MBEDTLS_USE_PSA_CRYPTO */
 
-    /*
-     * Set to sane values
-     */
-    mbedtls_x509_csr_init(&csr);
-
     if (argc < 2) {
 usage:
         mbedtls_printf(USAGE);