Skip to content

Commit

Permalink
Merge pull request #6587 from gojimmypi/ESP32WROOM-refactor
Browse files Browse the repository at this point in the history
Espressif refactor WROOM32 ESP32
  • Loading branch information
dgarske authored Jul 10, 2023
2 parents 9d18648 + 5754640 commit e4c9a73
Show file tree
Hide file tree
Showing 19 changed files with 166 additions and 159 deletions.
14 changes: 7 additions & 7 deletions IDE/Espressif/ESP-IDF/examples/wolfssl_test/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,18 @@ void app_main(void)
ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));

/* check to see if we are using hardware encryption */
#if defined(NO_ESP32WROOM32_CRYPT)
ESP_LOGI(TAG, "NO_ESP32WROOM32_CRYPT defined! HW acceleration DISABLED.");
#if defined(NO_ESP32_CRYPT)
ESP_LOGI(TAG, "NO_ESP32_CRYPT defined! HW acceleration DISABLED.");
#else
#if defined(CONFIG_IDF_TARGET_ESP32C3)
#error "ESP32WROOM32_CRYPT not yet supported on ESP32-C3"
#error "ESP32_CRYPT not yet supported on ESP32-C3"
#elif defined(CONFIG_IDF_TARGET_ESP32S2)
#error "ESP32WROOM32_CRYPT not yet supported on ESP32-S2"
#error "ESP32_CRYPT not yet supported on ESP32-S2"
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
/* #error "ESP32WROOM32_CRYPT not yet supported on ESP32-S3" */
ESP_LOGI(TAG, "ESP32WROOM32_CRYPT is enabled for ESP32-S3.");
/* #error "ESP32_CRYPT not yet supported on ESP32-S3" */
ESP_LOGI(TAG, "ESP32_CRYPT is enabled for ESP32-S3.");
#else
ESP_LOGI(TAG, "ESP32WROOM32_CRYPT is enabled.");
ESP_LOGI(TAG, "ESP32_CRYPT is enabled.");
#endif
#endif

Expand Down
12 changes: 6 additions & 6 deletions IDE/Espressif/ESP-IDF/examples/wolfssl_test_idf/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,17 @@ void app_main(void)
ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));

/* check to see if we are using hardware encryption */
#if defined(NO_ESP32WROOM32_CRYPT)
ESP_LOGI(TAG, "NO_ESP32WROOM32_CRYPT defined! HW acceleration DISABLED.");
#if defined(NO_ESP32_CRYPT)
ESP_LOGI(TAG, "NO_ESP32_CRYPT defined! HW acceleration DISABLED.");
#else
#if defined(CONFIG_IDF_TARGET_ESP32C3)
#error "ESP32WROOM32_CRYPT not yet supported on ESP32-C3"
#error "ESP32_CRYPT not yet supported on ESP32-C3"
#elif defined(CONFIG_IDF_TARGET_ESP32S2)
#error "ESP32WROOM32_CRYPT not yet supported on ESP32-S2"
#error "ESP32_CRYPT not yet supported on ESP32-S2"
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
#error "ESP32WROOM32_CRYPT not yet supported on ESP32-S3"
#error "ESP32_CRYPT not yet supported on ESP32-S3"
#else
ESP_LOGI(TAG, "ESP32WROOM32_CRYPT is enabled.");
ESP_LOGI(TAG, "ESP32_CRYPT is enabled.");
#endif
#endif

Expand Down
20 changes: 10 additions & 10 deletions IDE/Espressif/ESP-IDF/user_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#undef WOLFSSL_ESPIDF
#undef WOLFSSL_ESPWROOM32
#undef WOLFSSL_ESP32
#undef WOLFSSL_ESPWROOM32SE
#undef WOLFSSL_ESPWROOM32
#undef WOLFSSL_ESP32
#undef WOLFSSL_ESP8266

#define WOLFSSL_ESPIDF

/*
* choose ONE of these Espressif chips to define:
*
* WOLFSSL_ESPWROOM32
* WOLFSSL_ESP32
* WOLFSSL_ESPWROOM32SE
* WOLFSSL_ESP8266
*/

#define WOLFSSL_ESPWROOM32
#define WOLFSSL_ESP32

/* #define DEBUG_WOLFSSL_VERBOSE */

Expand Down Expand Up @@ -86,7 +86,7 @@
#endif

