1 //===-------------------------- random.cpp --------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #if defined(_LIBCPP_USING_WIN32_RANDOM)
13 // Must be defined before including stdlib.h to enable rand_s().
15 #endif // defined(_LIBCPP_USING_WIN32_RANDOM)
18 #include "system_error"
21 #define rename solaris_headers_are_broken
22 #endif // defined(__sun__)
28 #if defined(_LIBCPP_USING_GETENTROPY)
29 #include <sys/random.h>
30 #elif defined(_LIBCPP_USING_DEV_RANDOM)
33 #elif defined(_LIBCPP_USING_NACL_RANDOM)
34 #include <nacl/nacl_random.h>
38 _LIBCPP_BEGIN_NAMESPACE_STD
40 #if defined(_LIBCPP_USING_GETENTROPY)
42 random_device::random_device(const string& __token)
44 if (__token != "/dev/urandom")
45 __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
48 random_device::~random_device()
53 random_device::operator()()
57 int err = getentropy(&r, n);
59 __throw_system_error(errno, "random_device getentropy failed");
63 #elif defined(_LIBCPP_USING_ARC4_RANDOM)
65 random_device::random_device(const string& __token)
67 if (__token != "/dev/urandom")
68 __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
71 random_device::~random_device()
76 random_device::operator()()
81 #elif defined(_LIBCPP_USING_DEV_RANDOM)
83 random_device::random_device(const string& __token)
84 : __f_(open(__token.c_str(), O_RDONLY))
87 __throw_system_error(errno, ("random_device failed to open " + __token).c_str());
90 random_device::~random_device()
96 random_device::operator()()
100 char* p = reinterpret_cast<char*>(&r);
103 ssize_t s = read(__f_, p, n);
105 __throw_system_error(ENODATA, "random_device got EOF");
109 __throw_system_error(errno, "random_device got an unexpected error");
112 n -= static_cast<size_t>(s);
113 p += static_cast<size_t>(s);
118 #elif defined(_LIBCPP_USING_NACL_RANDOM)
120 random_device::random_device(const string& __token)
122 if (__token != "/dev/urandom")
123 __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
124 int error = nacl_secure_random_init();
126 __throw_system_error(error, ("random device failed to open " + __token).c_str());
129 random_device::~random_device()
134 random_device::operator()()
137 size_t n = sizeof(r);
138 size_t bytes_written;
139 int error = nacl_secure_random(&r, n, &bytes_written);
141 __throw_system_error(error, "random_device failed getting bytes");
142 else if (bytes_written != n)
143 __throw_runtime_error("random_device failed to obtain enough bytes");
147 #elif defined(_LIBCPP_USING_WIN32_RANDOM)
149 random_device::random_device(const string& __token)
151 if (__token != "/dev/urandom")
152 __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
155 random_device::~random_device()
160 random_device::operator()()
163 errno_t err = rand_s(&r);
165 __throw_system_error(err, "random_device rand_s failed.");
170 #error "Random device not implemented for this architecture"
174 random_device::entropy() const _NOEXCEPT
179 _LIBCPP_END_NAMESPACE_STD