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