diff options
author | lloyd <[email protected]> | 2014-03-21 22:16:46 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-03-21 22:16:46 +0000 |
commit | 0e2b9db6eecb07d01b4756b8fa0a3307670715b6 (patch) | |
tree | be45ca028434e55065ac70da7290e42747bebd48 /src/lib/rng | |
parent | b4407e3d2191a166dfd0e71006180431726f3fc6 (diff) |
Add HMAC_DRBG
Diffstat (limited to 'src/lib/rng')
-rw-r--r-- | src/lib/rng/hmac_drbg/hmac_drbg.cpp | 103 | ||||
-rw-r--r-- | src/lib/rng/hmac_drbg/hmac_drbg.h | 50 | ||||
-rw-r--r-- | src/lib/rng/hmac_drbg/info.txt | 5 | ||||
-rw-r--r-- | src/lib/rng/rng.h | 2 |
4 files changed, 159 insertions, 1 deletions
diff --git a/src/lib/rng/hmac_drbg/hmac_drbg.cpp b/src/lib/rng/hmac_drbg/hmac_drbg.cpp new file mode 100644 index 000000000..3227841f0 --- /dev/null +++ b/src/lib/rng/hmac_drbg/hmac_drbg.cpp @@ -0,0 +1,103 @@ +/* +* HMAC_DRBG +* (C) 2014 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/hmac_drbg.h> +#include <algorithm> + +namespace Botan { + +HMAC_DRBG::HMAC_DRBG(MessageAuthenticationCode* mac, + RandomNumberGenerator* prng) : + m_mac(mac), + m_prng(prng), + m_V(m_mac->output_length(), 0x01), + m_reseed_counter(0) + { + m_mac->set_key(secure_vector<byte>(m_mac->output_length(), 0x00)); + } + +void HMAC_DRBG::randomize(byte out[], size_t length) + { + if(!is_seeded() || m_reseed_counter > BOTAN_RNG_MAX_OUTPUT_BEFORE_RESEED) + reseed(m_mac->output_length() * 8); + + if(!is_seeded()) + throw PRNG_Unseeded(name()); + + while(length) + { + const size_t to_copy = std::min(length, m_V.size()); + m_V = m_mac->process(m_V); + copy_mem(&out[0], &m_V[0], to_copy); + + length -= to_copy; + out += to_copy; + } + + update(nullptr, 0); // additional_data is always empty + } + +/* +* Reset V and the mac key with new values +*/ +void HMAC_DRBG::update(const byte input[], size_t input_len) + { + m_mac->update(m_V); + m_mac->update(0x00); + m_mac->update(input, input_len); + m_mac->set_key(m_mac->final()); + + m_V = m_mac->process(m_V); + + if(input_len) + { + m_mac->update(m_V); + m_mac->update(0x01); + m_mac->update(input, input_len); + m_mac->set_key(m_mac->final()); + + m_V = m_mac->process(m_V); + } + } + +void HMAC_DRBG::reseed(size_t poll_bits) + { + m_prng->reseed(poll_bits); + + if(m_prng->is_seeded()) + { + secure_vector<byte> input = m_prng->random_vec(m_mac->output_length()); + update(&input[0], input.size()); + m_reseed_counter = 1; + } + } + +void HMAC_DRBG::add_entropy(const byte input[], size_t length) + { + // Should we also poll the underlying PRNG here? + update(input, length); + } + +bool HMAC_DRBG::is_seeded() const + { + return m_reseed_counter > 0; + } + +void HMAC_DRBG::clear() + { + m_mac->clear(); + m_prng->clear(); + zeroise(m_V); + zeroise(m_K); + } + +std::string HMAC_DRBG::name() const + { + return "HMAC_DRBG(" + m_mac->name() + ")"; + } + +} diff --git a/src/lib/rng/hmac_drbg/hmac_drbg.h b/src/lib/rng/hmac_drbg/hmac_drbg.h new file mode 100644 index 000000000..43729c7fa --- /dev/null +++ b/src/lib/rng/hmac_drbg/hmac_drbg.h @@ -0,0 +1,50 @@ +/* +* HMAC_DRBG (SP800-90A) +* (C) 2014 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_HMAC_DRBG_H__ +#define BOTAN_HMAC_DRBG_H__ + +#include <botan/rng.h> +#include <botan/mac.h> + +namespace Botan { + +/** +* HMAC_DRBG (SP800-90A) +*/ +class BOTAN_DLL HMAC_DRBG : public RandomNumberGenerator + { + public: + void randomize(byte buf[], size_t buf_len); + bool is_seeded() const; + void clear(); + std::string name() const; + + void reseed(size_t poll_bits); + + void add_entropy(const byte input[], size_t input_len); + + /** + * @param mac the underlying mac function (eg HMAC(SHA-512)) + * @param underlying_rng RNG used generating inputs (eg HMAC_RNG) + */ + HMAC_DRBG(MessageAuthenticationCode* mac, + RandomNumberGenerator* underlying_rng); + + private: + void update(const byte input[], size_t input_len); + + std::unique_ptr<MessageAuthenticationCode> m_mac; + std::unique_ptr<RandomNumberGenerator> m_prng; + + secure_vector<byte> m_K, m_V; + size_t m_reseed_counter; + }; + +} + +#endif diff --git a/src/lib/rng/hmac_drbg/info.txt b/src/lib/rng/hmac_drbg/info.txt new file mode 100644 index 000000000..f386db199 --- /dev/null +++ b/src/lib/rng/hmac_drbg/info.txt @@ -0,0 +1,5 @@ +define HMAC_DRBG 20140319 + +<requires> +hmac +</requires> diff --git a/src/lib/rng/rng.h b/src/lib/rng/rng.h index 812b13527..c69c147e6 100644 --- a/src/lib/rng/rng.h +++ b/src/lib/rng/rng.h @@ -45,7 +45,7 @@ class BOTAN_DLL RandomNumberGenerator * @param bytes number of bytes in the result * @return randomized vector of length bytes */ - secure_vector<byte> random_vec(size_t bytes) + virtual secure_vector<byte> random_vec(size_t bytes) { secure_vector<byte> output(bytes); randomize(&output[0], output.size()); |