aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-09-28 18:03:20 +0000
committerlloyd <[email protected]>2008-09-28 18:03:20 +0000
commit47ffeed7f8270855596c4f1d7b2d405172d78e8c (patch)
tree371535473f023f310a9c9c0281d5f62b3faedad0
parent389dd2cdf55a57960581f686a8a766475a1f5d38 (diff)
Modularize KDFs, PBKDFs, and PRFs
-rw-r--r--include/kdf.h81
-rw-r--r--modules/kdf/kdf2/kdf2.h29
-rw-r--r--modules/kdf/pbkdf2/pbkdf2.cpp (renamed from src/pkcs5.cpp)55
-rw-r--r--modules/kdf/pbkdf2/pbkdf2.h (renamed from include/pkcs5.h)21
-rw-r--r--modules/kdf/pgps2k/pgp_s2k.cpp (renamed from src/pgp_s2k.cpp)0
-rw-r--r--modules/kdf/pgps2k/pgp_s2k.h (renamed from include/pgp_s2k.h)0
-rw-r--r--modules/kdf/sslv3/prf_ssl3.cpp (renamed from src/ssl3_prf.cpp)4
-rw-r--r--modules/kdf/tlsv1/prf_tls.cpp (renamed from src/tls_prf.cpp)2
-rw-r--r--modules/kdf/x942/prf_x942.cpp (renamed from src/prf_x942.cpp)2
-rw-r--r--src/def_alg.cpp26
-rw-r--r--src/get_enc.cpp31
-rw-r--r--src/kdf.cpp65
12 files changed, 94 insertions, 222 deletions
diff --git a/include/kdf.h b/include/kdf.h
deleted file mode 100644
index ad7a11dbe..000000000
--- a/include/kdf.h
+++ /dev/null
@@ -1,81 +0,0 @@
-/*************************************************
-* KDF Header File *
-* (C) 1999-2007 Jack Lloyd *
-*************************************************/
-
-#ifndef BOTAN_KDF_H__
-#define BOTAN_KDF_H__
-
-#include <botan/pk_util.h>
-
-namespace Botan {
-
-/*************************************************
-* KDF1 *
-*************************************************/
-class BOTAN_DLL KDF1 : public KDF
- {
- public:
- SecureVector<byte> derive(u32bit, const byte[], u32bit,
- const byte[], u32bit) const;
-
- KDF1(const std::string&);
- private:
- const std::string hash_name;
- };
-
-/*************************************************
-* KDF2 *
-*************************************************/
-class BOTAN_DLL KDF2 : public KDF
- {
- public:
- SecureVector<byte> derive(u32bit, const byte[], u32bit,
- const byte[], u32bit) const;
-
- KDF2(const std::string&);
- private:
- const std::string hash_name;
- };
-
-/*************************************************
-* X9.42 PRF *
-*************************************************/
-class BOTAN_DLL X942_PRF : public KDF
- {
- public:
- SecureVector<byte> derive(u32bit, const byte[], u32bit,
- const byte[], u32bit) const;
-
- X942_PRF(const std::string&);
- private:
- std::string key_wrap_oid;
- };
-
-/*************************************************
-* SSL3 PRF *
-*************************************************/
-class BOTAN_DLL SSL3_PRF : public KDF
- {
- public:
- SecureVector<byte> derive(u32bit, const byte[], u32bit,
- const byte[], u32bit) const;
- };
-
-/*************************************************
-* TLS PRF *
-*************************************************/
-class BOTAN_DLL TLS_PRF : public KDF
- {
- public:
- SecureVector<byte> derive(u32bit, const byte[], u32bit,
- const byte[], u32bit) const;
- private:
- SecureVector<byte> P_hash(const std::string&, u32bit,
- const byte[], u32bit,
- const byte[], u32bit) const;
- };
-
-}
-
-#endif
diff --git a/modules/kdf/kdf2/kdf2.h b/modules/kdf/kdf2/kdf2.h
new file mode 100644
index 000000000..003f0fc45
--- /dev/null
+++ b/modules/kdf/kdf2/kdf2.h
@@ -0,0 +1,29 @@
+/*************************************************
+* KDF2 Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_KDF2_H__
+#define BOTAN_KDF2_H__
+
+#include <botan/pk_util.h>
+
+namespace Botan {
+
+/*************************************************
+* KDF2 *
+*************************************************/
+class BOTAN_DLL KDF2 : public KDF
+ {
+ public:
+ SecureVector<byte> derive(u32bit, const byte[], u32bit,
+ const byte[], u32bit) const;
+
+ KDF2(const std::string&);
+ private:
+ const std::string hash_name;
+ };
+
+}
+
+#endif
diff --git a/src/pkcs5.cpp b/modules/kdf/pbkdf2/pbkdf2.cpp
index 8a6e7b5a8..09d51d2a6 100644
--- a/src/pkcs5.cpp
+++ b/modules/kdf/pbkdf2/pbkdf2.cpp
@@ -1,64 +1,17 @@
/*************************************************
-* PKCS #5 Source File *
+* PBKDF2 Source File *
* (C) 1999-2007 Jack Lloyd *
*************************************************/
-#include <botan/pkcs5.h>
-#include <botan/lookup.h>
+#include <botan/pbkdf2.h>
#include <botan/loadstor.h>
-#include <botan/xor_buf.h>
#include <botan/hmac.h>
-#include <algorithm>
-#include <memory>
+#include <botan/lookup.h>
+#include <botan/xor_buf.h>
namespace Botan {
/*************************************************
-* Return a PKCS#5 PBKDF1 derived key *
-*************************************************/
-OctetString PKCS5_PBKDF1::derive(u32bit key_len,
- const std::string& passphrase,
- const byte salt[], u32bit salt_size,
- u32bit iterations) const
- {
- if(iterations == 0)
- throw Invalid_Argument("PKCS#5 PBKDF1: Invalid iteration count");
-
- std::auto_ptr<HashFunction> hash(get_hash(hash_name));
- if(key_len > hash->OUTPUT_LENGTH)
- throw Exception("PKCS#5 PBKDF1: Requested output length too long");
-
- hash->update(passphrase);
- hash->update(salt, salt_size);
- SecureVector<byte> key = hash->final();
-
- for(u32bit j = 1; j != iterations; ++j)
- {
- hash->update(key);
- hash->final(key);
- }
-
- return OctetString(key, std::min(key_len, key.size()));
- }
-
-/*************************************************
-* Return the name of this type *
-*************************************************/
-std::string PKCS5_PBKDF1::name() const
- {
- return "PBKDF1(" + hash_name + ")";
- }
-
-/*************************************************
-* PKCS5_PBKDF1 Constructor *
-*************************************************/
-PKCS5_PBKDF1::PKCS5_PBKDF1(const std::string& h_name) : hash_name(h_name)
- {
- if(!have_hash(hash_name))
- throw Algorithm_Not_Found(hash_name);
- }
-
-/*************************************************
* Return a PKCS#5 PBKDF2 derived key *
*************************************************/
OctetString PKCS5_PBKDF2::derive(u32bit key_len,
diff --git a/include/pkcs5.h b/modules/kdf/pbkdf2/pbkdf2.h
index 22b16b7c5..dc6e41b9e 100644
--- a/include/pkcs5.h
+++ b/modules/kdf/pbkdf2/pbkdf2.h
@@ -1,31 +1,16 @@
/*************************************************
-* PKCS #5 Header File *
+* PBKDF2 Header File *
* (C) 1999-2007 Jack Lloyd *
*************************************************/
-#ifndef BOTAN_PKCS5_H__
-#define BOTAN_PKCS5_H__
+#ifndef BOTAN_PBKDF2_H__
+#define BOTAN_PBKDF2_H__
#include <botan/s2k.h>
namespace Botan {
/*************************************************
-* PKCS #5 PBKDF1 *
-*************************************************/
-class BOTAN_DLL PKCS5_PBKDF1 : public S2K
- {
- public:
- std::string name() const;
- S2K* clone() const { return new PKCS5_PBKDF1(hash_name); }
- PKCS5_PBKDF1(const std::string&);
- private:
- OctetString derive(u32bit, const std::string&,
- const byte[], u32bit, u32bit) const;
- const std::string hash_name;
- };
-
-/*************************************************
* PKCS #5 PBKDF2 *
*************************************************/
class BOTAN_DLL PKCS5_PBKDF2 : public S2K
diff --git a/src/pgp_s2k.cpp b/modules/kdf/pgps2k/pgp_s2k.cpp
index 66a243e45..66a243e45 100644
--- a/src/pgp_s2k.cpp
+++ b/modules/kdf/pgps2k/pgp_s2k.cpp
diff --git a/include/pgp_s2k.h b/modules/kdf/pgps2k/pgp_s2k.h
index cd263a735..cd263a735 100644
--- a/include/pgp_s2k.h
+++ b/modules/kdf/pgps2k/pgp_s2k.h
diff --git a/src/ssl3_prf.cpp b/modules/kdf/sslv3/prf_ssl3.cpp
index a86ed8ff7..b241bf60f 100644
--- a/src/ssl3_prf.cpp
+++ b/modules/kdf/sslv3/prf_ssl3.cpp
@@ -1,9 +1,9 @@
/*************************************************
-* SSL3 PRF Source File *
+* SSLv3 PRF Source File *
* (C) 2004-2006 Jack Lloyd *
*************************************************/
-#include <botan/kdf.h>
+#include <botan/prf_ssl3.h>
#include <botan/lookup.h>
#include <memory>
diff --git a/src/tls_prf.cpp b/modules/kdf/tlsv1/prf_tls.cpp
index 2222e3baa..e035ac85e 100644
--- a/src/tls_prf.cpp
+++ b/modules/kdf/tlsv1/prf_tls.cpp
@@ -3,7 +3,7 @@
* (C) 2004-2006 Jack Lloyd *
*************************************************/
-#include <botan/kdf.h>
+#include <botan/prf_tls.h>
#include <botan/lookup.h>
#include <botan/xor_buf.h>
#include <botan/hmac.h>
diff --git a/src/prf_x942.cpp b/modules/kdf/x942/prf_x942.cpp
index 4cd53fa27..3a7298771 100644
--- a/src/prf_x942.cpp
+++ b/modules/kdf/x942/prf_x942.cpp
@@ -3,7 +3,7 @@
* (C) 1999-2007 Jack Lloyd *
*************************************************/
-#include <botan/kdf.h>
+#include <botan/prf_x942.h>
#include <botan/der_enc.h>
#include <botan/oids.h>
#include <botan/lookup.h>
diff --git a/src/def_alg.cpp b/src/def_alg.cpp
index 6842d36d6..ad7ddae70 100644
--- a/src/def_alg.cpp
+++ b/src/def_alg.cpp
@@ -11,8 +11,6 @@
#include <botan/hmac.h>
#include <botan/par_hash.h>
#include <botan/mode_pad.h>
-#include <botan/pgp_s2k.h>
-#include <botan/pkcs5.h>
#ifdef BOTAN_HAS_AES
#include <botan/aes.h>
@@ -185,11 +183,23 @@
#endif
#ifdef BOTAN_HAS_SSL3_MAC
-#include <botan/ssl3_mac.h>
+ #include <botan/ssl3_mac.h>
#endif
#ifdef BOTAN_HAS_ANSI_X919_MAC
-#include <botan/x919_mac.h>
+ #include <botan/x919_mac.h>
+#endif
+
+#ifdef BOTAN_HAS_PBKDF1
+ #include <botan/pbkdf1.h>
+#endif
+
+#ifdef BOTAN_HAS_PBKDF2
+ #include <botan/pbkdf2.h>
+#endif
+
+#ifdef BOTAN_HAS_PGPS2K
+ #include <botan/pgp_s2k.h>
#endif
namespace Botan {
@@ -508,9 +518,17 @@ S2K* Default_Engine::find_s2k(const std::string& algo_spec) const
const std::string algo_name = global_state().deref_alias(name[0]);
+#ifdef BOTAN_HAS_PBKDF1
HANDLE_TYPE_ONE_STRING("PBKDF1", PKCS5_PBKDF1);
+#endif
+
+#ifdef BOTAN_HAS_PBKDF2
HANDLE_TYPE_ONE_STRING("PBKDF2", PKCS5_PBKDF2);
+#endif
+
+#ifdef BOTAN_HAS_PGPS2K
HANDLE_TYPE_ONE_STRING("OpenPGP-S2K", OpenPGP_S2K);
+#endif
return 0;
}
diff --git a/src/get_enc.cpp b/src/get_enc.cpp
index 77799d318..8137f4645 100644
--- a/src/get_enc.cpp
+++ b/src/get_enc.cpp
@@ -6,7 +6,6 @@
#include <botan/lookup.h>
#include <botan/libstate.h>
#include <botan/parsing.h>
-#include <botan/kdf.h>
#include <botan/mgf1.h>
#include <botan/util.h>
@@ -38,6 +37,26 @@
#include <botan/eme_pkcs.h>
#endif
+#ifdef BOTAN_HAS_KDF1
+ #include <botan/kdf1.h>
+#endif
+
+#ifdef BOTAN_HAS_KDF2
+ #include <botan/kdf2.h>
+#endif
+
+#ifdef BOTAN_HAS_X942_PRF
+ #include <botan/prf_x942.h>
+#endif
+
+#ifdef BOTAN_HAS_SSL_V3_PRF
+ #include <botan/prf_ssl3.h>
+#endif
+
+#ifdef BOTAN_HAS_TLS_V10_PRF
+ #include <botan/prf_tls.h>
+#endif
+
namespace Botan {
/*************************************************
@@ -132,35 +151,45 @@ KDF* get_kdf(const std::string& algo_spec)
std::vector<std::string> name = parse_algorithm_name(algo_spec);
const std::string kdf_name = global_state().deref_alias(name[0]);
+#ifdef BOTAN_HAS_KDF1
if(kdf_name == "KDF1")
{
if(name.size() == 2)
return new KDF1(name[1]);
}
+#endif
+#ifdef BOTAN_HAS_KDF2
if(kdf_name == "KDF2")
{
if(name.size() == 2)
return new KDF2(name[1]);
}
+#endif
+#ifdef BOTAN_HAS_X942_PRF
if(kdf_name == "X9.42-PRF")
{
if(name.size() == 2)
return new X942_PRF(name[1]);
}
+#endif
+#ifdef BOTAN_HAS_TLS_V10_PRF
if(kdf_name == "TLS-PRF")
{
if(name.size() == 1)
return new TLS_PRF;
}
+#endif
+#ifdef BOTAN_HAS_SSL_V3_PRF
if(kdf_name == "SSL3-PRF")
{
if(name.size() == 1)
return new SSL3_PRF;
}
+#endif
throw Algorithm_Not_Found(algo_spec);
}
diff --git a/src/kdf.cpp b/src/kdf.cpp
index 9d60a1839..dca56e1a6 100644
--- a/src/kdf.cpp
+++ b/src/kdf.cpp
@@ -1,9 +1,9 @@
/*************************************************
-* KDF1/KDF2 Source File *
+* KDF Base Class Source File *
* (C) 1999-2007 Jack Lloyd *
*************************************************/
-#include <botan/kdf.h>
+#include <botan/pk_util.h>
#include <botan/lookup.h>
#include <botan/loadstor.h>
#include <algorithm>
@@ -67,65 +67,4 @@ SecureVector<byte> KDF::derive_key(u32bit key_len,
return derive(key_len, secret, secret_len, salt, salt_len);
}
-/*************************************************
-* KDF1 Key Derivation Mechanism *
-*************************************************/
-SecureVector<byte> KDF1::derive(u32bit,
- const byte secret[], u32bit secret_len,
- const byte P[], u32bit P_len) const
- {
- std::auto_ptr<HashFunction> hash(get_hash(hash_name));
-
- hash->update(secret, secret_len);
- hash->update(P, P_len);
- return hash->final();
- }
-
-/*************************************************
-* KDF1 Constructor *
-*************************************************/
-KDF1::KDF1(const std::string& h_name) : hash_name(h_name)
- {
- if(!have_hash(hash_name))
- throw Algorithm_Not_Found(hash_name);
- }
-
-/*************************************************
-* KDF2 Key Derivation Mechanism *
-*************************************************/
-SecureVector<byte> KDF2::derive(u32bit out_len,
- const byte secret[], u32bit secret_len,
- const byte P[], u32bit P_len) const
- {
- SecureVector<byte> output;
- u32bit counter = 1;
-
- std::auto_ptr<HashFunction> hash(get_hash(hash_name));
- while(out_len && counter)
- {
- hash->update(secret, secret_len);
- for(u32bit j = 0; j != 4; ++j)
- hash->update(get_byte(j, counter));
- hash->update(P, P_len);
- SecureVector<byte> hash_result = hash->final();
-
- u32bit added = std::min(hash_result.size(), out_len);
- output.append(hash_result, added);
- out_len -= added;
-
- ++counter;
- }
-
- return output;
- }
-
-/*************************************************
-* KDF2 Constructor *
-*************************************************/
-KDF2::KDF2(const std::string& h_name) : hash_name(h_name)
- {
- if(!have_hash(hash_name))
- throw Algorithm_Not_Found(hash_name);
- }
-
}