Skip to content

Commit faf4657

Browse files
committed
generalize NEEDS_ refactor to all algos
1 parent d993650 commit faf4657

5 files changed

Lines changed: 263 additions & 106 deletions

File tree

include/user_settings/cascade.h

Lines changed: 166 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* user_settings/cascade.h
22
*
3-
* Lift Make-side feature implications into preprocessor cascades so an
4-
* IDE/CMake-only build (which sets only the high-level WOLFBOOT_* flags)
5-
* sees the same derived flags that options.mk would set.
3+
* Lift Make-side feature implications into preprocessor cascades, and
4+
* declare WOLFBOOT_NEEDS_* positive intent markers used by the rest
5+
* of the user_settings/ fragments and reconciled in finalize.h.
66
*
77
* Idempotent: every #define is #ifndef-guarded, so it's a no-op when
88
* options.mk has already emitted the same -D flag.
@@ -29,6 +29,10 @@
2929
#ifndef _WOLFBOOT_USER_SETTINGS_CASCADE_H_
3030
#define _WOLFBOOT_USER_SETTINGS_CASCADE_H_
3131

32+
/* ------------------------------------------------------------------
33+
* Feature-flag cascades
34+
* ------------------------------------------------------------------ */
35+
3236
/* Any feature that requires a hardware TPM 2.0 implies WOLFBOOT_TPM.
3337
* Mirrors options.mk:34-92 where the same Make variables force WOLFTPM:=1. */
3438
#if defined(WOLFBOOT_TPM_VERIFY) || \
@@ -40,15 +44,152 @@
4044
# endif
4145
#endif
4246

