Coverage Report

Created: 2026-08-14 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/src/lib/kdf/hkdf/hkdf.cpp
Line
Count
Source
1
/*
2
* HKDF
3
* (C) 2013,2015,2017 Jack Lloyd
4
* (C) 2016 René Korthaus, Rohde & Schwarz Cybersecurity
5
* (C) 2024 René Meusel, Rohde & Schwarz Cybersecurity
6
*
7
* Botan is released under the Simplified BSD License (see license.txt)
8
*/
9
10
#include <botan/internal/hkdf.h>
11
12
#include <botan/exceptn.h>
13
#include <botan/internal/fmt.h>
14
#include <botan/internal/loadstor.h>
15
#include <botan/internal/mem_utils.h>
16
#include <botan/internal/stl_util.h>
17
18
namespace Botan {
19
20
0
std::unique_ptr<KDF> HKDF::new_object() const {
21
0
   return std::make_unique<HKDF>(m_prf->new_object());
22
0
}
23
24
0
std::string HKDF::name() const {
25
0
   return fmt("HKDF({})", m_prf->name());
26
0
}
27
28
void HKDF::perform_kdf(std::span<uint8_t> key,
29
                       std::span<const uint8_t> secret,
30
                       std::span<const uint8_t> salt,
31
0
                       std::span<const uint8_t> label) const {
32
0
   HKDF_Extract extract(m_prf->new_object());
33
0
   HKDF_Expand expand(m_prf->new_object());
34
0
   secure_vector<uint8_t> prk(m_prf->output_length());
35
36
0
   extract.derive_key(prk, secret, salt, {});
37
0
   expand.derive_key(key, prk, {}, label);
38
0
}
39
40
0
std::unique_ptr<KDF> HKDF_Extract::new_object() const {
41
0
   return std::make_unique<HKDF_Extract>(m_prf->new_object());
42
0
}
43
44
0
std::string HKDF_Extract::name() const {
45
0
   return fmt("HKDF-Extract({})", m_prf->name());
46
0
}
47
48
void HKDF_Extract::perform_kdf(std::span<uint8_t> key,
49
                               std::span<const uint8_t> secret,
50
                               std::span<const uint8_t> salt,
51
0
                               std::span<const uint8_t> label) const {
52
0
   const size_t prf_output_len = m_prf->output_length();
53
0
   BOTAN_ARG_CHECK(key.size() <= prf_output_len, "HKDF-Extract maximum output length exceeeded");
54
0
   BOTAN_ARG_CHECK(label.empty(), "HKDF-Extract does not support a label input");
55
56
0
   if(key.empty()) {
57
0
      return;
58
0
   }
59
60
0
   if(salt.empty()) {
61
0
      m_prf->set_key(std::vector<uint8_t>(prf_output_len));
62
0
   } else {
63
0
      m_prf->set_key(salt);
64
0
   }
65
66
0
   m_prf->update(secret);
67
68
0
   if(key.size() == prf_output_len) {
69
0
      m_prf->final(key);
70
0
   } else {
71
0
      const auto prk = m_prf->final();
72
0
      copy_mem(key, std::span{prk}.first(key.size()));
73
0
   }
74
0
}
75
76
0
std::unique_ptr<KDF> HKDF_Expand::new_object() const {
77
0
   return std::make_unique<HKDF_Expand>(m_prf->new_object());
78
0
}
79
80
0
std::string HKDF_Expand::name() const {
81
0
   return fmt("HKDF-Expand({})", m_prf->name());
82
0
}
83
84
void HKDF_Expand::perform_kdf(std::span<uint8_t> key,
85
                              std::span<const uint8_t> secret,
86
                              std::span<const uint8_t> salt,
87
0
                              std::span<const uint8_t> label) const {
88
0
   const auto prf_output_length = m_prf->output_length();
89
0
   BOTAN_ARG_CHECK(key.size() <= prf_output_length * 255, "HKDF-Expand maximum output length exceeeded");
90
91
0
   if(key.empty()) {
92
0
      return;
93
0
   }
94
95
   // Keep a reference to the previous PRF output (empty by default).
96
0
   std::span<uint8_t> h = {};
97
98
0
   BufferStuffer k(key);
99
0
   m_prf->set_key(secret);
100
0
   for(uint8_t counter = 1; !k.full(); ++counter) {
101
0
      m_prf->update(h);
102
0
      m_prf->update(label);
103
0
      m_prf->update(salt);
104
0
      m_prf->update(counter);
105
106
      // Write straight into the output buffer, except if the PRF output needs
107
      // a truncation in the final iteration.
108
0
      if(k.remaining_capacity() >= prf_output_length) {
109
0
         h = k.next(prf_output_length);
110
0
         m_prf->final(h);
111
0
      } else {
112
0
         const auto full_prf_output = m_prf->final();
113
0
         h = {};  // this is the final iteration!
114
0
         k.append(std::span{full_prf_output}.first(k.remaining_capacity()));
115
0
      }
116
0
   }
117
0
}
118
119
secure_vector<uint8_t> hkdf_expand_label(std::string_view hash_fn,
120
                                         std::span<const uint8_t> secret,
121
                                         std::string_view label,
122
                                         std::span<const uint8_t> hash_val,
123
0
                                         size_t length) {
124
0
   BOTAN_ARG_CHECK(length <= 0xFFFF, "HKDF-Expand-Label requested output too large");
125
0
   BOTAN_ARG_CHECK(label.size() <= 0xFF, "HKDF-Expand-Label label too long");
126
0
   BOTAN_ARG_CHECK(hash_val.size() <= 0xFF, "HKDF-Expand-Label hash too long");
127
128
0
   HKDF_Expand hkdf(MessageAuthenticationCode::create_or_throw(fmt("HMAC({})", hash_fn)));
129
130
0
   const auto prefix = concat<std::vector<uint8_t>>(store_be(static_cast<uint16_t>(length)),
131
0
                                                    store_be(static_cast<uint8_t>(label.size())),
132
0
                                                    as_span_of_bytes(label),
133
0
                                                    store_be(static_cast<uint8_t>(hash_val.size())));
134
135
   /*
136
   * We do something a little dirty here to avoid copying the hash_val,
137
   * making use of the fact that Botan's KDF interface supports label+salt,
138
   * and knowing that our HKDF hashes first param label then param salt.
139
   */
140
0
   return hkdf.derive_key(length, secret, hash_val, prefix);
141
0
}
142
143
}  // namespace Botan