|
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,113 @@ 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 | + 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 | + |
368 | 481 | /* |
369 | 482 | ** All platforms: git details |
370 | 483 | */ |
|
0 commit comments