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 ¶ms, 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 | |
---|
43 | FixedSizeAlignedSecBlock<word32, 16> m_state; |
---|
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> |
---|
51 | struct Salsa20 : public Salsa20_Info, public SymmetricCipherDocumentation |
---|
52 | { |
---|
53 | typedef SymmetricCipherFinal<ConcretePolicyHolder<Salsa20_Policy, AdditiveCipherTemplate<> >, Salsa20_Info> Encryption; |
---|
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 ¶ms, const byte *key, size_t length); |
---|
70 | void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length); |
---|
71 | |
---|
72 | protected: |
---|
73 | FixedSizeSecBlock<word32, 8> m_key; |
---|
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> |
---|
80 | struct XSalsa20 : public XSalsa20_Info, public SymmetricCipherDocumentation |
---|
81 | { |
---|
82 | typedef SymmetricCipherFinal<ConcretePolicyHolder<XSalsa20_Policy, AdditiveCipherTemplate<> >, XSalsa20_Info> Encryption; |
---|
83 | typedef Encryption Decryption; |
---|
84 | }; |
---|
85 | |
---|
86 | NAMESPACE_END |
---|
87 | |
---|
88 | #endif |
---|