Skip to content

Commit 148d31b

Browse files
committed
feat: enable support for deterministic mode for esp32h2
1 parent 0fb8c2a commit 148d31b

File tree

11 files changed

+161
-35
lines changed

11 files changed

+161
-35
lines changed

components/hal/ecdsa_hal.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ static void configure_ecdsa_periph(ecdsa_hal_config_t *conf)
4747
}
4848

4949
#if SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE
50-
ecdsa_ll_set_k_type(conf->sign_type);
51-
50+
if (ecdsa_ll_is_deterministic_mode_supported()) {
51+
ecdsa_ll_set_k_type(conf->sign_type);
5252
#if !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP
53-
if (conf->sign_type == ECDSA_K_TYPE_DETERMINISITIC) {
54-
ecdsa_ll_set_deterministic_loop(conf->loop_number);
55-
}
53+
if (conf->sign_type == ECDSA_K_TYPE_DETERMINISITIC) {
54+
ecdsa_ll_set_deterministic_loop(conf->loop_number);
55+
}
5656
#endif /* !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP */
57+
}
5758
#endif
5859
}
5960

components/hal/esp32c5/include/hal/ecdsa_ll.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,14 @@ static inline int ecdsa_ll_get_operation_result(void)
405405
return REG_GET_BIT(ECDSA_RESULT_REG, ECDSA_OPERATION_RESULT);
406406
}
407407

408+
/**
409+
* @brief Check if the ECDSA deterministic mode is supported
410+
*/
411+
static inline bool ecdsa_ll_is_deterministic_mode_supported(void)
412+
{
413+
return true;
414+
}
415+
408416
#ifdef __cplusplus
409417
}
410418
#endif

components/hal/esp32c61/include/hal/ecdsa_ll.h

Lines changed: 9 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
*/
@@ -424,6 +424,14 @@ static inline int ecdsa_ll_check_k_value(void)
424424
return REG_GET_BIT(ECDSA_RESULT_REG, ECDSA_K_VALUE_WARNING);
425425
}
426426

427+
/**
428+
* @brief Check if the ECDSA deterministic mode is supported
429+
*/
430+
static inline bool ecdsa_ll_is_deterministic_mode_supported(void)
431+
{
432+
return true;
433+
}
434+
427435
#ifdef __cplusplus
428436
}
429437
#endif

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,26 @@ static inline void ecdsa_ll_set_z_mode(ecdsa_ll_sha_mode_t mode)
211211
}
212212
}
213213

214+
/**
215+
* @brief Set the signature generation type of ECDSA operation
216+
*
217+
* @param type Type of the ECDSA signature
218+
*/
219+
static inline void ecdsa_ll_set_k_type(ecdsa_sign_type_t type)
220+
{
221+
switch (type) {
222+
case ECDSA_K_TYPE_TRNG:
223+
REG_CLR_BIT(ECDSA_CONF_REG, ECDSA_DETERMINISTIC_K);
224+
break;
225+
case ECDSA_K_TYPE_DETERMINISITIC:
226+
REG_SET_BIT(ECDSA_CONF_REG, ECDSA_DETERMINISTIC_K);
227+
break;
228+
default:
229+
HAL_ASSERT(false && "Unsupported K type");
230+
break;
231+
}
232+
}
233+
214234
/**
215235
* @brief Set the stage of ECDSA operation
216236
*
@@ -388,6 +408,16 @@ static inline bool ecdsa_ll_is_configurable_curve_supported(void)
388408
return ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 102);
389409
}
390410

411+
/**
412+
* @brief Check if the ECDSA deterministic mode is supported
413+
* The ECDSA deterministic mode is only available in chip version
414+
* above 1.2 in ESP32-H2
415+
*/
416+
static inline bool ecdsa_ll_is_deterministic_mode_supported(void)
417+
{
418+
return ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 102);
419+
}
420+
391421
#ifdef __cplusplus
392422
}
393423
#endif

components/hal/esp32h21/include/hal/ecdsa_ll.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,14 @@ static inline bool ecdsa_ll_is_configurable_curve_supported(void)
428428
return true;
429429
}
430430

431+
/**
432+
* @brief Check if the ECDSA deterministic mode is supported
433+
*/
434+
static inline bool ecdsa_ll_is_deterministic_mode_supported(void)
435+
{
436+
return true;
437+
}
438+
431439
#ifdef __cplusplus
432440
}
433441
#endif

