Skip to content

Commit cadda99

Browse files
committed
Merge branch 'feat/update_mbedtls_4_1_1' into 'master'
feat(mbedtls): update to version 4.1.0 Closes IDF-15544 See merge request espressif/esp-idf!47234
2 parents f70550f + cd38d68 commit cadda99

39 files changed

Lines changed: 311 additions & 529 deletions

File tree

‎components/bootloader/Kconfig.projbuild‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -558,22 +558,13 @@ menu "Security features"
558558
depends on SECURE_SIGNED_APPS_ECDSA_V2_SCHEME
559559
default SECURE_BOOT_ECDSA_KEY_LEN_256_BITS
560560
help
561-
Select the ECDSA key size. Three key sizes are supported depending upon on the target:
561+
Select the ECDSA key size. Two key sizes are supported depending on the target:
562562

563-
- 192 bit key using NISTP192 curve (Legacy, not recommended)
564563
- 256 bit key using NISTP256 curve (Recommended)
565564
- 384 bit key using NISTP384 curve (Recommended)
566565

567-
The advantage of using 384 and 256 bit keys is the extra randomness which makes it difficult to be
568-
bruteforced compared to 192 bit key.
569566
At present, both key sizes are practically implausible to bruteforce.
570567

571-
config SECURE_BOOT_ECDSA_KEY_LEN_192_BITS
572-
bool "Using ECC curve NISTP192 (Legacy, not recommended)"
573-
depends on SECURE_SIGNED_APPS_ECDSA_V2_SCHEME
574-
help
575-
This legacy option is not recommended for new designs. Prefer NISTP256 or NISTP384.
576-
577568
config SECURE_BOOT_ECDSA_KEY_LEN_256_BITS
578569
bool "Using ECC curve NISTP256 (Recommended)"
579570
depends on SECURE_SIGNED_APPS_ECDSA_V2_SCHEME
@@ -708,7 +699,7 @@ menu "Security features"
708699

709700
Key file is an ECDSA private key (NIST256p curve) in PEM format for Secure Boot V1.
710701
Key file is an RSA private key in PEM format for Secure Boot V2 (RSA scheme).
711-
Key file is an ECDSA private key (NIST 192p, 256p or 384p) in PEM format for Secure Boot V2 (ECDSA scheme).
702+
Key file is an ECDSA private key (256p or 384p) in PEM format for Secure Boot V2 (ECDSA scheme).
712703

713704
Path is evaluated relative to the project directory.
714705

‎components/bootloader/project_include.cmake‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ if(CONFIG_SECURE_SIGNED_APPS)
4343
set(bootloader_binary_files
4444
${bootloader_binary_files}
4545
"${BOOTLOADER_BUILD_DIR}/bootloader-reflash-digest.bin"
46-
"${BOOTLOADER_BUILD_DIR}/secure-bootloader-key-192.bin"
4746
"${BOOTLOADER_BUILD_DIR}/secure-bootloader-key-256.bin"
4847
)
4948
endif()
@@ -66,9 +65,7 @@ if(CONFIG_SECURE_SIGNED_APPS)
6665
"Secure Boot Signing Key ${CONFIG_SECURE_BOOT_SIGNING_KEY} does not exist. Generate using:"
6766
"\tidf.py secure-generate-signing-key ${CONFIG_SECURE_BOOT_SIGNING_KEY}")
6867
else()
69-
if(CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_192_BITS)
70-
set(scheme "ecdsa192")
71-
elseif(CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_256_BITS)
68+
if(CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_256_BITS)
7269
set(scheme "ecdsa256")
7370
elseif(CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_384_BITS)
7471
set(scheme "ecdsa384")

