Skip to content

Commit e4a326d

Browse files
SparkiDevclaude
andcommitted
SAKKE, ECCSI: constant time operations on secrets, optional RNG blinding
- Without an RNG, operations on secrets (z_T, RSK, r) are constant time: reduction, inversion, field add/sub, ModExp and point mapping. - New wc_SetSakkeRng(): blinding allows faster algorithms to be used. wc_MakeSakkeRsk() uses it rather than instantiating an RNG per call. - Table lookups cache resistant unless WC_NO_CACHE_RESISTANT. - SP P1024: constant time lookups (AVX2 on x86_64), table multiply and ModExp; fix AVX2 entry over-read, add vzeroupper; fix p1024_order guard for NO_ECC_CHECK_KEY builds. - ecc_map_ex() constant time branch uses constant time reduction. - ECC generic projective add/double: constant time Montgomery reduction and point equality check when timing resistant. Public only operations (ecc_mul2add(), verify) keep the faster reduction. - ECC scalar range checks no longer count the bits of the secret scalar. - sp_int: sp_invmod_mont_ct() reductions are constant time; cheaper constant time final subtraction and input masking in Montgomery reduction. - TFM: honor the constant time flag of Montgomery reduction (plain reduction unchanged unless WOLFSSL_MONT_RED_CT), fixed length carry propagation, constant time clamp, fp_invmod_mont_ct() constant time reductions; fix fp_submod_ct() leaving a stale top digit. - ECCSI signing: r * SSK, HE + r * SSK, inversion and multiply by j are constant time (Fermat inversion in Montgomery form) with TFM and SP math all; other math builds keep blinded inversion. - Regenerate SP x86_64 assembly (32-bit parameters zero extended). - Tests and docs for wc_SetSakkeRng(); white-box tests updated for changed static helpers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 206e9a5 commit e4a326d

25 files changed

