Skip to content

Commit d99cc8e

Browse files
modules: openthread: Fix ECDSA issue in OpenThread.
To keep backward compatibility, we must use deterministic ECDSA and convert it to pure ECDSA for KMU only. Also, once KMU can be used to store OpenThread crypto materials, we can allow disabling MBEDTLS_PSA_CRYPTO_STORAGE_C by the application, so changed select to imply. Signed-off-by: Arkadiusz Balys <[email protected]>
1 parent aa2a3c4 commit d99cc8e

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

modules/openthread/platform/crypto_psa.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ static otError getKeyRef(otCryptoKeyRef *aInputKeyRef, psa_key_attributes_t *aAt
104104
if (psa_get_key_algorithm(aAttributes) == 0) {
105105
psa_set_key_algorithm(aAttributes, PSA_ALG_HMAC(PSA_ALG_SHA_256));
106106
}
107+
108+
/* KMU does not support deterministic ECDSA, so we need to set it to
109+
* PSA_ALG_ECDSA(PSA_ALG_SHA_256).
110+
* To keep backward compatibility with the previous functionality we must
111+
* leave PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256) for the ITS purposes.
112+
*/
113+
if (psa_get_key_algorithm(aAttributes) ==
114+
PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256)) {
115+
psa_set_key_algorithm(aAttributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
116+
}
107117
}
108118
#endif /* CONFIG_OPENTHREAD_PSA_NVM_BACKEND */
109119

@@ -680,9 +690,14 @@ otError otPlatCryptoEcdsaSignUsingKeyRef(otCryptoKeyRef aKeyRef,
680690

681691
GET_KEY_REF(&aKeyRef, NULL);
682692

683-
status = psa_sign_hash(aKeyRef, PSA_ALG_ECDSA(PSA_ALG_SHA_256), aHash->m8,
684-
OT_CRYPTO_SHA256_HASH_SIZE, aSignature->m8,
685-
OT_CRYPTO_ECDSA_SIGNATURE_SIZE, &signature_length);
693+
#if defined(CONFIG_OPENTHREAD_PSA_NVM_BACKEND_KMU)
694+
psa_algorithm_t algorithm = PSA_ALG_ECDSA(PSA_ALG_SHA_256);
695+
#else
696+
psa_algorithm_t algorithm = PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256);
697+
#endif
698+
699+
status = psa_sign_hash(aKeyRef, algorithm, aHash->m8, OT_CRYPTO_SHA256_HASH_SIZE,
700+
aSignature->m8, OT_CRYPTO_ECDSA_SIGNATURE_SIZE, &signature_length);
686701
if (status != PSA_SUCCESS) {
687702
goto out;
688703
}
@@ -700,7 +715,13 @@ otError otPlatCryptoEcdsaVerifyUsingKeyRef(otCryptoKeyRef aKeyRef,
700715

701716
GET_KEY_REF(&aKeyRef, NULL);
702717

703-
status = psa_verify_hash(aKeyRef, PSA_ALG_ECDSA(PSA_ALG_SHA_256), aHash->m8,
718+
#if defined(CONFIG_OPENTHREAD_PSA_NVM_BACKEND_KMU)
719+
psa_algorithm_t algorithm = PSA_ALG_ECDSA(PSA_ALG_SHA_256);
720+
#else
721+
psa_algorithm_t algorithm = PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256);
722+
#endif
723+
724+
status = psa_verify_hash(aKeyRef, algorithm, aHash->m8,
704725
OT_CRYPTO_SHA256_HASH_SIZE, aSignature->m8,
705726
OT_CRYPTO_ECDSA_SIGNATURE_SIZE);
706727
if (status != PSA_SUCCESS) {
@@ -739,14 +760,15 @@ otError otPlatCryptoEcdsaGenerateAndImportKey(otCryptoKeyRef aKeyRef)
739760
psa_key_id_t key_id = (psa_key_id_t)aKeyRef;
740761

741762
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_HASH);
742-
psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
763+
psa_set_key_algorithm(&attributes, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256));
743764
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
744765
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT);
745766
psa_set_key_bits(&attributes, 256);
746767

747768
GET_KEY_REF(&key_id, &attributes);
748769

749770
psa_set_key_id(&attributes, key_id);
771+
750772
status = psa_generate_key(&attributes, &key_id);
751773
if (status != PSA_SUCCESS) {
752774
goto out;

subsys/net/openthread/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ config OPENTHREAD_NRF_SECURITY_PSA
6363
functions if available as well as fast oberon backend for software encryption.
6464

6565
config OPENTHREAD_NRF_SECURITY_PSA
66-
select MBEDTLS_PSA_CRYPTO_STORAGE_C if (!PSA_SSF_CRYPTO_CLIENT && !BUILD_WITH_TFM)
66+
imply MBEDTLS_PSA_CRYPTO_STORAGE_C if (!PSA_SSF_CRYPTO_CLIENT && !BUILD_WITH_TFM)
6767
imply TRUSTED_STORAGE if (!PSA_SSF_CRYPTO_CLIENT && !BUILD_WITH_TFM)
6868
# TRUSTED_STORAGE requires Settings
6969
imply SETTINGS

0 commit comments

Comments
 (0)