43-
/* WOLFBOOT_NEEDS_* declarations -- positive intent markers reconciled by
44-
* user_settings/finalize.h. Fragments may also set these from their own
45-
* headers; cascade.h handles the cases that today live as #undef blocks
46-
* scattered through user_settings.h. */
47+
/* TPM keystore and seal both require TPM session parameter encryption. */
48+
#if defined(WOLFBOOT_TPM_KEYSTORE) || defined(WOLFBOOT_TPM_SEAL)
49+
# ifndef WOLFBOOT_TPM_PARMENC
50+
# define WOLFBOOT_TPM_PARMENC
51+
# endif
52+
#endif
53+
54+
/* Any RSA SIGN flag (or WOLFCRYPT_SECURE_MODE without PKCS11_SMALL) means
55+
* the build links wolfCrypt's RSA code. sign_rsa.h handles the actual
56+
* configuration; the marker is set here so finalize.h can see it ahead
57+
* of finalize-time and skip NO_ASN. */
58+
#if defined(WOLFBOOT_SIGN_RSA2048) || \
59+
defined(WOLFBOOT_SIGN_RSA3072) || \
60+
defined(WOLFBOOT_SIGN_RSA4096) || \
61+
defined(WOLFBOOT_SIGN_SECONDARY_RSA2048) || \
62+
defined(WOLFBOOT_SIGN_SECONDARY_RSA3072) || \
63+
defined(WOLFBOOT_SIGN_SECONDARY_RSA4096) || \
64+
defined(WOLFBOOT_SIGN_RSAPSS2048) || \
65+
defined(WOLFBOOT_SIGN_RSAPSS3072) || \
66+
defined(WOLFBOOT_SIGN_RSAPSS4096) || \
67+
defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS2048) || \
68+
defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS3072) || \
69+
defined(WOLFBOOT_SIGN_SECONDARY_RSAPSS4096) || \
70+
(defined(WOLFCRYPT_SECURE_MODE) && !defined(PKCS11_SMALL))
71+
# ifndef WOLFBOOT_NEEDS_RSA
72+
# define WOLFBOOT_NEEDS_RSA
73+
# endif
74+
#endif
75+
76+
/* ------------------------------------------------------------------
77+
* WOLFBOOT_NEEDS_* declarations
78+
* ------------------------------------------------------------------
79+
* Positive intent markers. user_settings/finalize.h tests them and
80+
* applies the corresponding wolfCrypt negative flag (NO_*, WC_NO_*) to
81+
* builds that did NOT opt in. Fragments may also set additional markers
82+
* from their own headers. */
83+
84+
/* NEEDS_RNG: any feature that uses wolfCrypt's RNG.
85+
* Driven by: TPM parm-enc, secure-mode (TZ-PSA / TZ-FWTPM), test/bench,
86+
* wolfHSM server, and wolfHSM client + ML-DSA. */
87+
#if defined(WOLFBOOT_TPM_PARMENC) || \
88+
defined(WOLFCRYPT_SECURE_MODE) || \
89+
defined(WOLFCRYPT_TEST) || \
90+
defined(WOLFCRYPT_BENCHMARK) || \
91+
defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) || \
92+
(defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) && \
93+
defined(WOLFBOOT_SIGN_ML_DSA))
94+
# ifndef WOLFBOOT_NEEDS_RNG
95+
# define WOLFBOOT_NEEDS_RNG
96+
# endif
97+
#endif
98+
99+
/* NEEDS_HASHDRBG: features that use wolfCrypt's HASHDRBG specifically.
100+
* Note: TEST/BENCH non-LPC55S69 builds use a custom RNG and do NOT
101+
* declare this marker; their explicit `#define WC_NO_HASHDRBG` lives
102+
* in test_bench.h. */
103+
#if defined(WOLFBOOT_TPM_PARMENC) || \
104+
defined(WOLFCRYPT_SECURE_MODE) || \
105+
((defined(WOLFCRYPT_TEST) || defined(WOLFCRYPT_BENCHMARK)) && \
106+
(defined(WOLFSSL_NXP_LPC55S69_WITH_HWACCEL) || \
107+
defined(WOLFSSL_NXP_LPC55S69_NO_HWACCEL)))
108+
# ifndef WOLFBOOT_NEEDS_HASHDRBG
109+
# define WOLFBOOT_NEEDS_HASHDRBG
110+
# endif
111+
#endif
112+
113+
/* NEEDS_AES_CBC: features that use AES-CBC (entropy-using paths). */
114+
#if defined(WOLFBOOT_TPM_PARMENC) || \
115+
defined(WOLFCRYPT_SECURE_MODE) || \
116+
defined(WOLFCRYPT_TEST) || \
117+
defined(WOLFCRYPT_BENCHMARK)
118+
# ifndef WOLFBOOT_NEEDS_AES_CBC
119+
# define WOLFBOOT_NEEDS_AES_CBC
120+
# endif
121+
#endif
122+
123+
/* NEEDS_AES: features that use AES core. */
124+
#if defined(ENCRYPT_WITH_AES128) || \
125+
defined(ENCRYPT_WITH_AES256) || \
126+
defined(WOLFBOOT_TPM_PARMENC) || \
127+
defined(WOLFCRYPT_SECURE_MODE) || \
128+
defined(SECURE_PKCS11) || \
129+
defined(WOLFCRYPT_TZ_PSA) || \
130+
defined(WOLFCRYPT_TEST) || \
131+
defined(WOLFCRYPT_BENCHMARK)
132+
# ifndef WOLFBOOT_NEEDS_AES
133+
# define WOLFBOOT_NEEDS_AES
134+
# endif
135+
#endif
47136

