@@ -1999,29 +1999,76 @@ 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 */
2015
+ /* Call update without calling setup beforehand. */
2014
2016
memset( &operation, 0, sizeof( operation ) );
2015
2017
TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2016
2018
PSA_ERROR_BAD_STATE );
2017
2019
2018
- /* psa_hash_verify without calling psa_hash_setup beforehand */
2020
+ /* Call update after finish. */
2019
2021
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 ) ),
2021
2032
PSA_ERROR_BAD_STATE );
2022
2033
2023
- /* psa_hash_finish without calling psa_hash_setup beforehand */
2034
+ /* Call verify after finish. */
2024
2035
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 ) ) );
2025
2072
TEST_EQUAL( psa_hash_finish( &operation,
2026
2073
hash, sizeof( hash ), &hash_len ),
2027
2074
PSA_ERROR_BAD_STATE );
0 commit comments