Skip to content

Commit 1f0a3e6

Browse files
SE keys: support destroy, import, export
1 parent e1bd159 commit 1f0a3e6

File tree

1 file changed

+63
-6
lines changed

1 file changed

+63
-6
lines changed

library/psa_crypto.c

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -939,17 +939,36 @@ psa_status_t psa_destroy_key( psa_key_handle_t handle )
939939
psa_key_slot_t *slot;
940940
psa_status_t status = PSA_SUCCESS;
941941
psa_status_t storage_status = PSA_SUCCESS;
942+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
943+
const psa_se_drv_table_entry_t *driver;
944+
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
942945

943946
status = psa_get_key_slot( handle, &slot );
944947
if( status != PSA_SUCCESS )
945948
return( status );
949+
950+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
951+
driver = psa_get_se_driver_entry( slot->lifetime );
952+
if( driver != NULL )
953+
{
954+
const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
955+
psa_key_slot_number_t slot_number = slot->data.se.slot_number;
956+
if( drv->key_management == NULL ||
957+
drv->key_management->p_destroy == NULL )
958+
return( PSA_ERROR_NOT_PERMITTED );
959+
status = drv->key_management->p_destroy( slot_number );
960+
psa_update_se_slot_usage( driver, slot_number, 0 );
961+
}
962+
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
963+
946964
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
947965
if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
948966
{
949967
storage_status =
950968
psa_destroy_persistent_key( slot->persistent_storage_id );
951969
}
952970
#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
971+
953972
status = psa_wipe_key_slot( slot );
954973
if( status != PSA_SUCCESS )
955974
return( status );
@@ -1122,11 +1141,29 @@ static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
11221141
size_t *data_length,
11231142
int export_public_key )
11241143
{
1144+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1145+
const psa_drv_se_t *drv = psa_get_se_driver( slot->lifetime );
1146+
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1147+
11251148
*data_length = 0;
11261149

11271150
if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
11281151
return( PSA_ERROR_INVALID_ARGUMENT );
11291152

1153+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1154+
if( drv != NULL )
1155+
{
1156+
psa_drv_se_export_key_t method;
1157+
if( drv->key_management == NULL )
1158+
return( PSA_ERROR_NOT_SUPPORTED );
1159+
method = ( export_public_key ?
1160+
drv->key_management->p_export_public :
1161+
drv->key_management->p_export );
1162+
return( ( *method )( slot->data.se.slot_number,
1163+
data, data_size, data_length ) );
1164+
}
1165+
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1166+
11301167
if( key_type_is_raw_bytes( slot->type ) )
11311168
{
11321169
if( slot->data.raw.bytes > data_size )
@@ -1528,12 +1565,32 @@ psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
15281565
if( status != PSA_SUCCESS )
15291566
goto exit;
15301567

1531-
status = psa_import_key_into_slot( slot, data, data_length );
1532-
if( status != PSA_SUCCESS )
1533-
goto exit;
1534-
status = psa_check_key_slot_attributes( slot, attributes );
1535-
if( status != PSA_SUCCESS )
1536-
goto exit;
1568+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1569+
if( driver != NULL )
1570+
{
1571+
const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
1572+
if( drv->key_management == NULL ||
1573+
drv->key_management->p_import == NULL )
1574+
{
1575+
status = PSA_ERROR_NOT_SUPPORTED;
1576+
goto exit;
1577+
}
1578+
status = drv->key_management->p_import(
1579+
slot->data.se.slot_number,
1580+
slot->lifetime, slot->type, slot->policy.alg, slot->policy.usage,
1581+
data, data_length );
1582+
/* TOnogrepDO: psa_check_key_slot_attributes? */
1583+
}
1584+
else
1585+
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1586+
{
1587+
status = psa_import_key_into_slot( slot, data, data_length );
1588+
if( status != PSA_SUCCESS )
1589+
goto exit;
1590+
status = psa_check_key_slot_attributes( slot, attributes );
1591+
if( status != PSA_SUCCESS )
1592+
goto exit;
1593+
}
15371594

15381595
status = psa_finish_key_creation( slot, driver );
15391596
exit:

0 commit comments

Comments
 (0)