Cipher tests: calculate and verify the actual output size

This commit is contained in:
Gilles Peskine 2018-06-08 14:40:59 +02:00 committed by itayzafrir
parent 50e586b691
commit a7ec95f1ea

View File

@ -285,6 +285,7 @@ void cipher_encrypt( int alg_arg, int key_type_arg,
size_t expected_output_size; size_t expected_output_size;
size_t output_buffer_size = 0; size_t output_buffer_size = 0;
size_t function_output_length = 0; size_t function_output_length = 0;
size_t total_output_length = 0;
psa_cipher_operation_t operation; psa_cipher_operation_t operation;
@ -314,17 +315,22 @@ void cipher_encrypt( int alg_arg, int key_type_arg,
TEST_ASSERT( psa_cipher_update( &operation, input, input_size, TEST_ASSERT( psa_cipher_update( &operation, input, input_size,
output, output_buffer_size, output, output_buffer_size,
&function_output_length ) == PSA_SUCCESS ); &function_output_length ) == PSA_SUCCESS );
total_output_length += function_output_length;
status = psa_cipher_finish( &operation, status = psa_cipher_finish( &operation,
output + function_output_length, output + function_output_length,
output_buffer_size, output_buffer_size,
&function_output_length ); &function_output_length );
total_output_length += function_output_length;
TEST_ASSERT( status == (psa_status_t) expected_status ); TEST_ASSERT( status == (psa_status_t) expected_status );
if( expected_status == PSA_SUCCESS ) if( expected_status == PSA_SUCCESS )
{ {
TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
TEST_ASSERT( total_output_length == expected_output_size );
TEST_ASSERT( memcmp( expected_output, output, TEST_ASSERT( memcmp( expected_output, output,
expected_output_size ) == 0 ); expected_output_size ) == 0 );
} }
exit: exit:
mbedtls_free( key ); mbedtls_free( key );
mbedtls_free( input ); mbedtls_free( input );
@ -352,9 +358,9 @@ void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
size_t expected_output_size; size_t expected_output_size;
size_t output_buffer_size = 0; size_t output_buffer_size = 0;
size_t function_output_length = 0; size_t function_output_length = 0;
size_t total_output_length = 0;
psa_cipher_operation_t operation; psa_cipher_operation_t operation;
key = unhexify_alloc( key_hex, &key_size ); key = unhexify_alloc( key_hex, &key_size );
TEST_ASSERT( key != NULL ); TEST_ASSERT( key != NULL );
@ -382,19 +388,21 @@ void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size,
output, output_buffer_size, output, output_buffer_size,
&function_output_length ) == PSA_SUCCESS ); &function_output_length ) == PSA_SUCCESS );
total_output_length += function_output_length;
TEST_ASSERT( psa_cipher_update( &operation, TEST_ASSERT( psa_cipher_update( &operation,
input + first_part_size, input + first_part_size,
input_size - first_part_size, input_size - first_part_size,
output, output_buffer_size, output, output_buffer_size,
&function_output_length ) == PSA_SUCCESS ); &function_output_length ) == PSA_SUCCESS );
total_output_length += function_output_length;
TEST_ASSERT( psa_cipher_finish( &operation, TEST_ASSERT( psa_cipher_finish( &operation,
output + function_output_length, output + function_output_length,
output_buffer_size, output_buffer_size,
&function_output_length ) == PSA_SUCCESS ); &function_output_length ) == PSA_SUCCESS );
total_output_length += function_output_length;
TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
TEST_ASSERT( input_size == expected_output_size ); TEST_ASSERT( total_output_length == expected_output_size );
TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 );
exit: exit:
@ -425,9 +433,9 @@ void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
size_t expected_output_size; size_t expected_output_size;
size_t output_buffer_size = 0; size_t output_buffer_size = 0;
size_t function_output_length = 0; size_t function_output_length = 0;
size_t total_output_length = 0;
psa_cipher_operation_t operation; psa_cipher_operation_t operation;
key = unhexify_alloc( key_hex, &key_size ); key = unhexify_alloc( key_hex, &key_size );
TEST_ASSERT( key != NULL ); TEST_ASSERT( key != NULL );
@ -456,18 +464,21 @@ void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size,
output, output_buffer_size, output, output_buffer_size,
&function_output_length ) == PSA_SUCCESS ); &function_output_length ) == PSA_SUCCESS );
total_output_length += function_output_length;
TEST_ASSERT( psa_cipher_update( &operation, TEST_ASSERT( psa_cipher_update( &operation,
input + first_part_size, input + first_part_size,
input_size - first_part_size, input_size - first_part_size,
output, output_buffer_size, output, output_buffer_size,
&function_output_length ) == PSA_SUCCESS ); &function_output_length ) == PSA_SUCCESS );
total_output_length += function_output_length;
TEST_ASSERT( psa_cipher_finish( &operation, TEST_ASSERT( psa_cipher_finish( &operation,
output + function_output_length, output + function_output_length,
output_buffer_size, output_buffer_size,
&function_output_length ) == PSA_SUCCESS ); &function_output_length ) == PSA_SUCCESS );
total_output_length += function_output_length;
TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
TEST_ASSERT( input_size == expected_output_size ); TEST_ASSERT( total_output_length == expected_output_size );
TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 );
exit: exit:
@ -499,6 +510,7 @@ void cipher_decrypt( int alg_arg, int key_type_arg,
size_t expected_output_size; size_t expected_output_size;
size_t output_buffer_size = 0; size_t output_buffer_size = 0;
size_t function_output_length = 0; size_t function_output_length = 0;
size_t total_output_length = 0;
psa_cipher_operation_t operation; psa_cipher_operation_t operation;
@ -529,15 +541,18 @@ void cipher_decrypt( int alg_arg, int key_type_arg,
TEST_ASSERT( psa_cipher_update( &operation, input, input_size, TEST_ASSERT( psa_cipher_update( &operation, input, input_size,
output, output_buffer_size, output, output_buffer_size,
&function_output_length ) == PSA_SUCCESS ); &function_output_length ) == PSA_SUCCESS );
total_output_length += function_output_length;
status = psa_cipher_finish( &operation, status = psa_cipher_finish( &operation,
output + function_output_length, output + function_output_length,
output_buffer_size, output_buffer_size,
&function_output_length ); &function_output_length );
total_output_length += function_output_length;
TEST_ASSERT( status == (psa_status_t) expected_status ); TEST_ASSERT( status == (psa_status_t) expected_status );
if( expected_status == PSA_SUCCESS ) if( expected_status == PSA_SUCCESS )
{ {
TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
TEST_ASSERT( total_output_length == expected_output_size );
TEST_ASSERT( memcmp( expected_output, output, TEST_ASSERT( memcmp( expected_output, output,
expected_output_size ) == 0 ); expected_output_size ) == 0 );
} }