monotone

monotone Mtn Source Tree

Root/botan/eng_base.cpp

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

Archive Download this file

Branches

Tags

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