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.
 
 
 
 

363 lines
12 KiB

  1. cmake_minimum_required(VERSION 3.13)
  2. project(cryptocore VERSION 0.0.1 LANGUAGES C)
  3. include(FetchContent)
  4. set(CMAKE_CXX_STANDARD 20)
  5. set(CMAKE_C_STANDARD 99)
  6. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  7. enable_language(C)
  8. enable_language(CXX)
  9. enable_language(ASM)
  10. # Dependencies
  11. FetchContent_Declare(
  12. gtest
  13. SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/gtest
  14. GIT_REPOSITORY https://github.com/google/googletest.git
  15. GIT_TAG a3460d1aeeaa43fdf137a6adefef10ba0b59fe4b
  16. )
  17. FetchContent_Populate(gtest)
  18. FetchContent_Declare(
  19. cpu_features
  20. SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/cpu_features
  21. GIT_REPOSITORY https://github.com/google/cpu_features.git
  22. GIT_TAG bc2846e78faeb26b8a46c17df369d4e5f1f9e2bb
  23. GIT_SHALLOW TRUE
  24. )
  25. FetchContent_Populate(cpu_features)
  26. FetchContent_Declare(
  27. gbench
  28. SOURCE_DIR ${PROJECT_SOURCE_DIR}/3rd/gbench
  29. GIT_REPOSITORY https://github.com/kriskwiatkowski/benchmark.git
  30. GIT_TAG 49862ab56b6b7c3afd87b80bd5d787ed78ce3b96
  31. GIT_SHALLOW TRUE
  32. )
  33. FetchContent_Populate(gbench)
  34. if(MEMSAN)
  35. # PQC_MEMSAN enables usage of some internals from clang
  36. if (NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
  37. message(FATAL_ERROR "Must use clang if compiled with memory sanitizer.")
  38. endif()
  39. if(ADDRSAN)
  40. message(FATAL_ERROR "Can't use MSAN and ASAN")
  41. endif()
  42. include(.cmake/libstd-memcheck.mk)
  43. #set(C_CXX_FLAGS "${C_CXX_FLAGS} -DPQC_MEMSAN=1 -fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer")
  44. endif()
  45. add_subdirectory(3rd/gtest)
  46. set(BUILD_PIC ON CACHE BOOL "")
  47. add_subdirectory(3rd/cpu_features)
  48. set(CMAKE_VERBOSE_MAKEFILE ON)
  49. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "~/.cmake/Modules")
  50. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "3rd/cmake-modules")
  51. set(CMAKE_CXX_STANDARD 11)
  52. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  53. string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
  54. if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
  55. set(ARCH "ARCH_x86_64")
  56. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
  57. set(ARCH "ARCH_x86_64")
  58. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
  59. set(ARCH "ARCH_x86_64")
  60. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
  61. set(ARCH "ARCH_x86")
  62. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
  63. set(ARCH "ARCH_x86")
  64. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
  65. set(ARCH "ARCH_x86")
  66. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
  67. set(ARCH "ARCH_aarch64")
  68. elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
  69. set(ARCH "ARCH_aarch64")
  70. else()
  71. message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
  72. endif()
  73. if(NOT CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
  74. # settings below are required by benchmark library
  75. set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)
  76. # Target for benchmark - it also builds gtest library
  77. set(BENCHMARK_ENABLE_GTEST_TESTS ON CACHE BOOL "Enable testing of the benchmark library." FORCE)
  78. set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Disable benchmark tests" FORCE)
  79. set(GOOGLETEST_PATH "${CMAKE_SOURCE_DIR}/3rd/gtest" CACHE PATH "Path to the gtest sources" FORCE)
  80. #if (NOT MACOSX)
  81. # set(BENCHMARK_ENABLE_LTO ON CACHE BOOL "Enable link time optim" FORCE)
  82. #endif()
  83. set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
  84. add_subdirectory(${CMAKE_SOURCE_DIR}/3rd/gbench)
  85. endif()
  86. # Arch settings
  87. if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  88. set(MACOSX TRUE)
  89. endif()
  90. if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  91. # Additional flags only useful when compiling with clang
  92. set(C_CXX_FLAGS
  93. "-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. -Wundef \
  108. -Wunused-result \
  109. -Wno-unused-command-line-argument")
  110. # Build with address sanitizer
  111. if(ADDRSAN)
  112. set(C_CXX_FLAGS "${C_CXX_FLAGS} -fsanitize=undefined,address,leak -fno-omit-frame-pointer")
  113. set(LDFLAGS "${LDFLAGS} -fsanitize=undefined,address,leak")
  114. endif()
  115. include(.cmake/common.mk)
  116. # Control Debug/Release mode
  117. if(CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
  118. set(C_CXX_FLAGS "${C_CXX_FLAGS} -g3 -O0 -Wno-unused")
  119. else()
  120. set(C_CXX_FLAGS "${C_CXX_FLAGS} -O3")
  121. endif()
  122. include_directories(
  123. public
  124. src/common/
  125. src
  126. 3rd/cpu_features/include
  127. )
  128. set_property(GLOBAL PROPERTY obj_libs "")
  129. # Set CPU architecture
  130. set(C_CXX_FLAGS "${C_CXX_FLAGS} -D${ARCH}")
  131. # Build for haswell if on x86_64
  132. if(${ARCH} STREQUAL "ARCH_x86_64")
  133. string(APPEND C_CXX_FLAGS " -march=haswell")
  134. endif()
  135. set(PQC_CMAKE_C_FLAGS "${C_CXX_FLAGS}")
  136. set(PQC_CMAKE_CXX_FLAGS "${C_CXX_FLAGS}")
  137. set(CMAKE_C_FLAGS ${PQC_CMAKE_C_FLAGS})
  138. set(CMAKE_CXX_FLAGS ${PQC_CMAKE_CXX_FLAGS})
  139. if(PQC_WEAK_RANDOMBYTES)
  140. string(APPEND PQC_CMAKE_C_FLAGS " -DPQC_WEAK_RANDOMBYTES")
  141. endif()
  142. # Define sources of the components
  143. add_subdirectory(src/sign/dilithium/dilithium2/clean)
  144. add_subdirectory(src/sign/dilithium/dilithium3/clean)
  145. add_subdirectory(src/sign/dilithium/dilithium5/clean)
  146. add_subdirectory(src/sign/falcon)
  147. add_subdirectory(src/sign/rainbow/rainbowV-classic/clean)
  148. add_subdirectory(src/sign/rainbow/rainbowI-classic/clean)
  149. add_subdirectory(src/sign/rainbow/rainbowIII-classic/clean)
  150. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-simple/clean)
  151. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-simple/clean)
  152. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-robust/clean)
  153. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-simple/clean)
  154. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-simple/clean)
  155. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-simple/clean)
  156. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-robust/clean)
  157. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-robust/clean)
  158. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-robust/clean)
  159. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-robust/clean)
  160. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-robust/clean)
  161. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-simple/clean)
  162. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-simple/clean)
  163. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-robust/clean)
  164. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-simple/clean)
  165. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-simple/clean)
  166. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-simple/clean)
  167. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-robust/clean)
  168. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-robust/clean)
  169. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-simple/clean)
  170. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/clean)
  171. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-robust/clean)
  172. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-simple/clean)
  173. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-robust/clean)
  174. add_subdirectory(src/kem/kyber/kyber512/clean)
  175. add_subdirectory(src/kem/kyber/kyber768/clean)
  176. add_subdirectory(src/kem/kyber/kyber1024/clean)
  177. add_subdirectory(src/kem/saber/lightsaber/clean)
  178. add_subdirectory(src/kem/saber/firesaber/clean)
  179. add_subdirectory(src/kem/saber/saber/clean)
  180. add_subdirectory(src/kem/frodo/frodokem640shake/clean)
  181. add_subdirectory(src/kem/frodo/frodokem976shake/clean)
  182. add_subdirectory(src/kem/frodo/frodokem1344shake/clean)
  183. add_subdirectory(src/kem/ntru/ntruhps4096821/clean)
  184. add_subdirectory(src/kem/ntru/ntruhps2048509/clean)
  185. add_subdirectory(src/kem/ntru/ntruhrss701/clean)
  186. add_subdirectory(src/kem/ntru/ntruhps2048677/clean)
  187. add_subdirectory(src/kem/ntru_prime/ntrulpr761/clean)
  188. add_subdirectory(src/kem/ntru_prime/ntrulpr653/clean)
  189. add_subdirectory(src/kem/ntru_prime/ntrulpr857/clean)
  190. add_subdirectory(src/kem/hqc/hqc-rmrs-128/clean)
  191. add_subdirectory(src/kem/hqc/hqc-rmrs-192/clean)
  192. add_subdirectory(src/kem/hqc/hqc-rmrs-256/clean)
  193. add_subdirectory(src/kem/sike)
  194. add_subdirectory(src/kem/mceliece/mceliece348864/clean)
  195. add_subdirectory(src/kem/mceliece/mceliece460896/clean)
  196. add_subdirectory(src/kem/mceliece/mceliece6688128/clean)
  197. add_subdirectory(src/kem/mceliece/mceliece6960119/clean)
  198. add_subdirectory(src/kem/mceliece/mceliece8192128/clean)
  199. add_subdirectory(src/kem/mceliece/mceliece348864f/clean)
  200. add_subdirectory(src/kem/mceliece/mceliece460896f/clean)
  201. add_subdirectory(src/kem/mceliece/mceliece6688128f/clean)
  202. add_subdirectory(src/kem/mceliece/mceliece6960119f/clean)
  203. add_subdirectory(src/kem/mceliece/mceliece8192128f/clean)
  204. # Hardware optimized targets
  205. if(${ARCH} STREQUAL "ARCH_x86_64")
  206. set(SRC_COMMON_AVX2
  207. src/common/keccak4x/KeccakP-1600-times4-SIMD256.c
  208. )
  209. # Sign
  210. add_subdirectory(src/sign/dilithium/dilithium2/avx2)
  211. add_subdirectory(src/sign/dilithium/dilithium3/avx2)
  212. add_subdirectory(src/sign/dilithium/dilithium5/avx2)
  213. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-simple/avx2)
  214. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-robust/avx2)
  215. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-robust/avx2)
  216. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-simple/avx2)
  217. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-simple/avx2)
  218. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-robust/avx2)
  219. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-robust/avx2)
  220. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-simple/avx2)
  221. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-robust/avx2)
  222. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-simple/avx2)
  223. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-simple/avx2)
  224. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-robust/avx2)
  225. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-robust/avx2)
  226. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-simple/avx2)
  227. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-robust/avx2)
  228. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-simple/avx2)
  229. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-simple/avx2)
  230. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-simple/avx2)
  231. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-robust/avx2)
  232. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-robust/avx2)
  233. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-simple/avx2)
  234. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-robust/avx2)
  235. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-simple/avx2)
  236. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/avx2)
  237. # KEMs
  238. add_subdirectory(src/kem/kyber/kyber512/avx2)
  239. add_subdirectory(src/kem/kyber/kyber768/avx2)
  240. add_subdirectory(src/kem/kyber/kyber1024/avx2)
  241. add_subdirectory(src/kem/saber/lightsaber/avx2)
  242. add_subdirectory(src/kem/saber/firesaber/avx2)
  243. add_subdirectory(src/kem/saber/saber/avx2)
  244. add_subdirectory(src/kem/ntru/ntruhps4096821/avx2)
  245. add_subdirectory(src/kem/ntru/ntruhps2048509/avx2)
  246. add_subdirectory(src/kem/ntru/ntruhrss701/avx2)
  247. add_subdirectory(src/kem/ntru/ntruhps2048677/avx2)
  248. add_subdirectory(src/kem/ntru_prime/ntrulpr761/avx2)
  249. add_subdirectory(src/kem/ntru_prime/ntrulpr653/avx2)
  250. add_subdirectory(src/kem/ntru_prime/ntrulpr857/avx2)
  251. add_subdirectory(src/kem/hqc/hqc-rmrs-128/avx2)
  252. add_subdirectory(src/kem/hqc/hqc-rmrs-192/avx2)
  253. add_subdirectory(src/kem/hqc/hqc-rmrs-256/avx2)
  254. endif()
  255. # The rest of the library
  256. set(SRC_COMMON_GENERIC
  257. src/common/aes.c
  258. src/common/fips202.c
  259. src/common/sp800-185.c
  260. src/common/randombytes.c
  261. src/common/sha2.c
  262. src/common/nistseedexpander.c
  263. src/capi/pqapi.c
  264. )
  265. add_library(
  266. common
  267. OBJECT
  268. ${SRC_COMMON_GENERIC}
  269. ${SRC_COMMON_AVX2}
  270. )
  271. add_library(
  272. pqc
  273. SHARED
  274. )
  275. add_library(
  276. pqc_s
  277. STATIC
  278. )
  279. get_property(OBJ_LIBS GLOBAL PROPERTY obj_libs)
  280. target_link_libraries(
  281. pqc
  282. ${OBJ_LIBS}
  283. cpu_features
  284. common
  285. )
  286. target_link_libraries(
  287. pqc_s
  288. cpu_features
  289. common
  290. ${OBJ_LIBS}
  291. )
  292. SET(UT_SRC test/ut.cpp)
  293. if(MEMSAN)
  294. SET(UT_SRC ${UT_SRC} test/ct.cpp)
  295. endif()
  296. add_executable(
  297. ut
  298. ${UT_SRC}
  299. )
  300. target_link_libraries(
  301. ut
  302. gtest
  303. gtest_main
  304. pqc_s)
  305. target_include_directories(
  306. ut PRIVATE
  307. ${CMAKE_SOURCE_DIR})
  308. if(NOT CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
  309. add_subdirectory(test/bench)
  310. endif()
  311. install(TARGETS pqc pqc_s
  312. PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ GROUP_WRITE WORLD_READ WORLD_WRITE
  313. LIBRARY DESTINATION lib
  314. ARCHIVE DESTINATION lib)
  315. install(FILES
  316. ${QRS_PUBLIC_INC}
  317. DESTINATION include/pqc)