Crypto++  5.6.3
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 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 #if CRYPTOPP_BOOL_X32
14 # define CRYPTOPP_DISABLE_PANAMA_ASM
15 #endif
16 
17 NAMESPACE_BEGIN(CryptoPP)
18 
19 /// base class, do not use directly
20 template <class B>
21 class CRYPTOPP_NO_VTABLE Panama
22 {
23 public:
24  void Reset();
25  void Iterate(size_t count, const word32 *p=NULL, byte *output=NULL, const byte *input=NULL, KeystreamOperation operation=WRITE_KEYSTREAM);
26 
27 protected:
28  typedef word32 Stage[8];
29  CRYPTOPP_CONSTANT(STAGES = 32)
30 
32 };
33 
34 namespace Weak {
35 /// <a href="http://www.weidai.com/scan-mirror/md.html#Panama">Panama Hash</a>
36 template <class B = LittleEndian>
37 class PanamaHash : protected Panama<B>, public AlgorithmImpl<IteratedHash<word32, NativeByteOrder, 32>, PanamaHash<B> >
38 {
39 public:
40  CRYPTOPP_CONSTANT(DIGESTSIZE = 32)
42  unsigned int DigestSize() const {return DIGESTSIZE;}
43  void TruncatedFinal(byte *hash, size_t size);
44  static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
45 
46 protected:
47  void Init() {Panama<B>::Reset();}
48  void HashEndianCorrectedBlock(const word32 *data) {this->Iterate(1, data);} // push
49  size_t HashMultipleBlocks(const word32 *input, size_t length);
50  word32* StateBuf() {return NULL;}
51 };
52 }
53 
54 //! MAC construction using a hermetic hash function
55 template <class T_Hash, class T_Info = T_Hash>
56 class HermeticHashFunctionMAC : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<MessageAuthenticationCode, VariableKeyLength<32, 0, INT_MAX> > >, T_Info>
57 {
58 public:
59  void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
60  {
61  CRYPTOPP_UNUSED(params);
62 
63  m_key.Assign(key, length);
64  Restart();
65  }
66 
67  void Restart()
68  {
69  m_hash.Restart();
70  m_keyed = false;
71  }
72 
73  void Update(const byte *input, size_t length)
74  {
75  if (!m_keyed)
76  KeyHash();
77  m_hash.Update(input, length);
78  }
79 
80  void TruncatedFinal(byte *digest, size_t digestSize)
81  {
82  if (!m_keyed)
83  KeyHash();
84  m_hash.TruncatedFinal(digest, digestSize);
85  m_keyed = false;
86  }
87 
88  unsigned int DigestSize() const
89  {return m_hash.DigestSize();}
90  unsigned int BlockSize() const
91  {return m_hash.BlockSize();}
92  unsigned int OptimalBlockSize() const
93  {return m_hash.OptimalBlockSize();}
94  unsigned int OptimalDataAlignment() const
95  {return m_hash.OptimalDataAlignment();}
96 
97 protected:
98  void KeyHash()
99  {
100  m_hash.Update(m_key, m_key.size());
101  m_keyed = true;
102  }
103 
104  T_Hash m_hash;
105  bool m_keyed;
106  SecByteBlock m_key;
107 };
108 
109 namespace Weak {
110 /// Panama MAC
111 template <class B = LittleEndian>
112 class PanamaMAC : public HermeticHashFunctionMAC<PanamaHash<B> >
113 {
114 public:
115  PanamaMAC() {}
116  PanamaMAC(const byte *key, unsigned int length)
117  {this->SetKey(key, length);}
118 };
119 }
120 
121 //! algorithm info
122 template <class B>
123 struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32>
124 {
125  static const char * StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
126 };
127 
128 //! _
129 template <class B>
131  public PanamaCipherInfo<B>,
132  protected Panama<B>
133 {
134 protected:
135  void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
136  void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
137  bool CipherIsRandomAccess() const {return false;}
138  void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
139  unsigned int GetAlignment() const;
140 
142 };
143 
144 //! <a href="http://www.cryptolounge.org/wiki/PANAMA">Panama Stream Cipher</a>
145 template <class B = LittleEndian>
147 {
149  typedef Encryption Decryption;
150 };
151 
152 NAMESPACE_END
153 
154 #endif
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:113
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: panama.h:42
SecByteBlock is a SecBlock<byte> typedef.
Definition: secblock.h:719
Classes and functions for secure memory allocations.
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition: panama.cpp:431
Panama Stream Cipher
Definition: panama.h:146
Panama MAC.
Definition: panama.h:112
MAC construction using a hermetic hash function.
Definition: panama.h:56
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher...
Definition: seckey.h:401
base class, do not use directly
Definition: panama.h:21
algorithm info
Definition: panama.h:123
Crypto++ library namespace.
Panama Hash
Definition: panama.h:37
Namespace containing weak and wounded algorithms.
Definition: arc4.cpp:14
Interface for retrieving values given their names.
Definition: cryptlib.h:261
Base class for identifying alogorithm.
Definition: simple.h:38