Skip to content

Commit f5b76a8

Browse files
committed
add wolfHSM support
1 parent fc3185b commit f5b76a8

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

src/image.c

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ static void wolfBoot_verify_signature_rsa_pss(uint8_t key_slot,
584584
word32 inOutIdx = 0;
585585
struct RsaKey rsa;
586586

587+
(void)inOutIdx;
588+
587589
#if defined(WOLFBOOT_HASH_SHA256)
588590
enum wc_HashType hash_type = WC_HASH_TYPE_SHA256;
589591
int mgf = WC_MGF1SHA256;
@@ -594,14 +596,19 @@ static void wolfBoot_verify_signature_rsa_pss(uint8_t key_slot,
594596
#error "RSA-PSS requires SHA-256 or SHA-384"
595597
#endif
596598

599+
#if (!defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) && \
600+
!defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)) || \
601+
(defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) && \
602+
!defined(WOLFBOOT_USE_WOLFHSM_PUBKEY_ID))
597603
uint8_t *pubkey = keystore_get_buffer(key_slot);
598604
int pubkey_sz = keystore_get_size(key_slot);
599605

600606
if (pubkey == NULL || pubkey_sz < 0) {
601607
return;
602608
}
609+
#endif
603610

604-
/* wolfCrypt software RSA-PSS verify (two-step)
611+
/* RSA-PSS verify (two-step)
605612
*
606613
* Step 1 (RSA_VERIFY_FN): wc_RsaPSS_VerifyInline performs the RSA
607614
* operation and PSS unmasking, returning a pointer to the PSS data and
@@ -610,6 +617,78 @@ static void wolfBoot_verify_signature_rsa_pss(uint8_t key_slot,
610617
* Step 2 (RSA_PSS_VERIFY_HASH): wc_RsaPSS_CheckPadding verifies the PSS
611618
* padding against img->sha_hash. Returns 0 on success. Both steps are
612619
* armored when WOLFBOOT_ARMORED is enabled. */
620+
#if defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) || \
621+
defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)
622+
ret = wc_InitRsaKey_ex(&rsa, NULL, hsmDevIdPubKey);
623+
if (ret != 0) {
624+
return;
625+
}
626+
#if defined(WOLFBOOT_USE_WOLFHSM_PUBKEY_ID) || \
627+
(defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) && \
628+
defined(WOLFBOOT_CERT_CHAIN_VERIFY))
629+
(void)key_slot;
630+
/* public key is stored on server at hsmKeyIdPubKey*/
631+
#if defined(WOLFBOOT_CERT_CHAIN_VERIFY)
632+
/* If using certificate chain verification and we have a verified leaf key
633+
* ID */
634+
if (g_leafKeyIdValid) {
635+
/* Use the leaf key ID from certificate verification */
636+
#if defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT)
637+
ret = wh_Client_RsaSetKeyId(&rsa, g_certLeafKeyId);
638+
#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)
639+
ret = wh_Server_CacheExportRsaKey(&hsmServerCtx, g_certLeafKeyId, &rsa);
640+
#endif
641+
wolfBoot_printf(
642+
"Using leaf cert public key (ID: %08x) for RSA-PSS verification\n",
643+
(unsigned int)g_certLeafKeyId);
644+
}
645+
else {
646+
#if defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT)
647+
/* Default behavior: use the pre-configured public key ID */
648+
ret = wh_Client_RsaSetKeyId(&rsa, hsmKeyIdPubKey);
649+
#endif
650+
}
651+
#else
652+
ret = wh_Client_RsaSetKeyId(&rsa, hsmKeyIdPubKey);
653+
#endif
654+
if (ret != 0) {
655+
return;
656+
}
657+
#else
658+
whKeyId hsmKeyId = WH_KEYID_ERASED;
659+
/* Cache the public key on the server */
660+
ret = wh_Client_KeyCache(&hsmClientCtx, WH_NVM_FLAGS_USAGE_VERIFY, NULL, 0,
661+
pubkey, pubkey_sz, &hsmKeyId);
662+
if (ret != WH_ERROR_OK) {
663+
return;
664+
}
665+
/* Associate this RSA struct with the keyId of the cached key */
666+
ret = wh_Client_RsaSetKeyId(&rsa, hsmKeyId);
667+
if (ret != WH_ERROR_OK) {
668+
return;
669+
}
670+
#endif /* !WOLFBOOT_USE_WOLFHSM_PUBKEY_ID */
671+
XMEMCPY(output, sig, RSA_IMAGE_SIGNATURE_SIZE);
672+
RSA_VERIFY_FN(ret, wc_RsaPSS_VerifyInline, output, RSA_IMAGE_SIGNATURE_SIZE,
673+
&digest_out, hash_type, mgf, &rsa);
674+
#if defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) && \
675+
!defined(WOLFBOOT_USE_WOLFHSM_PUBKEY_ID)
676+
/* evict the key after use, since we aren't using the RSA import API */
677+
if (WH_ERROR_OK != wh_Client_KeyEvict(&hsmClientCtx, hsmKeyId)) {
678+
return;
679+
}
680+
#elif defined(WOLFBOOT_CERT_CHAIN_VERIFY)
681+
if (g_leafKeyIdValid) {
682+
#if defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT)
683+
(void)wh_Client_KeyEvict(&hsmClientCtx, g_certLeafKeyId);
684+
#elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)
685+
(void)wh_Server_KeystoreEvictKey(&hsmServerCtx, g_certLeafKeyId);
686+
#endif
687+
g_leafKeyIdValid = 0;
688+
}
689+
#endif /* !WOLFBOOT_USE_WOLFHSM_PUBKEY_ID */
690+
#else
691+
/* wolfCrypt software RSA-PSS verify */
613692
ret = wc_InitRsaKey(&rsa, NULL);
614693
if (ret == 0) {
615694
/* Import public key */
@@ -621,6 +700,7 @@ static void wolfBoot_verify_signature_rsa_pss(uint8_t key_slot,
621700
&digest_out, hash_type, mgf, &rsa);
622701
}
623702
}
703+
#endif /* WOLFBOOT_ENABLE_WOLFHSM */
624704
wc_FreeRsaKey(&rsa);
625705
if (ret >= WOLFBOOT_SHA_DIGEST_SIZE && img && digest_out) {
626706
RSA_PSS_VERIFY_HASH(img, digest_out, ret, hash_type);

0 commit comments

Comments
 (0)