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/prf_x942 | |
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/prf_x942')
-rw-r--r-- | src/lib/kdf/prf_x942/prf_x942.cpp | 37 | ||||
-rw-r--r-- | src/lib/kdf/prf_x942/prf_x942.h | 12 |
2 files changed, 25 insertions, 24 deletions
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; }; } |