Move psa_crypto_init() after other init calls

Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
This commit is contained in:
Przemek Stekiel 2023-04-17 11:10:05 +02:00
parent 6260ee9cab
commit a0a1c1eab5
13 changed files with 119 additions and 118 deletions

View file

@ -97,16 +97,6 @@ int main(int argc, char *argv[])
((void) argc); ((void) argc);
((void) argv); ((void) argv);
#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);
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */
#if defined(MBEDTLS_DEBUG_C) #if defined(MBEDTLS_DEBUG_C)
mbedtls_debug_set_threshold(DEBUG_LEVEL); mbedtls_debug_set_threshold(DEBUG_LEVEL);
#endif #endif
@ -119,11 +109,21 @@ int main(int argc, char *argv[])
mbedtls_ssl_config_init(&conf); mbedtls_ssl_config_init(&conf);
mbedtls_x509_crt_init(&cacert); mbedtls_x509_crt_init(&cacert);
mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_entropy_init(&entropy);
#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);
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */
mbedtls_printf("\n . Seeding the random number generator..."); mbedtls_printf("\n . Seeding the random number generator...");
fflush(stdout); fflush(stdout);
mbedtls_entropy_init(&entropy);
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers, (const unsigned char *) pers,
strlen(pers))) != 0) { strlen(pers))) != 0) {

View file

@ -106,16 +106,6 @@ int main(void)
mbedtls_ssl_cache_context cache; mbedtls_ssl_cache_context cache;
#endif #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);
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */
mbedtls_net_init(&listen_fd); mbedtls_net_init(&listen_fd);
mbedtls_net_init(&client_fd); mbedtls_net_init(&client_fd);
mbedtls_ssl_init(&ssl); mbedtls_ssl_init(&ssl);
@ -129,6 +119,16 @@ int main(void)
mbedtls_entropy_init(&entropy); mbedtls_entropy_init(&entropy);
mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_ctr_drbg_init(&ctr_drbg);
#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);
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */
#if defined(MBEDTLS_DEBUG_C) #if defined(MBEDTLS_DEBUG_C)
mbedtls_debug_set_threshold(DEBUG_LEVEL); mbedtls_debug_set_threshold(DEBUG_LEVEL);
#endif #endif

View file

@ -166,6 +166,17 @@ int main(void)
mbedtls_ssl_config conf; mbedtls_ssl_config conf;
mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_ctr_drbg_init(&ctr_drbg);
/*
* 0. Initialize and setup stuff
*/
mbedtls_net_init(&server_fd);
mbedtls_ssl_init(&ssl);
mbedtls_ssl_config_init(&conf);
#if defined(MBEDTLS_X509_CRT_PARSE_C)
mbedtls_x509_crt_init(&ca);
#endif
mbedtls_entropy_init(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO) #if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init(); psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
@ -176,17 +187,6 @@ int main(void)
} }
#endif /* MBEDTLS_USE_PSA_CRYPTO */ #endif /* MBEDTLS_USE_PSA_CRYPTO */
/*
* 0. Initialize and setup stuff
*/
mbedtls_net_init(&server_fd);
mbedtls_ssl_init(&ssl);
mbedtls_ssl_config_init(&conf);
#if defined(MBEDTLS_X509_CRT_PARSE_C)
mbedtls_x509_crt_init(&ca);
#endif
mbedtls_entropy_init(&entropy);
if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers, strlen(pers)) != 0) { (const unsigned char *) pers, strlen(pers)) != 0) {
ret = ctr_drbg_seed_failed; ret = ctr_drbg_seed_failed;

View file

@ -83,6 +83,16 @@ int main(void)
mbedtls_debug_set_threshold(DEBUG_LEVEL); mbedtls_debug_set_threshold(DEBUG_LEVEL);
#endif #endif
/*
* 0. Initialize the RNG and the session data
*/
mbedtls_net_init(&server_fd);
mbedtls_ssl_init(&ssl);
mbedtls_ssl_config_init(&conf);
mbedtls_x509_crt_init(&cacert);
mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_entropy_init(&entropy);
#if defined(MBEDTLS_USE_PSA_CRYPTO) #if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init(); psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
@ -92,19 +102,10 @@ int main(void)
} }
#endif /* MBEDTLS_USE_PSA_CRYPTO */ #endif /* MBEDTLS_USE_PSA_CRYPTO */
/*
* 0. Initialize the RNG and the session data
*/
mbedtls_net_init(&server_fd);
mbedtls_ssl_init(&ssl);
mbedtls_ssl_config_init(&conf);
mbedtls_x509_crt_init(&cacert);
mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_printf("\n . Seeding the random number generator..."); mbedtls_printf("\n . Seeding the random number generator...");
fflush(stdout); fflush(stdout);
mbedtls_entropy_init(&entropy);
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers, (const unsigned char *) pers,
strlen(pers))) != 0) { strlen(pers))) != 0) {

View file

@ -96,15 +96,6 @@ int main(void)
mbedtls_x509_crt srvcert; mbedtls_x509_crt srvcert;
mbedtls_pk_context pkey; mbedtls_pk_context pkey;
#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 */
mbedtls_net_init(&listen_fd); mbedtls_net_init(&listen_fd);
mbedtls_net_init(&client_fd); mbedtls_net_init(&client_fd);
mbedtls_ssl_init(&ssl); mbedtls_ssl_init(&ssl);
@ -114,6 +105,15 @@ int main(void)
mbedtls_x509_crt_init(&srvcert); mbedtls_x509_crt_init(&srvcert);
mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_ctr_drbg_init(&ctr_drbg);
#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 */
signal(SIGCHLD, SIG_IGN); signal(SIGCHLD, SIG_IGN);
/* /*

View file

@ -358,15 +358,6 @@ int main(int argc, char *argv[])
char *p, *q; char *p, *q;
const int *list; const int *list;
#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 */
/* /*
* Make sure memory references are valid in case we exit early. * Make sure memory references are valid in case we exit early.
*/ */
@ -378,6 +369,16 @@ int main(int argc, char *argv[])
mbedtls_x509_crt_init(&clicert); mbedtls_x509_crt_init(&clicert);
mbedtls_pk_init(&pkey); mbedtls_pk_init(&pkey);
mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_entropy_init(&entropy);
#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) { if (argc < 2) {
usage: usage:
@ -467,7 +468,6 @@ usage:
mbedtls_printf("\n . Seeding the random number generator..."); mbedtls_printf("\n . Seeding the random number generator...");
fflush(stdout); fflush(stdout);
mbedtls_entropy_init(&entropy);
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers, (const unsigned char *) pers,
strlen(pers))) != 0) { strlen(pers))) != 0) {

View file

@ -306,16 +306,6 @@ int main(void)
mbedtls_ssl_cache_context cache; mbedtls_ssl_cache_context cache;
#endif #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);
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf)); mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
#endif #endif
@ -342,6 +332,16 @@ int main(void)
*/ */
mbedtls_entropy_init(&entropy); mbedtls_entropy_init(&entropy);
#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);
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */
/* /*
* 1a. Seed the random number generator * 1a. Seed the random number generator
*/ */

