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

Share

Re: XMALLOC with static memory allocation

Hi Thomas,

Can you try changing the RSA init to:

wc_InitRsaKey(&rsaPubKey, hint);

?

Have you tried using a larger static memory pool? These get split into buckets so you won't have a large enough buffer. See WOLFMEM_BUCKETS. See https://www.wolfssl.com/docs/static-buffer-allocation/

Thanks,
David Garske, wolfSSL

Share

Re: XMALLOC with static memory allocation

Thank you very much for pointing out the error in wc_InitRsaKey.

And so if I am understanding this correctly, I can edit the values in WOLFMEM_DIST to create different numbers of buckets, the size of which are dictated by WOLFMEM_BUCKETS. Is that the correct way to interpret this? If so, is there a minimum size that I need to allocate for the wolfcrypt libraries to work? Or is that just dependent on how much memory my application actually needs to utilize based on my own usage?

Thank you,
Thomas Hickey

Share

Re: XMALLOC with static memory allocation

Hi Thomas,

Correct. The buckets and number of buckets can be overridden at build-time. See https://github.com/wolfSSL/wolfssl/blob … mory.h#L99

The buckets `WOLFMEM_BUCKETS` and number of buckets `WOLFMEM_MAX_BUCKETS` can all be setup for your specific implementation.

I don't have a good template for the buckets with RSA. I would recommend logging the mallocs() using `WOLFSSL_DEBUG_MEMORY` to see what your heap profile looks like. Then setup buckets of appropriate size. The buckets are buffer pools are various sizes. If the next available size is not available it will select the next largest.

Thanks,
David Garske, wolfSSL

Share

Re: XMALLOC with static memory allocation

Hi David,

Thank you for your help with the static memory allocation. I seem to have resolved my issues in regards to that, but now I am facing another issue.

Within the function wc_RsaFuncitonSync called from within wc_RsaFunction I am seeing the following errors and I am not sure how to resolve these issues:

/* tmpa = tmp^dP mod p */
r = mp_exptmod(&tmp, &key->dP, &key->p, &tmpa);
ret = RET_ERR(ret, r, MP_EXPTMOD_E);

R = -1
Ret = -112 (MP_EXPTMOD_E)

/* tmpb = tmp^dQ mod q */
r = mp_exptmod(&tmp, &key->dQ, &key->q, &tmpb);
ret = RET_ERR(ret, r, MP_EXPTMOD_E);

R = -1
Ret = -112

r = mp_mulmod(&tmp, &key->u, &key->p, &tmp);
ret = RET_ERR(ret, r, MP_MULMOD_E);

R = -1

Upon digging a bit further it seems that when mp_exptmod() ends up eventually calling making the call

x = fp_count_bits (X);

from within

static int _fp_exptmod(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)

I see it enter the conditional statement

if (a->used == 0) {
    return 0;
}

which eventually causes the error that I see at the higher level.

In regards to the error with mp_mulmod() I see that when the call

err = fp_mod(&t, c, d);

is made from within

int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)

I see that the conditional statement

if ((err = fp_div(a, b, NULL, &t)) != FP_OKAY) {
   return err;
}

is the root of the error that I see at the higher level.

Do you have any clue as to why these errors would be occuring?

Thank you,
Thomas Hickey

Share