-
Notifications
You must be signed in to change notification settings - Fork 1k
Constant time hardening #11159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Constant time hardening #11159
Changes from all commits
5df8022
0d9994d
0fa0b1d
024fcd1
c87338e
23cc479
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13780,7 +13780,9 @@ static int build_lut(int idx, mp_int* a, mp_int* modulus, mp_digit mp, | |
| return err; | ||
| } | ||
|
|
||
| /* perform a fixed point ECC mulmod */ | ||
| #ifndef ECC_TIMING_RESISTANT | ||
| /* perform a fixed point ECC mulmod. Not constant-time; do not use with | ||
| * secret scalars. */ | ||
| static int accel_fp_mul(int idx, const mp_int* k, ecc_point *R, mp_int* a, | ||
| mp_int* modulus, mp_digit mp, int map) | ||
| { | ||
|
|
@@ -13960,6 +13962,7 @@ static int accel_fp_mul(int idx, const mp_int* k, ecc_point *R, mp_int* a, | |
|
|
||
| return err; | ||
| } | ||
| #endif /* !ECC_TIMING_RESISTANT */ | ||
| #endif | ||
|
|
||
| #ifdef ECC_SHAMIR | ||
|
|
@@ -14456,6 +14459,7 @@ int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a, | |
| } | ||
|
|
||
|
|
||
| #ifndef ECC_TIMING_RESISTANT | ||
|
stenslae marked this conversation as resolved.
stenslae marked this conversation as resolved.
|
||
| if (err == MP_OKAY) { | ||
| /* if it's 2 build the LUT, if it's higher just use the LUT */ | ||
| if (idx >= 0 && fp_cache[idx].lru_count >= 2 && !fp_cache[idx].LUT_set) { | ||
|
|
@@ -14486,6 +14490,15 @@ int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a, | |
| err = normal_ecc_mulmod(k, G, R, a, modulus, NULL, map, heap); | ||
| } | ||
| } | ||
| #else | ||
| /* No RNG here, so FP-cache LUT can't be blinded; skip building/using | ||
| * it and always take the constant-time ladder. */ | ||
| if (err == MP_OKAY) { | ||
| err = normal_ecc_mulmod(k, G, R, a, modulus, NULL, map, heap); | ||
| } | ||
| #endif | ||
| (void)mp; | ||
| (void)mpSetup; | ||
|
|
||
| out: | ||
|
|
||
|
|
@@ -14615,6 +14628,12 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a, | |
| } | ||
|
|
||
|
|
||
| #if !defined(ECC_TIMING_RESISTANT) || defined(ECC_SHAMIR) | ||
| /* Build/refresh the FP-cache LUT for this point. Needed directly by | ||
| * accel_fp_mul below when not timing-resistant, and also pre-warms | ||
| * the cache for accel_fp_mul2add's Shamir-trick path (public-scalar | ||
| * only, safe without RNG blinding), which may hit this same point | ||
| * later even when the ladder is used here. */ | ||
| if (err == MP_OKAY) { | ||
| /* if it's 2 build the LUT, if it's higher just use the LUT */ | ||
| if (idx >= 0 && fp_cache[idx].lru_count >= 2 && !fp_cache[idx].LUT_set) { | ||
|
|
@@ -14632,7 +14651,16 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a, | |
| err = build_lut(idx, a, modulus, mp, mu); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⚪ [Info] wc_ecc_mulmod_ex2 builds an FP-cache LUT it never uses · Dead/unreachable code Under Fix: Skip the LUT build in |
||
| } | ||
| } | ||
|
stenslae marked this conversation as resolved.
stenslae marked this conversation as resolved.
|
||
| #endif | ||
|
|
||
| #ifdef ECC_TIMING_RESISTANT | ||
| if (err == MP_OKAY) { | ||
| /* accel_fp_mul is not safe for secret scalars. Fall back to ladder. */ | ||
| (void)mpSetup; | ||
| (void)mp; | ||
| err = normal_ecc_mulmod(k, G, R, a, modulus, rng, map, heap); | ||
| } | ||
| #else | ||
| if (err == MP_OKAY) { | ||
| if (idx >= 0 && fp_cache[idx].LUT_set) { | ||
| if (mpSetup == 0) { | ||
|
|
@@ -14645,6 +14673,7 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a, | |
| err = normal_ecc_mulmod(k, G, R, a, modulus, rng, map, heap); | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
| out: | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 [Low] build_lut/lut_orders become unused and the FP cache becomes dead weight when ECC_TIMING_RESISTANT is set without… · Dead/unreachable code
With
FP_ECC+ECC_TIMING_RESISTANTandECC_SHAMIRundefined, every call site ofbuild_lut(ecc.c:14477, 14651, and thefp_ecc_mul2addsites) is compiled out, leavingbuild_lutand thelut_orderstable with no users —-Wunused-function/-Wunused-const-variablebreaks-Werrorbuilds.add_entrystill allocates2^FP_LUTecc_points per cache entry that are never populated or read.Fix: Guard
build_lut/lut_orderswith#if !defined(ECC_TIMING_RESISTANT) || defined(ECC_SHAMIR)and skip the fp-cache entry allocation when no LUT consumer is compiled in.