aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstate
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-11-10 21:14:59 +0000
committerlloyd <[email protected]>2008-11-10 21:14:59 +0000
commit04280f4ff97d842a4af15f2e4ea2b87e6095b126 (patch)
treebf56e261ab2ef6b033b7e0761868eb0437e9e12d /src/libstate
parent13a012955bf68d028ee0cd515e49137b7d95f5e6 (diff)
Move MACs also to Algorithm_Factory
Diffstat (limited to 'src/libstate')
-rw-r--r--src/libstate/algo_factory.cpp60
-rw-r--r--src/libstate/algo_factory.h9
-rw-r--r--src/libstate/engine/def_engine/def_eng.h5
-rw-r--r--src/libstate/engine/def_engine/lookup_mac.cpp11
-rw-r--r--src/libstate/engine/engine.cpp17
-rw-r--r--src/libstate/engine/engine.h21
-rw-r--r--src/libstate/lookup.cpp91
7 files changed, 139 insertions, 75 deletions
diff --git a/src/libstate/algo_factory.cpp b/src/libstate/algo_factory.cpp
index 282f6795e..4ae3a251a 100644
--- a/src/libstate/algo_factory.cpp
+++ b/src/libstate/algo_factory.cpp
@@ -7,6 +7,9 @@ Algorithm Factory
#include <botan/stl_util.h>
#include <botan/engine.h>
#include <botan/exceptn.h>
+#include <botan/hash.h>
+#include <botan/mac.h>
+
#include <algorithm>
namespace Botan {
@@ -71,6 +74,9 @@ HashFunction* Algorithm_Factory::make_hash_function(const SCAN_Name& request)
throw Algorithm_Not_Found(request.as_string());
}
+/**
+* Add a new object
+*/
void Algorithm_Factory::add_hash_function(HashFunction* hash)
{
for(u32bit i = 0; i != engines.size(); ++i)
@@ -85,4 +91,58 @@ void Algorithm_Factory::add_hash_function(HashFunction* hash)
throw Exception("Algorithm_Factory::add_hash_function: No engine found");
}
+
+
+/**
+* Return the prototypical object cooresponding to this request
+*/
+const MessageAuthenticationCode*
+Algorithm_Factory::prototype_mac(const SCAN_Name& request)
+ {
+ for(u32bit i = 0; i != engines.size(); ++i)
+ {
+ if(request.provider_allowed(engines[i]->provider_name()))
+ {
+ const MessageAuthenticationCode* algo =
+ engines[i]->prototype_mac(request, *this);
+
+ if(algo)
+ return algo;
+ }
+ }
+
+ return 0;
+ }
+
+/**
+* Return a new object cooresponding to this request
+*/
+MessageAuthenticationCode*
+Algorithm_Factory::make_mac(const SCAN_Name& request)
+ {
+ const MessageAuthenticationCode* prototype = prototype_mac(request);
+ if(prototype)
+ return prototype->clone();
+
+ throw Algorithm_Not_Found(request.as_string());
+ }
+
+/**
+* Add a new object
+*/
+void Algorithm_Factory::add_mac(MessageAuthenticationCode* hash)
+ {
+ for(u32bit i = 0; i != engines.size(); ++i)
+ {
+ if(engines[i]->can_add_algorithms())
+ {
+ engines[i]->add_algorithm(hash);
+ return;
+ }
+ }
+
+ throw Exception("Algorithm_Factory::add_mac: No engine found");
+ }
+
+
}
diff --git a/src/libstate/algo_factory.h b/src/libstate/algo_factory.h
index 9591d1dd0..098c7836b 100644
--- a/src/libstate/algo_factory.h
+++ b/src/libstate/algo_factory.h
@@ -8,12 +8,14 @@
#include <botan/scan_name.h>
#include <botan/mutex.h>
-#include <botan/hash.h>
#include <string>
#include <vector>
namespace Botan {
+class HashFunction;
+class MessageAuthenticationCode;
+
/**
* Algorithm Factory
*/
@@ -40,6 +42,11 @@ class BOTAN_DLL Algorithm_Factory
HashFunction* make_hash_function(const SCAN_Name& request);
void add_hash_function(HashFunction* hash);
+ // MAC operations
+ const MessageAuthenticationCode* prototype_mac(const SCAN_Name& request);
+ MessageAuthenticationCode* make_mac(const SCAN_Name& request);
+ void add_mac(MessageAuthenticationCode* mac);
+
private:
class Engine* get_engine_n(u32bit) const;
diff --git a/src/libstate/engine/def_engine/def_eng.h b/src/libstate/engine/def_engine/def_eng.h
index 490ef1def..fe05e9fce 100644
--- a/src/libstate/engine/def_engine/def_eng.h
+++ b/src/libstate/engine/def_engine/def_eng.h
@@ -64,10 +64,11 @@ class BOTAN_DLL Default_Engine : public Engine
BlockCipher* find_block_cipher(const std::string&) const;
StreamCipher* find_stream_cipher(const std::string&) const;
- HashFunction* find_hash(const SCAN_Name& requst,
+ HashFunction* find_hash(const SCAN_Name& reqeust,
Algorithm_Factory&) const;
- MessageAuthenticationCode* find_mac(const std::string&) const;
+ MessageAuthenticationCode* find_mac(const SCAN_Name& reqeust,
+ Algorithm_Factory&) const;
};
}
diff --git a/src/libstate/engine/def_engine/lookup_mac.cpp b/src/libstate/engine/def_engine/lookup_mac.cpp
index 5cdc20cf2..7feda835c 100644
--- a/src/libstate/engine/def_engine/lookup_mac.cpp
+++ b/src/libstate/engine/def_engine/lookup_mac.cpp
@@ -4,8 +4,9 @@
*************************************************/
#include <botan/def_eng.h>
-#include <botan/lookup.h>
#include <botan/scan_name.h>
+#include <botan/algo_factory.h>
+#include <botan/lookup.h>
#if defined(BOTAN_HAS_CBC_MAC)
#include <botan/cbc_mac.h>
@@ -33,9 +34,9 @@ namespace Botan {
* Look for an algorithm with this name *
*************************************************/
MessageAuthenticationCode*
-Default_Engine::find_mac(const std::string& algo_spec) const
+Default_Engine::find_mac(const SCAN_Name& request,
+ Algorithm_Factory& af) const
{
- SCAN_Name request(algo_spec);
#if defined(BOTAN_HAS_CBC_MAC)
if(request.algo_name() == "CBC-MAC" && request.arg_count() == 1)
@@ -49,12 +50,12 @@ Default_Engine::find_mac(const std::string& algo_spec) const
#if defined(BOTAN_HAS_HMAC)
if(request.algo_name() == "HMAC" && request.arg_count() == 1)
- return new HMAC(get_hash(request.argument(0)));
+ return new HMAC(af.make_hash_function(request.argument(0)));
#endif
#if defined(BOTAN_HAS_SSL3_MAC)
if(request.algo_name() == "SSL3-MAC" && request.arg_count() == 1)
- return new SSL3_MAC(get_hash(request.argument(0)));
+ return new SSL3_MAC(af.make_hash_function(request.argument(0)));
#endif
#if defined(BOTAN_HAS_ANSI_X919_MAC)
diff --git a/src/libstate/engine/engine.cpp b/src/libstate/engine/engine.cpp
index 8afb170db..f973c0939 100644
--- a/src/libstate/engine/engine.cpp
+++ b/src/libstate/engine/engine.cpp
@@ -101,10 +101,21 @@ Engine::prototype_hash_function(const SCAN_Name& request,
/*************************************************
* Acquire a MessageAuthenticationCode *
*************************************************/
-const MessageAuthenticationCode* Engine::mac(const std::string& name) const
+const MessageAuthenticationCode*
+Engine::prototype_mac(const SCAN_Name& request,
+ Algorithm_Factory& af) const
{
- return lookup_algo(cache_of_mac, global_state().deref_alias(name),
- this, &Engine::find_mac);
+ // This needs to respect provider settings
+ MessageAuthenticationCode* algo = cache_of_mac->get(request.as_string());
+ if(algo)
+ return algo;
+
+ // cache miss: do full search
+ algo = find_mac(request, af);
+ if(algo)
+ cache_of_mac->add(algo, request.as_string());
+
+ return algo;
}
/*************************************************
diff --git a/src/libstate/engine/engine.h b/src/libstate/engine/engine.h
index 0f61fe1e2..74b40e637 100644
--- a/src/libstate/engine/engine.h
+++ b/src/libstate/engine/engine.h
@@ -119,21 +119,26 @@ class BOTAN_DLL Engine
virtual Keyed_Filter* get_cipher(const std::string&, Cipher_Dir)
{ return 0; }
+ // Prototype object accessors
const BlockCipher* block_cipher(const std::string&) const;
const StreamCipher* stream_cipher(const std::string&) const;
- const HashFunction* prototype_hash_function(const SCAN_Name& request,
- Algorithm_Factory& af) const;
+ const HashFunction*
+ prototype_hash_function(const SCAN_Name& request,
+ Algorithm_Factory& af) const;
- const MessageAuthenticationCode* mac(const std::string&) const;
-
- virtual bool can_add_algorithms() { return false; }
+ const MessageAuthenticationCode*
+ prototype_mac(const SCAN_Name& request,
+ Algorithm_Factory& af) const;
+ // Add new algorithms
void add_algorithm(BlockCipher*) const;
void add_algorithm(StreamCipher*) const;
void add_algorithm(HashFunction*) const;
void add_algorithm(MessageAuthenticationCode*) const;
+ // Engine information
+ virtual bool can_add_algorithms() { return false; }
virtual std::string provider_name() const = 0;
Engine();
@@ -145,10 +150,12 @@ class BOTAN_DLL Engine
virtual StreamCipher* find_stream_cipher(const std::string&) const
{ return 0; }
- virtual HashFunction* find_hash(const SCAN_Name&, Algorithm_Factory&) const
+ virtual HashFunction* find_hash(const SCAN_Name&,
+ Algorithm_Factory&) const
{ return 0; }
- virtual MessageAuthenticationCode* find_mac(const std::string&) const
+ virtual MessageAuthenticationCode* find_mac(const SCAN_Name&,
+ Algorithm_Factory&) const
{ return 0; }
template<typename T>
diff --git a/src/libstate/lookup.cpp b/src/libstate/lookup.cpp
index f53b1a099..501f973b0 100644
--- a/src/libstate/lookup.cpp
+++ b/src/libstate/lookup.cpp
@@ -43,6 +43,40 @@ bool have_hash(const std::string& algo_spec)
}
/*************************************************
+* Acquire an authentication code *
+*************************************************/
+const MessageAuthenticationCode* retrieve_mac(Library_State& libstate,
+ const std::string& algo_spec)
+ {
+ return libstate.algo_factory().prototype_mac(algo_spec);
+ }
+
+/*************************************************
+* Get a MAC by name *
+*************************************************/
+MessageAuthenticationCode* get_mac(const std::string& algo_spec)
+ {
+ return global_state().algo_factory().make_mac(algo_spec);
+ }
+
+/*************************************************
+* Query if Botan has the named MAC *
+*************************************************/
+bool have_mac(const std::string& algo_spec)
+ {
+ return global_state().algo_factory().prototype_mac(algo_spec);
+ }
+
+/*************************************************
+* Add a new authentication code *
+*************************************************/
+void add_algorithm(Library_State& libstate,
+ MessageAuthenticationCode* algo)
+ {
+ libstate.algo_factory().add_mac(algo);
+ }
+
+/*************************************************
* Get a block cipher by name *
*************************************************/
BlockCipher* get_block_cipher(const std::string& name)
@@ -65,17 +99,6 @@ StreamCipher* get_stream_cipher(const std::string& name)
}
/*************************************************
-* Get a MAC by name *
-*************************************************/
-MessageAuthenticationCode* get_mac(const std::string& name)
- {
- const MessageAuthenticationCode* mac = retrieve_mac(global_state(), name);
- if(mac)
- return mac->clone();
- throw Algorithm_Not_Found(name);
- }
-
-/*************************************************
* Query if an algorithm exists *
*************************************************/
bool have_algorithm(const std::string& name)
@@ -108,14 +131,6 @@ bool have_stream_cipher(const std::string& name)
}
/*************************************************
-* Query if Botan has the named MAC *
-*************************************************/
-bool have_mac(const std::string& name)
- {
- return (retrieve_mac(global_state(), name) != 0);
- }
-
-/*************************************************
* Query the block size of a cipher or hash *
*************************************************/
u32bit block_size_of(const std::string& name)
@@ -264,24 +279,6 @@ const StreamCipher* retrieve_stream_cipher(Library_State& libstate,
}
/*************************************************
-* Acquire an authentication code *
-*************************************************/
-const MessageAuthenticationCode* retrieve_mac(Library_State& libstate,
- const std::string& name)
- {
- Algorithm_Factory::Engine_Iterator i(libstate.algo_factory());
-
- while(const Engine* engine = i.next())
- {
- const MessageAuthenticationCode* algo = engine->mac(name);
- if(algo)
- return algo;
- }
-
- return 0;
- }
-
-/*************************************************
* Add a new block cipher *
*************************************************/
void add_algorithm(Library_State& libstate, BlockCipher* algo)
@@ -320,26 +317,6 @@ void add_algorithm(Library_State& libstate, StreamCipher* algo)
}
/*************************************************
-* Add a new authentication code *
-*************************************************/
-void add_algorithm(Library_State& libstate,
- MessageAuthenticationCode* algo)
- {
- Algorithm_Factory::Engine_Iterator i(libstate.algo_factory());
-
- 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,