]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - CMakeLists.txt
Merge branch 'master' into update-codeql-analysis
[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 3.0 REQUIRED)
12 find_program(OPENSSL_PROGRAM openssl
13   PATHS ${OPENSSL_ROOT_DIR} PATH_SUFFIXES apps bin NO_DEFAULT_PATH)
14 message("-- Found OpenSSL application: ${OPENSSL_PROGRAM}")
15 include_directories(${OPENSSL_INCLUDE_DIR})
16
17 if (CMAKE_C_COMPILER_ID MATCHES "Clang")
18   set(CMAKE_C_FLAGS_RELEASE -O2)
19   set(CMAKE_C_FLAGS_DEBUG "-O0 -ggdb")
20   set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -ggdb")
21   add_compile_options(-Werror -Wall -Wno-unused-parameter -Wno-unused-function -Wno-missing-braces -Qunused-arguments -Wno-deprecated-declarations)
22 elseif(CMAKE_C_COMPILER_ID MATCHES "GNU")
23   set(CMAKE_C_FLAGS_RELEASE -O2)
24   set(CMAKE_C_FLAGS_DEBUG "-O0 -ggdb")
25   set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -ggdb")
26   add_compile_options(-Werror -Wall -Wno-unused-parameter -Wno-unused-function -Wno-missing-braces -Wno-error=unknown-pragmas -Wno-error=pragmas -Wno-deprecated-declarations)
27 elseif(CMAKE_C_COMPILER_ID MATCHES "MSVC")
28   add_definitions(-D_CRT_SECURE_NO_WARNINGS)
29   add_definitions(-D_CRT_DEPRECATED_NO_WARNINGS)
30   add_definitions(-D_CRT_NONSTDC_NO_WARNINGS)
31   add_compile_options(/MP /WX /W4 /wd4100 /wd4267 /wd4206 /wd4706 /wd4244 /wd4115)
32 endif()
33
34 if (ASAN)
35   message(STATUS "address sanitizer enabled")
36   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -g3 -fno-omit-frame-pointer")
37   set(SKIP_PERL_TESTS 1)
38 endif()
39
40 # DEPRECATEDIN_3_0 CMAC
41 set_source_files_properties(gost_omac.c PROPERTIES COMPILE_FLAGS -Wno-error=deprecated-declarations)
42 # DEPRECATEDIN_3_0 HMAC
43 set_source_files_properties(gost_keyexpimp.c PROPERTIES COMPILE_FLAGS -Wno-error=deprecated-declarations)
44
45 set(CMAKE_C_STANDARD 90)
46 CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME_C)
47 CHECK_LIBRARY_EXISTS(rt clock_gettime "" HAVE_CLOCK_GETTIME_RT)
48 if(HAVE_CLOCK_GETTIME_RT AND NOT HAVE_CLOCK_GETTIME_C)
49   set(CLOCK_GETTIME_LIB rt)
50 endif()
51
52 include (TestBigEndian)
53 TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
54 if(IS_BIG_ENDIAN)
55  message(STATUS "BIG_ENDIAN")
56 else()
57  message(STATUS "LITTLE_ENDIAN")
58  add_definitions(-DL_ENDIAN)
59 endif()
60
61 check_c_source_runs("
62   #ifdef _MSC_VER
63   # include <intrin.h>
64   #else
65   # include <x86intrin.h>
66   #endif
67   int main(void) {
68     unsigned long long x = -1, y = 1, r;
69     unsigned char cf;
70     cf = _addcarry_u64(1, x, y, &r);
71     return !(cf == 1 && r == 1);
72   }
73   " ADDCARRY_U64)
74 if (ADDCARRY_U64)
75   add_definitions(-DHAVE_ADDCARRY_U64)
76 endif()
77
78 check_c_source_runs("
79   int main(void) {
80     char buf[16] = { 0, 1, 2 };
81     int *p = (int *)(buf + 1);
82     int *q = (int *)(buf + 2);
83     return (*p == *q);
84   }
85   " RELAXED_ALIGNMENT)
86 if (NOT RELAXED_ALIGNMENT)
87   add_definitions(-DSTRICT_ALIGNMENT)
88 endif()
89
90 set(BIN_DIRECTORY bin)
91
92 set(OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${BIN_DIRECTORY})
93
94 #set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY})
95 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY})
96 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY})
97
98 # Remove when https://gitlab.kitware.com/cmake/cmake/issues/18525 is addressed
99 set(OPENSSL_ENGINES_DIR "" CACHE PATH "OpenSSL Engines Directory")
100 if ("${OPENSSL_ENGINES_DIR}" STREQUAL "")
101         include(FindPkgConfig)
102         pkg_get_variable(OPENSSL_ENGINES_DIR libcrypto enginesdir)
103         if ("${OPENSSL_ENGINES_DIR}" STREQUAL "")
104                 message( FATAL_ERROR "Unable to discover the OpenSSL engines directory. Provide the path using -DOPENSSL_ENGINES_DIR" )
105         endif()
106 endif()
107
108 set(GOST_89_SOURCE_FILES
109         gost89.c
110         gost89.h
111         )
112
113 set(GOST_HASH_SOURCE_FILES
114         gosthash.c
115         gosthash.h
116         )
117
118 set(GOST_HASH_2012_SOURCE_FILES
119         gosthash2012.c
120         gosthash2012.h
121         gosthash2012_const.h
122         gosthash2012_precalc.h
123         gosthash2012_ref.h
124         gosthash2012_sse2.h
125         )
126
127 set(GOST_GRASSHOPPER_SOURCE_FILES
128         gost_grasshopper.h
129         gost_grasshopper_core.h
130         gost_grasshopper_core.c
131         gost_grasshopper_defines.h
132         gost_grasshopper_defines.c
133         gost_grasshopper_math.h
134         gost_grasshopper_galois_precompiled.c
135         gost_grasshopper_precompiled.c
136         gost_grasshopper_cipher.h
137         gost_grasshopper_cipher.c
138         )
139
140 set(GOST_CORE_SOURCE_FILES
141         e_gost_err.c
142         e_gost_err.h
143         gost_ameth.c
144         gost_pmeth.c
145         gost_ctl.c
146         gost_asn1.c
147         gost_crypt.c
148         gost_keywrap.c
149         gost_keywrap.h
150         gost_md.c
151         gost_md2012.c
152         gost_omac.c
153         gost_omac_acpkm.c
154         gost_gost2015.c
155         gost_lcl.h
156         gost_params.c
157         gost_keyexpimp.c
158         )
159
160 set(GOST_EC_SOURCE_FILES
161         gost_ec_keyx.c
162         gost_ec_sign.c
163         ecp_id_GostR3410_2001_CryptoPro_A_ParamSet.c
164         ecp_id_GostR3410_2001_CryptoPro_B_ParamSet.c
165         ecp_id_GostR3410_2001_CryptoPro_C_ParamSet.c
166         ecp_id_GostR3410_2001_TestParamSet.c
167         ecp_id_tc26_gost_3410_2012_256_paramSetA.c
168         ecp_id_tc26_gost_3410_2012_512_paramSetA.c
169         ecp_id_tc26_gost_3410_2012_512_paramSetB.c
170         ecp_id_tc26_gost_3410_2012_512_paramSetC.c
171         )
172
173 set (GOST_OMAC_SOURCE_FILES
174         gost_omac.c
175         gost_omac_acpkm.c
176         )
177
178 set(GOST_LIB_SOURCE_FILES
179         ${GOST_CORE_SOURCE_FILES}
180         ${GOST_89_SOURCE_FILES}
181         ${GOST_HASH_SOURCE_FILES}
182         ${GOST_HASH_2012_SOURCE_FILES}
183         ${GOST_GRASSHOPPER_SOURCE_FILES}
184         ${GOST_EC_SOURCE_FILES}
185         ${GOST_OMAC_SOURCE_FILES}
186         )
187
188 set(GOST_ENGINE_SOURCE_FILES
189         gost_eng.c
190         )
191
192 set(TEST_ENVIRONMENT
193         CMAKE_CURRENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}
194         PERL5LIB=${CMAKE_CURRENT_SOURCE_DIR}/test
195         OPENSSL_ENGINES=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
196         OPENSSL_PROGRAM=${OPENSSL_PROGRAM}
197         OPENSSL_CRYPTO_LIBRARY=${OPENSSL_CRYPTO_LIBRARY}
198         OPENSSL_CONF=${CMAKE_CURRENT_SOURCE_DIR}/test/engine.cnf
199         )
200 add_executable(test_digest test_digest.c)
201 target_link_libraries(test_digest ${OPENSSL_CRYPTO_LIBRARY})
202 add_test(NAME digest COMMAND test_digest)
203 set_tests_properties(digest PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
204
205 add_executable(test_ciphers test_ciphers.c)
206 target_link_libraries(test_ciphers ${OPENSSL_CRYPTO_LIBRARY})
207 add_test(NAME ciphers COMMAND test_ciphers)
208 set_tests_properties(ciphers PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
209
210 # test_curves is an internals testing program, it doesn't need a test env
211 add_executable(test_curves test_curves.c)
212 target_link_libraries(test_curves gost_core ${OPENSSL_CRYPTO_LIBRARY})
213 add_test(NAME curves COMMAND test_curves)
214
215 add_executable(test_params test_params.c)
216 target_link_libraries(test_params ${OPENSSL_CRYPTO_LIBRARY})
217 add_test(NAME parameters COMMAND test_params)
218 set_tests_properties(parameters PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
219
220 add_executable(test_derive test_derive.c)
221 target_link_libraries(test_derive ${OPENSSL_CRYPTO_LIBRARY})
222 add_test(NAME derive COMMAND test_derive)
223 set_tests_properties(derive PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
224
225 add_executable(test_sign test_sign.c)
226 target_link_libraries(test_sign ${OPENSSL_CRYPTO_LIBRARY})
227 add_test(NAME sign/verify COMMAND test_sign)
228 set_tests_properties(sign/verify PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
229
230 add_executable(test_tls test_tls.c)
231 target_link_libraries(test_tls ${OPENSSL_CRYPTO_LIBRARY} ${OPENSSL_SSL_LIBRARY})
232 add_test(NAME TLS COMMAND test_tls)
233 set_tests_properties(TLS PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
234
235 add_executable(test_context test_context.c)
236 target_link_libraries(test_context ${OPENSSL_CRYPTO_LIBRARY})
237 add_test(NAME context COMMAND test_context)
238 set_tests_properties(context PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
239
240 # test_keyexpimp is an internals testing program, it doesn't need a test env
241 add_executable(test_keyexpimp test_keyexpimp.c)
242 #target_compile_definitions(test_keyexpimp PUBLIC -DOPENSSL_LOAD_CONF)
243 target_link_libraries(test_keyexpimp gost_core ${OPENSSL_CRYPTO_LIBRARY})
244 add_test(NAME keyexpimp COMMAND test_keyexpimp)
245
246 # test_gost89 is an internals testing program, it doesn't need a test env
247 add_executable(test_gost89 test_gost89.c)
248 target_link_libraries(test_gost89 gost_core ${OPENSSL_CRYPTO_LIBRARY})
249 add_test(NAME gost89 COMMAND test_gost89)
250
251 if(NOT SKIP_PERL_TESTS)
252     execute_process(COMMAND perl -MTest2::V0 -e ""
253         ERROR_QUIET RESULT_VARIABLE HAVE_TEST2_V0)
254     if(NOT HAVE_TEST2_V0)
255         add_test(NAME engine
256             COMMAND prove --merge -PWrapOpenSSL ${CMAKE_CURRENT_SOURCE_DIR}/test)
257         set_tests_properties(engine PROPERTIES ENVIRONMENT "${TEST_ENVIRONMENT}")
258     else()
259       message(STATUS "No Test2::V0 perl module (engine tests skipped)")
260     endif()
261 endif()
262
263 add_executable(sign benchmark/sign.c)
264 target_link_libraries(sign gost_core ${OPENSSL_CRYPTO_LIBRARY} ${CLOCK_GETTIME_LIB})
265
266 # All that may need to load just built engine will have path to it defined.
267 set(BINARY_TESTS_TARGETS
268         test_digest
269         test_ciphers
270         test_curves
271         test_params
272         test_derive
273         test_sign
274         test_context
275         test_keyexpimp
276         test_gost89
277         test_tls
278         )
279 set_property(TARGET ${BINARY_TESTS_TARGETS} APPEND PROPERTY COMPILE_DEFINITIONS ENGINE_DIR="${OUTPUT_DIRECTORY}")
280
281 add_library(gost_core STATIC ${GOST_LIB_SOURCE_FILES})
282 set_target_properties(gost_core PROPERTIES POSITION_INDEPENDENT_CODE ON)
283
284 # The GOST engine in module form
285 add_library(gost_engine MODULE ${GOST_ENGINE_SOURCE_FILES})
286 # Set the suffix explicitly to adapt to OpenSSL's idea of what a
287 # module suffix should be
288 set_target_properties(gost_engine PROPERTIES
289   PREFIX "" OUTPUT_NAME "gost" SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
290 target_link_libraries(gost_engine PRIVATE gost_core ${OPENSSL_CRYPTO_LIBRARY})
291
292 # The GOST engine in library form
293 add_library(lib_gost_engine SHARED ${GOST_ENGINE_SOURCE_FILES})
294 set_target_properties(lib_gost_engine PROPERTIES
295   COMPILE_DEFINITIONS "BUILDING_ENGINE_AS_LIBRARY"
296   PUBLIC_HEADER gost-engine.h
297   OUTPUT_NAME "gost")
298 target_link_libraries(lib_gost_engine PRIVATE gost_core ${OPENSSL_CRYPTO_LIBRARY})
299
300
301 set(GOST_SUM_SOURCE_FILES
302         gostsum.c
303         )
304
305 add_executable(gostsum ${GOST_SUM_SOURCE_FILES})
306 target_link_libraries(gostsum gost_core ${OPENSSL_CRYPTO_LIBRARY})
307
308 set(GOST_12_SUM_SOURCE_FILES
309         gost12sum.c
310         )
311
312 add_executable(gost12sum ${GOST_12_SUM_SOURCE_FILES})
313 target_link_libraries(gost12sum gost_core)
314
315 set_source_files_properties(tags PROPERTIES GENERATED true)
316 add_custom_target(tags
317     COMMAND ctags -R . ${OPENSSL_ROOT_DIR}
318     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
319
320 add_custom_target(tcl_tests
321     COMMAND OPENSSL_LIBCRYPTO=${OPENSSL_CRYPTO_LIBRARY}
322             OPENSSL_APP=${OPENSSL_PROGRAM}
323             TESTSRC=${CMAKE_SOURCE_DIR}/tcl_tests
324             TESTDIR=${CMAKE_BINARY_DIR}/tcl_tests
325             ENGINE_DIR=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
326             sh ./runtest.sh
327     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tcl_tests)
328
329 add_executable(test_tlstree test_tlstree.c)
330 target_link_libraries(test_tlstree PUBLIC ${OPENSSL_CRYPTO_LIBRARY})
331
332 # install programs and manuals
333 install(TARGETS gostsum gost12sum RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
334 install(FILES gostsum.1 gost12sum.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
335
336 # install engine in library and module form
337 install(TARGETS lib_gost_engine EXPORT GostEngineConfig LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
338 install(TARGETS gost_engine EXPORT GostEngineConfig
339         LIBRARY  DESTINATION ${OPENSSL_ENGINES_DIR}
340         RUNTIME  DESTINATION ${OPENSSL_ENGINES_DIR})
341 if (MSVC)
342   install(FILES $<TARGET_PDB_FILE:lib_gost_engine>
343     EXPORT GostEngineConfig DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
344   install(FILES $<TARGET_PDB_FILE:gostsum> $<TARGET_PDB_FILE:gost12sum>
345     EXPORT GostEngineConfig DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
346   install(FILES $<TARGET_PDB_FILE:gost_engine>
347     EXPORT GostEngineConfig DESTINATION ${OPENSSL_ENGINES_DIR} OPTIONAL)
348 endif()
349 install(EXPORT GostEngineConfig DESTINATION GostEngine/share/cmake/GostEngine)