Skip to content

Commit 57facf1

Browse files
committed
aux algo support for RSA pss
1 parent 4a54197 commit 57facf1

3 files changed

Lines changed: 66 additions & 21 deletions

File tree

include/user_settings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ extern int tolower(int c);
249249
defined(WOLFBOOT_SIGN_RSAPSS4096) || \
250250
defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS2048) || \
251251
defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS3072) || \
252-
defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS4096)
252+
defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS4096) || \
253+
defined(WOLFBOOT_AUX_RSA_PSS)
253254
# define WC_RSA_PSS
254255
# endif
255256
# if !defined(WOLFBOOT_TPM) && !defined(WOLFCRYPT_SECURE_MODE) && \

options.mk

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,15 @@ ifneq ($(CERT_CHAIN_VERIFY),)
14391439
ifeq ($(SIGN),RSA4096)
14401440
CERT_CHAIN_GEN_ALGO+=rsa4096
14411441
endif
1442+
ifeq ($(SIGN),RSAPSS2048)
1443+
CERT_CHAIN_GEN_ALGO+=rsapss2048
1444+
endif
1445+
ifeq ($(SIGN),RSAPSS3072)
1446+
CERT_CHAIN_GEN_ALGO+=rsapss3072
1447+
endif
1448+
ifeq ($(SIGN),RSAPSS4096)
1449+
CERT_CHAIN_GEN_ALGO+=rsapss4096
1450+
endif
14421451

14431452
# Per-level overrides for the dummy chain generator. Defaults: CA chain
14441453
# uses the same algo as the leaf (SIGN-derived), SHA256 for cert sigs.
@@ -1472,7 +1481,12 @@ endif
14721481
# cert chain verifier auto-populates these (see above), but the variables
14731482
# are also available as a generic primitive for any future feature that
14741483
# needs extra algo support compiled in.
1475-
# Usage: AUX_HASH_ALGOS=sha384,sha512 AUX_PK_ALGOS=rsa4096,ecc256
1484+
# Usage: AUX_HASH_ALGOS=sha384,sha512 AUX_PK_ALGOS=rsa4096,rsapss4096,ecc256
1485+
#
1486+
# RSA tokens are orthogonal along two axes: size (rsa2048/3072/4096) and
1487+
# padding mode (rsa* = PKCS#1 v1.5, rsapss* = PSS). Each rsapssN token
1488+
# enables both the RSAN size and PSS padding; mixing rsaN and rsapssN for
1489+
# the same size accepts either padding for that key length.
14761490
ifneq ($(strip $(AUX_PK_ALGOS)$(AUX_HASH_ALGOS)),)
14771491
comma := ,
14781492
AUX_HASH_ALGOS_LIST := $(sort $(subst $(comma), ,$(AUX_HASH_ALGOS)))
@@ -1500,17 +1514,23 @@ ifneq ($(strip $(AUX_PK_ALGOS)$(AUX_HASH_ALGOS)),)
15001514
endif
15011515

15021516
# --- PK algorithms ---
1503-
ifneq ($(filter rsa2048,$(AUX_PK_ALGOS_LIST)),)
1517+
# RSA size flags - rsa{N} and rsapss{N} both select the same N-bit
1518+
# modulus support; padding is set separately below.
1519+
ifneq ($(filter rsa2048 rsapss2048,$(AUX_PK_ALGOS_LIST)),)
15041520
CFLAGS += -DWOLFBOOT_AUX_PK_RSA2048
15051521
endif
1506-
ifneq ($(filter rsa3072,$(AUX_PK_ALGOS_LIST)),)
1522+
ifneq ($(filter rsa3072 rsapss3072,$(AUX_PK_ALGOS_LIST)),)
15071523
CFLAGS += -DWOLFBOOT_AUX_PK_RSA3072
15081524
endif
1509-
ifneq ($(filter rsa4096,$(AUX_PK_ALGOS_LIST)),)
1525+
ifneq ($(filter rsa4096 rsapss4096,$(AUX_PK_ALGOS_LIST)),)
15101526
CFLAGS += -DWOLFBOOT_AUX_PK_RSA4096
15111527
endif
1512-
# Add RSA objects if any RSA aux PK is requested
1513-
ifneq ($(filter rsa2048 rsa3072 rsa4096,$(AUX_PK_ALGOS_LIST)),)
1528+
# PSS padding - any rsapss* token enables PSS for all selected RSA sizes
1529+
ifneq ($(filter rsapss2048 rsapss3072 rsapss4096,$(AUX_PK_ALGOS_LIST)),)
1530+
CFLAGS += -DWOLFBOOT_AUX_RSA_PSS
1531+
endif
1532+
# Add RSA objects if any RSA (PKCS#1 v1.5 or PSS) aux PK is requested
1533+
ifneq ($(filter rsa2048 rsa3072 rsa4096 rsapss2048 rsapss3072 rsapss4096,$(AUX_PK_ALGOS_LIST)),)
15141534
ifeq ($(filter %/rsa.o,$(WOLFCRYPT_OBJS)),)
15151535
WOLFCRYPT_OBJS += $(RSA_OBJS)
15161536
endif

