Skip to content

Commit 70163d7

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 206e9a5 commit 70163d7

8 files changed

Lines changed: 1437 additions & 264 deletions

File tree

‎tests/api/test_wolfmath.c‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,69 @@ int test_mp_grow_zeroize(void)
626626
return EXPECT_RESULT();
627627
} /* End test_mp_grow_zeroize */
628628

629+
/*
630+
* Testing TFM exponentiation with a modulus of FP_SIZE / 2 digits - the largest
631+
* accepted. Constant time exponentiation keeps working values at the length of
632+
* the modulus so products use all FP_SIZE digits.
633+
*/
634+
int test_mp_exptmod_fp_size_boundary(void)
635+
{
636+
EXPECT_DECLS;
637+
#if defined(USE_FAST_MATH) && defined(WOLFSSL_PUBLIC_MP) && \
638+
defined(TFM_TIMING_RESISTANT) && !defined(WC_NO_MODEXP_TIMING_RESISTANT)
639+
mp_int g, x, p, y, e;
640+
int k = (FP_SIZE / 2) * DIGIT_BIT;
641+
642+
XMEMSET(&g, 0, sizeof(g));
643+
XMEMSET(&x, 0, sizeof(x));
644+
XMEMSET(&p, 0, sizeof(p));
645+
XMEMSET(&y, 0, sizeof(y));
646+
XMEMSET(&e, 0, sizeof(e));
647+
ExpectIntEQ(mp_init_multi(&g, &x, &p, &y, &e, NULL), MP_OKAY);
648+
649+
/* p = 2^k - 1: odd and all FP_SIZE / 2 digits set. */
650+
ExpectIntEQ(mp_set(&p, 1), MP_OKAY);
651+
ExpectIntEQ(mp_mul_2d(&p, k, &p), MP_OKAY);
652+
ExpectIntEQ(mp_sub_d(&p, 1, &p), MP_OKAY);
653+
ExpectIntEQ(p.used, FP_SIZE / 2);
654+
655+
/* Base 2: 2^(k.2^64 + 7) mod (2^k - 1) = 2^7 as 2^k = 1 mod p. */
656+
ExpectIntEQ(mp_set(&g, 2), MP_OKAY);
657+
ExpectIntEQ(mp_set_int(&x, (unsigned long)k), MP_OKAY);
658+
ExpectIntEQ(mp_mul_2d(&x, 64, &x), MP_OKAY);
659+
ExpectIntEQ(mp_add_d(&x, 7, &x), MP_OKAY);
660+
ExpectIntEQ(mp_exptmod(&g, &x, &p, &y), MP_OKAY);
661+
ExpectIntEQ(mp_cmp_d(&y, 128), MP_EQ);
662+
663+
/* General base: (p - 1)^x mod p = p - 1 when x odd and 1 when x even.
664+
* Full width base and exponent. */
665+
ExpectIntEQ(mp_sub_d(&p, 1, &g), MP_OKAY);
666+
ExpectIntEQ(mp_sub_d(&p, 2, &x), MP_OKAY);
667+
ExpectIntEQ(mp_exptmod(&g, &x, &p, &y), MP_OKAY);
668+
ExpectIntEQ(mp_cmp(&y, &g), MP_EQ);
669+
ExpectIntEQ(mp_sub_d(&p, 3, &x), MP_OKAY);
670+
ExpectIntEQ(mp_exptmod(&g, &x, &p, &y), MP_OKAY);
671+
ExpectIntEQ(mp_cmp_d(&y, 1), MP_EQ);
672+
673+
/* Exponent processed as a fixed number of digits. */
674+
ExpectIntEQ(mp_sub_d(&p, 2, &x), MP_OKAY);
675+
ExpectIntEQ(mp_exptmod_ex(&g, &x, x.used, &p, &y), MP_OKAY);
676+
ExpectIntEQ(mp_cmp(&y, &g), MP_EQ);
677+
678+
/* One digit more than the boundary is rejected. */
679+
ExpectIntEQ(mp_mul_2d(&p, DIGIT_BIT, &e), MP_OKAY);
680+
ExpectIntEQ(mp_add_d(&e, 1, &e), MP_OKAY);
681+
ExpectIntEQ(mp_exptmod(&g, &x, &e, &y), WC_NO_ERR_TRACE(MP_VAL));
682+
683+
mp_clear(&g);
684+
mp_clear(&x);
685+
mp_clear(&p);
686+
mp_clear(&y);
687+
mp_clear(&e);
688+
#endif
689+
return EXPECT_RESULT();
690+
}
691+
629692
/*
630693
* Testing wc_export_int
631694
*/

‎tests/api/test_wolfmath.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ int test_mp_rand(void);
3333
int test_mp_exptmod_even_2k(void);
3434
int test_mp_clear_zeroize(void);
3535
int test_mp_grow_zeroize(void);
36+
int test_mp_exptmod_fp_size_boundary(void);
3637
int test_wc_export_int(void);
3738
int test_mp_invmod_large_odd_modulus(void);
3839
int test_wc_SpIntSizeDecisionCoverage(void);
@@ -53,6 +54,7 @@ int test_mp_exptmod_neg_base(void);
5354
TEST_DECL_GROUP("wolfmath", test_mp_exptmod_even_2k), \
5455
TEST_DECL_GROUP("wolfmath", test_mp_clear_zeroize), \
5556
TEST_DECL_GROUP("wolfmath", test_mp_grow_zeroize), \
57+
TEST_DECL_GROUP("wolfmath", test_mp_exptmod_fp_size_boundary), \
5658
TEST_DECL_GROUP("wolfmath", test_wc_export_int), \
5759
TEST_DECL_GROUP("wolfmath", test_mp_invmod_large_odd_modulus), \
5860
TEST_DECL_GROUP("wolfmath", test_wc_SpIntSizeDecisionCoverage), \

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