Fabcoin Core  0.16.2
P2P Digital Currency
panama.h
Go to the documentation of this file.
1 // panama.h - written and placed in the public domain by Wei Dai
2 
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 
19 
20 // Base class, do not use directly
21 template <class B>
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 
32  FixedSizeAlignedSecBlock<word32, 20 + 8*32> m_state;
33 };
34 
35 namespace Weak {
39 template <class B = LittleEndian>
40 class PanamaHash : protected Panama<B>, public AlgorithmImpl<IteratedHash<word32, NativeByteOrder, 32>, PanamaHash<B> >
41 {
42 public:
43  CRYPTOPP_CONSTANT(DIGESTSIZE = 32)
45  unsigned int DigestSize() const {return DIGESTSIZE;}
46  void TruncatedFinal(byte *hash, size_t size);
47  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
48 
49 protected:
50  void Init() {Panama<B>::Reset();}
51  void HashEndianCorrectedBlock(const word32 *data) {this->Iterate(1, data);} // push
52  size_t HashMultipleBlocks(const word32 *input, size_t length);
53  word32* StateBuf() {return NULL;}
54 };
55 }
56 
59 template <class T_Hash, class T_Info = T_Hash>
60 class HermeticHashFunctionMAC : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<MessageAuthenticationCode, VariableKeyLength<32, 0, INT_MAX> > >, T_Info>
61 {
62 public:
63  void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
64  {
65  CRYPTOPP_UNUSED(params);
66 
67  m_key.Assign(key, length);
68  Restart();
69  }
70 
71  void Restart()
72  {
73  m_hash.Restart();
74  m_keyed = false;
75  }
76 
77  void Update(const byte *input, size_t length)
78  {
79  if (!m_keyed)
80  KeyHash();
81  m_hash.Update(input, length);
82  }
83 
84  void TruncatedFinal(byte *digest, size_t digestSize)
85  {
86  if (!m_keyed)
87  KeyHash();
88  m_hash.TruncatedFinal(digest, digestSize);
89  m_keyed = false;
90  }
91 
92  unsigned int DigestSize() const
93  {return m_hash.DigestSize();}
94  unsigned int BlockSize() const
95  {return m_hash.BlockSize();}
96  unsigned int OptimalBlockSize() const
97  {return m_hash.OptimalBlockSize();}
98  unsigned int OptimalDataAlignment() const
99  {return m_hash.OptimalDataAlignment();}
100 
101 protected:
102  void KeyHash()
103  {
104  m_hash.Update(m_key, m_key.size());
105  m_keyed = true;
106  }
107 
108  T_Hash m_hash;
109  bool m_keyed;
111 };
112 
113 namespace Weak {
116 template <class B = LittleEndian>
117 class PanamaMAC : public HermeticHashFunctionMAC<PanamaHash<B> >
118 {
119 public:
121  PanamaMAC(const byte *key, unsigned int length)
122  {this->SetKey(key, length);}
123 };
124 }
125 
128 template <class B>
129 struct PanamaCipherInfo : public FixedKeyLength<32, SimpleKeyingInterface::UNIQUE_IV, 32>
130 {
131  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return B::ToEnum() == BIG_ENDIAN_ORDER ? "Panama-BE" : "Panama-LE";}
132 };
133 
136 template <class B>
138  public PanamaCipherInfo<B>,
139  protected Panama<B>
140 {
141 protected:
142  void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
143  void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
144  bool CipherIsRandomAccess() const {return false;}
145  void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
146  unsigned int GetAlignment() const;
147 
149 };
150 
154 template <class B = LittleEndian>
156 {
158  typedef Encryption Decryption;
159 };
160 
162 
163 #endif
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:127
uint8_t byte
Definition: Common.h:57
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: panama.h:45
#define CRYPTOPP_STATIC_CONSTEXPR
Definition: config.h:892
Base class for additive stream ciphers.
Definition: strciphr.h:186
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: panama.h:77
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
Encryption Decryption
Definition: panama.h:158
size_t count
Definition: ExecStats.cpp:37
Wirte the keystream to the output buffer, input is NULL.
Definition: strciphr.h:92
SymmetricCipherFinal< ConcretePolicyHolder< PanamaCipherPolicy< B >, AdditiveCipherTemplate<> >, PanamaCipherInfo< B > > Encryption
Definition: panama.h:157
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: panama.h:98
Classes and functions for secure memory allocations.
void Reset()
Definition: panama.cpp:21
void HashEndianCorrectedBlock(const word32 *data)
Definition: panama.h:51
unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: panama.h:94
void TruncatedFinal(byte *digest, size_t digestSize)
Computes the hash of the current message.
Definition: panama.h:84
CRYPTOPP_STATIC_CONSTEXPR const char * StaticAlgorithmName()
Definition: panama.h:131
Fixed size stack-based SecBlock with 16-byte alignment.
Definition: secblock.h:766
Panama stream cipher.
Definition: panama.h:155
Panama message authentication code.
Definition: panama.h:117
FixedSizeSecBlock< word32, 8 > m_key
Definition: panama.h:148
MAC construction using a hermetic hash function.
Definition: panama.h:60
#define CRYPTOPP_CONSTANT(x)
Definition: config.h:540
byte order is big-endian
Definition: cryptlib.h:128
word32 * StateBuf()
Definition: panama.h:53
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: panama.h:92
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this hash.
Definition: panama.h:96
#define CRYPTOPP_NO_VTABLE
Definition: config.h:369
Classes for implementing stream ciphers.
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher...
Definition: seckey.h:424
Panama stream cipher operation.
Definition: panama.h:137
CRYPTOPP_STATIC_CONSTEXPR const char * StaticAlgorithmName()
Definition: panama.h:47
uint8_t const size_t const size
Definition: sha3.h:20
bool CipherIsRandomAccess() const
Flag indicating random access.
Definition: panama.h:144
PanamaMAC(const byte *key, unsigned int length)
Definition: panama.h:121
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
void Init()
Definition: panama.h:50
Definition: panama.h:22
#define NAMESPACE_END
Definition: config.h:201
Panama stream cipher information.
Definition: panama.h:129
KeystreamOperation
Keystream operation flags.
Definition: strciphr.h:90
void Restart()
Restart the hash.
Definition: panama.h:71
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
Definition: panama.h:63
SymmetricCipher implementation.
Definition: strciphr.h:584
unsigned int word32
Definition: config.h:231
Base class for additive stream ciphers with SymmetricCipher interface.
Definition: strciphr.h:267
SecByteBlock m_key
Definition: panama.h:110
uint8_t const * data
Definition: sha3.h:19
Panama hash.
Definition: panama.h:40
Definition: panama.cpp:423
Interface for retrieving values given their names.
Definition: cryptlib.h:279
Base class for identifying alogorithm.
Definition: simple.h:38