diff options
author | Petr Hosek <phosek@chromium.org> | 2017-12-01 06:34:33 +0000 |
---|---|---|
committer | Petr Hosek <phosek@chromium.org> | 2017-12-01 06:34:33 +0000 |
commit | 4861f6dfdd7cd6d36e81f89c2b548fc71de021d0 (patch) | |
tree | 5e9fbe4ce410955289fb5e15a0b6b0624e696656 /src | |
parent | aec189a8d17d840426dff40ad5ade0b532414014 (diff) | |
download | external_libcxx-4861f6dfdd7cd6d36e81f89c2b548fc71de021d0.tar.gz external_libcxx-4861f6dfdd7cd6d36e81f89c2b548fc71de021d0.tar.bz2 external_libcxx-4861f6dfdd7cd6d36e81f89c2b548fc71de021d0.zip |
[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
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@319523 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'src')
-rw-r--r-- | src/random.cpp | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/src/random.cpp b/src/random.cpp index eb2510a48..4a2468368 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -25,7 +25,9 @@ #include <stdio.h> #include <stdlib.h> -#if defined(_LIBCPP_USING_DEV_RANDOM) +#if defined(_LIBCPP_USING_GETENTROPY) +#include <sys/random.h> +#elif defined(_LIBCPP_USING_DEV_RANDOM) #include <fcntl.h> #include <unistd.h> #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) { |