aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/kdf
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-01-18 21:26:38 +0000
committerlloyd <[email protected]>2014-01-18 21:26:38 +0000
commitb3bffeff3553f4b609afe634c8c8b56ca0a2384c (patch)
treee81f39a9f86fcefffdf9e7704dd0b7a7c337edb7 /src/lib/kdf
parentef465af87d61c0cfbba17b86a3e1cc48b90ab391 (diff)
More unique_ptr, also cleanup MGF1 usage
Diffstat (limited to 'src/lib/kdf')
-rw-r--r--src/lib/kdf/kdf.h14
-rw-r--r--src/lib/kdf/kdf1/kdf1.h6
-rw-r--r--src/lib/kdf/kdf2/kdf2.h5
-rw-r--r--src/lib/kdf/mgf1/info.txt5
-rw-r--r--src/lib/kdf/mgf1/mgf1.cpp56
-rw-r--r--src/lib/kdf/mgf1/mgf1.h36
-rw-r--r--src/lib/kdf/prf_ssl3/prf_ssl3.h1
-rw-r--r--src/lib/kdf/prf_tls/prf_tls.cpp35
-rw-r--r--src/lib/kdf/prf_tls/prf_tls.h10
9 files changed, 22 insertions, 146 deletions
diff --git a/src/lib/kdf/kdf.h b/src/lib/kdf/kdf.h
index 39e7253f9..0e8f77681 100644
--- a/src/lib/kdf/kdf.h
+++ b/src/lib/kdf/kdf.h
@@ -1,5 +1,5 @@
/*
-* KDF/MGF
+* Key Derivation Function interfaces
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
@@ -114,18 +114,6 @@ class BOTAN_DLL KDF
};
/**
-* Mask Generation Function
-*/
-class BOTAN_DLL MGF
- {
- public:
- virtual void mask(const byte in[], size_t in_len,
- byte out[], size_t out_len) const = 0;
-
- virtual ~MGF() {}
- };
-
-/**
* Factory method for KDF (key derivation function)
* @param algo_spec the name of the KDF to create
* @return pointer to newly allocated object of that type
diff --git a/src/lib/kdf/kdf1/kdf1.h b/src/lib/kdf/kdf1/kdf1.h
index 6a14d2995..539aeaa6e 100644
--- a/src/lib/kdf/kdf1/kdf1.h
+++ b/src/lib/kdf/kdf1/kdf1.h
@@ -10,6 +10,7 @@
#include <botan/kdf.h>
#include <botan/hash.h>
+#include <memory>
namespace Botan {
@@ -27,11 +28,8 @@ class BOTAN_DLL KDF1 : public KDF
KDF* clone() const { return new KDF1(hash->clone()); }
KDF1(HashFunction* h) : hash(h) {}
- KDF1(const KDF1& other) : KDF(), hash(other.hash->clone()) {}
-
- ~KDF1() { delete hash; }
private:
- HashFunction* hash;
+ std::unique_ptr<HashFunction> hash;
};
}
diff --git a/src/lib/kdf/kdf2/kdf2.h b/src/lib/kdf/kdf2/kdf2.h
index e33939df9..60bbf5db1 100644
--- a/src/lib/kdf/kdf2/kdf2.h
+++ b/src/lib/kdf/kdf2/kdf2.h
@@ -10,6 +10,7 @@
#include <botan/kdf.h>
#include <botan/hash.h>
+#include <memory>
namespace Botan {
@@ -26,10 +27,8 @@ class BOTAN_DLL KDF2 : public KDF
KDF* clone() const { return new KDF2(hash->clone()); }
KDF2(HashFunction* h) : hash(h) {}
- KDF2(const KDF2& other) : KDF(), hash(other.hash->clone()) {}
- ~KDF2() { delete hash; }
private:
- HashFunction* hash;
+ std::unique_ptr<HashFunction> hash;
};
}
diff --git a/src/lib/kdf/mgf1/info.txt b/src/lib/kdf/mgf1/info.txt
deleted file mode 100644
index c6254b8a0..000000000
--- a/src/lib/kdf/mgf1/info.txt
+++ /dev/null
@@ -1,5 +0,0 @@
-define MGF1 20131128
-
-<requires>
-hash
-</requires>
diff --git a/src/lib/kdf/mgf1/mgf1.cpp b/src/lib/kdf/mgf1/mgf1.cpp
deleted file mode 100644
index e0433a02f..000000000
--- a/src/lib/kdf/mgf1/mgf1.cpp
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
-* MGF1
-* (C) 1999-2007 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#include <botan/mgf1.h>
-#include <botan/exceptn.h>
-#include <botan/internal/xor_buf.h>
-#include <algorithm>
-#include <memory>
-
-namespace Botan {
-
-/*
-* MGF1 Mask Generation Function
-*/
-void MGF1::mask(const byte in[], size_t in_len, byte out[],
- size_t out_len) const
- {
- u32bit counter = 0;
-
- while(out_len)
- {
- hash->update(in, in_len);
- hash->update_be(counter);
- secure_vector<byte> buffer = hash->final();
-
- size_t xored = std::min<size_t>(buffer.size(), out_len);
- xor_buf(out, &buffer[0], xored);
- out += xored;
- out_len -= xored;
-
- ++counter;
- }
- }
-
-/*
-* MGF1 Constructor
-*/
-MGF1::MGF1(HashFunction* h) : hash(h)
- {
- if(!hash)
- throw Invalid_Argument("MGF1 given null hash object");
- }
-
-/*
-* MGF1 Destructor
-*/
-MGF1::~MGF1()
- {
- delete hash;
- }
-
-}
diff --git a/src/lib/kdf/mgf1/mgf1.h b/src/lib/kdf/mgf1/mgf1.h
deleted file mode 100644
index 95a2a2bc5..000000000
--- a/src/lib/kdf/mgf1/mgf1.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
-* MGF1
-* (C) 1999-2007 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#ifndef BOTAN_MGF1_H__
-#define BOTAN_MGF1_H__
-
-#include <botan/kdf.h>
-#include <botan/hash.h>
-
-namespace Botan {
-
-/**
-* MGF1 from PKCS #1 v2.0
-*/
-class BOTAN_DLL MGF1 : public MGF
- {
- public:
- void mask(const byte[], size_t, byte[], size_t) const;
-
- /**
- MGF1 constructor: takes ownership of hash
- */
- MGF1(HashFunction* hash);
-
- ~MGF1();
- private:
- HashFunction* hash;
- };
-
-}
-
-#endif
diff --git a/src/lib/kdf/prf_ssl3/prf_ssl3.h b/src/lib/kdf/prf_ssl3/prf_ssl3.h
index bae8badb8..9b9ecb578 100644
--- a/src/lib/kdf/prf_ssl3/prf_ssl3.h
+++ b/src/lib/kdf/prf_ssl3/prf_ssl3.h
@@ -9,6 +9,7 @@
#define BOTAN_SSLV3_PRF_H__
#include <botan/kdf.h>
+#include <memory>
namespace Botan {
diff --git a/src/lib/kdf/prf_tls/prf_tls.cpp b/src/lib/kdf/prf_tls/prf_tls.cpp
index 006b418c9..6437932a8 100644
--- a/src/lib/kdf/prf_tls/prf_tls.cpp
+++ b/src/lib/kdf/prf_tls/prf_tls.cpp
@@ -19,13 +19,13 @@ namespace {
* TLS PRF P_hash function
*/
void P_hash(secure_vector<byte>& output,
- MessageAuthenticationCode* mac,
+ MessageAuthenticationCode& mac,
const byte secret[], size_t secret_len,
const byte seed[], size_t seed_len)
{
try
{
- mac->set_key(secret, secret_len);
+ mac.set_key(secret, secret_len);
}
catch(Invalid_Key_Length)
{
@@ -41,13 +41,13 @@ void P_hash(secure_vector<byte>& output,
while(offset != output.size())
{
const size_t this_block_len =
- std::min<size_t>(mac->output_length(), output.size() - offset);
+ std::min<size_t>(mac.output_length(), output.size() - offset);
- A = mac->process(A);
+ A = mac.process(A);
- mac->update(A);
- mac->update(seed, seed_len);
- secure_vector<byte> block = mac->final();
+ mac.update(A);
+ mac.update(seed, seed_len);
+ secure_vector<byte> block = mac.final();
xor_buf(&output[offset], &block[0], this_block_len);
offset += this_block_len;
@@ -61,14 +61,8 @@ void P_hash(secure_vector<byte>& output,
*/
TLS_PRF::TLS_PRF()
{
- hmac_md5 = new HMAC(new MD5);
- hmac_sha1 = new HMAC(new SHA_160);
- }
-
-TLS_PRF::~TLS_PRF()
- {
- delete hmac_md5;
- delete hmac_sha1;
+ hmac_md5.reset(new HMAC(new MD5));
+ hmac_sha1.reset(new HMAC(new SHA_160));
}
/*
@@ -85,8 +79,8 @@ secure_vector<byte> TLS_PRF::derive(size_t key_len,
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);
+ P_hash(output, *hmac_md5, S1, S1_len, seed, seed_len);
+ P_hash(output, *hmac_sha1, S2, S2_len, seed, seed_len);
return output;
}
@@ -98,18 +92,13 @@ TLS_12_PRF::TLS_12_PRF(MessageAuthenticationCode* mac) : hmac(mac)
{
}
-TLS_12_PRF::~TLS_12_PRF()
- {
- delete hmac;
- }
-
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, hmac, secret, secret_len, seed, seed_len);
+ P_hash(output, *hmac, secret, secret_len, seed, seed_len);
return output;
}
diff --git a/src/lib/kdf/prf_tls/prf_tls.h b/src/lib/kdf/prf_tls/prf_tls.h
index fce11eae0..654b7abdb 100644
--- a/src/lib/kdf/prf_tls/prf_tls.h
+++ b/src/lib/kdf/prf_tls/prf_tls.h
@@ -10,7 +10,7 @@
#include <botan/kdf.h>
#include <botan/mac.h>
-#include <botan/hash.h>
+#include <memory>
namespace Botan {
@@ -28,10 +28,9 @@ class BOTAN_DLL TLS_PRF : public KDF
KDF* clone() const { return new TLS_PRF; }
TLS_PRF();
- ~TLS_PRF();
private:
- MessageAuthenticationCode* hmac_md5;
- MessageAuthenticationCode* hmac_sha1;
+ std::unique_ptr<MessageAuthenticationCode> hmac_md5;
+ std::unique_ptr<MessageAuthenticationCode> hmac_sha1;
};
/**
@@ -48,9 +47,8 @@ class BOTAN_DLL TLS_12_PRF : public KDF
KDF* clone() const { return new TLS_12_PRF(hmac->clone()); }
TLS_12_PRF(MessageAuthenticationCode* hmac);
- ~TLS_12_PRF();
private:
- MessageAuthenticationCode* hmac;
+ std::unique_ptr<MessageAuthenticationCode> hmac;
};
}