Skip to content

Commit 1ba8abd

Browse files
committed
fix(mbedtls): Enable signature verification s/w fallback when ECDSA curve is disabled
1 parent 5b1588e commit 1ba8abd

File tree

7 files changed

+83
-77
lines changed

7 files changed

+83
-77
lines changed

components/efuse/esp32h21/esp_efuse_fields.c

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "stdlib.h"
1111
#include "esp_types.h"
1212
#include "assert.h"
13-
#include "hal/efuse_ll.h"
1413
#include "esp_err.h"
1514
#include "esp_log.h"
1615
#include "soc/efuse_periph.h"
@@ -52,40 +51,3 @@ esp_err_t esp_efuse_enable_rom_secure_download_mode(void)
5251
}
5352
return esp_efuse_write_field_bit(ESP_EFUSE_ENABLE_SECURITY_DOWNLOAD);
5453
}
55-
56-
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED
57-
bool esp_efuse_is_ecdsa_p192_curve_supported(void)
58-
{
59-
uint32_t current_curve = efuse_ll_get_ecdsa_curve_mode();
60-
return (current_curve == ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_BOTH_P192_P256_BIT || current_curve == ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P192_BIT);
61-
}
62-
63-
esp_err_t esp_efuse_enable_ecdsa_p192_curve_mode(void)
64-
{
65-
esp_err_t err;
66-
uint8_t current_curve, next_curve;
67-
68-
current_curve = efuse_ll_get_ecdsa_curve_mode();
69-
// Check if already in desired state
70-
if (current_curve == ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_BOTH_P192_P256_BIT || current_curve == ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P192_BIT) {
71-
ESP_EARLY_LOGD(TAG, "ECDSA P-192 curve mode is already enabled");
72-
return ESP_OK;
73-
}
74-
75-
// Check if write is disabled or already locked to P256
76-
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) {
77-
ESP_EARLY_LOGE(TAG, "ECDSA curve mode is locked, cannot enable P-192 curve");
78-
return ESP_FAIL;
79-
}
80-
81-
// Attempt to write new curve mode
82-
next_curve = ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_BOTH_P192_P256_BIT;
83-
err = esp_efuse_write_field_blob(ESP_EFUSE_ECDSA_CURVE_MODE, &next_curve, ESP_EFUSE_ECDSA_CURVE_MODE[0]->bit_count);
84-
if (err != ESP_OK) {
85-
ESP_EARLY_LOGE(TAG, "Failed to enable ECDSA P-192 curve %d", err);
86-
return err;
87-
}
88-
89-
return ESP_OK;
90-
}
91-
#endif /* SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED */

components/efuse/esp32h21/include/esp_efuse_chip.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,6 @@ typedef enum {
7777
ESP_EFUSE_KEY_PURPOSE_MAX, /**< MAX PURPOSE */
7878
} esp_efuse_purpose_t;
7979

80-
typedef enum {
81-
ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P256_BIT = 0,
82-
ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P192_BIT = 1,
83-
ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_BOTH_P192_P256_BIT = 2,
84-
ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P256_BIT_LOCKED = 3,
85-
} esp_efuse_ecdsa_curve_mode_t;
86-
8780
#ifdef __cplusplus
8881
}
8982
#endif

components/efuse/include/esp_efuse.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,14 +806,30 @@ esp_err_t esp_efuse_check_errors(void);
806806
*/
807807
esp_err_t esp_efuse_destroy_block(esp_efuse_block_t block);
808808

809-
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED
809+
#if SOC_ECDSA_SUPPORTED
810810
/**
811811
* @brief Checks if 192-bit ECDSA curve operations are supported.
812812
*
813813
* This function checks if the current eFuse configuration supports 192-bit ECDSA curve operations.
814814
*/
815815
bool esp_efuse_is_ecdsa_p192_curve_supported(void);
816816

