Skip to content

Commit b57fcd0

Browse files
author
gojimmypi
committed
Update Espressif sha, util, mem, time helpers
1 parent 80a63a3 commit b57fcd0

6 files changed

Lines changed: 274 additions & 43 deletions

File tree

wolfcrypt/src/port/Espressif/esp32_sha.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ static const char* TAG = "wolf_hw_sha";
135135
#endif
136136

137137
static uintptr_t mutex_ctx_owner = NULLPTR;
138+
139+
#if (defined(ESP_MONITOR_HW_TASK_LOCK) && !defined(SINGLE_THREADED)) \
140+
|| defined(WOLFSSL_DEBUG_MUTEX)
138141
static portMUX_TYPE sha_crit_sect = portMUX_INITIALIZER_UNLOCKED;
142+
#endif
139143

140144
#if defined(ESP_MONITOR_HW_TASK_LOCK)
141145
#ifdef SINGLE_THREADED
@@ -506,7 +510,7 @@ int esp_sha224_ctx_copy(struct wc_Sha256* src, struct wc_Sha256* dst)
506510
dst->ctx.initializer = (uintptr_t)&dst->ctx;
507511
#if defined(ESP_MONITOR_HW_TASK_LOCK) && !defined(SINGLE_THREADED)
508512
{
509-
/* not HW mode for copy, so we are not interested in task owner: */
513+
/* Not HW mode for copy, so we are not interested in task owner: */
510514
dst->ctx.task_owner = 0;
511515
}
512516
#endif
@@ -985,8 +989,10 @@ int esp_sha_hw_in_use()
985989
*/
986990
uintptr_t esp_sha_hw_islocked(WC_ESP32SHA* ctx)
987991
{
988-
TaskHandle_t mutexHolder;
989992
uintptr_t ret = 0;
993+
#ifndef SINGLE_THREADED
994+
TaskHandle_t mutexHolder;
995+
#endif
990996
CTX_STACK_CHECK(ctx);
991997

992998
#ifdef WOLFSSL_DEBUG_MUTEX
@@ -1132,7 +1138,9 @@ uintptr_t esp_sha_release_unfinished_lock(WC_ESP32SHA* ctx)
11321138
ESP_LOGW(TAG, "esp_sha_release_unfinished_lock mode = %d", ctx->mode);
11331139
#endif
11341140
if (ctx->mode == ESP32_SHA_HW) {
1141+
#if defined(DEBUG_WOLFSSL_ESP32_UNFINISHED_HW)
11351142
ESP_LOGW(TAG, "esp_sha_release_unfinished_lock HW!");
1143+
#endif
11361144
}
11371145
}
11381146
return ret;

wolfcrypt/src/port/Espressif/esp32_util.c

Lines changed: 123 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,39 +98,81 @@ int esp_CryptHwMutexInit(wolfSSL_Mutex* mutex) {
9898
}
9999

100100
/*
101-
* call the ESP-IDF mutex lock; xSemaphoreTake
101+
* Call the ESP-IDF mutex lock; xSemaphoreTake
102102
* this is a general mutex locker, used for different mutex objects for
103103
* different HW acclerators or other single-use HW features.
104+
*
105+
* We should already have known if the resource is in use or not.
106+
*
107+
* Return 0 (ESP_OK) on success, otherwise BAD_MUTEX_E
104108
*/
105109
int esp_CryptHwMutexLock(wolfSSL_Mutex* mutex, TickType_t block_time) {
110+
int ret;
106111
if (mutex == NULL) {
107112
WOLFSSL_ERROR_MSG("esp_CryptHwMutexLock called with null mutex");
108113
return BAD_MUTEX_E;
109114
}
110115

111116
#ifdef SINGLE_THREADED
112-
return wc_LockMutex(mutex); /* xSemaphoreTake take with portMAX_DELAY */
117+
/* does nothing in single thread mode, always return 0 */
118+
ret = wc_LockMutex(mutex);
113119
#else
114-
return ((xSemaphoreTake(*mutex, block_time) == pdTRUE) ? 0 : BAD_MUTEX_E);
120+
ret = xSemaphoreTake(*mutex, block_time);
121+
ESP_LOGV(TAG, "xSemaphoreTake 0x%x = %d", (intptr_t)*mutex, ret);
122+
if (ret == pdTRUE) {
123+
ret = ESP_OK;
124+
}
125+
else {
126+
if (ret == pdFALSE) {
127+
ESP_LOGW(TAG, "xSemaphoreTake failed for 0x%x. Still busy?",
128+
(intptr_t)*mutex);
129+
ret = ESP_ERR_NOT_FINISHED;
130+
}
131+
else {
132+
ESP_LOGE(TAG, "xSemaphoreTake 0x%x unexpected = %d",
133+
(intptr_t)*mutex, ret);
134+
ret = BAD_MUTEX_E;
135+
}
136+
}
115137
#endif
138+
return ret;
116139
}
117140

