-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
310 lines (260 loc) · 10.3 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Copyright (c) (2018-2020) Apple Inc. All rights reserved.
#
# corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
# is contained in the License.txt file distributed with corecrypto) and only to
# people who accept that license. IMPORTANT: Any license rights granted to you by
# Apple Inc. (if any) are limited to internal use within your organization only on
# devices and computers you own or control, for the sole purpose of verifying the
# security characteristics and correct functioning of the Apple Software. You may
# not, directly or indirectly, redistribute the Apple Software or any portions thereof.
#
# CMake corecrypto build for Linux
#
# This CMake generates corecrypto_static library. It is meant to be
# used for Linux only.
#
cmake_minimum_required(VERSION 3.4.3)
set(CMAKE_OSX_SYSROOT "macosx.internal") # NOTE: This must be set before the call to project
project (corecrypto C)
option(CC_LINUX_ASM "Enable assembler support on Linux platform" OFF)
include (CoreCryptoSources.cmake)
#
# Build Macros and Targets
#
# get_include_dirs: extract include directories from list of headers
macro (get_include_dirs out in)
foreach (file ${in})
# Add directory including the header
get_filename_component(dir ${file} DIRECTORY)
list(APPEND ${out} ${dir})
# If the directory is corecrypto, we should also add its
# parent to the include dir.
get_filename_component(dirname ${dir} NAME)
if (${dirname} STREQUAL "corecrypto")
get_filename_component(parent ${dir} DIRECTORY)
list(APPEND ${out} ${parent})
endif()
endforeach()
endmacro()
# Project-level settings
## Build all objects with -fPIC
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
## CMake spelling of -std=gnu99
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_EXTENSIONS ON)
## Project-globals
set_property(DIRECTORY
APPEND PROPERTY COMPILE_DEFINITIONS
COMPILING_CORECRYPTO=1
$<$<CONFIG:Debug>:DEBUG=1>
$<$<CONFIG:Release>:NDEBUG>
)
set(CC_C_OPTIONS
-DBUILDKERNEL=0
-Wundef
-Wcast-qual
-Wno-error=deprecated-declarations
$<$<CONFIG:Debug>:-Werror>
)
add_compile_options(
"$<$<COMPILE_LANGUAGE:C>:${CC_C_OPTIONS}>"
)
# System dependencies
find_package(UnixCommands REQUIRED) # For ${BASH}
find_package(Threads REQUIRED)
find_library(MATH_LIBRARY m DOC "libm")
if(NOT MATH_LIBRARY)
message(SEND_ERROR "Could not find libm")
endif()
# Platform-specific dependencies
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
find_library(SYSTEM_FRAMEWORK NAMES System)
mark_as_advanced(SYSTEM_FRAMEWORK)
find_path(SYSTEM_CPU_CAPABILITIES_PATH i386/cpu_capabilities.h
HINTS "${SYSTEM_FRAMEWORK}/PrivateHeaders")
mark_as_advanced(SYSTEM_CPU_CAPABILITIES_PATH)
if(NOT SYSTEM_FRAMEWORK OR NOT SYSTEM_CPU_CAPABILITIES_PATH)
unset(SYSTEM_FRAMEWORK CACHE)
message(SEND_ERROR
"Could not find internal System.framework\n"
"HINT: Run cmake with xcrun to point it at the right SDK, or try:\n"
" ${CMAKE_COMMAND} -DCMAKE_OSX_SYSROOT=macosx.internal .")
else()
message("-- Found internal System.framework")
endif()
# Compile assembler sources in OSX
enable_language(ASM)
# Enable FIPS POST trace in OSX
set_source_files_properties(cc_fips/src/fipspost_trace.c cc_fips/crypto_test/crypto_test_cc_fips.c
PROPERTIES COMPILE_FLAGS -DCORECRYPTO_POST_TRACE=1)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
# Exclude sources that don't apply to Linux (or haven't yet been ported)
set (CORECRYPTO_EXCLUDE_SRCS
# exclude files that are OSX dependent
cc_fips/src/fipspost_get_cpu_key.c
cc_fips/src/fipspost_get_hmac.c
cckprng/src/cckprng_diag.c
cckprng/src/cckprng_diaggens.c
cckprng/src/cckprng_generate.c
cckprng/src/cckprng_init.c
cckprng/src/cckprng_initgen.c
cckprng/src/cckprng_loadseed.c
cckprng/src/cckprng_printdiag.c
cckprng/src/cckprng_ratchetseed.c
cckprng/src/cckprng_refresh.c
cckprng/src/cckprng_rekeygen.c
cckprng/src/cckprng_rekeygens.c
cckprng/src/cckprng_reseed.c
cckprng/src/cckprng_storeseed.c
cckprng/src/prng.c
)
set (CORECRYPTO_TEST_EXCLUDE_SRCS
# exclude files that are OSX dependent
cc_fips/src/fipspost_get_cpu_key.c
cc_fips/src/fipspost_get_hmac.c
corecrypto_test/lib/ccshadow.c
corecrypto_test/lib/cccycles.c
cckprng/crypto_test/crypto_test_kprng.c
# this test requires trace to be enabled
cc_fips/crypto_test/crypto_test_cc_fips.c
)
set (CORECRYPTO_PERF_EXCLUDE_SRCS
# exclude files that are OSX dependent
corecrypto_perf/src/ccperf_kprng.c
)
if (CC_LINUX_ASM)
enable_language(ASM)
# Add assembler specific clang flags
set (CC_ASM_OPTIONS
-integrated-as # Always use clang internal assembler
-x assembler-with-cpp # Run preprocessor despite .s name
)
add_compile_options(
"$<$<COMPILE_LANGUAGE:ASM>:${CC_ASM_OPTIONS}>"
)
# Enable Linux assembler in corecrypto
add_compile_options(
"-DCC_LINUX_ASM=1"
)
endif()
endif()
include(GNUInstallDirs)
if(NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
message(FATAL_ERROR "Only clang is supported for compilation, found ${CMAKE_C_COMPILER_ID} (${CMAKE_C_COMPILER})")
endif()
#
# corecrypto_static library target
#
# A few include dirs cannot be automatically generated by the above headers
# list. Manually fix it up.
set (CORECRYPTO_FIXED_INCLUDE_DIRS
ccaes/src/vng
cckprng
cckprng/corecrypto
corecrypto_test/include
acceleratecrypto/Include
acceleratecrypto/Header
ccec25519/src
)
# Find include dirs for corecrypto_static headers.
set (cc_include_dir ${CORECRYPTO_FIXED_INCLUDE_DIRS})
get_include_dirs (cc_include_dir "${CORECRYPTO_PROJECT_HDRS}")
get_include_dirs (cc_include_dir "${CORECRYPTO_PUBLIC_HDRS}")
get_include_dirs (cc_include_dir "${CORECRYPTO_PRIVATE_HDRS}")
list (REMOVE_DUPLICATES cc_include_dir)
# Filter out excluded sources
if(CORECRYPTO_EXCLUDE_SRCS)
list(REMOVE_ITEM CORECRYPTO_SRCS ${CORECRYPTO_EXCLUDE_SRCS})
endif()
# Create target for corecrypto_static
add_library(corecrypto_static STATIC ${CORECRYPTO_SRCS})
target_link_libraries(corecrypto_static
PRIVATE $<$<PLATFORM_ID:Darwin>:${SYSTEM_FRAMEWORK}> ${MATH_LIBRARY})
target_include_directories(corecrypto_static PRIVATE ${cc_include_dir})
set_property(TARGET corecrypto_static PROPERTY POSITION_INDEPENDENT_CODE ON)
# Generate pkgconfig for corecrypto_static
configure_file("corecrypto.pc.in" "corecrypto.pc" @ONLY)
# Install corecrypto_static
install (TARGETS corecrypto_static ARCHIVE
DESTINATION "${CMAKE_INSTALL_LIBDIR}")
install (FILES ${CORECRYPTO_PUBLIC_HDRS} ${CORECRYPTO_PRIVATE_HDRS}
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/corecrypto")
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/corecrypto.pc
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
#
# corecrypto_test target
#
# Remove the .inc and other non C files from the sources
foreach (file ${CORECRYPTO_TEST_SRCS})
string (REGEX MATCH ".+\\.c$" match ${file})
if (NOT match)
list (REMOVE_ITEM CORECRYPTO_TEST_SRCS ${file})
endif()
endforeach()
# A few include dirs cannot be automatically generated by the above headers
# list. Manually fix it up.
set (CORECRYPTO_TEST_FIXED_INCLUDE_DIRS
ccsha2/src
ccrng/src
ccec25519/src
ccaes/src/ios_hardware
corecrypto_test
cczp/src
)
# Find include dirs for corecrypto_test headers.
set (cctest_include_dir ${CORECRYPTO_TEST_FIXED_INCLUDE_DIRS})
get_include_dirs (cctest_include_dir "${CORECRYPTO_TEST_HDRS}")
get_include_dirs (cctest_include_dir "${CORECRYPTO_TEST_SRCS}")
list (REMOVE_DUPLICATES cctest_include_dir)
# Create target for corecrypto_test
if(CORECRYPTO_TEST_EXCLUDE_SRCS)
list (REMOVE_ITEM CORECRYPTO_TEST_SRCS ${CORECRYPTO_TEST_EXCLUDE_SRCS})
endif()
add_executable(corecrypto_test ${CORECRYPTO_TEST_SRCS})
target_compile_definitions(corecrypto_test PRIVATE CC_UNITTEST=1)
target_include_directories(corecrypto_test
PRIVATE ${cctest_include_dir} ${cc_include_dir})
target_link_libraries(corecrypto_test PRIVATE corecrypto_static
Threads::Threads ${MATH_LIBRARY} ${CMAKE_DL_LIBS})
# Generate test vectors
set(CC_CONVERT_TEST_VECTORS scripts/convert_testvectors.sh)
set(CC_TEST_VECTORS corecrypto_test/test_vectors/wycheproof/chacha20_poly1305_test.json)
set(GENERATED_TEST_VECTORS_DIR ${CMAKE_CURRENT_BINARY_DIR}/gen/corecrypto_test/include)
set(GENERATED_TEST_VECTORS ${GENERATED_TEST_VECTORS_DIR}/cc_generated_test_vectors.h
)
add_custom_command(
OUTPUT ${GENERATED_TEST_VECTORS}
COMMAND ${CMAKE_COMMAND} -E make_directory ${GENERATED_TEST_VECTORS_DIR}
COMMAND ${BASH} ${CMAKE_SOURCE_DIR}/${CC_CONVERT_TEST_VECTORS} ${GENERATED_TEST_VECTORS} ${CMAKE_CURRENT_SOURCE_DIR}/corecrypto_test/test_vectors/wycheproof
COMMENT "Generating test vectors"
DEPENDS ${CC_CONVERT_TEST_VECTORS} ${CC_TEST_VECTORS}
)
target_sources(corecrypto_test PRIVATE ${GENERATED_TEST_VECTORS})
target_include_directories(corecrypto_test PRIVATE ${GENERATED_TEST_VECTORS_DIR})
set(CC_CONVERT_TEST_VECTORS_PC scripts/convert_h2c_testvectors.py)
message(STATUS "Running python convert_h2c_testvectors.py")
execute_process(
COMMAND ${PYTHON} ${CMAKE_SOURCE_DIR}/${CC_CONVERT_TEST_VECTORS_PC} ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE RESULT_PC
OUTPUT_VARIABLE OUTPUT_PC
ERROR_VARIABLE ERROR_PC
)
message(STATUS "result convert_vectors: ${RESULT_PC}")
message(STATUS "output convert_vectors: ${OUTPUT_PC}")
message(STATUS "error convert_vectors: ${ERROR_PC}")
#
# corecrypto_perf target
#
# ccperf.h lives in corecrypto_perf/corecrypto. Add it up
set (CORECRYPTO_PERF_FIXED_INCLUDE_DIRS
corecrypto_perf/corecrypto
)
set (ccperf_include_dir ${CORECRYPTO_PERF_FIXED_INCLUDE_DIRS})
# Create target for corecrypto_perf
if(CORECRYPTO_PERF_EXCLUDE_SRCS)
list (REMOVE_ITEM CORECRYPTO_PERF_SRCS ${CORECRYPTO_PERF_EXCLUDE_SRCS})
endif()
add_executable(corecrypto_perf ${CORECRYPTO_PERF_SRCS})
target_include_directories(corecrypto_perf
PRIVATE ${ccperf_include_dir} ${cctest_include_dir} ${cc_include_dir})
target_link_libraries(corecrypto_perf PRIVATE corecrypto_static Threads::Threads ${MATH_LIBRARY})