/* rsa primitive specific definition */
#if defined(WOLFSSL_ESPWROOM32) || defined(WOLFSSL_ESPWROOM32SE)
#if defined(WOLFSSL_ESP32) || defined(WOLFSSL_ESPWROOM32SE)
/* Define USE_FAST_MATH and SMALL_STACK */
#define ESP32_USE_RSA_PRIMITIVE
/* threshold for performance adjustment for hw primitive use */
Expand All @@ -98,7 +98,7 @@

/* debug options */
/* #define DEBUG_WOLFSSL */
/* #define WOLFSSL_ESP32WROOM32_CRYPT_DEBUG */
/* #define WOLFSSL_ESP32_CRYPT_DEBUG */
/* #define WOLFSSL_ATECC508A_DEBUG */

/* date/time */
Expand All @@ -108,10 +108,10 @@
/* #define XTIME time */

/* when you want not to use HW acceleration */
/* #define NO_ESP32WROOM32_CRYPT */
/* #define NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH*/
/* #define NO_WOLFSSL_ESP32WROOM32_CRYPT_AES */
/* #define NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI */
/* #define NO_ESP32_CRYPT */
/* #define NO_WOLFSSL_ESP32_CRYPT_HASH*/
/* #define NO_WOLFSSL_ESP32_CRYPT_AES */
/* #define NO_WOLFSSL_ESP32_CRYPT_RSA_PRI */

/* adjust wait-timeout count if you see timeout in rsa hw acceleration */
#define ESP_RSA_TIMEOUT_CNT 0x249F00
20 changes: 10 additions & 10 deletions wolfcrypt/src/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,8 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
#error nRF51 AES Hardware does not support decrypt
#endif /* HAVE_AES_DECRYPT */

#elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
#elif defined(WOLFSSL_ESP32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_AES)

#include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"

Expand Down Expand Up @@ -2865,8 +2865,8 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(
{
return wc_AesSetKey(aes, userKey, keylen, iv, dir);
}
#elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
#elif defined(WOLFSSL_ESP32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_AES)

int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
const byte* iv, int dir)
Expand Down Expand Up @@ -3176,8 +3176,8 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(
rk = aes->key;
XMEMCPY(rk, userKey, keylen);
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
(!defined(WOLFSSL_ESP32WROOM32_CRYPT) || \
defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES))
(!defined(WOLFSSL_ESP32_CRYPT) || \
defined(NO_WOLFSSL_ESP32_CRYPT_AES))
ByteReverseWords(rk, rk, keylen);
#endif

Expand Down Expand Up @@ -4152,8 +4152,8 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
return ret;
}
#endif /* HAVE_AES_DECRYPT */
#elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
#elif defined(WOLFSSL_ESP32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_AES)

int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
Expand Down Expand Up @@ -4641,8 +4641,8 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
#elif defined(WOLFSSL_DEVCRYPTO_AES)
/* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */

#elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
#elif defined(WOLFSSL_ESP32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_AES)
/* esp32 doesn't support CRT mode by hw. */
/* use aes ecnryption plus sw implementation */
#define NEED_AES_CTR_SOFT
Expand Down
6 changes: 3 additions & 3 deletions wolfcrypt/src/port/Espressif/esp32_aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@

#ifndef NO_AES

#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
#if defined(WOLFSSL_ESP32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_AES)
#include "sdkconfig.h" /* programmatically generated from sdkconfig */
#include <wolfssl/wolfcrypt/aes.h>
#include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"
Expand Down Expand Up @@ -428,5 +428,5 @@ int wc_esp32AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
return 0;
} /* wc_esp32AesCbcDecrypt */

#endif /* WOLFSSL_ESP32WROOM32_CRYPT */
#endif /* WOLFSSL_ESP32_CRYPT */
#endif /* NO_AES */
8 changes: 4 additions & 4 deletions wolfcrypt/src/port/Espressif/esp32_mp.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

#if !defined(NO_RSA) || defined(HAVE_ECC)

#if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \
!defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI)
#if defined(WOLFSSL_ESP32_CRYPT_RSA_PRI) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_RSA_PRI)

#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
Expand Down Expand Up @@ -908,7 +908,7 @@ int esp_mp_exptmod(MATH_INT_T* X, MATH_INT_T* Y, word32 Ys, MATH_INT_T* M, MATH_
#endif
}

