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 
41  HmacContext(const Secret& key)
42  {
44  }
45 
50  HmacContext& init(const Secret& key)
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 
88  Hash getHash()
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
Wraps a pointer to some data with size.
Definition: Blob.h:28
std::array< uint8_t, size_ > ByteArray
Class template for fixed byte array.
Definition: ByteArray.h:32
Hash getHash()
Definition: HmacContext.h:104
static constexpr size_t blocksize
Definition: HmacContext.h:45
HmacContext & update(Ts &&... args)
Update HMAC with some message content.
Definition: HmacContext.h:98
Hash getHash()
Finalise and return the final hash value.
Definition: HashContext.h:120
void size_t const void * key
Definition: blake2s.h:33
ByteArray< Engine::hashsize > Hash
Definition: HashContext.h:42
Hash calculate(Ts &&... args)
Calculate hash for some data.
Definition: HmacContext.h:124
HmacContext & init(const Secret &key)
Initialise HMAC with key.
Definition: HmacContext.h:66
Definition: Blake2s.h:18
Engine_ Engine
Definition: HashContext.h:41
HmacContext()=default
Default HMAC constructor.
HashContext & update(const Blob &blob)
Data from Blob.
Definition: HashContext.h:80
HashContext & reset(EngineArgs &&... engineArgs)
Reset the context for a new calculation.
Definition: HashContext.h:57
typename HashContext::Engine Engine
Definition: HmacContext.h:43
HMAC class template.
Definition: HmacContext.h:32
Blob Secret
Identifies data which should be treated with care.
Definition: Blob.h:57
typename HashContext::Hash Hash
Definition: HmacContext.h:44