X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=sshbn.c;h=ec7d3a6d5de57c64a0e341fd87ccb63e853904e9;hb=0cc6fb8bfea07360afeac83ba67aceffb56499c8;hp=76529cfafa06c2ea82d141873acd591ff604df62;hpb=5bcb8d6aac70ea3f8761e384de7b49cf4711b2df;p=PuTTY.git diff --git a/sshbn.c b/sshbn.c index 76529cfa..ec7d3a6d 100644 --- a/sshbn.c +++ b/sshbn.c @@ -869,6 +869,7 @@ Bignum modpow(Bignum base_in, Bignum exp, Bignum mod) len = mod[0]; r = bn_power_2(BIGNUM_INT_BITS * len); inv = modinv(mod, r); + assert(inv); /* cannot fail, since mod is odd and r is a power of 2 */ /* * Multiply the base by r mod n, to get it into Montgomery @@ -1634,8 +1635,22 @@ Bignum modinv(Bignum number, Bignum modulus) assert(modulus[modulus[0]] != 0); while (bignum_cmp(b, One) != 0) { - Bignum t = newbn(b[0]); - Bignum q = newbn(a[0]); + Bignum t, q; + + if (bignum_cmp(b, Zero) == 0) { + /* + * Found a common factor between the inputs, so we cannot + * return a modular inverse at all. + */ + freebn(b); + freebn(a); + freebn(xp); + freebn(x); + return NULL; + } + + t = newbn(b[0]); + q = newbn(a[0]); bigdivmod(a, b, t, q); while (t[0] > 1 && t[t[0]] == 0) t[0]--;