Skip to content

Commit b323439

Browse files
tfm: clear the digit above the modulus in the CT modular helpers
- fp_addmod_ct(), fp_submod_ct() and fp_div_2_mod_ct() zero the destination's digit at the modulus length after writing the result. - mp_test_addsubmod_ct() runs on fast math and starts each destination one digit wider than the modulus; it and mp_test_mont() check the result with mp_test_no_digits_above_used(). - os-check-linux.json gains a fastmath-mont-red-ct leg. Issue: F-18450, F-18447
1 parent d1a69c6 commit b323439

3 files changed

Lines changed: 32 additions & 10 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@
123123
{"name": "fastmath-modexp-copy-default", "minutes": 2.4,
124124
"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.",
125125
"configure": ["--enable-fastmath"]},
126+
{"name": "fastmath-mont-red-ct", "minutes": 2.4,
127+
"comment": "Only build of WOLFSSL_MONT_RED_CT, where tfm.c's Montgomery reduction ends with fp_submod_ct. Public mp so testwolfcrypt's mp_test and the wolfmath API tests run on fast math; custom curves add the Koblitz and Brainpool curves whose moduli make the reduction carry.",
128+
"configure": ["--enable-fastmath", "--enable-ecccustcurves=all",
129+
"CPPFLAGS=-DWOLFSSL_MONT_RED_CT -DWOLFSSL_PUBLIC_MP"]},
126130
{"name": "fastmath-modexp-copy-fast", "minutes": 2.4,
127131
"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.",
128132
"configure": ["--enable-fastmath",

‎wolfcrypt/src/tfm.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,9 @@ int fp_div_2_mod_ct(fp_int *a, fp_int *b, fp_int *c)
957957
}
958958
c->dp[i] = (c->dp[i] >> 1) | ((fp_digit)w << (DIGIT_BIT - 1));
959959
c->used = i + 1;
960+
if (c->used < FP_SIZE) {
961+
c->dp[c->used] = 0;
962+
}
960963
c->sign = FP_ZPOS;
961964
fp_clamp(c);
962965

@@ -1612,6 +1615,7 @@ int fp_submod_ct(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
16121615
}
16131616
/* Result will always have digits equal to or less than those in modulus. */
16141617
d->used = i;
1618+
d->dp[i] = 0;
16151619
d->sign = FP_ZPOS;
16161620
fp_clamp(d);
16171621

@@ -1661,6 +1665,9 @@ int fp_addmod_ct(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
16611665
}
16621666
/* Result will always have digits equal to or less than those in modulus. */
16631667
d->used = i;
1668+
if (i < FP_SIZE) {
1669+
d->dp[i] = 0;
1670+
}
16641671
d->sign = FP_ZPOS;
16651672
fp_clamp(d);
16661673

‎wolfcrypt/test/test.c‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98347,7 +98347,8 @@ static wc_test_ret_t mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1,
9834798347
return 0;
9834898348
}
9834998349

98350-
#if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
98350+
#if (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
98351+
defined(HAVE_ECC)
9835198352
/* Functional tests for the constant-time modular arithmetic used by ECC:
9835298353
* mp_addmod_ct, mp_submod_ct and mp_div_2_mod_ct. These are not reached by
9835398354
* the parameter-validation tests so their computation paths are exercised
@@ -98378,49 +98379,56 @@ static wc_test_ret_t mp_test_addsubmod_ct(mp_int* a, mp_int* b, mp_int* m,
9837898379
break;
9837998380
}
9838098381

98381-
/* addmod_ct must match the variable-time addmod. */
98382-
ret = mp_addmod_ct(a, b, m, r);
98382+
/* addmod_ct must match the variable-time addmod. The destination
98383+
* starts one digit wider than the modulus each time. */
98384+
ret = mp_2expt(r, m->used * DIGIT_BIT);
98385+
if (ret == MP_OKAY)
98386+
ret = mp_addmod_ct(a, b, m, r);
9838398387
if (ret == MP_OKAY)
9838498388
ret = mp_addmod(a, b, m, t);
9838598389
if (ret != MP_OKAY) {
9838698390
res = WC_TEST_RET_ENC_EC(ret);
9838798391
break;
9838898392
}
98389-
if (mp_cmp(r, t) != MP_EQ) {
98393+
if ((mp_cmp(r, t) != MP_EQ) || !mp_test_no_digits_above_used(r)) {
9839098394
res = WC_TEST_RET_ENC_NC;
9839198395
break;
9839298396
}
9839398397

9839498398
/* submod_ct: (a - b) mod m, verified by adding b back gives a. */
98395-
ret = mp_submod_ct(a, b, m, r);
98399+
ret = mp_2expt(r, m->used * DIGIT_BIT);
98400+
if (ret == MP_OKAY)
98401+
ret = mp_submod_ct(a, b, m, r);
9839698402
if (ret == MP_OKAY)
9839798403
ret = mp_addmod_ct(r, b, m, t);
9839898404
if (ret != MP_OKAY) {
9839998405
res = WC_TEST_RET_ENC_EC(ret);
9840098406
break;
9840198407
}
98402-
if (mp_cmp(t, a) != MP_EQ) {
98408+
if ((mp_cmp(t, a) != MP_EQ) || !mp_test_no_digits_above_used(r)) {
9840398409
res = WC_TEST_RET_ENC_NC;
9840498410
break;
9840598411
}
9840698412

9840798413
/* div_2_mod_ct: r = a/2 mod m, verified by doubling: 2*r mod m == a. */
98408-
ret = mp_div_2_mod_ct(a, m, r);
98414+
ret = mp_2expt(r, m->used * DIGIT_BIT);
98415+
if (ret == MP_OKAY)
98416+
ret = mp_div_2_mod_ct(a, m, r);
9840998417
if (ret == MP_OKAY)
9841098418
ret = mp_addmod_ct(r, r, m, t);
9841198419
if (ret != MP_OKAY) {
9841298420
res = WC_TEST_RET_ENC_EC(ret);
9841398421
break;
9841498422
}
98415-
if (mp_cmp(t, a) != MP_EQ) {
98423+
if ((mp_cmp(t, a) != MP_EQ) || !mp_test_no_digits_above_used(r)) {
9841698424
res = WC_TEST_RET_ENC_NC;
9841798425
break;
9841898426
}
9841998427
}
9842098428

9842198429
return res;
9842298430
}
98423-
#endif /* WOLFSSL_SP_MATH_ALL && HAVE_ECC */
98431+
#endif /* (WOLFSSL_SP_MATH_ALL || USE_FAST_MATH) && HAVE_ECC */
9842498432

9842598433
#if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
9842698434
/* Exercises mp_cmp_mag - magnitude comparison (sign-independent) including
@@ -99198,6 +99206,8 @@ static wc_test_ret_t mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC
9919899206
/* Result is m or 0 if reduced to range of modulus. */
9919999207
if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
9920099208
return WC_TEST_RET_ENC_NC;
99209+
if (!mp_test_no_digits_above_used(a))
99210+
return WC_TEST_RET_ENC_NC;
9920199211
}
9920299212

9920399213
#ifdef WOLFSSL_SP_INVMOD_MONT_CT
@@ -99604,7 +99614,8 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void)
9960499614
if ((ret = mp_test_mont(a, b, r1, r2, &rng)) != 0)
9960599615
goto done;
9960699616
#endif
99607-
#if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
99617+
#if (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
99618+
defined(HAVE_ECC)
9960899619
if ((ret = mp_test_addsubmod_ct(a, b, r1, r2, p, &rng)) != 0)
9960999620
goto done;
9961099621
#endif

0 commit comments

Comments
 (0)