@@ -1035,6 +1035,11 @@ psa_status_t psa_destroy_key( psa_key_handle_t handle )
1035
1035
/* Return the size of the key in the given slot, in bits. */
1036
1036
static size_t psa_get_key_slot_bits ( const psa_key_slot_t * slot )
1037
1037
{
1038
+ #if defined(MBEDTLS_PSA_CRYPTO_SE_C )
1039
+ if ( psa_get_se_driver ( slot -> lifetime , NULL , NULL ) )
1040
+ return ( slot -> data .se .bits );
1041
+ #endif /* defined(MBEDTLS_PSA_CRYPTO_SE_C) */
1042
+
1038
1043
if ( key_type_is_raw_bytes ( slot -> type ) )
1039
1044
return ( slot -> data .raw .bytes * 8 );
1040
1045
#if defined(MBEDTLS_RSA_C )
@@ -1140,10 +1145,10 @@ static psa_status_t psa_get_rsa_public_exponent(
1140
1145
}
1141
1146
#endif /* MBEDTLS_RSA_C */
1142
1147
1143
- /** Retrieve the readily-accessible attributes of a key in a slot.
1148
+ /** Retrieve the generic attributes of a key in a slot.
1144
1149
*
1145
- * This function does not compute attributes that are not directly
1146
- * stored in the slot, such as the bit size of a transparent key .
1150
+ * This function does not retrieve domain parameters, which require
1151
+ * additional memory management .
1147
1152
*/
1148
1153
static void psa_get_key_slot_attributes ( psa_key_slot_t * slot ,
1149
1154
psa_key_attributes_t * attributes )
@@ -1152,6 +1157,7 @@ static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
1152
1157
attributes -> lifetime = slot -> lifetime ;
1153
1158
attributes -> policy = slot -> policy ;
1154
1159
attributes -> type = slot -> type ;
1160
+ attributes -> bits = psa_get_key_slot_bits ( slot );
1155
1161
}
1156
1162
1157
1163
/** Retrieve all the publicly-accessible attributes of a key.
@@ -1164,21 +1170,26 @@ psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1164
1170
1165
1171
psa_reset_key_attributes ( attributes );
1166
1172
1167
- status = psa_get_transparent_key ( handle , & slot , 0 , 0 );
1173
+ status = psa_get_key_from_slot ( handle , & slot , 0 , 0 );
1168
1174
if ( status != PSA_SUCCESS )
1169
1175
return ( status );
1170
1176
1171
1177
psa_get_key_slot_attributes ( slot , attributes );
1172
- attributes -> bits = psa_get_key_slot_bits ( slot );
1173
1178
1174
1179
switch ( slot -> type )
1175
1180
{
1176
1181
#if defined(MBEDTLS_RSA_C )
1177
1182
case PSA_KEY_TYPE_RSA_KEY_PAIR :
1178
1183
case PSA_KEY_TYPE_RSA_PUBLIC_KEY :
1184
+ #if defined(MBEDTLS_PSA_CRYPTO_SE_C )
1185
+ /* TOnogrepDO: reporting the public exponent for opaque keys
1186
+ * is not yet implemented. */
1187
+ if ( psa_get_se_driver ( slot -> lifetime , NULL , NULL ) )
1188
+ break ;
1189
+ #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1179
1190
status = psa_get_rsa_public_exponent ( slot -> data .rsa , attributes );
1180
1191
break ;
1181
- #endif
1192
+ #endif /* MBEDTLS_RSA_C */
1182
1193
default :
1183
1194
/* Nothing else to do. */
1184
1195
break ;
@@ -1489,6 +1500,10 @@ static psa_status_t psa_start_key_creation(
1489
1500
(void ) psa_crypto_stop_transaction ( );
1490
1501
return ( status );
1491
1502
}
1503
+
1504
+ /* TOnogrepDO: validate bits. How to do this depends on the key
1505
+ * creation method, so setting bits might not belong here. */
1506
+ slot -> data .se .bits = psa_get_key_bits ( attributes );
1492
1507
}
1493
1508
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1494
1509
@@ -1523,40 +1538,32 @@ static psa_status_t psa_finish_key_creation(
1523
1538
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C )
1524
1539
if ( slot -> lifetime != PSA_KEY_LIFETIME_VOLATILE )
1525
1540
{
1526
- uint8_t * buffer = NULL ;
1527
- size_t buffer_size = 0 ;
1528
- size_t length = 0 ;
1541
+ psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT ;
1542
+ psa_get_key_slot_attributes ( slot , & attributes );
1529
1543
1530
1544
#if defined(MBEDTLS_PSA_CRYPTO_SE_C )
1531
1545
if ( driver != NULL )
1532
1546
{
1533
- buffer = (uint8_t * ) & slot -> data .se .slot_number ;
1534
- length = sizeof ( slot -> data .se .slot_number );
1547
+ status = psa_save_persistent_key ( & attributes ,
1548
+ (uint8_t * ) & slot -> data .se ,
1549
+ sizeof ( slot -> data .se ) );
1535
1550
}
1536
1551
else
1537
1552
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1538
1553
{
1539
- buffer_size = PSA_KEY_EXPORT_MAX_SIZE ( slot -> type ,
1540
- psa_get_key_slot_bits ( slot ) );
1541
- buffer = mbedtls_calloc ( 1 , buffer_size );
1554
+ size_t buffer_size =
1555
+ PSA_KEY_EXPORT_MAX_SIZE ( slot -> type ,
1556
+ psa_get_key_bits ( & attributes ) );
1557
+ uint8_t * buffer = mbedtls_calloc ( 1 , buffer_size );
1558
+ size_t length = 0 ;
1542
1559
if ( buffer == NULL && buffer_size != 0 )
1543
1560
return ( PSA_ERROR_INSUFFICIENT_MEMORY );
1544
1561
status = psa_internal_export_key ( slot ,
1545
1562
buffer , buffer_size , & length ,
1546
1563
0 );
1547
- }
1564
+ if ( status == PSA_SUCCESS )
1565
+ status = psa_save_persistent_key ( & attributes , buffer , length );
1548
1566
1549
- if ( status == PSA_SUCCESS )
1550
- {
1551
- psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT ;
1552
- psa_get_key_slot_attributes ( slot , & attributes );
1553
- status = psa_save_persistent_key ( & attributes , buffer , length );
1554
- }
1555
-
1556
- #if defined(MBEDTLS_PSA_CRYPTO_SE_C )
1557
- if ( driver == NULL )
1558
- #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1559
- {
1560
1567
if ( buffer_size != 0 )
1561
1568
mbedtls_platform_zeroize ( buffer , buffer_size );
1562
1569
mbedtls_free ( buffer );
@@ -1696,19 +1703,19 @@ psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
1696
1703
psa_get_se_driver_context ( driver ),
1697
1704
slot -> data .se .slot_number ,
1698
1705
slot -> lifetime , slot -> type , slot -> policy .alg , slot -> policy .usage ,
1699
- data , data_length );
1700
- /* TOnogrepDO: psa_check_key_slot_attributes? */
1706
+ data , data_length ,
1707
+ & slot -> data . se . bits );
1701
1708
}
1702
1709
else
1703
1710
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1704
1711
{
1705
1712
status = psa_import_key_into_slot ( slot , data , data_length );
1706
1713
if ( status != PSA_SUCCESS )
1707
1714
goto exit ;
1708
- status = psa_check_key_slot_attributes ( slot , attributes );
1709
- if ( status != PSA_SUCCESS )
1710
- goto exit ;
1711
1715
}
1716
+ status = psa_check_key_slot_attributes ( slot , attributes );
1717
+ if ( status != PSA_SUCCESS )
1718
+ goto exit ;
1712
1719
1713
1720
status = psa_finish_key_creation ( slot , driver );
1714
1721
exit :
0 commit comments