aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/rng
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-08-03 07:31:03 -0400
committerJack Lloyd <[email protected]>2017-08-03 10:37:00 -0400
commitd1af646c365197de243c844d138f245d1dcac6ba (patch)
treede18e27d17c368dbcfdbe16c0d2ecdeb01ca8b06 /src/lib/rng
parent7edeec69f09fbed01881e93b0f45dbb038bfac73 (diff)
Refactor stateful RNG tests to cover both HMAC_DRBG and ChaCha_RNG
Diffstat (limited to 'src/lib/rng')
-rw-r--r--src/lib/rng/chacha_rng/chacha_rng.cpp33
-rw-r--r--src/lib/rng/chacha_rng/chacha_rng.h2
-rw-r--r--src/lib/rng/hmac_drbg/hmac_drbg.h3
-rw-r--r--src/lib/rng/stateful_rng/stateful_rng.h17
4 files changed, 38 insertions, 17 deletions
diff --git a/src/lib/rng/chacha_rng/chacha_rng.cpp b/src/lib/rng/chacha_rng/chacha_rng.cpp
index 86c71f9fe..ceccfef2c 100644
--- a/src/lib/rng/chacha_rng/chacha_rng.cpp
+++ b/src/lib/rng/chacha_rng/chacha_rng.cpp
@@ -57,7 +57,6 @@ void ChaCha_RNG::clear()
Stateful_RNG::clear();
m_hmac->set_key(std::vector<uint8_t>(m_hmac->output_length(), 0x00));
- m_chacha->set_key(m_hmac->final());
}
void ChaCha_RNG::randomize(uint8_t output[], size_t output_len)
@@ -68,28 +67,34 @@ void ChaCha_RNG::randomize(uint8_t output[], size_t output_len)
void ChaCha_RNG::randomize_with_input(uint8_t output[], size_t output_len,
const uint8_t input[], size_t input_len)
{
- add_entropy(input, input_len);
reseed_check();
+ if(input_len > 0)
+ {
+ update(input, input_len);
+ }
+
clear_mem(output, output_len);
m_chacha->cipher1(output, output_len);
}
-void ChaCha_RNG::add_entropy(const uint8_t input[], size_t input_len)
+void ChaCha_RNG::update(const uint8_t input[], size_t input_len)
{
- if(input_len > 0)
- {
- m_hmac->update(input, input_len);
- m_chacha->set_key(m_hmac->final());
+ m_hmac->update(input, input_len);
+ m_chacha->set_key(m_hmac->final());
- secure_vector<uint8_t> mac_key(m_hmac->output_length());
- m_chacha->cipher1(mac_key.data(), mac_key.size());
- m_hmac->set_key(mac_key);
+ secure_vector<uint8_t> mac_key(m_hmac->output_length());
+ m_chacha->cipher1(mac_key.data(), mac_key.size());
+ m_hmac->set_key(mac_key);
+ }
- if(8*input_len >= security_level())
- {
- m_reseed_counter = 1;
- }
+void ChaCha_RNG::add_entropy(const uint8_t input[], size_t input_len)
+ {
+ update(input, input_len);
+
+ if(8*input_len >= security_level())
+ {
+ m_reseed_counter = 1;
}
}
diff --git a/src/lib/rng/chacha_rng/chacha_rng.h b/src/lib/rng/chacha_rng/chacha_rng.h
index b6a763f62..7deaa2d89 100644
--- a/src/lib/rng/chacha_rng/chacha_rng.h
+++ b/src/lib/rng/chacha_rng/chacha_rng.h
@@ -115,6 +115,8 @@ class BOTAN_DLL ChaCha_RNG final : public Stateful_RNG
size_t security_level() const override;
+ size_t max_number_of_bytes_per_request() const override { return 0; }
+
private:
void update(const uint8_t input[], size_t input_len);
diff --git a/src/lib/rng/hmac_drbg/hmac_drbg.h b/src/lib/rng/hmac_drbg/hmac_drbg.h
index 1c95cb304..4d3faa082 100644
--- a/src/lib/rng/hmac_drbg/hmac_drbg.h
+++ b/src/lib/rng/hmac_drbg/hmac_drbg.h
@@ -143,6 +143,9 @@ class BOTAN_DLL HMAC_DRBG final : public Stateful_RNG
size_t security_level() const override;
+ size_t max_number_of_bytes_per_request() const override
+ { return m_max_number_of_bytes_per_request; }
+
private:
void update(const uint8_t input[], size_t input_len);
diff --git a/src/lib/rng/stateful_rng/stateful_rng.h b/src/lib/rng/stateful_rng/stateful_rng.h
index ed51aac6a..0d59d396b 100644
--- a/src/lib/rng/stateful_rng/stateful_rng.h
+++ b/src/lib/rng/stateful_rng/stateful_rng.h
@@ -103,12 +103,23 @@ class BOTAN_DLL Stateful_RNG : public RandomNumberGenerator
*/
virtual size_t security_level() const = 0;
+ /**
+ * Some DRBGs have a notion of the maximum number of bytes per
+ * request. Longer requests (to randomize) will be treated as
+ * multiple requests, and may initiate reseeding multiple times,
+ * depending on the values of max_number_of_bytes_per_request and
+ * reseed_interval(). This function returns zero if the RNG in
+ * question does not have such a notion.
+ *
+ * @return max number of bytes per request (or zero)
+ */
+ virtual size_t max_number_of_bytes_per_request() const = 0;
+
+ size_t reseed_interval() const { return m_reseed_interval; }
+
void clear() override;
protected:
- /**
- * Called with lock held
- */
void reseed_check();
uint32_t last_pid() const { return m_last_pid; }