Skip to content

Commit ecc547f

Browse files
committed
Merge branch 'feat/esp_tee_hmac_ds_prot' into 'master'
feat(esp_tee): Protect the HMAC, DS and ECC peripherals from REE access See merge request espressif/esp-idf!38285
2 parents 8a9d659 + 4a4d63d commit ecc547f

File tree

24 files changed

+615
-190
lines changed

24 files changed

+615
-190
lines changed

components/esp_hw_support/include/esp_ds.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -14,6 +14,8 @@
1414

1515
#ifdef SOC_DIG_SIGN_SUPPORTED
1616

17+
#include "rom/digital_signature.h"
18+
1719
#ifdef __cplusplus
1820
extern "C" {
1921
#endif
@@ -38,7 +40,15 @@ extern "C" {
3840
+ ESP_DS_SIGNATURE_L_BIT_LEN \
3941
+ ESP_DS_SIGNATURE_PADDING_BIT_LEN) / 8))
4042

41-
typedef struct esp_ds_context esp_ds_context_t;
43+
/**
44+
* @brief Context object used for non-blocking digital signature operations
45+
*
46+
* This object is allocated by \c esp_ds_start_sign() and must be passed to
47+
* \c esp_ds_finish_sign() to complete the digital signature operation.
48+
*/
49+
typedef struct esp_ds_context {
50+
const ets_ds_data_t *data; /*!< Pointer to the encrypted private key data */
51+
} esp_ds_context_t;
4252

4353
typedef enum {
4454
ESP_DS_RSA_1024 = (1024 / 32) - 1,

components/esp_security/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ if(NOT non_os_build)
3232
list(APPEND srcs "src/esp_crypto_lock.c" "src/esp_crypto_periph_clk.c")
3333
list(APPEND priv_requires efuse esp_hw_support esp_system esp_timer)
3434
elseif(esp_tee_build)
35-
list(APPEND srcs "src/esp_crypto_periph_clk.c")
35+
list(APPEND srcs "src/esp_crypto_lock.c" "src/esp_crypto_periph_clk.c"
36+
"src/esp_hmac.c" "src/esp_ds.c")
3637
list(APPEND includes "src/${IDF_TARGET}")
3738
list(APPEND priv_requires esp_hw_support)
3839
endif()
@@ -44,4 +45,6 @@ idf_component_register(SRCS ${srcs}
4445

4546
if(NOT non_os_build)
4647
target_link_libraries(${COMPONENT_LIB} PRIVATE "-u esp_security_init_include_impl")
48+
elseif(esp_tee_build)
49+
target_link_libraries(${COMPONENT_LIB} PRIVATE idf::efuse)
4750
endif()

components/esp_security/src/esp_crypto_lock.c

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -18,6 +18,7 @@ DS: needs HMAC (which needs SHA), AES and MPI
1818
ECDSA: needs ECC and MPI
1919
*/
2020

21+
#if !NON_OS_BUILD
2122
#ifdef SOC_DIG_SIGN_SUPPORTED
2223
/* Lock for DS peripheral */
2324
static _lock_t s_crypto_ds_lock;
@@ -162,3 +163,52 @@ void esp_crypto_key_manager_lock_release(void)
162163
_lock_release(&s_crypto_key_manager_lock);
163164
}
164165
#endif /* SOC_KEY_MANAGER_SUPPORTED */
166+
#else /* NON_OS_BUILD */
167+
#ifdef SOC_HMAC_SUPPORTED
168+
void esp_crypto_hmac_lock_acquire(void) {}
169+
170+
void esp_crypto_hmac_lock_release(void) {}
171+
#endif /* SOC_HMAC_SUPPORTED */
172+
173+
#ifdef SOC_DIG_SIGN_SUPPORTED
174+
void esp_crypto_ds_lock_acquire(void) {}
175+
176+
void esp_crypto_ds_lock_release(void) {}
177+
#endif /* SOC_DIG_SIGN_SUPPORTED */
178+
179+
#if defined(SOC_SHA_SUPPORTED) || defined(SOC_AES_SUPPORTED)
180+
void esp_crypto_sha_aes_lock_acquire(void) {}
181+
182+
void esp_crypto_sha_aes_lock_release(void) {}
183+
#endif /* defined(SOC_SHA_SUPPORTED) || defined(SOC_AES_SUPPORTED) */
184+
185+
#if defined(SOC_SHA_CRYPTO_DMA) || defined(SOC_AES_CRYPTO_DMA)
186+
void esp_crypto_dma_lock_acquire(void) {}
187+
188+
void esp_crypto_dma_lock_release(void) {}
189+
#endif /* defined(SOC_SHA_CRYPTO_DMA) || defined(SOC_AES_CRYPTO_DMA) */
190+
191+
#ifdef SOC_MPI_SUPPORTED
192+
void esp_crypto_mpi_lock_acquire(void) {}
193+
194+
void esp_crypto_mpi_lock_release(void) {}
195+
#endif /* SOC_MPI_SUPPORTED */
196+
197+
#ifdef SOC_ECC_SUPPORTED
198+
void esp_crypto_ecc_lock_acquire(void) {}
199+
200+
void esp_crypto_ecc_lock_release(void) {}
201+
#endif /* SOC_ECC_SUPPORTED */
202+
203+
#ifdef SOC_ECDSA_SUPPORTED
204+
void esp_crypto_ecdsa_lock_acquire(void) {}
205+
206+
void esp_crypto_ecdsa_lock_release(void) {}
207+
#endif /* SOC_ECDSA_SUPPORTED */
208+
209+
#ifdef SOC_KEY_MANAGER_SUPPORTED
210+
void esp_crypto_key_manager_lock_acquire(void) {}
211+
212+
void esp_crypto_key_manager_lock_release(void) {}
213+
#endif /* SOC_KEY_MANAGER_SUPPORTED */
214+
#endif /* !NON_OS_BUILD */

components/esp_security/src/esp_ds.c

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
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"
@@ -32,42 +37,6 @@
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+
266244
static 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

components/esp_system/port/soc/esp32c6/clk.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,10 @@ __attribute__((weak)) void esp_perip_clk_init(void)
295295
// NOTE: [ESP-TEE] The TEE is responsible for the AES and SHA peripherals
296296
periph_ll_disable_clk_set_rst(PERIPH_AES_MODULE);
297297
periph_ll_disable_clk_set_rst(PERIPH_SHA_MODULE);
298-
#endif
299-
periph_ll_disable_clk_set_rst(PERIPH_ECC_MODULE);
300298
periph_ll_disable_clk_set_rst(PERIPH_HMAC_MODULE);
301299
periph_ll_disable_clk_set_rst(PERIPH_DS_MODULE);
300+
periph_ll_disable_clk_set_rst(PERIPH_ECC_MODULE);
301+
#endif
302302

303303
// TODO: Replace with hal implementation
304304
REG_CLR_BIT(PCR_CTRL_TICK_CONF_REG, PCR_TICK_ENABLE);

components/esp_tee/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ else()
7373

7474
idf_component_register(INCLUDE_DIRS include
7575
SRCS ${srcs}
76-
PRIV_REQUIRES efuse esp_system spi_flash)
76+
PRIV_REQUIRES efuse esp_security esp_system spi_flash)
7777