Lines changed: 6697 additions & 819 deletions

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@
106106
"configure": ["--enable-ecc=nonblock", "--enable-sp=yes,nonblocklarge", "--disable-sp-asm",
107107
"CPPFLAGS=-DWOLFSSL_PUBLIC_MP -DWOLFSSL_DEBUG_NONBLOCK"]},
108108
{"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.",
109+
"comment": "Default (address-oblivious) cache-resistant modexp copy on a normal target: _sp_exptmod_mont_ex (sp_int.c) via _sp_cond_copy, which touches both t[0] and t[1] so the accessed address is secret-independent, alongside the SAKKE F_p* exponentiation (sakke.c), which always uses mp_cond_copy. 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 sp_int.c to the pointer-copy opt-in.",
110110
"configure": ["--enable-sakke", "--enable-sp-math-all"]},
111111
{"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.",
112+
"comment": "WOLFSSL_SP_FAST_MODEXP_COPY opt-in: the faster secret-dependent-pointer copy in _sp_exptmod_mont_ex (sp_int.c). SAKKE is built to check that its F_p* exponentiation (sakke.c), which ignores the opt-in and always uses mp_cond_copy, still passes with it defined. The sp_int.c 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.",
113113
"configure": ["--enable-sakke", "--enable-sp-math-all",
114114
"CPPFLAGS=-DWOLFSSL_SP_FAST_MODEXP_COPY"]},
115115
{"name": "sp-ladder-modexp", "minutes": 2.0,

‎doc/ASM_AND_MATH_DEFINES.md‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -290,25 +290,27 @@ wolfSSL recommends against defining `WC_NO_HARDEN`, `WC_NO_CACHE_RESISTANT` or
290290
your threat model genuinely excludes an attacker able to observe local timing or
291291
cache behaviour. The performance gain is small relative to the protection lost.
292292

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
293+
The cache-resistant modular exponentiation (and the ECC scalar-mult ladders)
294+
select the working value from one of two objects on each secret exponent bit.
295+
By default that selection is **address-oblivious**: both objects are accessed
296+
every iteration through a masked constant-time copy (`_sp_cond_copy` in
297+
`sp_int.c`, `sp_cond_select`/`sp_cond_memcpy` in the specialised `sp_<arch>.c`,
298+
`fp_cond_copy` in `tfm.c`), so the touched memory address does not depend on
299+
the secret. This resists a co-resident attacker that can observe cache-line
300+
accesses, for example `Prime+Probe` from a sibling SMT thread sharing the L1
301+
data cache. Defining `WOLFSSL_SP_FAST_MODEXP_COPY` switches to the older,
302+
faster variant that computes a single secret-dependent pointer by address
303+
masking and copies only the selected object; it is a few percent faster on private-key operations but
305304
discloses the exponent bits at cache-line granularity, so enable it only where a
306305
co-resident cache observer is outside your threat model. It requires rebuilding a
307306
pointer from integer arithmetic on two addresses, which capability targets (those
308307
that define `WC_NO_PTR_INT_CAST`, such as CHERI and Fil-C) cannot do, so the
309308
option is ignored and the oblivious copy is always used there. It is also a no-op
310309
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.
310+
which select from a table scanned in full rather than from two objects, and
311+
for the SAKKE `F_p*` exponentiation in `sakke.c`, which performs the same
312+
operations for every exponent bit and always updates its working value with
313+
`mp_cond_copy`.
312314

313315
The generated SP back ends (`sp_c32.c`, `sp_c64.c` and the assembly targets
314316
`sp_x86_64.c`, `sp_arm64.c`, `sp_arm32.c`, `sp_armthumb.c`, `sp_cortexm.c`,

‎doc/dox_comments/header_files/doxygen_groups.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,12 @@
205205
The sender must know the identity of the receiver and the KMS Public Key.\n
206206
The receiver must have obtained a Receiver Secret Key (RSK) for the identity from a KMS in order to derive the secret.
207207
208+
Setting an RNG on a SAKKE Key with wc_SetSakkeRng() is recommended for
209+
performance - it allows operations on secrets to be blinded.
210+
208211
KMS:
209212
-# Initialize SAKKE Key: wc_InitSakkeKey()
213+
-# [Recommended] Set an RNG for blinding: wc_SetSakkeRng()
210214
-# Make and save or load SAKKE Key:
211215
-# wc_MakeSakkeKey(), wc_ExportSakkeKey(), wc_ExportSakkePublicKey() or
212216
-# wc_ImportSakkeKey()
@@ -217,6 +221,7 @@
217221
218222
Key Exchange, Peer A:
219223
-# Initialize SAKKE Key: wc_InitSakkeKey()
224+
-# [Recommended] Set an RNG for blinding: wc_SetSakkeRng()
220225
-# Load KMS Public Key: wc_ImportSakkePublicKey()
221226
-# Generate a random SSV: wc_GenerateSakkeSSV()
222227
-# Set the identity of Peer B: wc_SetSakkeIdentity()
@@ -227,6 +232,7 @@
227232
Key Exchange, Peer B:
228233
-# Receive encapsulated data.
229234
-# Initialize SAKKE Key: wc_InitSakkeKey()
235+
-# [Recommended] Set an RNG for blinding: wc_SetSakkeRng()
230236
-# Load KMS Public Key: wc_ImportSakkePublicKey()
231237
-# Decode RSK transferred from KMS or stored locally: wc_DecodeSakkeRsk()
232238
-# [Optional] Validate RSK before first use: wc_ValidateSakkeRsk()
@@ -237,6 +243,7 @@
237243
238244
Transfer secret, Peer A:
239245
-# Initialize SAKKE Key: wc_InitSakkeKey()
246+
-# [Recommended] Set an RNG for blinding: wc_SetSakkeRng()
240247
-# Load KMS Public Key: wc_ImportSakkePublicKey()
241248
-# Set the identity of Peer B: wc_SetSakkeIdentity()
242249
-# Make an encapsulation of the SSV and auth data: wc_MakeSakkeEncapsulatedSSV()
@@ -245,6 +252,7 @@
245252
246253
Transfer secret, Peer B:
247254
-# Initialize SAKKE Key: wc_InitSakkeKey()
255+
-# [Recommended] Set an RNG for blinding: wc_SetSakkeRng()
248256
-# Load KMS Public Key: wc_ImportSakkePublicKey()
249257
-# Decode RSK transferred from KMS or stored locally: wc_DecodeSakkeRsk()
250258
-# [Optional] Validate RSK before first use: wc_ValidateSakkeRsk()

‎doc/dox_comments/header_files/sakke.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ int wc_InitSakkeKey_ex(SakkeKey* key, int keySize, int curveId,
1212
\ingroup SAKKE_Setup
1313
*/
1414
void wc_FreeSakkeKey(SakkeKey* key);
15+
/*!
16+
\ingroup SAKKE_Setup
17+
18+
\brief Sets the random number generator used to blind operations on
19+
secret values. Recommended for performance: operations on secrets are
20+
constant time without an RNG, but blinding allows faster algorithms to be
21+
used. It speeds up wc_MakeSakkeRsk() (KMS), which otherwise instantiates
22+
an RNG for each call, and, without SP code,
23+
wc_MakeSakkeEncapsulatedSSV(), wc_ValidateSakkeRsk() and
24+
wc_DeriveSakkeSSV(). The RNG is not owned by the key and must remain valid
25+
while set. wc_MakeSakkeKey() uses the RNG passed to it when none is set.
26+
27+
\return 0 on success.
28+
\return BAD_FUNC_ARG when key is NULL.
29+
30+
\param [in] key SAKKE key.
31+
\param [in] rng Random number generator. NULL to clear.
32+
*/
33+
int wc_SetSakkeRng(SakkeKey* key, WC_RNG* rng);
1534

1635
/*!
1736
\ingroup SAKKE_Setup

‎tests/unit-mcdc/test_eccsi_whitebox.c‎

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,39 @@ WB_MAYBE_UNUSED static int wb_am_addmod(const mp_int* a, const mp_int* b,
178178
return ret;
179179
}
180180

181+
/* Constant time signing adds with mp_addmod_ct() - same forcing modes. */
182+
WB_MAYBE_UNUSED static int wb_am_addmod_ct(const mp_int* a, const mp_int* b,
183+
const mp_int* m, mp_int* r)
184+
{
185+
int mode = WB_AM_OFF;
186+
int ret;
187+
188+
if ((wb_am_mode != WB_AM_OFF) && (r == wb_am_dst)) {
189+
mode = wb_am_mode;
190+
wb_am_mode = WB_AM_OFF;
191+
}
192+
if (mode == WB_AM_FAIL) {
193+
return MP_VAL;
194+
}
195+
ret = mp_addmod_ct((mp_int*)a, (mp_int*)b, (mp_int*)m, r);
196+
if (ret == 0) {
197+
if (mode == WB_AM_ZERO) {
198+
mp_zero(r);
199+
}
200+
else if (mode == WB_AM_COPY_A) {
201+
ret = mp_copy((mp_int*)a, r);
202+
}
203+
else if (mode == WB_AM_COPY_B) {
204+
ret = mp_copy((mp_int*)b, r);
205+
}
206+
}
207+
return ret;
208+
}
209+
181210
#undef mp_addmod
182211
#define mp_addmod(a, b, m, r) wb_am_addmod((a), (b), (m), (r))
212+
#undef mp_addmod_ct
213+
#define mp_addmod_ct(a, b, m, r) wb_am_addmod_ct((a), (b), (m), (r))
183214

184215
#endif /* WOLFCRYPT_HAVE_ECCSI */
185216

@@ -218,6 +249,14 @@ static void wb_eccsi_retry_loops(void)
218249
byte hash[WC_MAX_DIGEST_SIZE];
219250
int ready = 0;
220251
int ret;
252+
mp_digit ctMp = 0;
253+
#ifdef ECCSI_SIGN_CT
254+
mp_int ctTmp[ECCSI_CT_CNT];
255+
mp_int* ct = ctTmp;
256+
int ctReady = 0;
257+
#else
258+
mp_int* ct = NULL;
259+
#endif
221260

222261
XMEMSET(&rng, 0, sizeof(rng));
223262
XMEMSET(&key, 0, sizeof(key));
@@ -319,11 +358,20 @@ static void wb_eccsi_retry_loops(void)
319358
wb_fail = 1;
320359
goto done;
321360
}
361+
#ifdef ECCSI_SIGN_CT
362+
/* Constant time signing temporaries, as wc_SignEccsiHash() sets up. */
363+
if (eccsi_sign_ct_init(&key, ct, &ctMp) != 0) {
364+
WB_NOTE("eccsi constant time setup failed; 1934 vectors skipped");
365+
wb_fail = 1;
366+
goto done;
367+
}
368+
ctReady = 1;
369+
#endif
322370

323371
/* (d) all-false single-pass exit. */
324372
wb_am_mode = WB_AM_OFF;
325373
ret = eccsi_gen_sig(&key, &rng, WC_HASH_TYPE_SHA256, msg,
326-
(word32)sizeof(msg), &r, &s);
374+
(word32)sizeof(msg), &r, &s, ctMp, ct);
327375
if (ret != 0) {
328376
WB_NOTE("eccsi_gen_sig baseline (1934 all-false) failed");
329377
wb_fail = 1;
@@ -333,20 +381,22 @@ static void wb_eccsi_retry_loops(void)
333381
wb_am_dst = &s;
334382
wb_am_mode = WB_AM_ZERO;
335383
ret = eccsi_gen_sig(&key, &rng, WC_HASH_TYPE_SHA256, msg,
336-
(word32)sizeof(msg), &r, &s);
384+
(word32)sizeof(msg), &r, &s, ctMp, ct);
337385
wb_am_mode = WB_AM_OFF;
338386
if (ret != 0) {
339387
WB_NOTE("eccsi_gen_sig zero-s retry vector failed");
340388
wb_fail = 1;
341389
}
342390

343-
/* (c) mp_cmp(s, he) == MP_EQ on pass 1, retry, then normal exit.
391+
/* (c) s == he on pass 1, retry, then normal exit.
344392
* Operand a of the 1931 mp_addmod IS he (== &key->tmp), which
345-
* eccsi_gen_sig() reloads from the digest on every iteration. */
393+
* eccsi_gen_sig() reloads from the digest on every iteration. With
394+
* constant time signing, operand a of the mp_addmod_ct() is he mod q and
395+
* s is compared with it without an early exit. */
346396
wb_am_dst = &s;
347397
wb_am_mode = WB_AM_COPY_A;
348398
ret = eccsi_gen_sig(&key, &rng, WC_HASH_TYPE_SHA256, msg,
349-
(word32)sizeof(msg), &r, &s);
399+
(word32)sizeof(msg), &r, &s, ctMp, ct);
350400
wb_am_mode = WB_AM_OFF;
351401
if (ret != 0) {
352402
WB_NOTE("eccsi_gen_sig s==he retry vector failed");
@@ -357,14 +407,21 @@ static void wb_eccsi_retry_loops(void)
357407
wb_am_dst = &s;
358408
wb_am_mode = WB_AM_FAIL;
359409
(void)eccsi_gen_sig(&key, &rng, WC_HASH_TYPE_SHA256, msg,
360-
(word32)sizeof(msg), &r, &s);
410+
(word32)sizeof(msg), &r, &s, ctMp, ct);
361411
wb_am_mode = WB_AM_OFF;
362412