48-
/* WOLFCRYPT_TZ_PSA and WOLFBOOT_TZ_FWTPM both keep CMAC and KDF enabled
49-
* (today by `#undef NO_CMAC` / `#undef NO_KDF` after the always-on block).
50-
* Lift those to positive intent so finalize.h can simply skip the
51-
* `#define NO_CMAC` / `#define NO_KDF`. */
137+
/* NEEDS_HMAC: features that use HMAC. */
138+
#if defined(WOLFBOOT_TPM) || \
139+
defined(WOLFCRYPT_SECURE_MODE) || \
140+
defined(WOLFCRYPT_TEST) || \
141+
defined(WOLFCRYPT_BENCHMARK)
142+
# ifndef WOLFBOOT_NEEDS_HMAC
143+
# define WOLFBOOT_NEEDS_HMAC
144+
# endif
145+
#endif
146+
147+
/* NEEDS_DEV_RANDOM: features that may want OS /dev/random as entropy. */
148+
#if defined(WOLFBOOT_TPM) || \
149+
defined(WOLFCRYPT_SECURE_MODE) || \
150+
defined(WOLFCRYPT_TEST) || \
151+
defined(WOLFCRYPT_BENCHMARK)
152+
# ifndef WOLFBOOT_NEEDS_DEV_RANDOM
153+
# define WOLFBOOT_NEEDS_DEV_RANDOM
154+
# endif
155+
#endif
156+
157+
/* NEEDS_ECC_KEY_EXPORT: features that need to export ECC keys. */
158+
#if defined(WOLFBOOT_TPM) || \
159+
defined(WOLFCRYPT_SECURE_MODE) || \
160+
defined(WOLFCRYPT_TEST) || \
161+
defined(WOLFCRYPT_BENCHMARK) || \
162+
defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) || \
163+
defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)
164+
# ifndef WOLFBOOT_NEEDS_ECC_KEY_EXPORT
165+
# define WOLFBOOT_NEEDS_ECC_KEY_EXPORT
166+
# endif
167+
#endif
168+
169+
/* NEEDS_ASN: features that need ASN.1 parsing. NEEDS_RSA also implies
170+
* this (RSA always parses ASN.1). */
171+
#if defined(WOLFBOOT_NEEDS_RSA) || \
172+
defined(WOLFBOOT_TPM) || \
173+
defined(WOLFCRYPT_SECURE_MODE) || \
174+
defined(WOLFCRYPT_TEST) || \
175+
defined(WOLFCRYPT_BENCHMARK) || \
176+
defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) || \
177+
defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)
178+
# ifndef WOLFBOOT_NEEDS_ASN
179+
# define WOLFBOOT_NEEDS_ASN
180+
# endif
181+
#endif
182+
183+
/* NEEDS_BASE64: features that use base64 encoding. */
184+
#if (defined(WOLFBOOT_TPM_SEAL) && defined(WOLFBOOT_ATA_DISK_LOCK)) || \
185+
defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) || \
186+
defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)
187+
# ifndef WOLFBOOT_NEEDS_BASE64
188+
# define WOLFBOOT_NEEDS_BASE64
189+
# endif
190+
#endif
191+
192+
/* NEEDS_CMAC and NEEDS_KDF: TZ_PSA and TZ_FWTPM need both. */
52193
#if defined(WOLFCRYPT_TZ_PSA) || defined(WOLFBOOT_TZ_FWTPM)
53194
# ifndef WOLFBOOT_NEEDS_CMAC
54195
# define WOLFBOOT_NEEDS_CMAC
@@ -58,4 +199,18 @@
58199
# endif
59200
#endif
60201

202+
/* NEEDS_MALLOC: features whose code-paths use heap allocation.
203+
* SECURE_PKCS11, WOLFCRYPT_TZ_PSA, the wolfHSM server, and the
204+
* test/bench harnesses all expect a working malloc. Default builds
205+
* (no marker) get NO_WOLFSSL_MEMORY + WOLFSSL_NO_MALLOC instead. */
206+
#if defined(SECURE_PKCS11) || \
207+
defined(WOLFCRYPT_TZ_PSA) || \
208+
defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) || \
209+
defined(WOLFCRYPT_TEST) || \
210+
defined(WOLFCRYPT_BENCHMARK)
211+
# ifndef WOLFBOOT_NEEDS_MALLOC
212+
# define WOLFBOOT_NEEDS_MALLOC
213+
# endif
214+
#endif
215+
61216
#endif /* _WOLFBOOT_USER_SETTINGS_CASCADE_H_ */

include/user_settings/finalize.h

Lines changed: 55 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -28,112 +28,76 @@
2828
#ifndef _WOLFBOOT_USER_SETTINGS_FINALIZE_H_
2929
#define _WOLFBOOT_USER_SETTINGS_FINALIZE_H_
3030

31-
/* WOLFBOOT_NEEDS_* reconciliation
31+
/* ------------------------------------------------------------------
32+
* NEEDS_* reconciliation
3233
* ------------------------------------------------------------------
33-
* Positive intent markers from cascade.h or feature fragments map here
34-
* to wolfCrypt's negative (NO_*, WC_NO_*) flags. The full marker
35-
* vocabulary is documented in the refactor plan. */
36-
#ifndef WOLFBOOT_NEEDS_CMAC
37-
# define NO_CMAC
38-
#endif
39-
#ifndef WOLFBOOT_NEEDS_KDF
40-
# define NO_KDF
41-
#endif
42-
43-
/* HAVE_PWDBASED is opted into by EXT_ENCRYPTED, SECURE_PKCS11, and
44-
* WOLFCRYPT_TZ_PSA. If none of them set it, default to NO_PWDBASED. */
45-
#ifndef HAVE_PWDBASED
46-
# define NO_PWDBASED
47-
#endif
34+
* Each negative wolfCrypt flag (NO_*, WC_NO_*) is gated by the absence
35+
* of its matching WOLFBOOT_NEEDS_* marker. Markers are declared in
36+
* cascade.h (from feature flags) or in fragment headers. */
4837

