Botan 3.14.1
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#include <botan/mem_ops.h>
12
13namespace Botan {
14
16 m_hmac(MessageAuthenticationCode::create_or_throw(hmac_algo)),
17 m_chacha(StreamCipher::create_or_throw(stream_cipher_algo)),
18 m_key_erasure(key_erasure) {
19 clear();
20}
21
23 add_entropy(seed);
24}
25
27 Stateful_RNG(underlying_rng, reseed_interval),
28 m_hmac(MessageAuthenticationCode::create_or_throw(hmac_algo)),
29 m_chacha(StreamCipher::create_or_throw(stream_cipher_algo)),
30 m_key_erasure(key_erasure) {
31 clear();
32}
33
35 Entropy_Sources& entropy_sources,
36 size_t reseed_interval,
38 Stateful_RNG(underlying_rng, entropy_sources, reseed_interval),
39 m_hmac(MessageAuthenticationCode::create_or_throw(hmac_algo)),
40 m_chacha(StreamCipher::create_or_throw(stream_cipher_algo)),
41 m_key_erasure(key_erasure) {
42 clear();
43}
44
46 Stateful_RNG(entropy_sources, reseed_interval),
47 m_hmac(MessageAuthenticationCode::create_or_throw(hmac_algo)),
48 m_chacha(StreamCipher::create_or_throw(stream_cipher_algo)),
49 m_key_erasure(key_erasure) {
50 clear();
51}
52
54 m_hmac->set_key(std::vector<uint8_t>(m_hmac->output_length(), 0x00));
55 update_chacha_state(m_hmac->final());
56}
57
58void ChaCha_RNG::update_chacha_state(std::span<const uint8_t> key_material) {
59 /*
60 * HMAC(X) should produce more than 32 + 8 = 40 byte. As there is no
61 * SHA-320, this check needs to be >= for SHA-384 and SHA-512 or their SHA-3
62 * equivalents. SHA-256 won't produce enough output with a single invocation.
63 *
64 * This assertion is necessary, as key_material has a dynamic extent.
65 * km has a fixed extent, which can be checked at compile time.
66 */
67 BOTAN_ASSERT_NOMSG(key_material.size() >= chacha_key_len + chacha_iv_len);
68
69 const std::span<const uint8_t, chacha_key_len + chacha_iv_len> km =
70 key_material.first<chacha_key_len + chacha_iv_len>();
71 m_chacha->set_key(km.first<chacha_key_len>());
72 m_chacha->set_iv(km.last<chacha_iv_len>());
73}
74
75void ChaCha_RNG::generate_output(std::span<uint8_t> output, std::span<const uint8_t> input) {
76 BOTAN_ASSERT_NOMSG(!output.empty());
77
78 if(!input.empty()) {
79 update(input);
80 }
81
82 const bool erase_key = (m_key_erasure == KeyErasure::WithKeyErasure);
83
84 // Use a fixed structure for fast key erasure. Next key is always the first output of
85 // the keystream (without updates in between). Therefore the next key becomes independent
86 // of the request pattern. This should not harm security and may ease mathematical analysis,
87 // as no variable position of the next key in the output stream has to be modeled.
88 std::array<uint8_t, chacha_key_len + chacha_iv_len> key_material{};
89 if(erase_key) {
90 m_chacha->write_keystream(key_material);
91 }
92
93 m_chacha->write_keystream(output);
94
95 // optionally overwrite key after each output operation for backtracking resistance
96 if(erase_key) {
97 update_chacha_state(key_material);
98 secure_scrub_memory(key_material);
99 }
100}
101
102void ChaCha_RNG::update(std::span<const uint8_t> input) {
103 update_chacha_state(m_hmac->process(input));
104 m_hmac->set_key(m_chacha->keystream_bytes(m_hmac->output_length()));
105}
106
108 /*
109 * as we use a 64 bit nonce as extended key and a wide enough hash
110 * function with SHA-512, could also be set to 256 + 64 = 320
111 */
112 return 256;
113}
114
115} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
KeyErasure key_erasure() const
Definition chacha_rng.h:186
size_t security_level() const override
ChaCha_RNG(KeyErasure key_erasure=KeyErasure::NoErasure)
void add_entropy(std::span< const uint8_t > input)
Definition rng.h:121
size_t reseed_interval() const
virtual void clear_state()=0
Stateful_RNG(RandomNumberGenerator &rng, Entropy_Sources &entropy_sources, size_t reseed_interval)
void secure_scrub_memory(void *ptr, size_t n)
Definition mem_utils.cpp:24