Skip to content

Commit 26fa710

Browse files
committed
fix(esp_tee): Protect the AES/SHA clock registers from REE access
1 parent d7222cc commit 26fa710

File tree

9 files changed

+90
-33
lines changed

9 files changed

+90
-33
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,11 @@ __attribute__((weak)) void esp_perip_clk_init(void)
291291
periph_ll_disable_clk_set_rst(PERIPH_ASSIST_DEBUG_MODULE);
292292
#endif
293293
periph_ll_disable_clk_set_rst(PERIPH_RSA_MODULE);
294+
#if !CONFIG_SECURE_ENABLE_TEE
295+
// NOTE: [ESP-TEE] The TEE is responsible for the AES and SHA peripherals
294296
periph_ll_disable_clk_set_rst(PERIPH_AES_MODULE);
295297
periph_ll_disable_clk_set_rst(PERIPH_SHA_MODULE);
298+
#endif
296299
periph_ll_disable_clk_set_rst(PERIPH_ECC_MODULE);
297300
periph_ll_disable_clk_set_rst(PERIPH_HMAC_MODULE);
298301
periph_ll_disable_clk_set_rst(PERIPH_DS_MODULE);

components/esp_tee/scripts/esp32c6/sec_srv_tbl_default.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ secure_services:
208208
type: IDF
209209
function: esp_sha_write_digest_state
210210
args: 2
211+
- id: 132
212+
type: IDF
213+
function: esp_sha_enable_periph_clk
214+
args: 1
211215
# ID: 134-149 (16) - eFuse
212216
- family: efuse
213217
entries:

components/esp_tee/src/esp_secure_service_wrapper.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ void __wrap_esp_sha_write_digest_state(esp_sha_type sha_type, void *digest_state
228228
esp_tee_service_call(3, SS_ESP_SHA_WRITE_DIGEST_STATE, sha_type, digest_state);
229229
}
230230

231+
void __wrap_esp_sha_enable_periph_clk(bool enable)
232+
{
233+
esp_tee_service_call(2, SS_ESP_SHA_ENABLE_PERIPH_CLK, enable);
234+
}
235+
231236
/* ---------------------------------------------- MMU HAL ------------------------------------------------- */
232237

233238
void IRAM_ATTR __wrap_mmu_hal_map_region(uint32_t mmu_id, mmu_target_t mem_type, uint32_t vaddr, uint32_t paddr, uint32_t len, uint32_t *out_len)

components/esp_tee/subproject/main/core/esp_secure_services.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "soc/soc_caps.h"
2727
#include "aes/esp_aes.h"
2828
#include "sha/sha_core.h"
29+
#include "esp_sha_internal.h"
2930

3031
#include "esp_tee.h"
3132
#include "esp_tee_memory_utils.h"
@@ -325,6 +326,11 @@ void _ss_esp_sha_block(esp_sha_type sha_type, const void *data_block, bool is_fi
325326
esp_sha_block(sha_type, data_block, is_first_block);
326327
}
327328

329+
void _ss_esp_sha_enable_periph_clk(bool enable)
330+
{
331+
esp_sha_enable_periph_clk(enable);
332+
}
333+
328334
/* ---------------------------------------------- OTA ------------------------------------------------- */
329335

330336
int _ss_esp_tee_ota_begin(void)

components/esp_tee/subproject/main/soc/esp32c6/esp_tee_apm_prot_cfg.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "soc/soc.h"
1212
#include "soc/spi_mem_reg.h"
1313
#include "soc/efuse_reg.h"
14+
#include "soc/pcr_reg.h"
1415

1516
extern void tee_apm_violation_isr(void *arg);
1617