components/hal/esp32p4/include/hal/ecdsa_ll.h

Lines changed: 9 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
*/
@@ -432,6 +432,14 @@ static inline int ecdsa_ll_check_k_value(void)
432432
return REG_GET_BIT(ECDSA_RESULT_REG, ECDSA_K_VALUE_WARNING);
433433
}
434434

435+
/**
436+
* @brief Check if the ECDSA deterministic mode is supported
437+
*/
438+
static inline bool ecdsa_ll_is_deterministic_mode_supported(void)
439+
{
440+
return true;
441+
}
442+
435443
#ifdef __cplusplus
436444
}
437445
#endif

components/hal/test_apps/crypto/main/ecdsa/test_ecdsa.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ void test_ecdsa_sign(bool is_p256, uint8_t* sha, uint8_t* r_le, uint8_t* s_le, b
153153
uint8_t zeroes[32] = {0};
154154
uint16_t len;
155155

156-
#ifdef SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE
156+
#if !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP
157157
uint16_t det_loop_number = 1;
158-
#endif /* SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE */
158+
#endif /* !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP */
159159

160160
ecdsa_hal_config_t conf = {
161161
.mode = ECDSA_MODE_SIGN_GEN,
@@ -186,11 +186,11 @@ void test_ecdsa_sign(bool is_p256, uint8_t* sha, uint8_t* r_le, uint8_t* s_le, b
186186
bool process_again = false;
187187

188188
do {
189-
#ifdef SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE
190-
if (k_type == ECDSA_K_TYPE_DETERMINISITIC) {
189+
#if !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP
190+
if (ecdsa_ll_is_deterministic_mode_supported() && k_type == ECDSA_K_TYPE_DETERMINISITIC) {
191191
conf.loop_number = det_loop_number++;
192192
}
193-
#endif /* SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE */
193+
#endif /* !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP */
194194

195195
ecdsa_hal_gen_signature(&conf, sha_le, r_le, s_le, len);
196196

@@ -199,7 +199,7 @@ void test_ecdsa_sign(bool is_p256, uint8_t* sha, uint8_t* r_le, uint8_t* s_le, b
199199
|| !memcmp(s_le, zeroes, len);
200200

201201
#if SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE && !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP
202-
if (k_type == ECDSA_K_TYPE_DETERMINISITIC) {
202+
if (ecdsa_ll_is_deterministic_mode_supported() && k_type == ECDSA_K_TYPE_DETERMINISITIC) {
203203
process_again |= !ecdsa_hal_det_signature_k_check();
204204
}
205205
#endif /* SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE && !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP */
@@ -332,7 +332,9 @@ TEST(ecdsa, ecdsa_SECP256R1_corrupt_signature)
332332
#ifdef SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE
333333
TEST(ecdsa, ecdsa_SECP192R1_det_sign_and_verify)
334334
{
335-
if (!esp_efuse_is_ecdsa_p192_curve_supported()) {
335+
if (!ecdsa_ll_is_deterministic_mode_supported()) {
336+
ESP_LOGI(TAG, "Skipping test because ECDSA deterministic mode is not supported.");
337+
} else if (!esp_efuse_is_ecdsa_p192_curve_supported()) {
336338
ESP_LOGI(TAG, "Skipping test because ECDSA 192-curve operations are disabled.");
337339
} else {
338340
test_ecdsa_sign_and_verify(0, sha, ecdsa192_pub_x, ecdsa192_pub_y, false, ECDSA_K_TYPE_DETERMINISITIC);
@@ -341,7 +343,11 @@ TEST(ecdsa, ecdsa_SECP192R1_det_sign_and_verify)
341343

342344
TEST(ecdsa, ecdsa_SECP256R1_det_sign_and_verify)
343345
{
344-
test_ecdsa_sign_and_verify(1, sha, ecdsa256_pub_x, ecdsa256_pub_y, false, ECDSA_K_TYPE_DETERMINISITIC);
346+
if (!ecdsa_ll_is_deterministic_mode_supported()) {
347+
ESP_LOGI(TAG, "Skipping test because ECDSA deterministic mode is not supported.");
348+
} else {
349+
test_ecdsa_sign_and_verify(1, sha, ecdsa256_pub_x, ecdsa256_pub_y, false, ECDSA_K_TYPE_DETERMINISITIC);
350+
}
345351
}
346352
#endif /* SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE */
347353

components/mbedtls/port/ecdsa/ecdsa_alt.c

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -356,20 +356,22 @@ static int esp_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi* r, mbedtls_mpi* s
356356

357357
bool process_again = false;
358358

359-
#ifdef SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE
360-
uint16_t deterministic_loop_number = 1;
361-
#endif /* SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE */
359+
#if !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP
360+
uint16_t deterministic_loop_number __attribute__((unused)) = 1;
361+
#endif /* !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP */
362362

363363
do {
364364
ecdsa_hal_config_t conf = {
365365
.mode = ECDSA_MODE_SIGN_GEN,
366366
.curve = curve,
367367
.sha_mode = ECDSA_Z_USER_PROVIDED,
368368
.sign_type = k_type,
369-
#ifdef SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE
370-
.loop_number = deterministic_loop_number++,
371-
#endif /* SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE */
372369
};
370+
#if !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP
371+
if (ecdsa_ll_is_deterministic_mode_supported()) {
372+
conf.loop_number = deterministic_loop_number++;
373+
}
374+
#endif /* !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP */
373375

374376
if (use_km_key) {
375377
conf.use_km_key = 1;
@@ -395,7 +397,7 @@ static int esp_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi* r, mbedtls_mpi* s
395397
|| !memcmp(s_le, zeroes, len);
396398

397399
#if SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE && !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP
398-
if (k_type == ECDSA_K_TYPE_DETERMINISITIC) {
400+
if (ecdsa_ll_is_deterministic_mode_supported() && k_type == ECDSA_K_TYPE_DETERMINISITIC) {
399401
process_again |= !ecdsa_hal_det_signature_k_check();
400402
}
401403
#endif /* SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE && !SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP */
@@ -662,12 +664,30 @@ int __wrap_mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r,
662664
/*
663665
* Check `d` whether it contains the hardware key
664666
*/
667+
#if CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN
665668
if (d->MBEDTLS_PRIVATE(s) == ECDSA_KEY_MAGIC) {
666-
// Use hardware ECDSA peripheral
667-
return esp_ecdsa_sign(grp, r, s, d, buf, blen, ECDSA_K_TYPE_DETERMINISITIC);
668-
} else {
669-
return __real_mbedtls_ecdsa_sign_det_ext(grp, r, s, d, buf, blen, md_alg, f_rng_blind, p_rng_blind);
669+
if (ecdsa_ll_is_deterministic_mode_supported()) {
670+
// Use hardware ECDSA peripheral
671+
return esp_ecdsa_sign(grp, r, s, d, buf, blen, ECDSA_K_TYPE_DETERMINISITIC);
672+
} else {
673+
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
674+
}
670675
}
676+
#endif
677+
678+
#if CONFIG_MBEDTLS_TEE_SEC_STG_ECDSA_SIGN
679+
if (d->MBEDTLS_PRIVATE(s) == ECDSA_KEY_MAGIC_TEE) {
680+
if (ecdsa_ll_is_deterministic_mode_supported()) {
681+
// Use TEE secure storage
682+
return esp_ecdsa_tee_sign(grp, r, s, d, buf, blen, ECDSA_K_TYPE_DETERMINISITIC);
683+
} else {
684+
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
685+
}
686+
}
687+
#endif
688+
689+
// Fallback to software implementation
690+
return __real_mbedtls_ecdsa_sign_det_ext(grp, r, s, d, buf, blen, md_alg, f_rng_blind, p_rng_blind);
671691
}
672692

673693
extern int __real_mbedtls_ecdsa_sign_det_restartable(mbedtls_ecp_group *grp,
@@ -697,12 +717,30 @@ int __wrap_mbedtls_ecdsa_sign_det_restartable(mbedtls_ecp_group *grp,
697717
/*
698718
* Check `d` whether it contains the hardware key
699719
*/
720+
#if CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN
700721
if (d->MBEDTLS_PRIVATE(s) == ECDSA_KEY_MAGIC) {
701-
// Use hardware ECDSA peripheral
702-
return esp_ecdsa_sign(grp, r, s, d, buf, blen, ECDSA_K_TYPE_DETERMINISITIC);
703-
} else {
704-
return __real_mbedtls_ecdsa_sign_det_restartable(grp, r, s, d, buf, blen, md_alg, f_rng_blind, p_rng_blind, NULL);
722+
if (ecdsa_ll_is_deterministic_mode_supported()) {
723+
// Use hardware ECDSA peripheral
724+
return esp_ecdsa_sign(grp, r, s, d, buf, blen, ECDSA_K_TYPE_DETERMINISITIC);
725+
} else {
726+
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
727+
}
705728
}
729+
#endif
730+
731+
#if CONFIG_MBEDTLS_TEE_SEC_STG_ECDSA_SIGN
732+
if (d->MBEDTLS_PRIVATE(s) == ECDSA_KEY_MAGIC_TEE) {
733+
if (ecdsa_ll_is_deterministic_mode_supported()) {
734+
// Use TEE secure storage
735+
return esp_ecdsa_tee_sign(grp, r, s, d, buf, blen, ECDSA_K_TYPE_DETERMINISITIC);
736+
} else {
737+
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
738+
}
739+
}
740+
#endif
741+
742+
// Fallback to software implementation
743+
return __real_mbedtls_ecdsa_sign_det_ext(grp, r, s, d, buf, blen, md_alg, f_rng_blind, p_rng_blind);
706744
}
707745

708746
#endif /* SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE */
@@ -774,10 +812,11 @@ int __wrap_mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx,
774812
mbedtls_mpi_init(&r);
775813
mbedtls_mpi_init(&s);
776814

815+
ecdsa_sign_type_t k_type = ECDSA_K_TYPE_TRNG;
777816
#if defined(SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE) && defined(CONFIG_MBEDTLS_ECDSA_DETERMINISTIC)
778-
ecdsa_sign_type_t k_type = ECDSA_K_TYPE_DETERMINISITIC;
779-
#else
780-
ecdsa_sign_type_t k_type = ECDSA_K_TYPE_TRNG;
817+
if (ecdsa_ll_is_deterministic_mode_supported()) {
818+
k_type = ECDSA_K_TYPE_DETERMINISITIC;
819+
}
781820
#endif
782821

783822
/*

components/mbedtls/test_apps/main/test_mbedtls_ecdsa.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,20 @@ TEST_CASE("mbedtls ECDSA signature generation on SECP256R1", "[mbedtls][efuse_ke
277277

278278
TEST_CASE("mbedtls ECDSA deterministic signature generation on SECP192R1", "[mbedtls][efuse_key]")
279279
{
280-
test_ecdsa_sign(MBEDTLS_ECP_DP_SECP192R1, sha, ecdsa192_sign_pub_x, ecdsa192_sign_pub_y, true, SECP192R1_EFUSE_BLOCK);
280+
if (!ecdsa_ll_is_deterministic_mode_supported()) {
281+
ESP_LOGI(TAG, "Skipping test because ECDSA deterministic mode is not supported.");
282+
} else {
283+
test_ecdsa_sign(MBEDTLS_ECP_DP_SECP192R1, sha, ecdsa192_sign_pub_x, ecdsa192_sign_pub_y, true, SECP192R1_EFUSE_BLOCK);
284+
}
281285
}
282286

283287
TEST_CASE("mbedtls ECDSA deterministic signature generation on SECP256R1", "[mbedtls][efuse_key]")
284288
{
285-
test_ecdsa_sign(MBEDTLS_ECP_DP_SECP256R1, sha, ecdsa256_sign_pub_x, ecdsa256_sign_pub_y, true, SECP256R1_EFUSE_BLOCK);
289+
if (!ecdsa_ll_is_deterministic_mode_supported()) {
290+
ESP_LOGI(TAG, "Skipping test because ECDSA deterministic mode is not supported.");
291+
} else {
292+
test_ecdsa_sign(MBEDTLS_ECP_DP_SECP256R1, sha, ecdsa256_sign_pub_x, ecdsa256_sign_pub_y, true, SECP256R1_EFUSE_BLOCK);
293+
}
286294
}
287295

288296
#endif /* SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE */

components/soc/esp32h2/include/soc/Kconfig.soc_caps.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,14 @@ config SOC_ECDSA_USES_MPI
13311331
bool
13321332
default y
13331333

1334+
config SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE
1335+
bool
1336+
default y
1337+
1338+
config SOC_ECDSA_SUPPORT_HW_DETERMINISTIC_LOOP
1339+
bool
1340+
default y
1341+
13341342
config SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED
13351343
bool
13361344
default y

0 commit comments

Comments
 (0)