mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-10 01:48:00 +00:00
Improve integer-type hygiene in bignum code.
In many places I was using an 'unsigned int', or an implicit int by virtue of writing an undecorated integer literal, where what was really wanted was a BignumInt. In particular, this substitution breaks in any situation where BignumInt is _larger_ than unsigned - which it is shortly about to be.
This commit is contained in:
parent
0aa92c8fa2
commit
e28b35b0a3
25
sshbn.c
25
sshbn.c
@ -515,7 +515,7 @@ static void monty_reduce(BignumInt *x, const BignumInt *n,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void internal_add_shifted(BignumInt *number,
|
static void internal_add_shifted(BignumInt *number,
|
||||||
unsigned n, int shift)
|
BignumInt n, int shift)
|
||||||
{
|
{
|
||||||
int word = 1 + (shift / BIGNUM_INT_BITS);
|
int word = 1 + (shift / BIGNUM_INT_BITS);
|
||||||
int bshift = shift % BIGNUM_INT_BITS;
|
int bshift = shift % BIGNUM_INT_BITS;
|
||||||
@ -546,8 +546,7 @@ static void internal_mod(BignumInt *a, int alen,
|
|||||||
BignumInt *m, int mlen,
|
BignumInt *m, int mlen,
|
||||||
BignumInt *quot, int qshift)
|
BignumInt *quot, int qshift)
|
||||||
{
|
{
|
||||||
BignumInt m0, m1;
|
BignumInt m0, m1, h;
|
||||||
unsigned int h;
|
|
||||||
int i, k;
|
int i, k;
|
||||||
|
|
||||||
m0 = m[0];
|
m0 = m[0];
|
||||||
@ -559,7 +558,7 @@ static void internal_mod(BignumInt *a, int alen,
|
|||||||
|
|
||||||
for (i = 0; i <= alen - mlen; i++) {
|
for (i = 0; i <= alen - mlen; i++) {
|
||||||
BignumDblInt t;
|
BignumDblInt t;
|
||||||
unsigned int q, r, c, ai1;
|
BignumInt q, r, c, ai1;
|
||||||
|
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
h = 0;
|
h = 0;
|
||||||
@ -614,7 +613,7 @@ static void internal_mod(BignumInt *a, int alen,
|
|||||||
for (k = mlen - 1; k >= 0; k--) {
|
for (k = mlen - 1; k >= 0; k--) {
|
||||||
t = MUL_WORD(q, m[k]);
|
t = MUL_WORD(q, m[k]);
|
||||||
t += c;
|
t += c;
|
||||||
c = (unsigned)(t >> BIGNUM_INT_BITS);
|
c = (BignumInt)(t >> BIGNUM_INT_BITS);
|
||||||
if ((BignumInt) t > a[i + k])
|
if ((BignumInt) t > a[i + k])
|
||||||
c++;
|
c++;
|
||||||
a[i + k] -= (BignumInt) t;
|
a[i + k] -= (BignumInt) t;
|
||||||
@ -697,7 +696,7 @@ Bignum modpow_simple(Bignum base_in, Bignum exp, Bignum mod)
|
|||||||
/* Skip leading zero bits of exp. */
|
/* Skip leading zero bits of exp. */
|
||||||
i = 0;
|
i = 0;
|
||||||
j = BIGNUM_INT_BITS-1;
|
j = BIGNUM_INT_BITS-1;
|
||||||
while (i < (int)exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
|
while (i < (int)exp[0] && (exp[exp[0] - i] & ((BignumInt)1 << j)) == 0) {
|
||||||
j--;
|
j--;
|
||||||
if (j < 0) {
|
if (j < 0) {
|
||||||
i++;
|
i++;
|
||||||
@ -710,7 +709,7 @@ Bignum modpow_simple(Bignum base_in, Bignum exp, Bignum mod)
|
|||||||
while (j >= 0) {
|
while (j >= 0) {
|
||||||
internal_mul(a + mlen, a + mlen, b, mlen, scratch);
|
internal_mul(a + mlen, a + mlen, b, mlen, scratch);
|
||||||
internal_mod(b, mlen * 2, m, mlen, NULL, 0);
|
internal_mod(b, mlen * 2, m, mlen, NULL, 0);
|
||||||
if ((exp[exp[0] - i] & (1 << j)) != 0) {
|
if ((exp[exp[0] - i] & ((BignumInt)1 << j)) != 0) {
|
||||||
internal_mul(b + mlen, n, a, mlen, scratch);
|
internal_mul(b + mlen, n, a, mlen, scratch);
|
||||||
internal_mod(a, mlen * 2, m, mlen, NULL, 0);
|
internal_mod(a, mlen * 2, m, mlen, NULL, 0);
|
||||||
} else {
|
} else {
|
||||||
@ -847,7 +846,7 @@ Bignum modpow(Bignum base_in, Bignum exp, Bignum mod)
|
|||||||
/* Skip leading zero bits of exp. */
|
/* Skip leading zero bits of exp. */
|
||||||
i = 0;
|
i = 0;
|
||||||
j = BIGNUM_INT_BITS-1;
|
j = BIGNUM_INT_BITS-1;
|
||||||
while (i < (int)exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
|
while (i < (int)exp[0] && (exp[exp[0] - i] & ((BignumInt)1 << j)) == 0) {
|
||||||
j--;
|
j--;
|
||||||
if (j < 0) {
|
if (j < 0) {
|
||||||
i++;
|
i++;
|
||||||
@ -860,7 +859,7 @@ Bignum modpow(Bignum base_in, Bignum exp, Bignum mod)
|
|||||||
while (j >= 0) {
|
while (j >= 0) {
|
||||||
internal_mul(a + len, a + len, b, len, scratch);
|
internal_mul(a + len, a + len, b, len, scratch);
|
||||||
monty_reduce(b, n, mninv, scratch, len);
|
monty_reduce(b, n, mninv, scratch, len);
|
||||||
if ((exp[exp[0] - i] & (1 << j)) != 0) {
|
if ((exp[exp[0] - i] & ((BignumInt)1 << j)) != 0) {
|
||||||
internal_mul(b + len, x, a, len, scratch);
|
internal_mul(b + len, x, a, len, scratch);
|
||||||
monty_reduce(a, n, mninv, scratch, len);
|
monty_reduce(a, n, mninv, scratch, len);
|
||||||
} else {
|
} else {
|
||||||
@ -1139,7 +1138,8 @@ Bignum bignum_from_bytes(const unsigned char *data, int nbytes)
|
|||||||
result[i] = 0;
|
result[i] = 0;
|
||||||
for (i = nbytes; i--;) {
|
for (i = nbytes; i--;) {
|
||||||
unsigned char byte = *data++;
|
unsigned char byte = *data++;
|
||||||
result[1 + i / BIGNUM_INT_BYTES] |= byte << (8*i % BIGNUM_INT_BITS);
|
result[1 + i / BIGNUM_INT_BYTES] |=
|
||||||
|
(BignumInt)byte << (8*i % BIGNUM_INT_BITS);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (result[0] > 1 && result[result[0]] == 0)
|
while (result[0] > 1 && result[result[0]] == 0)
|
||||||
@ -1161,7 +1161,8 @@ Bignum bignum_from_bytes_le(const unsigned char *data, int nbytes)
|
|||||||
result[i] = 0;
|
result[i] = 0;
|
||||||
for (i = 0; i < nbytes; ++i) {
|
for (i = 0; i < nbytes; ++i) {
|
||||||
unsigned char byte = *data++;
|
unsigned char byte = *data++;
|
||||||
result[1 + i / BIGNUM_INT_BYTES] |= byte << (8*i % BIGNUM_INT_BITS);
|
result[1 + i / BIGNUM_INT_BYTES] |=
|
||||||
|
(BignumInt)byte << (8*i % BIGNUM_INT_BITS);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (result[0] > 1 && result[result[0]] == 0)
|
while (result[0] > 1 && result[result[0]] == 0)
|
||||||
@ -1314,7 +1315,7 @@ void bignum_set_bit(Bignum bn, int bitnum, int value)
|
|||||||
abort(); /* beyond the end */
|
abort(); /* beyond the end */
|
||||||
else {
|
else {
|
||||||
int v = bitnum / BIGNUM_INT_BITS + 1;
|
int v = bitnum / BIGNUM_INT_BITS + 1;
|
||||||
int mask = 1 << (bitnum % BIGNUM_INT_BITS);
|
BignumInt mask = (BignumInt)1 << (bitnum % BIGNUM_INT_BITS);
|
||||||
if (value)
|
if (value)
|
||||||
bn[v] |= mask;
|
bn[v] |= mask;
|
||||||
else
|
else
|
||||||
|
6
sshccp.c
6
sshccp.c
@ -198,7 +198,8 @@ static void bigval_import_le(bigval *r, const void *vdata, int len)
|
|||||||
int i;
|
int i;
|
||||||
bigval_clear(r);
|
bigval_clear(r);
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
r->w[i / BIGNUM_INT_BYTES] |= data[i] << (8 * (i % BIGNUM_INT_BYTES));
|
r->w[i / BIGNUM_INT_BYTES] |=
|
||||||
|
(BignumInt)data[i] << (8 * (i % BIGNUM_INT_BYTES));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void bigval_export_le(const bigval *r, void *vdata, int len)
|
static void bigval_export_le(const bigval *r, void *vdata, int len)
|
||||||
@ -951,7 +952,8 @@ static void poly1305_feed_chunk(struct poly1305 *ctx,
|
|||||||
{
|
{
|
||||||
bigval c;
|
bigval c;
|
||||||
bigval_import_le(&c, chunk, len);
|
bigval_import_le(&c, chunk, len);
|
||||||
c.w[len / BIGNUM_INT_BYTES] |= 1 << (8 * (len % BIGNUM_INT_BYTES));
|
c.w[len / BIGNUM_INT_BYTES] |=
|
||||||
|
(BignumInt)1 << (8 * (len % BIGNUM_INT_BYTES));
|
||||||
bigval_add(&c, &c, &ctx->h);
|
bigval_add(&c, &c, &ctx->h);
|
||||||
bigval_mul_mod_p(&ctx->h, &c, &ctx->r);
|
bigval_mul_mod_p(&ctx->h, &c, &ctx->r);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user