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 */
|
#include <stdlib.h> /* we're using malloc / free direct here */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <string.h>
|
#ifndef STRING_USER
|
||||||
#include <stdio.h>
|
#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
|
#endif
|
||||||
|
|
||||||
#include <wolfssl/wolfcrypt/memory.h>
|
#include <wolfssl/wolfcrypt/memory.h>
|
||||||
|
@@ -274,7 +274,7 @@ enum {
|
|||||||
drbgInitV
|
drbgInitV
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* NOTE: if DRBG struct is changed please update random.h drbg_data size */
|
||||||
typedef struct DRBG {
|
typedef struct DRBG {
|
||||||
word32 reseedCtr;
|
word32 reseedCtr;
|
||||||
word32 lastBlock;
|
word32 lastBlock;
|
||||||
@@ -753,9 +753,18 @@ 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
|
||||||
|
/* 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) {
|
if (rng->drbg == NULL) {
|
||||||
ret = MEMORY_E;
|
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);
|
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 +964,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;
|
||||||
|
@@ -105,8 +105,15 @@
|
|||||||
#ifdef XMALLOC_USER
|
#ifdef XMALLOC_USER
|
||||||
#include <stdlib.h> /* we're using malloc / free direct here */
|
#include <stdlib.h> /* we're using malloc / free direct here */
|
||||||
#endif
|
#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
|
#endif
|
||||||
|
|
||||||
#include <wolfssl/wolfcrypt/memory.h>
|
#include <wolfssl/wolfcrypt/memory.h>
|
||||||
@@ -443,7 +450,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)
|
||||||
@@ -23469,11 +23476,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)
|
||||||
@@ -23507,6 +23516,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)
|
||||||
@@ -23525,6 +23535,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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23540,6 +23551,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) {
|
||||||
@@ -23565,6 +23577,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:
|
||||||
|
|
||||||
|
@@ -29,7 +29,9 @@
|
|||||||
#ifndef WOLFSSL_MEMORY_H
|
#ifndef WOLFSSL_MEMORY_H
|
||||||
#define WOLFSSL_MEMORY_H
|
#define WOLFSSL_MEMORY_H
|
||||||
|
|
||||||
|
#ifndef STRING_USER
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#endif
|
||||||
#include <wolfssl/wolfcrypt/types.h>
|
#include <wolfssl/wolfcrypt/types.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
@@ -156,6 +156,20 @@ 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)*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;
|
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) \
|
||||||
@@ -412,6 +430,7 @@
|
|||||||
/* snprintf is used in asn.c for GetTimeString, PKCS7 test, and when
|
/* snprintf is used in asn.c for GetTimeString, PKCS7 test, and when
|
||||||
debugging is turned on */
|
debugging is turned on */
|
||||||
#ifndef USE_WINDOWS_API
|
#ifndef USE_WINDOWS_API
|
||||||
|
#ifndef XSNPRINTF
|
||||||
#if defined(NO_FILESYSTEM) && (defined(OPENSSL_EXTRA) || \
|
#if defined(NO_FILESYSTEM) && (defined(OPENSSL_EXTRA) || \
|
||||||
defined(HAVE_PKCS7)) && !defined(NO_STDIO_FILESYSTEM)
|
defined(HAVE_PKCS7)) && !defined(NO_STDIO_FILESYSTEM)
|
||||||
/* case where stdio is not included else where but is needed
|
/* case where stdio is not included else where but is needed
|
||||||
@@ -419,6 +438,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#endif
|
#endif
|
||||||
#define XSNPRINTF snprintf
|
#define XSNPRINTF snprintf
|
||||||
|
#endif
|
||||||
#else
|
#else
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#if (_MSC_VER >= 1900)
|
#if (_MSC_VER >= 1900)
|
||||||
|
Reference in New Issue
Block a user