diff options
author | lloyd <[email protected]> | 2015-01-31 16:18:09 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2015-01-31 16:18:09 +0000 |
commit | fd3016d10124d2b7ccd7bc885235f2e407d73800 (patch) | |
tree | e237b54c3a8d465c893a012157d9d52014eaccc9 /src/lib/kdf | |
parent | 00c9b3f4834603946065c15b9b2e9fa5e973b979 (diff) |
Use registry also for KDF, EMSA, and EME
Diffstat (limited to 'src/lib/kdf')
-rw-r--r-- | src/lib/kdf/info.txt | 8 | ||||
-rw-r--r-- | src/lib/kdf/kdf.cpp | 49 | ||||
-rw-r--r-- | src/lib/kdf/kdf.h | 3 | ||||
-rw-r--r-- | src/lib/kdf/kdf1/kdf1.cpp | 3 | ||||
-rw-r--r-- | src/lib/kdf/kdf2/kdf2.cpp | 3 | ||||
-rw-r--r-- | src/lib/kdf/prf_tls/prf_tls.cpp | 24 | ||||
-rw-r--r-- | src/lib/kdf/prf_tls/prf_tls.h | 10 | ||||
-rw-r--r-- | src/lib/kdf/prf_x942/prf_x942.cpp | 3 |
8 files changed, 47 insertions, 56 deletions
diff --git a/src/lib/kdf/info.txt b/src/lib/kdf/info.txt index f33a4bc8d..91489ca24 100644 --- a/src/lib/kdf/info.txt +++ b/src/lib/kdf/info.txt @@ -4,3 +4,11 @@ define KDF_BASE 20131128 alloc libstate </requires> + +<header:public> +kdf.h +</header:public> + +<header:internal> +kdf_utils.h +</header:internal> diff --git a/src/lib/kdf/kdf.cpp b/src/lib/kdf/kdf.cpp index a0ee580ab..e18d9ce75 100644 --- a/src/lib/kdf/kdf.cpp +++ b/src/lib/kdf/kdf.cpp @@ -6,24 +6,8 @@ */ #include <botan/kdf.h> -#include <botan/libstate.h> -#include <botan/scan_name.h> - -#if defined(BOTAN_HAS_KDF1) - #include <botan/kdf1.h> -#endif - -#if defined(BOTAN_HAS_KDF2) - #include <botan/kdf2.h> -#endif - -#if defined(BOTAN_HAS_X942_PRF) - #include <botan/prf_x942.h> -#endif - -#if defined(BOTAN_HAS_TLS_V10_PRF) - #include <botan/prf_tls.h> -#endif +#include <botan/algo_registry.h> +#include <botan/exceptn.h> namespace Botan { @@ -31,36 +15,11 @@ KDF* get_kdf(const std::string& algo_spec) { SCAN_Name request(algo_spec); - Algorithm_Factory& af = global_state().algorithm_factory(); - if(request.algo_name() == "Raw") return nullptr; // No KDF -#if defined(BOTAN_HAS_KDF1) - if(request.algo_name() == "KDF1" && request.arg_count() == 1) - return new KDF1(af.make_hash_function(request.arg(0))); -#endif - -#if defined(BOTAN_HAS_KDF2) - if(request.algo_name() == "KDF2" && request.arg_count() == 1) - return new KDF2(af.make_hash_function(request.arg(0))); -#endif - -#if defined(BOTAN_HAS_X942_PRF) - if(request.algo_name() == "X9.42-PRF" && request.arg_count() == 1) - return new X942_PRF(request.arg(0)); // OID -#endif - -#if defined(BOTAN_HAS_TLS_V10_PRF) - if(request.algo_name() == "TLS-PRF" && request.arg_count() == 0) - return new TLS_PRF; -#endif - -#if defined(BOTAN_HAS_TLS_V12_PRF) - if(request.algo_name() == "TLS-12-PRF" && request.arg_count() == 1) - return new TLS_12_PRF(af.make_mac("HMAC(" + request.arg(0) + ")")); -#endif - + if(KDF* kdf = make_a<KDF>(algo_spec)) + return kdf; throw Algorithm_Not_Found(algo_spec); } diff --git a/src/lib/kdf/kdf.h b/src/lib/kdf/kdf.h index 7b417fcd9..9d8ca57fc 100644 --- a/src/lib/kdf/kdf.h +++ b/src/lib/kdf/kdf.h @@ -8,6 +8,7 @@ #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> @@ -107,6 +108,8 @@ class BOTAN_DLL KDF } virtual KDF* clone() const = 0; + + typedef SCAN_Name Spec; private: virtual secure_vector<byte> derive(size_t key_len, diff --git a/src/lib/kdf/kdf1/kdf1.cpp b/src/lib/kdf/kdf1/kdf1.cpp index b0fa97443..df84a1a00 100644 --- a/src/lib/kdf/kdf1/kdf1.cpp +++ b/src/lib/kdf/kdf1/kdf1.cpp @@ -5,10 +5,13 @@ * Botan is released under the Simplified BSD License (see license.txt) */ +#include <botan/internal/kdf_utils.h> #include <botan/kdf1.h> namespace Botan { +BOTAN_REGISTER_KDF_1HASH(KDF1, "KDF1"); + /* * KDF1 Key Derivation Mechanism */ diff --git a/src/lib/kdf/kdf2/kdf2.cpp b/src/lib/kdf/kdf2/kdf2.cpp index 052df0b6a..c7b355580 100644 --- a/src/lib/kdf/kdf2/kdf2.cpp +++ b/src/lib/kdf/kdf2/kdf2.cpp @@ -5,10 +5,13 @@ * Botan is released under the Simplified BSD License (see license.txt) */ +#include <botan/internal/kdf_utils.h> #include <botan/kdf2.h> namespace Botan { +BOTAN_REGISTER_KDF_1HASH(KDF2, "KDF2"); + /* * KDF2 Key Derivation Mechanism */ diff --git a/src/lib/kdf/prf_tls/prf_tls.cpp b/src/lib/kdf/prf_tls/prf_tls.cpp index 79292922c..f1061fd10 100644 --- a/src/lib/kdf/prf_tls/prf_tls.cpp +++ b/src/lib/kdf/prf_tls/prf_tls.cpp @@ -5,14 +5,24 @@ * Botan is released under the Simplified BSD License (see license.txt) */ +#include <botan/internal/kdf_utils.h> #include <botan/prf_tls.h> -#include <botan/internal/xor_buf.h> #include <botan/hmac.h> -#include <botan/md5.h> -#include <botan/sha160.h> namespace Botan { +TLS_12_PRF* TLS_12_PRF::make(const Spec& spec) + { + if(auto mac = make_a<MessageAuthenticationCode>(spec.arg(0))) + return new TLS_12_PRF(mac); + if(auto hash = make_a<HashFunction>(spec.arg(0))) + return new TLS_12_PRF(new HMAC(hash)); + return nullptr; + } + +BOTAN_REGISTER_NAMED_T(KDF, "TLS-12-PRF", TLS_12_PRF, TLS_12_PRF::make); +BOTAN_REGISTER_KDF_NOARGS(TLS_PRF, "TLS-PRF"); + namespace { /* @@ -61,8 +71,8 @@ void P_hash(secure_vector<byte>& output, */ TLS_PRF::TLS_PRF() { - hmac_md5.reset(new HMAC(new MD5)); - hmac_sha1.reset(new HMAC(new SHA_160)); + hmac_md5.reset(make_a<MessageAuthenticationCode>("HMAC(MD5)")); + hmac_sha1.reset(make_a<MessageAuthenticationCode>("HMAC(SHA-1)")); } /* @@ -88,7 +98,7 @@ secure_vector<byte> TLS_PRF::derive(size_t key_len, /* * TLS v1.2 PRF Constructor and Destructor */ -TLS_12_PRF::TLS_12_PRF(MessageAuthenticationCode* mac) : hmac(mac) +TLS_12_PRF::TLS_12_PRF(MessageAuthenticationCode* mac) : m_mac(mac) { } @@ -98,7 +108,7 @@ secure_vector<byte> TLS_12_PRF::derive(size_t key_len, { secure_vector<byte> output(key_len); - P_hash(output, *hmac, secret, secret_len, seed, seed_len); + P_hash(output, *m_mac, 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 71b4d55e9..c3adc6caf 100644 --- a/src/lib/kdf/prf_tls/prf_tls.h +++ b/src/lib/kdf/prf_tls/prf_tls.h @@ -42,12 +42,14 @@ class BOTAN_DLL TLS_12_PRF : public KDF const byte secret[], size_t secret_len, const byte seed[], size_t seed_len) const; - std::string name() const { return "TLSv12-PRF(" + hmac->name() + ")"; } - KDF* clone() const { return new TLS_12_PRF(hmac->clone()); } + std::string name() const { return "TLS-12-PRF(" + m_mac->name() + ")"; } + KDF* clone() const { return new TLS_12_PRF(m_mac->clone()); } - TLS_12_PRF(MessageAuthenticationCode* hmac); + TLS_12_PRF(MessageAuthenticationCode* mac); + + static TLS_12_PRF* make(const Spec& spec); private: - std::unique_ptr<MessageAuthenticationCode> hmac; + std::unique_ptr<MessageAuthenticationCode> m_mac; }; } diff --git a/src/lib/kdf/prf_x942/prf_x942.cpp b/src/lib/kdf/prf_x942/prf_x942.cpp index a5ee1f3e4..30bf737a9 100644 --- a/src/lib/kdf/prf_x942/prf_x942.cpp +++ b/src/lib/kdf/prf_x942/prf_x942.cpp @@ -5,6 +5,7 @@ * Botan is released under the Simplified BSD License (see license.txt) */ +#include <botan/internal/kdf_utils.h> #include <botan/prf_x942.h> #include <botan/der_enc.h> #include <botan/oids.h> @@ -14,6 +15,8 @@ namespace Botan { +BOTAN_REGISTER_KDF_NAMED_1STR(X942_PRF, "X9.42-PRF"); + namespace { /* |