Crypto++  5.6.5
Free C++ class library of cryptographic schemes
panama.h
Go to the documentation of this file.
1 // panama.h - written and placed in the public domain by Wei Dai
2 
3 //! \file panama.h
4 //! \brief Classes for Panama hash and stream cipher
5 
6 #ifndef CRYPTOPP_PANAMA_H
7 #define CRYPTOPP_PANAMA_H
8 
9 #include "strciphr.h"
10 #include "iterhash.h"
11 #include "secblock.h"
12 
13 // Clang 3.3 integrated assembler crash on Linux. Clang 3.4 due to compiler error with .intel_syntax
14 #if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_INTEL_ASM)
15 # define CRYPTOPP_DISABLE_PANAMA_ASM
16 #endif
17 
18 NAMESPACE_BEGIN(CryptoPP)
19 
20 // Base class, do not use directly
21 template <class B>
22 class CRYPTOPP_NO_VTABLE Panama
23 {
24 public:
25  void Reset();
26  void Iterate(size_t count, const word32 *p=NULL, byte *output=NULL, const byte *input=NULL, KeystreamOperation operation=WRITE_KEYSTREAM);
27 
28 protected:
29  typedef word32 Stage[8];
30  CRYPTOPP_CONSTANT(STAGES = 32)
31 
33 };
34 
35 namespace Weak {
36 //! \class PanamaHash
37 //! \brief Panama hash
38 //! \sa <a href="http://www.weidai.com/scan-mirror/md.html#Panama">Panama Hash</a>
39 template <class B = LittleEndian>
40 class PanamaHash : protected Panama<B>, public AlgorithmImpl<IteratedHash<word32, NativeByteOrder, 32>, PanamaHash<B> >
41 {
42 public:
43  CRYPTOPP_CONSTANT(DIGESTSIZE = 32)
45  unsigned int DigestSize() const {return DIGESTSIZE;}
46  void TruncatedFinal(byte *hash, size_t size);
47  CRYPTOPP_STATIC_CONSTEXPR char* const StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
48 
49 protected:
50  void Init() {Panama<B>::Reset();}
51  void HashEndianCorrectedBlock(const word32 *data) {this->Iterate(1, data);} // push
52  size_t HashMultipleBlocks(const word32 *input, size_t length);
53  word32* StateBuf() {return NULL;}
54 };
55 }
56 
57 //! \class HermeticHashFunctionMAC
58 //! \brief MAC construction using a hermetic hash function
59 template <class T_Hash, class T_Info = T_Hash>
60 class HermeticHashFunctionMAC : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<MessageAuthenticationCode, VariableKeyLength<32, 0, INT_MAX> > >, T_Info>
61 {
62 public:
63  void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
64  {
65  CRYPTOPP_UNUSED(params);
66 
67  m_key.Assign(key, length);
68  Restart();
69  }
70 
71  void Restart()
72  {
73  m_hash.Restart();
74  m_keyed = false;
75  }
76 
77  void Update(const byte *input, size_t length)
78  {
79  if (!m_keyed)
80  KeyHash();
81  m_hash.Update(input, length);
82  }
83 
84  void TruncatedFinal(byte *digest, size_t digestSize)
85  {
86  if (!m_keyed)
87  KeyHash();
88  m_hash.TruncatedFinal(digest, digestSize);
89  m_keyed = false;
90  }
91 
92  unsigned int DigestSize() const
93  {return m_hash.DigestSize();}
94  unsigned int BlockSize() const
95  {return m_hash.BlockSize();}
96  unsigned int OptimalBlockSize() const
97  {return m_hash.OptimalBlockSize();}
98  unsigned int OptimalDataAlignment() const
99  {return m_hash.OptimalDataAlignment();}
100 
101 protected:
102  void KeyHash()
103  {
104  m_hash.Update(m_key, m_key.size());
105  m_keyed = true;
106  }
107 
108  T_Hash m_hash;
109  bool m_keyed;
110  SecByteBlock m_key;
111 };
112 
113 namespace Weak {
114 //! \class PanamaMAC
115 //! \brief Panama message authentication code
116 template <class B = LittleEndian>
117 class PanamaMAC : public HermeticHashFunctionMAC<PanamaHash<B> >
118 {
119 public:
120  PanamaMAC() {}
121  PanamaMAC(const byte *key, unsigned int length)
122  {this->SetKey(key, length);}
123 };
124 }
125 
126 //! \class PanamaCipherInfo
127 //! \brief Panama stream cipher information
128 template <class B>
129 struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32>
130 {
131  CRYPTOPP_STATIC_CONSTEXPR char* const StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
132 };
133 
134 //! \class PanamaCipherPolicy
135 //! \brief Panama stream cipher operation
136 template <class B>
138  public PanamaCipherInfo<B>,
139  protected Panama<B>
140 {
141 protected:
142  void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
143  void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
144  bool CipherIsRandomAccess() const {return false;}
145  void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
146  unsigned int GetAlignment() const;
147 
149 };
150 
151 //! \class PanamaCipher
152 //! \brief Panama stream cipher
153 //! \sa <a href="http://www.cryptolounge.org/wiki/PANAMA">Panama Stream Cipher</a>
154 template <class B = LittleEndian>
156 {
158  typedef Encryption Decryption;
159 };
160 
161 NAMESPACE_END
162 
163 #endif
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:121
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: panama.h:45
Base class for additive stream ciphers.
Definition: strciphr.h:188
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:524
Wirte the keystream to the output buffer, input is NULL.
Definition: strciphr.h:94
SecBlock typedef.
Definition: secblock.h:731
Classes and functions for secure memory allocations.
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition: panama.cpp:432
void Assign(const T *ptr, size_type len)
Set contents and size from an array.
Definition: secblock.h:544
Panama stream cipher.
Definition: panama.h:155
Panama message authentication code.
Definition: panama.h:117
MAC construction using a hermetic hash function.
Definition: panama.h:60
byte order is big-endian
Definition: cryptlib.h:128
Classes for implementing stream ciphers.
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher...
Definition: seckey.h:418
Panama stream cipher operation.
Definition: panama.h:137
Definition: panama.h:22
Panama stream cipher information.
Definition: panama.h:129
KeystreamOperation
Keystream operation flags.
Definition: strciphr.h:92
Crypto++ library namespace.
SymmetricCipher implementation.
Definition: strciphr.h:584
Base class for additive stream ciphers with SymmetricCipher interface.
Definition: strciphr.h:269
Panama hash.
Definition: panama.h:40
Namespace containing weak and wounded algorithms.
Definition: arc4.cpp:14
Interface for retrieving values given their names.
Definition: cryptlib.h:278
Base class for identifying alogorithm.
Definition: simple.h:38