Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/configs/os-check-linux.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@
"configure": ["--enable-ecc=nonblock", "--enable-sp=yes,nonblocklarge", "--disable-sp-asm",
"CPPFLAGS=-DWOLFSSL_PUBLIC_MP -DWOLFSSL_DEBUG_NONBLOCK"]},
{"name": "modexp-copy-default-sakke", "minutes": 2.6,
"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.",
"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.",
"configure": ["--enable-sakke", "--enable-sp-math-all"]},
{"name": "modexp-copy-fast-sakke", "minutes": 2.6,
"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.",
"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.",
"configure": ["--enable-sakke", "--enable-sp-math-all",
"CPPFLAGS=-DWOLFSSL_SP_FAST_MODEXP_COPY"]},
{"name": "sp-ladder-modexp", "minutes": 2.0,
Expand Down
28 changes: 15 additions & 13 deletions doc/ASM_AND_MATH_DEFINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,25 +290,27 @@ wolfSSL recommends against defining `WC_NO_HARDEN`, `WC_NO_CACHE_RESISTANT` or
your threat model genuinely excludes an attacker able to observe local timing or
cache behaviour. The performance gain is small relative to the protection lost.

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

The generated SP back ends (`sp_c32.c`, `sp_c64.c` and the assembly targets
`sp_x86_64.c`, `sp_arm64.c`, `sp_arm32.c`, `sp_armthumb.c`, `sp_cortexm.c`,
Expand Down
8 changes: 8 additions & 0 deletions doc/dox_comments/header_files/doxygen_groups.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,12 @@
The sender must know the identity of the receiver and the KMS Public Key.\n
The receiver must have obtained a Receiver Secret Key (RSK) for the identity from a KMS in order to derive the secret.

Setting an RNG on a SAKKE Key with wc_SetSakkeRng() is recommended for
performance - it allows operations on secrets to be blinded.

KMS:
-# Initialize SAKKE Key: wc_InitSakkeKey()
-# [Recommended] Set an RNG for blinding: wc_SetSakkeRng()
-# Make and save or load SAKKE Key:
-# wc_MakeSakkeKey(), wc_ExportSakkeKey(), wc_ExportSakkePublicKey() or
-# wc_ImportSakkeKey()
Expand All @@ -217,6 +221,7 @@

Key Exchange, Peer A:
-# Initialize SAKKE Key: wc_InitSakkeKey()
-# [Recommended] Set an RNG for blinding: wc_SetSakkeRng()
-# Load KMS Public Key: wc_ImportSakkePublicKey()
-# Generate a random SSV: wc_GenerateSakkeSSV()
-# Set the identity of Peer B: wc_SetSakkeIdentity()
Expand All @@ -227,6 +232,7 @@
Key Exchange, Peer B:
-# Receive encapsulated data.
-# Initialize SAKKE Key: wc_InitSakkeKey()
-# [Recommended] Set an RNG for blinding: wc_SetSakkeRng()
-# Load KMS Public Key: wc_ImportSakkePublicKey()
-# Decode RSK transferred from KMS or stored locally: wc_DecodeSakkeRsk()
-# [Optional] Validate RSK before first use: wc_ValidateSakkeRsk()
Expand All @@ -237,6 +243,7 @@

Transfer secret, Peer A:
-# Initialize SAKKE Key: wc_InitSakkeKey()
-# [Recommended] Set an RNG for blinding: wc_SetSakkeRng()
-# Load KMS Public Key: wc_ImportSakkePublicKey()
-# Set the identity of Peer B: wc_SetSakkeIdentity()
-# Make an encapsulation of the SSV and auth data: wc_MakeSakkeEncapsulatedSSV()
Expand All @@ -245,6 +252,7 @@

Transfer secret, Peer B:
-# Initialize SAKKE Key: wc_InitSakkeKey()
-# [Recommended] Set an RNG for blinding: wc_SetSakkeRng()
-# Load KMS Public Key: wc_ImportSakkePublicKey()
-# Decode RSK transferred from KMS or stored locally: wc_DecodeSakkeRsk()
-# [Optional] Validate RSK before first use: wc_ValidateSakkeRsk()
Expand Down
19 changes: 19 additions & 0 deletions doc/dox_comments/header_files/sakke.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ int wc_InitSakkeKey_ex(SakkeKey* key, int keySize, int curveId,
\ingroup SAKKE_Setup
*/
void wc_FreeSakkeKey(SakkeKey* key);
/*!
\ingroup SAKKE_Setup

\brief Sets the random number generator used to blind operations on
secret values. Recommended for performance: operations on secrets are
constant time without an RNG, but blinding allows faster algorithms to be
used. It speeds up wc_MakeSakkeRsk() (KMS), which otherwise instantiates
an RNG for each call, and, without SP code,
wc_MakeSakkeEncapsulatedSSV(), wc_ValidateSakkeRsk() and
wc_DeriveSakkeSSV(). The RNG is not owned by the key and must remain valid
while set. wc_MakeSakkeKey() uses the RNG passed to it when none is set.

\return 0 on success.
\return BAD_FUNC_ARG when key is NULL.

\param [in] key SAKKE key.
\param [in] rng Random number generator. NULL to clear.
*/
int wc_SetSakkeRng(SakkeKey* key, WC_RNG* rng);

/*!
\ingroup SAKKE_Setup
Expand Down
69 changes: 63 additions & 6 deletions tests/unit-mcdc/test_eccsi_whitebox.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,39 @@ WB_MAYBE_UNUSED static int wb_am_addmod(const mp_int* a, const mp_int* b,
return ret;
}

/* Constant time signing adds with mp_addmod_ct() - same forcing modes. */
WB_MAYBE_UNUSED static int wb_am_addmod_ct(const mp_int* a, const mp_int* b,
const mp_int* m, mp_int* r)
{
int mode = WB_AM_OFF;
int ret;

if ((wb_am_mode != WB_AM_OFF) && (r == wb_am_dst)) {
mode = wb_am_mode;
wb_am_mode = WB_AM_OFF;
}
if (mode == WB_AM_FAIL) {
return MP_VAL;
}
ret = mp_addmod_ct((mp_int*)a, (mp_int*)b, (mp_int*)m, r);
if (ret == 0) {
if (mode == WB_AM_ZERO) {
mp_zero(r);
}
else if (mode == WB_AM_COPY_A) {
ret = mp_copy((mp_int*)a, r);
}
else if (mode == WB_AM_COPY_B) {
ret = mp_copy((mp_int*)b, r);
}
}
return ret;
}

#undef mp_addmod
#define mp_addmod(a, b, m, r) wb_am_addmod((a), (b), (m), (r))
#undef mp_addmod_ct
#define mp_addmod_ct(a, b, m, r) wb_am_addmod_ct((a), (b), (m), (r))

#endif /* WOLFCRYPT_HAVE_ECCSI */

Expand Down Expand Up @@ -218,6 +249,14 @@ static void wb_eccsi_retry_loops(void)
byte hash[WC_MAX_DIGEST_SIZE];
int ready = 0;
int ret;
mp_digit ctMp = 0;
#ifdef ECCSI_SIGN_CT
mp_int ctTmp[ECCSI_CT_CNT];
mp_int* ct = ctTmp;
int ctReady = 0;
#else
mp_int* ct = NULL;
#endif

XMEMSET(&rng, 0, sizeof(rng));
XMEMSET(&key, 0, sizeof(key));
Expand Down Expand Up @@ -319,11 +358,20 @@ static void wb_eccsi_retry_loops(void)
wb_fail = 1;
goto done;
}
#ifdef ECCSI_SIGN_CT
/* Constant time signing temporaries, as wc_SignEccsiHash() sets up. */
if (eccsi_sign_ct_init(&key, ct, &ctMp) != 0) {
WB_NOTE("eccsi constant time setup failed; 1934 vectors skipped");
wb_fail = 1;
goto done;
}
ctReady = 1;
#endif

/* (d) all-false single-pass exit. */
wb_am_mode = WB_AM_OFF;
ret = eccsi_gen_sig(&key, &rng, WC_HASH_TYPE_SHA256, msg,
(word32)sizeof(msg), &r, &s);
(word32)sizeof(msg), &r, &s, ctMp, ct);
if (ret != 0) {
WB_NOTE("eccsi_gen_sig baseline (1934 all-false) failed");
wb_fail = 1;
Expand All @@ -333,20 +381,22 @@ static void wb_eccsi_retry_loops(void)
wb_am_dst = &s;
wb_am_mode = WB_AM_ZERO;
ret = eccsi_gen_sig(&key, &rng, WC_HASH_TYPE_SHA256, msg,
(word32)sizeof(msg), &r, &s);
(word32)sizeof(msg), &r, &s, ctMp, ct);
wb_am_mode = WB_AM_OFF;
if (ret != 0) {
WB_NOTE("eccsi_gen_sig zero-s retry vector failed");
wb_fail = 1;
}

/* (c) mp_cmp(s, he) == MP_EQ on pass 1, retry, then normal exit.
/* (c) s == he on pass 1, retry, then normal exit.
* Operand a of the 1931 mp_addmod IS he (== &key->tmp), which
* eccsi_gen_sig() reloads from the digest on every iteration. */
* eccsi_gen_sig() reloads from the digest on every iteration. With
* constant time signing, operand a of the mp_addmod_ct() is he mod q and
* s is compared with it without an early exit. */
wb_am_dst = &s;
wb_am_mode = WB_AM_COPY_A;
ret = eccsi_gen_sig(&key, &rng, WC_HASH_TYPE_SHA256, msg,
(word32)sizeof(msg), &r, &s);
(word32)sizeof(msg), &r, &s, ctMp, ct);
wb_am_mode = WB_AM_OFF;
if (ret != 0) {
WB_NOTE("eccsi_gen_sig s==he retry vector failed");
Expand All @@ -357,14 +407,21 @@ static void wb_eccsi_retry_loops(void)
wb_am_dst = &s;
wb_am_mode = WB_AM_FAIL;
(void)eccsi_gen_sig(&key, &rng, WC_HASH_TYPE_SHA256, msg,
(word32)sizeof(msg), &r, &s);
(word32)sizeof(msg), &r, &s, ctMp, ct);
wb_am_mode = WB_AM_OFF;

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