363413
WB_NOTE("eccsi_gen_sig 1934 retry-loop pairs exercised");
364414

365415
done:
366416
wb_am_mode = WB_AM_OFF;
367417
wb_am_dst = NULL;
418+
#ifdef ECCSI_SIGN_CT
419+
if (ctReady) {
420+
eccsi_sign_ct_free(ct);
421+
}
422+
#endif
423+
(void)ctMp;
424+
(void)ct;
368425
mp_free(&s);
369426
mp_free(&r);
370427
mp_forcezero(&ssk);

‎tests/unit-mcdc/test_sakke_fault_whitebox.c‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -640,19 +640,22 @@ int main(int argc, char** argv)
640640
*
641641
* The accepting (T,T) row at 1632 is the unarmed a == m call right
642642
* before it; the (T,F) row comes from the a=40,m=100 tplmod calls
643-
* in the sweep below (300 -> 200 -> 100 -> stop). */
643+
* in the sweep below (300 -> 200 -> 100 -> stop).
644+
*
645+
* With constant time modular addition (SAKKE_HAVE_ADDMOD_CT) the
646+
* helper is two branch-free mp_addmod_ct() calls instead. */
644647
{
645-
mp_int a, m, r;
648+
mp_int a, m, r, t;
646649
int e0, e1;
647-
mp_init(&a); mp_init(&m); mp_init(&r);
650+
mp_init(&a); mp_init(&m); mp_init(&r); mp_init(&t);
648651
mp_set(&a, 100); mp_set(&m, 100);
649-
e1 = sakke_tplmod(&a, &m, &r); /* 1632 (T,T) */
652+
e1 = sakke_tplmod(&a, &m, &r, &t); /* 1632 (T,T) */
650653
mcdc_fm_arm(3);
651-
e0 = sakke_tplmod(&a, &m, &r); /* 1632 (F,-) */
654+
e0 = sakke_tplmod(&a, &m, &r, &t); /* 1632 (F,-) */
652655
mcdc_fm_disarm();
653656
printf(" [wb] sakke_tplmod 1632 vectors: (T,T)=%d armed=%d\n",
654657
e1, e0);
655-
mp_free(&a); mp_free(&m); mp_free(&r);
658+
mp_free(&a); mp_free(&m); mp_free(&r); mp_free(&t);
656659
}
657660

