/src/botan/src/lib/kdf/kdf1/kdf1.cpp
Line | Count | Source |
1 | | /* |
2 | | * KDF1 |
3 | | * (C) 1999-2007 Jack Lloyd |
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.h> |
10 | | |
11 | | #include <botan/exceptn.h> |
12 | | #include <botan/mem_ops.h> |
13 | | #include <botan/internal/fmt.h> |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | 0 | std::string KDF1::name() const { |
18 | 0 | return fmt("KDF1({})", m_hash->name()); |
19 | 0 | } |
20 | | |
21 | 0 | std::unique_ptr<KDF> KDF1::new_object() const { |
22 | 0 | return std::make_unique<KDF1>(m_hash->new_object()); |
23 | 0 | } |
24 | | |
25 | | void KDF1::perform_kdf(std::span<uint8_t> key, |
26 | | std::span<const uint8_t> secret, |
27 | | std::span<const uint8_t> salt, |
28 | 0 | std::span<const uint8_t> label) const { |
29 | 0 | if(key.empty()) { |
30 | 0 | return; |
31 | 0 | } |
32 | | |
33 | 0 | const size_t hash_output_len = m_hash->output_length(); |
34 | 0 | BOTAN_ARG_CHECK(key.size() <= hash_output_len, "KDF1 maximum output length exceeeded"); |
35 | |
|
36 | 0 | m_hash->update(secret); |
37 | 0 | m_hash->update(label); |
38 | 0 | m_hash->update(salt); |
39 | |
|
40 | 0 | if(key.size() == hash_output_len) { |
41 | | // In this case we can hash directly into the output buffer |
42 | 0 | m_hash->final(key); |
43 | 0 | } else { |
44 | | // Otherwise a copy is required |
45 | 0 | const auto v = m_hash->final(); |
46 | 0 | copy_mem(key, std::span{v}.first(key.size())); |
47 | 0 | } |
48 | 0 | } |
49 | | |
50 | | } // namespace Botan |