panama.h
Go to the documentation of this file.
// panama.h - written and placed in the public domain by Wei Dai
//! \file panama.h
//! \brief Classes for Panama hash and stream cipher
#ifndef CRYPTOPP_PANAMA_H
#define CRYPTOPP_PANAMA_H
#include "strciphr.h"
#include "iterhash.h"
#include "secblock.h"
// Clang 3.3 integrated assembler crash on Linux. Clang 3.4 due to compiler error with .intel_syntax
#if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_INTEL_ASM)
# define CRYPTOPP_DISABLE_PANAMA_ASM
#endif
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 {
//! \class PanamaHash
//! \brief Panama hash
//! \sa <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);
CRYPTOPP_CONSTEXPR 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;}
};
}
//! \class HermeticHashFunctionMAC
//! \brief 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)
{
CRYPTOPP_UNUSED(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 {
//! \class PanamaMAC
//! \brief Panama message authentication code
template <class B = LittleEndian>
class PanamaMAC : public HermeticHashFunctionMAC<PanamaHash<B> >
{
public:
PanamaMAC() {}
PanamaMAC(const byte *key, unsigned int length)
{this->SetKey(key, length);}
};
}
//! \class PanamaCipherInfo
//! \brief Panama stream cipher information
template <class B>
struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32>
{
CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
};
//! \class PanamaCipherPolicy
//! \brief Panama stream cipher operation
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);
unsigned int GetAlignment() const;
FixedSizeSecBlock<word32, 8> m_key;
};
//! \class PanamaCipher
//! \brief Panama stream cipher
//! \sa <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
FixedSizeAlignedSecBlock< word32, 20+8 *32 >
Weak::PanamaHash
Panama hash.
Definition: panama.h:40
BIG_ENDIAN_ORDER
@ BIG_ENDIAN_ORDER
byte order is big-endian
Definition: cryptlib.h:127
SymmetricCipherDocumentation
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher.
Definition: seckey.h:439
SymmetricCipherFinal
SymmetricCipher implementation.
Definition: strciphr.h:582
AdditiveCipherConcretePolicy
Base class for additive stream ciphers.
Definition: strciphr.h:188
secblock.h
Classes and functions for secure memory allocations.
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:731
Weak::PanamaHash::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: panama.h:45
Weak::PanamaHash::TruncatedFinal
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition: panama.cpp:432
FixedKeyLength
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:128
HermeticHashFunctionMAC
MAC construction using a hermetic hash function.
Definition: panama.h:60
AdditiveCipherTemplate
Base class for additive stream ciphers with SymmetricCipher interface.
Definition: strciphr.h:269
PanamaCipher
Panama stream cipher.
Definition: panama.h:155
WRITE_KEYSTREAM
@ WRITE_KEYSTREAM
Wirte the keystream to the output buffer, input is NULL.
Definition: strciphr.h:94
KeystreamOperation
KeystreamOperation
Keystream operation flags.
Definition: strciphr.h:92
Weak::PanamaMAC
Panama message authentication code.
Definition: panama.h:117
PanamaCipherPolicy
Panama stream cipher operation.
Definition: panama.h:137
FixedSizeSecBlock< word32, 8 >
Panama
Definition: panama.h:22
SecBlock::size
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:524
SecBlock::Assign
void Assign(const T *ptr, size_type len)
Set contents and size from an array.
Definition: secblock.h:544
PanamaCipherInfo
Panama stream cipher information.
Definition: panama.h:129
CryptoPP
Crypto++ library namespace.
Weak
Namespace containing weak and wounded algorithms.
Definition: arc4.cpp:14
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:277
AlgorithmImpl
Base class for identifying alogorithm.
Definition: simple.h:38
strciphr.h
Classes for implementing stream ciphers.