Skip to content

Commit 448b836

Browse files
Merge pull request #7035 from gojimmypi/PR-Espressif-wolfcrypt
Espressif wolfcrypt updates
2 parents ae9632b + 7de5710 commit 448b836

6 files changed

Lines changed: 394 additions & 116 deletions

File tree

wolfcrypt/benchmark/benchmark.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@
309309
defined(CONFIG_IDF_TARGET_ESP32S3)
310310
#include <xtensa/hal.h>
311311
#else
312-
#error "CONFIG_IDF_TARGET not implemented"
312+
/* Other platform */
313313
#endif
314314
#include <esp_log.h>
315315
#endif /* WOLFSSL_ESPIDF */
@@ -1259,12 +1259,16 @@ static const char* bench_result_words3[][5] = {
12591259
/* reminder: unsigned long long max = 18,446,744,073,709,551,615 */
12601260

12611261
/* the currently observed clock counter value */
1262-
#if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6)
12631262
uint64_t thisVal = 0;
1263+
#if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6)
12641264
ESP_ERROR_CHECK(gptimer_get_raw_count(esp_gptimer, &thisVal));
12651265
#else
12661266
/* reminder unsupported CONFIG_IDF_TARGET captured above */
1267-
uint64_t thisVal = xthal_get_ccount();
1267+
#ifndef __XTENSA__
1268+
thisVal = esp_cpu_get_cycle_count();
1269+
#else
1270+
thisVal = xthal_get_ccount(); /* or esp_cpu_get_cycle_count(); */
1271+
#endif
12681272
#endif
12691273
/* if the current value is less than the previous value,
12701274
** we likely overflowed at least once.
@@ -1296,7 +1300,11 @@ static const char* bench_result_words3[][5] = {
12961300
ESP_ERROR_CHECK(gptimer_get_raw_count(esp_gptimer,
12971301
&_xthal_get_ccount_last));
12981302
#else
1299-
_xthal_get_ccount_last = xthal_get_ccount();
1303+
#ifndef __XTENSA__
1304+
thisVal = esp_cpu_get_cycle_count();
1305+
#else
1306+
thisVal = xthal_get_ccount(); /* or esp_cpu_get_cycle_count(); */
1307+
#endif
13001308
#endif
13011309
return _xthal_get_ccount_ex;
13021310
}

wolfcrypt/src/port/Espressif/esp32_mp.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
#endif
9191

9292
#ifndef ESP_RSA_EXPT_YBITS
93-
#define ESP_RSA_EXPT_YBITS 8
93+
#define ESP_RSA_EXPT_YBITS 8
9494
#endif
9595

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

141141
/* usage metrics can be turned on independently of debugging */
142142
#ifdef WOLFSSL_HW_METRICS
143-
static unsigned long esp_mp_max_used = 0;
143+
static unsigned long esp_mp_max_used = 0;
144144

145145
static unsigned long esp_mp_mulmod_small_x_ct = 0;
146146
static unsigned long esp_mp_mulmod_small_y_ct = 0;
147147

148-
#ifndef NO_WOLFSSL_ESP32_CRYPT_RSA_PRI_MP_MUL
148+
static unsigned long esp_mp_max_timeout = 0;
149+
150+
#ifndef NO_WOLFSSL_ESP32_CRYPT_RSA_PRI_MP_MUL
149151
static unsigned long esp_mp_mul_usage_ct = 0;
150152
static unsigned long esp_mp_mul_error_ct = 0;
151153
#endif /* !NO_WOLFSSL_ESP32_CRYPT_RSA_PRI_MP_MUL */
@@ -236,6 +238,13 @@ static int esp_mp_hw_wait_clean(void)
236238
/* no HW timeout if we don't know the platform. assumes no HW */
237239
#endif
238240

241+
#if defined(WOLFSSL_HW_METRICS)
242+
{
243+
esp_mp_max_timeout = (timeout > esp_mp_max_timeout) ? timeout :
244+
esp_mp_max_timeout;
245+
}
246+
#endif
247+
239248
if (ESP_TIMEOUT(timeout)) {
240249
ESP_LOGE(TAG, "esp_mp_hw_wait_clean waiting HW ready timed out.");
241250
ret = WC_HW_WAIT_E; /* hardware is busy, MP_HW_BUSY; */
@@ -1016,7 +1025,7 @@ int esp_mp_montgomery_init(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* M,
10161025
}
10171026
if ((X == NULL) || (Y == NULL) || (M == NULL) ) {
10181027
/* if a bad operand passed, we cannot use HW */
1019-
ESP_LOGE(TAG, "ERROR: Bad montgomery operand, falling back to SW");
1028+
ESP_LOGE(TAG, "ERROR: Bad Montgomery operand, falling back to SW");
10201029
return MP_HW_FALLBACK;
10211030
}
10221031
XMEMSET(mph, 0, sizeof(struct esp_mp_helper));
@@ -1298,7 +1307,7 @@ int esp_mp_mul(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* Z)
12981307

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

30623071
ESP_LOGI(TAG, "Max N->used: esp_mp_max_used = %lu", esp_mp_max_used);
3072+
ESP_LOGI(TAG, "Max timeout: esp_mp_max_timeout = %lu", esp_mp_max_timeout);
3073+
30633074
#else
30643075
/* no HW math, no HW math metrics */
30653076
ret = ESP_OK;

0 commit comments

Comments
 (0)