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.
 
 
 
 

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