Skip to content

Commit 95bbbf7

Browse files
committed
psa: Disallow use of invalid cipher contexts
Ensure that when doing cipher operations out of order, PSA_ERROR_BAD_STATE is returned as documented in crypto.h and the PSA Crypto specification.
1 parent 4d18f94 commit 95bbbf7

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

library/psa_crypto.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3075,6 +3075,13 @@ psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
30753075
psa_status_t status;
30763076
int ret;
30773077
size_t expected_output_size;
3078+
3079+
if( operation->alg == 0)
3080+
{
3081+
status = PSA_ERROR_BAD_STATE;
3082+
goto exit;
3083+
}
3084+
30783085
if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
30793086
{
30803087
/* Take the unprocessed partial block left over from previous

tests/suites/test_suite_psa_crypto.data

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,9 @@ depends_on:MBEDTLS_ARC4_C:MBEDTLS_CIPHER_MODE_CTR
925925
# Either INVALID_ARGUMENT or NOT_SUPPORTED would be reasonable here
926926
cipher_setup:PSA_KEY_TYPE_ARC4:"000102030405060708090a0b0c0d0e0f":PSA_ALG_CTR:PSA_ERROR_NOT_SUPPORTED
927927

928+
PSA cipher: bad order function calls
929+
cipher_bad_order:
930+
928931
PSA symmetric encrypt: AES-CBC-nopad, 16 bytes, good
929932
depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC
930933
cipher_encrypt:PSA_ALG_CBC_NO_PADDING:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b":PSA_SUCCESS

tests/suites/test_suite_psa_crypto.function

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,9 @@ exit:
24522452
/* BEGIN_CASE */
24532453
void cipher_operation_init( )
24542454
{
2455+
const uint8_t input[1];
2456+
unsigned char output[1];
2457+
size_t output_length;
24552458
/* Test each valid way of initializing the object, except for `= {0}`, as
24562459
* Clang 5 complains when `-Wmissing-field-initializers` is used, even
24572460
* though it's OK by the C standard. We could test for this, but we'd need
@@ -2462,6 +2465,23 @@ void cipher_operation_init( )
24622465

24632466
memset( &zero, 0, sizeof( zero ) );
24642467

2468+
/* A default cipher operation should not be usable. */
2469+
TEST_EQUAL( psa_cipher_update( &func,
2470+
input, sizeof( input ),
2471+
output, sizeof( output ),
2472+
&output_length ),
2473+
PSA_ERROR_BAD_STATE );
2474+
TEST_EQUAL( psa_cipher_update( &init,
2475+
input, sizeof( input ),
2476+
output, sizeof( output ),
2477+
&output_length ),
2478+
PSA_ERROR_BAD_STATE );
2479+
TEST_EQUAL( psa_cipher_update( &zero,
2480+
input, sizeof( input ),
2481+
output, sizeof( output ),
2482+
&output_length ),
2483+
PSA_ERROR_BAD_STATE );
2484+
24652485
/* A default cipher operation should be abortable without error. */
24662486
PSA_ASSERT( psa_cipher_abort( &func ) );
24672487
PSA_ASSERT( psa_cipher_abort( &init ) );
@@ -2502,6 +2522,147 @@ exit:
25022522
}
25032523
/* END_CASE */
25042524

2525+
/* BEGIN_CASE */
2526+
void cipher_bad_order( )
2527+
{
2528+
psa_key_handle_t handle = 0;
2529+
psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2530+
psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
2531+
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2532+
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2533+
unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2534+
const uint8_t key[] = {
2535+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2536+
0xaa, 0xaa, 0xaa, 0xaa };
2537+
const uint8_t text[] = {
2538+
0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2539+
0xbb, 0xbb, 0xbb, 0xbb };
2540+
uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2541+
size_t length = 0;
2542+
2543+
PSA_ASSERT( psa_crypto_init( ) );
2544+
PSA_ASSERT( psa_allocate_key( &handle ) );
2545+
psa_key_policy_set_usage( &policy,
2546+
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2547+
alg );
2548+
PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2549+
PSA_ASSERT( psa_import_key( handle, key_type,
2550+
key, sizeof(key) ) );
2551+
2552+
2553+
/* Generate an IV without calling setup beforehand. */
2554+
memset( &operation, 0, sizeof( operation ) );
2555+
TEST_EQUAL( psa_cipher_generate_iv( &operation,
2556+
buffer, sizeof( buffer ),
2557+
&length ),
2558+
PSA_ERROR_BAD_STATE );
2559+
2560+
/* Generate an IV twice in a row. */
2561+
memset( &operation, 0, sizeof( operation ) );
2562+
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2563+
PSA_ASSERT( psa_cipher_generate_iv( &operation,
2564+
buffer, sizeof( buffer ),
2565+
&length ) );
2566+
TEST_EQUAL( psa_cipher_generate_iv( &operation,
2567+
buffer, sizeof( buffer ),
2568+
&length ),
2569+
PSA_ERROR_BAD_STATE );
2570+
2571+
/* Generate an IV after it's already set. */
2572+
memset( &operation, 0, sizeof( operation ) );
2573+
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2574+
PSA_ASSERT( psa_cipher_set_iv( &operation,
2575+
iv, sizeof( iv ) ) );
2576+
TEST_EQUAL( psa_cipher_generate_iv( &operation,
2577+
buffer, sizeof( buffer ),
2578+
&length ),
2579+
PSA_ERROR_BAD_STATE );
2580+
2581+
/* Set an IV without calling setup beforehand. */
2582+
memset( &operation, 0, sizeof( operation ) );
2583+
TEST_EQUAL( psa_cipher_set_iv( &operation,
2584+
iv, sizeof( iv ) ),
2585+
PSA_ERROR_BAD_STATE );
2586+
2587+
/* Set an IV after it's already set. */
2588+
memset( &operation, 0, sizeof( operation ) );
2589+
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2590+
PSA_ASSERT( psa_cipher_set_iv( &operation,
2591+
iv, sizeof( iv ) ) );
2592+
TEST_EQUAL( psa_cipher_set_iv( &operation,
2593+
iv, sizeof( iv ) ),
2594+
PSA_ERROR_BAD_STATE );
2595+
2596+
/* Set an IV after it's already generated. */
2597+
memset( &operation, 0, sizeof( operation ) );
2598+
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2599+
PSA_ASSERT( psa_cipher_generate_iv( &operation,
2600+
buffer, sizeof( buffer ),
2601+
&length ) );
2602+
TEST_EQUAL( psa_cipher_set_iv( &operation,
2603+
iv, sizeof( iv ) ),
2604+
PSA_ERROR_BAD_STATE );
2605+
2606+
/* Call update without calling setup beforehand. */
2607+
memset( &operation, 0, sizeof( operation ) );
2608+
TEST_EQUAL( psa_cipher_update( &operation,
2609+
text, sizeof( text ),
2610+
buffer, sizeof( buffer ),
2611+
&length ),
2612+
PSA_ERROR_BAD_STATE );
2613+
2614+
/* Call update without an IV where an IV is required. */
2615+
memset( &operation, 0, sizeof( operation ) );
2616+
TEST_EQUAL( psa_cipher_update( &operation,
2617+
text, sizeof( text ),
2618+
buffer, sizeof( buffer ),
2619+
&length ),
2620+
PSA_ERROR_BAD_STATE );
2621+
2622+
/* Call update after finish. */
2623+
memset( &operation, 0, sizeof( operation ) );
2624+
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2625+
PSA_ASSERT( psa_cipher_set_iv( &operation,
2626+
iv, sizeof( iv ) ) );
2627+
PSA_ASSERT( psa_cipher_finish( &operation,
2628+
buffer, sizeof( buffer ), &length ) );
2629+
TEST_EQUAL( psa_cipher_update( &operation,
2630+
text, sizeof( text ),
2631+
buffer, sizeof( buffer ),
2632+
&length ),
2633+
PSA_ERROR_BAD_STATE );
2634+
2635+
/* Call finish without calling setup beforehand. */
2636+
memset( &operation, 0, sizeof( operation ) );
2637+
TEST_EQUAL( psa_cipher_finish( &operation,
2638+
buffer, sizeof( buffer ), &length ),
2639+
PSA_ERROR_BAD_STATE );
2640+
2641+
/* Call finish without an IV where an IV is required. */
2642+
memset( &operation, 0, sizeof( operation ) );
2643+
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2644+
/* Not calling update means we are encrypting an empty buffer, which is OK
2645+
* for cipher modes with padding. */
2646+
TEST_EQUAL( psa_cipher_finish( &operation,
2647+
buffer, sizeof( buffer ), &length ),
2648+
PSA_ERROR_BAD_STATE );
2649+
2650+
/* Call finish twice in a row. */
2651+
memset( &operation, 0, sizeof( operation ) );
2652+
PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2653+
PSA_ASSERT( psa_cipher_set_iv( &operation,
2654+
iv, sizeof( iv ) ) );
2655+
PSA_ASSERT( psa_cipher_finish( &operation,
2656+
buffer, sizeof( buffer ), &length ) );
2657+
TEST_EQUAL( psa_cipher_finish( &operation,
2658+
buffer, sizeof( buffer ), &length ),
2659+
PSA_ERROR_BAD_STATE );
2660+
2661+
exit:
2662+
mbedtls_psa_crypto_free( );
2663+
}
2664+
/* END_CASE */
2665+
25052666
/* BEGIN_CASE */
25062667
void cipher_encrypt( int alg_arg, int key_type_arg,
25072668
data_t *key,

0 commit comments

Comments
 (0)