Botan 3.13.0
Crypto and TLS for C&
chacha.h
Go to the documentation of this file.
1/*
2* ChaCha20
3* (C) 2014,2018 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_CHACHA_H_
9#define BOTAN_CHACHA_H_
10
11#include <botan/stream_cipher.h>
12
13namespace Botan {
14
15/**
16* DJB's ChaCha (https://cr.yp.to/chacha.html)
17*/
18class ChaCha final : public StreamCipher {
19 public:
20 /**
21 * @param rounds number of rounds
22 * @note Currently only 8, 12 or 20 rounds are supported, all others
23 * will throw an exception
24 */
25 explicit ChaCha(size_t rounds = 20);
26
27 std::string provider() const override;
28
29 /*
30 * ChaCha accepts 0, 8, 12 or 24 byte IVs.
31 * The default IV is a 8 zero bytes.
32 * An IV of length 0 is treated the same as the default zero IV.
33 * An IV of length 24 selects XChaCha mode
34 */
35 bool valid_iv_length(size_t iv_len) const override;
36
37 size_t default_iv_length() const override;
38
39 Key_Length_Specification key_spec() const override;
40
41 void clear() override;
42
43 std::unique_ptr<StreamCipher> new_object() const override;
44
45 std::string name() const override;
46
47 void seek(uint64_t offset) override;
48
49 bool supports_seek() const override { return true; }
50
51 std::optional<uint64_t> remaining_keystream_bytes() const override;
52
53 bool has_keying_material() const override;
54
55 size_t buffer_size() const override;
56
57 private:
58 void key_schedule(std::span<const uint8_t> key) override;
59
60 void cipher_bytes(const uint8_t in[], uint8_t out[], size_t length) override;
61
62 void generate_keystream(uint8_t out[], size_t len) override;
63
64 void set_iv_bytes(const uint8_t iv[], size_t iv_len) override;
65
66 void initialize_state();
67
68 static size_t parallelism();
69
70 static void chacha(uint8_t output[], size_t output_blocks, uint32_t state[16], size_t rounds);
71
72#if defined(BOTAN_HAS_CHACHA_SIMD32)
73 static void chacha_simd32_x4(uint8_t output[64 * 4], uint32_t state[16], size_t rounds);
74#endif
75
76#if defined(BOTAN_HAS_CHACHA_AVX2)
77 static void chacha_avx2_x8(uint8_t output[64 * 8], uint32_t state[16], size_t rounds);
78#endif
79
80#if defined(BOTAN_HAS_CHACHA_AVX512)
81 static void chacha_avx512_x16(uint8_t output[64 * 16], uint32_t state[16], size_t rounds);
82#endif
83
84 size_t m_rounds;
88 size_t m_position = 0;
89 size_t m_iv_length = 0;
90 uint32_t m_state13_post_iv = 0;
91 // Valid only when m_iv_length == 12: bytes the user can still
92 // generate before the 32-bit counter would wrap into the nonce.
93 uint64_t m_bytes_remaining = 0;
94};
95
96} // namespace Botan
97
98#endif
void clear() override
Definition chacha.cpp:429
std::string name() const override
Definition chacha.cpp:446
size_t buffer_size() const override
Definition chacha.cpp:341
bool supports_seek() const override
Definition chacha.h:49
std::optional< uint64_t > remaining_keystream_bytes() const override
Definition chacha.cpp:439
std::unique_ptr< StreamCipher > new_object() const override
Definition chacha.cpp:368
Key_Length_Specification key_spec() const override
Definition chacha.cpp:364
bool valid_iv_length(size_t iv_len) const override
Definition chacha.cpp:372
size_t default_iv_length() const override
Definition chacha.cpp:360
std::string provider() const override
Definition chacha.cpp:112
ChaCha(size_t rounds=20)
Definition chacha.cpp:92
bool has_keying_material() const override
Definition chacha.cpp:337
void seek(uint64_t offset) override
Definition chacha.cpp:450
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128