Make the frankly ridiculous prototypes for modpow() and modmul() more sane
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Mon, 23 Oct 2000 16:11:31 +0000 (16:11 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Mon, 23 Oct 2000 16:11:31 +0000 (16:11 +0000)
git-svn-id: svn://svn.tartarus.org/sgt/putty@752 cda61777-01e9-0310-a592-d414129be87e

ssh.h
sshbn.c
sshdh.c
sshdss.c
sshprime.c
sshrsa.c

diff --git a/ssh.h b/ssh.h
index 5fe33c3..331ff3b 100644 (file)
--- a/ssh.h
+++ b/ssh.h
@@ -153,8 +153,8 @@ Bignum newbn(int length);
 Bignum copybn(Bignum b);
 Bignum bignum_from_short(unsigned short n);
 void freebn(Bignum b);
-void modpow(Bignum base, Bignum exp, Bignum mod, Bignum result);
-void modmul(Bignum a, Bignum b, Bignum mod, Bignum result);
+Bignum modpow(Bignum base, Bignum exp, Bignum mod);
+Bignum modmul(Bignum a, Bignum b, Bignum mod);
 void decbn(Bignum n);
 extern Bignum Zero, One;
 int ssh1_read_bignum(unsigned char *data, Bignum *result);
diff --git a/sshbn.c b/sshbn.c
index 24af76f..8dfaa29 100644 (file)
--- a/sshbn.c
+++ b/sshbn.c
@@ -184,11 +184,12 @@ static void internal_mod(unsigned short *a, int alen,
  * The most significant word of mod MUST be non-zero.
  * We assume that the result array is the same size as the mod array.
  */
-void modpow(Bignum base, Bignum exp, Bignum mod, Bignum result)
+Bignum modpow(Bignum base, Bignum exp, Bignum mod)
 {
     unsigned short *a, *b, *n, *m;
     int mshift;
     int mlen, i, j;
+    Bignum result;
 
     /* Allocate m of size mlen, copy mod to m */
     /* We use big endian internally */
@@ -252,14 +253,18 @@ void modpow(Bignum base, Bignum exp, Bignum mod, Bignum result)
     }
 
     /* Copy result to buffer */
+    result = newbn(mod[0]);
     for (i = 0; i < mlen; i++)
        result[result[0] - i] = a[i+mlen];
+    while (result[0] > 1 && result[result[0]] == 0) result[0]--;
 
     /* Free temporary arrays */
     for (i = 0; i < 2*mlen; i++) a[i] = 0; free(a);
     for (i = 0; i < 2*mlen; i++) b[i] = 0; free(b);
     for (i = 0; i < mlen; i++) m[i] = 0; free(m);
     for (i = 0; i < mlen; i++) n[i] = 0; free(n);
+
+    return result;
 }
 
 /*
@@ -267,11 +272,12 @@ void modpow(Bignum base, Bignum exp, Bignum mod, Bignum result)
  * The most significant word of mod MUST be non-zero.
  * We assume that the result array is the same size as the mod array.
  */
-void modmul(Bignum p, Bignum q, Bignum mod, Bignum result)
+Bignum modmul(Bignum p, Bignum q, Bignum mod)
 {
     unsigned short *a, *n, *m, *o;
     int mshift;
     int pqlen, mlen, i, j;
+    Bignum result;
 
     /* Allocate m of size mlen, copy mod to m */
     /* We use big endian internally */
@@ -320,14 +326,18 @@ void modmul(Bignum p, Bignum q, Bignum mod, Bignum result)
     }
 
     /* Copy result to buffer */
+    result = newbn(mod[0]);
     for (i = 0; i < mlen; i++)
        result[result[0] - i] = a[i+2*pqlen-mlen];
+    while (result[0] > 1 && result[result[0]] == 0) result[0]--;
 
     /* Free temporary arrays */
     for (i = 0; i < 2*pqlen; i++) a[i] = 0; free(a);
     for (i = 0; i < mlen; i++) m[i] = 0; free(m);
     for (i = 0; i < pqlen; i++) n[i] = 0; free(n);
     for (i = 0; i < pqlen; i++) o[i] = 0; free(o);
+
+    return result;
 }
 
 /*
diff --git a/sshdh.c b/sshdh.c
index d556a25..84035c0 100644 (file)
--- a/sshdh.c
+++ b/sshdh.c
@@ -99,8 +99,7 @@ Bignum dh_create_e(void) {
     /*
      * Done. Now compute e = g^x mod p.
      */
-    e = newbn(P[0]);
-    modpow(G, x, P, e);
+    e = modpow(G, x, P);
 
     return e;
 }