49-
/* RNG / HASHDRBG / NO_AES_CBC: today disabled unless any of the
50-
* "needs entropy" features are active. */
51-
#if !defined(WOLFCRYPT_SECURE_MODE) && !defined(WOLFBOOT_TPM_PARMENC) && \
52-
!defined(WOLFCRYPT_TEST) && !defined(WOLFCRYPT_BENCHMARK)
53-
# if !(defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) && \
54-
defined(WOLFBOOT_SIGN_ML_DSA)) && \
55-
!defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)
56-
# define WC_NO_RNG
38+
/* HASHDRBG: positive when needed, WC_NO_HASHDRBG otherwise.
39+
* Note: test_bench.h's non-LPC55S69 path explicitly defines
40+
* WC_NO_HASHDRBG itself; this `#ifndef` won't redefine it. */
41+
#ifdef WOLFBOOT_NEEDS_HASHDRBG
42+
# ifndef HAVE_HASHDRBG
43+
# define HAVE_HASHDRBG
5744
# endif
58-
# define WC_NO_HASHDRBG
59-
# define NO_AES_CBC
6045
#else
61-
# if defined(WOLFCRYPT_TEST) || defined(WOLFCRYPT_BENCHMARK)
62-
# if defined(WOLFSSL_NXP_LPC55S69_WITH_HWACCEL) \
63-
|| defined(WOLFSSL_NXP_LPC55S69_NO_HWACCEL)
64-
/* use actual rng hardware for seed, HASHDRBG for generation */
65-
# define HAVE_HASHDRBG
66-
# define HAVE_AES_ECB
67-
# define WOLFSSL_AES_OFB
68-
# define WOLFSSL_AES_CFB
69-
# define WOLFSSL_AES_COUNTER
70-
# define WOLFSSL_STATIC_MEMORY_TEST_SZ (30 * 1024)
71-
# define WOLFSSL_SHA256
72-
# define WOLFSSL_SHA384
73-
# define WOLFSSL_SHA512
74-
# else
75-
/* Use custom RNG for tests/benchmarks (saves ~7KB vs HASHDRBG).
76-
* WARNING: my_rng_seed_gen is NOT cryptographically secure.
77-
* Only used in test-app builds, not in production wolfBoot. */
78-
# define WC_NO_HASHDRBG
79-
# define CUSTOM_RAND_GENERATE_SEED my_rng_seed_gen
80-
# define CUSTOM_RAND_GENERATE_BLOCK my_rng_seed_gen
81-
extern int my_rng_seed_gen(unsigned char* output, unsigned int sz);
82-
# endif
83-
84-
# define HAVE_AESGCM
85-
# define GCM_TABLE
86-
# else
87-
# define HAVE_HASHDRBG
88-
# define WOLFSSL_AES_CFB
46+
# ifndef WC_NO_HASHDRBG
47+
# define WC_NO_HASHDRBG
8948
# endif
9049
#endif
9150

92-
/* AES core: stripped unless any AES-using fragment is active. */
93-
#if !defined(ENCRYPT_WITH_AES128) && !defined(ENCRYPT_WITH_AES256) && \
94-
!defined(WOLFBOOT_TPM_PARMENC) && !defined(WOLFCRYPT_SECURE_MODE) && \
95-
!defined(SECURE_PKCS11) && !defined(WOLFCRYPT_TZ_PSA) && \
96-
!defined(WOLFCRYPT_TEST) && !defined(WOLFCRYPT_BENCHMARK)
97-
# define NO_AES
51+
#ifndef WOLFBOOT_NEEDS_RNG
52+
# define WC_NO_RNG
9853
#endif
9954

