Use ASSERT_COMPARE instead of memcmp in PSA tests

This commit fixes some missing size comparison. In
aead_encrypt_decrypt, aead_encrypt and aead_decrypt, the test code
would not have noticed if the library function had reported an output
length that was not the expected length.
This commit is contained in:
Gilles Peskine 2018-09-27 13:57:19 +02:00
parent 3c22596d9b
commit bd7dea9e64

View file

@ -419,8 +419,7 @@ static int is_oid_of_key_type( psa_key_type_t type,
return( 0 ); return( 0 );
} }
TEST_ASSERT( oid_length == expected_oid_length ); ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
TEST_ASSERT( memcmp( oid, expected_oid, oid_length ) == 0 );
return( 1 ); return( 1 );
exit: exit:
@ -854,8 +853,7 @@ void fill_slots( int max_arg )
TEST_ASSERT( psa_export_key( slot, TEST_ASSERT( psa_export_key( slot,
exported, sizeof( exported ), exported, sizeof( exported ),
&exported_size ) == PSA_SUCCESS ); &exported_size ) == PSA_SUCCESS );
TEST_ASSERT( exported_size == sizeof( slot ) ); ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
TEST_ASSERT( memcmp( exported, &slot, sizeof( slot ) ) == 0 );
} }
exit: exit:
@ -992,10 +990,7 @@ void import_export( data_t *data,
goto exit; goto exit;
if( canonical_input ) if( canonical_input )
{ ASSERT_COMPARE( data->x, data->len, exported, exported_length );
TEST_ASSERT( exported_length == data->len );
TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
}
else else
{ {
TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS ); TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
@ -1007,9 +1002,8 @@ void import_export( data_t *data,
reexported, reexported,
export_size, export_size,
&reexported_length ) == PSA_SUCCESS ); &reexported_length ) == PSA_SUCCESS );
TEST_ASSERT( reexported_length == exported_length ); ASSERT_COMPARE( exported, exported_length,
TEST_ASSERT( memcmp( reexported, exported, reexported, reexported_length );
exported_length ) == 0 );
} }
destroy: destroy:
@ -1580,9 +1574,8 @@ void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
TEST_ASSERT( psa_hash_finish( &operation, TEST_ASSERT( psa_hash_finish( &operation,
actual_hash, sizeof( actual_hash ), actual_hash, sizeof( actual_hash ),
&actual_hash_length ) == PSA_SUCCESS ); &actual_hash_length ) == PSA_SUCCESS );
TEST_ASSERT( actual_hash_length == expected_hash->len ); ASSERT_COMPARE( expected_hash->x, expected_hash->len,
TEST_ASSERT( memcmp( expected_hash->x, actual_hash, actual_hash, actual_hash_length );
expected_hash->len ) == 0 );
exit: exit:
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );
@ -1795,9 +1788,8 @@ void cipher_encrypt( int alg_arg, int key_type_arg,
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->len ); ASSERT_COMPARE( expected_output->x, expected_output->len,
TEST_ASSERT( memcmp( expected_output->x, output, output, total_output_length );
expected_output->len ) == 0 );
} }
exit: exit:
@ -1872,9 +1864,8 @@ void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
total_output_length += function_output_length; total_output_length += function_output_length;
TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
TEST_ASSERT( total_output_length == expected_output->len ); ASSERT_COMPARE( expected_output->x, expected_output->len,
TEST_ASSERT( memcmp( expected_output->x, output, output, total_output_length );
expected_output->len ) == 0 );
exit: exit:
mbedtls_free( output ); mbedtls_free( output );
@ -1951,9 +1942,8 @@ void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
total_output_length += function_output_length; total_output_length += function_output_length;
TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
TEST_ASSERT( total_output_length == expected_output->len ); ASSERT_COMPARE( expected_output->x, expected_output->len,
TEST_ASSERT( memcmp( expected_output->x, output, output, total_output_length );
expected_output->len ) == 0 );
exit: exit:
mbedtls_free( output ); mbedtls_free( output );
@ -2026,9 +2016,8 @@ void cipher_decrypt( int alg_arg, int key_type_arg,
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->len ); ASSERT_COMPARE( expected_output->x, expected_output->len,
TEST_ASSERT( memcmp( expected_output->x, output, output, total_output_length );
expected_output->len ) == 0 );
} }
exit: exit:
@ -2115,8 +2104,7 @@ void cipher_verify_output( int alg_arg, int key_type_arg,
TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
TEST_ASSERT( input->len == output2_length ); ASSERT_COMPARE( input->x, input->len, output2, output2_length );
TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
exit: exit:
mbedtls_free( output1 ); mbedtls_free( output1 );
@ -2224,8 +2212,7 @@ void cipher_verify_output_multipart( int alg_arg,
TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
TEST_ASSERT( input->len == output2_length ); ASSERT_COMPARE( input->x, input->len, output2, output2_length );
TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
exit: exit:
mbedtls_free( output1 ); mbedtls_free( output1 );
@ -2299,8 +2286,8 @@ void aead_encrypt_decrypt( int key_type_arg,
output_data2, output_length, output_data2, output_length,
&output_length2 ) == expected_result ); &output_length2 ) == expected_result );
TEST_ASSERT( memcmp( input_data->x, output_data2, ASSERT_COMPARE( input_data->x, input_data->len,
input_data->len ) == 0 ); output_data2, output_length2 );
} }
exit: exit:
@ -2357,8 +2344,8 @@ void aead_encrypt( int key_type_arg, data_t * key_data,
output_data, output_size, output_data, output_size,
&output_length ) == PSA_SUCCESS ); &output_length ) == PSA_SUCCESS );
TEST_ASSERT( memcmp( output_data, expected_result->x, ASSERT_COMPARE( expected_result->x, expected_result->len,
output_length ) == 0 ); output_data, output_length );
exit: exit:
psa_destroy_key( slot ); psa_destroy_key( slot );
@ -2416,10 +2403,8 @@ void aead_decrypt( int key_type_arg, data_t * key_data,
&output_length ) == expected_result ); &output_length ) == expected_result );
if( expected_result == PSA_SUCCESS ) if( expected_result == PSA_SUCCESS )
{ ASSERT_COMPARE( expected_data->x, expected_data->len,
TEST_ASSERT( memcmp( output_data, expected_data->x, output_data, output_length );
output_length ) == 0 );
}
exit: exit:
psa_destroy_key( slot ); psa_destroy_key( slot );
@ -2491,9 +2476,8 @@ void sign_deterministic( int key_type_arg, data_t *key_data,
signature, signature_size, signature, signature_size,
&signature_length ) == PSA_SUCCESS ); &signature_length ) == PSA_SUCCESS );
/* Verify that the signature is what is expected. */ /* Verify that the signature is what is expected. */
TEST_ASSERT( signature_length == output_data->len ); ASSERT_COMPARE( output_data->x, output_data->len,
TEST_ASSERT( memcmp( signature, output_data->x, signature, signature_length );
output_data->len ) == 0 );
exit: exit:
psa_destroy_key( slot ); psa_destroy_key( slot );
@ -2837,9 +2821,8 @@ void asymmetric_encrypt_decrypt( int key_type_arg,
label->x, label->len, label->x, label->len,
output2, output2_size, output2, output2_size,
&output2_length ) == PSA_SUCCESS ); &output2_length ) == PSA_SUCCESS );
TEST_ASSERT( output2_length == input_data->len ); ASSERT_COMPARE( input_data->x, input_data->len,
TEST_ASSERT( memcmp( input_data->x, output2, output2, output2_length );
input_data->len ) == 0 );
exit: exit:
psa_destroy_key( slot ); psa_destroy_key( slot );
@ -2891,8 +2874,8 @@ void asymmetric_decrypt( int key_type_arg,
output, output,
output_size, output_size,
&output_length ) == PSA_SUCCESS ); &output_length ) == PSA_SUCCESS );
TEST_ASSERT( expected_data->len == output_length ); ASSERT_COMPARE( expected_data->x, expected_data->len,
TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 ); output, output_length );
/* If the label is empty, the test framework puts a non-null pointer /* If the label is empty, the test framework puts a non-null pointer
* in label->x. Test that a null pointer works as well. */ * in label->x. Test that a null pointer works as well. */
@ -2906,8 +2889,8 @@ void asymmetric_decrypt( int key_type_arg,
output, output,
output_size, output_size,
&output_length ) == PSA_SUCCESS ); &output_length ) == PSA_SUCCESS );
TEST_ASSERT( expected_data->len == output_length ); ASSERT_COMPARE( expected_data->x, expected_data->len,
TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 ); output, output_length );
} }
exit: exit: