Skip to content

Commit

Permalink
Changes to build and complete wolf_test_task() successfully on ESP32s…
Browse files Browse the repository at this point in the history
…3 (and ESP32).

* esp32-crypt.h - removed ets_sys.h include; not required for ESP32 and breaks ESP32s3 builds.
* esp32_sha.c - update wc_esp_digest_state to skip reading digest for first block and correctly decode hash for SHA2_512.
* esp32_mp.c fix mul, mulmod, exptmod to pass tests ESP32s3
* random.c - added missing include required to build on Espressif devices when not using hardware acceleration.
* aes.c - return error code when setting Aes key if specific algorithms have been disabled (otherwise tests fail).
* user_settings.h
** disable ED25519 as it fails test for ESP32 & ESP32s3
** disable AES-192 as it is not supported on ESP32s3.
  • Loading branch information
Paul authored and Paul committed Jan 3, 2023
1 parent 56d587d commit b22d185
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 74 deletions.
20 changes: 19 additions & 1 deletion IDE/Espressif/ESP-IDF/user_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@

/* #define DEBUG_WOLFSSL_VERBOSE */

// Enable key generation:
// https://www.wolfssl.com/documentation/manuals/wolfssl/chapter07.html#rsa-key-generation
#define WOLFSSL_KEY_GEN

// Enable certificate generation:
// https://www.wolfssl.com/documentation/manuals/wolfssl/chapter07.html#certificate-generation
#define WOLFSSL_CERT_GEN

// Enable certificate request generation:
// https://www.wolfssl.com/documentation/manuals/wolfssl/chapter07.html#certificate-signing-request-csr-generation
#define WOLFSSL_CERT_REQ

#define BENCH_EMBEDDED
#define USE_CERT_BUFFERS_2048

Expand All @@ -60,7 +72,13 @@
#define HAVE_ECC
#define HAVE_CURVE25519
#define CURVE25519_SMALL
#define HAVE_ED25519

// ED25519 test fails on ESP32 & ESP32s3
// #define HAVE_ED25519

/* AES-192 is not supported on the ESP32s3; is available for ESP32
* and may be available for other devices. */
#define NO_AES_192

/* when you want to use pkcs7 */
/* #define HAVE_PKCS7 */
Expand Down
17 changes: 17 additions & 0 deletions wolfcrypt/src/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2676,6 +2676,23 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(
return BAD_FUNC_ARG;
}

#if !defined(WOLFSSL_AES_128)
if (keylen == 16) {
return BAD_FUNC_ARG;
}
#endif

#if !defined(WOLFSSL_AES_192)
if (keylen == 24) {
return BAD_FUNC_ARG;
}
#endif
#if !defined(WOLFSSL_AES_256)
if (keylen == 32) {
return BAD_FUNC_ARG;
}
#endif

aes->keylen = keylen;
aes->rounds = keylen/4 + 6;

Expand Down
3 changes: 3 additions & 0 deletions wolfcrypt/src/port/Espressif/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# ESP32 Port

Support for the ESP32-WROOM-32 on-board crypto hardware acceleration for symmetric AES, SHA1/SHA256/SHA384/SHA512 and RSA primitive including mul, mulmod and exptmod.
Supported hardware includes:
* ESP32
* ESP32s3

## ESP32 Acceleration

Expand Down
44 changes: 36 additions & 8 deletions wolfcrypt/src/port/Espressif/esp32_aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@

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

#include <wolfssl/wolfcrypt/error-crypt.h>

static const char* TAG = "wolf_hw_aes";

/* mutex */
Expand All @@ -56,7 +57,6 @@ static int espaes_CryptHwMutexInit = 0;
static int esp_aes_hw_InUse()
{
int ret = 0;
printf("esp_aes_hw_InUse\n");

ESP_LOGV(TAG, "enter esp_aes_hw_InUse");

Expand Down Expand Up @@ -114,8 +114,9 @@ static void esp_aes_hw_Leave( void )

/*
* set key to hardware key registers.
* return 0 on success; -1 if mode isn't supported.
*/
static void esp_aes_hw_Set_KeyMode(Aes *ctx, ESP32_AESPROCESS mode)
static int esp_aes_hw_Set_KeyMode(Aes *ctx, ESP32_AESPROCESS mode)
{
word32 i;
word32 mode_ = 0;
Expand All @@ -132,7 +133,7 @@ static void esp_aes_hw_Set_KeyMode(Aes *ctx, ESP32_AESPROCESS mode)
}
else {
ESP_LOGE(TAG, " >> unexpected error.");
return;
return -1;
}
}

