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.
 
 
 
 

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