#endif /* WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) &&
* !NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI */
#endif /* WOLFSSL_ESP32_CRYPT_RSA_PRI) &&
* !NO_WOLFSSL_ESP32_CRYPT_RSA_PRI */

#endif /* !NO_RSA || HAVE_ECC */
6 changes: 3 additions & 3 deletions wolfcrypt/src/port/Espressif/esp32_sha.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@


/* this entire file content is excluded if not using HW hash acceleration */
#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
#if defined(WOLFSSL_ESP32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH)

/* TODO this may be chip type dependent: add support for others */
#include <hal/clk_gate_ll.h> /* ESP32-WROOM */
Expand Down Expand Up @@ -1364,5 +1364,5 @@ int esp_sha512_digest_process(struct wc_Sha512* sha, byte blockproc)
return ret;
} /* esp_sha512_digest_process */
#endif /* WOLFSSL_SHA512 || WOLFSSL_SHA384 */
#endif /* WOLFSSL_ESP32WROOM32_CRYPT */
#endif /* WOLFSSL_ESP32_CRYPT */
#endif /* !defined(NO_SHA) ||... */
26 changes: 13 additions & 13 deletions wolfcrypt/src/port/Espressif/esp32_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/version.h>

#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
#if defined(WOLFSSL_ESP32_CRYPT) && \
(!defined(NO_AES) || !defined(NO_SHA) || !defined(NO_SHA256) ||\
defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512))

Expand Down Expand Up @@ -170,35 +170,35 @@ static int ShowExtendedSystemInfo_platform_espressif()
#endif

