Crypto++  7.0
Free C++ class library of cryptographic schemes
salsa.h
Go to the documentation of this file.
1 // salsa.h - originally written and placed in the public domain by Wei Dai
2 
3 /// \file salsa.h
4 /// \brief Classes for Salsa and Salsa20 stream ciphers
5 
6 #ifndef CRYPTOPP_SALSA_H
7 #define CRYPTOPP_SALSA_H
8 
9 #include "strciphr.h"
10 #include "secblock.h"
11 
12 // "Inline assembly operands don't work with .intel_syntax", http://llvm.org/bugs/show_bug.cgi?id=24232
13 #if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_INTEL_ASM)
14 # define CRYPTOPP_DISABLE_SALSA_ASM
15 #endif
16 
17 NAMESPACE_BEGIN(CryptoPP)
18 
19 /// \brief Salsa20 core transform
20 /// \param data the data to transform
21 /// \param rounds the number of rounds
22 /// \details Several algorithms, like CryptoBox and Scrypt, require access to
23 /// the core Salsa20 transform. The current Crypto++ implementation does not
24 /// lend itself to disgorging the Salsa20 cipher from the Salsa20 core transform.
25 /// Instead Salsa20_Core is provided with customary accelerations.
26 void Salsa20_Core(word32* data, unsigned int rounds);
27 
28 /// \brief Salsa20 stream cipher information
29 struct Salsa20_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>
30 {
31  static std::string StaticAlgorithmName() {return "Salsa20";}
32 };
33 
34 /// \brief Salsa20 stream cipher operation
35 class CRYPTOPP_NO_VTABLE Salsa20_Policy : public AdditiveCipherConcretePolicy<word32, 16>
36 {
37 protected:
38  void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
39  void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
40  void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
41  bool CipherIsRandomAccess() const {return true;}
42  void SeekToIteration(lword iterationCount);
43 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
44  unsigned int GetAlignment() const;
45  unsigned int GetOptimalBlockSize() const;
46 #endif
47 
49  int m_rounds;
50 };
51 
52 /// \brief Salsa20 stream cipher
53 /// \details Salsa20 provides a variable number of rounds: 8, 12 or 20. The default number of rounds is 20.
54 /// \sa <a href="http://www.cryptolounge.org/wiki/XSalsa20">XSalsa20</a>
56 {
58  typedef Encryption Decryption;
59 };
60 
61 /// \brief XSalsa20 stream cipher information
62 struct XSalsa20_Info : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 24>
63 {
64  static std::string StaticAlgorithmName() {return "XSalsa20";}
65 };
66 
67 /// \brief XSalsa20 stream cipher operation
68 class CRYPTOPP_NO_VTABLE XSalsa20_Policy : public Salsa20_Policy
69 {
70 public:
71  void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
72  void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
73 
74 protected:
76 };
77 
78 /// \brief XSalsa20 stream cipher
79 /// \details XSalsa20 provides a variable number of rounds: 8, 12 or 20. The default number of rounds is 20.
80 /// \sa <a href="http://www.cryptolounge.org/wiki/XSalsa20">XSalsa20</a>
82 {
84  typedef Encryption Decryption;
85 };
86 
87 NAMESPACE_END
88 
89 #endif
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:147
Base class for additive stream ciphers.
Definition: strciphr.h:181
XSalsa20 stream cipher information.
Definition: salsa.h:62
unsigned int GetAlignment() const
Provides data alignment requirements.
Definition: strciphr.h:191
Salsa20 stream cipher information.
Definition: salsa.h:29
Classes and functions for secure memory allocations.
XSalsa20 stream cipher.
Definition: salsa.h:81
XSalsa20 stream cipher operation.
Definition: salsa.h:68
void Salsa20_Core(word32 *data, unsigned int rounds)
Salsa20 core transform.
Definition: salsa.cpp:39
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)=0
Operates the keystream.
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
Salsa20 stream cipher.
Definition: salsa.h:55
Crypto++ library namespace.
SymmetricCipher implementation.
Definition: strciphr.h:571
Salsa20 stream cipher operation.
Definition: salsa.h:35
Interface for retrieving values given their names.
Definition: cryptlib.h:290