Arduino Cryptography Library
HKDF.h
1 /*
2  * Copyright (C) 2022 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef CRYPTO_HKDF_h
24 #define CRYPTO_HKDF_h
25 
26 #include "Hash.h"
27 #include "Crypto.h"
28 
30 {
31 public:
32  virtual ~HKDFCommon();
33 
34  void setKey(const void *key, size_t keyLen, const void *salt = 0, size_t saltLen = 0);
35 
36  void extract(void *out, size_t outLen, const void *info = 0, size_t infoLen = 0);
37 
38  void clear();
39 
40 protected:
41  HKDFCommon();
42  void setHashAlgorithm(Hash *hashAlg, uint8_t *buffer)
43  {
44  hash = hashAlg;
45  buf = buffer;
46  }
47 
48 private:
49  Hash *hash;
50  uint8_t *buf;
51  uint8_t counter;
52  uint8_t posn;
53 };
54 
55 template <typename T>
56 class HKDF : public HKDFCommon
57 {
58 public:
59  HKDF() { setHashAlgorithm(&hashAlg, buffer); }
60  ~HKDF() { ::clean(buffer, sizeof(buffer)); }
61 
62 private:
63  T hashAlg;
64  uint8_t buffer[T::HASH_SIZE * 2];
65 };
66 
67 template <typename T> void hkdf
68  (void *out, size_t outLen, const void *key, size_t keyLen,
69  const void *salt, size_t saltLen, const void *info, size_t infoLen)
70 {
71  HKDF<T> context;
72  context.setKey(key, keyLen, salt, saltLen);
73  context.extract(out, outLen, info, infoLen);
74 }
75 
76 #endif
Concrete base class to assist with implementing HKDF mode for hash algorithms.
Definition: HKDF.h:30
void setKey(const void *key, size_t keyLen, const void *salt=0, size_t saltLen=0)
Sets the key and salt for a HKDF session.
Definition: HKDF.cpp:64
void clear()
Clears sensitive information from this HKDF instance.
Definition: HKDF.cpp:128
HKDFCommon()
Constructs a new HKDF instance.
Definition: HKDF.cpp:41
void setHashAlgorithm(Hash *hashAlg, uint8_t *buffer)
Sets the hash algorithm to use for HKDF operations.
Definition: HKDF.h:42
void extract(void *out, size_t outLen, const void *info=0, size_t infoLen=0)
Extracts data from a HKDF session.
Definition: HKDF.cpp:96
virtual ~HKDFCommon()
Destroys this HKDF instance.
Definition: HKDF.cpp:52
Implementation of the HKDF mode for hash algorithms.
Definition: HKDF.h:57
~HKDF()
Destroys a HKDF instance and all sensitive data within it.
Definition: HKDF.h:60
HKDF()
Constructs a new HKDF object for the hash algorithm T.
Definition: HKDF.h:59
Abstract base class for cryptographic hash algorithms.
Definition: Hash.h:30