HashEngine.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * HashEngine.h - Hash engine template code
8  *
9  ****/
10 
11 #pragma once
12 
25 #define CRYPTO_HASH_ENGINE(class_, name_, hashsize_, statesize_, blocksize_, INIT) \
26  class class_##Engine \
27  { \
28  public: \
29  static constexpr const char* name = #name_; \
30  static constexpr size_t hashsize = hashsize_; \
31  static constexpr size_t statesize = statesize_; \
32  static constexpr size_t blocksize = blocksize_; \
33  \
34  INIT(name_) \
35  \
36  void update(const void* data, size_t size) \
37  { \
38  CRYPTO_NAME(name_, update)(&ctx, static_cast<const uint8_t*>(data), size); \
39  } \
40  \
41  void final(uint8_t* hash) \
42  { \
43  CRYPTO_NAME(name_, final)(hash, &ctx); \
44  } \
45  \
46  private: \
47  CRYPTO_CTX(name_) ctx; \
48  };
49 
53 #define CRYPTO_HASH_ENGINE_STD(class_, name_, hashsize_, statesize_, blocksize_) \
54  CRYPTO_HASH_ENGINE(class_, name_, hashsize_, statesize_, blocksize_, CRYPTO_HASH_ENGINE_STD_INIT)
55 
59 #define CRYPTO_HASH_ENGINE_STD_INIT(name_) \
60  void init() \
61  { \
62  CRYPTO_NAME(name_, init)(&ctx); \
63  } \
64  \
65  uint64_t get_state(void* state) \
66  { \
67  return CRYPTO_NAME(name_, get_state)(&ctx, state); \
68  } \
69  \
70  void set_state(const void* state, uint64_t count) \
71  { \
72  CRYPTO_NAME(name_, set_state)(&ctx, state, count); \
73  }