| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include <__config> |
| 10 | |
| 11 | #if defined(_LIBCPP_USING_WIN32_RANDOM) |
| 12 | // Must be defined before including stdlib.h to enable rand_s(). |
| 13 | # define _CRT_RAND_S |
| 14 | #endif // defined(_LIBCPP_USING_WIN32_RANDOM) |
| 15 | |
| 16 | #include <__system_error/throw_system_error.h> |
| 17 | #include <limits> |
| 18 | #include <random> |
| 19 | #include <string> |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | #if defined(_LIBCPP_USING_GETENTROPY) |
| 26 | # include <sys/random.h> |
| 27 | #elif defined(_LIBCPP_USING_DEV_RANDOM) |
| 28 | # include <fcntl.h> |
| 29 | # include <unistd.h> |
| 30 | # if __has_include(<sys/ioctl.h>) && __has_include(<linux/random.h>) |
| 31 | # include <linux/random.h> |
| 32 | # include <sys/ioctl.h> |
| 33 | # endif |
| 34 | #elif defined(_LIBCPP_USING_FUCHSIA_CPRNG) |
| 35 | # include <zircon/syscalls.h> |
| 36 | #endif |
| 37 | |
| 38 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 39 | _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS |
| 40 | |
| 41 | #if defined(_LIBCPP_USING_GETENTROPY) |
| 42 | |
| 43 | random_device::random_device(const string& __token) { |
| 44 | if (__token != "/dev/urandom" ) |
| 45 | std::__throw_system_error(ENOENT, ("random device not supported " + __token).c_str()); |
| 46 | } |
| 47 | |
| 48 | random_device::~random_device() {} |
| 49 | |
| 50 | unsigned random_device::operator()() { |
| 51 | unsigned r; |
| 52 | size_t n = sizeof(r); |
| 53 | int err = getentropy(&r, n); |
| 54 | if (err) |
| 55 | std::__throw_system_error(errno, "random_device getentropy failed" ); |
| 56 | return r; |
| 57 | } |
| 58 | |
| 59 | #elif defined(_LIBCPP_USING_ARC4_RANDOM) |
| 60 | |
| 61 | random_device::random_device(const string&) {} |
| 62 | |
| 63 | random_device::~random_device() {} |
| 64 | |
| 65 | unsigned random_device::operator()() { return arc4random(); } |
| 66 | |
| 67 | #elif defined(_LIBCPP_USING_DEV_RANDOM) |
| 68 | |
| 69 | random_device::random_device(const string& __token) : __f_(open(file: __token.c_str(), O_RDONLY)) { |
| 70 | if (__f_ < 0) |
| 71 | std::__throw_system_error(errno, what_arg: ("random_device failed to open " + __token).c_str()); |
| 72 | } |
| 73 | |
| 74 | random_device::~random_device() { close(fd: __f_); } |
| 75 | |
| 76 | unsigned random_device::operator()() { |
| 77 | unsigned r; |
| 78 | size_t n = sizeof(r); |
| 79 | char* p = reinterpret_cast<char*>(&r); |
| 80 | while (n > 0) { |
| 81 | ssize_t s = read(fd: __f_, buf: p, nbytes: n); |
| 82 | if (s == 0) |
| 83 | std::__throw_system_error(ENOMSG, what_arg: "random_device got EOF" ); |
| 84 | if (s == -1) { |
| 85 | if (errno != EINTR) |
| 86 | std::__throw_system_error(errno, what_arg: "random_device got an unexpected error" ); |
| 87 | continue; |
| 88 | } |
| 89 | n -= static_cast<size_t>(s); |
| 90 | p += static_cast<size_t>(s); |
| 91 | } |
| 92 | return r; |
| 93 | } |
| 94 | |
| 95 | #elif defined(_LIBCPP_USING_WIN32_RANDOM) |
| 96 | |
| 97 | random_device::random_device(const string& __token) { |
| 98 | if (__token != "/dev/urandom" ) |
| 99 | std::__throw_system_error(ENOENT, ("random device not supported " + __token).c_str()); |
| 100 | } |
| 101 | |
| 102 | random_device::~random_device() {} |
| 103 | |
| 104 | unsigned random_device::operator()() { |
| 105 | unsigned r; |
| 106 | errno_t err = rand_s(&r); |
| 107 | if (err) |
| 108 | std::__throw_system_error(err, "random_device rand_s failed." ); |
| 109 | return r; |
| 110 | } |
| 111 | |
| 112 | #elif defined(_LIBCPP_USING_FUCHSIA_CPRNG) |
| 113 | |
| 114 | random_device::random_device(const string& __token) { |
| 115 | if (__token != "/dev/urandom" ) |
| 116 | std::__throw_system_error(ENOENT, ("random device not supported " + __token).c_str()); |
| 117 | } |
| 118 | |
| 119 | random_device::~random_device() {} |
| 120 | |
| 121 | unsigned random_device::operator()() { |
| 122 | // Implicitly link against the vDSO system call ABI without |
| 123 | // requiring the final link to specify -lzircon explicitly when |
| 124 | // statically linking libc++. |
| 125 | # pragma comment(lib, "zircon") |
| 126 | |
| 127 | // The system call cannot fail. It returns only when the bits are ready. |
| 128 | unsigned r; |
| 129 | _zx_cprng_draw(&r, sizeof(r)); |
| 130 | return r; |
| 131 | } |
| 132 | |
| 133 | #else |
| 134 | # error "Random device not implemented for this architecture" |
| 135 | #endif |
| 136 | |
| 137 | double random_device::entropy() const noexcept { |
| 138 | #if defined(_LIBCPP_USING_DEV_RANDOM) && defined(RNDGETENTCNT) |
| 139 | int ent; |
| 140 | if (::ioctl(fd: __f_, RNDGETENTCNT, &ent) < 0) |
| 141 | return 0; |
| 142 | |
| 143 | if (ent < 0) |
| 144 | return 0; |
| 145 | |
| 146 | if (ent > std::numeric_limits<result_type>::digits) |
| 147 | return std::numeric_limits<result_type>::digits; |
| 148 | |
| 149 | return ent; |
| 150 | #elif defined(_LIBCPP_USING_ARC4_RANDOM) || defined(_LIBCPP_USING_FUCHSIA_CPRNG) |
| 151 | return std::numeric_limits<result_type>::digits; |
| 152 | #else |
| 153 | return 0; |
| 154 | #endif |
| 155 | } |
| 156 | |
| 157 | _LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS |
| 158 | _LIBCPP_END_NAMESPACE_STD |
| 159 | |