diff options
author | Jack Lloyd <[email protected]> | 2016-10-11 13:00:57 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2016-10-21 16:53:16 -0400 |
commit | 558808900bffc3c48da5e6d79ba602e88e619154 (patch) | |
tree | fd76ee2d009c2b707d888683cbd767351c4ff6b3 /src/lib/mac | |
parent | 6aa855bba613c7b6fedfbe71d15930964acb1633 (diff) |
Remove Algo_Registry
I repent my use of global constructors.
I repent my use of global locks.
Hopefully I will never touch this code again.
:)
Diffstat (limited to 'src/lib/mac')
-rw-r--r-- | src/lib/mac/cbc_mac/cbc_mac.cpp | 10 | ||||
-rw-r--r-- | src/lib/mac/cbc_mac/cbc_mac.h | 2 | ||||
-rw-r--r-- | src/lib/mac/cmac/cmac.cpp | 10 | ||||
-rw-r--r-- | src/lib/mac/cmac/cmac.h | 2 | ||||
-rw-r--r-- | src/lib/mac/hmac/hmac.cpp | 10 | ||||
-rw-r--r-- | src/lib/mac/hmac/hmac.h | 2 | ||||
-rw-r--r-- | src/lib/mac/mac.cpp | 117 | ||||
-rw-r--r-- | src/lib/mac/mac.h | 21 |
8 files changed, 101 insertions, 73 deletions
diff --git a/src/lib/mac/cbc_mac/cbc_mac.cpp b/src/lib/mac/cbc_mac/cbc_mac.cpp index 449865255..741d550e5 100644 --- a/src/lib/mac/cbc_mac/cbc_mac.cpp +++ b/src/lib/mac/cbc_mac/cbc_mac.cpp @@ -9,16 +9,6 @@ namespace Botan { -CBC_MAC* CBC_MAC::make(const Spec& spec) - { - if(spec.arg_count() == 1) - { - if(auto bc = BlockCipher::create(spec.arg(0))) - return new CBC_MAC(bc.release()); - } - return nullptr; - } - /* * Update an CBC-MAC Calculation */ diff --git a/src/lib/mac/cbc_mac/cbc_mac.h b/src/lib/mac/cbc_mac/cbc_mac.h index 4a3dece95..dd4877d1c 100644 --- a/src/lib/mac/cbc_mac/cbc_mac.h +++ b/src/lib/mac/cbc_mac/cbc_mac.h @@ -33,8 +33,6 @@ class BOTAN_DLL CBC_MAC final : public MessageAuthenticationCode * @param cipher the block cipher to use */ explicit CBC_MAC(BlockCipher* cipher); - - static CBC_MAC* make(const Spec& spec); private: void add_data(const byte[], size_t) override; void final_result(byte[]) override; diff --git a/src/lib/mac/cmac/cmac.cpp b/src/lib/mac/cmac/cmac.cpp index 1501bc316..9afd86cdb 100644 --- a/src/lib/mac/cmac/cmac.cpp +++ b/src/lib/mac/cmac/cmac.cpp @@ -9,16 +9,6 @@ namespace Botan { -CMAC* CMAC::make(const Spec& spec) - { - if(spec.arg_count() == 1) - { - if(auto bc = BlockCipher::create(spec.arg(0))) - return new CMAC(bc.release()); - } - return nullptr; - } - /* * Perform CMAC's multiplication in GF(2^n) */ diff --git a/src/lib/mac/cmac/cmac.h b/src/lib/mac/cmac/cmac.h index fac754e62..6897665c0 100644 --- a/src/lib/mac/cmac/cmac.h +++ b/src/lib/mac/cmac/cmac.h @@ -41,8 +41,6 @@ class BOTAN_DLL CMAC final : public MessageAuthenticationCode */ explicit CMAC(BlockCipher* cipher); - static CMAC* make(const Spec& spec); - CMAC(const CMAC&) = delete; CMAC& operator=(const CMAC&) = delete; private: diff --git a/src/lib/mac/hmac/hmac.cpp b/src/lib/mac/hmac/hmac.cpp index f445ab0cf..a2021515f 100644 --- a/src/lib/mac/hmac/hmac.cpp +++ b/src/lib/mac/hmac/hmac.cpp @@ -10,16 +10,6 @@ namespace Botan { -HMAC* HMAC::make(const Spec& spec) - { - if(spec.arg_count() == 1) - { - if(auto h = HashFunction::create(spec.arg(0))) - return new HMAC(h.release()); - } - return nullptr; - } - /* * Update a HMAC Calculation */ diff --git a/src/lib/mac/hmac/hmac.h b/src/lib/mac/hmac/hmac.h index 654a167e7..bfb425fa8 100644 --- a/src/lib/mac/hmac/hmac.h +++ b/src/lib/mac/hmac/hmac.h @@ -36,8 +36,6 @@ class BOTAN_DLL HMAC final : public MessageAuthenticationCode */ explicit HMAC(HashFunction* hash); - static HMAC* make(const Spec& spec); - HMAC(const HMAC&) = delete; HMAC& operator=(const HMAC&) = delete; private: diff --git a/src/lib/mac/mac.cpp b/src/lib/mac/mac.cpp index a3917141d..919ce959d 100644 --- a/src/lib/mac/mac.cpp +++ b/src/lib/mac/mac.cpp @@ -6,7 +6,7 @@ */ #include <botan/mac.h> -#include <botan/internal/algo_registry.h> +#include <botan/scan_name.h> #include <botan/mem_ops.h> #if defined(BOTAN_HAS_CBC_MAC) @@ -35,18 +35,97 @@ namespace Botan { -std::unique_ptr<MessageAuthenticationCode> MessageAuthenticationCode::create(const std::string& algo_spec, - const std::string& provider) +std::unique_ptr<MessageAuthenticationCode> +MessageAuthenticationCode::create(const std::string& algo_spec, + const std::string& provider) { - return std::unique_ptr<MessageAuthenticationCode>(make_a<MessageAuthenticationCode>(MessageAuthenticationCode::Spec(algo_spec), provider)); + const SCAN_Name req(algo_spec); + +#if defined(BOTAN_HAS_HMAC) + if(req.algo_name() == "HMAC" && req.arg_count() == 1) + { + // TODO OpenSSL + if(provider.empty() || provider == "base") + { + if(auto h = HashFunction::create(req.arg(0))) + return std::unique_ptr<MessageAuthenticationCode>(new HMAC(h.release())); + } + } +#endif + +#if defined(BOTAN_HAS_POLY1305) + if(req.algo_name() == "Poly1305" && req.arg_count() == 0) + { + if(provider.empty() || provider == "base") + return std::unique_ptr<MessageAuthenticationCode>(new Poly1305); + } +#endif + +#if defined(BOTAN_HAS_SIPHASH) + if(req.algo_name() == "SipHash") + { + if(provider.empty() || provider == "base") + { + return std::unique_ptr<MessageAuthenticationCode>( + new SipHash(req.arg_as_integer(0, 2), + req.arg_as_integer(1, 4))); + } + } +#endif + +#if defined(BOTAN_HAS_CMAC) + if(req.algo_name() == "CMAC" && req.arg_count() == 1) + { + if(provider.empty() || provider == "base") + { + if(auto bc = BlockCipher::create(req.arg(0))) + return std::unique_ptr<MessageAuthenticationCode>(new CMAC(bc.release())); + } + } +#endif + + +#if defined(BOTAN_HAS_CBC_MAC) + if(req.algo_name() == "CBC-MAC" && req.arg_count() == 1) + { + if(provider.empty() || provider == "base") + { + if(auto bc = BlockCipher::create(req.arg(0))) + return std::unique_ptr<MessageAuthenticationCode>(new CBC_MAC(bc.release())); + } + } +#endif + +#if defined(BOTAN_HAS_ANSI_X919_MAC) + if(req.algo_name() == "X9.19-MAC") + { + if(provider.empty() || provider == "base") + { + return std::unique_ptr<MessageAuthenticationCode>(new ANSI_X919_MAC); + } + } +#endif + + return nullptr; } -std::vector<std::string> MessageAuthenticationCode::providers(const std::string& algo_spec) +std::vector<std::string> +MessageAuthenticationCode::providers(const std::string& algo_spec) { - return providers_of<MessageAuthenticationCode>(MessageAuthenticationCode::Spec(algo_spec)); + return probe_providers_of<MessageAuthenticationCode>(algo_spec, {"base", "openssl"}); } -MessageAuthenticationCode::~MessageAuthenticationCode() {} +//static +std::unique_ptr<MessageAuthenticationCode> +MessageAuthenticationCode::create_or_throw(const std::string& algo, + const std::string& provider) + { + if(auto mac = MessageAuthenticationCode::create(algo, provider)) + { + return mac; + } + throw Lookup_Error("MAC", algo, provider); + } /* * Default (deterministic) MAC verification operation @@ -61,28 +140,4 @@ bool MessageAuthenticationCode::verify_mac(const byte mac[], size_t length) return same_mem(our_mac.data(), mac, length); } -#if defined(BOTAN_HAS_CBC_MAC) -BOTAN_REGISTER_NAMED_T(MessageAuthenticationCode, "CBC-MAC", CBC_MAC, CBC_MAC::make); -#endif - -#if defined(BOTAN_HAS_CMAC) -BOTAN_REGISTER_NAMED_T(MessageAuthenticationCode, "CMAC", CMAC, CMAC::make); -#endif - -#if defined(BOTAN_HAS_HMAC) -BOTAN_REGISTER_NAMED_T(MessageAuthenticationCode, "HMAC", HMAC, HMAC::make); -#endif - -#if defined(BOTAN_HAS_POLY1305) -BOTAN_REGISTER_T_NOARGS(MessageAuthenticationCode, Poly1305); -#endif - -#if defined(BOTAN_HAS_SIPHASH) -BOTAN_REGISTER_NAMED_T_2LEN(MessageAuthenticationCode, SipHash, "SipHash", "base", 2, 4); -#endif - -#if defined(BOTAN_HAS_ANSI_X919_MAC) -BOTAN_REGISTER_NAMED_T(MessageAuthenticationCode, "X9.19-MAC", ANSI_X919_MAC, make_new_T<ANSI_X919_MAC>); -#endif - } diff --git a/src/lib/mac/mac.h b/src/lib/mac/mac.h index 69b7ea581..f3befc512 100644 --- a/src/lib/mac/mac.h +++ b/src/lib/mac/mac.h @@ -10,7 +10,6 @@ #include <botan/buf_comp.h> #include <botan/sym_algo.h> -#include <botan/scan_name.h> #include <string> namespace Botan { @@ -22,8 +21,6 @@ class BOTAN_DLL MessageAuthenticationCode : public Buffered_Computation, public SymmetricAlgorithm { public: - typedef SCAN_Name Spec; - /** * Create an instance based on a name * If provider is empty then best available is chosen. @@ -31,15 +28,27 @@ class BOTAN_DLL MessageAuthenticationCode : public Buffered_Computation, * @param provider provider implementation to use * @return a null pointer if the algo/provider combination cannot be found */ - static std::unique_ptr<MessageAuthenticationCode> create(const std::string& algo_spec, - const std::string& provider = ""); + static std::unique_ptr<MessageAuthenticationCode> + create(const std::string& algo_spec, + const std::string& provider = ""); + + /* + * Create an instance based on a name + * If provider is empty then best available is chosen. + * @param algo_spec algorithm name + * @param provider provider implementation to use + * Throws a Lookup_Error if algo/provider combination cannot be found + */ + static std::unique_ptr<MessageAuthenticationCode> + create_or_throw(const std::string& algo_spec, + const std::string& provider = ""); /** * @return list of available providers for this algorithm, empty if not available */ static std::vector<std::string> providers(const std::string& algo_spec); - virtual ~MessageAuthenticationCode(); + virtual ~MessageAuthenticationCode() {} /** * Verify a MAC. |