diff options
author | Nick Mathewson <nickm@torproject.org> | 2019-03-18 12:50:11 -0400 |
---|---|---|
committer | David Goulet <dgoulet@torproject.org> | 2019-04-30 11:11:39 -0400 |
commit | 730dddc3806f904b4328f4e9281d370c65522c30 (patch) | |
tree | c959046311bbd7cad52808d6a5be0a89b76064c4 /src/test/rng_test_helpers.c | |
parent | 604e849d36232def4fa186c4eee4c1caa551e894 (diff) | |
download | tor-730dddc3806f904b4328f4e9281d370c65522c30.tar.gz tor-730dddc3806f904b4328f4e9281d370c65522c30.zip |
Make sure that the rng is not replaced if it is already replaced.
Diffstat (limited to 'src/test/rng_test_helpers.c')
-rw-r--r-- | src/test/rng_test_helpers.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/test/rng_test_helpers.c b/src/test/rng_test_helpers.c index c11709ed88..262d380bda 100644 --- a/src/test/rng_test_helpers.c +++ b/src/test/rng_test_helpers.c @@ -25,6 +25,11 @@ #endif /** + * True iff the RNG is currently replaced. Prevents double-replacement. + **/ +static bool rng_is_replaced = false; + +/** * Mutex to protect deterministic prng. * * Note that if you actually _use_ the prng from two threads at the same time, @@ -82,6 +87,9 @@ crypto_rand_deterministic(char *out, size_t n) static void enable_deterministic_rng_impl(const uint8_t *seed, size_t seed_len) { + tor_assert(!rng_is_replaced); + tor_assert(crypto_rand == crypto_rand__real); + memset(rng_seed, 0, sizeof(rng_seed)); memcpy(rng_seed, seed, MIN(seed_len, sizeof(rng_seed))); @@ -100,6 +108,8 @@ enable_deterministic_rng_impl(const uint8_t *seed, size_t seed_len) crypto_fast_rng_t *fast_rng = crypto_fast_rng_new_from_seed(fast_rng_seed); crypto_fast_rng_disable_reseed(fast_rng); stored_fast_rng = crypto_replace_thread_fast_rng(fast_rng); + + rng_is_replaced = true; } /** @@ -196,6 +206,9 @@ testing_prefilled_rng_reset(void) /** * Undo the overrides for our PRNG. To be used at the end of testing. + * + * Note that this function should be safe to call even if the rng has not + * yet been replaced. **/ void testing_disable_rng_override(void) @@ -208,4 +221,6 @@ testing_disable_rng_override(void) crypto_fast_rng_t *rng = crypto_replace_thread_fast_rng(stored_fast_rng); crypto_fast_rng_free(rng); + + rng_is_replaced = false; } |