Skip to content

Commit 7b2733a

Browse files
committed
Introduce wolfCrypt_Init warmup, NO_WOLFSSL_WARMUP
1 parent d4f8c9c commit 7b2733a

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ NO_WOLFSSL_SHA512_INTERLEAVE
437437
NO_WOLFSSL_SKIP_TRAILING_PAD
438438
NO_WOLFSSL_SMALL_STACK_STATIC
439439
NO_WOLFSSL_USE_ASM_CERT
440+
NO_WOLFSSL_WARMUP
440441
NO_WOLFSSL_XILINX_TAG_MALLOC
441442
NRF52
442443
NRF52_SERIES

wolfcrypt/src/wc_port.c

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@
150150
#endif
151151
#endif
152152

153+
#if !defined(NO_AES) && !defined(WC_NO_AES)
154+
#include <wolfssl/wolfcrypt/aes.h>
155+
#endif
156+
153157
/* prevent multiple mutex initializations */
154158
static volatile int initRefCount = 0;
155159

@@ -167,6 +171,53 @@ int wolfCrypt_Init(void)
167171
if (initRefCount == 0) {
168172
WOLFSSL_ENTER("wolfCrypt_Init");
169173

174+
/* */
175+
#ifndef NO_WOLFSSL_WARMUP
176+
/* Warm up the hardware to allocate any heap semaphore early */
177+
178+
WC_RNG rng;
179+
byte dummy;
180+
181+
#if !defined(NO_AES) && !defined(WC_NO_AES)
182+
Aes aes;
183+
unsigned char key16[16];
184+
unsigned char iv[12];
185+
unsigned char in[16];
186+
unsigned char out[16];
187+
unsigned char tag[16];
188+
int devId;
189+
#endif
190+
191+
#if defined(DEBUG_WOLFSSL_MALLOC_VERBOSE)
192+
WOLFSSL_MSG("Warming up RNG");
193+
#endif
194+
195+
if (wc_InitRng(&rng) == 0) {
196+
(void)wc_RNG_GenerateBlock(&rng, &dummy, 1); /* forces Hash_DRBG/SHA */
197+
wc_FreeRng(&rng);
198+
}
199+
200+
#if !defined(NO_AES) && !defined(WC_NO_AES)
201+
#if defined(DEBUG_WOLFSSL_MALLOC_VERBOSE)
202+
WOLFSSL_MSG("Warming up RNG");
203+
#endif
204+
memset(key16, 0, sizeof(key16));
205+
memset(iv, 0, sizeof(iv));
206+
memset(in, 0, sizeof(in));
207+
devId = INVALID_DEVID;
208+
209+
ret = wc_AesInit(&aes, NULL, devId);
210+
if (ret != 0) return ret;
211+
ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16));
212+
if (ret != 0) { wc_AesFree(&aes); return ret; }
213+
ret = wc_AesGcmEncrypt(&aes, out, in, (word32)sizeof(in),
214+
iv, (word32)sizeof(iv),
215+
tag, (word32)sizeof(tag),
216+
NULL, 0);
217+
wc_AesFree(&aes);
218+
#endif /* !NO_AES && !WC_NO_AES */
219+
#endif /* NO_WOLFSSL_WARMUP */
220+
170221
#if defined(__aarch64__) && defined(WOLFSSL_ARMASM_BARRIER_DETECT)
171222
aarch64_use_sb = IS_AARCH64_SB(cpuid_get_flags());
172223
#endif
@@ -444,11 +495,13 @@ int wolfCrypt_Init(void)
444495
return ret;
445496
}
446497
#endif
447-
}
498+
499+
} /* (initRefCount == 0) */
500+
448501
initRefCount++;
449502

450503
return ret;
451-
}
504+
} /* wolfCrypt_Init */
452505

453506
#if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
454507
long wolfCrypt_heap_peakAllocs_checkpoint(void) {

0 commit comments

Comments
 (0)