Skip to content

Commit 98f3647

Browse files
committed
Introduce Espressif wolfcrypt warmup
1 parent b90720c commit 98f3647

File tree

4 files changed

+132
-4
lines changed

4 files changed

+132
-4
lines changed

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ NO_TKERNEL_MEM_POOL
421421
NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
422422
NO_VERIFY_OID
423423
NO_WC_SSIZE_TYPE
424+
NO_WOLFCRYPT_WARMUP
424425
NO_WOLFSSL_ALLOC_ALIGN
425426
NO_WOLFSSL_AUTOSAR_CRYIF
426427
NO_WOLFSSL_AUTOSAR_CRYPTO

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: 113 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,113 @@ 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+
int ret_i = 0; /* intermediate wolfssl results*/
381+
#ifdef NO_WOLFCRYPT_WARMUP
382+
ESP_LOGW(TAG, "esp_sdk_wolfssl_warmup called with NO_WOLFCRYPT_WARMUP");
383+
#else
384+
/* Even though some [name]_NO_MALLOC may defined, there's always the host
385+
* freeRTOS heap. So here, we'll initialize things early on to attempt
386+
* having the heap allocate long term items near the edge of free memory,
387+
* rather than in the middle. */
388+
WC_RNG rng;
389+
int rng_inited = 0;
390+
unsigned char dummy;
391+
#if !defined(NO_AES) && defined(HAVE_AESGCM)
392+
Aes aes;
393+
unsigned char key16[WC_AES_BLOCK_SIZE];
394+
unsigned char out[WC_AES_BLOCK_SIZE];
395+
unsigned char in[WC_AES_BLOCK_SIZE];
396+
unsigned char iv[WC_AES_BLOCK_SIZE];
397+
int devId;
398+
int aes_inited = 0;
399+
#endif /* NO_AES && HAVE_AESGCM declarations */
400+
401+
#if defined(DEBUG_WOLFSSL_MALLOC_VERBOSE)
402+
ESP_LOGI(TAG, "Warming up RNG");
403+
#endif
404+
405+
ret_i = wc_InitRng(&rng);
406+
if (ret_i == 0) {
407+
rng_inited = 1;
408+
/* forces Hash_DRBG/SHA */
409+
ret_i = wc_RNG_GenerateBlock(&rng, &dummy, sizeof(dummy));
410+
if (ret_i != 0) {
411+
ESP_LOGE(TAG, "esp_sdk_wolfssl_warmup wc_RNG_GenerateBlock failed");
412+
}
413+
}
414+
if (ret_i != 0) {
415+
ret = ESP_FAIL;
416+
ESP_LOGE(TAG, "esp_sdk_wolfssl_warmup RNG warmup failed");
417+
}
418+
if (rng_inited == 1) {
419+
ret_i = wc_FreeRng(&rng);
420+
if (ret_i != 0) {
421+
ret = ESP_FAIL;
422+
ESP_LOGE(TAG, "esp_sdk_wolfssl_warmup wc_FreeRng failed");
423+
}
424+
}
425+
426+
#if !defined(NO_AES) && defined(HAVE_AESGCM)
427+
#if defined(DEBUG_WOLFSSL_MALLOC_VERBOSE)
428+
ESP_LOGI(TAG, "Warming up AES");
429+
#endif
430+
XMEMSET(key16, 0, (word32)sizeof(key16));
431+
XMEMSET(iv, 0, (word32)sizeof(iv));
432+
XMEMSET(in, 0, (word32)sizeof(in));
433+
#ifdef INVALID_DEVID
434+
devId = INVALID_DEVID; /* software by default */
435+
#else
436+
devId = 0;
437+
#endif
438+
439+
ret_i = wc_AesInit(&aes, NULL, devId);
440+
if (ret_i == 0) {
441+
aes_inited = 1;
442+
/* Set an ECB key (no IV). This avoids pulling in GCM/GHASH. */
443+
ret_i = wc_AesSetKey(&aes, key16, (word32)sizeof(key16), NULL,
444+
AES_ENCRYPTION);
445+
}
446+
if (ret_i == 0) {
447+
#ifdef WOLFSSL_AES_DIRECT
448+
/* Single direct block encrypt to exercise the core/driver. */
449+
ret_i = wc_AesEncryptDirect(&aes, out, in);
450+
#elif !defined(NO_AES_CBC)
451+
/* One-block CBC (tiny; no padding; does not pull GCM). */
452+
ret_i = wc_AesSetIV(&aes, iv);
453+
if (ret_i == 0) {
454+
ret_i = wc_AesCbcEncrypt(&aes, out, in, (word32)sizeof(in));
455+
}
456+
#elif defined(HAVE_AES_CTR) || defined(WOLFSSL_AES_COUNTER)
457+
/* As another lightweight option, CTR one-block. */
458+
ret_i = wc_AesSetIV(&aes, iv);
459+
if (ret_i == 0) {
460+
ret_i = wc_AesCtrEncrypt(&aes, out, in, (word32)sizeof(in));
461+
}
462+
#else
463+
/* No small mode available; setting key already did most of the warmup. */
464+
ret_i = 0;
465+
#endif /* WOLFSSL_AES_DIRECT, NO_AES_CBC, HAVE_AES_CTR, etc*/
466+
}
467+
if (ret_i != 0) {
468+
ret = ESP_FAIL;
469+
ESP_LOGE(TAG, "AES warmup failed during esp_sdk_wolfssl_warmup");
470+
}
471+
if (aes_inited == 1) {
472+
wc_AesFree(&aes);
473+
}
474+
475+
#endif /* !NO_AES && HAVE_AESGCM */
476+
#endif /* !NO_WOLFCRYPT_WARMUP */
477+
478+
return ret;
479+
}
480+
368481
/*
369482
** All platforms: git details
370483
*/

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)