diff options
-rw-r--r-- | src/libstate/algo_factory.cpp | 59 | ||||
-rw-r--r-- | src/libstate/algo_factory.h | 49 | ||||
-rw-r--r-- | src/libstate/engine/engine.cpp | 16 | ||||
-rw-r--r-- | src/libstate/info.txt | 2 | ||||
-rw-r--r-- | src/libstate/libstate.cpp | 50 | ||||
-rw-r--r-- | src/libstate/libstate.h | 50 | ||||
-rw-r--r-- | src/libstate/lookup.cpp | 19 |
7 files changed, 160 insertions, 85 deletions
diff --git a/src/libstate/algo_factory.cpp b/src/libstate/algo_factory.cpp new file mode 100644 index 000000000..4e5bfea85 --- /dev/null +++ b/src/libstate/algo_factory.cpp @@ -0,0 +1,59 @@ +/* +Algorithm Factory +(C) 2008 Jack Lloyd +*/ + +#include <botan/libstate.h> +#include <botan/stl_util.h> +#include <botan/engine.h> +#include <algorithm> + +namespace Botan { + +/** +* Delete all engines +*/ +Algorithm_Factory::~Algorithm_Factory() + { + std::for_each(engines.begin(), engines.end(), del_fun<Engine>()); + engines.clear(); + + delete mutex; + } + +/** +* Add a new engine to the list +*/ +void Algorithm_Factory::add_engine(Engine* engine) + { + Mutex_Holder lock(mutex); + engines.insert(engines.begin(), engine); + } + +/************************************************* +* Get an engine out of the list * +*************************************************/ +Engine* Algorithm_Factory::get_engine_n(u32bit n) const + { + Mutex_Holder lock(mutex); + + if(n >= engines.size()) + return 0; + return engines[n]; + } + +const HashFunction* Algorithm_Factory::prototype_hash_function(const std::string& algo_spec) + { + Mutex_Holder lock(mutex); + + for(u32bit i = 0; i != engines.size(); ++i) + { + const HashFunction* algo = engines[i]->hash(algo_spec); + if(algo) + return algo; + } + + return 0; + } + +} diff --git a/src/libstate/algo_factory.h b/src/libstate/algo_factory.h new file mode 100644 index 000000000..5a00f3b55 --- /dev/null +++ b/src/libstate/algo_factory.h @@ -0,0 +1,49 @@ +/** +* Algorithm Factory +* (C) 2008 Jack Lloyd +*/ + +#ifndef BOTAN_ALGORITHM_FACTORY_H__ +#define BOTAN_ALGORITHM_FACTORY_H__ + +#include <botan/mutex.h> +#include <botan/hash.h> +#include <string> +#include <vector> + +namespace Botan { + +/** +* Algorithm Factory +*/ +class BOTAN_DLL Algorithm_Factory + { + public: + Algorithm_Factory(Mutex* m) : mutex(m) {} + ~Algorithm_Factory(); + + const HashFunction* prototype_hash_function(const std::string& algo_spec); + + void add_engine(class Engine*); + + class BOTAN_DLL Engine_Iterator + { + public: + class Engine* next() { return af.get_engine_n(n++); } + Engine_Iterator(const Algorithm_Factory& a) : af(a) { n = 0; } + private: + const Algorithm_Factory& af; + u32bit n; + }; + friend class Engine_Iterator; + + private: + class Engine* get_engine_n(u32bit) const; + + Mutex* mutex; + std::vector<class Engine*> engines; + }; + +} + +#endif diff --git a/src/libstate/engine/engine.cpp b/src/libstate/engine/engine.cpp index 95111014e..c55ff2a78 100644 --- a/src/libstate/engine/engine.cpp +++ b/src/libstate/engine/engine.cpp @@ -163,7 +163,7 @@ IF_Operation* if_op(const BigInt& e, const BigInt& n, const BigInt& d, const BigInt& p, const BigInt& q, const BigInt& d1, const BigInt& d2, const BigInt& c) { - Library_State::Engine_Iterator i(global_state()); + Algorithm_Factory::Engine_Iterator i(global_state().algo_factory()); while(const Engine* engine = i.next()) { @@ -182,7 +182,7 @@ IF_Operation* if_op(const BigInt& e, const BigInt& n, const BigInt& d, *************************************************/ DSA_Operation* dsa_op(const DL_Group& group, const BigInt& y, const BigInt& x) { - Library_State::Engine_Iterator i(global_state()); + Algorithm_Factory::Engine_Iterator i(global_state().algo_factory()); while(const Engine* engine = i.next()) { @@ -201,7 +201,7 @@ DSA_Operation* dsa_op(const DL_Group& group, const BigInt& y, const BigInt& x) *************************************************/ NR_Operation* nr_op(const DL_Group& group, const BigInt& y, const BigInt& x) { - Library_State::Engine_Iterator i(global_state()); + Algorithm_Factory::Engine_Iterator i(global_state().algo_factory()); while(const Engine* engine = i.next()) { @@ -220,7 +220,7 @@ NR_Operation* nr_op(const DL_Group& group, const BigInt& y, const BigInt& x) *************************************************/ ELG_Operation* elg_op(const DL_Group& group, const BigInt& y, const BigInt& x) { - Library_State::Engine_Iterator i(global_state()); + Algorithm_Factory::Engine_Iterator i(global_state().algo_factory()); while(const Engine* engine = i.next()) { @@ -239,7 +239,7 @@ ELG_Operation* elg_op(const DL_Group& group, const BigInt& y, const BigInt& x) *************************************************/ DH_Operation* dh_op(const DL_Group& group, const BigInt& x) { - Library_State::Engine_Iterator i(global_state()); + Algorithm_Factory::Engine_Iterator i(global_state().algo_factory()); while(const Engine* engine = i.next()) { @@ -260,7 +260,7 @@ ECDSA_Operation* ecdsa_op(const EC_Domain_Params& dom_pars, const BigInt& priv_key, const PointGFp& pub_key) { - Library_State::Engine_Iterator i(global_state()); + Algorithm_Factory::Engine_Iterator i(global_state().algo_factory()); while(const Engine* engine = i.next()) { @@ -281,7 +281,7 @@ ECKAEG_Operation* eckaeg_op(const EC_Domain_Params& dom_pars, const BigInt& priv_key, const PointGFp& pub_key) { - Library_State::Engine_Iterator i(global_state()); + Algorithm_Factory::Engine_Iterator i(global_state().algo_factory()); while(const Engine* engine = i.next()) { @@ -299,7 +299,7 @@ ECKAEG_Operation* eckaeg_op(const EC_Domain_Params& dom_pars, *************************************************/ Modular_Exponentiator* mod_exp(const BigInt& n, Power_Mod::Usage_Hints hints) { - Library_State::Engine_Iterator i(global_state()); + Algorithm_Factory::Engine_Iterator i(global_state().algo_factory()); while(const Engine* engine = i.next()) { diff --git a/src/libstate/info.txt b/src/libstate/info.txt index 4e9ab1bfd..ac4a9561e 100644 --- a/src/libstate/info.txt +++ b/src/libstate/info.txt @@ -13,6 +13,8 @@ system_alloc </requires> <add> +algo_factory.cpp +algo_factory.h botan.h get_enc.cpp init.h diff --git a/src/libstate/libstate.cpp b/src/libstate/libstate.cpp index 60bb24fee..bc0f6794b 100644 --- a/src/libstate/libstate.cpp +++ b/src/libstate/libstate.cpp @@ -56,14 +56,6 @@ Library_State* swap_global_state(Library_State* new_state) } /************************************************* -* Increment the Engine iterator * -*************************************************/ -Engine* Library_State::Engine_Iterator::next() - { - return lib.get_engine_n(n++); - } - -/************************************************* * Get a new mutex object * *************************************************/ Mutex* Library_State::get_mutex() const @@ -123,27 +115,6 @@ void Library_State::set_default_allocator(const std::string& type) } /************************************************* -* Get an engine out of the list * -*************************************************/ -Engine* Library_State::get_engine_n(u32bit n) const - { - Mutex_Holder lock(engine_lock); - - if(n >= engines.size()) - return 0; - return engines[n]; - } - -/************************************************* -* Add a new engine to the list * -*************************************************/ -void Library_State::add_engine(Engine* engine) - { - Mutex_Holder lock(engine_lock); - engines.insert(engines.begin(), engine); - } - -/************************************************* * Get a configuration value * *************************************************/ std::string Library_State::get(const std::string& section, @@ -219,6 +190,16 @@ std::string Library_State::option(const std::string& key) const return get("conf", key); } +/** +Return a reference to the Algorithm_Factory +*/ +Algorithm_Factory& Library_State::algo_factory() + { + if(!algorithm_factory) + throw Invalid_State("Uninitialized in Library_State::algo_factory"); + return *algorithm_factory; + } + /************************************************* * Load a set of modules * *************************************************/ @@ -234,7 +215,6 @@ void Library_State::initialize(const InitializerOptions& args, throw Invalid_State("Could not acquire a mutex module at init"); allocator_lock = get_mutex(); - engine_lock = get_mutex(); config_lock = get_mutex(); cached_default_allocator = 0; @@ -247,9 +227,11 @@ void Library_State::initialize(const InitializerOptions& args, load_default_config(); + algorithm_factory = new Algorithm_Factory(get_mutex()); + std::vector<Engine*> mod_engines = modules.engines(); for(u32bit j = 0; j != mod_engines.size(); ++j) - engines.push_back(mod_engines[j]); + algorithm_factory->add_engine(mod_engines[j]); #if defined(BOTAN_HAS_SELFTEST) if(args.fips_mode() || args.self_test()) @@ -266,8 +248,9 @@ void Library_State::initialize(const InitializerOptions& args, Library_State::Library_State() { mutex_factory = 0; - allocator_lock = engine_lock = config_lock = 0; + allocator_lock = config_lock = 0; cached_default_allocator = 0; + algorithm_factory = 0; } /************************************************* @@ -275,7 +258,7 @@ Library_State::Library_State() *************************************************/ Library_State::~Library_State() { - std::for_each(engines.begin(), engines.end(), del_fun<Engine>()); + delete algorithm_factory; cached_default_allocator = 0; @@ -286,7 +269,6 @@ Library_State::~Library_State() } delete allocator_lock; - delete engine_lock; delete mutex_factory; delete config_lock; } diff --git a/src/libstate/libstate.h b/src/libstate/libstate.h index 606a091d8..db0198923 100644 --- a/src/libstate/libstate.h +++ b/src/libstate/libstate.h @@ -9,6 +9,7 @@ #include <botan/types.h> #include <botan/init.h> #include <botan/allocate.h> +#include <botan/algo_factory.h> #include <string> #include <vector> @@ -29,18 +30,7 @@ class BOTAN_DLL Library_State void load(Modules&); - void add_engine(class Engine*); - - class BOTAN_DLL Engine_Iterator - { - public: - class Engine* next(); - Engine_Iterator(const Library_State& l) : lib(l) { n = 0; } - private: - const Library_State& lib; - u32bit n; - }; - friend class Engine_Iterator; + Algorithm_Factory& algo_factory(); Allocator* get_allocator(const std::string& = "") const; void add_allocator(Allocator*); @@ -65,29 +55,24 @@ class BOTAN_DLL Library_State */ bool is_set(const std::string& section, const std::string& key) const; - /** - * Set a configuration parameter. - * @param section the section of the desired key - * @param key the desired keys name - * @param overwrite if set to true, the parameters value - * will be overwritten even if it is already set, otherwise - * no existing values will be overwritten. - */ + /** + * Set a configuration parameter. + * @param section the section of the desired key + * @param key the desired keys name + * @param overwrite if set to true, the parameters value + * will be overwritten even if it is already set, otherwise + * no existing values will be overwritten. + */ void set(const std::string& section, const std::string& key, const std::string& value, bool overwrite = true); - /** - * Get a parameters value out of the "conf" section ( - * referred to as option). - * @param key the desired keys name - */ - std::string option(const std::string& key) const; - /** - * Set an option. - * @param key the key of the option to set - * @param value the value to set + * Get a parameters value out of the "conf" section ( + * referred to as option). + * @param key the desired keys name */ + std::string option(const std::string& key) const; + /** * Set an option. * @param key the key of the option to set @@ -116,8 +101,6 @@ class BOTAN_DLL Library_State Library_State(const Library_State&) {} Library_State& operator=(const Library_State&) { return (*this); } - class Engine* get_engine_n(u32bit) const; - class Mutex_Factory* mutex_factory; std::map<std::string, std::string> config; @@ -128,8 +111,7 @@ class BOTAN_DLL Library_State mutable Allocator* cached_default_allocator; std::vector<Allocator*> allocators; - class Mutex* engine_lock; - std::vector<class Engine*> engines; + Algorithm_Factory* algorithm_factory; }; /************************************************* diff --git a/src/libstate/lookup.cpp b/src/libstate/lookup.cpp index 956f508e4..dd18420b5 100644 --- a/src/libstate/lookup.cpp +++ b/src/libstate/lookup.cpp @@ -220,7 +220,7 @@ u32bit keylength_multiple_of(const std::string& name) const BlockCipher* retrieve_block_cipher(Library_State& libstate, const std::string& name) { - Library_State::Engine_Iterator i(libstate); + Algorithm_Factory::Engine_Iterator i(libstate.algo_factory()); while(const Engine* engine = i.next()) { @@ -238,7 +238,7 @@ const BlockCipher* retrieve_block_cipher(Library_State& libstate, const StreamCipher* retrieve_stream_cipher(Library_State& libstate, const std::string& name) { - Library_State::Engine_Iterator i(libstate); + Algorithm_Factory::Engine_Iterator i(libstate.algo_factory()); while(const Engine* engine = i.next()) { @@ -256,7 +256,8 @@ const StreamCipher* retrieve_stream_cipher(Library_State& libstate, const HashFunction* retrieve_hash(Library_State& libstate, const std::string& name) { - Library_State::Engine_Iterator i(libstate); + //return libstate.algo_factory().prototype_hash_function(name); + Algorithm_Factory::Engine_Iterator i(libstate.algo_factory()); while(const Engine* engine = i.next()) { @@ -274,7 +275,7 @@ const HashFunction* retrieve_hash(Library_State& libstate, const MessageAuthenticationCode* retrieve_mac(Library_State& libstate, const std::string& name) { - Library_State::Engine_Iterator i(libstate); + Algorithm_Factory::Engine_Iterator i(libstate.algo_factory()); while(const Engine* engine = i.next()) { @@ -291,7 +292,7 @@ const MessageAuthenticationCode* retrieve_mac(Library_State& libstate, *************************************************/ void add_algorithm(Library_State& libstate, BlockCipher* algo) { - Library_State::Engine_Iterator i(libstate); + Algorithm_Factory::Engine_Iterator i(libstate.algo_factory()); while(Engine* engine = i.next()) { @@ -310,7 +311,7 @@ void add_algorithm(Library_State& libstate, BlockCipher* algo) *************************************************/ void add_algorithm(Library_State& libstate, StreamCipher* algo) { - Library_State::Engine_Iterator i(libstate); + Algorithm_Factory::Engine_Iterator i(libstate.algo_factory()); while(Engine* engine = i.next()) { @@ -329,7 +330,7 @@ void add_algorithm(Library_State& libstate, StreamCipher* algo) *************************************************/ void add_algorithm(Library_State& libstate, HashFunction* algo) { - Library_State::Engine_Iterator i(libstate); + Algorithm_Factory::Engine_Iterator i(libstate.algo_factory()); while(Engine* engine = i.next()) { @@ -349,7 +350,7 @@ void add_algorithm(Library_State& libstate, HashFunction* algo) void add_algorithm(Library_State& libstate, MessageAuthenticationCode* algo) { - Library_State::Engine_Iterator i(libstate); + Algorithm_Factory::Engine_Iterator i(libstate.algo_factory()); while(Engine* engine = i.next()) { @@ -369,7 +370,7 @@ void add_algorithm(Library_State& libstate, Keyed_Filter* get_cipher(const std::string& algo_spec, Cipher_Dir direction) { - Library_State::Engine_Iterator i(global_state()); + Algorithm_Factory::Engine_Iterator i(global_state().algo_factory()); while(Engine* engine = i.next()) { |