Fabcoin Core  0.16.2
P2P Digital Currency
blake2.h
Go to the documentation of this file.
1 // blake2.h - written and placed in the public domain by Jeffrey Walton and Zooko
2 // Wilcox-O'Hearn. Copyright assigned to the Crypto++ project.
3 // Based on Aumasson, Neves, Wilcox-O'Hearn and Winnerlein's reference BLAKE2
4 // implementation at http://github.com/BLAKE2/BLAKE2.
5 
17 
18 #ifndef CRYPTOPP_BLAKE2_H
19 #define CRYPTOPP_BLAKE2_H
20 
21 #include "cryptlib.h"
22 #include "secblock.h"
23 #include "seckey.h"
24 
26 
27 template <bool T_64bit>
32 struct BLAKE2_Info : public VariableKeyLength<(T_64bit ? 64 : 32),0,(T_64bit ? 64 : 32),1,SimpleKeyingInterface::NOT_RESYNCHRONIZABLE>
33 {
35  CRYPTOPP_CONSTANT(MIN_KEYLENGTH = KeyBase::MIN_KEYLENGTH)
36  CRYPTOPP_CONSTANT(MAX_KEYLENGTH = KeyBase::MAX_KEYLENGTH)
37  CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH = KeyBase::DEFAULT_KEYLENGTH)
38 
39  CRYPTOPP_CONSTANT(BLOCKSIZE = (T_64bit ? 128 : 64))
40  CRYPTOPP_CONSTANT(DIGESTSIZE = (T_64bit ? 64 : 32))
41  CRYPTOPP_CONSTANT(SALTSIZE = (T_64bit ? 16 : 8))
42  CRYPTOPP_CONSTANT(PERSONALIZATIONSIZE = (T_64bit ? 16 : 8))
43 
44  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return (T_64bit ? "BLAKE2b" : "BLAKE2s");}
45 };
46 
53 template <bool T_64bit>
55 {
56 };
57 
59 template<>
60 struct CRYPTOPP_NO_VTABLE BLAKE2_ParameterBlock<true>
61 {
65 
66  BLAKE2_ParameterBlock()
67  {
68  memset(this, 0x00, sizeof(*this));
69  digestLength = DIGESTSIZE;
70  fanout = depth = 1;
71  }
72 
73  BLAKE2_ParameterBlock(size_t digestSize)
74  {
75  CRYPTOPP_ASSERT(digestSize <= DIGESTSIZE);
76  memset(this, 0x00, sizeof(*this));
77  digestLength = (byte)digestSize;
78  fanout = depth = 1;
79  }
80 
81  BLAKE2_ParameterBlock(size_t digestSize, size_t keyLength, const byte* salt, size_t saltLength,
82  const byte* personalization, size_t personalizationLength);
83 
85  byte keyLength, fanout, depth;
86  byte leafLength[4];
87  byte nodeOffset[8];
88  byte nodeDepth, innerLength, rfu[14];
89  byte salt[SALTSIZE];
90  byte personalization[PERSONALIZATIONSIZE];
91 };
92 
94 template<>
95 struct CRYPTOPP_NO_VTABLE BLAKE2_ParameterBlock<false>
96 {
100 
101  BLAKE2_ParameterBlock()
102  {
103  memset(this, 0x00, sizeof(*this));
104  digestLength = DIGESTSIZE;
105  fanout = depth = 1;
106  }
107 
108  BLAKE2_ParameterBlock(size_t digestSize)
109  {
110  CRYPTOPP_ASSERT(digestSize <= DIGESTSIZE);
111  memset(this, 0x00, sizeof(*this));
112  digestLength = (byte)digestSize;
113  fanout = depth = 1;
114  }
115 
116  BLAKE2_ParameterBlock(size_t digestSize, size_t keyLength, const byte* salt, size_t saltLength,
117  const byte* personalization, size_t personalizationLength);
118 
120  byte keyLength, fanout, depth;
121  byte leafLength[4];
122  byte nodeOffset[6];
123  byte nodeDepth, innerLength;
124  byte salt[SALTSIZE];
125  byte personalization[PERSONALIZATIONSIZE];
126 };
127 
135 template <class W, bool T_64bit>
137 {
139 
141  {
142  // Set all members except scratch buffer[]
143  h[0]=h[1]=h[2]=h[3]=h[4]=h[5]=h[6]=h[7] = 0;
144  t[0]=t[1]=f[0]=f[1] = 0;
145  length = 0;
146  }
147 
148  // SSE2, SSE4 and NEON depend upon t[] and f[] being side-by-side
149  W h[8], t[2], f[2];
150  byte buffer[BLOCKSIZE];
151  size_t length;
152 };
153 
161 template <class W, bool T_64bit>
162 class BLAKE2_Base : public SimpleKeyingInterfaceImpl<MessageAuthenticationCode, BLAKE2_Info<T_64bit> >
163 {
164 public:
168 
173 
174  typedef BLAKE2_State<W, T_64bit> State;
175  typedef BLAKE2_ParameterBlock<T_64bit> ParameterBlock;
176  typedef SecBlock<State, AllocatorWithCleanup<State, true> > AlignedState;
177  typedef SecBlock<ParameterBlock, AllocatorWithCleanup<ParameterBlock, true> > AlignedParameterBlock;
178 
179  virtual ~BLAKE2_Base() {}
180 
184 
190  std::string AlgorithmName() const {return std::string(StaticAlgorithmName()) + "-" + IntToString(this->DigestSize()*8);}
191 
192  unsigned int DigestSize() const {return m_digestSize;}
193  unsigned int OptimalDataAlignment() const {return (CRYPTOPP_BOOL_ALIGN16 ? 16 : GetAlignmentOf<W>());}
194 
195  void Update(const byte *input, size_t length);
196  void Restart();
197 
202  void Restart(const BLAKE2_ParameterBlock<T_64bit>& block, const W counter[2]);
203 
210  void SetTreeMode(bool mode) {m_treeMode=mode;}
211 
215  bool GetTreeMode() const {return m_treeMode;}
216 
217  void TruncatedFinal(byte *hash, size_t size);
218 
219 protected:
220  BLAKE2_Base();
221  BLAKE2_Base(bool treeMode, unsigned int digestSize);
222  BLAKE2_Base(const byte *key, size_t keyLength, const byte* salt, size_t saltLength,
223  const byte* personalization, size_t personalizationLength,
224  bool treeMode, unsigned int digestSize);
225 
226  // Operates on state buffer and/or input. Must be BLOCKSIZE, final block will pad with 0's.
227  void Compress(const byte *input);
228  inline void IncrementCounter(size_t count=BLOCKSIZE);
229 
230  void UncheckedSetKey(const byte* key, unsigned int length, const CryptoPP::NameValuePairs& params);
231 
232 private:
238 };
239 
249 class BLAKE2b : public BLAKE2_Base<word64, true>
250 {
251 public:
252  typedef BLAKE2_Base<word64, true> ThisBase; // Early Visual Studio workaround
254  CRYPTOPP_COMPILE_ASSERT(sizeof(ParameterBlock) == 64);
255 
259  BLAKE2b(bool treeMode=false, unsigned int digestSize = DIGESTSIZE) : ThisBase(treeMode, digestSize) {}
260 
270  BLAKE2b(const byte *key, size_t keyLength, const byte* salt = NULL, size_t saltLength = 0,
271  const byte* personalization = NULL, size_t personalizationLength = 0,
272  bool treeMode=false, unsigned int digestSize = DIGESTSIZE)
273  : ThisBase(key, keyLength, salt, saltLength, personalization, personalizationLength, treeMode, digestSize) {}
274 };
275 
285 class BLAKE2s : public BLAKE2_Base<word32, false>
286 {
287 public:
288  typedef BLAKE2_Base<word32, false> ThisBase; // Early Visual Studio workaround
290  CRYPTOPP_COMPILE_ASSERT(sizeof(ParameterBlock) == 32);
291 
295  BLAKE2s(bool treeMode=false, unsigned int digestSize = DIGESTSIZE) : ThisBase(treeMode, digestSize) {}
296 
306  BLAKE2s(const byte *key, size_t keyLength, const byte* salt = NULL, size_t saltLength = 0,
307  const byte* personalization = NULL, size_t personalizationLength = 0,
308  bool treeMode=false, unsigned int digestSize = DIGESTSIZE)
309  : ThisBase(key, keyLength, salt, saltLength, personalization, personalizationLength, treeMode, digestSize) {}
310 };
311 
313 
314 #endif
BLAKE2_ParameterBlock< false > ParameterBlock
Definition: blake2.h:289
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: blake2.h:193
uint8_t byte
Definition: Common.h:57
#define CRYPTOPP_STATIC_CONSTEXPR
Definition: config.h:892
BLAKE2_ParameterBlock< true > ParameterBlock
Definition: blake2.h:253
BLAKE2b(const byte *key, size_t keyLength, const byte *salt=NULL, size_t saltLength=0, const byte *personalization=NULL, size_t personalizationLength=0, bool treeMode=false, unsigned int digestSize=DIGESTSIZE)
Construct a BLAKE2b hash.
Definition: blake2.h:270
BLAKE2 hash implementation.
Definition: blake2.h:162
Provides a base implementation of SimpleKeyingInterface.
Definition: seckey.h:263
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define h(i)
Definition: sha.cpp:736
size_t count
Definition: ExecStats.cpp:37
Secure memory block with allocator and cleanup.
Definition: secblock.h:437
Abstract base classes that provide a uniform interface to this library.
VariableKeyLength<(T_64bit?64:32), 0,(T_64bit?64:32), 1, SimpleKeyingInterface::NOT_RESYNCHRONIZABLE > KeyBase
Definition: blake2.h:34
evm_mode mode
Definition: SmartVM.cpp:47
AlignedParameterBlock m_block
Definition: blake2.h:234
BLAKE2 hash information.
Definition: blake2.h:32
AlignedSecByteBlock m_key
Definition: blake2.h:235
BLAKE2s(bool treeMode=false, unsigned int digestSize=DIGESTSIZE)
Construct a BLAKE2s hash.
Definition: blake2.h:295
The BLAKE2s cryptographic hash function.
Definition: blake2.h:285
size_t length
Definition: blake2.h:151
Allocates a block of memory with cleanup.
Definition: secblock.h:151
Classes and functions for secure memory allocations.
BLAKE2_Base< word64, true > ThisBase
Definition: blake2.h:252
#define CRYPTOPP_COMPILE_ASSERT(assertion)
Definition: misc.h:139
AlignedState m_state
Definition: blake2.h:233
Classes and functions for implementing secret key algorithms.
BLAKE2_Base< word32, false > ThisBase
Definition: blake2.h:288
Interface for algorithms that take byte strings as keys.
Definition: cryptlib.h:524
#define CRYPTOPP_BOOL_ALIGN16
Definition: config.h:505
BLAKE2_ParameterBlock(size_t digestSize)
Definition: blake2.h:73
#define CRYPTOPP_CONSTANT(x)
Definition: config.h:540
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
CRYPTOPP_STATIC_CONSTEXPR const char * StaticAlgorithmName()
Definition: blake2.h:44
word32 m_digestSize
Definition: blake2.h:236
BLAKE2s parameter block specialization.
Definition: blake2.h:95
CRYPTOPP_STATIC_CONSTEXPR const char * StaticAlgorithmName()
Retrieve the static algorithm name.
Definition: blake2.h:183
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:169
#define CRYPTOPP_NO_VTABLE
Definition: config.h:369
BLAKE2 state information.
Definition: blake2.h:136
#define f(x)
Definition: gost.cpp:57
std::string AlgorithmName() const
Retrieve the object&#39;s name.
Definition: blake2.h:190
BLAKE2 parameter block.
Definition: blake2.h:54
bool GetTreeMode() const
Get tree mode.
Definition: blake2.h:215
void SetTreeMode(bool mode)
Set tree mode.
Definition: blake2.h:210
uint8_t const size_t const size
Definition: sha3.h:20
uint8_t byte
Definition: Common.h:10
bool m_treeMode
Definition: blake2.h:237
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:539
#define NAMESPACE_END
Definition: config.h:201
BLAKE2b(bool treeMode=false, unsigned int digestSize=DIGESTSIZE)
Construct a BLAKE2b hash.
Definition: blake2.h:259
BLAKE2_ParameterBlock(size_t digestSize)
Definition: blake2.h:108
unsigned int word32
Definition: config.h:231
BLAKE2s(const byte *key, size_t keyLength, const byte *salt=NULL, size_t saltLength=0, const byte *personalization=NULL, size_t personalizationLength=0, bool treeMode=false, unsigned int digestSize=DIGESTSIZE)
Construct a BLAKE2s hash.
Definition: blake2.h:306
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: blake2.h:192
The BLAKE2b cryptographic hash function.
Definition: blake2.h:249
BLAKE2b parameter block specialization.
Definition: blake2.h:60