Skip to content

Commit 05af3a8

Browse files
Merge branch 'feature/esp32h2_eco5_ecc' into 'master'
feat(ecc): enable ECC constant time mode for ESP32-H2 ECO5 Closes IDF-11051, IDF-11399, and DOC-10127 See merge request espressif/esp-idf!34364
2 parents 73c46b0 + 9643457 commit 05af3a8

File tree

26 files changed

+935
-234
lines changed

26 files changed

+935
-234
lines changed

components/esp_security/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ menu "ESP Security Specific"
3838
endmenu
3939

4040
config ESP_CRYPTO_FORCE_ECC_CONSTANT_TIME_POINT_MUL
41-
bool "Forcfully enable ECC constant time point multiplication operations"
41+
bool "Forcefully enable ECC constant time point multiplication operations"
4242
depends on SOC_ECC_CONSTANT_TIME_POINT_MUL
4343
default N
4444
help

components/esp_security/src/init.c

Lines changed: 11 additions & 3 deletions
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,8 @@
1212
#include "esp_efuse_table.h"
1313
#include "esp_security_priv.h"
1414
#include "esp_err.h"
15+
#include "hal/efuse_hal.h"
16+
1517
#if SOC_KEY_MANAGER_ECDSA_KEY_DEPLOY || SOC_KEY_MANAGER_FE_KEY_DEPLOY
1618
#include "hal/key_mgr_ll.h"
1719
#endif
@@ -40,8 +42,14 @@ ESP_SYSTEM_INIT_FN(esp_security_init, SECONDARY, BIT(0), 103)
4042
esp_crypto_dpa_protection_startup();
4143
#endif
4244

