Crypto++  5.6.4
Free C++ class library of cryptographic schemes
salsa.h
Go to the documentation of this file.
1 // salsa.h - 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 // TODO: work around GCC 4.8+ issue with SSE2 ASM until the exact details are known
13 // and fix is released. Duplicate with "valgrind ./cryptest.exe tv salsa"
14 // "Inline assembly operands don't work with .intel_syntax", http://llvm.org/bugs/show_bug.cgi?id=24232
15 #if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_INTEL_ASM) || (CRYPTOPP_GCC_VERSION >= 40800)
16 # define CRYPTOPP_DISABLE_SALSA_ASM
17 #endif
18 
19 NAMESPACE_BEGIN(CryptoPP)
20 
21 //! \class Salsa20_Info
22 //! \brief Salsa20 stream cipher information
23 struct Salsa20_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>
24 {
25  CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "Salsa20";}
26 };
27 
28 //! \class Salsa20_Policy
29 //! \brief Salsa20 stream cipher operation
30 class CRYPTOPP_NO_VTABLE Salsa20_Policy : public AdditiveCipherConcretePolicy<word32, 16>
31 {
32 protected:
33  void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
34  void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
35  void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
36  bool CipherIsRandomAccess() const {return true;}
37  void SeekToIteration(lword iterationCount);
38 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64) && !defined(CRYPTOPP_DISABLE_SALSA_ASM)
39  unsigned int GetAlignment() const;
40  unsigned int GetOptimalBlockSize() const;
41 #endif
42 
44  int m_rounds;
45 };
46 
47 //! \class Salsa20
48 //! \brief Salsa20 stream cipher
49 //! \details Salsa20 provides a variable number of rounds: 8, 12 or 20. The default number of rounds is 20.
50 //! \sa <a href="http://www.cryptolounge.org/wiki/XSalsa20">XSalsa20</a>
52 {
54  typedef Encryption Decryption;
55 };
56 
57 //! \class XSalsa20_Info
58 //! \brief XSalsa20 stream cipher information
59 struct XSalsa20_Info : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 24>
60 {
61  CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "XSalsa20";}
62 };
63 
64 //! \class XSalsa20_Policy
65 //! \brief XSalsa20 stream cipher operation
66 class CRYPTOPP_NO_VTABLE XSalsa20_Policy : public Salsa20_Policy
67 {
68 public:
69  void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
70  void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
71 
72 protected:
74 };
75 
76 //! \class XSalsa20
77 //! \brief XSalsa20 stream cipher
78 //! \details XSalsa20 provides a variable number of rounds: 8, 12 or 20. The default number of rounds is 20.
79 //! \sa <a href="http://www.cryptolounge.org/wiki/XSalsa20">XSalsa20</a>
81 {
83  typedef Encryption Decryption;
84 };
85 
86 NAMESPACE_END
87 
88 #endif
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:128
unsigned int GetAlignment() const
Provides data alignment requirements.
Definition: strciphr.h:198
Base class for additive stream ciphers.
Definition: strciphr.h:188
XSalsa20 stream cipher information.
Definition: salsa.h:59
Salsa20 stream cipher information.
Definition: salsa.h:23
Classes and functions for secure memory allocations.
XSalsa20 stream cipher.
Definition: salsa.h:80
XSalsa20 stream cipher operation.
Definition: salsa.h:66
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)=0
Operates the keystream.
Interface for algorithms that take byte strings as keys.
Definition: cryptlib.h:529
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:177
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:439
KeystreamOperation
Keystream operation flags.
Definition: strciphr.h:92
Salsa20 stream cipher.
Definition: salsa.h:51
Crypto++ library namespace.
SymmetricCipher implementation.
Definition: strciphr.h:584
Salsa20 stream cipher operation.
Definition: salsa.h:30
Interface for retrieving values given their names.
Definition: cryptlib.h:282