Skip to content

Commit 1d8a74c

Browse files
degjorvanordicjm
authored andcommitted
nrf_security: cracen: kmu: Fix ecdsa public key import
The ecdsa public key is 65 bytes long, but the kmu only supported keys up to 32 bytes. The first byte is Compact Binary Representation where only one option is supported so this is removed before storing and added back in when retrieved This means we only use 4 slots instead of 5 to store the key Fix bug in psa_crypto_driver_wrappers which meant kmu keys could not be used for sign and verify hash. Add support for storting ecdsa keys using sha256 to the kmu Signed-off-by: Dag Erik Gjørvad <[email protected]>
1 parent ffa68f5 commit 1d8a74c

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ static psa_status_t convert_from_psa_attributes(const psa_key_attributes_t *key_
712712
break;
713713

714714
case PSA_ALG_ECDSA(PSA_ALG_ANY_HASH):
715+
case PSA_ALG_ECDSA(PSA_ALG_SHA_256):
715716
if (PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(key_attr)) !=
716717
PSA_ECC_FAMILY_SECP_R1) {
717718
return PSA_ERROR_NOT_SUPPORTED;
@@ -836,7 +837,15 @@ psa_status_t cracen_kmu_provision(const psa_key_attributes_t *key_attr, int slot
836837
#endif /* PSA_NEED_CRACEN_KMU_ENCRYPTED_KEYS */
837838
case CRACEN_KMU_KEY_USAGE_SCHEME_RAW:
838839
push_address = (uint8_t *)kmu_push_area;
839-
if (key_buffer_size != 16 && key_buffer_size != 24 && key_buffer_size != 32) {
840+
if (key_buffer_size == 65) {
841+
/* ECDSA public keys are 65 bytes, but the first byte is CBR and compressed
842+
* points are not supported so the first byte is removed here and appended
843+
* when retrieved
844+
*/
845+
key_buffer++;
846+
key_buffer_size--;
847+
} else if (key_buffer_size != 16 && key_buffer_size != 24 &&
848+
key_buffer_size != 32) {
840849
return PSA_ERROR_INVALID_ARGUMENT;
841850
}
842851
break;
@@ -1013,6 +1022,12 @@ psa_status_t cracen_kmu_get_builtin_key(psa_drv_slot_number_t slot_number,
10131022
}
10141023

10151024
if (key_buffer_size >= opaque_key_size) {
1025+
if (psa_get_key_type(attributes) ==
1026+
PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1)) {
1027+
*key_buffer = SI_ECC_PUBKEY_UNCOMPRESSED;
1028+
key_buffer++;
1029+
key_buffer_size--;
1030+
}
10161031
*key_buffer_length = opaque_key_size;
10171032
kmu_opaque_key_buffer *key = (kmu_opaque_key_buffer *)key_buffer;
10181033
unsigned int slot_count;
@@ -1032,12 +1047,6 @@ psa_status_t cracen_kmu_get_builtin_key(psa_drv_slot_number_t slot_number,
10321047

10331048
/* ECC keys are getting loading into the key buffer like volatile keys */
10341049
if (PSA_KEY_TYPE_IS_ECC(psa_get_key_type(attributes))) {
1035-
if (psa_get_key_type(attributes) ==
1036-
PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1)) {
1037-
*key_buffer = SI_ECC_PUBKEY_UNCOMPRESSED;
1038-
key_buffer++;
1039-
key_buffer_size--;
1040-
}
10411050
return push_kmu_key_to_ram(key_buffer, key_buffer_size);
10421051
}
10431052

subsys/nrf_security/src/psa_crypto_driver_wrappers.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ psa_status_t psa_driver_wrapper_sign_hash(const psa_key_attributes_t *attributes
330330

331331
#if defined(PSA_NEED_CRACEN_ASYMMETRIC_SIGNATURE_DRIVER)
332332
case PSA_KEY_LOCATION_CRACEN:
333+
#if defined(PSA_NEED_CRACEN_KMU_DRIVER)
334+
case PSA_KEY_LOCATION_CRACEN_KMU:
335+
#endif /* PSA_NEED_CRACEN_KMU_DRIVER */
333336
status = cracen_sign_hash(attributes, key_buffer, key_buffer_size, alg, hash,
334337
hash_length, signature, signature_size, signature_length);
335338
/* Declared with fallback == true */
@@ -391,6 +394,9 @@ psa_status_t psa_driver_wrapper_verify_hash(const psa_key_attributes_t *attribut
391394
*/
392395
#if defined(PSA_NEED_CRACEN_ASYMMETRIC_SIGNATURE_DRIVER)
393396
case PSA_KEY_LOCATION_CRACEN:
397+
#if defined(PSA_NEED_CRACEN_KMU_DRIVER)
398+
case PSA_KEY_LOCATION_CRACEN_KMU:
399+
#endif /* PSA_NEED_CRACEN_KMU_DRIVER */
394400
status = cracen_verify_hash(attributes, key_buffer, key_buffer_size, alg, hash,
395401
hash_length, signature, signature_length);
396402

0 commit comments

Comments
 (0)