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.
 
 
 
 

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