Botan  2.7.0
Crypto and TLS for C++11
hkdf.cpp
Go to the documentation of this file.
1 /*
2 * HKDF
3 * (C) 2013,2015,2017 Jack Lloyd
4 * (C) 2016 RenĂ© Korthaus, Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #include <botan/hkdf.h>
10 
11 namespace Botan {
12 
13 size_t HKDF::kdf(uint8_t key[], size_t key_len,
14  const uint8_t secret[], size_t secret_len,
15  const uint8_t salt[], size_t salt_len,
16  const uint8_t label[], size_t label_len) const
17  {
18  HKDF_Extract extract(m_prf->clone());
19  HKDF_Expand expand(m_prf->clone());
20  secure_vector<uint8_t> prk(m_prf->output_length());
21 
22  extract.kdf(prk.data(), prk.size(), secret, secret_len, salt, salt_len, nullptr, 0);
23  return expand.kdf(key, key_len, prk.data(), prk.size(), nullptr, 0, label, label_len);
24  }
25 
26 size_t HKDF_Extract::kdf(uint8_t key[], size_t key_len,
27  const uint8_t secret[], size_t secret_len,
28  const uint8_t salt[], size_t salt_len,
29  const uint8_t[], size_t) const
30  {
32  if(salt_len == 0)
33  {
34  m_prf->set_key(std::vector<uint8_t>(m_prf->output_length()));
35  }
36  else
37  {
38  m_prf->set_key(salt, salt_len);
39  }
40 
41  m_prf->update(secret, secret_len);
42  m_prf->final(prk);
43 
44  const size_t written = std::min(prk.size(), key_len);
45  copy_mem(&key[0], prk.data(), written);
46  return written;
47  }
48 
49 size_t HKDF_Expand::kdf(uint8_t key[], size_t key_len,
50  const uint8_t secret[], size_t secret_len,
51  const uint8_t salt[], size_t salt_len,
52  const uint8_t label[], size_t label_len) const
53  {
54  m_prf->set_key(secret, secret_len);
55 
56  uint8_t counter = 1;
58  size_t offset = 0;
59 
60  while(offset != key_len && counter != 0)
61  {
62  m_prf->update(h);
63  m_prf->update(label, label_len);
64  m_prf->update(salt, salt_len);
65  m_prf->update(counter++);
66  m_prf->final(h);
67 
68  const size_t written = std::min(h.size(), key_len - offset);
69  copy_mem(&key[offset], h.data(), written);
70  offset += written;
71  }
72 
73  return offset;
74  }
75 
77 hkdf_expand_label(const std::string& hash_fn,
78  const uint8_t secret[], size_t secret_len,
79  const std::string& label,
80  const uint8_t hash_val[], size_t hash_val_len,
81  size_t length)
82  {
83  BOTAN_ARG_CHECK(length <= 0xFFFF, "HKDF-Expand-Label requested output too large");
84  BOTAN_ARG_CHECK(label.size() <= 0xFF, "HKDF-Expand-Label label too long");
85  BOTAN_ARG_CHECK(hash_val_len <= 0xFF, "HKDF-Expand-Label hash too long");
86 
87  const uint16_t length16 = static_cast<uint16_t>(length);
88 
89  auto mac = MessageAuthenticationCode::create_or_throw("HMAC(" + hash_fn + ")");
90 
91  HKDF_Expand hkdf(mac.release());
92 
93  secure_vector<uint8_t> output(length16);
94  std::vector<uint8_t> prefix(3 + label.size() + 1);
95 
96  prefix[0] = get_byte(0, length16);
97  prefix[1] = get_byte(1, length16);
98  prefix[2] = static_cast<uint8_t>(label.size());
99 
100  copy_mem(prefix.data() + 3,
101  cast_char_ptr_to_uint8(label.data()),
102  label.size());
103 
104  prefix[3 + label.size()] = static_cast<uint8_t>(hash_val_len);
105 
106  /*
107  * We do something a little dirty here to avoid copying the hash_val,
108  * making use of the fact that Botan's KDF interface supports label+salt,
109  * and knowing that our HKDF hashes first param label then param salt.
110  */
111  hkdf.kdf(output.data(), output.size(),
112  secret, secret_len,
113  hash_val, hash_val_len,
114  prefix.data(), prefix.size());
115 
116  return output;
117  }
118 
119 }
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:131
size_t kdf(uint8_t key[], size_t key_len, const uint8_t secret[], size_t secret_len, const uint8_t salt[], size_t salt_len, const uint8_t label[], size_t label_len) const override
Definition: hkdf.cpp:13
size_t kdf(uint8_t key[], size_t key_len, const uint8_t secret[], size_t secret_len, const uint8_t salt[], size_t salt_len, const uint8_t label[], size_t label_len) const override
Definition: hkdf.cpp:26
size_t salt_len
Definition: x509_obj.cpp:26
secure_vector< uint8_t > hkdf_expand_label(const std::string &hash_fn, const uint8_t secret[], size_t secret_len, const std::string &label, const uint8_t hash_val[], size_t hash_val_len, size_t length)
Definition: hkdf.cpp:77
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:108
Definition: alg_id.cpp:13
#define BOTAN_ARG_CHECK(expr, msg)
Definition: assert.h:37
size_t kdf(uint8_t key[], size_t key_len, const uint8_t secret[], size_t secret_len, const uint8_t salt[], size_t salt_len, const uint8_t label[], size_t label_len) const override
Definition: hkdf.cpp:49
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:141
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:39
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88