X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/3014da2b1f6b65da7a730050ee1a451acb9a47ba..f33ba69e92f389f5884eae5a586858a629a5260b:/sshbn.c diff --git a/sshbn.c b/sshbn.c index 7fe0309a..d32eb1bb 100644 --- a/sshbn.c +++ b/sshbn.c @@ -3,6 +3,7 @@ */ #include +#include #include #include @@ -226,16 +227,25 @@ static void internal_mod(BignumInt *a, int alen, /* * Compute (base ^ exp) % mod. - * The base MUST be smaller than the modulus. - * The most significant word of mod MUST be non-zero. - * We assume that the result array is the same size as the mod array. */ -Bignum modpow(Bignum base, Bignum exp, Bignum mod) +Bignum modpow(Bignum base_in, Bignum exp, Bignum mod) { BignumInt *a, *b, *n, *m; int mshift; int mlen, i, j; - Bignum result; + Bignum base, result; + + /* + * The most significant word of mod needs to be non-zero. It + * should already be, but let's make sure. + */ + assert(mod[mod[0]] != 0); + + /* + * Make sure the base is smaller than the modulus, by reducing + * it modulo the modulus if not. + */ + base = bigmod(base_in, mod); /* Allocate m of size mlen, copy mod to m */ /* We use big endian internally */ @@ -331,6 +341,8 @@ Bignum modpow(Bignum base, Bignum exp, Bignum mod) n[i] = 0; sfree(n); + freebn(base); + return result; } @@ -528,19 +540,25 @@ Bignum bignum_from_bytes(const unsigned char *data, int nbytes) /* * Read an ssh1-format bignum from a data buffer. Return the number - * of bytes consumed. + * of bytes consumed, or -1 if there wasn't enough data. */ -int ssh1_read_bignum(const unsigned char *data, Bignum * result) +int ssh1_read_bignum(const unsigned char *data, int len, Bignum * result) { const unsigned char *p = data; int i; int w, b; + if (len < 2) + return -1; + w = 0; for (i = 0; i < 2; i++) w = (w << 8) + *p++; b = (w + 7) / 8; /* bits -> bytes */ + if (len < b+2) + return -1; + if (!result) /* just return length */ return b + 2;