Crypto++  7.0
Free C++ class library of cryptographic schemes
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 and Bernstein's reference ChaCha
3 // family implementation at http://cr.yp.to/chacha.html.
4 
5 /// \file chacha.h
6 /// \brief Classes for ChaCha8, ChaCha12 and ChaCha20 stream ciphers
7 /// \details Crypto++ provides Bernstein and ECRYPT's ChaCha from <a href="http://cr.yp.to/chacha/chacha-20080128.pdf">ChaCha,
8 /// a variant of Salsa20</a> (2008.01.28). Bernstein's implementation is _slightly_ different from the TLS working group's
9 /// implementation for cipher suites <tt>TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256</tt>,
10 /// <tt>TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256</tt>, and <tt>TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256</tt>.
11 /// \since Crypto++ 5.6.4
12 
13 #ifndef CRYPTOPP_CHACHA_H
14 #define CRYPTOPP_CHACHA_H
15 
16 #include "strciphr.h"
17 #include "secblock.h"
18 
19 NAMESPACE_BEGIN(CryptoPP)
20 
21 /// \brief ChaCha stream cipher information
22 /// \since Crypto++ 5.6.4
23 template <unsigned int R>
24 struct ChaCha_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>, public FixedRounds<R>
25 {
26  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {
27  return (R==8?"ChaCha8":(R==12?"ChaCha12":(R==20?"ChaCha20":"ChaCha")));
28  }
29 };
30 
31 /// \brief ChaCha stream cipher implementation
32 /// \since Crypto++ 5.6.4
33 template <unsigned int R>
34 class CRYPTOPP_NO_VTABLE ChaCha_Policy : public AdditiveCipherConcretePolicy<word32, 16>
35 {
36 protected:
37  CRYPTOPP_CONSTANT(ROUNDS=FixedRounds<R>::ROUNDS)
38 
39  void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
40  void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
41  void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
42  bool CipherIsRandomAccess() const {return false;} // TODO
43  void SeekToIteration(lword iterationCount);
44  unsigned int GetAlignment() const;
45  unsigned int GetOptimalBlockSize() const;
46 
48 };
49 
50 /// \brief ChaCha8 stream cipher
51 /// \sa <a href="http://cr.yp.to/chacha/chacha-20080128.pdf">ChaCha, a variant of Salsa20</a> (2008.01.28).
52 /// \since Crypto++ 5.6.4
54 {
56  typedef Encryption Decryption;
57 };
58 
59 /// \brief ChaCha12 stream cipher
60 /// \details Bernstein and ECRYPT's ChaCha is _slightly_ different from the TLS working group's implementation for
61 /// cipher suites <tt>TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256</tt>,
62 /// <tt>TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256</tt>, and <tt>TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256</tt>.
63 /// \sa <a href="http://cr.yp.to/chacha/chacha-20080128.pdf">ChaCha, a variant of Salsa20</a> (2008.01.28).
64 /// \since Crypto++ 5.6.4
66 {
68  typedef Encryption Decryption;
69 };
70 
71 /// \brief ChaCha20 stream cipher
72 /// \sa <a href="http://cr.yp.to/chacha/chacha-20080128.pdf">ChaCha, a variant of Salsa20</a> (2008.01.28).
73 /// \details Bernstein and ECRYPT's ChaCha is _slightly_ different from the TLS working group's implementation for
74 /// cipher suites <tt>TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256</tt>,
75 /// <tt>TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256</tt>, and <tt>TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256</tt>.
76 /// \since Crypto++ 5.6.4
78 {
80  typedef Encryption Decryption;
81 };
82 
83 NAMESPACE_END
84 
85 #endif // CRYPTOPP_CHACHA_H
ChaCha stream cipher information.
Definition: chacha.h:24
Base class for additive stream ciphers.
Definition: strciphr.h:181
unsigned int GetAlignment() const
Provides data alignment requirements.
Definition: strciphr.h:191
ChaCha8 stream cipher.
Definition: chacha.h:53
Classes and functions for secure memory allocations.
ChaCha stream cipher implementation.
Definition: chacha.h:34
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)=0
Operates the keystream.
Inherited by algorithms with fixed number of rounds.
Definition: seckey.h:75
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:188
const char * IV()
ConstByteArrayParameter, also accepts const byte * for backwards compatibility.
Definition: argnames.h:21
Classes for implementing stream ciphers.
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher...
Definition: seckey.h:435
KeystreamOperation
Keystream operation flags.
Definition: strciphr.h:88
Crypto++ library namespace.
SymmetricCipher implementation.
Definition: strciphr.h:571
Base class for additive stream ciphers with SymmetricCipher interface.
Definition: strciphr.h:261
ChaCha12 stream cipher.
Definition: chacha.h:65
Interface for retrieving values given their names.
Definition: cryptlib.h:290
ChaCha20 stream cipher.
Definition: chacha.h:77