blob: 4ab424eaa6e58563c1d1abd36c58c31ad17c2afc [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===-------------------------- random.cpp --------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
Ed Schouten63e70b62015-03-10 07:46:06 +000010#if defined(_LIBCPP_USING_WIN32_RANDOM)
Marshall Clowb8dd5ca2013-10-09 21:49:03 +000011// Must be defined before including stdlib.h to enable rand_s().
12#define _CRT_RAND_S
Ed Schouten63e70b62015-03-10 07:46:06 +000013#endif // defined(_LIBCPP_USING_WIN32_RANDOM)
Marshall Clowb8dd5ca2013-10-09 21:49:03 +000014
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000015#include "random"
16#include "system_error"
17
JF Bastien2bd5ffd2014-12-01 19:19:55 +000018#if defined(__sun__)
David Chisnall997e4542012-02-29 13:05:08 +000019#define rename solaris_headers_are_broken
JF Bastien2bd5ffd2014-12-01 19:19:55 +000020#endif // defined(__sun__)
Ed Schouten63e70b62015-03-10 07:46:06 +000021
22#include <errno.h>
23#include <stdio.h>
24#include <stdlib.h>
25
26#if defined(_LIBCPP_USING_DEV_RANDOM)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000027#include <fcntl.h>
28#include <unistd.h>
Ed Schouten63e70b62015-03-10 07:46:06 +000029#elif defined(_LIBCPP_USING_NACL_RANDOM)
JF Bastien2bd5ffd2014-12-01 19:19:55 +000030#include <nacl/nacl_random.h>
Ed Schouten63e70b62015-03-10 07:46:06 +000031#endif
32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000033
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034_LIBCPP_BEGIN_NAMESPACE_STD
35
Ed Schouten63e70b62015-03-10 07:46:06 +000036#if defined(_LIBCPP_USING_ARC4_RANDOM)
JF Bastien2bd5ffd2014-12-01 19:19:55 +000037
38random_device::random_device(const string& __token)
39{
40 if (__token != "/dev/urandom")
41 __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
JF Bastien2bd5ffd2014-12-01 19:19:55 +000042}
43
44random_device::~random_device()
45{
46}
47
48unsigned
49random_device::operator()()
50{
Ed Schouten63e70b62015-03-10 07:46:06 +000051 return arc4random();
JF Bastien2bd5ffd2014-12-01 19:19:55 +000052}
53
Ed Schouten63e70b62015-03-10 07:46:06 +000054#elif defined(_LIBCPP_USING_DEV_RANDOM)
JF Bastien2bd5ffd2014-12-01 19:19:55 +000055
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056random_device::random_device(const string& __token)
57 : __f_(open(__token.c_str(), O_RDONLY))
58{
David Majnemer8db32cc2014-06-03 02:21:37 +000059 if (__f_ < 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000060 __throw_system_error(errno, ("random_device failed to open " + __token).c_str());
61}
62
63random_device::~random_device()
64{
65 close(__f_);
66}
67
68unsigned
69random_device::operator()()
70{
71 unsigned r;
David Majnemer4d9f97b2014-06-03 02:40:39 +000072 size_t n = sizeof(r);
73 char* p = reinterpret_cast<char*>(&r);
74 while (n > 0)
75 {
76 ssize_t s = read(__f_, p, n);
77 if (s == 0)
78 __throw_system_error(ENODATA, "random_device got EOF");
79 if (s == -1)
80 {
81 if (errno != EINTR)
82 __throw_system_error(errno, "random_device got an unexpected error");
83 continue;
84 }
85 n -= static_cast<size_t>(s);
86 p += static_cast<size_t>(s);
87 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088 return r;
89}
JF Bastien2bd5ffd2014-12-01 19:19:55 +000090
Ed Schouten63e70b62015-03-10 07:46:06 +000091#elif defined(_LIBCPP_USING_NACL_RANDOM)
92
93random_device::random_device(const string& __token)
94{
95 if (__token != "/dev/urandom")
96 __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
97 int error = nacl_secure_random_init();
98 if (error)
99 __throw_system_error(error, ("random device failed to open " + __token).c_str());
100}
101
102random_device::~random_device()
103{
104}
105
106unsigned
107random_device::operator()()
108{
109 unsigned r;
110 size_t n = sizeof(r);
111 size_t bytes_written;
112 int error = nacl_secure_random(&r, n, &bytes_written);
113 if (error != 0)
114 __throw_system_error(error, "random_device failed getting bytes");
115 else if (bytes_written != n)
116 __throw_runtime_error("random_device failed to obtain enough bytes");
117 return r;
118}
119
120#elif defined(_LIBCPP_USING_WIN32_RANDOM)
121
122random_device::random_device(const string& __token)
123{
124 if (__token != "/dev/urandom")
125 __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
126}
127
128random_device::~random_device()
129{
130}
131
132unsigned
133random_device::operator()()
134{
135 unsigned r;
136 errno_t err = rand_s(&r);
137 if (err)
138 __throw_system_error(err, "random_device rand_s failed.");
139 return r;
140}
141
142#else
143#error "Random device not implemented for this architecture"
144#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000145
146double
Howard Hinnantc83960a2012-07-20 21:44:27 +0000147random_device::entropy() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000148{
149 return 0;
150}
151
152_LIBCPP_END_NAMESPACE_STD