diff options
Diffstat (limited to 'src/lib/kdf')
-rw-r--r-- | src/lib/kdf/kdf.h | 47 | ||||
-rw-r--r-- | src/lib/kdf/kdf1/kdf1.cpp | 24 | ||||
-rw-r--r-- | src/lib/kdf/kdf1/kdf1.h | 15 | ||||
-rw-r--r-- | src/lib/kdf/kdf2/kdf2.cpp | 35 | ||||
-rw-r--r-- | src/lib/kdf/kdf2/kdf2.h | 14 | ||||
-rw-r--r-- | src/lib/kdf/prf_tls/prf_tls.cpp | 78 | ||||
-rw-r--r-- | src/lib/kdf/prf_tls/prf_tls.h | 24 | ||||
-rw-r--r-- | src/lib/kdf/prf_x942/prf_x942.cpp | 37 | ||||
-rw-r--r-- | src/lib/kdf/prf_x942/prf_x942.h | 12 |
9 files changed, 138 insertions, 148 deletions
diff --git a/src/lib/kdf/kdf.h b/src/lib/kdf/kdf.h index 9d8ca57fc..dc23a82bd 100644 --- a/src/lib/kdf/kdf.h +++ b/src/lib/kdf/kdf.h @@ -25,6 +25,30 @@ class BOTAN_DLL KDF virtual std::string name() const = 0; + virtual size_t kdf(byte key[], size_t key_len, + const byte secret[], size_t secret_len, + const byte salt[], size_t salt_len) const = 0; + + + /** + * Derive a key + * @param key_len the desired output length in bytes + * @param secret the secret input + * @param secret_len size of secret in bytes + * @param salt a diversifier + * @param salt_len size of salt in bytes + */ + secure_vector<byte> derive_key(size_t key_len, + const byte secret[], + size_t secret_len, + const byte salt[], + size_t salt_len) const + { + secure_vector<byte> key(key_len); + key.resize(kdf(&key[0], key.size(), secret, secret_len, salt, salt_len)); + return key; + } + /** * Derive a key * @param key_len the desired output length in bytes @@ -90,31 +114,10 @@ class BOTAN_DLL KDF salt.length()); } - /** - * Derive a key - * @param key_len the desired output length in bytes - * @param secret the secret input - * @param secret_len size of secret in bytes - * @param salt a diversifier - * @param salt_len size of salt in bytes - */ - secure_vector<byte> derive_key(size_t key_len, - const byte secret[], - size_t secret_len, - const byte salt[], - size_t salt_len) const - { - return derive(key_len, secret, secret_len, salt, salt_len); - } - virtual KDF* clone() const = 0; typedef SCAN_Name Spec; - private: - virtual secure_vector<byte> - derive(size_t key_len, - const byte secret[], size_t secret_len, - const byte salt[], size_t salt_len) const = 0; + }; /** diff --git a/src/lib/kdf/kdf1/kdf1.cpp b/src/lib/kdf/kdf1/kdf1.cpp index df84a1a00..c2a74027b 100644 --- a/src/lib/kdf/kdf1/kdf1.cpp +++ b/src/lib/kdf/kdf1/kdf1.cpp @@ -12,16 +12,22 @@ namespace Botan { BOTAN_REGISTER_KDF_1HASH(KDF1, "KDF1"); -/* -* KDF1 Key Derivation Mechanism -*/ -secure_vector<byte> KDF1::derive(size_t, - const byte secret[], size_t secret_len, - const byte P[], size_t P_len) const +size_t KDF1::kdf(byte key[], size_t key_len, + const byte secret[], size_t secret_len, + const byte salt[], size_t salt_len) const { - hash->update(secret, secret_len); - hash->update(P, P_len); - return hash->final(); + m_hash->update(secret, secret_len); + m_hash->update(salt, salt_len); + + if(key_len < m_hash->output_length()) + { + secure_vector<byte> v = m_hash->final(); + copy_mem(key, &v[0], key_len); + return key_len; + } + + m_hash->final(key); + return m_hash->output_length(); } } diff --git a/src/lib/kdf/kdf1/kdf1.h b/src/lib/kdf/kdf1/kdf1.h index 9f617878b..a22d19d97 100644 --- a/src/lib/kdf/kdf1/kdf1.h +++ b/src/lib/kdf/kdf1/kdf1.h @@ -19,16 +19,17 @@ namespace Botan { class BOTAN_DLL KDF1 : public KDF { public: - secure_vector<byte> derive(size_t, - const byte secret[], size_t secret_len, - const byte P[], size_t P_len) const; + std::string name() const override { return "KDF1(" + m_hash->name() + ")"; } - std::string name() const { return "KDF1(" + hash->name() + ")"; } - KDF* clone() const { return new KDF1(hash->clone()); } + KDF* clone() const override { return new KDF1(m_hash->clone()); } - KDF1(HashFunction* h) : hash(h) {} + 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; + + KDF1(HashFunction* h) : m_hash(h) {} private: - std::unique_ptr<HashFunction> hash; + std::unique_ptr<HashFunction> m_hash; }; } diff --git a/src/lib/kdf/kdf2/kdf2.cpp b/src/lib/kdf/kdf2/kdf2.cpp index c7b355580..f1a702887 100644 --- a/src/lib/kdf/kdf2/kdf2.cpp +++ b/src/lib/kdf/kdf2/kdf2.cpp @@ -12,32 +12,27 @@ namespace Botan { BOTAN_REGISTER_KDF_1HASH(KDF2, "KDF2"); -/* -* KDF2 Key Derivation Mechanism -*/ -secure_vector<byte> KDF2::derive(size_t out_len, - const byte secret[], size_t secret_len, - const byte P[], size_t P_len) const +size_t KDF2::kdf(byte key[], size_t key_len, + const byte secret[], size_t secret_len, + const byte salt[], size_t salt_len) const { - secure_vector<byte> output; u32bit counter = 1; + secure_vector<byte> h; - while(out_len && counter) + size_t offset = 0; + while(offset != key_len && counter != 0) { - hash->update(secret, secret_len); - hash->update_be(counter); - hash->update(P, P_len); - - secure_vector<byte> hash_result = hash->final(); - - size_t added = std::min(hash_result.size(), out_len); - output += std::make_pair(&hash_result[0], added); - out_len -= added; - - ++counter; + m_hash->update(secret, secret_len); + m_hash->update_be(counter++); + m_hash->update(salt, salt_len); + m_hash->final(h); + + const size_t added = std::min(h.size(), key_len - offset); + copy_mem(&key[offset], &h[0], added); + offset += added; } - return output; + return offset; } } diff --git a/src/lib/kdf/kdf2/kdf2.h b/src/lib/kdf/kdf2/kdf2.h index c574336b6..e8a8be1fa 100644 --- a/src/lib/kdf/kdf2/kdf2.h +++ b/src/lib/kdf/kdf2/kdf2.h @@ -19,15 +19,17 @@ namespace Botan { class BOTAN_DLL KDF2 : public KDF { public: - secure_vector<byte> derive(size_t, const byte[], size_t, - const byte[], size_t) const; + std::string name() const override { return "KDF2(" + m_hash->name() + ")"; } - std::string name() const { return "KDF2(" + hash->name() + ")"; } - KDF* clone() const { return new KDF2(hash->clone()); } + KDF* clone() const override { return new KDF2(m_hash->clone()); } - KDF2(HashFunction* h) : hash(h) {} + 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; + + KDF2(HashFunction* h) : m_hash(h) {} private: - std::unique_ptr<HashFunction> hash; + std::unique_ptr<HashFunction> m_hash; }; } diff --git a/src/lib/kdf/prf_tls/prf_tls.cpp b/src/lib/kdf/prf_tls/prf_tls.cpp index f1061fd10..9161dc71e 100644 --- a/src/lib/kdf/prf_tls/prf_tls.cpp +++ b/src/lib/kdf/prf_tls/prf_tls.cpp @@ -23,15 +23,21 @@ TLS_12_PRF* TLS_12_PRF::make(const Spec& spec) BOTAN_REGISTER_NAMED_T(KDF, "TLS-12-PRF", TLS_12_PRF, TLS_12_PRF::make); BOTAN_REGISTER_KDF_NOARGS(TLS_PRF, "TLS-PRF"); +TLS_PRF::TLS_PRF() + { + m_hmac_md5.reset(make_a<MessageAuthenticationCode>("HMAC(MD5)")); + m_hmac_sha1.reset(make_a<MessageAuthenticationCode>("HMAC(SHA-1)")); + } + namespace { /* * TLS PRF P_hash function */ -void P_hash(secure_vector<byte>& output, +void P_hash(byte out[], size_t out_len, MessageAuthenticationCode& mac, const byte secret[], size_t secret_len, - const byte seed[], size_t seed_len) + const byte salt[], size_t salt_len) { try { @@ -44,73 +50,47 @@ void P_hash(secure_vector<byte>& output, " bytes is too long for the PRF"); } - secure_vector<byte> A(seed, seed + seed_len); + secure_vector<byte> A(salt, salt + salt_len); + secure_vector<byte> h; size_t offset = 0; - while(offset != output.size()) + while(offset != out_len) { - const size_t this_block_len = - std::min<size_t>(mac.output_length(), output.size() - offset); - A = mac.process(A); mac.update(A); - mac.update(seed, seed_len); - secure_vector<byte> block = mac.final(); + mac.update(salt, salt_len); + mac.final(h); - xor_buf(&output[offset], &block[0], this_block_len); - offset += this_block_len; + const size_t writing = std::min(h.size(), out_len - offset); + xor_buf(&out[offset], &h[0], writing); + offset += writing; } } } -/* -* TLS PRF Constructor and Destructor -*/ -TLS_PRF::TLS_PRF() - { - hmac_md5.reset(make_a<MessageAuthenticationCode>("HMAC(MD5)")); - hmac_sha1.reset(make_a<MessageAuthenticationCode>("HMAC(SHA-1)")); - } - -/* -* TLS PRF -*/ -secure_vector<byte> TLS_PRF::derive(size_t key_len, - const byte secret[], size_t secret_len, - const byte seed[], size_t seed_len) const +size_t TLS_PRF::kdf(byte key[], size_t key_len, + const byte secret[], size_t secret_len, + const byte salt[], size_t salt_len) const { - secure_vector<byte> output(key_len); - - size_t S1_len = (secret_len + 1) / 2, - S2_len = (secret_len + 1) / 2; + const size_t S1_len = (secret_len + 1) / 2, + S2_len = (secret_len + 1) / 2; const byte* S1 = secret; const byte* S2 = secret + (secret_len - S2_len); - P_hash(output, *hmac_md5, S1, S1_len, seed, seed_len); - P_hash(output, *hmac_sha1, S2, S2_len, seed, seed_len); - - return output; + P_hash(key, key_len, *m_hmac_md5, S1, S1_len, salt, salt_len); + P_hash(key, key_len, *m_hmac_sha1, S2, S2_len, salt, salt_len); + return key_len; } -/* -* TLS v1.2 PRF Constructor and Destructor -*/ -TLS_12_PRF::TLS_12_PRF(MessageAuthenticationCode* mac) : m_mac(mac) +size_t TLS_12_PRF::kdf(byte key[], size_t key_len, + const byte secret[], size_t secret_len, + const byte salt[], size_t salt_len) const { - } - -secure_vector<byte> TLS_12_PRF::derive(size_t key_len, - const byte secret[], size_t secret_len, - const byte seed[], size_t seed_len) const - { - secure_vector<byte> output(key_len); - - P_hash(output, *m_mac, secret, secret_len, seed, seed_len); - - return output; + P_hash(key, key_len, *m_mac, secret, secret_len, salt, salt_len); + return key_len; } } diff --git a/src/lib/kdf/prf_tls/prf_tls.h b/src/lib/kdf/prf_tls/prf_tls.h index c3adc6caf..e2289a6e8 100644 --- a/src/lib/kdf/prf_tls/prf_tls.h +++ b/src/lib/kdf/prf_tls/prf_tls.h @@ -19,17 +19,18 @@ namespace Botan { class BOTAN_DLL TLS_PRF : public KDF { public: - secure_vector<byte> derive(size_t key_len, - const byte secret[], size_t secret_len, - const byte seed[], size_t seed_len) const; - std::string name() const { return "TLS-PRF"; } + KDF* clone() const { return new TLS_PRF; } + 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; + TLS_PRF(); private: - std::unique_ptr<MessageAuthenticationCode> hmac_md5; - std::unique_ptr<MessageAuthenticationCode> hmac_sha1; + std::unique_ptr<MessageAuthenticationCode> m_hmac_md5; + std::unique_ptr<MessageAuthenticationCode> m_hmac_sha1; }; /** @@ -38,14 +39,15 @@ class BOTAN_DLL TLS_PRF : public KDF class BOTAN_DLL TLS_12_PRF : public KDF { public: - secure_vector<byte> derive(size_t key_len, - const byte secret[], size_t secret_len, - const byte seed[], size_t seed_len) const; - std::string name() const { return "TLS-12-PRF(" + m_mac->name() + ")"; } + KDF* clone() const { return new TLS_12_PRF(m_mac->clone()); } - TLS_12_PRF(MessageAuthenticationCode* mac); + 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; + + TLS_12_PRF(MessageAuthenticationCode* mac) : m_mac(mac) {} static TLS_12_PRF* make(const Spec& spec); private: diff --git a/src/lib/kdf/prf_x942/prf_x942.cpp b/src/lib/kdf/prf_x942/prf_x942.cpp index 30bf737a9..5ca0f01ff 100644 --- a/src/lib/kdf/prf_x942/prf_x942.cpp +++ b/src/lib/kdf/prf_x942/prf_x942.cpp @@ -9,7 +9,7 @@ #include <botan/prf_x942.h> #include <botan/der_enc.h> #include <botan/oids.h> -#include <botan/sha160.h> +#include <botan/hash.h> #include <botan/loadstor.h> #include <algorithm> @@ -31,24 +31,22 @@ std::vector<byte> encode_x942_int(u32bit n) } -/* -* X9.42 PRF -*/ -secure_vector<byte> X942_PRF::derive(size_t key_len, - const byte secret[], size_t secret_len, - const byte salt[], size_t salt_len) const +size_t X942_PRF::kdf(byte key[], size_t key_len, + const byte secret[], size_t secret_len, + const byte salt[], size_t salt_len) const { - SHA_160 hash; - const OID kek_algo(key_wrap_oid); + std::unique_ptr<HashFunction> hash(make_a<HashFunction>("SHA-160")); + const OID kek_algo(m_key_wrap_oid); - secure_vector<byte> key; + secure_vector<byte> h; + size_t offset = 0; u32bit counter = 1; - while(key.size() != key_len && counter) + while(offset != key_len && counter) { - hash.update(secret, secret_len); + hash->update(secret, secret_len); - hash.update( + hash->update( DER_Encoder().start_cons(SEQUENCE) .start_cons(SEQUENCE) @@ -70,14 +68,15 @@ secure_vector<byte> X942_PRF::derive(size_t key_len, .end_cons().get_contents() ); - secure_vector<byte> digest = hash.final(); - const size_t needed = std::min(digest.size(), key_len - key.size()); - key += std::make_pair(&digest[0], needed); + hash->final(h); + const size_t copied = std::min(h.size(), key_len - offset); + copy_mem(&key[offset], &h[0], copied); + offset += copied; ++counter; } - return key; + return offset; } /* @@ -86,9 +85,9 @@ secure_vector<byte> X942_PRF::derive(size_t key_len, X942_PRF::X942_PRF(const std::string& oid) { if(OIDS::have_oid(oid)) - key_wrap_oid = OIDS::lookup(oid).as_string(); + m_key_wrap_oid = OIDS::lookup(oid).as_string(); else - key_wrap_oid = oid; + m_key_wrap_oid = oid; } } diff --git a/src/lib/kdf/prf_x942/prf_x942.h b/src/lib/kdf/prf_x942/prf_x942.h index d2678c127..242a83150 100644 --- a/src/lib/kdf/prf_x942/prf_x942.h +++ b/src/lib/kdf/prf_x942/prf_x942.h @@ -18,15 +18,17 @@ namespace Botan { class BOTAN_DLL X942_PRF : public KDF { public: - secure_vector<byte> derive(size_t, const byte[], size_t, - const byte[], size_t) const; + std::string name() const { return "X942_PRF(" + m_key_wrap_oid + ")"; } - std::string name() const { return "X942_PRF(" + key_wrap_oid + ")"; } - KDF* clone() const { return new X942_PRF(key_wrap_oid); } + KDF* clone() const { return new X942_PRF(m_key_wrap_oid); } + + 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; X942_PRF(const std::string& oid); private: - std::string key_wrap_oid; + std::string m_key_wrap_oid; }; } |