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.
 
 
 
 

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