CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. cmake_minimum_required(VERSION 3.12)
  2. project(libsrtp2 VERSION 2.4.0 LANGUAGES C)
  3. set(PACKAGE_VERSION ${CMAKE_PROJECT_VERSION})
  4. set(PACKAGE_STRING "${CMAKE_PROJECT_NAME} ${CMAKE_PROJECT_VERSION}")
  5. list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
  6. include(TestBigEndian)
  7. include(CheckIncludeFile)
  8. include(CheckFunctionExists)
  9. include(CheckTypeSize)
  10. include(CheckCSourceCompiles)
  11. test_big_endian(WORDS_BIGENDIAN)
  12. if (NOT APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
  13. set (HAVE_X86 TRUE)
  14. else ()
  15. set (HAVE_X86 FALSE)
  16. endif ()
  17. check_include_file(arpa/inet.h HAVE_ARPA_INET_H)
  18. check_include_file(byteswap.h HAVE_BYTESWAP_H)
  19. check_include_file(inttypes.h HAVE_INTTYPES_H)
  20. check_include_file(machine/types.h HAVE_MACHINE_TYPES_H)
  21. check_include_file(netinet/in.h HAVE_NETINET_IN_H)
  22. check_include_file(stdint.h HAVE_STDINT_H)
  23. check_include_file(stdlib.h HAVE_STDLIB_H)
  24. check_include_file(sys/int_types.h HAVE_SYS_INT_TYPES_H)
  25. check_include_file(sys/socket.h HAVE_SYS_SOCKET_H)
  26. check_include_file(sys/types.h HAVE_SYS_TYPES_H)
  27. check_include_file(unistd.h HAVE_UNISTD_H)
  28. check_include_file(windows.h HAVE_WINDOWS_H)
  29. check_include_file(winsock2.h HAVE_WINSOCK2_H)
  30. check_function_exists(sigaction HAVE_SIGACTION)
  31. check_function_exists(inet_aton HAVE_INET_ATON)
  32. check_function_exists(usleep HAVE_USLEEP)
  33. check_type_size(uint8_t UINT8_T)
  34. check_type_size(uint16_t UINT16_T)
  35. check_type_size(uint32_t UINT32_T)
  36. check_type_size(uint64_t UINT64_T)
  37. check_type_size(int32_t INT32_T)
  38. check_type_size("unsigned long" SIZEOF_UNSIGNED_LONG)
  39. check_type_size("unsigned long long" SIZEOF_UNSIGNED_LONG_LONG)
  40. check_c_source_compiles("inline void func(); void func() { } int main() { func(); return 0; }" HAVE_INLINE)
  41. if(NOT HAVE_INLINE)
  42. check_c_source_compiles("__inline void func(); void func() { } int main() { func(); return 0; }" HAVE___INLINE)
  43. endif()
  44. set(ENABLE_DEBUG_LOGGING OFF CACHE BOOL "Enable debug logging in all modules")
  45. set(ERR_REPORTING_STDOUT OFF CACHE BOOL "Enable logging to stdout")
  46. set(ERR_REPORTING_FILE "" CACHE FILEPATH "Use file for logging")
  47. set(ENABLE_OPENSSL OFF CACHE BOOL "Enable OpenSSL crypto engine")
  48. set(ENABLE_MBEDTLS OFF CACHE BOOL "Enable MbedTLS crypto engine")
  49. set(ENABLE_NSS OFF CACHE BOOL "Enable NSS crypto engine")
  50. set(TEST_APPS ON CACHE BOOL "Build test applications")
  51. set(BUILD_WITH_SANITIZERS OFF CACHE BOOL "Adds sanitizer flags to compiler")
  52. option(BUILD_SHARED_LIBS "Build shared library" OFF)
  53. if(ENABLE_OPENSSL OR ENABLE_MBEDTLS OR ENABLE_NSS)
  54. set(USE_EXTERNAL_CRYPTO TRUE)
  55. else()
  56. set(USE_EXTERNAL_CRYPTO FALSE)
  57. endif()
  58. if(ENABLE_OPENSSL)
  59. if(ENABLE_NSS OR ENABLE_MBEDTLS)
  60. message(FATAL_ERROR "ssl conflict. can not enable openssl and mbedtls or nss simultaneously.")
  61. endif()
  62. find_package(OpenSSL REQUIRED)
  63. set(OPENSSL ${ENABLE_OPENSSL} CACHE BOOL INTERNAL)
  64. set(GCM ${ENABLE_OPENSSL} CACHE BOOL INTERNAL)
  65. endif()
  66. if(ENABLE_MBEDTLS)
  67. if(ENABLE_OPENSSL OR ENABLE_NSS)
  68. message(FATAL_ERROR "ssl conflict. can not enable mbedtls and openssl or nss simultaneously.")
  69. endif()
  70. find_package(MbedTLS REQUIRED)
  71. set(MBEDTLS ${ENABLE_MBEDTLS} CACHE BOOL INTERNAL)
  72. set(GCM ${ENABLE_MBEDTLS} CACHE BOOL INTERNAL)
  73. endif()
  74. if(ENABLE_NSS)
  75. if(ENABLE_OPENSSL OR ENABLE_MBEDTLS)
  76. message(FATAL_ERROR "ssl conflict. can not enable nss and openssl or mbedtls simultaneously.")
  77. endif()
  78. find_package(NSS REQUIRED)
  79. set(NSS ${ENABLE_NSS} CACHE BOOL INTERNAL)
  80. set(GCM ${ENABLE_NSS} CACHE BOOL INTERNAL)
  81. endif()
  82. set(CONFIG_FILE_DIR ${CMAKE_CURRENT_BINARY_DIR})
  83. include_directories(${CONFIG_FILE_DIR})
  84. configure_file(config_in_cmake.h ${CONFIG_FILE_DIR}/config.h)
  85. add_definitions(-DHAVE_CONFIG_H)
  86. if(BUILD_WITH_SANITIZERS AND NOT WIN32)
  87. if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  88. set(SANITIZERS address,undefined)
  89. else()
  90. set(SANITIZERS leak,address,undefined)
  91. endif()
  92. message(STATUS "Using sanitizers: ${SANITIZERS}")
  93. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=${SANITIZERS}")
  94. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=${SANITIZERS}")
  95. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=${SANITIZERS}")
  96. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=${SANITIZERS}")
  97. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=${SANITIZERS}")
  98. endif()
  99. set(SOURCES_C
  100. srtp/srtp.c
  101. )
  102. set(CIPHERS_SOURCES_C
  103. crypto/cipher/cipher.c
  104. crypto/cipher/cipher_test_cases.c
  105. crypto/cipher/cipher_test_cases.h
  106. crypto/cipher/null_cipher.c
  107. )
  108. if(ENABLE_OPENSSL)
  109. list(APPEND CIPHERS_SOURCES_C
  110. crypto/cipher/aes_icm_ossl.c
  111. crypto/cipher/aes_gcm_ossl.c
  112. )
  113. elseif(ENABLE_MBEDTLS)
  114. list(APPEND CIPHERS_SOURCES_C
  115. crypto/cipher/aes_icm_mbedtls.c
  116. crypto/cipher/aes_gcm_mbedtls.c
  117. )
  118. elseif(ENABLE_NSS)
  119. list(APPEND CIPHERS_SOURCES_C
  120. crypto/cipher/aes_icm_nss.c
  121. crypto/cipher/aes_gcm_nss.c
  122. )
  123. else()
  124. list(APPEND CIPHERS_SOURCES_C
  125. crypto/cipher/aes.c
  126. crypto/cipher/aes_icm.c
  127. )
  128. endif()
  129. set(HASHES_SOURCES_C
  130. crypto/hash/auth.c
  131. crypto/hash/auth_test_cases.c
  132. crypto/hash/auth_test_cases.h
  133. crypto/hash/null_auth.c
  134. )
  135. if(ENABLE_OPENSSL)
  136. list(APPEND HASHES_SOURCES_C
  137. crypto/hash/hmac_ossl.c
  138. )
  139. elseif(ENABLE_MBEDTLS)
  140. list(APPEND HASHES_SOURCES_C
  141. crypto/hash/hmac_mbedtls.c
  142. )
  143. elseif(ENABLE_NSS)
  144. list(APPEND HASHES_SOURCES_C
  145. crypto/hash/hmac_nss.c
  146. )
  147. else()
  148. list(APPEND HASHES_SOURCES_C
  149. crypto/hash/hmac.c
  150. crypto/hash/sha1.c
  151. )
  152. endif()
  153. set(KERNEL_SOURCES_C
  154. crypto/kernel/alloc.c
  155. crypto/kernel/crypto_kernel.c
  156. crypto/kernel/err.c
  157. crypto/kernel/key.c
  158. )
  159. set(MATH_SOURCES_C
  160. crypto/math/datatypes.c
  161. )
  162. set(REPLAY_SOURCES_C
  163. crypto/replay/rdb.c
  164. crypto/replay/rdbx.c
  165. )
  166. set(SOURCES_H
  167. crypto/include/aes.h
  168. crypto/include/aes_icm.h
  169. crypto/include/alloc.h
  170. crypto/include/auth.h
  171. crypto/include/cipher.h
  172. crypto/include/cipher_types.h
  173. crypto/include/crypto_kernel.h
  174. crypto/include/crypto_types.h
  175. crypto/include/datatypes.h
  176. crypto/include/err.h
  177. crypto/include/hmac.h
  178. crypto/include/integers.h
  179. crypto/include/key.h
  180. crypto/include/null_auth.h
  181. crypto/include/null_cipher.h
  182. crypto/include/rdb.h
  183. crypto/include/rdbx.h
  184. crypto/include/sha1.h
  185. include/srtp.h
  186. include/srtp_priv.h
  187. ${CONFIG_FILE_DIR}/config.h
  188. )
  189. if(BUILD_SHARED_LIBS AND WIN32)
  190. list(APPEND SOURCES_C
  191. srtp.def
  192. )
  193. endif()
  194. source_group("src" FILES ${SOURCES_C})
  195. source_group("src\\Ciphers" FILES ${CIPHERS_SOURCES_C})
  196. source_group("src\\Hashes" FILES ${HASHES_SOURCES_C})
  197. source_group("src\\Kernel" FILES ${KERNEL_SOURCES_C})
  198. source_group("src\\Math" FILES ${MATH_SOURCES_C})
  199. source_group("src\\Replay" FILES ${REPLAY_SOURCES_C})
  200. source_group("include" FILES ${SOURCES_H})
  201. add_library(srtp2
  202. ${SOURCES_C}
  203. ${CIPHERS_SOURCES_C}
  204. ${HASHES_SOURCES_C}
  205. ${KERNEL_SOURCES_C}
  206. ${MATH_SOURCES_C}
  207. ${REPLAY_SOURCES_C}
  208. ${SOURCES_H}
  209. )
  210. set_target_properties(srtp2 PROPERTIES VERSION ${CMAKE_PROJECT_VERSION})
  211. target_include_directories(srtp2 PUBLIC crypto/include include)
  212. if(ENABLE_OPENSSL)
  213. target_include_directories(srtp2 PRIVATE ${OPENSSL_INCLUDE_DIR})
  214. target_link_libraries(srtp2 OpenSSL::Crypto)
  215. elseif(ENABLE_MBEDTLS)
  216. target_include_directories(srtp2 PRIVATE ${MBEDTLS_INCLUDE_DIRS})
  217. target_link_libraries(srtp2 ${MBEDTLS_LIBRARIES})
  218. elseif(ENABLE_NSS)
  219. target_include_directories(srtp2 PRIVATE ${NSS_INCLUDE_DIRS})
  220. target_link_libraries(srtp2 ${NSS_LIBRARIES})
  221. endif()
  222. if(WIN32)
  223. target_link_libraries(srtp2 ws2_32)
  224. target_compile_definitions(srtp2 PUBLIC _CRT_SECURE_NO_WARNINGS)
  225. endif()
  226. install(TARGETS srtp2 DESTINATION lib)
  227. install(FILES include/srtp.h crypto/include/auth.h
  228. crypto/include/cipher.h
  229. crypto/include/crypto_types.h
  230. DESTINATION include/srtp2)
  231. if(TEST_APPS)
  232. enable_testing()
  233. if(NOT (BUILD_SHARED_LIBS AND WIN32))
  234. if(NOT USE_EXTERNAL_CRYPTO)
  235. add_executable(aes_calc crypto/test/aes_calc.c test/getopt_s.c test/util.c)
  236. target_include_directories(aes_calc PRIVATE test)
  237. target_link_libraries(aes_calc srtp2)
  238. add_test(aes_calc_128 aes_calc 000102030405060708090a0b0c0d0e0f
  239. 00112233445566778899aabbccddeeff
  240. 69c4e0d86a7b0430d8cdb78070b4c55a)
  241. add_test(aes_calc_256 aes_calc 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
  242. 00112233445566778899aabbccddeeff
  243. 8ea2b7ca516745bfeafc49904b496089)
  244. add_executable(sha1_driver crypto/test/sha1_driver.c test/util.c)
  245. target_include_directories(sha1_driver PRIVATE test)
  246. target_link_libraries(sha1_driver srtp2)
  247. add_test(sha1_driver sha1_driver -v)
  248. endif()
  249. add_executable(datatypes_driver crypto/test/datatypes_driver.c test/util.c)
  250. target_include_directories(datatypes_driver PRIVATE test)
  251. target_link_libraries(datatypes_driver srtp2)
  252. add_test(datatypes_driver datatypes_driver -v)
  253. add_executable(cipher_driver crypto/test/cipher_driver.c test/getopt_s.c)
  254. target_include_directories(cipher_driver PRIVATE test)
  255. target_link_libraries(cipher_driver srtp2)
  256. add_test(cipher_driver cipher_driver -v)
  257. add_executable(kernel_driver crypto/test/kernel_driver.c test/getopt_s.c)
  258. target_include_directories(kernel_driver PRIVATE test)
  259. target_link_libraries(kernel_driver srtp2)
  260. add_test(kernel_driver kernel_driver -v)
  261. add_executable(rdbx_driver test/rdbx_driver.c test/getopt_s.c test/ut_sim.c)
  262. target_include_directories(rdbx_driver PRIVATE test)
  263. target_link_libraries(rdbx_driver srtp2)
  264. add_test(rdbx_driver rdbx_driver -v)
  265. add_executable(replay_driver test/replay_driver.c test/ut_sim.c)
  266. target_include_directories(replay_driver PRIVATE test)
  267. target_link_libraries(replay_driver srtp2)
  268. add_test(replay_driver replay_driver -v)
  269. add_executable(roc_driver test/roc_driver.c test/ut_sim.c)
  270. target_include_directories(roc_driver PRIVATE test)
  271. target_link_libraries(roc_driver srtp2)
  272. add_test(roc_driver roc_driver -v)
  273. endif()
  274. add_executable(srtp_driver test/srtp_driver.c
  275. test/util.c test/getopt_s.c)
  276. target_link_libraries(srtp_driver srtp2)
  277. add_test(srtp_driver srtp_driver -v)
  278. if(NOT (BUILD_SHARED_LIBS AND WIN32))
  279. add_executable(test_srtp test/test_srtp.c)
  280. if(ENABLE_OPENSSL)
  281. target_include_directories(test_srtp PRIVATE ${OPENSSL_INCLUDE_DIR})
  282. elseif(ENABLE_MBEDTLS)
  283. target_include_directories(test_srtp PRIVATE ${MBEDTLS_INCLUDE_DIRS})
  284. elseif(ENABLE_NSS)
  285. target_include_directories(test_srtp PRIVATE ${NSS_INCLUDE_DIRS})
  286. endif()
  287. target_link_libraries(test_srtp srtp2)
  288. add_test(test_srtp test_srtp)
  289. endif()
  290. find_program(BASH_PROGRAM bash)
  291. if(BASH_PROGRAM AND NOT WIN32)
  292. add_executable(rtpw test/rtpw.c test/rtp.c test/util.c test/getopt_s.c)
  293. target_link_libraries(rtpw srtp2)
  294. add_test(NAME rtpw_test
  295. COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test/rtpw_test.sh -w ${CMAKE_CURRENT_SOURCE_DIR}/test/words.txt
  296. WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
  297. if(GCM)
  298. add_test(NAME rtpw_test_gcm
  299. COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test/rtpw_test_gcm.sh -w ${CMAKE_CURRENT_SOURCE_DIR}/test/words.txt
  300. WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
  301. endif()
  302. endif()
  303. endif()