View file

@ -92,16 +92,6 @@ int main(void)
mbedtls_ssl_cache_context cache; mbedtls_ssl_cache_context cache;
#endif #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);
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */
mbedtls_net_init(&listen_fd); mbedtls_net_init(&listen_fd);
mbedtls_net_init(&client_fd); mbedtls_net_init(&client_fd);
mbedtls_ssl_init(&ssl); mbedtls_ssl_init(&ssl);
@ -114,6 +104,16 @@ int main(void)
mbedtls_entropy_init(&entropy); mbedtls_entropy_init(&entropy);
mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_ctr_drbg_init(&ctr_drbg);
#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);
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
goto exit;
}
#endif /* MBEDTLS_USE_PSA_CRYPTO */
#if defined(MBEDTLS_DEBUG_C) #if defined(MBEDTLS_DEBUG_C)
mbedtls_debug_set_threshold(DEBUG_LEVEL); mbedtls_debug_set_threshold(DEBUG_LEVEL);
#endif #endif

View file

@ -145,15 +145,6 @@ int main(int argc, char *argv[])
char *p, *q; char *p, *q;
const char *pers = "cert_app"; 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 * Set to sane values
*/ */
@ -171,6 +162,15 @@ int main(int argc, char *argv[])
memset(&cacrl, 0, sizeof(mbedtls_x509_crl)); memset(&cacrl, 0, sizeof(mbedtls_x509_crl));
#endif #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) { if (argc < 2) {
usage: usage:
mbedtls_printf(USAGE); mbedtls_printf(USAGE);

