Skip to content

Commit d8d9ba3

Browse files
fix(soc): Fixed ECDSA register compatibility
1 parent bef2a72 commit d8d9ba3

File tree

15 files changed

+846
-181
lines changed

15 files changed

+846
-181
lines changed

components/esp_security/src/init.c

Lines changed: 5 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
*/
@@ -42,6 +42,10 @@ ESP_SYSTEM_INIT_FN(esp_security_init, SECONDARY, BIT(0), 103)
4242
esp_crypto_dpa_protection_startup();
4343
#endif
4444

45+
#if SOC_ECDSA_REGISTER_INCOMPATIBILITY_ACROSS_REV
46+
ecdsa_compatible_mem_reg_addr_init();
47+
#endif
48+
4549
#if CONFIG_ESP_CRYPTO_FORCE_ECC_CONSTANT_TIME_POINT_MUL
4650
bool force_constant_time = true;
4751
#if CONFIG_IDF_TARGET_ESP32H2

components/hal/ecdsa_hal.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -137,11 +137,15 @@ void ecdsa_hal_gen_signature(ecdsa_hal_config_t *conf, const uint8_t *hash,
137137
configure_ecdsa_periph(conf);
138138

139139
#if CONFIG_HAL_ECDSA_GEN_SIG_CM
140+
#if CONFIG_IDF_TARGET_ESP32H2
140141
if (!ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 102)) {
141142
ecdsa_hal_gen_signature_with_countermeasure(hash, r_out, s_out, len);
142143
} else {
143144
ecdsa_hal_gen_signature_inner(hash, r_out, s_out, len);
144145
}
146+
#else
147+
ecdsa_hal_gen_signature_with_countermeasure(hash, r_out, s_out, len);
148+
#endif
145149
#else /* CONFIG_HAL_ECDSA_GEN_SIG_CM */
146150
ecdsa_hal_gen_signature_inner(hash, r_out, s_out, len);
147151
#endif /* !CONFIG_HAL_ECDSA_GEN_SIG_CM */

components/hal/esp32h2/include/hal/ecc_ll.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "soc/pcr_struct.h"
1414
#include "soc/pcr_reg.h"
1515
#include "soc/chip_revision.h"
16-
#include "hal/efuse_ll.h"
16+
#include "hal/efuse_hal.h"
1717

