Skip to content

Commit 8dbfaff

Browse files
committed
Handle corner-cases of invmod()
This fixes #67
1 parent 6fa127b commit 8dbfaff

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

bn_fast_mp_invmod.c

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
4646
goto LBL_ERR;
4747
}
4848

49+
/* if one of x,y is zero return an error! */
50+
if ((mp_iszero(&x) == MP_YES) || (mp_iszero(&y) == MP_YES)) {
51+
res = MP_VAL;
52+
goto LBL_ERR;
53+
}
54+
4955
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
5056
if ((res = mp_copy(&x, &u)) != MP_OKAY) {
5157
goto LBL_ERR;

bn_mp_invmod.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
/* hac 14.61, pp608 */
1919
int mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
2020
{
21-
/* b cannot be negative */
22-
if ((b->sign == MP_NEG) || (mp_iszero(b) == MP_YES)) {
21+
/* b cannot be negative and has to be >1 */
22+
if ((b->sign == MP_NEG) || (mp_cmp_d(b, 1) != MP_GT)) {
2323
return MP_VAL;
2424
}
2525

2626
#ifdef BN_FAST_MP_INVMOD_C
2727
/* if the modulus is odd we can use a faster routine instead */
28-
if ((mp_isodd(b) == MP_YES) && (mp_cmp_d(b, 1) != MP_EQ)) {
28+
if ((mp_isodd(b) == MP_YES)) {
2929
return fast_mp_invmod(a, b, c);
3030
}
3131
#endif

0 commit comments

Comments
 (0)