1 //===-------------------------- random.cpp --------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #if defined(_WIN32)
11 // Must be defined before including stdlib.h to enable rand_s().
12 #define _CRT_RAND_S
13 #include <stdio.h>
14 #endif // defined(_WIN32)
15
16 #include "random"
17 #include "system_error"
18
19 #if defined(__sun__)
20 #define rename solaris_headers_are_broken
21 #endif // defined(__sun__)
22 #if !defined(_WIN32)
23 #include <fcntl.h>
24 #include <unistd.h>
25 #endif // !defined(_WIN32)
26 #include <errno.h>
27 #if defined(_LIBCPP_USING_NACL_RANDOM)
28 #include <nacl/nacl_random.h>
29 #endif // defined(_LIBCPP_USING_NACL_RANDOM)
30
31 _LIBCPP_BEGIN_NAMESPACE_STD
32
33 #if defined(_WIN32)
34
random_device(const string &)35 random_device::random_device(const string&)
36 {
37 }
38
~random_device()39 random_device::~random_device()
40 {
41 }
42
43 unsigned
operator ()()44 random_device::operator()()
45 {
46 unsigned r;
47 errno_t err = rand_s(&r);
48 if (err)
49 __throw_system_error(err, "random_device rand_s failed.");
50 return r;
51 }
52
53 #elif defined(_LIBCPP_USING_NACL_RANDOM)
54
55 random_device::random_device(const string& __token)
56 {
57 if (__token != "/dev/urandom")
58 __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
59 int error = nacl_secure_random_init();
60 if (error)
61 __throw_system_error(error, ("random device failed to open " + __token).c_str());
62 }
63
64 random_device::~random_device()
65 {
66 }
67
68 unsigned
69 random_device::operator()()
70 {
71 unsigned r;
72 size_t n = sizeof(r);
73 size_t bytes_written;
74 int error = nacl_secure_random(&r, n, &bytes_written);
75 if (error != 0)
76 __throw_system_error(error, "random_device failed getting bytes");
77 else if (bytes_written != n)
78 __throw_runtime_error("random_device failed to obtain enough bytes");
79 return r;
80 }
81
82 #else // !defined(_WIN32) && !defined(_LIBCPP_USING_NACL_RANDOM)
83
84 random_device::random_device(const string& __token)
85 : __f_(open(__token.c_str(), O_RDONLY))
86 {
87 if (__f_ < 0)
88 __throw_system_error(errno, ("random_device failed to open " + __token).c_str());
89 }
90
91 random_device::~random_device()
92 {
93 close(__f_);
94 }
95
96 unsigned
97 random_device::operator()()
98 {
99 unsigned r;
100 size_t n = sizeof(r);
101 char* p = reinterpret_cast<char*>(&r);
102 while (n > 0)
103 {
104 ssize_t s = read(__f_, p, n);
105 if (s == 0)
106 __throw_system_error(ENODATA, "random_device got EOF");
107 if (s == -1)
108 {
109 if (errno != EINTR)
110 __throw_system_error(errno, "random_device got an unexpected error");
111 continue;
112 }
113 n -= static_cast<size_t>(s);
114 p += static_cast<size_t>(s);
115 }
116 return r;
117 }
118
119 #endif // defined(_WIN32) || defined(_LIBCPP_USING_NACL_RANDOM)
120
121 double
entropy() const122 random_device::entropy() const _NOEXCEPT
123 {
124 return 0;
125 }
126
127 _LIBCPP_END_NAMESPACE_STD
128