]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - CMakeLists.txt
CMakeLists.txt: Fix warning on gcc-9
[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 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 -Wno-deprecated-declarations)
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 -Wno-error=unknown-pragmas -Wno-error=pragmas -Wno-deprecated-declarations)
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   set(SKIP_PERL_TESTS 1)
29 endif()
30
31 # DEPRECATEDIN_3_0 CMAC
32 set_source_files_properties(gost_omac.c PROPERTIES COMPILE_FLAGS -Wno-error=deprecated-declarations)
33 # DEPRECATEDIN_3_0 HMAC
34 set_source_files_properties(gost_keyexpimp.c PROPERTIES COMPILE_FLAGS -Wno-error=deprecated-declarations)
35
36 set(CMAKE_C_STANDARD 90)
37 CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME_C)
38 CHECK_LIBRARY_EXISTS(rt clock_gettime "" HAVE_CLOCK_GETTIME_RT)
39 if(HAVE_CLOCK_GETTIME_RT AND NOT HAVE_CLOCK_GETTIME_C)
40   set(CLOCK_GETTIME_LIB rt)
41 endif()
42
43 include (TestBigEndian)
44 TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
45 if(IS_BIG_ENDIAN)
46  message(STATUS "BIG_ENDIAN")
47 else()
48  message(STATUS "LITTLE_ENDIAN")
49  add_definitions(-DL_ENDIAN)
50 endif()
51
52 check_c_source_runs("
53   #ifdef _MSC_VER
54   # include <intrin.h>
55   #else
56   # include <x86intrin.h>
57   #endif
58   int main(void) {
59     unsigned long long x = -1, y = 1, r;
60     unsigned char cf;
61     cf = _addcarry_u64(1, x, y, &r);
62     return !(cf == 1 && r == 1);
63   }
64   " ADDCARRY_U64)
65 if (ADDCARRY_U64)
66   add_definitions(-DHAVE_ADDCARRY_U64)
67 endif()
68
69 check_c_source_runs("
70   int main(void) {
71     char buf[16] = { 0, 1, 2 };
72     int *p = (int *)(buf + 1);
73     int *q = (int *)(buf + 2);
74     return (*p == *q);
75   }
76   " RELAXED_ALIGNMENT)
77 if (NOT RELAXED_ALIGNMENT)
78   add_definitions(-DSTRICT_ALIGNMENT)
79 endif()
80
81 set(BIN_DIRECTORY bin)
82
83 # Same soversion as OpenSSL
84 set(GOST_SOVERSION "${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}")
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_asn1.c
138         gost_crypt.c
139         gost_ctl.c
140         gost_eng.c
141         gost_keywrap.c
142         gost_keywrap.h
143         gost_lcl.h
144         gost_params.c
145         gost_keyexpimp.c
146         )
147
148 set(GOST_EC_SOURCE_FILES
149         gost_ec_keyx.c
150         gost_ec_sign.c
151         ecp_id_GostR3410_2001_CryptoPro_A_ParamSet.c
152         ecp_id_GostR3410_2001_CryptoPro_B_ParamSet.c
153         ecp_id_GostR3410_2001_CryptoPro_C_ParamSet.c
154         ecp_id_GostR3410_2001_TestParamSet.c
155         ecp_id_tc26_gost_3410_2012_256_paramSetA.c
156         ecp_id_tc26_gost_3410_2012_512_paramSetA.c
157         ecp_id_tc26_gost_3410_2012_512_paramSetB.c
158         ecp_id_tc26_gost_3410_2012_512_paramSetC.c
159         )
160
161 set (GOST_OMAC_SOURCE_FILES
162         gost_omac.c
163         gost_omac_acpkm.c
164         )
165
166 set(GOST_LIB_SOURCE_FILES
167         ${GOST_89_SOURCE_FILES}
168         ${GOST_HASH_SOURCE_FILES}
169         ${GOST_HASH_2012_SOURCE_FILES}
170         ${GOST_GRASSHOPPER_SOURCE_FILES}
171         ${GOST_EC_SOURCE_FILES}
172         ${GOST_OMAC_SOURCE_FILES}
173         )
174
175 set(GOST_ENGINE_SOURCE_FILES
176         ${GOST_CORE_SOURCE_FILES}
177         gost_ameth.c
178         gost_md.c
179         gost_md2012.c
180         gost_pmeth.c
181         gost_omac.c
182         gost_omac_acpkm.c
183         gost_gost2015.c
184         )
185
186 add_executable(test_digest test_digest.c)
187 target_link_libraries(test_digest gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
188 add_test(NAME digest
189         COMMAND test_digest)
190
191 add_executable(test_ciphers test_ciphers.c)
192 target_link_libraries(test_ciphers gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
193 add_test(NAME ciphers
194         COMMAND test_ciphers)
195
196 add_executable(test_curves test_curves.c)
197 target_link_libraries(test_curves gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
198 add_test(NAME curves
199         COMMAND test_curves)
200
201 add_executable(test_params test_params.c)
202 target_link_libraries(test_params gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
203 add_test(NAME parameters
204         COMMAND test_params)
205
206 add_executable(test_derive test_derive.c)
207 target_link_libraries(test_derive gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
208 add_test(NAME derive
209         COMMAND test_derive)
210
211 add_executable(test_sign test_sign.c)
212 target_link_libraries(test_sign gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
213 add_test(NAME sign/verify
214         COMMAND test_sign)
215
216 add_executable(test_tls test_tls.c)
217 target_link_libraries(test_tls gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY} ${OPENSSL_SSL_LIBRARY})
218 add_test(NAME TLS
219         COMMAND test_tls)
220
221 add_executable(test_context test_context.c)
222 target_link_libraries(test_context gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
223 add_test(NAME context
224         COMMAND test_context)
225
226 add_executable(test_keyexpimp test_keyexpimp.c)
227 #target_compile_definitions(test_keyexpimp PUBLIC -DOPENSSL_LOAD_CONF)
228 target_link_libraries(test_keyexpimp gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
229 add_test(NAME keyexpimp
230         COMMAND test_keyexpimp)
231
232 add_executable(test_gost89 test_gost89.c)
233 target_link_libraries(test_gost89 gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
234 add_test(NAME gost89
235         COMMAND test_gost89)
236
237 if(NOT SKIP_PERL_TESTS)
238     execute_process(COMMAND perl -MTest2::V0 -e ""
239         ERROR_QUIET RESULT_VARIABLE HAVE_TEST2_V0)
240     if(NOT HAVE_TEST2_V0)
241         add_test(NAME engine
242             COMMAND perl run_tests
243             WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test)
244         set_tests_properties(engine PROPERTIES ENVIRONMENT
245             "OPENSSL_ROOT_DIR=${OPENSSL_ROOT_DIR};OPENSSL_ENGINES=${OUTPUT_DIRECTORY};OPENSSL_CONF=${CMAKE_SOURCE_DIR}/test/empty.cnf")
246     else()
247       message(STATUS "No Test2::V0 perl module (engine tests skipped)")
248     endif()
249 endif()
250
251 add_executable(sign benchmark/sign.c)
252 target_link_libraries(sign gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY} ${CLOCK_GETTIME_LIB})
253
254 # All that may need to load just built engine will have path to it defined.
255 set(BINARY_TESTS_TARGETS
256         test_digest
257         test_ciphers
258         test_curves
259         test_params
260         test_derive
261         test_sign
262         test_context
263         test_keyexpimp
264         test_gost89
265         test_tls
266         )
267 set_property(TARGET ${BINARY_TESTS_TARGETS} APPEND PROPERTY COMPILE_DEFINITIONS ENGINE_DIR="${OUTPUT_DIRECTORY}")
268
269 add_library(gost_core STATIC ${GOST_LIB_SOURCE_FILES})
270 set_target_properties(gost_core PROPERTIES POSITION_INDEPENDENT_CODE ON)
271
272 add_library(gost_engine SHARED ${GOST_ENGINE_SOURCE_FILES})
273 set_target_properties(gost_engine PROPERTIES PREFIX "" OUTPUT_NAME "gost")
274 set_target_properties(gost_engine PROPERTIES VERSION ${GOST_SOVERSION} SOVERSION ${GOST_SOVERSION})
275 target_link_libraries(gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
276
277 set(GOST_SUM_SOURCE_FILES
278         gostsum.c
279         )
280
281 add_executable(gostsum ${GOST_SUM_SOURCE_FILES})
282 target_link_libraries(gostsum gost_core ${OPENSSL_CRYPTO_LIBRARY})
283
284 set(GOST_12_SUM_SOURCE_FILES
285         gost12sum.c
286         )
287
288 add_executable(gost12sum ${GOST_12_SUM_SOURCE_FILES})
289 target_link_libraries(gost12sum gost_core)
290
291 set_source_files_properties(tags PROPERTIES GENERATED true)
292 add_custom_target(tags
293     COMMAND ctags -R . ${OPENSSL_ROOT_DIR}
294     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
295
296 add_custom_target(tcl_tests
297     COMMAND ENGINE_DIR=${OUTPUT_DIRECTORY} sh ./runtest.sh
298     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tcl_tests)
299
300 add_executable(test_tlstree test_tlstree.c)
301 target_link_libraries(test_tlstree PUBLIC ${OPENSSL_CRYPTO_LIBRARY})
302
303 # install
304 set(OPENSSL_MAN_INSTALL_DIR ${CMAKE_INSTALL_MANDIR}/man1)
305
306 install(TARGETS gost_engine gostsum gost12sum EXPORT GostEngineConfig
307         LIBRARY  DESTINATION ${OPENSSL_ENGINES_DIR}
308         RUNTIME  DESTINATION ${CMAKE_INSTALL_BINDIR})
309 install(FILES gostsum.1 gost12sum.1 DESTINATION ${OPENSSL_MAN_INSTALL_DIR})
310 if (MSVC)
311  install(FILES $<TARGET_PDB_FILE:gost_engine> DESTINATION ${OPENSSL_ENGINES_DIR} OPTIONAL)
312  install(FILES $<TARGET_PDB_FILE:gostsum> $<TARGET_PDB_FILE:gost12sum> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
313 endif()