Skip to content

Commit ace8e02

Browse files
committed
psa: Disallow repeated setup
Calling psa_*_setup() twice on a MAC, cipher, or hash context should result in a PSA_ERROR_BAD_STATE error because the operation has already been set up. Fixes #10
1 parent 6a8b986 commit ace8e02

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

library/psa_crypto.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,12 @@ psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
13791379
psa_algorithm_t alg )
13801380
{
13811381
int ret;
1382-
operation->alg = 0;
1382+
1383+
/* A context must be freshly initialized before it can be set up. */
1384+
if (operation->alg != 0) {
1385+
return( PSA_ERROR_BAD_STATE );
1386+
}
1387+
13831388
switch( alg )
13841389
{
13851390
#if defined(MBEDTLS_MD2_C)
@@ -2000,6 +2005,11 @@ static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
20002005
unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
20012006
psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
20022007

2008+
/* A context must be freshly initialized before it can be set up. */
2009+
if (operation->alg != 0) {
2010+
return( PSA_ERROR_BAD_STATE );
2011+
}
2012+
20032013
status = psa_mac_init( operation, full_length_alg );
20042014
if( status != PSA_SUCCESS )
20052015
return( status );
@@ -2913,6 +2923,11 @@ static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
29132923
PSA_KEY_USAGE_ENCRYPT :
29142924
PSA_KEY_USAGE_DECRYPT );
29152925

2926+
/* A context must be freshly initialized before it can be set up. */
2927+
if (operation->alg != 0) {
2928+
return( PSA_ERROR_BAD_STATE );
2929+
}
2930+
29162931
status = psa_cipher_init( operation, alg );
29172932
if( status != PSA_SUCCESS )
29182933
return( status );

tests/suites/test_suite_psa_crypto.function

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,12 @@ void hash_bad_order( )
20122012

20132013
PSA_ASSERT( psa_crypto_init( ) );
20142014

2015+
/* Call setup twice in a row. */
2016+
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2017+
TEST_EQUAL( psa_hash_setup( &operation, alg ),
2018+
PSA_ERROR_BAD_STATE );
2019+
PSA_ASSERT( psa_hash_abort( &operation ) );
2020+
20152021
/* Call update without calling setup beforehand. */
20162022
TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
20172023
PSA_ERROR_BAD_STATE );
@@ -2336,6 +2342,14 @@ void mac_bad_order( )
23362342
PSA_ERROR_BAD_STATE );
23372343
PSA_ASSERT( psa_mac_abort( &operation ) );
23382344

2345+
/* Call setup twice in a row. */
2346+
PSA_ASSERT( psa_mac_sign_setup( &operation,
2347+
handle, alg ) );
2348+
TEST_EQUAL( psa_mac_sign_setup( &operation,
2349+
handle, alg ),
2350+
PSA_ERROR_BAD_STATE );
2351+
PSA_ASSERT( psa_mac_abort( &operation ) );
2352+
23392353
/* Call update after sign finish. */
23402354
PSA_ASSERT( psa_mac_sign_setup( &operation,
23412355
handle, alg ) );
@@ -2609,6 +2623,18 @@ void cipher_bad_order( )
26092623
key, sizeof(key) ) );
26102624

26112625

2626+
/* Call encrypt setup twice in a row. */
2627+
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2628+
TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2629+
PSA_ERROR_BAD_STATE );
2630+
PSA_ASSERT( psa_cipher_abort( &operation ) );
2631+
2632+
/* Call decrypt setup twice in a row. */
2633+
PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2634+
TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2635+
PSA_ERROR_BAD_STATE );
2636+
PSA_ASSERT( psa_cipher_abort( &operation ) );
2637+
26122638
/* Generate an IV without calling setup beforehand. */
26132639
TEST_EQUAL( psa_cipher_generate_iv( &operation,
26142640
buffer, sizeof( buffer ),

0 commit comments

Comments
 (0)