Skip to content

Commit dea02cc

Browse files
committed
psa: Initialize crypto in tests that need to
Don't unconditionally enable PSA Crypto for all tests. Only enable it in tests that require it. This allows crypto tests to check that psa_crypto_init() fails when it is supposed to fail, since we want to perform some action in a test, and then call psa_crypto_init() and check the result without it having been called previously.
1 parent 772ce79 commit dea02cc

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

tests/suites/main_test.function

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -255,20 +255,6 @@ int main( int argc, const char *argv[] )
255255
return( -1 );
256256
}
257257

258-
#if defined(MBEDTLS_USE_PSA_CRYPTO)
259-
{
260-
psa_status_t status;
261-
status = psa_crypto_init();
262-
if( status != PSA_SUCCESS )
263-
{
264-
mbedtls_fprintf( stderr,
265-
"FATAL: Failed to initialize PSA Crypto - error %d\n",
266-
status );
267-
return( -1 );
268-
}
269-
}
270-
#endif /* MBEDTLS_USE_PSA_CRYPTO */
271-
272258
ret = execute_tests( argc, argv );
273259
platform_teardown();
274260
return( ret );

tests/suites/test_suite_cipher.function

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,8 @@ void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
962962
#else
963963
if( use_psa == 1 )
964964
{
965+
TEST_ASSERT( psa_crypto_init() == 0 );
966+
965967
/* PSA requires that the tag immediately follows the ciphertext. */
966968
tmp_cipher = mbedtls_calloc( 1, cipher->len + tag->len );
967969
TEST_ASSERT( tmp_cipher != NULL );
@@ -1104,6 +1106,7 @@ void test_vec_crypt( int cipher_id, int operation, char *hex_key,
11041106
#else
11051107
if( use_psa == 1 )
11061108
{
1109+
TEST_ASSERT( psa_crypto_init() == 0 );
11071110
TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
11081111
mbedtls_cipher_info_from_type( cipher_id ), 0 ) );
11091112
}

tests/suites/test_suite_pk.function

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ void pk_psa_utils( )
124124
size_t len;
125125
mbedtls_pk_debug_item dbg;
126126

127+
TEST_ASSERT( psa_crypto_init() == 0 );
128+
127129
mbedtls_pk_init( &pk );
128130
mbedtls_pk_init( &pk2 );
129131

tests/suites/test_suite_x509parse.function

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ void x509_verify( char *crt_file, char *ca_file, char *crl_file,
334334
char * cn_name = NULL;
335335
const mbedtls_x509_crt_profile *profile;
336336

337+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
338+
TEST_ASSERT( psa_crypto_init() == 0 );
339+
#endif
340+
337341
mbedtls_x509_crt_init( &crt );
338342
mbedtls_x509_crt_init( &ca );
339343
mbedtls_x509_crl_init( &crl );
@@ -387,6 +391,10 @@ void x509_verify_callback( char *crt_file, char *ca_file, char *name,
387391
uint32_t flags = 0;
388392
verify_print_context vrfy_ctx;
389393

394+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
395+
TEST_ASSERT( psa_crypto_init() == 0 );
396+
#endif
397+
390398
mbedtls_x509_crt_init( &crt );
391399
mbedtls_x509_crt_init( &ca );
392400
verify_print_init( &vrfy_ctx );
@@ -605,6 +613,10 @@ void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
605613
uint32_t flags;
606614
mbedtls_x509_crt trusted, chain;
607615

616+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
617+
TEST_ASSERT( psa_crypto_init() == 0 );
618+
#endif
619+
608620
/*
609621
* We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
610622
* with NN.crt signed by NN-1.crt
@@ -646,6 +658,10 @@ void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
646658
mbedtls_x509_crt trusted, chain;
647659
const mbedtls_x509_crt_profile *profile = NULL;
648660

661+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
662+
TEST_ASSERT( psa_crypto_init() == 0 );
663+
#endif
664+
649665
mbedtls_x509_crt_init( &chain );
650666
mbedtls_x509_crt_init( &trusted );
651667

0 commit comments

Comments
 (0)