Skip to content

Commit

Permalink
Merge pull request #7035 from gojimmypi/PR-Espressif-wolfcrypt
Browse files Browse the repository at this point in the history
Espressif wolfcrypt updates
  • Loading branch information
JacobBarthelmeh authored Dec 8, 2023
2 parents ae9632b + 7de5710 commit 448b836
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 116 deletions.
16 changes: 12 additions & 4 deletions wolfcrypt/benchmark/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@
defined(CONFIG_IDF_TARGET_ESP32S3)
#include <xtensa/hal.h>
#else
#error "CONFIG_IDF_TARGET not implemented"
/* Other platform */
#endif
#include <esp_log.h>
#endif /* WOLFSSL_ESPIDF */
Expand Down Expand Up @@ -1259,12 +1259,16 @@ static const char* bench_result_words3[][5] = {
/* reminder: unsigned long long max = 18,446,744,073,709,551,615 */

/* the currently observed clock counter value */
#if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6)
uint64_t thisVal = 0;
#if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6)
ESP_ERROR_CHECK(gptimer_get_raw_count(esp_gptimer, &thisVal));
#else
/* reminder unsupported CONFIG_IDF_TARGET captured above */
uint64_t thisVal = xthal_get_ccount();
#ifndef __XTENSA__
thisVal = esp_cpu_get_cycle_count();
#else
thisVal = xthal_get_ccount(); /* or esp_cpu_get_cycle_count(); */
#endif
#endif
/* if the current value is less than the previous value,
** we likely overflowed at least once.
Expand Down Expand Up @@ -1296,7 +1300,11 @@ static const char* bench_result_words3[][5] = {
ESP_ERROR_CHECK(gptimer_get_raw_count(esp_gptimer,
&_xthal_get_ccount_last));
#else
_xthal_get_ccount_last = xthal_get_ccount();
#ifndef __XTENSA__
thisVal = esp_cpu_get_cycle_count();
#else
thisVal = xthal_get_ccount(); /* or esp_cpu_get_cycle_count(); */
#endif
#endif
return _xthal_get_ccount_ex;
}
Expand Down
21 changes: 16 additions & 5 deletions wolfcrypt/src/port/Espressif/esp32_mp.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
#endif

#ifndef ESP_RSA_EXPT_YBITS
#define ESP_RSA_EXPT_YBITS 8
#define ESP_RSA_EXPT_YBITS 8
#endif

#define ESP_TIMEOUT(cnt) (cnt >= ESP_RSA_TIMEOUT_CNT)
Expand Down Expand Up @@ -140,12 +140,14 @@ static portMUX_TYPE wc_rsa_reg_lock = portMUX_INITIALIZER_UNLOCKED;

/* usage metrics can be turned on independently of debugging */
#ifdef WOLFSSL_HW_METRICS
static unsigned long esp_mp_max_used = 0;
static unsigned long esp_mp_max_used = 0;

static unsigned long esp_mp_mulmod_small_x_ct = 0;
static unsigned long esp_mp_mulmod_small_y_ct = 0;

#ifndef NO_WOLFSSL_ESP32_CRYPT_RSA_PRI_MP_MUL
static unsigned long esp_mp_max_timeout = 0;

#ifndef NO_WOLFSSL_ESP32_CRYPT_RSA_PRI_MP_MUL
static unsigned long esp_mp_mul_usage_ct = 0;
static unsigned long esp_mp_mul_error_ct = 0;
#endif /* !NO_WOLFSSL_ESP32_CRYPT_RSA_PRI_MP_MUL */
Expand Down Expand Up @@ -236,6 +238,13 @@ static int esp_mp_hw_wait_clean(void)
/* no HW timeout if we don't know the platform. assumes no HW */
#endif

#if defined(WOLFSSL_HW_METRICS)
{
esp_mp_max_timeout = (timeout > esp_mp_max_timeout) ? timeout :
esp_mp_max_timeout;
}
#endif

if (ESP_TIMEOUT(timeout)) {
ESP_LOGE(TAG, "esp_mp_hw_wait_clean waiting HW ready timed out.");
ret = WC_HW_WAIT_E; /* hardware is busy, MP_HW_BUSY; */
Expand Down Expand Up @@ -1016,7 +1025,7 @@ int esp_mp_montgomery_init(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* M,
}
if ((X == NULL) || (Y == NULL) || (M == NULL) ) {
/* if a bad operand passed, we cannot use HW */
ESP_LOGE(TAG, "ERROR: Bad montgomery operand, falling back to SW");
ESP_LOGE(TAG, "ERROR: Bad Montgomery operand, falling back to SW");
return MP_HW_FALLBACK;
}
XMEMSET(mph, 0, sizeof(struct esp_mp_helper));
Expand Down Expand Up @@ -1298,7 +1307,7 @@ int esp_mp_mul(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* Z)

resultWords_sz = bits2words(Xs + Ys);
/* sanity check */
if((hwWords_sz << 5) > ESP_HW_MULTI_RSAMAX_BITS) {
if ( (hwWords_sz << 5) > ESP_HW_MULTI_RSAMAX_BITS) {
ESP_LOGW(TAG, "exceeds max bit length(2048) (a)");
ret = MP_HW_FALLBACK; /* Error: value is not able to be used. */
}
Expand Down Expand Up @@ -3060,6 +3069,8 @@ int esp_hw_show_mp_metrics(void)
#endif /* EXPTMOD not disabled !NO_WOLFSSL_ESP32_CRYPT_RSA_PRI_EXPTMOD */

ESP_LOGI(TAG, "Max N->used: esp_mp_max_used = %lu", esp_mp_max_used);
ESP_LOGI(TAG, "Max timeout: esp_mp_max_timeout = %lu", esp_mp_max_timeout);

#else
/* no HW math, no HW math metrics */
ret = ESP_OK;
Expand Down
Loading

0 comments on commit 448b836

Please sign in to comment.