Topic: XMALLOC with static memory allocation
Hi, I am trying to implement RSA functionality on an embedded system with wolfCrypt and I have been running in to some issues that I was hoping to get some help with. Below is a snippet of code that I am trying to test the functionality of RSA with:
uint32_t serialNo = 691;
byte serialNumber[4] = {serialNo & 0xff, serialNo >> 8, serialNo >> 16, serialNo >> 24};
/* SHA begin */
wc_Sha sha;
byte shaSum[32];
wc_InitSha(&sha);
wc_ShaUpdate(&sha, serialNumber, sizeof(serialNumber));
wc_ShaFinal(&sha, shaSum);
/* SHA end */
/* RSA begin */
WOLFSSL_HEAP_HINT *hint = NULL;
unsigned char memory[2048];
int memorySz = 2048;
int flag = WOLFMEM_GENERAL;
int ret;
ret = wc_LoadStaticMemory(&hint, memory, memorySz, flag, 0);
RsaKey rsaPubKey;
wc_InitRsaKey(&rsaPubKey, &hint);
byte rsaKey[] = { //DER formatted RSA key};
word32 idx = 0;
wc_RsaPublicKeyDecode(rsaKey, &idx, &rsaPubKey, sizeof(rsaKey));
byte plain[1024];
word32 plainSz = wc_RsaPrivateDecrypt(&shaSum[0], sizeof(shaSum), &plain[0], sizeof(plain),
&rsaPubKey);
/* RSA end */
I am seeing that the function wc_LoadStaticMemory() returns without any errors, but when the following call to XMALLOC is made within the wc_RsaPrivateDecrypt() function I am seeing seeing that MEMORY_E is being returned, meaning that the actual malloc call to the static memory pool is failing. If anyone would be able to provide me with some insight as to why this may be occurring it would be greatly appreciated.
/* if not doing this inline then allocate a buffer for it */
if (outPtr == NULL) {
key->data = (byte*)XMALLOC(inLen, key->heap, DYNAMIC_TYPE_WOLF_BIGINT);
key->dataIsAlloc = 1;
if (key->data == NULL) {
ret = MEMORY_E;
break;
}
XMEMCPY(key->data, in, inLen);
}
Thank you for your time,
Thomas Hickey