diff options
| author | Carl Hetherington <cth@carlh.net> | 2016-01-14 22:27:20 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2019-12-03 16:45:01 +0100 |
| commit | f2ad6d58402e7cf5ae28011a2135a02b19215c58 (patch) | |
| tree | 72b45516af03ba886e76b1ecd4cf664fde3de63a /src/KM_prng.cpp | |
| parent | 9343b09f514076733528eddbdc68c8b133ac4886 (diff) | |
Patches for testing to allow predictable random number and date generation.
Diffstat (limited to 'src/KM_prng.cpp')
| -rwxr-xr-x | src/KM_prng.cpp | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/src/KM_prng.cpp b/src/KM_prng.cpp index fb4286c..5afc598 100755 --- a/src/KM_prng.cpp +++ b/src/KM_prng.cpp @@ -64,6 +64,7 @@ public: AES_KEY m_Context; byte_t m_ctr_buf[RNG_BLOCK_SIZE]; Mutex m_Lock; + unsigned int m_cth_test_rng_state; h__RNG() { @@ -97,8 +98,9 @@ public: } // end AutoMutex context set_key(rng_key); + reset(); } - + // void set_key(const byte_t* key_fodder) @@ -116,7 +118,7 @@ public: AES_set_encrypt_key(sha_buf, RNG_KEY_SIZE_BITS, &m_Context); *(ui32_t*)(m_ctr_buf + 12) = 1; } - + // void fill_rand(byte_t* buf, ui32_t len) @@ -131,14 +133,27 @@ public: *(ui32_t*)(m_ctr_buf + 12) += 1; gen_count += RNG_BLOCK_SIZE; } - + if ( len != gen_count ) // partial count needed? { byte_t tmp[RNG_BLOCK_SIZE]; AES_encrypt(m_ctr_buf, tmp, &m_Context); memcpy(buf + gen_count, tmp, len - gen_count); } + + if (cth_test) + { +#ifdef __unix__ + for (unsigned int i = 0; i < len; ++i) + buf[i] = rand_r(&m_cth_test_rng_state); +#endif + } } + + void reset() + { + m_cth_test_rng_state = 1; + } }; @@ -173,13 +188,13 @@ Kumu::FortunaRNG::FillRandom(byte_t* buf, ui32_t len) s_RNG->fill_rand(buf, gen_size); buf += gen_size; len -= gen_size; - + // re-seed the generator byte_t rng_key[RNG_KEY_SIZE]; s_RNG->fill_rand(rng_key, RNG_KEY_SIZE); s_RNG->set_key(rng_key); } - + return front_of_buffer; } @@ -192,6 +207,12 @@ Kumu::FortunaRNG::FillRandom(Kumu::ByteString& Buffer) return Buffer.Data(); } +void +Kumu::FortunaRNG::Reset() +{ + s_RNG->reset(); +} + //------------------------------------------------------------------------------------------ // |
