Skip to content

Commit 64e726a

Browse files
Przemyslaw Bidarlubos
Przemyslaw Bida
authored andcommitted
[nrf fromtree] net: openthread: Add implementation of crypto api.
This commit adds implementation of following new api functions from openthread: - otPlatCryptoEcdsaGenerateAndImportKey - otPlatCryptoEcdsaExportPublicKey - otPlatCryptoEcdsaVerifyUsingKeyRef - otPlatCryptoEcdsaSignUsingKeyRef Signed-off-by: Przemyslaw Bida <[email protected]> (cherry picked form commit f93613a)
1 parent 60bdd8a commit 64e726a

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

modules/openthread/platform/crypto_psa.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,4 +563,83 @@ otError otPlatCryptoEcdsaVerify(const otPlatCryptoEcdsaPublicKey *aPublicKey,
563563
return psaToOtError(status);
564564
}
565565

566+
otError otPlatCryptoEcdsaSignUsingKeyRef(otCryptoKeyRef aKeyRef,
567+
const otPlatCryptoSha256Hash *aHash,
568+
otPlatCryptoEcdsaSignature *aSignature)
569+
{
570+
psa_status_t status;
571+
size_t signature_length;
572+
573+
status = psa_sign_hash(aKeyRef, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256), aHash->m8,
574+
OT_CRYPTO_SHA256_HASH_SIZE, aSignature->m8,
575+
OT_CRYPTO_ECDSA_SIGNATURE_SIZE, &signature_length);
576+
if (status != PSA_SUCCESS) {
577+
goto out;
578+
}
579+
580+
__ASSERT_NO_MSG(signature_length == OT_CRYPTO_ECDSA_SIGNATURE_SIZE);
581+
out:
582+
return psaToOtError(status);
583+
}
584+
585+
otError otPlatCryptoEcdsaVerifyUsingKeyRef(otCryptoKeyRef aKeyRef,
586+
const otPlatCryptoSha256Hash *aHash,
587+
const otPlatCryptoEcdsaSignature *aSignature)
588+
{
589+
psa_status_t status;
590+
591+
status = psa_verify_hash(aKeyRef, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256), aHash->m8,
592+
OT_CRYPTO_SHA256_HASH_SIZE, aSignature->m8,
593+
OT_CRYPTO_ECDSA_SIGNATURE_SIZE);
594+
if (status != PSA_SUCCESS) {
595+
goto out;
596+
}
597+
598+
out:
599+
return psaToOtError(status);
600+
}
601+
602+
otError otPlatCryptoEcdsaExportPublicKey(otCryptoKeyRef aKeyRef,
603+
otPlatCryptoEcdsaPublicKey *aPublicKey)
604+
{
605+
psa_status_t status;
606+
size_t exported_length;
607+
uint8_t buffer[1 + OT_CRYPTO_ECDSA_PUBLIC_KEY_SIZE];
608+
609+
status = psa_export_public_key(aKeyRef, buffer, sizeof(buffer), &exported_length);
610+
if (status != PSA_SUCCESS) {
611+
goto out;
612+
}
613+
614+
__ASSERT_NO_MSG(exported_length == sizeof(buffer));
615+
memcpy(aPublicKey->m8, buffer + 1, OT_CRYPTO_ECDSA_PUBLIC_KEY_SIZE);
616+
617+
out:
618+
return psaToOtError(status);
619+
}
620+
621+
otError otPlatCryptoEcdsaGenerateAndImportKey(otCryptoKeyRef aKeyRef)
622+
{
623+
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
624+
psa_status_t status;
625+
psa_key_id_t key_id = (psa_key_id_t)aKeyRef;
626+
627+
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_HASH);
628+
psa_set_key_algorithm(&attributes, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256));
629+
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
630+
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT);
631+
psa_set_key_id(&attributes, key_id);
632+
psa_set_key_bits(&attributes, 256);
633+
634+
status = psa_generate_key(&attributes, &key_id);
635+
if (status != PSA_SUCCESS) {
636+
goto out;
637+
}
638+
639+
out:
640+
psa_reset_key_attributes(&attributes);
641+
642+
return psaToOtError(status);
643+
}
644+
566645
#endif /* #if CONFIG_OPENTHREAD_ECDSA */

0 commit comments

Comments
 (0)