Skip to content

Commit f41c3a2

Browse files
frkvbjarki-andreasen
authored andcommitted
crypto: CRACEN: KMU: Adding CRACEN_KEY_PERSISTENCE_READ_ONLY
-Adding CRACEN_KEY_PERSISTENCE_READ_ONLY to support importing keys that are read-only in KMU. These can never be erased without running ERASEALL (if ERASEPROTECT is not set) -Changing CRACEN identity key and MKEK/MEXT to use this type for consistency -Changed conversion of LIB_KMU_REV_POLICY_LOCKED from PSA_KEY_PERSISTENCE_READ_ONLY to CRACEN_KEY_PERSISTENCE_READ_ONLY -Updated cracen_kmu_get_key_slot to support all rpolicies ref: NCSDK-32134 Signed-off-by: Frank Audun Kvamtrø <[email protected]>
1 parent 55e280f commit f41c3a2

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

subsys/nrf_security/src/drivers/cracen/cracenpsa/include/cracen_psa_key_ids.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@
2525
*/
2626
#define CRACEN_KEY_PERSISTENCE_REVOKABLE 0x02
2727

28+
/*
29+
* Defines a persistence state where the key can't be erased
30+
* In this state the key will only be erased if ERASEALL is available and run
31+
*/
32+
#define CRACEN_KEY_PERSISTENCE_READ_ONLY 0x03
33+
2834
#endif

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/key_management.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ psa_status_t cracen_get_builtin_key(psa_drv_slot_number_t slot_number,
12021202
switch (slot_number) {
12031203
case CRACEN_IDENTITY_KEY_SLOT_NUMBER:
12041204
psa_set_key_lifetime(attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
1205-
PSA_KEY_PERSISTENCE_READ_ONLY,
1205+
CRACEN_KEY_PERSISTENCE_READ_ONLY,
12061206
PSA_KEY_LOCATION_CRACEN));
12071207
psa_set_key_type(attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
12081208
psa_set_key_bits(attributes, 256);
@@ -1236,7 +1236,7 @@ psa_status_t cracen_get_builtin_key(psa_drv_slot_number_t slot_number,
12361236
case CRACEN_MKEK_SLOT_NUMBER:
12371237
case CRACEN_MEXT_SLOT_NUMBER:
12381238
psa_set_key_lifetime(attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
1239-
PSA_KEY_PERSISTENCE_READ_ONLY,
1239+
CRACEN_KEY_PERSISTENCE_READ_ONLY,
12401240
PSA_KEY_LOCATION_CRACEN));
12411241
psa_set_key_type(attributes, PSA_KEY_TYPE_AES);
12421242
psa_set_key_bits(attributes, 256);
@@ -1299,7 +1299,7 @@ psa_status_t mbedtls_psa_platform_get_builtin_key(mbedtls_svc_key_id_t key_id,
12991299
#endif
13001300
};
13011301

1302-
*lifetime = PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(PSA_KEY_PERSISTENCE_READ_ONLY,
1302+
*lifetime = PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(CRACEN_KEY_PERSISTENCE_READ_ONLY,
13031303
PSA_KEY_LOCATION_CRACEN);
13041304

13051305
return PSA_SUCCESS;

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/kmu.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static psa_status_t get_encryption_key(const uint8_t *context, uint8_t *key)
117117
psa_set_key_id(&mkek_attr, mbedtls_svc_key_id_make(0, CRACEN_BUILTIN_MKEK_ID));
118118
psa_set_key_lifetime(&mkek_attr,
119119
PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
120-
PSA_KEY_PERSISTENCE_READ_ONLY, PSA_KEY_LOCATION_CRACEN));
120+
CRACEN_KEY_PERSISTENCE_READ_ONLY, PSA_KEY_LOCATION_CRACEN));
121121

122122
cracen_key_derivation_operation_t op = {};
123123

@@ -418,13 +418,19 @@ static psa_status_t get_kmu_slot_id_and_count(const psa_key_attributes_t *key_at
418418

419419
psa_status_t cracen_kmu_destroy_key(const psa_key_attributes_t *attributes)
420420
{
421-
psa_key_location_t location =
422-
PSA_KEY_LIFETIME_GET_LOCATION(psa_get_key_lifetime(attributes));
421+
psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
422+
423+
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(lifetime);
424+
psa_key_persistence_t persistence = PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime);
423425

424426
if (location == PSA_KEY_LOCATION_CRACEN_KMU) {
425427
psa_status_t status;
426428
unsigned int slot_id, slot_count;
427429

430+
if (persistence == CRACEN_KEY_PERSISTENCE_READ_ONLY) {
431+
return PSA_ERROR_NOT_PERMITTED;
432+
}
433+
428434
status = get_kmu_slot_id_and_count(attributes, &slot_id, &slot_count);
429435
if (status != PSA_SUCCESS) {
430436
return status;
@@ -458,7 +464,7 @@ static psa_status_t convert_to_psa_attributes(kmu_metadata *metadata,
458464
key_persistence = CRACEN_KEY_PERSISTENCE_REVOKABLE;
459465
break;
460466
case LIB_KMU_REV_POLICY_LOCKED:
461-
key_persistence = PSA_KEY_PERSISTENCE_READ_ONLY;
467+
key_persistence = CRACEN_KEY_PERSISTENCE_READ_ONLY;
462468
break;
463469
default:
464470
return PSA_ERROR_STORAGE_FAILURE;
@@ -783,6 +789,7 @@ static psa_status_t convert_from_psa_attributes(const psa_key_attributes_t *key_
783789

784790
switch (PSA_KEY_LIFETIME_GET_PERSISTENCE(psa_get_key_lifetime(key_attr))) {
785791
case PSA_KEY_PERSISTENCE_READ_ONLY:
792+
case CRACEN_KEY_PERSISTENCE_READ_ONLY:
786793
metadata->rpolicy = LIB_KMU_REV_POLICY_LOCKED;
787794
break;
788795
case PSA_KEY_PERSISTENCE_DEFAULT:
@@ -936,17 +943,28 @@ psa_status_t cracen_kmu_get_key_slot(mbedtls_svc_key_id_t key_id, psa_key_lifeti
936943
psa_status_t status;
937944
unsigned int slot_id;
938945
kmu_metadata metadata;
946+
psa_key_persistence_t persistence;
939947

940948
status = get_kmu_slot_id_and_metadata(key_id, &slot_id, &metadata);
941949
if (status != PSA_SUCCESS) {
942950
return status;
943951
}
944952

945-
psa_key_persistence_t read_only = metadata.rpolicy == LIB_KMU_REV_POLICY_ROTATING
946-
? PSA_KEY_PERSISTENCE_DEFAULT
947-
: PSA_KEY_PERSISTENCE_READ_ONLY;
953+
switch (metadata.rpolicy) {
954+
case LIB_KMU_REV_POLICY_ROTATING:
955+
persistence = PSA_KEY_PERSISTENCE_DEFAULT;
956+
break;
957+
case LIB_KMU_REV_POLICY_REVOKED:
958+
persistence = CRACEN_KEY_PERSISTENCE_REVOKABLE;
959+
break;
960+
case LIB_KMU_REV_POLICY_LOCKED:
961+
persistence = CRACEN_KEY_PERSISTENCE_READ_ONLY;
962+
break;
963+
default:
964+
return PSA_ERROR_INVALID_ARGUMENT;
965+
}
948966

949-
*lifetime = PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(read_only,
967+
*lifetime = PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence,
950968
PSA_KEY_LOCATION_CRACEN_KMU);
951969
*slot_number = slot_id;
952970

0 commit comments

Comments
 (0)