Skip to content

Commit b18bc86

Browse files
committed
Heap Math exptmod: fixes for valid modulus checks
mp_exptmod_base_2() uses Montogmery method and does not support even modulus. Added check. mp_exptmod_fast() uses Montogmery method when odd not when dr > 0. Comment updated at call. Reduce value of zero doesn't work for mp_reduece_2k. Changed setup of use to check for zero and won't use it. Other methods won't work either.
1 parent d6a6cbb commit b18bc86

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

wolfcrypt/src/integer.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ int wolfcrypt_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
955955
}
956956

957957
#ifdef BN_MP_EXPTMOD_BASE_2
958-
if (G->used == 1 && G->dp[0] == 2) {
958+
if (G->used == 1 && G->dp[0] == 2 && mp_isodd(P) == MP_YES) {
959959
return mp_exptmod_base_2(X, P, Y);
960960
}
961961
#endif
@@ -985,7 +985,7 @@ int wolfcrypt_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
985985
}
986986
#endif
987987

988-
/* if the modulus is odd or dr != 0 use the montgomery method */
988+
/* if the modulus is odd use the montgomery method, or use other known */
989989
#ifdef BN_MP_EXPTMOD_FAST_C
990990
if (mp_isodd (P) == MP_YES || dr != 0) {
991991
return mp_exptmod_fast (G, X, P, Y, dr);
@@ -1985,7 +1985,6 @@ int mp_dr_is_modulus(mp_int *a)
19851985
return 1;
19861986
}
19871987

1988-
19891988
/* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
19901989
*
19911990
* Uses a left-to-right k-ary sliding window to compute the modular
@@ -2113,7 +2112,10 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
21132112
if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
21142113
goto LBL_M;
21152114
}
2116-
redux = mp_reduce_2k;
2115+
/* mp of zero is not usable */
2116+
if (mp != 0) {
2117+
redux = mp_reduce_2k;
2118+
}
21172119
#endif
21182120
}
21192121

0 commit comments

Comments
 (0)