monotone

monotone Mtn Source Tree

Root/botan/engine.cpp

1/*************************************************
2* 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/eng_def.h>
9
10namespace Botan {
11
12namespace Engine_Core {
13
14/*************************************************
15* Acquire an IF op *
16*************************************************/
17IF_Operation* if_op(const BigInt& e, const BigInt& n, const BigInt& d,
18 const BigInt& p, const BigInt& q, const BigInt& d1,
19 const BigInt& d2, const BigInt& c)
20 {
21 Library_State::Engine_Iterator i(global_state());
22
23 while(const Engine* engine = i.next())
24 {
25 IF_Operation* op = engine->if_op(e, n, d, p, q, d1, d2, c);
26 if(op)
27 return op;
28 }
29
30 throw Lookup_Error("Engine_Core::if_op: Unable to find a working engine");
31 }
32
33/*************************************************
34* Acquire a DSA op *
35*************************************************/
36DSA_Operation* dsa_op(const DL_Group& group, const BigInt& y, const BigInt& x)
37 {
38 Library_State::Engine_Iterator i(global_state());
39
40 while(const Engine* engine = i.next())
41 {
42 DSA_Operation* op = engine->dsa_op(group, y, x);
43 if(op)
44 return op;
45 }
46
47 throw Lookup_Error("Engine_Core::dsa_op: Unable to find a working engine");
48 }
49
50/*************************************************
51* Acquire a NR op *
52*************************************************/
53NR_Operation* nr_op(const DL_Group& group, const BigInt& y, const BigInt& x)
54 {
55 Library_State::Engine_Iterator i(global_state());
56
57 while(const Engine* engine = i.next())
58 {
59 NR_Operation* op = engine->nr_op(group, y, x);
60 if(op)
61 return op;
62 }
63
64 throw Lookup_Error("Engine_Core::nr_op: Unable to find a working engine");
65 }
66
67/*************************************************
68* Acquire an ElGamal op *
69*************************************************/
70ELG_Operation* elg_op(const DL_Group& group, const BigInt& y, const BigInt& x)
71 {
72 Library_State::Engine_Iterator i(global_state());
73
74 while(const Engine* engine = i.next())
75 {
76 ELG_Operation* op = engine->elg_op(group, y, x);
77 if(op)
78 return op;
79 }
80
81 throw Lookup_Error("Engine_Core::elg_op: Unable to find a working engine");
82 }
83
84/*************************************************
85* Acquire a DH op *
86*************************************************/
87DH_Operation* dh_op(const DL_Group& group, const BigInt& x)
88 {
89 Library_State::Engine_Iterator i(global_state());
90
91 while(const Engine* engine = i.next())
92 {
93 DH_Operation* op = engine->dh_op(group, x);
94 if(op)
95 return op;
96 }
97
98 throw Lookup_Error("Engine_Core::dh_op: Unable to find a working engine");
99 }
100
101/*************************************************
102* Acquire a modular exponentiator *
103*************************************************/
104Modular_Exponentiator* mod_exp(const BigInt& n, Power_Mod::Usage_Hints hints)
105 {
106 Library_State::Engine_Iterator i(global_state());
107
108 while(const Engine* engine = i.next())
109 {
110 Modular_Exponentiator* op = engine->mod_exp(n, hints);
111
112 if(op)
113 return op;
114 }
115
116 throw Lookup_Error("Engine_Core::mod_exp: Unable to find a working engine");
117 }
118
119}
120
121/*************************************************
122* Acquire a block cipher *
123*************************************************/
124const BlockCipher* retrieve_block_cipher(const std::string& name)
125 {
126 Library_State::Engine_Iterator i(global_state());
127
128 while(const Engine* engine = i.next())
129 {
130 const BlockCipher* algo = engine->block_cipher(name);
131 if(algo)
132 return algo;
133 }
134
135 return 0;
136 }
137
138/*************************************************
139* Acquire a stream cipher *
140*************************************************/
141const StreamCipher* retrieve_stream_cipher(const std::string& name)
142 {
143 Library_State::Engine_Iterator i(global_state());
144
145 while(const Engine* engine = i.next())
146 {
147 const StreamCipher* algo = engine->stream_cipher(name);
148 if(algo)
149 return algo;
150 }
151
152 return 0;
153 }
154
155/*************************************************
156* Acquire a hash function *
157*************************************************/
158const HashFunction* retrieve_hash(const std::string& name)
159 {
160 Library_State::Engine_Iterator i(global_state());
161
162 while(const Engine* engine = i.next())
163 {
164 const HashFunction* algo = engine->hash(name);
165 if(algo)
166 return algo;
167 }
168
169 return 0;
170 }
171
172/*************************************************
173* Acquire an authentication code *
174*************************************************/
175const MessageAuthenticationCode* retrieve_mac(const std::string& name)
176 {
177 Library_State::Engine_Iterator i(global_state());
178
179 while(const Engine* engine = i.next())
180 {
181 const MessageAuthenticationCode* algo = engine->mac(name);
182 if(algo)
183 return algo;
184 }
185
186 return 0;
187 }
188
189/*************************************************
190* Acquire a string-to-key algorithm *
191*************************************************/
192const S2K* retrieve_s2k(const std::string& name)
193 {
194 Library_State::Engine_Iterator i(global_state());
195
196 while(const Engine* engine = i.next())
197 {
198 const S2K* algo = engine->s2k(name);
199 if(algo)
200 return algo;
201 }
202
203 return 0;
204 }
205
206/*************************************************
207* Retrieve a block cipher padding method *
208*************************************************/
209const BlockCipherModePaddingMethod* retrieve_bc_pad(const std::string& name)
210 {
211 Library_State::Engine_Iterator i(global_state());
212
213 while(const Engine* engine = i.next())
214 {
215 const BlockCipherModePaddingMethod* algo = engine->bc_pad(name);
216 if(algo)
217 return algo;
218 }
219
220 return 0;
221 }
222
223/*************************************************
224* Add a new block cipher *
225*************************************************/
226void add_algorithm(BlockCipher* algo)
227 {
228 Library_State::Engine_Iterator i(global_state());
229
230 while(Engine* engine_base = i.next())
231 {
232 Default_Engine* engine = dynamic_cast<Default_Engine*>(engine_base);
233 if(engine)
234 {
235 engine->add_algorithm(algo);
236 return;
237 }
238 }
239
240 throw Invalid_State("add_algorithm: Couldn't find the Default_Engine");
241 }
242
243/*************************************************
244* Add a new stream cipher *
245*************************************************/
246void add_algorithm(StreamCipher* algo)
247 {
248 Library_State::Engine_Iterator i(global_state());
249
250 while(Engine* engine_base = i.next())
251 {
252 Default_Engine* engine = dynamic_cast<Default_Engine*>(engine_base);
253 if(engine)
254 {
255 engine->add_algorithm(algo);
256 return;
257 }
258 }
259
260 throw Invalid_State("add_algorithm: Couldn't find the Default_Engine");
261 }
262
263/*************************************************
264* Add a new hash function *
265*************************************************/
266void add_algorithm(HashFunction* algo)
267 {
268 Library_State::Engine_Iterator i(global_state());
269
270 while(Engine* engine_base = i.next())
271 {
272 Default_Engine* engine = dynamic_cast<Default_Engine*>(engine_base);
273 if(engine)
274 {
275 engine->add_algorithm(algo);
276 return;
277 }
278 }
279
280 throw Invalid_State("add_algorithm: Couldn't find the Default_Engine");
281 }
282
283/*************************************************
284* Add a new authentication code *
285*************************************************/
286void add_algorithm(MessageAuthenticationCode* algo)
287 {
288 Library_State::Engine_Iterator i(global_state());
289
290 while(Engine* engine_base = i.next())
291 {
292 Default_Engine* engine = dynamic_cast<Default_Engine*>(engine_base);
293 if(engine)
294 {
295 engine->add_algorithm(algo);
296 return;
297 }
298 }
299
300 throw Invalid_State("add_algorithm: Couldn't find the Default_Engine");
301 }
302
303/*************************************************
304* Add a padding method to the lookup table *
305*************************************************/
306void add_algorithm(BlockCipherModePaddingMethod* algo)
307 {
308 Library_State::Engine_Iterator i(global_state());
309
310 while(Engine* engine_base = i.next())
311 {
312 Default_Engine* engine = dynamic_cast<Default_Engine*>(engine_base);
313 if(engine)
314 {
315 engine->add_algorithm(algo);
316 return;
317 }
318 }
319
320 throw Invalid_State("add_algorithm: Couldn't find the Default_Engine");
321 }
322
323/*************************************************
324* Get a cipher object *
325*************************************************/
326Keyed_Filter* get_cipher(const std::string& algo_spec, Cipher_Dir direction)
327 {
328 Library_State::Engine_Iterator i(global_state());
329
330 while(Engine* engine = i.next())
331 {
332 Keyed_Filter* algo = engine->get_cipher(algo_spec, direction);
333 if(algo)
334 return algo;
335 }
336
337 throw Algorithm_Not_Found(algo_spec);
338 }
339
340/*************************************************
341* Get a cipher object *
342*************************************************/
343Keyed_Filter* get_cipher(const std::string& algo_spec, const SymmetricKey& key,
344 const InitializationVector& iv, Cipher_Dir direction)
345 {
346 Keyed_Filter* cipher = get_cipher(algo_spec, direction);
347 cipher->set_key(key);
348 cipher->set_iv(iv);
349 return cipher;
350 }
351
352/*************************************************
353* Get a cipher object *
354*************************************************/
355Keyed_Filter* get_cipher(const std::string& algo_spec, const SymmetricKey& key,
356 Cipher_Dir direction)
357 {
358 return get_cipher(algo_spec, key, InitializationVector(), direction);
359 }
360
361}

Archive Download this file

Branches

Tags

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