View file

@ -172,6 +172,15 @@ int main(int argc, char *argv[])
const char *pers = "csr example app"; const char *pers = "csr example app";
mbedtls_x509_san_list *cur, *prev; 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) #if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init(); psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
@ -181,14 +190,6 @@ int main(int argc, char *argv[])
} }
#endif /* MBEDTLS_USE_PSA_CRYPTO */ #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) { if (argc < 2) {
usage: usage:
mbedtls_printf(USAGE); mbedtls_printf(USAGE);
@ -397,7 +398,6 @@ usage:
mbedtls_printf(" . Seeding the random number generator..."); mbedtls_printf(" . Seeding the random number generator...");
fflush(stdout); fflush(stdout);
mbedtls_entropy_init(&entropy);
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers, (const unsigned char *) pers,
strlen(pers))) != 0) { strlen(pers))) != 0) {

View file

@ -315,15 +315,6 @@ int main(int argc, char *argv[])
mbedtls_ctr_drbg_context ctr_drbg; mbedtls_ctr_drbg_context ctr_drbg;
const char *pers = "crt example app"; 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 * Set to sane values
*/ */
@ -339,6 +330,15 @@ int main(int argc, char *argv[])
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
memset(serial, 0, sizeof(serial)); 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) { if (argc < 2) {
usage: usage:
mbedtls_printf(USAGE); mbedtls_printf(USAGE);

View file

@ -65,6 +65,11 @@ int main(int argc, char *argv[])
int i; int i;
char *p, *q; char *p, *q;
/*
* Set to sane values
*/
mbedtls_x509_crl_init(&crl);
#if defined(MBEDTLS_USE_PSA_CRYPTO) #if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init(); psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
@ -74,11 +79,6 @@ int main(int argc, char *argv[])
} }
#endif /* MBEDTLS_USE_PSA_CRYPTO */ #endif /* MBEDTLS_USE_PSA_CRYPTO */
/*
* Set to sane values
*/
mbedtls_x509_crl_init(&crl);
if (argc < 2) { if (argc < 2) {
usage: usage:
mbedtls_printf(USAGE); mbedtls_printf(USAGE);

View file

@ -65,6 +65,11 @@ int main(int argc, char *argv[])
int i; int i;
char *p, *q; char *p, *q;
/*
* Set to sane values
*/
mbedtls_x509_csr_init(&csr);
#if defined(MBEDTLS_USE_PSA_CRYPTO) #if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_status_t status = psa_crypto_init(); psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
@ -74,11 +79,6 @@ int main(int argc, char *argv[])
} }
#endif /* MBEDTLS_USE_PSA_CRYPTO */ #endif /* MBEDTLS_USE_PSA_CRYPTO */
/*
* Set to sane values
*/
mbedtls_x509_csr_init(&csr);
if (argc < 2) { if (argc < 2) {
usage: usage:
mbedtls_printf(USAGE); mbedtls_printf(USAGE);