diff options
| author | Kartik Naik <kartik@bugqore.com> | 2026-08-03 17:06:47 +0530 |
|---|---|---|
| committer | Kartik Naik <kartik@bugqore.com> | 2026-08-03 17:06:47 +0530 |
| commit | 314bbeab844d35890726515d464acda282fcf066 (patch) | |
| tree | 93c974af1b634bc240d04adf5045494a8efb0cb7 | |
| parent | 8430c1d169b83d5bfef4651e4bdacc8925dce2d1 (diff) | |
| download | portable-314bbeab844d35890726515d464acda282fcf066.tar.gz portable-314bbeab844d35890726515d464acda282fcf066.tar.bz2 portable-314bbeab844d35890726515d464acda282fcf066.zip | |
cmake: use the builtin arc4random on weakly seeded platforms
| -rw-r--r-- | CMakeLists.txt | 58 |
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 | ||
| 22 | project(LibreSSL LANGUAGES C ASM) | 22 | project(LibreSSL LANGUAGES C ASM) |
| 23 | 23 | ||
| 24 | include(CheckCSourceCompiles) | ||
| 24 | include(CheckFunctionExists) | 25 | include(CheckFunctionExists) |
| 25 | include(CheckSymbolExists) | 26 | include(CheckSymbolExists) |
| 26 | include(CheckLibraryExists) | 27 | include(CheckLibraryExists) |
| @@ -307,7 +308,60 @@ if(HAVE_STRTONUM) | |||
| 307 | add_definitions(-DHAVE_STRTONUM) | 308 | add_definitions(-DHAVE_STRTONUM) |
| 308 | endif() | 309 | endif() |
| 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 | # | ||
| 316 | set(USE_BUILTIN_ARC4RANDOM FALSE) | ||
| 317 | if(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() | ||
| 337 | elseif(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() | ||
| 348 | elseif(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() | ||
| 359 | endif() | ||
| 360 | |||
| 310 | check_symbol_exists(arc4random_buf "stdlib.h" HAVE_ARC4RANDOM_BUF) | 361 | check_symbol_exists(arc4random_buf "stdlib.h" HAVE_ARC4RANDOM_BUF) |
| 362 | if(USE_BUILTIN_ARC4RANDOM) | ||
| 363 | set(HAVE_ARC4RANDOM_BUF FALSE) | ||
| 364 | endif() | ||
| 311 | if(HAVE_ARC4RANDOM_BUF) | 365 | if(HAVE_ARC4RANDOM_BUF) |
| 312 | add_definitions(-DHAVE_ARC4RANDOM_BUF) | 366 | add_definitions(-DHAVE_ARC4RANDOM_BUF) |
| 313 | endif() | 367 | endif() |
| @@ -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() |
| 331 | check_function_exists(getentropy HAVE_GETENTROPY) | 385 | check_function_exists(getentropy HAVE_GETENTROPY) |
| 386 | if(APPLE AND NOT HAVE_MACOS_GETENTROPY) | ||
| 387 | # Weakly linked against the SDK, but absent at runtime before 10.12. | ||
| 388 | set(HAVE_GETENTROPY FALSE) | ||
| 389 | endif() | ||
| 332 | if(HAVE_GETENTROPY) | 390 | if(HAVE_GETENTROPY) |
| 333 | add_definitions(-DHAVE_GETENTROPY) | 391 | add_definitions(-DHAVE_GETENTROPY) |
| 334 | endif() | 392 | endif() |
