Skip to content

Commit d31317c

Browse files
vitautmeta-codesync[bot]
authored andcommitted
Make libsodium optional
Summary: Fizz uses libsodium only for `X25519` and random bytes, which the OpenSSL it already requires provides (`EVP_PKEY_X25519` and `RAND_bytes`), yet every project that uses Fizz has to supply a second crypto library. Fizz's package config looks up Sodium without installing `FindSodium.cmake`, so Thrift installs one just for Fizz; Thrift's build compiles libsodium from source when it isn't installed but can't on Windows, where users have to install it separately; and the Fizz and Thrift getdeps workflows fetch and build it. Make libsodium optional: - `openssl::X25519KeyExchange` implements `X25519` with `EVP_PKEY_X25519`. `MultiBackendFactory` is the only place that picks the `X25519` implementation: libsodium's when Fizz is built with it, OpenSSL's otherwise. It uses it for `NamedGroup::x25519` and the hybrid post-quantum groups, and `MultiBackendFactory::makeX25519KeyExchange()` creates it from a private key for `FizzUtil`'s ECH keys and key pair generation. The tool's built-in ECH key and tests use `openssl::X25519KeyExchange`. - `fizz::random()` in `fizz/crypto/Random.h` fills a buffer with random bytes from libsodium, or from `RAND_bytes` in builds without it. `MultiBackendFactory::makeRandomBytes` and `RandomGenerator` use it, so neither depends on libsodium headers. `useMockRandom()` replaces its source through the test-only `detail::setRandomForTesting()` instead of libsodium's hook. - CMake builds Fizz with libsodium if it finds it, and only then compiles the libsodium backend, links libsodium, installs its headers and looks it up in the package config. `FindSodium` no longer defines a `sodium` target when it finds nothing, and on a platform or MSVC architecture it doesn't support, such as ARM64, it reports Sodium as not found instead of failing. Internal builds still take `X25519` in `MultiBackendFactory` and `FizzUtil`, and all random bytes, from libsodium. Reviewed By: mingtaoy Differential Revision: D129117795 fbshipit-source-id: 301002f13112f0791ae8873b29f2f5e99ce13e39
1 parent 96fbbc4 commit d31317c

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

‎build/fbcode_builder/CMake/FindSodium.cmake‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,16 @@ elseif (WIN32)
114114
)
115115
string(REGEX REPLACE ".*ARCH_VALUE ([a-zA-Z0-9_]+).*" "\\1" _TARGET_ARCH "${_COMPILATION_LOG}")
116116

117-
# construct library path
117+
# construct library path; on other architectures sodium is not found
118+
set(_PLATFORM_PATH "")
118119
if (_TARGET_ARCH STREQUAL "x86_32")
119-
string(APPEND _PLATFORM_PATH "Win32")
120+
set(_PLATFORM_PATH "Win32")
120121
elseif(_TARGET_ARCH STREQUAL "x86_64")
121-
string(APPEND _PLATFORM_PATH "x64")
122-
else()
123-
message(FATAL_ERROR "the ${_TARGET_ARCH} architecture is not supported by Findsodium.cmake.")
122+
set(_PLATFORM_PATH "x64")
124123
endif()
124+
endif()
125+
126+
if (MSVC AND _PLATFORM_PATH)
125127
string(APPEND _PLATFORM_PATH "/$$CONFIG$$")
126128

127129
if (MSVC_VERSION LESS 1900)
@@ -162,7 +164,7 @@ elseif (WIN32)
162164
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_BCK})
163165
endif()
164166

165-
elseif(_GCC_COMPATIBLE)
167+
elseif(NOT MSVC AND _GCC_COMPATIBLE)
166168
if (sodium_USE_STATIC_LIBS)
167169
find_library(sodium_LIBRARY_DEBUG libsodium.a
168170
HINTS ${sodium_DIR}
@@ -196,15 +198,8 @@ elseif (WIN32)
196198
PATH_SUFFIXES bin
197199
)
198200
endif()
199-
else()
200-
message(FATAL_ERROR "this platform is not supported by FindSodium.cmake")
201201
endif()
202202

203-
204-
########################################################################
205-
# unsupported
206-
else()
207-
message(FATAL_ERROR "this platform is not supported by FindSodium.cmake")
208203
endif()
209204

210205

@@ -248,6 +243,10 @@ if (WIN32)
248243
mark_as_advanced(sodium_DLL_RELEASE)
249244
endif()
250245

246+
if(NOT Sodium_FOUND)
247+
return()
248+
endif()
249+
251250
# create imported target
252251
if(sodium_USE_STATIC_LIBS)
253252
set(_LIB_TYPE STATIC)

0 commit comments

Comments
 (0)