Reference implementations of PQC
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.
 
 
 
 

401 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_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++")
  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")
  34. set(EXTRA_CXX_FLAGS "${CMAKE_ARGS_MEMCHECK_FLAGS} ${CMAKE_ARGS_MEMCHECK_LIB} ${CMAKE_ARGS_MEMCHECK_INC} -DPQC_MEMSAN_BUILD")
  35. set(CXXLIBS_FOR_MEMORY_SANITIZER cxx cxxabi)
  36. endif()
  37. set(CMAKE_VERBOSE_MAKEFILE ON)
  38. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "~/.cmake/Modules")
  39. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "3rd/cmake-modules")
  40. set(CMAKE_CXX_STANDARD 11)
  41. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  42. string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
  43. if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
  44. set(ARCH "ARCH_x86_64")
  45. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
  46. set(ARCH "ARCH_x86_64")
  47. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
  48. set(ARCH "ARCH_x86_64")
  49. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
  50. set(ARCH "ARCH_x86")
  51. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
  52. set(ARCH "ARCH_x86")
  53. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
  54. set(ARCH "ARCH_x86")
  55. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
  56. set(ARCH "ARCH_aarch64")
  57. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
  58. set(ARCH "ARCH_aarch64")
  59. else()
  60. message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
  61. endif()
  62. # Arch settings
  63. if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  64. set(MACOSX TRUE)
  65. endif()
  66. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  67. # Additional flags only useful when compiling with clang
  68. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wconditional-uninitialized -Wno-missing-variable-declarations -Wno-unused-command-line-argument")
  69. endif()
  70. if (MACOSX)
  71. set(CMAKE_C_COMPILER /usr/bin/cc CACHE PATH "" FORCE)
  72. set(CMAKE_CXX_COMPILER /usr/bin/c++ CACHE PATH "" FORCE)
  73. endif()
  74. # Global configuration
  75. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wno-ignored-qualifiers \
  76. -Wall \
  77. -Werror \
  78. -Wextra \
  79. -Wpedantic \
  80. -Wshadow \
  81. -Wno-variadic-macros \
  82. -Wunused-result \
  83. -Wno-unused-command-line-argument \
  84. -Wno-undef \
  85. ${EXTRA_CXX_FLAGS}")
  86. if(CMAKE_COMPILER_IS_GNUCC AND CMAKE_C_COMPILER_VERSION VERSION_GREATER 11.0)
  87. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -Wno-stringop-overread \
  88. -Wno-stringop-overflow \
  89. -Wno-array-parameter")
  90. endif()
  91. include(.cmake/common.mk)
  92. # Control Debug/Release mode
  93. if(CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
  94. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -g3 -O0 -Wno-unused")
  95. else()
  96. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -O3")
  97. endif()
  98. # Set CPU architecture
  99. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -D${ARCH}")
  100. # Build for haswell if on x86_64
  101. if(${ARCH} STREQUAL "ARCH_x86_64")
  102. add_compile_options("-march=haswell")
  103. endif()
  104. # Dependencies
  105. ExternalProject_Add(
  106. gtest_project
  107. SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/gtest
  108. GIT_REPOSITORY https://github.com/google/googletest.git
  109. GIT_TAG a3460d1aeeaa43fdf137a6adefef10ba0b59fe4b
  110. PREFIX ${CMAKE_CURRENT_BINARY_DIR}/3rd/gtest
  111. INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/3rd/gtest
  112. 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_CXX_FLAGS} -DCMAKE_C_FLAGS=${EXTRA_CXX_FLAGS} -Dgtest_disable_pthreads=ON
  113. )
  114. if(MEMSAN)
  115. add_dependencies(gtest_project ${CXXLIBS_FOR_MEMORY_SANITIZER})
  116. endif()
  117. FetchContent_Declare(
  118. gbench
  119. SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/gbench
  120. GIT_REPOSITORY https://github.com/kriskwiatkowski/benchmark.git
  121. GIT_TAG 49862ab56b6b7c3afd87b80bd5d787ed78ce3b96
  122. )
  123. FetchContent_Populate(gbench)
  124. FetchContent_Declare(
  125. cpu_features
  126. SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/cpu_features
  127. GIT_REPOSITORY https://github.com/kriskwiatkowski/cpu_features.git
  128. GIT_TAG 892991f352591e9bca6ec72936836650bde90802
  129. )
  130. FetchContent_Populate(cpu_features)
  131. if(PQC_WEAK_RANDOMBYTES)
  132. string(APPEND PQC_CMAKE_C_CXX_FLAGS " -DPQC_WEAK_RANDOMBYTES")
  133. endif()
  134. # Build CPU features
  135. set(CMAKE_C_FLAGS ${EXTRA_CXX_FLAGS})
  136. set(CMAKE_CXX_FLAGS ${EXTRA_CXX_FLAGS})
  137. set(BUILD_PIC ON CACHE BOOL "")
  138. add_subdirectory(3rd/cpu_features)
  139. # PQC library
  140. # Set C, CXX, and LD flags
  141. set(CMAKE_C_FLAGS "${PQC_CMAKE_C_CXX_FLAGS} ${EXTRA_CXX_FLAGS}")
  142. set(CMAKE_CXX_FLAGS "${PQC_CMAKE_C_CXX_FLAGS} ${EXTRA_CXX_FLAGS}")
  143. string(APPEND LDFLAGS "${EXTRA_LDFLAGS}")
  144. include_directories(
  145. public
  146. src/common/
  147. src
  148. 3rd/cpu_features/include
  149. )
  150. # Define sources of the components
  151. add_subdirectory(src/sign/dilithium/dilithium2/clean)
  152. add_subdirectory(src/sign/dilithium/dilithium3/clean)
  153. add_subdirectory(src/sign/dilithium/dilithium5/clean)
  154. add_subdirectory(src/sign/falcon)
  155. add_subdirectory(src/sign/rainbow/rainbowV-classic/clean)
  156. add_subdirectory(src/sign/rainbow/rainbowI-classic/clean)
  157. add_subdirectory(src/sign/rainbow/rainbowIII-classic/clean)
  158. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-simple/clean)
  159. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-simple/clean)
  160. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-robust/clean)
  161. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-simple/clean)
  162. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-simple/clean)
  163. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-simple/clean)
  164. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-robust/clean)
  165. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-robust/clean)
  166. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-robust/clean)
  167. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-robust/clean)
  168. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-robust/clean)
  169. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-simple/clean)
  170. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-simple/clean)
  171. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-robust/clean)
  172. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-simple/clean)
  173. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-simple/clean)
  174. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-simple/clean)
  175. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-robust/clean)
  176. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-robust/clean)
  177. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-simple/clean)
  178. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/clean)
  179. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-robust/clean)
  180. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-simple/clean)
  181. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-robust/clean)
  182. add_subdirectory(src/kem/kyber/kyber512/clean)
  183. add_subdirectory(src/kem/kyber/kyber768/clean)
  184. add_subdirectory(src/kem/kyber/kyber1024/clean)
  185. add_subdirectory(src/kem/saber/lightsaber/clean)
  186. add_subdirectory(src/kem/saber/firesaber/clean)
  187. add_subdirectory(src/kem/saber/saber/clean)
  188. add_subdirectory(src/kem/frodo/frodokem640shake/clean)
  189. add_subdirectory(src/kem/frodo/frodokem976shake/clean)
  190. add_subdirectory(src/kem/frodo/frodokem1344shake/clean)
  191. add_subdirectory(src/kem/ntru/ntruhps4096821/clean)
  192. add_subdirectory(src/kem/ntru/ntruhps2048509/clean)
  193. add_subdirectory(src/kem/ntru/ntruhrss701/clean)
  194. add_subdirectory(src/kem/ntru/ntruhps2048677/clean)
  195. add_subdirectory(src/kem/ntru_prime/ntrulpr761/clean)
  196. add_subdirectory(src/kem/ntru_prime/ntrulpr653/clean)
  197. add_subdirectory(src/kem/ntru_prime/ntrulpr857/clean)
  198. add_subdirectory(src/kem/hqc/hqc-rmrs-128/clean)
  199. add_subdirectory(src/kem/hqc/hqc-rmrs-192/clean)
  200. add_subdirectory(src/kem/hqc/hqc-rmrs-256/clean)
  201. add_subdirectory(src/kem/sike)
  202. add_subdirectory(src/kem/mceliece/mceliece348864/clean)
  203. add_subdirectory(src/kem/mceliece/mceliece460896/clean)
  204. add_subdirectory(src/kem/mceliece/mceliece6688128/clean)
  205. add_subdirectory(src/kem/mceliece/mceliece6960119/clean)
  206. add_subdirectory(src/kem/mceliece/mceliece8192128/clean)
  207. add_subdirectory(src/kem/mceliece/mceliece348864f/clean)
  208. add_subdirectory(src/kem/mceliece/mceliece460896f/clean)
  209. add_subdirectory(src/kem/mceliece/mceliece6688128f/clean)
  210. add_subdirectory(src/kem/mceliece/mceliece6960119f/clean)
  211. add_subdirectory(src/kem/mceliece/mceliece8192128f/clean)
  212. # Hardware optimized targets
  213. if(${ARCH} STREQUAL "ARCH_x86_64")
  214. set(COMMON_EXTRA_SRC "src/common/keccak4x/KeccakP-1600-times4-SIMD256.c")
  215. # Sign
  216. add_subdirectory(src/sign/dilithium/dilithium2/avx2)
  217. add_subdirectory(src/sign/dilithium/dilithium3/avx2)
  218. add_subdirectory(src/sign/dilithium/dilithium5/avx2)
  219. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-simple/avx2)
  220. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-robust/avx2)
  221. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-robust/avx2)
  222. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-simple/avx2)
  223. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-simple/avx2)
  224. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-robust/avx2)
  225. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-robust/avx2)
  226. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-simple/avx2)
  227. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-robust/avx2)
  228. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-simple/avx2)
  229. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-simple/avx2)
  230. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-robust/avx2)
  231. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-robust/avx2)
  232. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-simple/avx2)
  233. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-robust/avx2)
  234. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-simple/avx2)
  235. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-simple/avx2)
  236. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-simple/avx2)
  237. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-robust/avx2)
  238. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-robust/avx2)
  239. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-simple/avx2)
  240. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-robust/avx2)
  241. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-simple/avx2)
  242. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/avx2)
  243. # KEMs
  244. add_subdirectory(src/kem/kyber/kyber512/avx2)
  245. add_subdirectory(src/kem/kyber/kyber768/avx2)
  246. add_subdirectory(src/kem/kyber/kyber1024/avx2)
  247. add_subdirectory(src/kem/saber/lightsaber/avx2)
  248. add_subdirectory(src/kem/saber/firesaber/avx2)
  249. add_subdirectory(src/kem/saber/saber/avx2)
  250. add_subdirectory(src/kem/ntru/ntruhps4096821/avx2)
  251. add_subdirectory(src/kem/ntru/ntruhps2048509/avx2)
  252. add_subdirectory(src/kem/ntru/ntruhrss701/avx2)
  253. add_subdirectory(src/kem/ntru/ntruhps2048677/avx2)
  254. add_subdirectory(src/kem/ntru_prime/ntrulpr761/avx2)
  255. add_subdirectory(src/kem/ntru_prime/ntrulpr653/avx2)
  256. add_subdirectory(src/kem/ntru_prime/ntrulpr857/avx2)
  257. add_subdirectory(src/kem/hqc/hqc-rmrs-128/avx2)
  258. add_subdirectory(src/kem/hqc/hqc-rmrs-192/avx2)
  259. add_subdirectory(src/kem/hqc/hqc-rmrs-256/avx2)
  260. endif()
  261. # The rest of the library
  262. add_library(
  263. common
  264. OBJECT
  265. src/common/aes.c
  266. src/common/fips202.c
  267. src/common/sp800-185.c
  268. src/common/randombytes.c
  269. src/common/sha2.c
  270. src/common/nistseedexpander.c
  271. src/capi/pqapi.c
  272. ${COMMON_EXTRA_SRC})
  273. add_library(
  274. pqc
  275. SHARED
  276. )
  277. add_library(
  278. pqc_s
  279. STATIC
  280. )
  281. get_property(OBJ_LIBS GLOBAL PROPERTY obj_libs)
  282. target_link_libraries(
  283. pqc
  284. ${OBJ_LIBS}
  285. cpu_features
  286. common
  287. )
  288. target_link_libraries(
  289. pqc_s
  290. cpu_features
  291. common
  292. ${OBJ_LIBS}
  293. )
  294. SET(UT_SRC test/ut.cpp)
  295. if(MEMSAN)
  296. SET(UT_SRC ${UT_SRC} test/ct.cpp)
  297. endif()
  298. add_executable(
  299. ut
  300. ${UT_SRC}
  301. )
  302. target_link_libraries(
  303. ut
  304. gtest
  305. gtest_main
  306. pqc_s
  307. ${CXXLIBS_FOR_MEMORY_SANITIZER})
  308. ExternalProject_Get_Property(gtest_project INSTALL_DIR)
  309. target_include_directories(
  310. ut PRIVATE
  311. ${CMAKE_SOURCE_DIR}
  312. ${INSTALL_DIR}/include)
  313. target_link_directories(
  314. ut
  315. PRIVATE
  316. ${INSTALL_DIR}/lib)
  317. # github CI requires that
  318. add_dependencies(ut gtest_project)
  319. if(NOT CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
  320. # settings below are required by benchmark library
  321. set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)
  322. # Target for benchmark - it also builds gtest library
  323. set(BENCHMARK_ENABLE_GTEST_TESTS ON CACHE BOOL "Enable testing of the benchmark library." FORCE)
  324. set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Disable benchmark tests" FORCE)
  325. set(GOOGLETEST_PATH "${CMAKE_SOURCE_DIR}/3rd/gtest" CACHE PATH "Path to the gtest sources" FORCE)
  326. #if (NOT MACOSX)
  327. # set(BENCHMARK_ENABLE_LTO ON CACHE BOOL "Enable link time optim" FORCE)
  328. #endif()
  329. set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
  330. set(BENCHMARK_ENABLE_EXCEPTIONS OFF CACHE BOOL "" FORCE)
  331. set(CMAKE_C_FLAGS "${EXTRA_CXX_FLAGS}")
  332. set(CMAKE_CXX_FLAGS "${EXTRA_CXX_FLAGS}")
  333. if (MEMSAN)
  334. set(BENCHMARK_USE_LIBCXX ON CACHE BOOL "" FORCE)
  335. # Since build requires C++20 it is safe to assume that std::regex is available.
  336. # It seems I need to force it as benchmark build doesn't work very well with libc++
  337. set(HAVE_STD_REGEX ON CACHE BOOL "OK" FORCE)
  338. endif()
  339. add_subdirectory(${CMAKE_SOURCE_DIR}/3rd/gbench)
  340. add_subdirectory(test/bench)
  341. endif()
  342. install(TARGETS pqc pqc_s
  343. PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ GROUP_WRITE WORLD_READ WORLD_WRITE
  344. LIBRARY DESTINATION lib
  345. ARCHIVE DESTINATION lib)
  346. install(FILES
  347. ${QRS_PUBLIC_INC}
  348. DESTINATION include/pqc)