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 // Clang 3.3 integrated assembler crash on Linux. Clang 3.4 due to compiler
13 // error with .intel_syntax, http://llvm.org/bugs/show_bug.cgi?id=24232
14 #if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_MIXED_ASM)
15 # define CRYPTOPP_DISABLE_SALSA_ASM 1
16 #endif
17
18 NAMESPACE_BEGIN(CryptoPP)
19
20 /// \brief Salsa20 core transform
21 /// \param data the data to transform
22 /// \param rounds the number of rounds
23 /// \details Several algorithms, like CryptoBox and Scrypt, require access to
24 /// the core Salsa20 transform. The current Crypto++ implementation does not
25 /// lend itself to disgorging the Salsa20 cipher from the Salsa20 core transform.
26 /// Instead Salsa20_Core is provided with customary accelerations.
27 void Salsa20_Core(word32* data, unsigned int rounds);
28
29 /// \brief Salsa20 stream cipher information
30 struct Salsa20_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>
31 {
32 static std::string StaticAlgorithmName() {return "Salsa20";}
33 };
34
35 /// \brief Salsa20 stream cipher operation
36 class CRYPTOPP_NO_VTABLE Salsa20_Policy : public AdditiveCipherConcretePolicy<word32, 16>
37 {
38 protected:
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 true;}
43 void SeekToIteration(lword iterationCount);
44
45 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
46 unsigned int GetAlignment() const;
47 unsigned int GetOptimalBlockSize() const;
48 #endif
49
50 std::string AlgorithmProvider() const;
51
52 FixedSizeAlignedSecBlock<word32, 16> m_state;
53 int m_rounds;
54 };
55
56 /// \brief Salsa20 stream cipher
57 /// \details Salsa20 provides a variable number of rounds: 8, 12 or 20. The default number of rounds is 20.
58 /// \sa <a href="http://www.cryptolounge.org/wiki/XSalsa20">XSalsa20</a>
59 struct Salsa20 : public Salsa20_Info, public SymmetricCipherDocumentation
60 {
61 typedef SymmetricCipherFinal<ConcretePolicyHolder<Salsa20_Policy, AdditiveCipherTemplate<> >, Salsa20_Info> Encryption;
62 typedef Encryption Decryption;
63 };
64
65 /// \brief XSalsa20 stream cipher information
66 struct XSalsa20_Info : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 24>
67 {
68 static std::string StaticAlgorithmName() {return "XSalsa20";}
69 };
70
71 /// \brief XSalsa20 stream cipher operation
72 class CRYPTOPP_NO_VTABLE XSalsa20_Policy : public Salsa20_Policy
73 {
74 public:
75 void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
76 void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
77
78 protected:
79 FixedSizeSecBlock<word32, 8> m_key;
80 };
81
82 /// \brief XSalsa20 stream cipher
83 /// \details XSalsa20 provides a variable number of rounds: 8, 12 or 20. The default number of rounds is 20.
84 /// \sa <a href="http://www.cryptolounge.org/wiki/XSalsa20">XSalsa20</a>
85 struct XSalsa20 : public XSalsa20_Info, public SymmetricCipherDocumentation
86 {
87 typedef SymmetricCipherFinal<ConcretePolicyHolder<XSalsa20_Policy, AdditiveCipherTemplate<> >, XSalsa20_Info> Encryption;
88 typedef Encryption Decryption;
89 };
90
91 NAMESPACE_END
92
93 #endif
FixedKeyLength
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:124
AdditiveCipherConcretePolicy
Base class for additive stream ciphers.
Definition: strciphr.h:201
XSalsa20_Info
XSalsa20 stream cipher information.
Definition: salsa.h:66
AdditiveCipherConcretePolicy::GetAlignment
unsigned int GetAlignment() const
Provides data alignment requirements.
Definition: strciphr.h:213
Salsa20_Info
Salsa20 stream cipher information.
Definition: salsa.h:30
secblock.h
Classes and functions for secure memory allocations.
XSalsa20
XSalsa20 stream cipher.
Definition: salsa.h:85
XSalsa20_Policy
XSalsa20 stream cipher operation.
Definition: salsa.h:72
Salsa20_Core
void Salsa20_Core(word32 *data, unsigned int rounds)
Salsa20 core transform.
Definition: salsa.cpp:39
AdditiveCipherConcretePolicy::OperateKeystream
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)=0
Operates the keystream.
FixedSizeAlignedSecBlock< word32, 16 >
FixedSizeSecBlock< word32, 8 >
VariableKeyLength
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:165
Name::IV
const char * IV()
ConstByteArrayParameter, also accepts const byte * for backwards compatibility.
Definition: argnames.h:21
strciphr.h
Classes for implementing stream ciphers.
SymmetricCipherDocumentation
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher...
Definition: seckey.h:413
KeystreamOperation
KeystreamOperation
Keystream operation flags.
Definition: strciphr.h:88
Salsa20
Salsa20 stream cipher.
Definition: salsa.h:59
CryptoPP
Crypto++ library namespace.
SymmetricCipherFinal
SymmetricCipher implementation.
Definition: strciphr.h:664
Salsa20_Policy
Salsa20 stream cipher operation.
Definition: salsa.h:36
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:293