xref: /freebsd/contrib/llvm-project/libcxx/src/random.cpp (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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/system_error.h>
17 #include <limits>
18 #include <random>
19 
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #if defined(_LIBCPP_USING_GETENTROPY)
25 #   include <sys/random.h>
26 #elif defined(_LIBCPP_USING_DEV_RANDOM)
27 #   include <fcntl.h>
28 #   include <unistd.h>
29 #   if __has_include(<sys/ioctl.h>) && __has_include(<linux/random.h>)
30 #       include <sys/ioctl.h>
31 #       include <linux/random.h>
32 #   endif
33 #elif defined(_LIBCPP_USING_NACL_RANDOM)
34 #   include <nacl/nacl_random.h>
35 #elif defined(_LIBCPP_USING_FUCHSIA_CPRNG)
36 #  include <zircon/syscalls.h>
37 #endif
38 
39 
40 _LIBCPP_BEGIN_NAMESPACE_STD
41 
42 #if defined(_LIBCPP_USING_GETENTROPY)
43 
44 random_device::random_device(const string& __token)
45 {
46     if (__token != "/dev/urandom")
47         __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
48 }
49 
50 random_device::~random_device()
51 {
52 }
53 
54 unsigned
55 random_device::operator()()
56 {
57     unsigned r;
58     size_t n = sizeof(r);
59     int err = getentropy(&r, n);
60     if (err)
61         __throw_system_error(errno, "random_device getentropy failed");
62     return r;
63 }
64 
65 #elif defined(_LIBCPP_USING_ARC4_RANDOM)
66 
67 random_device::random_device(const string&)
68 {
69 }
70 
71 random_device::~random_device()
72 {
73 }
74 
75 unsigned
76 random_device::operator()()
77 {
78     return arc4random();
79 }
80 
81 #elif defined(_LIBCPP_USING_DEV_RANDOM)
82 
83 random_device::random_device(const string& __token)
84     : __f_(open(__token.c_str(), O_RDONLY))
85 {
86     if (__f_ < 0)
87         __throw_system_error(errno, ("random_device failed to open " + __token).c_str());
88 }
89 
90 random_device::~random_device()
91 {
92     close(__f_);
93 }
94 
95 unsigned
96 random_device::operator()()
97 {
98     unsigned r;
99     size_t n = sizeof(r);
100     char* p = reinterpret_cast<char*>(&r);
101     while (n > 0)
102     {
103         ssize_t s = read(__f_, p, n);
104         if (s == 0)
105             __throw_system_error(ENODATA, "random_device got EOF");
106         if (s == -1)
107         {
108             if (errno != EINTR)
109                 __throw_system_error(errno, "random_device got an unexpected error");
110             continue;
111         }
112         n -= static_cast<size_t>(s);
113         p += static_cast<size_t>(s);
114     }
115     return r;
116 }
117 
118 #elif defined(_LIBCPP_USING_NACL_RANDOM)
119 
120 random_device::random_device(const string& __token)
121 {
122     if (__token != "/dev/urandom")
123         __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
124     int error = nacl_secure_random_init();
125     if (error)
126         __throw_system_error(error, ("random device failed to open " + __token).c_str());
127 }
128 
129 random_device::~random_device()
130 {
131 }
132 
133 unsigned
134 random_device::operator()()
135 {
136     unsigned r;
137     size_t n = sizeof(r);
138     size_t bytes_written;
139     int error = nacl_secure_random(&r, n, &bytes_written);
140     if (error != 0)
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");
144     return r;
145 }
146 
147 #elif defined(_LIBCPP_USING_WIN32_RANDOM)
148 
149 random_device::random_device(const string& __token)
150 {
151     if (__token != "/dev/urandom")
152         __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
153 }
154 
155 random_device::~random_device()
156 {
157 }
158 
159 unsigned
160 random_device::operator()()
161 {
162     unsigned r;
163     errno_t err = rand_s(&r);
164     if (err)
165         __throw_system_error(err, "random_device rand_s failed.");
166     return r;
167 }
168 
169 #elif defined(_LIBCPP_USING_FUCHSIA_CPRNG)
170 
171 random_device::random_device(const string& __token) {
172   if (__token != "/dev/urandom")
173     __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
174 }
175 
176 random_device::~random_device() {}
177 
178 unsigned random_device::operator()() {
179   // Implicitly link against the vDSO system call ABI without
180   // requiring the final link to specify -lzircon explicitly when
181   // statically linking libc++.
182 #  pragma comment(lib, "zircon")
183 
184   // The system call cannot fail.  It returns only when the bits are ready.
185   unsigned r;
186   _zx_cprng_draw(&r, sizeof(r));
187   return r;
188 }
189 
190 #else
191 #error "Random device not implemented for this architecture"
192 #endif
193 
194 double
195 random_device::entropy() const noexcept
196 {
197 #if defined(_LIBCPP_USING_DEV_RANDOM) && defined(RNDGETENTCNT)
198   int ent;
199   if (::ioctl(__f_, RNDGETENTCNT, &ent) < 0)
200     return 0;
201 
202   if (ent < 0)
203     return 0;
204 
205   if (ent > std::numeric_limits<result_type>::digits)
206     return std::numeric_limits<result_type>::digits;
207 
208   return ent;
209 #elif defined(_LIBCPP_USING_ARC4_RANDOM) || defined(_LIBCPP_USING_FUCHSIA_CPRNG)
210   return std::numeric_limits<result_type>::digits;
211 #else
212   return 0;
213 #endif
214 }
215 
216 _LIBCPP_END_NAMESPACE_STD
217