Skip to content

Commit 7e69030

Browse files
author
gojimmypi
committed
Espressif ESP32-C3 ESP32-C6 ESP32-S2 Hardware Acceleration
1 parent 665469f commit 7e69030

17 files changed

Lines changed: 3617 additions & 760 deletions

File tree

IDE/Espressif/ESP-IDF/README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,48 @@ Including the following examples:
1414
* Simple [TLS client](./examples/wolfssl_client/)/[server](./examples/wolfssl_server/)
1515
* Cryptographic [test](./examples/wolfssl_test/)
1616
* Cryptographic [benchmark](./examples/wolfssl_benchmark/)
17+
* Bare-bones [template](./examples/template/)
1718

1819
The *user_settings.h* file enables some of the hardened settings.
1920

2021
## Requirements
2122

2223
1. [ESP-IDF development framework](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/)
2324

24-
## Setup for Linux
25+
## wolfSSL as an Espressif component
26+
27+
There are various methods available for using wolfSSL as a component:
28+
29+
* Managed Component - easiest to get started.
30+
* Local component directory - best for development.
31+
* Install locally - least flexible, but project is fully self-contained.
32+
33+
## Espressif Managed Components
34+
35+
Visit https://components.espressif.com/components/wolfssl/wolfssl and see the instructions. Typically:
36+
37+
```
38+
idf.py add-dependency "wolfssl/wolfssl^5.6.0-stable"
39+
```
40+
41+
## Standard local component:
42+
43+
See the [template example](./examples/template/README.md). Simply created a `wolfssl` directory in the
44+
local project `components` directory and place the [CMakeLists.txt](./examples/template/components/CMakeLists.txt)
45+
file there. Then add a `components/wolfssl/include` directory and place the [user_settings.h](/examples/template/components/wolfssl/include/user_settings.h)
46+
file there. If wolfSSL is in a structure such as `./workspace/wolfssl` with respect to your project at `./workspace/wolfssl`,
47+
then the cmake file should automatically find the wolfSSL source code. Otherwise set the cmake `WOLFSSL_ROOT` variable
48+
in the top-level CMake file. Examples:
49+
50+
```cmake
51+
set(WOLFSSL_ROOT "C:/some-path/wolfssl")
52+
set(WOLFSSL_ROOT "c:/workspace/wolfssl-[username]")
53+
set(WOLFSSL_ROOT "/mnt/c/somepath/wolfssl")
54+
```
55+
56+
See the specific examples for additional details.
57+
58+
## Setup for Linux (wolfSSL local copy)
2559

2660
1. Run `setup.sh` at _/path/to_`/wolfssl/IDE/Espressif/ESP-IDF/` to deploy files into ESP-IDF tree
2761
2. Find Wolfssl files at _/path/to/esp_`/esp-idf/components/wolfssl/`

wolfcrypt/src/aes.c

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
483483
#undef WOLFSSL_AES_DIRECT
484484
#define WOLFSSL_AES_DIRECT
485485

486-
/* If we choose to never have a fallback to SW: */
486+
/* Encrypt: If we choose to never have a fallback to SW: */
487487
#if !defined(NEED_AES_HW_FALLBACK) && (defined(HAVE_AESGCM) || defined(WOLFSSL_AES_DIRECT))
488488
static WARN_UNUSED_RESULT int wc_AesEncrypt( /* calling this one when NO_AES_192 is defined */
489489
Aes* aes, const byte* inBlock, byte* outBlock)
@@ -501,7 +501,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
501501
}
502502
#endif
503503

504-
/* If we choose to never have a fallback to SW */
504+
/* Decrypt: If we choose to never have a fallback to SW: */
505505
#if !defined(NEED_AES_HW_FALLBACK) && (defined(HAVE_AES_DECRYPT) && defined(WOLFSSL_AES_DIRECT))
506506
static WARN_UNUSED_RESULT int wc_AesDecrypt(
507507
Aes* aes, const byte* inBlock, byte* outBlock)
@@ -882,10 +882,9 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
882882
#ifdef NEED_AES_TABLES
883883

