Skip to content

Commit e039009

Browse files
committed
SAKKE: 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. - Fix zMont not reset when making or importing a key. - Tests and docs for wc_SetSakkeRng().
1 parent c76acb6 commit e039009

19 files changed

Lines changed: 5842 additions & 693 deletions

File tree

‎.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

‎wolfcrypt/src/ecc.c‎

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,9 +2986,8 @@ int ecc_map_ex(ecc_point* P, mp_int* modulus, mp_digit mp, int ct)
29862986
#if defined(ECC_TIMING_RESISTANT) && (defined(USE_FAST_MATH) || \
29872987
defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL))
29882988
if (ct) {
2989+
/* 1/z in Montgomery form. */
29892990
err = mp_invmod_mont_ct(z, modulus, t1, mp);
2990-
if (err == MP_OKAY)
2991-
err = mp_montgomery_reduce(t1, modulus, mp);
29922991
}
29932992
else
29942993
#endif
@@ -3000,25 +2999,60 @@ int ecc_map_ex(ecc_point* P, mp_int* modulus, mp_digit mp, int ct)
30002999
}
30013000
}
30023001

3003-
/* get 1/z^2 and 1/z^3 */
3004-
if (err == MP_OKAY)
3005-
err = mp_sqr(t1, t2);
3006-
if (err == MP_OKAY)
3007-
err = mp_mod(t2, modulus, t2);
3008-
if (err == MP_OKAY)
3009-
err = mp_mul(t1, t2, t1);
3010-
if (err == MP_OKAY)
3011-
err = mp_mod(t1, modulus, t1);
3002+
#if defined(ECC_TIMING_RESISTANT) && (defined(USE_FAST_MATH) || \
3003+
defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL))
3004+
if (ct) {
3005+
/* Stay in Montgomery form - mp_mod() is not constant time. */
3006+
/* get 1/z^2 and 1/z^3 */
3007+
if (err == MP_OKAY)
3008+
err = mp_sqr(t1, t2);
3009+
if (err == MP_OKAY)
3010+
err = mp_montgomery_reduce_ct(t2, modulus, mp);
3011+
if (err == MP_OKAY)
3012+
err = mp_mul(t1, t2, t1);
3013+
if (err == MP_OKAY)
3014+
err = mp_montgomery_reduce_ct(t1, modulus, mp);
3015+
}
3016+
else
3017+
#endif
3018+
{
3019+
/* get 1/z^2 and 1/z^3 */
3020+
if (err == MP_OKAY)
3021+
err = mp_sqr(t1, t2);
3022+
if (err == MP_OKAY)
3023+
err = mp_mod(t2, modulus, t2);
3024+
if (err == MP_OKAY)
3025+
err = mp_mul(t1, t2, t1);
3026+
if (err == MP_OKAY)
3027+
err = mp_mod(t1, modulus, t1);
3028+
}
30123029

30133030
/* multiply against x/y */
30143031
if (err == MP_OKAY)
30153032
err = mp_mul(x, t2, x);
3016-
if (err == MP_OKAY)
3017-
err = mp_montgomery_reduce(x, modulus, mp);
30183033
if (err == MP_OKAY)
30193034
err = mp_mul(y, t1, y);
3020-
if (err == MP_OKAY)
3021-
err = mp_montgomery_reduce(y, modulus, mp);
3035+
#if defined(ECC_TIMING_RESISTANT) && (defined(USE_FAST_MATH) || \
3036+
defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL))
3037+
if (ct) {
3038+
if (err == MP_OKAY)
3039+
err = mp_montgomery_reduce_ct(x, modulus, mp);
3040+
if (err == MP_OKAY)
3041+
err = mp_montgomery_reduce_ct(y, modulus, mp);
3042+
/* 1/z^2 and 1/z^3 were in Montgomery form - x and y still are. */
3043+
if (err == MP_OKAY)
3044+
err = mp_montgomery_reduce_ct(x, modulus, mp);
3045+
if (err == MP_OKAY)
3046+
err = mp_montgomery_reduce_ct(y, modulus, mp);
3047+
}
3048+
else
3049+
#endif
3050+
{
3051+
if (err == MP_OKAY)
3052+
err = mp_montgomery_reduce(x, modulus, mp);
3053+
if (err == MP_OKAY)
3054+
err = mp_montgomery_reduce(y, modulus, mp);
3055+
}
30223056

30233057
if (err == MP_OKAY)
30243058
err = mp_set(z, 1);

0 commit comments

Comments
 (0)