658661
/* sakke_addmod 1604 idx0, same shape but one reduction: a direct
@@ -673,17 +676,19 @@ int main(int argc, char** argv)
673676
}
674677

675678
for (n = 1; n <= SAKKE_K_SMALLMP; n++) {
676-
mp_int a, b, m, r;
679+
mp_int a, b, m, r, t;
677680
mp_init(&a); mp_init(&b); mp_init(&m); mp_init(&r);
681+
mp_init(&t);
678682
mp_set(&a, 60); mp_set(&b, 70); mp_set(&m, 100);
679683
mcdc_fa_arm(n);
680684
(void)sakke_addmod(&a, &b, &m, &r);
681685
mcdc_fa_disarm();
682686
mp_set(&a, 40); mp_set(&m, 100);
683687
mcdc_fa_arm(n);
684-
(void)sakke_tplmod(&a, &m, &r);
688+
(void)sakke_tplmod(&a, &m, &r, &t);
685689
mcdc_fa_disarm();
686690
mp_free(&a); mp_free(&b); mp_free(&m); mp_free(&r);
691+
mp_free(&t);
687692
}
688693
WB_NOTE("sakke_addmod / sakke_tplmod fault sweep done");
689694
}

‎tests/unit-mcdc/test_sakke_whitebox.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,13 @@ int main(void)
198198
/* --- sakke.c:1604 sakke_addmod() / 1626,1629,1632 sakke_tplmod() ---
199199
* Pure mp_int reduction helpers -- no key state needed at all. */
200200
{
201-
mp_int a, b, m, r;
201+
mp_int a, b, m, r, t;
202202

203203
mp_init(&a);
204204
mp_init(&b);
205205
mp_init(&m);
206206
mp_init(&r);
207+
mp_init(&t);
207208

208209
/* sakke_addmod(): r < m (guard false, no subtraction). */
209210
mp_set(&a, 3); mp_set(&b, 4); mp_set(&m, 100);
@@ -215,24 +216,27 @@ int main(void)
215216

216217
/* sakke_tplmod(): 3*10=30 < 100 -- all three checks false. */
217218
mp_set(&a, 10); mp_set(&m, 100);
218-
if (sakke_tplmod(&a, &m, &r) != 0) wb_fail = 1;
219+
if (sakke_tplmod(&a, &m, &r, &t) != 0) wb_fail = 1;
219220
/* 3*40=120 -> one subtraction to 20 -- 1st check true, 2nd/3rd
220221
* false. */
221222
mp_set(&a, 40); mp_set(&m, 100);
222-
if (sakke_tplmod(&a, &m, &r) != 0) wb_fail = 1;
223+
if (sakke_tplmod(&a, &m, &r, &t) != 0) wb_fail = 1;
223224
/* 3*25=75 -> 45 -> 15 -- 1st and 2nd checks true, 3rd false. */
224225
mp_set(&a, 25); mp_set(&m, 30);
225-
if (sakke_tplmod(&a, &m, &r) != 0) wb_fail = 1;
226+
if (sakke_tplmod(&a, &m, &r, &t) != 0) wb_fail = 1;
226227
/* 3*11=33 -> 23 -> 13 -> 3 -- all three checks true. */
227228
mp_set(&a, 11); mp_set(&m, 10);
228-
if (sakke_tplmod(&a, &m, &r) != 0) wb_fail = 1;
229+
if (sakke_tplmod(&a, &m, &r, &t) != 0) wb_fail = 1;
230+
/* With constant time modular addition (SAKKE_HAVE_ADDMOD_CT) the
231+
* helper is branch-free and these only exercise it. */
229232
WB_NOTE("sakke_tplmod() 3-stage reduction (lines 1626/1629/1632) "
230233
"all true/false combinations exercised");
231234

232235
mp_free(&a);
233236
mp_free(&b);
234237
mp_free(&m);
235238
mp_free(&r);
239+
mp_free(&t);
236240
}
237241
#else
238242
WB_NOTE("WOLFSSL_HAVE_SP_ECC defined -- sakke_mulmod_base_add()/"

0 commit comments

Comments
 (0)