884884
#ifndef WC_AES_BITSLICED
885-
#if (!defined(WOLFSSL_SILABS_SE_ACCEL) && \
886-
!defined(WOLFSSL_ESP32_CRYPT_RSA_PRI) \
887-
) || \
888-
(defined(WOLFSSL_ESP32_CRYPT_RSA_PRI) && defined(NEED_AES_HW_FALLBACK))
885+
#if !defined(WOLFSSL_SILABS_SE_ACCEL) || \
886+
defined(NO_ESP32_CRYPT) || defined(NO_WOLFSSL_ESP32_CRYPT_AES) || \
887+
defined(NEED_AES_HW_FALLBACK)
889888
static const FLASH_QUALIFIER word32 rcon[] = {
890889
0x01000000, 0x02000000, 0x04000000, 0x08000000,
891890
0x10000000, 0x20000000, 0x40000000, 0x80000000,
@@ -1535,8 +1534,8 @@ static WARN_UNUSED_RESULT word32 inv_col_mul(
15351534
byte t0 = t9 ^ tb ^ td;
15361535
return t0 ^ AES_XTIME(AES_XTIME(AES_XTIME(t0 ^ te) ^ td ^ te) ^ tb ^ te);
15371536
}
1538-
#endif
1539-
#endif
1537+
#endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
1538+
#endif /* WOLFSSL_AES_SMALL_TABLES */
15401539
#endif
15411540

15421541
#if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT) || \
@@ -3894,8 +3893,29 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
38943893
XMEMCPY(rk, key, keySz);
38953894
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
38963895
(!defined(WOLFSSL_ESP32_CRYPT) || defined(NO_WOLFSSL_ESP32_CRYPT_AES))
3897-
ByteReverseWords(rk, rk, keySz);
3898-
#endif
3896+
/* Always reverse words when using only SW */
3897+
{
3898+
ByteReverseWords(rk, rk, keySz);
3899+
}
3900+
#else
3901+
/* Sometimes reverse words when using supported HW */
3902+
#if defined(WOLFSSL_ESPIDF)
3903+
/* Some platforms may need SW fallback (e.g. AES192) */
3904+
#if defined(NEED_AES_HW_FALLBACK)
3905+
{
3906+
ESP_LOGV(TAG, "wc_AesEncrypt fallback check");
3907+
if (wc_esp32AesSupportedKeyLen(aes)) {
3908+
/* don't reverse for HW supported key lengths */
3909+
}
3910+
else {
3911+
ByteReverseWords(rk, rk, keySz);
3912+
}
3913+
}
3914+
#else
3915+
/* If we don't need SW fallback, don't need to reverse words. */
3916+
#endif /* NEED_AES_HW_FALLBACK */
3917+
#endif /* WOLFSSL_ESPIDF */
3918+
#endif /* LITTLE_ENDIAN_ORDER, etc */
38993919

39003920
switch (keySz) {
39013921
#if defined(AES_MAX_KEY_SIZE) && AES_MAX_KEY_SIZE >= 128 && \
@@ -4345,13 +4365,20 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
43454365
return wc_AesSetKey_for_ESP32(aes, userKey, keylen, iv, dir);
43464366
}
43474367
else {
4368+
#if defined(WOLFSSL_HW_METRICS)
4369+
/* It is interesting to know how many times we could not complete
4370+
* AES in hardware due to unsupported lengths. */
4371+
wc_esp32AesUnupportedLengthCountAdd();
4372+
#endif
43484373
#ifdef DEBUG_WOLFSSL
43494374
ESP_LOGW(TAG, "wc_AesSetKey HW Fallback, unsupported keylen = %d",
43504375
keylen);
43514376
#endif
43524377
}
4353-
#endif
4378+
#endif /* WOLFSSL_ESPIDF && NEED_AES_HW_FALLBACK */
4379+
43544380
return wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir, 1);
4381+
43554382
} /* wc_AesSetKey() */
43564383

43574384
#if defined(WOLFSSL_AES_DIRECT) || defined(WOLFSSL_AES_COUNTER)

wolfcrypt/src/port/Espressif/README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ For detail about ESP32 HW Acceleration, you can find in [Technical Reference Man
88

99
### Building
1010

11-
To enable hw acceleration :
11+
```
12+
git clone --recurse-submodules -j8 https://github.com/espressif/esp-idf master
13+
```
1214

13-
* Uncomment out `#define WOLFSSL_ESPIDF` in `/path/to/wolfssl/wolfssl/wolfcrypt/settings.h`
14-
* Uncomment out `#define WOLFSSL_ESP32` in `/path/to/wolfssl/wolfssl/wolfcrypt/settings.h`
15+
Hardware acceleration is enabled by default.
1516

1617
To disable portions of the hardware acceleration you can optionally define:
1718

@@ -28,7 +29,11 @@ To disable portions of the hardware acceleration you can optionally define:
2829

2930
### Coding
3031

31-
In your application you must include `<wolfssl/wolfcrypt/settings.h>` before any other wolfSSL headers. If building the sources directly we recommend defining `WOLFSSL_USER_SETTINGS` and adding your own `user_settings.h` file. You can find a good reference for this in `IDE/GCC-ARM/Header/user_settings.h`.
32+
In your application you must include `<wolfssl/wolfcrypt/settings.h>` before any other wolfSSL headers.
33+
If building the sources directly we recommend defining `WOLFSSL_USER_SETTINGS` and adding your own
34+
`user_settings.h` file. You can find a good reference for this in `IDE/GCC-ARM/Header/user_settings.h`.
35+
36+
To view disassembly, add `__attribute__((section(".iram1")))` decorator. Foe example:
3237

3338
To view disassembly, add `__attribute__((section(".iram1")))` decorator. Foe example:
3439

0 commit comments

Comments
 (0)