diff options
author | lloyd <[email protected]> | 2015-02-18 04:51:17 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2015-02-18 04:51:17 +0000 |
commit | 888aed4ec2f08684a9707c1251f27285942578c5 (patch) | |
tree | 4c78c5ca5a31b217f407c3adbd138a227181b1c5 /src/lib/kdf | |
parent | 88285f51ba4fd5bc1a1cc06b0760b3926046f29b (diff) |
Convert HKDF to the normal KDF interface
Diffstat (limited to 'src/lib/kdf')
-rw-r--r-- | src/lib/kdf/hkdf/hkdf.cpp | 64 | ||||
-rw-r--r-- | src/lib/kdf/hkdf/hkdf.h | 29 |
2 files changed, 34 insertions, 59 deletions
diff --git a/src/lib/kdf/hkdf/hkdf.cpp b/src/lib/kdf/hkdf/hkdf.cpp index 28f97cadb..8c92c779b 100644 --- a/src/lib/kdf/hkdf/hkdf.cpp +++ b/src/lib/kdf/hkdf/hkdf.cpp @@ -1,67 +1,51 @@ /* * HKDF -* (C) 2013 Jack Lloyd +* (C) 2013,2015 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ +#include <botan/internal/kdf_utils.h> #include <botan/hkdf.h> namespace Botan { -std::string HKDF::name() const - { - const std::string prf = m_prf->name(); - const std::string ext = m_extractor->name(); - - if(prf == ext) - return "HKDF(" + prf + ")"; - return "HKDF(" + ext + "," + prf + ")"; - } - -void HKDF::clear() - { - m_extractor->clear(); - m_prf->clear(); - } +BOTAN_REGISTER_NAMED_T(KDF, "HKDF", HKDF, HKDF::make); -void HKDF::start_extract(const byte salt[], size_t salt_len) +HKDF* HKDF::make(const Spec& spec) { - m_extractor->set_key(salt, salt_len); - } + if(auto mac = make_a<MessageAuthenticationCode>(spec.arg(0))) + return new HKDF(mac); -void HKDF::extract(const byte input[], size_t input_len) - { - m_extractor->update(input, input_len); - } + if(auto mac = make_a<MessageAuthenticationCode>("HMAC(" + spec.arg(0) + ")")) + return new HKDF(mac); -void HKDF::finish_extract() - { - m_prf->set_key(m_extractor->final()); + return nullptr; } -void HKDF::expand(byte output[], size_t output_len, - const byte info[], size_t info_len) +size_t HKDF::kdf(byte out[], size_t out_len, + const byte secret[], size_t secret_len, + const byte salt[], size_t salt_len) const { - if(output_len > m_prf->output_length() * 255) - throw std::invalid_argument("HKDF requested output too large"); + m_prf->set_key(secret, secret_len); byte counter = 1; + secure_vector<byte> h; + size_t offset = 0; - secure_vector<byte> T; - - while(output_len) + while(offset != out_len && counter != 0) { - m_prf->update(T); - m_prf->update(info, info_len); + m_prf->update(h); + m_prf->update(salt, salt_len); m_prf->update(counter++); - m_prf->final(T); + m_prf->final(h); - const size_t to_write = std::min(T.size(), output_len); - copy_mem(&output[0], &T[0], to_write); - output += to_write; - output_len -= to_write; + const size_t written = std::min(h.size(), out_len - offset); + copy_mem(&out[offset], &h[0], written); + offset += written; } + + return offset; } } diff --git a/src/lib/kdf/hkdf/hkdf.h b/src/lib/kdf/hkdf/hkdf.h index f1ae61453..c5737b1cc 100644 --- a/src/lib/kdf/hkdf/hkdf.h +++ b/src/lib/kdf/hkdf/hkdf.h @@ -1,6 +1,6 @@ /* * HKDF -* (C) 2013 Jack Lloyd +* (C) 2013,2015 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -16,33 +16,24 @@ namespace Botan { /** * HKDF, see @rfc 5869 for details +* This is only the expansion portion of HKDF */ -class BOTAN_DLL HKDF +class BOTAN_DLL HKDF : public KDF { public: - HKDF(MessageAuthenticationCode* extractor, - MessageAuthenticationCode* prf) : - m_extractor(extractor), m_prf(prf) {} + HKDF(MessageAuthenticationCode* prf) : m_prf(prf) {} - HKDF(MessageAuthenticationCode* prf) : - m_extractor(prf), m_prf(m_extractor->clone()) {} + static HKDF* make(const Spec& spec); - void start_extract(const byte salt[], size_t salt_len); - void extract(const byte input[], size_t input_len); - void finish_extract(); + KDF* clone() const { return new HKDF(m_prf->clone()); } - /** - * Only call after extract - * @param output_len must be less than 256*hashlen - */ - void expand(byte output[], size_t output_len, - const byte info[], size_t info_len); + std::string name() const { return "HKDF(" + m_prf->name() + ")"; } - std::string name() const; + size_t kdf(byte out[], size_t out_len, + const byte secret[], size_t secret_len, + const byte salt[], size_t salt_len) const override; - void clear(); private: - std::unique_ptr<MessageAuthenticationCode> m_extractor; std::unique_ptr<MessageAuthenticationCode> m_prf; }; |