blob: f38cfe00be336cf395b9a93b1343fb78706a5799 [file] [log] [blame]
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +00001option(USE_STATIC_MBEDTLS_LIBRARY "Build mbed TLS static library." ON)
2option(USE_SHARED_MBEDTLS_LIBRARY "Build mbed TLS shared library." OFF)
Manuel Pégourié-Gonnardcfa9a452015-01-23 13:33:31 +00003option(LINK_WITH_PTHREAD "Explicitly link mbed TLS library to pthread." OFF)
Moshe Shahar6763fe42019-07-24 14:19:35 +03004option(LINK_WITH_TRUSTED_STORAGE "Explicitly link mbed TLS library to trusted_storage." OFF)
Paul Bakker9d3a7e42011-01-05 15:24:43 +00005
Ashley Duncand85a7e92019-04-29 20:35:06 +12006# Set the project root directory if it's not already defined, as may happen if
7# the library folder is included directly by a parent project, without
8# including the top level CMakeLists.txt.
9if(NOT DEFINED MBEDTLS_DIR)
10 set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR})
11endif()
Paul Bakker9d3a7e42011-01-05 15:24:43 +000012
David Horstmanne31e2872023-01-09 12:41:58 +000013# If set, make MBEDTLS_CONFIG_FILE and MBEDTLS_USER_CONFIG_FILE into PATHs
14if(DEFINED MBEDTLS_CONFIG_FILE)
15 set(MBEDTLS_CONFIG_FILE "" CACHE PATH "Mbed TLS config file (overrides default).")
16endif()
17if(DEFINED MBEDTLS_USER_CONFIG_FILE)
18 set(MBEDTLS_USER_CONFIG_FILE "" CACHE PATH "Mbed TLS user config file (appended to default).")
19endif()
20
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020021set(src_crypto
22 aes.c
23 aesni.c
24 arc4.c
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000025 aria.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020026 asn1parse.c
27 asn1write.c
28 base64.c
29 bignum.c
30 blowfish.c
31 camellia.c
32 ccm.c
Daniel King34b822c2016-05-15 17:28:08 -030033 chacha20.c
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020034 chachapoly.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020035 cipher.c
36 cipher_wrap.c
gabor-mezei-arm90559722021-07-12 16:31:22 +020037 constant_time.c
Robert Cragiedc5c7b92015-12-11 15:49:45 +000038 cmac.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020039 ctr_drbg.c
40 des.c
41 dhm.c
42 ecdh.c
43 ecdsa.c
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020044 ecjpake.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020045 ecp.c
46 ecp_curves.c
47 entropy.c
48 entropy_poll.c
49 error.c
50 gcm.c
51 havege.c
Thomas Fossati656864b2016-07-17 08:51:22 +010052 hkdf.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020053 hmac_drbg.c
54 md.c
55 md2.c
56 md4.c
57 md5.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020058 memory_buffer_alloc.c
Hanno Beckerc518c3b2021-01-28 07:08:08 +000059 mps_reader.c
60 mps_trace.c
Ron Eldorcb349ac2018-07-15 09:29:47 +030061 nist_kw.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020062 oid.c
63 padlock.c
64 pem.c
65 pk.c
66 pk_wrap.c
67 pkcs12.c
68 pkcs5.c
69 pkparse.c
70 pkwrite.c
71 platform.c
Andres Amaya Garciabc7bdbf2018-04-24 08:29:20 -050072 platform_util.c
Daniel Kingadc32c02016-05-16 18:25:45 -030073 poly1305.c
Gilles Peskinee59236f2018-01-27 23:32:46 +010074 psa_crypto.c
Ronald Cron7ceee8d2021-03-17 16:55:43 +010075 psa_crypto_aead.c
Ronald Cron0ff57952021-03-08 16:46:35 +010076 psa_crypto_cipher.c
Ronald Crond7906322021-01-28 16:07:56 +010077 psa_crypto_client.c
Steven Cooremancd84cb42020-07-16 20:28:36 +020078 psa_crypto_driver_wrappers.c
Ronald Cron00b7bfc2020-11-25 15:25:26 +010079 psa_crypto_ecp.c
Steven Cooreman0e307642021-02-18 16:18:32 +010080 psa_crypto_hash.c
Steven Cooreman896d51e2021-03-19 15:24:23 +010081 psa_crypto_mac.c
Ronald Cron00b7bfc2020-11-25 15:25:26 +010082 psa_crypto_rsa.c
Gilles Peskinea899a722019-06-24 14:06:43 +020083 psa_crypto_se.c
Gilles Peskine961849f2018-11-30 18:54:54 +010084 psa_crypto_slot_management.c
Darryl Greendb2b8db2018-06-15 13:06:04 +010085 psa_crypto_storage.c
Gilles Peskine6194dc22018-11-16 22:24:15 +010086 psa_its_file.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020087 ripemd160.c
88 rsa.c
Hanno Beckera565f542017-10-11 11:00:19 +010089 rsa_internal.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020090 sha1.c
91 sha256.c
92 sha512.c
93 threading.c
Manuel Pégourié-Gonnard21dcc1e2015-06-25 10:59:15 +020094 timing.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +020095 version.c
96 version_features.c
97 xtea.c
98)
99
Christoph M. Wintersteiger655ddab2019-01-21 17:26:19 +0000100list(APPEND src_crypto ${thirdparty_src})
101
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +0200102set(src_x509
103 certs.c
104 pkcs11.c
105 x509.c
106 x509_create.c
107 x509_crl.c
108 x509_crt.c
109 x509_csr.c
110 x509write_crt.c
111 x509write_csr.c
112)
113
114set(src_tls
115 debug.c
Andres AG788aa4a2016-09-14 14:32:09 +0100116 net_sockets.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +0200117 ssl_cache.c
118 ssl_ciphersuites.c
119 ssl_cli.c
120 ssl_cookie.c
Hanno Becker6e7051a2020-02-05 11:33:54 +0000121 ssl_msg.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +0200122 ssl_srv.c
123 ssl_ticket.c
Hanno Becker471c0c92020-02-05 11:32:03 +0000124 ssl_tls.c
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100125 ssl_tls13_keys.c
Manuel Pégourié-Gonnard463e09d2015-06-24 11:54:19 +0200126)
127
Paul Bakker76f03112013-11-28 17:20:04 +0100128if(CMAKE_COMPILER_IS_GNUCC)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200129 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes")
Paul Bakker76f03112013-11-28 17:20:04 +0100130endif(CMAKE_COMPILER_IS_GNUCC)
131
Manuel Pégourié-Gonnard31855452014-06-25 15:59:50 +0200132if(CMAKE_COMPILER_IS_CLANG)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200133 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-declarations -Wmissing-prototypes -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
Manuel Pégourié-Gonnard31855452014-06-25 15:59:50 +0200134endif(CMAKE_COMPILER_IS_CLANG)
135
Manuel Pégourié-Gonnardba2c8762015-06-25 09:35:46 +0200136if(WIN32)
137 set(libs ${libs} ws2_32)
138endif(WIN32)
139
Andres Amaya Garciabf7fe4f2018-06-21 20:21:38 +0100140if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
TabascoEye7f3ef272018-04-27 13:14:59 +0200141 SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
142 SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
143 SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
144 SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
Andres Amaya Garciabf7fe4f2018-06-21 20:21:38 +0100145endif()
TabascoEye7f3ef272018-04-27 13:14:59 +0200146
Augustin Cavalier60bc47d2018-04-11 20:27:32 -0400147if(HAIKU)
Simon Butcher6331cb02018-07-10 11:48:42 +0100148 set(libs ${libs} network)
Augustin Cavalier60bc47d2018-04-11 20:27:32 -0400149endif(HAIKU)
150
Manuel Pégourié-Gonnardba2c8762015-06-25 09:35:46 +0200151if(USE_PKCS11_HELPER_LIBRARY)
152 set(libs ${libs} pkcs11-helper)
153endif(USE_PKCS11_HELPER_LIBRARY)
154
155if(ENABLE_ZLIB_SUPPORT)
156 set(libs ${libs} ${ZLIB_LIBRARIES})
157endif(ENABLE_ZLIB_SUPPORT)
158
159if(LINK_WITH_PTHREAD)
160 set(libs ${libs} pthread)
161endif()
162
Moshe Shahar7e367652019-07-15 15:50:19 +0300163if(LINK_WITH_TRUSTED_STORAGE)
164 set(libs ${libs} trusted_storage)
165endif()
166
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000167if (NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200168 message(FATAL_ERROR "Need to choose static or shared mbedtls build!")
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000169endif(NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
Paul Bakker9d3a7e42011-01-05 15:24:43 +0000170
Raef Coles995c66f2020-10-13 16:30:41 +0100171set(mbedtls_target "${MBEDTLS_TARGET_PREFIX}mbedtls")
172set(mbedx509_target "${MBEDTLS_TARGET_PREFIX}mbedx509")
173set(mbedcrypto_target "${MBEDTLS_TARGET_PREFIX}mbedcrypto")
174
175set(mbedtls_target ${mbedtls_target} PARENT_SCOPE)
176set(mbedx509_target ${mbedx509_target} PARENT_SCOPE)
177set(mbedcrypto_target ${mbedcrypto_target} PARENT_SCOPE)
178
179if (USE_STATIC_MBEDTLS_LIBRARY)
180 set(mbedtls_static_target ${mbedtls_target})
181 set(mbedx509_static_target ${mbedx509_target})
182 set(mbedcrypto_static_target ${mbedcrypto_target})
183endif()
184
185set(target_libraries ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
Gilles Peskine3ca8a922020-07-02 13:07:37 +0200186
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000187if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
Raef Coles995c66f2020-10-13 16:30:41 +0100188 string(APPEND mbedtls_static_target "_static")
189 string(APPEND mbedx509_static_target "_static")
190 string(APPEND mbedcrypto_static_target "_static")
191
Gilles Peskine3ca8a922020-07-02 13:07:37 +0200192 list(APPEND target_libraries
Raef Coles995c66f2020-10-13 16:30:41 +0100193 ${mbedcrypto_static_target}
194 ${mbedx509_static_target}
195 ${mbedtls_static_target})
hasufell3c6409b2014-03-06 15:49:08 +0100196endif()
Paul Bakker9d3a7e42011-01-05 15:24:43 +0000197
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000198if(USE_STATIC_MBEDTLS_LIBRARY)
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200199 add_library(${mbedcrypto_static_target} STATIC ${src_crypto})
200 set_target_properties(${mbedcrypto_static_target} PROPERTIES OUTPUT_NAME mbedcrypto)
Raef Coles4762edd2020-10-13 16:31:41 +0100201 target_link_libraries(${mbedcrypto_static_target} PUBLIC ${libs})
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200202
Gilles Peskine55d53532020-03-09 17:48:13 +0100203 add_library(${mbedx509_static_target} STATIC ${src_x509})
204 set_target_properties(${mbedx509_static_target} PROPERTIES OUTPUT_NAME mbedx509)
Raef Coles4762edd2020-10-13 16:31:41 +0100205 target_link_libraries(${mbedx509_static_target} PUBLIC ${libs} ${mbedcrypto_static_target})
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200206
Gilles Peskine55d53532020-03-09 17:48:13 +0100207 add_library(${mbedtls_static_target} STATIC ${src_tls})
208 set_target_properties(${mbedtls_static_target} PROPERTIES OUTPUT_NAME mbedtls)
Raef Coles4762edd2020-10-13 16:31:41 +0100209 target_link_libraries(${mbedtls_static_target} PUBLIC ${libs} ${mbedx509_static_target})
Manuel Pégourié-Gonnarde1d34d12015-06-25 14:53:13 +0200210endif(USE_STATIC_MBEDTLS_LIBRARY)
Paul Bakker9d3a7e42011-01-05 15:24:43 +0000211
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000212if(USE_SHARED_MBEDTLS_LIBRARY)
Ron Eldorb2832282017-10-09 19:07:26 +0300213 set(CMAKE_LIBRARY_PATH ${CMAKE_CURRENT_BINARY_DIR})
Raef Coles995c66f2020-10-13 16:30:41 +0100214 add_library(${mbedcrypto_target} SHARED ${src_crypto})
Dave Rodgmane4f1aac2023-03-23 18:06:54 +0000215 set_target_properties(${mbedcrypto_target} PROPERTIES VERSION 2.28.3 SOVERSION 7)
Raef Coles4762edd2020-10-13 16:31:41 +0100216 target_link_libraries(${mbedcrypto_target} PUBLIC ${libs})
Paul Bakker2a5c7a82012-05-10 21:54:28 +0000217
Raef Coles995c66f2020-10-13 16:30:41 +0100218 add_library(${mbedx509_target} SHARED ${src_x509})
Dave Rodgmane4f1aac2023-03-23 18:06:54 +0000219 set_target_properties(${mbedx509_target} PROPERTIES VERSION 2.28.3 SOVERSION 1)
Raef Coles4762edd2020-10-13 16:31:41 +0100220 target_link_libraries(${mbedx509_target} PUBLIC ${libs} ${mbedcrypto_target})
Manuel Pégourié-Gonnardc7781ad2015-06-25 09:49:02 +0200221
Raef Coles995c66f2020-10-13 16:30:41 +0100222 add_library(${mbedtls_target} SHARED ${src_tls})
Dave Rodgmane4f1aac2023-03-23 18:06:54 +0000223 set_target_properties(${mbedtls_target} PROPERTIES VERSION 2.28.3 SOVERSION 14)
Raef Coles4762edd2020-10-13 16:31:41 +0100224 target_link_libraries(${mbedtls_target} PUBLIC ${libs} ${mbedx509_target})
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000225endif(USE_SHARED_MBEDTLS_LIBRARY)
Manuel Pégourié-Gonnarde1d34d12015-06-25 14:53:13 +0200226
Gilles Peskine3ca8a922020-07-02 13:07:37 +0200227foreach(target IN LISTS target_libraries)
Gilles Peskine76dd3aa2020-07-02 15:58:37 +0200228 # Include public header files from /include and other directories
229 # declared by /3rdparty/**/CMakeLists.txt. Include private header files
230 # from /library and others declared by /3rdparty/**/CMakeLists.txt.
231 # /library needs to be listed explicitly when building .c files outside
232 # of /library (which currently means: under /3rdparty).
Gilles Peskine3ca8a922020-07-02 13:07:37 +0200233 target_include_directories(${target}
234 PUBLIC ${MBEDTLS_DIR}/include/
235 PUBLIC ${thirdparty_inc_public}
Gilles Peskinededff7a2020-07-02 13:13:27 +0200236 PRIVATE ${MBEDTLS_DIR}/library/
Gilles Peskine3ca8a922020-07-02 13:07:37 +0200237 PRIVATE ${thirdparty_inc})
238 target_compile_definitions(${target}
239 PRIVATE ${thirdparty_def})
David Horstmanne31e2872023-01-09 12:41:58 +0000240 # Pass-through MBEDTLS_CONFIG_FILE and MBEDTLS_USER_CONFIG_FILE
241 if(DEFINED MBEDTLS_CONFIG_FILE)
242 target_compile_definitions(${target}
243 PUBLIC MBEDTLS_CONFIG_FILE="${MBEDTLS_CONFIG_FILE}")
244 endif()
245 if(DEFINED MBEDTLS_USER_CONFIG_FILE)
246 target_compile_definitions(${target}
247 PUBLIC MBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}")
248 endif()
Gilles Peskine3ca8a922020-07-02 13:07:37 +0200249 install(TARGETS ${target}
hasufell3c6409b2014-03-06 15:49:08 +0100250 DESTINATION ${LIB_INSTALL_DIR}
Gilles Peskine280165c2020-07-02 13:19:17 +0200251 PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
Gilles Peskine3ca8a922020-07-02 13:07:37 +0200252endforeach(target)
Manuel Pégourié-Gonnarde1d34d12015-06-25 14:53:13 +0200253
Raef Coles995c66f2020-10-13 16:30:41 +0100254set(lib_target "${MBEDTLS_TARGET_PREFIX}lib")
255
256add_custom_target(${lib_target} DEPENDS ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
Gilles Peskine55d53532020-03-09 17:48:13 +0100257if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
Raef Coles995c66f2020-10-13 16:30:41 +0100258 add_dependencies(${lib_target} ${mbedcrypto_static_target} ${mbedx509_static_target} ${mbedtls_static_target})
Manuel Pégourié-Gonnarde1d34d12015-06-25 14:53:13 +0200259endif()