@@ -1373,7 +1373,13 @@ psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
13731373 psa_algorithm_t alg )
13741374{
13751375 int ret ;
1376- operation -> alg = 0 ;
1376+
1377+ /* A context must be freshly initialized before it can be set up. */
1378+ if ( operation -> alg != 0 )
1379+ {
1380+ return ( PSA_ERROR_BAD_STATE );
1381+ }
1382+
13771383 switch ( alg )
13781384 {
13791385#if defined(MBEDTLS_MD2_C )
@@ -1496,8 +1502,7 @@ psa_status_t psa_hash_update( psa_hash_operation_t *operation,
14961502 break ;
14971503#endif
14981504 default :
1499- ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA ;
1500- break ;
1505+ return ( PSA_ERROR_BAD_STATE );
15011506 }
15021507
15031508 if ( ret != 0 )
@@ -1569,8 +1574,7 @@ psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
15691574 break ;
15701575#endif
15711576 default :
1572- ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA ;
1573- break ;
1577+ return ( PSA_ERROR_BAD_STATE );
15741578 }
15751579 status = mbedtls_to_psa_error ( ret );
15761580
@@ -1994,6 +1998,12 @@ static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
19941998 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH ( alg );
19951999 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC ( alg );
19962000
2001+ /* A context must be freshly initialized before it can be set up. */
2002+ if ( operation -> alg != 0 )
2003+ {
2004+ return ( PSA_ERROR_BAD_STATE );
2005+ }
2006+
19972007 status = psa_mac_init ( operation , full_length_alg );
19982008 if ( status != PSA_SUCCESS )
19992009 return ( status );
@@ -2112,9 +2122,9 @@ psa_status_t psa_mac_update( psa_mac_operation_t *operation,
21122122{
21132123 psa_status_t status = PSA_ERROR_BAD_STATE ;
21142124 if ( ! operation -> key_set )
2115- goto cleanup ;
2125+ return ( PSA_ERROR_BAD_STATE ) ;
21162126 if ( operation -> iv_required && ! operation -> iv_set )
2117- goto cleanup ;
2127+ return ( PSA_ERROR_BAD_STATE ) ;
21182128 operation -> has_input = 1 ;
21192129
21202130#if defined(MBEDTLS_CMAC_C )
@@ -2137,10 +2147,9 @@ psa_status_t psa_mac_update( psa_mac_operation_t *operation,
21372147 {
21382148 /* This shouldn't happen if `operation` was initialized by
21392149 * a setup function. */
2140- status = PSA_ERROR_BAD_STATE ;
2150+ return ( PSA_ERROR_BAD_STATE ) ;
21412151 }
21422152
2143- cleanup :
21442153 if ( status != PSA_SUCCESS )
21452154 psa_mac_abort ( operation );
21462155 return ( status );
@@ -2232,6 +2241,11 @@ psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
22322241{
22332242 psa_status_t status ;
22342243
2244+ if ( operation -> alg == 0 )
2245+ {
2246+ return ( PSA_ERROR_BAD_STATE );
2247+ }
2248+
22352249 /* Fill the output buffer with something that isn't a valid mac
22362250 * (barring an attack on the mac and deliberately-crafted input),
22372251 * in case the caller doesn't check the return status properly. */
@@ -2243,13 +2257,11 @@ psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
22432257
22442258 if ( ! operation -> is_sign )
22452259 {
2246- status = PSA_ERROR_BAD_STATE ;
2247- goto cleanup ;
2260+ return ( PSA_ERROR_BAD_STATE );
22482261 }
22492262
22502263 status = psa_mac_finish_internal ( operation , mac , mac_size );
22512264
2252- cleanup :
22532265 if ( status == PSA_SUCCESS )
22542266 {
22552267 status = psa_mac_abort ( operation );
@@ -2270,10 +2282,14 @@ psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
22702282 uint8_t actual_mac [PSA_MAC_MAX_SIZE ];
22712283 psa_status_t status ;
22722284
2285+ if ( operation -> alg == 0 )
2286+ {
2287+ return ( PSA_ERROR_BAD_STATE );
2288+ }
2289+
22732290 if ( operation -> is_sign )
22742291 {
2275- status = PSA_ERROR_BAD_STATE ;
2276- goto cleanup ;
2292+ return ( PSA_ERROR_BAD_STATE );
22772293 }
22782294 if ( operation -> mac_size != mac_length )
22792295 {
@@ -2895,6 +2911,12 @@ static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
28952911 PSA_KEY_USAGE_ENCRYPT :
28962912 PSA_KEY_USAGE_DECRYPT );
28972913
2914+ /* A context must be freshly initialized before it can be set up. */
2915+ if ( operation -> alg != 0 )
2916+ {
2917+ return ( PSA_ERROR_BAD_STATE );
2918+ }
2919+
28982920 status = psa_cipher_init ( operation , alg );
28992921 if ( status != PSA_SUCCESS )
29002922 return ( status );
@@ -2996,8 +3018,7 @@ psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
29963018 int ret ;
29973019 if ( operation -> iv_set || ! operation -> iv_required )
29983020 {
2999- status = PSA_ERROR_BAD_STATE ;
3000- goto exit ;
3021+ return ( PSA_ERROR_BAD_STATE );
30013022 }
30023023 if ( iv_size < operation -> iv_size )
30033024 {
@@ -3029,8 +3050,7 @@ psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
30293050 int ret ;
30303051 if ( operation -> iv_set || ! operation -> iv_required )
30313052 {
3032- status = PSA_ERROR_BAD_STATE ;
3033- goto exit ;
3053+ return ( PSA_ERROR_BAD_STATE );
30343054 }
30353055 if ( iv_length != operation -> iv_size )
30363056 {
@@ -3057,6 +3077,12 @@ psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
30573077 psa_status_t status ;
30583078 int ret ;
30593079 size_t expected_output_size ;
3080+
3081+ if ( operation -> alg == 0 )
3082+ {
3083+ return ( PSA_ERROR_BAD_STATE );
3084+ }
3085+
30603086 if ( ! PSA_ALG_IS_STREAM_CIPHER ( operation -> alg ) )
30613087 {
30623088 /* Take the unprocessed partial block left over from previous
@@ -3098,13 +3124,11 @@ psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
30983124
30993125 if ( ! operation -> key_set )
31003126 {
3101- status = PSA_ERROR_BAD_STATE ;
3102- goto error ;
3127+ return ( PSA_ERROR_BAD_STATE );
31033128 }
31043129 if ( operation -> iv_required && ! operation -> iv_set )
31053130 {
3106- status = PSA_ERROR_BAD_STATE ;
3107- goto error ;
3131+ return ( PSA_ERROR_BAD_STATE );
31083132 }
31093133
31103134 if ( operation -> ctx .cipher .operation == MBEDTLS_ENCRYPT &&
0 commit comments