|
50 | 50 | #include <wolfssl/wolfcrypt/types.h> |
51 | 51 | #include <wolfssl/version.h> |
52 | 52 |
|
| 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 |
53 | 59 | /* |
54 | 60 | ** Version / Platform info. |
55 | 61 | ** |
@@ -365,6 +371,95 @@ static int ShowExtendedSystemInfo_platform_espressif(void) |
365 | 371 | ******************************************************************************* |
366 | 372 | */ |
367 | 373 |
|
| 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 | + |
368 | 463 | /* |
369 | 464 | ** All platforms: git details |
370 | 465 | */ |
|
0 commit comments