Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===-------------------------- random.cpp --------------------------------===// |
| 2 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Saleem Abdulrasool | f46e049 | 2016-12-31 00:00:21 +0000 | [diff] [blame] | 9 | #include <__config> |
| 10 | |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 11 | #if defined(_LIBCPP_USING_WIN32_RANDOM) |
Marshall Clow | 653fd20 | 2013-10-09 21:49:03 +0000 | [diff] [blame] | 12 | // Must be defined before including stdlib.h to enable rand_s(). |
| 13 | #define _CRT_RAND_S |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 14 | #endif // defined(_LIBCPP_USING_WIN32_RANDOM) |
Marshall Clow | 653fd20 | 2013-10-09 21:49:03 +0000 | [diff] [blame] | 15 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 16 | #include "random" |
| 17 | #include "system_error" |
| 18 | |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 19 | #if defined(__sun__) |
David Chisnall | 8074c34 | 2012-02-29 13:05:08 +0000 | [diff] [blame] | 20 | #define rename solaris_headers_are_broken |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 21 | #endif // defined(__sun__) |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 22 | |
| 23 | #include <errno.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | |
Petr Hosek | 02834ef | 2017-12-01 06:34:33 +0000 | [diff] [blame] | 27 | #if defined(_LIBCPP_USING_GETENTROPY) |
| 28 | #include <sys/random.h> |
| 29 | #elif defined(_LIBCPP_USING_DEV_RANDOM) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
| 31 | #include <unistd.h> |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 32 | #elif defined(_LIBCPP_USING_NACL_RANDOM) |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 33 | #include <nacl/nacl_random.h> |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 36 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 37 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 38 | |
Petr Hosek | 02834ef | 2017-12-01 06:34:33 +0000 | [diff] [blame] | 39 | #if defined(_LIBCPP_USING_GETENTROPY) |
| 40 | |
| 41 | random_device::random_device(const string& __token) |
| 42 | { |
| 43 | if (__token != "/dev/urandom") |
| 44 | __throw_system_error(ENOENT, ("random device not supported " + __token).c_str()); |
| 45 | } |
| 46 | |
| 47 | random_device::~random_device() |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | unsigned |
| 52 | random_device::operator()() |
| 53 | { |
| 54 | unsigned r; |
| 55 | size_t n = sizeof(r); |
| 56 | int err = getentropy(&r, n); |
| 57 | if (err) |
| 58 | __throw_system_error(errno, "random_device getentropy failed"); |
| 59 | return r; |
| 60 | } |
| 61 | |
| 62 | #elif defined(_LIBCPP_USING_ARC4_RANDOM) |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 63 | |
| 64 | random_device::random_device(const string& __token) |
| 65 | { |
| 66 | if (__token != "/dev/urandom") |
| 67 | __throw_system_error(ENOENT, ("random device not supported " + __token).c_str()); |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | random_device::~random_device() |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | unsigned |
| 75 | random_device::operator()() |
| 76 | { |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 77 | return arc4random(); |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 80 | #elif defined(_LIBCPP_USING_DEV_RANDOM) |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 81 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 82 | random_device::random_device(const string& __token) |
| 83 | : __f_(open(__token.c_str(), O_RDONLY)) |
| 84 | { |
David Majnemer | 3230e1a | 2014-06-03 02:21:37 +0000 | [diff] [blame] | 85 | if (__f_ < 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 86 | __throw_system_error(errno, ("random_device failed to open " + __token).c_str()); |
| 87 | } |
| 88 | |
| 89 | random_device::~random_device() |
| 90 | { |
| 91 | close(__f_); |
| 92 | } |
| 93 | |
| 94 | unsigned |
| 95 | random_device::operator()() |
| 96 | { |
| 97 | unsigned r; |
David Majnemer | 89874f7 | 2014-06-03 02:40:39 +0000 | [diff] [blame] | 98 | size_t n = sizeof(r); |
| 99 | char* p = reinterpret_cast<char*>(&r); |
| 100 | while (n > 0) |
| 101 | { |
| 102 | ssize_t s = read(__f_, p, n); |
| 103 | if (s == 0) |
| 104 | __throw_system_error(ENODATA, "random_device got EOF"); |
| 105 | if (s == -1) |
| 106 | { |
| 107 | if (errno != EINTR) |
| 108 | __throw_system_error(errno, "random_device got an unexpected error"); |
| 109 | continue; |
| 110 | } |
| 111 | n -= static_cast<size_t>(s); |
| 112 | p += static_cast<size_t>(s); |
| 113 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 114 | return r; |
| 115 | } |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 116 | |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 117 | #elif defined(_LIBCPP_USING_NACL_RANDOM) |
| 118 | |
| 119 | random_device::random_device(const string& __token) |
| 120 | { |
| 121 | if (__token != "/dev/urandom") |
| 122 | __throw_system_error(ENOENT, ("random device not supported " + __token).c_str()); |
| 123 | int error = nacl_secure_random_init(); |
| 124 | if (error) |
| 125 | __throw_system_error(error, ("random device failed to open " + __token).c_str()); |
| 126 | } |
| 127 | |
| 128 | random_device::~random_device() |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | unsigned |
| 133 | random_device::operator()() |
| 134 | { |
| 135 | unsigned r; |
| 136 | size_t n = sizeof(r); |
| 137 | size_t bytes_written; |
| 138 | int error = nacl_secure_random(&r, n, &bytes_written); |
| 139 | if (error != 0) |
| 140 | __throw_system_error(error, "random_device failed getting bytes"); |
| 141 | else if (bytes_written != n) |
| 142 | __throw_runtime_error("random_device failed to obtain enough bytes"); |
| 143 | return r; |
| 144 | } |
| 145 | |
| 146 | #elif defined(_LIBCPP_USING_WIN32_RANDOM) |
| 147 | |
| 148 | random_device::random_device(const string& __token) |
| 149 | { |
| 150 | if (__token != "/dev/urandom") |
| 151 | __throw_system_error(ENOENT, ("random device not supported " + __token).c_str()); |
| 152 | } |
| 153 | |
| 154 | random_device::~random_device() |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | unsigned |
| 159 | random_device::operator()() |
| 160 | { |
| 161 | unsigned r; |
| 162 | errno_t err = rand_s(&r); |
| 163 | if (err) |
| 164 | __throw_system_error(err, "random_device rand_s failed."); |
| 165 | return r; |
| 166 | } |
| 167 | |
| 168 | #else |
| 169 | #error "Random device not implemented for this architecture" |
| 170 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 171 | |
| 172 | double |
Howard Hinnant | 6f926b1 | 2012-07-20 21:44:27 +0000 | [diff] [blame] | 173 | random_device::entropy() const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 174 | { |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | _LIBCPP_END_NAMESPACE_STD |