panama.h
#ifndef CRYPTOPP_PANAMA_H
#define CRYPTOPP_PANAMA_H
#include "strciphr.h"
#include "iterhash.h"
NAMESPACE_BEGIN(CryptoPP)
/// base class, do not use directly
template <class B>
class CRYPTOPP_NO_VTABLE Panama
{
public:
void Reset();
void Iterate(size_t count, const word32 *p=NULL, byte *output=NULL, const byte *input=NULL, KeystreamOperation operation=WRITE_KEYSTREAM);
protected:
typedef word32 Stage[8];
CRYPTOPP_CONSTANT(STAGES = 32)
FixedSizeAlignedSecBlock<word32, 20 + 8*32> m_state;
};
namespace Weak {
/// <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> >
{
public:
CRYPTOPP_CONSTANT(DIGESTSIZE = 32)
PanamaHash() {Panama<B>::Reset();}
unsigned int DigestSize() const {return DIGESTSIZE;}
void TruncatedFinal(byte *hash, size_t size);
static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
protected:
void Init() {Panama<B>::Reset();}
void HashEndianCorrectedBlock(const word32 *data) {this->Iterate(1, data);} // push
size_t HashMultipleBlocks(const word32 *input, size_t length);
word32* StateBuf() {return NULL;}
};
}
//! MAC construction using a hermetic hash function
template <class T_Hash, class T_Info = T_Hash>
class HermeticHashFunctionMAC : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<MessageAuthenticationCode, VariableKeyLength<32, 0, INT_MAX> > >, T_Info>
{
public:
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
{
m_key.Assign(key, length);
Restart();
}
void Restart()
{
m_hash.Restart();
m_keyed = false;
}
void Update(const byte *input, size_t length)
{
if (!m_keyed)
KeyHash();
m_hash.Update(input, length);
}
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();}
protected:
void KeyHash()
{
m_hash.Update(m_key, m_key.size());
m_keyed = true;
}
T_Hash m_hash;
bool m_keyed;
SecByteBlock m_key;
};
namespace Weak {
/// Panama MAC
template <class B = LittleEndian>
class PanamaMAC : public HermeticHashFunctionMAC<PanamaHash<B> >
{
public:
PanamaMAC() {}
PanamaMAC(const byte *key, unsigned int length)
{this->SetKey(key, length);}
};
}
//! algorithm info
template <class B>
struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32>
{
static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
};
//! _
template <class B>
class PanamaCipherPolicy : public AdditiveCipherConcretePolicy<word32, 8>,
public PanamaCipherInfo<B>,
protected Panama<B>
{
protected:
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);
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X64
unsigned int GetAlignment() const;
#endif
FixedSizeSecBlock<word32, 8> m_key;
};
//! <a href="http://www.cryptolounge.org/wiki/PANAMA">Panama Stream Cipher</a>
template <class B = LittleEndian>
struct PanamaCipher : public PanamaCipherInfo<B>, public SymmetricCipherDocumentation
{
typedef SymmetricCipherFinal<ConcretePolicyHolder<PanamaCipherPolicy<B>, AdditiveCipherTemplate<> >, PanamaCipherInfo<B> > Encryption;
typedef Encryption Decryption;
};
NAMESPACE_END
#endif
FixedKeyLength
to be inherited by keyed algorithms with fixed key length
Definition: seckey.h:66
Weak::PanamaHash::DigestSize
unsigned int DigestSize() const
size of the hash/digest/MAC returned by Final()
Definition: panama.h:32
SimpleKeyingInterface::SetKey
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
set or reset the key of this object
AdditiveCipherConcretePolicy
Definition: strciphr.h:84
HermeticHashFunctionMAC::Update
void Update(const byte *input, size_t length)
process more input
Definition: panama.h:61
SecBlock< byte >
HermeticHashFunctionMAC::OptimalDataAlignment
unsigned int OptimalDataAlignment() const
returns how input should be aligned for optimal performance
Definition: panama.h:82
Weak::PanamaHash::TruncatedFinal
void TruncatedFinal(byte *hash, size_t size)
truncated version of Final()
HermeticHashFunctionMAC::BlockSize
unsigned int BlockSize() const
block size of underlying compression function, or 0 if not block based
Definition: panama.h:78
HermeticHashFunctionMAC::TruncatedFinal
void TruncatedFinal(byte *digest, size_t digestSize)
truncated version of Final()
Definition: panama.h:68
SecBlock::Assign
void Assign(const T *t, size_type len)
set contents and size
Definition: secblock.h:310
FixedSizeAlignedSecBlock< word32, 20+8 *32 >
PanamaCipher
Panama Stream Cipher
Definition: panama.h:136
Weak::PanamaMAC
Panama MAC.
Definition: panama.h:100
HermeticHashFunctionMAC
MAC construction using a hermetic hash function.
Definition: panama.h:46
FixedSizeSecBlock< word32, 8 >
HermeticHashFunctionMAC::DigestSize
unsigned int DigestSize() const
size of the hash/digest/MAC returned by Final()
Definition: panama.h:76
HermeticHashFunctionMAC::OptimalBlockSize
unsigned int OptimalBlockSize() const
input to Update() should have length a multiple of this for optimal speed
Definition: panama.h:80
strciphr.h
SymmetricCipherDocumentation
Each class derived from this one defines two types, Encryption and Decryption, both of which implemen...
Definition: seckey.h:201
PanamaCipherPolicy
_
Definition: panama.h:118
Panama
base class, do not use directly
Definition: panama.h:11
PanamaCipherInfo
algorithm info
Definition: panama.h:111
HermeticHashFunctionMAC::Restart
void Restart()
discard the current state, and restart with a new message
Definition: panama.h:55
SymmetricCipherFinal
_
Definition: strciphr.h:278
AdditiveCipherTemplate
Definition: strciphr.h:134
Weak::PanamaHash
Panama Hash
Definition: panama.h:27
NameValuePairs
interface for retrieving values given their names
Definition: cryptlib.h:224
AlgorithmImpl
_
Definition: simple.h:24