]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - CMakeLists.txt
Delete .travis.yml
[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 -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 -Wno-error=unknown-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 string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type)
26 if (build_type STREQUAL debug)
27  message(STATUS "Debug build")
28  add_compile_options(-ggdb)
29 endif()
30
31 if (ASAN)
32   message(STATUS "address sanitizer enabled")
33   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -g3 -fno-omit-frame-pointer")
34   set(SKIP_PERL_TESTS 1)
35 endif()
36
37 # DEPRECATEDIN_3_0 CMAC
38 set_source_files_properties(gost_omac.c PROPERTIES COMPILE_FLAGS -Wno-error=deprecated-declarations)
39 # DEPRECATEDIN_3_0 HMAC
40 set_source_files_properties(gost_keyexpimp.c PROPERTIES COMPILE_FLAGS -Wno-error=deprecated-declarations)
41
42 set(CMAKE_C_STANDARD 90)
43 CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME_C)
44 CHECK_LIBRARY_EXISTS(rt clock_gettime "" HAVE_CLOCK_GETTIME_RT)
45 if(HAVE_CLOCK_GETTIME_RT AND NOT HAVE_CLOCK_GETTIME_C)
46   set(CLOCK_GETTIME_LIB rt)
47 endif()
48
49 include (TestBigEndian)
50 TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
51 if(IS_BIG_ENDIAN)
52  message(STATUS "BIG_ENDIAN")
53 else()
54  message(STATUS "LITTLE_ENDIAN")
55  add_definitions(-DL_ENDIAN)
56 endif()
57
58 check_c_source_runs("
59   #ifdef _MSC_VER
60   # include <intrin.h>
61   #else
62   # include <x86intrin.h>
63   #endif
64   int main(void) {
65     unsigned long long x = -1, y = 1, r;
66     unsigned char cf;
67     cf = _addcarry_u64(1, x, y, &r);
68     return !(cf == 1 && r == 1);
69   }
70   " ADDCARRY_U64)
71 if (ADDCARRY_U64)
72   add_definitions(-DHAVE_ADDCARRY_U64)
73 endif()
74
75 check_c_source_runs("
76   int main(void) {
77     char buf[16] = { 0, 1, 2 };
78     int *p = buf + 1;
79     int *q = buf + 2;
80     return (*p == *q);
81   }
82   " RELAXED_ALIGNMENT)
83 if (NOT RELAXED_ALIGNMENT)
84   add_definitions(-DSTRICT_ALIGNMENT)
85 endif()
86
87 set(BIN_DIRECTORY bin)
88
89 # Same soversion as OpenSSL
90 set(GOST_SOVERSION "${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}")
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_asn1.c
144         gost_crypt.c
145         gost_ctl.c
146         gost_eng.c
147         gost_keywrap.c
148         gost_keywrap.h
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_89_SOURCE_FILES}
174         ${GOST_HASH_SOURCE_FILES}
175         ${GOST_HASH_2012_SOURCE_FILES}
176         ${GOST_GRASSHOPPER_SOURCE_FILES}
177         ${GOST_EC_SOURCE_FILES}
178         ${GOST_OMAC_SOURCE_FILES}
179         )
180
181 set(GOST_ENGINE_SOURCE_FILES
182         ${GOST_CORE_SOURCE_FILES}
183         gost_ameth.c
184         gost_md.c
185         gost_md2012.c
186         gost_pmeth.c
187         gost_omac.c
188         gost_omac_acpkm.c
189         gost_gost2015.c
190         )
191
192 add_executable(test_digest test_digest.c)
193 target_link_libraries(test_digest gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
194 add_test(NAME digest
195         COMMAND test_digest)
196
197 add_executable(test_ciphers test_ciphers.c)
198 target_link_libraries(test_ciphers gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
199 add_test(NAME ciphers
200         COMMAND test_ciphers)
201
202 add_executable(test_curves test_curves.c)
203 target_link_libraries(test_curves gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
204 add_test(NAME curves
205         COMMAND test_curves)
206
207 add_executable(test_params test_params.c)
208 target_link_libraries(test_params gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
209 add_test(NAME parameters
210         COMMAND test_params)
211
212 add_executable(test_derive test_derive.c)
213 target_link_libraries(test_derive gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
214 add_test(NAME derive
215         COMMAND test_derive)
216
217 add_executable(test_sign test_sign.c)
218 target_link_libraries(test_sign gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
219 add_test(NAME sign/verify
220         COMMAND test_sign)
221
222 add_executable(test_tls test_tls.c)
223 target_link_libraries(test_tls gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY} ${OPENSSL_SSL_LIBRARY})
224 add_test(NAME TLS
225         COMMAND test_tls)
226
227 add_executable(test_context test_context.c)
228 target_link_libraries(test_context gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
229 add_test(NAME context
230         COMMAND test_context)
231
232 add_executable(test_keyexpimp test_keyexpimp.c)
233 #target_compile_definitions(test_keyexpimp PUBLIC -DOPENSSL_LOAD_CONF)
234 target_link_libraries(test_keyexpimp gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
235 add_test(NAME keyexpimp
236         COMMAND test_keyexpimp)
237
238 add_executable(test_gost89 test_gost89.c)
239 target_link_libraries(test_gost89 gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
240 add_test(NAME gost89
241         COMMAND test_gost89)
242
243 add_executable(test_mgm test_mgm.c)
244 target_link_libraries(test_mgm gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
245 add_test(NAME mgm
246         COMMAND test_mgm)
247
248 if(NOT SKIP_PERL_TESTS)
249     execute_process(COMMAND perl -MTest2::V0 -e ""
250         ERROR_QUIET RESULT_VARIABLE HAVE_TEST2_V0)
251     if(NOT HAVE_TEST2_V0)
252         add_test(NAME engine
253             COMMAND perl run_tests
254             WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test)
255         set_tests_properties(engine PROPERTIES ENVIRONMENT
256             "OPENSSL_ROOT_DIR=${OPENSSL_ROOT_DIR};OPENSSL_ENGINES=${OUTPUT_DIRECTORY};OPENSSL_CONF=${CMAKE_SOURCE_DIR}/test/empty.cnf")
257     else()
258       message(STATUS "No Test2::V0 perl module (engine tests skipped)")
259     endif()
260 endif()
261
262 add_executable(sign benchmark/sign.c)
263 target_link_libraries(sign gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY} ${CLOCK_GETTIME_LIB})
264
265 # All that may need to load just built engine will have path to it defined.
266 set(BINARY_TESTS_TARGETS
267         test_digest
268         test_ciphers
269         test_curves
270         test_params
271         test_derive
272         test_sign
273         test_context
274         test_keyexpimp
275         test_gost89
276         test_tls
277         test_mgm
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 add_library(gost_engine SHARED ${GOST_ENGINE_SOURCE_FILES})
285 set_target_properties(gost_engine PROPERTIES PREFIX "" OUTPUT_NAME "gost")
286 set_target_properties(gost_engine PROPERTIES VERSION ${GOST_SOVERSION} SOVERSION ${GOST_SOVERSION})
287 target_link_libraries(gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
288
289 set(GOST_SUM_SOURCE_FILES
290         gostsum.c
291         )
292
293 add_executable(gostsum ${GOST_SUM_SOURCE_FILES})
294 target_link_libraries(gostsum gost_core ${OPENSSL_CRYPTO_LIBRARY})
295
296 set(GOST_12_SUM_SOURCE_FILES
297         gost12sum.c
298         )
299
300 add_executable(gost12sum ${GOST_12_SUM_SOURCE_FILES})
301 target_link_libraries(gost12sum gost_core)
302
303 set_source_files_properties(tags PROPERTIES GENERATED true)
304 add_custom_target(tags
305     COMMAND ctags -R . ${OPENSSL_ROOT_DIR}
306     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
307
308 add_custom_target(tcl_tests
309     COMMAND ENGINE_DIR=${OUTPUT_DIRECTORY} sh ./runtest.sh
310     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tcl_tests)
311
312 add_executable(test_tlstree test_tlstree.c)
313 target_link_libraries(test_tlstree PUBLIC ${OPENSSL_CRYPTO_LIBRARY})
314
315 # install
316 set(OPENSSL_MAN_INSTALL_DIR ${CMAKE_INSTALL_MANDIR}/man1)
317
318 install(TARGETS gost_engine gostsum gost12sum EXPORT GostEngineConfig
319         LIBRARY  DESTINATION ${OPENSSL_ENGINES_DIR}
320         RUNTIME  DESTINATION ${CMAKE_INSTALL_BINDIR})
321 install(FILES gostsum.1 gost12sum.1 DESTINATION ${OPENSSL_MAN_INSTALL_DIR})
322 if (MSVC)
323  install(FILES $<TARGET_PDB_FILE:gost_engine> DESTINATION ${OPENSSL_ENGINES_DIR} OPTIONAL)
324  install(FILES $<TARGET_PDB_FILE:gostsum> $<TARGET_PDB_FILE:gost12sum> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
325 endif()