Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:08

0001 // salsa.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file salsa.h

0004 /// \brief Classes for Salsa and Salsa20 stream ciphers

0005 
0006 #ifndef CRYPTOPP_SALSA_H
0007 #define CRYPTOPP_SALSA_H
0008 
0009 #include "strciphr.h"
0010 #include "secblock.h"
0011 
0012 // Clang 3.3 integrated assembler crash on Linux. Clang 3.4 due to compiler

0013 // error with .intel_syntax, http://llvm.org/bugs/show_bug.cgi?id=24232

0014 #if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_MIXED_ASM)
0015 # define CRYPTOPP_DISABLE_SALSA_ASM 1
0016 #endif
0017 
0018 NAMESPACE_BEGIN(CryptoPP)
0019 
0020 /// \brief Salsa20 core transform

0021 /// \param data the data to transform

0022 /// \param rounds the number of rounds

0023 /// \details Several algorithms, like CryptoBox and Scrypt, require access to

0024 ///  the core Salsa20 transform. The current Crypto++ implementation does not

0025 ///  lend itself to disgorging the Salsa20 cipher from the Salsa20 core transform.

0026 ///  Instead Salsa20_Core is provided with customary accelerations.

0027 void Salsa20_Core(word32* data, unsigned int rounds);
0028 
0029 /// \brief Salsa20 stream cipher information

0030 /// \since Crypto++ 5.4

0031 struct Salsa20_Info : public VariableKeyLength<32, 16, 32, 16, SimpleKeyingInterface::UNIQUE_IV, 8>
0032 {
0033     static std::string StaticAlgorithmName() {return "Salsa20";}
0034 };
0035 
0036 /// \brief Salsa20 stream cipher operation

0037 /// \since Crypto++ 5.4

0038 class CRYPTOPP_NO_VTABLE Salsa20_Policy : public AdditiveCipherConcretePolicy<word32, 16>
0039 {
0040 protected:
0041     Salsa20_Policy() : m_rounds(ROUNDS) {}
0042     void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
0043     void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
0044     void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
0045     bool CipherIsRandomAccess() const {return true;}
0046     void SeekToIteration(lword iterationCount);
0047 
0048 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
0049     unsigned int GetAlignment() const;
0050     unsigned int GetOptimalBlockSize() const;
0051 #endif
0052 
0053     std::string AlgorithmProvider() const;
0054 
0055     CRYPTOPP_CONSTANT(ROUNDS = 20);  // Default rounds

0056     FixedSizeAlignedSecBlock<word32, 16> m_state;
0057     int m_rounds;
0058 };
0059 
0060 /// \brief Salsa20 stream cipher

0061 /// \details Salsa20 provides a variable number of rounds: 8, 12 or 20. The default number of rounds is 20.

0062 /// \sa <A HREF="https://cr.yp.to/snuffle/salsafamily-20071225.pdf">The Salsa20

0063 ///  family of stream ciphers (20071225)</A>,

0064 ///  <A HREF="https://cr.yp.to/snuffle.html">Snuffle 2005: the Salsa20 encryption

0065 ///  function</A> and <A HREF="https://www.cryptopp.com/wiki/Salsa20">Salsa20</A>

0066 /// \since Crypto++ 5.4

0067 struct Salsa20 : public Salsa20_Info, public SymmetricCipherDocumentation
0068 {
0069     typedef SymmetricCipherFinal<ConcretePolicyHolder<Salsa20_Policy, AdditiveCipherTemplate<> >, Salsa20_Info> Encryption;
0070     typedef Encryption Decryption;
0071 };
0072 
0073 /// \brief XSalsa20 stream cipher information

0074 /// \since Crypto++ 5.4

0075 struct XSalsa20_Info : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 24>
0076 {
0077     static std::string StaticAlgorithmName() {return "XSalsa20";}
0078 };
0079 
0080 /// \brief XSalsa20 stream cipher operation

0081 /// \since Crypto++ 5.4

0082 class CRYPTOPP_NO_VTABLE XSalsa20_Policy : public Salsa20_Policy
0083 {
0084 public:
0085     void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
0086     void CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length);
0087 
0088 protected:
0089     FixedSizeSecBlock<word32, 8> m_key;
0090 };
0091 
0092 /// \brief XSalsa20 stream cipher

0093 /// \details XSalsa20 provides a variable number of rounds: 8, 12 or 20. The default number of rounds is 20.

0094 /// \sa <a href="http://www.cryptolounge.org/wiki/XSalsa20">XSalsa20</a>

0095 /// \since Crypto++ 5.4

0096 struct XSalsa20 : public XSalsa20_Info, public SymmetricCipherDocumentation
0097 {
0098     typedef SymmetricCipherFinal<ConcretePolicyHolder<XSalsa20_Policy, AdditiveCipherTemplate<> >, XSalsa20_Info> Encryption;
0099     typedef Encryption Decryption;
0100 };
0101 
0102 NAMESPACE_END
0103 
0104 #endif