1 cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2 project(gost-engine LANGUAGES C)
4 include(GNUInstallDirs)
5 include(CheckLibraryExists)
6 include(CheckFunctionExists)
7 include(CheckCSourceRuns)
11 find_package(OpenSSL 1.1.1 REQUIRED)
12 include_directories(${OPENSSL_INCLUDE_DIR})
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)
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)
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)
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)
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)
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)
43 include (TestBigEndian)
44 TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
46 message(STATUS "BIG_ENDIAN")
48 message(STATUS "LITTLE_ENDIAN")
49 add_definitions(-DL_ENDIAN)
56 # include <x86intrin.h>
59 unsigned long long x = -1, y = 1, r;
61 cf = _addcarry_u64(1, x, y, &r);
62 return !(cf == 1 && r == 1);
66 add_definitions(-DHAVE_ADDCARRY_U64)
69 set(BIN_DIRECTORY bin)
71 # Same soversion as OpenSSL
72 set(GOST_SOVERSION "${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}")
74 set(OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${BIN_DIRECTORY})
76 #set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY})
77 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY})
78 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIRECTORY})
80 # Remove when https://gitlab.kitware.com/cmake/cmake/issues/18525 is addressed
81 set(OPENSSL_ENGINES_DIR "" CACHE PATH "OpenSSL Engines Directory")
82 if ("${OPENSSL_ENGINES_DIR}" STREQUAL "")
83 include(FindPkgConfig)
84 pkg_get_variable(OPENSSL_ENGINES_DIR libcrypto enginesdir)
85 if ("${OPENSSL_ENGINES_DIR}" STREQUAL "")
86 message( FATAL_ERROR "Unable to discover the OpenSSL engines directory. Provide the path using -DOPENSSL_ENGINES_DIR" )
90 set(GOST_89_SOURCE_FILES
95 set(GOST_HASH_SOURCE_FILES
100 set(GOST_HASH_2012_SOURCE_FILES
104 gosthash2012_precalc.h
109 set(GOST_GRASSHOPPER_SOURCE_FILES
111 gost_grasshopper_core.h
112 gost_grasshopper_core.c
113 gost_grasshopper_defines.h
114 gost_grasshopper_defines.c
115 gost_grasshopper_math.h
116 gost_grasshopper_galois_precompiled.c
117 gost_grasshopper_precompiled.c
118 gost_grasshopper_cipher.h
119 gost_grasshopper_cipher.c
122 set(GOST_CORE_SOURCE_FILES
136 set(GOST_EC_SOURCE_FILES
141 set (GOST_OMAC_SOURCE_FILES
146 set(GOST_LIB_SOURCE_FILES
147 ${GOST_89_SOURCE_FILES}
148 ${GOST_HASH_SOURCE_FILES}
149 ${GOST_HASH_2012_SOURCE_FILES}
150 ${GOST_GRASSHOPPER_SOURCE_FILES}
151 ${GOST_EC_SOURCE_FILES}
152 ${GOST_OMAC_SOURCE_FILES}
155 set(GOST_ENGINE_SOURCE_FILES
156 ${GOST_CORE_SOURCE_FILES}
165 add_executable(test_digest test_digest.c)
166 target_link_libraries(test_digest gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
170 add_executable(test_curves test_curves.c)
171 target_link_libraries(test_curves gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
175 add_executable(test_params test_params.c)
176 target_link_libraries(test_params gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
177 add_test(NAME parameters
180 add_executable(test_sign test_sign.c)
181 target_link_libraries(test_sign gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
182 add_test(NAME sign/verify
185 add_executable(test_tls test_tls.c)
186 target_link_libraries(test_tls gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY} ${OPENSSL_SSL_LIBRARY})
190 add_executable(test_context test_context.c)
191 target_link_libraries(test_context gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
192 add_test(NAME context
193 COMMAND test_context)
195 add_executable(test_grasshopper test_grasshopper.c)
196 target_link_libraries(test_grasshopper gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
197 add_test(NAME grasshopper
198 COMMAND test_grasshopper)
200 add_executable(test_keyexpimp test_keyexpimp.c)
201 #target_compile_definitions(test_keyexpimp PUBLIC -DOPENSSL_LOAD_CONF)
202 target_link_libraries(test_keyexpimp gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
203 add_test(NAME keyexpimp
204 COMMAND test_keyexpimp)
206 add_executable(test_gost89 test_gost89.c)
207 target_link_libraries(test_gost89 gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
211 if(NOT SKIP_PERL_TESTS)
212 execute_process(COMMAND perl -MTest2::V0 -e ""
213 ERROR_QUIET RESULT_VARIABLE HAVE_TEST2_V0)
214 if(NOT HAVE_TEST2_V0)
216 COMMAND perl run_tests
217 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/test)
218 set_tests_properties(engine PROPERTIES ENVIRONMENT
219 "OPENSSL_ROOT_DIR=${OPENSSL_ROOT_DIR};OPENSSL_ENGINES=${OUTPUT_DIRECTORY};OPENSSL_CONF=${CMAKE_SOURCE_DIR}/test/empty.cnf")
221 message(STATUS "No Test2::V0 perl module (engine tests skipped)")
225 add_executable(sign benchmark/sign.c)
226 target_link_libraries(sign gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY} ${CLOCK_GETTIME_LIB})
228 # All that may need to load just built engine will have path to it defined.
229 set(BINARY_TESTS_TARGETS
240 set_property(TARGET ${BINARY_TESTS_TARGETS} APPEND PROPERTY COMPILE_DEFINITIONS ENGINE_DIR="${OUTPUT_DIRECTORY}")
242 add_library(gost_core STATIC ${GOST_LIB_SOURCE_FILES})
243 set_target_properties(gost_core PROPERTIES POSITION_INDEPENDENT_CODE ON)
245 add_library(gost_engine SHARED ${GOST_ENGINE_SOURCE_FILES})
246 set_target_properties(gost_engine PROPERTIES PREFIX "" OUTPUT_NAME "gost")
247 set_target_properties(gost_engine PROPERTIES VERSION ${GOST_SOVERSION} SOVERSION ${GOST_SOVERSION})
248 target_link_libraries(gost_engine gost_core ${OPENSSL_CRYPTO_LIBRARY})
250 set(GOST_SUM_SOURCE_FILES
254 add_executable(gostsum ${GOST_SUM_SOURCE_FILES})
255 target_link_libraries(gostsum gost_core ${OPENSSL_CRYPTO_LIBRARY})
257 set(GOST_12_SUM_SOURCE_FILES
261 add_executable(gost12sum ${GOST_12_SUM_SOURCE_FILES})
262 target_link_libraries(gost12sum gost_core)
264 set_source_files_properties(tags PROPERTIES GENERATED true)
265 add_custom_target(tags
266 COMMAND ctags -R . ${OPENSSL_ROOT_DIR}
267 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
269 add_custom_target(tcl_tests
270 COMMAND ENGINE_DIR=${OUTPUT_DIRECTORY} sh ./runtest.sh
271 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tcl_tests)
273 add_executable(test_tlstree test_tlstree.c)
274 target_link_libraries(test_tlstree PUBLIC ${OPENSSL_CRYPTO_LIBRARY})
277 set(OPENSSL_MAN_INSTALL_DIR ${CMAKE_INSTALL_MANDIR}/man1)
279 install(TARGETS gost_engine gostsum gost12sum EXPORT GostEngineConfig
280 LIBRARY DESTINATION ${OPENSSL_ENGINES_DIR}
281 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
282 install(FILES gostsum.1 gost12sum.1 DESTINATION ${OPENSSL_MAN_INSTALL_DIR})
284 install(FILES $<TARGET_PDB_FILE:gost_engine> DESTINATION ${OPENSSL_ENGINES_DIR} OPTIONAL)
285 install(FILES $<TARGET_PDB_FILE:gostsum> $<TARGET_PDB_FILE:gost12sum> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)