Skip to content

Commit 166f7cc

Browse files
committed
unify rsa and rsa-pss into single verify method
1 parent f5b76a8 commit 166f7cc

File tree

3 files changed

+76
-156
lines changed

3 files changed

+76
-156
lines changed

include/image.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ extern "C" {
122122
#define wolfBoot_verify_signature_secondary wolfBoot_verify_signature_ml_dsa
123123
#endif
124124

125+
/* Thin wrappers: dispatch RSA / RSA-PSS to the unified verify function */
126+
#define wolfBoot_verify_signature_rsa(ks, img, sig) \
127+
wolfBoot_verify_signature_rsa_common(ks, img, sig, 0)
128+
#define wolfBoot_verify_signature_rsa_pss(ks, img, sig) \
129+
wolfBoot_verify_signature_rsa_common(ks, img, sig, 1)
130+
125131
#if defined(WOLFBOOT_TPM) && defined (WOLFBOOT_TPM_VERIFY)
126132
#undef wolfBoot_verify_signature_primary
127133
#define wolfBoot_verify_signature_primary wolfBoot_verify_signature_tpm

include/loader.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ extern "C" {
4040
#define ECC_IMAGE_SIGNATURE_SIZE (132)
4141
#endif
4242

43+
/* Consolidated RSA-PSS flag: set when any RSA-PSS variant is enabled
44+
* (primary or secondary). Used to guard PSS-specific code paths in the
45+
* unified RSA verify function. */
46+
#if defined(WOLFBOOT_SIGN_RSAPSS2048) || defined(WOLFBOOT_SIGN_RSAPSS3072) || \
47+
defined(WOLFBOOT_SIGN_RSAPSS4096) || \
48+
defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS2048) || \
49+
defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS3072) || \
50+
defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS4096)
51+
#define WOLFBOOT_RSA_PSS
52+
#endif
53+
4354
#if defined(WOLFBOOT_SIGN_RSA2048) || defined(WOLFBOOT_SIGN_SECONDARY_RSA2048) || \
4455
defined(WOLFBOOT_SIGN_RSAPSS2048) || defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS2048)
4556
#define RSA_IMAGE_SIGNATURE_SIZE (256)

src/image.c

Lines changed: 59 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ static int RsaDecodeSignature(uint8_t** pInput, int inputSz)
430430
}
431431
#endif /* !NO_RSA_SIG_ENCODING */
432432

