Botan 3.6.1
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
10#include <botan/exceptn.h>
11#include <botan/internal/fmt.h>
12
13namespace Botan {
14
15std::string KDF2::name() const {
16 return fmt("KDF2({})", m_hash->name());
17}
18
19std::unique_ptr<KDF> KDF2::new_object() const {
20 return std::make_unique<KDF2>(m_hash->new_object());
21}
22
23void KDF2::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 const size_t blocks_required = key_len / m_hash->output_length();
36
37 if(blocks_required >= 0xFFFFFFFE) {
38 throw Invalid_Argument("KDF2 maximum output length exceeeded");
39 }
40
41 uint32_t counter = 1;
43
44 size_t offset = 0;
45 while(offset != key_len) {
46 m_hash->update(secret, secret_len);
47 m_hash->update_be(counter);
48 m_hash->update(label, label_len);
49 m_hash->update(salt, salt_len);
50 m_hash->final(h);
51
52 const size_t added = std::min(h.size(), key_len - offset);
53 copy_mem(&key[offset], h.data(), added);
54 offset += added;
55
56 counter += 1;
57 BOTAN_ASSERT_NOMSG(counter != 0); // no overflow
58 }
59}
60
61} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
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:23
std::unique_ptr< KDF > new_object() const override
Definition kdf2.cpp:19
std::string name() const override
Definition kdf2.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