1818
#ifdef __cplusplus
1919
extern "C" {
@@ -216,7 +216,7 @@ static inline ecc_mod_base_t ecc_ll_get_mod_base(void)
216216
static inline void ecc_ll_enable_constant_time_point_mul(bool enable)
217217
{
218218
// ECC constant time point multiplication is supported only on rev 1.2 and above
219-
if ((efuse_ll_get_chip_wafer_version_major() >= 1) && (efuse_ll_get_chip_wafer_version_minor() >= 2)) {
219+
if (ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 102)){
220220
if (enable) {
221221
REG_SET_BIT(ECC_MULT_CONF_REG, ECC_MULT_SECURITY_MODE);
222222
} else {

components/hal/esp32h2/include/hal/ecdsa_ll.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -9,8 +9,10 @@
99
#include <string.h>
1010
#include "hal/assert.h"
1111
#include "soc/ecdsa_reg.h"
12+
#include "soc/ecdsa_struct.h"
1213
#include "soc/pcr_struct.h"
1314
#include "hal/ecdsa_types.h"
15+
#include "hal/ecc_ll.h"
1416

1517
#ifdef __cplusplus
1618
extern "C" {
@@ -31,7 +33,7 @@ typedef enum {
3133
* @brief Interrupt types in ECDSA
3234
*/
3335
typedef enum {
34-
ECDSA_INT_CALC_DONE,
36+
ECDSA_INT_PREP_DONE,
3537
ECDSA_INT_SHA_RELEASE,
3638
} ecdsa_ll_intr_type_t;
3739

@@ -97,8 +99,8 @@ static inline void ecdsa_ll_reset_register(void)
9799
static inline void ecdsa_ll_enable_intr(ecdsa_ll_intr_type_t type)
98100
{
99101
switch (type) {
100-
case ECDSA_INT_CALC_DONE:
101-
REG_SET_FIELD(ECDSA_INT_ENA_REG, ECDSA_CALC_DONE_INT_ENA, 1);
102+
case ECDSA_INT_PREP_DONE:
103+
REG_SET_FIELD(ECDSA_INT_ENA_REG, ECDSA_PREP_DONE_INT_ENA, 1);
102104
break;
103105
case ECDSA_INT_SHA_RELEASE:
104106
REG_SET_FIELD(ECDSA_INT_ENA_REG, ECDSA_SHA_RELEASE_INT_ENA, 1);
@@ -117,8 +119,8 @@ static inline void ecdsa_ll_enable_intr(ecdsa_ll_intr_type_t type)
117119
static inline void ecdsa_ll_disable_intr(ecdsa_ll_intr_type_t type)
118120
{
119121
switch (type) {
120-
case ECDSA_INT_CALC_DONE:
121-
REG_SET_FIELD(ECDSA_INT_ENA_REG, ECDSA_CALC_DONE_INT_ENA, 0);
122+
case ECDSA_INT_PREP_DONE:
123+
REG_SET_FIELD(ECDSA_INT_ENA_REG, ECDSA_PREP_DONE_INT_ENA, 0);
122124
break;
123125
case ECDSA_INT_SHA_RELEASE:
124126
REG_SET_FIELD(ECDSA_INT_ENA_REG, ECDSA_SHA_RELEASE_INT_ENA, 0);
@@ -137,8 +139,8 @@ static inline void ecdsa_ll_disable_intr(ecdsa_ll_intr_type_t type)
137139
static inline void ecdsa_ll_clear_intr(ecdsa_ll_intr_type_t type)
138140
{
139141
switch (type) {
140-
case ECDSA_INT_CALC_DONE:
141-
REG_SET_FIELD(ECDSA_INT_CLR_REG, ECDSA_CALC_DONE_INT_CLR, 1);
142+
case ECDSA_INT_PREP_DONE:
143+
REG_SET_FIELD(ECDSA_INT_CLR_REG, ECDSA_PREP_DONE_INT_CLR, 1);
142144
break;
143145
case ECDSA_INT_SHA_RELEASE:
144146
REG_SET_FIELD(ECDSA_INT_CLR_REG, ECDSA_SHA_RELEASE_INT_CLR, 1);

components/hal/include/hal/ecdsa_hal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/

components/hal/test_apps/crypto/main/ecc/test_ecc.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: CC0-1.0
55
*/
@@ -159,12 +159,18 @@ TEST(ecc, ecc_point_multiplication_on_SECP192R1_and_SECP256R1)
159159
test_ecc_point_mul_inner(false);
160160
}
161161

162-
#if SOC_ECC_CONSTANT_TIME_POINT_MUL || (CONFIG_IDF_TARGET_ESP32H2 && CONFIG_ESP32H2_REV_MIN_FULL >= 102)
162+
#if SOC_ECC_CONSTANT_TIME_POINT_MUL
163163

164164
#define CONST_TIME_DEVIATION_PERCENT 0.002
165165

166166
static void test_ecc_point_mul_inner_constant_time(void)
167167
{
168+
#if CONFIG_IDF_TARGET_ESP32H2
169+
if (!ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 102)) {
170+
TEST_IGNORE_MESSAGE("Skipping test, not supported on ESP32-H2 <v1.2\n");
171+
return;
172+
}
173+
#endif
168174
uint8_t scalar_le[32];
169175
uint8_t x_le[32];
170176
uint8_t y_le[32];
@@ -559,7 +565,7 @@ TEST_GROUP_RUNNER(ecc)
559565
{
560566
#if SOC_ECC_SUPPORT_POINT_MULT
561567
RUN_TEST_CASE(ecc, ecc_point_multiplication_on_SECP192R1_and_SECP256R1);
562-
#if SOC_ECC_CONSTANT_TIME_POINT_MUL || (CONFIG_IDF_TARGET_ESP32H2 && CONFIG_ESP32H2_REV_MIN_FULL >= 102)
568+
#if SOC_ECC_CONSTANT_TIME_POINT_MUL
563569
RUN_TEST_CASE(ecc, ecc_point_multiplication_const_time_check_on_SECP192R1_and_SECP256R1);
564570
#endif
565571
#endif

components/mbedtls/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ menu "mbedTLS"
586586

587587
menu "Enable Software Countermeasure for ECDSA signing using on-chip ECDSA peripheral"
588588
depends on MBEDTLS_HARDWARE_ECDSA_SIGN
589-
depends on IDF_TARGET_ESP32H2 && ESP32H2_REV_MIN_FULL < 102
589+
depends on IDF_TARGET_ESP32H2
590590
config MBEDTLS_HARDWARE_ECDSA_SIGN_MASKING_CM
591591
bool "Mask original ECDSA sign operation under dummy sign operations"
592592
select HAL_ECDSA_GEN_SIG_CM

components/soc/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ if(target STREQUAL "esp32")
2727
list(APPEND srcs "${target_folder}/dport_access.c")
2828
endif()
2929

30+
if(target STREQUAL "esp32h2")
31+
list(APPEND srcs "${target_folder}/ecdsa_reg_addr.c")
32+
endif()
33+
3034
if(CONFIG_SOC_ADC_SUPPORTED)
3135
list(APPEND srcs "${target_folder}/adc_periph.c")
3236
endif()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// This file initialises the memory register addresses for the ECDSA accelerator
8+
// This software initialization is required due to incompatibility between the old and new ECDSA versions
9+
// for the ESP32-H2 ECDSA accelerator
10+
#include <stddef.h>
11+
#include "soc/ecdsa_reg.h"
12+
13+
// Initializing the memory address with the base address of the old ECDSA version
14+
uint32_t ECDSA_R_MEM = (DR_REG_ECDSA_BASE + 0xA00);
15+
uint32_t ECDSA_S_MEM = (DR_REG_ECDSA_BASE + 0xA20);
16+
uint32_t ECDSA_Z_MEM = (DR_REG_ECDSA_BASE + 0xA40);
17+
uint32_t ECDSA_QAX_MEM = (DR_REG_ECDSA_BASE + 0xA60);
18+
uint32_t ECDSA_QAY_MEM = (DR_REG_ECDSA_BASE + 0xA80);
19+
20+
void ecdsa_compatible_mem_reg_addr_init(void)
21+
{
22+
// set the memory registers based on the DATE register value
23+
ECDSA_R_MEM = (DR_REG_ECDSA_BASE + ECDSA_REG_GET_OFFSET(0xA00, 0x340));
24+
ECDSA_S_MEM = (DR_REG_ECDSA_BASE + ECDSA_REG_GET_OFFSET(0xA20, 0x360));
25+
ECDSA_Z_MEM = (DR_REG_ECDSA_BASE + ECDSA_REG_GET_OFFSET(0xA40, 0x380));
26+
ECDSA_QAX_MEM = (DR_REG_ECDSA_BASE + ECDSA_REG_GET_OFFSET(0xA60, 0x3A0));
27+
ECDSA_QAY_MEM = (DR_REG_ECDSA_BASE + ECDSA_REG_GET_OFFSET(0xA80, 0x3C0));
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#pragma once
7+
8+
#include <stdint.h>
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
#include "soc/ecdsa_rev_0_0_struct.h"
14+
#include "soc/ecdsa_rev_1_2_struct.h"
15+
16+
/**
17+
* @brief Compatible ecdsa struct wrapper
18+
*
19+
*/
20+
typedef union {
21+
volatile ecdsa_dev_rev_0_0_t rev_0_0;
22+
volatile ecdsa_dev_rev_1_2_t rev_1_2;
23+
} ecdsa_dev_t;
24+
25+
extern ecdsa_dev_t ECDSA;
26+
27+
/* Note: For ECDSA register on ESP32-H2, you need to use the ECDSA struct through
28+
* ECDSA_REG_GET and ECDSA_REG_SET to access the ECDSA peripheral register and its fields respectively.
29+
* For e.g., ECDSA_REG_SET(ECDSA.clk.clk_gate_force_on, enable) is used to set the register value.
30+
* The ECDSA struct should not be referenced directly.
31+
*/
32+
33+
/** The ECDSA date version of chip revision 1.2*/
34+
#define ECDSA_REV1_2_DATE (0x2403120)
35+
36+
/**
37+
* @brief Set the register value compatibly
38+
* @param reg The register to set
39+
* @param val The value to set
40+
*/
41+
#define ECDSA_REG_SET(reg, val) (ECDSA.rev_1_2.date.ecdsa_date >= ECDSA_REV1_2_DATE ? \
42+
(ECDSA.rev_1_2.reg = (val)) : (ECDSA.rev_0_0.reg = (val)))
43+
44+
/**
45+
* @brief Get the register value compatibly
46+
* @param reg The register to get
47+
*/
48+
#define ECDSA_REG_GET(reg) (ECDSA.rev_1_2.date.ecdsa_date >= ECDSA_REV1_2_DATE ? \
49+
(ECDSA.rev_1_2.reg) : (ECDSA.rev_0_0.reg))
50+
51+
#ifdef __cplusplus
52+
}
53+
#endif

0 commit comments

Comments
 (0)