aead: Support custom AEAD salt sizes
[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, size_t salt_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 * @param salt_size size of salt, implicit part of the nonce
104 * @return aead_t instance, NULL if not supported
105 */
106 aead_t* (*create_aead)(crypto_factory_t *this,
107 encryption_algorithm_t algo,
108 size_t key_size, size_t salt_size);
109
110 /**
111 * Create a symmetric signer instance.
112 *
113 * @param algo MAC algorithm to use
114 * @return signer_t instance, NULL if not supported
115 */
116 signer_t* (*create_signer)(crypto_factory_t *this,
117 integrity_algorithm_t algo);
118
119 /**
120 * Create a hasher instance.
121 *
122 * @param algo hash algorithm
123 * @return hasher_t instance, NULL if not supported
124 */
125 hasher_t* (*create_hasher)(crypto_factory_t *this, hash_algorithm_t algo);
126
127 /**
128 * Create a pseudo random function instance.
129 *
130 * @param algo PRF algorithm to use
131 * @return prf_t instance, NULL if not supported
132 */
133 prf_t* (*create_prf)(crypto_factory_t *this, pseudo_random_function_t algo);
134
135 /**
136 * Create a source of randomness.
137 *
138 * @param quality required randomness quality
139 * @return rng_t instance, NULL if no RNG with such a quality
140 */
141 rng_t* (*create_rng)(crypto_factory_t *this, rng_quality_t quality);
142
143 /**
144 * Create a nonce generator instance.
145 *
146 * @return nonce_gen_t instance, NULL if not supported
147 */
148 nonce_gen_t* (*create_nonce_gen)(crypto_factory_t *this);
149
150 /**
151 * Create a diffie hellman instance.
152 *
153 * Additional arguments are passed to the DH constructor.
154 *
155 * @param group diffie hellman group
156 * @return diffie_hellman_t instance, NULL if not supported
157 */
158 diffie_hellman_t* (*create_dh)(crypto_factory_t *this,
159 diffie_hellman_group_t group, ...);
160
161 /**
162 * Register a crypter constructor.
163 *
164 * @param algo algorithm to constructor
165 * @param plugin_name plugin that registered this algorithm
166 * @param create constructor function for that algorithm
167 * @return TRUE if registered, FALSE if test vector failed
168 */
169 bool (*add_crypter)(crypto_factory_t *this, encryption_algorithm_t algo,
170 const char *plugin_name, crypter_constructor_t create);
171
172 /**
173 * Unregister a crypter constructor.
174 *
175 * @param create constructor function to unregister
176 */
177 void (*remove_crypter)(crypto_factory_t *this, crypter_constructor_t create);
178
179 /**
180 * Unregister a aead constructor.
181 *
182 * @param create constructor function to unregister
183 */
184 void (*remove_aead)(crypto_factory_t *this, aead_constructor_t create);
185
186 /**
187 * Register a aead constructor.
188 *
189 * @param algo algorithm to constructor
190 * @param plugin_name plugin that registered this algorithm
191 * @param create constructor function for that algorithm
192 * @return TRUE if registered, FALSE if test vector failed
193 */
194 bool (*add_aead)(crypto_factory_t *this, encryption_algorithm_t algo,
195 const char *plugin_name, aead_constructor_t create);
196
197 /**
198 * Register a signer constructor.
199 *
200 * @param algo algorithm to constructor
201 * @param plugin_name plugin that registered this algorithm
202 * @param create constructor function for that algorithm
203 * @return TRUE if registered, FALSE if test vector failed
204 */
205 bool (*add_signer)(crypto_factory_t *this, integrity_algorithm_t algo,
206 const char *plugin_name, signer_constructor_t create);
207
208 /**
209 * Unregister a signer constructor.
210 *
211 * @param create constructor function to unregister
212 */
213 void (*remove_signer)(crypto_factory_t *this, signer_constructor_t create);
214
215 /**
216 * Register a hasher constructor.
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 TRUE if registered, FALSE if test vector failed
222 */
223 bool (*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 TRUE if registered, FALSE if test vector failed
240 */
241 bool (*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 * @return TRUE if registered, FALSE if test vector failed
258 */
259 bool (*add_rng)(crypto_factory_t *this, rng_quality_t quality,
260 const char *plugin_name, rng_constructor_t create);
261
262 /**
263 * Unregister a source of randomness.
264 *
265 * @param create constructor function to unregister
266 */
267 void (*remove_rng)(crypto_factory_t *this, rng_constructor_t create);
268
269 /**
270 * Register a nonce generator.
271 *
272 * @param plugin_name plugin that registered this algorithm
273 * @param create constructor function for that nonce generator
274 * @return TRUE if registered, FALSE if test vector failed
275 */
276 bool (*add_nonce_gen)(crypto_factory_t *this, const char *plugin_name,
277 nonce_gen_constructor_t create);
278
279 /**
280 * Unregister a nonce generator.
281 *
282 * @param create constructor function to unregister
283 */
284 void (*remove_nonce_gen)(crypto_factory_t *this,
285 nonce_gen_constructor_t create);
286
287 /**
288 * Register a diffie hellman constructor.
289 *
290 * @param group dh group to constructor
291 * @param plugin_name plugin that registered this algorithm
292 * @param create constructor function for that algorithm
293 * @return TRUE if registered, FALSE if test vector failed
294 */
295 bool (*add_dh)(crypto_factory_t *this, diffie_hellman_group_t group,
296 const char *plugin_name, dh_constructor_t create);
297
298 /**
299 * Unregister a diffie hellman constructor.
300 *
301 * @param create constructor function to unregister
302 */
303 void (*remove_dh)(crypto_factory_t *this, dh_constructor_t create);
304
305 /**
306 * Create an enumerator over all registered crypter algorithms.
307 *
308 * @return enumerator over encryption_algorithm_t, plugin
309 */
310 enumerator_t* (*create_crypter_enumerator)(crypto_factory_t *this);
311
312 /**
313 * Create an enumerator over all registered aead algorithms.
314 *
315 * @return enumerator over encryption_algorithm_t, plugin
316 */
317 enumerator_t* (*create_aead_enumerator)(crypto_factory_t *this);
318
319 /**
320 * Create an enumerator over all registered signer algorithms.
321 *
322 * @return enumerator over integrity_algorithm_t, plugin
323 */
324 enumerator_t* (*create_signer_enumerator)(crypto_factory_t *this);
325
326 /**
327 * Create an enumerator over all registered hasher algorithms.
328 *
329 * @return enumerator over hash_algorithm_t, plugin
330 */
331 enumerator_t* (*create_hasher_enumerator)(crypto_factory_t *this);
332
333 /**
334 * Create an enumerator over all registered PRFs.
335 *
336 * @return enumerator over pseudo_random_function_t, plugin
337 */
338 enumerator_t* (*create_prf_enumerator)(crypto_factory_t *this);
339
340 /**
341 * Create an enumerator over all registered diffie hellman groups.
342 *
343 * @return enumerator over diffie_hellman_group_t, plugin
344 */
345 enumerator_t* (*create_dh_enumerator)(crypto_factory_t *this);
346
347 /**
348 * Create an enumerator over all registered random generators.
349 *
350 * @return enumerator over rng_quality_t, plugin
351 */
352 enumerator_t* (*create_rng_enumerator)(crypto_factory_t *this);
353
354 /**
355 * Create an enumerator over all registered nonce generators.
356 *
357 * @return enumerator over plugin
358 */
359 enumerator_t* (*create_nonce_gen_enumerator)(crypto_factory_t *this);
360
361 /**
362 * Add a test vector to the crypto factory.
363 *
364 * @param type type of the test vector
365 * @param vector pointer to a test vector, defined in crypto_tester.h
366 */
367 void (*add_test_vector)(crypto_factory_t *this, transform_type_t type,
368 void *vector);
369
370 /**
371 * Get the number of test vector failures encountered during add.
372 *
373 * This counter gets incremented only if transforms get tested during
374 * registration.
375 *
376 * @return number of failed test vectors
377 */
378 u_int (*get_test_vector_failures)(crypto_factory_t *this);
379
380 /**
381 * Destroy a crypto_factory instance.
382 */
383 void (*destroy)(crypto_factory_t *this);
384 };
385
386 /**
387 * Create a crypto_factory instance.
388 */
389 crypto_factory_t *crypto_factory_create();
390
391 #endif /** CRYPTO_FACTORY_H_ @}*/