forked from wolfSSL/wolfssl
Merge pull request #2258 from dgarske/no_stdlib
Improvements for no standard lib and no malloc/free builds
This commit is contained in:
@@ -113,8 +113,16 @@
|
||||
#include <stdlib.h> /* we're using malloc / free direct here */
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#ifndef STRING_USER
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
/* enable way for customer to override test/bench printf */
|
||||
#ifdef XPRINTF
|
||||
#undef printf
|
||||
#define printf XPRINTF
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/memory.h>
|
||||
|
@@ -274,7 +274,7 @@ enum {
|
||||
drbgInitV
|
||||
};
|
||||
|
||||
|
||||
/* NOTE: if DRBG struct is changed please update random.h drbg_data size */
|
||||
typedef struct DRBG {
|
||||
word32 reseedCtr;
|
||||
word32 lastBlock;
|
||||
@@ -753,9 +753,18 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
|
||||
byte seed[MAX_SEED_SZ];
|
||||
#endif
|
||||
|
||||
#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
|
||||
rng->drbg =
|
||||
(struct DRBG*)XMALLOC(sizeof(DRBG), rng->heap,
|
||||
DYNAMIC_TYPE_RNG);
|
||||
#else
|
||||
/* compile-time validation of drbg_data size */
|
||||
typedef char drbg_data_test[sizeof(rng->drbg_data) >=
|
||||
sizeof(struct DRBG) ? 1 : -1];
|
||||
(void)sizeof(drbg_data_test);
|
||||
rng->drbg = (struct DRBG*)rng->drbg_data;
|
||||
#endif
|
||||
|
||||
if (rng->drbg == NULL) {
|
||||
ret = MEMORY_E;
|
||||
}
|
||||
@@ -775,7 +784,9 @@ static int _InitRng(WC_RNG* rng, byte* nonce, word32 nonceSz,
|
||||
ret = Hash_DRBG_Generate(rng->drbg, NULL, 0);
|
||||
|
||||
if (ret != DRBG_SUCCESS) {
|
||||
#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
|
||||
XFREE(rng->drbg, rng->heap, DYNAMIC_TYPE_RNG);
|
||||
#endif
|
||||
rng->drbg = NULL;
|
||||
}
|
||||
}
|
||||
@@ -953,7 +964,9 @@ int wc_FreeRng(WC_RNG* rng)
|
||||
if (Hash_DRBG_Uninstantiate(rng->drbg) != DRBG_SUCCESS)
|
||||
ret = RNG_FAILURE_E;
|
||||
|
||||
#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
|
||||
XFREE(rng->drbg, rng->heap, DYNAMIC_TYPE_RNG);
|
||||
#endif
|
||||
rng->drbg = NULL;
|
||||
}
|
||||
|
||||
|
@@ -1293,6 +1293,14 @@ static int RsaUnPad_PSS(byte *pkcsBlock, unsigned int pkcsBlockLen,
|
||||
int ret;
|
||||
byte* tmp;
|
||||
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);
|
||||
if (hLen < 0)
|
||||
@@ -1316,9 +1324,11 @@ static int RsaUnPad_PSS(byte *pkcsBlock, unsigned int pkcsBlockLen,
|
||||
return BAD_PADDING_E;
|
||||
}
|
||||
|
||||
#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
|
||||
tmp = (byte*)XMALLOC(pkcsBlockLen, heap, DYNAMIC_TYPE_RSA_BUFFER);
|
||||
if (tmp == NULL)
|
||||
return MEMORY_E;
|
||||
#endif
|
||||
|
||||
if ((ret = RsaMGF(mgf, pkcsBlock + pkcsBlockLen - 1 - hLen, hLen,
|
||||
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++)
|
||||
pkcsBlock[i] ^= tmp[i];
|
||||
|
||||
#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
|
||||
XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
|
||||
#endif
|
||||
|
||||
*output = pkcsBlock + pkcsBlockLen - (hLen + saltLen + 1);
|
||||
return saltLen + hLen;
|
||||
|
@@ -105,8 +105,15 @@
|
||||
#ifdef XMALLOC_USER
|
||||
#include <stdlib.h> /* we're using malloc / free direct here */
|
||||
#endif
|
||||
#ifndef STRING_USER
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
/* enable way for customer to override test/bench printf */
|
||||
#ifdef XPRINTF
|
||||
#undef printf
|
||||
#define printf XPRINTF
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/memory.h>
|
||||
@@ -443,7 +450,7 @@ static void myFipsCb(int ok, int err, const char* hash)
|
||||
|
||||
#ifdef WOLFSSL_STATIC_MEMORY
|
||||
#ifdef BENCH_EMBEDDED
|
||||
static byte gTestMemory[10000];
|
||||
static byte gTestMemory[14000];
|
||||
#elif defined(WOLFSSL_CERT_EXT)
|
||||
static byte gTestMemory[140000];
|
||||
#elif defined(USE_FAST_MATH) && !defined(ALT_ECC_SIZE)
|
||||
@@ -23469,11 +23476,13 @@ int mutex_test(void)
|
||||
#ifdef WOLFSSL_PTHREADS
|
||||
wolfSSL_Mutex m;
|
||||
#endif
|
||||
#ifndef WOLFSSL_NO_MALLOC
|
||||
wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
|
||||
if (mm == NULL)
|
||||
return -9900;
|
||||
wc_FreeMutex(mm);
|
||||
XFREE(mm, NULL, DYNAMIC_TYPE_MUTEX);
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSL_PTHREADS
|
||||
if (wc_InitMutex(&m) != 0)
|
||||
@@ -23507,6 +23516,7 @@ static void *my_Malloc_cb(size_t size)
|
||||
return malloc(size);
|
||||
#else
|
||||
WOLFSSL_MSG("No malloc available");
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
static void my_Free_cb(void *ptr)
|
||||
@@ -23525,6 +23535,7 @@ static void *my_Realloc_cb(void *ptr, size_t size)
|
||||
return realloc(ptr, size);
|
||||
#else
|
||||
WOLFSSL_MSG("No realloc available");
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -23540,6 +23551,7 @@ int memcb_test(void)
|
||||
if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
|
||||
return -10000;
|
||||
|
||||
#ifndef WOLFSSL_NO_MALLOC
|
||||
/* test realloc */
|
||||
b = (byte*)XREALLOC(b, 1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
if (b == NULL) {
|
||||
@@ -23565,6 +23577,7 @@ int memcb_test(void)
|
||||
if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
|
||||
#endif
|
||||
ret = -10006;
|
||||
#endif /* !WOLFSSL_NO_MALLOC */
|
||||
|
||||
exit_memcb:
|
||||
|
||||
|
@@ -29,7 +29,9 @@
|
||||
#ifndef WOLFSSL_MEMORY_H
|
||||
#define WOLFSSL_MEMORY_H
|
||||
|
||||
#ifndef STRING_USER
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@@ -156,6 +156,20 @@ struct WC_RNG {
|
||||
#ifdef HAVE_HASHDRBG
|
||||
/* Hash-based Deterministic Random Bit Generator */
|
||||
struct DRBG* drbg;
|
||||
#if defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_STATIC_MEMORY)
|
||||
#define DRBG_STRUCT_SZ ((sizeof(word32)*3) + (DRBG_SEED_LEN*2))
|
||||
#ifdef WOLFSSL_SMALL_STACK_CACHE
|
||||
#define DRBG_STRUCT_SZ_SHA256 (sizeof(wc_Sha256))
|
||||
#else
|
||||
#define DRBG_STRUCT_SZ_SHA256 0
|
||||
#endif
|
||||
#if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
|
||||
#define DRBG_STRUCT_SZ_ASYNC (sizeof(void*) + sizeof(int))
|
||||
#else
|
||||
#define DRBG_STRUCT_SZ_ASYNC 0
|
||||
#endif
|
||||
byte drbg_data[DRBG_STRUCT_SZ + DRBG_STRUCT_SZ_SHA256 + DRBG_STRUCT_SZ_ASYNC];
|
||||
#endif
|
||||
byte status;
|
||||
#endif
|
||||
#ifdef WOLFSSL_ASYNC_CRYPT
|
||||
|
@@ -271,11 +271,29 @@
|
||||
#define XREALLOC(p, n, h, t) m2mb_os_realloc((p), (n))
|
||||
|
||||
#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 */
|
||||
#include <stdlib.h>
|
||||
#define XMALLOC(s, h, t) ((void)h, (void)t, malloc((s)))
|
||||
#define XFREE(p, h, t) {void* xp = (p); if((xp)) free((xp));}
|
||||
#define XREALLOC(p, n, h, t) realloc((p), (n))
|
||||
#endif
|
||||
#elif !defined(MICRIUM_MALLOC) && !defined(EBSNET) \
|
||||
&& !defined(WOLFSSL_SAFERTOS) && !defined(FREESCALE_MQX) \
|
||||
&& !defined(FREESCALE_KSDK_MQX) && !defined(FREESCALE_FREE_RTOS) \
|
||||
@@ -412,6 +430,7 @@
|
||||
/* snprintf is used in asn.c for GetTimeString, PKCS7 test, and when
|
||||
debugging is turned on */
|
||||
#ifndef USE_WINDOWS_API
|
||||
#ifndef XSNPRINTF
|
||||
#if defined(NO_FILESYSTEM) && (defined(OPENSSL_EXTRA) || \
|
||||
defined(HAVE_PKCS7)) && !defined(NO_STDIO_FILESYSTEM)
|
||||
/* case where stdio is not included else where but is needed
|
||||
@@ -419,6 +438,7 @@
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
#define XSNPRINTF snprintf
|
||||
#endif
|
||||
#else
|
||||
#ifdef _MSC_VER
|
||||
#if (_MSC_VER >= 1900)
|
||||
|
Reference in New Issue
Block a user