Skip to content

Commit 67293f2

Browse files
committed
ECC, TFM: constant time point arithmetic, signing and modexp
Use constant time Montgomery reduction and compares in ECC point operations, ECDH shared secret output and ECDSA signing mod n. Constant time, fixed length TFM modular exponentiation including base 2.
1 parent 626840e commit 67293f2

8 files changed

Lines changed: 1363 additions & 239 deletions

File tree

‎tests/api/test_wolfmath.c‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,69 @@ int test_mp_exptmod_even_2k(void)
217217
return EXPECT_RESULT();
218218
}
219219

220+
/*
221+
* Testing TFM exponentiation with a modulus of FP_SIZE / 2 digits - the largest
222+
* accepted. Constant time exponentiation keeps working values at the length of
223+
* the modulus so products use all FP_SIZE digits.
224+
*/
225+
int test_mp_exptmod_fp_size_boundary(void)
226+
{
227+
EXPECT_DECLS;
228+
#if defined(USE_FAST_MATH) && defined(WOLFSSL_PUBLIC_MP) && \
229+
defined(TFM_TIMING_RESISTANT) && !defined(WC_NO_MODEXP_TIMING_RESISTANT)
230+
mp_int g, x, p, y, e;
231+
int k = (FP_SIZE / 2) * DIGIT_BIT;
232+
233+
XMEMSET(&g, 0, sizeof(g));
234+
XMEMSET(&x, 0, sizeof(x));
235+
XMEMSET(&p, 0, sizeof(p));
236+
XMEMSET(&y, 0, sizeof(y));
237+
XMEMSET(&e, 0, sizeof(e));
238+
ExpectIntEQ(mp_init_multi(&g, &x, &p, &y, &e, NULL), MP_OKAY);
239+
240+
/* p = 2^k - 1: odd and all FP_SIZE / 2 digits set. */
241+
ExpectIntEQ(mp_set(&p, 1), MP_OKAY);
242+
ExpectIntEQ(mp_mul_2d(&p, k, &p), MP_OKAY);
243+
ExpectIntEQ(mp_sub_d(&p, 1, &p), MP_OKAY);
244+
ExpectIntEQ(p.used, FP_SIZE / 2);
245+
246+
/* Base 2: 2^(k.2^64 + 7) mod (2^k - 1) = 2^7 as 2^k = 1 mod p. */
247+
ExpectIntEQ(mp_set(&g, 2), MP_OKAY);
248+
ExpectIntEQ(mp_set_int(&x, (unsigned long)k), MP_OKAY);
249+
ExpectIntEQ(mp_mul_2d(&x, 64, &x), MP_OKAY);
250+
ExpectIntEQ(mp_add_d(&x, 7, &x), MP_OKAY);
251+
ExpectIntEQ(mp_exptmod(&g, &x, &p, &y), MP_OKAY);
252+
ExpectIntEQ(mp_cmp_d(&y, 128), MP_EQ);
253+
254+
/* General base: (p - 1)^x mod p = p - 1 when x odd and 1 when x even.
255+
* Full width base and exponent. */
256+
ExpectIntEQ(mp_sub_d(&p, 1, &g), MP_OKAY);
257+
ExpectIntEQ(mp_sub_d(&p, 2, &x), MP_OKAY);
258+
ExpectIntEQ(mp_exptmod(&g, &x, &p, &y), MP_OKAY);
259+
ExpectIntEQ(mp_cmp(&y, &g), MP_EQ);
260+
ExpectIntEQ(mp_sub_d(&p, 3, &x), MP_OKAY);
261+
ExpectIntEQ(mp_exptmod(&g, &x, &p, &y), MP_OKAY);
262+
ExpectIntEQ(mp_cmp_d(&y, 1), MP_EQ);
263+
264+
/* Exponent processed as a fixed number of digits. */
265+
ExpectIntEQ(mp_sub_d(&p, 2, &x), MP_OKAY);
266+
ExpectIntEQ(mp_exptmod_ex(&g, &x, x.used, &p, &y), MP_OKAY);
267+
ExpectIntEQ(mp_cmp(&y, &g), MP_EQ);
268+
269+
/* One digit more than the boundary is rejected. */
270+
ExpectIntEQ(mp_mul_2d(&p, DIGIT_BIT, &e), MP_OKAY);
271+
ExpectIntEQ(mp_add_d(&e, 1, &e), MP_OKAY);
272+
ExpectIntEQ(mp_exptmod(&g, &x, &e, &y), WC_NO_ERR_TRACE(MP_VAL));
273+
274+
mp_clear(&g);
275+
mp_clear(&x);
276+
mp_clear(&p);
277+
mp_clear(&y);
278+
mp_clear(&e);
279+
#endif
280+
return EXPECT_RESULT();
281+
}
282+
220283
/*
221284
* Testing wc_export_int
222285
*/

