diff options
Diffstat (limited to 'src/engine/engine.cpp')
-rw-r--r-- | src/engine/engine.cpp | 497 |
1 files changed, 243 insertions, 254 deletions
diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 75b49b6a6..e52304c59 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -1,15 +1,255 @@ /************************************************* -* Engine Source File * +* Basic No-Op Engine Source File * * (C) 1999-2007 Jack Lloyd * *************************************************/ #include <botan/engine.h> #include <botan/libstate.h> -#include <botan/lookup.h> -#include <botan/look_add.h> +#include <botan/stl_util.h> +#include <botan/mode_pad.h> +#include <botan/s2k.h> namespace Botan { +namespace { + +/************************************************* +* Algorithm Cache * +*************************************************/ +template<typename T> +class Algorithm_Cache_Impl : public Engine::Algorithm_Cache<T> + { + public: + T* get(const std::string& name) const + { + Mutex_Holder lock(mutex); + return search_map(mappings, name); + } + + void add(T* algo, const std::string& index_name = "") const + { + if(!algo) + return; + + Mutex_Holder lock(mutex); + + const std::string name = + (index_name != "" ? index_name : algo->name()); + + if(mappings.find(name) != mappings.end()) + delete mappings[name]; + mappings[name] = algo; + } + + Algorithm_Cache_Impl() + { + mutex = global_state().get_mutex(); + } + + ~Algorithm_Cache_Impl() + { + typename std::map<std::string, T*>::iterator i = mappings.begin(); + + while(i != mappings.end()) + { + delete i->second; + ++i; + } + delete mutex; + } + private: + Mutex* mutex; + mutable std::map<std::string, T*> mappings; + }; + +} + +/************************************************* +* Acquire a BlockCipher * +*************************************************/ +const BlockCipher* Engine::block_cipher(const std::string& name) const + { + return lookup_algo(cache_of_bc, global_state().deref_alias(name), + this, &Engine::find_block_cipher); + } + +/************************************************* +* Acquire a StreamCipher * +*************************************************/ +const StreamCipher* Engine::stream_cipher(const std::string& name) const + { + return lookup_algo(cache_of_sc, global_state().deref_alias(name), + this, &Engine::find_stream_cipher); + } + +/************************************************* +* Acquire a HashFunction * +*************************************************/ +const HashFunction* Engine::hash(const std::string& name) const + { + return lookup_algo(cache_of_hf, global_state().deref_alias(name), + this, &Engine::find_hash); + } + +/************************************************* +* Acquire a MessageAuthenticationCode * +*************************************************/ +const MessageAuthenticationCode* Engine::mac(const std::string& name) const + { + return lookup_algo(cache_of_mac, global_state().deref_alias(name), + this, &Engine::find_mac); + } + +/************************************************* +* Acquire a S2K object * +*************************************************/ +const S2K* Engine::s2k(const std::string& name) const + { + return lookup_algo(cache_of_s2k, global_state().deref_alias(name), + this, &Engine::find_s2k); + } + +/************************************************* +* Acquire a cipher padding object * +*************************************************/ +const BlockCipherModePaddingMethod* +Engine::bc_pad(const std::string& name) const + { + return lookup_algo(cache_of_bc_pad, global_state().deref_alias(name), + this, &Engine::find_bc_pad); + } + +/************************************************* +* Add a block cipher to the lookup table * +*************************************************/ +void Engine::add_algorithm(BlockCipher* algo) const + { + cache_of_bc->add(algo); + } + +/************************************************* +* Add a stream cipher to the lookup table * +*************************************************/ +void Engine::add_algorithm(StreamCipher* algo) const + { + cache_of_sc->add(algo); + } + +/************************************************* +* Add a hash function to the lookup table * +*************************************************/ +void Engine::add_algorithm(HashFunction* algo) const + { + cache_of_hf->add(algo); + } + +/************************************************* +* Add a MAC to the lookup table * +*************************************************/ +void Engine::add_algorithm(MessageAuthenticationCode* algo) const + { + cache_of_mac->add(algo); + } + +/************************************************* +* Add a S2K to the lookup table * +*************************************************/ +void Engine::add_algorithm(S2K* algo) const + { + cache_of_s2k->add(algo); + } + +/************************************************* +* Add a cipher pad method to the lookup table * +*************************************************/ +void Engine::add_algorithm(BlockCipherModePaddingMethod* algo) const + { + cache_of_bc_pad->add(algo); + } + +/************************************************* +* Create an Engine * +*************************************************/ +Engine::Engine() + { + cache_of_bc = new Algorithm_Cache_Impl<BlockCipher>(); + cache_of_sc = new Algorithm_Cache_Impl<StreamCipher>(); + cache_of_hf = new Algorithm_Cache_Impl<HashFunction>(); + cache_of_mac = new Algorithm_Cache_Impl<MessageAuthenticationCode>(); + cache_of_s2k = new Algorithm_Cache_Impl<S2K>(); + cache_of_bc_pad = + new Algorithm_Cache_Impl<BlockCipherModePaddingMethod>(); + } + +/************************************************* +* Destroy an Engine * +*************************************************/ +Engine::~Engine() + { + delete cache_of_bc; + delete cache_of_sc; + delete cache_of_hf; + delete cache_of_mac; + delete cache_of_s2k; + delete cache_of_bc_pad; + } + +/************************************************* +* Basic No-Op Engine Implementation * +*************************************************/ +BlockCipher* Engine::find_block_cipher(const std::string&) const + { + return 0; + } + +/************************************************* +* Basic No-Op Engine Implementation * +*************************************************/ +StreamCipher* Engine::find_stream_cipher(const std::string&) const + { + return 0; + } + +/************************************************* +* Basic No-Op Engine Implementation * +*************************************************/ +HashFunction* Engine::find_hash(const std::string&) const + { + return 0; + } + +/************************************************* +* Basic No-Op Engine Implementation * +*************************************************/ +MessageAuthenticationCode* Engine::find_mac(const std::string&) const + { + return 0; + } + +/************************************************* +* Basic No-Op Engine Implementation * +*************************************************/ +S2K* Engine::find_s2k(const std::string&) const + { + return 0; + } + +/************************************************* +* Basic No-Op Engine Implementation * +*************************************************/ +BlockCipherModePaddingMethod* Engine::find_bc_pad(const std::string&) const + { + return 0; + } + +/************************************************* +* Basic No-Op Engine Implementation * +*************************************************/ +Keyed_Filter* Engine::get_cipher(const std::string&, Cipher_Dir) + { + return 0; + } + namespace Engine_Core { #if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) @@ -171,255 +411,4 @@ Modular_Exponentiator* mod_exp(const BigInt& n, Power_Mod::Usage_Hints hints) } -/************************************************* -* Acquire a block cipher * -*************************************************/ -const BlockCipher* retrieve_block_cipher(Library_State& libstate, - const std::string& name) - { - Library_State::Engine_Iterator i(libstate); - - while(const Engine* engine = i.next()) - { - const BlockCipher* algo = engine->block_cipher(name); - if(algo) - return algo; - } - - return 0; - } - -/************************************************* -* Acquire a stream cipher * -*************************************************/ -const StreamCipher* retrieve_stream_cipher(Library_State& libstate, - const std::string& name) - { - Library_State::Engine_Iterator i(libstate); - - while(const Engine* engine = i.next()) - { - const StreamCipher* algo = engine->stream_cipher(name); - if(algo) - return algo; - } - - return 0; - } - -/************************************************* -* Acquire a hash function * -*************************************************/ -const HashFunction* retrieve_hash(Library_State& libstate, - const std::string& name) - { - Library_State::Engine_Iterator i(libstate); - - while(const Engine* engine = i.next()) - { - const HashFunction* algo = engine->hash(name); - if(algo) - return algo; - } - - return 0; - } - -/************************************************* -* Acquire an authentication code * -*************************************************/ -const MessageAuthenticationCode* retrieve_mac(Library_State& libstate, - const std::string& name) - { - Library_State::Engine_Iterator i(libstate); - - while(const Engine* engine = i.next()) - { - const MessageAuthenticationCode* algo = engine->mac(name); - if(algo) - return algo; - } - - return 0; - } - -/************************************************* -* Acquire a string-to-key algorithm * -*************************************************/ -const S2K* retrieve_s2k(Library_State& libstate, - const std::string& name) - { - Library_State::Engine_Iterator i(libstate); - - while(const Engine* engine = i.next()) - { - const S2K* algo = engine->s2k(name); - if(algo) - return algo; - } - - return 0; - } - -/************************************************* -* Retrieve a block cipher padding method * -*************************************************/ -const BlockCipherModePaddingMethod* retrieve_bc_pad(Library_State& libstate, - const std::string& name) - { - Library_State::Engine_Iterator i(libstate); - - while(const Engine* engine = i.next()) - { - const BlockCipherModePaddingMethod* algo = engine->bc_pad(name); - if(algo) - return algo; - } - - return 0; - } - -/************************************************* -* Add a new block cipher * -*************************************************/ -void add_algorithm(Library_State& libstate, BlockCipher* algo) - { - Library_State::Engine_Iterator i(libstate); - - while(Engine* engine = i.next()) - { - if(engine->can_add_algorithms()) - { - engine->add_algorithm(algo); - return; - } - } - - throw Invalid_State("add_algorithm: Couldn't find the Default_Engine"); - } - -/************************************************* -* Add a new stream cipher * -*************************************************/ -void add_algorithm(Library_State& libstate, StreamCipher* algo) - { - Library_State::Engine_Iterator i(libstate); - - while(Engine* engine = i.next()) - { - if(engine->can_add_algorithms()) - { - engine->add_algorithm(algo); - return; - } - } - - throw Invalid_State("add_algorithm: Couldn't find the Default_Engine"); - } - -/************************************************* -* Add a new hash function * -*************************************************/ -void add_algorithm(Library_State& libstate, HashFunction* algo) - { - Library_State::Engine_Iterator i(libstate); - - while(Engine* engine = i.next()) - { - if(engine->can_add_algorithms()) - { - engine->add_algorithm(algo); - return; - } - } - - throw Invalid_State("add_algorithm: Couldn't find the Default_Engine"); - } - -/************************************************* -* Add a new authentication code * -*************************************************/ -void add_algorithm(Library_State& libstate, - MessageAuthenticationCode* algo) - { - Library_State::Engine_Iterator i(libstate); - - while(Engine* engine = i.next()) - { - if(engine->can_add_algorithms()) - { - engine->add_algorithm(algo); - return; - } - } - - throw Invalid_State("add_algorithm: Couldn't find the Default_Engine"); - } - -/************************************************* -* Add a padding method to the lookup table * -*************************************************/ -void add_algorithm(Library_State& libstate, - BlockCipherModePaddingMethod* algo) - { - Library_State::Engine_Iterator i(libstate); - - while(Engine* engine = i.next()) - { - if(engine->can_add_algorithms()) - { - engine->add_algorithm(algo); - return; - } - } - - throw Invalid_State("add_algorithm: Couldn't find the Default_Engine"); - } - -/************************************************* -* Get a cipher object * -*************************************************/ -Keyed_Filter* get_cipher(const std::string& algo_spec, - Cipher_Dir direction) - { - Library_State::Engine_Iterator i(global_state()); - - while(Engine* engine = i.next()) - { - Keyed_Filter* algo = engine->get_cipher(algo_spec, direction); - if(algo) - return algo; - } - - throw Algorithm_Not_Found(algo_spec); - } - -/************************************************* -* Get a cipher object * -*************************************************/ -Keyed_Filter* get_cipher(const std::string& algo_spec, - const SymmetricKey& key, - const InitializationVector& iv, - Cipher_Dir direction) - { - Keyed_Filter* cipher = get_cipher(algo_spec, direction); - cipher->set_key(key); - - if(iv.length()) - cipher->set_iv(iv); - - return cipher; - } - -/************************************************* -* Get a cipher object * -*************************************************/ -Keyed_Filter* get_cipher(const std::string& algo_spec, - const SymmetricKey& key, - Cipher_Dir direction) - { - return get_cipher(algo_spec, - key, InitializationVector(), direction); - } - } |