monotone

monotone Mtn Source Tree

Root/botan/eng_base.cpp

1/*************************************************
2* Basic No-Op Engine Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/engine.h>
7#include <botan/libstate.h>
8#include <botan/stl_util.h>
9#include <botan/lookup.h>
10
11namespace Botan {
12
13namespace {
14
15/*************************************************
16* Algorithm Cache *
17*************************************************/
18template<typename T>
19class Algorithm_Cache_Impl : public Engine::Algorithm_Cache<T>
20 {
21 public:
22 T* get(const std::string& name) const
23 {
24 Mutex_Holder lock(mutex);
25 return search_map(mappings, name);
26 }
27
28 void add(T* algo, const std::string& index_name = "") const
29 {
30 if(!algo)
31 return;
32
33 Mutex_Holder lock(mutex);
34
35 const std::string name =
36 (index_name != "" ? index_name : algo->name());
37
38 if(mappings.find(name) != mappings.end())
39 delete mappings[name];
40 mappings[name] = algo;
41 }
42
43 Algorithm_Cache_Impl()
44 {
45 mutex = global_state().get_mutex();
46 }
47
48 ~Algorithm_Cache_Impl()
49 {
50 typename std::map<std::string, T*>::iterator i = mappings.begin();
51
52 while(i != mappings.end())
53 {
54 delete i->second;
55 ++i;
56 }
57 delete mutex;
58 }
59 private:
60 Mutex* mutex;
61 mutable std::map<std::string, T*> mappings;
62 };
63
64}
65
66/*************************************************
67* Basic No-Op Engine Implementation *
68*************************************************/
69IF_Operation* Engine::if_op(const BigInt&, const BigInt&, const BigInt&,
70 const BigInt&, const BigInt&, const BigInt&,
71 const BigInt&, const BigInt&) const
72 {
73 return 0;
74 }
75
76/*************************************************
77* Basic No-Op Engine Implementation *
78*************************************************/
79DSA_Operation* Engine::dsa_op(const DL_Group&, const BigInt&,
80 const BigInt&) const
81 {
82 return 0;
83 }
84
85/*************************************************
86* Basic No-Op Engine Implementation *
87*************************************************/
88NR_Operation* Engine::nr_op(const DL_Group&, const BigInt&,
89 const BigInt&) const
90 {
91 return 0;
92 }
93
94/*************************************************
95* Basic No-Op Engine Implementation *
96*************************************************/
97ELG_Operation* Engine::elg_op(const DL_Group&, const BigInt&,
98 const BigInt&) const
99 {
100 return 0;
101 }
102
103/*************************************************
104* Basic No-Op Engine Implementation *
105*************************************************/
106DH_Operation* Engine::dh_op(const DL_Group&, const BigInt&) const
107 {
108 return 0;
109 }
110
111/*************************************************
112* Basic No-Op Engine Implementation *
113*************************************************/
114Modular_Exponentiator* Engine::mod_exp(const BigInt&,
115 Power_Mod::Usage_Hints) const
116 {
117 return 0;
118 }
119
120/*************************************************
121* Acquire a BlockCipher *
122*************************************************/
123const BlockCipher* Engine::block_cipher(const std::string& name) const
124 {
125 return lookup_algo(cache_of_bc, deref_alias(name),
126 this, &Engine::find_block_cipher);
127 }
128
129/*************************************************
130* Acquire a StreamCipher *
131*************************************************/
132const StreamCipher* Engine::stream_cipher(const std::string& name) const
133 {
134 return lookup_algo(cache_of_sc, deref_alias(name),
135 this, &Engine::find_stream_cipher);
136 }
137
138/*************************************************
139* Acquire a HashFunction *
140*************************************************/
141const HashFunction* Engine::hash(const std::string& name) const
142 {
143 return lookup_algo(cache_of_hf, deref_alias(name),
144 this, &Engine::find_hash);
145 }
146
147/*************************************************
148* Acquire a MessageAuthenticationCode *
149*************************************************/
150const MessageAuthenticationCode* Engine::mac(const std::string& name) const
151 {
152 return lookup_algo(cache_of_mac, deref_alias(name),
153 this, &Engine::find_mac);
154 }
155
156/*************************************************
157* Acquire a S2K object *
158*************************************************/
159const S2K* Engine::s2k(const std::string& name) const
160 {
161 return lookup_algo(cache_of_s2k, deref_alias(name),
162 this, &Engine::find_s2k);
163 }
164
165/*************************************************
166* Acquire a cipher padding object *
167*************************************************/
168const BlockCipherModePaddingMethod*
169Engine::bc_pad(const std::string& name) const
170 {
171 return lookup_algo(cache_of_bc_pad, deref_alias(name),
172 this, &Engine::find_bc_pad);
173 }
174
175/*************************************************
176* Add a block cipher to the lookup table *
177*************************************************/
178void Engine::add_algorithm(BlockCipher* algo) const
179 {
180 cache_of_bc->add(algo);
181 }
182
183/*************************************************
184* Add a stream cipher to the lookup table *
185*************************************************/
186void Engine::add_algorithm(StreamCipher* algo) const
187 {
188 cache_of_sc->add(algo);
189 }
190
191/*************************************************
192* Add a hash function to the lookup table *
193*************************************************/
194void Engine::add_algorithm(HashFunction* algo) const
195 {
196 cache_of_hf->add(algo);
197 }
198
199/*************************************************
200* Add a MAC to the lookup table *
201*************************************************/
202void Engine::add_algorithm(MessageAuthenticationCode* algo) const
203 {
204 cache_of_mac->add(algo);
205 }
206
207/*************************************************
208* Add a S2K to the lookup table *
209*************************************************/
210void Engine::add_algorithm(S2K* algo) const
211 {
212 cache_of_s2k->add(algo);
213 }
214
215/*************************************************
216* Add a cipher pad method to the lookup table *
217*************************************************/
218void Engine::add_algorithm(BlockCipherModePaddingMethod* algo) const
219 {
220 cache_of_bc_pad->add(algo);
221 }
222
223/*************************************************
224* Create an Engine *
225*************************************************/
226Engine::Engine()
227 {
228 cache_of_bc = new Algorithm_Cache_Impl<BlockCipher>();
229 cache_of_sc = new Algorithm_Cache_Impl<StreamCipher>();
230 cache_of_hf = new Algorithm_Cache_Impl<HashFunction>();
231 cache_of_mac = new Algorithm_Cache_Impl<MessageAuthenticationCode>();
232 cache_of_s2k = new Algorithm_Cache_Impl<S2K>();
233 cache_of_bc_pad =
234 new Algorithm_Cache_Impl<BlockCipherModePaddingMethod>();
235 }
236
237/*************************************************
238* Destroy an Engine *
239*************************************************/
240Engine::~Engine()
241 {
242 delete cache_of_bc;
243 delete cache_of_sc;
244 delete cache_of_hf;
245 delete cache_of_mac;
246 delete cache_of_s2k;
247 delete cache_of_bc_pad;
248 }
249
250/*************************************************
251* Basic No-Op Engine Implementation *
252*************************************************/
253BlockCipher* Engine::find_block_cipher(const std::string&) const
254 {
255 return 0;
256 }
257
258/*************************************************
259* Basic No-Op Engine Implementation *
260*************************************************/
261StreamCipher* Engine::find_stream_cipher(const std::string&) const
262 {
263 return 0;
264 }
265
266/*************************************************
267* Basic No-Op Engine Implementation *
268*************************************************/
269HashFunction* Engine::find_hash(const std::string&) const
270 {
271 return 0;
272 }
273
274/*************************************************
275* Basic No-Op Engine Implementation *
276*************************************************/
277MessageAuthenticationCode* Engine::find_mac(const std::string&) const
278 {
279 return 0;
280 }
281
282/*************************************************
283* Basic No-Op Engine Implementation *
284*************************************************/
285S2K* Engine::find_s2k(const std::string&) const
286 {
287 return 0;
288 }
289
290/*************************************************
291* Basic No-Op Engine Implementation *
292*************************************************/
293BlockCipherModePaddingMethod* Engine::find_bc_pad(const std::string&) const
294 {
295 return 0;
296 }
297
298/*************************************************
299* Basic No-Op Engine Implementation *
300*************************************************/
301Keyed_Filter* Engine::get_cipher(const std::string&, Cipher_Dir)
302 {
303 return 0;
304 }
305
306}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status