Botan 3.3.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 has_keying_material() const override;
50
51 size_t buffer_size() const override;
52
53 private:
54 void key_schedule(std::span<const uint8_t> key) override;
55
56 void cipher_bytes(const uint8_t in[], uint8_t out[], size_t length) override;
57
58 void generate_keystream(uint8_t out[], size_t len) override;
59
60 void set_iv_bytes(const uint8_t iv[], size_t iv_len) override;
61
62 void initialize_state();
63
64 static size_t parallelism();
65
66 static void chacha(uint8_t output[], size_t output_blocks, uint32_t state[16], size_t rounds);
67
68#if defined(BOTAN_HAS_CHACHA_SIMD32)
69 static void chacha_simd32_x4(uint8_t output[64 * 4], uint32_t state[16], size_t rounds);
70#endif
71
72#if defined(BOTAN_HAS_CHACHA_AVX2)
73 static void chacha_avx2_x8(uint8_t output[64 * 8], uint32_t state[16], size_t rounds);
74#endif
75
76#if defined(BOTAN_HAS_CHACHA_AVX512)
77 static void chacha_avx512_x16(uint8_t output[64 * 16], uint32_t state[16], size_t rounds);
78#endif
79
80 size_t m_rounds;
84 size_t m_position = 0;
85};
86
87} // namespace Botan
88
89#endif
void clear() override
Definition chacha.cpp:371
std::string name() const override
Definition chacha.cpp:378
size_t buffer_size() const override
Definition chacha.cpp:289
std::unique_ptr< StreamCipher > new_object() const override
Definition chacha.cpp:316
Key_Length_Specification key_spec() const override
Definition chacha.cpp:312
bool valid_iv_length(size_t iv_len) const override
Definition chacha.cpp:320
size_t default_iv_length() const override
Definition chacha.cpp:308
std::string provider() const override
Definition chacha.cpp:89
ChaCha(size_t rounds=20)
Definition chacha.cpp:69
bool has_keying_material() const override
Definition chacha.cpp:285
void seek(uint64_t offset) override
Definition chacha.cpp:382
int(* final)(unsigned char *, CTX *)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61