Botan 3.4.0
Crypto and TLS for C&
kdf1_iso18033.cpp
Go to the documentation of this file.
1/*
2* KDF1 from ISO 18033-2
3* (C) 2016 Philipp Weber
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/kdf1_iso18033.h>
9
10#include <botan/exceptn.h>
11#include <botan/internal/fmt.h>
12
13namespace Botan {
14
15void KDF1_18033::kdf(uint8_t key[],
16 size_t key_len,
17 const uint8_t secret[],
18 size_t secret_len,
19 const uint8_t salt[],
20 size_t salt_len,
21 const uint8_t label[],
22 size_t label_len) const {
23 if(key_len == 0) {
24 return;
25 }
26
27 const size_t blocks_required = key_len / m_hash->output_length();
28
29 if(blocks_required >= 0xFFFFFFFE) {
30 throw Invalid_Argument("KDF1-18033 maximum output length exceeeded");
31 }
32
33 uint32_t counter = 0;
35
36 size_t offset = 0;
37 while(offset != key_len) {
38 m_hash->update(secret, secret_len);
39 m_hash->update_be(counter++);
40 m_hash->update(label, label_len);
41 m_hash->update(salt, salt_len);
42 m_hash->final(h);
43
44 const size_t added = std::min(h.size(), key_len - offset);
45 copy_mem(&key[offset], h.data(), added);
46 offset += added;
47 }
48}
49
50std::string KDF1_18033::name() const {
51 return fmt("KDF1-18033({})", m_hash->name());
52}
53
54std::unique_ptr<KDF> KDF1_18033::new_object() const {
55 return std::make_unique<KDF1_18033>(m_hash->new_object());
56}
57
58} // 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
std::unique_ptr< KDF > new_object() const override
std::string name() const override
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