9#include <botan/internal/hkdf.h>
11#include <botan/exceptn.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/loadstor.h>
18 return std::make_unique<HKDF>(m_prf->new_object());
22 return fmt(
"HKDF({})", m_prf->name());
27 const uint8_t secret[],
31 const uint8_t label[],
32 size_t label_len)
const {
37 extract.
kdf(prk.data(), prk.size(), secret, secret_len, salt, salt_len,
nullptr, 0);
38 expand.
kdf(key, key_len, prk.data(), prk.size(),
nullptr, 0, label, label_len);
42 return std::make_unique<HKDF_Extract>(m_prf->new_object());
46 return fmt(
"HKDF-Extract({})", m_prf->name());
51 const uint8_t secret[],
56 size_t label_len)
const {
61 const size_t prf_output_len = m_prf->output_length();
63 if(key_len > prf_output_len) {
72 m_prf->set_key(std::vector<uint8_t>(prf_output_len));
74 m_prf->set_key(salt, salt_len);
77 m_prf->update(secret, secret_len);
79 if(key_len == prf_output_len) {
84 copy_mem(&key[0], prk.data(), key_len);
89 return std::make_unique<HKDF_Expand>(m_prf->new_object());
93 return fmt(
"HKDF-Expand({})", m_prf->name());
98 const uint8_t secret[],
100 const uint8_t salt[],
102 const uint8_t label[],
103 size_t label_len)
const {
108 if(key_len > m_prf->output_length() * 255) {
112 m_prf->set_key(secret, secret_len);
118 while(offset != key_len) {
120 m_prf->update(label, label_len);
121 m_prf->update(salt, salt_len);
122 m_prf->update(counter++);
125 const size_t written = std::min(h.size(), key_len - offset);
126 copy_mem(&key[offset], h.data(), written);
132 const uint8_t secret[],
134 std::string_view label,
135 const uint8_t hash_val[],
138 BOTAN_ARG_CHECK(length <= 0xFFFF,
"HKDF-Expand-Label requested output too large");
139 BOTAN_ARG_CHECK(label.size() <= 0xFF,
"HKDF-Expand-Label label too long");
140 BOTAN_ARG_CHECK(hash_val_len <= 0xFF,
"HKDF-Expand-Label hash too long");
142 const uint16_t length16 =
static_cast<uint16_t
>(length);
147 std::vector<uint8_t> prefix(3 + label.size() + 1);
151 prefix[2] =
static_cast<uint8_t
>(label.size());
155 prefix[3 + label.size()] =
static_cast<uint8_t
>(hash_val_len);
162 hkdf.
kdf(output.data(), output.size(), secret, secret_len, hash_val, hash_val_len, prefix.data(), prefix.size());
#define BOTAN_ARG_CHECK(expr, msg)
std::string name() const override
std::unique_ptr< KDF > new_object() const override
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::string name() const override
std::unique_ptr< KDF > new_object() const override
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
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
constexpr uint8_t get_byte(T input)
std::string fmt(std::string_view format, const T &... args)
secure_vector< uint8_t > hkdf_expand_label(std::string_view hash_fn, const uint8_t secret[], size_t secret_len, std::string_view label, const uint8_t hash_val[], size_t hash_val_len, size_t length)
std::vector< T, secure_allocator< T > > secure_vector
constexpr void copy_mem(T *out, const T *in, size_t n)
const uint8_t * cast_char_ptr_to_uint8(const char *s)