ext-cryptopp/panama.h

145 lines
3.9 KiB
C
Raw Normal View History

2002-10-04 17:31:41 +00:00
#ifndef CRYPTOPP_PANAMA_H
#define CRYPTOPP_PANAMA_H
#include "strciphr.h"
#include "iterhash.h"
2002-10-04 17:31:41 +00:00
NAMESPACE_BEGIN(CryptoPP)
/// base class, do not use directly
template <class B>
2003-05-16 00:53:53 +00:00
class CRYPTOPP_NO_VTABLE Panama
2002-10-04 17:31:41 +00:00
{
public:
void Reset();
void Iterate(size_t count, const word32 *p=NULL, byte *output=NULL, const byte *input=NULL, KeystreamOperation operation=WRITE_KEYSTREAM);
2002-10-04 17:31:41 +00:00
protected:
typedef word32 Stage[8];
2006-12-14 11:41:39 +00:00
CRYPTOPP_CONSTANT(STAGES = 32)
2002-10-04 17:31:41 +00:00
2007-04-15 22:54:31 +00:00
FixedSizeAlignedSecBlock<word32, 20 + 8*32> m_state;
2002-10-04 17:31:41 +00:00
};
2007-04-15 22:54:31 +00:00
namespace Weak {
2002-10-04 17:31:41 +00:00
/// <a href="http://www.weidai.com/scan-mirror/md.html#Panama">Panama Hash</a>
template <class B = LittleEndian>
class PanamaHash : protected Panama<B>, public AlgorithmImpl<IteratedHash<word32, NativeByteOrder, 32>, PanamaHash<B> >
2002-10-04 17:31:41 +00:00
{
public:
2006-12-14 11:41:39 +00:00
CRYPTOPP_CONSTANT(DIGESTSIZE = 32)
PanamaHash() {Panama<B>::Reset();}
2002-10-04 17:31:41 +00:00
unsigned int DigestSize() const {return DIGESTSIZE;}
2005-07-12 04:23:32 +00:00
void TruncatedFinal(byte *hash, size_t size);
static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
2002-10-04 17:31:41 +00:00
protected:
void Init() {Panama<B>::Reset();}
2004-06-19 08:28:09 +00:00
void HashEndianCorrectedBlock(const word32 *data) {this->Iterate(1, data);} // push
2005-07-12 04:23:32 +00:00
size_t HashMultipleBlocks(const word32 *input, size_t length);
2007-04-15 22:54:31 +00:00
word32* StateBuf() {return NULL;}
2002-10-04 17:31:41 +00:00
};
2007-04-15 22:54:31 +00:00
}
2002-10-04 17:31:41 +00:00
//! MAC construction using a hermetic hash function
template <class T_Hash, class T_Info = T_Hash>
2006-12-14 11:41:39 +00:00
class HermeticHashFunctionMAC : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<MessageAuthenticationCode, VariableKeyLength<32, 0, INT_MAX> > >, T_Info>
2002-10-04 17:31:41 +00:00
{
public:
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
2002-10-04 17:31:41 +00:00
{
m_key.Assign(key, length);
2002-10-04 17:31:41 +00:00
Restart();
}
void Restart()
{
m_hash.Restart();
m_keyed = false;
}
2005-07-12 04:23:32 +00:00
void Update(const byte *input, size_t length)
{
if (!m_keyed)
KeyHash();
m_hash.Update(input, length);
}
2005-07-12 04:23:32 +00:00
void TruncatedFinal(byte *digest, size_t digestSize)
{
if (!m_keyed)
KeyHash();
m_hash.TruncatedFinal(digest, digestSize);
m_keyed = false;
}
unsigned int DigestSize() const
{return m_hash.DigestSize();}
unsigned int BlockSize() const
{return m_hash.BlockSize();}
unsigned int OptimalBlockSize() const
{return m_hash.OptimalBlockSize();}
unsigned int OptimalDataAlignment() const
{return m_hash.OptimalDataAlignment();}
2002-10-04 17:31:41 +00:00
protected:
void KeyHash()
2002-10-04 17:31:41 +00:00
{
m_hash.Update(m_key, m_key.size());
m_keyed = true;
2002-10-04 17:31:41 +00:00
}
T_Hash m_hash;
bool m_keyed;
2002-10-04 17:31:41 +00:00
SecByteBlock m_key;
};
2007-04-15 22:54:31 +00:00
namespace Weak {
2002-10-04 17:31:41 +00:00
/// Panama MAC
template <class B = LittleEndian>
class PanamaMAC : public HermeticHashFunctionMAC<PanamaHash<B> >
2002-10-04 17:31:41 +00:00
{
public:
PanamaMAC() {}
PanamaMAC(const byte *key, unsigned int length)
2004-06-19 08:28:09 +00:00
{this->SetKey(key, length);}
2002-10-04 17:31:41 +00:00
};
2007-04-15 22:54:31 +00:00
}
2002-10-04 17:31:41 +00:00
//! algorithm info
2002-10-04 17:31:41 +00:00
template <class B>
2007-04-15 22:54:31 +00:00
struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32>
2002-10-04 17:31:41 +00:00
{
static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
};
//! _
2002-10-04 17:31:41 +00:00
template <class B>
2003-03-26 21:50:44 +00:00
class PanamaCipherPolicy : public AdditiveCipherConcretePolicy<word32, 8>,
2002-10-04 17:31:41 +00:00
public PanamaCipherInfo<B>,
protected Panama<B>
{
protected:
2005-07-12 04:23:32 +00:00
void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
bool CipherIsRandomAccess() const {return false;}
void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
2007-04-15 22:54:31 +00:00
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X64
unsigned int GetAlignment() const;
#endif
FixedSizeSecBlock<word32, 8> m_key;
2002-10-04 17:31:41 +00:00
};
2007-04-15 22:54:31 +00:00
//! <a href="http://www.cryptolounge.org/wiki/PANAMA">Panama Stream Cipher</a>
2002-10-04 17:31:41 +00:00
template <class B = LittleEndian>
struct PanamaCipher : public PanamaCipherInfo<B>, public SymmetricCipherDocumentation
{
2006-12-14 11:41:39 +00:00
typedef SymmetricCipherFinal<ConcretePolicyHolder<PanamaCipherPolicy<B>, AdditiveCipherTemplate<> >, PanamaCipherInfo<B> > Encryption;
2002-10-04 17:31:41 +00:00
typedef Encryption Decryption;
};
NAMESPACE_END
#endif