summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>2019-03-22 12:39:43 +0000
committerMarc Espie <espie@cvs.openbsd.org>2019-03-22 12:39:43 +0000
commit7d8cb6dd6899243e9cf862f004e150b134b5ecfe (patch)
treebd5b3ffc54b3edf1f7d5cd75a123f11fc74328fe /lib
parent6faba1243a23c866b9b2ae908b99ad894902a1e8 (diff)
that "device" is an abstraction to the underlying OS PRNG.
In OpenBSD's case, we know it has optimal entropy properties, so just say so. okay mikeb@, "why not" deraadt@
Diffstat (limited to 'lib')
-rw-r--r--lib/libcxx/src/random.cpp36
1 files changed, 34 insertions, 2 deletions
diff --git a/lib/libcxx/src/random.cpp b/lib/libcxx/src/random.cpp
index 4ab424eaa6e..9efac7a66a7 100644
--- a/lib/libcxx/src/random.cpp
+++ b/lib/libcxx/src/random.cpp
@@ -7,6 +7,8 @@
//
//===----------------------------------------------------------------------===//
+#include <__config>
+
#if defined(_LIBCPP_USING_WIN32_RANDOM)
// Must be defined before including stdlib.h to enable rand_s().
#define _CRT_RAND_S
@@ -23,17 +25,43 @@
#include <stdio.h>
#include <stdlib.h>
-#if defined(_LIBCPP_USING_DEV_RANDOM)
+#if defined(_LIBCPP_USING_GETENTROPY)
+#include <sys/random.h>
+#elif defined(_LIBCPP_USING_DEV_RANDOM)
#include <fcntl.h>
#include <unistd.h>
#elif defined(_LIBCPP_USING_NACL_RANDOM)
#include <nacl/nacl_random.h>
#endif
+#include <limits>
_LIBCPP_BEGIN_NAMESPACE_STD
-#if defined(_LIBCPP_USING_ARC4_RANDOM)
+#if defined(_LIBCPP_USING_GETENTROPY)
+
+random_device::random_device(const string& __token)
+{
+ if (__token != "/dev/urandom")
+ __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
+}
+
+random_device::~random_device()
+{
+}
+
+unsigned
+random_device::operator()()
+{
+ unsigned r;
+ size_t n = sizeof(r);
+ int err = getentropy(&r, n);
+ if (err)
+ __throw_system_error(errno, "random_device getentropy failed");
+ return r;
+}
+
+#elif defined(_LIBCPP_USING_ARC4_RANDOM)
random_device::random_device(const string& __token)
{
@@ -146,7 +174,11 @@ random_device::operator()()
double
random_device::entropy() const _NOEXCEPT
{
+#ifdef __OpenBSD__
+ return std::numeric_limits<unsigned int>::digits;
+#else
return 0;
+#endif
}
_LIBCPP_END_NAMESPACE_STD