aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2021-10-19 12:31:06 +0100
committerJonathan Wakely <jwakely@redhat.com>2021-10-19 17:27:06 +0100
commit58f339fc5eaae7db9526f81ab91f282ad4a9b8cc (patch)
tree3c6974aae38d7c6d988ac5625bc143db6147405d
parent3cfbe5dc08b574bccc398256946cc03e2a767329 (diff)
downloadgcc-58f339fc5eaae7db9526f81ab91f282ad4a9b8cc.zip
gcc-58f339fc5eaae7db9526f81ab91f282ad4a9b8cc.tar.gz
gcc-58f339fc5eaae7db9526f81ab91f282ad4a9b8cc.tar.bz2
libstdc++: Implement std::random_device::entropy() for other sources
Currently this function only returns a non-zero value for /dev/random and /dev/urandom. When a hardware instruction such as RDRAND is in use it should (in theory) be perfectly random and produce 32 bits of entropy in each 32-bit result. Add a helper function to identify the source of randomness from the _M_func and _M_file data members, and return a suitable value when RDRAND or RDSEED is being used. libstdc++-v3/ChangeLog: * src/c++11/random.cc (which_source): New helper function. (random_device::_M_getentropy()): Use which_source and return suitable values for sources other than device files. * testsuite/26_numerics/random/random_device/entropy.cc: New test.
-rw-r--r--libstdc++-v3/src/c++11/random.cc70
-rw-r--r--libstdc++-v3/testsuite/26_numerics/random/random_device/entropy.cc37
2 files changed, 100 insertions, 7 deletions
diff --git a/libstdc++-v3/src/c++11/random.cc b/libstdc++-v3/src/c++11/random.cc
index 44b9f30..4b64bde 100644
--- a/libstdc++-v3/src/c++11/random.cc
+++ b/libstdc++-v3/src/c++11/random.cc
@@ -192,6 +192,51 @@ namespace std _GLIBCXX_VISIBILITY(default)
return lcg();
}
#endif
+
+ enum Which {
+ rand_s = 1, rdseed = 2, rdrand = 4, device_file = 8, prng = 16,
+ any = 0xffff
+ };
+
+ inline Which
+ which_source(random_device::result_type (*func [[maybe_unused]])(void*),
+ void* file [[maybe_unused]])
+ {
+#ifdef _GLIBCXX_USE_CRT_RAND_S
+ if (func == &__winxp_rand_s)
+ return rand_s;
+#endif
+
+#ifdef USE_RDSEED
+#ifdef USE_RDRAND
+ if (func == &__x86_rdseed_rdrand)
+ return rdseed;
+#endif
+ if (func == &__x86_rdseed)
+ return rdseed;
+#endif
+
+#ifdef USE_RDRAND
+ if (func == &__x86_rdrand)
+ return rdrand;
+#endif
+
+#ifdef _GLIBCXX_USE_DEV_RANDOM
+ if (file != nullptr)
+ return device_file;
+#endif
+
+#ifdef USE_LCG
+ if (func == &__lcg)
+ return prng;
+#endif
+
+#ifdef USE_MT19937
+ return prng;
+#endif
+
+ return any; // should be unreachable
+ }
}
void
@@ -209,10 +254,7 @@ namespace std _GLIBCXX_VISIBILITY(default)
const char* fname [[gnu::unused]] = nullptr;
- enum {
- rand_s = 1, rdseed = 2, rdrand = 4, device_file = 8, prng = 16,
- any = 0xffff
- } which;
+ Which which;
if (token == "default")
{
@@ -449,10 +491,25 @@ namespace std _GLIBCXX_VISIBILITY(default)
double
random_device::_M_getentropy() const noexcept
{
+ const int max = sizeof(result_type) * __CHAR_BIT__;
+
+ switch(which_source(_M_func, _M_file))
+ {
+ case rdrand:
+ case rdseed:
+ return (double) max;
+ case rand_s:
+ case prng:
+ return 0.0;
+ case device_file:
+ // handled below
+ break;
+ default:
+ return 0.0;
+ }
+
#if defined _GLIBCXX_USE_DEV_RANDOM \
&& defined _GLIBCXX_HAVE_SYS_IOCTL_H && defined RNDGETENTCNT
- if (!_M_file)
- return 0.0;
#ifdef USE_POSIX_FILE_IO
const int fd = _M_fd;
@@ -469,7 +526,6 @@ namespace std _GLIBCXX_VISIBILITY(default)
if (ent < 0)
return 0.0;
- const int max = sizeof(result_type) * __CHAR_BIT__;
if (ent > max)
ent = max;
diff --git a/libstdc++-v3/testsuite/26_numerics/random/random_device/entropy.cc b/libstdc++-v3/testsuite/26_numerics/random/random_device/entropy.cc
new file mode 100644
index 0000000..9ef1538
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/random/random_device/entropy.cc
@@ -0,0 +1,37 @@
+// { dg-do run { target c++11 } }
+
+#include <random>
+#include <testsuite_hooks.h>
+#include <testsuite_random.h>
+
+void
+test01()
+{
+ for (auto token : { "mt19937", "prng", "rand_s" })
+ if (__gnu_test::random_device_available(token))
+ VERIFY( std::random_device(token).entropy() == 0.0 );
+
+ using result_type = std::random_device::result_type;
+ const double max = std::log2(std::numeric_limits<result_type>::max() + 1.0);
+
+ for (auto token : { "/dev/random", "/dev/urandom" })
+ if (__gnu_test::random_device_available(token))
+ {
+ const double entropy = std::random_device(token).entropy();
+ VERIFY( entropy >= 0.0 );
+ VERIFY( entropy <= max );
+ }
+
+ for (auto token : { "rdrand", "rdseed" })
+ if (__gnu_test::random_device_available(token))
+ {
+ const double entropy = std::random_device(token).entropy();
+ VERIFY( entropy == max );
+ }
+}
+
+int
+main()
+{
+ test01();
+}