From eac7e041f100291169304a99cc9c7300093f2c78 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 1 Nov 2014 19:20:51 +0000 Subject: [PATCH] Add some missing invariants in bigdiv and bigmod. The underlying function 'bigdivmod' does not ensure either of its outputs is normalised, so its callers must do so. --- sshbn.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sshbn.c b/sshbn.c index a5e0552f..b1ea5d63 100644 --- a/sshbn.c +++ b/sshbn.c @@ -1610,6 +1610,8 @@ Bignum bigdiv(Bignum a, Bignum b) { Bignum q = newbn(a[0]); bigdivmod(a, b, NULL, q); + while (q[0] > 1 && q[q[0]] == 0) + q[0]--; return q; } @@ -1620,6 +1622,8 @@ Bignum bigmod(Bignum a, Bignum b) { Bignum r = newbn(b[0]); bigdivmod(a, b, r, NULL); + while (r[0] > 1 && r[r[0]] == 0) + r[0]--; return r; } @@ -1679,6 +1683,8 @@ Bignum modinv(Bignum number, Bignum modulus) bigdivmod(a, b, t, q); while (t[0] > 1 && t[t[0]] == 0) t[0]--; + while (q[0] > 1 && q[q[0]] == 0) + q[0]--; freebn(a); a = b; b = t; -- 2.45.2