forked from wolfSSL/wolfssl
Fixes for building with WOLFSSL_NO_MALLOC
. Fixes for static memory in bench embedded case. Added support for elimination of XMALLOC/XFREE when building with NO_WOLFSSL_MEMORY
and WOLFSSL_NO_MALLOC
. If used with WOLFSSL_MALLOC_CHECK
will trap failures with malloc_check
This commit is contained in:
@@ -753,9 +753,14 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
|
|||||||
byte seed[MAX_SEED_SZ];
|
byte seed[MAX_SEED_SZ];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
|
||||||
rng->drbg =
|
rng->drbg =
|
||||||
(struct DRBG*)XMALLOC(sizeof(DRBG), rng->heap,
|
(struct DRBG*)XMALLOC(sizeof(DRBG), rng->heap,
|
||||||
DYNAMIC_TYPE_RNG);
|
DYNAMIC_TYPE_RNG);
|
||||||
|
#else
|
||||||
|
rng->drbg = (struct DRBG*)rng->drbg_data;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (rng->drbg == NULL) {
|
if (rng->drbg == NULL) {
|
||||||
ret = MEMORY_E;
|
ret = MEMORY_E;
|
||||||
}
|
}
|
||||||
@@ -775,7 +780,9 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
|
|||||||
ret = Hash_DRBG_Generate(rng->drbg, NULL, 0);
|
ret = Hash_DRBG_Generate(rng->drbg, NULL, 0);
|
||||||
|
|
||||||
if (ret != DRBG_SUCCESS) {
|
if (ret != DRBG_SUCCESS) {
|
||||||
|
#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
|
||||||
XFREE(rng->drbg, rng->heap, DYNAMIC_TYPE_RNG);
|
XFREE(rng->drbg, rng->heap, DYNAMIC_TYPE_RNG);
|
||||||
|
#endif
|
||||||
rng->drbg = NULL;
|
rng->drbg = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -953,7 +960,9 @@ int wc_FreeRng(WC_RNG* rng)
|
|||||||
if (Hash_DRBG_Uninstantiate(rng->drbg) != DRBG_SUCCESS)
|
if (Hash_DRBG_Uninstantiate(rng->drbg) != DRBG_SUCCESS)
|
||||||
ret = RNG_FAILURE_E;
|
ret = RNG_FAILURE_E;
|
||||||
|
|
||||||
|
#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
|
||||||
XFREE(rng->drbg, rng->heap, DYNAMIC_TYPE_RNG);
|
XFREE(rng->drbg, rng->heap, DYNAMIC_TYPE_RNG);
|
||||||
|
#endif
|
||||||
rng->drbg = NULL;
|
rng->drbg = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1293,6 +1293,14 @@ static int RsaUnPad_PSS(byte *pkcsBlock, unsigned int pkcsBlockLen,
|
|||||||
int ret;
|
int ret;
|
||||||
byte* tmp;
|
byte* tmp;
|
||||||
int hLen, i;
|
int hLen, i;
|
||||||
|
#if defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_STATIC_MEMORY)
|
||||||
|
byte tmp_buf[RSA_MAX_SIZE/8];
|
||||||
|
tmp = tmp_buf;
|
||||||
|
|
||||||
|
if (pkcsBlockLen > RSA_MAX_SIZE/8) {
|
||||||
|
return MEMORY_E;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
hLen = wc_HashGetDigestSize(hType);
|
hLen = wc_HashGetDigestSize(hType);
|
||||||
if (hLen < 0)
|
if (hLen < 0)
|
||||||
@@ -1316,9 +1324,11 @@ static int RsaUnPad_PSS(byte *pkcsBlock, unsigned int pkcsBlockLen,
|
|||||||
return BAD_PADDING_E;
|
return BAD_PADDING_E;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
|
||||||
tmp = (byte*)XMALLOC(pkcsBlockLen, heap, DYNAMIC_TYPE_RSA_BUFFER);
|
tmp = (byte*)XMALLOC(pkcsBlockLen, heap, DYNAMIC_TYPE_RSA_BUFFER);
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
return MEMORY_E;
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
|
||||||
if ((ret = RsaMGF(mgf, pkcsBlock + pkcsBlockLen - 1 - hLen, hLen,
|
if ((ret = RsaMGF(mgf, pkcsBlock + pkcsBlockLen - 1 - hLen, hLen,
|
||||||
tmp, pkcsBlockLen - 1 - hLen, heap)) != 0) {
|
tmp, pkcsBlockLen - 1 - hLen, heap)) != 0) {
|
||||||
@@ -1342,7 +1352,9 @@ static int RsaUnPad_PSS(byte *pkcsBlock, unsigned int pkcsBlockLen,
|
|||||||
for (i++; i < (int)(pkcsBlockLen - 1 - hLen); i++)
|
for (i++; i < (int)(pkcsBlockLen - 1 - hLen); i++)
|
||||||
pkcsBlock[i] ^= tmp[i];
|
pkcsBlock[i] ^= tmp[i];
|
||||||
|
|
||||||
|
#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
|
||||||
XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
|
XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
|
||||||
|
#endif
|
||||||
|
|
||||||
*output = pkcsBlock + pkcsBlockLen - (hLen + saltLen + 1);
|
*output = pkcsBlock + pkcsBlockLen - (hLen + saltLen + 1);
|
||||||
return saltLen + hLen;
|
return saltLen + hLen;
|
||||||
|
@@ -440,7 +440,7 @@ static void myFipsCb(int ok, int err, const char* hash)
|
|||||||
|
|
||||||
#ifdef WOLFSSL_STATIC_MEMORY
|
#ifdef WOLFSSL_STATIC_MEMORY
|
||||||
#ifdef BENCH_EMBEDDED
|
#ifdef BENCH_EMBEDDED
|
||||||
static byte gTestMemory[10000];
|
static byte gTestMemory[14000];
|
||||||
#elif defined(WOLFSSL_CERT_EXT)
|
#elif defined(WOLFSSL_CERT_EXT)
|
||||||
static byte gTestMemory[140000];
|
static byte gTestMemory[140000];
|
||||||
#elif defined(USE_FAST_MATH) && !defined(ALT_ECC_SIZE)
|
#elif defined(USE_FAST_MATH) && !defined(ALT_ECC_SIZE)
|
||||||
@@ -23389,11 +23389,13 @@ int mutex_test(void)
|
|||||||
#ifdef WOLFSSL_PTHREADS
|
#ifdef WOLFSSL_PTHREADS
|
||||||
wolfSSL_Mutex m;
|
wolfSSL_Mutex m;
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef WOLFSSL_NO_MALLOC
|
||||||
wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
|
wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
|
||||||
if (mm == NULL)
|
if (mm == NULL)
|
||||||
return -9900;
|
return -9900;
|
||||||
wc_FreeMutex(mm);
|
wc_FreeMutex(mm);
|
||||||
XFREE(mm, NULL, DYNAMIC_TYPE_MUTEX);
|
XFREE(mm, NULL, DYNAMIC_TYPE_MUTEX);
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef WOLFSSL_PTHREADS
|
#ifdef WOLFSSL_PTHREADS
|
||||||
if (wc_InitMutex(&m) != 0)
|
if (wc_InitMutex(&m) != 0)
|
||||||
@@ -23427,6 +23429,7 @@ static void *my_Malloc_cb(size_t size)
|
|||||||
return malloc(size);
|
return malloc(size);
|
||||||
#else
|
#else
|
||||||
WOLFSSL_MSG("No malloc available");
|
WOLFSSL_MSG("No malloc available");
|
||||||
|
return NULL;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
static void my_Free_cb(void *ptr)
|
static void my_Free_cb(void *ptr)
|
||||||
@@ -23445,6 +23448,7 @@ static void *my_Realloc_cb(void *ptr, size_t size)
|
|||||||
return realloc(ptr, size);
|
return realloc(ptr, size);
|
||||||
#else
|
#else
|
||||||
WOLFSSL_MSG("No realloc available");
|
WOLFSSL_MSG("No realloc available");
|
||||||
|
return NULL;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23460,6 +23464,7 @@ int memcb_test(void)
|
|||||||
if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
|
if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
|
||||||
return -10000;
|
return -10000;
|
||||||
|
|
||||||
|
#ifndef WOLFSSL_NO_MALLOC
|
||||||
/* test realloc */
|
/* test realloc */
|
||||||
b = (byte*)XREALLOC(b, 1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
b = (byte*)XREALLOC(b, 1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
if (b == NULL) {
|
if (b == NULL) {
|
||||||
@@ -23485,6 +23490,7 @@ int memcb_test(void)
|
|||||||
if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
|
if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
|
||||||
#endif
|
#endif
|
||||||
ret = -10006;
|
ret = -10006;
|
||||||
|
#endif /* !WOLFSSL_NO_MALLOC */
|
||||||
|
|
||||||
exit_memcb:
|
exit_memcb:
|
||||||
|
|
||||||
|
@@ -156,6 +156,10 @@ struct WC_RNG {
|
|||||||
#ifdef HAVE_HASHDRBG
|
#ifdef HAVE_HASHDRBG
|
||||||
/* Hash-based Deterministic Random Bit Generator */
|
/* Hash-based Deterministic Random Bit Generator */
|
||||||
struct DRBG* drbg;
|
struct DRBG* drbg;
|
||||||
|
#if defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_STATIC_MEMORY)
|
||||||
|
#define DRBG_STRUCT_SZ ((sizeof(word32)*2) + (DRBG_SEED_LEN*2) + sizeof(byte))
|
||||||
|
byte drbg_data[DRBG_STRUCT_SZ];
|
||||||
|
#endif
|
||||||
byte status;
|
byte status;
|
||||||
#endif
|
#endif
|
||||||
#ifdef WOLFSSL_ASYNC_CRYPT
|
#ifdef WOLFSSL_ASYNC_CRYPT
|
||||||
|
@@ -271,11 +271,29 @@
|
|||||||
#define XREALLOC(p, n, h, t) m2mb_os_realloc((p), (n))
|
#define XREALLOC(p, n, h, t) m2mb_os_realloc((p), (n))
|
||||||
|
|
||||||
#elif defined(NO_WOLFSSL_MEMORY)
|
#elif defined(NO_WOLFSSL_MEMORY)
|
||||||
|
#ifdef WOLFSSL_NO_MALLOC
|
||||||
|
/* this platform does not support heap use */
|
||||||
|
#ifdef WOLFSSL_MALLOC_CHECK
|
||||||
|
#include <stdio.h>
|
||||||
|
static inline void* malloc_check(size_t sz) {
|
||||||
|
printf("wolfSSL_malloc failed");
|
||||||
|
return NULL;
|
||||||
|
};
|
||||||
|
#define XMALLOC(s, h, t) malloc_check((s))
|
||||||
|
#define XFREE(p, h, t)
|
||||||
|
#define XREALLOC(p, n, h, t) (NULL)
|
||||||
|
#else
|
||||||
|
#define XMALLOC(s, h, t) (NULL)
|
||||||
|
#define XFREE(p, h, t)
|
||||||
|
#define XREALLOC(p, n, h, t) (NULL)
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
/* just use plain C stdlib stuff if desired */
|
/* just use plain C stdlib stuff if desired */
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#define XMALLOC(s, h, t) ((void)h, (void)t, malloc((s)))
|
#define XMALLOC(s, h, t) ((void)h, (void)t, malloc((s)))
|
||||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) free((xp));}
|
#define XFREE(p, h, t) {void* xp = (p); if((xp)) free((xp));}
|
||||||
#define XREALLOC(p, n, h, t) realloc((p), (n))
|
#define XREALLOC(p, n, h, t) realloc((p), (n))
|
||||||
|
#endif
|
||||||
#elif !defined(MICRIUM_MALLOC) && !defined(EBSNET) \
|
#elif !defined(MICRIUM_MALLOC) && !defined(EBSNET) \
|
||||||
&& !defined(WOLFSSL_SAFERTOS) && !defined(FREESCALE_MQX) \
|
&& !defined(WOLFSSL_SAFERTOS) && !defined(FREESCALE_MQX) \
|
||||||
&& !defined(FREESCALE_KSDK_MQX) && !defined(FREESCALE_FREE_RTOS) \
|
&& !defined(FREESCALE_KSDK_MQX) && !defined(FREESCALE_FREE_RTOS) \
|
||||||
|
Reference in New Issue
Block a user