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