@@ -1999,32 +1999,79 @@ exit:
1999
1999
/* BEGIN_CASE */
2000
2000
void hash_bad_order( )
2001
2001
{
2002
+ psa_algorithm_t alg = PSA_ALG_SHA_256;
2002
2003
unsigned char input[] = "";
2003
2004
/* SHA-256 hash of an empty string */
2004
- unsigned char hash [] = {
2005
+ const unsigned char valid_hash [] = {
2005
2006
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2006
2007
0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2007
2008
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
2009
+ unsigned char hash[sizeof(valid_hash)] = { 0 };
2008
2010
size_t hash_len;
2009
2011
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2010
2012
2011
2013
PSA_ASSERT( psa_crypto_init( ) );
2012
2014
2013
- /* psa_hash_update without calling psa_hash_setup beforehand */
2014
- memset( &operation, 0, sizeof( operation ) );
2015
+ /* Call update without calling setup beforehand. */
2015
2016
TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2016
2017
PSA_ERROR_BAD_STATE );
2018
+ PSA_ASSERT( psa_hash_abort( &operation ) );
2017
2019
2018
- /* psa_hash_verify without calling psa_hash_setup beforehand */
2019
- memset( &operation, 0, sizeof( operation ) );
2020
- TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
2020
+ /* Call update after finish. */
2021
+ PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2022
+ PSA_ASSERT( psa_hash_finish( &operation,
2023
+ hash, sizeof( hash ), &hash_len ) );
2024
+ TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2025
+ PSA_ERROR_BAD_STATE );
2026
+ PSA_ASSERT( psa_hash_abort( &operation ) );
2027
+
2028
+ /* Call verify without calling setup beforehand. */
2029
+ TEST_EQUAL( psa_hash_verify( &operation,
2030
+ valid_hash, sizeof( valid_hash ) ),
2031
+ PSA_ERROR_BAD_STATE );
2032
+ PSA_ASSERT( psa_hash_abort( &operation ) );
2033
+
2034
+ /* Call verify after finish. */
2035
+ PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2036
+ PSA_ASSERT( psa_hash_finish( &operation,
2037
+ hash, sizeof( hash ), &hash_len ) );
2038
+ TEST_EQUAL( psa_hash_verify( &operation,
2039
+ valid_hash, sizeof( valid_hash ) ),
2040
+ PSA_ERROR_BAD_STATE );
2041
+ PSA_ASSERT( psa_hash_abort( &operation ) );
2042
+
2043
+ /* Call verify twice in a row. */
2044
+ PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2045
+ PSA_ASSERT( psa_hash_verify( &operation,
2046
+ valid_hash, sizeof( valid_hash ) ) );
2047
+ TEST_EQUAL( psa_hash_verify( &operation,
2048
+ valid_hash, sizeof( valid_hash ) ),
2049
+ PSA_ERROR_BAD_STATE );
2050
+ PSA_ASSERT( psa_hash_abort( &operation ) );
2051
+
2052
+ /* Call finish without calling setup beforehand. */
2053
+ TEST_EQUAL( psa_hash_finish( &operation,
2054
+ hash, sizeof( hash ), &hash_len ),
2021
2055
PSA_ERROR_BAD_STATE );
2056
+ PSA_ASSERT( psa_hash_abort( &operation ) );
2022
2057
2023
- /* psa_hash_finish without calling psa_hash_setup beforehand */
2024
- memset( &operation, 0, sizeof( operation ) );
2058
+ /* Call finish twice in a row. */
2059
+ PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2060
+ PSA_ASSERT( psa_hash_finish( &operation,
2061
+ hash, sizeof( hash ), &hash_len ) );
2062
+ TEST_EQUAL( psa_hash_finish( &operation,
2063
+ hash, sizeof( hash ), &hash_len ),
2064
+ PSA_ERROR_BAD_STATE );
2065
+ PSA_ASSERT( psa_hash_abort( &operation ) );
2066
+
2067
+ /* Call finish after calling verify. */
2068
+ PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2069
+ PSA_ASSERT( psa_hash_verify( &operation,
2070
+ valid_hash, sizeof( valid_hash ) ) );
2025
2071
TEST_EQUAL( psa_hash_finish( &operation,
2026
2072
hash, sizeof( hash ), &hash_len ),
2027
2073
PSA_ERROR_BAD_STATE );
2074
+ PSA_ASSERT( psa_hash_abort( &operation ) );
2028
2075
2029
2076
exit:
2030
2077
mbedtls_psa_crypto_free( );
0 commit comments