Skip to content

Commit bf61ca7

Browse files
authored
Merge pull request #58 from Patater/disallow-invalid-context
Disallow use of invalid contexts
2 parents c37fff9 + e236c2a commit bf61ca7

File tree

8 files changed

+446
-41
lines changed

8 files changed

+446
-41
lines changed

library/cipher.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
12361236
(mbedtls_cipher_context_psa *) ctx->cipher_ctx;
12371237

12381238
psa_status_t status;
1239-
psa_cipher_operation_t cipher_op;
1239+
psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
12401240
size_t part_len;
12411241

12421242
if( ctx->operation == MBEDTLS_DECRYPT )

library/psa_crypto.c

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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 &&

library/ssl_tls.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6529,7 +6529,7 @@ static void ssl_calc_finished_tls_sha256(
65296529
unsigned char padbuf[32];
65306530
#if defined(MBEDTLS_USE_PSA_CRYPTO)
65316531
size_t hash_size;
6532-
psa_hash_operation_t sha256_psa;
6532+
psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
65336533
psa_status_t status;
65346534
#else
65356535
mbedtls_sha256_context sha256;
@@ -6605,7 +6605,7 @@ static void ssl_calc_finished_tls_sha384(
66056605
unsigned char padbuf[48];
66066606
#if defined(MBEDTLS_USE_PSA_CRYPTO)
66076607
size_t hash_size;
6608-
psa_hash_operation_t sha384_psa;
6608+
psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
66096609
psa_status_t status;
66106610
#else
66116611
mbedtls_sha512_context sha512;
@@ -10203,7 +10203,7 @@ int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
1020310203
mbedtls_md_type_t md_alg )
1020410204
{
1020510205
psa_status_t status;
10206-
psa_hash_operation_t hash_operation;
10206+
psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
1020710207
psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
1020810208

1020910209
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );

library/x509_crt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,7 @@ static int x509_crt_check_signature( const mbedtls_x509_crt *child,
19081908
if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
19091909
return( -1 );
19101910
#else
1911-
psa_hash_operation_t hash_operation;
1911+
psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
19121912
psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
19131913

19141914
if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )

library/x509write_csr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, s
142142
size_t len = 0;
143143
mbedtls_pk_type_t pk_alg;
144144
#if defined(MBEDTLS_USE_PSA_CRYPTO)
145-
psa_hash_operation_t hash_operation;
145+
psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
146146
size_t hash_len;
147147
psa_algorithm_t hash_alg = mbedtls_psa_translate_md( ctx->md_alg );
148148
#endif /* MBEDTLS_USE_PSA_CRYPTO */

programs/psa/crypto_examples.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static psa_status_t cipher_encrypt( psa_key_handle_t key_handle,
109109
size_t *output_len )
110110
{
111111
psa_status_t status;
112-
psa_cipher_operation_t operation;
112+
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
113113
size_t iv_len = 0;
114114

115115
memset( &operation, 0, sizeof( operation ) );
@@ -140,7 +140,7 @@ static psa_status_t cipher_decrypt( psa_key_handle_t key_handle,
140140
size_t *output_len )
141141
{
142142
psa_status_t status;
143-
psa_cipher_operation_t operation;
143+
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
144144

145145
memset( &operation, 0, sizeof( operation ) );
146146
status = psa_cipher_decrypt_setup( &operation, key_handle, alg );

tests/suites/test_suite_psa_crypto.data

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ depends_on:MBEDTLS_MD_C:MBEDTLS_SHA256_C
655655
hash_setup:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_ERROR_INVALID_ARGUMENT
656656

657657
PSA hash: bad order function calls
658+
depends_on:MBEDTLS_MD_C:MBEDTLS_SHA256_C
658659
hash_bad_order:
659660

660661
PSA hash verify: bad arguments
@@ -705,6 +706,10 @@ depends_on:MBEDTLS_CMAC_C
705706
# Either INVALID_ARGUMENT or NOT_SUPPORTED would be reasonable here
706707
mac_setup:PSA_KEY_TYPE_HMAC:"000102030405060708090a0b0c0d0e0f":PSA_ALG_CMAC:PSA_ERROR_NOT_SUPPORTED
707708

709+
PSA MAC: bad order function calls
710+
depends_on:MBEDTLS_MD_C:MBEDTLS_SHA256_C
711+
mac_bad_order:
712+
708713
PSA MAC sign: RFC4231 Test case 1 - HMAC-SHA-224
709714
depends_on:MBEDTLS_MD_C:MBEDTLS_SHA256_C
710715
mac_sign:PSA_KEY_TYPE_HMAC:"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b":PSA_ALG_HMAC(PSA_ALG_SHA_224):"4869205468657265":"896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22"
@@ -922,6 +927,10 @@ depends_on:MBEDTLS_ARC4_C:MBEDTLS_CIPHER_MODE_CTR
922927
# Either INVALID_ARGUMENT or NOT_SUPPORTED would be reasonable here
923928
cipher_setup:PSA_KEY_TYPE_ARC4:"000102030405060708090a0b0c0d0e0f":PSA_ALG_CTR:PSA_ERROR_NOT_SUPPORTED
924929

930+
PSA cipher: bad order function calls
931+
depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC
932+
cipher_bad_order:
933+
925934
PSA symmetric encrypt: AES-CBC-nopad, 16 bytes, good
926935
depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC
927936
cipher_encrypt:PSA_ALG_CBC_NO_PADDING:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b":PSA_SUCCESS

0 commit comments

Comments
 (0)