Skip to content

Commit ac6181d

Browse files
author
gojimmypi
committed
Improved Espressif SHA HW/SW selection
1 parent 578735e commit ac6181d

5 files changed

Lines changed: 56 additions & 13 deletions

File tree

wolfcrypt/src/port/Espressif/esp32_sha.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,10 @@ int esp_sha256_ctx_copy(struct wc_Sha256* src, struct wc_Sha256* dst)
679679
} /* esp_sha256_ctx_copy */
680680
#endif
681681

682-
#if defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
682+
#if !(defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384) && \
683+
defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512) \
684+
) && \
685+
(defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512))
683686
/*
684687
** internal sha384 ctx copy for ESP HW
685688
*/
@@ -744,7 +747,10 @@ int esp_sha384_ctx_copy(struct wc_Sha512* src, struct wc_Sha512* dst)
744747
} /* esp_sha384_ctx_copy */
745748
#endif
746749

747-
#if defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
750+
#if !(defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384) && \
751+
defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512) \
752+
) && \
753+
(defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512))
748754
/*
749755
** Internal sha512 ctx copy for ESP HW.
750756
** If HW already active, fall back to SW for this ctx.
@@ -1190,7 +1196,7 @@ int esp_sha_try_hw_lock(WC_ESP32SHA* ctx)
11901196
ESP_LOGE(TAG, "unexpected error in esp_sha_try_hw_lock.");
11911197
return ESP_FAIL;
11921198
}
1193-
#else /* not ESP_FAILfined(SINGLE_THREADED) */
1199+
#else /* not SINGLE_THREADED */
11941200
/*
11951201
** there's only one SHA engine for all the hash types
11961202
** so when any hash is in use, no others can use it.
@@ -2013,7 +2019,7 @@ int wc_esp_digest_state(WC_ESP32SHA* ctx, byte* hash)
20132019
pwrd1[i] ^= pwrd1[i + 1];
20142020
}
20152021
}
2016-
#endif
2022+
#endif /* SHA512 or SHA384*/
20172023
#endif /* not CONFIG_IDF_TARGET_ESP32S3, C3, else... */
20182024

20192025
ESP_LOGV(TAG, "leave esp_digest_state");
@@ -2122,6 +2128,9 @@ int esp_sha256_digest_process(struct wc_Sha256* sha, byte blockprocess)
21222128
}
21232129

21242130
wc_esp_digest_state(&sha->ctx, (byte*)sha->digest);
2131+
#else
2132+
ESP_LOGE(TAG, "Call esp_sha256_digest_process with "
2133+
"NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256 ");
21252134
#endif
21262135
ESP_LOGV(TAG, "leave esp_sha256_digest_process");
21272136
return ret;
@@ -2130,7 +2139,10 @@ int esp_sha256_digest_process(struct wc_Sha256* sha, byte blockprocess)
21302139

21312140
#endif /* NO_SHA256 */
21322141

2133-
#if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
2142+
#if !(defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384) && \
2143+
defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512) \
2144+
) && \
2145+
(defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384))
21342146
/*
21352147
** sha512 process. this is used for sha384 too.
21362148
*/

wolfcrypt/src/sha256.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ on the specific device platform.
109109
**
110110
** Beware of possible conflict in test.c (that one now named TEST_TAG)
111111
*/
112-
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
112+
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
113+
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
113114
static const char* TAG = "wc_sha256";
114115
#endif
115116
#endif
@@ -731,7 +732,7 @@ static int InitSha256(wc_Sha256* sha256)
731732
sha256->hiLen = 0;
732733

733734
#ifndef NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256
734-
ret = esp_sha_init(&(sha256->ctx), WC_HASH_TYPE_SHA256);
735+
ret = esp_sha_init((WC_ESP32SHA*)&(sha256->ctx), WC_HASH_TYPE_SHA256);
735736
#endif
736737
return ret;
737738
}
@@ -748,15 +749,14 @@ static int InitSha256(wc_Sha256* sha256)
748749
return BAD_FUNC_ARG;
749750
}
750751

