salsa.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // salsa.h - originally written and placed in the public domain by Wei Dai
  2. /// \file salsa.h
  3. /// \brief Classes for Salsa and Salsa20 stream ciphers
  4. #ifndef CRYPTOPP_SALSA_H
  5. #define CRYPTOPP_SALSA_H
  6. #include "strciphr.h"
  7. #include "secblock.h"
  8. // Clang 3.3 integrated assembler crash on Linux. Clang 3.4 due to compiler
  9. // error with .intel_syntax, http://llvm.org/bugs/show_bug.cgi?id=24232
  10. #if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_MIXED_ASM)
  11. # define CRYPTOPP_DISABLE_SALSA_ASM 1
  12. #endif
  13. NAMESPACE_BEGIN(CryptoPP)
  14. /// \brief Salsa20 core transform
  15. /// \param data the data to transform
  16. /// \param rounds the number of rounds
  17. /// \details Several algorithms, like CryptoBox and Scrypt, require access to
  18. /// the core Salsa20 transform. The current Crypto++ implementation does not
  19. /// lend itself to disgorging the Salsa20 cipher from the Salsa20 core transform.
  20. /// Instead Salsa20_Core is provided with customary accelerations.
  21. void Salsa20_Core(word32* data, unsigned int rounds);
  22. /// \brief Salsa20 stream cipher information
  23. /// \since Crypto++ 5.4
  24. struct Salsa20_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>
  25. {
  26. static std::string StaticAlgorithmName() {return "Salsa20";}
  27. };
  28. /// \brief Salsa20 stream cipher operation
  29. /// \since Crypto++ 5.4
  30. class CRYPTOPP_NO_VTABLE Salsa20_Policy : public AdditiveCipherConcretePolicy<word32, 16>
  31. {
  32. protected:
  33. Salsa20_Policy() : m_rounds(ROUNDS) {}
  34. void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
  35. void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
  36. void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
  37. bool CipherIsRandomAccess() const {return true;}
  38. void SeekToIteration(lword iterationCount);
  39. #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
  40. unsigned int GetAlignment() const;
  41. unsigned int GetOptimalBlockSize() const;
  42. #endif
  43. std::string AlgorithmProvider() const;
  44. CRYPTOPP_CONSTANT(ROUNDS = 20); // Default rounds
  45. FixedSizeAlignedSecBlock<word32, 16> m_state;
  46. int m_rounds;
  47. };
  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="https://cr.yp.to/snuffle/salsafamily-20071225.pdf">The Salsa20
  51. /// family of stream ciphers (20071225)</A>,
  52. /// <A HREF="https://cr.yp.to/snuffle.html">Snuffle 2005: the Salsa20 encryption
  53. /// function</A> and <A HREF="https://www.cryptopp.com/wiki/Salsa20">Salsa20</A>
  54. /// \since Crypto++ 5.4
  55. struct Salsa20 : public Salsa20_Info, public SymmetricCipherDocumentation
  56. {
  57. typedef SymmetricCipherFinal<ConcretePolicyHolder<Salsa20_Policy, AdditiveCipherTemplate<> >, Salsa20_Info> Encryption;
  58. typedef Encryption Decryption;
  59. };
  60. /// \brief XSalsa20 stream cipher information
  61. /// \since Crypto++ 5.4
  62. struct XSalsa20_Info : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 24>
  63. {
  64. static std::string StaticAlgorithmName() {return "XSalsa20";}
  65. };
  66. /// \brief XSalsa20 stream cipher operation
  67. /// \since Crypto++ 5.4
  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. protected:
  74. FixedSizeSecBlock<word32, 8> m_key;
  75. };
  76. /// \brief XSalsa20 stream cipher
  77. /// \details XSalsa20 provides a variable number of rounds: 8, 12 or 20. The default number of rounds is 20.
  78. /// \sa <a href="http://www.cryptolounge.org/wiki/XSalsa20">XSalsa20</a>
  79. /// \since Crypto++ 5.4
  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. NAMESPACE_END
  86. #endif