-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCMakeLists.txt
174 lines (153 loc) · 5.93 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
##############################################################################
# Copyright 2022-2025 Leon Lynch
#
# This file is licensed under the terms of the MIT license.
# See LICENSE file.
##############################################################################
cmake_minimum_required(VERSION 3.16)
# NOTE: This is not intended to be a standalone project. It is intended to be
# an object library that can be added to other projects.
project(crypto
VERSION 0.1.9
DESCRIPTION "OpenEMV common crypto abstraction"
HOMEPAGE_URL "https://github.com/openemv/crypto"
LANGUAGES C
)
include(FindPackageHandleStandardArgs) # Provides find_package() messages
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
# Prefer MbedTLS and allow the FETCH_MBEDTLS option to download and build
# a local copy of MbedTLS for monolithic builds on platforms without package
# managers like Windows and MacOS.
option(FETCH_MBEDTLS "Download and build MbedTLS")
if(FETCH_MBEDTLS)
include(FetchMbedTLS)
else()
# MbedTLS version 3 and later provides a CMake config file
find_package(MbedTLS 3 CONFIG)
if(MbedTLS_FOUND)
set(MbedTLS_DEP_PARAMS "3 CONFIG")
# MbedTLS CMake config file does not print a message
find_package_handle_standard_args(MbedTLS CONFIG_MODE)
else()
# MbedTLS version 2 does not provide a CMake config file but this project
# provides FindMbedTLS.cmake to find it
find_package(MbedTLS 2.16)
if(MbedTLS_FOUND)
# Latest version available on Ubuntu 20.04 and Fedora 34
set(MbedTLS_DEP_PARAMS 2.16)
endif()
endif()
endif()
if(NOT MbedTLS_FOUND)
# Alternatively, try OpenSSL
set(OpenSSL_MIN_VERSION 1.1)
find_package(OpenSSL ${OpenSSL_MIN_VERSION} COMPONENTS Crypto)
endif()
# Choose crypto implementation and inform parent scope.
# The CRYPTO_PACKAGE_DEPENDENCIES variable is set for the parent scope to
# facilitate the generation of CMake package configuration files.
# The CRYPTO_PKGCONFIG_REQ_PRIV and CRYPTO_PKGCONFIG_LIBS_PRIV variables are
# set for the parent scope to facilitate the generation of pkg-config files.
# Note that these variables should only be set if the the platform's crypto
# libraries were used, but not for a local copy (like when MbedTLS is fetched).
if(MbedTLS_FOUND)
# Prefer MbedTLS
message(STATUS "Using MbedTLS ${MbedTLS_VERSION}")
set(USE_MBEDTLS TRUE)
if(NOT FETCH_MBEDTLS)
list(APPEND CRYPTO_PACKAGE_DEPENDENCIES "MbedTLS ${MbedTLS_DEP_PARAMS}")
set(CRYPTO_PACKAGE_DEPENDENCIES ${CRYPTO_PACKAGE_DEPENDENCIES} PARENT_SCOPE)
# NOTE: MbedTLS has no pkg-config file so CRYPTO_PKGCONFIG_REQ_PRIV cannot be set
set(CRYPTO_PKGCONFIG_LIBS_PRIV "-lmbedcrypto" PARENT_SCOPE)
endif()
elseif(OpenSSL_FOUND)
message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
set(USE_OPENSSL TRUE)
list(APPEND CRYPTO_PACKAGE_DEPENDENCIES "OpenSSL ${OpenSSL_MIN_VERSION} COMPONENTS Crypto")
set(CRYPTO_PACKAGE_DEPENDENCIES ${CRYPTO_PACKAGE_DEPENDENCIES} PARENT_SCOPE)
set(CRYPTO_PKGCONFIG_REQ_PRIV "libcrypto" PARENT_SCOPE)
set(CRYPTO_PKGCONFIG_LIBS_PRIV "-lcrypto" PARENT_SCOPE)
else()
message(FATAL_ERROR "Either MbedTLS or OpenSSL is required. Enable FETCH_MBEDTLS to download and build MbedTLS.")
endif()
# Memory helper library
add_library(crypto_mem OBJECT EXCLUDE_FROM_ALL)
target_sources(crypto_mem PRIVATE generic/crypto_mem.c)
target_include_directories(crypto_mem PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
# Random data helper library
add_library(crypto_rand OBJECT EXCLUDE_FROM_ALL)
target_include_directories(crypto_rand PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
# TDES crypto library
add_library(crypto_tdes OBJECT EXCLUDE_FROM_ALL)
target_include_directories(crypto_tdes PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
# AES crypto library
add_library(crypto_aes OBJECT EXCLUDE_FROM_ALL)
target_include_directories(crypto_aes PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
# HMAC crypto library
add_library(crypto_hmac OBJECT EXCLUDE_FROM_ALL)
target_include_directories(crypto_hmac PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
if(USE_MBEDTLS)
# Random data helper library
target_sources(crypto_rand PRIVATE mbedtls/crypto_rand.c)
target_link_libraries(crypto_rand PRIVATE MbedTLS::mbedcrypto)
# TDES crypto library
target_sources(crypto_tdes PRIVATE mbedtls/crypto_tdes.c generic/crypto_tdes_mac.c generic/crypto_tdes_kcv.c)
target_link_libraries(crypto_tdes PRIVATE MbedTLS::mbedcrypto)
# AES crypto library
target_sources(crypto_aes PRIVATE mbedtls/crypto_aes.c generic/crypto_aes_mac.c generic/crypto_aes_kcv.c)
target_link_libraries(crypto_aes PRIVATE MbedTLS::mbedcrypto)
# HMAC crypto library
target_sources(crypto_hmac PRIVATE mbedtls/crypto_hmac.c)
target_link_libraries(crypto_hmac PRIVATE MbedTLS::mbedcrypto)
elseif(USE_OPENSSL)
# Random data helper library
target_sources(crypto_rand PRIVATE openssl/crypto_rand.c)
target_link_libraries(crypto_rand PRIVATE OpenSSL::Crypto)
# TDES crypto library
target_sources(crypto_tdes PRIVATE openssl/crypto_tdes.c generic/crypto_tdes_mac.c generic/crypto_tdes_kcv.c)
target_link_libraries(crypto_tdes PRIVATE OpenSSL::Crypto)
# AES crypto library
target_sources(crypto_aes PRIVATE openssl/crypto_aes.c generic/crypto_aes_mac.c generic/crypto_aes_kcv.c)
target_link_libraries(crypto_aes PRIVATE OpenSSL::Crypto)
# HMAC crypto library
target_sources(crypto_hmac PRIVATE openssl/crypto_hmac.c)
target_link_libraries(crypto_hmac PRIVATE OpenSSL::Crypto)
endif()
# Configure various compilation properties
set_target_properties(
crypto_mem
crypto_rand
crypto_tdes
crypto_aes
crypto_hmac
PROPERTIES
C_STANDARD 11
C_EXTENSIONS OFF
C_VISIBILITY_PRESET hidden
)
if(BUILD_SHARED_LIBS)
set_target_properties(
crypto_mem
crypto_rand
crypto_tdes
crypto_aes
crypto_hmac
PROPERTIES
POSITION_INDEPENDENT_CODE True
)
endif()
# Only add tests if this is the top-level project
if(crypto_IS_TOP_LEVEL)
include(CTest)
add_subdirectory(test)
endif()