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.
 
 
 
 

394 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-memcheck.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++ -L${LLVM_PRJ_LIB} -lc++abi -Wl,-rpath,${LLVM_PRJ_LIB}")
  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}")
  29. endif()
  30. # Dependencies
  31. ExternalProject_Add(
  32. gtest_project
  33. SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/gtest
  34. GIT_REPOSITORY https://github.com/google/googletest.git
  35. GIT_TAG a3460d1aeeaa43fdf137a6adefef10ba0b59fe4b
  36. PREFIX ${CMAKE_CURRENT_BINARY_DIR}/3rd/gtest
  37. INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/3rd/gtest
  38. 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
  39. )
  40. FetchContent_Declare(
  41. gbench
  42. SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/gbench
  43. GIT_REPOSITORY https://github.com/kriskwiatkowski/benchmark.git
  44. GIT_TAG 49862ab56b6b7c3afd87b80bd5d787ed78ce3b96
  45. GIT_SHALLOW TRUE
  46. )
  47. FetchContent_Populate(gbench)
  48. FetchContent_Declare(
  49. cpu_features
  50. SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/cpu_features
  51. GIT_REPOSITORY https://github.com/google/cpu_features.git
  52. GIT_TAG bc2846e78faeb26b8a46c17df369d4e5f1f9e2bb
  53. GIT_SHALLOW TRUE
  54. )
  55. FetchContent_Populate(cpu_features)
  56. set(BUILD_PIC ON CACHE BOOL "")
  57. add_subdirectory(3rd/cpu_features)
  58. set(CMAKE_VERBOSE_MAKEFILE ON)
  59. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "~/.cmake/Modules")
  60. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "3rd/cmake-modules")
  61. set(CMAKE_CXX_STANDARD 11)
  62. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  63. string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
  64. if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
  65. set(ARCH "ARCH_x86_64")
  66. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
  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 "x86")
  71. set(ARCH "ARCH_x86")
  72. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
  73. set(ARCH "ARCH_x86")
  74. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
  75. set(ARCH "ARCH_x86")
  76. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
  77. set(ARCH "ARCH_aarch64")
  78. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
  79. set(ARCH "ARCH_aarch64")
  80. else()
  81. message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
  82. endif()
  83. # Arch settings
  84. if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  85. set(MACOSX TRUE)
  86. endif()
  87. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  88. # Additional flags only useful when compiling with clang
  89. string(APPEND C_CXX_FLAGS " -Wconditional-uninitialized -Wno-missing-variable-declarations -Wno-unused-command-line-argument")
  90. endif()
  91. if (MACOSX)
  92. set(CMAKE_C_COMPILER /usr/bin/cc CACHE PATH "" FORCE)
  93. set(CMAKE_CXX_COMPILER /usr/bin/c++ CACHE PATH "" FORCE)
  94. endif()
  95. # Global configuration
  96. set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-ignored-qualifiers \
  97. -Wall \
  98. -Werror \
  99. -Wextra \
  100. -Wpedantic \
  101. -Wshadow \
  102. -Wno-variadic-macros \
  103. -Wunused-result \
  104. -Wno-unused-command-line-argument \
  105. -Wno-undef \
  106. ${EXTRA_CXX_FLAGS}")
  107. if(CMAKE_COMPILER_IS_GNUCC AND CMAKE_C_COMPILER_VERSION VERSION_GREATER 11.0)
  108. set(C_CXX_FLAGS "${C_CXX_FLAGS} \
  109. -Wno-stringop-overread \
  110. -Wno-stringop-overflow \
  111. -Wno-array-parameter")
  112. endif()
  113. # Build with address sanitizer
  114. if(ADDRSAN)
  115. set(C_CXX_FLAGS "${C_CXX_FLAGS} -fsanitize=undefined,address,leak -fno-omit-frame-pointer")
  116. set(LDFLAGS "${LDFLAGS} -fsanitize=undefined,address,leak")
  117. endif()
  118. include(.cmake/common.mk)
  119. # Control Debug/Release mode
  120. if(CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
  121. set(C_CXX_FLAGS "${C_CXX_FLAGS} -g3 -O0 -Wno-unused")
  122. else()
  123. set(C_CXX_FLAGS "${C_CXX_FLAGS} -O3")
  124. endif()
  125. include_directories(
  126. public
  127. src/common/
  128. src
  129. 3rd/cpu_features/include
  130. )
  131. set_property(GLOBAL PROPERTY obj_libs "")
  132. # Set CPU architecture
  133. set(C_CXX_FLAGS "${C_CXX_FLAGS} -D${ARCH}")
  134. # Build for haswell if on x86_64
  135. if(${ARCH} STREQUAL "ARCH_x86_64")
  136. string(APPEND C_CXX_FLAGS " -march=haswell")
  137. endif()
  138. set(PQC_CMAKE_C_FLAGS "${PQC_CMAKE_C_FLAGS} ${C_CXX_FLAGS}")
  139. set(PQC_CMAKE_CXX_FLAGS "${PQC_CMAKE_CXX_FLAGS} ${C_CXX_FLAGS}")
  140. set(CMAKE_C_FLAGS ${PQC_CMAKE_C_FLAGS})
  141. set(CMAKE_CXX_FLAGS ${PQC_CMAKE_CXX_FLAGS})
  142. if(PQC_WEAK_RANDOMBYTES)
  143. string(APPEND PQC_CMAKE_C_FLAGS " -DPQC_WEAK_RANDOMBYTES")
  144. endif()
  145. # Define sources of the components
  146. add_subdirectory(src/sign/dilithium/dilithium2/clean)
  147. add_subdirectory(src/sign/dilithium/dilithium3/clean)
  148. add_subdirectory(src/sign/dilithium/dilithium5/clean)
  149. add_subdirectory(src/sign/falcon)
  150. add_subdirectory(src/sign/rainbow/rainbowV-classic/clean)
  151. add_subdirectory(src/sign/rainbow/rainbowI-classic/clean)
  152. add_subdirectory(src/sign/rainbow/rainbowIII-classic/clean)
  153. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-simple/clean)
  154. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-simple/clean)
  155. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-robust/clean)
  156. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-simple/clean)
  157. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-simple/clean)
  158. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-simple/clean)
  159. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-robust/clean)
  160. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-robust/clean)
  161. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-robust/clean)
  162. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-robust/clean)
  163. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-robust/clean)
  164. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-simple/clean)
  165. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-simple/clean)
  166. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-robust/clean)
  167. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-simple/clean)
  168. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-simple/clean)
  169. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-simple/clean)
  170. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-robust/clean)
  171. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-robust/clean)
  172. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-simple/clean)
  173. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/clean)
  174. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-robust/clean)
  175. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-simple/clean)
  176. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-robust/clean)
  177. add_subdirectory(src/kem/kyber/kyber512/clean)
  178. add_subdirectory(src/kem/kyber/kyber768/clean)
  179. add_subdirectory(src/kem/kyber/kyber1024/clean)
  180. add_subdirectory(src/kem/saber/lightsaber/clean)
  181. add_subdirectory(src/kem/saber/firesaber/clean)
  182. add_subdirectory(src/kem/saber/saber/clean)
  183. add_subdirectory(src/kem/frodo/frodokem640shake/clean)
  184. add_subdirectory(src/kem/frodo/frodokem976shake/clean)
  185. add_subdirectory(src/kem/frodo/frodokem1344shake/clean)
  186. add_subdirectory(src/kem/ntru/ntruhps4096821/clean)
  187. add_subdirectory(src/kem/ntru/ntruhps2048509/clean)
  188. add_subdirectory(src/kem/ntru/ntruhrss701/clean)
  189. add_subdirectory(src/kem/ntru/ntruhps2048677/clean)
  190. add_subdirectory(src/kem/ntru_prime/ntrulpr761/clean)
  191. add_subdirectory(src/kem/ntru_prime/ntrulpr653/clean)
  192. add_subdirectory(src/kem/ntru_prime/ntrulpr857/clean)
  193. add_subdirectory(src/kem/hqc/hqc-rmrs-128/clean)
  194. add_subdirectory(src/kem/hqc/hqc-rmrs-192/clean)
  195. add_subdirectory(src/kem/hqc/hqc-rmrs-256/clean)
  196. add_subdirectory(src/kem/sike)
  197. add_subdirectory(src/kem/mceliece/mceliece348864/clean)
  198. add_subdirectory(src/kem/mceliece/mceliece460896/clean)
  199. add_subdirectory(src/kem/mceliece/mceliece6688128/clean)
  200. add_subdirectory(src/kem/mceliece/mceliece6960119/clean)
  201. add_subdirectory(src/kem/mceliece/mceliece8192128/clean)
  202. add_subdirectory(src/kem/mceliece/mceliece348864f/clean)
  203. add_subdirectory(src/kem/mceliece/mceliece460896f/clean)
  204. add_subdirectory(src/kem/mceliece/mceliece6688128f/clean)
  205. add_subdirectory(src/kem/mceliece/mceliece6960119f/clean)
  206. add_subdirectory(src/kem/mceliece/mceliece8192128f/clean)
  207. # Hardware optimized targets
  208. if(${ARCH} STREQUAL "ARCH_x86_64")
  209. set(SRC_COMMON_AVX2
  210. src/common/keccak4x/KeccakP-1600-times4-SIMD256.c
  211. )
  212. # Sign
  213. add_subdirectory(src/sign/dilithium/dilithium2/avx2)
  214. add_subdirectory(src/sign/dilithium/dilithium3/avx2)
  215. add_subdirectory(src/sign/dilithium/dilithium5/avx2)
  216. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-simple/avx2)
  217. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-robust/avx2)
  218. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-robust/avx2)
  219. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-simple/avx2)
  220. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-simple/avx2)
  221. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-robust/avx2)
  222. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-robust/avx2)
  223. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-simple/avx2)
  224. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-robust/avx2)
  225. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-simple/avx2)
  226. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-simple/avx2)
  227. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-robust/avx2)
  228. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-robust/avx2)
  229. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-simple/avx2)
  230. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-robust/avx2)
  231. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-simple/avx2)
  232. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-simple/avx2)
  233. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-simple/avx2)
  234. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-robust/avx2)
  235. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-robust/avx2)
  236. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-simple/avx2)
  237. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-robust/avx2)
  238. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-simple/avx2)
  239. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/avx2)
  240. # KEMs
  241. add_subdirectory(src/kem/kyber/kyber512/avx2)
  242. add_subdirectory(src/kem/kyber/kyber768/avx2)
  243. add_subdirectory(src/kem/kyber/kyber1024/avx2)
  244. add_subdirectory(src/kem/saber/lightsaber/avx2)
  245. add_subdirectory(src/kem/saber/firesaber/avx2)
  246. add_subdirectory(src/kem/saber/saber/avx2)
  247. add_subdirectory(src/kem/ntru/ntruhps4096821/avx2)
  248. add_subdirectory(src/kem/ntru/ntruhps2048509/avx2)
  249. add_subdirectory(src/kem/ntru/ntruhrss701/avx2)
  250. add_subdirectory(src/kem/ntru/ntruhps2048677/avx2)
  251. add_subdirectory(src/kem/ntru_prime/ntrulpr761/avx2)
  252. add_subdirectory(src/kem/ntru_prime/ntrulpr653/avx2)
  253. add_subdirectory(src/kem/ntru_prime/ntrulpr857/avx2)
  254. add_subdirectory(src/kem/hqc/hqc-rmrs-128/avx2)
  255. add_subdirectory(src/kem/hqc/hqc-rmrs-192/avx2)
  256. add_subdirectory(src/kem/hqc/hqc-rmrs-256/avx2)
  257. endif()
  258. # The rest of the library
  259. set(SRC_COMMON_GENERIC
  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/capi/pqapi.c
  267. )
  268. add_library(
  269. common
  270. OBJECT
  271. ${SRC_COMMON_GENERIC}
  272. ${SRC_COMMON_AVX2}
  273. )
  274. add_library(
  275. pqc
  276. SHARED
  277. )
  278. add_library(
  279. pqc_s
  280. STATIC
  281. )
  282. get_property(OBJ_LIBS GLOBAL PROPERTY obj_libs)
  283. target_link_libraries(
  284. pqc
  285. ${OBJ_LIBS}
  286. cpu_features
  287. common
  288. )
  289. target_link_libraries(
  290. pqc_s
  291. cpu_features
  292. common
  293. ${OBJ_LIBS}
  294. )
  295. SET(UT_SRC test/ut.cpp)
  296. if(MEMSAN)
  297. SET(UT_SRC ${UT_SRC} test/ct.cpp)
  298. endif()
  299. add_executable(
  300. ut
  301. ${UT_SRC}
  302. )
  303. target_link_libraries(
  304. ut
  305. gtest
  306. gtest_main
  307. pqc_s)
  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(CMAKE_C_FLAGS "${C_CXX_FLAGS} ${EXTRA_CXX_FLAGS}")
  331. set(CMAKE_CXX_FLAGS "${C_CXX_FLAGS} ${EXTRA_CXX_FLAGS}")
  332. add_subdirectory(${CMAKE_SOURCE_DIR}/3rd/gbench)
  333. add_subdirectory(test/bench)
  334. endif()
  335. install(TARGETS pqc pqc_s
  336. PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ GROUP_WRITE WORLD_READ WORLD_WRITE
  337. LIBRARY DESTINATION lib
  338. ARCHIVE DESTINATION lib)
  339. install(FILES
  340. ${QRS_PUBLIC_INC}
  341. DESTINATION include/pqc)