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.
 
 
 
 

357 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. set(CLANG 1)
  79. endif()
  80. if (MACOSX)
  81. set(CMAKE_C_COMPILER /usr/bin/cc CACHE PATH "" FORCE)
  82. set(CMAKE_CXX_COMPILER /usr/bin/c++ CACHE PATH "" FORCE)
  83. endif()
  84. # Global configuration
  85. set(C_CXX_FLAGS
  86. "-Wno-ignored-qualifiers \
  87. -Wall \
  88. -Werror \
  89. -Wextra \
  90. -Wpedantic \
  91. -Wshadow \
  92. -Wno-variadic-macros \
  93. -Wundef \
  94. -Wunused-result")
  95. if(CLANG)
  96. set(C_CXX_FLAGS
  97. "-Wconditional-uninitialized \
  98. -Wmissing-variable-declarations")
  99. endif()
  100. # Build with address sanitizer
  101. if(ADDRSAN)
  102. set(CLANG 1)
  103. set(C_CXX_FLAGS "${C_CXX_FLAGS} -fsanitize=undefined,address,leak -fno-omit-frame-pointer")
  104. set(LDFLAGS "${LDFLAGS} -fsanitize=undefined,address,leak")
  105. endif()
  106. # Build with memory sanitizer
  107. if(MEMSAN)
  108. if(ADDRSAN)
  109. message(FATAL_ERROR "Can't use MSAN and ASAN")
  110. endif()
  111. set(CLANG 1)
  112. set(C_CXX_FLAGS "${C_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
  113. endif()
  114. include(.cmake/common.mk)
  115. # Control Debug/Release mode
  116. if(CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
  117. set(C_CXX_FLAGS "${C_CXX_FLAGS} -g3 -O0 -Wno-unused")
  118. else()
  119. set(C_CXX_FLAGS "${C_CXX_FLAGS} -O3")
  120. endif()
  121. include_directories(
  122. public
  123. src/common/
  124. src
  125. 3rd/cpu_features/include
  126. )
  127. set_property(GLOBAL PROPERTY obj_libs "")
  128. # Set CPU architecture
  129. set(CMAKE_C_FLAGS "${C_CXX_FLAGS} -D${ARCH}")
  130. set(CMAKE_CXX_FLAGS "${C_CXX_FLAGS} -D${ARCH}")
  131. if(PQC_WEAK_RANDOMBYTES)
  132. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPQC_WEAK_RANDOMBYTES")
  133. endif()
  134. # Define sources of the components
  135. add_subdirectory(src/sign/dilithium/dilithium2/clean)
  136. add_subdirectory(src/sign/dilithium/dilithium3/clean)
  137. add_subdirectory(src/sign/dilithium/dilithium5/clean)
  138. add_subdirectory(src/sign/falcon)
  139. add_subdirectory(src/sign/rainbow/rainbowV-classic/clean)
  140. add_subdirectory(src/sign/rainbow/rainbowI-classic/clean)
  141. add_subdirectory(src/sign/rainbow/rainbowIII-classic/clean)
  142. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-simple/clean)
  143. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-simple/clean)
  144. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-robust/clean)
  145. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-simple/clean)
  146. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-simple/clean)
  147. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-simple/clean)
  148. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-robust/clean)
  149. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-robust/clean)
  150. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-robust/clean)
  151. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-robust/clean)
  152. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-robust/clean)
  153. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-simple/clean)
  154. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-simple/clean)
  155. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-robust/clean)
  156. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-simple/clean)
  157. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-simple/clean)
  158. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-simple/clean)
  159. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-robust/clean)
  160. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-robust/clean)
  161. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-simple/clean)
  162. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/clean)
  163. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-robust/clean)
  164. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-simple/clean)
  165. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-robust/clean)
  166. add_subdirectory(src/kem/kyber/kyber512/clean)
  167. add_subdirectory(src/kem/kyber/kyber768/clean)
  168. add_subdirectory(src/kem/kyber/kyber1024/clean)
  169. add_subdirectory(src/kem/saber/lightsaber/clean)
  170. add_subdirectory(src/kem/saber/firesaber/clean)
  171. add_subdirectory(src/kem/saber/saber/clean)
  172. add_subdirectory(src/kem/frodo/frodokem640shake/clean)
  173. add_subdirectory(src/kem/frodo/frodokem976shake/clean)
  174. add_subdirectory(src/kem/frodo/frodokem1344shake/clean)
  175. add_subdirectory(src/kem/ntru/ntruhps4096821/clean)
  176. add_subdirectory(src/kem/ntru/ntruhps2048509/clean)
  177. add_subdirectory(src/kem/ntru/ntruhrss701/clean)
  178. add_subdirectory(src/kem/ntru/ntruhps2048677/clean)
  179. add_subdirectory(src/kem/ntru_prime/ntrulpr761/clean)
  180. add_subdirectory(src/kem/ntru_prime/ntrulpr653/clean)
  181. add_subdirectory(src/kem/ntru_prime/ntrulpr857/clean)
  182. add_subdirectory(src/kem/hqc/hqc-rmrs-128/clean)
  183. add_subdirectory(src/kem/hqc/hqc-rmrs-192/clean)
  184. add_subdirectory(src/kem/hqc/hqc-rmrs-256/clean)
  185. add_subdirectory(src/kem/sike)
  186. add_subdirectory(src/kem/mceliece/mceliece348864/clean)
  187. add_subdirectory(src/kem/mceliece/mceliece460896/clean)
  188. add_subdirectory(src/kem/mceliece/mceliece6688128/clean)
  189. add_subdirectory(src/kem/mceliece/mceliece6960119/clean)
  190. add_subdirectory(src/kem/mceliece/mceliece8192128/clean)
  191. add_subdirectory(src/kem/mceliece/mceliece348864f/clean)
  192. add_subdirectory(src/kem/mceliece/mceliece460896f/clean)
  193. add_subdirectory(src/kem/mceliece/mceliece6688128f/clean)
  194. add_subdirectory(src/kem/mceliece/mceliece6960119f/clean)
  195. add_subdirectory(src/kem/mceliece/mceliece8192128f/clean)
  196. # Hardware optimized targets
  197. if(${ARCH} STREQUAL "ARCH_x86_64")
  198. set(CMAKE_C_FLAGS
  199. "${CMAKE_C_FLAGS} -march=haswell")
  200. set(SRC_COMMON_AVX2
  201. src/common/keccak4x/KeccakP-1600-times4-SIMD256.c
  202. )
  203. # Sign
  204. add_subdirectory(src/sign/dilithium/dilithium2/avx2)
  205. add_subdirectory(src/sign/dilithium/dilithium3/avx2)
  206. add_subdirectory(src/sign/dilithium/dilithium5/avx2)
  207. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-simple/avx2)
  208. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-robust/avx2)
  209. add_subdirectory(src/sign/sphincs/sphincs-shake256-128s-robust/avx2)
  210. add_subdirectory(src/sign/sphincs/sphincs-shake256-128f-simple/avx2)
  211. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-simple/avx2)
  212. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-robust/avx2)
  213. add_subdirectory(src/sign/sphincs/sphincs-shake256-192s-robust/avx2)
  214. add_subdirectory(src/sign/sphincs/sphincs-shake256-192f-simple/avx2)
  215. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-robust/avx2)
  216. add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-simple/avx2)
  217. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-simple/avx2)
  218. add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-robust/avx2)
  219. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-robust/avx2)
  220. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-simple/avx2)
  221. add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-robust/avx2)
  222. add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-simple/avx2)
  223. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-simple/avx2)
  224. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-simple/avx2)
  225. add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-robust/avx2)
  226. add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-robust/avx2)
  227. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-simple/avx2)
  228. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-robust/avx2)
  229. add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-simple/avx2)
  230. add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/avx2)
  231. # KEMs
  232. add_subdirectory(src/kem/kyber/kyber512/avx2)
  233. add_subdirectory(src/kem/kyber/kyber768/avx2)
  234. add_subdirectory(src/kem/kyber/kyber1024/avx2)
  235. add_subdirectory(src/kem/saber/lightsaber/avx2)
  236. add_subdirectory(src/kem/saber/firesaber/avx2)
  237. add_subdirectory(src/kem/saber/saber/avx2)
  238. add_subdirectory(src/kem/ntru/ntruhps4096821/avx2)
  239. add_subdirectory(src/kem/ntru/ntruhps2048509/avx2)
  240. add_subdirectory(src/kem/ntru/ntruhrss701/avx2)
  241. add_subdirectory(src/kem/ntru/ntruhps2048677/avx2)
  242. add_subdirectory(src/kem/ntru_prime/ntrulpr761/avx2)
  243. add_subdirectory(src/kem/ntru_prime/ntrulpr653/avx2)
  244. add_subdirectory(src/kem/ntru_prime/ntrulpr857/avx2)
  245. add_subdirectory(src/kem/hqc/hqc-rmrs-128/avx2)
  246. add_subdirectory(src/kem/hqc/hqc-rmrs-192/avx2)
  247. add_subdirectory(src/kem/hqc/hqc-rmrs-256/avx2)
  248. endif()
  249. # The rest of the library
  250. set(SRC_COMMON_GENERIC
  251. src/common/aes.c
  252. src/common/fips202.c
  253. src/common/sp800-185.c
  254. src/common/randombytes.c
  255. src/common/sha2.c
  256. src/common/nistseedexpander.c
  257. src/capi/pqapi.c
  258. )
  259. add_library(
  260. common
  261. OBJECT
  262. ${SRC_COMMON_GENERIC}
  263. ${SRC_COMMON_AVX2}
  264. )
  265. add_library(
  266. pqc
  267. SHARED
  268. )
  269. add_library(
  270. pqc_s
  271. STATIC
  272. )
  273. get_property(OBJ_LIBS GLOBAL PROPERTY obj_libs)
  274. target_link_libraries(
  275. pqc
  276. ${OBJ_LIBS}
  277. cpu_features
  278. common
  279. )
  280. target_link_libraries(
  281. pqc_s
  282. cpu_features
  283. common
  284. ${OBJ_LIBS}
  285. )
  286. add_executable(
  287. ut
  288. test/ut.cpp
  289. )
  290. target_link_libraries(
  291. ut
  292. gtest
  293. gtest_main
  294. pqc_s)
  295. target_include_directories(
  296. ut PRIVATE
  297. ${CMAKE_SOURCE_DIR})
  298. if(NOT CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
  299. add_subdirectory(test/bench)
  300. endif()
  301. install(TARGETS pqc pqc_s
  302. PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ GROUP_WRITE WORLD_READ WORLD_WRITE
  303. LIBRARY DESTINATION lib
  304. ARCHIVE DESTINATION lib)
  305. install(FILES
  306. ${QRS_PUBLIC_INC}
  307. DESTINATION include/pqc)