817+
/**
818+
* @brief Checks if 256-bit ECDSA curve operations are supported.
819+
*
820+
* This function checks if the current eFuse configuration supports 256-bit ECDSA curve operations.
821+
*/
822+
bool esp_efuse_is_ecdsa_p256_curve_supported(void);
823+
#endif /* SOC_ECDSA_SUPPORTED*/
824+
825+
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED
826+
typedef enum {
827+
ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P256_BIT = 0,
828+
ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P192_BIT = 1,
829+
ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_BOTH_P192_P256_BIT = 2,
830+
ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P256_BIT_LOCKED = 3,
831+
} esp_efuse_ecdsa_curve_mode_t;
832+
817833
/**
818834
* @brief Enables 192-bit ECDSA curve operations by setting the appropriate eFuse value.
819835
*

components/efuse/src/esp_efuse_fields.c

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -16,6 +16,8 @@
1616
#include "esp_log.h"
1717
#include "soc/efuse_periph.h"
1818
#include "sys/param.h"
19+
#include "soc/soc_caps.h"
20+
#include "hal/efuse_ll.h"
1921

2022
static __attribute__((unused)) const char *TAG = "efuse";
2123

@@ -81,3 +83,56 @@ esp_err_t esp_efuse_update_secure_version(uint32_t secure_version)
8183
}
8284
return ESP_OK;
8385
}
86+
87+
#if SOC_ECDSA_SUPPORTED
88+
bool esp_efuse_is_ecdsa_p192_curve_supported(void)
89+
{
90+
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED
91+
uint32_t current_curve = efuse_ll_get_ecdsa_curve_mode();
92+
return (current_curve == ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_BOTH_P192_P256_BIT || current_curve == ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P192_BIT);
93+
#else
94+
return true;
95+
#endif /* SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED */
96+
}
97+
98+
bool esp_efuse_is_ecdsa_p256_curve_supported(void)
99+
{
100+
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED
101+
uint32_t current_curve = efuse_ll_get_ecdsa_curve_mode();
102+
return (current_curve != ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P192_BIT);
103+
#else
104+
return true;
105+
#endif /* SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED */
106+
}
107+
#endif /* SOC_ECDSA_SUPPORTED */
108+
109+
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED
110+
esp_err_t esp_efuse_enable_ecdsa_p192_curve_mode(void)
111+
{
112+
esp_err_t err;
113+
uint8_t current_curve, next_curve;
114+
115+
current_curve = efuse_ll_get_ecdsa_curve_mode();
116+
// Check if already in desired state
117+
if (current_curve == ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_BOTH_P192_P256_BIT || current_curve == ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_ONLY_P192_BIT) {
118+
ESP_EARLY_LOGD(TAG, "ECDSA P-192 curve mode is already enabled");
119+
return ESP_OK;
120+
}
121+
122+
// Check if write is disabled or already locked to P256
123+
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) {
124+
ESP_EARLY_LOGE(TAG, "ECDSA curve mode is locked, cannot enable P-192 curve");
125+
return ESP_FAIL;
126+
}
127+
128+
// Attempt to write new curve mode
129+
next_curve = ESP_EFUSE_ECDSA_CURVE_MODE_ALLOW_BOTH_P192_P256_BIT;
130+
err = esp_efuse_write_field_blob(ESP_EFUSE_ECDSA_CURVE_MODE, &next_curve, ESP_EFUSE_ECDSA_CURVE_MODE[0]->bit_count);
131+
if (err != ESP_OK) {
132+
ESP_EARLY_LOGE(TAG, "Failed to enable ECDSA P-192 curve %d", err);
133+
return err;
134+
}
135+
136+
return ESP_OK;
137+
}
138+
#endif /* SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED */

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

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -289,36 +289,27 @@ TEST_TEAR_DOWN(ecdsa)
289289

290290
TEST(ecdsa, ecdsa_SECP192R1_signature_verification)
291291
{
292-
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED
293292
if (!esp_efuse_is_ecdsa_p192_curve_supported()) {
294293
ESP_LOGI(TAG, "Skipping test because ECDSA 192-curve operations are disabled.");
295-
} else
296-
#endif
297-
{
294+
} else {
298295
TEST_ASSERT_EQUAL(0, test_ecdsa_verify(0, sha, ecdsa192_r, ecdsa192_s, ecdsa192_pub_x, ecdsa192_pub_y));
299296
}
300297
}
301298

302299
TEST(ecdsa, ecdsa_SECP192R1_sign_and_verify)
303300
{
304-
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED
305301
if (!esp_efuse_is_ecdsa_p192_curve_supported()) {
306302
ESP_LOGI(TAG, "Skipping test because ECDSA 192-curve operations are disabled.");
307-
} else
308-
#endif
309-
{
303+
} else {
310304
test_ecdsa_sign_and_verify(0, sha, ecdsa192_pub_x, ecdsa192_pub_y, false, ECDSA_K_TYPE_TRNG);
311305
}
312306
}
313307