Expand Down Expand Up @@ -161,10 +162,13 @@ static void esp_aes_hw_Set_KeyMode(Aes *ctx, ESP32_AESPROCESS mode)
if (mode_ == 1 || mode_ == 5 || mode_ == 7)
{
ESP_LOGE(TAG, " unsupported mode");
return -1;
}
#endif
DPORT_REG_WRITE(AES_MODE_REG, mode_);
ESP_LOGV(TAG, " leave esp_aes_hw_Setkey");

return 0;
}

/*
Expand Down Expand Up @@ -232,14 +236,20 @@ static void esp_aes_bk(const byte* in, byte* out)
* @param in : a pointer of the input buffer containing plain text to be encrypted
* @param out: a pointer of the output buffer in which to store the cipher text of
* the encrypted message
* @return: 0 on success, BAD_FUNC_ARG if the AES algorithm isn't supported.
*/
int wc_esp32AesEncrypt(Aes *aes, const byte* in, byte* out)
{
ESP_LOGV(TAG, "enter wc_esp32AesEncrypt");
/* lock the hw engine */
esp_aes_hw_InUse();
/* load the key into the register */
esp_aes_hw_Set_KeyMode(aes, ESP32_AES_UPDATEKEY_ENCRYPT);
if (0 != esp_aes_hw_Set_KeyMode(aes, ESP32_AES_UPDATEKEY_ENCRYPT))
{
/* release hw */
esp_aes_hw_Leave();
return BAD_FUNC_ARG;
}
/* process a one block of AES */
esp_aes_bk(in, out);
/* release hw */
Expand All @@ -254,14 +264,20 @@ int wc_esp32AesEncrypt(Aes *aes, const byte* in, byte* out)
* @param in : a pointer of the input buffer containing plain text to be decrypted
* @param out: a pointer of the output buffer in which to store the cipher text of
* the decrypted message
* @return: 0 on success, BAD_FUNC_ARG if the AES algorithm isn't supported.
*/
int wc_esp32AesDecrypt(Aes *aes, const byte* in, byte* out)
{
ESP_LOGV(TAG, "enter wc_esp32AesDecrypt");
/* lock the hw engine */
esp_aes_hw_InUse();
/* load the key into the register */
esp_aes_hw_Set_KeyMode(aes, ESP32_AES_UPDATEKEY_DECRYPT);
if (0 != esp_aes_hw_Set_KeyMode(aes, ESP32_AES_UPDATEKEY_DECRYPT))
{
/* release hw */
esp_aes_hw_Leave();
return BAD_FUNC_ARG;
}
/* process a one block of AES */
esp_aes_bk(in, out);
/* release hw engine */
Expand All @@ -279,6 +295,7 @@ int wc_esp32AesDecrypt(Aes *aes, const byte* in, byte* out)
* the encrypted message
* @param in : a pointer of the input buffer containing plain text to be encrypted
* @param sz : size of input message
* @return: 0 on success, BAD_FUNC_ARG if the AES algorithm isn't supported.
*/
int wc_esp32AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
Expand All @@ -294,7 +311,12 @@ int wc_esp32AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)

esp_aes_hw_InUse();

esp_aes_hw_Set_KeyMode(aes, ESP32_AES_UPDATEKEY_ENCRYPT);
if (0 != esp_aes_hw_Set_KeyMode(aes, ESP32_AES_UPDATEKEY_ENCRYPT))
{
/* release hw */
esp_aes_hw_Leave();
return BAD_FUNC_ARG;
}

while (blocks--) {
XMEMCPY(temp_block, in + offset, AES_BLOCK_SIZE);
Expand Down Expand Up @@ -325,6 +347,7 @@ int wc_esp32AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
* the decrypted message
* @param in : a pointer of the input buffer containing plain text to be decrypted
* @param sz : size of input message
* @return: 0 on success, BAD_FUNC_ARG if the AES algorithm isn't supported.
*/
int wc_esp32AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
Expand All @@ -340,7 +363,12 @@ int wc_esp32AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)

esp_aes_hw_InUse();

esp_aes_hw_Set_KeyMode(aes, ESP32_AES_UPDATEKEY_DECRYPT);
if (0 != esp_aes_hw_Set_KeyMode(aes, ESP32_AES_UPDATEKEY_DECRYPT))
{
/* release hw */
esp_aes_hw_Leave();
return BAD_FUNC_ARG;
}

while (blocks--) {
XMEMCPY(temp_block, in + offset, AES_BLOCK_SIZE);
Expand Down
Loading

0 comments on commit b22d185

Please sign in to comment.