done:
wb_am_mode = WB_AM_OFF;
wb_am_dst = NULL;
#ifdef ECCSI_SIGN_CT
if (ctReady) {
eccsi_sign_ct_free(ct);
}
#endif
(void)ctMp;
(void)ct;
mp_free(&s);
mp_free(&r);
mp_forcezero(&ssk);
Expand Down
21 changes: 13 additions & 8 deletions tests/unit-mcdc/test_sakke_fault_whitebox.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,19 +640,22 @@ int main(int argc, char** argv)
*
* The accepting (T,T) row at 1632 is the unarmed a == m call right
* before it; the (T,F) row comes from the a=40,m=100 tplmod calls
* in the sweep below (300 -> 200 -> 100 -> stop). */
* in the sweep below (300 -> 200 -> 100 -> stop).
*
* With constant time modular addition (SAKKE_HAVE_ADDMOD_CT) the
* helper is two branch-free mp_addmod_ct() calls instead. */
{
mp_int a, m, r;
mp_int a, m, r, t;
int e0, e1;
mp_init(&a); mp_init(&m); mp_init(&r);
mp_init(&a); mp_init(&m); mp_init(&r); mp_init(&t);
mp_set(&a, 100); mp_set(&m, 100);
e1 = sakke_tplmod(&a, &m, &r); /* 1632 (T,T) */
e1 = sakke_tplmod(&a, &m, &r, &t); /* 1632 (T,T) */
mcdc_fm_arm(3);
e0 = sakke_tplmod(&a, &m, &r); /* 1632 (F,-) */
e0 = sakke_tplmod(&a, &m, &r, &t); /* 1632 (F,-) */
mcdc_fm_disarm();
printf(" [wb] sakke_tplmod 1632 vectors: (T,T)=%d armed=%d\n",
e1, e0);
mp_free(&a); mp_free(&m); mp_free(&r);
mp_free(&a); mp_free(&m); mp_free(&r); mp_free(&t);
}

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

