Louis Dionne | 9bd9388 | 2021-11-17 16:25:01 -0500 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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) |
Louis Dionne | f4b7eae | 2021-12-21 11:27:19 -0500 | [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 | |
Arthur O'Dwyer | cf9bf39 | 2022-02-11 13:00:39 -0500 | [diff] [blame] | 16 | #include <limits> |
| 17 | #include <random> |
| 18 | #include <system_error> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 19 | |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 20 | #if defined(__sun__) |
Louis Dionne | f4b7eae | 2021-12-21 11:27:19 -0500 | [diff] [blame] | 21 | # define rename solaris_headers_are_broken |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 22 | #endif // defined(__sun__) |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 23 | |
| 24 | #include <errno.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | |
Petr Hosek | 02834ef | 2017-12-01 06:34:33 +0000 | [diff] [blame] | 28 | #if defined(_LIBCPP_USING_GETENTROPY) |
Louis Dionne | f4b7eae | 2021-12-21 11:27:19 -0500 | [diff] [blame] | 29 | # include <sys/random.h> |
Petr Hosek | 02834ef | 2017-12-01 06:34:33 +0000 | [diff] [blame] | 30 | #elif defined(_LIBCPP_USING_DEV_RANDOM) |
Louis Dionne | f4b7eae | 2021-12-21 11:27:19 -0500 | [diff] [blame] | 31 | # include <fcntl.h> |
| 32 | # include <unistd.h> |
| 33 | # if __has_include(<sys/ioctl.h>) && __has_include(<linux/random.h>) |
| 34 | # include <sys/ioctl.h> |
| 35 | # include <linux/random.h> |
| 36 | # endif |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 37 | #elif defined(_LIBCPP_USING_NACL_RANDOM) |
Louis Dionne | f4b7eae | 2021-12-21 11:27:19 -0500 | [diff] [blame] | 38 | # include <nacl/nacl_random.h> |
Roland McGrath | dea0448 | 2022-01-02 11:53:53 -0800 | [diff] [blame] | 39 | #elif defined(_LIBCPP_USING_FUCHSIA_CPRNG) |
| 40 | # include <zircon/syscalls.h> |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 43 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 44 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 45 | |
Petr Hosek | 02834ef | 2017-12-01 06:34:33 +0000 | [diff] [blame] | 46 | #if defined(_LIBCPP_USING_GETENTROPY) |
| 47 | |
| 48 | random_device::random_device(const string& __token) |
| 49 | { |
| 50 | if (__token != "/dev/urandom") |
| 51 | __throw_system_error(ENOENT, ("random device not supported " + __token).c_str()); |
| 52 | } |
| 53 | |
| 54 | random_device::~random_device() |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | unsigned |
| 59 | random_device::operator()() |
| 60 | { |
| 61 | unsigned r; |
| 62 | size_t n = sizeof(r); |
| 63 | int err = getentropy(&r, n); |
| 64 | if (err) |
| 65 | __throw_system_error(errno, "random_device getentropy failed"); |
| 66 | return r; |
| 67 | } |
| 68 | |
| 69 | #elif defined(_LIBCPP_USING_ARC4_RANDOM) |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 70 | |
Louis Dionne | 1eb0c9d | 2021-12-21 11:49:04 -0500 | [diff] [blame] | 71 | random_device::random_device(const string&) |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 72 | { |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | random_device::~random_device() |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | unsigned |
| 80 | random_device::operator()() |
| 81 | { |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 82 | return arc4random(); |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 85 | #elif defined(_LIBCPP_USING_DEV_RANDOM) |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 86 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 87 | random_device::random_device(const string& __token) |
| 88 | : __f_(open(__token.c_str(), O_RDONLY)) |
| 89 | { |
David Majnemer | 3230e1a | 2014-06-03 02:21:37 +0000 | [diff] [blame] | 90 | if (__f_ < 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 91 | __throw_system_error(errno, ("random_device failed to open " + __token).c_str()); |
| 92 | } |
| 93 | |
| 94 | random_device::~random_device() |
| 95 | { |
| 96 | close(__f_); |
| 97 | } |
| 98 | |
| 99 | unsigned |
| 100 | random_device::operator()() |
| 101 | { |
| 102 | unsigned r; |
David Majnemer | 89874f7 | 2014-06-03 02:40:39 +0000 | [diff] [blame] | 103 | size_t n = sizeof(r); |
| 104 | char* p = reinterpret_cast<char*>(&r); |
| 105 | while (n > 0) |
| 106 | { |
| 107 | ssize_t s = read(__f_, p, n); |
| 108 | if (s == 0) |
| 109 | __throw_system_error(ENODATA, "random_device got EOF"); |
| 110 | if (s == -1) |
| 111 | { |
| 112 | if (errno != EINTR) |
| 113 | __throw_system_error(errno, "random_device got an unexpected error"); |
| 114 | continue; |
| 115 | } |
| 116 | n -= static_cast<size_t>(s); |
| 117 | p += static_cast<size_t>(s); |
| 118 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 119 | return r; |
| 120 | } |
JF Bastien | d483203 | 2014-12-01 19:19:55 +0000 | [diff] [blame] | 121 | |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 122 | #elif defined(_LIBCPP_USING_NACL_RANDOM) |
| 123 | |
| 124 | random_device::random_device(const string& __token) |
| 125 | { |
| 126 | if (__token != "/dev/urandom") |
| 127 | __throw_system_error(ENOENT, ("random device not supported " + __token).c_str()); |
| 128 | int error = nacl_secure_random_init(); |
| 129 | if (error) |
| 130 | __throw_system_error(error, ("random device failed to open " + __token).c_str()); |
| 131 | } |
| 132 | |
| 133 | random_device::~random_device() |
| 134 | { |
| 135 | } |
| 136 | |
| 137 | unsigned |
| 138 | random_device::operator()() |
| 139 | { |
| 140 | unsigned r; |
| 141 | size_t n = sizeof(r); |
| 142 | size_t bytes_written; |
| 143 | int error = nacl_secure_random(&r, n, &bytes_written); |
| 144 | if (error != 0) |
| 145 | __throw_system_error(error, "random_device failed getting bytes"); |
| 146 | else if (bytes_written != n) |
| 147 | __throw_runtime_error("random_device failed to obtain enough bytes"); |
| 148 | return r; |
| 149 | } |
| 150 | |
| 151 | #elif defined(_LIBCPP_USING_WIN32_RANDOM) |
| 152 | |
| 153 | random_device::random_device(const string& __token) |
| 154 | { |
| 155 | if (__token != "/dev/urandom") |
| 156 | __throw_system_error(ENOENT, ("random device not supported " + __token).c_str()); |
| 157 | } |
| 158 | |
| 159 | random_device::~random_device() |
| 160 | { |
| 161 | } |
| 162 | |
| 163 | unsigned |
| 164 | random_device::operator()() |
| 165 | { |
| 166 | unsigned r; |
| 167 | errno_t err = rand_s(&r); |
| 168 | if (err) |
| 169 | __throw_system_error(err, "random_device rand_s failed."); |
| 170 | return r; |
| 171 | } |
| 172 | |
Roland McGrath | dea0448 | 2022-01-02 11:53:53 -0800 | [diff] [blame] | 173 | #elif defined(_LIBCPP_USING_FUCHSIA_CPRNG) |
| 174 | |
| 175 | random_device::random_device(const string& __token) { |
| 176 | if (__token != "/dev/urandom") |
| 177 | __throw_system_error(ENOENT, ("random device not supported " + __token).c_str()); |
| 178 | } |
| 179 | |
| 180 | random_device::~random_device() {} |
| 181 | |
| 182 | unsigned random_device::operator()() { |
| 183 | // Implicitly link against the vDSO system call ABI without |
| 184 | // requiring the final link to specify -lzircon explicitly when |
| 185 | // statically linking libc++. |
| 186 | # pragma comment(lib, "zircon") |
| 187 | |
| 188 | // The system call cannot fail. It returns only when the bits are ready. |
| 189 | unsigned r; |
| 190 | _zx_cprng_draw(&r, sizeof(r)); |
| 191 | return r; |
| 192 | } |
| 193 | |
Ed Schouten | dcf3740 | 2015-03-10 07:46:06 +0000 | [diff] [blame] | 194 | #else |
| 195 | #error "Random device not implemented for this architecture" |
| 196 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 197 | |
| 198 | double |
Louis Dionne | 65358e1 | 2021-03-01 12:09:45 -0500 | [diff] [blame] | 199 | random_device::entropy() const noexcept |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 200 | { |
Marek Kurdej | 66fcf01 | 2021-01-21 17:55:19 +0100 | [diff] [blame] | 201 | #if defined(_LIBCPP_USING_DEV_RANDOM) && defined(RNDGETENTCNT) |
| 202 | int ent; |
| 203 | if (::ioctl(__f_, RNDGETENTCNT, &ent) < 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 204 | return 0; |
Marek Kurdej | 66fcf01 | 2021-01-21 17:55:19 +0100 | [diff] [blame] | 205 | |
| 206 | if (ent < 0) |
| 207 | return 0; |
| 208 | |
| 209 | if (ent > std::numeric_limits<result_type>::digits) |
| 210 | return std::numeric_limits<result_type>::digits; |
| 211 | |
| 212 | return ent; |
Roland McGrath | dea0448 | 2022-01-02 11:53:53 -0800 | [diff] [blame] | 213 | #elif defined(__OpenBSD__) || defined(_LIBCPP_USING_FUCHSIA_CPRNG) |
Brad Smith | 616b1e1 | 2021-01-25 20:55:09 -0500 | [diff] [blame] | 214 | return std::numeric_limits<result_type>::digits; |
Marek Kurdej | 66fcf01 | 2021-01-21 17:55:19 +0100 | [diff] [blame] | 215 | #else |
| 216 | return 0; |
| 217 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | _LIBCPP_END_NAMESPACE_STD |