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