Call software implementation as a driver

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
This commit is contained in:
Ronald Cron 2020-10-13 08:32:21 +02:00
parent 5c4d38639d
commit 977c247048
3 changed files with 59 additions and 92 deletions

View File

@ -6167,24 +6167,34 @@ psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
if( status != PSA_SUCCESS ) if( status != PSA_SUCCESS )
goto exit; goto exit;
/* In the case of a transparent key or an opaque key stored in local
* storage (thus not in the case of generating a key in a secure element
* or cryptoprocessor with storage), we have to allocate a buffer to
* hold the generated key material. */
if( slot->key.data == NULL )
{
if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
PSA_KEY_LOCATION_LOCAL_STORAGE )
{
status = psa_get_key_buffer_size( attributes->core.type,
attributes->core.bits,
&key_buffer_size );
}
else
{
status = psa_driver_wrapper_get_key_buffer_size(
attributes, &key_buffer_size );
}
if( status != PSA_SUCCESS )
goto exit;
status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
if( status != PSA_SUCCESS )
goto exit;
}
status = psa_driver_wrapper_generate_key( attributes, status = psa_driver_wrapper_generate_key( attributes,
slot ); slot->key.data, slot->key.bytes, &slot->key.bytes );
if( status != PSA_ERROR_NOT_SUPPORTED ||
psa_key_lifetime_is_external( attributes->core.lifetime ) )
goto exit;
status = psa_get_key_buffer_size( attributes->core.type,
attributes->core.bits,
&key_buffer_size );
if( status != PSA_SUCCESS )
goto exit;
status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
if( status != PSA_SUCCESS )
goto exit;
status = psa_generate_key_internal(
attributes, slot->key.data, slot->key.bytes, &slot->key.bytes );
if( status != PSA_SUCCESS ) if( status != PSA_SUCCESS )
psa_remove_key_data_from_memory( slot ); psa_remove_key_data_from_memory( slot );

View File

@ -306,16 +306,20 @@ psa_status_t psa_driver_wrapper_get_key_buffer_size(
} }
} }
psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes, psa_status_t psa_driver_wrapper_generate_key(
psa_key_slot_t *slot ) const psa_key_attributes_t *attributes,
uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
{ {
#if defined(PSA_CRYPTO_DRIVER_PRESENT) psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
psa_key_location_t location =
PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
/* Try dynamically-registered SE interface first */ /* Try dynamically-registered SE interface first */
#if defined(MBEDTLS_PSA_CRYPTO_SE_C) #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
const psa_drv_se_t *drv; const psa_drv_se_t *drv;
psa_drv_se_context_t *drv_context; psa_drv_se_context_t *drv_context;
if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
{ {
size_t pubkey_length = 0; /* We don't support this feature yet */ size_t pubkey_length = 0; /* We don't support this feature yet */
if( drv->key_management == NULL || if( drv->key_management == NULL ||
@ -325,100 +329,53 @@ psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attrib
return( PSA_ERROR_NOT_SUPPORTED ); return( PSA_ERROR_NOT_SUPPORTED );
} }
return( drv->key_management->p_generate( return( drv->key_management->p_generate(
drv_context, psa_key_slot_get_slot_number( slot ), drv_context,
*( (psa_key_slot_number_t *)key_buffer ),
attributes, NULL, 0, &pubkey_length ) ); attributes, NULL, 0, &pubkey_length ) );
} }
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */ #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
/* Then try accelerator API */
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
size_t export_size = 0;
/* If this is not to generate a key in a secure element or cryptoprocessor
* with storage. */
if( slot->key.data == NULL )
{
if( location == PSA_KEY_LOCATION_LOCAL_STORAGE )
{
export_size = PSA_KEY_EXPORT_MAX_SIZE( attributes->core.type,
attributes->core.bits );
if( export_size == 0 )
return( PSA_ERROR_NOT_SUPPORTED );
}
else
{
status = psa_driver_wrapper_get_key_buffer_size( attributes,
&export_size );
if( status != PSA_SUCCESS )
return( status );
}
slot->key.data = mbedtls_calloc(1, export_size);
if( slot->key.data == NULL )
return( PSA_ERROR_INSUFFICIENT_MEMORY );
slot->key.bytes = export_size;
}
switch( location ) switch( location )
{ {
case PSA_KEY_LOCATION_LOCAL_STORAGE: case PSA_KEY_LOCATION_LOCAL_STORAGE:
/* Key is stored in the slot in export representation, so #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
* cycle through all known transparent accelerators */
/* Transparent drivers are limited to generating asymmetric keys */ /* Transparent drivers are limited to generating asymmetric keys */
if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
{ {
status = PSA_ERROR_NOT_SUPPORTED; /* Cycle through all known transparent accelerators */
break;
}
#if defined(PSA_CRYPTO_DRIVER_TEST) #if defined(PSA_CRYPTO_DRIVER_TEST)
status = test_transparent_generate_key( attributes, status = test_transparent_generate_key(
slot->key.data, attributes, key_buffer, key_buffer_size,
slot->key.bytes, key_buffer_length );
&slot->key.bytes ); /* Declared with fallback == true */
/* Declared with fallback == true */ if( status != PSA_ERROR_NOT_SUPPORTED )
if( status != PSA_ERROR_NOT_SUPPORTED ) break;
break;
#endif /* PSA_CRYPTO_DRIVER_TEST */ #endif /* PSA_CRYPTO_DRIVER_TEST */
/* Fell through, meaning no accelerator supports this operation */ }
status = PSA_ERROR_NOT_SUPPORTED; #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
/* Software fallback */
status = psa_generate_key_internal(
attributes, key_buffer, key_buffer_size, key_buffer_length );
break; break;
/* Add cases for opaque driver here */ /* Add cases for opaque driver here */
#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
#if defined(PSA_CRYPTO_DRIVER_TEST) #if defined(PSA_CRYPTO_DRIVER_TEST)
case PSA_CRYPTO_TEST_DRIVER_LIFETIME: case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
status = test_opaque_generate_key( attributes, status = test_opaque_generate_key(
slot->key.data, attributes, key_buffer, key_buffer_size, key_buffer_length );
slot->key.bytes,
&slot->key.bytes );
break; break;
#endif /* PSA_CRYPTO_DRIVER_TEST */ #endif /* PSA_CRYPTO_DRIVER_TEST */
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
default: default:
/* Key is declared with a lifetime not known to us */ /* Key is declared with a lifetime not known to us */
status = PSA_ERROR_INVALID_ARGUMENT; status = PSA_ERROR_INVALID_ARGUMENT;
break; break;
} }
if( status != PSA_SUCCESS )
{
/* free allocated buffer */
mbedtls_free( slot->key.data );
slot->key.data = NULL;
slot->key.bytes = 0;
}
return( status ); return( status );
#else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
return( PSA_ERROR_NOT_SUPPORTED );
#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
#else /* PSA_CRYPTO_DRIVER_PRESENT */
(void) attributes;
(void) slot;
return( PSA_ERROR_NOT_SUPPORTED );
#endif /* PSA_CRYPTO_DRIVER_PRESENT */
} }
psa_status_t psa_driver_wrapper_import_key( psa_status_t psa_driver_wrapper_import_key(

View File

@ -69,7 +69,7 @@ psa_status_t psa_driver_wrapper_get_key_buffer_size(
psa_status_t psa_driver_wrapper_generate_key( psa_status_t psa_driver_wrapper_generate_key(
const psa_key_attributes_t *attributes, const psa_key_attributes_t *attributes,
psa_key_slot_t *slot ); uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length );
/* /*
* Cipher functions * Cipher functions