@@ -91,34 +92,41 @@ apm_ctrl_region_config_data_t hp_apm_pms_data[] = {
9192
.regn_pms = 0x6,
9293
.filter_enable = 1,
9394
},
94-
/* Region 5: Peripherals [RSA - TEE Controller & APM] (RW) */
95-
/* Protected: APM, TEE Controller */
95+
/* Region 5/6: Peripherals [RSA - TEE Controller & APM] (RW) */
96+
/* Protected: AES + SHA PCR, APM, TEE Controller */
9697
{
9798
.regn_num = 5,
9899
.regn_start_addr = DR_REG_RSA_BASE,
99-
.regn_end_addr = (DR_REG_TEE_BASE - 0x4),
100+
.regn_end_addr = (PCR_AES_CONF_REG - 0x4),
100101
.regn_pms = 0x6,
101102
.filter_enable = 1,
102103
},
103-
/* Region 6: Peripherals [Miscellaneous - PMU] (RW) */
104104
{
105105
.regn_num = 6,
106+
.regn_start_addr = PCR_RSA_CONF_REG,
107+
.regn_end_addr = (DR_REG_TEE_BASE - 0x4),
108+
.regn_pms = 0x6,
109+
.filter_enable = 1,
110+
},
111+
/* Region 7: Peripherals [Miscellaneous - PMU] (RW) */
112+
{
113+
.regn_num = 7,
106114
.regn_start_addr = DR_REG_MISC_BASE,
107115
.regn_end_addr = (DR_REG_PMU_BASE - 0x04),
108116
.regn_pms = 0x6,
109117
.filter_enable = 1,
110118
},
111-
/* Region 7: Peripherals [DEBUG - PWDET] (RW) */
119+
/* Region 8: Peripherals [DEBUG - PWDET] (RW) */
112120
{
113-
.regn_num = 7,
121+
.regn_num = 8,
114122
.regn_start_addr = DR_REG_OPT_DEBUG_BASE,
115123
.regn_end_addr = 0x600D0000,
116124
.regn_pms = 0x6,
117125
.filter_enable = 1,
118126
},
119-
/* Region 8: REE SRAM region (RW) */
127+
/* Region 9: REE SRAM region (RW) */
120128
{
121-
.regn_num = 8,
129+
.regn_num = 9,
122130
.regn_start_addr = SOC_NS_IRAM_START,
123131
.regn_end_addr = SOC_IRAM_HIGH,
124132
.regn_pms = 0x6,
@@ -164,9 +172,9 @@ apm_ctrl_secure_mode_config_t hp_apm_sec_mode_data = {
164172

165173
/* HP_APM: TEE mode accessible regions */
166174
apm_ctrl_region_config_data_t hp_apm_pms_data_tee[] = {
167-
/* Region 9: Entire memory region (RWX)*/
175+
/* Region 10: Entire memory region (RWX)*/
168176
{
169-
.regn_num = 9,
177+
.regn_num = 10,
170178
.regn_start_addr = 0x0,
171179
.regn_end_addr = ~0x0,
172180
.regn_pms = 0x7,
@@ -303,6 +311,9 @@ void esp_tee_configure_apm_protection(void)
303311
/* Disable all control filter first to have full access of address rage. */
304312
apm_hal_apm_ctrl_filter_enable_all(false);
305313

314+
/* Switch HP_CPU to TEE mode */
315+
apm_tee_hal_set_master_secure_mode(HP_APM_CTRL, APM_LL_MASTER_HPCORE, APM_LL_SECURE_MODE_TEE);
316+
306317
/* LP APM0 configuration. */
307318
lp_apm0_sec_mode_data.regn_count = sizeof(lp_apm0_pms_data) / sizeof(apm_ctrl_region_config_data_t);
308319
apm_hal_apm_ctrl_master_sec_mode_config(&lp_apm0_sec_mode_data);

components/esp_tee/subproject/main/soc/esp32c6/esp_tee_secure_sys_cfg.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -12,6 +12,7 @@
1212
#include "esp_cpu.h"
1313
#include "esp_log.h"
1414
#include "hal/apm_hal.h"
15+
#include "hal/clk_gate_ll.h"
1516

1617
#include "esp_tee.h"
1718
#include "esp_tee_intr.h"
@@ -91,6 +92,10 @@ void esp_tee_soc_secure_sys_init(void)
9192
esp_tee_protect_intr_src(ETS_EFUSE_INTR_SOURCE); // eFuse
9293
esp_tee_protect_intr_src(ETS_AES_INTR_SOURCE); // AES
9394
esp_tee_protect_intr_src(ETS_SHA_INTR_SOURCE); // SHA
95+
96+
/* Disable AES/SHA peripheral clocks; they will be toggled as needed when the peripheral is in use */
97+
periph_ll_disable_clk_set_rst(PERIPH_AES_MODULE);
98+
periph_ll_disable_clk_set_rst(PERIPH_SHA_MODULE);
9499
}
95100

96101
IRAM_ATTR inline void esp_tee_switch_to_ree(uint32_t ree_entry_addr)

components/mbedtls/port/sha/core/include/esp_sha_internal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ static inline esp_sha_mode sha_operation_mode(size_t length)
5656
return SHA_BLOCK_MODE;
5757
}
5858

59+
/**
60+
* @brief Enable or disable the SHA peripheral clock
61+
*
62+
* @param enable true to enable, false to disable
63+
*/
64+
void esp_sha_enable_periph_clk(bool enable);
65+
5966
#ifdef __cplusplus
6067
}
6168
#endif

components/mbedtls/port/sha/core/sha.c

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "esp_private/esp_crypto_lock_internal.h"
1717
#include "esp_log.h"
1818
#include "sha/sha_core.h"
19+
#include "esp_sha_internal.h"
1920
#include "hal/sha_hal.h"
2021
#include "hal/sha_ll.h"
2122
#include "soc/soc_caps.h"
@@ -51,6 +52,15 @@
5152
#endif
5253
#endif /* SOC_SHA_SUPPORT_DMA */
5354

55+
#if !ESP_TEE_BUILD
56+
#define SHA_LOCK() esp_crypto_sha_aes_lock_acquire()
57+
#define SHA_RELEASE() esp_crypto_sha_aes_lock_release()
58+
#else
59+
#define SHA_RCC_ATOMIC()
60+
#define SHA_LOCK()
61+
#define SHA_RELEASE()
62+
#endif
63+
5464
void esp_sha_write_digest_state(esp_sha_type sha_type, void *digest_state)
5565
{
5666
sha_hal_write_digest(sha_type, digest_state);
@@ -89,34 +99,16 @@ inline static size_t block_length(esp_sha_type type)
8999
/* Enable SHA peripheral and then lock it */
90100
void esp_sha_acquire_hardware(void)
91101
{
92-
#if !ESP_TEE_BUILD
93102
/* Released when releasing hw with esp_sha_release_hardware() */
94-
esp_crypto_sha_aes_lock_acquire();
95-
#endif
96-
97-
SHA_RCC_ATOMIC() {
98-
sha_ll_enable_bus_clock(true);
99-
sha_ll_reset_register();
100-
#if SOC_AES_CRYPTO_DMA
101-
crypto_dma_ll_enable_bus_clock(true);
102-
crypto_dma_ll_reset_register();
103-
#endif
104-
}
103+
SHA_LOCK();
104+
esp_sha_enable_periph_clk(true);
105105
}
106106

107107
/* Disable SHA peripheral block and then release it */
108108
void esp_sha_release_hardware(void)
109109
{
110-
SHA_RCC_ATOMIC() {
111-
sha_ll_enable_bus_clock(false);
112-
#if SOC_AES_CRYPTO_DMA
113-
crypto_dma_ll_enable_bus_clock(false);
114-
#endif
115-
}
116-
117-
#if !ESP_TEE_BUILD
118-
esp_crypto_sha_aes_lock_release();
119-
#endif
110+
esp_sha_enable_periph_clk(false);
111+
SHA_RELEASE();
120112
}
121113

122114
void esp_sha_block(esp_sha_type sha_type, const void *data_block, bool is_first_block)

components/mbedtls/port/sha/esp_sha.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <string.h>
88
#include <stdio.h>
99
#include <assert.h>
10+
#include "hal/sha_ll.h"
1011
#include "hal/sha_hal.h"
1112
#include "hal/sha_types.h"
1213
#include "soc/soc_caps.h"
@@ -20,10 +21,33 @@
2021
#include "sha/sha_parallel_engine.h"
2122
#else
2223
#include "sha/sha_core.h"
24+
#include "esp_sha_internal.h"
25+
#include "esp_private/esp_crypto_lock_internal.h"
26+
#if SOC_SHA_CRYPTO_DMA
27+
#include "hal/crypto_dma_ll.h"
28+
#endif
2329
#endif
2430

2531
static const char *TAG = "esp_sha";
2632

33+
#if !SOC_SHA_SUPPORT_PARALLEL_ENG
34+
void esp_sha_enable_periph_clk(bool enable)
35+
{
36+
SHA_RCC_ATOMIC() {
37+
sha_ll_enable_bus_clock(enable);
38+
if (enable) {
39+
sha_ll_reset_register();
40+
}
41+
#if SOC_SHA_CRYPTO_DMA
42+
crypto_dma_ll_enable_bus_clock(enable);
43+
if (enable) {
44+
crypto_dma_ll_reset_register();
45+
}
46+
#endif
47+
}
48+
}
49+
#endif
50+
2751
void esp_sha(esp_sha_type sha_type, const unsigned char *input, size_t ilen, unsigned char *output)
2852
{
2953
union {

0 commit comments

Comments
 (0)