[libc++] Use ioctl when available to get random_device entropy.
authorMarek Kurdej <marek.kurdej@gmail.com>
Thu, 21 Jan 2021 16:55:19 +0000 (17:55 +0100)
committerMarek Kurdej <marek.kurdej@gmail.com>
Thu, 21 Jan 2021 17:01:02 +0000 (18:01 +0100)
Implemented the idea from D94571 to improve entropy on Linux.

Reviewed By: ldionne, #libc

Differential Revision: https://reviews.llvm.org/D94953

libcxx/src/random.cpp
libcxx/test/std/numerics/rand/rand.device/entropy.pass.cpp

index 04adc59..7d0431e 100644 (file)
@@ -13,6 +13,7 @@
 #define _CRT_RAND_S
 #endif // defined(_LIBCPP_USING_WIN32_RANDOM)
 
+#include "limits"
 #include "random"
 #include "system_error"
 
 #elif defined(_LIBCPP_USING_DEV_RANDOM)
 #include <fcntl.h>
 #include <unistd.h>
+#if __has_include(<sys/ioctl.h>) && __has_include(<linux/random.h>)
+#include <sys/ioctl.h>
+#include <linux/random.h>
+#endif
 #elif defined(_LIBCPP_USING_NACL_RANDOM)
 #include <nacl/nacl_random.h>
 #endif
@@ -172,7 +177,21 @@ random_device::operator()()
 double
 random_device::entropy() const _NOEXCEPT
 {
+#if defined(_LIBCPP_USING_DEV_RANDOM) && defined(RNDGETENTCNT)
+  int ent;
+  if (::ioctl(__f_, RNDGETENTCNT, &ent) < 0)
+    return 0;
+
+  if (ent < 0)
     return 0;
+
+  if (ent > std::numeric_limits<result_type>::digits)
+    return std::numeric_limits<result_type>::digits;
+
+  return ent;
+#else
+  return 0;
+#endif
 }
 
 _LIBCPP_END_NAMESPACE_STD
index 4f09d05..3f9ea85 100644 (file)
 
 #include <random>
 #include <cassert>
+#include <climits>
 
 #include "test_macros.h"
 
-int main(int, char**)
-{
-    std::random_device r;
-    double e = r.entropy();
-    ((void)e); // Prevent unused warning
+int main(int, char**) {
+  std::random_device r;
+  double e = r.entropy();
+  assert(e >= 0);
+  assert(e <= sizeof(typename std::random_device::result_type) * CHAR_BIT);
 
   return 0;
 }