Arduino Cryptography Library
HKDF.cpp
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 #include "HKDF.h"
24 #include <string.h>
25 
42  : hash(0)
43  , buf(0)
44  , counter(1)
45  , posn(255)
46 {
47 }
48 
53 {
54 }
55 
64 void HKDFCommon::setKey(const void *key, size_t keyLen, const void *salt, size_t saltLen)
65 {
66  // Initialise the HKDF context with the key and salt to generate the PRK.
67  size_t hashSize = hash->hashSize();
68  if (salt && saltLen) {
69  hash->resetHMAC(salt, saltLen);
70  hash->update(key, keyLen);
71  hash->finalizeHMAC(salt, saltLen, buf + hashSize, hashSize);
72  } else {
73  // If no salt is provided, RFC 5869 says that a string of
74  // hashSize zeroes should be used instead.
75  memset(buf, 0, hashSize);
76  hash->resetHMAC(buf, hashSize);
77  hash->update(key, keyLen);
78  hash->finalizeHMAC(buf, hashSize, buf + hashSize, hashSize);
79  }
80  counter = 1;
81  posn = hashSize;
82 }
83 
96 void HKDFCommon::extract(void *out, size_t outLen, const void *info, size_t infoLen)
97 {
98  size_t hashSize = hash->hashSize();
99  uint8_t *outPtr = (uint8_t *)out;
100  while (outLen > 0) {
101  // Generate a new output block if necessary.
102  if (posn >= hashSize) {
103  hash->resetHMAC(buf + hashSize, hashSize);
104  if (counter != 1)
105  hash->update(buf, hashSize);
106  if (info && infoLen)
107  hash->update(info, infoLen);
108  hash->update(&counter, 1);
109  hash->finalizeHMAC(buf + hashSize, hashSize, buf, hashSize);
110  ++counter;
111  posn = 0;
112  }
113 
114  // Copy as much output data as we can for this block.
115  size_t len = hashSize - posn;
116  if (len > outLen)
117  len = outLen;
118  memcpy(outPtr, buf + posn, len);
119  posn += len;
120  outPtr += len;
121  outLen -= len;
122  }
123 }
124 
129 {
130  size_t hashSize = hash->hashSize();
131  hash->clear();
132  clean(buf, hashSize * 2);
133  counter = 1;
134  posn = hashSize;
135 }
136 
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 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
virtual void clear()=0
Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing ...
virtual void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)=0
Finalizes the HMAC hashing process and returns the hash.
virtual size_t hashSize() const =0
Size of the hash result from finalize().
virtual void resetHMAC(const void *key, size_t keyLen)=0
Resets the hash ready for a new HMAC hashing process.
virtual void update(const void *data, size_t len)=0
Updates the hash with more data.