88#include <string.h>
99#include <assert.h>
1010
11+ #if !ESP_TEE_BUILD
1112#include "freertos/FreeRTOS.h"
1213#include "freertos/task.h"
13-
1414#include "esp_timer.h"
15+ #else
16+ #include "esp_rom_sys.h"
17+ #include "esp_cpu.h"
18+ #endif
19+
1520#include "esp_ds.h"
1621#include "esp_crypto_lock.h"
1722#include "esp_crypto_periph_clk.h"
3237#include "hal/sha_ll.h"
3338#endif /* !CONFIG_IDF_TARGET_ESP32S2 */
3439
35- #if CONFIG_IDF_TARGET_ESP32S2
36- #include "esp32s2/rom/digital_signature.h"
37- #endif
38-
39- #if CONFIG_IDF_TARGET_ESP32S3
40- #include "esp32s3/rom/digital_signature.h"
41- #endif
42-
43- #if CONFIG_IDF_TARGET_ESP32C3
44- #include "esp32c3/rom/digital_signature.h"
45- #endif
46-
47- #if CONFIG_IDF_TARGET_ESP32C6
48- #include "esp32c6/rom/digital_signature.h"
49- #endif
50-
51- #if CONFIG_IDF_TARGET_ESP32C5
52- #include "esp32c5/rom/digital_signature.h"
53- #endif
54-
55- #if CONFIG_IDF_TARGET_ESP32H2
56- #include "esp32h2/rom/digital_signature.h"
57- #endif
58-
59- #if CONFIG_IDF_TARGET_ESP32H21
60- #include "esp32h21/rom/digital_signature.h"
61- #endif
62-
63- #if CONFIG_IDF_TARGET_ESP32P4
64- #include "esp32p4/rom/digital_signature.h"
65- #endif
66-
67- struct esp_ds_context {
68- const ets_ds_data_t * data ;
69- };
70-
7140/**
7241 * The vtask delay \c esp_ds_sign() is using while waiting for completion of the signing operation.
7342 */
@@ -263,6 +232,15 @@ esp_err_t esp_ds_encrypt_params(esp_ds_data_t *data,
263232
264233#else /* !CONFIG_IDF_TARGET_ESP32S2 (targets other than esp32s2) */
265234
235+ static inline int64_t get_time_us (void )
236+ {
237+ #if !ESP_TEE_BUILD
238+ return esp_timer_get_time ();
239+ #else
240+ return (int64_t )esp_cpu_get_cycle_count () / (int64_t )esp_rom_get_cpu_ticks_per_us ();
241+ #endif
242+ }
243+
266244static void ds_acquire_enable (void )
267245{
268246 esp_crypto_ds_lock_acquire ();
@@ -301,14 +279,23 @@ esp_err_t esp_ds_sign(const void *message,
301279 return ESP_ERR_INVALID_ARG ;
302280 }
303281
304- esp_ds_context_t * context ;
282+ esp_ds_context_t * context = NULL ;
283+ #if ESP_TEE_BUILD
284+ esp_ds_context_t ctx ;
285+ context = & ctx ;
286+ #endif
287+
305288 esp_err_t result = esp_ds_start_sign (message , data , key_id , & context );
306289 if (result != ESP_OK ) {
307290 return result ;
308291 }
309292
310293 while (esp_ds_is_busy ()) {
294+ #if !ESP_TEE_BUILD
311295 vTaskDelay (ESP_DS_SIGN_TASK_DELAY_MS / portTICK_PERIOD_MS );
296+ #else
297+ esp_rom_delay_us (1 );
298+ #endif
312299 }
313300
314301 return esp_ds_finish_sign (signature , context );
@@ -349,16 +336,18 @@ esp_err_t esp_ds_start_sign(const void *message,
349336 ds_hal_start ();
350337
351338 // check encryption key from HMAC
352- int64_t start_time = esp_timer_get_time ();
339+ int64_t start_time = get_time_us ();
353340 while (ds_ll_busy () != 0 ) {
354- if ((esp_timer_get_time () - start_time ) > SOC_DS_KEY_CHECK_MAX_WAIT_US ) {
341+ if ((get_time_us () - start_time ) > SOC_DS_KEY_CHECK_MAX_WAIT_US ) {
355342 ds_disable_release ();
356343 return ESP_ERR_HW_CRYPTO_DS_INVALID_KEY ;
357344 }
358345 }
359346
360- esp_ds_context_t * context = malloc (sizeof (esp_ds_context_t ));
361- if (!context ) {
347+ #if !ESP_TEE_BUILD
348+ * esp_ds_ctx = malloc (sizeof (esp_ds_context_t ));
349+ #endif
350+ if (!* esp_ds_ctx ) {
362351 ds_disable_release ();
363352 return ESP_ERR_NO_MEM ;
364353 }
@@ -371,8 +360,7 @@ esp_err_t esp_ds_start_sign(const void *message,
371360 // initiate signing
372361 ds_hal_start_sign ();
373362
374- context -> data = (const ets_ds_data_t * )data ;
375- * esp_ds_ctx = context ;
363+ (* esp_ds_ctx )-> data = (const ets_ds_data_t * )data ;
376364
377365 return ESP_OK ;
378366}
@@ -405,7 +393,9 @@ esp_err_t esp_ds_finish_sign(void *signature, esp_ds_context_t *esp_ds_ctx)
405393 return_value = ESP_ERR_HW_CRYPTO_DS_INVALID_PADDING ;
406394 }
407395
396+ #if !ESP_TEE_BUILD
408397 free (esp_ds_ctx );
398+ #endif
409399
410400 hmac_hal_clean ();
411401
0 commit comments