crypto_se_driver: add signing mock test

This commit is contained in:
Unknown 2019-07-11 06:01:33 -04:00 committed by Darryl Green
parent 136901c24c
commit b7656a8a85
2 changed files with 96 additions and 0 deletions

View file

@ -27,3 +27,9 @@ mock_generate:PSA_ERROR_HARDWARE_FAILURE:PSA_SUCCESS:PSA_ERROR_HARDWARE_FAILURE
SE key generating mock test: generating failed
mock_generate:PSA_SUCCESS:PSA_ERROR_HARDWARE_FAILURE:PSA_ERROR_HARDWARE_FAILURE
SE signing mock test
mock_sign:PSA_SUCCESS:PSA_SUCCESS
SE signing mock test: sign failed
mock_sign:PSA_ERROR_HARDWARE_FAILURE:PSA_ERROR_HARDWARE_FAILURE

View file

@ -38,6 +38,16 @@ static struct
psa_status_t return_value;
} mock_export_public_data;
static struct
{
uint16_t called;
psa_key_slot_number_t key_slot;
psa_algorithm_t alg;
size_t hash_length;
size_t signature_size;
psa_status_t return_value;
} mock_sign_data;
static struct
{
uint16_t called;
@ -56,6 +66,7 @@ static void mock_teardown( void )
memset( &mock_import_data, 0, sizeof( mock_import_data ) );
memset( &mock_export_data, 0, sizeof( mock_export_data ) );
memset( &mock_export_public_data, 0, sizeof( mock_export_public_data ) );
memset( &mock_sign_data, 0, sizeof( mock_sign_data ) );
memset( &mock_allocate_data, 0, sizeof( mock_allocate_data ) );
memset( &mock_destroy_data, 0, sizeof( mock_destroy_data ) );
memset( &mock_generate_data, 0, sizeof( mock_generate_data ) );
@ -133,6 +144,29 @@ psa_status_t mock_export_public( psa_drv_se_context_t *context,
return( mock_export_public_data.return_value );
}
psa_status_t mock_sign( psa_drv_se_context_t *context,
psa_key_slot_number_t key_slot,
psa_algorithm_t alg,
const uint8_t *p_hash,
size_t hash_length,
uint8_t *p_signature,
size_t signature_size,
size_t *p_signature_length )
{
(void) context;
(void) p_hash;
(void) p_signature;
(void) p_signature_length;
mock_sign_data.called++;
mock_sign_data.key_slot = key_slot;
mock_sign_data.alg = alg;
mock_sign_data.hash_length = hash_length;
mock_sign_data.signature_size = signature_size;
return mock_sign_data.return_value;
}
psa_status_t mock_allocate( psa_drv_se_context_t *drv_context,
void *persistent_data,
const psa_key_attributes_t *attributes,
@ -380,3 +414,59 @@ exit:
mock_teardown( );
}
/* END_CASE */
/* BEGIN_CASE */
void mock_sign( int mock_sign_return_value, int expected_result )
{
psa_drv_se_t driver;
psa_drv_se_key_management_t key_management;
psa_drv_se_asymmetric_t asymmetric;
psa_key_lifetime_t lifetime = 2;
psa_key_id_t id = 1;
psa_key_handle_t handle = 0;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
const uint8_t key_material[3] = {0xfa, 0xca, 0xde};
psa_algorithm_t algorithm = PSA_ALG_ECDSA(PSA_ALG_SHA_256);
size_t signature_length;
mock_sign_data.return_value = mock_sign_return_value;
memset( &driver, 0, sizeof( driver ) );
memset( &key_management, 0, sizeof( key_management ) );
memset( &asymmetric, 0, sizeof( asymmetric ) );
driver.hal_version = PSA_DRV_SE_HAL_VERSION;
driver.key_management = &key_management;
key_management.p_import = mock_import;
key_management.p_destroy = mock_destroy;
key_management.p_allocate = mock_allocate;
driver.asymmetric = &asymmetric;
asymmetric.p_sign = mock_sign;
PSA_ASSERT( psa_register_se_driver( lifetime, &driver ) );
PSA_ASSERT( psa_crypto_init( ) );
psa_set_key_id( &attributes, id );
psa_set_key_lifetime( &attributes, lifetime );
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
psa_set_key_algorithm( &attributes, algorithm );
psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
PSA_ASSERT( psa_import_key( &attributes,
key_material, sizeof( key_material ),
&handle ) );
TEST_ASSERT( psa_asymmetric_sign( handle, algorithm, NULL, 0, NULL, 0,
&signature_length)
== expected_result );
TEST_ASSERT( mock_sign_data.called == 1 );
PSA_ASSERT( psa_destroy_key( handle ) );
TEST_ASSERT( mock_destroy_data.called == 1 );
exit:
PSA_DONE( );
mock_teardown( );
}
/* END_CASE */