433-
static void wolfBoot_verify_signature_rsa(uint8_t key_slot,
434-
struct wolfBoot_image *img, uint8_t *sig)
433+
static void wolfBoot_verify_signature_rsa_common(uint8_t key_slot,
434+
struct wolfBoot_image *img, uint8_t *sig, int is_pss)
435435
{
436436
int ret;
437437
uint8_t output[RSA_IMAGE_SIGNATURE_SIZE];
@@ -440,6 +440,21 @@ static void wolfBoot_verify_signature_rsa(uint8_t key_slot,
440440
struct RsaKey rsa;
441441

442442
(void)inOutIdx;
443+
(void)is_pss;
444+
445+
#ifdef WOLFBOOT_RSA_PSS
446+
enum wc_HashType hash_type;
447+
int mgf;
448+
#if defined(WOLFBOOT_HASH_SHA256)
449+
hash_type = WC_HASH_TYPE_SHA256;
450+
mgf = WC_MGF1SHA256;
451+
#elif defined(WOLFBOOT_HASH_SHA384)
452+
hash_type = WC_HASH_TYPE_SHA384;
453+
mgf = WC_MGF1SHA384;
454+
#else
455+
#error "RSA-PSS requires SHA-256 or SHA-384"
456+
#endif
457+
#endif /* WOLFBOOT_RSA_PSS */
443458

444459
#if (!defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) && \
445460
!defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)) || \
@@ -456,6 +471,7 @@ static void wolfBoot_verify_signature_rsa(uint8_t key_slot,
456471
#if defined(WOLFBOOT_RENESAS_SCEPROTECT) || \
457472
defined(WOLFBOOT_RENESAS_TSIP) || \
458473
defined(WOLFBOOT_RENESAS_RSIP)
474+
/* Renesas crypto callback — RSA PKCS#1 v1.5 only */
459475
ret = wc_InitRsaKey_ex(&rsa, NULL, RENESAS_DEVID);
460476
if (ret == 0) {
461477
XMEMCPY(output, sig, RSA_IMAGE_SIGNATURE_SIZE);
@@ -519,8 +535,17 @@ static void wolfBoot_verify_signature_rsa(uint8_t key_slot,
519535
}
520536
#endif /* !WOLFBOOT_USE_WOLFHSM_PUBKEY_ID */
521537
XMEMCPY(output, sig, RSA_IMAGE_SIGNATURE_SIZE);
522-
RSA_VERIFY_FN(ret, wc_RsaSSL_VerifyInline, output, RSA_IMAGE_SIGNATURE_SIZE,
523-
&digest_out, &rsa);
538+
#ifdef WOLFBOOT_RSA_PSS
539+
if (is_pss) {
540+
RSA_VERIFY_FN(ret, wc_RsaPSS_VerifyInline, output,
541+
RSA_IMAGE_SIGNATURE_SIZE, &digest_out, hash_type, mgf,
542+
&rsa);
543+
} else
544+
#endif
545+
{
546+
RSA_VERIFY_FN(ret, wc_RsaSSL_VerifyInline, output,
547+
RSA_IMAGE_SIGNATURE_SIZE, &digest_out, &rsa);
548+
}
524549
#if defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) && \
525550
!defined(WOLFBOOT_USE_WOLFHSM_PUBKEY_ID)
526551
/* evict the key after use, since we aren't using the RSA import API */
@@ -545,170 +570,48 @@ static void wolfBoot_verify_signature_rsa(uint8_t key_slot,
545570
ret = wc_RsaPublicKeyDecode((byte*)pubkey, &inOutIdx, &rsa, pubkey_sz);
546571
if (ret >= 0) {
547572
XMEMCPY(output, sig, RSA_IMAGE_SIGNATURE_SIZE);
548-
RSA_VERIFY_FN(ret,
549-
wc_RsaSSL_VerifyInline, output, RSA_IMAGE_SIGNATURE_SIZE,
550-
&digest_out, &rsa);
573+
#ifdef WOLFBOOT_RSA_PSS
574+
if (is_pss) {
575+
RSA_VERIFY_FN(ret,
576+
wc_RsaPSS_VerifyInline, output, RSA_IMAGE_SIGNATURE_SIZE,
577+
&digest_out, hash_type, mgf, &rsa);
578+
} else
579+
#endif
580+
{
581+
RSA_VERIFY_FN(ret,
582+
wc_RsaSSL_VerifyInline, output, RSA_IMAGE_SIGNATURE_SIZE,
583+
&digest_out, &rsa);
584+
}
551585
}
552586
}
553587
#endif /* SCE || TSIP */
554588
wc_FreeRsaKey(&rsa);
555589

590+
#ifdef WOLFBOOT_RSA_PSS
591+
if (is_pss) {
592+
if (ret >= WOLFBOOT_SHA_DIGEST_SIZE && img && digest_out) {
593+
RSA_PSS_VERIFY_HASH(img, digest_out, ret, hash_type);
594+
}
595+
} else
596+
#endif
597+
{
556598
#ifndef NO_RSA_SIG_ENCODING
557-
if (ret > WOLFBOOT_SHA_DIGEST_SIZE) {
558-
/* larger result indicates it might have an ASN.1 encoded header */
559-
ret = RsaDecodeSignature(&digest_out, ret);
560-
}
599+
if (ret > WOLFBOOT_SHA_DIGEST_SIZE) {
600+
/* larger result indicates it might have an ASN.1 encoded header */
601+
ret = RsaDecodeSignature(&digest_out, ret);
602+
}
561603
#endif
562-
if (ret == WOLFBOOT_SHA_DIGEST_SIZE && img && digest_out) {
563-
RSA_VERIFY_HASH(img, digest_out);
604+
if (ret == WOLFBOOT_SHA_DIGEST_SIZE && img && digest_out) {
605+
RSA_VERIFY_HASH(img, digest_out);
606+
}
564607
}
565608
}
566609

567610
#endif /* WOLFBOOT_SIGN_RSA2048 || WOLFBOOT_SIGN_RSA3072 || \
568611
* WOLFBOOT_SIGN_RSA4096 || WOLFBOOT_SIGN_SECONDARY_RSA2048 ||
569-
* WOLFBOOT_SIGN_SECONDARY_RSA3072 || WOLFBOOT_SIGN_SECONDARY_RSA4096 */
570-
571-
#if defined(WOLFBOOT_SIGN_RSAPSS2048) || \
572-
defined(WOLFBOOT_SIGN_RSAPSS3072) || \
573-
defined(WOLFBOOT_SIGN_RSAPSS4096) || \
574-
defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS2048) || \
575-
defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS3072) || \
576-
defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS4096)
577-
578-
static void wolfBoot_verify_signature_rsa_pss(uint8_t key_slot,
579-
struct wolfBoot_image *img, uint8_t *sig)
580-
{
581-
int ret;
582-
uint8_t output[RSA_IMAGE_SIGNATURE_SIZE];
583-
uint8_t* digest_out = NULL;
584-
word32 inOutIdx = 0;
585-
struct RsaKey rsa;
586-
587-
(void)inOutIdx;
588-
589-
#if defined(WOLFBOOT_HASH_SHA256)
590-
enum wc_HashType hash_type = WC_HASH_TYPE_SHA256;
591-
int mgf = WC_MGF1SHA256;
592-
#elif defined(WOLFBOOT_HASH_SHA384)
593-
enum wc_HashType hash_type = WC_HASH_TYPE_SHA384;
594-
int mgf = WC_MGF1SHA384;
595-
#else
596-
#error "RSA-PSS requires SHA-256 or SHA-384"
597-
#endif
598-
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))
603-
uint8_t *pubkey = keystore_get_buffer(key_slot);
604-
int pubkey_sz = keystore_get_size(key_slot);
605-
606-
if (pubkey == NULL || pubkey_sz < 0) {
607-
return;
608-
}
609-
#endif
610-
611-
/* RSA-PSS verify (two-step)
612-
*
613-
* Step 1 (RSA_VERIFY_FN): wc_RsaPSS_VerifyInline performs the RSA
614-
* operation and PSS unmasking, returning a pointer to the PSS data and
615-
* its length.
616-
*
617-
* Step 2 (RSA_PSS_VERIFY_HASH): wc_RsaPSS_CheckPadding verifies the PSS
618-
* padding against img->sha_hash. Returns 0 on success. Both steps are
619-
* 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 */
692-
ret = wc_InitRsaKey(&rsa, NULL);
693-
if (ret == 0) {
694-
/* Import public key */
695-
ret = wc_RsaPublicKeyDecode((byte*)pubkey, &inOutIdx, &rsa, pubkey_sz);
696-
if (ret >= 0) {
697-
XMEMCPY(output, sig, RSA_IMAGE_SIGNATURE_SIZE);
698-
RSA_VERIFY_FN(ret,
699-
wc_RsaPSS_VerifyInline, output, RSA_IMAGE_SIGNATURE_SIZE,
700-
&digest_out, hash_type, mgf, &rsa);
701-
}
702-
}
703-
#endif /* WOLFBOOT_ENABLE_WOLFHSM */
704-
wc_FreeRsaKey(&rsa);
705-
if (ret >= WOLFBOOT_SHA_DIGEST_SIZE && img && digest_out) {
706-
RSA_PSS_VERIFY_HASH(img, digest_out, ret, hash_type);
707-
}
708-
}
709-
710-
#endif /* WOLFBOOT_SIGN_RSAPSS2048 || WOLFBOOT_SIGN_RSAPSS3072 || \
711-
* WOLFBOOT_SIGN_RSAPSS4096 || WOLFBOOT_SIGN_SECONDARY_RSAPSS2048 || \
612+
* WOLFBOOT_SIGN_SECONDARY_RSA3072 || WOLFBOOT_SIGN_SECONDARY_RSA4096 ||
613+
* WOLFBOOT_SIGN_RSAPSS2048 || WOLFBOOT_SIGN_RSAPSS3072 ||
614+
* WOLFBOOT_SIGN_RSAPSS4096 || WOLFBOOT_SIGN_SECONDARY_RSAPSS2048 ||
712615
* WOLFBOOT_SIGN_SECONDARY_RSAPSS3072 || WOLFBOOT_SIGN_SECONDARY_RSAPSS4096 */
713616

714617
#ifdef WOLFBOOT_SIGN_LMS

0 commit comments

Comments
 (0)