Botan  2.4.0
Crypto and TLS for C++11
chacha_rng.cpp
Go to the documentation of this file.
1 /*
2 * ChaCha_RNG
3 * (C) 2017 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/chacha_rng.h>
9 
10 namespace Botan {
11 
13  {
14  m_hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
15  m_chacha = StreamCipher::create_or_throw("ChaCha(20)");
16  clear();
17  }
18 
20  {
21  m_hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
22  m_chacha = StreamCipher::create_or_throw("ChaCha(20)");
23  clear();
24  add_entropy(seed.data(), seed.size());
25  }
26 
28  size_t reseed_interval) :
29  Stateful_RNG(underlying_rng, reseed_interval)
30  {
31  m_hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
32  m_chacha = StreamCipher::create_or_throw("ChaCha(20)");
33  clear();
34  }
35 
37  Entropy_Sources& entropy_sources,
38  size_t reseed_interval) :
39  Stateful_RNG(underlying_rng, entropy_sources, reseed_interval)
40  {
41  m_hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
42  m_chacha = StreamCipher::create_or_throw("ChaCha(20)");
43  clear();
44  }
45 
47  size_t reseed_interval) :
48  Stateful_RNG(entropy_sources, reseed_interval)
49  {
50  m_hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
51  m_chacha = StreamCipher::create_or_throw("ChaCha(20)");
52  clear();
53  }
54 
56  {
58 
59  m_hmac->set_key(std::vector<uint8_t>(m_hmac->output_length(), 0x00));
60  m_chacha->set_key(m_hmac->final());
61  }
62 
63 void ChaCha_RNG::randomize(uint8_t output[], size_t output_len)
64  {
65  randomize_with_input(output, output_len, nullptr, 0);
66  }
67 
68 void ChaCha_RNG::randomize_with_input(uint8_t output[], size_t output_len,
69  const uint8_t input[], size_t input_len)
70  {
71  reseed_check();
72 
73  if(input_len > 0)
74  {
75  update(input, input_len);
76  }
77 
78  clear_mem(output, output_len);
79  m_chacha->cipher1(output, output_len);
80  }
81 
82 void ChaCha_RNG::update(const uint8_t input[], size_t input_len)
83  {
84  m_hmac->update(input, input_len);
85  m_chacha->set_key(m_hmac->final());
86 
87  secure_vector<uint8_t> mac_key(m_hmac->output_length());
88  m_chacha->cipher1(mac_key.data(), mac_key.size());
89  m_hmac->set_key(mac_key);
90  }
91 
92 void ChaCha_RNG::add_entropy(const uint8_t input[], size_t input_len)
93  {
94  update(input, input_len);
95 
96  if(8*input_len >= security_level())
97  {
99  }
100  }
101 
103  {
104  return 256;
105  }
106 
107 }
void add_entropy(const uint8_t input[], size_t input_len) override
Definition: chacha_rng.cpp:92
static std::unique_ptr< StreamCipher > create_or_throw(const std::string &algo_spec, const std::string &provider="")
size_t security_level() const override
Definition: chacha_rng.cpp:102
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:86
void clear() override
size_t reseed_interval() const
Definition: stateful_rng.h:118
void clear() override
Definition: chacha_rng.cpp:55
Definition: alg_id.cpp:13
void randomize(uint8_t output[], size_t output_len) override
Definition: chacha_rng.cpp:63
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:140
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
void randomize_with_input(uint8_t output[], size_t output_len, const uint8_t input[], size_t input_len) override
Definition: chacha_rng.cpp:68