Botan 3.6.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*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/kdf1.h>
9
10#include <botan/exceptn.h>
11#include <botan/internal/fmt.h>
12
13namespace Botan {
14
15std::string KDF1::name() const {
16 return fmt("KDF1({})", m_hash->name());
17}
18
19std::unique_ptr<KDF> KDF1::new_object() const {
20 return std::make_unique<KDF1>(m_hash->new_object());
21}
22
23void KDF1::kdf(uint8_t key[],
24 size_t key_len,
25 const uint8_t secret[],
26 size_t secret_len,
27 const uint8_t salt[],
28 size_t salt_len,
29 const uint8_t label[],
30 size_t label_len) const {
31 if(key_len == 0) {
32 return;
33 }
34
35 if(key_len > m_hash->output_length()) {
36 throw Invalid_Argument("KDF1 maximum output length exceeeded");
37 }
38
39 m_hash->update(secret, secret_len);
40 m_hash->update(label, label_len);
41 m_hash->update(salt, salt_len);
42
43 if(key_len == m_hash->output_length()) {
44 // In this case we can hash directly into the output buffer
45 m_hash->final(key);
46 } else {
47 // Otherwise a copy is required
48 secure_vector<uint8_t> v = m_hash->final();
49 copy_mem(key, v.data(), key_len);
50 }
51}
52
53} // namespace Botan
void kdf(uint8_t key[], size_t key_len, const uint8_t secret[], size_t secret_len, const uint8_t salt[], size_t salt_len, const uint8_t label[], size_t label_len) const override
Definition kdf1.cpp:23
std::unique_ptr< KDF > new_object() const override
Definition kdf1.cpp:19
std::string name() const override
Definition kdf1.cpp:15
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146