Botan 3.9.0
Crypto and TLS for C&
sp800_108.cpp
Go to the documentation of this file.
1/*
2* KDFs defined in NIST SP 800-108
3* (C) 2016 Kai Michaelis
4* (C) 2024 René Meusel, Rohde & Schwarz Cybersecurity
5* (C) 2025 René Meusel, Amos Treiber, Rohde & Schwarz Cybersecurity
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#include <botan/internal/sp800_108.h>
11
12#include <botan/exceptn.h>
13#include <botan/internal/bit_ops.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/loadstor.h>
16#include <botan/internal/stl_util.h>
17
18#include <limits>
19
20namespace Botan {
21
22namespace {
23
24class CounterParams {
25 public:
26 constexpr static void validate_bit_lengths(size_t counter_bits, size_t output_length_bits) {
27 BOTAN_ARG_CHECK(counter_bits % 8 == 0 && counter_bits <= 32,
28 "SP.800-108 counter length may be one of {8, 16, 24, 32} only");
29 BOTAN_ARG_CHECK(output_length_bits % 8 == 0 && output_length_bits <= 32,
30 "SP.800-108 output length encoding may be one of {8, 16, 24, 32} only");
31 }
32
33 static CounterParams create_or_throw(size_t output_bytes,
34 size_t output_length_bits,
35 size_t counter_bits,
36 size_t prf_output_bytes) {
37 // The maximum legal output bit length is limited by the requested encoding
38 // bit length of the "L" field.
39 BOTAN_ARG_CHECK(static_cast<uint64_t>(output_bytes) * 8 <= std::numeric_limits<uint32_t>::max(),
40 "SP.800-108 output size in bits does not fit into 32-bits");
41 const auto output_bits = static_cast<uint32_t>(output_bytes * 8);
42 const auto max_output_bits = (uint64_t(1) << output_length_bits) - 1;
43 BOTAN_ARG_CHECK(output_bits <= max_output_bits,
44 "SP.800-108 output size does not fit into the requested field length");
45
46 // Calculate the number of blocks needed to serve the requested bytes
47 const auto blocks_required = ceil_division<uint64_t /* for 32bit systems */>(output_bytes, prf_output_bytes);
48
49 // The maximal legal invocation count of the PRF is limited by the requested
50 // encoding bit length of the counter ("r"). The counter starts at "1", so the
51 // maximum number of usable blocks is 2^r - 1.
52 const auto max_blocks = (uint64_t(1) << counter_bits) - 1;
53 BOTAN_ARG_CHECK(blocks_required < max_blocks, "SP.800-108 output size too large");
54
55 return CounterParams(
56 output_bits, output_length_bits / 8, counter_bits / 8, static_cast<uint32_t>(blocks_required));
57 }
58
59 template <std::invocable<std::span<const uint8_t>, std::span<const uint8_t>> Fn>
60 constexpr void generate_blocks(Fn fn) const {
61 const auto outlen_encoded = store_be(m_output_length_bits);
62 for(uint32_t counter = 1; counter <= m_blocks_required; ++counter) {
63 const auto counter_encoded = store_be(counter);
64 fn(std::span{counter_encoded}.last(m_counter_bytes),
65 std::span{outlen_encoded}.last(m_output_length_encoding_bytes));
66 }
67 }
68
69 private:
70 CounterParams(uint32_t output_length_bits,
71 size_t output_length_encoding_bytes,
72 size_t counter_bytes,
73 uint32_t blocks_required) :
74 m_output_length_bits(output_length_bits),
75 m_output_length_encoding_bytes(output_length_encoding_bytes),
76 m_counter_bytes(counter_bytes),
77 m_blocks_required(blocks_required) {}
78
79 private:
80 uint32_t m_output_length_bits;
81 size_t m_output_length_encoding_bytes;
82 size_t m_counter_bytes;
83 uint32_t m_blocks_required;
84};
85
86} // namespace
87
88SP800_108_Counter::SP800_108_Counter(std::unique_ptr<MessageAuthenticationCode> mac, size_t r, size_t L) :
89 m_prf(std::move(mac)), m_counter_bits(r), m_output_length_bits(L) {
90 CounterParams::validate_bit_lengths(m_counter_bits, m_output_length_bits);
91}
92
93std::string SP800_108_Counter::name() const {
94 // TODO(Botan4): make this always return the explicit lengths
95 return (m_counter_bits == 32 && m_output_length_bits == 32)
96 ? fmt("SP800-108-Counter({})", m_prf->name()) // keep the name consistent with previous versions
97 : fmt("SP800-108-Counter({},{},{})", m_prf->name(), m_counter_bits, m_output_length_bits);
98}
99
100std::unique_ptr<KDF> SP800_108_Counter::new_object() const {
101 return std::make_unique<SP800_108_Counter>(m_prf->new_object(), m_counter_bits, m_output_length_bits);
102}
103
104void SP800_108_Counter::perform_kdf(std::span<uint8_t> key,
105 std::span<const uint8_t> secret,
106 std::span<const uint8_t> salt,
107 std::span<const uint8_t> label) const {
108 if(key.empty()) {
109 return;
110 }
111
112 const auto prf_len = m_prf->output_length();
113 const auto params = CounterParams::create_or_throw(key.size(), m_output_length_bits, m_counter_bits, prf_len);
114
115 constexpr uint8_t delim = 0;
116
117 BufferStuffer k(key);
118 m_prf->set_key(secret);
119
120 params.generate_blocks([&](auto counter_encoded, auto outlen_encoded) {
121 m_prf->update(counter_encoded);
122 m_prf->update(label);
123 m_prf->update(delim);
124 m_prf->update(salt);
125 m_prf->update(outlen_encoded);
126
127 // Write straight into the output buffer, except if the PRF output needs
128 // a truncation in the final iteration.
129 if(k.remaining_capacity() >= prf_len) {
130 m_prf->final(k.next(prf_len));
131 } else {
132 const auto h = m_prf->final();
133 k.append(std::span{h}.first(k.remaining_capacity()));
134 }
135 });
136
137 BOTAN_ASSERT_NOMSG(k.full());
138}
139
140SP800_108_Feedback::SP800_108_Feedback(std::unique_ptr<MessageAuthenticationCode> mac, size_t r, size_t L) :
141 m_prf(std::move(mac)), m_counter_bits(r), m_output_length_bits(L) {
142 CounterParams::validate_bit_lengths(m_counter_bits, m_output_length_bits);
143}
144
145std::string SP800_108_Feedback::name() const {
146 // TODO(Botan4): make this always return the explicit lengths
147 return (m_counter_bits == 32 && m_output_length_bits == 32)
148 ? fmt("SP800-108-Feedback({})", m_prf->name()) // keep the name consistent with previous versions
149 : fmt("SP800-108-Feedback({},{},{})", m_prf->name(), m_counter_bits, m_output_length_bits);
150}
151
152std::unique_ptr<KDF> SP800_108_Feedback::new_object() const {
153 return std::make_unique<SP800_108_Feedback>(m_prf->new_object(), m_counter_bits, m_output_length_bits);
154}
155
156void SP800_108_Feedback::perform_kdf(std::span<uint8_t> key,
157 std::span<const uint8_t> secret,
158 std::span<const uint8_t> salt,
159 std::span<const uint8_t> label) const {
160 if(key.empty()) {
161 return;
162 }
163
164 const auto prf_len = m_prf->output_length();
165 const auto iv_len = (salt.size() >= prf_len ? prf_len : 0);
166 constexpr uint8_t delim = 0;
167 const auto params = CounterParams::create_or_throw(key.size(), m_output_length_bits, m_counter_bits, prf_len);
168
169 BufferSlicer s(salt);
170 auto prev = s.copy_as_secure_vector(iv_len);
171 const auto ctx = s.take(s.remaining());
172 BOTAN_ASSERT_NOMSG(s.empty());
173
174 BufferStuffer k(key);
175 m_prf->set_key(secret);
176
177 params.generate_blocks([&](auto counter_encoded, auto outlen_encoded) {
178 m_prf->update(prev);
179 m_prf->update(counter_encoded);
180 m_prf->update(label);
181 m_prf->update(delim);
182 m_prf->update(ctx);
183 m_prf->update(outlen_encoded);
184 m_prf->final(prev);
185
186 const auto bytes_to_write = std::min(prev.size(), k.remaining_capacity());
187 k.append(std::span{prev}.first(bytes_to_write));
188 });
189
190 BOTAN_ASSERT_NOMSG(k.full());
191}
192
193SP800_108_Pipeline::SP800_108_Pipeline(std::unique_ptr<MessageAuthenticationCode> mac, size_t r, size_t L) :
194 m_prf(std::move(mac)), m_counter_bits(r), m_output_length_bits(L) {
195 CounterParams::validate_bit_lengths(m_counter_bits, m_output_length_bits);
196}
197
198std::string SP800_108_Pipeline::name() const {
199 // TODO(Botan4): make this always return the explicit lengths
200 return (m_counter_bits == 32 && m_output_length_bits == 32)
201 ? fmt("SP800-108-Pipeline({})", m_prf->name()) // keep the name consistent with previous versions
202 : fmt("SP800-108-Pipeline({},{},{})", m_prf->name(), m_counter_bits, m_output_length_bits);
203}
204
205std::unique_ptr<KDF> SP800_108_Pipeline::new_object() const {
206 return std::make_unique<SP800_108_Pipeline>(m_prf->new_object(), m_counter_bits, m_output_length_bits);
207}
208
209void SP800_108_Pipeline::perform_kdf(std::span<uint8_t> key,
210 std::span<const uint8_t> secret,
211 std::span<const uint8_t> salt,
212 std::span<const uint8_t> label) const {
213 if(key.empty()) {
214 return;
215 }
216
217 const auto prf_len = m_prf->output_length();
218 constexpr uint8_t delim = 0;
219 const auto params = CounterParams::create_or_throw(key.size(), m_output_length_bits, m_counter_bits, prf_len);
220
221 BufferStuffer k(key);
222 m_prf->set_key(secret);
224
225 params.generate_blocks([&](auto counter_encoded, auto outlen_encoded) {
226 auto incorporate_constant_input = [label, salt, outlen_encoded](MessageAuthenticationCode* prf) {
227 prf->update(label);
228 prf->update(delim);
229 prf->update(salt);
230 prf->update(outlen_encoded);
231 };
232
233 if(scratch.empty()) {
234 // A(0)
235 incorporate_constant_input(m_prf.get());
236 scratch = m_prf->final();
237 } else {
238 // A(i)
239 m_prf->update(scratch);
240 m_prf->final(scratch);
241 }
242
243 m_prf->update(scratch);
244 m_prf->update(counter_encoded);
245 incorporate_constant_input(m_prf.get());
246
247 // Write straight into the output buffer, except if the PRF output needs
248 // a truncation in the final iteration.
249 if(k.remaining_capacity() >= prf_len) {
250 m_prf->final(k.next(prf_len));
251 } else {
252 // This must be the final iteration anyway, so we can just reuse the
253 // 'ai' scratch space to avoid allocating another short-lived buffer.
254 m_prf->final(scratch);
255 k.append(std::span{scratch}.first(k.remaining_capacity()));
256 }
257 });
258
259 BOTAN_ASSERT_NOMSG(k.full());
260}
261
262} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
SP800_108_Counter(std::unique_ptr< MessageAuthenticationCode > mac, size_t r, size_t L)
Definition sp800_108.cpp:88
std::string name() const override
Definition sp800_108.cpp:93
std::unique_ptr< KDF > new_object() const override
std::string name() const override
SP800_108_Feedback(std::unique_ptr< MessageAuthenticationCode > mac, size_t r, size_t L)
std::unique_ptr< KDF > new_object() const override
std::unique_ptr< KDF > new_object() const override
std::string name() const override
SP800_108_Pipeline(std::unique_ptr< MessageAuthenticationCode > mac, size_t r, size_t L)
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
BOTAN_FORCE_INLINE constexpr T ceil_division(T a, T b)
Definition bit_ops.h:147
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745