43-
#ifdef CONFIG_ESP_CRYPTO_FORCE_ECC_CONSTANT_TIME_POINT_MUL
44-
if (!esp_efuse_read_field_bit(ESP_EFUSE_ECC_FORCE_CONST_TIME)) {
45+
#if CONFIG_ESP_CRYPTO_FORCE_ECC_CONSTANT_TIME_POINT_MUL
46+
bool force_constant_time = true;
47+
#if CONFIG_IDF_TARGET_ESP32H2
48+
if (!ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 102)) {
49+
force_constant_time = false;
50+
}
51+
#endif
52+
if (!esp_efuse_read_field_bit(ESP_EFUSE_ECC_FORCE_CONST_TIME) && force_constant_time) {
4553
ESP_EARLY_LOGD(TAG, "Forcefully enabling ECC constant time operations");
4654
esp_err_t err = esp_efuse_write_field_bit(ESP_EFUSE_ECC_FORCE_CONST_TIME);
4755
if (err != ESP_OK) {

components/hal/Kconfig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ menu "Hardware Abstraction Layer (HAL) and Low Level (LL)"
105105

106106
config HAL_ECDSA_GEN_SIG_CM
107107
bool "Enable countermeasure for ECDSA signature generation"
108+
depends on IDF_TARGET_ESP32H2
108109
default n
109-
# ToDo - IDF-11051
110110
help
111111
Enable this option to apply the countermeasure for ECDSA signature operation
112112
This countermeasure masks the real ECDSA sign operation
113113
under dummy sign operations to add randomness in the generated power signature.
114+
This countermeasure is only necessary for ESP32-H2 < v1.2.
115+
114116

115117
endmenu

components/hal/ecc_hal.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,7 @@ int ecc_hal_read_mod_op_result(uint8_t *r, uint16_t len)
178178

179179
#endif /* SOC_ECC_EXTENDED_MODES_SUPPORTED */
180180

181-
#ifdef SOC_ECC_CONSTANT_TIME_POINT_MUL
182181
void ecc_hal_enable_constant_time_point_mul(bool enable)
183182
{
184183
ecc_ll_enable_constant_time_point_mul(enable);
185184
}
186-
#endif /* SOC_ECC_CONSTANT_TIME_POINT_MUL */

components/hal/ecdsa_hal.c

Lines changed: 10 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
*/
@@ -12,6 +12,7 @@
1212
#if CONFIG_HAL_ECDSA_GEN_SIG_CM
1313
#include "esp_fault.h"
1414
#include "esp_random.h"
15+
#include "soc/chip_revision.h"
1516
#endif
1617

1718
#ifdef SOC_KEY_MANAGER_ECDSA_KEY_DEPLOY
@@ -136,7 +137,15 @@ void ecdsa_hal_gen_signature(ecdsa_hal_config_t *conf, const uint8_t *hash,
136137
configure_ecdsa_periph(conf);
137138

138139
#if CONFIG_HAL_ECDSA_GEN_SIG_CM
140+
#if CONFIG_IDF_TARGET_ESP32H2
141+
if (!ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 102)) {
142+
ecdsa_hal_gen_signature_with_countermeasure(hash, r_out, s_out, len);
143+
} else {
144+
ecdsa_hal_gen_signature_inner(hash, r_out, s_out, len);
145+
}
146+
#else
139147
ecdsa_hal_gen_signature_with_countermeasure(hash, r_out, s_out, len);
148+
#endif
140149
#else /* CONFIG_HAL_ECDSA_GEN_SIG_CM */
141150
ecdsa_hal_gen_signature_inner(hash, r_out, s_out, len);
142151
#endif /* !CONFIG_HAL_ECDSA_GEN_SIG_CM */

components/hal/esp32c2/include/hal/ecc_ll.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -171,6 +171,12 @@ static inline void ecc_ll_read_param(ecc_ll_param_t param, uint8_t *buf, uint16_
171171
memcpy(buf, (void *)reg, len);
172172
}
173173

174+
static inline void ecc_ll_enable_constant_time_point_mul(bool enable)
175+
{
176+
// Not supported for ESP32-C2
177+
(void) enable; //unused
178+
}
179+
174180
#ifdef __cplusplus
175181
}
176182
#endif

components/hal/esp32c6/include/hal/ecc_ll.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ static inline void ecc_ll_read_param(ecc_ll_param_t param, uint8_t *buf, uint16_
173173
memcpy(buf, (void *)reg, len);
174174
}
175175

176+
static inline void ecc_ll_enable_constant_time_point_mul(bool enable)
177+
{
178+
// Not supported for ESP32-C6
179+
(void) enable; //unused
180+
}
181+
176182
#ifdef __cplusplus
177183
}
178184
#endif

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "soc/ecc_mult_reg.h"
1313
#include "soc/pcr_struct.h"
1414
#include "soc/pcr_reg.h"
15+
#include "soc/chip_revision.h"
16+
#include "hal/efuse_hal.h"
1517

1618
#ifdef __cplusplus
1719
extern "C" {
@@ -211,6 +213,18 @@ static inline ecc_mod_base_t ecc_ll_get_mod_base(void)
211213
return (ecc_mod_base_t)(REG_GET_FIELD(ECC_MULT_CONF_REG, ECC_MULT_MOD_BASE));
212214
}
213215

216+
static inline void ecc_ll_enable_constant_time_point_mul(bool enable)
217+
{
218+
// ECC constant time point multiplication is supported only on rev 1.2 and above
219+
if (ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 102)){
220+
if (enable) {
221+
REG_SET_BIT(ECC_MULT_CONF_REG, ECC_MULT_SECURITY_MODE);
222+
} else {
223+
REG_CLR_BIT(ECC_MULT_CONF_REG, ECC_MULT_SECURITY_MODE);
224+
}
225+
}
226+
}
227+
214228
static inline void ecc_ll_read_param(ecc_ll_param_t param, uint8_t *buf, uint16_t len)
215229
{
216230
uint32_t reg;

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/esp32p4/include/hal/ecc_ll.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -240,6 +240,12 @@ static inline void ecc_ll_read_param(ecc_ll_param_t param, uint8_t *buf, uint16_
240240
memcpy(buf, (void *)reg, len);
241241
}
242242

243+
static inline void ecc_ll_enable_constant_time_point_mul(bool enable)
244+
{
245+
// Not supported for ESP32-P4
246+
(void) enable; //unused
247+
}
248+
243249
#ifdef __cplusplus
244250
}
245251
#endif

0 commit comments

Comments
 (0)