wolfcrypt/src/integer.c: fix mp_read_unsigned_bin() accounting on mp_int.used_bits to avoid spurious .used > .alloc condition at loop exit.

This commit is contained in:
Daniel Pouzzner
2020-12-18 02:05:18 -06:00
parent b11b08bb10
commit b0ec2bf058

View File

@ -697,10 +697,18 @@ int mp_mod_2d (mp_int * a, int b, mp_int * c)
int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c) int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
{ {
int res; int res;
int digits_needed;
/* make sure there are at least two digits */ while (c > 0 && b[0] == 0) {
if (a->alloc < 2) { c--;
if ((res = mp_grow(a, 2)) != MP_OKAY) { b++;
}
digits_needed = ((c * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT;
/* make sure there are enough digits available */
if (a->alloc < digits_needed) {
if ((res = mp_grow(a, digits_needed)) != MP_OKAY) {
return res; return res;
} }
} }
@ -716,11 +724,13 @@ int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
#ifndef MP_8BIT #ifndef MP_8BIT
a->dp[0] |= *b++; a->dp[0] |= *b++;
a->used += 1; if (a->used == 0)
a->used = 1;
#else #else
a->dp[0] = (*b & MP_MASK); a->dp[0] = (*b & MP_MASK);
a->dp[1] |= ((*b++ >> 7U) & 1); a->dp[1] |= ((*b++ >> 7U) & 1);
a->used += 2; if (a->used == 0)
a->used = 2;
#endif #endif
} }
mp_clamp (a); mp_clamp (a);