Botan 3.10.0
Crypto and TLS for C&
kdf1_iso18033.cpp
Go to the documentation of this file.
1/*
2* KDF1 from ISO 18033-2
3* (C) 2016 Philipp Weber
4* (C) 2024 René Meusel, Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/kdf1_iso18033.h>
10
11#include <botan/internal/bit_ops.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/stl_util.h>
14
15namespace Botan {
16
17void KDF1_18033::perform_kdf(std::span<uint8_t> key,
18 std::span<const uint8_t> secret,
19 std::span<const uint8_t> salt,
20 std::span<const uint8_t> label) const {
21 if(key.empty()) {
22 return;
23 }
24
25 const auto hash_output_length = m_hash->output_length();
26 const auto blocks_required = ceil_division<uint64_t /* for 32bit systems */>(key.size(), hash_output_length);
27
28 // This KDF uses a 32-bit counter for the hash blocks, initialized at 0.
29 // It will wrap around after 2^32 iterations which limits the theoretically
30 // possible output to 2^32 blocks.
31 BOTAN_ARG_CHECK(blocks_required <= 0xFFFFFFFF, "KDF1-18033 maximum output length exceeded");
32
33 BufferStuffer k(key);
34 for(uint32_t counter = 0; !k.full(); ++counter) {
35 m_hash->update(secret);
36 m_hash->update_be(counter);
37 m_hash->update(label);
38 m_hash->update(salt);
39
40 // Write straight into the output buffer, except if the hash output needs
41 // a truncation in the final iteration.
42 if(k.remaining_capacity() >= hash_output_length) {
43 m_hash->final(k.next(hash_output_length));
44 } else {
45 const auto h = m_hash->final();
46 k.append(std::span{h}.first(k.remaining_capacity()));
47 }
48 }
49}
50
51std::string KDF1_18033::name() const {
52 return fmt("KDF1-18033({})", m_hash->name());
53}
54
55std::unique_ptr<KDF> KDF1_18033::new_object() const {
56 return std::make_unique<KDF1_18033>(m_hash->new_object());
57}
58
59} // namespace Botan
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
std::unique_ptr< KDF > new_object() const override
std::string name() const override
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:160