‎tests/api/test_wolfmath.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ int test_mp_get_rand_digit(void);
3030
int test_mp_cond_copy(void);
3131
int test_mp_rand(void);
3232
int test_mp_exptmod_even_2k(void);
33+
int test_mp_exptmod_fp_size_boundary(void);
3334
int test_wc_export_int(void);
3435
int test_wc_SpIntSizeDecisionCoverage(void);
3536
int test_wc_SpIntShiftDecisionCoverage(void);
@@ -45,6 +46,7 @@ int test_wc_SpIntExptGcdDecisionCoverage(void);
4546
TEST_DECL_GROUP("wolfmath", test_mp_cond_copy), \
4647
TEST_DECL_GROUP("wolfmath", test_mp_rand), \
4748
TEST_DECL_GROUP("wolfmath", test_mp_exptmod_even_2k), \
49+
TEST_DECL_GROUP("wolfmath", test_mp_exptmod_fp_size_boundary), \
4850
TEST_DECL_GROUP("wolfmath", test_wc_export_int), \
4951
TEST_DECL_GROUP("wolfmath", test_wc_SpIntSizeDecisionCoverage), \
5052
TEST_DECL_GROUP("wolfmath", test_wc_SpIntShiftDecisionCoverage), \

‎tests/unit-mcdc/test_ecc_fault_whitebox.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -794,38 +794,38 @@ static void wb_degenerate_points(void)
794794
wb_fail = 1;
795795
goto out;
796796
}
797-
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp);
797+
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp, 0);
798798

799799
/* Q = -P: x and z equal, y differs -> the general add gives Z == 0 */
800800
(void)mp_sub(&prime, P->y, Q->y);
801-
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp);
801+
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp, 0);
802802

803803
/* Q->z == 0: the digit-count operand FALSE with x still equal */
804804
(void)wc_ecc_copy_point(P, Q);
805805
(void)mp_set(Q->z, 0);
806-
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp);
806+
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp, 0);
807807

808808
/* Q->z differs (non-zero): the z comparison FALSE */
809809
(void)mp_set(Q->z, 2);
810-
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp);
810+
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp, 0);
811811

812812
/* Q->x differs: the first operand FALSE */
813813
(void)wc_ecc_copy_point(P, Q);
814814
(void)mp_set(Q->x, 3);
815-
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp);
815+
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp, 0);
816816

817817
/* y differs and Q is not -P either: the last operand FALSE */
818818
(void)wc_ecc_copy_point(P, Q);
819819
(void)mp_set(Q->y, 5);
820-
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp);
820+
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp, 0);
821821

822822
/* curve coefficient zero -> "modulus - a is zero" TRUE in the doubling
823823
* formula's coefficient selection; the real a gives the FALSE row. */
824-
(void)_ecc_projective_dbl_point(P, R, &zero, &prime, mp);
825-
(void)_ecc_projective_dbl_point(P, R, &a, &prime, mp);
824+
(void)_ecc_projective_dbl_point(P, R, &zero, &prime, mp, 0);
825+
(void)_ecc_projective_dbl_point(P, R, &a, &prime, mp, 0);
826826
for (n = 1; n <= 10; n++) {
827827
mcdc_fm_arm(n);
828-
(void)_ecc_projective_dbl_point(P, R, &zero, &prime, mp);
828+
(void)_ecc_projective_dbl_point(P, R, &zero, &prime, mp, 0);
829829
mcdc_fm_disarm();
830830
}
831831

0 commit comments

Comments
 (0)