Skip to content

Commit 1d1af75

Browse files
committed
Introduce Espressif wolfcrypt warmup
1 parent b90720c commit 1d1af75

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed

IDE/Espressif/ESP-IDF/examples/wolfssl_test/main/main.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@
2020
*/
2121

2222
/* ESP-IDF */
23-
#include <esp_log.h>
2423
#include "sdkconfig.h"
24+
#include <esp_log.h>
2525

2626
/* wolfSSL */
27-
/* Always include wolfcrypt/settings.h before any other wolfSSL file. */
28-
/* Reminder: settings.h pulls in user_settings.h; don't include it here. */
27+
/* The wolfSSL user_settings.h is automatically included by settings.h file.
28+
* Never explicitly include wolfSSL user_settings.h in any source file.
29+
* The settings.h should also be listed above wolfssl library include files. */
2930
#if defined(WOLFSSL_USER_SETTINGS)
3031
#include <wolfssl/wolfcrypt/settings.h>
3132
#if defined(WOLFSSL_ESPIDF)
3233
#include <wolfssl/version.h>
3334
#include <wolfssl/wolfcrypt/types.h>
35+
#include <wolfssl/wolfcrypt/logging.h>
3436
#include <wolfcrypt/test/test.h>
3537
#include <wolfssl/wolfcrypt/port/Espressif/esp-sdk-lib.h>
3638
#include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h>
@@ -187,7 +189,16 @@ void app_main(void)
187189
ESP_LOGI(TAG, "--------------------------------------------------------");
188190
ESP_LOGI(TAG, "--------------------------------------------------------");
189191
ESP_LOGI(TAG, "Stack Start: 0x%x", stack_start);
190-
192+
#ifdef HAVE_WOLFCRYPT_WARMUP
193+
/* Unless disabled, we'll try to allocate known, long-term heap items early
194+
* in an attempt to avoid later allocations that may cause fragmentation. */
195+
ESP_ERROR_CHECK(esp_sdk_wolfssl_warmup());
196+
#endif
197+
#ifdef DEBUG_WOLFSSL
198+
/* Turn debugging on and off as needed: */
199+
wolfSSL_Debugging_ON();
200+
wolfSSL_Debugging_OFF();
201+
#endif
191202
#ifdef WOLFSSL_ESP_NO_WATCHDOG
192203
ESP_LOGW(TAG, "Found WOLFSSL_ESP_NO_WATCHDOG, disabling...");
193204
esp_DisableWatchdog();

wolfcrypt/src/port/Espressif/esp32_util.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
#include <wolfssl/wolfcrypt/types.h>
5151
#include <wolfssl/version.h>
5252

53+
#ifndef NO_WOLFCRYPT_WARMUP
54+
#define HAVE_WOLFCRYPT_WARMUP
55+
#if !defined(NO_AES) && defined(HAVE_AESGCM)
56+
#include <wolfssl/wolfcrypt/aes.h>
57+
#endif
58+
#endif
5359
/*
5460
** Version / Platform info.
5561
**
@@ -365,6 +371,95 @@ static int ShowExtendedSystemInfo_platform_espressif(void)
365371
*******************************************************************************
366372
*/
367373

