Skip to content

Commit 3e8b6b0

Browse files
committed
psa: Extend hash bad order test
Extend hash bad order test in line with the new bad order tests for MAC and cipher, covering more cases and making comments and test layout consistent. Ensure that when doing hash operations out of order, PSA_ERROR_BAD_STATE is returned as documented in crypto.h and the PSA Crypto specification.
1 parent 283d514 commit 3e8b6b0

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

tests/suites/test_suite_psa_crypto.function

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,29 +1999,76 @@ exit:
19991999
/* BEGIN_CASE */
20002000
void hash_bad_order( )
20012001
{
2002+
psa_algorithm_t alg = PSA_ALG_SHA_256;
20022003
unsigned char input[] = "";
20032004
/* SHA-256 hash of an empty string */
2004-
unsigned char hash[] = {
2005+
const unsigned char valid_hash[] = {
20052006
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
20062007
0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
20072008
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
2009+
unsigned char hash[sizeof(valid_hash)] = { 0 };
20082010
size_t hash_len;
20092011
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
20102012

20112013
PSA_ASSERT( psa_crypto_init( ) );
20122014

2013-
/* psa_hash_update without calling psa_hash_setup beforehand */
2015+
/* Call update without calling setup beforehand. */
20142016
memset( &operation, 0, sizeof( operation ) );
20152017
TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
20162018
PSA_ERROR_BAD_STATE );
20172019

2018-
/* psa_hash_verify without calling psa_hash_setup beforehand */
2020+
/* Call update after finish. */
20192021
memset( &operation, 0, sizeof( operation ) );
2020-
TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
2022+
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2023+
PSA_ASSERT( psa_hash_finish( &operation,
2024+
hash, sizeof( hash ), &hash_len ) );
2025+
TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2026+
PSA_ERROR_BAD_STATE );
2027+
2028+
/* Call verify without calling setup beforehand. */
2029+
memset( &operation, 0, sizeof( operation ) );
2030+
TEST_EQUAL( psa_hash_verify( &operation,
2031+
valid_hash, sizeof( valid_hash ) ),
20212032
PSA_ERROR_BAD_STATE );
20222033

2023-
/* psa_hash_finish without calling psa_hash_setup beforehand */
2034+
/* Call verify after finish. */
20242035
memset( &operation, 0, sizeof( operation ) );
2036+
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2037+
PSA_ASSERT( psa_hash_finish( &operation,
2038+
hash, sizeof( hash ), &hash_len ) );
2039+
TEST_EQUAL( psa_hash_verify( &operation,
2040+
valid_hash, sizeof( valid_hash ) ),
2041+
PSA_ERROR_BAD_STATE );
2042+
2043+
/* Call verify twice in a row. */
2044+
memset( &operation, 0, sizeof( operation ) );
2045+
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2046+
PSA_ASSERT( psa_hash_verify( &operation,
2047+
valid_hash, sizeof( valid_hash ) ) );
2048+
TEST_EQUAL( psa_hash_verify( &operation,
2049+
valid_hash, sizeof( valid_hash ) ),
2050+
PSA_ERROR_BAD_STATE );
2051+
2052+
/* Call finish without calling setup beforehand. */
2053+
memset( &operation, 0, sizeof( operation ) );
2054+
TEST_EQUAL( psa_hash_finish( &operation,
2055+
hash, sizeof( hash ), &hash_len ),
2056+
PSA_ERROR_BAD_STATE );
2057+
2058+
/* Call finish twice in a row. */
2059+
memset( &operation, 0, sizeof( operation ) );
2060+
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2061+
PSA_ASSERT( psa_hash_finish( &operation,
2062+
hash, sizeof( hash ), &hash_len ) );
2063+
TEST_EQUAL( psa_hash_finish( &operation,
2064+
hash, sizeof( hash ), &hash_len ),
2065+
PSA_ERROR_BAD_STATE );
2066+
2067+
/* Call finish after calling verify. */
2068+
memset( &operation, 0, sizeof( operation ) );
2069+
PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2070+
PSA_ASSERT( psa_hash_verify( &operation,
2071+
valid_hash, sizeof( valid_hash ) ) );
20252072
TEST_EQUAL( psa_hash_finish( &operation,
20262073
hash, sizeof( hash ), &hash_len ),
20272074
PSA_ERROR_BAD_STATE );

0 commit comments

Comments
 (0)