for (n = 1; n <= SAKKE_K_SMALLMP; n++) {
mp_int a, b, m, r;
mp_int a, b, m, r, t;
mp_init(&a); mp_init(&b); mp_init(&m); mp_init(&r);
mp_init(&t);
mp_set(&a, 60); mp_set(&b, 70); mp_set(&m, 100);
mcdc_fa_arm(n);
(void)sakke_addmod(&a, &b, &m, &r);
mcdc_fa_disarm();
mp_set(&a, 40); mp_set(&m, 100);
mcdc_fa_arm(n);
(void)sakke_tplmod(&a, &m, &r);
(void)sakke_tplmod(&a, &m, &r, &t);
mcdc_fa_disarm();
mp_free(&a); mp_free(&b); mp_free(&m); mp_free(&r);
mp_free(&t);
}
WB_NOTE("sakke_addmod / sakke_tplmod fault sweep done");
}
Expand Down
14 changes: 9 additions & 5 deletions tests/unit-mcdc/test_sakke_whitebox.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,13 @@ int main(void)
/* --- sakke.c:1604 sakke_addmod() / 1626,1629,1632 sakke_tplmod() ---
* Pure mp_int reduction helpers -- no key state needed at all. */
{
mp_int a, b, m, r;
mp_int a, b, m, r, t;

mp_init(&a);
mp_init(&b);
mp_init(&m);
mp_init(&r);
mp_init(&t);

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

/* sakke_tplmod(): 3*10=30 < 100 -- all three checks false. */
mp_set(&a, 10); mp_set(&m, 100);
if (sakke_tplmod(&a, &m, &r) != 0) wb_fail = 1;
if (sakke_tplmod(&a, &m, &r, &t) != 0) wb_fail = 1;
/* 3*40=120 -> one subtraction to 20 -- 1st check true, 2nd/3rd
* false. */
mp_set(&a, 40); mp_set(&m, 100);
if (sakke_tplmod(&a, &m, &r) != 0) wb_fail = 1;
if (sakke_tplmod(&a, &m, &r, &t) != 0) wb_fail = 1;
/* 3*25=75 -> 45 -> 15 -- 1st and 2nd checks true, 3rd false. */
mp_set(&a, 25); mp_set(&m, 30);
if (sakke_tplmod(&a, &m, &r) != 0) wb_fail = 1;
if (sakke_tplmod(&a, &m, &r, &t) != 0) wb_fail = 1;
/* 3*11=33 -> 23 -> 13 -> 3 -- all three checks true. */
mp_set(&a, 11); mp_set(&m, 10);
if (sakke_tplmod(&a, &m, &r) != 0) wb_fail = 1;
if (sakke_tplmod(&a, &m, &r, &t) != 0) wb_fail = 1;
/* With constant time modular addition (SAKKE_HAVE_ADDMOD_CT) the
* helper is branch-free and these only exercise it. */
WB_NOTE("sakke_tplmod() 3-stage reduction (lines 1626/1629/1632) "
"all true/false combinations exercised");

mp_free(&a);
mp_free(&b);
mp_free(&m);
mp_free(&r);
mp_free(&t);
}
#else
WB_NOTE("WOLFSSL_HAVE_SP_ECC defined -- sakke_mulmod_base_add()/"
Expand Down
Loading
Loading