Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Espressif SHA HW/SW selection #7173

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions wolfcrypt/src/port/Espressif/esp32_sha.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,10 @@ int esp_sha256_ctx_copy(struct wc_Sha256* src, struct wc_Sha256* dst)
} /* esp_sha256_ctx_copy */
#endif

#if defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
#if !(defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384) && \
defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512) \
) && \
(defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512))
/*
** internal sha384 ctx copy for ESP HW
*/
Expand Down Expand Up @@ -744,7 +747,10 @@ int esp_sha384_ctx_copy(struct wc_Sha512* src, struct wc_Sha512* dst)
} /* esp_sha384_ctx_copy */
#endif

#if defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
#if !(defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384) && \
defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512) \
) && \
(defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512))
/*
** Internal sha512 ctx copy for ESP HW.
** If HW already active, fall back to SW for this ctx.
Expand Down Expand Up @@ -1190,7 +1196,7 @@ int esp_sha_try_hw_lock(WC_ESP32SHA* ctx)
ESP_LOGE(TAG, "unexpected error in esp_sha_try_hw_lock.");
return ESP_FAIL;
}
#else /* not ESP_FAILfined(SINGLE_THREADED) */
#else /* not SINGLE_THREADED */
/*
** there's only one SHA engine for all the hash types
** so when any hash is in use, no others can use it.
Expand Down Expand Up @@ -2013,7 +2019,7 @@ int wc_esp_digest_state(WC_ESP32SHA* ctx, byte* hash)
pwrd1[i] ^= pwrd1[i + 1];
}
}
#endif
#endif /* SHA512 or SHA384*/
#endif /* not CONFIG_IDF_TARGET_ESP32S3, C3, else... */

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

wc_esp_digest_state(&sha->ctx, (byte*)sha->digest);
#else
ESP_LOGE(TAG, "Call esp_sha256_digest_process with "
"NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256 ");
#endif
ESP_LOGV(TAG, "leave esp_sha256_digest_process");
return ret;
Expand All @@ -2130,7 +2139,10 @@ int esp_sha256_digest_process(struct wc_Sha256* sha, byte blockprocess)

#endif /* NO_SHA256 */

#if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
#if !(defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384) && \
defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512) \
) && \
(defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384))
/*
** sha512 process. this is used for sha384 too.
*/
Expand Down
10 changes: 5 additions & 5 deletions wolfcrypt/src/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ on the specific device platform.
**
** Beware of possible conflict in test.c (that one now named TEST_TAG)
*/
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
static const char* TAG = "wc_sha256";
#endif
#endif
Expand Down Expand Up @@ -731,7 +732,7 @@ static int InitSha256(wc_Sha256* sha256)
sha256->hiLen = 0;

#ifndef NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256
ret = esp_sha_init(&(sha256->ctx), WC_HASH_TYPE_SHA256);
ret = esp_sha_init((WC_ESP32SHA*)&(sha256->ctx), WC_HASH_TYPE_SHA256);
#endif
return ret;
}
Expand All @@ -748,15 +749,14 @@ static int InitSha256(wc_Sha256* sha256)
return BAD_FUNC_ARG;
}

#ifdef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
#ifndef NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
/* We know this is a fresh, uninitialized item, so set to INIT */
if (sha256->ctx.mode != ESP32_SHA_INIT) {
ESP_LOGV(TAG, "Set ctx mode from prior value: "
"%d", sha256->ctx.mode);
}
sha256->ctx.mode = ESP32_SHA_INIT;
#endif
#endif

return InitSha256(sha256);
Expand Down
5 changes: 4 additions & 1 deletion wolfcrypt/src/sha512.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
* but individual components can be turned off. See user_settings.h
*/
#define WOLFSSL_USE_ESP32_CRYPT_HASH_HW
static const char* TAG = "wc_sha_512";
#if !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
static const char* TAG = "wc_sha_512";
#endif
#else
#undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
#endif
Expand Down
22 changes: 20 additions & 2 deletions wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ enum {
** See NO_HW_MATH_TEST.
**
*******************************************************************************
** WOLFSSL_FULL_WOLFSSH_SUPPORT
** TODO - there's a known, unresolved problem with SHA256 in wolfSSH
** Until fixed by a release version or this macro being define once resolved,
** this macro should remain undefined.
**
*/
#ifdef WOLFSSL_ESP32_CRYPT_DEBUG
#undef LOG_LOCAL_LEVEL
Expand Down Expand Up @@ -452,7 +457,10 @@ enum {
#endif

#ifdef SINGLE_THREADED
#undef ESP_MONITOR_HW_TASK_LOCK
#ifdef WOLFSSL_DEBUG_MUTEX
#undef ESP_MONITOR_HW_TASK_LOCK
#define ESP_MONITOR_HW_TASK_LOCK
#endif
#else
/* Unless explicitly disabled, monitor task lock when not single thread. */
#ifndef ESP_DISABLE_HW_TASK_LOCK
Expand Down Expand Up @@ -616,7 +624,7 @@ extern "C"
{
/* pointer to object the initialized HW; to track copies */
void* initializer;
#ifndef SINGLE_THREADED
#if !defined(SINGLE_THREADED) || defined(ESP_MONITOR_HW_TASK_LOCK)
void* task_owner;
#endif

Expand Down Expand Up @@ -857,6 +865,16 @@ extern "C"
}
#endif

/* Compatibility checks */
#if defined(DEBUG_WOLFSSH) || defined(ESP_ENABLE_WOLFSSH) || \
defined(WOLFSSH_TERM) || defined(WOLFSSH_TEST_SERVER)
#ifndef NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256
/* need to add this line to wolfssl component user_settings.h
* #define NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256 */
#error "ESP32_CRYPT_HASH_SHA256 not supported on wolfSSL at this time"
#endif
#endif /* SSH SHA256 HW check */

#endif /* WOLFSSL_ESPIDF (entire contents excluded when not Espressif ESP-IDF) */

#endif /* __ESP32_CRYPT_H__ */
10 changes: 10 additions & 0 deletions wolfssl/wolfcrypt/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,16 @@
#define WC_NO_CACHE_RESISTANT
#endif /* !WOLFSSL_ESPIDF_NO_DEFAULT */

#if defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
#error "NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384 cannot be defined without" \
"NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512 (enable or disable both)"
#endif
#if defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384)
#error "NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512 cannot be defined without" \
"NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384 (enable or disable both)"
#endif
#if defined(WOLFSSL_ESPWROOM32)
/* WOLFSSL_ESPWROOM32 is a legacy macro gate.
** Not be be confused with WOLFSSL_ESPWROOM32SE, naming a specific board */
Expand Down
Loading