Add a return value to hasher_t.get_hash()
[strongswan.git] / src / libstrongswan / crypto / crypto_factory.h
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 /**
17 * @defgroup crypto_factory crypto_factory
18 * @{ @ingroup crypto
19 */
20
21 #ifndef CRYPTO_FACTORY_H_
22 #define CRYPTO_FACTORY_H_
23
24 typedef struct crypto_factory_t crypto_factory_t;
25
26 #include <library.h>
27 #include <crypto/crypters/crypter.h>
28 #include <crypto/aead.h>
29 #include <crypto/signers/signer.h>
30 #include <crypto/hashers/hasher.h>
31 #include <crypto/prfs/prf.h>
32 #include <crypto/rngs/rng.h>
33 #include <crypto/nonce_gen.h>
34 #include <crypto/diffie_hellman.h>
35 #include <crypto/transform.h>
36
37 #define CRYPTO_MAX_ALG_LINE 120 /* characters */
38
39 /**
40 * Constructor function for crypters
41 */
42 typedef crypter_t* (*crypter_constructor_t)(encryption_algorithm_t algo,
43 size_t key_size);
44 /**
45 * Constructor function for aead transforms
46 */
47 typedef aead_t* (*aead_constructor_t)(encryption_algorithm_t algo,
48 size_t key_size);
49 /**
50 * Constructor function for signers
51 */
52 typedef signer_t* (*signer_constructor_t)(integrity_algorithm_t algo);
53
54 /**
55 * Constructor function for hashers
56 */
57 typedef hasher_t* (*hasher_constructor_t)(hash_algorithm_t algo);
58
59 /**
60 * Constructor function for pseudo random functions
61 */
62 typedef prf_t* (*prf_constructor_t)(pseudo_random_function_t algo);
63
64 /**
65 * Constructor function for source of randomness
66 */
67 typedef rng_t* (*rng_constructor_t)(rng_quality_t quality);
68
69 /**
70 * Constructor function for nonce generators
71 */
72 typedef nonce_gen_t* (*nonce_gen_constructor_t)();
73
74 /**
75 * Constructor function for diffie hellman
76 *
77 * The DH constructor accepts additional arguments for:
78 * - MODP_CUSTOM: chunk_t generator, chunk_t prime
79 */
80 typedef diffie_hellman_t* (*dh_constructor_t)(diffie_hellman_group_t group, ...);
81
82 /**
83 * Handles crypto modules and creates instances.
84 */
85 struct crypto_factory_t {
86
87 /**
88 * Create a crypter instance.
89 *
90 * @param algo encryption algorithm
91 * @param key_size length of the key in bytes
92 * @return crypter_t instance, NULL if not supported
93 */
94 crypter_t* (*create_crypter)(crypto_factory_t *this,
95 encryption_algorithm_t algo, size_t key_size);
96
97 /**
98 * Create a aead instance.
99 *
100 * @param algo encryption algorithm
101 * @param key_size length of the key in bytes
102 * @return aead_t instance, NULL if not supported
103 */
104 aead_t* (*create_aead)(crypto_factory_t *this,
105 encryption_algorithm_t algo, size_t key_size);
106
107 /**
108 * Create a symmetric signer instance.
109 *
110 * @param algo MAC algorithm to use
111 * @return signer_t instance, NULL if not supported
112 */
113 signer_t* (*create_signer)(crypto_factory_t *this,
114 integrity_algorithm_t algo);
115
116 /**
117 * Create a hasher instance.
118 *
119 * @param algo hash algorithm
120 * @return hasher_t instance, NULL if not supported
121 */
122 hasher_t* (*create_hasher)(crypto_factory_t *this, hash_algorithm_t algo);
123
124 /**
125 * Create a pseudo random function instance.
126 *
127 * @param algo PRF algorithm to use
128 * @return prf_t instance, NULL if not supported
129 */
130 prf_t* (*create_prf)(crypto_factory_t *this, pseudo_random_function_t algo);
131
132 /**
133 * Create a source of randomness.
134 *
135 * @param quality required randomness quality
136 * @return rng_t instance, NULL if no RNG with such a quality
137 */
138 rng_t* (*create_rng)(crypto_factory_t *this, rng_quality_t quality);
139
140 /**
141 * Create a nonce generator instance.
142 *
143 * @return nonce_gen_t instance, NULL if not supported
144 */
145 nonce_gen_t* (*create_nonce_gen)(crypto_factory_t *this);
146
147 /**
148 * Create a diffie hellman instance.
149 *
150 * Additional arguments are passed to the DH constructor.
151 *
152 * @param group diffie hellman group
153 * @return diffie_hellman_t instance, NULL if not supported
154 */
155 diffie_hellman_t* (*create_dh)(crypto_factory_t *this,
156 diffie_hellman_group_t group, ...);
157
158 /**
159 * Register a crypter constructor.
160 *
161 * @param algo algorithm to constructor
162 * @param plugin_name plugin that registered this algorithm
163 * @param create constructor function for that algorithm
164 * @return
165 */
166 void (*add_crypter)(crypto_factory_t *this, encryption_algorithm_t algo,
167 const char *plugin_name, crypter_constructor_t create);
168
169 /**
170 * Unregister a crypter constructor.
171 *
172 * @param create constructor function to unregister
173 */
174 void (*remove_crypter)(crypto_factory_t *this, crypter_constructor_t create);
175
176 /**
177 * Unregister a aead constructor.
178 *
179 * @param create constructor function to unregister
180 */
181 void (*remove_aead)(crypto_factory_t *this, aead_constructor_t create);
182
183 /**
184 * Register a aead constructor.
185 *
186 * @param algo algorithm to constructor
187 * @param plugin_name plugin that registered this algorithm
188 * @param create constructor function for that algorithm
189 * @return
190 */
191 void (*add_aead)(crypto_factory_t *this, encryption_algorithm_t algo,
192 const char *plugin_name, aead_constructor_t create);
193
194 /**
195 * Register a signer constructor.
196 *
197 * @param algo algorithm to constructor
198 * @param plugin_name plugin that registered this algorithm
199 * @param create constructor function for that algorithm
200 * @return
201 */
202 void (*add_signer)(crypto_factory_t *this, integrity_algorithm_t algo,
203 const char *plugin_name, signer_constructor_t create);
204
205 /**
206 * Unregister a signer constructor.
207 *
208 * @param create constructor function to unregister
209 */
210 void (*remove_signer)(crypto_factory_t *this, signer_constructor_t create);
211
212 /**
213 * Register a hasher constructor.
214 *
215 * The first added hasher is the preferred hasher returned on
216 * create_hasher(HASH_PREFERRED).
217 *
218 * @param algo algorithm to constructor
219 * @param plugin_name plugin that registered this algorithm
220 * @param create constructor function for that algorithm
221 * @return
222 */
223 void (*add_hasher)(crypto_factory_t *this, hash_algorithm_t algo,
224 const char *plugin_name, hasher_constructor_t create);
225
226 /**
227 * Unregister a hasher constructor.
228 *
229 * @param create constructor function to unregister
230 */
231 void (*remove_hasher)(crypto_factory_t *this, hasher_constructor_t create);
232
233 /**
234 * Register a prf constructor.
235 *
236 * @param algo algorithm to constructor
237 * @param plugin_name plugin that registered this algorithm
238 * @param create constructor function for that algorithm
239 * @return
240 */
241 void (*add_prf)(crypto_factory_t *this, pseudo_random_function_t algo,
242 const char *plugin_name, prf_constructor_t create);
243
244 /**
245 * Unregister a prf constructor.
246 *
247 * @param create constructor function to unregister
248 */
249 void (*remove_prf)(crypto_factory_t *this, prf_constructor_t create);
250
251 /**
252 * Register a source of randomness.
253 *
254 * @param quality quality of randomness this RNG serves
255 * @param plugin_name plugin that registered this algorithm
256 * @param create constructor function for such a quality
257 */
258 void (*add_rng)(crypto_factory_t *this, rng_quality_t quality,
259 const char *plugin_name, rng_constructor_t create);
260
261 /**
262 * Unregister a source of randomness.
263 *
264 * @param create constructor function to unregister
265 */
266 void (*remove_rng)(crypto_factory_t *this, rng_constructor_t create);
267
268 /**
269 * Register a nonce generator.
270 *
271 * @param plugin_name plugin that registered this algorithm
272 * @param create constructor function for that nonce generator
273 */
274 void (*add_nonce_gen)(crypto_factory_t *this, const char *plugin_name,
275 nonce_gen_constructor_t create);
276
277 /**
278 * Unregister a nonce generator.
279 *
280 * @param create constructor function to unregister
281 */
282 void (*remove_nonce_gen)(crypto_factory_t *this,
283 nonce_gen_constructor_t create);
284
285 /**
286 * Register a diffie hellman constructor.
287 *
288 * @param group dh group to constructor
289 * @param plugin_name plugin that registered this algorithm
290 * @param create constructor function for that algorithm
291 * @return
292 */
293 void (*add_dh)(crypto_factory_t *this, diffie_hellman_group_t group,
294 const char *plugin_name, dh_constructor_t create);
295
296 /**
297 * Unregister a diffie hellman constructor.
298 *
299 * @param create constructor function to unregister
300 */
301 void (*remove_dh)(crypto_factory_t *this, dh_constructor_t create);
302
303 /**
304 * Create an enumerator over all registered crypter algorithms.
305 *
306 * @return enumerator over encryption_algorithm_t, plugin
307 */
308 enumerator_t* (*create_crypter_enumerator)(crypto_factory_t *this);
309
310 /**
311 * Create an enumerator over all registered aead algorithms.
312 *
313 * @return enumerator over encryption_algorithm_t, plugin
314 */
315 enumerator_t* (*create_aead_enumerator)(crypto_factory_t *this);
316
317 /**
318 * Create an enumerator over all registered signer algorithms.
319 *
320 * @return enumerator over integrity_algorithm_t, plugin
321 */
322 enumerator_t* (*create_signer_enumerator)(crypto_factory_t *this);
323
324 /**
325 * Create an enumerator over all registered hasher algorithms.
326 *
327 * @return enumerator over hash_algorithm_t, plugin
328 */
329 enumerator_t* (*create_hasher_enumerator)(crypto_factory_t *this);
330
331 /**
332 * Create an enumerator over all registered PRFs.
333 *
334 * @return enumerator over pseudo_random_function_t, plugin
335 */
336 enumerator_t* (*create_prf_enumerator)(crypto_factory_t *this);
337
338 /**
339 * Create an enumerator over all registered diffie hellman groups.
340 *
341 * @return enumerator over diffie_hellman_group_t, plugin
342 */
343 enumerator_t* (*create_dh_enumerator)(crypto_factory_t *this);
344
345 /**
346 * Create an enumerator over all registered random generators.
347 *
348 * @return enumerator over rng_quality_t, plugin
349 */
350 enumerator_t* (*create_rng_enumerator)(crypto_factory_t *this);
351
352 /**
353 * Create an enumerator over all registered nonce generators.
354 *
355 * @return enumerator over plugin
356 */
357 enumerator_t* (*create_nonce_gen_enumerator)(crypto_factory_t *this);
358
359 /**
360 * Add a test vector to the crypto factory.
361 *
362 * @param type type of the test vector
363 * @param vector pointer to a test vector, defined in crypto_tester.h
364 */
365 void (*add_test_vector)(crypto_factory_t *this, transform_type_t type,
366 void *vector);
367
368 /**
369 * Destroy a crypto_factory instance.
370 */
371 void (*destroy)(crypto_factory_t *this);
372 };
373
374 /**
375 * Create a crypto_factory instance.
376 */
377 crypto_factory_t *crypto_factory_create();
378
379 #endif /** CRYPTO_FACTORY_H_ @}*/