tools/scripts/sim-gen-dummy-chain.sh

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LEAF_ALGO="" # Defaults to CA_ALGO if not explicitly set
1515
CA_HASH="sha256" # Hash used for cert signatures throughout the chain
1616

1717
# Whitelists
18-
SUPPORTED_ALGOS="ecc256 ecc384 rsa2048 rsa3072 rsa4096"
18+
SUPPORTED_ALGOS="ecc256 ecc384 rsa2048 rsa3072 rsa4096 rsapss2048 rsapss3072 rsapss4096"
1919
SUPPORTED_HASHES="sha256 sha384 sha512"
2020

2121
is_supported_algo() {
@@ -35,16 +35,18 @@ is_supported_hash() {
3535
}
3636

3737
# Helper functions for key operations. Each takes the target algo as $1.
38+
# rsapss* tokens carry an RSA key plus a PSS padding intent — the key
39+
# itself is generated/handled identically to plain rsa*.
3840
generate_private_key() {
3941
local algo=$1
4042
local output_file=$2
4143

4244
case "$algo" in
43-
ecc256) openssl ecparam -genkey -name prime256v1 -noout -out "$output_file" ;;
44-
ecc384) openssl ecparam -genkey -name secp384r1 -noout -out "$output_file" ;;
45-
rsa2048) openssl genrsa -out "$output_file" 2048 ;;
46-
rsa3072) openssl genrsa -out "$output_file" 3072 ;;
47-
rsa4096) openssl genrsa -out "$output_file" 4096 ;;
45+
ecc256) openssl ecparam -genkey -name prime256v1 -noout -out "$output_file" ;;
46+
ecc384) openssl ecparam -genkey -name secp384r1 -noout -out "$output_file" ;;
47+
rsa2048|rsapss2048) openssl genrsa -out "$output_file" 2048 ;;
48+
rsa3072|rsapss3072) openssl genrsa -out "$output_file" 3072 ;;
49+
rsa4096|rsapss4096) openssl genrsa -out "$output_file" 4096 ;;
4850
*) echo "Unsupported algo: $algo" >&2; exit 1 ;;
4951
esac
5052
}
@@ -58,7 +60,7 @@ convert_key_to_der() {
5860
ecc256|ecc384)
5961
openssl ec -in "$input_file" -outform DER -out "$output_file"
6062
;;
61-
rsa2048|rsa3072|rsa4096)
63+
rsa2048|rsa3072|rsa4096|rsapss2048|rsapss3072|rsapss4096)
6264
openssl rsa -in "$input_file" -outform DER -out "$output_file"
6365
;;
6466
*) echo "Unsupported algo: $algo" >&2; exit 1 ;;
@@ -79,7 +81,7 @@ extract_public_key() {
7981
ecc256|ecc384)
8082
openssl ec -pubin -in "$pubkey_pem" -outform DER -out "$pubkey_der"
8183
;;
82-
rsa2048|rsa3072|rsa4096)
84+
rsa2048|rsa3072|rsa4096|rsapss2048|rsapss3072|rsapss4096)
8385
openssl rsa -pubin -in "$pubkey_pem" -outform DER -out "$pubkey_der"
8486
;;
8587
*) echo "Unsupported algo: $algo" >&2; exit 1 ;;
@@ -94,13 +96,27 @@ validate_key_format() {
9496
ecc256|ecc384)
9597
openssl ec -in "$key_file" -noout
9698
;;
97-
rsa2048|rsa3072|rsa4096)
99+
rsa2048|rsa3072|rsa4096|rsapss2048|rsapss3072|rsapss4096)
98100
openssl rsa -in "$key_file" -noout
99101
;;
100102
*) echo "Unsupported algo: $algo" >&2; exit 1 ;;
101103
esac
102104
}
103105