‎components/bootloader_support/include/esp_secure_boot.h‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,9 @@ typedef enum {
7070
#define ESP_SECURE_BOOT_SCHEME ESP_SECURE_BOOT_V2_ECDSA
7171
#endif
7272

73-
/* Expected ECDSA curve ID from menuconfig "ECDSA key size" (matches ECDSA_CURVE_P192/P256/P384 in ROM) */
73+
/* Expected ECDSA curve ID from menuconfig "ECDSA key size" (matches ECDSA_CURVE_P256/P384 in ROM) */
7474
#if CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME
75-
#if CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_192_BITS
76-
#define ESP_SECURE_BOOT_ECDSA_CURVE_ID ECDSA_CURVE_P192
77-
#elif CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_256_BITS
75+
#if CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_256_BITS
7876
#define ESP_SECURE_BOOT_ECDSA_CURVE_ID ECDSA_CURVE_P256
7977
#elif CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_384_BITS
8078
#define ESP_SECURE_BOOT_ECDSA_CURVE_ID ECDSA_CURVE_P384

‎components/bootloader_support/src/secure_boot_v2/secure_boot_ecdsa_signature.c‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313

1414
ESP_LOG_ATTR_TAG(TAG, "secure_boot_v2_ecdsa");
1515

16-
#if CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_192_BITS
17-
#define ECDSA_INTEGER_LEN 24
18-
#elif CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_384_BITS
16+
#if CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_384_BITS
1917
#define ECDSA_INTEGER_LEN 48
2018
#else
2119
#define ECDSA_INTEGER_LEN 32
@@ -41,13 +39,6 @@ esp_err_t verify_ecdsa_signature_block(const ets_secure_boot_signature_t *sig_bl
4139
psa_ecc_family_t curve_family;
4240

4341
switch(trusted_block->ecdsa.key.curve_id) {
44-
#if CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_192_BITS
45-
case ECDSA_CURVE_P192:
46-
key_size = 24;
47-
curve_family = PSA_ECC_FAMILY_SECP_R1;
48-
psa_set_key_bits(&key_attributes, PSA_BYTES_TO_BITS(key_size));
49-
break;
50-
#endif /* CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_192_BITS */
5142
#if CONFIG_SECURE_BOOT_ECDSA_KEY_LEN_256_BITS
5243
case ECDSA_CURVE_P256:
5344
key_size = 32;

‎components/bootloader_support/src/secure_boot_v2/secure_boot_signatures_app.c‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,6 @@ static esp_err_t validate_signature_block(const ets_secure_boot_sig_block_t *blo
5050
}
5151
#endif
5252

53-
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED && CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME
54-
if (block->ecdsa.key.curve_id == ECDSA_CURVE_P192) {
55-
// Enabling ECDSA-192 Curve mode
56-
esp_err_t err = esp_efuse_enable_ecdsa_p192_curve_mode();
57-
if (err != ESP_OK) {
58-
ESP_LOGE(TAG, "Failed to enable ECDSA-192 curve mode: %d", err);
59-
return err;
60-
}
61-
}
62-
#endif
63-
6453
return ESP_OK;
6554
}
6655

‎components/bootloader_support/src/secure_boot_v2/secure_boot_signatures_bootloader.c‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,6 @@ static esp_err_t validate_signature_block(const ets_secure_boot_sig_block_t *blo
7979
}
8080
#endif
8181

82-
#if SOC_ECDSA_P192_CURVE_DEFAULT_DISABLED && CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME
83-
if (block->ecdsa.key.curve_id == ECDSA_CURVE_P192) {
84-
// Enabling ECDSA-192 Curve mode
85-
esp_err_t err = esp_efuse_enable_ecdsa_p192_curve_mode();
86-
if (err != ESP_OK) {
87-
ESP_LOGE(TAG, "Failed to enable ECDSA-192 curve mode: %d", err);
88-
return err;
89-
}
90-
}
91-
#endif
92-
9382
return ESP_OK;
9483
}
9584

‎components/esp-tls/esp_tls_mbedtls.c‎

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -493,59 +493,37 @@ void esp_mbedtls_cleanup(esp_tls_t *tls)
493493
mbedtls_x509_crt_free(&tls->cacert);
494494
mbedtls_x509_crt_free(&tls->clientcert);
495495

496+
/* For opaque keys (DS peripheral, hardware ECDSA), mbedtls_pk_free() does
497+
* not destroy the PSA key — ownership is external. Destroy it manually
498+
* before calling mbedtls_pk_free(). */
496499
#ifdef CONFIG_ESP_TLS_USE_DS_PERIPHERAL
497500
if (mbedtls_pk_get_type(&tls->clientkey) == MBEDTLS_PK_RSASSA_PSS) {
498-
mbedtls_rsa_context *rsa = tls->clientkey.MBEDTLS_PRIVATE(pk_ctx);
499-
if (rsa != NULL) {
500-
mbedtls_rsa_free(rsa);
501-
mbedtls_free(rsa);
502-
rsa = NULL;
503-
}
504501
if (tls->clientkey.MBEDTLS_PRIVATE(priv_id) != PSA_KEY_ID_NULL) {
505502
psa_destroy_key(tls->clientkey.MBEDTLS_PRIVATE(priv_id));
503+
tls->clientkey.MBEDTLS_PRIVATE(priv_id) = PSA_KEY_ID_NULL;
506504
}
507-
tls->clientkey.MBEDTLS_PRIVATE(pk_ctx) = NULL;
508505
}
509-
510-
// Similar cleanup for server key
511506
if (mbedtls_pk_get_type(&tls->serverkey) == MBEDTLS_PK_RSASSA_PSS) {
512-
mbedtls_rsa_context *rsa = tls->serverkey.MBEDTLS_PRIVATE(pk_ctx);
513-
if (rsa != NULL) {
514-
mbedtls_rsa_free(rsa);
515-
mbedtls_free(rsa);
516-
rsa = NULL;
517-
}
518507
if (tls->serverkey.MBEDTLS_PRIVATE(priv_id) != PSA_KEY_ID_NULL) {
519508
psa_destroy_key(tls->serverkey.MBEDTLS_PRIVATE(priv_id));
509+
tls->serverkey.MBEDTLS_PRIVATE(priv_id) = PSA_KEY_ID_NULL;
520510
}
521-
tls->serverkey.MBEDTLS_PRIVATE(pk_ctx) = NULL;
522511
}
523512
#endif
524513

525514
#ifdef CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN
526-
/* In mbedtls v4.0, ECDSA keys require manual cleanup of the keypair structure */
527515
if (mbedtls_pk_get_type(&tls->clientkey) == MBEDTLS_PK_ECDSA) {
528516
ESP_LOGD(TAG, "Cleaning up client key");
529-
mbedtls_ecp_keypair *keypair = tls->clientkey.MBEDTLS_PRIVATE(pk_ctx);
530-
if (keypair != NULL) {
531-
mbedtls_ecp_keypair_free(keypair);
532-
mbedtls_free(keypair);
533-
keypair = NULL;
517+
if (tls->clientkey.MBEDTLS_PRIVATE(priv_id) != PSA_KEY_ID_NULL) {
518+
psa_destroy_key(tls->clientkey.MBEDTLS_PRIVATE(priv_id));
519+
tls->clientkey.MBEDTLS_PRIVATE(priv_id) = PSA_KEY_ID_NULL;
534520
}
535-
psa_destroy_key(tls->clientkey.MBEDTLS_PRIVATE(priv_id));
536-
tls->clientkey.MBEDTLS_PRIVATE(pk_ctx) = NULL;
537521
}
538-
539-
// Similar cleanup for server key
540522
if (mbedtls_pk_get_type(&tls->serverkey) == MBEDTLS_PK_ECDSA) {
541-
mbedtls_ecp_keypair *keypair = tls->serverkey.MBEDTLS_PRIVATE(pk_ctx);
542-
if (keypair != NULL) {
543-
mbedtls_ecp_keypair_free(keypair);
544-
mbedtls_free(keypair);
545-
keypair = NULL;
523+
if (tls->serverkey.MBEDTLS_PRIVATE(priv_id) != PSA_KEY_ID_NULL) {
524+
psa_destroy_key(tls->serverkey.MBEDTLS_PRIVATE(priv_id));
525+
tls->serverkey.MBEDTLS_PRIVATE(priv_id) = PSA_KEY_ID_NULL;
546526
}
547-
psa_destroy_key(tls->serverkey.MBEDTLS_PRIVATE(priv_id));
548-
tls->serverkey.MBEDTLS_PRIVATE(pk_ctx) = NULL;
549527
}
550528
#endif
551529

‎components/esp-tls/test_apps/main/app_main.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2026 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -16,6 +16,7 @@
1616
#endif
1717
#include "esp_newlib.h"
1818
#include "psa/crypto.h"
19+
#include "esp_crypto_lock.h"
1920
#if SOC_SHA_SUPPORT_SHA512
2021
#define SHA_TYPE SHA2_512
2122
#else
@@ -59,6 +60,12 @@ void setUp(void)
5960
heap_caps_free(buf);
6061
psa_destroy_key(key_id);
6162

63+
// Trigger lazy initialization of the MPI hardware mutex.
64+
// In mbedtls 4.x, RSA key parsing goes through PSA which validates
65+
// the key using MPI hardware, creating this lock on first use.
66+
esp_crypto_mpi_lock_acquire();
67+
esp_crypto_mpi_lock_release();
68+
6269
test_utils_record_free_mem();
6370
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
6471
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_GENERAL));

‎components/esp-tls/test_apps/main/test_esp_tls.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ TEST_CASE("esp_tls_server session create delete", "[esp-tls]")
135135
TEST_ASSERT_LESS_THAN_INT(0, ret);
136136
// free the allocated memory.
137137
esp_tls_server_session_delete(tls);
138-
139138
}
140139
#endif /* CONFIG_ESP_TLS_USING_MBEDTLS */
141140

