Crypto++
panama.h
1 #ifndef CRYPTOPP_PANAMA_H
2 #define CRYPTOPP_PANAMA_H
3 
4 #include "strciphr.h"
5 #include "iterhash.h"
6 
7 NAMESPACE_BEGIN(CryptoPP)
8 
9 /// base class, do not use directly
10 template <class B>
11 class CRYPTOPP_NO_VTABLE Panama
12 {
13 public:
14  void Reset();
15  void Iterate(size_t count, const word32 *p=NULL, byte *output=NULL, const byte *input=NULL, KeystreamOperation operation=WRITE_KEYSTREAM);
16 
17 protected:
18  typedef word32 Stage[8];
19  CRYPTOPP_CONSTANT(STAGES = 32)
20 
22 };
23 
24 namespace Weak {
25 /// <a href="http://www.weidai.com/scan-mirror/md.html#Panama">Panama Hash</a>
26 template <class B = LittleEndian>
27 class PanamaHash : protected Panama<B>, public AlgorithmImpl<IteratedHash<word32, NativeByteOrder, 32>, PanamaHash<B> >
28 {
29 public:
30  CRYPTOPP_CONSTANT(DIGESTSIZE = 32)
32  unsigned int DigestSize() const {return DIGESTSIZE;}
33  void TruncatedFinal(byte *hash, size_t size);
34  static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
35 
36 protected:
37  void Init() {Panama<B>::Reset();}
38  void HashEndianCorrectedBlock(const word32 *data) {this->Iterate(1, data);} // push
39  size_t HashMultipleBlocks(const word32 *input, size_t length);
40  word32* StateBuf() {return NULL;}
41 };
42 }
43 
44 //! MAC construction using a hermetic hash function
45 template <class T_Hash, class T_Info = T_Hash>
46 class HermeticHashFunctionMAC : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<MessageAuthenticationCode, VariableKeyLength<32, 0, INT_MAX> > >, T_Info>
47 {
48 public:
49  void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
50  {
51  m_key.Assign(key, length);
52  Restart();
53  }
54 
55  void Restart()
56  {
57  m_hash.Restart();
58  m_keyed = false;
59  }
60 
61  void Update(const byte *input, size_t length)
62  {
63  if (!m_keyed)
64  KeyHash();
65  m_hash.Update(input, length);
66  }
67 
68  void TruncatedFinal(byte *digest, size_t digestSize)
69  {
70  if (!m_keyed)
71  KeyHash();
72  m_hash.TruncatedFinal(digest, digestSize);
73  m_keyed = false;
74  }
75 
76  unsigned int DigestSize() const
77  {return m_hash.DigestSize();}
78  unsigned int BlockSize() const
79  {return m_hash.BlockSize();}
80  unsigned int OptimalBlockSize() const
81  {return m_hash.OptimalBlockSize();}
82  unsigned int OptimalDataAlignment() const
83  {return m_hash.OptimalDataAlignment();}
84 
85 protected:
86  void KeyHash()
87  {
88  m_hash.Update(m_key, m_key.size());
89  m_keyed = true;
90  }
91 
92  T_Hash m_hash;
93  bool m_keyed;
94  SecByteBlock m_key;
95 };
96 
97 namespace Weak {
98 /// Panama MAC
99 template <class B = LittleEndian>
100 class PanamaMAC : public HermeticHashFunctionMAC<PanamaHash<B> >
101 {
102 public:
103  PanamaMAC() {}
104  PanamaMAC(const byte *key, unsigned int length)
105  {this->SetKey(key, length);}
106 };
107 }
108 
109 //! algorithm info
110 template <class B>
111 struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32>
112 {
113  static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
114 };
115 
116 //! _
117 template <class B>
119  public PanamaCipherInfo<B>,
120  protected Panama<B>
121 {
122 protected:
123  void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
124  void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
125  bool CipherIsRandomAccess() const {return false;}
126  void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
127 #if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X64
128  unsigned int GetAlignment() const;
129 #endif
130 
132 };
133 
134 //! <a href="http://www.cryptolounge.org/wiki/PANAMA">Panama Stream Cipher</a>
135 template <class B = LittleEndian>
137 {
139  typedef Encryption Decryption;
140 };
141 
142 NAMESPACE_END
143 
144 #endif
to be inherited by keyed algorithms with fixed key length
Definition: seckey.h:66
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
set or reset the key of this object
void Update(const byte *input, size_t length)
process more input
Definition: panama.h:61
unsigned int OptimalDataAlignment() const
returns how input should be aligned for optimal performance
Definition: panama.h:82
unsigned int BlockSize() const
block size of underlying compression function, or 0 if not block based
Definition: panama.h:78
void TruncatedFinal(byte *digest, size_t digestSize)
truncated version of Final()
Definition: panama.h:68
void Assign(const T *t, size_type len)
set contents and size
Definition: secblock.h:310
Panama Stream Cipher
Definition: panama.h:136
Panama MAC.
Definition: panama.h:100
MAC construction using a hermetic hash function.
Definition: panama.h:46
unsigned int DigestSize() const
size of the hash/digest/MAC returned by Final()
Definition: panama.h:76
unsigned int OptimalBlockSize() const
input to Update() should have length a multiple of this for optimal speed
Definition: panama.h:80
Each class derived from this one defines two types, Encryption and Decryption, both of which implemen...
Definition: seckey.h:201
base class, do not use directly
Definition: panama.h:11
algorithm info
Definition: panama.h:111
void Restart()
discard the current state, and restart with a new message
Definition: panama.h:55
Panama Hash
Definition: panama.h:27
Definition: panama.h:24
interface for retrieving values given their names
Definition: cryptlib.h:224