100-
/* HMAC: stripped unless TPM / secure mode / test/bench is active. */
101-
#if !defined(WOLFBOOT_TPM) && !defined(WOLFCRYPT_SECURE_MODE) && \
102-
!defined(WOLFCRYPT_TEST) && !defined(WOLFCRYPT_BENCHMARK)
55+
#ifndef WOLFBOOT_NEEDS_AES
56+
# define NO_AES
57+
#endif
58+
#ifndef WOLFBOOT_NEEDS_AES_CBC
59+
# define NO_AES_CBC
60+
#endif
61+
#ifndef WOLFBOOT_NEEDS_HMAC
10362
# define NO_HMAC
10463
#endif
105-
106-
/* RNG / ECC key export / ASN: second copy of the negated chain for the
107-
* "no TPM, no secure, no test/bench" path. Distinct from the block above
108-
* because it runs after WC_NO_HASHDRBG / NO_DEV_RANDOM are decided. */
109-
#if !defined(WOLFBOOT_TPM) && !defined(WOLFCRYPT_SECURE_MODE) && \
110-
!defined(WOLFCRYPT_TEST) && !defined(WOLFCRYPT_BENCHMARK)
111-
# if !(defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) && \
112-
defined(WOLFBOOT_SIGN_ML_DSA)) && \
113-
!defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)
114-
# define WC_NO_RNG
115-
# endif
116-
# define WC_NO_HASHDRBG
64+
#ifndef WOLFBOOT_NEEDS_DEV_RANDOM
11765
# define NO_DEV_RANDOM
118-
# if !defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) && \
119-
!defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)
120-
# define NO_ECC_KEY_EXPORT
121-
# if defined(NO_RSA)
122-
# define NO_ASN
123-
# endif
124-
# endif
66+
#endif
67+
#ifndef WOLFBOOT_NEEDS_ECC_KEY_EXPORT
68+
# define NO_ECC_KEY_EXPORT
69+
#endif
70+
#ifndef WOLFBOOT_NEEDS_ASN
71+
# define NO_ASN
72+
#endif
73+
#ifndef WOLFBOOT_NEEDS_CMAC
74+
# define NO_CMAC
75+
#endif
76+
#ifndef WOLFBOOT_NEEDS_KDF
77+
# define NO_KDF
78+
#endif
79+
80+
/* RSA: skip NO_RSA when NEEDS_RSA is set. */
81+
#ifndef WOLFBOOT_NEEDS_RSA
82+
# define NO_RSA
83+
#endif
84+
85+
/* HAVE_PWDBASED is opted into by EXT_ENCRYPTED, SECURE_PKCS11, and
86+
* WOLFCRYPT_TZ_PSA. If none of them set it, default to NO_PWDBASED. */
87+
#ifndef HAVE_PWDBASED
88+
# define NO_PWDBASED
12589
#endif
12690

127-
/* BASE64 / NO_CODING: opt-in via TPM_SEAL+ATA_DISK_LOCK or wolfHSM. */
128-
#if (defined(WOLFBOOT_TPM_SEAL) && defined(WOLFBOOT_ATA_DISK_LOCK)) || \
129-
defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) || \
130-
defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER)
91+
/* BASE64 / NO_CODING. */
92+
#ifdef WOLFBOOT_NEEDS_BASE64
13193
# define WOLFSSL_BASE64_ENCODE
13294
#else
13395
# define NO_CODING
13496
#endif
13597

136-
/* Always-on disables (no fragment opts out). */
98+
/* ------------------------------------------------------------------
99+
* Always-on disables (no fragment opts out today).
100+
* ------------------------------------------------------------------ */
137101
#define NO_DH
138102
#define WOLFSSL_NO_PEM
139103
#define NO_ASN_TIME
@@ -169,7 +133,9 @@
169133
# define BENCH_EMBEDDED
170134
#endif
171135

172-
/* Memory model. */
136+
/* ------------------------------------------------------------------
137+
* Memory model.
138+
* ------------------------------------------------------------------ */
173139
#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
174140
/* Disable VLAs */
175141
# define WOLFSSL_SP_NO_DYN_STACK
@@ -185,9 +151,7 @@
185151
# define WOLFSSL_SP_NO_MALLOC
186152
# define WOLFSSL_SP_NO_DYN_STACK
187153
# endif
188-
# if !defined(SECURE_PKCS11) && !defined(WOLFCRYPT_TZ_PSA) && \
189-
!defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER) && \
190-
!defined(WOLFCRYPT_TEST) && !defined(WOLFCRYPT_BENCHMARK)
154+
# ifndef WOLFBOOT_NEEDS_MALLOC
191155
# define NO_WOLFSSL_MEMORY
192156
# define WOLFSSL_NO_MALLOC
193157
# endif

0 commit comments

Comments
 (0)