‎components/mbedtls/CMakeLists.txt‎

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if(NOT ${IDF_TARGET} STREQUAL "linux")
2222
set(priv_requires soc esp_hw_support)
2323
if(NOT BOOTLOADER_BUILD)
2424
list(APPEND priv_requires esp_pm esp_driver_dma)
25-
set(requires esp_security)
25+
set(requires esp_security esp_hal_security)
2626
endif()
2727
endif()
2828

@@ -33,6 +33,7 @@ set(mbedtls_include_dirs
3333
"mbedtls/library"
3434
"mbedtls/tf-psa-crypto/core"
3535
"mbedtls/tf-psa-crypto/drivers/builtin/src/"
36+
"mbedtls/tf-psa-crypto/extras"
3637
)
3738

3839
if(CONFIG_MBEDTLS_USE_CRYPTO_ROM_IMPL)
@@ -44,6 +45,7 @@ if(CONFIG_MBEDTLS_CERTIFICATE_BUNDLE)
4445
list(APPEND mbedtls_include_dirs "esp_crt_bundle/include")
4546
endif()
4647

48+
4749
list(APPEND mbedtls_include_dirs "${COMPONENT_DIR}/port/psa_driver/include")
4850

4951
idf_component_register(SRCS "${mbedtls_srcs}"
@@ -54,6 +56,7 @@ idf_component_register(SRCS "${mbedtls_srcs}"
5456

5557
# Add MBEDTLS_MAJOR_VERSION definition to the component library
5658
target_compile_definitions(${COMPONENT_LIB} INTERFACE MBEDTLS_MAJOR_VERSION=4)
59+
target_compile_definitions(${COMPONENT_LIB} INTERFACE __STDC_WANT_LIB_EXT1__=0)
5760

5861
# Set the type of mbedtls component library
5962
set(linkage_type PUBLIC)
@@ -177,16 +180,26 @@ if(CONFIG_MBEDTLS_SSL_PROTO_GMTSSL1_1)
177180
set_property(TARGET mbedx509 PROPERTY SOURCES ${src_x509})
178181
endif()
179182

180-
# Core libraries from the mbedTLS project
181-
set(mbedtls_targets mbedtls mbedx509 tfpsacrypto builtin)
183+
# Core libraries from the mbedTLS project, including 3rd-party (everest,
184+
# p256-m) and the new tf-psa-crypto sub-libs introduced in 4.1
185+
# (extras, platform, utilities). All of these need MBEDTLS_CONFIG_FILE
186+
# and the optimization flags applied uniformly; previously the loop ran
187+
# before the 3rd-party / new targets were appended, leaving them at the
188+
# default ESP-IDF -Og without esp_config.h.
189+
set(mbedtls_compile_targets mbedtls mbedx509 tfpsacrypto builtin
190+
everest p256-m extras platform utilities)
191+
set(mbedtls_link_targets mbedtls mbedx509 tfpsacrypto)
182192

183193
add_library(mbed-builtin ALIAS builtin)
184194
set_target_properties(builtin PROPERTIES OUTPUT_NAME "mbed-builtin")
185195

186196
target_include_directories(tfpsacrypto PUBLIC "port/include")
187197

188198
message(STATUS "Setting up mbedtls configuration")
189-
foreach(target ${mbedtls_targets})
199+
foreach(target ${mbedtls_compile_targets})
200+
if(NOT TARGET ${target})
201+
continue()
202+
endif()
190203
target_compile_definitions(${target} PUBLIC -DMBEDTLS_CONFIG_FILE="mbedtls/esp_config.h")
191204
set_config_files_compile_definitions(${target})
192205
target_compile_definitions(${target} PUBLIC MBEDTLS_MAJOR_VERSION=4)
@@ -200,9 +213,6 @@ foreach(target ${mbedtls_targets})
200213
endif()
201214
endforeach()
202215

203-
# 3rd party libraries from the mbedTLS project
204-
list(APPEND mbedtls_targets everest p256m)
205-
206216
set(mbedtls_target_sources "${COMPONENT_DIR}/port/mbedtls_debug.c"
207217
"${COMPONENT_DIR}/port/esp_platform_time.c"
208218
"${COMPONENT_DIR}/port/esp_timing.c")
@@ -212,8 +222,8 @@ list(APPEND mbedtls_target_sources "${COMPONENT_DIR}/port/esp_psa_crypto_init.c"
212222
# Only compile esp_psa_its.c if nvs_flash component is available
213223
if(NOT ${IDF_TARGET} STREQUAL "linux")
214224
if(IDF_BUILD_V2)
215-
# For v2: conditionally compile source and link only if nvs_flash target exists
216-
target_sources(
225+
# For v2: conditionally compile source and link only if nvs_flash target exists
226+
target_sources(
217227
tfpsacrypto PRIVATE
218228
"$<$<TARGET_EXISTS:idf::nvs_flash>:${COMPONENT_DIR}/port/psa_crypto_storage/esp_psa_its.c>"
219229
)
@@ -270,13 +280,31 @@ endif()
270280

271281
# Add port files to mbedtls targets
272282
target_sources(mbedtls PRIVATE ${mbedtls_target_sources})
283+
target_compile_definitions(mbedtls PUBLIC __STDC_WANT_LIB_EXT1__=0)
273284

274285
if(NOT ${IDF_TARGET} STREQUAL "linux")
275-
target_link_libraries(tfpsacrypto PUBLIC idf::esp_security)
286+
# All tf-psa-crypto internal targets need esp_security includes because
287+
# the public PSA crypto header chain (psa/crypto.h -> crypto_struct.h ->
288+
# crypto_driver_contexts_composites.h) includes ESP driver context headers
289+
# that depend on esp_security headers (e.g., esp_ds.h).
290+
target_link_libraries(tfpsacrypto PRIVATE idf::esp_security)
276291
target_link_libraries(builtin PRIVATE idf::esp_security)
277-
target_link_libraries(p256m PRIVATE idf::esp_security)
292+
target_link_libraries(p256-m PRIVATE idf::esp_security)
293+
target_link_libraries(extras PRIVATE idf::esp_security)
294+
target_link_libraries(platform PRIVATE idf::esp_security)
295+
target_link_libraries(utilities PRIVATE idf::esp_security)
296+
target_link_libraries(mbedtls PRIVATE idf::esp_security)
297+
target_link_libraries(mbedx509 PRIVATE idf::esp_security)
298+
278299
endif()
279300

301+
# Workaround: CMake 4.x Unix Makefiles generator fails to create build-order
302+
# dependencies for STATIC library targets consumed via $<TARGET_OBJECTS:> when
303+
# they are also linked via target_link_libraries in the same target. This
304+
# ensures builtin/everest/p256-m are compiled before tfpsacrypto archives
305+
# their objects into libtfpsacrypto.a.
306+
add_dependencies(tfpsacrypto builtin everest p256-m extras platform utilities)
307+
280308
# Choose peripheral type
281309

282310
if(CONFIG_SOC_SHA_SUPPORTED)
@@ -377,11 +405,11 @@ if(CONFIG_SOC_HMAC_SUPPORTED)
377405
endif()
378406

379407
if(CONFIG_SOC_DIG_SIGN_SUPPORTED AND CONFIG_MBEDTLS_HARDWARE_RSA_DS_PERIPHERAL)
380-
target_sources(tfpsacrypto PRIVATE
408+
target_sources(tfpsacrypto PRIVATE
381409
"${COMPONENT_DIR}/port/psa_driver/esp_rsa_ds/psa_crypto_driver_esp_rsa_ds.c"
382410
"${COMPONENT_DIR}/port/psa_driver/esp_rsa_ds/psa_crypto_driver_esp_rsa_ds_utilities.c"
383411
)
384-
target_link_libraries(tfpsacrypto PRIVATE idf::efuse)
412+
target_link_libraries(tfpsacrypto PRIVATE idf::efuse)
385413
endif()
386414

387415
if(CONFIG_SOC_HMAC_SUPPORTED)
@@ -479,7 +507,7 @@ if(CONFIG_PM_ENABLE)
479507
target_link_libraries(tfpsacrypto PRIVATE idf::esp_pm)
480508
endif()
481509

482-
target_link_libraries(${COMPONENT_LIB} ${linkage_type} ${mbedtls_targets})
510+
target_link_libraries(${COMPONENT_LIB} ${linkage_type} ${mbedtls_link_targets})
483511

484512
# Ensure PSA crypto initialization is included in the build.
485513
# On macOS (Mach-O), C symbols have a leading underscore prefix.

0 commit comments

Comments
 (0)