@@ -1373,7 +1373,13 @@ psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
1373
1373
psa_algorithm_t alg )
1374
1374
{
1375
1375
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
+
1377
1383
switch ( alg )
1378
1384
{
1379
1385
#if defined(MBEDTLS_MD2_C )
@@ -1496,8 +1502,7 @@ psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1496
1502
break ;
1497
1503
#endif
1498
1504
default :
1499
- ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA ;
1500
- break ;
1505
+ return ( PSA_ERROR_BAD_STATE );
1501
1506
}
1502
1507
1503
1508
if ( ret != 0 )
@@ -1569,8 +1574,7 @@ psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1569
1574
break ;
1570
1575
#endif
1571
1576
default :
1572
- ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA ;
1573
- break ;
1577
+ return ( PSA_ERROR_BAD_STATE );
1574
1578
}
1575
1579
status = mbedtls_to_psa_error ( ret );
1576
1580
@@ -1994,6 +1998,12 @@ static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1994
1998
unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH ( alg );
1995
1999
psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC ( alg );
1996
2000
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
+
1997
2007
status = psa_mac_init ( operation , full_length_alg );
1998
2008
if ( status != PSA_SUCCESS )
1999
2009
return ( status );
@@ -2112,9 +2122,9 @@ psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2112
2122
{
2113
2123
psa_status_t status = PSA_ERROR_BAD_STATE ;
2114
2124
if ( ! operation -> key_set )
2115
- goto cleanup ;
2125
+ return ( PSA_ERROR_BAD_STATE ) ;
2116
2126
if ( operation -> iv_required && ! operation -> iv_set )
2117
- goto cleanup ;
2127
+ return ( PSA_ERROR_BAD_STATE ) ;
2118
2128
operation -> has_input = 1 ;
2119
2129
2120
2130
#if defined(MBEDTLS_CMAC_C )
@@ -2137,10 +2147,9 @@ psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2137
2147
{
2138
2148
/* This shouldn't happen if `operation` was initialized by
2139
2149
* a setup function. */
2140
- status = PSA_ERROR_BAD_STATE ;
2150
+ return ( PSA_ERROR_BAD_STATE ) ;
2141
2151
}
2142
2152
2143
- cleanup :
2144
2153
if ( status != PSA_SUCCESS )
2145
2154
psa_mac_abort ( operation );
2146
2155
return ( status );
@@ -2232,6 +2241,11 @@ psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2232
2241
{
2233
2242
psa_status_t status ;
2234
2243
2244
+ if ( operation -> alg == 0 )
2245
+ {
2246
+ return ( PSA_ERROR_BAD_STATE );
2247
+ }
2248
+
2235
2249
/* Fill the output buffer with something that isn't a valid mac
2236
2250
* (barring an attack on the mac and deliberately-crafted input),
2237
2251
* 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,
2243
2257
2244
2258
if ( ! operation -> is_sign )
2245
2259
{
2246
- status = PSA_ERROR_BAD_STATE ;
2247
- goto cleanup ;
2260
+ return ( PSA_ERROR_BAD_STATE );
2248
2261
}
2249
2262
2250
2263
status = psa_mac_finish_internal ( operation , mac , mac_size );
2251
2264
2252
- cleanup :
2253
2265
if ( status == PSA_SUCCESS )
2254
2266
{
2255
2267
status = psa_mac_abort ( operation );
@@ -2270,10 +2282,14 @@ psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2270
2282
uint8_t actual_mac [PSA_MAC_MAX_SIZE ];
2271
2283
psa_status_t status ;
2272
2284
2285
+ if ( operation -> alg == 0 )
2286
+ {
2287
+ return ( PSA_ERROR_BAD_STATE );
2288
+ }
2289
+
2273
2290
if ( operation -> is_sign )
2274
2291
{
2275
- status = PSA_ERROR_BAD_STATE ;
2276
- goto cleanup ;
2292
+ return ( PSA_ERROR_BAD_STATE );
2277
2293
}
2278
2294
if ( operation -> mac_size != mac_length )
2279
2295
{
@@ -2895,6 +2911,12 @@ static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2895
2911
PSA_KEY_USAGE_ENCRYPT :
2896
2912
PSA_KEY_USAGE_DECRYPT );
2897
2913
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
+
2898
2920
status = psa_cipher_init ( operation , alg );
2899
2921
if ( status != PSA_SUCCESS )
2900
2922
return ( status );
@@ -2996,8 +3018,7 @@ psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2996
3018
int ret ;
2997
3019
if ( operation -> iv_set || ! operation -> iv_required )
2998
3020
{
2999
- status = PSA_ERROR_BAD_STATE ;
3000
- goto exit ;
3021
+ return ( PSA_ERROR_BAD_STATE );
3001
3022
}
3002
3023
if ( iv_size < operation -> iv_size )
3003
3024
{
@@ -3029,8 +3050,7 @@ psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3029
3050
int ret ;
3030
3051
if ( operation -> iv_set || ! operation -> iv_required )
3031
3052
{
3032
- status = PSA_ERROR_BAD_STATE ;
3033
- goto exit ;
3053
+ return ( PSA_ERROR_BAD_STATE );
3034
3054
}
3035
3055
if ( iv_length != operation -> iv_size )
3036
3056
{
@@ -3057,6 +3077,12 @@ psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3057
3077
psa_status_t status ;
3058
3078
int ret ;
3059
3079
size_t expected_output_size ;
3080
+
3081
+ if ( operation -> alg == 0 )
3082
+ {
3083
+ return ( PSA_ERROR_BAD_STATE );
3084
+ }
3085
+
3060
3086
if ( ! PSA_ALG_IS_STREAM_CIPHER ( operation -> alg ) )
3061
3087
{
3062
3088
/* Take the unprocessed partial block left over from previous
@@ -3098,13 +3124,11 @@ psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3098
3124
3099
3125
if ( ! operation -> key_set )
3100
3126
{
3101
- status = PSA_ERROR_BAD_STATE ;
3102
- goto error ;
3127
+ return ( PSA_ERROR_BAD_STATE );
3103
3128
}
3104
3129
if ( operation -> iv_required && ! operation -> iv_set )
3105
3130
{
3106
- status = PSA_ERROR_BAD_STATE ;
3107
- goto error ;
3131
+ return ( PSA_ERROR_BAD_STATE );
3108
3132
}
3109
3133
3110
3134
if ( operation -> ctx .cipher .operation == MBEDTLS_ENCRYPT &&
0 commit comments