From 5c704281767f8baa0a18de110bb4462549c16e49 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Fri, 1 Dec 2017 06:34:33 +0000 Subject: [libcxx] Support getentropy as a source of randomness for std::random_device Use this source use on Fuchsia where this is the oficially way to obtain randomness. This could be also used on other platforms that already support getentropy such as *BSD or Linux. Differential Revision: https://reviews.llvm.org/D40319 llvm-svn: 319523 --- libcxx/src/random.cpp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'libcxx/src') diff --git a/libcxx/src/random.cpp b/libcxx/src/random.cpp index eb2510a48c8..4a2468368d0 100644 --- a/libcxx/src/random.cpp +++ b/libcxx/src/random.cpp @@ -25,7 +25,9 @@ #include #include -#if defined(_LIBCPP_USING_DEV_RANDOM) +#if defined(_LIBCPP_USING_GETENTROPY) +#include +#elif defined(_LIBCPP_USING_DEV_RANDOM) #include #include #elif defined(_LIBCPP_USING_NACL_RANDOM) @@ -35,7 +37,30 @@ _LIBCPP_BEGIN_NAMESPACE_STD -#if defined(_LIBCPP_USING_ARC4_RANDOM) +#if defined(_LIBCPP_USING_GETENTROPY) + +random_device::random_device(const string& __token) +{ + if (__token != "/dev/urandom") + __throw_system_error(ENOENT, ("random device not supported " + __token).c_str()); +} + +random_device::~random_device() +{ +} + +unsigned +random_device::operator()() +{ + unsigned r; + size_t n = sizeof(r); + int err = getentropy(&r, n); + if (err) + __throw_system_error(errno, "random_device getentropy failed"); + return r; +} + +#elif defined(_LIBCPP_USING_ARC4_RANDOM) random_device::random_device(const string& __token) { -- cgit v1.2.3