Make the frankly ridiculous prototypes for modpow() and modmul() more sane
[u/mdw/putty] / sshrsa.c
index 5ea4cc7..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;
 }
 
@@ -106,25 +104,29 @@ int rsastr_len(struct RSAKey *key) {
 
     md = key->modulus;
     ex = key->exponent;
-    return 4 * (ex[0]+md[0]) + 10;
+    return 4 * (ex[0]+md[0]) + 20;
 }
 
 void rsastr_fmt(char *str, struct RSAKey *key) {
     Bignum md, ex;
-    int len = 0, i;
+    int len = 0, i, nibbles;
+    static const char hex[] = "0123456789abcdef";
 
     md = key->modulus;
     ex = key->exponent;
 
-    for (i=1; i<=ex[0]; i++) {
-       sprintf(str+len, "%04x", ex[i]);
-       len += strlen(str+len);
-    }
-    str[len++] = '/';
-    for (i=1; i<=md[0]; i++) {
-       sprintf(str+len, "%04x", md[i]);
-       len += strlen(str+len);
-    }
+    len += sprintf(str+len, "0x");
+
+    nibbles = (3 + ssh1_bignum_bitcount(ex))/4; if (nibbles<1) nibbles=1;
+    for (i=nibbles; i-- ;)
+        str[len++] = hex[(bignum_byte(ex, i/2) >> (4*(i%2))) & 0xF];
+
+    len += sprintf(str+len, ",0x");
+
+    nibbles = (3 + ssh1_bignum_bitcount(md))/4; if (nibbles<1) nibbles=1;
+    for (i=nibbles; i-- ;)
+        str[len++] = hex[(bignum_byte(md, i/2) >> (4*(i%2))) & 0xF];
+
     str[len] = '\0';
 }