Back to home page

EIC code displayed by LXR

 
 

    


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

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

0002 
0003 /// \file panama.h

0004 /// \brief Classes for Panama hash and stream cipher

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

0014 //#if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_MIXED_ASM)

0015 //# define CRYPTOPP_DISABLE_PANAMA_ASM

0016 //#endif

0017 
0018 // https://github.com/weidai11/cryptopp/issues/758

0019 #define CRYPTOPP_DISABLE_PANAMA_ASM 1
0020 
0021 NAMESPACE_BEGIN(CryptoPP)
0022 
0023 // Base class, do not use directly

0024 template <class B>
0025 class CRYPTOPP_NO_VTABLE Panama
0026 {
0027 public:
0028     virtual ~Panama() {}
0029     std::string AlgorithmProvider() const;
0030     void Reset();
0031     void Iterate(size_t count, const word32 *p=NULLPTR, byte *output=NULLPTR, const byte *input=NULLPTR, KeystreamOperation operation=WRITE_KEYSTREAM);
0032 
0033 protected:
0034     typedef word32 Stage[8];
0035     CRYPTOPP_CONSTANT(STAGES = 32);
0036 
0037     FixedSizeAlignedSecBlock<word32, 20 + 8*32> m_state;
0038 };
0039 
0040 namespace Weak {
0041 /// \brief Panama hash

0042 /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#Panama">Panama Hash</a>

0043 template <class B = LittleEndian>
0044 class PanamaHash : protected Panama<B>, public AlgorithmImpl<IteratedHash<word32, NativeByteOrder, 32>, PanamaHash<B> >
0045 {
0046 public:
0047     CRYPTOPP_CONSTANT(DIGESTSIZE = 32);
0048     virtual ~PanamaHash() {}
0049     PanamaHash() {Panama<B>::Reset();}
0050     unsigned int DigestSize() const {return DIGESTSIZE;}
0051     void TruncatedFinal(byte *hash, size_t size);
0052     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
0053     std::string AlgorithmProvider() const {return Panama<B>::AlgorithmProvider();} // Fix https://github.com/weidai11/cryptopp/issues/801

0054 
0055 protected:
0056     void Init() {Panama<B>::Reset();}
0057     void HashEndianCorrectedBlock(const word32 *data) {this->Iterate(1, data);} // push

0058     size_t HashMultipleBlocks(const word32 *input, size_t length);
0059     word32* StateBuf() {return NULLPTR;}
0060 
0061     FixedSizeSecBlock<word32, 8> m_buf;
0062 };
0063 }
0064 
0065 /// \brief MAC construction using a hermetic hash function

0066 template <class T_Hash, class T_Info = T_Hash>
0067 class HermeticHashFunctionMAC : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<MessageAuthenticationCode, VariableKeyLength<32, 0, INT_MAX> > >, T_Info>
0068 {
0069 public:
0070     void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
0071     {
0072         CRYPTOPP_UNUSED(params);
0073 
0074         m_key.Assign(key, length);
0075         Restart();
0076     }
0077 
0078     void Restart()
0079     {
0080         m_hash.Restart();
0081         m_keyed = false;
0082     }
0083 
0084     void Update(const byte *input, size_t length)
0085     {
0086         if (!m_keyed)
0087             KeyHash();
0088         m_hash.Update(input, length);
0089     }
0090 
0091     void TruncatedFinal(byte *digest, size_t digestSize)
0092     {
0093         if (!m_keyed)
0094             KeyHash();
0095         m_hash.TruncatedFinal(digest, digestSize);
0096         m_keyed = false;
0097     }
0098 
0099     unsigned int DigestSize() const
0100         {return m_hash.DigestSize();}
0101     unsigned int BlockSize() const
0102         {return m_hash.BlockSize();}
0103     unsigned int OptimalBlockSize() const
0104         {return m_hash.OptimalBlockSize();}
0105     unsigned int OptimalDataAlignment() const
0106         {return m_hash.OptimalDataAlignment();}
0107 
0108 protected:
0109     void KeyHash()
0110     {
0111         m_hash.Update(m_key, m_key.size());
0112         m_keyed = true;
0113     }
0114 
0115     T_Hash m_hash;
0116     bool m_keyed;
0117     SecByteBlock m_key;
0118 };
0119 
0120 namespace Weak {
0121 /// \brief Panama message authentication code

0122 template <class B = LittleEndian>
0123 class PanamaMAC : public HermeticHashFunctionMAC<PanamaHash<B> >
0124 {
0125 public:
0126     PanamaMAC() {}
0127     PanamaMAC(const byte *key, unsigned int length)
0128         {this->SetKey(key, length);}
0129 };
0130 }
0131 
0132 /// \brief Panama stream cipher information

0133 template <class B>
0134 struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32>
0135 {
0136     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
0137 };
0138 
0139 /// \brief Panama stream cipher operation

0140 template <class B>
0141 class PanamaCipherPolicy : public AdditiveCipherConcretePolicy<word32, 8>,
0142                             public PanamaCipherInfo<B>,
0143                             protected Panama<B>
0144 {
0145 protected:
0146     virtual ~PanamaCipherPolicy() {}
0147     std::string AlgorithmProvider() const;
0148     void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
0149     void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
0150     bool CipherIsRandomAccess() const {return false;}
0151     void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
0152     unsigned int GetAlignment() const;
0153 
0154     FixedSizeSecBlock<word32, 8> m_key;
0155     FixedSizeSecBlock<word32, 8> m_buf;
0156 };
0157 
0158 /// \brief Panama stream cipher

0159 /// \sa <a href="http://www.cryptolounge.org/wiki/PANAMA">Panama Stream Cipher</a>

0160 template <class B = LittleEndian>
0161 struct PanamaCipher : public PanamaCipherInfo<B>, public SymmetricCipherDocumentation
0162 {
0163     typedef SymmetricCipherFinal<ConcretePolicyHolder<PanamaCipherPolicy<B>, AdditiveCipherTemplate<> >, PanamaCipherInfo<B> > Encryption;
0164     typedef Encryption Decryption;
0165 };
0166 
0167 NAMESPACE_END
0168 
0169 #endif