diff options
author | lloyd <[email protected]> | 2015-02-18 04:21:21 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2015-02-18 04:21:21 +0000 |
commit | 88285f51ba4fd5bc1a1cc06b0760b3926046f29b (patch) | |
tree | 7443b2b266b8445433b9c63704b7a09e216282f2 /src/lib/kdf/kdf2 | |
parent | aced9e88d970546c6324e768ce11b0a483bd3bd0 (diff) |
Modify interfaces of KDF and PBKDF to write output to an array, with
higher level functions on interface handling returning a vector.
Diffstat (limited to 'src/lib/kdf/kdf2')
-rw-r--r-- | src/lib/kdf/kdf2/kdf2.cpp | 35 | ||||
-rw-r--r-- | src/lib/kdf/kdf2/kdf2.h | 14 |
2 files changed, 23 insertions, 26 deletions
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; }; } |