Skip to content

Commit bef2a72

Browse files
fix(hal): Make the ECDSA countermeasure dynamically applicable
This commit makes the ECDSA countermeasure dynamically applicable across different revisions of the ESP32H2 SoC.
1 parent 6875cbf commit bef2a72

File tree

6 files changed

+24
-11
lines changed

6 files changed

+24
-11
lines changed

components/esp_hw_support/port/esp32h2/Kconfig.hw_support

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@ choice ESP32H2_REV_MIN
1515
bool "Rev v0.1 (ECO1)"
1616
config ESP32H2_REV_MIN_2
1717
bool "Rev v0.2 (ECO2)"
18-
config ESP32H2_REV_MIN_102
19-
bool "Rev v1.2 (ECO5)"
2018
endchoice
2119

2220
config ESP32H2_REV_MIN_FULL
2321
int
2422
default 0 if ESP32H2_REV_MIN_0
2523
default 1 if ESP32H2_REV_MIN_1
2624
default 2 if ESP32H2_REV_MIN_2
27-
default 102 if ESP32H2_REV_MIN_102
2825

2926
config ESP_REV_MIN_FULL
3027
int
@@ -34,7 +31,7 @@ config ESP_REV_MIN_FULL
3431
# MAX Revision
3532
#
3633

37-
comment "Maximum Supported ESP32-H2 Revision (Rev v1.99)"
34+
comment "Maximum Supported ESP32-H2 Revision (Rev v0.99)"
3835
# Maximum revision that IDF supports.
3936
# It can not be changed by user.
4037
# Only Espressif can change it when a new version will be supported in IDF.

components/esp_security/Kconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ menu "ESP Security Specific"
4040
config ESP_CRYPTO_FORCE_ECC_CONSTANT_TIME_POINT_MUL
4141
bool "Forcefully enable ECC constant time point multiplication operations"
4242
depends on SOC_ECC_CONSTANT_TIME_POINT_MUL
43-
depends on !(IDF_TARGET_ESP32H2 && ESP32H2_REV_MIN_FULL < 102)
4443
default N
4544
help
4645
If enabled, the app startup code will burn the ECC_FORCE_CONST_TIME efuse bit to force the

components/esp_security/src/init.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
@@ -41,7 +43,13 @@ ESP_SYSTEM_INIT_FN(esp_security_init, SECONDARY, BIT(0), 103)
4143
#endif
4244

4345
#if CONFIG_ESP_CRYPTO_FORCE_ECC_CONSTANT_TIME_POINT_MUL
44-
if (!esp_efuse_read_field_bit(ESP_EFUSE_ECC_FORCE_CONST_TIME)) {
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ 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 && ESP32H2_REV_MIN_FULL < 102
108+
depends on IDF_TARGET_ESP32H2
109109
default n
110110
help
111111
Enable this option to apply the countermeasure for ECDSA signature operation

components/hal/ecdsa_hal.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,11 @@ 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
139-
ecdsa_hal_gen_signature_with_countermeasure(hash, r_out, s_out, len);
140+
if (!ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 102)) {
141+
ecdsa_hal_gen_signature_with_countermeasure(hash, r_out, s_out, len);
142+
} else {
143+
ecdsa_hal_gen_signature_inner(hash, r_out, s_out, len);
144+
}
140145
#else /* CONFIG_HAL_ECDSA_GEN_SIG_CM */
141146
ecdsa_hal_gen_signature_inner(hash, r_out, s_out, len);
142147
#endif /* !CONFIG_HAL_ECDSA_GEN_SIG_CM */

components/mbedtls/port/ecdsa/ecdsa_alt.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#if CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN_CONSTANT_TIME_CM
2929
#include "esp_timer.h"
30+
#include "soc/chip_revision.h"
31+
#include "hal/efuse_hal.h"
3032

3133
#if CONFIG_ESP_CRYPTO_DPA_PROTECTION_LEVEL_HIGH
3234
/*
@@ -362,9 +364,11 @@ static int esp_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi* r, mbedtls_mpi* s
362364
#endif
363365
ecdsa_hal_gen_signature(&conf, sha_le, r_le, s_le, len);
364366
#if CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN_CONSTANT_TIME_CM
365-
sig_time = esp_timer_get_time() - sig_time;
366-
if (sig_time < ECDSA_CM_FIXED_SIG_TIME) {
367-
esp_rom_delay_us(ECDSA_CM_FIXED_SIG_TIME - sig_time);
367+
if (!ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 102)) {
368+
sig_time = esp_timer_get_time() - sig_time;
369+
if (sig_time < ECDSA_CM_FIXED_SIG_TIME) {
370+
esp_rom_delay_us(ECDSA_CM_FIXED_SIG_TIME - sig_time);
371+
}
368372
}
369373
#endif
370374
process_again = !ecdsa_hal_get_operation_result()

0 commit comments

Comments
 (0)