2015-01-04 23:26:26 -07:00
|
|
|
/* memory.c
|
2014-12-19 09:56:51 -07:00
|
|
|
*
|
2022-12-30 17:12:11 -07:00
|
|
|
* Copyright (C) 2006-2023 wolfSSL Inc.
|
2014-12-19 09:56:51 -07:00
|
|
|
*
|
2016-03-17 16:02:13 -06:00
|
|
|
* This file is part of wolfSSL.
|
2014-12-19 09:56:51 -07:00
|
|
|
*
|
2014-12-29 10:27:03 -07:00
|
|
|
* wolfSSL is free software; you can redistribute it and/or modify
|
2014-12-19 09:56:51 -07:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2014-12-29 10:27:03 -07:00
|
|
|
* wolfSSL is distributed in the hope that it will be useful,
|
2014-12-19 09:56:51 -07:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
2016-03-17 16:02:13 -06:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
2014-12-19 09:56:51 -07:00
|
|
|
*/
|
|
|
|
|
|
2016-03-17 16:02:13 -06:00
|
|
|
|
2014-12-19 09:56:51 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-05-31 15:17:01 -05:00
|
|
|
#ifdef WOLFSSL_LINUXKM
|
|
|
|
|
/* inhibit "#undef current" in linuxkm_wc_port.h, included from wc_port.h,
|
|
|
|
|
* because needed in linuxkm_memory.c, included below.
|
|
|
|
|
*/
|
|
|
|
|
#define WOLFSSL_NEED_LINUX_CURRENT
|
|
|
|
|
#endif
|
|
|
|
|
|
refactor AESNI implementations and *VECTOR_REGISTERS* macros to allow dynamic as-needed fallback to pure C, via WC_AES_C_DYNAMIC_FALLBACK.
wolfssl/wolfcrypt/aes.h: add key_C_fallback[] to struct Aes, and remove comment that "AESNI needs key first, rounds 2nd, not sure why yet" now that AES_128_Key_Expansion_AESNI no longer writes rounds after the expanded key.
wolfcrypt/src/aes.c:
* add _AESNI or _aesni suffixes/infixes to AESNI implementations that were missing them: AES_CBC_encrypt(), AES_CBC_decrypt_by*(), AES_ECB_encrypt(), AES_*_Key_Expansion(), AES_set_encrypt_key(), AES_set_decrypt_key(), AES_GCM_encrypt(), AES_GCM_decrypt(), AES_XTS_encrypt(), and AES_XTS_decrypt().
* move key size check from to start of wc_AesSetKeyLocal().
* refactor pure-C AES setkey and cipher implementations to use aes->key_C_fallback when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor wc_AesSetKeyLocal() to set up both AESNI and pure-C expanded keys when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor all (haveAESNI && aes->use_aesni) conditions to just (aes->use_aesni).
* add macros VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, which do nothing but push a brace level when !defined(WC_AES_C_DYNAMIC_FALLBACK), but when defined(WC_AES_C_DYNAMIC_FALLBACK), they call SAVE_VECTOR_REGISTERS2() and on failure, temporarily clear aes->use_aesni and restore at _POP().
* refactor all invocations of SAVE_VECTOR_REGISTERS() and RESTORE_VECTOR_REGISTERS() to VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, except in wc_AesSetKeyLocal(), wc_AesXtsEncrypt(), and wc_AesXtsDecrypt(), which are refactored to use SAVE_VECTOR_REGISTERS2(), with graceful failure concealment if defined(WC_AES_C_DYNAMIC_FALLBACK).
* orthogonalize cleanup code in wc_AesCbcEncrypt(), wc_AesCcmEncrypt() and wc_AesCcmDecrypt().
* streamline fallthrough software definitions of wc_AesEncryptDirect() and wc_AesDecryptDirect(), and remove special-casing for defined(WOLFSSL_LINUXKM)&&defined(WOLFSSL_AESNI).
wolfcrypt/src/aes_asm.{S,asm}:
* remove errant "movl $10, 240(%rsi)" from AES_128_Key_Expansion_AESNI.
* add _AESNI suffixes/infixes to implementations that needed them.
wolfcrypt/src/{aes_gcm_asm.{S,asm},aes_xts_asm.S}: regenerate from revisions in scripts#357 -- adds _aesni suffixes to implementations that were missing them.
wolfssl/wolfcrypt/types.h: remove DEBUG_VECTOR_REGISTER_ACCESS macros, and add dummy fallthrough definitions for SAVE_VECTOR_REGISTERS2 and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
wolfssl/wolfcrypt/memory.h: adopt DEBUG_VECTOR_REGISTER_ACCESS code from types.h, and add definitions for WC_DEBUG_VECTOR_REGISTERS_RETVAL_INITVAL and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
linuxkm/linuxkm_wc_port.h: add arch-specific macro definitions for SAVE_VECTOR_REGISTERS2().
wolfcrypt/benchmark/benchmark.c: add missing gates around calls to RESTORE_VECTOR_REGISTERS().
configure.ac:
* cover various interdependencies in enable-all/enable-all-crypto, for better behavior in combination with --disable-aesgcm, --disable-ecc, --disable-ocsp, --disable-hmac, --disable-chacha, --disable-ed25519, and --disable-ed448.
* inhibit aesgcm_stream in enable-all/enable-all-crypto when ENABLED_LINUXKM_DEFAULTS, because it is currently incompatible with WC_AES_C_DYNAMIC_FALLBACK.
* add -DWC_AES_C_DYNAMIC_FALLBACK when ENABLED_LINUXKM_DEFAULTS.
* add 3 new interdependency checks: "ECCSI requires ECC.", "SAKKE requires ECC.", "WOLFSSH requires HMAC."
wolfcrypt/src/asn.c: tweak gating to accommodate defined(NO_RSA) && !defined(HAVE_ECC).
wolfcrypt/src/evp.c: tweak gating to accommodate defined(NO_HMAC).
wolfcrypt/src/logging.c: remove DEBUG_VECTOR_REGISTER_ACCESS code (moved to memory.c).
wolfcrypt/src/memory.c: change #include of settings.h to types.h; adopt DEBUG_VECTOR_REGISTER_ACCESS code from logging.c; add implementation of SAVE_VECTOR_REGISTERS2_fuzzer().
wolfcrypt/src/pwdbased.c: add explanatory #error scrypt requires HMAC.
wolfcrypt/test/test.c:
* add DEBUG_VECTOR_REGISTER_ACCESS clauses to aes_xts_128_test(), aesecb_test(), aesctr_test(), aes_test() CBC section, aes256_test() CBC section, and aesgcm_default_test_helper()
* remove duplicate wc_AesEcbDecrypt() in aesecb_test().
* add gating for pbkdf2_test().
* fix cleanup code in dsa_test().
* fix gating in pkcs7authenveloped_run_vectors() to accommodate !defined(HAVE_AESGCM).
* fix gating in cryptocb_test() to accommodate defined(NO_HMAC).
wolfssl/wolfcrypt/cryptocb.h: remove gates around "pk" sub-struct of struct wc_CryptoInfo -- wc_CryptoInfo.pk.type (an int) is used unconditionally when --enable-debug, and is used with DH.
wolfssl/wolfcrypt/error-crypt.h: fix whitespace.
2023-11-17 01:15:28 -06:00
|
|
|
#include <wolfssl/wolfcrypt/types.h>
|
2014-12-19 09:56:51 -07:00
|
|
|
|
2018-07-31 09:03:40 -07:00
|
|
|
/*
|
|
|
|
|
Possible memory options:
|
|
|
|
|
* NO_WOLFSSL_MEMORY: Disables wolf memory callback support. When not defined settings.h defines USE_WOLFSSL_MEMORY.
|
|
|
|
|
* WOLFSSL_STATIC_MEMORY: Turns on the use of static memory buffers and functions.
|
|
|
|
|
This allows for using static memory instead of dynamic.
|
|
|
|
|
* WOLFSSL_STATIC_ALIGN: Define defaults to 16 to indicate static memory alignment.
|
|
|
|
|
* HAVE_IO_POOL: Enables use of static thread safe memory pool for input/output buffers.
|
|
|
|
|
* XMALLOC_OVERRIDE: Allows override of the XMALLOC, XFREE and XREALLOC macros.
|
|
|
|
|
* XMALLOC_USER: Allows custom XMALLOC, XFREE and XREALLOC functions to be defined.
|
|
|
|
|
* WOLFSSL_NO_MALLOC: Disables the fall-back case to use STDIO malloc/free when no callbacks are set.
|
|
|
|
|
* WOLFSSL_TRACK_MEMORY: Enables memory tracking for total stats and list of allocated memory.
|
|
|
|
|
* WOLFSSL_DEBUG_MEMORY: Enables extra function and line number args for memory callbacks.
|
|
|
|
|
* WOLFSSL_DEBUG_MEMORY_PRINT: Enables printing of each malloc/free.
|
|
|
|
|
* WOLFSSL_MALLOC_CHECK: Reports malloc or alignment failure using WOLFSSL_STATIC_ALIGN
|
|
|
|
|
* WOLFSSL_FORCE_MALLOC_FAIL_TEST: Used for internal testing to induce random malloc failures.
|
|
|
|
|
* WOLFSSL_HEAP_TEST: Used for internal testing of heap hint
|
2023-05-26 15:49:14 +10:00
|
|
|
* WOLFSSL_MEM_FAIL_COUNT: Fail memory allocation at a count from
|
|
|
|
|
* environment variable: MEM_FAIL_CNT.
|
2018-07-31 09:03:40 -07:00
|
|
|
*/
|
|
|
|
|
|
2019-02-07 16:11:17 +10:00
|
|
|
#ifdef WOLFSSL_ZEPHYR
|
|
|
|
|
#undef realloc
|
|
|
|
|
void *z_realloc(void *ptr, size_t size)
|
|
|
|
|
{
|
|
|
|
|
if (ptr == NULL)
|
|
|
|
|
ptr = malloc(size);
|
|
|
|
|
else
|
|
|
|
|
ptr = realloc(ptr, size);
|
|
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
#define realloc z_realloc
|
|
|
|
|
#endif
|
2018-07-31 09:03:40 -07:00
|
|
|
|
2014-12-29 10:27:03 -07:00
|
|
|
#ifdef USE_WOLFSSL_MEMORY
|
2014-12-19 09:56:51 -07:00
|
|
|
|
|
|
|
|
#include <wolfssl/wolfcrypt/memory.h>
|
|
|
|
|
#include <wolfssl/wolfcrypt/error-crypt.h>
|
2016-03-23 10:21:26 -06:00
|
|
|
#include <wolfssl/wolfcrypt/logging.h>
|
2014-12-19 09:56:51 -07:00
|
|
|
|
2018-09-07 17:02:34 -07:00
|
|
|
#if defined(WOLFSSL_DEBUG_MEMORY) && defined(WOLFSSL_DEBUG_MEMORY_PRINT)
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-07-27 10:16:14 -07:00
|
|
|
#ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
|
|
|
|
|
static int gMemFailCountSeed;
|
|
|
|
|
static int gMemFailCount;
|
|
|
|
|
void wolfSSL_SetMemFailCount(int memFailCount)
|
|
|
|
|
{
|
|
|
|
|
if (gMemFailCountSeed == 0) {
|
|
|
|
|
gMemFailCountSeed = memFailCount;
|
|
|
|
|
gMemFailCount = memFailCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2018-08-21 09:03:11 +10:00
|
|
|
#if defined(WOLFSSL_MALLOC_CHECK) || defined(WOLFSSL_TRACK_MEMORY_FULL) || \
|
|
|
|
|
defined(WOLFSSL_MEMORY_LOG)
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#endif
|
2018-07-27 10:16:14 -07:00
|
|
|
|
2016-05-04 10:04:38 -07:00
|
|
|
|
2014-12-19 09:56:51 -07:00
|
|
|
/* Set these to default values initially. */
|
2018-07-23 16:00:03 -07:00
|
|
|
static wolfSSL_Malloc_cb malloc_function = NULL;
|
|
|
|
|
static wolfSSL_Free_cb free_function = NULL;
|
|
|
|
|
static wolfSSL_Realloc_cb realloc_function = NULL;
|
2014-12-19 09:56:51 -07:00
|
|
|
|
2014-12-19 10:47:38 -07:00
|
|
|
int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf,
|
|
|
|
|
wolfSSL_Free_cb ff,
|
|
|
|
|
wolfSSL_Realloc_cb rf)
|
2014-12-19 09:56:51 -07:00
|
|
|
{
|
2018-07-31 09:24:44 -07:00
|
|
|
malloc_function = mf;
|
|
|
|
|
free_function = ff;
|
|
|
|
|
realloc_function = rf;
|
2018-07-23 16:00:03 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
Intel QuickAssist (QAT) support and async enhancements/fixes:
* Adds ./configure "--with-intelqa=../QAT1.6”, port files, memory management and README.md (see wolfcrypt/src/port/intel/).
* Added Intel QAT support for RSA public/private (CRT/non-CRT), AES CBC/GCM, ECDH/ECDSA, DH, DES3, SHA, SHA224, SHA256, SHA384, SHA512, MD5 and HMAC.
* wolfSSL async enabled all client and server: PKI, Encrypt/Decrypt, Hashing/HMAC and Certificate Sign/Verify.
* wolfSSL async support in functions: Encrypt, Decrypt, VerifyMAC, BuildMessage, ConfirmSignature, DoCertificate, ParseCertRelative, and MakeSignature.
* wolfCrypt test and benchmark async support added for all HW acceleration.
* wolfCrypt benchmark multi-threading support.
* Added QuickAssist memory overrides for XMALLOC, XFREE and XREALLOC. XREALLOC determines if existing pointer needs reallocated for NUMA.
* Refactor to make sure “heap” is available for async dev init.
* Added async support for all examples for connect, accept, read and write.
* Added new WC_BIGINT (in wolfmath.c) for async hardware support.
* Added async simulator tests for DES3 CBC, AES CBC/GCM.
* Added QAT standalone build for unit testing.
* Added int return code to SHA and MD5 functions.
* Refactor of the async stack variable handling, so async operations have generic args buffer area and cleanup function pointer.
* Combined duplicate code for async push/pop handling.
* Refactor internal.c to add AllocKey / FreeKey.
* Refactor of hash init/free in TLS to use InitHashes and FreeHashes.
* Refactor of the async event->context to use WOLF_EVENT_TYPE_ASYNC_WOLFSSL for WOLFSSL* and WOLF_EVENT_TYPE_ASYNC_WOLFCRYPT for WC_ASYNC_DEV*.
* Suppress error message for WC_PENDING_E.
* Implemented "wolfSSL_EVP_MD_CTX_init" to do memset.
* Cleanup of the openssl compat CTX sizes when async is enabled.
* Cleanup of AES, DES3, DH, SHA, MD5, DES3, DH, HMAC, MD5 for consistency and readability.
* Cleanup of the OPAQUE_LEN.
* Cleanup to use ENCRYPT_LEN instead of sizeof(ssl->arrays.preMasterSecret).
* Changed ssl->arrays.preMasterSecret to use XMALLOC (accelerates HW operations)
* Reduce verbosity with debug enabled for "GetMyVersion", "wolfSSL Using RSA OAEP padding" and "wolfSSL Using RSA PKCSV15 padding".
* Updated RSA un-padding error message so its different than one above it for better debugging.
* Added QAT async enables for each algorithm.
* Refactor of the async init to use _ex.
* Added WC_ASYNC_THRESH_NONE to allow bypass of the async thresholds for testing.
* Reformatted the benchmark results:
PKI: "RSA 2048 private HW 18522 ops took 1.003 sec, avg 0.054 ms, 18467.763 ops/sec"
Crypto/Hashing: SHA-256 SW 350 megs took 1.009 seconds, 346.946 MB/s Cycles per byte = 9.87
* Added min execution time for all benchmarks.
* Moved wc_*GetHash and wc_*RestorePos to appropriate files so use of isCopy flag is local.
* Fix for ECC sign status sometimes being invalid due to uninitialized ECC digest in benchmark.
* Added new DECLARE_VAR/FREE_VAR and DECLARE_ARRAY/FREE_ARRAY macros for helping setup test/benchmark variables to accelerate async.
* Added NO_SW_BENCH option to only run HW bench.
* Added support for PRNG to use hardware SHA256 if _wc devId provided.
* Fix to prevent curve tests from running against wrong curve sizes. Changed wc_ecc_set_curve to match on exact size.
* Added the wc_*GetHash calls to the wolfCrypt tests.
* Added async hardware start/stop to wolfSSL init/cleanup.
* Refactor to add wc_*Copy for hashing context (for async), which replaces wc_*RestorePos.
* Fixes for building with TI hashing (including: SHA224, missing new API’s and building with dummy build for non hw testing). Note: We need to add build test for this `./configure CFLAGS="-DWOLFSSL_TI_HASH -DTI_DUMMY_BUILD”`.
* Added arg checks on wc_*GetHash and wc_*Copy.
* Cleanup of the BuildMD5, BuildSHA, BuildMD5_CertVerify and BuildSHA_CertVerify functions.
* Added new ./configure --enable-asyncthreads, to allow enable/disable of the async threading support. If --enable-asynccrypt set this will be enabled by default if pthread is supported. Allows multi-threaded benchmarks with async simulator.
* Added checks for all hashing to verify valid ->buffLen.
* Fix for SHA512 scan-build warning about un-initialized “W_X”.
* Fix for valgrind un-initialized use of buffer in AllocDer (der->buffer) and BuildTlsFinished handshake_hash.
* Refactor of the benchmarking to use common function for start, check and finish of the stats.
* Fixed issue with ECC cache loading in multi-threading.
* Fix bug with AESNI not aligned code that assumes XMALLOC is 16-byte aligned.
* Added new WC_ASYNC_NO_… options to allow disabling of individual async algorithms. New defines are: WC_ASYNC_NO_CRYPT, WC_ASYNC_NO_PKI and WC_ASYNC_NO_HASH. Additionally each algorithm has a WC_ASYNC_NO_[ALGO] define.
* Added “wolfSSL_GetAllocators” API and fixed the wolfCrypt memcb_test so it restores callback pointers after test is complete (fixes issue with using custom allocators and test breaking it).
2017-04-07 15:46:32 -07:00
|
|
|
int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf,
|
|
|
|
|
wolfSSL_Free_cb* ff,
|
|
|
|
|
wolfSSL_Realloc_cb* rf)
|
|
|
|
|
{
|
|
|
|
|
if (mf) *mf = malloc_function;
|
|
|
|
|
if (ff) *ff = free_function;
|
|
|
|
|
if (rf) *rf = realloc_function;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 08:11:50 +10:00
|
|
|
#ifdef WOLFSSL_MEM_FAIL_COUNT
|
2024-02-29 02:11:32 -06:00
|
|
|
static wolfSSL_Mutex memFailMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(memFailMutex);
|
2023-06-22 08:11:50 +10:00
|
|
|
int mem_fail_allocs = 0;
|
|
|
|
|
int mem_fail_frees = 0;
|
|
|
|
|
int mem_fail_cnt = 0;
|
|
|
|
|
|
|
|
|
|
void wc_MemFailCount_Init()
|
|
|
|
|
{
|
|
|
|
|
char* cnt;
|
2024-02-29 02:11:32 -06:00
|
|
|
#ifndef WOLFSSL_MUTEX_INITIALIZER
|
2023-06-22 08:11:50 +10:00
|
|
|
wc_InitMutex(&memFailMutex);
|
2024-02-29 02:11:32 -06:00
|
|
|
#endif
|
2023-06-22 08:11:50 +10:00
|
|
|
cnt = getenv("MEM_FAIL_CNT");
|
|
|
|
|
if (cnt != NULL) {
|
|
|
|
|
fprintf(stderr, "MemFailCount At: %d\n", mem_fail_cnt);
|
|
|
|
|
mem_fail_cnt = atoi(cnt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
static int wc_MemFailCount_AllocMem(void)
|
|
|
|
|
{
|
|
|
|
|
int ret = 1;
|
|
|
|
|
|
|
|
|
|
wc_LockMutex(&memFailMutex);
|
|
|
|
|
if ((mem_fail_cnt > 0) && (mem_fail_cnt <= mem_fail_allocs + 1)) {
|
|
|
|
|
ret = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
mem_fail_allocs++;
|
|
|
|
|
}
|
|
|
|
|
wc_UnLockMutex(&memFailMutex);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
static void wc_MemFailCount_FreeMem(void)
|
|
|
|
|
{
|
|
|
|
|
wc_LockMutex(&memFailMutex);
|
|
|
|
|
mem_fail_frees++;
|
|
|
|
|
wc_UnLockMutex(&memFailMutex);
|
|
|
|
|
}
|
|
|
|
|
void wc_MemFailCount_Free()
|
|
|
|
|
{
|
2024-02-29 02:11:32 -06:00
|
|
|
#ifndef WOLFSSL_MUTEX_INITIALIZER
|
2023-06-22 08:11:50 +10:00
|
|
|
wc_FreeMutex(&memFailMutex);
|
2024-02-29 02:11:32 -06:00
|
|
|
#endif
|
2023-06-22 08:11:50 +10:00
|
|
|
fprintf(stderr, "MemFailCount Total: %d\n", mem_fail_allocs);
|
|
|
|
|
fprintf(stderr, "MemFailCount Frees: %d\n", mem_fail_frees);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
#ifndef WOLFSSL_STATIC_MEMORY
|
2022-06-13 16:21:41 +10:00
|
|
|
#ifdef WOLFSSL_CHECK_MEM_ZERO
|
|
|
|
|
|
|
|
|
|
#ifndef WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN
|
|
|
|
|
/* Number of entries in table of addresses to check. */
|
|
|
|
|
#define WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN 256
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Alignment to maintain when adding length to allocated pointer.
|
|
|
|
|
* Intel x64 wants to use aligned loads of XMM registers.
|
|
|
|
|
*/
|
|
|
|
|
#define MEM_ALIGN 16
|
|
|
|
|
|
|
|
|
|
/* An address that is meant to be all zeros for its length. */
|
|
|
|
|
typedef struct MemZero {
|
|
|
|
|
/* Name of address to check. */
|
|
|
|
|
const char* name;
|
|
|
|
|
/* Address to check. */
|
|
|
|
|
const void* addr;
|
|
|
|
|
/* Length of data that must be zero. */
|
|
|
|
|
size_t len;
|
|
|
|
|
} MemZero;
|
|
|
|
|
|
|
|
|
|
/* List of addresses to check. */
|
|
|
|
|
static MemZero memZero[WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN];
|
|
|
|
|
/* Next index to place address at.
|
|
|
|
|
* -1 indicates uninitialized.
|
|
|
|
|
* If nextIdx is equal to WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN then all entries
|
|
|
|
|
* have been used.
|
|
|
|
|
*/
|
|
|
|
|
static int nextIdx = -1;
|
|
|
|
|
/* Mutex to protect modifying list of addresses to check. */
|
2024-02-29 02:11:32 -06:00
|
|
|
static wolfSSL_Mutex zeroMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(zeroMutex);
|
2022-06-13 16:21:41 +10:00
|
|
|
|
|
|
|
|
/* Initialize the table of addresses and the mutex.
|
|
|
|
|
*/
|
|
|
|
|
void wc_MemZero_Init()
|
|
|
|
|
{
|
|
|
|
|
/* Clear the table to more easily see what is valid. */
|
|
|
|
|
XMEMSET(memZero, 0, sizeof(memZero));
|
|
|
|
|
/* Initialize mutex. */
|
2024-02-29 02:11:32 -06:00
|
|
|
#ifndef WOLFSSL_MUTEX_INITIALIZER
|
2022-06-13 16:21:41 +10:00
|
|
|
wc_InitMutex(&zeroMutex);
|
2024-02-29 02:11:32 -06:00
|
|
|
#endif
|
2022-06-13 16:21:41 +10:00
|
|
|
/* Next index is first entry. */
|
|
|
|
|
nextIdx = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Free the mutex and check we have not any uncheck addresses.
|
|
|
|
|
*/
|
|
|
|
|
void wc_MemZero_Free()
|
|
|
|
|
{
|
|
|
|
|
/* Free mutex. */
|
2024-02-29 02:11:32 -06:00
|
|
|
#ifndef WOLFSSL_MUTEX_INITIALIZER
|
2022-06-13 16:21:41 +10:00
|
|
|
wc_FreeMutex(&zeroMutex);
|
2024-02-29 02:11:32 -06:00
|
|
|
#endif
|
2022-06-13 16:21:41 +10:00
|
|
|
/* Make sure we checked all addresses. */
|
|
|
|
|
if (nextIdx > 0) {
|
|
|
|
|
int i;
|
|
|
|
|
fprintf(stderr, "[MEM_ZERO] Unseen: %d\n", nextIdx);
|
|
|
|
|
for (i = 0; i < nextIdx; i++) {
|
|
|
|
|
fprintf(stderr, " %s - %p:%ld\n", memZero[i].name, memZero[i].addr,
|
|
|
|
|
memZero[i].len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Uninitialized value in next index. */
|
|
|
|
|
nextIdx = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add an address to check.
|
|
|
|
|
*
|
|
|
|
|
* @param [in] name Name of address to check.
|
|
|
|
|
* @param [in] addr Address that needs to be checked.
|
|
|
|
|
* @param [in] len Length of data that must be zero.
|
|
|
|
|
*/
|
|
|
|
|
void wc_MemZero_Add(const char* name, const void* addr, size_t len)
|
|
|
|
|
{
|
|
|
|
|
/* Initialize if not done. */
|
|
|
|
|
if (nextIdx == -1) {
|
|
|
|
|
wc_MemZero_Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add an entry to the table while locked. */
|
|
|
|
|
wc_LockMutex(&zeroMutex);
|
|
|
|
|
if (nextIdx < WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN) {
|
|
|
|
|
/* Fill in the next entry and update next index. */
|
|
|
|
|
memZero[nextIdx].name = name;
|
|
|
|
|
memZero[nextIdx].addr = addr;
|
|
|
|
|
memZero[nextIdx].len = len;
|
|
|
|
|
nextIdx++;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* Abort when too many entries. */
|
|
|
|
|
fprintf(stderr, "\n[MEM_ZERO] Too many addresses to check\n");
|
|
|
|
|
fprintf(stderr, "[MEM_ZERO] WOLFSSL_MEM_CHECK_ZERO_CACHE_LEN\n");
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
wc_UnLockMutex(&zeroMutex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check the memory in the range of the address for memory that must be zero.
|
|
|
|
|
*
|
|
|
|
|
* @param [in] addr Start address of memory that is to be checked.
|
|
|
|
|
* @param [in] len Length of data associated with address.
|
|
|
|
|
*/
|
|
|
|
|
void wc_MemZero_Check(void* addr, size_t len)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
size_t j;
|
|
|
|
|
|
|
|
|
|
wc_LockMutex(&zeroMutex);
|
|
|
|
|
/* Look at each address for overlap with address passes in. */
|
|
|
|
|
for (i = 0; i < nextIdx; i++) {
|
|
|
|
|
if ((memZero[i].addr < addr) ||
|
|
|
|
|
((size_t)memZero[i].addr >= (size_t)addr + len)) {
|
|
|
|
|
/* Check address not part of memory to check. */
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Address is in range of memory being freed - check each byte zero. */
|
|
|
|
|
for (j = 0; j < memZero[i].len; j++) {
|
|
|
|
|
if (((unsigned char*)memZero[i].addr)[j] != 0) {
|
|
|
|
|
/* Byte not zero - abort! */
|
|
|
|
|
fprintf(stderr, "\n[MEM_ZERO] %s:%p + %ld is not zero\n",
|
|
|
|
|
memZero[i].name, memZero[i].addr, j);
|
|
|
|
|
fprintf(stderr, "[MEM_ZERO] Checking %p:%ld\n", addr, len);
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Update next index to write to. */
|
|
|
|
|
nextIdx--;
|
|
|
|
|
if (nextIdx > 0) {
|
|
|
|
|
/* Remove entry. */
|
|
|
|
|
XMEMCPY(memZero + i, memZero + i + 1,
|
|
|
|
|
sizeof(MemZero) * (nextIdx - i));
|
|
|
|
|
/* Clear out top to make it easier to see what is to be checked. */
|
|
|
|
|
XMEMSET(&memZero[nextIdx], 0, sizeof(MemZero));
|
|
|
|
|
}
|
|
|
|
|
/* Need to check this index again with new data. */
|
|
|
|
|
i--;
|
|
|
|
|
}
|
|
|
|
|
wc_UnLockMutex(&zeroMutex);
|
|
|
|
|
}
|
|
|
|
|
#endif /* WOLFSSL_CHECK_MEM_ZERO */
|
|
|
|
|
|
2016-05-04 10:04:38 -07:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line)
|
|
|
|
|
#else
|
2014-12-19 09:56:51 -07:00
|
|
|
void* wolfSSL_Malloc(size_t size)
|
2016-05-04 10:04:38 -07:00
|
|
|
#endif
|
2014-12-19 09:56:51 -07:00
|
|
|
{
|
|
|
|
|
void* res = 0;
|
|
|
|
|
|
2023-05-26 15:49:14 +10:00
|
|
|
#ifdef WOLFSSL_MEM_FAIL_COUNT
|
|
|
|
|
if (!wc_MemFailCount_AllocMem()) {
|
|
|
|
|
WOLFSSL_MSG("MemFailCnt: Fail malloc");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-06-13 16:21:41 +10:00
|
|
|
#ifdef WOLFSSL_CHECK_MEM_ZERO
|
|
|
|
|
/* Space for requested size. */
|
|
|
|
|
size += MEM_ALIGN;
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-05-04 10:04:38 -07:00
|
|
|
if (malloc_function) {
|
|
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
res = malloc_function(size, func, line);
|
|
|
|
|
#else
|
2014-12-19 09:56:51 -07:00
|
|
|
res = malloc_function(size);
|
2016-05-04 10:04:38 -07:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
else {
|
2018-02-15 06:54:14 -08:00
|
|
|
#ifndef WOLFSSL_NO_MALLOC
|
2021-07-01 09:40:04 -07:00
|
|
|
#ifdef WOLFSSL_TRAP_MALLOC_SZ
|
|
|
|
|
if (size > WOLFSSL_TRAP_MALLOC_SZ) {
|
|
|
|
|
WOLFSSL_MSG("Malloc too big!");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-11-05 18:16:08 -05:00
|
|
|
|
2014-12-19 09:56:51 -07:00
|
|
|
res = malloc(size);
|
2018-02-15 06:54:14 -08:00
|
|
|
#else
|
|
|
|
|
WOLFSSL_MSG("No malloc available");
|
|
|
|
|
#endif
|
2016-05-04 10:04:38 -07:00
|
|
|
}
|
2014-12-19 09:56:51 -07:00
|
|
|
|
2022-06-13 16:21:41 +10:00
|
|
|
#ifdef WOLFSSL_CHECK_MEM_ZERO
|
|
|
|
|
/* Restore size to requested value. */
|
|
|
|
|
size -= MEM_ALIGN;
|
|
|
|
|
if (res != NULL) {
|
|
|
|
|
/* Place size at front of allocated data and move pointer passed it. */
|
|
|
|
|
*(size_t*)res = size;
|
|
|
|
|
res = ((unsigned char*)res) + MEM_ALIGN;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-07-31 09:03:40 -07:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
2018-09-21 09:33:40 -07:00
|
|
|
#if defined(WOLFSSL_DEBUG_MEMORY_PRINT) && !defined(WOLFSSL_TRACK_MEMORY)
|
2022-05-12 13:07:32 -05:00
|
|
|
fprintf(stderr, "Alloc: %p -> %u at %s:%u\n", res, (word32)size, func, line);
|
2018-07-31 09:03:40 -07:00
|
|
|
#else
|
|
|
|
|
(void)func;
|
|
|
|
|
(void)line;
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef WOLFSSL_MALLOC_CHECK
|
|
|
|
|
if (res == NULL)
|
|
|
|
|
WOLFSSL_MSG("wolfSSL_malloc failed");
|
|
|
|
|
#endif
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2018-07-27 10:16:14 -07:00
|
|
|
#ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
|
|
|
|
|
if (res && --gMemFailCount == 0) {
|
2022-05-12 13:07:32 -05:00
|
|
|
fprintf(stderr, "\n---FORCED MEM FAIL TEST---\n");
|
2018-07-27 10:16:14 -07:00
|
|
|
if (free_function) {
|
|
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
free_function(res, func, line);
|
|
|
|
|
#else
|
|
|
|
|
free_function(res);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
free(res); /* clear */
|
|
|
|
|
}
|
|
|
|
|
gMemFailCount = gMemFailCountSeed; /* reset */
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-12-19 09:56:51 -07:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-04 10:04:38 -07:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
void wolfSSL_Free(void *ptr, const char* func, unsigned int line)
|
|
|
|
|
#else
|
2014-12-19 09:56:51 -07:00
|
|
|
void wolfSSL_Free(void *ptr)
|
2016-05-04 10:04:38 -07:00
|
|
|
#endif
|
2014-12-19 09:56:51 -07:00
|
|
|
{
|
2018-07-31 09:03:40 -07:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
2018-09-21 09:33:40 -07:00
|
|
|
#if defined(WOLFSSL_DEBUG_MEMORY_PRINT) && !defined(WOLFSSL_TRACK_MEMORY)
|
2022-05-12 13:07:32 -05:00
|
|
|
fprintf(stderr, "Free: %p at %s:%u\n", ptr, func, line);
|
2018-07-31 09:03:40 -07:00
|
|
|
#else
|
|
|
|
|
(void)func;
|
|
|
|
|
(void)line;
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-06-13 16:21:41 +10:00
|
|
|
#ifdef WOLFSSL_CHECK_MEM_ZERO
|
|
|
|
|
/* Move pointer back to originally allocated pointer. */
|
|
|
|
|
ptr = ((unsigned char*)ptr) - MEM_ALIGN;
|
|
|
|
|
/* Check that the pointer is zero where required. */
|
|
|
|
|
wc_MemZero_Check(((unsigned char*)ptr) + MEM_ALIGN, *(size_t*)ptr);
|
|
|
|
|
#endif
|
2023-05-26 15:49:14 +10:00
|
|
|
#ifdef WOLFSSL_MEM_FAIL_COUNT
|
|
|
|
|
wc_MemFailCount_FreeMem();
|
|
|
|
|
#endif
|
2022-06-13 16:21:41 +10:00
|
|
|
|
2016-05-04 10:04:38 -07:00
|
|
|
if (free_function) {
|
|
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
free_function(ptr, func, line);
|
|
|
|
|
#else
|
2014-12-19 09:56:51 -07:00
|
|
|
free_function(ptr);
|
2016-05-04 10:04:38 -07:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
else {
|
2018-02-15 06:54:14 -08:00
|
|
|
#ifndef WOLFSSL_NO_MALLOC
|
2014-12-19 09:56:51 -07:00
|
|
|
free(ptr);
|
2018-02-15 06:54:14 -08:00
|
|
|
#else
|
|
|
|
|
WOLFSSL_MSG("No free available");
|
|
|
|
|
#endif
|
2016-05-04 10:04:38 -07:00
|
|
|
}
|
2014-12-19 09:56:51 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-04 10:04:38 -07:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line)
|
|
|
|
|
#else
|
2014-12-19 09:56:51 -07:00
|
|
|
void* wolfSSL_Realloc(void *ptr, size_t size)
|
2016-05-04 10:04:38 -07:00
|
|
|
#endif
|
2014-12-19 09:56:51 -07:00
|
|
|
{
|
2022-06-13 16:21:41 +10:00
|
|
|
#ifdef WOLFSSL_CHECK_MEM_ZERO
|
|
|
|
|
/* Can't check data that has been freed during realloc.
|
|
|
|
|
* Manually allocated new memory, copy data and free original pointer.
|
|
|
|
|
*/
|
|
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
void* res = wolfSSL_Malloc(size, func, line);
|
|
|
|
|
#else
|
|
|
|
|
void* res = wolfSSL_Malloc(size);
|
|
|
|
|
#endif
|
|
|
|
|
if (ptr != NULL) {
|
|
|
|
|
/* Copy the minimum of old and new size. */
|
|
|
|
|
size_t copySize = *(size_t*)(((unsigned char*)ptr) - MEM_ALIGN);
|
|
|
|
|
if (size < copySize) {
|
|
|
|
|
copySize = size;
|
|
|
|
|
}
|
|
|
|
|
XMEMCPY(res, ptr, copySize);
|
|
|
|
|
/* Dispose of old pointer. */
|
|
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
wolfSSL_Free(ptr, func, line);
|
|
|
|
|
#else
|
|
|
|
|
wolfSSL_Free(ptr);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return new pointer with data copied into it. */
|
|
|
|
|
return res;
|
|
|
|
|
#else
|
2014-12-19 09:56:51 -07:00
|
|
|
void* res = 0;
|
|
|
|
|
|
2023-05-26 15:49:14 +10:00
|
|
|
#ifdef WOLFSSL_MEM_FAIL_COUNT
|
|
|
|
|
if (!wc_MemFailCount_AllocMem()) {
|
|
|
|
|
WOLFSSL_MSG("MemFailCnt: Fail realloc");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-05-04 10:04:38 -07:00
|
|
|
if (realloc_function) {
|
|
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
res = realloc_function(ptr, size, func, line);
|
|
|
|
|
#else
|
2014-12-19 09:56:51 -07:00
|
|
|
res = realloc_function(ptr, size);
|
2016-05-04 10:04:38 -07:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
else {
|
2018-02-15 07:15:30 -08:00
|
|
|
#ifndef WOLFSSL_NO_MALLOC
|
2014-12-19 09:56:51 -07:00
|
|
|
res = realloc(ptr, size);
|
2018-02-15 07:15:30 -08:00
|
|
|
#else
|
|
|
|
|
WOLFSSL_MSG("No realloc available");
|
|
|
|
|
#endif
|
2016-05-04 10:04:38 -07:00
|
|
|
}
|
2014-12-19 09:56:51 -07:00
|
|
|
|
2023-05-26 15:49:14 +10:00
|
|
|
#ifdef WOLFSSL_MEM_FAIL_COUNT
|
|
|
|
|
if (ptr != NULL) {
|
|
|
|
|
wc_MemFailCount_FreeMem();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-12-19 09:56:51 -07:00
|
|
|
return res;
|
2022-06-13 16:21:41 +10:00
|
|
|
#endif
|
2014-12-19 09:56:51 -07:00
|
|
|
}
|
2016-06-04 19:03:48 -06:00
|
|
|
#endif /* WOLFSSL_STATIC_MEMORY */
|
2014-12-19 09:56:51 -07:00
|
|
|
|
2016-03-23 10:21:26 -06:00
|
|
|
#ifdef WOLFSSL_STATIC_MEMORY
|
|
|
|
|
|
2016-06-10 14:13:27 -06:00
|
|
|
struct wc_Memory {
|
2016-03-23 10:21:26 -06:00
|
|
|
byte* buffer;
|
|
|
|
|
struct wc_Memory* next;
|
2016-06-10 14:13:27 -06:00
|
|
|
word32 sz;
|
|
|
|
|
};
|
2016-03-23 10:21:26 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/* returns amount of memory used on success. On error returns negative value
|
|
|
|
|
wc_Memory** list is the list that new buckets are prepended to
|
|
|
|
|
*/
|
2024-04-01 15:12:08 -07:00
|
|
|
static int wc_create_memory_buckets(byte* buffer, word32 bufSz,
|
2016-03-23 10:21:26 -06:00
|
|
|
word32 buckSz, word32 buckNum, wc_Memory** list) {
|
|
|
|
|
word32 i;
|
|
|
|
|
byte* pt = buffer;
|
|
|
|
|
int ret = 0;
|
2016-06-10 14:13:27 -06:00
|
|
|
word32 memSz = (word32)sizeof(wc_Memory);
|
2016-06-10 11:15:54 -06:00
|
|
|
word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
|
2016-03-23 10:21:26 -06:00
|
|
|
|
|
|
|
|
/* if not enough space available for bucket size then do not try */
|
2016-06-10 11:15:54 -06:00
|
|
|
if (buckSz + memSz + padSz > bufSz) {
|
2016-03-23 10:21:26 -06:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < buckNum; i++) {
|
2016-06-10 11:15:54 -06:00
|
|
|
if ((buckSz + memSz + padSz) <= (bufSz - ret)) {
|
2016-03-23 10:21:26 -06:00
|
|
|
/* create a new struct and set its values */
|
2016-06-10 11:15:54 -06:00
|
|
|
wc_Memory* mem = (struct wc_Memory*)(pt);
|
2016-03-23 10:21:26 -06:00
|
|
|
mem->sz = buckSz;
|
2016-06-10 11:15:54 -06:00
|
|
|
mem->buffer = (byte*)pt + padSz + memSz;
|
2016-03-23 10:21:26 -06:00
|
|
|
mem->next = NULL;
|
|
|
|
|
|
|
|
|
|
/* add the newly created struct to front of list */
|
|
|
|
|
if (*list == NULL) {
|
|
|
|
|
*list = mem;
|
|
|
|
|
} else {
|
|
|
|
|
mem->next = *list;
|
|
|
|
|
*list = mem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* advance pointer and keep track of memory used */
|
2016-06-10 11:15:54 -06:00
|
|
|
ret += buckSz + padSz + memSz;
|
|
|
|
|
pt += buckSz + padSz + memSz;
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
break; /* not enough space left for more buckets of this size */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 15:12:08 -07:00
|
|
|
static int wc_partition_static_memory(byte* buffer, word32 sz, int flag,
|
|
|
|
|
WOLFSSL_HEAP* heap)
|
2016-08-29 10:38:06 -07:00
|
|
|
{
|
2024-04-01 15:12:08 -07:00
|
|
|
word32 ava = sz;
|
|
|
|
|
byte* pt = buffer;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
word32 memSz = (word32)sizeof(wc_Memory);
|
|
|
|
|
word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
|
|
|
|
|
|
|
|
|
|
WOLFSSL_ENTER("wc_partition_static_memory");
|
|
|
|
|
|
|
|
|
|
/* align pt */
|
|
|
|
|
while ((wc_ptr_t)pt % WOLFSSL_STATIC_ALIGN && pt < (buffer + sz)) {
|
|
|
|
|
*pt = 0x00;
|
|
|
|
|
pt++;
|
|
|
|
|
ava--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
fprintf(stderr, "Allocated %d bytes for static memory @ %p\n", ava, pt);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* divide into chunks of memory and add them to available list */
|
|
|
|
|
while (ava >= (heap->sizeList[0] + padSz + memSz)) {
|
|
|
|
|
/* creating only IO buffers from memory passed in, max TLS is 16k */
|
|
|
|
|
if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
|
|
|
|
|
if ((ret = wc_create_memory_buckets(pt, ava,
|
2024-04-26 15:08:17 -07:00
|
|
|
WOLFMEM_IO_SZ, 1, &(heap->io))) < 0) {
|
2024-04-01 15:12:08 -07:00
|
|
|
WOLFSSL_LEAVE("wc_partition_static_memory", ret);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* check if no more room left for creating IO buffers */
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-08-29 10:38:06 -07:00
|
|
|
|
2024-04-01 15:12:08 -07:00
|
|
|
/* advance pointer in buffer for next buckets and keep track
|
|
|
|
|
of how much memory is left available */
|
|
|
|
|
pt += ret;
|
|
|
|
|
ava -= ret;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
int i;
|
|
|
|
|
/* start at largest and move to smaller buckets */
|
|
|
|
|
for (i = (WOLFMEM_MAX_BUCKETS - 1); i >= 0; i--) {
|
|
|
|
|
if ((heap->sizeList[i] + padSz + memSz) <= ava) {
|
|
|
|
|
if ((ret = wc_create_memory_buckets(pt, ava,
|
|
|
|
|
heap->sizeList[i], heap->distList[i],
|
|
|
|
|
&(heap->ava[i]))) < 0) {
|
|
|
|
|
WOLFSSL_LEAVE("wc_partition_static_memory", ret);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* advance pointer in buffer for next buckets and keep track
|
|
|
|
|
of how much memory is left available */
|
|
|
|
|
pt += ret;
|
|
|
|
|
ava -= ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int wc_init_memory_heap(WOLFSSL_HEAP* heap, unsigned int listSz,
|
2024-04-11 13:59:28 -07:00
|
|
|
const unsigned int* sizeList, const unsigned int* distList)
|
2024-04-01 15:12:08 -07:00
|
|
|
{
|
2016-08-29 10:38:06 -07:00
|
|
|
XMEMSET(heap, 0, sizeof(WOLFSSL_HEAP));
|
|
|
|
|
|
2024-04-01 15:12:08 -07:00
|
|
|
XMEMCPY(heap->sizeList, sizeList, listSz * sizeof(sizeList[0]));
|
|
|
|
|
XMEMCPY(heap->distList, distList, listSz * sizeof(distList[0]));
|
2016-08-29 10:38:06 -07:00
|
|
|
|
2016-09-26 17:25:01 -06:00
|
|
|
if (wc_InitMutex(&(heap->memory_mutex)) != 0) {
|
2016-08-29 10:38:06 -07:00
|
|
|
WOLFSSL_MSG("Error creating heap memory mutex");
|
|
|
|
|
return BAD_MUTEX_E;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 15:12:08 -07:00
|
|
|
int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,
|
2024-04-26 15:08:17 -07:00
|
|
|
unsigned int listSz, const unsigned int* sizeList,
|
|
|
|
|
const unsigned int* distList, unsigned char* buf,
|
2024-04-11 13:59:28 -07:00
|
|
|
unsigned int sz, int flag, int maxSz)
|
2016-08-29 10:38:06 -07:00
|
|
|
{
|
2024-04-01 11:35:45 -07:00
|
|
|
WOLFSSL_HEAP* heap = NULL;
|
|
|
|
|
WOLFSSL_HEAP_HINT* hint = NULL;
|
2016-08-29 10:38:06 -07:00
|
|
|
word32 idx = 0;
|
2024-04-01 11:35:45 -07:00
|
|
|
int ret;
|
2016-08-29 10:38:06 -07:00
|
|
|
|
2024-04-26 15:08:17 -07:00
|
|
|
WOLFSSL_ENTER("wc_LoadStaticMemory_ex");
|
|
|
|
|
|
2024-05-03 16:15:38 -07:00
|
|
|
if (pHint == NULL || buf == NULL || sizeList == NULL || distList == NULL) {
|
2016-08-29 10:38:06 -07:00
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 16:15:38 -07:00
|
|
|
/* Cap the listSz to the actual number of items allocated in the list. */
|
|
|
|
|
if (listSz > WOLFMEM_MAX_BUCKETS) {
|
|
|
|
|
WOLFSSL_MSG("Truncating the list of memory buckets");
|
|
|
|
|
listSz = WOLFMEM_MAX_BUCKETS;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-29 10:38:06 -07:00
|
|
|
if ((sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) > sz - idx) {
|
2024-04-26 15:08:17 -07:00
|
|
|
WOLFSSL_MSG("Not enough memory for partition tracking");
|
2016-08-29 10:38:06 -07:00
|
|
|
return BUFFER_E; /* not enough memory for structures */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* check if hint has already been assigned */
|
|
|
|
|
if (*pHint == NULL) {
|
|
|
|
|
heap = (WOLFSSL_HEAP*)buf;
|
|
|
|
|
idx += sizeof(WOLFSSL_HEAP);
|
|
|
|
|
hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
|
|
|
|
|
idx += sizeof(WOLFSSL_HEAP_HINT);
|
|
|
|
|
|
2024-04-26 15:08:17 -07:00
|
|
|
ret = wc_init_memory_heap(heap, listSz, sizeList, distList);
|
2016-08-29 10:38:06 -07:00
|
|
|
if (ret != 0) {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
|
|
|
|
|
hint->memory = heap;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
#ifdef WOLFSSL_HEAP_TEST
|
|
|
|
|
/* do not load in memory if test has been set */
|
|
|
|
|
if (heap == (void*)WOLFSSL_HEAP_TEST) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
hint = (WOLFSSL_HEAP_HINT*)(*pHint);
|
|
|
|
|
heap = hint->memory;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 15:12:08 -07:00
|
|
|
ret = wc_partition_static_memory(buf + idx, sz - idx, flag, heap);
|
2016-08-29 10:38:06 -07:00
|
|
|
if (ret != 1) {
|
|
|
|
|
WOLFSSL_MSG("Error partitioning memory");
|
2024-04-26 15:08:17 -07:00
|
|
|
return MEMORY_E;
|
2016-08-29 10:38:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* determine what max applies too */
|
|
|
|
|
if ((flag & WOLFMEM_IO_POOL) || (flag & WOLFMEM_IO_POOL_FIXED)) {
|
2020-12-16 16:44:22 +10:00
|
|
|
heap->maxIO = maxSz;
|
2016-08-29 10:38:06 -07:00
|
|
|
}
|
|
|
|
|
else { /* general memory used in handshakes */
|
2020-12-16 16:44:22 +10:00
|
|
|
heap->maxHa = maxSz;
|
2016-08-29 10:38:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
heap->flag |= flag;
|
|
|
|
|
*pHint = hint;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2024-04-01 15:12:08 -07:00
|
|
|
int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
|
|
|
|
|
unsigned char* buf, unsigned int sz, int flag, int maxSz)
|
2016-03-23 10:21:26 -06:00
|
|
|
{
|
2024-04-01 15:12:08 -07:00
|
|
|
word32 sizeList[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_BUCKETS };
|
|
|
|
|
word32 distList[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_DIST };
|
2024-04-26 15:08:17 -07:00
|
|
|
int ret = 0;
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2024-04-26 15:08:17 -07:00
|
|
|
WOLFSSL_ENTER("wc_LoadStaticMemory");
|
|
|
|
|
ret = wc_LoadStaticMemory_ex(pHint,
|
2024-04-01 15:12:08 -07:00
|
|
|
WOLFMEM_DEF_BUCKETS, sizeList, distList,
|
|
|
|
|
buf, sz, flag, maxSz);
|
2024-04-26 15:08:17 -07:00
|
|
|
WOLFSSL_LEAVE("wc_LoadStaticMemory", ret);
|
|
|
|
|
return ret;
|
2016-06-04 19:03:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-04-29 22:22:08 -07:00
|
|
|
void wc_UnloadStaticMemory(WOLFSSL_HEAP_HINT* heap)
|
|
|
|
|
{
|
|
|
|
|
WOLFSSL_ENTER("wc_UnloadStaticMemory");
|
|
|
|
|
if (heap != NULL && heap->memory != NULL) {
|
|
|
|
|
wc_FreeMutex(&heap->memory->memory_mutex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-07-21 12:11:15 -06:00
|
|
|
/* returns the size of management memory needed for each bucket.
|
|
|
|
|
* This is memory that is used to keep track of and align memory buckets. */
|
|
|
|
|
int wolfSSL_MemoryPaddingSz(void)
|
|
|
|
|
{
|
|
|
|
|
word32 memSz = (word32)sizeof(wc_Memory);
|
|
|
|
|
word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
|
|
|
|
|
return memSz + padSz;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Used to calculate memory size for optimum use with buckets.
|
|
|
|
|
returns the suggested size rounded down to the nearest bucket. */
|
2024-04-11 13:59:28 -07:00
|
|
|
int wolfSSL_StaticBufferSz_ex(unsigned int listSz,
|
|
|
|
|
const unsigned int *sizeList, const unsigned int *distList,
|
|
|
|
|
byte* buffer, word32 sz, int flag)
|
2016-07-21 12:11:15 -06:00
|
|
|
{
|
|
|
|
|
word32 ava = sz;
|
|
|
|
|
byte* pt = buffer;
|
|
|
|
|
word32 memSz = (word32)sizeof(wc_Memory);
|
|
|
|
|
word32 padSz = -(int)memSz & (WOLFSSL_STATIC_ALIGN - 1);
|
|
|
|
|
|
2024-04-11 13:59:28 -07:00
|
|
|
WOLFSSL_ENTER("wolfSSL_StaticBufferSz_ex");
|
2016-07-21 12:11:15 -06:00
|
|
|
|
2024-05-03 16:15:38 -07:00
|
|
|
if (buffer == NULL || sizeList == NULL || distList == NULL) {
|
2016-07-21 12:11:15 -06:00
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-03 16:15:38 -07:00
|
|
|
/* Cap the listSz to the actual number of items allocated in the list. */
|
|
|
|
|
if (listSz > WOLFMEM_MAX_BUCKETS) {
|
|
|
|
|
WOLFSSL_MSG("Truncating the list of memory buckets");
|
|
|
|
|
listSz = WOLFMEM_MAX_BUCKETS;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 12:11:15 -06:00
|
|
|
/* align pt */
|
2021-05-03 11:23:55 -07:00
|
|
|
while ((wc_ptr_t)pt % WOLFSSL_STATIC_ALIGN && pt < (buffer + sz)) {
|
2016-07-21 12:11:15 -06:00
|
|
|
pt++;
|
|
|
|
|
ava--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* creating only IO buffers from memory passed in, max TLS is 16k */
|
|
|
|
|
if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
|
2017-01-27 10:50:47 -07:00
|
|
|
if (ava < (memSz + padSz + WOLFMEM_IO_SZ)) {
|
|
|
|
|
return 0; /* not enough room for even one bucket */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ava = ava % (memSz + padSz + WOLFMEM_IO_SZ);
|
2016-07-21 12:11:15 -06:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
int i, k;
|
2017-01-27 10:50:47 -07:00
|
|
|
|
2024-04-11 13:59:28 -07:00
|
|
|
if (ava < (sizeList[0] + padSz + memSz)) {
|
2017-01-27 10:50:47 -07:00
|
|
|
return 0; /* not enough room for even one bucket */
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 13:59:28 -07:00
|
|
|
while ((ava >= (sizeList[0] + padSz + memSz)) && (ava > 0)) {
|
2016-07-21 12:11:15 -06:00
|
|
|
/* start at largest and move to smaller buckets */
|
2024-04-12 09:31:40 -07:00
|
|
|
for (i = (listSz - 1); i >= 0; i--) {
|
2016-07-21 12:11:15 -06:00
|
|
|
for (k = distList[i]; k > 0; k--) {
|
2024-04-11 13:59:28 -07:00
|
|
|
if ((sizeList[i] + padSz + memSz) <= ava) {
|
|
|
|
|
ava -= sizeList[i] + padSz + memSz;
|
2016-07-21 12:11:15 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 13:59:28 -07:00
|
|
|
WOLFSSL_LEAVE("wolfSSL_StaticBufferSz_ex", sz - ava);
|
2016-07-21 12:11:15 -06:00
|
|
|
return sz - ava; /* round down */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-04-11 13:59:28 -07:00
|
|
|
/* Calls wolfSSL_StaticBufferSz_ex with the static memory pool config
|
|
|
|
|
* used by wolfSSL by default. */
|
|
|
|
|
int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag)
|
|
|
|
|
{
|
2024-04-12 09:31:40 -07:00
|
|
|
word32 bucketSz[WOLFMEM_DEF_BUCKETS] = {WOLFMEM_BUCKETS};
|
|
|
|
|
word32 distList[WOLFMEM_DEF_BUCKETS] = {WOLFMEM_DIST};
|
2024-04-11 13:59:28 -07:00
|
|
|
|
2024-04-12 09:31:40 -07:00
|
|
|
return wolfSSL_StaticBufferSz_ex(WOLFMEM_DEF_BUCKETS, bucketSz, distList,
|
2024-04-11 13:59:28 -07:00
|
|
|
buffer, sz, flag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
int FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io)
|
|
|
|
|
{
|
|
|
|
|
WOLFSSL_MSG("Freeing fixed IO buffer");
|
|
|
|
|
|
|
|
|
|
/* check if fixed buffer was set */
|
|
|
|
|
if (*io == NULL) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (heap == NULL) {
|
|
|
|
|
WOLFSSL_MSG("No heap to return fixed IO too");
|
|
|
|
|
}
|
2016-06-07 15:39:16 -06:00
|
|
|
else {
|
|
|
|
|
/* put IO buffer back into IO pool */
|
|
|
|
|
(*io)->next = heap->io;
|
|
|
|
|
heap->io = *io;
|
|
|
|
|
*io = NULL;
|
|
|
|
|
}
|
2016-03-23 10:21:26 -06:00
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
int SetFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io)
|
2016-03-23 10:21:26 -06:00
|
|
|
{
|
2016-06-04 19:03:48 -06:00
|
|
|
WOLFSSL_MSG("Setting fixed IO for SSL");
|
|
|
|
|
if (heap == NULL) {
|
|
|
|
|
return MEMORY_E;
|
|
|
|
|
}
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
*io = heap->io;
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
if (*io != NULL) {
|
|
|
|
|
heap->io = (*io)->next;
|
|
|
|
|
(*io)->next = NULL;
|
|
|
|
|
}
|
|
|
|
|
else { /* failed to grab an IO buffer */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
return 1;
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, WOLFSSL_MEM_STATS* stats)
|
2016-03-23 10:21:26 -06:00
|
|
|
{
|
2016-06-04 19:03:48 -06:00
|
|
|
word32 i;
|
|
|
|
|
wc_Memory* pt;
|
|
|
|
|
|
|
|
|
|
XMEMSET(stats, 0, sizeof(WOLFSSL_MEM_STATS));
|
|
|
|
|
|
|
|
|
|
stats->totalAlloc = heap->alloc;
|
|
|
|
|
stats->totalFr = heap->frAlc;
|
|
|
|
|
stats->curAlloc = stats->totalAlloc - stats->totalFr;
|
|
|
|
|
stats->maxHa = heap->maxHa;
|
|
|
|
|
stats->maxIO = heap->maxIO;
|
|
|
|
|
for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
|
|
|
|
|
stats->blockSz[i] = heap->sizeList[i];
|
|
|
|
|
for (pt = heap->ava[i]; pt != NULL; pt = pt->next) {
|
|
|
|
|
stats->avaBlock[i] += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
for (pt = heap->io; pt != NULL; pt = pt->next) {
|
|
|
|
|
stats->avaIO++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stats->flag = heap->flag; /* flag used */
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
return 1;
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-04-26 08:25:16 -06:00
|
|
|
/* global heap hint to fall back on when no heap hint is passed to
|
|
|
|
|
* XMALLOC/XFREE
|
|
|
|
|
* NOT thread safe, should be set once before any expected XMALLOC XFREE calls
|
|
|
|
|
*/
|
|
|
|
|
static void* globalHeapHint = NULL;
|
2024-04-30 16:38:45 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Used to set a new global heap hint. Returns a pointer to the current global
|
|
|
|
|
* heap hint before being set. */
|
|
|
|
|
void* wolfSSL_SetGlobalHeapHint(void* heap)
|
2024-04-26 08:25:16 -06:00
|
|
|
{
|
2024-04-30 16:38:45 -06:00
|
|
|
void *oldHint = globalHeapHint;
|
|
|
|
|
|
2024-04-26 08:25:16 -06:00
|
|
|
globalHeapHint = heap;
|
2024-04-30 16:38:45 -06:00
|
|
|
return oldHint;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* returns a pointer to the current global heap hint */
|
2024-05-15 10:28:39 -06:00
|
|
|
void* wolfSSL_GetGlobalHeapHint(void)
|
2024-04-30 16:38:45 -06:00
|
|
|
{
|
|
|
|
|
return globalHeapHint;
|
2024-04-26 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-08-29 10:38:06 -07:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line)
|
|
|
|
|
#else
|
2016-06-04 19:03:48 -06:00
|
|
|
void* wolfSSL_Malloc(size_t size, void* heap, int type)
|
2016-08-29 10:38:06 -07:00
|
|
|
#endif
|
2016-03-23 10:21:26 -06:00
|
|
|
{
|
|
|
|
|
void* res = 0;
|
|
|
|
|
wc_Memory* pt = NULL;
|
|
|
|
|
int i;
|
|
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
/* check for testing heap hint was set */
|
|
|
|
|
#ifdef WOLFSSL_HEAP_TEST
|
|
|
|
|
if (heap == (void*)WOLFSSL_HEAP_TEST) {
|
|
|
|
|
return malloc(size);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* if no heap hint then use dynamic memory*/
|
2024-04-26 08:25:16 -06:00
|
|
|
if (heap == NULL && globalHeapHint == NULL) {
|
2016-06-04 19:03:48 -06:00
|
|
|
#ifdef WOLFSSL_HEAP_TEST
|
|
|
|
|
/* allow using malloc for creating ctx and method */
|
|
|
|
|
if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
|
|
|
|
|
type == DYNAMIC_TYPE_CERT_MANAGER) {
|
2021-12-16 13:30:43 -07:00
|
|
|
WOLFSSL_MSG("ERROR allowing null heap hint for ctx/method");
|
2016-06-04 19:03:48 -06:00
|
|
|
res = malloc(size);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2021-12-16 13:30:43 -07:00
|
|
|
WOLFSSL_MSG("ERROR null heap hint passed into XMALLOC");
|
2016-06-04 19:03:48 -06:00
|
|
|
res = NULL;
|
|
|
|
|
}
|
|
|
|
|
#else
|
2016-06-06 14:32:49 -06:00
|
|
|
#ifndef WOLFSSL_NO_MALLOC
|
2018-02-06 09:28:27 -08:00
|
|
|
#ifdef FREERTOS
|
|
|
|
|
res = pvPortMalloc(size);
|
2022-06-09 11:34:55 +01:00
|
|
|
#elif defined(WOLFSSL_EMBOS)
|
|
|
|
|
res = OS_HEAP_malloc(size);
|
2018-02-06 09:28:27 -08:00
|
|
|
#else
|
|
|
|
|
res = malloc(size);
|
|
|
|
|
#endif
|
2021-10-21 13:40:30 -07:00
|
|
|
|
|
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
2022-05-12 13:07:32 -05:00
|
|
|
fprintf(stderr, "Alloc: %p -> %u at %s:%d\n", res, (word32)size, func, line);
|
2021-10-21 13:40:30 -07:00
|
|
|
#endif
|
2016-06-06 14:32:49 -06:00
|
|
|
#else
|
|
|
|
|
WOLFSSL_MSG("No heap hint found to use and no malloc");
|
2017-05-18 09:31:09 -06:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
2022-05-12 13:07:32 -05:00
|
|
|
fprintf(stderr, "ERROR: at %s:%d\n", func, line);
|
2017-05-18 09:31:09 -06:00
|
|
|
#endif
|
2016-06-06 14:32:49 -06:00
|
|
|
#endif /* WOLFSSL_NO_MALLOC */
|
|
|
|
|
#endif /* WOLFSSL_HEAP_TEST */
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
else {
|
2016-06-04 19:03:48 -06:00
|
|
|
WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
|
2024-04-26 08:25:16 -06:00
|
|
|
WOLFSSL_HEAP* mem;
|
|
|
|
|
|
|
|
|
|
if (hint == NULL) {
|
|
|
|
|
hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
|
2024-04-30 16:38:45 -06:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
fprintf(stderr, "(Using global heap hint %p) ", hint);
|
|
|
|
|
#endif
|
2024-04-26 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
mem = hint->memory;
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2016-09-26 17:25:01 -06:00
|
|
|
if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
|
2016-03-23 10:21:26 -06:00
|
|
|
WOLFSSL_MSG("Bad memory_mutex lock");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
/* case of using fixed IO buffers */
|
2017-03-01 11:39:00 -07:00
|
|
|
if (mem->flag & WOLFMEM_IO_POOL_FIXED &&
|
|
|
|
|
(type == DYNAMIC_TYPE_OUT_BUFFER ||
|
|
|
|
|
type == DYNAMIC_TYPE_IN_BUFFER)) {
|
2016-06-04 19:03:48 -06:00
|
|
|
if (type == DYNAMIC_TYPE_OUT_BUFFER) {
|
|
|
|
|
pt = hint->outBuf;
|
|
|
|
|
}
|
|
|
|
|
if (type == DYNAMIC_TYPE_IN_BUFFER) {
|
|
|
|
|
pt = hint->inBuf;
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
2017-03-01 11:39:00 -07:00
|
|
|
else {
|
|
|
|
|
/* check if using IO pool flag */
|
|
|
|
|
if (mem->flag & WOLFMEM_IO_POOL &&
|
2016-06-04 19:03:48 -06:00
|
|
|
(type == DYNAMIC_TYPE_OUT_BUFFER ||
|
|
|
|
|
type == DYNAMIC_TYPE_IN_BUFFER)) {
|
2017-03-01 11:39:00 -07:00
|
|
|
if (mem->io != NULL) {
|
|
|
|
|
pt = mem->io;
|
|
|
|
|
mem->io = pt->next;
|
|
|
|
|
}
|
2016-06-04 19:03:48 -06:00
|
|
|
}
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2017-03-01 11:39:00 -07:00
|
|
|
/* general static memory */
|
|
|
|
|
if (pt == NULL) {
|
|
|
|
|
for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
|
2021-08-17 10:38:27 -07:00
|
|
|
if ((word32)size <= mem->sizeList[i]) {
|
2017-03-01 11:39:00 -07:00
|
|
|
if (mem->ava[i] != NULL) {
|
|
|
|
|
pt = mem->ava[i];
|
|
|
|
|
mem->ava[i] = pt->next;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-02-22 17:14:19 +10:00
|
|
|
#ifdef WOLFSSL_DEBUG_STATIC_MEMORY
|
2024-04-16 16:25:17 +02:00
|
|
|
else {
|
2024-05-06 14:45:09 +02:00
|
|
|
fprintf(stderr, "Size: %lu, Empty: %d\n", (unsigned long) size,
|
2024-04-16 16:25:17 +02:00
|
|
|
mem->sizeList[i]);
|
|
|
|
|
}
|
2019-02-22 17:14:19 +10:00
|
|
|
#endif
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-04 19:03:48 -06:00
|
|
|
}
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
if (pt != NULL) {
|
2016-06-10 11:15:54 -06:00
|
|
|
mem->inUse += pt->sz;
|
2016-06-04 19:03:48 -06:00
|
|
|
mem->alloc += 1;
|
|
|
|
|
res = pt->buffer;
|
|
|
|
|
|
2016-08-29 10:38:06 -07:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
2022-05-12 13:07:32 -05:00
|
|
|
fprintf(stderr, "Alloc: %p -> %u at %s:%d\n", pt->buffer, pt->sz, func, line);
|
2016-08-29 10:38:06 -07:00
|
|
|
#endif
|
|
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
/* keep track of connection statistics if flag is set */
|
|
|
|
|
if (mem->flag & WOLFMEM_TRACK_STATS) {
|
|
|
|
|
WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
|
|
|
|
|
if (stats != NULL) {
|
2016-06-10 11:15:54 -06:00
|
|
|
stats->curMem += pt->sz;
|
2016-06-04 19:03:48 -06:00
|
|
|
if (stats->peakMem < stats->curMem) {
|
|
|
|
|
stats->peakMem = stats->curMem;
|
|
|
|
|
}
|
|
|
|
|
stats->curAlloc++;
|
|
|
|
|
if (stats->peakAlloc < stats->curAlloc) {
|
|
|
|
|
stats->peakAlloc = stats->curAlloc;
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
2016-06-04 19:03:48 -06:00
|
|
|
stats->totalAlloc++;
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-04 19:03:48 -06:00
|
|
|
else {
|
|
|
|
|
WOLFSSL_MSG("ERROR ran out of static memory");
|
2017-05-18 09:31:09 -06:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
2024-05-06 14:45:09 +02:00
|
|
|
fprintf(stderr, "Looking for %lu bytes at %s:%d\n", (unsigned long) size, func,
|
2024-04-16 16:25:17 +02:00
|
|
|
line);
|
2017-05-18 09:31:09 -06:00
|
|
|
#endif
|
2016-06-04 19:03:48 -06:00
|
|
|
}
|
|
|
|
|
|
2016-09-26 17:25:01 -06:00
|
|
|
wc_UnLockMutex(&(mem->memory_mutex));
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef WOLFSSL_MALLOC_CHECK
|
2021-05-03 11:23:55 -07:00
|
|
|
if ((wc_ptr_t)res % WOLFSSL_STATIC_ALIGN) {
|
2019-12-24 12:29:33 -06:00
|
|
|
WOLFSSL_MSG("ERROR memory is not aligned");
|
2016-06-10 11:15:54 -06:00
|
|
|
res = NULL;
|
2016-06-09 11:36:31 -06:00
|
|
|
}
|
2016-03-23 10:21:26 -06:00
|
|
|
#endif
|
|
|
|
|
|
2016-06-10 11:15:54 -06:00
|
|
|
|
2016-03-23 10:21:26 -06:00
|
|
|
(void)i;
|
|
|
|
|
(void)pt;
|
2016-06-04 19:03:48 -06:00
|
|
|
(void)type;
|
2016-03-23 10:21:26 -06:00
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
|
2016-08-29 10:38:06 -07:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line)
|
|
|
|
|
#else
|
2016-06-04 19:03:48 -06:00
|
|
|
void wolfSSL_Free(void *ptr, void* heap, int type)
|
2016-08-29 10:38:06 -07:00
|
|
|
#endif
|
2016-03-23 10:21:26 -06:00
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
wc_Memory* pt;
|
|
|
|
|
|
|
|
|
|
if (ptr) {
|
2016-06-04 19:03:48 -06:00
|
|
|
/* check for testing heap hint was set */
|
|
|
|
|
#ifdef WOLFSSL_HEAP_TEST
|
|
|
|
|
if (heap == (void*)WOLFSSL_HEAP_TEST) {
|
2021-10-21 13:40:30 -07:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
2022-05-12 13:07:32 -05:00
|
|
|
fprintf(stderr, "Free: %p at %s:%d\n", pt, func, line);
|
2021-10-21 13:40:30 -07:00
|
|
|
#endif
|
2016-06-04 19:03:48 -06:00
|
|
|
return free(ptr);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2024-04-26 08:25:16 -06:00
|
|
|
if (heap == NULL && globalHeapHint == NULL) {
|
2016-06-04 19:03:48 -06:00
|
|
|
#ifdef WOLFSSL_HEAP_TEST
|
|
|
|
|
/* allow using malloc for creating ctx and method */
|
|
|
|
|
if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
|
|
|
|
|
type == DYNAMIC_TYPE_CERT_MANAGER) {
|
2021-12-16 13:30:43 -07:00
|
|
|
WOLFSSL_MSG("ERROR allowing null heap hint for ctx/method");
|
2016-06-04 19:03:48 -06:00
|
|
|
}
|
|
|
|
|
else {
|
2021-12-16 13:30:43 -07:00
|
|
|
WOLFSSL_MSG("ERROR null heap hint passed into XFREE");
|
2016-06-04 19:03:48 -06:00
|
|
|
}
|
|
|
|
|
#endif
|
2016-06-06 14:32:49 -06:00
|
|
|
#ifndef WOLFSSL_NO_MALLOC
|
2018-02-06 09:28:27 -08:00
|
|
|
#ifdef FREERTOS
|
|
|
|
|
vPortFree(ptr);
|
2022-06-09 11:34:55 +01:00
|
|
|
#elif defined(WOLFSSL_EMBOS)
|
|
|
|
|
OS_HEAP_free(ptr);
|
2018-02-06 09:28:27 -08:00
|
|
|
#else
|
|
|
|
|
free(ptr);
|
|
|
|
|
#endif
|
2016-06-06 14:32:49 -06:00
|
|
|
#else
|
|
|
|
|
WOLFSSL_MSG("Error trying to call free when turned off");
|
|
|
|
|
#endif /* WOLFSSL_NO_MALLOC */
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
else {
|
2016-06-04 19:03:48 -06:00
|
|
|
WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
|
2024-04-26 08:25:16 -06:00
|
|
|
WOLFSSL_HEAP* mem;
|
2016-06-10 11:15:54 -06:00
|
|
|
word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2024-04-26 08:25:16 -06:00
|
|
|
if (hint == NULL) {
|
|
|
|
|
hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
|
2024-04-30 16:38:45 -06:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
fprintf(stderr, "(Using global heap hint %p) ", hint);
|
|
|
|
|
#endif
|
2024-04-26 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
mem = hint->memory;
|
|
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
/* get memory struct and add it to available list */
|
2016-06-10 11:15:54 -06:00
|
|
|
pt = (wc_Memory*)((byte*)ptr - sizeof(wc_Memory) - padSz);
|
2016-09-26 17:25:01 -06:00
|
|
|
if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
|
2016-06-10 15:35:02 -06:00
|
|
|
WOLFSSL_MSG("Bad memory_mutex lock");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-04 19:03:48 -06:00
|
|
|
|
|
|
|
|
/* case of using fixed IO buffers */
|
|
|
|
|
if (mem->flag & WOLFMEM_IO_POOL_FIXED &&
|
|
|
|
|
(type == DYNAMIC_TYPE_OUT_BUFFER ||
|
|
|
|
|
type == DYNAMIC_TYPE_IN_BUFFER)) {
|
|
|
|
|
/* fixed IO pools are free'd at the end of SSL lifetime
|
|
|
|
|
using FreeFixedIO(WOLFSSL_HEAP* heap, wc_Memory** io) */
|
|
|
|
|
}
|
2017-03-01 11:39:00 -07:00
|
|
|
else if (mem->flag & WOLFMEM_IO_POOL && pt->sz == WOLFMEM_IO_SZ &&
|
2016-06-04 19:03:48 -06:00
|
|
|
(type == DYNAMIC_TYPE_OUT_BUFFER ||
|
|
|
|
|
type == DYNAMIC_TYPE_IN_BUFFER)) {
|
|
|
|
|
pt->next = mem->io;
|
|
|
|
|
mem->io = pt;
|
|
|
|
|
}
|
|
|
|
|
else { /* general memory free */
|
|
|
|
|
for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
|
|
|
|
|
if (pt->sz == mem->sizeList[i]) {
|
|
|
|
|
pt->next = mem->ava[i];
|
|
|
|
|
mem->ava[i] = pt;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
2016-06-10 11:15:54 -06:00
|
|
|
mem->inUse -= pt->sz;
|
2016-06-04 19:03:48 -06:00
|
|
|
mem->frAlc += 1;
|
|
|
|
|
|
2016-08-29 10:38:06 -07:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
2022-05-12 13:07:32 -05:00
|
|
|
fprintf(stderr, "Free: %p -> %u at %s:%d\n", pt->buffer, pt->sz, func, line);
|
2016-08-29 10:38:06 -07:00
|
|
|
#endif
|
|
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
/* keep track of connection statistics if flag is set */
|
|
|
|
|
if (mem->flag & WOLFMEM_TRACK_STATS) {
|
|
|
|
|
WOLFSSL_MEM_CONN_STATS* stats = hint->stats;
|
|
|
|
|
if (stats != NULL) {
|
|
|
|
|
/* avoid under flow */
|
2016-06-10 11:15:54 -06:00
|
|
|
if (stats->curMem > pt->sz) {
|
|
|
|
|
stats->curMem -= pt->sz;
|
2016-06-04 19:03:48 -06:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
stats->curMem = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stats->curAlloc > 0) {
|
|
|
|
|
stats->curAlloc--;
|
|
|
|
|
}
|
|
|
|
|
stats->totalFr++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-26 17:25:01 -06:00
|
|
|
wc_UnLockMutex(&(mem->memory_mutex));
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(void)i;
|
|
|
|
|
(void)pt;
|
2016-06-04 19:03:48 -06:00
|
|
|
(void)type;
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
|
2016-08-29 10:38:06 -07:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line)
|
|
|
|
|
#else
|
2016-06-04 19:03:48 -06:00
|
|
|
void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
|
2016-08-29 10:38:06 -07:00
|
|
|
#endif
|
2016-03-23 10:21:26 -06:00
|
|
|
{
|
|
|
|
|
void* res = 0;
|
|
|
|
|
wc_Memory* pt = NULL;
|
2016-06-04 19:03:48 -06:00
|
|
|
int i;
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
/* check for testing heap hint was set */
|
|
|
|
|
#ifdef WOLFSSL_HEAP_TEST
|
|
|
|
|
if (heap == (void*)WOLFSSL_HEAP_TEST) {
|
|
|
|
|
return realloc(ptr, size);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-04-26 08:25:16 -06:00
|
|
|
if (heap == NULL && globalHeapHint == NULL) {
|
2016-06-04 19:03:48 -06:00
|
|
|
#ifdef WOLFSSL_HEAP_TEST
|
2021-12-16 13:30:43 -07:00
|
|
|
WOLFSSL_MSG("ERROR null heap hint passed in to XREALLOC");
|
2016-06-04 19:03:48 -06:00
|
|
|
#endif
|
2016-06-06 14:32:49 -06:00
|
|
|
#ifndef WOLFSSL_NO_MALLOC
|
|
|
|
|
res = realloc(ptr, size);
|
|
|
|
|
#else
|
|
|
|
|
WOLFSSL_MSG("NO heap found to use for realloc");
|
|
|
|
|
#endif /* WOLFSSL_NO_MALLOC */
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
else {
|
2016-06-04 19:03:48 -06:00
|
|
|
WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
|
2024-04-26 08:25:16 -06:00
|
|
|
WOLFSSL_HEAP* mem;
|
2016-06-10 11:15:54 -06:00
|
|
|
word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
|
2016-03-23 10:21:26 -06:00
|
|
|
|
2024-04-26 08:25:16 -06:00
|
|
|
if (hint == NULL) {
|
|
|
|
|
hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
|
2024-04-30 16:38:45 -06:00
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
fprintf(stderr, "(Using global heap hint %p) ", hint);
|
|
|
|
|
#endif
|
2024-04-26 08:25:16 -06:00
|
|
|
}
|
|
|
|
|
mem = hint->memory;
|
|
|
|
|
|
2019-02-22 17:14:19 +10:00
|
|
|
if (ptr == NULL) {
|
|
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
return wolfSSL_Malloc(size, heap, type, func, line);
|
|
|
|
|
#else
|
|
|
|
|
return wolfSSL_Malloc(size, heap, type);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-26 17:25:01 -06:00
|
|
|
if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
|
2016-03-23 10:21:26 -06:00
|
|
|
WOLFSSL_MSG("Bad memory_mutex lock");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
/* case of using fixed IO buffers or IO pool */
|
|
|
|
|
if (((mem->flag & WOLFMEM_IO_POOL)||(mem->flag & WOLFMEM_IO_POOL_FIXED))
|
|
|
|
|
&& (type == DYNAMIC_TYPE_OUT_BUFFER ||
|
|
|
|
|
type == DYNAMIC_TYPE_IN_BUFFER)) {
|
|
|
|
|
/* no realloc, is fixed size */
|
2016-06-10 11:15:54 -06:00
|
|
|
pt = (wc_Memory*)((byte*)ptr - padSz - sizeof(wc_Memory));
|
2016-06-04 19:03:48 -06:00
|
|
|
if (pt->sz < size) {
|
|
|
|
|
WOLFSSL_MSG("Error IO memory was not large enough");
|
|
|
|
|
res = NULL; /* return NULL in error case */
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
2016-06-04 19:03:48 -06:00
|
|
|
res = pt->buffer;
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
2016-06-04 19:03:48 -06:00
|
|
|
else {
|
|
|
|
|
/* general memory */
|
|
|
|
|
for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
|
2021-08-17 10:38:27 -07:00
|
|
|
if ((word32)size <= mem->sizeList[i]) {
|
2016-06-04 19:03:48 -06:00
|
|
|
if (mem->ava[i] != NULL) {
|
|
|
|
|
pt = mem->ava[i];
|
|
|
|
|
mem->ava[i] = pt->next;
|
|
|
|
|
break;
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-04 19:03:48 -06:00
|
|
|
if (pt != NULL && res == NULL) {
|
|
|
|
|
res = pt->buffer;
|
|
|
|
|
|
|
|
|
|
/* copy over original information and free ptr */
|
2023-04-03 16:51:07 +10:00
|
|
|
word32 prvSz = ((wc_Memory*)((byte*)ptr - padSz -
|
2016-06-10 11:15:54 -06:00
|
|
|
sizeof(wc_Memory)))->sz;
|
2016-06-04 19:03:48 -06:00
|
|
|
prvSz = (prvSz > pt->sz)? pt->sz: prvSz;
|
|
|
|
|
XMEMCPY(pt->buffer, ptr, prvSz);
|
2016-06-10 11:15:54 -06:00
|
|
|
mem->inUse += pt->sz;
|
2016-06-04 19:03:48 -06:00
|
|
|
mem->alloc += 1;
|
|
|
|
|
|
|
|
|
|
/* free memory that was previously being used */
|
2016-09-26 17:25:01 -06:00
|
|
|
wc_UnLockMutex(&(mem->memory_mutex));
|
2016-08-29 10:38:06 -07:00
|
|
|
wolfSSL_Free(ptr, heap, type
|
|
|
|
|
#ifdef WOLFSSL_DEBUG_MEMORY
|
|
|
|
|
, func, line
|
|
|
|
|
#endif
|
|
|
|
|
);
|
2016-09-26 17:25:01 -06:00
|
|
|
if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
|
2016-03-23 10:21:26 -06:00
|
|
|
WOLFSSL_MSG("Bad memory_mutex lock");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-26 17:25:01 -06:00
|
|
|
wc_UnLockMutex(&(mem->memory_mutex));
|
2016-03-23 10:21:26 -06:00
|
|
|
}
|
|
|
|
|
|
2016-06-09 11:36:31 -06:00
|
|
|
#ifdef WOLFSSL_MALLOC_CHECK
|
2021-05-03 11:23:55 -07:00
|
|
|
if ((wc_ptr_t)res % WOLFSSL_STATIC_ALIGN) {
|
2019-12-24 12:29:33 -06:00
|
|
|
WOLFSSL_MSG("ERROR memory is not aligned");
|
2016-06-10 11:15:54 -06:00
|
|
|
res = NULL;
|
2016-06-09 11:36:31 -06:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-03-23 10:21:26 -06:00
|
|
|
(void)i;
|
|
|
|
|
(void)pt;
|
2016-06-04 19:03:48 -06:00
|
|
|
(void)type;
|
2016-03-23 10:21:26 -06:00
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
#endif /* WOLFSSL_STATIC_MEMORY */
|
|
|
|
|
|
2014-12-29 10:27:03 -07:00
|
|
|
#endif /* USE_WOLFSSL_MEMORY */
|
2014-12-19 09:56:51 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_IO_POOL
|
|
|
|
|
|
|
|
|
|
/* Example for user io pool, shared build may need definitions in lib proper */
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#ifndef HAVE_THREAD_LS
|
|
|
|
|
#error "Oops, simple I/O pool example needs thread local storage"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* allow simple per thread in and out pools */
|
2016-08-29 10:38:06 -07:00
|
|
|
/* use 17k size since max record size is 16k plus overhead */
|
2014-12-19 09:56:51 -07:00
|
|
|
static THREAD_LS_T byte pool_in[17*1024];
|
|
|
|
|
static THREAD_LS_T byte pool_out[17*1024];
|
|
|
|
|
|
|
|
|
|
|
2015-01-05 09:53:00 -07:00
|
|
|
void* XMALLOC(size_t n, void* heap, int type)
|
2014-12-19 09:56:51 -07:00
|
|
|
{
|
|
|
|
|
(void)heap;
|
|
|
|
|
|
|
|
|
|
if (type == DYNAMIC_TYPE_IN_BUFFER) {
|
|
|
|
|
if (n < sizeof(pool_in))
|
|
|
|
|
return pool_in;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type == DYNAMIC_TYPE_OUT_BUFFER) {
|
|
|
|
|
if (n < sizeof(pool_out))
|
|
|
|
|
return pool_out;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return malloc(n);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-05 09:53:00 -07:00
|
|
|
void* XREALLOC(void *p, size_t n, void* heap, int type)
|
2014-12-19 09:56:51 -07:00
|
|
|
{
|
|
|
|
|
(void)heap;
|
|
|
|
|
|
|
|
|
|
if (type == DYNAMIC_TYPE_IN_BUFFER) {
|
|
|
|
|
if (n < sizeof(pool_in))
|
|
|
|
|
return pool_in;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type == DYNAMIC_TYPE_OUT_BUFFER) {
|
|
|
|
|
if (n < sizeof(pool_out))
|
|
|
|
|
return pool_out;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return realloc(p, n);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-29 10:38:06 -07:00
|
|
|
void XFREE(void *p, void* heap, int type)
|
2014-12-19 09:56:51 -07:00
|
|
|
{
|
|
|
|
|
(void)heap;
|
|
|
|
|
|
|
|
|
|
if (type == DYNAMIC_TYPE_IN_BUFFER)
|
|
|
|
|
return; /* do nothing, static pool */
|
|
|
|
|
|
|
|
|
|
if (type == DYNAMIC_TYPE_OUT_BUFFER)
|
|
|
|
|
return; /* do nothing, static pool */
|
|
|
|
|
|
|
|
|
|
free(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* HAVE_IO_POOL */
|
|
|
|
|
|
2018-08-21 09:03:11 +10:00
|
|
|
#ifdef WOLFSSL_MEMORY_LOG
|
2018-07-05 22:18:51 +10:00
|
|
|
void *xmalloc(size_t n, void* heap, int type, const char* func,
|
2018-08-21 09:03:11 +10:00
|
|
|
const char* file, unsigned int line)
|
2018-07-05 22:18:51 +10:00
|
|
|
{
|
2021-01-12 12:25:52 +10:00
|
|
|
void* p = NULL;
|
2018-07-05 22:18:51 +10:00
|
|
|
word32* p32;
|
|
|
|
|
|
2023-05-26 15:49:14 +10:00
|
|
|
#ifdef WOLFSSL_MEM_FAIL_COUNT
|
|
|
|
|
if (!wc_MemFailCount_AllocMem()) {
|
|
|
|
|
WOLFSSL_MSG("MemFailCnt: Fail malloc");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-06-22 08:11:50 +10:00
|
|
|
if (malloc_function) {
|
|
|
|
|
#ifndef WOLFSSL_STATIC_MEMORY
|
2018-07-05 22:18:51 +10:00
|
|
|
p32 = malloc_function(n + sizeof(word32) * 4);
|
2023-06-22 08:11:50 +10:00
|
|
|
#else
|
|
|
|
|
p32 = malloc_function(n + sizeof(word32) * 4, heap, type);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2018-07-05 22:18:51 +10:00
|
|
|
else
|
|
|
|
|
p32 = malloc(n + sizeof(word32) * 4);
|
|
|
|
|
|
2021-01-12 12:25:52 +10:00
|
|
|
if (p32 != NULL) {
|
|
|
|
|
p32[0] = (word32)n;
|
|
|
|
|
p = (void*)(p32 + 4);
|
2018-07-05 22:18:51 +10:00
|
|
|
|
2021-01-12 12:25:52 +10:00
|
|
|
fprintf(stderr, "Alloc: %p -> %u (%d) at %s:%s:%u\n", p, (word32)n,
|
|
|
|
|
type, func, file, line);
|
|
|
|
|
}
|
2018-07-05 22:18:51 +10:00
|
|
|
|
|
|
|
|
(void)heap;
|
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
void *xrealloc(void *p, size_t n, void* heap, int type, const char* func,
|
2018-08-21 09:03:11 +10:00
|
|
|
const char* file, unsigned int line)
|
2018-07-05 22:18:51 +10:00
|
|
|
{
|
|
|
|
|
void* newp = NULL;
|
|
|
|
|
word32* p32;
|
|
|
|
|
word32* oldp32 = NULL;
|
2018-08-21 09:03:11 +10:00
|
|
|
word32 oldLen;
|
2018-07-05 22:18:51 +10:00
|
|
|
|
2023-05-26 15:49:14 +10:00
|
|
|
#ifdef WOLFSSL_MEM_FAIL_COUNT
|
|
|
|
|
if (!wc_MemFailCount_AllocMem()) {
|
|
|
|
|
WOLFSSL_MSG("MemFailCnt: Fail malloc");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-07-05 22:18:51 +10:00
|
|
|
if (p != NULL) {
|
|
|
|
|
oldp32 = (word32*)p;
|
|
|
|
|
oldp32 -= 4;
|
2018-08-21 09:03:11 +10:00
|
|
|
oldLen = oldp32[0];
|
2018-07-05 22:18:51 +10:00
|
|
|
}
|
|
|
|
|
|
2023-06-22 08:11:50 +10:00
|
|
|
if (realloc_function) {
|
|
|
|
|
#ifndef WOLFSSL_STATIC_MEMORY
|
2018-07-05 22:18:51 +10:00
|
|
|
p32 = realloc_function(oldp32, n + sizeof(word32) * 4);
|
2023-06-22 08:11:50 +10:00
|
|
|
#else
|
|
|
|
|
p32 = realloc_function(oldp32, n + sizeof(word32) * 4, heap, type);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2018-07-05 22:18:51 +10:00
|
|
|
else
|
|
|
|
|
p32 = realloc(oldp32, n + sizeof(word32) * 4);
|
|
|
|
|
|
|
|
|
|
if (p32 != NULL) {
|
2019-01-10 16:25:09 -08:00
|
|
|
p32[0] = (word32)n;
|
2018-07-05 22:18:51 +10:00
|
|
|
newp = (void*)(p32 + 4);
|
|
|
|
|
|
|
|
|
|
if (p != NULL) {
|
2020-01-15 22:15:38 +10:00
|
|
|
fprintf(stderr, "Free: %p -> %u (%d) at %s:%s:%u\n", p, oldLen,
|
2018-08-21 09:03:11 +10:00
|
|
|
type, func, file, line);
|
2018-07-05 22:18:51 +10:00
|
|
|
}
|
2022-07-07 12:16:12 +10:00
|
|
|
fprintf(stderr, "Alloc: %p -> %u (%d) at %s:%s:%u\n", newp, (word32)n,
|
|
|
|
|
type, func, file, line);
|
2018-07-05 22:18:51 +10:00
|
|
|
}
|
|
|
|
|
|
2023-05-26 15:49:14 +10:00
|
|
|
#ifdef WOLFSSL_MEM_FAIL_COUNT
|
|
|
|
|
if (p != NULL) {
|
|
|
|
|
wc_MemFailCount_FreeMem();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-07-05 22:18:51 +10:00
|
|
|
(void)heap;
|
|
|
|
|
|
|
|
|
|
return newp;
|
|
|
|
|
}
|
2018-08-21 09:03:11 +10:00
|
|
|
void xfree(void *p, void* heap, int type, const char* func, const char* file,
|
|
|
|
|
unsigned int line)
|
2018-07-05 22:18:51 +10:00
|
|
|
{
|
|
|
|
|
word32* p32 = (word32*)p;
|
|
|
|
|
|
|
|
|
|
if (p != NULL) {
|
2023-05-26 15:49:14 +10:00
|
|
|
#ifdef WOLFSSL_MEM_FAIL_COUNT
|
|
|
|
|
wc_MemFailCount_FreeMem();
|
|
|
|
|
#endif
|
2018-07-05 22:18:51 +10:00
|
|
|
p32 -= 4;
|
|
|
|
|
|
2020-01-15 22:15:38 +10:00
|
|
|
fprintf(stderr, "Free: %p -> %u (%d) at %s:%s:%u\n", p, p32[0], type,
|
2018-08-21 09:03:11 +10:00
|
|
|
func, file, line);
|
2018-07-05 22:18:51 +10:00
|
|
|
|
2023-06-22 08:11:50 +10:00
|
|
|
if (free_function) {
|
|
|
|
|
#ifndef WOLFSSL_STATIC_MEMORY
|
2018-07-05 22:18:51 +10:00
|
|
|
free_function(p32);
|
2023-06-22 08:11:50 +10:00
|
|
|
#else
|
|
|
|
|
free_function(p32, heap, type);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2018-07-05 22:18:51 +10:00
|
|
|
else
|
|
|
|
|
free(p32);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(void)heap;
|
|
|
|
|
}
|
2018-08-21 09:03:11 +10:00
|
|
|
#endif /* WOLFSSL_MEMORY_LOG */
|
2018-07-05 22:18:51 +10:00
|
|
|
|
2018-08-29 10:16:51 +10:00
|
|
|
#ifdef WOLFSSL_STACK_LOG
|
|
|
|
|
/* Note: this code only works with GCC using -finstrument-functions. */
|
|
|
|
|
void __attribute__((no_instrument_function))
|
|
|
|
|
__cyg_profile_func_enter(void *func, void *caller)
|
|
|
|
|
{
|
|
|
|
|
register void* sp asm("sp");
|
2021-05-03 11:23:55 -07:00
|
|
|
fprintf(stderr, "ENTER: %016lx %p\n", (unsigned long)(wc_ptr_t)func, sp);
|
2018-08-29 10:16:51 +10:00
|
|
|
(void)caller;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void __attribute__((no_instrument_function))
|
|
|
|
|
__cyg_profile_func_exit(void *func, void *caller)
|
|
|
|
|
{
|
|
|
|
|
register void* sp asm("sp");
|
2021-05-03 11:23:55 -07:00
|
|
|
fprintf(stderr, "EXIT: %016lx %p\n", (unsigned long)(wc_ptr_t)func, sp);
|
2018-08-29 10:16:51 +10:00
|
|
|
(void)caller;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
fix AES-related code, in both crypto and TLS layers, for various uninitialized data and resource leak defects around wc_AesInit() and wc_AesFree():
* followup to https://github.com/wolfSSL/wolfssl/pull/7009 "20231128-misc-fixes" and https://github.com/wolfSSL/wolfssl/pull/7011 "Add missing wc_AesInit calls."
* adds WC_DEBUG_CIPHER_LIFECYCLE, which embeds asserts in low-level AES implementations for proper usage of wc_AesInit() and wc_AesFree().
* fixes native CMAC, AES-EAX, and AES-XTS implementations to assure resource release.
* adds missing wc_AesXtsInit() API, and adds a new wc_AesXtsSetKey_NoInit().
* fixes misspellings in EVP that unconditionally gated out AES-OFB and AES-XTS.
* fixes misspellings in EVP that unconditionally gated out AES-CBC and AES-CFB code in wolfSSL_EVP_CIPHER_CTX_cleanup_cipher().
* openssl compat AES low level cipher API has no counterpart to wc_AesFree(), so these compat APIs will now be gated out in configurations where they would otherwise leak memory or file descriptors (WOLFSSL_AFALG, WOLFSSL_DEVCRYPTO, WOLF_CRYPTO_CB, etc.). A new macro, WC_AESFREE_IS_MANDATORY, is defined in wolfcrypt/aes.h to streamline this dependency.
* fixes 40 missing EVP_CIPHER_CTX_cleanup()s and 11 wc_AesFree()s in src/ssl.c, src/ssl_crypto.c, tests/api.c, and wolfcrypt/test/test.c.
2023-12-05 15:49:32 -06:00
|
|
|
#ifdef WC_DEBUG_CIPHER_LIFECYCLE
|
|
|
|
|
static const byte wc_debug_cipher_lifecycle_tag_value[] =
|
|
|
|
|
{ 'W', 'o', 'l', 'f' };
|
|
|
|
|
|
|
|
|
|
WOLFSSL_LOCAL int wc_debug_CipherLifecycleInit(
|
|
|
|
|
void **CipherLifecycleTag,
|
|
|
|
|
void *heap)
|
|
|
|
|
{
|
|
|
|
|
if (CipherLifecycleTag == NULL)
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
*CipherLifecycleTag = (void *)XMALLOC(
|
|
|
|
|
sizeof(wc_debug_cipher_lifecycle_tag_value),
|
|
|
|
|
heap,
|
|
|
|
|
DYNAMIC_TYPE_DEBUG_TAG);
|
|
|
|
|
if (*CipherLifecycleTag == NULL)
|
|
|
|
|
return MEMORY_E;
|
|
|
|
|
XMEMCPY(*CipherLifecycleTag,
|
|
|
|
|
wc_debug_cipher_lifecycle_tag_value,
|
|
|
|
|
sizeof(wc_debug_cipher_lifecycle_tag_value));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WOLFSSL_LOCAL int wc_debug_CipherLifecycleCheck(
|
|
|
|
|
void *CipherLifecycleTag,
|
|
|
|
|
int abort_p)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
if (CipherLifecycleTag == NULL) {
|
|
|
|
|
ret = BAD_STATE_E;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
if (XMEMCMP(CipherLifecycleTag,
|
|
|
|
|
wc_debug_cipher_lifecycle_tag_value,
|
|
|
|
|
sizeof(wc_debug_cipher_lifecycle_tag_value)) != 0)
|
|
|
|
|
{
|
|
|
|
|
ret = BAD_STATE_E;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
if ((ret < 0) && abort_p)
|
|
|
|
|
abort();
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WOLFSSL_LOCAL int wc_debug_CipherLifecycleFree(
|
|
|
|
|
void **CipherLifecycleTag,
|
|
|
|
|
void *heap,
|
|
|
|
|
int abort_p)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
if (CipherLifecycleTag == NULL)
|
|
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
ret = wc_debug_CipherLifecycleCheck(*CipherLifecycleTag, abort_p);
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
return ret;
|
|
|
|
|
XFREE(*CipherLifecycleTag, heap, DYNAMIC_TYPE_DEBUG_TAG);
|
|
|
|
|
*CipherLifecycleTag = NULL;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif /* WC_DEBUG_CIPHER_LIFECYCLE */
|
|
|
|
|
|
refactor AESNI implementations and *VECTOR_REGISTERS* macros to allow dynamic as-needed fallback to pure C, via WC_AES_C_DYNAMIC_FALLBACK.
wolfssl/wolfcrypt/aes.h: add key_C_fallback[] to struct Aes, and remove comment that "AESNI needs key first, rounds 2nd, not sure why yet" now that AES_128_Key_Expansion_AESNI no longer writes rounds after the expanded key.
wolfcrypt/src/aes.c:
* add _AESNI or _aesni suffixes/infixes to AESNI implementations that were missing them: AES_CBC_encrypt(), AES_CBC_decrypt_by*(), AES_ECB_encrypt(), AES_*_Key_Expansion(), AES_set_encrypt_key(), AES_set_decrypt_key(), AES_GCM_encrypt(), AES_GCM_decrypt(), AES_XTS_encrypt(), and AES_XTS_decrypt().
* move key size check from to start of wc_AesSetKeyLocal().
* refactor pure-C AES setkey and cipher implementations to use aes->key_C_fallback when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor wc_AesSetKeyLocal() to set up both AESNI and pure-C expanded keys when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor all (haveAESNI && aes->use_aesni) conditions to just (aes->use_aesni).
* add macros VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, which do nothing but push a brace level when !defined(WC_AES_C_DYNAMIC_FALLBACK), but when defined(WC_AES_C_DYNAMIC_FALLBACK), they call SAVE_VECTOR_REGISTERS2() and on failure, temporarily clear aes->use_aesni and restore at _POP().
* refactor all invocations of SAVE_VECTOR_REGISTERS() and RESTORE_VECTOR_REGISTERS() to VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, except in wc_AesSetKeyLocal(), wc_AesXtsEncrypt(), and wc_AesXtsDecrypt(), which are refactored to use SAVE_VECTOR_REGISTERS2(), with graceful failure concealment if defined(WC_AES_C_DYNAMIC_FALLBACK).
* orthogonalize cleanup code in wc_AesCbcEncrypt(), wc_AesCcmEncrypt() and wc_AesCcmDecrypt().
* streamline fallthrough software definitions of wc_AesEncryptDirect() and wc_AesDecryptDirect(), and remove special-casing for defined(WOLFSSL_LINUXKM)&&defined(WOLFSSL_AESNI).
wolfcrypt/src/aes_asm.{S,asm}:
* remove errant "movl $10, 240(%rsi)" from AES_128_Key_Expansion_AESNI.
* add _AESNI suffixes/infixes to implementations that needed them.
wolfcrypt/src/{aes_gcm_asm.{S,asm},aes_xts_asm.S}: regenerate from revisions in scripts#357 -- adds _aesni suffixes to implementations that were missing them.
wolfssl/wolfcrypt/types.h: remove DEBUG_VECTOR_REGISTER_ACCESS macros, and add dummy fallthrough definitions for SAVE_VECTOR_REGISTERS2 and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
wolfssl/wolfcrypt/memory.h: adopt DEBUG_VECTOR_REGISTER_ACCESS code from types.h, and add definitions for WC_DEBUG_VECTOR_REGISTERS_RETVAL_INITVAL and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
linuxkm/linuxkm_wc_port.h: add arch-specific macro definitions for SAVE_VECTOR_REGISTERS2().
wolfcrypt/benchmark/benchmark.c: add missing gates around calls to RESTORE_VECTOR_REGISTERS().
configure.ac:
* cover various interdependencies in enable-all/enable-all-crypto, for better behavior in combination with --disable-aesgcm, --disable-ecc, --disable-ocsp, --disable-hmac, --disable-chacha, --disable-ed25519, and --disable-ed448.
* inhibit aesgcm_stream in enable-all/enable-all-crypto when ENABLED_LINUXKM_DEFAULTS, because it is currently incompatible with WC_AES_C_DYNAMIC_FALLBACK.
* add -DWC_AES_C_DYNAMIC_FALLBACK when ENABLED_LINUXKM_DEFAULTS.
* add 3 new interdependency checks: "ECCSI requires ECC.", "SAKKE requires ECC.", "WOLFSSH requires HMAC."
wolfcrypt/src/asn.c: tweak gating to accommodate defined(NO_RSA) && !defined(HAVE_ECC).
wolfcrypt/src/evp.c: tweak gating to accommodate defined(NO_HMAC).
wolfcrypt/src/logging.c: remove DEBUG_VECTOR_REGISTER_ACCESS code (moved to memory.c).
wolfcrypt/src/memory.c: change #include of settings.h to types.h; adopt DEBUG_VECTOR_REGISTER_ACCESS code from logging.c; add implementation of SAVE_VECTOR_REGISTERS2_fuzzer().
wolfcrypt/src/pwdbased.c: add explanatory #error scrypt requires HMAC.
wolfcrypt/test/test.c:
* add DEBUG_VECTOR_REGISTER_ACCESS clauses to aes_xts_128_test(), aesecb_test(), aesctr_test(), aes_test() CBC section, aes256_test() CBC section, and aesgcm_default_test_helper()
* remove duplicate wc_AesEcbDecrypt() in aesecb_test().
* add gating for pbkdf2_test().
* fix cleanup code in dsa_test().
* fix gating in pkcs7authenveloped_run_vectors() to accommodate !defined(HAVE_AESGCM).
* fix gating in cryptocb_test() to accommodate defined(NO_HMAC).
wolfssl/wolfcrypt/cryptocb.h: remove gates around "pk" sub-struct of struct wc_CryptoInfo -- wc_CryptoInfo.pk.type (an int) is used unconditionally when --enable-debug, and is used with DH.
wolfssl/wolfcrypt/error-crypt.h: fix whitespace.
2023-11-17 01:15:28 -06:00
|
|
|
#ifdef DEBUG_VECTOR_REGISTER_ACCESS
|
|
|
|
|
THREAD_LS_T int wc_svr_count = 0;
|
|
|
|
|
THREAD_LS_T const char *wc_svr_last_file = NULL;
|
|
|
|
|
THREAD_LS_T int wc_svr_last_line = -1;
|
|
|
|
|
THREAD_LS_T int wc_debug_vector_registers_retval =
|
|
|
|
|
WC_DEBUG_VECTOR_REGISTERS_RETVAL_INITVAL;
|
2024-02-10 01:09:15 -06:00
|
|
|
#endif
|
refactor AESNI implementations and *VECTOR_REGISTERS* macros to allow dynamic as-needed fallback to pure C, via WC_AES_C_DYNAMIC_FALLBACK.
wolfssl/wolfcrypt/aes.h: add key_C_fallback[] to struct Aes, and remove comment that "AESNI needs key first, rounds 2nd, not sure why yet" now that AES_128_Key_Expansion_AESNI no longer writes rounds after the expanded key.
wolfcrypt/src/aes.c:
* add _AESNI or _aesni suffixes/infixes to AESNI implementations that were missing them: AES_CBC_encrypt(), AES_CBC_decrypt_by*(), AES_ECB_encrypt(), AES_*_Key_Expansion(), AES_set_encrypt_key(), AES_set_decrypt_key(), AES_GCM_encrypt(), AES_GCM_decrypt(), AES_XTS_encrypt(), and AES_XTS_decrypt().
* move key size check from to start of wc_AesSetKeyLocal().
* refactor pure-C AES setkey and cipher implementations to use aes->key_C_fallback when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor wc_AesSetKeyLocal() to set up both AESNI and pure-C expanded keys when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor all (haveAESNI && aes->use_aesni) conditions to just (aes->use_aesni).
* add macros VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, which do nothing but push a brace level when !defined(WC_AES_C_DYNAMIC_FALLBACK), but when defined(WC_AES_C_DYNAMIC_FALLBACK), they call SAVE_VECTOR_REGISTERS2() and on failure, temporarily clear aes->use_aesni and restore at _POP().
* refactor all invocations of SAVE_VECTOR_REGISTERS() and RESTORE_VECTOR_REGISTERS() to VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, except in wc_AesSetKeyLocal(), wc_AesXtsEncrypt(), and wc_AesXtsDecrypt(), which are refactored to use SAVE_VECTOR_REGISTERS2(), with graceful failure concealment if defined(WC_AES_C_DYNAMIC_FALLBACK).
* orthogonalize cleanup code in wc_AesCbcEncrypt(), wc_AesCcmEncrypt() and wc_AesCcmDecrypt().
* streamline fallthrough software definitions of wc_AesEncryptDirect() and wc_AesDecryptDirect(), and remove special-casing for defined(WOLFSSL_LINUXKM)&&defined(WOLFSSL_AESNI).
wolfcrypt/src/aes_asm.{S,asm}:
* remove errant "movl $10, 240(%rsi)" from AES_128_Key_Expansion_AESNI.
* add _AESNI suffixes/infixes to implementations that needed them.
wolfcrypt/src/{aes_gcm_asm.{S,asm},aes_xts_asm.S}: regenerate from revisions in scripts#357 -- adds _aesni suffixes to implementations that were missing them.
wolfssl/wolfcrypt/types.h: remove DEBUG_VECTOR_REGISTER_ACCESS macros, and add dummy fallthrough definitions for SAVE_VECTOR_REGISTERS2 and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
wolfssl/wolfcrypt/memory.h: adopt DEBUG_VECTOR_REGISTER_ACCESS code from types.h, and add definitions for WC_DEBUG_VECTOR_REGISTERS_RETVAL_INITVAL and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
linuxkm/linuxkm_wc_port.h: add arch-specific macro definitions for SAVE_VECTOR_REGISTERS2().
wolfcrypt/benchmark/benchmark.c: add missing gates around calls to RESTORE_VECTOR_REGISTERS().
configure.ac:
* cover various interdependencies in enable-all/enable-all-crypto, for better behavior in combination with --disable-aesgcm, --disable-ecc, --disable-ocsp, --disable-hmac, --disable-chacha, --disable-ed25519, and --disable-ed448.
* inhibit aesgcm_stream in enable-all/enable-all-crypto when ENABLED_LINUXKM_DEFAULTS, because it is currently incompatible with WC_AES_C_DYNAMIC_FALLBACK.
* add -DWC_AES_C_DYNAMIC_FALLBACK when ENABLED_LINUXKM_DEFAULTS.
* add 3 new interdependency checks: "ECCSI requires ECC.", "SAKKE requires ECC.", "WOLFSSH requires HMAC."
wolfcrypt/src/asn.c: tweak gating to accommodate defined(NO_RSA) && !defined(HAVE_ECC).
wolfcrypt/src/evp.c: tweak gating to accommodate defined(NO_HMAC).
wolfcrypt/src/logging.c: remove DEBUG_VECTOR_REGISTER_ACCESS code (moved to memory.c).
wolfcrypt/src/memory.c: change #include of settings.h to types.h; adopt DEBUG_VECTOR_REGISTER_ACCESS code from logging.c; add implementation of SAVE_VECTOR_REGISTERS2_fuzzer().
wolfcrypt/src/pwdbased.c: add explanatory #error scrypt requires HMAC.
wolfcrypt/test/test.c:
* add DEBUG_VECTOR_REGISTER_ACCESS clauses to aes_xts_128_test(), aesecb_test(), aesctr_test(), aes_test() CBC section, aes256_test() CBC section, and aesgcm_default_test_helper()
* remove duplicate wc_AesEcbDecrypt() in aesecb_test().
* add gating for pbkdf2_test().
* fix cleanup code in dsa_test().
* fix gating in pkcs7authenveloped_run_vectors() to accommodate !defined(HAVE_AESGCM).
* fix gating in cryptocb_test() to accommodate defined(NO_HMAC).
wolfssl/wolfcrypt/cryptocb.h: remove gates around "pk" sub-struct of struct wc_CryptoInfo -- wc_CryptoInfo.pk.type (an int) is used unconditionally when --enable-debug, and is used with DH.
wolfssl/wolfcrypt/error-crypt.h: fix whitespace.
2023-11-17 01:15:28 -06:00
|
|
|
|
|
|
|
|
#ifdef DEBUG_VECTOR_REGISTER_ACCESS_FUZZING
|
|
|
|
|
|
2024-02-10 01:09:15 -06:00
|
|
|
#ifdef HAVE_THREAD_LS
|
|
|
|
|
|
refactor AESNI implementations and *VECTOR_REGISTERS* macros to allow dynamic as-needed fallback to pure C, via WC_AES_C_DYNAMIC_FALLBACK.
wolfssl/wolfcrypt/aes.h: add key_C_fallback[] to struct Aes, and remove comment that "AESNI needs key first, rounds 2nd, not sure why yet" now that AES_128_Key_Expansion_AESNI no longer writes rounds after the expanded key.
wolfcrypt/src/aes.c:
* add _AESNI or _aesni suffixes/infixes to AESNI implementations that were missing them: AES_CBC_encrypt(), AES_CBC_decrypt_by*(), AES_ECB_encrypt(), AES_*_Key_Expansion(), AES_set_encrypt_key(), AES_set_decrypt_key(), AES_GCM_encrypt(), AES_GCM_decrypt(), AES_XTS_encrypt(), and AES_XTS_decrypt().
* move key size check from to start of wc_AesSetKeyLocal().
* refactor pure-C AES setkey and cipher implementations to use aes->key_C_fallback when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor wc_AesSetKeyLocal() to set up both AESNI and pure-C expanded keys when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor all (haveAESNI && aes->use_aesni) conditions to just (aes->use_aesni).
* add macros VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, which do nothing but push a brace level when !defined(WC_AES_C_DYNAMIC_FALLBACK), but when defined(WC_AES_C_DYNAMIC_FALLBACK), they call SAVE_VECTOR_REGISTERS2() and on failure, temporarily clear aes->use_aesni and restore at _POP().
* refactor all invocations of SAVE_VECTOR_REGISTERS() and RESTORE_VECTOR_REGISTERS() to VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, except in wc_AesSetKeyLocal(), wc_AesXtsEncrypt(), and wc_AesXtsDecrypt(), which are refactored to use SAVE_VECTOR_REGISTERS2(), with graceful failure concealment if defined(WC_AES_C_DYNAMIC_FALLBACK).
* orthogonalize cleanup code in wc_AesCbcEncrypt(), wc_AesCcmEncrypt() and wc_AesCcmDecrypt().
* streamline fallthrough software definitions of wc_AesEncryptDirect() and wc_AesDecryptDirect(), and remove special-casing for defined(WOLFSSL_LINUXKM)&&defined(WOLFSSL_AESNI).
wolfcrypt/src/aes_asm.{S,asm}:
* remove errant "movl $10, 240(%rsi)" from AES_128_Key_Expansion_AESNI.
* add _AESNI suffixes/infixes to implementations that needed them.
wolfcrypt/src/{aes_gcm_asm.{S,asm},aes_xts_asm.S}: regenerate from revisions in scripts#357 -- adds _aesni suffixes to implementations that were missing them.
wolfssl/wolfcrypt/types.h: remove DEBUG_VECTOR_REGISTER_ACCESS macros, and add dummy fallthrough definitions for SAVE_VECTOR_REGISTERS2 and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
wolfssl/wolfcrypt/memory.h: adopt DEBUG_VECTOR_REGISTER_ACCESS code from types.h, and add definitions for WC_DEBUG_VECTOR_REGISTERS_RETVAL_INITVAL and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
linuxkm/linuxkm_wc_port.h: add arch-specific macro definitions for SAVE_VECTOR_REGISTERS2().
wolfcrypt/benchmark/benchmark.c: add missing gates around calls to RESTORE_VECTOR_REGISTERS().
configure.ac:
* cover various interdependencies in enable-all/enable-all-crypto, for better behavior in combination with --disable-aesgcm, --disable-ecc, --disable-ocsp, --disable-hmac, --disable-chacha, --disable-ed25519, and --disable-ed448.
* inhibit aesgcm_stream in enable-all/enable-all-crypto when ENABLED_LINUXKM_DEFAULTS, because it is currently incompatible with WC_AES_C_DYNAMIC_FALLBACK.
* add -DWC_AES_C_DYNAMIC_FALLBACK when ENABLED_LINUXKM_DEFAULTS.
* add 3 new interdependency checks: "ECCSI requires ECC.", "SAKKE requires ECC.", "WOLFSSH requires HMAC."
wolfcrypt/src/asn.c: tweak gating to accommodate defined(NO_RSA) && !defined(HAVE_ECC).
wolfcrypt/src/evp.c: tweak gating to accommodate defined(NO_HMAC).
wolfcrypt/src/logging.c: remove DEBUG_VECTOR_REGISTER_ACCESS code (moved to memory.c).
wolfcrypt/src/memory.c: change #include of settings.h to types.h; adopt DEBUG_VECTOR_REGISTER_ACCESS code from logging.c; add implementation of SAVE_VECTOR_REGISTERS2_fuzzer().
wolfcrypt/src/pwdbased.c: add explanatory #error scrypt requires HMAC.
wolfcrypt/test/test.c:
* add DEBUG_VECTOR_REGISTER_ACCESS clauses to aes_xts_128_test(), aesecb_test(), aesctr_test(), aes_test() CBC section, aes256_test() CBC section, and aesgcm_default_test_helper()
* remove duplicate wc_AesEcbDecrypt() in aesecb_test().
* add gating for pbkdf2_test().
* fix cleanup code in dsa_test().
* fix gating in pkcs7authenveloped_run_vectors() to accommodate !defined(HAVE_AESGCM).
* fix gating in cryptocb_test() to accommodate defined(NO_HMAC).
wolfssl/wolfcrypt/cryptocb.h: remove gates around "pk" sub-struct of struct wc_CryptoInfo -- wc_CryptoInfo.pk.type (an int) is used unconditionally when --enable-debug, and is used with DH.
wolfssl/wolfcrypt/error-crypt.h: fix whitespace.
2023-11-17 01:15:28 -06:00
|
|
|
WOLFSSL_LOCAL int SAVE_VECTOR_REGISTERS2_fuzzer(void) {
|
|
|
|
|
static THREAD_LS_T struct drand48_data wc_svr_fuzzing_state;
|
|
|
|
|
static THREAD_LS_T int wc_svr_fuzzing_seeded = 0;
|
|
|
|
|
long result;
|
|
|
|
|
|
2024-02-10 01:09:15 -06:00
|
|
|
#ifdef DEBUG_VECTOR_REGISTER_ACCESS
|
refactor AESNI implementations and *VECTOR_REGISTERS* macros to allow dynamic as-needed fallback to pure C, via WC_AES_C_DYNAMIC_FALLBACK.
wolfssl/wolfcrypt/aes.h: add key_C_fallback[] to struct Aes, and remove comment that "AESNI needs key first, rounds 2nd, not sure why yet" now that AES_128_Key_Expansion_AESNI no longer writes rounds after the expanded key.
wolfcrypt/src/aes.c:
* add _AESNI or _aesni suffixes/infixes to AESNI implementations that were missing them: AES_CBC_encrypt(), AES_CBC_decrypt_by*(), AES_ECB_encrypt(), AES_*_Key_Expansion(), AES_set_encrypt_key(), AES_set_decrypt_key(), AES_GCM_encrypt(), AES_GCM_decrypt(), AES_XTS_encrypt(), and AES_XTS_decrypt().
* move key size check from to start of wc_AesSetKeyLocal().
* refactor pure-C AES setkey and cipher implementations to use aes->key_C_fallback when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor wc_AesSetKeyLocal() to set up both AESNI and pure-C expanded keys when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor all (haveAESNI && aes->use_aesni) conditions to just (aes->use_aesni).
* add macros VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, which do nothing but push a brace level when !defined(WC_AES_C_DYNAMIC_FALLBACK), but when defined(WC_AES_C_DYNAMIC_FALLBACK), they call SAVE_VECTOR_REGISTERS2() and on failure, temporarily clear aes->use_aesni and restore at _POP().
* refactor all invocations of SAVE_VECTOR_REGISTERS() and RESTORE_VECTOR_REGISTERS() to VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, except in wc_AesSetKeyLocal(), wc_AesXtsEncrypt(), and wc_AesXtsDecrypt(), which are refactored to use SAVE_VECTOR_REGISTERS2(), with graceful failure concealment if defined(WC_AES_C_DYNAMIC_FALLBACK).
* orthogonalize cleanup code in wc_AesCbcEncrypt(), wc_AesCcmEncrypt() and wc_AesCcmDecrypt().
* streamline fallthrough software definitions of wc_AesEncryptDirect() and wc_AesDecryptDirect(), and remove special-casing for defined(WOLFSSL_LINUXKM)&&defined(WOLFSSL_AESNI).
wolfcrypt/src/aes_asm.{S,asm}:
* remove errant "movl $10, 240(%rsi)" from AES_128_Key_Expansion_AESNI.
* add _AESNI suffixes/infixes to implementations that needed them.
wolfcrypt/src/{aes_gcm_asm.{S,asm},aes_xts_asm.S}: regenerate from revisions in scripts#357 -- adds _aesni suffixes to implementations that were missing them.
wolfssl/wolfcrypt/types.h: remove DEBUG_VECTOR_REGISTER_ACCESS macros, and add dummy fallthrough definitions for SAVE_VECTOR_REGISTERS2 and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
wolfssl/wolfcrypt/memory.h: adopt DEBUG_VECTOR_REGISTER_ACCESS code from types.h, and add definitions for WC_DEBUG_VECTOR_REGISTERS_RETVAL_INITVAL and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
linuxkm/linuxkm_wc_port.h: add arch-specific macro definitions for SAVE_VECTOR_REGISTERS2().
wolfcrypt/benchmark/benchmark.c: add missing gates around calls to RESTORE_VECTOR_REGISTERS().
configure.ac:
* cover various interdependencies in enable-all/enable-all-crypto, for better behavior in combination with --disable-aesgcm, --disable-ecc, --disable-ocsp, --disable-hmac, --disable-chacha, --disable-ed25519, and --disable-ed448.
* inhibit aesgcm_stream in enable-all/enable-all-crypto when ENABLED_LINUXKM_DEFAULTS, because it is currently incompatible with WC_AES_C_DYNAMIC_FALLBACK.
* add -DWC_AES_C_DYNAMIC_FALLBACK when ENABLED_LINUXKM_DEFAULTS.
* add 3 new interdependency checks: "ECCSI requires ECC.", "SAKKE requires ECC.", "WOLFSSH requires HMAC."
wolfcrypt/src/asn.c: tweak gating to accommodate defined(NO_RSA) && !defined(HAVE_ECC).
wolfcrypt/src/evp.c: tweak gating to accommodate defined(NO_HMAC).
wolfcrypt/src/logging.c: remove DEBUG_VECTOR_REGISTER_ACCESS code (moved to memory.c).
wolfcrypt/src/memory.c: change #include of settings.h to types.h; adopt DEBUG_VECTOR_REGISTER_ACCESS code from logging.c; add implementation of SAVE_VECTOR_REGISTERS2_fuzzer().
wolfcrypt/src/pwdbased.c: add explanatory #error scrypt requires HMAC.
wolfcrypt/test/test.c:
* add DEBUG_VECTOR_REGISTER_ACCESS clauses to aes_xts_128_test(), aesecb_test(), aesctr_test(), aes_test() CBC section, aes256_test() CBC section, and aesgcm_default_test_helper()
* remove duplicate wc_AesEcbDecrypt() in aesecb_test().
* add gating for pbkdf2_test().
* fix cleanup code in dsa_test().
* fix gating in pkcs7authenveloped_run_vectors() to accommodate !defined(HAVE_AESGCM).
* fix gating in cryptocb_test() to accommodate defined(NO_HMAC).
wolfssl/wolfcrypt/cryptocb.h: remove gates around "pk" sub-struct of struct wc_CryptoInfo -- wc_CryptoInfo.pk.type (an int) is used unconditionally when --enable-debug, and is used with DH.
wolfssl/wolfcrypt/error-crypt.h: fix whitespace.
2023-11-17 01:15:28 -06:00
|
|
|
if (wc_debug_vector_registers_retval)
|
|
|
|
|
return wc_debug_vector_registers_retval;
|
2024-02-10 01:09:15 -06:00
|
|
|
#endif
|
refactor AESNI implementations and *VECTOR_REGISTERS* macros to allow dynamic as-needed fallback to pure C, via WC_AES_C_DYNAMIC_FALLBACK.
wolfssl/wolfcrypt/aes.h: add key_C_fallback[] to struct Aes, and remove comment that "AESNI needs key first, rounds 2nd, not sure why yet" now that AES_128_Key_Expansion_AESNI no longer writes rounds after the expanded key.
wolfcrypt/src/aes.c:
* add _AESNI or _aesni suffixes/infixes to AESNI implementations that were missing them: AES_CBC_encrypt(), AES_CBC_decrypt_by*(), AES_ECB_encrypt(), AES_*_Key_Expansion(), AES_set_encrypt_key(), AES_set_decrypt_key(), AES_GCM_encrypt(), AES_GCM_decrypt(), AES_XTS_encrypt(), and AES_XTS_decrypt().
* move key size check from to start of wc_AesSetKeyLocal().
* refactor pure-C AES setkey and cipher implementations to use aes->key_C_fallback when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor wc_AesSetKeyLocal() to set up both AESNI and pure-C expanded keys when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor all (haveAESNI && aes->use_aesni) conditions to just (aes->use_aesni).
* add macros VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, which do nothing but push a brace level when !defined(WC_AES_C_DYNAMIC_FALLBACK), but when defined(WC_AES_C_DYNAMIC_FALLBACK), they call SAVE_VECTOR_REGISTERS2() and on failure, temporarily clear aes->use_aesni and restore at _POP().
* refactor all invocations of SAVE_VECTOR_REGISTERS() and RESTORE_VECTOR_REGISTERS() to VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, except in wc_AesSetKeyLocal(), wc_AesXtsEncrypt(), and wc_AesXtsDecrypt(), which are refactored to use SAVE_VECTOR_REGISTERS2(), with graceful failure concealment if defined(WC_AES_C_DYNAMIC_FALLBACK).
* orthogonalize cleanup code in wc_AesCbcEncrypt(), wc_AesCcmEncrypt() and wc_AesCcmDecrypt().
* streamline fallthrough software definitions of wc_AesEncryptDirect() and wc_AesDecryptDirect(), and remove special-casing for defined(WOLFSSL_LINUXKM)&&defined(WOLFSSL_AESNI).
wolfcrypt/src/aes_asm.{S,asm}:
* remove errant "movl $10, 240(%rsi)" from AES_128_Key_Expansion_AESNI.
* add _AESNI suffixes/infixes to implementations that needed them.
wolfcrypt/src/{aes_gcm_asm.{S,asm},aes_xts_asm.S}: regenerate from revisions in scripts#357 -- adds _aesni suffixes to implementations that were missing them.
wolfssl/wolfcrypt/types.h: remove DEBUG_VECTOR_REGISTER_ACCESS macros, and add dummy fallthrough definitions for SAVE_VECTOR_REGISTERS2 and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
wolfssl/wolfcrypt/memory.h: adopt DEBUG_VECTOR_REGISTER_ACCESS code from types.h, and add definitions for WC_DEBUG_VECTOR_REGISTERS_RETVAL_INITVAL and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
linuxkm/linuxkm_wc_port.h: add arch-specific macro definitions for SAVE_VECTOR_REGISTERS2().
wolfcrypt/benchmark/benchmark.c: add missing gates around calls to RESTORE_VECTOR_REGISTERS().
configure.ac:
* cover various interdependencies in enable-all/enable-all-crypto, for better behavior in combination with --disable-aesgcm, --disable-ecc, --disable-ocsp, --disable-hmac, --disable-chacha, --disable-ed25519, and --disable-ed448.
* inhibit aesgcm_stream in enable-all/enable-all-crypto when ENABLED_LINUXKM_DEFAULTS, because it is currently incompatible with WC_AES_C_DYNAMIC_FALLBACK.
* add -DWC_AES_C_DYNAMIC_FALLBACK when ENABLED_LINUXKM_DEFAULTS.
* add 3 new interdependency checks: "ECCSI requires ECC.", "SAKKE requires ECC.", "WOLFSSH requires HMAC."
wolfcrypt/src/asn.c: tweak gating to accommodate defined(NO_RSA) && !defined(HAVE_ECC).
wolfcrypt/src/evp.c: tweak gating to accommodate defined(NO_HMAC).
wolfcrypt/src/logging.c: remove DEBUG_VECTOR_REGISTER_ACCESS code (moved to memory.c).
wolfcrypt/src/memory.c: change #include of settings.h to types.h; adopt DEBUG_VECTOR_REGISTER_ACCESS code from logging.c; add implementation of SAVE_VECTOR_REGISTERS2_fuzzer().
wolfcrypt/src/pwdbased.c: add explanatory #error scrypt requires HMAC.
wolfcrypt/test/test.c:
* add DEBUG_VECTOR_REGISTER_ACCESS clauses to aes_xts_128_test(), aesecb_test(), aesctr_test(), aes_test() CBC section, aes256_test() CBC section, and aesgcm_default_test_helper()
* remove duplicate wc_AesEcbDecrypt() in aesecb_test().
* add gating for pbkdf2_test().
* fix cleanup code in dsa_test().
* fix gating in pkcs7authenveloped_run_vectors() to accommodate !defined(HAVE_AESGCM).
* fix gating in cryptocb_test() to accommodate defined(NO_HMAC).
wolfssl/wolfcrypt/cryptocb.h: remove gates around "pk" sub-struct of struct wc_CryptoInfo -- wc_CryptoInfo.pk.type (an int) is used unconditionally when --enable-debug, and is used with DH.
wolfssl/wolfcrypt/error-crypt.h: fix whitespace.
2023-11-17 01:15:28 -06:00
|
|
|
|
|
|
|
|
if (wc_svr_fuzzing_seeded == 0) {
|
|
|
|
|
long seed = WC_DEBUG_VECTOR_REGISTERS_FUZZING_SEED;
|
|
|
|
|
char *seed_envstr = getenv("WC_DEBUG_VECTOR_REGISTERS_FUZZING_SEED");
|
|
|
|
|
if (seed_envstr)
|
|
|
|
|
seed = strtol(seed_envstr, NULL, 0);
|
|
|
|
|
(void)srand48_r(seed, &wc_svr_fuzzing_state);
|
|
|
|
|
wc_svr_fuzzing_seeded = 1;
|
|
|
|
|
}
|
|
|
|
|
(void)lrand48_r(&wc_svr_fuzzing_state, &result);
|
|
|
|
|
if (result & 1)
|
|
|
|
|
return IO_FAILED_E;
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-10 01:09:15 -06:00
|
|
|
#else /* !HAVE_THREAD_LS */
|
2024-01-26 14:07:58 -06:00
|
|
|
|
2024-02-10 01:09:15 -06:00
|
|
|
/* alternate implementation useful for testing in the kernel module build, where
|
|
|
|
|
* glibc and thread-local storage are unavailable.
|
linuxkm: completion and stabilization of LKCAPI integration for AES-CBC, AES-CFB, AES-GCM, and AES-XTS:
linuxkm/lkcapi_glue.c (added in earlier commit):
implement linuxkm_lkcapi_register() and linuxkm_lkcapi_unregister() with idempotency.
add AES-XTS algorithm glue and self-test implementations.
add per-algorithm gating: LINUXKM_LKCAPI_REGISTER_AESCBC, _AESCFB, _AESGCM, and _AESXTS.
carry forward philljj's implementations for AES-CBC, AES-CFB, and AES-GCM, with various cleanups.
linuxkm/module_hooks.c:
print the "wolfCrypt container hashes" message only if DEBUG_LINUXKM_PIE_SUPPORT is set.
render the FIPS version for the self-test success message using the HAVE_FIPS_VERSION* macros.
add a "skipping full wolfcrypt_test() ..." message for --disable-crypttests builds.
add CONFIG_FORTIFY_SOURCE gates.
configure.ac:
add support for --enable-linuxkm-lkcapi-register;
add AES-XTS to output config summary;
rename --enable-xts to --enable-aesxts (retaining old option for backward compatibility).
linuxkm/linuxkm_wc_port.h: add support for CONFIG_FORTIFY_SOURCE.
linuxkm/linuxkm_memory.c:
fix retvals in save_vector_registers_x86() (wc-style MEMORY_E, not sys-style ENOMEM).
add __my_fortify_panic() implementation.
linuxkm/Kbuild: for ENABLED_LINUXKM_PIE in rename-pie-text-and-data-sections recipe, create an .rodata.wolfcrypt section.
linuxkm/include.am: add linuxkm/lkcapi_glue.c to EXTRA_DIST.
wolfcrypt/test/test.c:
when defined(HAVE_FIPS_VERSION), inhibit a test clause in aes_xts_128_test() disallowed by FIPS ("FIPS AES-XTS main and tweak keys must differ").
fix out-of-order user message in ecc_test().
2024-01-26 20:01:19 -06:00
|
|
|
*
|
|
|
|
|
* note this is not a well-behaved PRNG, but is adequate for fuzzing purposes.
|
|
|
|
|
* the prn sequence is incompressible according to ent and xz, and does not
|
|
|
|
|
* cycle within 10M iterations with various seeds including zero, but the Chi
|
|
|
|
|
* square distribution is poor, and the unconditioned lsb bit balance is ~54%
|
|
|
|
|
* regardless of seed.
|
2024-02-10 01:09:15 -06:00
|
|
|
*
|
|
|
|
|
* deterministic only if access is single-threaded, but never degenerate.
|
2024-01-26 14:07:58 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
WOLFSSL_LOCAL int SAVE_VECTOR_REGISTERS2_fuzzer(void) {
|
|
|
|
|
static unsigned long prn = WC_DEBUG_VECTOR_REGISTERS_FUZZING_SEED;
|
linuxkm: completion and stabilization of LKCAPI integration for AES-CBC, AES-CFB, AES-GCM, and AES-XTS:
linuxkm/lkcapi_glue.c (added in earlier commit):
implement linuxkm_lkcapi_register() and linuxkm_lkcapi_unregister() with idempotency.
add AES-XTS algorithm glue and self-test implementations.
add per-algorithm gating: LINUXKM_LKCAPI_REGISTER_AESCBC, _AESCFB, _AESGCM, and _AESXTS.
carry forward philljj's implementations for AES-CBC, AES-CFB, and AES-GCM, with various cleanups.
linuxkm/module_hooks.c:
print the "wolfCrypt container hashes" message only if DEBUG_LINUXKM_PIE_SUPPORT is set.
render the FIPS version for the self-test success message using the HAVE_FIPS_VERSION* macros.
add a "skipping full wolfcrypt_test() ..." message for --disable-crypttests builds.
add CONFIG_FORTIFY_SOURCE gates.
configure.ac:
add support for --enable-linuxkm-lkcapi-register;
add AES-XTS to output config summary;
rename --enable-xts to --enable-aesxts (retaining old option for backward compatibility).
linuxkm/linuxkm_wc_port.h: add support for CONFIG_FORTIFY_SOURCE.
linuxkm/linuxkm_memory.c:
fix retvals in save_vector_registers_x86() (wc-style MEMORY_E, not sys-style ENOMEM).
add __my_fortify_panic() implementation.
linuxkm/Kbuild: for ENABLED_LINUXKM_PIE in rename-pie-text-and-data-sections recipe, create an .rodata.wolfcrypt section.
linuxkm/include.am: add linuxkm/lkcapi_glue.c to EXTRA_DIST.
wolfcrypt/test/test.c:
when defined(HAVE_FIPS_VERSION), inhibit a test clause in aes_xts_128_test() disallowed by FIPS ("FIPS AES-XTS main and tweak keys must differ").
fix out-of-order user message in ecc_test().
2024-01-26 20:01:19 -06:00
|
|
|
static int balance_bit = 0;
|
|
|
|
|
unsigned long new_prn = prn ^ 0xba86943da66ee701ul; /* note this magic
|
|
|
|
|
* random number is
|
|
|
|
|
* bit-balanced.
|
|
|
|
|
*/
|
2024-02-10 01:09:15 -06:00
|
|
|
|
|
|
|
|
#ifdef DEBUG_VECTOR_REGISTER_ACCESS
|
|
|
|
|
if (wc_debug_vector_registers_retval)
|
|
|
|
|
return wc_debug_vector_registers_retval;
|
|
|
|
|
#endif
|
|
|
|
|
|
linuxkm: completion and stabilization of LKCAPI integration for AES-CBC, AES-CFB, AES-GCM, and AES-XTS:
linuxkm/lkcapi_glue.c (added in earlier commit):
implement linuxkm_lkcapi_register() and linuxkm_lkcapi_unregister() with idempotency.
add AES-XTS algorithm glue and self-test implementations.
add per-algorithm gating: LINUXKM_LKCAPI_REGISTER_AESCBC, _AESCFB, _AESGCM, and _AESXTS.
carry forward philljj's implementations for AES-CBC, AES-CFB, and AES-GCM, with various cleanups.
linuxkm/module_hooks.c:
print the "wolfCrypt container hashes" message only if DEBUG_LINUXKM_PIE_SUPPORT is set.
render the FIPS version for the self-test success message using the HAVE_FIPS_VERSION* macros.
add a "skipping full wolfcrypt_test() ..." message for --disable-crypttests builds.
add CONFIG_FORTIFY_SOURCE gates.
configure.ac:
add support for --enable-linuxkm-lkcapi-register;
add AES-XTS to output config summary;
rename --enable-xts to --enable-aesxts (retaining old option for backward compatibility).
linuxkm/linuxkm_wc_port.h: add support for CONFIG_FORTIFY_SOURCE.
linuxkm/linuxkm_memory.c:
fix retvals in save_vector_registers_x86() (wc-style MEMORY_E, not sys-style ENOMEM).
add __my_fortify_panic() implementation.
linuxkm/Kbuild: for ENABLED_LINUXKM_PIE in rename-pie-text-and-data-sections recipe, create an .rodata.wolfcrypt section.
linuxkm/include.am: add linuxkm/lkcapi_glue.c to EXTRA_DIST.
wolfcrypt/test/test.c:
when defined(HAVE_FIPS_VERSION), inhibit a test clause in aes_xts_128_test() disallowed by FIPS ("FIPS AES-XTS main and tweak keys must differ").
fix out-of-order user message in ecc_test().
2024-01-26 20:01:19 -06:00
|
|
|
/* barrel-roll using the bottom 6 bits. */
|
2024-01-26 14:07:58 -06:00
|
|
|
if (new_prn & 0x3f)
|
2024-01-29 17:48:31 -06:00
|
|
|
new_prn = (new_prn << (new_prn & 0x3f)) |
|
|
|
|
|
(new_prn >> (0x40 - (new_prn & 0x3f)));
|
2024-01-26 14:07:58 -06:00
|
|
|
prn = new_prn;
|
|
|
|
|
|
linuxkm: completion and stabilization of LKCAPI integration for AES-CBC, AES-CFB, AES-GCM, and AES-XTS:
linuxkm/lkcapi_glue.c (added in earlier commit):
implement linuxkm_lkcapi_register() and linuxkm_lkcapi_unregister() with idempotency.
add AES-XTS algorithm glue and self-test implementations.
add per-algorithm gating: LINUXKM_LKCAPI_REGISTER_AESCBC, _AESCFB, _AESGCM, and _AESXTS.
carry forward philljj's implementations for AES-CBC, AES-CFB, and AES-GCM, with various cleanups.
linuxkm/module_hooks.c:
print the "wolfCrypt container hashes" message only if DEBUG_LINUXKM_PIE_SUPPORT is set.
render the FIPS version for the self-test success message using the HAVE_FIPS_VERSION* macros.
add a "skipping full wolfcrypt_test() ..." message for --disable-crypttests builds.
add CONFIG_FORTIFY_SOURCE gates.
configure.ac:
add support for --enable-linuxkm-lkcapi-register;
add AES-XTS to output config summary;
rename --enable-xts to --enable-aesxts (retaining old option for backward compatibility).
linuxkm/linuxkm_wc_port.h: add support for CONFIG_FORTIFY_SOURCE.
linuxkm/linuxkm_memory.c:
fix retvals in save_vector_registers_x86() (wc-style MEMORY_E, not sys-style ENOMEM).
add __my_fortify_panic() implementation.
linuxkm/Kbuild: for ENABLED_LINUXKM_PIE in rename-pie-text-and-data-sections recipe, create an .rodata.wolfcrypt section.
linuxkm/include.am: add linuxkm/lkcapi_glue.c to EXTRA_DIST.
wolfcrypt/test/test.c:
when defined(HAVE_FIPS_VERSION), inhibit a test clause in aes_xts_128_test() disallowed by FIPS ("FIPS AES-XTS main and tweak keys must differ").
fix out-of-order user message in ecc_test().
2024-01-26 20:01:19 -06:00
|
|
|
balance_bit = !balance_bit;
|
|
|
|
|
|
2024-01-29 17:48:31 -06:00
|
|
|
return ((prn & 1) ^ balance_bit) ? IO_FAILED_E : 0;
|
2024-01-26 14:07:58 -06:00
|
|
|
}
|
|
|
|
|
|
2024-02-10 01:09:15 -06:00
|
|
|
#endif /* !HAVE_THREAD_LS */
|
|
|
|
|
|
|
|
|
|
#endif /* DEBUG_VECTOR_REGISTER_ACCESS_FUZZING */
|
refactor AESNI implementations and *VECTOR_REGISTERS* macros to allow dynamic as-needed fallback to pure C, via WC_AES_C_DYNAMIC_FALLBACK.
wolfssl/wolfcrypt/aes.h: add key_C_fallback[] to struct Aes, and remove comment that "AESNI needs key first, rounds 2nd, not sure why yet" now that AES_128_Key_Expansion_AESNI no longer writes rounds after the expanded key.
wolfcrypt/src/aes.c:
* add _AESNI or _aesni suffixes/infixes to AESNI implementations that were missing them: AES_CBC_encrypt(), AES_CBC_decrypt_by*(), AES_ECB_encrypt(), AES_*_Key_Expansion(), AES_set_encrypt_key(), AES_set_decrypt_key(), AES_GCM_encrypt(), AES_GCM_decrypt(), AES_XTS_encrypt(), and AES_XTS_decrypt().
* move key size check from to start of wc_AesSetKeyLocal().
* refactor pure-C AES setkey and cipher implementations to use aes->key_C_fallback when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor wc_AesSetKeyLocal() to set up both AESNI and pure-C expanded keys when defined(WC_AES_C_DYNAMIC_FALLBACK).
* refactor all (haveAESNI && aes->use_aesni) conditions to just (aes->use_aesni).
* add macros VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, which do nothing but push a brace level when !defined(WC_AES_C_DYNAMIC_FALLBACK), but when defined(WC_AES_C_DYNAMIC_FALLBACK), they call SAVE_VECTOR_REGISTERS2() and on failure, temporarily clear aes->use_aesni and restore at _POP().
* refactor all invocations of SAVE_VECTOR_REGISTERS() and RESTORE_VECTOR_REGISTERS() to VECTOR_REGISTERS_PUSH and VECTOR_REGISTERS_POP, except in wc_AesSetKeyLocal(), wc_AesXtsEncrypt(), and wc_AesXtsDecrypt(), which are refactored to use SAVE_VECTOR_REGISTERS2(), with graceful failure concealment if defined(WC_AES_C_DYNAMIC_FALLBACK).
* orthogonalize cleanup code in wc_AesCbcEncrypt(), wc_AesCcmEncrypt() and wc_AesCcmDecrypt().
* streamline fallthrough software definitions of wc_AesEncryptDirect() and wc_AesDecryptDirect(), and remove special-casing for defined(WOLFSSL_LINUXKM)&&defined(WOLFSSL_AESNI).
wolfcrypt/src/aes_asm.{S,asm}:
* remove errant "movl $10, 240(%rsi)" from AES_128_Key_Expansion_AESNI.
* add _AESNI suffixes/infixes to implementations that needed them.
wolfcrypt/src/{aes_gcm_asm.{S,asm},aes_xts_asm.S}: regenerate from revisions in scripts#357 -- adds _aesni suffixes to implementations that were missing them.
wolfssl/wolfcrypt/types.h: remove DEBUG_VECTOR_REGISTER_ACCESS macros, and add dummy fallthrough definitions for SAVE_VECTOR_REGISTERS2 and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
wolfssl/wolfcrypt/memory.h: adopt DEBUG_VECTOR_REGISTER_ACCESS code from types.h, and add definitions for WC_DEBUG_VECTOR_REGISTERS_RETVAL_INITVAL and WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL.
linuxkm/linuxkm_wc_port.h: add arch-specific macro definitions for SAVE_VECTOR_REGISTERS2().
wolfcrypt/benchmark/benchmark.c: add missing gates around calls to RESTORE_VECTOR_REGISTERS().
configure.ac:
* cover various interdependencies in enable-all/enable-all-crypto, for better behavior in combination with --disable-aesgcm, --disable-ecc, --disable-ocsp, --disable-hmac, --disable-chacha, --disable-ed25519, and --disable-ed448.
* inhibit aesgcm_stream in enable-all/enable-all-crypto when ENABLED_LINUXKM_DEFAULTS, because it is currently incompatible with WC_AES_C_DYNAMIC_FALLBACK.
* add -DWC_AES_C_DYNAMIC_FALLBACK when ENABLED_LINUXKM_DEFAULTS.
* add 3 new interdependency checks: "ECCSI requires ECC.", "SAKKE requires ECC.", "WOLFSSH requires HMAC."
wolfcrypt/src/asn.c: tweak gating to accommodate defined(NO_RSA) && !defined(HAVE_ECC).
wolfcrypt/src/evp.c: tweak gating to accommodate defined(NO_HMAC).
wolfcrypt/src/logging.c: remove DEBUG_VECTOR_REGISTER_ACCESS code (moved to memory.c).
wolfcrypt/src/memory.c: change #include of settings.h to types.h; adopt DEBUG_VECTOR_REGISTER_ACCESS code from logging.c; add implementation of SAVE_VECTOR_REGISTERS2_fuzzer().
wolfcrypt/src/pwdbased.c: add explanatory #error scrypt requires HMAC.
wolfcrypt/test/test.c:
* add DEBUG_VECTOR_REGISTER_ACCESS clauses to aes_xts_128_test(), aesecb_test(), aesctr_test(), aes_test() CBC section, aes256_test() CBC section, and aesgcm_default_test_helper()
* remove duplicate wc_AesEcbDecrypt() in aesecb_test().
* add gating for pbkdf2_test().
* fix cleanup code in dsa_test().
* fix gating in pkcs7authenveloped_run_vectors() to accommodate !defined(HAVE_AESGCM).
* fix gating in cryptocb_test() to accommodate defined(NO_HMAC).
wolfssl/wolfcrypt/cryptocb.h: remove gates around "pk" sub-struct of struct wc_CryptoInfo -- wc_CryptoInfo.pk.type (an int) is used unconditionally when --enable-debug, and is used with DH.
wolfssl/wolfcrypt/error-crypt.h: fix whitespace.
2023-11-17 01:15:28 -06:00
|
|
|
|
2022-01-07 22:39:38 -06:00
|
|
|
#ifdef WOLFSSL_LINUXKM
|
|
|
|
|
#include "../../linuxkm/linuxkm_memory.c"
|
|
|
|
|
#endif
|