Coverage Report

Created: 2025-03-09 06:52

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