Crypto++  8.6
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  std::string AlgorithmProvider() const {return Panama<B>::AlgorithmProvider();} // Fix https://github.com/weidai11/cryptopp/issues/801
54 
55 protected:
56  void Init() {Panama<B>::Reset();}
57  void HashEndianCorrectedBlock(const word32 *data) {this->Iterate(1, data);} // push
58  size_t HashMultipleBlocks(const word32 *input, size_t length);
59  word32* StateBuf() {return NULLPTR;}
60 
62 };
63 }
64 
65 /// \brief MAC construction using a hermetic hash function
66 template <class T_Hash, class T_Info = T_Hash>
67 class HermeticHashFunctionMAC : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<MessageAuthenticationCode, VariableKeyLength<32, 0, INT_MAX> > >, T_Info>
68 {
69 public:
70  void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
71  {
72  CRYPTOPP_UNUSED(params);
73 
74  m_key.Assign(key, length);
75  Restart();
76  }
77 
78  void Restart()
79  {
80  m_hash.Restart();
81  m_keyed = false;
82  }
83 
84  void Update(const byte *input, size_t length)
85  {
86  if (!m_keyed)
87  KeyHash();
88  m_hash.Update(input, length);
89  }
90 
91  void TruncatedFinal(byte *digest, size_t digestSize)
92  {
93  if (!m_keyed)
94  KeyHash();
95  m_hash.TruncatedFinal(digest, digestSize);
96  m_keyed = false;
97  }
98 
99  unsigned int DigestSize() const
100  {return m_hash.DigestSize();}
101  unsigned int BlockSize() const
102  {return m_hash.BlockSize();}
103  unsigned int OptimalBlockSize() const
104  {return m_hash.OptimalBlockSize();}
105  unsigned int OptimalDataAlignment() const
106  {return m_hash.OptimalDataAlignment();}
107 
108 protected:
109  void KeyHash()
110  {
111  m_hash.Update(m_key, m_key.size());
112  m_keyed = true;
113  }
114 
115  T_Hash m_hash;
116  bool m_keyed;
117  SecByteBlock m_key;
118 };
119 
120 namespace Weak {
121 /// \brief Panama message authentication code
122 template <class B = LittleEndian>
123 class PanamaMAC : public HermeticHashFunctionMAC<PanamaHash<B> >
124 {
125 public:
126  PanamaMAC() {}
127  PanamaMAC(const byte *key, unsigned int length)
128  {this->SetKey(key, length);}
129 };
130 }
131 
132 /// \brief Panama stream cipher information
133 template <class B>
134 struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32>
135 {
136  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
137 };
138 
139 /// \brief Panama stream cipher operation
140 template <class B>
142  public PanamaCipherInfo<B>,
143  protected Panama<B>
144 {
145 protected:
146  virtual ~PanamaCipherPolicy() {}
147  std::string AlgorithmProvider() const;
148  void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
149  void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
150  bool CipherIsRandomAccess() const {return false;}
151  void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
152  unsigned int GetAlignment() const;
153 
156 };
157 
158 /// \brief Panama stream cipher
159 /// \sa <a href="http://www.cryptolounge.org/wiki/PANAMA">Panama Stream Cipher</a>
160 template <class B = LittleEndian>
162 {
164  typedef Encryption Decryption;
165 };
166 
167 NAMESPACE_END
168 
169 #endif
FixedSizeAlignedSecBlock< word32, 20+8 *32 >
Weak::PanamaHash
Panama hash.
Definition: panama.h:44
BIG_ENDIAN_ORDER
@ BIG_ENDIAN_ORDER
byte order is big-endian
Definition: cryptlib.h:147
SymmetricCipherDocumentation
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher.
Definition: seckey.h:413
SymmetricCipherFinal
SymmetricCipher implementation.
Definition: strciphr.h:686
AdditiveCipherConcretePolicy
Base class for additive stream ciphers.
Definition: strciphr.h:201
secblock.h
Classes and functions for secure memory allocations.
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:1226
Weak::PanamaHash::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: panama.h:50
iterhash.h
Base classes for iterated hashes.
Weak::PanamaHash::TruncatedFinal
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition: panama.cpp:440
word32
unsigned int word32
32-bit unsigned datatype
Definition: config_int.h:62
FixedKeyLength
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:124
HermeticHashFunctionMAC
MAC construction using a hermetic hash function.
Definition: panama.h:67
AdditiveCipherTemplate<>
PanamaCipher
Panama stream cipher.
Definition: panama.h:161
WRITE_KEYSTREAM
@ WRITE_KEYSTREAM
Write the keystream to the output buffer, input is NULL.
Definition: strciphr.h:90
KeystreamOperation
KeystreamOperation
Keystream operation flags.
Definition: strciphr.h:88
Weak::PanamaMAC
Panama message authentication code.
Definition: panama.h:123
PanamaCipherPolicy
Panama stream cipher operation.
Definition: panama.h:141
FixedSizeSecBlock< word32, 8 >
Panama
Definition: panama.h:25
SecBlock::size
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:867
SecBlock::Assign
void Assign(const T *ptr, size_type len)
Set contents and size from an array.
Definition: secblock.h:898
Weak::PanamaHash::AlgorithmProvider
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition: panama.h:53
PanamaCipherInfo
Panama stream cipher information.
Definition: panama.h:134
CryptoPP
Crypto++ library namespace.
Weak
Namespace containing weak and wounded algorithms.
Definition: arc4.cpp:14
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:321
AlgorithmImpl
Base class information.
Definition: simple.h:39
strciphr.h
Classes for implementing stream ciphers.