Skip to content

Commit 6198282

Browse files
Merge pull request wolfSSL#12081 from ageprocpp/fix/fenrir-17457
Fix 2k reduction selection for even moduli
2 parents 1ea4739 + 525183d commit 6198282

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎tests/api/test_wolfmath.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,53 @@ int test_mp_rand(void)
170170
return EXPECT_RESULT();
171171
} /* End test_mp_rand */
172172

173+
/* An even modulus with a zero low digit must not use single-digit 2k
174+
* reduction: its setup value is 2^DIGIT_BIT and does not fit in a digit. */
175+
int test_mp_exptmod_even_2k(void)
176+
{
177+
EXPECT_DECLS;
178+
#if defined(USE_INTEGER_HEAP_MATH) && defined(WOLFSSL_PUBLIC_MP) && \
179+
defined(BN_MP_REDUCE_IS_2K_C) && defined(BN_S_MP_EXPTMOD_C)
180+
mp_int g, x, p, y;
181+
182+
XMEMSET(&g, 0, sizeof(g));
183+
XMEMSET(&x, 0, sizeof(x));
184+
XMEMSET(&p, 0, sizeof(p));
185+
XMEMSET(&y, 0, sizeof(y));
186+
ExpectIntEQ(mp_init_multi(&g, &x, &p, &y, NULL, NULL), MP_OKAY);
187+
188+
/* p = 2^DIGIT_BIT, then p = 3 * 2^DIGIT_BIT. */
189+
ExpectIntEQ(mp_set(&p, 1), MP_OKAY);
190+
ExpectIntEQ(mp_mul_2d(&p, DIGIT_BIT, &p), MP_OKAY);
191+
ExpectIntEQ(mp_reduce_is_2k(&p), MP_NO);
192+
/* (p - 1)^2 mod p = 1. */
193+
ExpectIntEQ(mp_sub_d(&p, 1, &g), MP_OKAY);
194+
ExpectIntEQ(mp_set(&x, 2), MP_OKAY);
195+
ExpectIntEQ(mp_exptmod(&g, &x, &p, &y), MP_OKAY);
196+
ExpectIntEQ(mp_cmp_d(&y, 1), MP_EQ);
197+
198+
ExpectIntEQ(mp_set(&p, 3), MP_OKAY);
199+
ExpectIntEQ(mp_mul_2d(&p, DIGIT_BIT, &p), MP_OKAY);
200+
ExpectIntEQ(mp_reduce_is_2k(&p), MP_NO);
201+
ExpectIntEQ(mp_sub_d(&p, 1, &g), MP_OKAY);
202+
ExpectIntEQ(mp_exptmod(&g, &x, &p, &y), MP_OKAY);
203+
ExpectIntEQ(mp_cmp_d(&y, 1), MP_EQ);
204+
205+
/* The adjacent modulus has a representable single-digit setup value. */
206+
ExpectIntEQ(mp_add_d(&p, 1, &p), MP_OKAY);
207+
ExpectIntEQ(mp_reduce_is_2k(&p), MP_YES);
208+
ExpectIntEQ(mp_sub_d(&p, 1, &g), MP_OKAY);
209+
ExpectIntEQ(mp_exptmod(&g, &x, &p, &y), MP_OKAY);
210+
ExpectIntEQ(mp_cmp_d(&y, 1), MP_EQ);
211+
212+
mp_clear(&g);
213+
mp_clear(&x);
214+
mp_clear(&p);
215+
mp_clear(&y);
216+
#endif
217+
return EXPECT_RESULT();
218+
}
219+
173220
/*
174221
* Testing wc_export_int
175222
*/

‎tests/api/test_wolfmath.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ int test_mp_get_digit(void);
2929
int test_mp_get_rand_digit(void);
3030
int test_mp_cond_copy(void);
3131
int test_mp_rand(void);
32+
int test_mp_exptmod_even_2k(void);
3233
int test_wc_export_int(void);
3334
int test_wc_SpIntSizeDecisionCoverage(void);
3435
int test_wc_SpIntShiftDecisionCoverage(void);
@@ -43,6 +44,7 @@ int test_wc_SpIntExptGcdDecisionCoverage(void);
4344
TEST_DECL_GROUP("wolfmath", test_mp_get_rand_digit), \
4445
TEST_DECL_GROUP("wolfmath", test_mp_cond_copy), \
4546
TEST_DECL_GROUP("wolfmath", test_mp_rand), \
47+
TEST_DECL_GROUP("wolfmath", test_mp_exptmod_even_2k), \
4648
TEST_DECL_GROUP("wolfmath", test_wc_export_int), \
4749
TEST_DECL_GROUP("wolfmath", test_wc_SpIntSizeDecisionCoverage), \
4850
TEST_DECL_GROUP("wolfmath", test_wc_SpIntShiftDecisionCoverage), \

‎wolfcrypt/src/integer.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,11 @@ int mp_reduce_is_2k(mp_int *a)
19711971
} else if (a->used == 1) {
19721972
return MP_YES;
19731973
} else if (a->used > 1) {
1974+
/* When a->dp[0] == 0, a cannot be reduced */
1975+
if (a->dp[0] == 0) {
1976+
return MP_NO;
1977+
}
1978+
19741979
iy = mp_count_bits(a);
19751980
iz = 1;
19761981
iw = 1;

0 commit comments

Comments
 (0)