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