Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.1 KiB

  1. // salsa.h - 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. // TODO: work around GCC 4.8+ issue with SSE2 ASM until the exact details are known
  9. // and fix is released. Duplicate with "valgrind ./cryptest.exe tv salsa"
  10. // "Inline assembly operands don't work with .intel_syntax", http://llvm.org/bugs/show_bug.cgi?id=24232
  11. #if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_INTEL_ASM) || (CRYPTOPP_GCC_VERSION >= 40800)
  12. # define CRYPTOPP_DISABLE_SALSA_ASM
  13. #endif
  14. NAMESPACE_BEGIN(CryptoPP)
  15. //! \class Salsa20_Info
  16. //! \brief Salsa block cipher information
  17. struct Salsa20_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>
  18. {
  19. static const char *StaticAlgorithmName() {return "Salsa20";}
  20. };
  21. class CRYPTOPP_NO_VTABLE Salsa20_Policy : public AdditiveCipherConcretePolicy<word32, 16>
  22. {
  23. protected:
  24. void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
  25. void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
  26. void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
  27. bool CipherIsRandomAccess() const {return true;}
  28. void SeekToIteration(lword iterationCount);
  29. #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64) && !defined(CRYPTOPP_DISABLE_SALSA_ASM)
  30. unsigned int GetAlignment() const;
  31. unsigned int GetOptimalBlockSize() const;
  32. #endif
  33. FixedSizeAlignedSecBlock<word32, 16> m_state;
  34. int m_rounds;
  35. };
  36. // <a href="http://www.cryptolounge.org/wiki/Salsa20">Salsa20</a>, variable rounds: 8, 12 or 20 (default 20)
  37. //! \class Salsa20
  38. //! \brief Salsa20 block cipher information
  39. //! \details Salsa20 provides a variable number of rounds: 8, 12 or 20. The default number of rounds is 20.
  40. struct Salsa20 : public Salsa20_Info, public SymmetricCipherDocumentation
  41. {
  42. typedef SymmetricCipherFinal<ConcretePolicyHolder<Salsa20_Policy, AdditiveCipherTemplate<> >, Salsa20_Info> Encryption;
  43. typedef Encryption Decryption;
  44. };
  45. //! \class XSalsa20_Info
  46. //! \brief XSalsa20 block cipher information
  47. struct XSalsa20_Info : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 24>
  48. {
  49. static const char *StaticAlgorithmName() {return "XSalsa20";}
  50. };
  51. class CRYPTOPP_NO_VTABLE XSalsa20_Policy : public Salsa20_Policy
  52. {
  53. public:
  54. void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
  55. void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
  56. protected:
  57. FixedSizeSecBlock<word32, 8> m_key;
  58. };
  59. // <a href="http://www.cryptolounge.org/wiki/XSalsa20">XSalsa20</a>, variable rounds: 8, 12 or 20 (default 20)
  60. //! \class XSalsa20
  61. //! \brief XSalsa20 block cipher information
  62. //! \details XSalsa20 provides a variable number of rounds: 8, 12 or 20. The default number of rounds is 20.
  63. struct XSalsa20 : public XSalsa20_Info, public SymmetricCipherDocumentation
  64. {
  65. typedef SymmetricCipherFinal<ConcretePolicyHolder<XSalsa20_Policy, AdditiveCipherTemplate<> >, XSalsa20_Info> Encryption;
  66. typedef Encryption Decryption;
  67. };
  68. NAMESPACE_END
  69. #endif