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