Skip to content

Commit c76acb6

Browse files
authored
Merge pull request #11967 from SparkiDev/rsa_dh_sc_fixes
sp, tfm, sakke: default cache-resistant modexp copy to address-oblivious
2 parents 803796e + a537c08 commit c76acb6

16 files changed

Lines changed: 1828 additions & 663 deletions

‎.github/configs/os-check-linux.json‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,28 @@
105105
"comment": "Non-blocking ECC on the portable C implementation without the small math; RSA/DH non-block are left off because their mod_exp only exists in the small variant.",
106106
"configure": ["--enable-ecc=nonblock", "--enable-sp=yes,nonblocklarge", "--disable-sp-asm",
107107
"CPPFLAGS=-DWOLFSSL_PUBLIC_MP -DWOLFSSL_DEBUG_NONBLOCK"]},
108+
{"name": "modexp-copy-default-sakke", "minutes": 2.6,
109+
"comment": "Default (address-oblivious) cache-resistant modexp copy on a normal target: _sp_exptmod_mont_ex (sp_int.c) via _sp_cond_copy and the SAKKE ladder (sakke.c) via mp_cond_copy, which touch both t[0] and t[1] so the accessed address is secret-independent. Nothing else in this matrix builds SAKKE, and on a non-capability target the sp_int.c default is only otherwise reached incidentally. Pairs with modexp-copy-fast-sakke, which flips both to the pointer-copy opt-in.",
110+
"configure": ["--enable-sakke", "--enable-sp-math-all"]},
111+
{"name": "modexp-copy-fast-sakke", "minutes": 2.6,
112+
"comment": "WOLFSSL_SP_FAST_MODEXP_COPY opt-in: the faster secret-dependent-pointer copy in _sp_exptmod_mont_ex (sp_int.c) and the SAKKE ladder (sakke.c). That branch is the #else of the oblivious default and is compiled by no other entry, and the macro is forced off on the capability (WC_NO_PTR_INT_CAST) legs in fil-c.yml, so a normal target is the only place it is built.",
113+
"configure": ["--enable-sakke", "--enable-sp-math-all",
114+
"CPPFLAGS=-DWOLFSSL_SP_FAST_MODEXP_COPY"]},
115+
{"name": "sp-ladder-modexp", "minutes": 2.0,
116+
"comment": "Specialized SP in portable C (sp_c64.c on x86_64) with WOLFSSL_SP_LADDER_MODEXP, which replaces the windowed exptmod with the cache-resistant ladder. Default (oblivious) copy via sp_cond_select/sp_cond_memcpy. --disable-sp-asm keeps RSA/DH/ECC on the generated C files; nothing else here builds the non-small ladder on a normal target.",
117+
"configure": ["--enable-sp", "--disable-sp-asm",
118+
"CPPFLAGS=-DWOLFSSL_SP_LADDER_MODEXP"]},
119+
{"name": "sp-ladder-modexp-fast-copy", "minutes": 2.0,
120+
"comment": "Same sp_c64.c ladder as sp-ladder-modexp but with WOLFSSL_SP_FAST_MODEXP_COPY opting the copy onto addr_mask pointer selection (the #else branch) instead of sp_cond_select/sp_cond_memcpy.",
121+
"configure": ["--enable-sp", "--disable-sp-asm",
122+
"CPPFLAGS=-DWOLFSSL_SP_LADDER_MODEXP -DWOLFSSL_SP_FAST_MODEXP_COPY"]},
123+
{"name": "fastmath-modexp-copy-default", "minutes": 2.4,
124+
"comment": "TomsFastMath (tfm.c) on a normal target: fp_exptmod_ct selects the working value with the address-oblivious fp_cond_copy default. os-check otherwise builds no fastmath leg (only fil-c.yml does, where WC_NO_PTR_INT_CAST already forces this branch); this covers it under plain gcc. Pairs with fastmath-modexp-copy-fast.",
125+
"configure": ["--enable-fastmath"]},
126+
{"name": "fastmath-modexp-copy-fast", "minutes": 2.4,
127+
"comment": "tfm.c fp_exptmod_ct with WOLFSSL_SP_FAST_MODEXP_COPY opting onto the wc_off_on_addr pointer copy (the #else branch). Forced off on the fil-c.yml capability legs, so a normal target is the only place this branch is built.",
128+
"configure": ["--enable-fastmath",
129+
"CPPFLAGS=-DWOLFSSL_SP_FAST_MODEXP_COPY"]},
108130
{"name": "all-asn-template", "minutes": 3.5,
109131
"configure": ["--enable-all", "--enable-asn=template"]},
110132
{"name": "chain-verify-cb-async", "minutes": 3.5,

‎.wolfssl_known_macro_extras‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,7 @@ WOLFSSL_SILABS_TRNG
12131213
WOLFSSL_SLHDSA_FULL_HASH
12141214
WOLFSSL_SLHDSA_NO_VERIFY_ONLY
12151215
WOLFSSL_SNIFFER_NO_RECOVERY
1216+
WOLFSSL_SP_FAST_MODEXP_COPY
12161217
WOLFSSL_SP_FAST_NCT_EXPTMOD
12171218
WOLFSSL_SP_INT_SQR_VOLATILE
12181219
WOLFSSL_SP_LADDER_MODEXP

‎doc/ASM_AND_MATH_DEFINES.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ so choose according to what your target has:
281281
| `WC_PROTECT_ENCRYPTED_MEM` | Extra protection for operations on encrypted memory. |
282282
| `WC_NO_CACHE_RESISTANT` | Disable cache-resistant (constant-address) table access. Faster, weaker. |
283283
| `WC_NO_HARDEN` | Disable timing-attack resistance. Faster, weaker. |
284+
| `WOLFSSL_SP_FAST_MODEXP_COPY` | Select the cache-resistant `mod_exp` working value with a single secret-dependent pointer copy instead of touching both candidates. Faster, but leaks the exponent at cache-line granularity. Off by default; forced off on capability targets. |
284285
| `WOLFSSL_NO_CT_OPS` | Disable constant-time operations. |
285286
| `WOLFSSL_CHECK_MEM_ZERO` | Check that sensitive memory is zeroed on free (debug). |
286287

@@ -289,6 +290,42 @@ wolfSSL recommends against defining `WC_NO_HARDEN`, `WC_NO_CACHE_RESISTANT` or
289290
your threat model genuinely excludes an attacker able to observe local timing or
290291
cache behaviour. The performance gain is small relative to the protection lost.
291292

293+
The cache-resistant modular exponentiation (and the ECC/SAKKE scalar-mult
294+
ladders) select the working value from one of two objects on each secret
295+
exponent bit. By default that selection is **address-oblivious**: both objects
296+
are accessed every iteration through a masked constant-time copy
297+
(`_sp_cond_copy` in `sp_int.c`, `sp_cond_select`/`sp_cond_memcpy` in the
298+
specialised `sp_<arch>.c`, `fp_cond_copy` in `tfm.c`, `mp_cond_copy` in
299+
`sakke.c`), so the touched memory address does not depend on the secret. This
300+
resists a co-resident attacker that can observe cache-line accesses, for example
301+
`Prime+Probe` from a sibling SMT thread sharing the L1 data cache. Defining
302+
`WOLFSSL_SP_FAST_MODEXP_COPY` switches to the older, faster variant that computes
303+
a single secret-dependent pointer by address masking and copies only the
304+
selected object; it is a few percent faster on private-key operations but
305+
discloses the exponent bits at cache-line granularity, so enable it only where a
306+
co-resident cache observer is outside your threat model. It requires rebuilding a
307+
pointer from integer arithmetic on two addresses, which capability targets (those
308+
that define `WC_NO_PTR_INT_CAST`, such as CHERI and Fil-C) cannot do, so the
309+
option is ignored and the oblivious copy is always used there. It is also a no-op
310+
for the default windowed `mod_exp` used by the specialised SP assembly back ends,
311+
which select from a table scanned in full rather than from two objects.
312+
313+
The generated SP back ends (`sp_c32.c`, `sp_c64.c` and the assembly targets
314+
`sp_x86_64.c`, `sp_arm64.c`, `sp_arm32.c`, `sp_armthumb.c`, `sp_cortexm.c`,
315+
`sp_riscv64.c`) all take the address-oblivious copy by default: both the
316+
`mod_exp` ladders and the non-blocking ECC scalar-multiplication ladders write
317+
each product/sum to a scratch object and distribute it into the secret-selected
318+
slot by touching both candidates, so no store address depends on the secret.
319+
320+
One backend is not covered: the Hexagon DSP source `sp_dsp32.c` (shipped as
321+
`EXTRA_DIST`, not built by `./configure`, and not produced by the sp scripts).
322+
It holds the P-256 ECC scalar-multiplication ladder (it has no private-key
323+
`mod_exp`) and still selects the working point with an unconditional
324+
secret-dependent pointer copy, honouring neither the oblivious default nor
325+
`WOLFSSL_SP_FAST_MODEXP_COPY`. Treat that scalar multiplication as leaking the
326+
scalar at cache-line granularity until it is converted to the address-oblivious
327+
copy.
328+
292329
### Non-blocking
293330

294331
`WOLFSSL_SP_NONBLOCK` makes long operations return `FP_WOULDBLOCK` and expect

‎wolfcrypt/src/sakke.c‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,6 +2825,24 @@ static int sakke_modexp_loop(SakkeKey* key, mp_int* b, mp_int* e, mp_proj* c,
28252825

28262826
#elif defined(WOLFSSL_SAKKE_SMALL_MODEXP) || !defined(USE_FAST_MATH)
28272827

2828+
/* The cache-resistant SAKKE modexp ladder selects the working point from c[0]
2829+
* or c[1] on each exponent bit. By default this is done with mp_cond_copy(),
2830+
* which accesses both objects every iteration so the touched address does not
2831+
* depend on the secret bit (resists co-resident L1D cache attacks such as
2832+
* Prime+Probe between SMT siblings).
2833+
*
2834+
* Defining WOLFSSL_SP_FAST_MODEXP_COPY (the same opt-in used by the SP math
2835+
* backends) selects the faster variant that instead computes a single
2836+
* secret-dependent pointer with wc_off_on_addr masking and copies only the
2837+
* selected point. It is faster but leaks the exponent at cache-line
2838+
* granularity, so only enable it where a co-resident cache observer is outside
2839+
* the threat model. The fast variant rebuilds a pointer from integer arithmetic
2840+
* on two addresses, which capability targets cannot do, so it is forced off
2841+
* when WC_NO_PTR_INT_CAST is defined. */
2842+
#if defined(WOLFSSL_SP_FAST_MODEXP_COPY) && !defined(WC_NO_PTR_INT_CAST)
2843+
#define WC_SAKKE_MODEXP_PTR_COPY
2844+
#endif
2845+
28282846
/*
28292847
* Modular exponentiate the value in F_p*.
28302848
*
@@ -2851,7 +2869,10 @@ static int sakke_modexp_loop(SakkeKey* key, mp_int* b, mp_int* e, mp_proj* r,
28512869
mp_int* by = key->tmp.p1->z;
28522870
mp_int* prime = &key->params.prime;
28532871
int i;
2854-
#ifdef WC_NO_GLOBAL_OBJECT_POINTERS
2872+
/* Local address mask only when the pointer-copy opt-in is active and global
2873+
* object pointers are unavailable; the default oblivious copy never uses
2874+
* it, so declaring it unconditionally is an unused variable. */
2875+
#if defined(WC_NO_GLOBAL_OBJECT_POINTERS) && defined(WC_SAKKE_MODEXP_PTR_COPY)
28552876
static const wc_ptr_t wc_off_on_addr[2] =
28562877
{
28572878
#if defined(WC_64BIT_CPU)
@@ -2894,11 +2915,19 @@ static int sakke_modexp_loop(SakkeKey* key, mp_int* b, mp_int* e, mp_proj* r,
28942915
err = sakke_proj_mul_qx1(c[0], by, prime, mp, c[j^1], t1, t2);
28952916
#else
28962917
err = sakke_proj_mul_qx1(c[0], by, prime, mp, c[2], t1, t2);
2897-
#ifdef WC_NO_PTR_INT_CAST
2898-
err = mp_cond_copy(c[2]->x, j, c[0]->x);
2899-
err = mp_cond_copy(c[2]->x, j^1, c[1]->x);
2900-
err = mp_cond_copy(c[2]->y, j, c[0]->y);
2901-
err = mp_cond_copy(c[2]->y, j^1, c[1]->y);
2918+
#ifndef WC_SAKKE_MODEXP_PTR_COPY
2919+
/* Guard each copy on err so a failure in the multiply above or in
2920+
* an earlier copy is preserved rather than overwritten. err does
2921+
* not depend on the secret bit, so all four copies still run (and
2922+
* touch both c[0] and c[1]) on the success path. */
2923+
if (err == 0)
2924+
err = mp_cond_copy(c[2]->x, j, c[0]->x);
2925+
if (err == 0)
2926+
err = mp_cond_copy(c[2]->x, j^1, c[1]->x);
2927+
if (err == 0)
2928+
err = mp_cond_copy(c[2]->y, j, c[0]->y);
2929+
if (err == 0)
2930+
err = mp_cond_copy(c[2]->y, j^1, c[1]->y);
29022931
#else
29032932
mp_copy(c[2]->x,
29042933
(mp_int*) ( ((wc_ptr_t)c[0]->x & wc_off_on_addr[j]) +

‎wolfcrypt/src/sp_arm32.c‎

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22+
/* Generated by wolfSSL/scripts:
23+
* ruby sp/sp.rb ARM32 \
24+
* ../wolfssl/wolfcrypt/src/sp_arm32
25+
*/
26+
2227
/* Implementation by Sean Parkinson. */
2328

2429
#define WC_FIPS_LL_CRYPTO
@@ -166,41 +171,62 @@
166171
#if defined(WOLFSSL_HAVE_SP_ECC) && defined(WOLFSSL_SP_NONBLOCK)
167172
/* Conditionally copy len bytes from a to r when copy is 1, in constant time.
168173
* Reads r, so r must be initialized. Both candidates are always touched, so
169-
* which one was selected is not observable. */
174+
* which one was selected is not observable. All callers pass sp_digit aligned
175+
* buffers a whole number of sp_digit wide (sp_digit arrays or sp_point structs),
176+
* so the copy runs a word at a time rather than a byte at a time. The words are
177+
* loaded and stored with XMEMCPY through a byte pointer, so a struct member that
178+
* is not an sp_digit (such as sp_point's int infinity) is never accessed through
179+
* an sp_digit lvalue - that would break strict aliasing and could leave an
180+
* optimiser using a stale value. */
170181
WC_MAYBE_UNUSED static void sp_cond_memcpy(void* r, const void* a, int copy,
171182
size_t len)
172183
{
173184
byte* rb = (byte*)r;
174185
const byte* ab = (const byte*)a;
175186
/* Read the mask back through a volatile so the compiler cannot tell it
176-
* is only ever 0 or 0xff. Knowing that, it may turn the masking into a
187+
* is only ever 0 or all-ones. Knowing that, it may turn the masking into a
177188
* branch on copy, e.g. by unswitching the loop, which leaks copy. */
178-
volatile byte vmask = (byte)(0U - (unsigned int)(copy != 0));
179-
byte mask = vmask;
189+
volatile sp_digit vmask = (sp_digit)0 - (sp_digit)(copy != 0);
190+
sp_digit mask = vmask;
180191
size_t i;
181192

182-
for (i = 0; i < len; i++) {
183-
rb[i] ^= (byte)((rb[i] ^ ab[i]) & mask);
193+
for (i = 0; i < len; i += sizeof(sp_digit)) {
194+
sp_digit rw;
195+
sp_digit aw;
196+
XMEMCPY(&rw, rb + i, sizeof(sp_digit));
197+
XMEMCPY(&aw, ab + i, sizeof(sp_digit));
198+
rw ^= (rw ^ aw) & mask;
199+
XMEMCPY(rb + i, &rw, sizeof(sp_digit));
184200
}
185201
}
186202

187203
/* Set r to a when sel is 0 and to b when sel is 1, in constant time.
188-
* Writes r without reading it, so r may be an uninitialized scratch buffer. */
204+
* Writes r without reading it, so r may be an uninitialized scratch buffer.
205+
* As with sp_cond_memcpy, all callers pass sp_digit aligned buffers a whole
206+
* number of sp_digit wide, so the select runs a word at a time, loading and
207+
* storing through a byte pointer with XMEMCPY to avoid aliasing a non-sp_digit
208+
* struct member (such as sp_point's int infinity) through an sp_digit lvalue. */
189209
WC_MAYBE_UNUSED static void sp_cond_select(void* r, const void* a,
190210
const void* b, int sel, size_t len)
191211
{
192212
byte* rb = (byte*)r;
193213
const byte* ab = (const byte*)a;
194214
const byte* bb = (const byte*)b;
195215
/* Read the mask back through a volatile so the compiler cannot tell it
196-
* is only ever 0 or 0xff. Knowing that, it may turn the masking into a
216+
* is only ever 0 or all-ones. Knowing that, it may turn the masking into a
197217
* branch on sel, e.g. by unswitching the loop, which leaks sel. */
198-
volatile byte vmask = (byte)(0U - (unsigned int)(sel != 0));
199-
byte mask = vmask;
218+
volatile sp_digit vmask = (sp_digit)0 - (sp_digit)(sel != 0);
219+
sp_digit mask = vmask;
200220
size_t i;
201221

202-
for (i = 0; i < len; i++) {
203-
rb[i] = (byte)((ab[i] & (byte)~mask) | (bb[i] & mask));
222+
for (i = 0; i < len; i += sizeof(sp_digit)) {
223+
sp_digit aw;
224+
sp_digit bw;
225+
sp_digit rw;
226+
XMEMCPY(&aw, ab + i, sizeof(sp_digit));
227+
XMEMCPY(&bw, bb + i, sizeof(sp_digit));
228+
rw = (aw & ~mask) | (bw & mask);
229+
XMEMCPY(rb + i, &rw, sizeof(sp_digit));
204230
}
205231
}
206232
#endif /* WOLFSSL_HAVE_SP_ECC && WOLFSSL_SP_NONBLOCK */
@@ -76887,8 +76913,14 @@ static int sp_256_ecc_mulmod_8_nb(sp_ecc_ctx_t* sp_ctx, sp_point_256* r,
7688776913
ctx->state = 5;
7688876914
break;
7688976915
case 5: /* ADD */
76890-
sp_256_proj_point_add_8(&ctx->t[ctx->y^1], &ctx->t[0], &ctx->t[1],
76891-
ctx->tmp);
76916+
/* Address-oblivious: sum -> scratch t[2], moved into t[y^1] by touching
76917+
* both t[0] and t[1]; t[y] copied into t[2] for the double. */
76918+
sp_256_proj_point_add_8(&ctx->t[2], &ctx->t[0],
76919+
&ctx->t[1], ctx->tmp);
76920+
sp_cond_memcpy(&ctx->t[0], &ctx->t[2], (ctx->y),
76921+
sizeof(sp_point_256));
76922+
sp_cond_memcpy(&ctx->t[1], &ctx->t[2], (ctx->y)^1,
76923+
sizeof(sp_point_256));
7689276924
sp_cond_select(&ctx->t[2], &ctx->t[0], &ctx->t[1], (ctx->y),
7689376925
sizeof(sp_point_256));
7689476926
ctx->state = 6;
@@ -95396,8 +95428,14 @@ static int sp_384_ecc_mulmod_12_nb(sp_ecc_ctx_t* sp_ctx, sp_point_384* r,
9539695428
ctx->state = 5;
9539795429
break;
9539895430
case 5: /* ADD */
95399-
sp_384_proj_point_add_12(&ctx->t[ctx->y^1], &ctx->t[0], &ctx->t[1],
95400-
ctx->tmp);
95431+
/* Address-oblivious: sum -> scratch t[2], moved into t[y^1] by touching
95432+
* both t[0] and t[1]; t[y] copied into t[2] for the double. */
95433+
sp_384_proj_point_add_12(&ctx->t[2], &ctx->t[0],
95434+
&ctx->t[1], ctx->tmp);
95435+
sp_cond_memcpy(&ctx->t[0], &ctx->t[2], (ctx->y),
95436+
sizeof(sp_point_384));
95437+
sp_cond_memcpy(&ctx->t[1], &ctx->t[2], (ctx->y)^1,
95438+
sizeof(sp_point_384));
9540195439
sp_cond_select(&ctx->t[2], &ctx->t[0], &ctx->t[1], (ctx->y),
9540295440
sizeof(sp_point_384));
9540395441
ctx->state = 6;
@@ -123055,8 +123093,14 @@ static int sp_521_ecc_mulmod_17_nb(sp_ecc_ctx_t* sp_ctx, sp_point_521* r,
123055123093
ctx->state = 5;
123056123094
break;
123057123095
case 5: /* ADD */
123058-
sp_521_proj_point_add_17(&ctx->t[ctx->y^1], &ctx->t[0], &ctx->t[1],
123059-
ctx->tmp);
123096+
/* Address-oblivious: sum -> scratch t[2], moved into t[y^1] by touching
123097+
* both t[0] and t[1]; t[y] copied into t[2] for the double. */
123098+
sp_521_proj_point_add_17(&ctx->t[2], &ctx->t[0],
123099+
&ctx->t[1], ctx->tmp);
123100+
sp_cond_memcpy(&ctx->t[0], &ctx->t[2], (ctx->y),
123101+
sizeof(sp_point_521));
123102+
sp_cond_memcpy(&ctx->t[1], &ctx->t[2], (ctx->y)^1,
123103+
sizeof(sp_point_521));
123060123104
sp_cond_select(&ctx->t[2], &ctx->t[0], &ctx->t[1], (ctx->y),
123061123105
sizeof(sp_point_521));
123062123106
ctx->state = 6;

0 commit comments

Comments
 (0)