Botan 2.19.5 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
13 BOTAN_FUTURE_INTERNAL_HEADER(chacha.h)
14
15namespace Botan {
16
17/**
18* DJB's ChaCha (https://cr.yp.to/chacha.html)
19*/
20 class BOTAN_PUBLIC_API(2,0) ChaCha final : public StreamCipher
21 {
22 public:
23 /**
24 * @param rounds number of rounds
25 * @note Currently only 8, 12 or 20 rounds are supported, all others
26 * will throw an exception
27 */
28 explicit ChaCha(size_t rounds = 20);
29
30 std::string provider() const override;
31
32 void cipher(const uint8_t in[], uint8_t out[], size_t length) override;
33
34 void write_keystream(uint8_t out[], size_t len) override;
35
36 void set_iv(const uint8_t iv[], size_t iv_len) override;
37
38 /*
39 * ChaCha accepts 0, 8, 12 or 24 byte IVs.
40 * The default IV is a 8 zero bytes.
41 * An IV of length 0 is treated the same as the default zero IV.
42 * An IV of length 24 selects XChaCha mode
43 */
44 bool valid_iv_length(size_t iv_len) const override;
45
46 size_t default_iv_length() const override;
47
48 Key_Length_Specification key_spec() const override;
49
50 void clear() override;
51
52 StreamCipher* clone() const override;
53
54 std::string name() const override;
55
56 void seek(uint64_t offset) override;
57
58 private:
59 void key_schedule(const uint8_t key[], size_t key_len) override;
60
61 void initialize_state();
62
63 void chacha_x8(uint8_t output[64*8], uint32_t state[16], size_t rounds);
64
65#if defined(BOTAN_HAS_CHACHA_SIMD32)
66 void chacha_simd32_x4(uint8_t output[64*4], uint32_t state[16], size_t rounds);
67#endif
68
69#if defined(BOTAN_HAS_CHACHA_AVX2)
70 void chacha_avx2_x8(uint8_t output[64*8], uint32_t state[16], size_t rounds);
71#endif
72
73 size_t m_rounds;
74 secure_vector<uint32_t> m_key;
75 secure_vector<uint32_t> m_state;
76 secure_vector<uint8_t> m_buffer;
77 size_t m_position = 0;
78 };
79
80}
81
82#endif
Botan::ChaCha
Definition: chacha.h:21
Botan::Key_Length_Specification
Definition: sym_algo.h:20
Botan::StreamCipher
Definition: stream_cipher.h:22
Botan::StreamCipher::cipher
virtual void cipher(const uint8_t in[], uint8_t out[], size_t len)=0
Botan::StreamCipher::default_iv_length
virtual size_t default_iv_length() const
Definition: stream_cipher.h:119
Botan::StreamCipher::clone
virtual StreamCipher * clone() const =0
Botan::StreamCipher::set_iv
virtual void set_iv(const uint8_t iv[], size_t iv_len)=0
Botan::StreamCipher::write_keystream
virtual void write_keystream(uint8_t out[], size_t len)
Definition: stream_cipher.h:66
Botan::StreamCipher::seek
virtual void seek(uint64_t offset)=0
Botan::StreamCipher::valid_iv_length
virtual bool valid_iv_length(size_t iv_len) const
Definition: stream_cipher.h:125
Botan::StreamCipher::provider
virtual std::string provider() const
Definition: stream_cipher.h:142
Botan::SymmetricAlgorithm::name
virtual std::string name() const =0
Botan::SymmetricAlgorithm::clear
virtual void clear()=0
Botan::SymmetricAlgorithm::key_spec
virtual Key_Length_Specification key_spec() const =0
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:29
BOTAN_PUBLIC_API
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
BOTAN_FUTURE_INTERNAL_HEADER
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:136
Botan
Definition: alg_id.cpp:13
Botan::secure_vector
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65