make sure to always check mp_to_*, normal math could fail

This commit is contained in:
toddouska 2013-10-29 17:38:12 -07:00
parent f402d7eed9
commit 3d19604bfb
3 changed files with 23 additions and 15 deletions

View File

@ -1359,7 +1359,7 @@ static int GetCertHeader(DecodedCert* cert)
len = mp_unsigned_bin_size(&mpi);
if (len < (int)sizeof(serialTmp)) {
if (mp_to_unsigned_bin(&mpi, serialTmp) == MP_OKAY) {
if ( (ret = mp_to_unsigned_bin(&mpi, serialTmp)) == MP_OKAY) {
if (len > EXTERNAL_SERIAL_SIZE)
len = EXTERNAL_SERIAL_SIZE;
XMEMCPY(cert->serial, serialTmp, len);

View File

@ -1582,20 +1582,23 @@ static int ecc_mul2add(ecc_point* A, mp_int* kA,
if (err == MP_OKAY) {
/* extract and justify kA */
mp_to_unsigned_bin(kA, (len - lenA) + tA);
err = mp_to_unsigned_bin(kA, (len - lenA) + tA);
/* extract and justify kB */
mp_to_unsigned_bin(kB, (len - lenB) + tB);
if (err == MP_OKAY)
err = mp_to_unsigned_bin(kB, (len - lenB) + tB);
/* allocate the table */
for (x = 0; x < 16; x++) {
precomp[x] = ecc_new_point();
if (precomp[x] == NULL) {
for (y = 0; y < x; ++y) {
ecc_del_point(precomp[y]);
if (err == MP_OKAY) {
for (x = 0; x < 16; x++) {
precomp[x] = ecc_new_point();
if (precomp[x] == NULL) {
for (y = 0; y < x; ++y) {
ecc_del_point(precomp[y]);
}
err = GEN_MEM_ERR;
break;
}
err = GEN_MEM_ERR;
break;
}
}
}
@ -1943,6 +1946,7 @@ int ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
{
byte buf[ECC_BUFSIZE];
word32 numlen;
int ret = MP_OKAY;
if (key == NULL || out == NULL || outLen == NULL)
return ECC_BAD_ARG_E;
@ -1962,14 +1966,18 @@ int ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
/* pad and store x */
XMEMSET(buf, 0, sizeof(buf));
mp_to_unsigned_bin(&key->pubkey.x,
ret = mp_to_unsigned_bin(&key->pubkey.x,
buf + (numlen - mp_unsigned_bin_size(&key->pubkey.x)));
if (ret != MP_OKAY)
return ret;
XMEMCPY(out+1, buf, numlen);
/* pad and store y */
XMEMSET(buf, 0, sizeof(buf));
mp_to_unsigned_bin(&key->pubkey.y,
ret = mp_to_unsigned_bin(&key->pubkey.y,
buf + (numlen - mp_unsigned_bin_size(&key->pubkey.y)));
if (ret != MP_OKAY)
return ret;
XMEMCPY(out+1+numlen, buf, numlen);
*outLen = 1 + 2*numlen;

View File

@ -325,15 +325,15 @@ int PKCS12_PBKDF(byte* output, const byte* passwd, int passLen,const byte* salt,
if (outSz > (int)v) {
/* take off MSB */
byte tmp[129];
mp_to_unsigned_bin(&res, tmp);
ret = mp_to_unsigned_bin(&res, tmp);
XMEMCPY(I + i, tmp + 1, v);
}
else if (outSz < (int)v) {
XMEMSET(I + i, 0, v - outSz);
mp_to_unsigned_bin(&res, I + i + v - outSz);
ret = mp_to_unsigned_bin(&res, I + i + v - outSz);
}
else
mp_to_unsigned_bin(&res, I + i);
ret = mp_to_unsigned_bin(&res, I + i);
}
mp_clear(&i1);