Botan 3.0.0
Crypto and TLS for C&
kdf2.cpp
Go to the documentation of this file.
1/*
2* KDF2
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/kdf2.h>
9#include <botan/internal/fmt.h>
10#include <botan/exceptn.h>
11
12namespace Botan {
13
14std::string KDF2::name() const
15 {
16 return fmt("KDF2({})", m_hash->name());
17 }
18
19std::unique_ptr<KDF> KDF2::new_object() const
20 {
21 return std::make_unique<KDF2>(m_hash->new_object());
22 }
23
24void KDF2::kdf(uint8_t key[], size_t key_len,
25 const uint8_t secret[], size_t secret_len,
26 const uint8_t salt[], size_t salt_len,
27 const uint8_t label[], size_t label_len) const
28 {
29 if(key_len == 0)
30 return;
31
32 const size_t blocks_required = key_len / m_hash->output_length();
33
34 if(blocks_required >= 0xFFFFFFFE)
35 throw Invalid_Argument("KDF2 maximum output length exceeeded");
36
37 uint32_t counter = 1;
39
40 size_t offset = 0;
41 while(offset != key_len)
42 {
43 m_hash->update(secret, secret_len);
44 m_hash->update_be(counter);
45 m_hash->update(label, label_len);
46 m_hash->update(salt, salt_len);
47 m_hash->final(h);
48
49 const size_t added = std::min(h.size(), key_len - offset);
50 copy_mem(&key[offset], h.data(), added);
51 offset += added;
52
53 counter += 1;
54 BOTAN_ASSERT_NOMSG(counter != 0); // no overflow
55 }
56 }
57
58}
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:67
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: kdf2.cpp:24
std::unique_ptr< KDF > new_object() const override
Definition: kdf2.cpp:19
std::string name() const override
Definition: kdf2.cpp:14
Definition: alg_id.cpp:12
std::string fmt(std::string_view format, const T &... args)
Definition: fmt.h:60
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:126
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:64