Remove the last lingering knowledge, outside sshbn.c, of the
[u/mdw/putty] / sshrsa.c
index 5ea4cc7..537d0f1 100644 (file)
--- a/sshrsa.c
+++ b/sshrsa.c
@@ -48,7 +48,7 @@ int makeprivate(unsigned char *data, struct RSAKey *result) {
 
 void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) {
     Bignum b1, b2;
-    int w, i;
+    int i;
     unsigned char *p;
 
     memmove(data+key->bytes-length, data, length);
@@ -62,32 +62,13 @@ void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) {
     }
     data[key->bytes-length-1] = 0;
 
-    w = (key->bytes+1)/2;
-
-    b1 = newbn(w);
-    b2 = newbn(w);
-
-    p = data;
-    for (i=1; i<=w; i++)
-       b1[i] = 0;
-    for (i=key->bytes; i-- ;) {
-       unsigned char byte = *p++;
-       if (i & 1)
-           b1[1+i/2] |= byte<<8;
-       else
-           b1[1+i/2] |= byte;
-    }
+    b1 = bignum_from_bytes(data, key->bytes);
 
-    modpow(b1, key->exponent, key->modulus, b2);
+    b2 = modpow(b1, key->exponent, key->modulus);
 
     p = data;
     for (i=key->bytes; i-- ;) {
-       unsigned char b;
-       if (i & 1)
-           b = b2[1+i/2] >> 8;
-       else
-           b = b2[1+i/2] & 0xFF;
-       *p++ = b;
+        *p++ = bignum_byte(b2, i);
     }
 
     freebn(b1);
@@ -96,35 +77,41 @@ 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;
 }
 
 int rsastr_len(struct RSAKey *key) {
     Bignum md, ex;
+    int mdlen, exlen;
 
     md = key->modulus;
     ex = key->exponent;
-    return 4 * (ex[0]+md[0]) + 10;
+    mdlen = (ssh1_bignum_bitcount(md)+15) / 16;
+    exlen = (ssh1_bignum_bitcount(ex)+15) / 16;
+    return 4 * (mdlen+exlen) + 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';
 }
 
@@ -167,5 +154,5 @@ void freersakey(struct RSAKey *key) {
     if (key->modulus) freebn(key->modulus);
     if (key->exponent) freebn(key->exponent);
     if (key->private_exponent) freebn(key->private_exponent);
-    if (key->comment) free(key->comment);
+    if (key->comment) sfree(key->comment);
 }