Skip to content

Commit 5070676

Browse files
authored
Merge pull request #12061 from yosuke-wolfssl/fix/f-17453
integer: size the fast_mp_invmod pass limit from the modulus
2 parents c84c488 + 28307fa commit 5070676

4 files changed

Lines changed: 59 additions & 2 deletions

File tree

‎tests/api/test_wolfmath.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,53 @@ int test_wc_export_int(void)
478478
return EXPECT_RESULT();
479479
} /* End test_wc_export_int */
480480

481+
/*
482+
* Testing mp_invmod with an odd modulus of about 8192 bits
483+
*/
484+
int test_mp_invmod_large_odd_modulus(void)
485+
{
486+
EXPECT_DECLS;
487+
#if defined(USE_INTEGER_HEAP_MATH) && defined(WOLFSSL_PUBLIC_MP) && \
488+
!defined(WC_NO_RNG) && (!defined(HAVE_FIPS) || FIPS_VERSION3_GE(7,0,0))
489+
mp_int a;
490+
mp_int k;
491+
mp_int m;
492+
mp_int r;
493+
mp_int t;
494+
WC_RNG rng;
495+
int digits = (4096 + DIGIT_BIT - 1) / DIGIT_BIT;
496+
497+
XMEMSET(&rng, 0, sizeof(WC_RNG));
498+
499+
ExpectIntEQ(mp_init_multi(&a, &k, &m, &r, &t, NULL), MP_OKAY);
500+
ExpectIntEQ(wc_InitRng(&rng), 0);
501+
502+
/* m = 2*a*k + 1 is odd and coprime to a, so the inverse exists. */
503+
ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
504+
ExpectIntEQ(mp_set_bit(&a, 4095), MP_OKAY);
505+
ExpectIntEQ(mp_rand(&k, digits, &rng), 0);
506+
ExpectIntEQ(mp_set_bit(&k, 4095), MP_OKAY);
507+
ExpectIntEQ(mp_mul(&a, &k, &m), MP_OKAY);
508+
ExpectIntEQ(mp_mul_2d(&m, 1, &m), MP_OKAY);
509+
ExpectIntEQ(mp_add_d(&m, 1, &m), MP_OKAY);
510+
ExpectIntGE(mp_count_bits(&m), 8192);
511+
512+
ExpectIntEQ(mp_invmod(&a, &m, &r), MP_OKAY);
513+
ExpectIntEQ(mp_isneg(&r), MP_NO);
514+
ExpectIntEQ(mp_cmp(&r, &m), MP_LT);
515+
ExpectIntEQ(mp_mulmod(&a, &r, &m, &t), MP_OKAY);
516+
ExpectIntEQ(mp_cmp_d(&t, 1), MP_EQ);
517+
518+
mp_clear(&t);
519+
mp_clear(&r);
520+
mp_clear(&m);
521+
mp_clear(&k);
522+
mp_clear(&a);
523+
DoExpectIntEQ(wc_FreeRng(&rng), 0);
524+
#endif
525+
return EXPECT_RESULT();
526+
} /* End test_mp_invmod_large_odd_modulus */
527+
481528
/*
482529
* MC/DC coverage for the sp_int.c multi-precision engine (the sp-math
483530
* module, iso26262/mcdc-per-module). These call the sp_* primitives

‎tests/api/test_wolfmath.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ int test_mp_addsubmod_ct_heap(void);
3232
int test_mp_rand(void);
3333
int test_mp_exptmod_even_2k(void);
3434
int test_wc_export_int(void);
35+
int test_mp_invmod_large_odd_modulus(void);
3536
int test_wc_SpIntSizeDecisionCoverage(void);
3637
int test_wc_SpIntShiftDecisionCoverage(void);
3738
int test_wc_SpIntDigitArithDecisionCoverage(void);
@@ -49,6 +50,7 @@ int test_mp_exptmod_neg_base(void);
4950
TEST_DECL_GROUP("wolfmath", test_mp_rand), \
5051
TEST_DECL_GROUP("wolfmath", test_mp_exptmod_even_2k), \
5152
TEST_DECL_GROUP("wolfmath", test_wc_export_int), \
53+
TEST_DECL_GROUP("wolfmath", test_mp_invmod_large_odd_modulus), \
5254
TEST_DECL_GROUP("wolfmath", test_wc_SpIntSizeDecisionCoverage), \
5355
TEST_DECL_GROUP("wolfmath", test_wc_SpIntShiftDecisionCoverage), \
5456
TEST_DECL_GROUP("wolfmath", test_wc_SpIntDigitArithDecisionCoverage),\

‎wolfcrypt/src/integer.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,13 +1082,20 @@ int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
10821082
int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
10831083
{
10841084
mp_int x, y, u, v, B, D;
1085-
int res, loop_check = 0;
1085+
int res, loop_check = 0, max_loops;
10861086

10871087
/* 2. [modified] b must be odd */
10881088
if (mp_iseven (b) == MP_YES) {
10891089
return MP_VAL;
10901090
}
10911091

1092+
/* Each pass shortens u or v by at least one bit, and both start
1093+
* no longer than b, so 2 * bits(b) passes always suffice. */
1094+
max_loops = 2 * mp_count_bits (b) + 2;
1095+
if (max_loops < MAX_INVMOD_SZ) {
1096+
max_loops = MAX_INVMOD_SZ;
1097+
}
1098+
10921099
/* init all our temps */
10931100
if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D)) != MP_OKAY) {
10941101
return res;
@@ -1182,7 +1189,7 @@ int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
11821189

11831190
/* if not zero goto step 4 */
11841191
if (mp_iszero (&u) == MP_NO) {
1185-
if (++loop_check > MAX_INVMOD_SZ) {
1192+
if (++loop_check > max_loops) {
11861193
res = MP_VAL;
11871194
goto LBL_ERR;
11881195
}

‎wolfssl/wolfcrypt/integer.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
278278
#define PRIME_SIZE 256
279279
#endif
280280

281+
/* Minimum pass limit for fast_mp_invmod() */
281282
#ifndef MAX_INVMOD_SZ
282283
#if defined(WOLFSSL_MYSQL_COMPATIBLE)
283284
#define MAX_INVMOD_SZ 8192

0 commit comments

Comments
 (0)