/* check to see if we are using hardware encryption */
#if defined(NO_ESP32WROOM32_CRYPT)
WOLFSSL_VERSION_PRINTF("NO_ESP32WROOM32_CRYPT defined! "
#if defined(NO_ESP32_CRYPT)
WOLFSSL_VERSION_PRINTF("NO_ESP32_CRYPT defined! "
"HW acceleration DISABLED.");
#else
/* first show what platform hardware acceleration is enabled
** (some new platforms may not be supported yet) */
#if defined(CONFIG_IDF_TARGET_ESP32)
WOLFSSL_VERSION_PRINTF("ESP32WROOM32_CRYPT is enabled for ESP32.");
WOLFSSL_VERSION_PRINTF("ESP32_CRYPT is enabled for ESP32.");
#elif defined(CONFIG_IDF_TARGET_ESP32S2)
WOLFSSL_VERSION_PRINTF("ESP32WROOM32_CRYPT is enabled for ESP32-S2.");
WOLFSSL_VERSION_PRINTF("ESP32_CRYPT is enabled for ESP32-S2.");
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
WOLFSSL_VERSION_PRINTF("ESP32WROOM32_CRYPT is enabled for ESP32-S3.");
WOLFSSL_VERSION_PRINTF("ESP32_CRYPT is enabled for ESP32-S3.");
#else
#error "ESP32WROOM32_CRYPT not yet supported on this IDF TARGET"
#error "ESP32_CRYPT not yet supported on this IDF TARGET"
#endif

/* Even though enabled, some specifics may be disabled */
#if defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
WOLFSSL_VERSION_PRINTF("NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH is defined!"
#if defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
WOLFSSL_VERSION_PRINTF("NO_WOLFSSL_ESP32_CRYPT_HASH is defined!"
"(disabled HW SHA).");
#endif

#if defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
WOLFSSL_VERSION_PRINTF("NO_WOLFSSL_ESP32WROOM32_CRYPT_AES is defined!"
#if defined(NO_WOLFSSL_ESP32_CRYPT_AES)
WOLFSSL_VERSION_PRINTF("NO_WOLFSSL_ESP32_CRYPT_AES is defined!"
"(disabled HW AES).");
#endif

#if defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI)
WOLFSSL_VERSION_PRINTF("NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI defined!"
#if defined(NO_WOLFSSL_ESP32_CRYPT_RSA_PRI)
WOLFSSL_VERSION_PRINTF("NO_WOLFSSL_ESP32_CRYPT_RSA_PRI defined!"
"(disabled HW RSA)");
#endif
#endif
Expand Down
4 changes: 2 additions & 2 deletions wolfcrypt/src/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -3431,7 +3431,7 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
#elif defined(WOLFSSL_ESPIDF)

/* Espressif */
#if defined(WOLFSSL_ESPWROOM32) || defined(WOLFSSL_ESPWROOM32SE)
#if defined(WOLFSSL_ESP32) || defined(WOLFSSL_ESPWROOM32SE)

/* Espressif ESP32 */
#include <esp_system.h>
Expand Down Expand Up @@ -3477,7 +3477,7 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)

return 0;
}
#endif /* end WOLFSSL_ESPWROOM32 */
#endif /* end WOLFSSL_ESP32 */

#elif defined(WOLFSSL_LINUXKM)
#include <linux/random.h>
Expand Down
24 changes: 12 additions & 12 deletions wolfcrypt/src/sha.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@
#include <wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h>
#endif

#undef WOLFSSL_USE_ESP32WROOM32_CRYPT_HASH_HW
#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
#undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
#if defined(WOLFSSL_ESP32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
/* define a single keyword for simplicity & readability
*
* by default the HW acceleration is on for ESP32-WROOM32
* but individual components can be turned off.
*/
#define WOLFSSL_USE_ESP32WROOM32_CRYPT_HASH_HW
#define WOLFSSL_USE_ESP32_CRYPT_HASH_HW
#include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"

/* Although we have hardware acceleration,
Expand All @@ -78,7 +78,7 @@
** We'll be using software for RISC-V at this time */
static const char* TAG = "wc_sha-c3";
#else
#undef WOLFSSL_USE_ESP32WROOM32_CRYPT_HASH_HW
#undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
#endif

/* fips wrapper calls, user can call direct */
Expand Down Expand Up @@ -321,7 +321,7 @@
!defined(WOLFSSL_QNX_CAAM)
/* wolfcrypt/src/port/caam/caam_sha.c */

#elif defined(WOLFSSL_USE_ESP32WROOM32_CRYPT_HASH_HW) || \
#elif defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) || \
defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW)

/* This function initializes SHA.
Expand Down Expand Up @@ -567,7 +567,7 @@ int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
sha->devCtx = NULL;
#endif

#ifdef WOLFSSL_USE_ESP32WROOM32_CRYPT_HASH_HW
#ifdef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
if (sha->ctx.mode != ESP32_SHA_INIT) {
/* it may be interesting to see old values during debugging */
ESP_LOGV(TAG, "Set ctx mode from prior value: %d", sha->ctx.mode);
Expand Down Expand Up @@ -651,7 +651,7 @@ int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
#endif

#if defined(WOLFSSL_USE_ESP32WROOM32_CRYPT_HASH_HW)
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
if (sha->ctx.mode == ESP32_SHA_INIT) {
ESP_LOGV(TAG, "wc_ShaUpdate try hardware");
esp_sha_try_hw_lock(&sha->ctx);
Expand Down Expand Up @@ -712,7 +712,7 @@ int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
ByteReverseWords(local32, local32, WC_SHA_BLOCK_SIZE);
#endif

#if defined(WOLFSSL_USE_ESP32WROOM32_CRYPT_HASH_HW)
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
if (sha->ctx.mode == ESP32_SHA_INIT){
esp_sha_try_hw_lock(&sha->ctx);
}
Expand Down Expand Up @@ -806,7 +806,7 @@ int wc_ShaFinal(wc_Sha* sha, byte* hash)
ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
#endif

#if defined(WOLFSSL_USE_ESP32WROOM32_CRYPT_HASH_HW)
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
/* For a fresh sha.ctx, try to use hardware acceleration */
if (sha->ctx.mode == ESP32_SHA_INIT) {
esp_sha_try_hw_lock(&sha->ctx);
Expand Down Expand Up @@ -855,7 +855,7 @@ int wc_ShaFinal(wc_Sha* sha, byte* hash)
2 * sizeof(word32));
#endif

#if defined(WOLFSSL_USE_ESP32WROOM32_CRYPT_HASH_HW)
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
if (sha->ctx.mode == ESP32_SHA_INIT) {
esp_sha_try_hw_lock(&sha->ctx);
}
Expand Down Expand Up @@ -1012,7 +1012,7 @@ int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
ret = se050_hash_copy(&src->se050Ctx, &dst->se050Ctx);
#endif

#if defined(WOLFSSL_USE_ESP32WROOM32_CRYPT_HASH_HW)
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
esp_sha_ctx_copy(src, dst);
#endif

Expand Down
Loading

0 comments on commit e4c9a73

Please sign in to comment.