Fix to reduce memory use with small stack on ECC key import.

This commit is contained in:
David Garske
2020-10-22 12:41:49 -07:00
parent f75dc4727d
commit be8e4d1949

View File

@ -15474,7 +15474,7 @@ int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key,
int curve_id = ECC_CURVE_DEF; int curve_id = ECC_CURVE_DEF;
#ifdef WOLFSSL_SMALL_STACK #ifdef WOLFSSL_SMALL_STACK
byte* priv; byte* priv;
byte* pub; byte* pub = NULL;
#else #else
byte priv[ECC_MAXSIZE+1]; byte priv[ECC_MAXSIZE+1];
byte pub[2*(ECC_MAXSIZE+1)]; /* public key has two parts plus header */ byte pub[2*(ECC_MAXSIZE+1)]; /* public key has two parts plus header */
@ -15507,15 +15507,9 @@ int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key,
return BUFFER_E; return BUFFER_E;
#ifdef WOLFSSL_SMALL_STACK #ifdef WOLFSSL_SMALL_STACK
priv = (byte*)XMALLOC(ECC_MAXSIZE+1, key->heap, DYNAMIC_TYPE_TMP_BUFFER); priv = (byte*)XMALLOC(length+1, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
if (priv == NULL) if (priv == NULL)
return MEMORY_E; return MEMORY_E;
pub = (byte*)XMALLOC(2*(ECC_MAXSIZE+1), key->heap, DYNAMIC_TYPE_TMP_BUFFER);
if (pub == NULL) {
XFREE(priv, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
return MEMORY_E;
}
#endif #endif
/* priv key */ /* priv key */
@ -15563,10 +15557,20 @@ int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key,
if (ret == 0) { if (ret == 0) {
/* pub key */ /* pub key */
pubSz = length; pubSz = length;
if (pubSz < 2*(ECC_MAXSIZE+1)) { if (pubSz <= 2*ECC_MAXSIZE) {
XMEMCPY(pub, &input[*inOutIdx], pubSz); #ifdef WOLFSSL_SMALL_STACK
*inOutIdx += length; pub = (byte*)XMALLOC(pubSz+1, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
pubData = pub; if (pub == NULL) {
XFREE(priv, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
ret = MEMORY_E;
}
else
#endif
{
XMEMCPY(pub, &input[*inOutIdx], pubSz);
*inOutIdx += length;
pubData = pub;
}
} }
else else
ret = BUFFER_E; ret = BUFFER_E;