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