HmacContext.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  * HmacContext.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include <cstring>
14 #include "Blob.h"
15 #include "ByteArray.h"
16 
17 namespace Crypto
18 {
24 template <class HashContext> class HmacContext
25 {
26 public:
27  using Engine = typename HashContext::Engine;
28  using Hash = typename HashContext::Hash;
29  static constexpr size_t blocksize = Engine::blocksize;
30 
36  HmacContext() = default;
37 
42  {
43  init(key);
44  }
45 
51  {
52  ByteArray<blocksize> inputPad{};
53  if(key.size() <= blocksize) {
54  memcpy(inputPad.data(), key.data(), key.size());
55  } else {
56  ctx.reset();
57  ctx.update(key);
58  auto hash = ctx.getHash();
59  memcpy(inputPad.data(), hash.data(), hash.size());
60  }
61 
62  outputPad = inputPad;
63 
64  for(auto& c : inputPad) {
65  c ^= 0x36;
66  }
67  for(auto& c : outputPad) {
68  c ^= 0x5c;
69  }
70 
71  ctx.reset();
72  ctx.update(inputPad);
73 
74  return *this;
75  }
76 
82  template <typename... Ts> HmacContext& update(Ts&&... args)
83  {
84  ctx.update(std::forward<Ts>(args)...);
85  return *this;
86  }
87 
89  {
90  auto tmp = ctx.getHash();
91 
92  ctx.reset();
93  ctx.update(outputPad);
94  ctx.update(tmp);
95  return ctx.getHash();
96  }
97 
108  template <typename... Ts> Hash calculate(Ts&&... args)
109  {
110  ctx.update(std::forward<Ts>(args)...);
111  return getHash();
112  }
113 
114 private:
115  ByteArray<blocksize> outputPad;
116  HashContext ctx;
117 };
118 
119 } // namespace Crypto
typename HashContext::Hash Hash
Definition: HmacContext.h:28
HmacContext(const Secret &key)
Initialise HMAC context with key.
Definition: HmacContext.h:41
HashContext & update(const Blob &blob)
Definition: HashContext.h:62
ByteArray< Engine::hashsize > Hash
Definition: HashContext.h:26
static constexpr size_t blocksize
Definition: HmacContext.h:29
typename HashContext::Engine Engine
Definition: HmacContext.h:27
Hash getHash()
Definition: HmacContext.h:88
HmacContext()=default
Default HMAC constructor.
std::array< uint8_t, size_ > ByteArray
Class template for fixed byte array.
Definition: ByteArray.h:24
HmacContext & init(const Secret &key)
Initialise HMAC with key.
Definition: HmacContext.h:50
HashContext & reset(EngineArgs &&... engineArgs)
Reset the context for a new calculation.
Definition: HashContext.h:41
HmacContext & update(Ts &&... args)
Update HMAC with some message content.
Definition: HmacContext.h:82
size_t size() const
Definition: Blob.h:36
Class template for a Hash implementation &#39;Context&#39;.
Definition: HashContext.h:22
void size_t const void * key
Definition: blake2s.h:33
Hash calculate(Ts &&... args)
Calculate hash for some data.
Definition: HmacContext.h:108
Engine_ Engine
Definition: HashContext.h:25
Wraps a pointer to some data with size.
Definition: Blob.h:20
Definition: Blake2s.h:18
const uint8_t * data() const
Definition: Blob.h:31
Hash getHash()
Finalise and return the final hash value.
Definition: HashContext.h:95
HMAC class template.
Definition: HmacContext.h:24