CMakeLists.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. idf_build_get_property(idf_target IDF_TARGET)
  2. idf_build_get_property(python PYTHON)
  3. if(NOT ${IDF_TARGET} STREQUAL "linux")
  4. set(priv_requires soc esp_hw_support)
  5. if(NOT BOOTLOADER_BUILD)
  6. list(APPEND priv_requires esp_pm)
  7. endif()
  8. endif()
  9. set(mbedtls_srcs "")
  10. set(mbedtls_include_dirs "port/include" "mbedtls/include" "mbedtls/library")
  11. if(CONFIG_MBEDTLS_CERTIFICATE_BUNDLE)
  12. list(APPEND mbedtls_srcs "esp_crt_bundle/esp_crt_bundle.c")
  13. list(APPEND mbedtls_include_dirs "esp_crt_bundle/include")
  14. endif()
  15. idf_component_register(SRCS "${mbedtls_srcs}"
  16. INCLUDE_DIRS "${mbedtls_include_dirs}"
  17. PRIV_REQUIRES "${priv_requires}"
  18. )
  19. # Determine the type of mbedtls component library
  20. if(mbedtls_srcs STREQUAL "")
  21. # For no sources in component library we must use "INTERFACE"
  22. set(linkage_type INTERFACE)
  23. else()
  24. set(linkage_type PUBLIC)
  25. endif()
  26. if(CONFIG_MBEDTLS_CERTIFICATE_BUNDLE)
  27. set(bundle_name "x509_crt_bundle")
  28. set(DEFAULT_CRT_DIR ${COMPONENT_DIR}/esp_crt_bundle)
  29. # Generate custom certificate bundle using the generate_cert_bundle utility
  30. set(GENERATE_CERT_BUNDLEPY ${python} ${COMPONENT_DIR}/esp_crt_bundle/gen_crt_bundle.py)
  31. if(CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL)
  32. list(APPEND crt_paths ${DEFAULT_CRT_DIR}/cacrt_all.pem ${DEFAULT_CRT_DIR}/cacrt_local.pem)
  33. elseif(CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN)
  34. list(APPEND crt_paths ${DEFAULT_CRT_DIR}/cacrt_all.pem ${DEFAULT_CRT_DIR}/cacrt_local.pem)
  35. list(APPEND args --filter ${DEFAULT_CRT_DIR}/cmn_crt_authorities.csv)
  36. endif()
  37. # Add deprecated root certs if enabled. This config is not visible if the default cert
  38. # bundle is not selected
  39. if(CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEPRECATED_LIST)
  40. list(APPEND crt_paths ${DEFAULT_CRT_DIR}/cacrt_deprecated.pem)
  41. endif()
  42. if(CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE)
  43. get_filename_component(custom_bundle_path
  44. ${CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE_PATH} ABSOLUTE BASE_DIR "${project_dir}")
  45. list(APPEND crt_paths ${custom_bundle_path})
  46. endif()
  47. list(APPEND args --input ${crt_paths} -q)
  48. get_filename_component(crt_bundle
  49. ${bundle_name}
  50. ABSOLUTE BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  51. # Generate bundle according to config
  52. add_custom_command(OUTPUT ${crt_bundle}
  53. COMMAND ${GENERATE_CERT_BUNDLEPY} ${args}
  54. DEPENDS ${custom_bundle_path}
  55. VERBATIM)
  56. add_custom_target(custom_bundle DEPENDS ${cert_bundle})
  57. add_dependencies(${COMPONENT_LIB} custom_bundle)
  58. target_add_binary_data(${COMPONENT_LIB} ${crt_bundle} BINARY)
  59. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  60. APPEND PROPERTY ADDITIONAL_CLEAN_FILES
  61. "${crt_bundle}")
  62. endif()
  63. # Only build mbedtls libraries
  64. set(ENABLE_TESTING CACHE BOOL OFF)
  65. set(ENABLE_PROGRAMS CACHE BOOL OFF)
  66. # Use pre-generated source files in mbedtls repository
  67. set(GEN_FILES CACHE BOOL OFF)
  68. # Make sure mbedtls finds the same Python interpreter as IDF uses
  69. idf_build_get_property(python PYTHON)
  70. set(Python3_EXECUTABLE ${python})
  71. # Needed to for include_next includes to work from within mbedtls
  72. include_directories("${COMPONENT_DIR}/port/include")
  73. # Import mbedtls library targets
  74. add_subdirectory(mbedtls)
  75. # Use port specific implementation of net_socket.c instead of one from mbedtls
  76. get_target_property(src_tls mbedtls SOURCES)
  77. list(REMOVE_ITEM src_tls net_sockets.c)
  78. set_property(TARGET mbedtls PROPERTY SOURCES ${src_tls})
  79. if(CONFIG_MBEDTLS_SSL_PROTO_GMTSSL1_1)
  80. get_target_property(src_tls mbedtls SOURCES)
  81. list(REMOVE_ITEM src_tls ssl_ciphersuites.c ssl_cli.c ssl_tls.c)
  82. set_property(TARGET mbedtls PROPERTY SOURCES ${src_tls})
  83. get_target_property(src_crypto mbedcrypto SOURCES)
  84. list(REMOVE_ITEM src_crypto cipher_wrap.c ecdsa.c ecp.c ecp_curves.c oid.c pk_wrap.c)
  85. set_property(TARGET mbedcrypto PROPERTY SOURCES ${src_crypto})
  86. get_target_property(src_x509 mbedx509 SOURCES)
  87. list(REMOVE_ITEM src_x509 x509_crt.c)
  88. set_property(TARGET mbedx509 PROPERTY SOURCES ${src_x509})
  89. endif()
  90. # Core libraries from the mbedTLS project
  91. set(mbedtls_targets mbedtls mbedcrypto mbedx509)
  92. # 3rd party libraries from the mbedTLS project
  93. list(APPEND mbedtls_targets everest p256m)
  94. set(mbedtls_target_sources "${COMPONENT_DIR}/port/mbedtls_debug.c"
  95. "${COMPONENT_DIR}/port/esp_platform_time.c")
  96. if(CONFIG_MBEDTLS_DYNAMIC_BUFFER)
  97. set(mbedtls_target_sources ${mbedtls_target_sources}
  98. "${COMPONENT_DIR}/port/dynamic/esp_mbedtls_dynamic_impl.c"
  99. "${COMPONENT_DIR}/port/dynamic/esp_ssl_cli.c"
  100. "${COMPONENT_DIR}/port/dynamic/esp_ssl_srv.c"
  101. "${COMPONENT_DIR}/port/dynamic/esp_ssl_tls.c")
  102. endif()
  103. if(${IDF_TARGET} STREQUAL "linux")
  104. set(mbedtls_target_sources ${mbedtls_target_sources} "${COMPONENT_DIR}/port/net_sockets.c")
  105. endif()
  106. # While updating to MbedTLS release/v3.4.0, building mbedtls/library/psa_crypto.c
  107. # clang produces an unreachable-code warning.
  108. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  109. target_compile_options(mbedcrypto PRIVATE "-Wno-unreachable-code")
  110. endif()
  111. # net_sockets.c should only be compiled if BSD socket functions are available.
  112. # Do this by checking if lwip component is included into the build.
  113. if(CONFIG_LWIP_ENABLE)
  114. list(APPEND mbedtls_target_sources "${COMPONENT_DIR}/port/net_sockets.c")
  115. idf_component_get_property(lwip_lib lwip COMPONENT_LIB)
  116. target_link_libraries(${COMPONENT_LIB} ${linkage_type} ${lwip_lib})
  117. endif()
  118. # Add port files to mbedtls targets
  119. target_sources(mbedtls PRIVATE ${mbedtls_target_sources})
  120. # Choose perihperal type
  121. if(CONFIG_SOC_SHA_SUPPORTED)
  122. if(CONFIG_SOC_SHA_SUPPORT_DMA)
  123. set(SHA_PERIPHERAL_TYPE "dma")
  124. elseif(CONFIG_SOC_SHA_SUPPORT_PARALLEL_ENG)
  125. set(SHA_PERIPHERAL_TYPE "parallel_engine")
  126. else()
  127. set(SHA_PERIPHERAL_TYPE "block")
  128. endif()
  129. endif()
  130. if(CONFIG_SOC_AES_SUPPORTED)
  131. if(CONFIG_SOC_AES_SUPPORT_DMA)
  132. set(AES_PERIPHERAL_TYPE "dma")
  133. else()
  134. set(AES_PERIPHERAL_TYPE "block")
  135. endif()
  136. endif()
  137. if(SHA_PERIPHERAL_TYPE STREQUAL "dma")
  138. target_include_directories(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/sha/dma/include")
  139. if(NOT CONFIG_SOC_SHA_GDMA)
  140. set(SHA_DMA_SRCS "${COMPONENT_DIR}/port/sha/dma/esp_sha_crypto_dma_impl.c")
  141. else()
  142. set(SHA_DMA_SRCS "${COMPONENT_DIR}/port/sha/dma/esp_sha_gdma_impl.c")
  143. endif()
  144. target_sources(mbedcrypto PRIVATE "${SHA_DMA_SRCS}")
  145. endif()
  146. if(AES_PERIPHERAL_TYPE STREQUAL "dma")
  147. if(NOT CONFIG_SOC_AES_GDMA)
  148. set(AES_DMA_SRCS "${COMPONENT_DIR}/port/aes/dma/esp_aes_crypto_dma_impl.c")
  149. else()
  150. set(AES_DMA_SRCS "${COMPONENT_DIR}/port/aes/dma/esp_aes_gdma_impl.c"
  151. "${COMPONENT_DIR}/port/crypto_shared_gdma/esp_crypto_shared_gdma.c")
  152. endif()
  153. target_include_directories(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/aes/dma/include")
  154. target_sources(mbedcrypto PRIVATE "${AES_DMA_SRCS}")
  155. endif()
  156. if(NOT ${IDF_TARGET} STREQUAL "linux")
  157. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_hardware.c")
  158. endif()
  159. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_mem.c"
  160. "${COMPONENT_DIR}/port/esp_timing.c"
  161. )
  162. if(CONFIG_SOC_AES_SUPPORTED)
  163. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/aes/esp_aes_xts.c"
  164. "${COMPONENT_DIR}/port/aes/esp_aes_common.c"
  165. "${COMPONENT_DIR}/port/aes/${AES_PERIPHERAL_TYPE}/esp_aes.c"
  166. )
  167. endif()
  168. if(CONFIG_SOC_SHA_SUPPORTED)
  169. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/sha/esp_sha.c"
  170. "${COMPONENT_DIR}/port/sha/${SHA_PERIPHERAL_TYPE}/sha.c"
  171. )
  172. endif()
  173. # CONFIG_ESP_TLS_USE_DS_PERIPHERAL can be enabled only for the supported targets.
  174. if(CONFIG_ESP_TLS_USE_DS_PERIPHERAL)
  175. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_ds/esp_rsa_sign_alt.c")
  176. endif()
  177. # Note: some mbedTLS hardware acceleration can be enabled/disabled by config.
  178. #
  179. # We don't need to filter aes.c as this uses a different prefix (esp_aes_x) and the
  180. # config option only changes the prefixes in the header so mbedtls_aes_x compiles to esp_aes_x
  181. #
  182. # The other port-specific files don't override internal mbedTLS functions, they just add new functions.
  183. if(CONFIG_MBEDTLS_HARDWARE_MPI)
  184. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/bignum/esp_bignum.c"
  185. "${COMPONENT_DIR}/port/bignum/bignum_alt.c")
  186. endif()
  187. if(CONFIG_MBEDTLS_HARDWARE_SHA)
  188. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/sha/${SHA_PERIPHERAL_TYPE}/esp_sha1.c"
  189. "${COMPONENT_DIR}/port/sha/${SHA_PERIPHERAL_TYPE}/esp_sha256.c"
  190. "${COMPONENT_DIR}/port/sha/${SHA_PERIPHERAL_TYPE}/esp_sha512.c"
  191. )
  192. endif()
  193. if(CONFIG_MBEDTLS_HARDWARE_GCM OR (NOT CONFIG_SOC_AES_SUPPORT_GCM AND CONFIG_MBEDTLS_HARDWARE_AES))
  194. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/aes/esp_aes_gcm.c")
  195. endif()
  196. if(CONFIG_MBEDTLS_HARDWARE_ECC)
  197. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/ecc/esp_ecc.c"
  198. "${COMPONENT_DIR}/port/ecc/ecc_alt.c")
  199. endif()
  200. if(CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN OR CONFIG_MBEDTLS_HARDWARE_ECDSA_VERIFY)
  201. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/ecdsa/ecdsa_alt.c")
  202. if(CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN)
  203. target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=mbedtls_ecdsa_sign")
  204. target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=mbedtls_ecdsa_sign_restartable")
  205. target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=mbedtls_ecdsa_write_signature")
  206. target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=mbedtls_ecdsa_write_signature_restartable")
  207. endif()
  208. if(CONFIG_MBEDTLS_HARDWARE_ECDSA_VERIFY)
  209. target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=mbedtls_ecdsa_verify")
  210. endif()
  211. endif()
  212. if(CONFIG_MBEDTLS_ROM_MD5)
  213. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/md/esp_md.c")
  214. endif()
  215. if(CONFIG_MBEDTLS_USE_CRYPTO_ROM_IMPL)
  216. target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/mbedtls_rom/mbedtls_rom_osi.c")
  217. target_link_libraries(${COMPONENT_LIB} PRIVATE "-u mbedtls_rom_osi_functions_init")
  218. endif()
  219. foreach(target ${mbedtls_targets})
  220. target_compile_definitions(${target} PUBLIC -DMBEDTLS_CONFIG_FILE="mbedtls/esp_config.h")
  221. endforeach()
  222. if(CONFIG_MBEDTLS_DYNAMIC_BUFFER)
  223. set(WRAP_FUNCTIONS
  224. mbedtls_ssl_write_client_hello
  225. mbedtls_ssl_handshake_client_step
  226. mbedtls_ssl_handshake_server_step
  227. mbedtls_ssl_read
  228. mbedtls_ssl_write
  229. mbedtls_ssl_session_reset
  230. mbedtls_ssl_free
  231. mbedtls_ssl_setup
  232. mbedtls_ssl_send_alert_message
  233. mbedtls_ssl_close_notify)
  234. foreach(wrap ${WRAP_FUNCTIONS})
  235. target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=${wrap}")
  236. endforeach()
  237. endif()
  238. set_property(TARGET mbedcrypto APPEND PROPERTY LINK_INTERFACE_LIBRARIES mbedtls)
  239. if(CONFIG_PM_ENABLE)
  240. target_link_libraries(mbedcrypto PRIVATE idf::esp_pm)
  241. endif()
  242. if(CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN OR CONFIG_MBEDTLS_HARDWARE_ECDSA_VERIFY)
  243. target_link_libraries(mbedcrypto PRIVATE idf::efuse)
  244. endif()
  245. target_link_libraries(${COMPONENT_LIB} ${linkage_type} ${mbedtls_targets})
  246. if(CONFIG_ESP_TLS_USE_DS_PERIPHERAL)
  247. # The linker seems to be unable to resolve all the dependencies without increasing this
  248. set_property(TARGET mbedcrypto APPEND PROPERTY LINK_INTERFACE_MULTIPLICITY 6)
  249. endif()
  250. # Link esp-cryptoauthlib to mbedtls
  251. if(CONFIG_ATCA_MBEDTLS_ECDSA)
  252. idf_component_optional_requires(PRIVATE espressif__esp-cryptoauthlib esp-cryptoauthlib)
  253. endif()