]> asedeno.scripts.mit.edu Git - PuTTY.git/commitdiff
Add some missing invariants in bigdiv and bigmod.
authorSimon Tatham <anakin@pobox.com>
Sat, 1 Nov 2014 19:20:51 +0000 (19:20 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 1 Nov 2014 19:48:47 +0000 (19:48 +0000)
The underlying function 'bigdivmod' does not ensure either of its
outputs is normalised, so its callers must do so.

sshbn.c

diff --git a/sshbn.c b/sshbn.c
index a5e0552ff57523002adbafa6502cb0beda792a97..b1ea5d63c488cde28e8744285a2368601863f56b 100644 (file)
--- 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;