Security Scol plugin
chacha.h
Go to the documentation of this file.
1// chacha.h - written and placed in the public domain by Jeffrey Walton.
2// Based on Wei Dai's Salsa20, Botan's SSE2 implementation,
3// and Bernstein's reference ChaCha family implementation at
4// http://cr.yp.to/chacha.html.
5
6// The library added Bernstein's ChaCha classes at Crypto++ 5.6.4. The IETF
7// uses a slightly different implementation than Bernstein, and the IETF
8// ChaCha and XChaCha classes were added at Crypto++ 8.1. We wanted to maintain
9// ABI compatibility at the 8.1 release so the original ChaCha classes were not
10// disturbed. Instead new classes were added for IETF ChaCha. The back-end
11// implementation shares code as expected, however.
12
28
29#ifndef CRYPTOPP_CHACHA_H
30#define CRYPTOPP_CHACHA_H
31
32#include "strciphr.h"
33#include "secblock.h"
34
35NAMESPACE_BEGIN(CryptoPP)
36
37
38
39
41struct ChaCha_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>
42{
50 static const char* StaticAlgorithmName() {
51 return "ChaCha";
52 }
53};
54
57class CRYPTOPP_NO_VTABLE ChaCha_Policy : public AdditiveCipherConcretePolicy<word32, 16>
58{
59public:
60 virtual ~ChaCha_Policy() {}
61 ChaCha_Policy() : m_rounds(ROUNDS) {}
62
63protected:
64 void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
65 void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
66 void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
67 bool CipherIsRandomAccess() const {return true;}
68 void SeekToIteration(lword iterationCount);
69 unsigned int GetAlignment() const;
70 unsigned int GetOptimalBlockSize() const;
71
72 std::string AlgorithmName() const;
73 std::string AlgorithmProvider() const;
74
75 CRYPTOPP_CONSTANT(ROUNDS = 20); // Default rounds
77 unsigned int m_rounds;
78};
79
93
95
98struct ChaChaTLS_Info : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 12>, FixedRounds<20>
99{
107 static const char* StaticAlgorithmName() {
108 return "ChaChaTLS";
109 }
110};
111
114class CRYPTOPP_NO_VTABLE ChaChaTLS_Policy : public AdditiveCipherConcretePolicy<word32, 16>
115{
116public:
117 virtual ~ChaChaTLS_Policy() {}
118 ChaChaTLS_Policy() : m_counter(0) {}
119
120protected:
121 void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
122 void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
123 void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
124 bool CipherIsRandomAccess() const {return true;}
125 void SeekToIteration(lword iterationCount);
126 unsigned int GetAlignment() const;
127 unsigned int GetOptimalBlockSize() const;
128
129 std::string AlgorithmName() const;
130 std::string AlgorithmProvider() const;
131
133 unsigned int m_counter;
134 CRYPTOPP_CONSTANT(ROUNDS = ChaChaTLS_Info::ROUNDS);
135 CRYPTOPP_CONSTANT(KEY = 16); // Index into m_state
136 CRYPTOPP_CONSTANT(CTR = 24); // Index into m_state
137};
138
161
163
166struct XChaCha20_Info : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 24>
167{
173 static const char* StaticAlgorithmName() {
174 return "XChaCha20";
175 }
176};
177
180class CRYPTOPP_NO_VTABLE XChaCha20_Policy : public AdditiveCipherConcretePolicy<word32, 16>
181{
182public:
183 virtual ~XChaCha20_Policy() {}
184 XChaCha20_Policy() : m_counter(0), m_rounds(ROUNDS) {}
185
186protected:
187 void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
188 void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
189 void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
190 bool CipherIsRandomAccess() const {return false;}
191 void SeekToIteration(lword iterationCount);
192 unsigned int GetAlignment() const;
193 unsigned int GetOptimalBlockSize() const;
194
195 std::string AlgorithmName() const;
196 std::string AlgorithmProvider() const;
197
199 unsigned int m_counter, m_rounds;
200 CRYPTOPP_CONSTANT(ROUNDS = 20); // Default rounds
201 CRYPTOPP_CONSTANT(KEY = 16); // Index into m_state
202};
203
220
221NAMESPACE_END
222
223#endif // CRYPTOPP_CHACHA_H
ChaCha stream cipher implementation.
Definition chacha.h:58
bool CipherIsRandomAccess() const
Flag indicating random access.
Definition chacha.h:67
IETF ChaCha20 stream cipher implementation.
Definition chacha.h:115
bool CipherIsRandomAccess() const
Flag indicating random access.
Definition chacha.h:124
Inherited by keyed algorithms with fixed key length.
Definition seckey.h:125
Inherited by algorithms with fixed number of rounds.
Definition seckey.h:53
Fixed size stack-based SecBlock with 16-byte alignment.
Definition secblock.h:1259
Interface for retrieving values given their names.
Definition cryptlib.h:322
Interface for algorithms that take byte strings as keys.
Definition cryptlib.h:642
SymmetricCipher implementation.
Definition strciphr.h:684
Inherited by keyed algorithms with variable key length.
Definition seckey.h:166
IETF XChaCha20 stream cipher implementation.
Definition chacha.h:181
bool CipherIsRandomAccess() const
Flag indicating random access.
Definition chacha.h:190
word64 lword
Large word type.
Definition config_int.h:158
Classes and functions for secure memory allocations.
Classes for implementing stream ciphers.
KeystreamOperation
Keystream operation flags.
Definition strciphr.h:88
virtual std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition strciphr.h:192
virtual unsigned int GetOptimalBlockSize() const
Provides number of ideal bytes to process.
Definition strciphr.h:123
virtual void SeekToIteration(lword iterationCount)
Seeks to a random position in the stream.
Definition strciphr.h:174
virtual void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)=0
Key the cipher.
virtual void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
Resynchronize the cipher.
Definition strciphr.h:163
Base class for additive stream ciphers.
Definition strciphr.h:202
unsigned int GetAlignment() const
Provides data alignment requirements.
Definition strciphr.h:220
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)=0
Operates the keystream.
ChaCha stream cipher information.
Definition chacha.h:42
static const char * StaticAlgorithmName()
The algorithm name.
Definition chacha.h:50
ChaCha stream cipher.
Definition chacha.h:87
SymmetricCipherFinal< ConcretePolicyHolder< ChaCha_Policy, AdditiveCipherTemplate<> >, ChaCha_Info > Encryption
ChaCha Encryption.
Definition chacha.h:89
Encryption Decryption
ChaCha Decryption.
Definition chacha.h:91
IETF ChaCha20 stream cipher information.
Definition chacha.h:99
static const char * StaticAlgorithmName()
The algorithm name.
Definition chacha.h:107
IETF ChaCha20 stream cipher.
Definition chacha.h:155
SymmetricCipherFinal< ConcretePolicyHolder< ChaChaTLS_Policy, AdditiveCipherTemplate<> >, ChaChaTLS_Info > Encryption
ChaCha-TLS Encryption.
Definition chacha.h:157
Encryption Decryption
ChaCha-TLS Decryption.
Definition chacha.h:159
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher.
Definition seckey.h:414
IETF XChaCha20 stream cipher information.
Definition chacha.h:167
static const char * StaticAlgorithmName()
The algorithm name.
Definition chacha.h:173
IETF XChaCha20 stream cipher.
Definition chacha.h:214
SymmetricCipherFinal< ConcretePolicyHolder< XChaCha20_Policy, AdditiveCipherTemplate<> >, XChaCha20_Info > Encryption
XChaCha Encryption.
Definition chacha.h:216
Encryption Decryption
XChaCha Decryption.
Definition chacha.h:218