mirror of
https://github.com/cuberite/polarssl.git
synced 2025-09-09 23:26:29 -04:00
Merge pull request #3612 from gilles-peskine-arm/psa-mac-negative-tests
PSA: add negative MAC tests
This commit is contained in:
commit
2db7be1cbb
@ -3028,17 +3028,21 @@ void mac_sign( int key_type_arg,
|
|||||||
psa_algorithm_t alg = alg_arg;
|
psa_algorithm_t alg = alg_arg;
|
||||||
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
||||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||||
/* Leave a little extra room in the output buffer. At the end of the
|
uint8_t *actual_mac = NULL;
|
||||||
* test, we'll check that the implementation didn't overwrite onto
|
|
||||||
* this extra room. */
|
|
||||||
uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
|
|
||||||
size_t mac_buffer_size =
|
size_t mac_buffer_size =
|
||||||
PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
|
PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
|
||||||
size_t mac_length = 0;
|
size_t mac_length = 0;
|
||||||
|
const size_t output_sizes_to_test[] = {
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
expected_mac->len - 1,
|
||||||
|
expected_mac->len,
|
||||||
|
expected_mac->len + 1,
|
||||||
|
};
|
||||||
|
|
||||||
memset( actual_mac, '+', sizeof( actual_mac ) );
|
|
||||||
TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
|
TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
|
||||||
TEST_ASSERT( expected_mac->len <= mac_buffer_size );
|
/* We expect PSA_MAC_FINAL_SIZE to be exact. */
|
||||||
|
TEST_ASSERT( expected_mac->len == mac_buffer_size );
|
||||||
|
|
||||||
PSA_ASSERT( psa_crypto_init( ) );
|
PSA_ASSERT( psa_crypto_init( ) );
|
||||||
|
|
||||||
@ -3048,26 +3052,40 @@ void mac_sign( int key_type_arg,
|
|||||||
|
|
||||||
PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
|
PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
|
||||||
|
|
||||||
|
for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
|
||||||
|
{
|
||||||
|
const size_t output_size = output_sizes_to_test[i];
|
||||||
|
psa_status_t expected_status =
|
||||||
|
( output_size >= expected_mac->len ? PSA_SUCCESS :
|
||||||
|
PSA_ERROR_BUFFER_TOO_SMALL );
|
||||||
|
|
||||||
|
test_set_step( output_size );
|
||||||
|
ASSERT_ALLOC( actual_mac, output_size );
|
||||||
|
|
||||||
/* Calculate the MAC. */
|
/* Calculate the MAC. */
|
||||||
PSA_ASSERT( psa_mac_sign_setup( &operation,
|
PSA_ASSERT( psa_mac_sign_setup( &operation,
|
||||||
handle, alg ) );
|
handle, alg ) );
|
||||||
PSA_ASSERT( psa_mac_update( &operation,
|
PSA_ASSERT( psa_mac_update( &operation,
|
||||||
input->x, input->len ) );
|
input->x, input->len ) );
|
||||||
PSA_ASSERT( psa_mac_sign_finish( &operation,
|
TEST_EQUAL( psa_mac_sign_finish( &operation,
|
||||||
actual_mac, mac_buffer_size,
|
actual_mac, output_size,
|
||||||
&mac_length ) );
|
&mac_length ),
|
||||||
|
expected_status );
|
||||||
|
PSA_ASSERT( psa_mac_abort( &operation ) );
|
||||||
|
|
||||||
/* Compare with the expected value. */
|
if( expected_status == PSA_SUCCESS )
|
||||||
|
{
|
||||||
ASSERT_COMPARE( expected_mac->x, expected_mac->len,
|
ASSERT_COMPARE( expected_mac->x, expected_mac->len,
|
||||||
actual_mac, mac_length );
|
actual_mac, mac_length );
|
||||||
|
}
|
||||||
/* Verify that the end of the buffer is untouched. */
|
mbedtls_free( actual_mac );
|
||||||
TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
|
actual_mac = NULL;
|
||||||
sizeof( actual_mac ) - mac_length ) );
|
}
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
psa_destroy_key( handle );
|
psa_destroy_key( handle );
|
||||||
PSA_DONE( );
|
PSA_DONE( );
|
||||||
|
mbedtls_free( actual_mac );
|
||||||
}
|
}
|
||||||
/* END_CASE */
|
/* END_CASE */
|
||||||
|
|
||||||
@ -3083,6 +3101,7 @@ void mac_verify( int key_type_arg,
|
|||||||
psa_algorithm_t alg = alg_arg;
|
psa_algorithm_t alg = alg_arg;
|
||||||
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
||||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||||
|
uint8_t *perturbed_mac = NULL;
|
||||||
|
|
||||||
TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
|
TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
|
||||||
|
|
||||||
@ -3094,18 +3113,57 @@ void mac_verify( int key_type_arg,
|
|||||||
|
|
||||||
PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
|
PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
|
||||||
|
|
||||||
|
/* Test the correct MAC. */
|
||||||
PSA_ASSERT( psa_mac_verify_setup( &operation,
|
PSA_ASSERT( psa_mac_verify_setup( &operation,
|
||||||
handle, alg ) );
|
handle, alg ) );
|
||||||
PSA_ASSERT( psa_destroy_key( handle ) );
|
|
||||||
PSA_ASSERT( psa_mac_update( &operation,
|
PSA_ASSERT( psa_mac_update( &operation,
|
||||||
input->x, input->len ) );
|
input->x, input->len ) );
|
||||||
PSA_ASSERT( psa_mac_verify_finish( &operation,
|
PSA_ASSERT( psa_mac_verify_finish( &operation,
|
||||||
expected_mac->x,
|
expected_mac->x,
|
||||||
expected_mac->len ) );
|
expected_mac->len ) );
|
||||||
|
|
||||||
|
/* Test a MAC that's too short. */
|
||||||
|
PSA_ASSERT( psa_mac_verify_setup( &operation,
|
||||||
|
handle, alg ) );
|
||||||
|
PSA_ASSERT( psa_mac_update( &operation,
|
||||||
|
input->x, input->len ) );
|
||||||
|
TEST_EQUAL( psa_mac_verify_finish( &operation,
|
||||||
|
expected_mac->x,
|
||||||
|
expected_mac->len - 1 ),
|
||||||
|
PSA_ERROR_INVALID_SIGNATURE );
|
||||||
|
|
||||||
|
/* Test a MAC that's too long. */
|
||||||
|
ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
|
||||||
|
memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
|
||||||
|
PSA_ASSERT( psa_mac_verify_setup( &operation,
|
||||||
|
handle, alg ) );
|
||||||
|
PSA_ASSERT( psa_mac_update( &operation,
|
||||||
|
input->x, input->len ) );
|
||||||
|
TEST_EQUAL( psa_mac_verify_finish( &operation,
|
||||||
|
perturbed_mac,
|
||||||
|
expected_mac->len + 1 ),
|
||||||
|
PSA_ERROR_INVALID_SIGNATURE );
|
||||||
|
|
||||||
|
/* Test changing one byte. */
|
||||||
|
for( size_t i = 0; i < expected_mac->len; i++ )
|
||||||
|
{
|
||||||
|
test_set_step( i );
|
||||||
|
perturbed_mac[i] ^= 1;
|
||||||
|
PSA_ASSERT( psa_mac_verify_setup( &operation,
|
||||||
|
handle, alg ) );
|
||||||
|
PSA_ASSERT( psa_mac_update( &operation,
|
||||||
|
input->x, input->len ) );
|
||||||
|
TEST_EQUAL( psa_mac_verify_finish( &operation,
|
||||||
|
perturbed_mac,
|
||||||
|
expected_mac->len ),
|
||||||
|
PSA_ERROR_INVALID_SIGNATURE );
|
||||||
|
perturbed_mac[i] ^= 1;
|
||||||
|
}
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
psa_destroy_key( handle );
|
psa_destroy_key( handle );
|
||||||
PSA_DONE( );
|
PSA_DONE( );
|
||||||
|
mbedtls_free( perturbed_mac );
|
||||||
}
|
}
|
||||||
/* END_CASE */
|
/* END_CASE */
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user