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 "HashContext.h"
14 #include <cstring>
15 #include <cassert>
16 
17 namespace Crypto
18 {
19 template <class HashContext> class HmacContext
20 {
21 public:
22  using Engine = typename HashContext::Engine;
23  using Hash = typename HashContext::Hash;
24  static constexpr size_t blocksize = Engine::blocksize;
25 
31  HmacContext() = default;
32 
36  HmacContext(const Secret& key)
37  {
38  init(key);
39  }
40 
45  HmacContext& init(const Secret& key)
46  {
47  ByteArray<blocksize> inputPad{};
48  if(key.size() <= blocksize) {
49  memcpy(inputPad.data(), key.data(), key.size());
50  } else {
51  ctx.reset();
52  ctx.update(key);
53  auto hash = ctx.getHash();
54  memcpy(inputPad.data(), hash.data(), hash.size());
55  }
56 
57  outputPad = inputPad;
58 
59  for(auto& c : inputPad) {
60  c ^= 0x36;
61  }
62  for(auto& c : outputPad) {
63  c ^= 0x5c;
64  }
65 
66  ctx.reset();
67  ctx.update(inputPad);
68 
69  return *this;
70  }
71 
77  template <typename... Ts> HmacContext& update(Ts&&... args)
78  {
79  ctx.update(std::forward<Ts>(args)...);
80  return *this;
81  }
82 
84  {
85  auto tmp = ctx.getHash();
86 
87  ctx.reset();
88  ctx.update(outputPad);
89  ctx.update(tmp);
90  return ctx.getHash();
91  }
92 
103  template <typename... Ts> Hash calculate(Ts&&... args)
104  {
105  ctx.update(std::forward<Ts>(args)...);
106  return getHash();
107  }
108 
109 private:
110  ByteArray<blocksize> outputPad;
111  HashContext ctx;
112 };
113 
114 /*
115  * HMAC definitions
116  */
117 
124 
125 } // namespace Crypto
typename HashContext::Hash Hash
Definition: HmacContext.h:23
HmacContext(const Secret &key)
Initialise HMAC context with key.
Definition: HmacContext.h:36
HashContext & update(const Blob &blob)
Definition: HashContext.h:63
ByteArray< Engine::hashsize > Hash
Definition: HashContext.h:27
static constexpr size_t blocksize
Definition: HmacContext.h:24
typename HashContext::Engine Engine
Definition: HmacContext.h:22
Hash getHash()
Definition: HmacContext.h:83
HashContext & reset()
Reset the context for a new calculation.
Definition: HashContext.h:42
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:45
HmacContext & update(Ts &&... args)
Update HMAC with some message content.
Definition: HmacContext.h:77
size_t size() const
Definition: Blob.h:36
Class template for a Hash implementation &#39;Context&#39;.
Definition: HashContext.h:23
Hash calculate(Ts &&... args)
Calculate hash for some data.
Definition: HmacContext.h:103
Engine_ Engine
Definition: HashContext.h:26
Wraps a pointer to some data with size.
Definition: Blob.h:20
Definition: Blob.h:15
const uint8_t * data() const
Definition: Blob.h:31
Hash getHash()
Finalise and return the final hash value.
Definition: HashContext.h:96
Definition: HmacContext.h:19