- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# panama.h

    00001 #ifndef CRYPTOPP_PANAMA_H
    00002 #define CRYPTOPP_PANAMA_H
    00003
    00004 #include "seckey.h"
    00005 #include "secblock.h"
    00006 #include "iterhash.h"
    00007 #include "strciphr.h"
    00008
    00009 NAMESPACE_BEGIN(CryptoPP)
    00010
    00011 /// base class, do not use directly
    00012 template <class B>
    00013 class CRYPTOPP_NO_VTABLE Panama
    00014 {
    00015 public:
    00016         void Reset();
    00017         void Iterate(size_t count, const word32 *p=NULL, word32 *z=NULL, const word32 *y=NULL);
    00018
    00019 protected:
    00020         typedef word32 Stage[8];
    00021         enum {STAGES = 32};
    00022
    00023         FixedSizeSecBlock<word32, 17*2 + STAGES*sizeof(Stage)> m_state;
    00024         unsigned int m_bstart;
    00025 };
    00026
    00027 /// <a href="http://www.weidai.com/scan-mirror/md.html#Panama">Panama Hash</a>
    00028 template <class B = LittleEndian>
    00029 class PanamaHash : protected Panama<B>, public AlgorithmImpl<IteratedHash<word32, NativeByteOrder, 32>, PanamaHash<B> >
    00030 {
    00031 public:
    00032         enum {DIGESTSIZE = 32};
    00033         PanamaHash() {Panama<B>::Reset();}
    00034         unsigned int DigestSize() const {return DIGESTSIZE;}
    00035         void TruncatedFinal(byte *hash, size_t size);
    00036         static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
    00037
    00038 protected:
    00039         void Init() {Panama<B>::Reset();}
    00040         void HashEndianCorrectedBlock(const word32 *data) {this->Iterate(1, data);}     // push
    00041         size_t HashMultipleBlocks(const word32 *input, size_t length);
    00042 };
    00043
    00044 //! MAC construction using a hermetic hash function
    00045 template <class T_Hash, class T_Info = T_Hash>
    00046 class HermeticHashFunctionMAC : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<MessageAuthenticationCode, VariableKeyLength<32, 0, UINT_MAX> > >, T_Info>
    00047 {
    00048 public:
    00049         void SetKey(const byte *key, size_t length, const NameValuePairs &params = g_nullNameValuePairs)
    00050         {
    00051                 m_key.Assign(key, length);
    00052                 Restart();
    00053         }
    00054
    00055         void Restart()
    00056         {
    00057                 m_hash.Restart();
    00058                 m_keyed = false;
    00059         }
    00060
    00061         void Update(const byte *input, size_t length)
    00062         {
    00063                 if (!m_keyed)
    00064                         KeyHash();
    00065                 m_hash.Update(input, length);
    00066         }
    00067
    00068         void TruncatedFinal(byte *digest, size_t digestSize)
    00069         {
    00070                 if (!m_keyed)
    00071                         KeyHash();
    00072                 m_hash.TruncatedFinal(digest, digestSize);
    00073                 m_keyed = false;
    00074         }
    00075
    00076         unsigned int DigestSize() const
    00077                 {return m_hash.DigestSize();}
    00078         unsigned int BlockSize() const
    00079                 {return m_hash.BlockSize();}
    00080         unsigned int OptimalBlockSize() const
    00081                 {return m_hash.OptimalBlockSize();}
    00082         unsigned int OptimalDataAlignment() const
    00083                 {return m_hash.OptimalDataAlignment();}
    00084
    00085 protected:
    00086         void KeyHash()
    00087         {
    00088                 m_hash.Update(m_key, m_key.size());
    00089                 m_keyed = true;
    00090         }
    00091
    00092         T_Hash m_hash;
    00093         bool m_keyed;
    00094         SecByteBlock m_key;
    00095 };
    00096
    00097 /// Panama MAC
    00098 template <class B = LittleEndian>
    00099 class PanamaMAC : public HermeticHashFunctionMAC<PanamaHash<B> >
    00100 {
    00101 public:
    00102         PanamaMAC() {}
    00103         PanamaMAC(const byte *key, unsigned int length)
    00104                 {this->SetKey(key, length);}
    00105 };
    00106
    00107 //! algorithm info
    00108 template <class B>
    00109 struct PanamaCipherInfo : public VariableKeyLength<32, 32, 64, 32, SimpleKeyingInterface::NOT_RESYNCHRONIZABLE>
    00110 {
    00111         static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
    00112 };
    00113
    00114 //! _
    00115 template <class B>
    00116 class PanamaCipherPolicy : public AdditiveCipherConcretePolicy<word32, 8>,
    00117                                                         public PanamaCipherInfo<B>,
    00118                                                         protected Panama<B>
    00119 {
    00120 protected:
    00121         void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
    00122         void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
    00123         bool IsRandomAccess() const {return false;}
    00124 };
    00125
    00126 //! <a href="http://www.weidai.com/scan-mirror/cs.html#Panama">Panama Stream Cipher</a>
    00127 template <class B = LittleEndian>
    00128 struct PanamaCipher : public PanamaCipherInfo<B>, public SymmetricCipherDocumentation
    00129 {
    00130         typedef SymmetricCipherFinal<ConcretePolicyHolder<PanamaCipherPolicy<B>, AdditiveCipherTemplate<> > > Encryption;
    00131         typedef Encryption Decryption;
    00132 };
    00133
    00134 NAMESPACE_END
    00135
    00136 #endif