Skip to content

Commit dc5bfe9

Browse files
SE keys: implement and test psa_get_key_attributes
1 parent 424f894 commit dc5bfe9

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

library/psa_crypto.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,10 +1145,10 @@ static psa_status_t psa_get_rsa_public_exponent(
11451145
}
11461146
#endif /* MBEDTLS_RSA_C */
11471147

1148-
/** Retrieve the readily-accessible attributes of a key in a slot.
1148+
/** Retrieve the generic attributes of a key in a slot.
11491149
*
1150-
* This function does not compute attributes that are not directly
1151-
* 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.
11521152
*/
11531153
static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
11541154
psa_key_attributes_t *attributes )
@@ -1157,6 +1157,7 @@ static void psa_get_key_slot_attributes( psa_key_slot_t *slot,
11571157
attributes->lifetime = slot->lifetime;
11581158
attributes->policy = slot->policy;
11591159
attributes->type = slot->type;
1160+
attributes->bits = psa_get_key_slot_bits( slot );
11601161
}
11611162

11621163
/** Retrieve all the publicly-accessible attributes of a key.
@@ -1169,21 +1170,26 @@ psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
11691170

11701171
psa_reset_key_attributes( attributes );
11711172

1172-
status = psa_get_transparent_key( handle, &slot, 0, 0 );
1173+
status = psa_get_key_from_slot( handle, &slot, 0, 0 );
11731174
if( status != PSA_SUCCESS )
11741175
return( status );
11751176

11761177
psa_get_key_slot_attributes( slot, attributes );
1177-
attributes->bits = psa_get_key_slot_bits( slot );
11781178

11791179
switch( slot->type )
11801180
{
11811181
#if defined(MBEDTLS_RSA_C)
11821182
case PSA_KEY_TYPE_RSA_KEY_PAIR:
11831183
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 */
11841190
status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
11851191
break;
1186-
#endif
1192+
#endif /* MBEDTLS_RSA_C */
11871193
default:
11881194
/* Nothing else to do. */
11891195
break;

tests/suites/test_suite_psa_crypto_se_driver_hal.function

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,41 @@ static psa_status_t ram_allocate( psa_drv_se_context_t *context,
178178
/* Other test helper functions */
179179
/****************************************************************/
180180

181+
/* Check that the attributes of a key reported by psa_get_key_attributes()
182+
* are consistent with the attributes used when creating the key. */
183+
static int check_key_attributes(
184+
psa_key_handle_t handle,
185+
const psa_key_attributes_t *reference_attributes )
186+
{
187+
int ok = 0;
188+
psa_key_attributes_t actual_attributes = PSA_KEY_ATTRIBUTES_INIT;
189+
190+
PSA_ASSERT( psa_get_key_attributes( handle, &actual_attributes ) );
191+
192+
TEST_EQUAL( psa_get_key_id( &actual_attributes ),
193+
psa_get_key_id( reference_attributes ) );
194+
TEST_EQUAL( psa_get_key_lifetime( &actual_attributes ),
195+
psa_get_key_lifetime( reference_attributes ) );
196+
TEST_EQUAL( psa_get_key_type( &actual_attributes ),
197+
psa_get_key_type( reference_attributes ) );
198+
TEST_EQUAL( psa_get_key_usage_flags( &actual_attributes ),
199+
psa_get_key_usage_flags( reference_attributes ) );
200+
TEST_EQUAL( psa_get_key_algorithm( &actual_attributes ),
201+
psa_get_key_algorithm( reference_attributes ) );
202+
TEST_EQUAL( psa_get_key_enrollment_algorithm( &actual_attributes ),
203+
psa_get_key_enrollment_algorithm( reference_attributes ) );
204+
if( psa_get_key_bits( reference_attributes ) != 0 )
205+
{
206+
TEST_EQUAL( psa_get_key_bits( &actual_attributes ),
207+
psa_get_key_bits( reference_attributes ) );
208+
}
209+
210+
ok = 1;
211+
212+
exit:
213+
return( ok );
214+
}
215+
181216
/* Check that a function's return status is "smoke-free", i.e. that
182217
* it's an acceptable error code when calling an API function that operates
183218
* on a key with potentially bogus parameters. */
@@ -445,6 +480,9 @@ void key_creation_import_export( int min_slot, int restart )
445480
/* Test that the key was created in the expected slot. */
446481
TEST_ASSERT( ram_slots[min_slot].type == PSA_KEY_TYPE_RAW_DATA );
447482

483+
/* Test the key attributes and the key data. */
484+
if( ! check_key_attributes( handle, &attributes ) )
485+
goto exit;
448486
PSA_ASSERT( psa_export_key( handle,
449487
exported, sizeof( exported ),
450488
&exported_length ) );

0 commit comments

Comments
 (0)