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.
 
 
 
 

326 lines
10 KiB

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