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
63 changes: 63 additions & 0 deletions tests/api/test_wolfmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,69 @@ int test_mp_exptmod_even_2k(void)
return EXPECT_RESULT();
}

/*
* Testing TFM exponentiation with a modulus of FP_SIZE / 2 digits - the largest
* accepted. Constant time exponentiation keeps working values at the length of
* the modulus so products use all FP_SIZE digits.
*/
int test_mp_exptmod_fp_size_boundary(void)
{
EXPECT_DECLS;
#if defined(USE_FAST_MATH) && defined(WOLFSSL_PUBLIC_MP) && \
defined(TFM_TIMING_RESISTANT) && !defined(WC_NO_MODEXP_TIMING_RESISTANT)
mp_int g, x, p, y, e;
int k = (FP_SIZE / 2) * DIGIT_BIT;

XMEMSET(&g, 0, sizeof(g));
XMEMSET(&x, 0, sizeof(x));
XMEMSET(&p, 0, sizeof(p));
XMEMSET(&y, 0, sizeof(y));
XMEMSET(&e, 0, sizeof(e));
ExpectIntEQ(mp_init_multi(&g, &x, &p, &y, &e, NULL), MP_OKAY);

/* p = 2^k - 1: odd and all FP_SIZE / 2 digits set. */
ExpectIntEQ(mp_set(&p, 1), MP_OKAY);
ExpectIntEQ(mp_mul_2d(&p, k, &p), MP_OKAY);
ExpectIntEQ(mp_sub_d(&p, 1, &p), MP_OKAY);
ExpectIntEQ(p.used, FP_SIZE / 2);

/* Base 2: 2^(k.2^64 + 7) mod (2^k - 1) = 2^7 as 2^k = 1 mod p. */
ExpectIntEQ(mp_set(&g, 2), MP_OKAY);
ExpectIntEQ(mp_set_int(&x, (unsigned long)k), MP_OKAY);
ExpectIntEQ(mp_mul_2d(&x, 64, &x), MP_OKAY);
ExpectIntEQ(mp_add_d(&x, 7, &x), MP_OKAY);
ExpectIntEQ(mp_exptmod(&g, &x, &p, &y), MP_OKAY);
ExpectIntEQ(mp_cmp_d(&y, 128), MP_EQ);

/* General base: (p - 1)^x mod p = p - 1 when x odd and 1 when x even.
* Full width base and exponent. */
ExpectIntEQ(mp_sub_d(&p, 1, &g), MP_OKAY);
ExpectIntEQ(mp_sub_d(&p, 2, &x), MP_OKAY);
ExpectIntEQ(mp_exptmod(&g, &x, &p, &y), MP_OKAY);
ExpectIntEQ(mp_cmp(&y, &g), MP_EQ);
ExpectIntEQ(mp_sub_d(&p, 3, &x), MP_OKAY);
ExpectIntEQ(mp_exptmod(&g, &x, &p, &y), MP_OKAY);
ExpectIntEQ(mp_cmp_d(&y, 1), MP_EQ);

/* Exponent processed as a fixed number of digits. */
ExpectIntEQ(mp_sub_d(&p, 2, &x), MP_OKAY);
ExpectIntEQ(mp_exptmod_ex(&g, &x, x.used, &p, &y), MP_OKAY);
ExpectIntEQ(mp_cmp(&y, &g), MP_EQ);

/* One digit more than the boundary is rejected. */
ExpectIntEQ(mp_mul_2d(&p, DIGIT_BIT, &e), MP_OKAY);
ExpectIntEQ(mp_add_d(&e, 1, &e), MP_OKAY);
ExpectIntEQ(mp_exptmod(&g, &x, &e, &y), WC_NO_ERR_TRACE(MP_VAL));

mp_clear(&g);
mp_clear(&x);
mp_clear(&p);
mp_clear(&y);
mp_clear(&e);
#endif
return EXPECT_RESULT();
}

/*
* Testing wc_export_int
*/
Expand Down
2 changes: 2 additions & 0 deletions tests/api/test_wolfmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ int test_mp_get_rand_digit(void);
int test_mp_cond_copy(void);
int test_mp_rand(void);
int test_mp_exptmod_even_2k(void);
int test_mp_exptmod_fp_size_boundary(void);
int test_wc_export_int(void);
int test_wc_SpIntSizeDecisionCoverage(void);
int test_wc_SpIntShiftDecisionCoverage(void);
Expand All @@ -45,6 +46,7 @@ int test_wc_SpIntExptGcdDecisionCoverage(void);
TEST_DECL_GROUP("wolfmath", test_mp_cond_copy), \
TEST_DECL_GROUP("wolfmath", test_mp_rand), \
TEST_DECL_GROUP("wolfmath", test_mp_exptmod_even_2k), \
TEST_DECL_GROUP("wolfmath", test_mp_exptmod_fp_size_boundary), \
TEST_DECL_GROUP("wolfmath", test_wc_export_int), \
TEST_DECL_GROUP("wolfmath", test_wc_SpIntSizeDecisionCoverage), \
TEST_DECL_GROUP("wolfmath", test_wc_SpIntShiftDecisionCoverage), \
Expand Down
18 changes: 9 additions & 9 deletions tests/unit-mcdc/test_ecc_fault_whitebox.c
Original file line number Diff line number Diff line change
Expand Up @@ -794,38 +794,38 @@ static void wb_degenerate_points(void)
wb_fail = 1;
goto out;
}
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp);
(void)_ecc_projective_add_point(P, Q, R, &a, &prime, mp, 0);

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

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

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

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

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

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

Expand Down
Loading
Loading