From 6c290c09f3a7224f6a76baf048f55a32260e3706 Mon Sep 17 00:00:00 2001 From: "nilesh.kale" Date: Tue, 13 May 2025 17:01:07 +0530 Subject: [PATCH] feat: enabled ECDSA-P192 support for ESP32H2 --- .../src/esp32h2/secure_boot_secure_features.c | 9 ++- .../bootloader_support/src/secure_boot.c | 23 +++++- components/efuse/include/esp_efuse.h | 45 +++++++++++- components/efuse/src/esp_efuse_fields.c | 72 ++++++++++++++++++- components/esp_hw_support/Kconfig | 7 ++ components/esp_system/startup.c | 5 ++ components/hal/esp32h2/efuse_hal.c | 13 +++- components/hal/esp32h2/include/hal/ecdsa_ll.h | 10 +++ .../hal/esp32h2/include/hal/efuse_hal.h | 9 ++- components/hal/esp32h2/include/hal/efuse_ll.h | 7 +- components/mbedtls/port/ecdsa/ecdsa_alt.c | 4 +- .../test_apps/main/test_mbedtls_ecdsa.c | 8 ++- .../esp32h2/include/soc/Kconfig.soc_caps.in | 4 ++ components/soc/esp32h2/include/soc/soc_caps.h | 2 + docs/en/api-reference/peripherals/ecdsa.rst | 16 +++++ 15 files changed, 223 insertions(+), 11 deletions(-) diff --git a/components/bootloader_support/src/esp32h2/secure_boot_secure_features.c b/components/bootloader_support/src/esp32h2/secure_boot_secure_features.c index 9ea0f69e4b..da7f84df5c 100644 --- a/components/bootloader_support/src/esp32h2/secure_boot_secure_features.c +++ b/components/bootloader_support/src/esp32h2/secure_boot_secure_features.c @@ -1,10 +1,11 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #include +#include "hal/ecdsa_ll.h" #include "esp_flash_encrypt.h" #include "esp_secure_boot.h" #include "esp_efuse.h" @@ -36,6 +37,12 @@ esp_err_t esp_secure_boot_enable_secure_features(void) ESP_LOGW(TAG, "UART ROM Download mode kept enabled - SECURITY COMPROMISED"); #endif +#ifdef SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED + if (ecdsa_ll_is_configurable_curve_supported()) { + esp_efuse_write_field_bit(ESP_EFUSE_WR_DIS_ECDSA_CURVE_MODE); + } +#endif + #ifndef CONFIG_SECURE_BOOT_ALLOW_JTAG ESP_LOGI(TAG, "Disable hardware & software JTAG..."); esp_efuse_write_field_bit(ESP_EFUSE_DIS_PAD_JTAG); diff --git a/components/bootloader_support/src/secure_boot.c b/components/bootloader_support/src/secure_boot.c index 9015bdfd5a..164a629934 100644 --- a/components/bootloader_support/src/secure_boot.c +++ b/components/bootloader_support/src/secure_boot.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -12,6 +12,10 @@ #include "esp_secure_boot.h" #include "hal/efuse_hal.h" +#ifdef SOC_ECDSA_SUPPORTED +#include "hal/ecdsa_ll.h" +#endif + #ifndef BOOTLOADER_BUILD static __attribute__((unused)) const char *TAG = "secure_boot"; @@ -331,6 +335,23 @@ bool esp_secure_boot_cfg_verify_release_mode(void) } #endif +#ifdef SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED + if (ecdsa_ll_is_configurable_curve_supported()) { + secure = esp_efuse_read_field_bit(ESP_EFUSE_WR_DIS_ECDSA_CURVE_MODE); + if (!secure) { + uint8_t current_curve; + esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_ECDSA_CURVE_MODE, ¤t_curve, ESP_EFUSE_ECDSA_CURVE_MODE[0]->bit_count); + if (err == ESP_OK) { + if (current_curve != ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P256_BIT_LOCKED) { + // If not P256 mode + result &= secure; + ESP_LOGW(TAG, "Not write disabled ECDSA curve mode (set WR_DIS_ECDSA_CURVE_MODE->1)"); + } + } + } + } +#endif + #ifdef CONFIG_SECURE_BOOT_ENABLE_AGGRESSIVE_KEY_REVOKE secure = esp_efuse_read_field_bit(ESP_EFUSE_SECURE_BOOT_AGGRESSIVE_REVOKE); result &= secure; diff --git a/components/efuse/include/esp_efuse.h b/components/efuse/include/esp_efuse.h index fa075f91ac..460e69bd6b 100644 --- a/components/efuse/include/esp_efuse.h +++ b/components/efuse/include/esp_efuse.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -159,7 +159,7 @@ esp_err_t esp_efuse_write_field_cnt(const esp_efuse_desc_t* field[], size_t cnt) * * @return * - ESP_OK: The operation was successfully completed, or the bit was already set to value 1. - * - ESP_ERR_INVALID_ARG: Error in the passed arugments, including if the efuse field is not 1 bit wide. + * - ESP_ERR_INVALID_ARG: Error in the passed arguments, including if the efuse field is not 1 bit wide. */ esp_err_t esp_efuse_write_field_bit(const esp_efuse_desc_t* field[]); @@ -468,7 +468,7 @@ esp_err_t esp_efuse_batch_write_begin(void); * * @return * - ESP_OK: Successful. - * - ESP_ERR_INVALID_STATE: Tha batch mode was not set. + * - ESP_ERR_INVALID_STATE: The batch mode was not set. */ esp_err_t esp_efuse_batch_write_cancel(void); @@ -783,6 +783,45 @@ esp_err_t esp_secure_boot_read_key_digests(esp_secure_boot_key_digests_t *truste */ esp_err_t esp_efuse_check_errors(void); +#if SOC_ECDSA_SUPPORTED +/** + * @brief Checks if 192-bit ECDSA curve operations are supported. + * + * This function checks if the current eFuse configuration supports 192-bit ECDSA curve operations. +*/ +bool esp_efuse_is_ecdsa_p192_curve_supported(void); + +/** + * @brief Checks if 256-bit ECDSA curve operations are supported. + * + * This function checks if the current eFuse configuration supports 256-bit ECDSA curve operations. +*/ +bool esp_efuse_is_ecdsa_p256_curve_supported(void); +#endif /* SOC_ECDSA_SUPPORTED*/ + +#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED +typedef enum { + ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P256_BIT = 0, + ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P192_BIT = 1, + ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_BOTH_P192_P256_BIT = 2, + ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P256_BIT_LOCKED = 3, +} esp_efuse_ecdsa_curve_mode_t; + +/** + * @brief Enables 192-bit ECDSA curve operations by setting the appropriate eFuse value. + * + * This function enables support for 192-bit ECDSA curve operations by configuring the + * ECDSA curve mode eFuse. It checks the current curve mode and attempts to set it to + * allow both P192 and P256 operations if not already set. + * + * @return + * - ESP_OK: Successfully enabled 192-bit ECDSA operations or already enabled + * - ESP_FAIL: Failed to enable operations due to write protection + * - Other error codes: Failed to read/write eFuse + */ +esp_err_t esp_efuse_enable_ecdsa_p192_curve_mode(void); +#endif + #ifdef __cplusplus } #endif diff --git a/components/efuse/src/esp_efuse_fields.c b/components/efuse/src/esp_efuse_fields.c index a73ed03915..9a13be8f44 100644 --- a/components/efuse/src/esp_efuse_fields.c +++ b/components/efuse/src/esp_efuse_fields.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -16,6 +16,13 @@ #include "esp_log.h" #include "soc/efuse_periph.h" #include "sys/param.h" +#include "soc/soc_caps.h" +#include "hal/efuse_ll.h" +#include "hal/efuse_hal.h" + +#ifdef SOC_ECDSA_SUPPORTED +#include "hal/ecdsa_ll.h" +#endif /* SOC_ECDSA_SUPPORTED */ static __attribute__((unused)) const char *TAG = "efuse"; @@ -81,3 +88,66 @@ esp_err_t esp_efuse_update_secure_version(uint32_t secure_version) } return ESP_OK; } + +#if SOC_ECDSA_SUPPORTED +bool esp_efuse_is_ecdsa_p192_curve_supported(void) +{ +#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED + if (ecdsa_ll_is_configurable_curve_supported()) { + uint32_t current_curve = efuse_hal_get_ecdsa_curve_mode(); + return (current_curve == ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_BOTH_P192_P256_BIT || current_curve == ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P192_BIT); + } else { + return true; + } +#else + return true; +#endif /* SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED */ +} + +bool esp_efuse_is_ecdsa_p256_curve_supported(void) +{ +#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED + if (ecdsa_ll_is_configurable_curve_supported()) { + uint32_t current_curve = efuse_hal_get_ecdsa_curve_mode(); + return (current_curve != ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P192_BIT); + } else { + return true; + } +#else + return true; +#endif /* SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED */ +} +#endif /* SOC_ECDSA_SUPPORTED */ + +#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED +esp_err_t esp_efuse_enable_ecdsa_p192_curve_mode(void) +{ + + if (ecdsa_ll_is_configurable_curve_supported()) { + esp_err_t err; + uint8_t current_curve, next_curve; + + current_curve = efuse_hal_get_ecdsa_curve_mode(); + // Check if already in desired state + if (current_curve == ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_BOTH_P192_P256_BIT || current_curve == ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P192_BIT) { + ESP_EARLY_LOGD(TAG, "ECDSA P-192 curve mode is already enabled"); + return ESP_OK; + } + + // Check if write is disabled or already locked to P256 + if (esp_efuse_read_field_bit(ESP_EFUSE_WR_DIS_ECDSA_CURVE_MODE) || current_curve == ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P256_BIT_LOCKED) { + ESP_EARLY_LOGE(TAG, "ECDSA curve mode is locked, cannot enable P-192 curve"); + return ESP_FAIL; + } + + // Attempt to write new curve mode + next_curve = ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_BOTH_P192_P256_BIT; + err = esp_efuse_write_field_blob(ESP_EFUSE_ECDSA_CURVE_MODE, &next_curve, ESP_EFUSE_ECDSA_CURVE_MODE[0]->bit_count); + if (err != ESP_OK) { + ESP_EARLY_LOGE(TAG, "Failed to enable ECDSA P-192 curve %d", err); + return err; + } + } + return ESP_OK; +} +#endif /* SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED */ diff --git a/components/esp_hw_support/Kconfig b/components/esp_hw_support/Kconfig index 418fbbe87b..8ef4337d5f 100644 --- a/components/esp_hw_support/Kconfig +++ b/components/esp_hw_support/Kconfig @@ -346,4 +346,11 @@ menu "Hardware Settings" time point multiplication operations by changing the default ESP-IDF configurations. Performing constant time operations protect the ECC multiplication operations from timing attacks. + config ESP_ECDSA_ENABLE_P192_CURVE + bool "Enable ECDSA 192-curve operations" + depends on SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED + default N + help + By default, only the 256-bit curve operations are allowed. If this configuration is enabled, + it will set the eFuse to allow ECDSA operations using both the 192-bit and 256-bit curves endmenu diff --git a/components/esp_system/startup.c b/components/esp_system/startup.c index a1076ff224..0c19da28db 100644 --- a/components/esp_system/startup.c +++ b/components/esp_system/startup.c @@ -428,6 +428,11 @@ static void do_core_init(void) esp_efuse_set_rom_log_scheme(ROM_LOG_MODE); #endif +#if CONFIG_ESP_ECDSA_ENABLE_P192_CURVE + err = esp_efuse_enable_ecdsa_p192_curve_mode(); + assert(err == ESP_OK && "Failed to enable ECDSA 192-curve operations"); +#endif + #if CONFIG_ESP_XT_WDT esp_xt_wdt_config_t cfg = { .timeout = CONFIG_ESP_XT_WDT_TIMEOUT, diff --git a/components/hal/esp32h2/efuse_hal.c b/components/hal/esp32h2/efuse_hal.c index 697f581e5f..4b09fb854d 100644 --- a/components/hal/esp32h2/efuse_hal.c +++ b/components/hal/esp32h2/efuse_hal.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -7,6 +7,7 @@ #include "sdkconfig.h" #include #include "soc/soc_caps.h" +#include "soc/chip_revision.h" #include "hal/assert.h" #include "hal/efuse_hal.h" #include "hal/efuse_ll.h" @@ -78,6 +79,16 @@ void efuse_hal_rs_calculate(const void *data, void *rs_values) ets_efuse_rs_calculate(data, rs_values); } +uint32_t efuse_hal_get_ecdsa_curve_mode(void) +{ + if (ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 102)) { + return efuse_ll_get_ecdsa_curve_mode(); + } else { + // Curve mode is not configurable for previous versions + return 0; + } +} + /******************* eFuse control functions *************************/ bool efuse_hal_is_coding_error_in_block(unsigned block) diff --git a/components/hal/esp32h2/include/hal/ecdsa_ll.h b/components/hal/esp32h2/include/hal/ecdsa_ll.h index bd352b2e95..8c99171fdb 100644 --- a/components/hal/esp32h2/include/hal/ecdsa_ll.h +++ b/components/hal/esp32h2/include/hal/ecdsa_ll.h @@ -357,6 +357,16 @@ static inline int ecdsa_ll_get_operation_result(void) return REG_GET_BIT(ECDSA_RESULT_REG, ECDSA_OPERATION_RESULT); } +/** + * @brief Check if the ECDSA curves configuration is supported + * The ECDSA curves configuration is only avliable in chip version + * above 1.2 in ESP32-H2 + */ +static inline bool ecdsa_ll_is_configurable_curve_supported(void) +{ + return ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 102); +} + #ifdef __cplusplus } #endif diff --git a/components/hal/esp32h2/include/hal/efuse_hal.h b/components/hal/esp32h2/include/hal/efuse_hal.h index b6c83d6da6..37c4f72cd9 100644 --- a/components/hal/esp32h2/include/hal/efuse_hal.h +++ b/components/hal/esp32h2/include/hal/efuse_hal.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -54,6 +54,13 @@ void efuse_hal_program(uint32_t block); */ void efuse_hal_rs_calculate(const void *data, void *rs_values); +/** + * @brief Get ECDSA curve mode + * + * @return ECDSA curve mode + */ +uint32_t efuse_hal_get_ecdsa_curve_mode(void); + /** * @brief Checks coding error in a block * diff --git a/components/hal/esp32h2/include/hal/efuse_ll.h b/components/hal/esp32h2/include/hal/efuse_ll.h index d2aa40fcb3..c099c4db97 100644 --- a/components/hal/esp32h2/include/hal/efuse_ll.h +++ b/components/hal/esp32h2/include/hal/efuse_ll.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -20,6 +20,11 @@ extern "C" { /******************* eFuse fields *************************/ +__attribute__((always_inline)) static inline uint32_t efuse_ll_get_ecdsa_curve_mode(void) +{ + return EFUSE.rd_repeat_data0.ecdsa_curve_mode; +} + __attribute__((always_inline)) static inline uint32_t efuse_ll_get_flash_crypt_cnt(void) { return EFUSE.rd_repeat_data1.spi_boot_crypt_cnt; diff --git a/components/mbedtls/port/ecdsa/ecdsa_alt.c b/components/mbedtls/port/ecdsa/ecdsa_alt.c index c8b396cf00..a562d2bdc5 100644 --- a/components/mbedtls/port/ecdsa/ecdsa_alt.c +++ b/components/mbedtls/port/ecdsa/ecdsa_alt.c @@ -446,7 +446,9 @@ int __wrap_mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp, const mbedtls_mpi *s, mbedtls_ecdsa_restart_ctx *rs_ctx) { - if ((grp->id == MBEDTLS_ECP_DP_SECP192R1 || grp->id == MBEDTLS_ECP_DP_SECP256R1) && blen == ECDSA_SHA_LEN) { + if (((grp->id == MBEDTLS_ECP_DP_SECP192R1 && esp_efuse_is_ecdsa_p192_curve_supported()) + || (grp->id == MBEDTLS_ECP_DP_SECP256R1 && esp_efuse_is_ecdsa_p256_curve_supported())) + && blen == ECDSA_SHA_LEN) { return esp_ecdsa_verify(grp, buf, blen, Q, r, s); } else { return __real_mbedtls_ecdsa_verify_restartable(grp, buf, blen, Q, r, s, rs_ctx); diff --git a/components/mbedtls/test_apps/main/test_mbedtls_ecdsa.c b/components/mbedtls/test_apps/main/test_mbedtls_ecdsa.c index aeb830f0b2..46ab27d04f 100644 --- a/components/mbedtls/test_apps/main/test_mbedtls_ecdsa.c +++ b/components/mbedtls/test_apps/main/test_mbedtls_ecdsa.c @@ -1,6 +1,6 @@ /* mbedTLS Elliptic Curve Digital Signature performance tests * - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -22,6 +22,10 @@ #include "ecdsa/ecdsa_alt.h" +#if SOC_ECDSA_SUPPORTED +#include "hal/ecdsa_ll.h" +#endif + #define TEST_ASSERT_MBEDTLS_OK(X) TEST_ASSERT_EQUAL_HEX32(0, -(X)) #if CONFIG_NEWLIB_NANO_FORMAT @@ -32,6 +36,8 @@ #define NEWLIB_NANO_COMPAT_CAST(int64_t_var) int64_t_var #endif +__attribute__((unused)) static const char * TAG = "mbedtls_test"; + /* * All the following values are in big endian format, as required by the mbedTLS APIs */ diff --git a/components/soc/esp32h2/include/soc/Kconfig.soc_caps.in b/components/soc/esp32h2/include/soc/Kconfig.soc_caps.in index 91cbc33d46..6009686a44 100644 --- a/components/soc/esp32h2/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32h2/include/soc/Kconfig.soc_caps.in @@ -1075,6 +1075,10 @@ config SOC_ECDSA_USES_MPI bool default y +config SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED + bool + default y + config SOC_UART_NUM int default 2 diff --git a/components/soc/esp32h2/include/soc/soc_caps.h b/components/soc/esp32h2/include/soc/soc_caps.h index 233c66c2b7..932888c9fe 100644 --- a/components/soc/esp32h2/include/soc/soc_caps.h +++ b/components/soc/esp32h2/include/soc/soc_caps.h @@ -462,6 +462,8 @@ /*------------------------- ECDSA CAPS -------------------------*/ #define SOC_ECDSA_USES_MPI (1) +#define SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED (1) + /*-------------------------- UART CAPS ---------------------------------------*/ // ESP32-H2 has 2 UARTs #define SOC_UART_NUM (2) diff --git a/docs/en/api-reference/peripherals/ecdsa.rst b/docs/en/api-reference/peripherals/ecdsa.rst index d6e19cf5bc..ef96098b5a 100644 --- a/docs/en/api-reference/peripherals/ecdsa.rst +++ b/docs/en/api-reference/peripherals/ecdsa.rst @@ -60,6 +60,22 @@ Following code snippet uses :cpp:func:`esp_efuse_write_key` to set physical key } +.. only:: SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED + + ECDSA Curve Configuration + ------------------------- + + .. only:: esp32h2 + + The ECDSA peripheral of the ESP32-H2 supports both ECDSA-P192 and ECDSA-P256 operations. However, starting with ESP32-H2 revision 1.2, only ECDSA-P256 operations are enabled by default. You can enable ECDSA-P192 operations using the following configuration options: + + - :ref:`CONFIG_ESP_ECDSA_ENABLE_P192_CURVE` enables support for ECDSA-P192 curve operations, allowing the device to perform ECDSA operations with both 192-bit and 256-bit curves. However, if ECDSA-P192 operations have already been permanently disabled during eFuse write protection, enabling this option can not re-enable ECDSA-P192 curve operations. + + - :cpp:func:`esp_efuse_enable_ecdsa_p192_curve_mode()` enables ECDSA-P192 curve operations programmatically by writing the appropriate value to the eFuse, allowing both P-192 and P-256 curve operations. Note that this API will fail if the eFuse is already write-protected. + +Non-Determinisitic Signature Generation +--------------------------------------- + Dependency on TRNG ------------------