Peer review fixes.

This commit is contained in:
David Garske
2020-10-28 17:09:15 -07:00
parent 589057245f
commit ef7a987759

View File

@@ -15427,22 +15427,34 @@ int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r, mp_int* s)
return 0; return 0;
} }
/* determine if leading bit is set and trim leading zeros */ /* determine if leading bit is set */
static int is_leading_bit_set(const byte** input, word32 *sz) static int is_leading_bit_set(const byte* input, word32 sz)
{ {
int i; int i;
word32 leadingZeroCount = 0;
const byte* tmp = *input;
byte c = 0; byte c = 0;
for (i=0; i<(int)*sz; i++) { for (i=0; i<(int)sz; i++) {
c = tmp[i]; c = input[i];
if (c != 0) if (c != 0)
break; break;
}
return (c & 0x80) != 0;
}
static int trim_leading_zeros(const byte** input, word32 sz)
{
int i, leadingZeroCount = 0;
const byte* tmp = *input;
for (i=0; i<(int)sz; i++) {
if (tmp[i] != 0)
break;
leadingZeroCount++; leadingZeroCount++;
} }
/* catch all zero case */
if (sz > 0 && leadingZeroCount == (int)sz) {
leadingZeroCount--;
}
*input += leadingZeroCount; *input += leadingZeroCount;
*sz -= leadingZeroCount; sz -= leadingZeroCount;
return (c & 0x80) != 0; return sz;
} }
/* Der Encode r & s ints into out, outLen is (in/out) size */ /* Der Encode r & s ints into out, outLen is (in/out) size */
@@ -15456,9 +15468,11 @@ int StoreECC_DSA_Sig_Bin(byte* out, word32* outLen, const byte* r, word32 rLen,
/* If the leading bit on the INTEGER is a 1, add a leading zero */ /* If the leading bit on the INTEGER is a 1, add a leading zero */
/* Add leading zero if MSB is set */ /* Add leading zero if MSB is set */
rAddLeadZero = is_leading_bit_set(r, rLen);
sAddLeadZero = is_leading_bit_set(s, sLen);
/* Trim leading zeros */ /* Trim leading zeros */
rAddLeadZero = is_leading_bit_set(&r, &rLen); rLen = trim_leading_zeros(&r, rLen);
sAddLeadZero = is_leading_bit_set(&s, &sLen); sLen = trim_leading_zeros(&s, sLen);
if (*outLen < (rLen + rAddLeadZero + sLen + sAddLeadZero + if (*outLen < (rLen + rAddLeadZero + sLen + sAddLeadZero +
headerSz + 2)) /* SEQ_TAG + LEN(ENUM) */ headerSz + 2)) /* SEQ_TAG + LEN(ENUM) */