Skip to content

Commit 748949f

Browse files
committed
psa: Disallow use of invalid cipher contexts
A cipher context must have a setup function called on it in order to make the cipher context usable. Return PSA_ERROR_BAD_STATE for use of cipher contexts that haven't been set up. Test that blank cipher contexts are not usable.
1 parent d7c468f commit 748949f

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-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.function

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,6 +2345,9 @@ exit:
23452345
/* BEGIN_CASE */
23462346
void cipher_operation_init( )
23472347
{
2348+
const uint8_t input[1];
2349+
unsigned char output[1];
2350+
size_t output_length;
23482351
/* Test each valid way of initializing the object, except for `= {0}`, as
23492352
* Clang 5 complains when `-Wmissing-field-initializers` is used, even
23502353
* though it's OK by the C standard. We could test for this, but we'd need
@@ -2355,6 +2358,23 @@ void cipher_operation_init( )
23552358

23562359
memset( &zero, 0, sizeof( zero ) );
23572360

2361+
/* A default cipher operation should not be usable. */
2362+
TEST_EQUAL( psa_cipher_update( &func,
2363+
input, sizeof( input ),
2364+
output, sizeof( output ),
2365+
&output_length ),
2366+
PSA_ERROR_BAD_STATE );
2367+
TEST_EQUAL( psa_cipher_update( &init,
2368+
input, sizeof( input ),
2369+
output, sizeof( output ),
2370+
&output_length ),
2371+
PSA_ERROR_BAD_STATE );
2372+
TEST_EQUAL( psa_cipher_update( &zero,
2373+
input, sizeof( input ),
2374+
output, sizeof( output ),
2375+
&output_length ),
2376+
PSA_ERROR_BAD_STATE );
2377+
23582378
/* A default cipher operation should be abortable without error. */
23592379
PSA_ASSERT( psa_cipher_abort( &func ) );
23602380
PSA_ASSERT( psa_cipher_abort( &init ) );

0 commit comments

Comments
 (0)