CMakeLists.txt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. add_sources_from_current_dir(crypto
  2. aes-common.c
  3. aes-select.c
  4. aes-sw.c
  5. aesgcm-common.c
  6. aesgcm-select.c
  7. aesgcm-sw.c
  8. aesgcm-ref-poly.c
  9. arcfour.c
  10. argon2.c
  11. bcrypt.c
  12. blake2.c
  13. blowfish.c
  14. chacha20-poly1305.c
  15. crc32.c
  16. des.c
  17. diffie-hellman.c
  18. dsa.c
  19. ecc-arithmetic.c
  20. ecc-ssh.c
  21. hash_simple.c
  22. hmac.c
  23. mac.c
  24. mac_simple.c
  25. md5.c
  26. mpint.c
  27. ntru.c
  28. openssh-certs.c
  29. prng.c
  30. pubkey-pem.c
  31. pubkey-ppk.c
  32. pubkey-ssh1.c
  33. rfc6979.c
  34. rsa.c
  35. sha256-common.c
  36. sha256-select.c
  37. sha256-sw.c
  38. sha512-common.c
  39. sha512-select.c
  40. sha512-sw.c
  41. sha3.c
  42. sha1-common.c
  43. sha1-select.c
  44. sha1-sw.c
  45. xdmauth.c)
  46. include(CheckCSourceCompiles)
  47. function(test_compile_with_flags outvar)
  48. cmake_parse_arguments(OPT "" ""
  49. "GNU_FLAGS;MSVC_FLAGS;ADD_SOURCES_IF_SUCCESSFUL;TEST_SOURCE" "${ARGN}")
  50. # Figure out what flags are applicable to this compiler.
  51. set(flags)
  52. if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR
  53. CMAKE_C_COMPILER_ID MATCHES "Clang")
  54. set(flags ${OPT_GNU_FLAGS})
  55. endif()
  56. if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
  57. set(flags ${OPT_MSVC_FLAGS})
  58. endif()
  59. # See if we can compile the provided test program.
  60. foreach(i ${flags})
  61. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${i}")
  62. endforeach()
  63. check_c_source_compiles("${OPT_TEST_SOURCE}" "${outvar}")
  64. if(${outvar} AND OPT_ADD_SOURCES_IF_SUCCESSFUL)
  65. # Make an object library that compiles the implementation with the
  66. # necessary flags, and add the resulting objects to the crypto
  67. # library.
  68. set(libname object_lib_${outvar})
  69. add_library(${libname} OBJECT ${OPT_ADD_SOURCES_IF_SUCCESSFUL})
  70. target_compile_options(${libname} PRIVATE ${flags})
  71. target_sources(crypto PRIVATE $<TARGET_OBJECTS:${libname}>)
  72. endif()
  73. # Export the output to the caller's scope, so that further tests can
  74. # be based on it.
  75. set(${outvar} ${${outvar}} PARENT_SCOPE)
  76. endfunction()
  77. # ----------------------------------------------------------------------
  78. # Try to enable x86 intrinsics-based crypto implementations.
  79. test_compile_with_flags(HAVE_WMMINTRIN_H
  80. GNU_FLAGS -msse4.1
  81. TEST_SOURCE "
  82. #include <wmmintrin.h>
  83. #include <smmintrin.h>
  84. volatile __m128i r, a, b;
  85. int main(void) { r = _mm_xor_si128(a, b); }")
  86. if(HAVE_WMMINTRIN_H)
  87. test_compile_with_flags(HAVE_AES_NI
  88. GNU_FLAGS -msse4.1 -maes
  89. TEST_SOURCE "
  90. #include <wmmintrin.h>
  91. #include <smmintrin.h>
  92. volatile __m128i r, a, b;
  93. int main(void) { r = _mm_aesenc_si128(a, b); }"
  94. ADD_SOURCES_IF_SUCCESSFUL aes-ni aes-ni.c)
  95. # shaintrin.h doesn't exist on all compilers; sometimes it's folded
  96. # into the other headers
  97. test_compile_with_flags(HAVE_SHAINTRIN_H
  98. GNU_FLAGS -msse4.1 -msha
  99. TEST_SOURCE "
  100. #include <wmmintrin.h>
  101. #include <smmintrin.h>
  102. #include <immintrin.h>
  103. #include <shaintrin.h>
  104. volatile __m128i r, a, b;
  105. int main(void) { r = _mm_xor_si128(a, b); }")
  106. if(HAVE_SHAINTRIN_H)
  107. set(include_shaintrin "#include <shaintrin.h>")
  108. else()
  109. set(include_shaintrin "")
  110. endif()
  111. test_compile_with_flags(HAVE_SHA_NI
  112. GNU_FLAGS -msse4.1 -msha
  113. TEST_SOURCE "
  114. #include <wmmintrin.h>
  115. #include <smmintrin.h>
  116. #include <immintrin.h>
  117. ${include_shaintrin}
  118. volatile __m128i r, a, b, c;
  119. int main(void) { r = _mm_sha256rnds2_epu32(a, b, c); }"
  120. ADD_SOURCES_IF_SUCCESSFUL sha256-ni.c sha1-ni.c)
  121. test_compile_with_flags(HAVE_CLMUL
  122. GNU_FLAGS -msse4.1 -mpclmul
  123. TEST_SOURCE "
  124. #include <wmmintrin.h>
  125. #include <tmmintrin.h>
  126. volatile __m128i r, a, b;
  127. int main(void) { r = _mm_clmulepi64_si128(a, b, 5);
  128. r = _mm_shuffle_epi8(r, a); }"
  129. ADD_SOURCES_IF_SUCCESSFUL aesgcm-clmul.c)
  130. endif()
  131. # ----------------------------------------------------------------------
  132. # Try to enable Arm Neon intrinsics-based crypto implementations.
  133. # Start by checking which header file we need. ACLE specifies that it
  134. # ought to be <arm_neon.h>, on both 32- and 64-bit Arm, but Visual
  135. # Studio for some reason renamed the header to <arm64_neon.h> in
  136. # 64-bit, and gives an error if you use the standard name. (However,
  137. # clang-cl does let you use the standard name.)
  138. test_compile_with_flags(HAVE_ARM_NEON_H
  139. MSVC_FLAGS -D_ARM_USE_NEW_NEON_INTRINSICS
  140. TEST_SOURCE "
  141. #include <arm_neon.h>
  142. volatile uint8x16_t r, a, b;
  143. int main(void) { r = veorq_u8(a, b); }")
  144. if(HAVE_ARM_NEON_H)
  145. set(neon ON)
  146. set(neon_header "arm_neon.h")
  147. else()
  148. test_compile_with_flags(HAVE_ARM64_NEON_H TEST_SOURCE "
  149. #include <arm64_neon.h>
  150. volatile uint8x16_t r, a, b;
  151. int main(void) { r = veorq_u8(a, b); }")
  152. if(HAVE_ARM64_NEON_H)
  153. set(neon ON)
  154. set(neon_header "arm64_neon.h")
  155. set(USE_ARM64_NEON_H ON)
  156. endif()
  157. endif()
  158. if(neon)
  159. # If we have _some_ NEON header, look for the individual things we
  160. # can enable with it.
  161. # The 'crypto' architecture extension includes support for AES,
  162. # SHA-1, and SHA-256.
  163. test_compile_with_flags(HAVE_NEON_CRYPTO
  164. GNU_FLAGS -march=armv8-a+crypto
  165. MSVC_FLAGS -D_ARM_USE_NEW_NEON_INTRINSICS
  166. TEST_SOURCE "
  167. #include <${neon_header}>
  168. volatile uint8x16_t r, a, b;
  169. volatile uint32x4_t s, x, y, z;
  170. int main(void) { r = vaeseq_u8(a, b); s = vsha256hq_u32(x, y, z); }"
  171. ADD_SOURCES_IF_SUCCESSFUL aes-neon.c sha256-neon.c sha1-neon.c)
  172. test_compile_with_flags(HAVE_NEON_PMULL
  173. GNU_FLAGS -march=armv8-a+crypto
  174. MSVC_FLAGS -D_ARM_USE_NEW_NEON_INTRINSICS
  175. TEST_SOURCE "
  176. #include <${neon_header}>
  177. volatile poly128_t r;
  178. volatile poly64_t a, b;
  179. volatile poly64x2_t u, v;
  180. int main(void) { r = vmull_p64(a, b); r = vmull_high_p64(u, v); }"
  181. ADD_SOURCES_IF_SUCCESSFUL aesgcm-neon.c)
  182. test_compile_with_flags(HAVE_NEON_VADDQ_P128
  183. GNU_FLAGS -march=armv8-a+crypto
  184. MSVC_FLAGS -D_ARM_USE_NEW_NEON_INTRINSICS
  185. TEST_SOURCE "
  186. #include <${neon_header}>
  187. volatile poly128_t r;
  188. int main(void) { r = vaddq_p128(r, r); }")
  189. # The 'sha3' architecture extension, despite the name, includes
  190. # support for SHA-512 (from the SHA-2 standard) as well as SHA-3
  191. # proper.
  192. #
  193. # Versions of clang up to and including clang 12 support this
  194. # extension in assembly language, but not the ACLE intrinsics for
  195. # it. So we check both.
  196. test_compile_with_flags(HAVE_NEON_SHA512_INTRINSICS
  197. GNU_FLAGS -march=armv8.2-a+crypto+sha3
  198. TEST_SOURCE "
  199. #include <${neon_header}>
  200. volatile uint64x2_t r, a, b;
  201. int main(void) { r = vsha512su0q_u64(a, b); }"
  202. ADD_SOURCES_IF_SUCCESSFUL sha512-neon.c)
  203. if(HAVE_NEON_SHA512_INTRINSICS)
  204. set(HAVE_NEON_SHA512 ON)
  205. else()
  206. test_compile_with_flags(HAVE_NEON_SHA512_ASM
  207. GNU_FLAGS -march=armv8.2-a+crypto+sha3
  208. TEST_SOURCE "
  209. #include <${neon_header}>
  210. volatile uint64x2_t r, a;
  211. int main(void) { __asm__(\"sha512su0 %0.2D,%1.2D\" : \"+w\" (r) : \"w\" (a)); }"
  212. ADD_SOURCES_IF_SUCCESSFUL sha512-neon.c)
  213. if(HAVE_NEON_SHA512_ASM)
  214. set(HAVE_NEON_SHA512 ON)
  215. endif()
  216. endif()
  217. endif()
  218. set(HAVE_AES_NI ${HAVE_AES_NI} PARENT_SCOPE)
  219. set(HAVE_SHA_NI ${HAVE_SHA_NI} PARENT_SCOPE)
  220. set(HAVE_SHAINTRIN_H ${HAVE_SHAINTRIN_H} PARENT_SCOPE)
  221. set(HAVE_NEON_CRYPTO ${HAVE_NEON_CRYPTO} PARENT_SCOPE)
  222. set(HAVE_NEON_SHA512 ${HAVE_NEON_SHA512} PARENT_SCOPE)
  223. set(HAVE_NEON_SHA512_INTRINSICS ${HAVE_NEON_SHA512_INTRINSICS} PARENT_SCOPE)
  224. set(USE_ARM64_NEON_H ${USE_ARM64_NEON_H} PARENT_SCOPE)