Skip to content

Commit b445a0d

Browse files
author
Steven Noonan
committed
CMake: add -DUSE_CRYPTO and -DUSE_CRYPTO25519 options
Signed-off-by: Steven Noonan <[email protected]>
1 parent c5721e1 commit b445a0d

File tree

3 files changed

+88
-34
lines changed

3 files changed

+88
-34
lines changed

.travis/build-cmake.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cmake_build() {
2020

2121
cleanup() {
2222
echo "Cleaning up CMake build directories" >&2
23-
rm -rf build-{a,ub,t}san build-cmake
23+
rm -rf build-{a,ub,t}san build-cmake build-cmake-ref
2424
}
2525

2626
trap cleanup EXIT
@@ -57,11 +57,14 @@ if [[ $BUILD_SANITIZERS -ne 0 ]]; then
5757
fi
5858
fi
5959

60-
cmake_configure build-cmake ${CMAKE_ARGS[@]} -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
61-
6260
# Build normal unsanitized binaries
61+
cmake_configure build-cmake ${CMAKE_ARGS[@]} -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
6362
cmake_build build-cmake
6463

64+
# Build binaries with reference ed25519/curve25519
65+
cmake_configure build-cmake-ref ${CMAKE_ARGS[@]} -DCMAKE_BUILD_TYPE=RelWithDebInfo -DUSE_CRYPTO25519=Reference ..
66+
cmake_build build-cmake-ref
67+
6568
# Build specific extended tests for code correctness validation
6669
if [[ $BUILD_SANITIZERS -ne 0 ]]; then
6770
cmake_build build-asan test_connection test_crypto
@@ -72,6 +75,7 @@ if [[ $BUILD_SANITIZERS -ne 0 ]]; then
7275
fi
7376

7477
# Run basic tests
78+
build-cmake-ref/tests/test_crypto
7579
build-cmake/tests/test_crypto
7680
build-cmake/tests/test_connection
7781

CMakeLists.txt

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ endif()
3030
option(Protobuf_USE_STATIC_LIBS "Build with a static Protobuf library" OFF)
3131
option(LIGHT_TESTS "Use smaller/shorter tests for simple integration testing (e.g. Travis)" OFF)
3232

33+
#
34+
# Primary crypto library (for AES, SHA256, etc)
35+
#
36+
set(useCryptoOptions OpenSSL BCrypt)
37+
set(USE_CRYPTO "OpenSSL" CACHE STRING "Crypto library to use for AES/SHA256")
38+
set_property(CACHE USE_CRYPTO PROPERTY STRINGS ${useCryptoOptions})
39+
40+
list(FIND useCryptoOptions ${USE_CRYPTO} useCryptoIndex)
41+
if(useCryptoIndex EQUAL -1)
42+
message(FATAL_ERROR "USE_CRYPTO must be one of: ${useCryptoOptions}")
43+
endif()
44+
if(USE_CRYPTO STREQUAL "BCrypt" AND NOT WIN32)
45+
message(FATAL_ERROR "USE_CRYPTO=\"BCrypt\" is only valid on Windows")
46+
endif()
47+
3348
if (WIN32)
3449
#
3550
# Strip compiler flags which conflict with ones we explicitly set. If we don't
@@ -45,14 +60,60 @@ if (WIN32)
4560
set(CMAKE_REQUIRED_LIBRARIES bcrypt)
4661
check_symbol_exists(BCryptEncrypt windows.h BCRYPT_AVAILABLE)
4762
cmake_pop_check_state()
48-
option(USE_BCRYPT "Use Windows BCrypt API for hashing and encryption" OFF)
49-
if (NOT BCRYPT_AVAILABLE AND USE_BCRYPT)
63+
if (NOT BCRYPT_AVAILABLE AND USE_CRYPTO STREQUAL "BCrypt")
5064
message(FATAL_ERROR "You're on Windows but BCrypt seems to be unavailable, you will need OpenSSL")
5165
endif()
5266
endif()
5367

68+
if (USE_CRYPTO STREQUAL "BCrypt")
69+
set(useCrypto25519Default "Reference")
70+
else()
71+
set(useCrypto25519Default "OpenSSL")
72+
endif()
73+
74+
#
75+
# Secondary crypto library (for ed25519/curve25519).
76+
#
77+
set(useCrypto25519Options OpenSSL Reference)
78+
set(USE_CRYPTO25519 "${useCrypto25519Default}" CACHE STRING "Crypto library to use for ed25519/curve25519")
79+
set_property(CACHE USE_CRYPTO25519 PROPERTY STRINGS ${useCrypto25519Options})
80+
81+
list(FIND useCrypto25519Options ${USE_CRYPTO25519} useCrypto25519Index)
82+
if(useCrypto25519Index EQUAL -1)
83+
message(FATAL_ERROR "USE_CRYPTO25519 must be one of: ${useCrypto25519Options}")
84+
endif()
85+
86+
if (USE_CRYPTO25519 STREQUAL "OpenSSL" OR USE_CRYPTO STREQUAL "OpenSSL")
87+
find_package(OpenSSL REQUIRED)
88+
89+
# Ensure the OpenSSL version is recent enough. We need a bunch of EVP
90+
# functionality.
91+
cmake_push_check_state()
92+
set(CMAKE_REQUIRED_LIBRARIES OpenSSL::Crypto)
93+
check_symbol_exists(EVP_MD_CTX_free openssl/evp.h OPENSSL_NEW_ENOUGH)
94+
if (NOT OPENSSL_NEW_ENOUGH)
95+
message(FATAL_ERROR "Your OpenSSL version appears to be too old. Check that you're using OpenSSL 1.1.0 or later.")
96+
endif()
97+
cmake_pop_check_state()
98+
cmake_push_check_state()
99+
set(CMAKE_REQUIRED_LIBRARIES OpenSSL::Crypto)
100+
if(USE_CRYPTO25519 STREQUAL "OpenSSL")
101+
check_symbol_exists(EVP_PKEY_get_raw_public_key openssl/evp.h OPENSSL_HAS_25519_RAW)
102+
endif()
103+
cmake_pop_check_state()
104+
endif()
105+
106+
if(USE_CRYPTO25519 STREQUAL "OpenSSL" AND NOT OPENSSL_HAS_25519_RAW)
107+
message(FATAL_ERROR "This version of OpenSSL does not support ed25519/curve25519. Please use -DUSE_CRYPTO25519=Reference or upgrade OpenSSL to 1.1.1 or later")
108+
endif()
109+
54110
add_subdirectory(examples)
55111
add_subdirectory(src)
56112
add_subdirectory(tests)
57113

114+
message(STATUS "---------------------------------------------------------")
115+
message(STATUS "Crypto library for AES/SHA256: ${USE_CRYPTO}")
116+
message(STATUS "Crypto library for ed25519/curve25519: ${USE_CRYPTO25519}")
117+
message(STATUS "---------------------------------------------------------")
118+
58119
# vim: set ts=4 sts=4 sw=4 noet:

src/CMakeLists.txt

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,6 @@ include(CheckSymbolExists)
44
include(CMakePushCheckState)
55

66
find_package(Protobuf REQUIRED)
7-
if (NOT USE_BCRYPT)
8-
find_package(OpenSSL REQUIRED)
9-
10-
# Ensure the OpenSSL version is recent enough. We need a bunch of EVP
11-
# functionality.
12-
cmake_push_check_state()
13-
set(CMAKE_REQUIRED_LIBRARIES OpenSSL::Crypto)
14-
check_symbol_exists(EVP_MD_CTX_free openssl/evp.h OPENSSL_NEW_ENOUGH)
15-
if (NOT OPENSSL_NEW_ENOUGH)
16-
message(FATAL_ERROR "Your OpenSSL version appears to be too old. Check that you're using OpenSSL 1.1.0 or later.")
17-
endif()
18-
cmake_pop_check_state()
19-
cmake_push_check_state()
20-
set(CMAKE_REQUIRED_LIBRARIES OpenSSL::Crypto)
21-
check_symbol_exists(EVP_PKEY_get_raw_public_key openssl/evp.h OPENSSL_HAS_25519_RAW)
22-
cmake_pop_check_state()
23-
endif()
247
find_package(Threads REQUIRED)
258

269
set(GNS_PROTOS
@@ -53,7 +36,7 @@ set(GNS_SRCS
5336
"vstdlib/strtools.cpp"
5437
)
5538

56-
if(USE_BCRYPT)
39+
if(USE_CRYPTO STREQUAL "BCrypt")
5740
set(GNS_CRYPTO_DEFINES ${GNS_CRYPTO_DEFINES} STEAMNETWORKINGSOCKETS_CRYPTO_BCRYPT ED25519_HASH_BCRYPT)
5841
set(GNS_SRCS ${GNS_SRCS}
5942
"common/crypto_bcrypt.cpp"
@@ -64,18 +47,18 @@ else()
6447
"common/crypto_openssl.cpp"
6548
"common/opensslwrapper.cpp"
6649
)
50+
endif()
6751

68-
# Use OpenSSL for 25519 if possible
69-
if(OPENSSL_HAS_25519_RAW)
70-
set(GNS_CRYPTO_DEFINES ${GNS_CRYPTO_DEFINES} STEAMNETWORKINGSOCKETS_CRYPTO_25519_OPENSSL)
71-
set(GNS_SRCS ${GNS_SRCS}
72-
"common/crypto_25519_openssl.cpp"
73-
)
74-
endif()
52+
# Use OpenSSL for 25519 if possible
53+
if(USE_CRYPTO25519 STREQUAL "OpenSSL")
54+
set(GNS_CRYPTO_DEFINES ${GNS_CRYPTO_DEFINES} STEAMNETWORKINGSOCKETS_CRYPTO_25519_OPENSSL)
55+
set(GNS_SRCS ${GNS_SRCS}
56+
"common/crypto_25519_openssl.cpp"
57+
)
7558
endif()
7659

7760
# Use reference 25519 crypto implementation?
78-
if(USE_BCRYPT OR NOT OPENSSL_HAS_25519_RAW)
61+
if(USE_CRYPTO25519 STREQUAL "Reference")
7962
set(GNS_CRYPTO_DEFINES ${GNS_CRYPTO_DEFINES} VALVE_CRYPTO_25519_DONNA)
8063
set(GNS_SRCS ${GNS_SRCS}
8164
"common/crypto_25519_donna.cpp"
@@ -98,14 +81,20 @@ set(GNS_COMMON_FLAGS
9881
-fvisibility=hidden
9982
-fno-strict-aliasing
10083
-Wall
101-
#-Wextra
10284
-Wno-unknown-pragmas
10385
-Wno-sign-compare
10486
-Wno-unused-local-typedef
10587
-Wno-unused-const-variable
10688
-Wno-nested-anon-types
10789
)
10890

91+
if(USE_CRYPTO25519 STREQUAL "Reference")
92+
# We don't use some of the 25519 functions with static linkage. Silence
93+
# -Wunused-function if we're including the reference ed25519/curve25519
94+
# stuff.
95+
set(GNS_COMMON_FLAGS ${GNS_COMMON_FLAGS} -Wno-unused-function)
96+
endif()
97+
10998
if(WERROR)
11099
set(GNS_COMMON_FLAGS
111100
${GNS_COMMON_FLAGS}
@@ -158,7 +147,7 @@ macro(gamenetworkingsockets_common GNS_TARGET)
158147
Threads::Threads
159148
)
160149

161-
if(NOT USE_BCRYPT)
150+
if(USE_CRYPTO STREQUAL "OpenSSL" OR USE_CRYPTO25519 STREQUAL "OpenSSL")
162151
target_link_libraries(${GNS_TARGET} PUBLIC
163152
OpenSSL::Crypto
164153
)
@@ -240,7 +229,7 @@ macro(gamenetworkingsockets_common GNS_TARGET)
240229
target_compile_options(${GNS_TARGET} PRIVATE -fno-stack-protector)
241230
endif()
242231
target_link_libraries(${GNS_TARGET} PUBLIC ws2_32 crypt32)
243-
if(USE_BCRYPT)
232+
if(USE_CRYPTO STREQUAL "BCrypt")
244233
target_link_libraries(${GNS_TARGET} PUBLIC bcrypt)
245234
endif()
246235
else()

0 commit comments

Comments
 (0)