Botan 3.9.0
Crypto and TLS for C&
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#include <botan/assert.h>
11
12namespace Botan {
13
15 m_hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
16 m_chacha = StreamCipher::create_or_throw("ChaCha(20)");
17 clear();
18}
19
20ChaCha_RNG::ChaCha_RNG(std::span<const uint8_t> seed) {
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);
25}
26
28 Stateful_RNG(underlying_rng, reseed_interval) {
29 m_hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
30 m_chacha = StreamCipher::create_or_throw("ChaCha(20)");
31 clear();
32}
33
35 Entropy_Sources& entropy_sources,
36 size_t reseed_interval) :
37 Stateful_RNG(underlying_rng, entropy_sources, reseed_interval) {
38 m_hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
39 m_chacha = StreamCipher::create_or_throw("ChaCha(20)");
40 clear();
41}
42
44 Stateful_RNG(entropy_sources, reseed_interval) {
45 m_hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
46 m_chacha = StreamCipher::create_or_throw("ChaCha(20)");
47 clear();
48}
49
51 m_hmac->set_key(std::vector<uint8_t>(m_hmac->output_length(), 0x00));
52 m_chacha->set_key(m_hmac->final());
53}
54
55void ChaCha_RNG::generate_output(std::span<uint8_t> output, std::span<const uint8_t> input) {
56 BOTAN_ASSERT_NOMSG(!output.empty());
57
58 if(!input.empty()) {
59 update(input);
60 }
61
62 m_chacha->write_keystream(output);
63}
64
65void ChaCha_RNG::update(std::span<const uint8_t> input) {
66 m_hmac->update(input);
67 m_chacha->set_key(m_hmac->final());
68 const auto mac_key = m_chacha->keystream_bytes(m_hmac->output_length());
69 m_hmac->set_key(mac_key);
70}
71
73 return 256;
74}
75
76} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
size_t security_level() const override
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:148
void add_entropy(std::span< const uint8_t > input)
Definition rng.h:94
size_t reseed_interval() const
virtual void clear_state()=0
Stateful_RNG(RandomNumberGenerator &rng, Entropy_Sources &entropy_sources, size_t reseed_interval)
static std::unique_ptr< StreamCipher > create_or_throw(std::string_view algo_spec, std::string_view provider="")