Skip to content

Commit ff9d2bc

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 ed50cec commit ff9d2bc

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

library/psa_crypto.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,13 @@ 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+
{
1386+
return( PSA_ERROR_BAD_STATE );
1387+
}
1388+
13831389
switch( alg )
13841390
{
13851391
#if defined(MBEDTLS_MD2_C)
@@ -1998,6 +2004,12 @@ static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
19982004
unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
19992005
psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
20002006

2007+
/* A context must be freshly initialized before it can be set up. */
2008+
if (operation->alg != 0)
2009+
{
2010+
return( PSA_ERROR_BAD_STATE );
2011+
}
2012+
20012013
status = psa_mac_init( operation, full_length_alg );
20022014
if( status != PSA_SUCCESS )
20032015
return( status );
@@ -2909,6 +2921,12 @@ static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
29092921
PSA_KEY_USAGE_ENCRYPT :
29102922
PSA_KEY_USAGE_DECRYPT );
29112923

2924+
/* A context must be freshly initialized before it can be set up. */
2925+
if (operation->alg != 0)
2926+
{
2927+
return( PSA_ERROR_BAD_STATE );
2928+
}
2929+
29122930
status = psa_cipher_init( operation, alg );
29132931
if( status != PSA_SUCCESS )
29142932
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 ) );
@@ -2607,6 +2621,18 @@ void cipher_bad_order( )
26072621
key, sizeof(key) ) );
26082622

26092623

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

0 commit comments

Comments
 (0)