From a7ec95f1ea08139290184dcaf2f5b450da51a4af Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 8 Jun 2018 14:40:59 +0200 Subject: [PATCH] Cipher tests: calculate and verify the actual output size --- tests/suites/test_suite_psa_crypto.function | 25 ++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index bee64ef6c..6be41c350 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -285,6 +285,7 @@ void cipher_encrypt( int alg_arg, int key_type_arg, size_t expected_output_size; size_t output_buffer_size = 0; size_t function_output_length = 0; + size_t total_output_length = 0; 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, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; status = psa_cipher_finish( &operation, output + function_output_length, output_buffer_size, &function_output_length ); + total_output_length += function_output_length; + TEST_ASSERT( status == (psa_status_t) expected_status ); if( expected_status == PSA_SUCCESS ) { TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); + TEST_ASSERT( total_output_length == expected_output_size ); TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); } + exit: mbedtls_free( key ); 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 output_buffer_size = 0; size_t function_output_length = 0; + size_t total_output_length = 0; psa_cipher_operation_t operation; - key = unhexify_alloc( key_hex, &key_size ); 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, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; TEST_ASSERT( psa_cipher_finish( &operation, output + function_output_length, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); - + total_output_length += function_output_length; 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 ); exit: @@ -425,9 +433,9 @@ void cipher_decrypt_multipart( int alg_arg, int key_type_arg, size_t expected_output_size; size_t output_buffer_size = 0; size_t function_output_length = 0; + size_t total_output_length = 0; psa_cipher_operation_t operation; - key = unhexify_alloc( key_hex, &key_size ); 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, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; TEST_ASSERT( psa_cipher_finish( &operation, output + function_output_length, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; 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 ); exit: @@ -499,6 +510,7 @@ void cipher_decrypt( int alg_arg, int key_type_arg, size_t expected_output_size; size_t output_buffer_size = 0; size_t function_output_length = 0; + size_t total_output_length = 0; 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, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; status = psa_cipher_finish( &operation, output + function_output_length, output_buffer_size, &function_output_length ); + total_output_length += function_output_length; TEST_ASSERT( status == (psa_status_t) expected_status ); if( expected_status == PSA_SUCCESS ) { TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); + TEST_ASSERT( total_output_length == expected_output_size ); TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); }