diff options
Diffstat (limited to 'src/lib/kdf/sp800_56c')
-rw-r--r-- | src/lib/kdf/sp800_56c/info.txt | 6 | ||||
-rw-r--r-- | src/lib/kdf/sp800_56c/sp800_56c.cpp | 45 | ||||
-rw-r--r-- | src/lib/kdf/sp800_56c/sp800_56c.h | 39 |
3 files changed, 90 insertions, 0 deletions
diff --git a/src/lib/kdf/sp800_56c/info.txt b/src/lib/kdf/sp800_56c/info.txt new file mode 100644 index 000000000..203c05a83 --- /dev/null +++ b/src/lib/kdf/sp800_56c/info.txt @@ -0,0 +1,6 @@ +define SP800_56C 20160211 + +<requires> +sp800_108 +hmac +</requires> diff --git a/src/lib/kdf/sp800_56c/sp800_56c.cpp b/src/lib/kdf/sp800_56c/sp800_56c.cpp new file mode 100644 index 000000000..664d32b30 --- /dev/null +++ b/src/lib/kdf/sp800_56c/sp800_56c.cpp @@ -0,0 +1,45 @@ +/* +* KDF defined in NIST SP 800-56c +* (C) 2016 Kai Michaelis +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/sp800_108.h> +#include <botan/sp800_56c.h> +#include <botan/hmac.h> + +namespace Botan { + +SP800_56C* SP800_56C::make(const Spec& spec) + { + if(auto exp = SP800_108_Feedback::make(spec)) + { + if(auto mac = MessageAuthenticationCode::create(spec.arg(0))) + return new SP800_56C(mac.release(), exp); + + if(auto mac = MessageAuthenticationCode::create("HMAC(" + spec.arg(0) + ")")) + return new SP800_56C(mac.release(), exp); + } + + return nullptr; + } + +size_t SP800_56C::kdf(byte key[], size_t key_len, + const byte secret[], size_t secret_len, + const byte salt[], size_t salt_len) const + { + // Randomness Extraction + secure_vector< byte > k_dk, context; + + m_prf->set_key(salt, salt_len); + m_prf->update(secret, secret_len); + m_prf->final(k_dk); + + // Key Expansion + m_exp->kdf(key, key_len, k_dk.data(), k_dk.size(), context.data(), context.size()); + + return key_len; + } + +} diff --git a/src/lib/kdf/sp800_56c/sp800_56c.h b/src/lib/kdf/sp800_56c/sp800_56c.h new file mode 100644 index 000000000..d1b6f39b5 --- /dev/null +++ b/src/lib/kdf/sp800_56c/sp800_56c.h @@ -0,0 +1,39 @@ +/* +* KDF defined in NIST SP 800-56c +* (C) 2016 Kai Michaelis +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_SP800_56C_H__ +#define BOTAN_SP800_56C_H__ + +#include <botan/kdf.h> +#include <botan/mac.h> + +namespace Botan { + +/** + * NIST SP 800-56C KDF + */ +class BOTAN_DLL SP800_56C : public KDF + { + public: + std::string name() const override { return "SP800-56C(" + m_prf->name() + ")"; } + + KDF* clone() const override { return new SP800_56C(m_prf->clone(), m_exp->clone()); } + + size_t kdf(byte key[], size_t key_len, + const byte secret[], size_t secret_len, + const byte salt[], size_t salt_len) const override; + + SP800_56C(MessageAuthenticationCode* mac, KDF* exp) : m_prf(mac), m_exp(exp) {} + + static SP800_56C* make(const Spec& spec); + private: + std::unique_ptr<MessageAuthenticationCode> m_prf; + std::unique_ptr<KDF> m_exp; + }; +} + +#endif |