Skip to content

Commit 4a4d63d

Browse files
committed
feat(esp_tee): Protect the ECC peripheral from REE access
1 parent fc4802c commit 4a4d63d

File tree

16 files changed

+90
-24
lines changed

16 files changed

+90
-24
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ __attribute__((weak)) void esp_perip_clk_init(void)
297297
periph_ll_disable_clk_set_rst(PERIPH_SHA_MODULE);
298298
periph_ll_disable_clk_set_rst(PERIPH_HMAC_MODULE);
299299
periph_ll_disable_clk_set_rst(PERIPH_DS_MODULE);
300-
#endif
301300
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/scripts/esp32c6/sec_srv_tbl_default.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,14 @@ secure_services:
248248
type: IDF
249249
function: esp_crypto_mpi_enable_periph_clk
250250
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
251259
# ID: 134-149 (16) - eFuse
252260
- family: efuse
253261
entries:

components/esp_tee/src/esp_secure_service_wrapper.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,33 @@ void __wrap_esp_crypto_mpi_enable_periph_clk(bool enable)
342342
esp_tee_service_call(2, SS_ESP_CRYPTO_MPI_ENABLE_PERIPH_CLK, enable);
343343
}
344344

345+
/* ---------------------------------------------- ECC ------------------------------------------------- */
346+
347+
#define P256_LEN (256/8)
348+
#define P192_LEN (192/8)
349+
350+
typedef struct {
351+
uint8_t x[P256_LEN]; /* Little endian order */
352+
uint8_t y[P256_LEN]; /* Little endian order */
353+
unsigned len; /* P192_LEN or P256_LEN */
354+
} ecc_point_t;
355+
356+
int __wrap_esp_ecc_point_multiply(const ecc_point_t *point, const uint8_t *scalar, ecc_point_t *result, bool verify_first)
357+
{
358+
esp_crypto_ecc_lock_acquire();
359+
esp_err_t err = esp_tee_service_call(5, SS_ESP_ECC_POINT_MULTIPLY, point, scalar, result, verify_first);
360+
esp_crypto_ecc_lock_release();
361+
return err;
362+
}
363+
364+
int __wrap_esp_ecc_point_verify(const ecc_point_t *point)
365+
{
366+
esp_crypto_ecc_lock_acquire();
367+
esp_err_t err = esp_tee_service_call(2, SS_ESP_ECC_POINT_VERIFY, point);
368+
esp_crypto_ecc_lock_release();
369+
return err;
370+
}
371+
345372
/* ---------------------------------------------- MMU HAL ------------------------------------------------- */
346373

347374
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "esp_hmac.h"
3030
#include "esp_ds.h"
3131
#include "esp_crypto_periph_clk.h"
32+
#include "ecc_impl.h"
3233

3334
#include "esp_tee.h"
3435
#include "esp_tee_memory_utils.h"
@@ -444,6 +445,26 @@ void _ss_esp_crypto_mpi_enable_periph_clk(bool enable)
444445
esp_crypto_mpi_enable_periph_clk(enable);
445446
}
446447

448+
/* ---------------------------------------------- ECC ------------------------------------------------- */
449+
450+
int _ss_esp_ecc_point_multiply(const ecc_point_t *point, const uint8_t *scalar, ecc_point_t *result, bool verify_first)
451+
{
452+
bool valid_addr = (esp_tee_ptr_in_ree((void *)result)) &&
453+
esp_tee_ptr_in_ree((void *)((char *)result + sizeof(ecc_point_t)));
454+
455+
if (!valid_addr) {
456+
return -1;
457+
}
458+
ESP_FAULT_ASSERT(valid_addr);
459+
460+
return esp_ecc_point_multiply(point, scalar, result, verify_first);
461+
}
462+
463+
int _ss_esp_ecc_point_verify(const ecc_point_t *point)
464+
{
465+
return esp_ecc_point_verify(point);
466+
}
467+
447468
/* ---------------------------------------------- OTA ------------------------------------------------- */
448469

449470
int _ss_esp_tee_ota_begin(void)

