aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/kdf
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/kdf')
-rw-r--r--src/lib/kdf/hkdf/hkdf.cpp11
-rw-r--r--src/lib/kdf/hkdf/hkdf.h2
-rw-r--r--src/lib/kdf/kdf.cpp183
-rw-r--r--src/lib/kdf/kdf.h20
-rw-r--r--src/lib/kdf/prf_tls/prf_tls.cpp11
-rw-r--r--src/lib/kdf/prf_tls/prf_tls.h2
-rw-r--r--src/lib/kdf/sp800_108/sp800_108.cpp33
-rw-r--r--src/lib/kdf/sp800_108/sp800_108.h5
-rw-r--r--src/lib/kdf/sp800_56c/sp800_56c.cpp14
-rw-r--r--src/lib/kdf/sp800_56c/sp800_56c.h2
10 files changed, 157 insertions, 126 deletions
diff --git a/src/lib/kdf/hkdf/hkdf.cpp b/src/lib/kdf/hkdf/hkdf.cpp
index 56dc72f09..13d5832d2 100644
--- a/src/lib/kdf/hkdf/hkdf.cpp
+++ b/src/lib/kdf/hkdf/hkdf.cpp
@@ -9,17 +9,6 @@
namespace Botan {
-HKDF* HKDF::make(const Spec& spec)
- {
- if(auto mac = MessageAuthenticationCode::create(spec.arg(0)))
- return new HKDF(mac.release());
-
- if(auto mac = MessageAuthenticationCode::create("HMAC(" + spec.arg(0) + ")"))
- return new HKDF(mac.release());
-
- return nullptr;
- }
-
size_t HKDF::kdf(byte out[], size_t out_len,
const byte secret[], size_t secret_len,
const byte salt[], size_t salt_len,
diff --git a/src/lib/kdf/hkdf/hkdf.h b/src/lib/kdf/hkdf/hkdf.h
index ea17f8c01..54ecc5283 100644
--- a/src/lib/kdf/hkdf/hkdf.h
+++ b/src/lib/kdf/hkdf/hkdf.h
@@ -27,8 +27,6 @@ class BOTAN_DLL HKDF final : public KDF
*/
explicit HKDF(MessageAuthenticationCode* prf) : m_prf(prf) {}
- static HKDF* make(const Spec& spec);
-
KDF* clone() const override { return new HKDF(m_prf->clone()); }
std::string name() const override { return "HKDF(" + m_prf->name() + ")"; }
diff --git a/src/lib/kdf/kdf.cpp b/src/lib/kdf/kdf.cpp
index 66296bf96..f8f822809 100644
--- a/src/lib/kdf/kdf.cpp
+++ b/src/lib/kdf/kdf.cpp
@@ -6,8 +6,8 @@
*/
#include <botan/kdf.h>
+#include <botan/scan_name.h>
#include <botan/exceptn.h>
-#include <botan/internal/algo_registry.h>
#if defined(BOTAN_HAS_HKDF)
#include <botan/hkdf.h>
@@ -45,77 +45,176 @@
#include <botan/sp800_56c.h>
#endif
-#define BOTAN_REGISTER_KDF_NOARGS(type, name) \
- BOTAN_REGISTER_NAMED_T(KDF, name, type, (make_new_T<type>))
-#define BOTAN_REGISTER_KDF_1HASH(type, name) \
- BOTAN_REGISTER_NAMED_T(KDF, name, type, (make_new_T_1X<type, HashFunction>))
-
-#define BOTAN_REGISTER_KDF_NAMED_1STR(type, name) \
- BOTAN_REGISTER_NAMED_T(KDF, name, type, (make_new_T_1str_req<type>))
-
namespace Botan {
-KDF::~KDF() {}
+namespace {
-std::unique_ptr<KDF> KDF::create(const std::string& algo_spec,
- const std::string& provider)
+template<typename KDF_Type>
+std::unique_ptr<KDF>
+kdf_create_mac_or_hash(const std::string& nm)
{
- return std::unique_ptr<KDF>(make_a<KDF>(Botan::KDF::Spec(algo_spec), provider));
- }
+ if(auto mac = MessageAuthenticationCode::create(nm))
+ return std::unique_ptr<KDF>(new KDF_Type(mac.release()));
-std::vector<std::string> KDF::providers(const std::string& algo_spec)
- {
- return providers_of<KDF>(KDF::Spec(algo_spec));
- }
+ if(auto mac = MessageAuthenticationCode::create("HMAC(" + nm + ")"))
+ return std::unique_ptr<KDF>(new KDF_Type(mac.release()));
-KDF* get_kdf(const std::string& algo_spec)
- {
- SCAN_Name request(algo_spec);
+ return nullptr;
+ }
- if(request.algo_name() == "Raw")
- return nullptr; // No KDF
+}
- auto kdf = KDF::create(algo_spec);
- if(!kdf)
- throw Algorithm_Not_Found(algo_spec);
- return kdf.release();
- }
+std::unique_ptr<KDF> KDF::create(const std::string& algo_spec,
+ const std::string& provider)
+ {
+ const SCAN_Name req(algo_spec);
#if defined(BOTAN_HAS_HKDF)
-BOTAN_REGISTER_NAMED_T(KDF, "HKDF", HKDF, HKDF::make);
-#endif
-
-#if defined(BOTAN_HAS_KDF1)
-BOTAN_REGISTER_KDF_1HASH(KDF1, "KDF1");
+ if(req.algo_name() == "HKDF" && req.arg_count() == 1)
+ {
+ if(provider.empty() || provider == "base")
+ {
+ return kdf_create_mac_or_hash<HKDF>(req.arg(0));
+ }
+ }
#endif
#if defined(BOTAN_HAS_KDF2)
-BOTAN_REGISTER_KDF_1HASH(KDF2, "KDF2");
+ if(req.algo_name() == "KDF2" && req.arg_count() == 1)
+ {
+ if(provider.empty() || provider == "base")
+ {
+ if(auto hash = HashFunction::create(req.arg(0)))
+ return std::unique_ptr<KDF>(new KDF2(hash.release()));
+ }
+ }
#endif
#if defined(BOTAN_HAS_KDF1_18033)
-BOTAN_REGISTER_KDF_1HASH( KDF1_18033, "KDF1-18033" );
+ if(req.algo_name() == "KDF1-18033" && req.arg_count() == 1)
+ {
+ if(provider.empty() || provider == "base")
+ {
+ if(auto hash = HashFunction::create(req.arg(0)))
+ return std::unique_ptr<KDF>(new KDF1_18033(hash.release()));
+ }
+ }
+#endif
+
+#if defined(BOTAN_HAS_KDF1)
+ if(req.algo_name() == "KDF1" && req.arg_count() == 1)
+ {
+ if(provider.empty() || provider == "base")
+ {
+ if(auto hash = HashFunction::create(req.arg(0)))
+ return std::unique_ptr<KDF>(new KDF1(hash.release()));
+ }
+ }
#endif
#if defined(BOTAN_HAS_TLS_V10_PRF)
-BOTAN_REGISTER_KDF_NOARGS(TLS_PRF, "TLS-PRF");
+ if(req.algo_name() == "TLS-PRF" && req.arg_count() == 0)
+ {
+ if(provider.empty() || provider == "base")
+ {
+ return std::unique_ptr<KDF>(new TLS_PRF);
+ }
+ }
#endif
#if defined(BOTAN_HAS_TLS_V12_PRF)
-BOTAN_REGISTER_NAMED_T(KDF, "TLS-12-PRF", TLS_12_PRF, TLS_12_PRF::make);
+ if(req.algo_name() == "TLS-12-PRF" && req.arg_count() == 1)
+ {
+ if(provider.empty() || provider == "base")
+ {
+ return kdf_create_mac_or_hash<TLS_12_PRF>(req.arg(0));
+ }
+ }
#endif
#if defined(BOTAN_HAS_X942_PRF)
-BOTAN_REGISTER_KDF_NAMED_1STR(X942_PRF, "X9.42-PRF");
+ if(req.algo_name() == "X9.42-PRF" && req.arg_count() == 1)
+ {
+ if(provider.empty() || provider == "base")
+ {
+ return std::unique_ptr<KDF>(new X942_PRF(req.arg(0)));
+ }
+ }
#endif
#if defined(BOTAN_HAS_SP800_108)
-BOTAN_REGISTER_NAMED_T(KDF, "SP800-108-Counter", SP800_108_Counter, SP800_108_Counter::make);
-BOTAN_REGISTER_NAMED_T(KDF, "SP800-108-Feedback", SP800_108_Feedback, SP800_108_Feedback::make);
-BOTAN_REGISTER_NAMED_T(KDF, "SP800-108-Pipeline", SP800_108_Pipeline, SP800_108_Pipeline::make);
+ if(req.algo_name() == "SP800-108-Counter" && req.arg_count() == 1)
+ {
+ if(provider.empty() || provider == "base")
+ {
+ return kdf_create_mac_or_hash<SP800_108_Counter>(req.arg(0));
+ }
+ }
+
+ if(req.algo_name() == "SP800-108-Feedback" && req.arg_count() == 1)
+ {
+ if(provider.empty() || provider == "base")
+ {
+ return kdf_create_mac_or_hash<SP800_108_Feedback>(req.arg(0));
+ }
+ }
+
+ if(req.algo_name() == "SP800-108-Pipeline" && req.arg_count() == 1)
+ {
+ if(provider.empty() || provider == "base")
+ {
+ return kdf_create_mac_or_hash<SP800_108_Pipeline>(req.arg(0));
+ }
+ }
#endif
#if defined(BOTAN_HAS_SP800_56C)
-BOTAN_REGISTER_NAMED_T(KDF, "SP800-56C", SP800_56C, SP800_56C::make);
+ if(req.algo_name() == "SP800-56C" && req.arg_count() == 1)
+ {
+ std::unique_ptr<KDF> exp(kdf_create_mac_or_hash<SP800_108_Feedback>(req.arg(0)));
+ if(exp)
+ {
+ if(auto mac = MessageAuthenticationCode::create(req.arg(0)))
+ return std::unique_ptr<KDF>(new SP800_56C(mac.release(), exp.release()));
+
+ if(auto mac = MessageAuthenticationCode::create("HMAC(" + req.arg(0) + ")"))
+ return std::unique_ptr<KDF>(new SP800_56C(mac.release(), exp.release()));
+ }
+ }
#endif
+
+ return nullptr;
+ }
+
+//static
+std::unique_ptr<KDF>
+KDF::create_or_throw(const std::string& algo,
+ const std::string& provider)
+ {
+ if(auto bc = KDF::create(algo, provider))
+ {
+ return bc;
+ }
+ throw Lookup_Error("Block cipher", algo, provider);
+ }
+
+std::vector<std::string> KDF::providers(const std::string& algo_spec)
+ {
+ return probe_providers_of<KDF>(algo_spec, { "base" });
+ }
+
+KDF* get_kdf(const std::string& algo_spec)
+ {
+ SCAN_Name request(algo_spec);
+
+ if(request.algo_name() == "Raw")
+ return nullptr; // No KDF
+
+ //return KDF::create_or_throw(algo_spec).release();
+ auto kdf = KDF::create(algo_spec);
+ if(!kdf)
+ throw Algorithm_Not_Found(algo_spec);
+ return kdf.release();
+ }
+
}
diff --git a/src/lib/kdf/kdf.h b/src/lib/kdf/kdf.h
index f9acb9d38..ab793da87 100644
--- a/src/lib/kdf/kdf.h
+++ b/src/lib/kdf/kdf.h
@@ -8,7 +8,6 @@
#ifndef BOTAN_KDF_BASE_H__
#define BOTAN_KDF_BASE_H__
-#include <botan/scan_name.h>
#include <botan/secmem.h>
#include <botan/types.h>
#include <string>
@@ -21,7 +20,7 @@ namespace Botan {
class BOTAN_DLL KDF
{
public:
- virtual ~KDF();
+ virtual ~KDF() {}
/**
* Create an instance based on a name
@@ -30,8 +29,18 @@ class BOTAN_DLL KDF
* @param provider provider implementation to choose
* @return a null pointer if the algo/provider combination cannot be found
*/
- static std::unique_ptr<KDF> create(const std::string& algo_spec,
- const std::string& provider = "");
+ static std::unique_ptr<KDF>
+ create(const std::string& algo_spec,
+ const std::string& provider = "");
+
+ /**
+ * Create an instance based on a name, or throw if the
+ * algo/provider combination cannot be found. If provider is
+ * empty then best available is chosen.
+ */
+ static std::unique_ptr<KDF>
+ create_or_throw(const std::string& algo_spec,
+ const std::string& provider = "");
/**
* @return list of available providers for this algorithm, empty if not available
@@ -173,8 +182,11 @@ class BOTAN_DLL KDF
* @return new object representing the same algorithm as *this
*/
virtual KDF* clone() const = 0;
+<<<<<<< HEAD
typedef SCAN_Name Spec;
+=======
+>>>>>>> 8978841... Remove Algo_Registry
};
/**
diff --git a/src/lib/kdf/prf_tls/prf_tls.cpp b/src/lib/kdf/prf_tls/prf_tls.cpp
index 14b330901..e83d07692 100644
--- a/src/lib/kdf/prf_tls/prf_tls.cpp
+++ b/src/lib/kdf/prf_tls/prf_tls.cpp
@@ -10,17 +10,6 @@
namespace Botan {
-TLS_12_PRF* TLS_12_PRF::make(const Spec& spec)
- {
- if(auto mac = MessageAuthenticationCode::create(spec.arg(0)))
- return new TLS_12_PRF(mac.release());
-
- if(auto mac = MessageAuthenticationCode::create("HMAC(" + spec.arg(0) + ")"))
- return new TLS_12_PRF(mac.release());
-
- return nullptr;
- }
-
TLS_PRF::TLS_PRF() :
m_hmac_md5(MessageAuthenticationCode::create("HMAC(MD5)")),
m_hmac_sha1(MessageAuthenticationCode::create("HMAC(SHA-1)"))
diff --git a/src/lib/kdf/prf_tls/prf_tls.h b/src/lib/kdf/prf_tls/prf_tls.h
index 58cd5758e..64891144a 100644
--- a/src/lib/kdf/prf_tls/prf_tls.h
+++ b/src/lib/kdf/prf_tls/prf_tls.h
@@ -53,8 +53,6 @@ class BOTAN_DLL TLS_12_PRF final : public KDF
* @param mac MAC algorithm to use
*/
explicit TLS_12_PRF(MessageAuthenticationCode* mac) : m_mac(mac) {}
-
- static TLS_12_PRF* make(const Spec& spec);
private:
std::unique_ptr<MessageAuthenticationCode> m_mac;
};
diff --git a/src/lib/kdf/sp800_108/sp800_108.cpp b/src/lib/kdf/sp800_108/sp800_108.cpp
index aafb349b2..77973600a 100644
--- a/src/lib/kdf/sp800_108/sp800_108.cpp
+++ b/src/lib/kdf/sp800_108/sp800_108.cpp
@@ -12,17 +12,6 @@
namespace Botan {
-SP800_108_Counter* SP800_108_Counter::make(const Spec& spec)
- {
- if(auto mac = MessageAuthenticationCode::create(spec.arg(0)))
- return new SP800_108_Counter(mac.release());
-
- if(auto mac = MessageAuthenticationCode::create("HMAC(" + spec.arg(0) + ")"))
- return new SP800_108_Counter(mac.release());
-
- return nullptr;
- }
-
size_t SP800_108_Counter::kdf(byte key[], size_t key_len,
const byte secret[], size_t secret_len,
const byte salt[], size_t salt_len,
@@ -65,17 +54,6 @@ size_t SP800_108_Counter::kdf(byte key[], size_t key_len,
return key_len;
}
-SP800_108_Feedback* SP800_108_Feedback::make(const Spec& spec)
- {
- if(auto mac = MessageAuthenticationCode::create(spec.arg(0)))
- return new SP800_108_Feedback(mac.release());
-
- if(auto mac = MessageAuthenticationCode::create("HMAC(" + spec.arg(0) + ")"))
- return new SP800_108_Feedback(mac.release());
-
- return nullptr;
- }
-
size_t SP800_108_Feedback::kdf(byte key[], size_t key_len,
const byte secret[], size_t secret_len,
const byte salt[], size_t salt_len,
@@ -122,17 +100,6 @@ size_t SP800_108_Feedback::kdf(byte key[], size_t key_len,
return key_len;
}
-SP800_108_Pipeline* SP800_108_Pipeline::make(const Spec& spec)
- {
- if(auto mac = MessageAuthenticationCode::create(spec.arg(0)))
- return new SP800_108_Pipeline(mac.release());
-
- if(auto mac = MessageAuthenticationCode::create("HMAC(" + spec.arg(0) + ")"))
- return new SP800_108_Pipeline(mac.release());
-
- return nullptr;
- }
-
size_t SP800_108_Pipeline::kdf(byte key[], size_t key_len,
const byte secret[], size_t secret_len,
const byte salt[], size_t salt_len,
diff --git a/src/lib/kdf/sp800_108/sp800_108.h b/src/lib/kdf/sp800_108/sp800_108.h
index 2d4d028b2..e368457b4 100644
--- a/src/lib/kdf/sp800_108/sp800_108.h
+++ b/src/lib/kdf/sp800_108/sp800_108.h
@@ -49,8 +49,6 @@ class BOTAN_DLL SP800_108_Counter : public KDF
* @param mac MAC algorithm to use
*/
SP800_108_Counter(MessageAuthenticationCode* mac) : m_prf(mac) {}
-
- static SP800_108_Counter* make(const Spec& spec);
private:
std::unique_ptr<MessageAuthenticationCode> m_prf;
};
@@ -88,8 +86,6 @@ class BOTAN_DLL SP800_108_Feedback : public KDF
const byte label[], size_t label_len) const override;
SP800_108_Feedback(MessageAuthenticationCode* mac) : m_prf(mac) {}
-
- static SP800_108_Feedback* make(const Spec& spec);
private:
std::unique_ptr<MessageAuthenticationCode> m_prf;
};
@@ -128,7 +124,6 @@ class BOTAN_DLL SP800_108_Pipeline : public KDF
SP800_108_Pipeline(MessageAuthenticationCode* mac) : m_prf(mac) {}
- static SP800_108_Pipeline* make(const Spec& spec);
private:
std::unique_ptr<MessageAuthenticationCode> m_prf;
};
diff --git a/src/lib/kdf/sp800_56c/sp800_56c.cpp b/src/lib/kdf/sp800_56c/sp800_56c.cpp
index 338feba2a..f6d01ec2f 100644
--- a/src/lib/kdf/sp800_56c/sp800_56c.cpp
+++ b/src/lib/kdf/sp800_56c/sp800_56c.cpp
@@ -11,20 +11,6 @@
namespace Botan {
-SP800_56C* SP800_56C::make(const Spec& spec)
- {
- if(auto exp = SP800_108_Feedback::make(spec))
- {
- if(auto mac = MessageAuthenticationCode::create(spec.arg(0)))
- return new SP800_56C(mac.release(), exp);
-
- if(auto mac = MessageAuthenticationCode::create("HMAC(" + spec.arg(0) + ")"))
- return new SP800_56C(mac.release(), exp);
- }
-
- return nullptr;
- }
-
size_t SP800_56C::kdf(byte key[], size_t key_len,
const byte secret[], size_t secret_len,
const byte salt[], size_t salt_len,
diff --git a/src/lib/kdf/sp800_56c/sp800_56c.h b/src/lib/kdf/sp800_56c/sp800_56c.h
index 83f11906a..5c5acb075 100644
--- a/src/lib/kdf/sp800_56c/sp800_56c.h
+++ b/src/lib/kdf/sp800_56c/sp800_56c.h
@@ -50,8 +50,6 @@ class BOTAN_DLL SP800_56C : public KDF
* @param exp KDF used for key expansion
*/
SP800_56C(MessageAuthenticationCode* mac, KDF* exp) : m_prf(mac), m_exp(exp) {}
-
- static SP800_56C* make(const Spec& spec);
private:
std::unique_ptr<MessageAuthenticationCode> m_prf;
std::unique_ptr<KDF> m_exp;