374+
/*
375+
** All platforms: Warmup wolfssl
376+
*/
377+
esp_err_t esp_sdk_wolfssl_warmup(void)
378+
{
379+
esp_err_t ret = ESP_OK;
380+
#ifdef NO_WOLFCRYPT_WARMUP
381+
ESP_LOGW(TAG, "esp_sdk_wolfssl_warmup called with NO_WOLFCRYPT_WARMUP");
382+
#else
383+
/* Even though some [name]_NO_MALLOC may defined, there's always the host
384+
* freeRTOS heap. So here, we'll initialize things early on to attempt
385+
* having the heap allocate long term items near the endge of free memory,
386+
* rather than in the middle. */
387+
WC_RNG rng;
388+
byte dummy;
389+
#if !defined(NO_AES) && defined(HAVE_AESGCM)
390+
Aes aes;
391+
unsigned char key16[16];
392+
unsigned char out[16];
393+
unsigned char in[16];
394+
unsigned char iv[12];
395+
int devId;
396+
#endif /* NO_AES && HAVE_AESGCM declarations */
397+
398+
#if defined(DEBUG_WOLFSSL_MALLOC_VERBOSE)
399+
ESP_LOGI(TAG, "Warming up RNG");
400+
#endif
401+
ret = wc_InitRng(&rng);
402+
if (ret == 0) {
403+
/* forces Hash_DRBG/SHA */
404+
ret = wc_RNG_GenerateBlock(&rng, &dummy, 1);
405+
if (ret != 0) {
406+
ESP_LOGI(TAG, "wolfCrypt_Init wc_RNG_GenerateBlock failed");
407+
}
408+
}
409+
if (ret != 0) {
410+
ESP_LOGI(TAG, "wolfCrypt_Init RNG warmup failed");
411+
}
412+
ret = wc_FreeRng(&rng);
413+
if (ret != 0) {
414+
ESP_LOGI(TAG, "wolfCrypt_Init wc_FreeRng failed");
415+
}
416+
417+
#if !defined(NO_AES) && defined(HAVE_AESGCM)
418+
#if defined(DEBUG_WOLFSSL_MALLOC_VERBOSE)
419+
ESP_LOGI(TAG, "Warming up AES");
420+
#endif
421+
memset(key16, 0, sizeof(key16));
422+
memset(iv, 0, sizeof(iv));
423+
memset(in, 0, sizeof(in));
424+
devId = INVALID_DEVID;
425+
426+
ret = wc_AesInit(&aes, NULL, devId);
427+
if (ret == 0) {
428+
/* Set an ECB key (no IV). This avoids pulling in GCM/GHASH. */
429+
ret = wc_AesSetKey(&aes, key16, (word32)sizeof(key16), NULL,
430+
AES_ENCRYPTION);
431+
}
432+
if (ret == 0) {
433+
#ifdef WOLFSSL_AES_DIRECT
434+
/* Single direct block encrypt to exercise the core/driver. */
435+
ret = wc_AesEncryptDirect(&aes, out, in);
436+
#elif !defined(NO_AES_CBC)
437+
/* One-block CBC (tiny; no padding; does not pull GCM). */
438+
ret = wc_AesSetIV(&aes, iv);
439+
if (ret == 0) {
440+
ret = wc_AesCbcEncrypt(&aes, out, in, (word32)sizeof(in));
441+
}
442+
#elif defined(HAVE_AES_CTR) || defined(WOLFSSL_AES_COUNTER)
443+
/* As another lightweight option, CTR one-block. */
444+
ret = wc_AesSetIV(&aes, iv);
445+
if (ret == 0) {
446+
ret = wc_AesCtrEncrypt(&aes, out, in, (word32)sizeof(in));
447+
}
448+
#else
449+
/* No small mode available; setting key already did most of the warmup. */
450+
ret = 0;
451+
#endif /* WOLFSSL_AES_DIRECT, NO_AES_CBC, HAVE_AES_CTR, etc*/
452+
}
453+
if (ret != 0) {
454+
ESP_LOGI(TAG, "AES warmup failed during wolfCrypt_Init");
455+
}
456+
wc_AesFree(&aes);
457+
#endif /* !NO_AES && HAVE_AESGCM */
458+
#endif /* !NO_WOLFCRYPT_WARMUP */
459+
460+
return ret;
461+
}
462+
368463
/*
369464
** All platforms: git details
370465
*/

wolfssl/wolfcrypt/port/Espressif/esp-sdk-lib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@
142142
extern "C" {
143143
#endif
144144

145+
#define HAVE_WOLFCRYPT_WARMUP
146+
WOLFSSL_LOCAL esp_err_t esp_sdk_wolfssl_warmup(void);
147+
145148
WOLFSSL_LOCAL esp_err_t esp_sdk_time_mem_init(void);
146149

147150
WOLFSSL_LOCAL esp_err_t sdk_var_whereis(const char* v_name, void* v);

0 commit comments

Comments
 (0)