@@ -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 ()
2727endif ()
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
3839if (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" )
4546endif ()
4647
48+
4749list (APPEND mbedtls_include_dirs "${COMPONENT_DIR} /port/psa_driver/include" )
4850
4951idf_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
5658target_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
5962set (linkage_type PUBLIC)
@@ -177,16 +180,26 @@ if(CONFIG_MBEDTLS_SSL_PROTO_GMTSSL1_1)
177180 set_property (TARGET mbedx509 PROPERTY SOURCES ${src_x509} )
178181endif ()
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
183193add_library (mbed-builtin ALIAS builtin )
184194set_target_properties (builtin PROPERTIES OUTPUT_NAME "mbed-builtin" )
185195
186196target_include_directories (tfpsacrypto PUBLIC "port/include" )
187197
188198message (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 ()
201214endforeach ()
202215
203- # 3rd party libraries from the mbedTLS project
204- list (APPEND mbedtls_targets everest p256m)
205-
206216set (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
213223if (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
272282target_sources (mbedtls PRIVATE ${mbedtls_target_sources} )
283+ target_compile_definitions (mbedtls PUBLIC __STDC_WANT_LIB_EXT1__=0 )
273284
274285if (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+
278299endif ()
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
282310if (CONFIG_SOC_SHA_SUPPORTED)
@@ -377,11 +405,11 @@ if(CONFIG_SOC_HMAC_SUPPORTED)
377405endif ()
378406
379407if (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 )
385413endif ()
386414
387415if (CONFIG_SOC_HMAC_SUPPORTED)
@@ -479,7 +507,7 @@ if(CONFIG_PM_ENABLE)
479507 target_link_libraries (tfpsacrypto PRIVATE idf::esp_pm )
480508endif ()
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