106+
# Build openssl -sigopt args for a given algo + hash. For rsapss* algos
107+
# we ask openssl to use PSS padding with salt length equal to the digest
108+
# length and MGF1 keyed by the same hash (the standard interoperable
109+
# choice that wolfCrypt's PSS verify expects). Empty for non-PSS algos.
110+
sig_opts_for_algo() {
111+
local algo=$1
112+
local hash=$2
113+
case "$algo" in
114+
rsapss*)
115+
echo "-sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:digest -sigopt rsa_mgf1_md:$hash"
116+
;;
117+
esac
118+
}
119+
104120
usage() {
105121
cat <<EOT
106122
Usage: $0 [options]
@@ -191,6 +207,14 @@ if [[ -z "$LEAF_ALGO" ]]; then
191207
LEAF_ALGO="$CA_ALGO"
192208
fi
193209

210+
# Resolve PSS sigopts once based on the selected algos. CA_SIG_OPTS
211+
# applies to every cert signed by a CA-tier key (root self-sig,
212+
# intermediate CSR self-sig, intermediate cert, leaf cert); LEAF_SIG_OPTS
213+
# applies to the leaf CSR self-sig only (the leaf cert itself is signed
214+
# by the intermediate's CA-tier key). Both are empty for non-PSS algos.
215+
CA_SIG_OPTS=$(sig_opts_for_algo "$CA_ALGO" "$CA_HASH")
216+
LEAF_SIG_OPTS=$(sig_opts_for_algo "$LEAF_ALGO" "$CA_HASH")
217+
194218
# Configuration
195219
ROOT_SUBJECT="/C=US/ST=California/L=San Francisco/O=MyOrganization/OU=Root CA/CN=My Root CA"
196220
INTERMEDIATE_SUBJECT="/C=US/ST=California/L=San Francisco/O=MyOrganization/OU=Intermediate CA/CN=My Intermediate CA"
@@ -210,7 +234,7 @@ echo "Generating Root CA..."
210234
generate_private_key "$CA_ALGO" "${OUTPUT_DIR}/temp/root.key.pem"
211235

212236
# Create PEM format root certificate (temporary)
213-
openssl req -new -x509 -days 3650 -$CA_HASH \
237+
openssl req -new -x509 -days 3650 -$CA_HASH $CA_SIG_OPTS \
214238
-key ${OUTPUT_DIR}/temp/root.key.pem \
215239
-out ${OUTPUT_DIR}/temp/root.crt.pem \
216240
-subj "$ROOT_SUBJECT" \
@@ -225,13 +249,13 @@ openssl x509 -in ${OUTPUT_DIR}/temp/root.crt.pem -outform DER -out ${OUTPUT_DIR}
225249
echo "Generating Intermediate CA..."
226250
generate_private_key "$CA_ALGO" "${OUTPUT_DIR}/temp/intermediate.key.pem"
227251

228-
openssl req -new -$CA_HASH \
252+
openssl req -new -$CA_HASH $CA_SIG_OPTS \
229253
-key ${OUTPUT_DIR}/temp/intermediate.key.pem \
230254
-out ${OUTPUT_DIR}/temp/intermediate.csr \
231255
-subj "$INTERMEDIATE_SUBJECT"
232256

233257
# Step 3: Sign Intermediate certificate with Root
234-
openssl x509 -req -days 1825 -$CA_HASH \
258+
openssl x509 -req -days 1825 -$CA_HASH $CA_SIG_OPTS \
235259
-in ${OUTPUT_DIR}/temp/intermediate.csr \
236260
-out ${OUTPUT_DIR}/temp/intermediate.crt.pem \
237261
-CA ${OUTPUT_DIR}/temp/root.crt.pem \
@@ -258,13 +282,13 @@ fi
258282
# Create CSR for leaf certificate (CSR is signed by leaf key, but the
259283
# resulting cert signature is set by the CA when signing - so the CSR
260284
# self-signature uses CA_HASH for consistency).
261-
openssl req -new -$CA_HASH \
285+
openssl req -new -$CA_HASH $LEAF_SIG_OPTS \
262286
-key ${OUTPUT_DIR}/temp/leaf.key.pem \
263287
-out ${OUTPUT_DIR}/temp/leaf.csr \
264288
-subj "$LEAF_SUBJECT"
265289

266290
# Step 5: Sign Leaf certificate with Intermediate (uses CA_HASH)
267-
openssl x509 -req -days 365 -$CA_HASH \
291+
openssl x509 -req -days 365 -$CA_HASH $CA_SIG_OPTS \
268292
-in ${OUTPUT_DIR}/temp/leaf.csr \
269293
-out ${OUTPUT_DIR}/temp/leaf.crt.pem \
270294
-CA ${OUTPUT_DIR}/temp/intermediate.crt.pem \

0 commit comments

Comments
 (0)