]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - CMakeLists.txt
gosthash2012: Clean up cmake test for _addcarry_u64
[openssl-gost/engine.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2 project(gost-engine LANGUAGES C)
3
4 include(GNUInstallDirs)
5 include(CheckLibraryExists)
6 include(CheckFunctionExists)
7 include(CheckCSourceRuns)
8
9 enable_testing()
10
11 find_package(OpenSSL 1.1.1 REQUIRED)
12 include_directories(${OPENSSL_INCLUDE_DIR})
13
14 if (CMAKE_C_COMPILER_ID MATCHES "Clang")
15  add_compile_options(-O2 -Werror -Wall -Wno-unused-parameter -Wno-unused-function -Wno-missing-braces -ggdb -Qunused-arguments)
16 elseif(CMAKE_C_COMPILER_ID MATCHES "GNU")
17  add_compile_options(-O2 -Werror -Wall -Wno-unused-parameter -Wno-unused-function -Wno-missing-braces -ggdb)
18 elseif(CMAKE_C_COMPILER_ID MATCHES "MSVC")
19  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
20  add_definitions(-D_CRT_DEPRECATED_NO_WARNINGS)
21  add_definitions(-D_CRT_NONSTDC_NO_WARNINGS)
22  add_compile_options(/MP /WX /W4 /wd4100 /wd4267 /wd4206 /wd4706 /wd4244 /wd4115)
23 endif()
24
25 if (ASAN)
26   message(STATUS "address sanitizer enabled")
27   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -g3 -fno-omit-frame-pointer")
28 endif()
29
30 # DEPRECATEDIN_3_0 CMAC
31 set_source_files_properties(gost_omac.c PROPERTIES COMPILE_FLAGS -Wno-error=deprecated-declarations)
32 # DEPRECATEDIN_3_0 HMAC
33 set_source_files_properties(gost_keyexpimp.c PROPERTIES COMPILE_FLAGS -Wno-error=deprecated-declarations)
34
35 set(CMAKE_C_STANDARD 90)
36 CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME_C)
37 CHECK_LIBRARY_EXISTS(rt clock_gettime "" HAVE_CLOCK_GETTIME_RT)
38 if(HAVE_CLOCK_GETTIME_RT AND NOT HAVE_CLOCK_GETTIME_C)
39   set(CLOCK_GETTIME_LIB rt)
40 endif()
41
42 include (TestBigEndian)
43 TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
44 if(IS_BIG_ENDIAN)
45  message(STATUS "BIG_ENDIAN")
46 else()
47  message(STATUS "LITTLE_ENDIAN")
48  add_definitions(-DL_ENDIAN)
49 endif()
50
51 check_c_source_runs("
52   #ifdef _MSC_VER
53   # include <intrin.h>
54   #else
55   # include <x86intrin.h>
56   #endif
57   int main(void) {
58     unsigned long long x = -1, y = 1, r;
59     unsigned char cf;
60     cf = _addcarry_u64(1, x, y, &r);
61     return !(cf == 1 && r == 1);
62   }
63   " ADDCARRY_U64)
64 if (ADDCARRY_U64)
65   add_definitions(-DHAVE_ADDCARRY_U64)
66 endif()
67
68 set(BIN_DIRECTORY bin)
69
70 # Same soversion as OpenSSL
71 set(GOST_SOVERSION "${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}")
72
73 set(OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${BIN_DIRECTORY})
74
75 #set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY})
76 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY})
77 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY})
78
79 # Remove when https://gitlab.kitware.com/cmake/cmake/issues/18525 is addressed
80 set(OPENSSL_ENGINES_DIR "" CACHE PATH "OpenSSL Engines Directory")
81 if ("${OPENSSL_ENGINES_DIR}" STREQUAL "")
82         include(FindPkgConfig)
83         pkg_get_variable(OPENSSL_ENGINES_DIR libcrypto enginesdir)
84         if ("${OPENSSL_ENGINES_DIR}" STREQUAL "")
85                 message( FATAL_ERROR "Unable to discover the OpenSSL engines directory. Provide the path using -DOPENSSL_ENGINES_DIR" )
86         endif()
87 endif()
88
89 set(GOST_89_SOURCE_FILES
90         gost89.c
91         gost89.h
92         )
93
94 set(GOST_HASH_SOURCE_FILES
95         gosthash.c
96         gosthash.h
97         )
98
99 set(GOST_HASH_2012_SOURCE_FILES
100         gosthash2012.c
101         gosthash2012.h
102         gosthash2012_const.h
103         gosthash2012_precalc.h
104         gosthash2012_ref.h
105         gosthash2012_sse2.h
106         )
107
108 set(GOST_GRASSHOPPER_SOURCE_FILES
109         gost_grasshopper.h
110         gost_grasshopper_core.h
111         gost_grasshopper_core.c
112         gost_grasshopper_defines.h
113         gost_grasshopper_defines.c
114         gost_grasshopper_math.h
115         gost_grasshopper_galois_precompiled.c
116         gost_grasshopper_precompiled.c
117         gost_grasshopper_cipher.h
118         gost_grasshopper_cipher.c
119         )
120
121 set(GOST_CORE_SOURCE_FILES
122         e_gost_err.c
123         e_gost_err.h
124         gost_asn1.c
125         gost_crypt.c
126         gost_ctl.c
127         gost_eng.c
128         gost_keywrap.c
129         gost_keywrap.h
130         gost_lcl.h
131         gost_params.c
132         gost_keyexpimp.c
133         )
134
135 set(GOST_EC_SOURCE_FILES
136         gost_ec_keyx.c
137         gost_ec_sign.c
138         )
139
140 set (GOST_OMAC_SOURCE_FILES
141         gost_omac.c
142         gost_omac_acpkm.c
143         )
144
145 set(GOST_LIB_SOURCE_FILES
146         ${GOST_89_SOURCE_FILES}
147         ${GOST_HASH_SOURCE_FILES}
148         ${GOST_HASH_2012_SOURCE_FILES}
149         ${GOST_GRASSHOPPER_SOURCE_FILES}
150         ${GOST_EC_SOURCE_FILES}
151         ${GOST_OMAC_SOURCE_FILES}
152         )
153
154 set(GOST_ENGINE_SOURCE_FILES
155         ${GOST_CORE_SOURCE_FILES}
156         gost_ameth.c
157         gost_md.c
158         gost_md2012.c
159         gost_pmeth.c
160         gost_omac.c
161         gost_omac_acpkm.c
162         )
163
164 add_executable(test_digest test_digest.c)
165 target_link_libraries(test_digest gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
166 add_test(NAME digest
167         COMMAND test_digest)
168
169 add_executable(test_curves test_curves.c)
170 target_link_libraries(test_curves gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
171 add_test(NAME curves
172         COMMAND test_curves)
173
174 add_executable(test_params test_params.c)
175 target_link_libraries(test_params gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
176 add_test(NAME parameters
177         COMMAND test_params)
178
179 add_executable(test_sign test_sign.c)
180 target_link_libraries(test_sign gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
181 add_test(NAME sign/verify
182         COMMAND test_sign)
183
184 add_executable(test_tls test_tls.c)
185 target_link_libraries(test_tls gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY} ${OPENSSL_SSL_LIBRARY})
186 add_test(NAME TLS
187         COMMAND test_tls)
188
189 add_executable(test_context test_context.c)
190 target_link_libraries(test_context gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
191 add_test(NAME context
192         COMMAND test_context)
193
194 add_executable(test_grasshopper test_grasshopper.c)
195 target_link_libraries(test_grasshopper gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
196 add_test(NAME grasshopper
197         COMMAND test_grasshopper)
198
199 add_executable(test_keyexpimp test_keyexpimp.c)
200 #target_compile_definitions(test_keyexpimp PUBLIC -DOPENSSL_LOAD_CONF)
201 target_link_libraries(test_keyexpimp gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
202 add_test(NAME keyexpimp
203         COMMAND test_keyexpimp)
204
205 add_executable(test_gost89 test_gost89.c)
206 target_link_libraries(test_gost89 gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
207 add_test(NAME gost89
208         COMMAND test_gost89)
209
210 if(NOT ASAN)
211 add_test(NAME engine
212         COMMAND perl run_tests
213         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test)
214 set_tests_properties(engine PROPERTIES ENVIRONMENT
215         "OPENSSL_ROOT_DIR=${OPENSSL_ROOT_DIR};OPENSSL_ENGINES=${OUTPUT_DIRECTORY};OPENSSL_CONF=${CMAKE_SOURCE_DIR}/test/empty.cnf")
216 endif()
217
218 add_executable(sign benchmark/sign.c)
219 target_link_libraries(sign gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY} ${CLOCK_GETTIME_LIB})
220
221 # All that may need to load just built engine will have path to it defined.
222 set(BINARY_TESTS_TARGETS
223         test_digest
224         test_curves
225         test_params
226         test_sign
227         test_context
228         test_grasshopper
229         test_keyexpimp
230         test_gost89
231         test_tls
232         )
233 set_property(TARGET ${BINARY_TESTS_TARGETS} APPEND PROPERTY COMPILE_DEFINITIONS ENGINE_DIR="${OUTPUT_DIRECTORY}")
234
235 add_library(gost_core STATIC ${GOST_LIB_SOURCE_FILES})
236 set_target_properties(gost_core PROPERTIES POSITION_INDEPENDENT_CODE ON)
237
238 add_library(gost_engine SHARED ${GOST_ENGINE_SOURCE_FILES})
239 set_target_properties(gost_engine PROPERTIES PREFIX "" OUTPUT_NAME "gost")
240 set_target_properties(gost_engine PROPERTIES VERSION ${GOST_SOVERSION} SOVERSION ${GOST_SOVERSION})
241 target_link_libraries(gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
242
243 set(GOST_SUM_SOURCE_FILES
244         gostsum.c
245         )
246
247 add_executable(gostsum ${GOST_SUM_SOURCE_FILES})
248 target_link_libraries(gostsum gost_core)
249
250 set(GOST_12_SUM_SOURCE_FILES
251         gost12sum.c
252         )
253
254 add_executable(gost12sum ${GOST_12_SUM_SOURCE_FILES})
255 target_link_libraries(gost12sum gost_core)
256
257 set_source_files_properties(tags PROPERTIES GENERATED true)
258 add_custom_target(tags
259     COMMAND ctags -R . ${OPENSSL_ROOT_DIR}
260     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
261
262 add_custom_target(tcl_tests
263     COMMAND ENGINE_DIR=${OUTPUT_DIRECTORY} sh ./runtest.sh
264     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tcl_tests)
265
266 add_executable(test_tlstree test_tlstree.c)
267 target_link_libraries(test_tlstree PUBLIC ${OPENSSL_CRYPTO_LIBRARY})
268
269 # install
270 set(OPENSSL_MAN_INSTALL_DIR ${CMAKE_INSTALL_MANDIR}/man1)
271
272 install(TARGETS gost_engine gostsum gost12sum EXPORT GostEngineConfig
273         LIBRARY  DESTINATION ${OPENSSL_ENGINES_DIR}
274         RUNTIME  DESTINATION ${CMAKE_INSTALL_BINDIR})
275 install(FILES gostsum.1 gost12sum.1 DESTINATION ${OPENSSL_MAN_INSTALL_DIR})
276 if (MSVC)
277  install(FILES $<TARGET_PDB_FILE:gost_engine> DESTINATION ${OPENSSL_ENGINES_DIR} OPTIONAL)
278  install(FILES $<TARGET_PDB_FILE:gostsum> $<TARGET_PDB_FILE:gost12sum> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
279 endif()