diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index ef2cc2d5a..9781b2d1a 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -113,8 +113,16 @@ #include /* we're using malloc / free direct here */ #endif - #include - #include + #ifndef STRING_USER + #include + #include + #endif + + /* enable way for customer to override test/bench printf */ + #ifdef XPRINTF + #undef printf + #define printf XPRINTF + #endif #endif #include diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index f6751330e..7ab7f92ec 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -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; } diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c index 5441c576d..e7704286e 100644 --- a/wolfcrypt/src/rsa.c +++ b/wolfcrypt/src/rsa.c @@ -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; diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index a16003806..9e29cf59e 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -105,8 +105,15 @@ #ifdef XMALLOC_USER #include /* we're using malloc / free direct here */ #endif + #ifndef STRING_USER + #include + #endif - #include + /* enable way for customer to override test/bench printf */ + #ifdef XPRINTF + #undef printf + #define printf XPRINTF + #endif #endif #include @@ -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: diff --git a/wolfssl/wolfcrypt/memory.h b/wolfssl/wolfcrypt/memory.h index f9295b392..a2ed27b91 100644 --- a/wolfssl/wolfcrypt/memory.h +++ b/wolfssl/wolfcrypt/memory.h @@ -29,7 +29,9 @@ #ifndef WOLFSSL_MEMORY_H #define WOLFSSL_MEMORY_H +#ifndef STRING_USER #include +#endif #include #ifdef __cplusplus diff --git a/wolfssl/wolfcrypt/random.h b/wolfssl/wolfcrypt/random.h index 761e764b4..62eb25979 100644 --- a/wolfssl/wolfcrypt/random.h +++ b/wolfssl/wolfcrypt/random.h @@ -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 diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 280be27c2..cc54ada78 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -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 + 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 #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 #endif #define XSNPRINTF snprintf + #endif #else #ifdef _MSC_VER #if (_MSC_VER >= 1900)