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