Botan 3.9.0
Crypto and TLS for C&
kdf1.cpp
Go to the documentation of this file.
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
15namespace Botan {
16
17std::string KDF1::name() const {
18 return fmt("KDF1({})", m_hash->name());
19}
20
21std::unique_ptr<KDF> KDF1::new_object() const {
22 return std::make_unique<KDF1>(m_hash->new_object());
23}
24
25void KDF1::perform_kdf(std::span<uint8_t> key,
26 std::span<const uint8_t> secret,
27 std::span<const uint8_t> salt,
28 std::span<const uint8_t> label) const {
29 if(key.empty()) {
30 return;
31 }
32
33 const size_t hash_output_len = m_hash->output_length();
34 BOTAN_ARG_CHECK(key.size() <= hash_output_len, "KDF1 maximum output length exceeeded");
35
36 m_hash->update(secret);
37 m_hash->update(label);
38 m_hash->update(salt);
39
40 if(key.size() == hash_output_len) {
41 // In this case we can hash directly into the output buffer
42 m_hash->final(key);
43 } else {
44 // Otherwise a copy is required
45 const auto v = m_hash->final();
46 copy_mem(key, std::span{v}.first(key.size()));
47 }
48}
49
50} // namespace Botan
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
std::unique_ptr< KDF > new_object() const override
Definition kdf1.cpp:21
std::string name() const override
Definition kdf1.cpp:17
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:145
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53