]> git.ipfire.org Git - people/ms/strongswan.git/blob - src/libstrongswan/crypto/crypto_factory.h
crypto-factory: count the number of test vector failures during registration
[people/ms/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 <collections/enumerator.h>
28 #include <crypto/crypters/crypter.h>
29 #include <crypto/aead.h>
30 #include <crypto/signers/signer.h>
31 #include <crypto/hashers/hasher.h>
32 #include <crypto/prfs/prf.h>
33 #include <crypto/rngs/rng.h>
34 #include <crypto/nonce_gen.h>
35 #include <crypto/diffie_hellman.h>
36 #include <crypto/transform.h>
37
38 #define CRYPTO_MAX_ALG_LINE 120 /* characters */
39
40 /**
41 * Constructor function for crypters
42 */
43 typedef crypter_t* (*crypter_constructor_t)(encryption_algorithm_t algo,
44 size_t key_size);
45 /**
46 * Constructor function for aead transforms
47 */
48 typedef aead_t* (*aead_constructor_t)(encryption_algorithm_t algo,
49 size_t key_size);
50 /**
51 * Constructor function for signers
52 */
53 typedef signer_t* (*signer_constructor_t)(integrity_algorithm_t algo);
54
55 /**
56 * Constructor function for hashers
57 */
58 typedef hasher_t* (*hasher_constructor_t)(hash_algorithm_t algo);
59
60 /**
61 * Constructor function for pseudo random functions
62 */
63 typedef prf_t* (*prf_constructor_t)(pseudo_random_function_t algo);
64
65 /**
66 * Constructor function for source of randomness
67 */
68 typedef rng_t* (*rng_constructor_t)(rng_quality_t quality);
69
70 /**
71 * Constructor function for nonce generators
72 */
73 typedef nonce_gen_t* (*nonce_gen_constructor_t)();
74
75 /**
76 * Constructor function for diffie hellman
77 *
78 * The DH constructor accepts additional arguments for:
79 * - MODP_CUSTOM: chunk_t generator, chunk_t prime
80 */
81 typedef diffie_hellman_t* (*dh_constructor_t)(diffie_hellman_group_t group, ...);
82
83 /**
84 * Handles crypto modules and creates instances.
85 */
86 struct crypto_factory_t {
87
88 /**
89 * Create a crypter instance.
90 *
91 * @param algo encryption algorithm
92 * @param key_size length of the key in bytes
93 * @return crypter_t instance, NULL if not supported
94 */
95 crypter_t* (*create_crypter)(crypto_factory_t *this,
96 encryption_algorithm_t algo, size_t key_size);
97
98 /**
99 * Create a aead instance.
100 *
101 * @param algo encryption algorithm
102 * @param key_size length of the key in bytes
103 * @return aead_t instance, NULL if not supported
104 */
105 aead_t* (*create_aead)(crypto_factory_t *this,
106 encryption_algorithm_t algo, size_t key_size);
107
108 /**
109 * Create a symmetric signer instance.
110 *
111 * @param algo MAC algorithm to use
112 * @return signer_t instance, NULL if not supported
113 */
114 signer_t* (*create_signer)(crypto_factory_t *this,
115 integrity_algorithm_t algo);
116
117 /**
118 * Create a hasher instance.
119 *
120 * @param algo hash algorithm
121 * @return hasher_t instance, NULL if not supported
122 */
123 hasher_t* (*create_hasher)(crypto_factory_t *this, hash_algorithm_t algo);
124
125 /**
126 * Create a pseudo random function instance.
127 *
128 * @param algo PRF algorithm to use
129 * @return prf_t instance, NULL if not supported
130 */
131 prf_t* (*create_prf)(crypto_factory_t *this, pseudo_random_function_t algo);
132
133 /**
134 * Create a source of randomness.
135 *
136 * @param quality required randomness quality
137 * @return rng_t instance, NULL if no RNG with such a quality
138 */
139 rng_t* (*create_rng)(crypto_factory_t *this, rng_quality_t quality);
140
141 /**
142 * Create a nonce generator instance.
143 *
144 * @return nonce_gen_t instance, NULL if not supported
145 */
146 nonce_gen_t* (*create_nonce_gen)(crypto_factory_t *this);
147
148 /**
149 * Create a diffie hellman instance.
150 *
151 * Additional arguments are passed to the DH constructor.
152 *
153 * @param group diffie hellman group
154 * @return diffie_hellman_t instance, NULL if not supported
155 */
156 diffie_hellman_t* (*create_dh)(crypto_factory_t *this,
157 diffie_hellman_group_t group, ...);
158
159 /**
160 * Register a crypter constructor.
161 *
162 * @param algo algorithm to constructor
163 * @param plugin_name plugin that registered this algorithm
164 * @param create constructor function for that algorithm
165 * @return TRUE if registered, FALSE if test vector failed
166 */
167 bool (*add_crypter)(crypto_factory_t *this, encryption_algorithm_t algo,
168 const char *plugin_name, crypter_constructor_t create);
169
170 /**
171 * Unregister a crypter constructor.
172 *
173 * @param create constructor function to unregister
174 */
175 void (*remove_crypter)(crypto_factory_t *this, crypter_constructor_t create);
176
177 /**
178 * Unregister a aead constructor.
179 *
180 * @param create constructor function to unregister
181 */
182 void (*remove_aead)(crypto_factory_t *this, aead_constructor_t create);
183
184 /**
185 * Register a aead constructor.
186 *
187 * @param algo algorithm to constructor
188 * @param plugin_name plugin that registered this algorithm
189 * @param create constructor function for that algorithm
190 * @return TRUE if registered, FALSE if test vector failed
191 */
192 bool (*add_aead)(crypto_factory_t *this, encryption_algorithm_t algo,
193 const char *plugin_name, aead_constructor_t create);
194
195 /**
196 * Register a signer constructor.
197 *
198 * @param algo algorithm to constructor
199 * @param plugin_name plugin that registered this algorithm
200 * @param create constructor function for that algorithm
201 * @return TRUE if registered, FALSE if test vector failed
202 */
203 bool (*add_signer)(crypto_factory_t *this, integrity_algorithm_t algo,
204 const char *plugin_name, signer_constructor_t create);
205
206 /**
207 * Unregister a signer constructor.
208 *
209 * @param create constructor function to unregister
210 */
211 void (*remove_signer)(crypto_factory_t *this, signer_constructor_t create);
212
213 /**
214 * Register a hasher constructor.
215 *
216 * The first added hasher is the preferred hasher returned on
217 * create_hasher(HASH_PREFERRED).
218 *
219 * @param algo algorithm to constructor
220 * @param plugin_name plugin that registered this algorithm
221 * @param create constructor function for that algorithm
222 * @return TRUE if registered, FALSE if test vector failed
223 */
224 bool (*add_hasher)(crypto_factory_t *this, hash_algorithm_t algo,
225 const char *plugin_name, hasher_constructor_t create);
226
227 /**
228 * Unregister a hasher constructor.
229 *
230 * @param create constructor function to unregister
231 */
232 void (*remove_hasher)(crypto_factory_t *this, hasher_constructor_t create);
233
234 /**
235 * Register a prf constructor.
236 *
237 * @param algo algorithm to constructor
238 * @param plugin_name plugin that registered this algorithm
239 * @param create constructor function for that algorithm
240 * @return TRUE if registered, FALSE if test vector failed
241 */
242 bool (*add_prf)(crypto_factory_t *this, pseudo_random_function_t algo,
243 const char *plugin_name, prf_constructor_t create);
244
245 /**
246 * Unregister a prf constructor.
247 *
248 * @param create constructor function to unregister
249 */
250 void (*remove_prf)(crypto_factory_t *this, prf_constructor_t create);
251
252 /**
253 * Register a source of randomness.
254 *
255 * @param quality quality of randomness this RNG serves
256 * @param plugin_name plugin that registered this algorithm
257 * @param create constructor function for such a quality
258 * @return TRUE if registered, FALSE if test vector failed
259 */
260 bool (*add_rng)(crypto_factory_t *this, rng_quality_t quality,
261 const char *plugin_name, rng_constructor_t create);
262
263 /**
264 * Unregister a source of randomness.
265 *
266 * @param create constructor function to unregister
267 */
268 void (*remove_rng)(crypto_factory_t *this, rng_constructor_t create);
269
270 /**
271 * Register a nonce generator.
272 *
273 * @param plugin_name plugin that registered this algorithm
274 * @param create constructor function for that nonce generator
275 * @return TRUE if registered, FALSE if test vector failed
276 */
277 bool (*add_nonce_gen)(crypto_factory_t *this, const char *plugin_name,
278 nonce_gen_constructor_t create);
279
280 /**
281 * Unregister a nonce generator.
282 *
283 * @param create constructor function to unregister
284 */
285 void (*remove_nonce_gen)(crypto_factory_t *this,
286 nonce_gen_constructor_t create);
287
288 /**
289 * Register a diffie hellman constructor.
290 *
291 * @param group dh group to constructor
292 * @param plugin_name plugin that registered this algorithm
293 * @param create constructor function for that algorithm
294 * @return TRUE if registered, FALSE if test vector failed
295 */
296 bool (*add_dh)(crypto_factory_t *this, diffie_hellman_group_t group,
297 const char *plugin_name, dh_constructor_t create);
298
299 /**
300 * Unregister a diffie hellman constructor.
301 *
302 * @param create constructor function to unregister
303 */
304 void (*remove_dh)(crypto_factory_t *this, dh_constructor_t create);
305
306 /**
307 * Create an enumerator over all registered crypter algorithms.
308 *
309 * @return enumerator over encryption_algorithm_t, plugin
310 */
311 enumerator_t* (*create_crypter_enumerator)(crypto_factory_t *this);
312
313 /**
314 * Create an enumerator over all registered aead algorithms.
315 *
316 * @return enumerator over encryption_algorithm_t, plugin
317 */
318 enumerator_t* (*create_aead_enumerator)(crypto_factory_t *this);
319
320 /**
321 * Create an enumerator over all registered signer algorithms.
322 *
323 * @return enumerator over integrity_algorithm_t, plugin
324 */
325 enumerator_t* (*create_signer_enumerator)(crypto_factory_t *this);
326
327 /**
328 * Create an enumerator over all registered hasher algorithms.
329 *
330 * @return enumerator over hash_algorithm_t, plugin
331 */
332 enumerator_t* (*create_hasher_enumerator)(crypto_factory_t *this);
333
334 /**
335 * Create an enumerator over all registered PRFs.
336 *
337 * @return enumerator over pseudo_random_function_t, plugin
338 */
339 enumerator_t* (*create_prf_enumerator)(crypto_factory_t *this);
340
341 /**
342 * Create an enumerator over all registered diffie hellman groups.
343 *
344 * @return enumerator over diffie_hellman_group_t, plugin
345 */
346 enumerator_t* (*create_dh_enumerator)(crypto_factory_t *this);
347
348 /**
349 * Create an enumerator over all registered random generators.
350 *
351 * @return enumerator over rng_quality_t, plugin
352 */
353 enumerator_t* (*create_rng_enumerator)(crypto_factory_t *this);
354
355 /**
356 * Create an enumerator over all registered nonce generators.
357 *
358 * @return enumerator over plugin
359 */
360 enumerator_t* (*create_nonce_gen_enumerator)(crypto_factory_t *this);
361
362 /**
363 * Add a test vector to the crypto factory.
364 *
365 * @param type type of the test vector
366 * @param vector pointer to a test vector, defined in crypto_tester.h
367 */
368 void (*add_test_vector)(crypto_factory_t *this, transform_type_t type,
369 void *vector);
370
371 /**
372 * Get the number of test vector failures encountered during add.
373 *
374 * This counter gets incremented only if transforms get tested during
375 * registration.
376 *
377 * @return number of failed test vectors
378 */
379 u_int (*get_test_vector_failures)(crypto_factory_t *this);
380
381 /**
382 * Destroy a crypto_factory instance.
383 */
384 void (*destroy)(crypto_factory_t *this);
385 };
386
387 /**
388 * Create a crypto_factory instance.
389 */
390 crypto_factory_t *crypto_factory_create();
391
392 #endif /** CRYPTO_FACTORY_H_ @}*/