aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKartik Naik <kartik@bugqore.com>2026-08-03 17:06:47 +0530
committerKartik Naik <kartik@bugqore.com>2026-08-03 17:06:47 +0530
commit314bbeab844d35890726515d464acda282fcf066 (patch)
tree93c974af1b634bc240d04adf5045494a8efb0cb7
parent8430c1d169b83d5bfef4651e4bdacc8925dce2d1 (diff)
downloadportable-314bbeab844d35890726515d464acda282fcf066.tar.gz
portable-314bbeab844d35890726515d464acda282fcf066.tar.bz2
portable-314bbeab844d35890726515d464acda282fcf066.zip
cmake: use the builtin arc4random on weakly seeded platforms
-rw-r--r--CMakeLists.txt58
1 files changed, 58 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 98a0299..ac0ca31 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -21,6 +21,7 @@ endif()
21 21
22project(LibreSSL LANGUAGES C ASM) 22project(LibreSSL LANGUAGES C ASM)
23 23
24include(CheckCSourceCompiles)
24include(CheckFunctionExists) 25include(CheckFunctionExists)
25include(CheckSymbolExists) 26include(CheckSymbolExists)
26include(CheckLibraryExists) 27include(CheckLibraryExists)
@@ -307,7 +308,60 @@ if(HAVE_STRTONUM)
307 add_definitions(-DHAVE_STRTONUM) 308 add_definitions(-DHAVE_STRTONUM)
308endif() 309endif()
309 310
311#
312# arc4random on these platform versions falls back to a weak seed when it
313# cannot open /dev/random, so the builtin one is used there instead. Same
314# versions as the USE_BUILTIN_ARC4RANDOM checks in m4/check-os-options.m4.
315#
316set(USE_BUILTIN_ARC4RANDOM FALSE)
317if(APPLE)
318 # getentropy(2) arrived in 10.12 but is not tagged as introduced
319 # there, so the deployment target has to be tested directly.
320 check_c_source_compiles("
321 #include <AvailabilityMacros.h>
322 #include <unistd.h>
323 #include <sys/random.h>
324 #ifndef MAC_OS_X_VERSION_10_12
325 #define MAC_OS_X_VERSION_10_12 101200
326 #endif
327 #if defined(MAC_OS_X_VERSION_MIN_REQUIRED)
328 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12
329 #error \"Targeting Mac OS X 10.11 or earlier\"
330 #endif
331 #endif
332 int main(void) { char buf[1]; return getentropy(buf, 1); }"
333 HAVE_MACOS_GETENTROPY)
334 if(NOT HAVE_MACOS_GETENTROPY)
335 set(USE_BUILTIN_ARC4RANDOM TRUE)
336 endif()
337elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
338 check_c_source_compiles("
339 #include <sys/param.h>
340 #if __FreeBSD_version < 1200000
341 #error \"FreeBSD 11 or earlier\"
342 #endif
343 int main(void) { return 0; }"
344 HAVE_FREEBSD_ARC4RANDOM)
345 if(NOT HAVE_FREEBSD_ARC4RANDOM)
346 set(USE_BUILTIN_ARC4RANDOM TRUE)
347 endif()
348elseif(CMAKE_SYSTEM_NAME MATCHES "NetBSD")
349 check_c_source_compiles("
350 #include <sys/param.h>
351 #if __NetBSD_Version__ < 700000001
352 #error \"NetBSD 6 or earlier\"
353 #endif
354 int main(void) { return 0; }"
355 HAVE_NETBSD_ARC4RANDOM)
356 if(NOT HAVE_NETBSD_ARC4RANDOM)
357 set(USE_BUILTIN_ARC4RANDOM TRUE)
358 endif()
359endif()
360
310check_symbol_exists(arc4random_buf "stdlib.h" HAVE_ARC4RANDOM_BUF) 361check_symbol_exists(arc4random_buf "stdlib.h" HAVE_ARC4RANDOM_BUF)
362if(USE_BUILTIN_ARC4RANDOM)
363 set(HAVE_ARC4RANDOM_BUF FALSE)
364endif()
311if(HAVE_ARC4RANDOM_BUF) 365if(HAVE_ARC4RANDOM_BUF)
312 add_definitions(-DHAVE_ARC4RANDOM_BUF) 366 add_definitions(-DHAVE_ARC4RANDOM_BUF)
313endif() 367endif()
@@ -329,6 +383,10 @@ endif()
329 383
330# XXX macos fails to find getentropy with check_symbol_exists() 384# XXX macos fails to find getentropy with check_symbol_exists()
331check_function_exists(getentropy HAVE_GETENTROPY) 385check_function_exists(getentropy HAVE_GETENTROPY)
386if(APPLE AND NOT HAVE_MACOS_GETENTROPY)
387 # Weakly linked against the SDK, but absent at runtime before 10.12.
388 set(HAVE_GETENTROPY FALSE)
389endif()
332if(HAVE_GETENTROPY) 390if(HAVE_GETENTROPY)
333 add_definitions(-DHAVE_GETENTROPY) 391 add_definitions(-DHAVE_GETENTROPY)
334endif() 392endif()