118141
/*
119142
* call the ESP-IDF mutex UNlock; xSemaphoreGive
120143
*
121144
*/
122145
esp_err_t esp_CryptHwMutexUnLock(wolfSSL_Mutex* mutex) {
146+
int ret = pdTRUE;
123147
if (mutex == NULL) {
124148
WOLFSSL_ERROR_MSG("esp_CryptHwMutexLock called with null mutex");
125149
return BAD_MUTEX_E;
126150
}
127151

128152
#ifdef SINGLE_THREADED
129-
return wc_UnLockMutex(mutex);
153+
ret = wc_UnLockMutex(mutex);
130154
#else
131-
xSemaphoreGive(*mutex);
132-
return ESP_OK;
155+
ESP_LOGV(TAG, ">> xSemaphoreGive 0x%x", (intptr_t)*mutex);
156+
TaskHandle_t mutexHolder = xSemaphoreGetMutexHolder(*mutex);
157+
158+
if (mutexHolder == NULL) {
159+
ESP_LOGW(TAG, "esp_CryptHwMutexUnLock with no lock owner 0x%x",
160+
(intptr_t)*mutex);
161+
ret = ESP_OK;
162+
}
163+
else {
164+
ret = xSemaphoreGive(*mutex);
165+
if (ret == pdTRUE) {
166+
ESP_LOGV(TAG, "Success: give mutex 0x%x", (intptr_t)*mutex);
167+
ret = ESP_OK;
168+
}
169+
else {
170+
ESP_LOGV(TAG, "Failed: give mutex 0x%x", (intptr_t)*mutex);
171+
ret = ESP_FAIL;
172+
}
173+
}
133174
#endif
175+
return ret;
134176
}
135177
#endif /* WOLFSSL_ESP32_CRYPT, etc. */
136178

@@ -168,6 +210,7 @@ static int ShowExtendedSystemInfo_platform_espressif(void)
168210

169211
WOLFSSL_VERSION_PRINTF("Xthal_have_ccount: %u",
170212
Xthal_have_ccount);
213+
#endif
171214

172215
/* this is the legacy stack size */
173216
#if defined(CONFIG_MAIN_TASK_STACK_SIZE)
@@ -205,24 +248,35 @@ static int ShowExtendedSystemInfo_platform_espressif(void)
205248

206249
#endif
207250

