Merge pull request #4223 from SparkiDev/mem_usage_fixes_1

Memory allocation: fixes from memory usage generation
This commit is contained in:
David Garske
2021-07-21 08:20:09 -07:00
committed by GitHub
4 changed files with 53 additions and 12 deletions

View File

@ -1100,7 +1100,7 @@ AC_ARG_ENABLE([lowresource],
if test "$ENABLED_LOWRESOURCE" = "yes"
then
# low memory / flash flags
AM_CFLAGS="$AM_CFLAGS -DNO_SESSION_CACHE -DRSA_LOW_MEM -DALT_ECC_SIZE -DGCM_SMALL -DCURVE25519_SMALL -DED25519_SMALL -DWOLFSSL_SMALL_CERT_VERIFY"
AM_CFLAGS="$AM_CFLAGS -DNO_SESSION_CACHE -DRSA_LOW_MEM -DGCM_SMALL -DCURVE25519_SMALL -DED25519_SMALL -DWOLFSSL_SMALL_CERT_VERIFY"
# low flash flags
AM_CFLAGS="$AM_CFLAGS -DUSE_SLOW_SHA -DUSE_SLOW_SHA256 -DUSE_SLOW_SHA512"
@ -2044,6 +2044,11 @@ then
AM_CFLAGS="$AM_CFLAGS -DWC_ECC_NONBLOCK"
fi
if test "$ENABLED_LOWRESOURCE" = "yes" && test "$ENABLED_FASTMATH" = "yes"
then
AM_CFLAGS="$AM_CFLAGS -DALT_ECC_SIZE"
fi
ENABLED_CERTS=yes
fi

View File

@ -7043,8 +7043,7 @@ static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
kse->pubKeyLen = keySize * 2 + 1;
/* Allocate an ECC key to hold private key. */
kse->key = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap,
DYNAMIC_TYPE_PRIVATE_KEY);
kse->key = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap, DYNAMIC_TYPE_ECC);
if (kse->key == NULL) {
WOLFSSL_MSG("EccTempKey Memory error");
return MEMORY_E;

View File

@ -699,16 +699,18 @@ int wc_ed25519_verify_msg_ex(const byte* sig, word32 sigLen, const byte* msg,
return ret;
#endif
ret = ed25519_verify_msg_init_with_sha(sig, sigLen, key, sha,
type, context, contextLen);
if (ret < 0)
return ret;
ret = ed25519_verify_msg_init_with_sha(sig, sigLen, key, sha, type, context,
contextLen);
if (ret == 0)
ret = ed25519_verify_msg_update_with_sha(msg, msgLen, key, sha);
if (ret < 0)
return ret;
if (ret == 0)
ret = ed25519_verify_msg_final_with_sha(sig, sigLen, res, key, sha);
return ed25519_verify_msg_final_with_sha(sig, sigLen, res, key, sha);
#ifndef WOLFSSL_ED25519_PERSISTENT_SHA
ed25519_hash_free(key, sha);
#endif
return ret;
}
/*

View File

@ -823,6 +823,9 @@ static int RsaMGF1(enum wc_HashType hType, byte* seed, word32 seedSz,
int ret;
word32 counter;
word32 idx;
#ifdef WOLFSSL_SMALL_STACK_CACHE
wc_HashAlg *hash;
#endif
hLen = wc_HashGetDigestSize(hType);
counter = 0;
idx = 0;
@ -847,11 +850,31 @@ static int RsaMGF1(enum wc_HashType hType, byte* seed, word32 seedSz,
}
else {
/* use array on the stack */
#ifndef WOLFSSL_SMALL_STACK_CACHE
tmpSz = sizeof(tmpA);
#endif
tmp = tmpA;
tmpF = 0; /* no need to free memory at end */
}
#ifdef WOLFSSL_SMALL_STACK_CACHE
hash = (wc_HashAlg*)XMALLOC(sizeof(*hash), heap, DYNAMIC_TYPE_DIGEST);
if (hash == NULL) {
if (tmpF) {
XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
}
return MEMORY_E;
}
ret = wc_HashInit_ex(hash, hType, heap, INVALID_DEVID);
if (ret != 0) {
XFREE(hash, heap, DYNAMIC_TYPE_DIGEST);
if (tmpF) {
XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
}
return ret;
}
#endif
do {
int i = 0;
XMEMCPY(tmp, seed, seedSz);
@ -863,7 +886,15 @@ static int RsaMGF1(enum wc_HashType hType, byte* seed, word32 seedSz,
tmp[seedSz + 3] = (byte)((counter) & 0xFF);
/* hash and append to existing output */
if ((ret = wc_Hash(hType, tmp, (seedSz + 4), tmp, tmpSz)) != 0) {
#ifdef WOLFSSL_SMALL_STACK_CACHE
ret = wc_HashUpdate(hash, hType, tmp, (seedSz + 4));
if (ret == 0) {
ret = wc_HashFinal(hash, hType, tmp);
}
#else
ret = wc_Hash(hType, tmp, (seedSz + 4), tmp, tmpSz);
#endif
if (ret != 0) {
/* check for if dynamic memory was needed, then free */
if (tmpF) {
XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
@ -881,6 +912,10 @@ static int RsaMGF1(enum wc_HashType hType, byte* seed, word32 seedSz,
if (tmpF) {
XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
}
#ifdef WOLFSSL_SMALL_STACK_CACHE
wc_HashFree(hash, hType);
XFREE(hash, heap, DYNAMIC_TYPE_DIGEST);
#endif
return 0;
}