751-
#ifdef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
752-
#ifndef NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256
752+
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
753+
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
753754
/* We know this is a fresh, uninitialized item, so set to INIT */
754755
if (sha256->ctx.mode != ESP32_SHA_INIT) {
755756
ESP_LOGV(TAG, "Set ctx mode from prior value: "
756757
"%d", sha256->ctx.mode);
757758
}
758759
sha256->ctx.mode = ESP32_SHA_INIT;
759-
#endif
760760
#endif
761761

762762
return InitSha256(sha256);

wolfcrypt/src/sha512.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@
4040
* but individual components can be turned off. See user_settings.h
4141
*/
4242
#define WOLFSSL_USE_ESP32_CRYPT_HASH_HW
43-
static const char* TAG = "wc_sha_512";
43+
#if !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384) && \
44+
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
45+
static const char* TAG = "wc_sha_512";
46+
#endif
4447
#else
4548
#undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
4649
#endif

wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ enum {
238238
** See NO_HW_MATH_TEST.
239239
**
240240
*******************************************************************************
241+
** WOLFSSL_FULL_WOLFSSH_SUPPORT
242+
** TODO - there's a known, unresolved problem with SHA256 in wolfSSH
243+
** Until fixed by a release version or this macro being define once resolved,
244+
** this macro should remain undefined.
245+
**
241246
*/
242247
#ifdef WOLFSSL_ESP32_CRYPT_DEBUG
243248
#undef LOG_LOCAL_LEVEL
@@ -452,7 +457,10 @@ enum {
452457
#endif
453458

454459
#ifdef SINGLE_THREADED
455-
#undef ESP_MONITOR_HW_TASK_LOCK
460+
#ifdef WOLFSSL_DEBUG_MUTEX
461+
#undef ESP_MONITOR_HW_TASK_LOCK
462+
#define ESP_MONITOR_HW_TASK_LOCK
463+
#endif
456464
#else
457465
/* Unless explicitly disabled, monitor task lock when not single thread. */
458466
#ifndef ESP_DISABLE_HW_TASK_LOCK
@@ -616,7 +624,7 @@ extern "C"
616624
{
617625
/* pointer to object the initialized HW; to track copies */
618626
void* initializer;
619-
#ifndef SINGLE_THREADED
627+
#if !defined(SINGLE_THREADED) || defined(ESP_MONITOR_HW_TASK_LOCK)
620628
void* task_owner;
621629
#endif
622630

@@ -857,6 +865,16 @@ extern "C"
857865
}
858866
#endif
859867

868+
/* Compatibility checks */
869+
#if defined(DEBUG_WOLFSSH) || defined(ESP_ENABLE_WOLFSSH) || \
870+
defined(WOLFSSH_TERM) || defined(WOLFSSH_TEST_SERVER)
871+
#ifndef NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256
872+
/* need to add this line to wolfssl component user_settings.h
873+
* #define NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256 */
874+
#error "ESP32_CRYPT_HASH_SHA256 not supported on wolfSSL at this time"
875+
#endif
876+
#endif /* SSH SHA256 HW check */
877+
860878
#endif /* WOLFSSL_ESPIDF (entire contents excluded when not Espressif ESP-IDF) */
861879

862880
#endif /* __ESP32_CRYPT_H__ */

wolfssl/wolfcrypt/settings.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,16 @@
436436
#define WC_NO_CACHE_RESISTANT
437437
#endif /* !WOLFSSL_ESPIDF_NO_DEFAULT */
438438

439+
#if defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384) && \
440+
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
441+
#error "NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384 cannot be defined without" \
442+
"NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512 (enable or disable both)"
443+
#endif
444+
#if defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512) && \
445+
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384)
446+
#error "NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512 cannot be defined without" \
447+
"NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384 (enable or disable both)"
448+
#endif
439449
#if defined(WOLFSSL_ESPWROOM32)
440450
/* WOLFSSL_ESPWROOM32 is a legacy macro gate.
441451
** Not be be confused with WOLFSSL_ESPWROOM32SE, naming a specific board */

0 commit comments

Comments
 (0)