7878
if(CONFIG_SECURE_ENABLE_TEE)
7979
set(EXTRA_LINK_FLAGS)

components/esp_tee/scripts/esp32c6/sec_srv_tbl_default.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,50 @@ secure_services:
212212
type: IDF
213213
function: esp_crypto_sha_enable_periph_clk
214214
args: 1
215+
- id: 99
216+
type: IDF
217+
function: esp_hmac_calculate
218+
args: 4
219+
- id: 100
220+
type: IDF
221+
function: esp_hmac_jtag_enable
222+
args: 2
223+
- id: 101
224+
type: IDF
225+
function: esp_hmac_jtag_disable
226+
args: 0
227+
- id: 102
228+
type: IDF
229+
function: esp_ds_sign
230+
args: 4
231+
- id: 103
232+
type: IDF
233+
function: esp_ds_start_sign
234+
args: 4
235+
- id: 104
236+
type: IDF
237+
function: esp_ds_is_busy
238+
args: 0
239+
- id: 105
240+
type: IDF
241+
function: esp_ds_finish_sign
242+
args: 2
243+
- id: 106
244+
type: IDF
245+
function: esp_ds_encrypt_params
246+
args: 4
247+
- id: 107
248+
type: IDF
249+
function: esp_crypto_mpi_enable_periph_clk
250+
args: 1
251+
- id: 108
252+
type: IDF
253+
function: esp_ecc_point_multiply
254+
args: 4
255+
- id: 109
256+
type: IDF
257+
function: esp_ecc_point_verify
258+
args: 1
215259
# ID: 134-149 (16) - eFuse
216260
- family: efuse
217261
entries:

0 commit comments

Comments
 (0)