diff options
author | lloyd <[email protected]> | 2010-11-04 02:13:29 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-11-04 02:13:29 +0000 |
commit | 20191a0e123d0098375be099fcce22f6a6415402 (patch) | |
tree | fc17c42295acee6bf168c3f61ca2470f7d1aa0a0 /src | |
parent | 51e9d52e4fcfcf486ee53af1a42c98e841bcfde0 (diff) |
Move PBKDF lookups to engine
Diffstat (limited to 'src')
-rw-r--r-- | src/algo_factory/algo_factory.cpp | 47 | ||||
-rw-r--r-- | src/algo_factory/algo_factory.h | 24 | ||||
-rw-r--r-- | src/engine/core_engine/core_engine.h | 3 | ||||
-rw-r--r-- | src/engine/core_engine/info.txt | 1 | ||||
-rw-r--r-- | src/engine/dyn_engine/dyn_engine.cpp | 2 | ||||
-rw-r--r-- | src/engine/dyn_engine/dyn_engine.h | 6 | ||||
-rw-r--r-- | src/engine/engine.cpp | 7 | ||||
-rw-r--r-- | src/engine/engine.h | 9 | ||||
-rw-r--r-- | src/engine/info.txt | 1 | ||||
-rw-r--r-- | src/libstate/get_enc.cpp | 35 |
10 files changed, 101 insertions, 34 deletions
diff --git a/src/algo_factory/algo_factory.cpp b/src/algo_factory/algo_factory.cpp index ba4a435d7..3d640ab8a 100644 --- a/src/algo_factory/algo_factory.cpp +++ b/src/algo_factory/algo_factory.cpp @@ -15,6 +15,7 @@ #include <botan/stream_cipher.h> #include <botan/hash.h> #include <botan/mac.h> +#include <botan/pbkdf.h> #include <algorithm> @@ -55,6 +56,12 @@ MessageAuthenticationCode* engine_get_algo(Engine* engine, Algorithm_Factory& af) { return engine->find_mac(request, af); } +template<> +PBKDF* engine_get_algo(Engine* engine, + const SCAN_Name& request, + Algorithm_Factory& af) + { return engine->find_pbkdf(request, af); } + template<typename T> const T* factory_prototype(const std::string& algo_spec, const std::string& provider, @@ -93,6 +100,7 @@ Algorithm_Factory::Algorithm_Factory(Mutex_Factory& mf) stream_cipher_cache = new Algorithm_Cache<StreamCipher>(mf.make()); hash_cache = new Algorithm_Cache<HashFunction>(mf.make()); mac_cache = new Algorithm_Cache<MessageAuthenticationCode>(mf.make()); + pbkdf_cache = new Algorithm_Cache<PBKDF>(mf.make()); } /* @@ -104,6 +112,7 @@ Algorithm_Factory::~Algorithm_Factory() delete stream_cipher_cache; delete hash_cache; delete mac_cache; + delete pbkdf_cache; std::for_each(engines.begin(), engines.end(), del_fun<Engine>()); } @@ -114,6 +123,7 @@ void Algorithm_Factory::clear_caches() stream_cipher_cache->clear_cache(); hash_cache->clear_cache(); mac_cache->clear_cache(); + pbkdf_cache->clear_cache(); } void Algorithm_Factory::add_engine(Engine* engine) @@ -136,6 +146,8 @@ void Algorithm_Factory::set_preferred_provider(const std::string& algo_spec, hash_cache->set_preferred_provider(algo_spec, provider); else if(prototype_mac(algo_spec)) mac_cache->set_preferred_provider(algo_spec, provider); + else if(prototype_pbkdf(algo_spec)) + pbkdf_cache->set_preferred_provider(algo_spec, provider); } /* @@ -168,6 +180,8 @@ Algorithm_Factory::providers_of(const std::string& algo_spec) return hash_cache->providers_of(algo_spec); else if(prototype_mac(algo_spec)) return mac_cache->providers_of(algo_spec); + else if(prototype_pbkdf(algo_spec)) + return pbkdf_cache->providers_of(algo_spec); else return std::vector<std::string>(); } @@ -218,6 +232,18 @@ Algorithm_Factory::prototype_mac(const std::string& algo_spec, } /* +* Return the prototypical object corresponding to this request +*/ +const PBKDF* +Algorithm_Factory::prototype_pbkdf(const std::string& algo_spec, + const std::string& provider) + { + return factory_prototype<PBKDF>(algo_spec, provider, + engines, + *this, pbkdf_cache); + } + +/* * Return a new block cipher corresponding to this request */ BlockCipher* @@ -266,6 +292,18 @@ Algorithm_Factory::make_mac(const std::string& algo_spec, } /* +* Return a new object corresponding to this request +*/ +PBKDF* +Algorithm_Factory::make_pbkdf(const std::string& algo_spec, + const std::string& provider) + { + if(const PBKDF* proto = prototype_pbkdf(algo_spec, provider)) + return proto->clone(); + throw Algorithm_Not_Found(algo_spec); + } + +/* * Add a new block cipher */ void Algorithm_Factory::add_block_cipher(BlockCipher* block_cipher, @@ -301,4 +339,13 @@ void Algorithm_Factory::add_mac(MessageAuthenticationCode* mac, mac_cache->add(mac, mac->name(), provider); } +/* +* Add a new PBKDF +*/ +void Algorithm_Factory::add_pbkdf(PBKDF* pbkdf, + const std::string& provider) + { + pbkdf_cache->add(pbkdf, pbkdf->name(), provider); + } + } diff --git a/src/algo_factory/algo_factory.h b/src/algo_factory/algo_factory.h index 10549d5d3..b9f15757d 100644 --- a/src/algo_factory/algo_factory.h +++ b/src/algo_factory/algo_factory.h @@ -21,6 +21,7 @@ class BlockCipher; class StreamCipher; class HashFunction; class MessageAuthenticationCode; +class PBKDF; template<typename T> class Algorithm_Cache; @@ -161,6 +162,28 @@ class BOTAN_DLL Algorithm_Factory const std::string& provider); /** + * @param algo_spec the algorithm we want + * @param provider the provider we would like to use + * @returns pointer to const prototype object, ready to clone(), or NULL + */ + const PBKDF* prototype_pbkdf(const std::string& algo_spec, + const std::string& provider = ""); + + /** + * @param algo_spec the algorithm we want + * @param provider the provider we would like to use + * @returns pointer to freshly created instance of the request algorithm + */ + PBKDF* make_pbkdf(const std::string& algo_spec, + const std::string& provider = ""); + + /** + * @param algo the algorithm to add + * @param provider the provider of this algorithm + */ + void add_pbkdf(PBKDF* algo, const std::string& provider); + + /** * An iterator for the engines in this factory * @deprecated Avoid in new code */ @@ -196,6 +219,7 @@ class BOTAN_DLL Algorithm_Factory Algorithm_Cache<StreamCipher>* stream_cipher_cache; Algorithm_Cache<HashFunction>* hash_cache; Algorithm_Cache<MessageAuthenticationCode>* mac_cache; + Algorithm_Cache<PBKDF>* pbkdf_cache; }; } diff --git a/src/engine/core_engine/core_engine.h b/src/engine/core_engine/core_engine.h index b8b8262ce..5386991c3 100644 --- a/src/engine/core_engine/core_engine.h +++ b/src/engine/core_engine/core_engine.h @@ -49,6 +49,9 @@ class Core_Engine : public Engine MessageAuthenticationCode* find_mac(const SCAN_Name& reqeust, Algorithm_Factory&) const; + + PBKDF* find_pbkdf(const SCAN_Name& algo_spec, + Algorithm_Factory& af) const; }; /** diff --git a/src/engine/core_engine/info.txt b/src/engine/core_engine/info.txt index ea059b3c6..1935b0518 100644 --- a/src/engine/core_engine/info.txt +++ b/src/engine/core_engine/info.txt @@ -12,6 +12,7 @@ lookup_block.cpp lookup_hash.cpp lookup_mac.cpp lookup_stream.cpp +lookup_pbkdf.cpp </source> <requires> diff --git a/src/engine/dyn_engine/dyn_engine.cpp b/src/engine/dyn_engine/dyn_engine.cpp index 83169f431..b76544d0f 100644 --- a/src/engine/dyn_engine/dyn_engine.cpp +++ b/src/engine/dyn_engine/dyn_engine.cpp @@ -32,7 +32,7 @@ Dynamically_Loaded_Engine::Dynamically_Loaded_Engine( const u32bit mod_version = get_version(); - if(mod_version != 20100908) + if(mod_version != 20101003) throw std::runtime_error("Incompatible version in " + library_path + " of " + to_string(mod_version)); diff --git a/src/engine/dyn_engine/dyn_engine.h b/src/engine/dyn_engine/dyn_engine.h index 46752f5a9..d8e92cb02 100644 --- a/src/engine/dyn_engine/dyn_engine.h +++ b/src/engine/dyn_engine/dyn_engine.h @@ -49,6 +49,12 @@ class BOTAN_DLL Dynamically_Loaded_Engine : public Engine return engine->find_mac(algo_spec, af); } + PBKDF* find_pbkdf(const SCAN_Name& algo_spec, + Algorithm_Factory& af) const + { + return engine->find_pbkdf(algo_spec, af); + } + Modular_Exponentiator* mod_exp(const BigInt& n, Power_Mod::Usage_Hints hints) const { diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 958d4148f..80712a2f8 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -37,6 +37,13 @@ Engine::find_mac(const SCAN_Name&, return 0; } +PBKDF* +Engine::find_pbkdf(const SCAN_Name&, + Algorithm_Factory&) const + { + return 0; + } + Modular_Exponentiator* Engine::mod_exp(const BigInt&, Power_Mod::Usage_Hints) const diff --git a/src/engine/engine.h b/src/engine/engine.h index c9bcd6126..a322b68ec 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -14,6 +14,7 @@ #include <botan/stream_cipher.h> #include <botan/hash.h> #include <botan/mac.h> +#include <botan/pbkdf.h> #include <botan/pow_mod.h> #include <botan/pk_keys.h> #include <botan/pk_ops.h> @@ -79,6 +80,14 @@ class BOTAN_DLL Engine Algorithm_Factory& af) const; /** + * @param algo_spec the algorithm name/specification + * @param af an algorithm factory object + * @return newly allocated object, or NULL + */ + virtual PBKDF* find_pbkdf(const SCAN_Name& algo_spec, + Algorithm_Factory& af) const; + + /** * @param n the modulus * @param hints any use hints * @return newly allocated object, or NULL diff --git a/src/engine/info.txt b/src/engine/info.txt index 5f787cebe..0c73450bc 100644 --- a/src/engine/info.txt +++ b/src/engine/info.txt @@ -14,6 +14,7 @@ hash libstate mac numbertheory +pbkdf pubkey stream </requires> diff --git a/src/libstate/get_enc.cpp b/src/libstate/get_enc.cpp index d4ca99535..6a87268e8 100644 --- a/src/libstate/get_enc.cpp +++ b/src/libstate/get_enc.cpp @@ -9,18 +9,6 @@ #include <botan/libstate.h> #include <botan/scan_name.h> -#if defined(BOTAN_HAS_PBKDF1) - #include <botan/pbkdf1.h> -#endif - -#if defined(BOTAN_HAS_PBKDF2) - #include <botan/pbkdf2.h> -#endif - -#if defined(BOTAN_HAS_PGPS2K) - #include <botan/pgp_s2k.h> -#endif - #if defined(BOTAN_HAS_MGF1) #include <botan/mgf1.h> #endif @@ -84,29 +72,10 @@ namespace Botan { */ PBKDF* get_pbkdf(const std::string& algo_spec) { - SCAN_Name request(algo_spec); - Algorithm_Factory& af = global_state().algorithm_factory(); -#if defined(BOTAN_HAS_PBKDF1) - if(request.algo_name() == "PBKDF1" && request.arg_count() == 1) - return new PKCS5_PBKDF1(af.make_hash_function(request.arg(0))); -#endif - -#if defined(BOTAN_HAS_PBKDF2) - if(request.algo_name() == "PBKDF2" && request.arg_count() == 1) - { - if(const MessageAuthenticationCode* mac_proto = af.prototype_mac(request.arg(0))) - return new PKCS5_PBKDF2(mac_proto->clone()); - - return new PKCS5_PBKDF2(af.make_mac("HMAC(" + request.arg(0) + ")")); - } -#endif - -#if defined(BOTAN_HAS_PGPS2K) - if(request.algo_name() == "OpenPGP-S2K" && request.arg_count() == 1) - return new OpenPGP_S2K(af.make_hash_function(request.arg(0))); -#endif + if(PBKDF* pbkdf = af.make_pbkdf(algo_spec)) + return pbkdf; throw Algorithm_Not_Found(algo_spec); } |