crypto_se_driver: add mock tests

Mock key importing and exporting
This commit is contained in:
Unknown 2019-07-10 06:45:31 -04:00 committed by Darryl Green
parent b090d5dc2a
commit c9ad5910aa
3 changed files with 212 additions and 0 deletions

View file

@ -143,6 +143,7 @@ add_test_suite(psa_crypto_init)
add_test_suite(psa_crypto_metadata)
add_test_suite(psa_crypto_persistent_key)
add_test_suite(psa_crypto_se_driver_hal)
add_test_suite(psa_crypto_se_driver_hal_mocks)
add_test_suite(psa_crypto_slot_management)
add_test_suite(psa_its)
add_test_suite(shax)

View file

@ -0,0 +1,5 @@
SE key importing mock test
mock_import:
SE key exporting mock test
mock_export:

View file

@ -0,0 +1,206 @@
/* BEGIN_HEADER */
#include "psa_crypto_helpers.h"
#include "psa/crypto_se_driver.h"
#include "psa_crypto_se.h"
static struct
{
uint16_t called;
psa_key_slot_number_t key_slot;
psa_key_attributes_t attributes;
size_t data_length;
} mock_import_data;
static struct
{
uint16_t called;
psa_key_slot_number_t slot_number;
size_t data_size;
} mock_export_data;
static struct
{
uint16_t called;
} mock_allocate_data;
static struct
{
uint16_t called;
psa_key_slot_number_t slot_number;
} mock_destroy_data;
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_allocate_data, 0, sizeof( mock_allocate_data ) );
memset( &mock_destroy_data, 0, sizeof( mock_destroy_data ) );
}
static psa_status_t mock_import( psa_drv_se_context_t *drv_context,
psa_key_slot_number_t key_slot,
const psa_key_attributes_t *attributes,
const uint8_t *data,
size_t data_length,
size_t *bits )
{
(void) drv_context;
(void) data;
(void) bits;
mock_import_data.called++;
mock_import_data.key_slot = key_slot;
mock_import_data.attributes = *attributes;
mock_import_data.data_length = data_length;
return( PSA_SUCCESS );
}
psa_status_t mock_export( psa_drv_se_context_t *context,
psa_key_slot_number_t slot_number,
uint8_t *p_data,
size_t data_size,
size_t *p_data_length )
{
(void) context;
(void) p_data;
(void) p_data_length;
mock_export_data.called++;
mock_export_data.slot_number = slot_number;
mock_export_data.data_size = data_size;
return( PSA_SUCCESS );
}
psa_status_t mock_allocate( psa_drv_se_context_t *drv_context,
void *persistent_data,
const psa_key_attributes_t *attributes,
psa_key_creation_method_t method,
psa_key_slot_number_t *key_slot )
{
(void) drv_context;
(void) persistent_data;
(void) attributes;
(void) method;
(void) key_slot;
mock_allocate_data.called++;
*key_slot = 0;
return( PSA_SUCCESS );
}
psa_status_t mock_destroy( psa_drv_se_context_t *context,
void *persistent_data,
psa_key_slot_number_t slot_number )
{
(void) context;
(void) persistent_data;
mock_destroy_data.called++;
mock_destroy_data.slot_number = slot_number;
return( PSA_SUCCESS );
}
/* END_HEADER */
/* BEGIN_DEPENDENCIES
* depends_on:MBEDTLS_PSA_CRYPTO_SE_C
* END_DEPENDENCIES
*/
/* BEGIN_CASE */
void mock_import( )
{
psa_drv_se_t driver;
psa_drv_se_key_management_t key_management;
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};
memset( &driver, 0, sizeof( driver ) );
memset( &key_management, 0, sizeof( key_management ) );
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;
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_EXPORT );
psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
PSA_ASSERT( psa_import_key( &attributes,
key_material, sizeof( key_material ),
&handle ) );
TEST_ASSERT( mock_allocate_data.called == 1 );
TEST_ASSERT( mock_import_data.called == 1 );
TEST_ASSERT( mock_import_data.attributes.core.type == PSA_KEY_TYPE_RAW_DATA );
PSA_ASSERT( psa_destroy_key( handle ) );
TEST_ASSERT( mock_destroy_data.called == 1 );
exit:
PSA_DONE( );
mock_teardown( );
}
/* END_CASE */
/* BEGIN_CASE */
void mock_export( )
{
psa_drv_se_t driver;
psa_drv_se_key_management_t key_management;
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};
uint8_t exported[sizeof( key_material )];
size_t exported_length;
memset( &driver, 0, sizeof( driver ) );
memset( &key_management, 0, sizeof( key_management ) );
driver.hal_version = PSA_DRV_SE_HAL_VERSION;
driver.key_management = &key_management;
key_management.p_import = mock_import;
key_management.p_export = mock_export;
key_management.p_destroy = mock_destroy;
key_management.p_allocate = mock_allocate;
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_EXPORT );
psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
PSA_ASSERT( psa_import_key( &attributes,
key_material, sizeof( key_material ),
&handle ) );
PSA_ASSERT( psa_export_key( handle,
exported, sizeof( exported ),
&exported_length ) );
TEST_ASSERT( mock_export_data.called == 1 );
PSA_ASSERT( psa_destroy_key( handle ) );
TEST_ASSERT( mock_destroy_data.called == 1 );
exit:
PSA_DONE( );
mock_teardown( );
}
/* END_CASE */