aboutsummaryrefslogtreecommitdiff
path: root/src/random.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2017-02-22 08:02:50 +0100
committerWladimir J. van der Laan <laanwj@gmail.com>2017-02-22 08:02:50 +0100
commit7cad84929907c4294f07377453aa77887911b486 (patch)
treea364b2912656b00dac9e397f767d6df1fd91e821 /src/random.cpp
parentaa09ccbb74ea9febd83ce3362238ac5339069909 (diff)
sanity: Move OS random to sanity check function
Move the OS random test to a sanity check function that is called every time bitcoind is initialized. Keep `src/test/random_tests.cpp` for the case that later random tests are added, and keep a rudimentary test that just calls the sanity check.
Diffstat (limited to 'src/random.cpp')
-rw-r--r--src/random.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/random.cpp b/src/random.cpp
index 5774e9a3c2..7fdada7525 100644
--- a/src/random.cpp
+++ b/src/random.cpp
@@ -239,3 +239,33 @@ FastRandomContext::FastRandomContext(bool fDeterministic)
}
}
+bool Random_SanityCheck()
+{
+ /* This does not measure the quality of randomness, but it does test that
+ * OSRandom() overwrites all 32 bytes of the output given a maximum
+ * number of tries.
+ */
+ static const ssize_t MAX_TRIES = 1024;
+ uint8_t data[NUM_OS_RANDOM_BYTES];
+ bool overwritten[NUM_OS_RANDOM_BYTES] = {}; /* Tracks which bytes have been overwritten at least once */
+ int num_overwritten;
+ int tries = 0;
+ /* Loop until all bytes have been overwritten at least once, or max number tries reached */
+ do {
+ memset(data, 0, NUM_OS_RANDOM_BYTES);
+ GetOSRand(data);
+ for (int x=0; x < NUM_OS_RANDOM_BYTES; ++x) {
+ overwritten[x] |= (data[x] != 0);
+ }
+
+ num_overwritten = 0;
+ for (int x=0; x < NUM_OS_RANDOM_BYTES; ++x) {
+ if (overwritten[x]) {
+ num_overwritten += 1;
+ }
+ }
+
+ tries += 1;
+ } while (num_overwritten < NUM_OS_RANDOM_BYTES && tries < MAX_TRIES);
+ return (num_overwritten == NUM_OS_RANDOM_BYTES); /* If this failed, bailed out after too many tries */
+}