diff options
author | Jack Lloyd <[email protected]> | 2015-09-17 17:08:01 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2015-09-17 17:08:01 -0400 |
commit | e2e0f8f2b595122c1f8acb3b3a46501f96a2b218 (patch) | |
tree | af5e031cb1f83fae45d59fc05c05185de1138f9a /src | |
parent | d83ef010522373a6f8ed3876c812b18b55513103 (diff) |
Handle dependencies re static linking. GH #279
Previously we were hanging on the type destructors to pull in
the relevant objects. However that fails in many simple cases
where the object is never deleted.
For every type involved in the algo registry add static create
and providers functions to access the algo registry. Modify
lookup.h to be inline and call those functions, and move
a few to sub-headers (eg, get_pbkdf going to pbkdf.h). So
accessing the registry involves going through the same file
that handles the initialization, so there is no way to end up
with missing objs.
Diffstat (limited to 'src')
35 files changed, 291 insertions, 192 deletions
diff --git a/src/lib/base/algo_registry.h b/src/lib/base/algo_registry.h index 8151551d3..e16522d94 100644 --- a/src/lib/base/algo_registry.h +++ b/src/lib/base/algo_registry.h @@ -7,7 +7,7 @@ #ifndef BOTAN_ALGO_REGISTRY_H__ #define BOTAN_ALGO_REGISTRY_H__ -#include <botan/lookup.h> +#include <botan/types.h> #include <functional> #include <stdexcept> #include <mutex> diff --git a/src/lib/base/info.txt b/src/lib/base/info.txt index 19eee6608..33d22a279 100644 --- a/src/lib/base/info.txt +++ b/src/lib/base/info.txt @@ -23,7 +23,6 @@ hash hex mac modes -pbkdf rng stream utils diff --git a/src/lib/base/lookup.cpp b/src/lib/base/lookup.cpp deleted file mode 100644 index 6655a4fb4..000000000 --- a/src/lib/base/lookup.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* -* Algorithm Retrieval -* (C) 1999-2007,2015 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#include <botan/lookup.h> -#include <botan/internal/algo_registry.h> -#include <botan/cipher_mode.h> -#include <botan/block_cipher.h> -#include <botan/stream_cipher.h> -#include <botan/hash.h> -#include <botan/mac.h> -#include <botan/pbkdf.h> - -namespace Botan { - -Transform* get_transform(const std::string& specstr, - const std::string& provider, - const std::string& dirstr) - { - Algo_Registry<Transform>::Spec spec(specstr, dirstr); - return Algo_Registry<Transform>::global_registry().make(spec, provider); - } - -BlockCipher* get_block_cipher(const std::string& algo_spec, const std::string& provider) - { - return make_a<BlockCipher>(algo_spec, provider); - } - -StreamCipher* get_stream_cipher(const std::string& algo_spec, const std::string& provider) - { - return make_a<StreamCipher>(algo_spec, provider); - } - -HashFunction* get_hash_function(const std::string& algo_spec, const std::string& provider) - { - return make_a<HashFunction>(algo_spec, provider); - } - -MessageAuthenticationCode* get_mac(const std::string& algo_spec, const std::string& provider) - { - return make_a<MessageAuthenticationCode>(algo_spec, provider); - } - -std::unique_ptr<BlockCipher> make_block_cipher(const std::string& algo_spec, - const std::string& provider) - { - if(auto x = get_block_cipher(algo_spec, provider)) - return std::unique_ptr<BlockCipher>(x); - throw Algorithm_Not_Found(algo_spec); - } - -std::unique_ptr<StreamCipher> make_stream_cipher(const std::string& algo_spec, - const std::string& provider) - { - if(auto x = get_stream_cipher(algo_spec, provider)) - return std::unique_ptr<StreamCipher>(x); - throw Algorithm_Not_Found(algo_spec); - } - -std::unique_ptr<HashFunction> make_hash_function(const std::string& algo_spec, - const std::string& provider) - { - if(auto x = get_hash_function(algo_spec, provider)) - return std::unique_ptr<HashFunction>(x); - throw Algorithm_Not_Found(algo_spec); - } - -std::unique_ptr<MessageAuthenticationCode> make_message_auth(const std::string& algo_spec, - const std::string& provider) - { - if(auto x = get_mac(algo_spec, provider)) - return std::unique_ptr<MessageAuthenticationCode>(x); - throw Algorithm_Not_Found(algo_spec); - } - -std::vector<std::string> get_block_cipher_providers(const std::string& algo_spec) - { - return providers_of<BlockCipher>(BlockCipher::Spec(algo_spec)); - } - -std::vector<std::string> get_stream_cipher_providers(const std::string& algo_spec) - { - return providers_of<StreamCipher>(StreamCipher::Spec(algo_spec)); - } - -std::vector<std::string> get_hash_function_providers(const std::string& algo_spec) - { - return providers_of<HashFunction>(HashFunction::Spec(algo_spec)); - } - -std::vector<std::string> get_mac_providers(const std::string& algo_spec) - { - return providers_of<MessageAuthenticationCode>(MessageAuthenticationCode::Spec(algo_spec)); - } - -/* -* Get a PBKDF algorithm by name -*/ -PBKDF* get_pbkdf(const std::string& algo_spec, const std::string& provider) - { - if(PBKDF* pbkdf = make_a<PBKDF>(algo_spec, provider)) - return pbkdf; - throw Algorithm_Not_Found(algo_spec); - } - -} diff --git a/src/lib/base/lookup.h b/src/lib/base/lookup.h index d5b17237e..9595d1f06 100644 --- a/src/lib/base/lookup.h +++ b/src/lib/base/lookup.h @@ -8,19 +8,17 @@ #ifndef BOTAN_LOOKUP_H__ #define BOTAN_LOOKUP_H__ -#include <botan/types.h> +#include <botan/block_cipher.h> +#include <botan/stream_cipher.h> +#include <botan/hash.h> +#include <botan/mac.h> +#include <botan/exceptn.h> #include <string> #include <vector> #include <memory> namespace Botan { -class BlockCipher; -class StreamCipher; -class HashFunction; -class MessageAuthenticationCode; -class PBKDF; - /* * Get an algorithm object * NOTE: these functions create and return new objects, letting the @@ -33,13 +31,25 @@ class PBKDF; * @param algo_spec the name of the desired block cipher * @return pointer to the block cipher object */ -BOTAN_DLL BlockCipher* get_block_cipher(const std::string& algo_spec, - const std::string& provider = ""); +inline BlockCipher* get_block_cipher(const std::string& algo_spec, + const std::string& provider = "") + { + return BlockCipher::create(algo_spec, provider).release(); + } -BOTAN_DLL std::unique_ptr<BlockCipher> make_block_cipher(const std::string& algo_spec, - const std::string& provider = ""); +inline std::unique_ptr<BlockCipher> make_block_cipher(const std::string& algo_spec, + const std::string& provider = "") + { + std::unique_ptr<BlockCipher> p(BlockCipher::create(algo_spec, provider)); + if(p) + return p; + throw Algorithm_Not_Found(algo_spec); + } -BOTAN_DLL std::vector<std::string> get_block_cipher_providers(const std::string& algo_spec); +inline std::vector<std::string> get_block_cipher_providers(const std::string& algo_spec) + { + return BlockCipher::providers(algo_spec); + } /** * Stream cipher factory method. @@ -47,13 +57,25 @@ BOTAN_DLL std::vector<std::string> get_block_cipher_providers(const std::string& * @param algo_spec the name of the desired stream cipher * @return pointer to the stream cipher object */ -BOTAN_DLL StreamCipher* get_stream_cipher(const std::string& algo_spec, - const std::string& provider = ""); +inline StreamCipher* get_stream_cipher(const std::string& algo_spec, + const std::string& provider = "") + { + return StreamCipher::create(algo_spec, provider).release(); + } -BOTAN_DLL std::unique_ptr<StreamCipher> make_stream_cipher(const std::string& algo_spec, - const std::string& provider = ""); +inline std::unique_ptr<StreamCipher> make_stream_cipher(const std::string& algo_spec, + const std::string& provider = "") + { + std::unique_ptr<StreamCipher> p(StreamCipher::create(algo_spec, provider)); + if(p) + return p; + throw Algorithm_Not_Found(algo_spec); + } -BOTAN_DLL std::vector<std::string> get_stream_cipher_providers(const std::string& algo_spec); +inline std::vector<std::string> get_stream_cipher_providers(const std::string& algo_spec) + { + return StreamCipher::providers(algo_spec); + } /** * Hash function factory method. @@ -61,11 +83,20 @@ BOTAN_DLL std::vector<std::string> get_stream_cipher_providers(const std::string * @param algo_spec the name of the desired hash function * @return pointer to the hash function object */ -BOTAN_DLL HashFunction* get_hash_function(const std::string& algo_spec, - const std::string& provider = ""); +inline HashFunction* get_hash_function(const std::string& algo_spec, + const std::string& provider = "") + { + return HashFunction::create(algo_spec, provider).release(); + } -BOTAN_DLL std::unique_ptr<HashFunction> make_hash_function(const std::string& algo_spec, - const std::string& provider = ""); +inline std::unique_ptr<HashFunction> make_hash_function(const std::string& algo_spec, + const std::string& provider = "") + { + std::unique_ptr<HashFunction> p(HashFunction::create(algo_spec, provider)); + if(p) + return p; + throw Algorithm_Not_Found(algo_spec); + } inline HashFunction* get_hash(const std::string& algo_spec, const std::string& provider = "") @@ -73,7 +104,10 @@ inline HashFunction* get_hash(const std::string& algo_spec, return get_hash_function(algo_spec, provider); } -BOTAN_DLL std::vector<std::string> get_hash_function_providers(const std::string& algo_spec); +inline std::vector<std::string> get_hash_function_providers(const std::string& algo_spec) + { + return HashFunction::providers(algo_spec); + } /** * MAC factory method. @@ -81,21 +115,25 @@ BOTAN_DLL std::vector<std::string> get_hash_function_providers(const std::string * @param algo_spec the name of the desired MAC * @return pointer to the MAC object */ -BOTAN_DLL MessageAuthenticationCode* get_mac(const std::string& algo_spec, - const std::string& provider = ""); - -BOTAN_DLL std::unique_ptr<MessageAuthenticationCode> make_message_auth(const std::string& algo_spec, - const std::string& provider = ""); +inline MessageAuthenticationCode* get_mac(const std::string& algo_spec, + const std::string& provider = "") + { + return MessageAuthenticationCode::create(algo_spec, provider).release(); + } -BOTAN_DLL std::vector<std::string> get_mac_providers(const std::string& algo_spec); +inline std::unique_ptr<MessageAuthenticationCode> make_message_auth(const std::string& algo_spec, + const std::string& provider = "") + { + std::unique_ptr<MessageAuthenticationCode> p(MessageAuthenticationCode::create(algo_spec, provider)); + if(p) + return p; + throw Algorithm_Not_Found(algo_spec); + } -/** -* Password based key derivation function factory method -* @param algo_spec the name of the desired PBKDF algorithm -* @return pointer to newly allocated object of that type -*/ -BOTAN_DLL PBKDF* get_pbkdf(const std::string& algo_spec, - const std::string& provider = ""); +inline std::vector<std::string> get_mac_providers(const std::string& algo_spec) + { + return MessageAuthenticationCode::providers(algo_spec); + } } diff --git a/src/lib/base/transform.cpp b/src/lib/base/transform.cpp new file mode 100644 index 000000000..8f05a33ad --- /dev/null +++ b/src/lib/base/transform.cpp @@ -0,0 +1,20 @@ +/* +* (C) 2015 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/internal/algo_registry.h> +#include <botan/transform.h> + +namespace Botan { + +Transform* get_transform(const std::string& specstr, + const std::string& provider, + const std::string& dirstr) + { + Algo_Registry<Transform>::Spec spec(specstr, dirstr); + return Algo_Registry<Transform>::global_registry().make(spec, provider); + } + +} diff --git a/src/lib/block/block_cipher.cpp b/src/lib/block/block_cipher.cpp index 616c6df5b..60e6d0db2 100644 --- a/src/lib/block/block_cipher.cpp +++ b/src/lib/block/block_cipher.cpp @@ -151,6 +151,17 @@ namespace Botan { BlockCipher::~BlockCipher() {} +std::unique_ptr<BlockCipher> BlockCipher::create(const std::string& algo_spec, + const std::string& provider) + { + return std::unique_ptr<BlockCipher>(make_a<BlockCipher>(algo_spec, provider)); + } + +std::vector<std::string> BlockCipher::providers(const std::string& algo_spec) + { + return providers_of<BlockCipher>(BlockCipher::Spec(algo_spec)); + } + #if defined(BOTAN_HAS_AES) BOTAN_REGISTER_BLOCK_CIPHER_NAMED_NOARGS(AES_128, "AES-128"); BOTAN_REGISTER_BLOCK_CIPHER_NAMED_NOARGS(AES_192, "AES-192"); diff --git a/src/lib/block/block_cipher.h b/src/lib/block/block_cipher.h index 3f017dc89..cea07ac2d 100644 --- a/src/lib/block/block_cipher.h +++ b/src/lib/block/block_cipher.h @@ -22,6 +22,19 @@ class BOTAN_DLL BlockCipher : public SymmetricAlgorithm typedef SCAN_Name Spec; /** + * Create an instance based on a name + * Will return a null pointer if the algo/provider combination cannot + * be found. If providers is empty then best available is chosen. + */ + static std::unique_ptr<BlockCipher> create(const std::string& algo_spec, + const std::string& provider = ""); + + /** + * Returns the list of available providers for this algorithm, empty if not available + */ + static std::vector<std::string> providers(const std::string& algo_spec); + + /** * @return block size of this algorithm */ virtual size_t block_size() const = 0; diff --git a/src/lib/hash/comb4p/comb4p.cpp b/src/lib/hash/comb4p/comb4p.cpp index ec39bbc31..ef5f74a38 100644 --- a/src/lib/hash/comb4p/comb4p.cpp +++ b/src/lib/hash/comb4p/comb4p.cpp @@ -5,7 +5,6 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/internal/hash_utils.h> #include <botan/comb4p.h> #include <botan/internal/xor_buf.h> #include <stdexcept> @@ -39,8 +38,8 @@ Comb4P* Comb4P::make(const Spec& spec) { if(spec.arg_count() == 2) { - std::unique_ptr<HashFunction> h1(make_hash_function(spec.arg(0))); - std::unique_ptr<HashFunction> h2(make_hash_function(spec.arg(1))); + std::unique_ptr<HashFunction> h1(HashFunction::create(spec.arg(0))); + std::unique_ptr<HashFunction> h2(HashFunction::create(spec.arg(1))); if(h1 && h2) return new Comb4P(h1.release(), h2.release()); diff --git a/src/lib/hash/hash.cpp b/src/lib/hash/hash.cpp index 723a7eba7..f965cfde8 100644 --- a/src/lib/hash/hash.cpp +++ b/src/lib/hash/hash.cpp @@ -91,6 +91,19 @@ namespace Botan { +std::unique_ptr<HashFunction> HashFunction::create(const std::string& algo_spec, + const std::string& provider) + { + return std::unique_ptr<HashFunction>(make_a<HashFunction>(algo_spec, provider)); + } + +std::vector<std::string> HashFunction::providers(const std::string& algo_spec) + { + return providers_of<HashFunction>(HashFunction::Spec(algo_spec)); + } + +HashFunction::HashFunction() {} + HashFunction::~HashFunction() {} #if defined(BOTAN_HAS_ADLER32) diff --git a/src/lib/hash/hash.h b/src/lib/hash/hash.h index 8406a4c0f..76c9d5cd7 100644 --- a/src/lib/hash/hash.h +++ b/src/lib/hash/hash.h @@ -23,10 +23,25 @@ class BOTAN_DLL HashFunction : public Buffered_Computation typedef SCAN_Name Spec; /** + * Create an instance based on a name + * Will return a null pointer if the algo/provider combination cannot + * be found. If providers is empty then best available is chosen. + */ + static std::unique_ptr<HashFunction> create(const std::string& algo_spec, + const std::string& provider = ""); + + /** + * Returns the list of available providers for this algorithm, empty if not available + */ + static std::vector<std::string> providers(const std::string& algo_spec); + + /** * @return new object representing the same algorithm as *this */ virtual HashFunction* clone() const = 0; + HashFunction(); + virtual ~HashFunction(); virtual void clear() = 0; diff --git a/src/lib/hash/par_hash/par_hash.cpp b/src/lib/hash/par_hash/par_hash.cpp index 12271640a..57140f0a8 100644 --- a/src/lib/hash/par_hash/par_hash.cpp +++ b/src/lib/hash/par_hash/par_hash.cpp @@ -17,7 +17,7 @@ Parallel* Parallel::make(const Spec& spec) for(size_t i = 0; i != spec.arg_count(); ++i) { - std::unique_ptr<HashFunction> h(get_hash_function(spec.arg(i))); + auto h = HashFunction::create(spec.arg(i)); if(!h) return nullptr; hashes.push_back(std::move(h)); diff --git a/src/lib/kdf/hkdf/hkdf.cpp b/src/lib/kdf/hkdf/hkdf.cpp index df6e49fad..d4c688afc 100644 --- a/src/lib/kdf/hkdf/hkdf.cpp +++ b/src/lib/kdf/hkdf/hkdf.cpp @@ -5,8 +5,8 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/internal/kdf_utils.h> #include <botan/hkdf.h> +#include <botan/lookup.h> namespace Botan { diff --git a/src/lib/kdf/info.txt b/src/lib/kdf/info.txt index 35032e159..68aa46895 100644 --- a/src/lib/kdf/info.txt +++ b/src/lib/kdf/info.txt @@ -7,7 +7,3 @@ base <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 836e9b982..3eba8a5cd 100644 --- a/src/lib/kdf/kdf.cpp +++ b/src/lib/kdf/kdf.cpp @@ -6,7 +6,7 @@ */ #include <botan/kdf.h> -#include <botan/internal/kdf_utils.h> +#include <botan/internal/algo_registry.h> #if defined(BOTAN_HAS_HKDF) #include <botan/hkdf.h> @@ -32,10 +32,29 @@ #include <botan/prf_x942.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() {} +std::unique_ptr<KDF> KDF::create(const std::string& algo_spec, + const std::string& provider) + { + return std::unique_ptr<KDF>(make_a<KDF>(algo_spec, provider)); + } + +std::vector<std::string> KDF::providers(const std::string& algo_spec) + { + return providers_of<KDF>(KDF::Spec(algo_spec)); + } + KDF* get_kdf(const std::string& algo_spec) { SCAN_Name request(algo_spec); @@ -43,9 +62,10 @@ KDF* get_kdf(const std::string& algo_spec) if(request.algo_name() == "Raw") return nullptr; // No KDF - if(KDF* kdf = make_a<KDF>(algo_spec)) - return kdf; - throw Algorithm_Not_Found(algo_spec); + auto kdf = KDF::create(algo_spec); + if(!kdf) + throw Algorithm_Not_Found(algo_spec); + return kdf.release(); } #if defined(BOTAN_HAS_HKDF) diff --git a/src/lib/kdf/kdf.h b/src/lib/kdf/kdf.h index 936e7c5f1..a8d4650e0 100644 --- a/src/lib/kdf/kdf.h +++ b/src/lib/kdf/kdf.h @@ -23,6 +23,19 @@ class BOTAN_DLL KDF public: virtual ~KDF(); + /** + * Create an instance based on a name + * Will return a null pointer if the algo/provider combination cannot + * be found. If providers is empty then best available is chosen. + */ + static std::unique_ptr<KDF> create(const std::string& algo_spec, + const std::string& provider = ""); + + /** + * Returns the list of available providers for this algorithm, empty if not available + */ + static std::vector<std::string> providers(const std::string& algo_spec); + virtual std::string name() const = 0; virtual size_t kdf(byte key[], size_t key_len, diff --git a/src/lib/kdf/kdf1/kdf1.cpp b/src/lib/kdf/kdf1/kdf1.cpp index c87bacd27..c7ea3c37e 100644 --- a/src/lib/kdf/kdf1/kdf1.cpp +++ b/src/lib/kdf/kdf1/kdf1.cpp @@ -5,7 +5,6 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/internal/kdf_utils.h> #include <botan/kdf1.h> namespace Botan { diff --git a/src/lib/kdf/kdf2/kdf2.cpp b/src/lib/kdf/kdf2/kdf2.cpp index 1b1c3638a..df2b7a91c 100644 --- a/src/lib/kdf/kdf2/kdf2.cpp +++ b/src/lib/kdf/kdf2/kdf2.cpp @@ -5,7 +5,6 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/internal/kdf_utils.h> #include <botan/kdf2.h> namespace Botan { diff --git a/src/lib/kdf/kdf_utils.h b/src/lib/kdf/kdf_utils.h deleted file mode 100644 index f67892437..000000000 --- a/src/lib/kdf/kdf_utils.h +++ /dev/null @@ -1,28 +0,0 @@ -/* -* KDF Utility Header -* (C) 2015 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#ifndef BOTAN_KDF_UTILS_H__ -#define BOTAN_KDF_UTILS_H__ - -#include <botan/kdf.h> -#include <botan/internal/algo_registry.h> -#include <botan/exceptn.h> -#include <botan/internal/xor_buf.h> - -namespace Botan { - -#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>)) - -} - -#endif diff --git a/src/lib/kdf/prf_tls/prf_tls.cpp b/src/lib/kdf/prf_tls/prf_tls.cpp index ef130d5ba..08a2a0899 100644 --- a/src/lib/kdf/prf_tls/prf_tls.cpp +++ b/src/lib/kdf/prf_tls/prf_tls.cpp @@ -5,9 +5,10 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/internal/kdf_utils.h> #include <botan/prf_tls.h> #include <botan/hmac.h> +#include <botan/lookup.h> +#include <botan/internal/xor_buf.h> namespace Botan { diff --git a/src/lib/kdf/prf_x942/prf_x942.cpp b/src/lib/kdf/prf_x942/prf_x942.cpp index e8f234e49..443e207f2 100644 --- a/src/lib/kdf/prf_x942/prf_x942.cpp +++ b/src/lib/kdf/prf_x942/prf_x942.cpp @@ -5,12 +5,12 @@ * 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> #include <botan/hash.h> #include <botan/loadstor.h> +#include <botan/lookup.h> #include <algorithm> namespace Botan { diff --git a/src/lib/mac/cbc_mac/cbc_mac.cpp b/src/lib/mac/cbc_mac/cbc_mac.cpp index 29507f17b..70a7e4116 100644 --- a/src/lib/mac/cbc_mac/cbc_mac.cpp +++ b/src/lib/mac/cbc_mac/cbc_mac.cpp @@ -7,6 +7,7 @@ #include <botan/internal/mac_utils.h> #include <botan/cbc_mac.h> +#include <botan/lookup.h> namespace Botan { diff --git a/src/lib/mac/cmac/cmac.cpp b/src/lib/mac/cmac/cmac.cpp index 85c19c19f..f3f6c6296 100644 --- a/src/lib/mac/cmac/cmac.cpp +++ b/src/lib/mac/cmac/cmac.cpp @@ -7,6 +7,7 @@ #include <botan/internal/mac_utils.h> #include <botan/cmac.h> +#include <botan/lookup.h> namespace Botan { diff --git a/src/lib/mac/hmac/hmac.cpp b/src/lib/mac/hmac/hmac.cpp index 2cd512746..94f455c56 100644 --- a/src/lib/mac/hmac/hmac.cpp +++ b/src/lib/mac/hmac/hmac.cpp @@ -8,6 +8,7 @@ #include <botan/internal/mac_utils.h> #include <botan/hmac.h> +#include <botan/lookup.h> namespace Botan { diff --git a/src/lib/mac/mac.cpp b/src/lib/mac/mac.cpp index af59bd4c6..da94a1daf 100644 --- a/src/lib/mac/mac.cpp +++ b/src/lib/mac/mac.cpp @@ -35,6 +35,17 @@ namespace Botan { +std::unique_ptr<MessageAuthenticationCode> MessageAuthenticationCode::create(const std::string& algo_spec, + const std::string& provider) + { + return std::unique_ptr<MessageAuthenticationCode>(make_a<MessageAuthenticationCode>(algo_spec, provider)); + } + +std::vector<std::string> MessageAuthenticationCode::providers(const std::string& algo_spec) + { + return providers_of<MessageAuthenticationCode>(MessageAuthenticationCode::Spec(algo_spec)); + } + MessageAuthenticationCode::~MessageAuthenticationCode() {} /* diff --git a/src/lib/mac/mac.h b/src/lib/mac/mac.h index 28894bbcd..6f18fce19 100644 --- a/src/lib/mac/mac.h +++ b/src/lib/mac/mac.h @@ -24,6 +24,19 @@ class BOTAN_DLL MessageAuthenticationCode : public Buffered_Computation, public: typedef SCAN_Name Spec; + /** + * Create an instance based on a name + * Will return a null pointer if the algo/provider combination cannot + * be found. If providers is empty then best available is chosen. + */ + static std::unique_ptr<MessageAuthenticationCode> create(const std::string& algo_spec, + const std::string& provider = ""); + + /** + * Returns the list of available providers for this algorithm, empty if not available + */ + static std::vector<std::string> providers(const std::string& algo_spec); + virtual ~MessageAuthenticationCode(); /** diff --git a/src/lib/mac/x919_mac/x919_mac.cpp b/src/lib/mac/x919_mac/x919_mac.cpp index ce7c38ebb..a58b07248 100644 --- a/src/lib/mac/x919_mac/x919_mac.cpp +++ b/src/lib/mac/x919_mac/x919_mac.cpp @@ -7,6 +7,7 @@ #include <botan/internal/mac_utils.h> #include <botan/x919_mac.h> +#include <botan/lookup.h> namespace Botan { diff --git a/src/lib/modes/aead/aead.cpp b/src/lib/modes/aead/aead.cpp index c101480b4..600e0eb18 100644 --- a/src/lib/modes/aead/aead.cpp +++ b/src/lib/modes/aead/aead.cpp @@ -6,6 +6,7 @@ #include <botan/internal/mode_utils.h> #include <botan/aead.h> +#include <botan/lookup.h> #if defined(BOTAN_HAS_AEAD_CCM) #include <botan/ccm.h> diff --git a/src/lib/modes/mode_utils.h b/src/lib/modes/mode_utils.h index 53aa41745..d8c185f7a 100644 --- a/src/lib/modes/mode_utils.h +++ b/src/lib/modes/mode_utils.h @@ -10,6 +10,7 @@ #include <botan/cipher_mode.h> #include <botan/internal/algo_registry.h> +#include <botan/lookup.h> #include <botan/block_cipher.h> #include <botan/loadstor.h> #include <botan/internal/xor_buf.h> diff --git a/src/lib/pbkdf/pbkdf.cpp b/src/lib/pbkdf/pbkdf.cpp index f11fbc44d..6d7a6542f 100644 --- a/src/lib/pbkdf/pbkdf.cpp +++ b/src/lib/pbkdf/pbkdf.cpp @@ -30,6 +30,19 @@ BOTAN_REGISTER_PBKDF_1HASH(PKCS5_PBKDF1, "PBKDF1"); BOTAN_REGISTER_NAMED_T(PBKDF, "PBKDF2", PKCS5_PBKDF2, PKCS5_PBKDF2::make); #endif +PBKDF::~PBKDF() {} + +std::unique_ptr<PBKDF> PBKDF::create(const std::string& algo_spec, + const std::string& provider) + { + return std::unique_ptr<PBKDF>(make_a<PBKDF>(algo_spec, provider)); + } + +std::vector<std::string> PBKDF::providers(const std::string& algo_spec) + { + return providers_of<PBKDF>(PBKDF::Spec(algo_spec)); + } + void PBKDF::pbkdf_timed(byte out[], size_t out_len, const std::string& passphrase, const byte salt[], size_t salt_len, diff --git a/src/lib/pbkdf/pbkdf.h b/src/lib/pbkdf/pbkdf.h index 5f6cd904c..1a1299c75 100644 --- a/src/lib/pbkdf/pbkdf.h +++ b/src/lib/pbkdf/pbkdf.h @@ -10,6 +10,8 @@ #include <botan/symkey.h> #include <botan/scan_name.h> +#include <botan/scan_name.h> +#include <botan/exceptn.h> #include <chrono> namespace Botan { @@ -22,8 +24,18 @@ namespace Botan { class BOTAN_DLL PBKDF { public: + /** + * Create an instance based on a name + * Will return a null pointer if the algo/provider combination cannot + * be found. If providers is empty then best available is chosen. + */ + static std::unique_ptr<PBKDF> create(const std::string& algo_spec, + const std::string& provider = ""); - virtual ~PBKDF() {} + /** + * Returns the list of available providers for this algorithm, empty if not available + */ + static std::vector<std::string> providers(const std::string& algo_spec); typedef SCAN_Name Spec; @@ -34,6 +46,8 @@ class BOTAN_DLL PBKDF virtual std::string name() const = 0; + virtual ~PBKDF(); + /** * Derive a key from a passphrase for a number of iterations * specified by either iterations or if iterations == 0 then @@ -147,6 +161,20 @@ class BOTAN_DLL PBKDF } }; +/** +* Password based key derivation function factory method +* @param algo_spec the name of the desired PBKDF algorithm +* @return pointer to newly allocated object of that type +*/ +inline PBKDF* get_pbkdf(const std::string& algo_spec, + const std::string& provider = "") + { + std::unique_ptr<PBKDF> p(PBKDF::create(algo_spec, provider)); + if(p) + return p.release(); + throw Algorithm_Not_Found(algo_spec); + } + } #endif diff --git a/src/lib/pk_pad/emsa_pkcs1/emsa_pkcs1.cpp b/src/lib/pk_pad/emsa_pkcs1/emsa_pkcs1.cpp index d928511d3..ecb7a6f29 100644 --- a/src/lib/pk_pad/emsa_pkcs1/emsa_pkcs1.cpp +++ b/src/lib/pk_pad/emsa_pkcs1/emsa_pkcs1.cpp @@ -8,6 +8,7 @@ #include <botan/internal/pad_utils.h> #include <botan/emsa_pkcs1.h> #include <botan/hash_id.h> +#include <botan/lookup.h> namespace Botan { diff --git a/src/lib/stream/ctr/ctr.cpp b/src/lib/stream/ctr/ctr.cpp index d025a03d3..e6d4b9d3e 100644 --- a/src/lib/stream/ctr/ctr.cpp +++ b/src/lib/stream/ctr/ctr.cpp @@ -7,6 +7,7 @@ #include <botan/internal/stream_utils.h> #include <botan/ctr.h> +#include <botan/lookup.h> namespace Botan { diff --git a/src/lib/stream/ofb/ofb.cpp b/src/lib/stream/ofb/ofb.cpp index 73ffef980..1979c676f 100644 --- a/src/lib/stream/ofb/ofb.cpp +++ b/src/lib/stream/ofb/ofb.cpp @@ -7,6 +7,7 @@ #include <botan/internal/stream_utils.h> #include <botan/ofb.h> +#include <botan/lookup.h> namespace Botan { diff --git a/src/lib/stream/stream_cipher.cpp b/src/lib/stream/stream_cipher.cpp index 2f1538914..3b8d35bc7 100644 --- a/src/lib/stream/stream_cipher.cpp +++ b/src/lib/stream/stream_cipher.cpp @@ -30,6 +30,18 @@ namespace Botan { +std::unique_ptr<StreamCipher> StreamCipher::create(const std::string& algo_spec, + const std::string& provider) + { + return std::unique_ptr<StreamCipher>(make_a<StreamCipher>(algo_spec, provider)); + } + +std::vector<std::string> StreamCipher::providers(const std::string& algo_spec) + { + return providers_of<StreamCipher>(StreamCipher::Spec(algo_spec)); + } + +StreamCipher::StreamCipher() {} StreamCipher::~StreamCipher() {} void StreamCipher::set_iv(const byte[], size_t iv_len) diff --git a/src/lib/stream/stream_cipher.h b/src/lib/stream/stream_cipher.h index 5500bca49..68c97f1c1 100644 --- a/src/lib/stream/stream_cipher.h +++ b/src/lib/stream/stream_cipher.h @@ -23,6 +23,19 @@ class BOTAN_DLL StreamCipher : public SymmetricAlgorithm typedef SCAN_Name Spec; /** + * Create an instance based on a name + * Will return a null pointer if the algo/provider combination cannot + * be found. If providers is empty then best available is chosen. + */ + static std::unique_ptr<StreamCipher> create(const std::string& algo_spec, + const std::string& provider = ""); + + /** + * Returns the list of available providers for this algorithm, empty if not available + */ + static std::vector<std::string> providers(const std::string& algo_spec); + + /** * Encrypt or decrypt a message * @param in the plaintext * @param out the byte array to hold the output, i.e. the ciphertext @@ -68,6 +81,7 @@ class BOTAN_DLL StreamCipher : public SymmetricAlgorithm */ virtual StreamCipher* clone() const = 0; + StreamCipher(); virtual ~StreamCipher(); }; |