Skip to content

Commit 44e0ee1

Browse files
committed
wolfssl/wolfcrypt/types.h:
* fix overallocation in WC_DECLARE_ARRAY() macro in the !WOLFSSL_SMALL_STACK path. * rename WC_INIT_ARRAY() to WC_ALLOC_ARRAY() for clarity (it doesn't initialize any memory). * rename WC_DECLARE_ARRAY_DYNAMIC_DEC(), WC_DECLARE_ARRAY_DYNAMIC_EXE(), and WC_FREE_ARRAY_DYNAMIC() to WC_DECLARE_HEAP_ARRAY(), WC_ALLOC_HEAP_ARRAY(), and WC_FREE_HEAP_ARRAY(), respectively, also for clarity, and refactor out the duplicate definitions. * add WC_ALLOC_VAR(), and move the XMALLOC() in smallstack WC_DECLARE_VAR() into it. smallstack WC_DECLARE_VAR() now initializes the pointer to NULL, like smallstack WC_DECLARE_ARRAY(), assuring all pointers are valid upon shortcircuit to cleanup for a failed allocation (see WC_ALLOC_DO_ON_FAILURE below). * add a new hook "WC_ALLOC_DO_ON_FAILURE" in WC_ALLOC_VAR(), WC_ALLOC_ARRAY(), and WC_DECLARE_ARRAY_DYNAMIC_EXE(), which is invoked when an allocation fails. by default the hook is defined to WC_DO_NOTHING. * add basic safety to WC_*_HEAP_ARRAY() by recording/detecting allocation state via idx##VAR_NAME. * add macros WC_ARRAY_OK() and WC_HEAP_ARRAY_OK() to test if allocation succeeded. * add macros WC_CALLOC_ARRAY() and WC_CALLOC_HEAP_ARRAY() which zero the objects. * add macro WC_CALLOC_VAR() which zeros the object. ED448: smallstack refactor of ge448_scalarmult_base(). src/tls.c tests/api.c wolfcrypt/test/test.c: update WC_DECLARE_VAR()s with now-required matching WC_ALLOC_VAR()s. wolfcrypt/benchmark/benchmark.c: * no functional changes in default error-free behavior. * add definition of WC_ALLOC_DO_ON_FAILURE() that prints error message, sets ret, and does goto exit. * add BENCH_NTIMES and BENCH_AGREETIMES overrideeable macros, to allow fast sanitizer runs and slow high-precision runs. * smallstack refactor of all declarations of stack arrays of the form foo[BENCH_MAX_PENDING], using WC_DECLARE_ARRAY() (35 in all). * additional smallstack refactors, using WC_DECLARE_VAR(), for bench_aesxts(), bench_ed448KeyGen(), bench_eccsi*(), and bench_sakke*(). * fixes for various unhandled error conditions around malloc failures. wolfcrypt/test/test.c: opportunistically constify several (42) static constants, moving them to the readonly data segment. linuxkm/Makefile: if ENABLED_LINUXKM_BENCHMARKS, add wolfcrypt/benchmark/benchmark.o to WOLFSSL_OBJ_FILES. linuxkm/Kbuild: enable FPU for benchmark.o, and remove enablement for module_hooks.o. linuxkm/module_hooks.c: remove inline include of benchmark.c.
1 parent 92b8196 commit 44e0ee1

12 files changed

Lines changed: 1169 additions & 923 deletions

File tree

linuxkm/Kbuild

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,8 @@ ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
9999
$(obj)/linuxkm/module_hooks.o: ccflags-y += $(PIE_SUPPORT_FLAGS)
100100
endif
101101

102-
ifeq "$(ENABLED_LINUXKM_BENCHMARKS)" "yes"
103-
$(obj)/linuxkm/module_hooks.o: ccflags-y = $(WOLFSSL_CFLAGS) $(CFLAGS_FPU_ENABLE) $(CFLAGS_SIMD_ENABLE) $(PIE_SUPPORT_FLAGS)
104-
$(obj)/linuxkm/module_hooks.o: asflags-y = $(WOLFSSL_ASFLAGS) $(ASFLAGS_FPU_ENABLE_SIMD_DISABLE)
105-
endif
102+
$(obj)/wolfcrypt/benchmark/benchmark.o: ccflags-y = $(WOLFSSL_CFLAGS) $(CFLAGS_FPU_ENABLE) $(CFLAGS_SIMD_ENABLE) $(PIE_SUPPORT_FLAGS) -DNO_MAIN_FUNCTION
103+
$(obj)/wolfcrypt/benchmark/benchmark.o: asflags-y = $(WOLFSSL_ASFLAGS) $(ASFLAGS_FPU_ENABLE_SIMD_DISABLE)
106104

107105
asflags-y := $(WOLFSSL_ASFLAGS) $(ASFLAGS_FPUSIMD_DISABLE)
108106

linuxkm/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ else
4747
WOLFSSL_CFLAGS+=-DNO_CRYPT_TEST
4848
endif
4949

50+
ifeq "$(ENABLED_LINUXKM_BENCHMARKS)" "yes"
51+
WOLFSSL_OBJ_FILES+=wolfcrypt/benchmark/benchmark.o
52+
endif
53+
5054
ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
5155
WOLFCRYPT_PIE_FILES := linuxkm/pie_first.o $(filter wolfcrypt/src/%,$(WOLFSSL_OBJ_FILES)) linuxkm/pie_redirect_table.o linuxkm/pie_last.o
5256
WOLFSSL_OBJ_FILES := $(WOLFCRYPT_PIE_FILES) $(filter-out $(WOLFCRYPT_PIE_FILES),$(WOLFSSL_OBJ_FILES))

linuxkm/module_hooks.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,7 @@ static int updateFipsHash(void);
121121
#endif
122122

123123
#ifdef WOLFSSL_LINUXKM_BENCHMARKS
124-
#define STRING_USER
125-
#define NO_MAIN_FUNCTION
126-
#define current_time benchmark_current_time
127-
#define WOLFSSL_NO_FLOAT_FMT
128-
#include "wolfcrypt/benchmark/benchmark.c"
124+
extern int wolfcrypt_benchmark_main(int argc, char** argv);
129125
#endif /* WOLFSSL_LINUXKM_BENCHMARKS */
130126

131127
#ifdef LINUXKM_LKCAPI_REGISTER

src/tls.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ int BuildTlsFinished(WOLFSSL* ssl, Hashes* hashes, const byte* sender)
182182
byte handshake_hash[HSHASH_SZ];
183183
#else
184184
WC_DECLARE_VAR(handshake_hash, byte, HSHASH_SZ, ssl->heap);
185+
WC_ALLOC_VAR(handshake_hash, byte, HSHASH_SZ, ssl->heap);
185186
if (handshake_hash == NULL)
186187
return MEMORY_E;
187188
#endif
@@ -317,6 +318,7 @@ static int _DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
317318
int ret;
318319
#if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
319320
WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
321+
WC_ALLOC_VAR(seed, byte, SEED_LEN, heap);
320322
if (seed == NULL)
321323
return MEMORY_E;
322324
#else
@@ -422,6 +424,7 @@ static int _MakeTlsMasterSecret(byte* ms, word32 msLen,
422424
byte seed[SEED_LEN];
423425
#else
424426
WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
427+
WC_ALLOC_VAR(seed, byte, SEED_LEN, heap);
425428
if (seed == NULL)
426429
return MEMORY_E;
427430
#endif

tests/api.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19754,6 +19754,10 @@ static int test_wc_RsaPublicEncryptDecrypt(void)
1975419754
WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
1975519755
WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
1975619756

19757+
WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
19758+
WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
19759+
WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
19760+
1975719761
#ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
1975819762
ExpectNotNull(in);
1975919763
ExpectNotNull(plain);
@@ -19816,6 +19820,10 @@ static int test_wc_RsaPublicEncryptDecrypt_ex(void)
1981619820
WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
1981719821
WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
1981819822

19823+
WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
19824+
WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
19825+
WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
19826+
1981919827
#ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
1982019828
ExpectNotNull(in);
1982119829
ExpectNotNull(plain);
@@ -19881,6 +19889,10 @@ static int test_wc_RsaSSL_SignVerify(void)
1988119889
WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
1988219890
WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
1988319891

19892+
WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
19893+
WC_ALLOC_VAR(out, byte, TEST_RSA_BYTES, NULL);
19894+
WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
19895+
1988419896
#ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
1988519897
ExpectNotNull(in);
1988619898
ExpectNotNull(out);

0 commit comments

Comments
 (0)