mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-03-12 18:13:50 -05:00
More consistently defend against division by zero with assertions. We
now check that all the modular functions (modpow, modinv, modmul, bigdivmod) have nonzero moduli, and that modinv also has a nonzero thing to try to invert. [originally from svn r9987]
This commit is contained in:
parent
76dc7c49a2
commit
5bcb8d6aac
16
sshbn.c
16
sshbn.c
@ -624,6 +624,7 @@ static void internal_mod(BignumInt *a, int alen,
|
||||
int i, k;
|
||||
|
||||
m0 = m[0];
|
||||
assert(m0 >> (BIGNUM_INT_BITS-1) == 1);
|
||||
if (mlen > 1)
|
||||
m1 = m[1];
|
||||
else
|
||||
@ -988,6 +989,12 @@ Bignum modmul(Bignum p, Bignum q, Bignum mod)
|
||||
int pqlen, mlen, rlen, i, j;
|
||||
Bignum result;
|
||||
|
||||
/*
|
||||
* The most significant word of mod needs to be non-zero. It
|
||||
* should already be, but let's make sure.
|
||||
*/
|
||||
assert(mod[mod[0]] != 0);
|
||||
|
||||
/* Allocate m of size mlen, copy mod to m */
|
||||
/* We use big endian internally */
|
||||
mlen = mod[0];
|
||||
@ -1087,6 +1094,12 @@ static void bigdivmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
|
||||
int mshift;
|
||||
int plen, mlen, i, j;
|
||||
|
||||
/*
|
||||
* The most significant word of mod needs to be non-zero. It
|
||||
* should already be, but let's make sure.
|
||||
*/
|
||||
assert(mod[mod[0]] != 0);
|
||||
|
||||
/* Allocate m of size mlen, copy mod to m */
|
||||
/* We use big endian internally */
|
||||
mlen = mod[0];
|
||||
@ -1617,6 +1630,9 @@ Bignum modinv(Bignum number, Bignum modulus)
|
||||
Bignum x = copybn(One);
|
||||
int sign = +1;
|
||||
|
||||
assert(number[number[0]] != 0);
|
||||
assert(modulus[modulus[0]] != 0);
|
||||
|
||||
while (bignum_cmp(b, One) != 0) {
|
||||
Bignum t = newbn(b[0]);
|
||||
Bignum q = newbn(a[0]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user