- Main Page
- Namespaces
- Classes
- Files

# panama.h

    00001 #ifndef CRYPTOPP_PANAMA_H
    00002 #define CRYPTOPP_PANAMA_H
    00003
    00004 #include "strciphr.h"
    00005 #include "iterhash.h"
    00006
    00007 NAMESPACE_BEGIN(CryptoPP)
    00008
    00009 /// base class, do not use directly
    00010 template <class B>
    00011 class CRYPTOPP_NO_VTABLE Panama
    00012 {
    00013 public:
    00014         void Reset();
    00015         void Iterate(size_t count, const word32 *p=NULL, word32 *z=NULL, const word32 *y=NULL);
    00016
    00017 protected:
    00018         typedef word32 Stage[8];
    00019         CRYPTOPP_CONSTANT(STAGES = 32)
    00020
    00021         FixedSizeAlignedSecBlock<word32, 20 + 8*32> m_state;
    00022 };
    00023
    00024 namespace Weak {
    00025 /// <a href="http://www.weidai.com/scan-mirror/md.html#Panama">Panama Hash</a>
    00026 template <class B = LittleEndian>
    00027 class PanamaHash : protected Panama<B>, public AlgorithmImpl<IteratedHash<word32, NativeByteOrder, 32>, PanamaHash<B> >
    00028 {
    00029 public:
    00030         CRYPTOPP_CONSTANT(DIGESTSIZE = 32)
    00031         PanamaHash() {Panama<B>::Reset();}
    00032         unsigned int DigestSize() const {return DIGESTSIZE;}
    00033         void TruncatedFinal(byte *hash, size_t size);
    00034         static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
    00035
    00036 protected:
    00037         void Init() {Panama<B>::Reset();}
    00038         void HashEndianCorrectedBlock(const word32 *data) {this->Iterate(1, data);}     // push
    00039         size_t HashMultipleBlocks(const word32 *input, size_t length);
    00040         word32* StateBuf() {return NULL;}
    00041 };
    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, INT_MAX> > >, T_Info>
    00047 {
    00048 public:
    00049         void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
    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 namespace Weak {
    00098 /// Panama MAC
    00099 template <class B = LittleEndian>
    00100 class PanamaMAC : public HermeticHashFunctionMAC<PanamaHash<B> >
    00101 {
    00102 public:
    00103         PanamaMAC() {}
    00104         PanamaMAC(const byte *key, unsigned int length)
    00105                 {this->SetKey(key, length);}
    00106 };
    00107 }
    00108
    00109 //! algorithm info
    00110 template <class B>
    00111 struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32>
    00112 {
    00113         static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
    00114 };
    00115
    00116 //! _
    00117 template <class B>
    00118 class PanamaCipherPolicy : public AdditiveCipherConcretePolicy<word32, 8>,
    00119                                                         public PanamaCipherInfo<B>,
    00120                                                         protected Panama<B>
    00121 {
    00122 protected:
    00123         void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
    00124         void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
    00125         bool CipherIsRandomAccess() const {return false;}
    00126         void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
    00127 #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X64
    00128         unsigned int GetAlignment() const;
    00129 #endif
    00130
    00131         FixedSizeSecBlock<word32, 8> m_key;
    00132 };
    00133
    00134 //! <a href="http://www.cryptolounge.org/wiki/PANAMA">Panama Stream Cipher</a>
    00135 template <class B = LittleEndian>
    00136 struct PanamaCipher : public PanamaCipherInfo<B>, public SymmetricCipherDocumentation
    00137 {
    00138         typedef SymmetricCipherFinal<ConcretePolicyHolder<PanamaCipherPolicy<B>, AdditiveCipherTemplate<> >, PanamaCipherInfo<B> > Encryption;
    00139         typedef Encryption Decryption;
    00140 };
    00141
    00142 NAMESPACE_END
    00143
    00144 #endif