@@ -110,6 +109,6 @@ Bignum dh_create_e(void) {
  */
 Bignum dh_find_K(Bignum f) {
     Bignum K = newbn(P[0]);
-    modpow(f, x, P, K);
+    K = modpow(f, x, P);
     return K;
 }
index a0a6e1f..15b97ba 100644 (file)
--- a/sshdss.c
+++ b/sshdss.c
@@ -190,7 +190,7 @@ static int dss_verifysig(char *sig, int siglen, char *data, int datalen) {
     char *p;
     int slen;
     char hash[20];
-    Bignum r, s, w, i1, i2, i3, u1, u2, sha, v;
+    Bignum r, s, w, gu1p, yu2p, gu1yu2p, u1, u2, sha, v;
     int ret;
 
     if (!dss_p)
@@ -243,34 +243,28 @@ static int dss_verifysig(char *sig, int siglen, char *data, int datalen) {
     /*
      * Step 2. u1 <- SHA(message) * w mod q.
      */
-    u1 = newbn(dss_q[0]);
     SHA_Simple(data, datalen, hash);
     p = hash; slen = 20; sha = get160(&p, &slen);
     diagbn("sha=", sha);
-    modmul(sha, w, dss_q, u1);
+    u1 = modmul(sha, w, dss_q);
     diagbn("u1=", u1);
 
     /*
      * Step 3. u2 <- r * w mod q.
      */
-    u2 = newbn(dss_q[0]);
-    modmul(r, w, dss_q, u2);
+    u2 = modmul(r, w, dss_q);
     diagbn("u2=", u2);
 
     /*
      * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
      */
-    i1 = newbn(dss_p[0]);
-    i2 = newbn(dss_p[0]);
-    i3 = newbn(dss_p[0]);
-    v = newbn(dss_q[0]);
-    modpow(dss_g, u1, dss_p, i1);
-    diagbn("gu1p=", i1);
-    modpow(dss_y, u2, dss_p, i2);
-    diagbn("yu2p=", i2);
-    modmul(i1, i2, dss_p, i3);
-    diagbn("gu1yu2p=", i3);
-    modmul(i3, One, dss_q, v);
+    gu1p = modpow(dss_g, u1, dss_p);
+    diagbn("gu1p=", gu1p);
+    yu2p = modpow(dss_y, u2, dss_p);
+    diagbn("yu2p=", yu2p);
+    gu1yu2p = modmul(gu1p, yu2p, dss_p);
+    diagbn("gu1yu2p=", gu1yu2p);
+    v = modmul(gu1yu2p, One, dss_q);
     diagbn("gu1yu2q=v=", v);
     diagbn("r=", r);
 
@@ -282,9 +276,9 @@ static int dss_verifysig(char *sig, int siglen, char *data, int datalen) {
 
     freebn(w);
     freebn(sha);
-    freebn(i1);
-    freebn(i2);
-    freebn(i3);
+    freebn(gu1p);
+    freebn(yu2p);
+    freebn(gu1yu2p);
     freebn(v);
     freebn(r);
     freebn(s);
index d1793e4..4a2a660 100644 (file)
@@ -670,8 +670,7 @@ Bignum primegen(int bits, int modulus, int residue,
         /*
          * Compute w^q mod p.
          */
-        wqp = newbn(p[0]);
-        modpow(w, q, p, wqp);
+        wqp = modpow(w, q, p);
         freebn(w);
 
         /*
@@ -683,8 +682,7 @@ Bignum primegen(int bits, int modulus, int residue,
             continue;
         }
         for (i = 0; i < k; i++) {
-            wqp2 = newbn(p[0]);
-            modmul(wqp, wqp, p, wqp2);
+            wqp2 = modmul(wqp, wqp, p);
             freebn(wqp);
             wqp = wqp2;
             if (bignum_cmp(wqp, One) == 0)
index d39f846..bc23c43 100644 (file)
--- a/sshrsa.c
+++ b/sshrsa.c
@@ -65,7 +65,6 @@ void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) {
     w = (key->bytes+1)/2;
 
     b1 = newbn(w);
-    b2 = newbn(w);
 
     p = data;
     for (i=1; i<=w; i++)
@@ -78,7 +77,7 @@ void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) {
            b1[1+i/2] |= byte;
     }
 
-    modpow(b1, key->exponent, key->modulus, b2);
+    b2 = modpow(b1, key->exponent, key->modulus);
 
     p = data;
     for (i=key->bytes; i-- ;) {
@@ -96,8 +95,7 @@ void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) {
 
 Bignum rsadecrypt(Bignum input, struct RSAKey *key) {
     Bignum ret;
-    ret = newbn(key->modulus[0]);
-    modpow(input, key->private_exponent, key->modulus, ret);
+    ret = modpow(input, key->private_exponent, key->modulus);
     return ret;
 }