314308
TEST(ecdsa, ecdsa_SECP192R1_corrupt_signature)
315309
{
316-
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED
317310
if (!esp_efuse_is_ecdsa_p192_curve_supported()) {
318311
ESP_LOGI(TAG, "Skipping test because ECDSA 192-curve operations are disabled.");
319-
} else
320-
#endif
321-
{
312+
} else {
322313
test_ecdsa_corrupt_data(0, sha, ecdsa192_r, ecdsa192_s, ecdsa192_pub_x, ecdsa192_pub_y);
323314
}
324315
}
@@ -341,12 +332,9 @@ TEST(ecdsa, ecdsa_SECP256R1_corrupt_signature)
341332
#ifdef SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE
342333
TEST(ecdsa, ecdsa_SECP192R1_det_sign_and_verify)
343334
{
344-
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED
345335
if (!esp_efuse_is_ecdsa_p192_curve_supported()) {
346336
ESP_LOGI(TAG, "Skipping test because ECDSA 192-curve operations are disabled.");
347-
} else
348-
#endif
349-
{
337+
} else {
350338
test_ecdsa_sign_and_verify(0, sha, ecdsa192_pub_x, ecdsa192_pub_y, false, ECDSA_K_TYPE_DETERMINISITIC);
351339
}
352340
}
@@ -360,12 +348,9 @@ TEST(ecdsa, ecdsa_SECP256R1_det_sign_and_verify)
360348
#ifdef SOC_ECDSA_SUPPORT_EXPORT_PUBKEY
361349
TEST(ecdsa, ecdsa_SECP192R1_export_pubkey)
362350
{
363-
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED
364351
if (!esp_efuse_is_ecdsa_p192_curve_supported()) {
365352
ESP_LOGI(TAG, "Skipping test because ECDSA 192-curve operations are disabled.");
366-
} else
367-
#endif
368-
{
353+
} else {
369354
test_ecdsa_export_pubkey(0, ecdsa192_pub_x, ecdsa192_pub_y, 0);
370355
}
371356
}

components/mbedtls/port/ecdsa/ecdsa_alt.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,9 @@ int __wrap_mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp,
911911
const mbedtls_mpi *s,
912912
mbedtls_ecdsa_restart_ctx *rs_ctx)
913913
{
914-
if ((grp->id == MBEDTLS_ECP_DP_SECP192R1 || grp->id == MBEDTLS_ECP_DP_SECP256R1) && blen == ECDSA_SHA_LEN) {
914+
if (((grp->id == MBEDTLS_ECP_DP_SECP192R1 && esp_efuse_is_ecdsa_p192_curve_supported())
915+
|| (grp->id == MBEDTLS_ECP_DP_SECP256R1 && esp_efuse_is_ecdsa_p256_curve_supported()))
916+
&& blen == ECDSA_SHA_LEN) {
915917
return esp_ecdsa_verify(grp, buf, blen, Q, r, s);
916918
} else {
917919
return __real_mbedtls_ecdsa_verify_restartable(grp, buf, blen, Q, r, s, rs_ctx);

components/mbedtls/test_apps/main/test_mbedtls_ecdsa.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,8 @@ void test_ecdsa_verify(mbedtls_ecp_group_id id, const uint8_t *hash, const uint8
146146

147147
TEST_CASE("mbedtls ECDSA signature verification performance on SECP192R1", "[mbedtls]")
148148
{
149-
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED
150-
if (!esp_efuse_is_ecdsa_p192_curve_supported()) {
151-
ESP_LOGI(TAG, "Skipping test because ECDSA 192-curve operations are disabled.");
152-
} else
153-
#endif
154-
{
155-
test_ecdsa_verify(MBEDTLS_ECP_DP_SECP192R1, sha, ecdsa192_r, ecdsa192_s,
156-
ecdsa192_pub_x, ecdsa192_pub_y);
157-
}
149+
test_ecdsa_verify(MBEDTLS_ECP_DP_SECP192R1, sha, ecdsa192_r, ecdsa192_s,
150+
ecdsa192_pub_x, ecdsa192_pub_y);
158151
}
159152

160153
TEST_CASE("mbedtls ECDSA signature verification performance on SECP256R1", "[mbedtls]")

0 commit comments

Comments
 (0)