Skip to content

Commit 0b2f1f2

Browse files
committed
psa: Disallow use of invalid hash contexts
If a hash context has not been set up, fail with PSA_ERROR_BAD_STATE as documented in crypto.h and the PSA Crypto specification.
1 parent 748949f commit 0b2f1f2

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

library/psa_crypto.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,8 +1502,8 @@ psa_status_t psa_hash_update( psa_hash_operation_t *operation,
15021502
break;
15031503
#endif
15041504
default:
1505-
ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1506-
break;
1505+
psa_hash_abort( operation );
1506+
return( PSA_ERROR_BAD_STATE );
15071507
}
15081508

15091509
if( ret != 0 )
@@ -1575,8 +1575,8 @@ psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
15751575
break;
15761576
#endif
15771577
default:
1578-
ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1579-
break;
1578+
status = PSA_ERROR_BAD_STATE;
1579+
goto exit;
15801580
}
15811581
status = mbedtls_to_psa_error( ret );
15821582

tests/suites/test_suite_psa_crypto.function

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,6 +1950,7 @@ exit:
19501950
/* BEGIN_CASE */
19511951
void hash_operation_init( )
19521952
{
1953+
const uint8_t input[1];
19531954
/* Test each valid way of initializing the object, except for `= {0}`, as
19541955
* Clang 5 complains when `-Wmissing-field-initializers` is used, even
19551956
* though it's OK by the C standard. We could test for this, but we'd need
@@ -1960,6 +1961,14 @@ void hash_operation_init( )
19601961

19611962
memset( &zero, 0, sizeof( zero ) );
19621963

1964+
/* A default hash operation should not be usable. */
1965+
TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1966+
PSA_ERROR_BAD_STATE );
1967+
TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1968+
PSA_ERROR_BAD_STATE );
1969+
TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1970+
PSA_ERROR_BAD_STATE );
1971+
19631972
/* A default hash operation should be abortable without error. */
19641973
PSA_ASSERT( psa_hash_abort( &func ) );
19651974
PSA_ASSERT( psa_hash_abort( &init ) );
@@ -2004,18 +2013,18 @@ void hash_bad_order( )
20042013
/* psa_hash_update without calling psa_hash_setup beforehand */
20052014
memset( &operation, 0, sizeof( operation ) );
20062015
TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2007-
PSA_ERROR_INVALID_ARGUMENT );
2016+
PSA_ERROR_BAD_STATE );
20082017

20092018
/* psa_hash_verify without calling psa_hash_setup beforehand */
20102019
memset( &operation, 0, sizeof( operation ) );
20112020
TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
2012-
PSA_ERROR_INVALID_ARGUMENT );
2021+
PSA_ERROR_BAD_STATE );
20132022

20142023
/* psa_hash_finish without calling psa_hash_setup beforehand */
20152024
memset( &operation, 0, sizeof( operation ) );
20162025
TEST_EQUAL( psa_hash_finish( &operation,
20172026
hash, sizeof( hash ), &hash_len ),
2018-
PSA_ERROR_INVALID_ARGUMENT );
2027+
PSA_ERROR_BAD_STATE );
20192028

20202029
exit:
20212030
mbedtls_psa_crypto_free( );

0 commit comments

Comments
 (0)