monotone

monotone Mtn Source Tree

Root/botan/engine.cpp

1/*************************************************
2* Engine Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/engine.h>
7#include <botan/def_eng.h>
8#include <botan/init.h>
9#include <botan/rng.h>
10
11namespace Botan {
12
13namespace {
14
15std::vector<Engine*> engines;
16
17}
18
19namespace Init {
20
21/*************************************************
22* Initialize the list of Engines *
23*************************************************/
24void startup_engines()
25 {
26 engines.push_back(new Default_Engine);
27 }
28
29/*************************************************
30* Delete the list of Engines *
31*************************************************/
32void shutdown_engines()
33 {
34 for(u32bit j = 0; j != engines.size(); j++)
35 delete engines[j];
36 engines.clear();
37 }
38
39}
40
41namespace Engine_Core {
42
43/*************************************************
44* Add an Engine to the list *
45*************************************************/
46void add_engine(Engine* engine)
47 {
48 engines.insert(engines.end() - 1, engine);
49 }
50
51/*************************************************
52* Acquire an IF op *
53*************************************************/
54IF_Operation* if_op(const BigInt& e, const BigInt& n, const BigInt& d,
55 const BigInt& p, const BigInt& q, const BigInt& d1,
56 const BigInt& d2, const BigInt& c)
57 {
58 for(u32bit j = 0; j != engines.size(); j++)
59 {
60 IF_Operation* op = engines[j]->if_op(e, n, d, p, q, d1, d2, c);
61 if(op) return op;
62 }
63 throw Lookup_Error("Engine_Core::if_op: Unable to find a working engine");
64 }
65
66/*************************************************
67* Acquire a DSA op *
68*************************************************/
69DSA_Operation* dsa_op(const DL_Group& group, const BigInt& y, const BigInt& x)
70 {
71 for(u32bit j = 0; j != engines.size(); j++)
72 {
73 DSA_Operation* op = engines[j]->dsa_op(group, y, x);
74 if(op) return op;
75 }
76 throw Lookup_Error("Engine_Core::dsa_op: Unable to find a working engine");
77 }
78
79/*************************************************
80* Acquire a NR op *
81*************************************************/
82NR_Operation* nr_op(const DL_Group& group, const BigInt& y, const BigInt& x)
83 {
84 for(u32bit j = 0; j != engines.size(); j++)
85 {
86 NR_Operation* op = engines[j]->nr_op(group, y, x);
87 if(op) return op;
88 }
89 throw Lookup_Error("Engine_Core::nr_op: Unable to find a working engine");
90 }
91
92/*************************************************
93* Acquire an ElGamal op *
94*************************************************/
95ELG_Operation* elg_op(const DL_Group& group, const BigInt& y, const BigInt& x)
96 {
97 for(u32bit j = 0; j != engines.size(); j++)
98 {
99 ELG_Operation* op = engines[j]->elg_op(group, y, x);
100 if(op) return op;
101 }
102 throw Lookup_Error("Engine_Core::elg_op: Unable to find a working engine");
103 }
104
105/*************************************************
106* Acquire a DH op *
107*************************************************/
108DH_Operation* dh_op(const DL_Group& group, const BigInt& x)
109 {
110 for(u32bit j = 0; j != engines.size(); j++)
111 {
112 DH_Operation* op = engines[j]->dh_op(group, x);
113 if(op) return op;
114 }
115 throw Lookup_Error("Engine_Core::dh_op: Unable to find a working engine");
116 }
117
118}
119
120/*************************************************
121* Acquire a modular reducer *
122*************************************************/
123ModularReducer* get_reducer(const BigInt& n, bool convert_ok)
124 {
125 for(u32bit j = 0; j != engines.size(); j++)
126 {
127 ModularReducer* op = engines[j]->reducer(n, convert_ok);
128 if(op) return op;
129 }
130 throw Lookup_Error("get_reducer: Unable to find a working engine");
131 }
132
133/*************************************************
134* Acquire a block cipher *
135*************************************************/
136const BlockCipher* retrieve_block_cipher(const std::string& name)
137 {
138 for(u32bit j = 0; j != engines.size(); j++)
139 {
140 const BlockCipher* algo = engines[j]->block_cipher(name);
141 if(algo) return algo;
142 }
143 return 0;
144 }
145
146/*************************************************
147* Acquire a stream cipher *
148*************************************************/
149const StreamCipher* retrieve_stream_cipher(const std::string& name)
150 {
151 for(u32bit j = 0; j != engines.size(); j++)
152 {
153 const StreamCipher* algo = engines[j]->stream_cipher(name);
154 if(algo) return algo;
155 }
156 return 0;
157 }
158
159/*************************************************
160* Acquire a hash function *
161*************************************************/
162const HashFunction* retrieve_hash(const std::string& name)
163 {
164 for(u32bit j = 0; j != engines.size(); j++)
165 {
166 const HashFunction* algo = engines[j]->hash(name);
167 if(algo) return algo;
168 }
169 return 0;
170 }
171
172/*************************************************
173* Acquire an authentication code *
174*************************************************/
175const MessageAuthenticationCode* retrieve_mac(const std::string& name)
176 {
177 for(u32bit j = 0; j != engines.size(); j++)
178 {
179 const MessageAuthenticationCode* algo = engines[j]->mac(name);
180 if(algo) return algo;
181 }
182 return 0;
183 }
184
185/*************************************************
186* Add a new block cipher *
187*************************************************/
188void add_algorithm(BlockCipher* algo)
189 {
190 for(u32bit j = 0; j != engines.size(); j++)
191 {
192 Default_Engine* engine = dynamic_cast<Default_Engine*>(engines[j]);
193 if(engine)
194 {
195 engine->add_algorithm(algo);
196 return;
197 }
198 }
199 throw Invalid_State("add_algorithm: Couldn't find the Default_Engine");
200 }
201
202/*************************************************
203* Add a new stream cipher *
204*************************************************/
205void add_algorithm(StreamCipher* algo)
206 {
207 for(u32bit j = 0; j != engines.size(); j++)
208 {
209 Default_Engine* engine = dynamic_cast<Default_Engine*>(engines[j]);
210 if(engine)
211 {
212 engine->add_algorithm(algo);
213 return;
214 }
215 }
216 throw Invalid_State("add_algorithm: Couldn't find the Default_Engine");
217 }
218
219/*************************************************
220* Add a new hash function *
221*************************************************/
222void add_algorithm(HashFunction* algo)
223 {
224 for(u32bit j = 0; j != engines.size(); j++)
225 {
226 Default_Engine* engine = dynamic_cast<Default_Engine*>(engines[j]);
227 if(engine)
228 {
229 engine->add_algorithm(algo);
230 return;
231 }
232 }
233 throw Invalid_State("add_algorithm: Couldn't find the Default_Engine");
234 }
235
236/*************************************************
237* Add a new authentication code *
238*************************************************/
239void add_algorithm(MessageAuthenticationCode* algo)
240 {
241 for(u32bit j = 0; j != engines.size(); j++)
242 {
243 Default_Engine* engine = dynamic_cast<Default_Engine*>(engines[j]);
244 if(engine)
245 {
246 engine->add_algorithm(algo);
247 return;
248 }
249 }
250 throw Invalid_State("add_algorithm: Couldn't find the Default_Engine");
251 }
252
253/*************************************************
254* Get a cipher object *
255*************************************************/
256Keyed_Filter* get_cipher(const std::string& algo_spec, Cipher_Dir direction)
257 {
258 for(u32bit j = 0; j != engines.size(); j++)
259 {
260 Keyed_Filter* algo = engines[j]->get_cipher(algo_spec, direction);
261 if(algo) return algo;
262 }
263 throw Algorithm_Not_Found(algo_spec);
264 }
265
266/*************************************************
267* Get a cipher object *
268*************************************************/
269Keyed_Filter* get_cipher(const std::string& algo_spec, const SymmetricKey& key,
270 const InitializationVector& iv, Cipher_Dir direction)
271 {
272 Keyed_Filter* cipher = get_cipher(algo_spec, direction);
273 cipher->set_key(key);
274 cipher->set_iv(iv);
275 return cipher;
276 }
277
278/*************************************************
279* Get a cipher object *
280*************************************************/
281Keyed_Filter* get_cipher(const std::string& algo_spec, const SymmetricKey& key,
282 Cipher_Dir direction)
283 {
284 return get_cipher(algo_spec, key, InitializationVector(), direction);
285 }
286
287}

Archive Download this file

Branches

Tags

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