208-
#elif CONFIG_IDF_TARGET_ESP32S2
209-
WOLFSSL_VERSION_PRINTF("Xthal_have_ccount = %u",
251+
/* Platform-specific attributes of interest*/
252+
#if CONFIG_IDF_TARGET_ESP32
253+
#if defined(CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ)
254+
WOLFSSL_VERSION_PRINTF("CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ: %u MHz",
255+
CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ);
256+
#endif
257+
WOLFSSL_VERSION_PRINTF("Xthal_have_ccount: %u",
210258
Xthal_have_ccount);
211-
#elif CONFIG_IDF_TARGET_ESP32C6
212-
/* TODO find Xthal for C6 */
259+
213260
#elif CONFIG_IDF_TARGET_ESP32C2
214-
/* TODO find Xthal for C6 */
215-
#elif defined(CONFIG_IDF_TARGET_ESP8684)
216-
/* TODO find Xthal for C6 */
261+
/* TODO find Xthal for C2 */
217262
#elif CONFIG_IDF_TARGET_ESP32C3
218263
/* not supported at this time */
219-
#elif CONFIG_IDF_TARGET_ESP32S3
220-
WOLFSSL_VERSION_PRINTF("Xthal_have_ccount = %u",
221-
Xthal_have_ccount);
264+
#elif CONFIG_IDF_TARGET_ESP32C6
265+
/* TODO find Xthal for C6 */
222266
#elif CONFIG_IDF_TARGET_ESP32H2
223-
/* not supported at this time */
224-
#elif CONFIG_IDF_TARGET_ESP32C2
225-
/* not supported at this time */
267+
/* TODO find Xthal for H2 */
268+
#elif CONFIG_IDF_TARGET_ESP32S2
269+
ESP_LOGI(TAG, "CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ = %u MHz",
270+
CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ
271+
);
272+
ESP_LOGI(TAG, "Xthal_have_ccount = %u", Xthal_have_ccount);
273+
#elif CONFIG_IDF_TARGET_ESP32S3
274+
ESP_LOGI(TAG, "CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ = %u MHz",
275+
CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ
276+
);
277+
ESP_LOGI(TAG, "Xthal_have_ccount = %u", Xthal_have_ccount);
278+
#elif defined(CONFIG_IDF_TARGET_ESP8684)
279+
/* TODO find Xthal for ESP8684 */
226280
#else
227281
/* not supported at this time */
228282
#endif
@@ -438,6 +492,7 @@ esp_err_t ShowExtendedSystemInfo_config(void)
438492
{
439493
esp_ShowMacroStatus_need_header = 1;
440494

495+
show_macro("NO_ESP32_CRYPT", STR_IFNDEF(NO_ESP32_CRYPT));
441496
show_macro("NO_ESPIDF_DEFAULT", STR_IFNDEF(NO_ESPIDF_DEFAULT));
442497

443498
show_macro("HW_MATH_ENABLED", STR_IFNDEF(HW_MATH_ENABLED));
@@ -562,11 +617,11 @@ int ShowExtendedSystemInfo(void)
562617

563618
#if defined(WOLFSSL_MULTI_INSTALL_WARNING)
564619
/* CMake may have detected undesired multiple installs, so give warning. */
565-
WOLFSSL_VERSION_PRINTF("");
620+
WOLFSSL_VERSION_PRINTF(WOLFSSL_ESPIDF_BLANKLINE_MESSAGE);
566621
WOLFSSL_VERSION_PRINTF("WARNING: Multiple wolfSSL installs found.");
567622
WOLFSSL_VERSION_PRINTF("Check ESP-IDF components and "
568623
"local project [components] directory.");
569-
WOLFSSL_VERSION_PRINTF("");
624+
WOLFSSL_VERSION_PRINTF(WOLFSSL_ESPIDF_BLANKLINE_MESSAGE);
570625
#else
571626
#ifdef WOLFSSL_USER_SETTINGS_DIR
572627
{
@@ -737,14 +792,11 @@ esp_err_t esp_EnabledWatchdog(void)
737792
ESP_IDF_VERSION_MAJOR);
738793
#endif
739794
#endif
740-
741-
#ifdef DEBUG_WOLFSSL
742-
ESP_LOGI(TAG, "Watchdog enabled.");
743-
#endif
744-
745795
return ret;
746796
}
747797

798+
799+
748800
/* Print a MATH_INT_T attribute list.
749801
*
750802
* Note with the right string parameters, the result can be pasted as
@@ -904,4 +956,49 @@ esp_err_t esp_hw_show_metrics(void)
904956
return ESP_OK;
905957
}
906958

959+
int show_binary(byte* theVar, size_t dataSz) {
960+
printf("*****************************************************\n");
961+
word32 i;
962+
for (i = 0; i < dataSz; i++)
963+
printf("%02X", theVar[i]);
964+
printf("\n");
965+
printf("******************************************************\n");
966+
return 0;
967+
}
968+
969+
int hexToBinary(byte* toVar, const char* fromHexString, size_t szHexString ) {
970+
int ret = 0;
971+
/* Calculate the actual binary length of the hex string */
972+
size_t byteLen = szHexString / 2;
973+
974+
if (toVar == NULL || fromHexString == NULL) {
975+
ESP_LOGE("ssh", " error");
976+
return -1;
977+
}
978+
if ((szHexString % 2 != 0)) {
979+
ESP_LOGE("ssh", "fromHexString length not even!");
980+
}
981+
982+
ESP_LOGW(TAG, "Replacing %d bytes at %x", byteLen, (word32)toVar);
983+
memset(toVar, 0, byteLen);
984+
/* Iterate through the hex string and convert to binary */
985+
for (size_t i = 0; i < szHexString; i += 2) {
986+
/* Convert hex character to decimal */
987+
int decimalValue;
988+
sscanf(&fromHexString[i], "%2x", &decimalValue);
989+
size_t index = i / 2;
990+
#if (0)
991+
/* Optionall peek at new values */
992+
byte new_val = (decimalValue & 0x0F) << ((i % 2) * 4);
993+
ESP_LOGI("hex", "Current char = %d", toVar[index]);
994+
ESP_LOGI("hex", "New val = %d", decimalValue);
995+
#endif
996+
toVar[index] = decimalValue;
997+
}
998+
999+
return ret;
1000+
}
1001+
1002+
1003+
9071004
#endif /* WOLFSSL_ESPIDF */

