]> asedeno.scripts.mit.edu Git - PuTTY.git/blobdiff - sshbn.c
Fix mpint signedness bug in importing PEM ECDSA keys.
[PuTTY.git] / sshbn.c
diff --git a/sshbn.c b/sshbn.c
index 8da7d8a00e46c7d3ad6ffc72cdbcc5e3df887eab..f2a28e00f5f9e15623d864edb4c7b8f652ca1e47 100644 (file)
--- a/sshbn.c
+++ b/sshbn.c
@@ -7,6 +7,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <limits.h>
+#include <ctype.h>
 
 #include "misc.h"
 
@@ -102,6 +103,7 @@ typedef BignumInt *Bignum;
 
 BignumInt bnZero[1] = { 0 };
 BignumInt bnOne[2] = { 1, 1 };
+BignumInt bnTen[2] = { 1, 10 };
 
 /*
  * The Bignum format is an array of `BignumInt'. The first
@@ -117,7 +119,7 @@ BignumInt bnOne[2] = { 1, 1 };
  * nonzero.
  */
 
-Bignum Zero = bnZero, One = bnOne;
+Bignum Zero = bnZero, One = bnOne, Ten = bnTen;
 
 static Bignum newbn(int length)
 {
@@ -126,8 +128,6 @@ static Bignum newbn(int length)
     assert(length >= 0 && length < INT_MAX / BIGNUM_INT_BITS);
 
     b = snewn(length + 1, BignumInt);
-    if (!b)
-       abort();                       /* FIXME */
     memset(b, 0, (length + 1) * sizeof(*b));
     b[0] = length;
     return b;
@@ -1110,12 +1110,9 @@ Bignum modsub(const Bignum a, const Bignum b, const Bignum n)
         /* Handle going round the corner of the modulus without having
          * negative support in Bignum */
         Bignum tmp = bigsub(n, b1);
-        if (tmp) {
-            ret = bigadd(tmp, a1);
-            freebn(tmp);
-        } else {
-            ret = NULL;
-        }
+        assert(tmp);
+        ret = bigadd(tmp, a1);
+        freebn(tmp);
     }
 
     if (a != a1) freebn(a1);
@@ -1233,6 +1230,52 @@ Bignum bignum_from_bytes(const unsigned char *data, int nbytes)
     return result;
 }
 
+Bignum bignum_from_bytes_le(const unsigned char *data, int nbytes)
+{
+    Bignum result;
+    int w, i;
+
+    assert(nbytes >= 0 && nbytes < INT_MAX/8);
+
+    w = (nbytes + BIGNUM_INT_BYTES - 1) / BIGNUM_INT_BYTES; /* bytes->words */
+
+    result = newbn(w);
+    for (i = 1; i <= w; i++)
+        result[i] = 0;
+    for (i = 0; i < nbytes; ++i) {
+        unsigned char byte = *data++;
+        result[1 + i / BIGNUM_INT_BYTES] |= byte << (8*i % BIGNUM_INT_BITS);
+    }
+
+    while (result[0] > 1 && result[result[0]] == 0)
+        result[0]--;
+    return result;
+}
+
+Bignum bignum_from_decimal(const char *decimal)
+{
+    Bignum result = copybn(Zero);
+
+    while (*decimal) {
+        Bignum tmp, tmp2;
+
+        if (!isdigit((unsigned char)*decimal)) {
+            freebn(result);
+            return 0;
+        }
+
+        tmp = bigmul(result, Ten);
+        tmp2 = bignum_from_long(*decimal - '0');
+        result = bigadd(tmp, tmp2);
+        freebn(tmp);
+        freebn(tmp2);
+
+        decimal++;
+    }
+
+    return result;
+}
+
 Bignum bignum_random_in_range(const Bignum lower, const Bignum upper)
 {
     Bignum ret = NULL;
@@ -1452,7 +1495,6 @@ Bignum bignum_lshift(Bignum a, int shift)
 
     bits = bignum_bitcount(a) + shift;
     ret = newbn((bits + BIGNUM_INT_BITS - 1) / BIGNUM_INT_BITS);
-    if (!ret) return NULL;
 
     shiftWords = shift / BIGNUM_INT_BITS;
     shiftBits = shift % BIGNUM_INT_BITS;
@@ -1922,7 +1964,7 @@ char *bignum_decimal(Bignum x)
  * testdata/bignum.py .
  */
 
-void modalfatalbox(char *p, ...)
+void modalfatalbox(const char *p, ...)
 {
     va_list ap;
     fprintf(stderr, "FATAL ERROR: ");