You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

400 lines
14 KiB

  1. cmake_minimum_required(VERSION 3.13)
  2. project(cryptocore VERSION 0.0.1 LANGUAGES C)
  3. include(FetchContent)
  4. include(ExternalProject)
  5. set(CMAKE_CXX_STANDARD 20)
  6. set(CMAKE_C_STANDARD 99)
  7. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  8. enable_language(C)
  9. enable_language(CXX)
  10. enable_language(ASM)
  11. set_property(GLOBAL PROPERTY obj_libs "")
  12. # Build with address sanitizer
  13. if(ADDRSAN)
  14. string(APPEND EXTRA_C_CXX_FLAGS " -fsanitize=undefined,address,leak -fno-omit-frame-pointer")
  15. set(EXTRA_LDFLAGS " -fsanitize=undefined,address,leak")
  16. endif()
  17. if(MEMSAN)
  18. # PQC_MEMSAN enables usage of some internals from clang
  19. if (NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
  20. message(FATAL_ERROR "Must use clang if compiled with memory sanitizer.")
  21. endif()
  22. if(ADDRSAN)
  23. message(FATAL_ERROR "Can't use MSAN and ASAN")
  24. endif()
  25. include(.cmake/libstd-memory_sanitizer.mk)
  26. # LLVM project location
  27. set(LLVM_PRJ ${CMAKE_CURRENT_BINARY_DIR}/3rd/llvm-project)
  28. set(LLVM_PRJ_LIB ${LLVM_PRJ}/usr/local/lib)
  29. set(LLVM_PRJ_INC ${LLVM_PRJ}/usr/local/include)
  30. # Add memory sanitizer instrumented libraries
  31. set(CMAKE_ARGS_MEMCHECK_LIB "-stdlib=libc++ -L${LLVM_PRJ_LIB}")
  32. set(CMAKE_ARGS_MEMCHECK_INC "-isystem -I${LLVM_PRJ_INC} -I${LLVM_PRJ_INC}/c++/v1")
  33. set(CMAKE_ARGS_MEMCHECK_FLAGS "-fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -Wno-unused-command-line-argument -fno-optimize-sibling-calls")
  34. # Enablin "keep-going" flag alows two things:
  35. # 1. Enables CT_EXPECT_UMR()/CT_REQUIRE_UMR() in tests. For some reason MSan will halt
  36. # on error even if it expects UMR. And hence, CT can't be tested. This is probably a bug.
  37. # 2. reports all the errors from the run, not only the first one (don't fail-fast)
  38. string(APPEND CMAKE_ARGS_MEMCHECK_FLAGS " -mllvm -msan-keep-going=1")
  39. set(EXTRA_C_CXX_FLAGS "${CMAKE_ARGS_MEMCHECK_FLAGS} ${CMAKE_ARGS_MEMCHECK_LIB} ${CMAKE_ARGS_MEMCHECK_INC} -DPQC_MEMSAN_BUILD")
  40. set(CXXLIBS_FOR_MEMORY_SANITIZER cxx cxxabi)
  41. endif()
  42. # Contant time memory checks with CTGRIND (requires clang and -DMEMSAN)
  43. if(CTSAN)
  44. if (NOT MEMSAN)
  45. message(FATAL_ERROR "Constant time sanitizer requires -DMEMSAN")
  46. endif()
  47. if (NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
  48. message(FATAL_ERROR "Constant time sanitizer requires Clang")
  49. endif()
  50. string(APPEND EXTRA_C_CXX_FLAGS " -DPQC_USE_CTSANITIZER")
  51. endif()
  52. # Contant time memory checks with CTGRIND (requires valgrind)
  53. if (CTGRIND)
  54. if (MEMSAN OR CTSAN)
  55. message(FATAL_ERROR "Can't use memory sanitizer (MEMSAN) and CTGRIND")
  56. endif()
  57. string(APPEND EXTRA_C_CXX_FLAGS " -DPQC_USE_CTGRIND")
  58. endif()
  59. set(CMAKE_VERBOSE_MAKEFILE ON)
  60. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "~/.cmake/Modules")
  61. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "3rd/cmake-modules")
  62. set(CMAKE_CXX_STANDARD 11)
  63. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  64. string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
  65. if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
  66. set(ARCH "ARCH_x86_64")
  67. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
  68. set(ARCH "ARCH_x86_64")
  69. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
  70. set(ARCH "ARCH_x86_64")
  71. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
  72. set(ARCH "ARCH_x86")
  73. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
  74. set(ARCH "ARCH_x86")
  75. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
  76. set(ARCH "ARCH_x86")
  77. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
  78. set(ARCH "ARCH_aarch64")
  79. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
  80. set(ARCH "ARCH_aarch64")
  81. else()
  82. message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
  83. endif()
  84. # Arch settings
  85. if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  86. set(MACOSX TRUE)
  87. endif()
  88. if (${PQC_NO_ASM})
  89. set(NO_ASM TRUE)
  90. endif()
  91. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  92. # Additional flags only useful when compiling with clang
  93. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wconditional-uninitialized -Wno-missing-variable-declarations -Wno-unused-command-line-argument")
  94. endif()
  95. if (MACOSX)
  96. set(CMAKE_C_COMPILER /usr/bin/cc CACHE PATH "" FORCE)
  97. set(CMAKE_CXX_COMPILER /usr/bin/c++ CACHE PATH "" FORCE)
  98. endif()
  99. # Global configuration
  100. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wall")
  101. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Werror")
  102. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wextra")
  103. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wshadow")
  104. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wno-variadic-macros")
  105. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wunused-result")
  106. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wno-unused-command-line-argument")
  107. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wno-undef")
  108. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wno-ignored-qualifiers")
  109. if(CMAKE_COMPILER_IS_GNUCC AND CMAKE_C_COMPILER_VERSION VERSION_GREATER 11.0)
  110. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wno-stringop-overread \
  111. -Wno-stringop-overflow \
  112. -Wno-array-parameter")
  113. endif()
  114. include(.cmake/common.mk)
  115. # Control Debug/Release mode
  116. if(CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
  117. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -g3 -O0 -Wno-unused")
  118. endif()
  119. # Set CPU architecture
  120. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -D${ARCH}")
  121. # Build for haswell if on x86_64
  122. if(${ARCH} STREQUAL "ARCH_x86_64")
  123. add_compile_options("-march=haswell")
  124. endif()
  125. # Dependencies
  126. ExternalProject_Add(
  127. gtest_project
  128. SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/gtest
  129. GIT_REPOSITORY https://github.com/google/googletest.git
  130. GIT_TAG a3460d1aeeaa43fdf137a6adefef10ba0b59fe4b
  131. PREFIX ${CMAKE_CURRENT_BINARY_DIR}/3rd/gtest
  132. INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/3rd/gtest
  133. CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/3rd/gtest -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_CXX_FLAGS=${EXTRA_C_CXX_FLAGS} -DCMAKE_C_FLAGS=${EXTRA_C_CXX_FLAGS} -Dgtest_disable_pthreads=ON
  134. )
  135. if(MEMSAN)
  136. add_dependencies(gtest_project ${CXXLIBS_FOR_MEMORY_SANITIZER})
  137. endif()
  138. FetchContent_Declare(
  139. gbench
  140. SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/gbench
  141. GIT_REPOSITORY https://github.com/kriskwiatkowski/benchmark.git
  142. GIT_TAG hdc/release_crypto
  143. )
  144. FetchContent_Populate(gbench)
  145. FetchContent_Declare(
  146. cpu_features
  147. SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/cpu_features
  148. GIT_REPOSITORY https://github.com/kriskwiatkowski/cpu_features.git
  149. GIT_TAG 38f4324533390b09079a38b524be8b178be8e435
  150. )
  151. FetchContent_Populate(cpu_features)
  152. if(PQC_WEAK_RANDOMBYTES)
  153. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -DPQC_WEAK_RANDOMBYTES")
  154. endif()
  155. # Build CPU features
  156. set(CMAKE_C_FLAGS "${PQC_CMAKE_C_CXX_FLAGS} ${EXTRA_C_CXX_FLAGS}")
  157. set(CMAKE_CXX_FLAGS "$${PQC_CMAKE_C_CXX_FLAGS} {EXTRA_C_CXX_FLAGS}")
  158. set(BUILD_PIC ON CACHE BOOL "")
  159. add_subdirectory(3rd/cpu_features)
  160. # PQC library
  161. # Set C, CXX, and LD flags
  162. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wpedantic")
  163. set(CMAKE_C_FLAGS "${PQC_CMAKE_C_CXX_FLAGS} ${EXTRA_C_CXX_FLAGS}")
  164. set(CMAKE_CXX_FLAGS "${PQC_CMAKE_C_CXX_FLAGS} ${EXTRA_C_CXX_FLAGS}")
  165. string(APPEND LDFLAGS "${EXTRA_LDFLAGS}")
  166. include_directories(
  167. public
  168. src/common/
  169. src
  170. 3rd/cpu_features/include
  171. )
  172. # Define sources of the components
  173. add_subdirectory(src/sign/dilithium/dilithium2/clean)
  174. add_subdirectory(src/sign/dilithium/dilithium3/clean)
  175. add_subdirectory(src/sign/dilithium/dilithium5/clean)
  176. add_subdirectory(src/sign/falcon)
  177. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-simple/clean)
  178. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-simple/clean)
  179. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-robust/clean)
  180. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-simple/clean)
  181. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-simple/clean)
  182. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-simple/clean)
  183. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-robust/clean)
  184. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-robust/clean)
  185. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-robust/clean)
  186. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-robust/clean)
  187. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-robust/clean)
  188. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-simple/clean)
  189. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-simple/clean)
  190. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-robust/clean)
  191. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-simple/clean)
  192. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-simple/clean)
  193. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-simple/clean)
  194. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-robust/clean)
  195. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-robust/clean)
  196. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-simple/clean)
  197. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/clean)
  198. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-robust/clean)
  199. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-simple/clean)
  200. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-robust/clean)
  201. add_subdirectory(src/kem/kyber/kyber512/clean)
  202. add_subdirectory(src/kem/kyber/kyber768/clean)
  203. add_subdirectory(src/kem/kyber/kyber1024/clean)
  204. add_subdirectory(src/kem/hqc/hqc-rmrs-128/clean)
  205. add_subdirectory(src/kem/hqc/hqc-rmrs-192/clean)
  206. add_subdirectory(src/kem/hqc/hqc-rmrs-256/clean)
  207. add_subdirectory(src/kem/mceliece/mceliece348864/clean)
  208. add_subdirectory(src/kem/mceliece/mceliece460896/clean)
  209. add_subdirectory(src/kem/mceliece/mceliece6688128/clean)
  210. add_subdirectory(src/kem/mceliece/mceliece6960119/clean)
  211. add_subdirectory(src/kem/mceliece/mceliece8192128/clean)
  212. add_subdirectory(src/kem/mceliece/mceliece348864f/clean)
  213. add_subdirectory(src/kem/mceliece/mceliece460896f/clean)
  214. add_subdirectory(src/kem/mceliece/mceliece6688128f/clean)
  215. add_subdirectory(src/kem/mceliece/mceliece6960119f/clean)
  216. add_subdirectory(src/kem/mceliece/mceliece8192128f/clean)
  217. # Hardware optimized targets
  218. if(${ARCH} STREQUAL "ARCH_x86_64")
  219. set(COMMON_EXTRA_SRC "src/common/keccak4x/KeccakP-1600-times4-SIMD256.c")
  220. # Sign
  221. add_subdirectory(src/sign/dilithium/dilithium2/avx2)
  222. add_subdirectory(src/sign/dilithium/dilithium3/avx2)
  223. add_subdirectory(src/sign/dilithium/dilithium5/avx2)
  224. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-simple/avx2)
  225. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-robust/avx2)
  226. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-robust/avx2)
  227. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-simple/avx2)
  228. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-simple/avx2)
  229. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-robust/avx2)
  230. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-robust/avx2)
  231. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-simple/avx2)
  232. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-robust/avx2)
  233. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-simple/avx2)
  234. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-simple/avx2)
  235. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-robust/avx2)
  236. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-robust/avx2)
  237. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-simple/avx2)
  238. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-robust/avx2)
  239. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-simple/avx2)
  240. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-simple/avx2)
  241. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-simple/avx2)
  242. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-robust/avx2)
  243. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-robust/avx2)
  244. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-simple/avx2)
  245. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-robust/avx2)
  246. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-simple/avx2)
  247. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/avx2)
  248. # KEMs
  249. add_subdirectory(src/kem/kyber/kyber512/avx2)
  250. add_subdirectory(src/kem/kyber/kyber768/avx2)
  251. add_subdirectory(src/kem/kyber/kyber1024/avx2)
  252. add_subdirectory(src/kem/hqc/hqc-rmrs-128/avx2)
  253. add_subdirectory(src/kem/hqc/hqc-rmrs-192/avx2)
  254. add_subdirectory(src/kem/hqc/hqc-rmrs-256/avx2)
  255. endif()
  256. # The rest of the library
  257. add_library(
  258. common
  259. OBJECT
  260. src/common/aes.c
  261. src/common/fips202.c
  262. src/common/sp800-185.c
  263. src/common/randombytes.c
  264. src/common/sha2.c
  265. src/common/nistseedexpander.c
  266. src/common/utils.c
  267. src/capi/pqapi.c
  268. ${COMMON_EXTRA_SRC})
  269. add_library(
  270. pqc
  271. SHARED
  272. )
  273. add_library(
  274. pqc_s
  275. STATIC
  276. )
  277. get_property(OBJ_LIBS GLOBAL PROPERTY obj_libs)
  278. target_link_libraries(
  279. pqc
  280. ${OBJ_LIBS}
  281. cpu_features
  282. common
  283. )
  284. target_link_libraries(
  285. pqc_s
  286. cpu_features
  287. common
  288. ${OBJ_LIBS}
  289. )
  290. SET(UT_SRC test/ut.cpp)
  291. if(CTGRIND OR CTSAN)
  292. SET(UT_SRC ${UT_SRC} test/ct.cpp)
  293. endif()
  294. add_executable(
  295. ut
  296. ${UT_SRC}
  297. )
  298. target_link_libraries(
  299. ut
  300. gtest
  301. gtest_main
  302. pqc_s
  303. ${CXXLIBS_FOR_MEMORY_SANITIZER})
  304. ExternalProject_Get_Property(gtest_project INSTALL_DIR)
  305. target_include_directories(
  306. ut PRIVATE
  307. ${CMAKE_SOURCE_DIR}
  308. ${INSTALL_DIR}/include)
  309. target_link_directories(
  310. ut
  311. PRIVATE
  312. ${INSTALL_DIR}/lib)
  313. # github CI requires that
  314. add_dependencies(ut gtest_project)
  315. # settings below are required by benchmark library
  316. set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)
  317. # Target for benchmark - it also builds gtest library
  318. set(BENCHMARK_ENABLE_GTEST_TESTS ON CACHE BOOL "Enable testing of the benchmark library." FORCE)
  319. set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Disable benchmark tests" FORCE)
  320. set(GOOGLETEST_PATH "${CMAKE_SOURCE_DIR}/3rd/gtest" CACHE PATH "Path to the gtest sources" FORCE)
  321. #if (NOT MACOSX)
  322. # set(BENCHMARK_ENABLE_LTO ON CACHE BOOL "Enable link time optim" FORCE)
  323. #endif()
  324. set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
  325. set(BENCHMARK_ENABLE_EXCEPTIONS OFF CACHE BOOL "" FORCE)
  326. set(CMAKE_C_FLAGS "${EXTRA_C_CXX_FLAGS}")
  327. set(CMAKE_CXX_FLAGS "${EXTRA_C_CXX_FLAGS}")
  328. if (MEMSAN)
  329. set(BENCHMARK_USE_LIBCXX ON CACHE BOOL "" FORCE)
  330. # Since build requires C++20 it is safe to assume that std::regex is available.
  331. # It seems I need to force it as benchmark build doesn't work very well with libc++
  332. set(HAVE_STD_REGEX ON CACHE BOOL "OK" FORCE)
  333. endif()
  334. add_subdirectory(${CMAKE_SOURCE_DIR}/3rd/gbench)
  335. add_subdirectory(test/bench)
  336. install(TARGETS pqc pqc_s
  337. PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ GROUP_WRITE WORLD_READ WORLD_WRITE
  338. LIBRARY DESTINATION lib
  339. ARCHIVE DESTINATION lib)
  340. install(FILES
  341. ${QRS_PUBLIC_INC}
  342. DESTINATION include/pqc)