components/esp_tee/subproject/main/ld/esp32c6/esp_tee.ld.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ SECTIONS
177177
* | SHA | text | Flash |
178178
* | HMAC | text | Flash |
179179
* | DS | text | Flash |
180+
* | ECC | text | Flash |
180181
* | BROWNOUT | text | Flash |
181182
* | EFUSE | text | Flash |
182183
* | LPTIMER | text | Flash |
@@ -196,6 +197,7 @@ SECTIONS
196197
*libhal.a:sha_hal.c*(.literal .text .literal.* .text.*)
197198
*libhal.a:hmac_hal.c*(.literal .text .literal.* .text.*)
198199
*libhal.a:ds_hal.c*(.literal .text .literal.* .text.*)
200+
*libhal.a:ecc_hal.c*(.literal .text .literal.* .text.*)
199201
*libhal.a:apm_hal.c*(.literal .text .literal.* .text.*)
200202
*libhal.a:brownout_hal.c*(.literal .text .literal.* .text.*)
201203
*libhal.a:spi_flash_hal.c*(.literal .text .literal.* .text.*)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ apm_ctrl_region_config_data_t hp_apm_pms_data[] = {
107107
.filter_enable = 1,
108108
},
109109
/* Region 6/7: Peripherals [H/W Lock - HMAC] (RW) */
110-
/* Protected: AES, SHA, DS, HMAC */
110+
/* Protected: AES, SHA, ECC, DS, HMAC */
111111
{
112112
.regn_num = 6,
113113
.regn_start_addr = DR_REG_ATOMIC_BASE,
@@ -118,12 +118,12 @@ apm_ctrl_region_config_data_t hp_apm_pms_data[] = {
118118
{
119119
.regn_num = 7,
120120
.regn_start_addr = DR_REG_RSA_BASE,
121-
.regn_end_addr = (DR_REG_DS_BASE - 0x4),
121+
.regn_end_addr = (DR_REG_ECC_MULT_BASE - 0x4),
122122
.regn_pms = 0x6,
123123
.filter_enable = 1,
124124
},
125-
/* Region 8/9/10: Peripherals [DS - TEE Controller & APM] (RW) */
126-
/* Protected: AES, SHA, DS, HMAC PCR, APM, TEE Controller */
125+
/* Region 8/9/10: Peripherals [IO_MUX - TEE Controller & APM] (RW) */
126+
/* Protected: AES, SHA, ECC, DS and HMAC PCRs, APM, TEE Controller */
127127
{
128128
.regn_num = 8,
129129
.regn_start_addr = DR_REG_IO_MUX_BASE,
@@ -134,7 +134,7 @@ apm_ctrl_region_config_data_t hp_apm_pms_data[] = {
134134
{
135135
.regn_num = 9,
136136
.regn_start_addr = PCR_RSA_CONF_REG,
137-
.regn_end_addr = (PCR_DS_CONF_REG - 0x4),
137+
.regn_end_addr = (PCR_ECC_CONF_REG - 0x4),
138138
.regn_pms = 0x6,
139139
.filter_enable = 1,
140140
},

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "hal/sha_ll.h"
1717
#include "hal/hmac_ll.h"
1818
#include "hal/ds_ll.h"
19+
#include "hal/ecc_ll.h"
1920

2021
#include "esp_tee.h"
2122
#include "esp_tee_intr.h"
@@ -95,12 +96,14 @@ void esp_tee_soc_secure_sys_init(void)
9596
esp_tee_protect_intr_src(ETS_EFUSE_INTR_SOURCE); // eFuse
9697
esp_tee_protect_intr_src(ETS_AES_INTR_SOURCE); // AES
9798
esp_tee_protect_intr_src(ETS_SHA_INTR_SOURCE); // SHA
99+
esp_tee_protect_intr_src(ETS_ECC_INTR_SOURCE); // ECC
98100

99101
/* Disable protected crypto peripheral clocks; they will be toggled as needed when the peripheral is in use */
100102
aes_ll_enable_bus_clock(false);
101103
sha_ll_enable_bus_clock(false);
102104
hmac_ll_enable_bus_clock(false);
103105
ds_ll_enable_bus_clock(false);
106+
ecc_ll_enable_bus_clock(false);
104107
}
105108

106109
IRAM_ATTR inline void esp_tee_switch_to_ree(uint32_t ree_entry_addr)

components/esp_tee/test_apps/tee_test_fw/main/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ list(APPEND srcs "${mbedtls_test_srcs_dir}/test_mbedtls_sha.c"
3232

3333
# Mixed
3434
list(APPEND srcs "${mbedtls_test_srcs_dir}/test_aes_sha_parallel.c")
35+
36+
# ECC
37+
list(APPEND srcs "${mbedtls_test_srcs_dir}/test_ecp.c")
38+
3539
# Utility
3640
list(APPEND srcs "${mbedtls_test_srcs_dir}/test_apb_dport_access.c"
3741
"${mbedtls_test_srcs_dir}/test_mbedtls_utils.c")

components/esp_tee/test_apps/tee_test_fw/main/test_esp_tee_att.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ TEST_CASE("Test TEE Attestation - Generate and verify the EAT", "[attestation]")
256256
uint8_t *token_buf = heap_caps_calloc(ESP_ATT_TK_BUF_SIZE, sizeof(uint8_t), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
257257
TEST_ASSERT_NOT_NULL(token_buf);
258258

259-
ESP_LOGI(TAG, "Generating EAT for all active firmwares (Bootloader, TEE and non-secure app)...");
260259
// Generating the attestation token
261260
uint32_t token_len = 0;
262261
TEST_ESP_OK(esp_tee_att_generate_token(0xA1B2C3D4, 0x0FACADE0, (const char *)ESP_ATT_TK_PSA_CERT_REF,
@@ -275,7 +274,6 @@ TEST_CASE("Test TEE Attestation - Generate and verify the EAT", "[attestation]")
275274
esp_tee_sec_storage_sign_t sign_ctx = {};
276275
fetch_signature((const char *)token_buf, &sign_ctx);
277276

278-
ESP_LOGI(TAG, "Verifying the generated EAT...");
279277
// Verifying the generated token
280278
TEST_ASSERT_EQUAL(0, verify_ecdsa_sign(digest, sizeof(digest), &pubkey_ctx, &sign_ctx, false));
281279
free(token_buf);

components/esp_tee/test_apps/tee_test_fw/main/test_esp_tee_panic.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ TEST_CASE("Test APM violation interrupt: AES", "[apm_violation]")
6363
TEST_CASE("Test APM violation interrupt: HMAC", "[apm_violation]")
6464
{
6565
uint32_t val = UINT32_MAX;
66-
REG_WRITE(HMAC_SET_PARA_KEY_REG, val);
66+
val = REG_READ(HMAC_SET_PARA_KEY_REG);
67+
TEST_ASSERT_EQUAL(0, val);
6768
TEST_FAIL_MESSAGE("APM violation interrupt should have been generated");
6869
}
6970

@@ -77,16 +78,15 @@ TEST_CASE("Test APM violation interrupt: DS", "[apm_violation]")
7778

7879
TEST_CASE("Test APM violation interrupt: SHA PCR", "[apm_violation]")
7980
{
80-
uint32_t val = UINT32_MAX;
81-
val = REG_READ(PCR_SHA_CONF_REG);
82-
TEST_ASSERT_EQUAL(0, val);
81+
uint32_t val = 0;
82+
REG_WRITE(PCR_SHA_CONF_REG, val);
8383
TEST_FAIL_MESSAGE("APM violation interrupt should have been generated");
8484
}
8585

86-
TEST_CASE("Test APM violation interrupt: DS PCR", "[apm_violation]")
86+
TEST_CASE("Test APM violation interrupt: ECC PCR", "[apm_violation]")
8787
{
88-
uint32_t val = UINT32_MAX;
89-
REG_WRITE(PCR_DS_CONF_REG, val);
88+
uint32_t val = 0;
89+
REG_WRITE(PCR_ECC_CONF_REG, val);
9090
TEST_FAIL_MESSAGE("APM violation interrupt should have been generated");
9191
}
9292

0 commit comments

Comments
 (0)