@@ -16,6 +16,7 @@ if(MSVC AND MSVC_VERSION GREATER_EQUAL 1925 AND MSVC_VERSION LESS 1929)
1616endif ()
1717
1818set (BASE_HEADERS
19+ ROOT/RCryptoRandom.hxx
1920 ROOT/RFloat16.hxx
2021 ROOT/TErrorDefaultHandler.hxx
2122 ROOT/TExecutorCRTP.hxx
@@ -118,6 +119,7 @@ set(BASE_HEADERS
118119
119120set (BASE_SOURCES
120121 src/Match.cxx
122+ src/RCryptoRandom.cxx
121123 src/String .cxx
122124 src/Stringio.cxx
123125 src/TApplication.cxx
@@ -288,3 +290,70 @@ generateManual(rootclingMan ${CMAKE_SOURCE_DIR}/core/dictgen/src/rootcling-argpa
288290endif ()
289291
290292ROOT_ADD_TEST_SUBDIRECTORY (test )
293+
294+ if (UNIX )
295+ # Determine cryptographic random number generator
296+
297+ CHECK_CXX_SOURCE_COMPILES ("#include <cstdlib>
298+ int main() { char buf[32]; arc4random_buf(buf, 32); return 0;}" found_arc4 )
299+
300+ if (found_arc4)
301+ message (STATUS "Found arc4random_buf in stdlib.h" )
302+ target_compile_definitions (Core PRIVATE R__ARC4_STDLIB )
303+ else ()
304+ set (OLD_CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} )
305+ set (OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} )
306+ if (DEFINED LIBBSDROOT)
307+ set (CMAKE_REQUIRED_INCLUDES ${LIBBSDROOT} /include)
308+ set (CMAKE_REQUIRED_LIBRARIES ${LIBBSDROOT} /lib/libbsd.so)
309+ endif ()
310+ CHECK_CXX_SOURCE_COMPILES ("#include <bsd/stdlib.h>
311+ int main() { char buf[32]; arc4random_buf(buf, 32); return 0;}" found_arc4_bsd )
312+ set (CMAKE_REQUIRED_INCLUDES ${OLD_CMAKE_REQUIRED_INCLUDES} )
313+ set (CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES} )
314+ if (found_arc4_bsd)
315+ message (STATUS "Found arc4random_buf in bsd/stdlib.h" )
316+ target_compile_definitions (Core PRIVATE R__ARC4_BSDLIB )
317+ if (DEFINED LIBBSDROOT)
318+ target_include_directories (Core PRIVATE ${LIBBSDROOT} /include )
319+ target_link_libraries (Core PRIVATE ${LIBBSDROOT} /lib/libbsd.so )
320+ endif ()
321+ else ()
322+ CHECK_CXX_SOURCE_COMPILES ("#include <sys/random.h>
323+ int main() { char buf[32]; int res = getrandom(buf, 32, GRND_NONBLOCK); return 0;}" found_getrandom )
324+ if (found_getrandom)
325+ message (STATUS "Found getrandom in sys/random.h" )
326+ target_compile_definitions (Core PRIVATE R__GETRANDOM_CLIB )
327+ else ()
328+ CHECK_CXX_SOURCE_RUNS ("
329+ #include <fstream>
330+
331+ int main() {
332+ std::ifstream urandom{\" /dev/urandom\" };
333+ if (!urandom) {
334+ // This will make the CMake command fail
335+ return 1;
336+ }
337+
338+ constexpr int len{32};
339+ char buf[len];
340+ for (int n = 0; n < len; n++) buf[n] = 0;
341+ urandom.read(buf, len);
342+
343+ int nmatch = 0;
344+ for (int n = 0; n < len; n++)
345+ if (buf[n] == 0) nmatch++;
346+
347+ // Fail if no values have changed
348+ return nmatch != len ? 0 : 1;
349+ }" found_urandom )
350+ if (found_urandom)
351+ message (STATUS "Found random device in /dev/urandom" )
352+ target_compile_definitions (Core PRIVATE R__USE_URANDOM )
353+ else ()
354+ message (FATAL_ERROR "Fail to detect cryptographic random generator" )
355+ endif ()
356+ endif ()
357+ endif ()
358+ endif ()
359+ endif (UNIX )
0 commit comments