wolfcrypt/src/port/Espressif/esp_sdk_mem_lib.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static const char* sdk_memory_segment_text[SDK_MEMORY_SEGMENT_COUNT + 1] = {
161161
int sdk_log_meminfo(enum sdk_memory_segment m, void* start, void* end)
162162
{
163163
const char* str;
164-
int len = 0;
164+
word32 len = 0;
165165
str = sdk_memory_segment_text[m];
166166
sdk_memory_segment_start[m] = start;
167167
sdk_memory_segment_end[m] = end;
@@ -173,7 +173,7 @@ int sdk_log_meminfo(enum sdk_memory_segment m, void* start, void* end)
173173
ESP_LOGI(TAG, " Start End Length");
174174
}
175175
else {
176-
len = (uint32_t)end - (uint32_t)start;
176+
len = (word32)end - (word32)start;
177177
ESP_LOGI(TAG, "%s: %p ~ %p : 0x%05x (%d)", str, start, end, len, len );
178178
}
179179
return ESP_OK;

wolfcrypt/src/port/Espressif/esp_sdk_time_lib.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@
2323
#include <config.h>
2424
#endif
2525

26-
/* Reminder: user_settings.h is needed and included from settings.h
27-
* Be sure to define WOLFSSL_USER_SETTINGS, typically in CMakeLists.txt */
28-
#include <wolfssl/wolfcrypt/settings.h>
26+
/* wolfSSL */
27+
/* Always include wolfcrypt/settings.h before any other wolfSSL file. */
28+
/* Reminder: settings.h pulls in user_settings.h; don't include it here. */
29+
#ifdef WOLFSSL_USER_SETTINGS
30+
#include <wolfssl/wolfcrypt/settings.h>
31+
#endif
32+
2933

3034
#if defined(WOLFSSL_ESPIDF) /* Entire file is only for Espressif EDP-IDF */
35+
#include "sdkconfig.h" /* programmatically generated from sdkconfig */
36+
3137
#if defined(USE_WOLFSSL_ESP_SDK_TIME)
3238
/* Espressif */
33-
#include "sdkconfig.h" /* programmatically generated from sdkconfig */
3439
#include <esp_log.h>
3540
#include <esp_err.h>
3641

@@ -145,11 +150,11 @@ int set_fixed_default_time(void)
145150
* but let's set a default time, just in case */
146151
struct tm timeinfo = {
147152
.tm_year = 2024 - 1900,
148-
.tm_mon = 1,
149-
.tm_mday = 05,
153+
.tm_mon = 9 - 1, /* Month, where 0 = Jan */
154+
.tm_mday = 3 , /* Day of the month 30 */
150155
.tm_hour = 13,
151-
.tm_min = 01,
152-
.tm_sec = 05
156+
.tm_min = 1,
157+
.tm_sec = 5
153158
};
154159
struct timeval now;
155160
time_t interim_time;

wolfssl/wolfcrypt/port/Espressif/esp-sdk-lib.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,13 @@ WOLFSSL_LOCAL esp_err_t sdk_var_whereis(const char* v_name, void* v);
148148

149149
WOLFSSL_LOCAL intptr_t esp_sdk_stack_pointer(void);
150150

151+
#if defined(USE_WOLFSSL_ESP_SDK_TIME)
152+
151153
/******************************************************************************
152154
* Time helpers
153155
******************************************************************************/
156+
WOLFSSL_LOCAL esp_err_t esp_sdk_time_mem_init(void);
157+
154158
WOLFSSL_LOCAL esp_err_t esp_sdk_time_lib_init(void);
155159

156160
/* a function to show the current data and time */
@@ -168,8 +172,9 @@ WOLFSSL_LOCAL esp_err_t set_time(void);
168172

169173
/* wait NTP_RETRY_COUNT seconds before giving up on NTP time */
170174
WOLFSSL_LOCAL esp_err_t set_time_wait_for_ntp(void);
175+
#endif
171176

172-
#ifndef NO_ESP_SDK_WIFI
177+
#if defined(USE_WOLFSSL_ESP_SDK_WIFI)
173178

174179
/******************************************************************************
175180
* WiFi helpers
@@ -201,8 +206,7 @@ WOLFSSL_LOCAL esp_err_t esp_sdk_wifi_init_sta(void);
201206

202207
WOLFSSL_LOCAL esp_err_t esp_sdk_wifi_show_ip(void);
203208

204-
#endif /* !NO_ESP_SDK_WIFI */
205-
209+
#endif /* USE_WOLFSSL_ESP_SDK_WIFI */
206210

207211
/******************************************************************************
208212
* Debug helpers

0 commit comments

Comments
 (0)