2 * Copyright (C) 2008 Martin Willi
3 * Copyright (C) 2016 Andreas Steffen
4 * HSR Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * @defgroup crypto_factory crypto_factory
22 #ifndef CRYPTO_FACTORY_H_
23 #define CRYPTO_FACTORY_H_
25 typedef struct crypto_factory_t crypto_factory_t
;
28 #include <collections/enumerator.h>
29 #include <crypto/crypters/crypter.h>
30 #include <crypto/aead.h>
31 #include <crypto/signers/signer.h>
32 #include <crypto/hashers/hasher.h>
33 #include <crypto/prfs/prf.h>
34 #include <crypto/rngs/rng.h>
35 #include <crypto/xofs/xof.h>
36 #include <crypto/nonce_gen.h>
37 #include <crypto/diffie_hellman.h>
38 #include <crypto/transform.h>
40 #define CRYPTO_MAX_ALG_LINE 120 /* characters */
43 * Constructor function for crypters
45 typedef crypter_t
* (*crypter_constructor_t
)(encryption_algorithm_t algo
,
48 * Constructor function for aead transforms
50 typedef aead_t
* (*aead_constructor_t
)(encryption_algorithm_t algo
,
51 size_t key_size
, size_t salt_size
);
53 * Constructor function for signers
55 typedef signer_t
* (*signer_constructor_t
)(integrity_algorithm_t algo
);
58 * Constructor function for hashers
60 typedef hasher_t
* (*hasher_constructor_t
)(hash_algorithm_t algo
);
63 * Constructor function for pseudo random functions
65 typedef prf_t
* (*prf_constructor_t
)(pseudo_random_function_t algo
);
68 * Constructor function for pseudo random functions
70 typedef xof_t
* (*xof_constructor_t
)(ext_out_function_t algo
);
73 * Constructor function for source of randomness
75 typedef rng_t
* (*rng_constructor_t
)(rng_quality_t quality
);
78 * Constructor function for nonce generators
80 typedef nonce_gen_t
* (*nonce_gen_constructor_t
)();
83 * Constructor function for diffie hellman
85 * The DH constructor accepts additional arguments for:
86 * - MODP_CUSTOM: chunk_t generator, chunk_t prime
88 typedef diffie_hellman_t
* (*dh_constructor_t
)(diffie_hellman_group_t group
, ...);
91 * Handles crypto modules and creates instances.
93 struct crypto_factory_t
{
96 * Create a crypter instance.
98 * @param algo encryption algorithm
99 * @param key_size length of the key in bytes
100 * @return crypter_t instance, NULL if not supported
102 crypter_t
* (*create_crypter
)(crypto_factory_t
*this,
103 encryption_algorithm_t algo
, size_t key_size
);
106 * Create a aead instance.
108 * @param algo encryption algorithm
109 * @param key_size length of the key in bytes
110 * @param salt_size size of salt, implicit part of the nonce
111 * @return aead_t instance, NULL if not supported
113 aead_t
* (*create_aead
)(crypto_factory_t
*this,
114 encryption_algorithm_t algo
,
115 size_t key_size
, size_t salt_size
);
118 * Create a symmetric signer instance.
120 * @param algo MAC algorithm to use
121 * @return signer_t instance, NULL if not supported
123 signer_t
* (*create_signer
)(crypto_factory_t
*this,
124 integrity_algorithm_t algo
);
127 * Create a hasher instance.
129 * @param algo hash algorithm
130 * @return hasher_t instance, NULL if not supported
132 hasher_t
* (*create_hasher
)(crypto_factory_t
*this, hash_algorithm_t algo
);
135 * Create a pseudo random function instance.
137 * @param algo PRF algorithm to use
138 * @return prf_t instance, NULL if not supported
140 prf_t
* (*create_prf
)(crypto_factory_t
*this, pseudo_random_function_t algo
);
143 * Create an extended output function instance.
145 * @param algo XOF algorithm to use
146 * @return xof_t instance, NULL if not supported
148 xof_t
* (*create_xof
)(crypto_factory_t
*this, ext_out_function_t algo
);
151 * Create a source of randomness.
153 * @param quality required randomness quality
154 * @return rng_t instance, NULL if no RNG with such a quality
156 rng_t
* (*create_rng
)(crypto_factory_t
*this, rng_quality_t quality
);
159 * Create a nonce generator instance.
161 * @return nonce_gen_t instance, NULL if not supported
163 nonce_gen_t
* (*create_nonce_gen
)(crypto_factory_t
*this);
166 * Create a diffie hellman instance.
168 * Additional arguments are passed to the DH constructor.
170 * @param group diffie hellman group
171 * @return diffie_hellman_t instance, NULL if not supported
173 diffie_hellman_t
* (*create_dh
)(crypto_factory_t
*this,
174 diffie_hellman_group_t group
, ...);
177 * Register a crypter constructor.
179 * @param algo algorithm to constructor
180 * @param key size key size to peform benchmarking for
181 * @param plugin_name plugin that registered this algorithm
182 * @param create constructor function for that algorithm
183 * @return TRUE if registered, FALSE if test vector failed
185 bool (*add_crypter
)(crypto_factory_t
*this, encryption_algorithm_t algo
,
186 size_t key_size
, const char *plugin_name
,
187 crypter_constructor_t create
);
190 * Unregister a crypter constructor.
192 * @param create constructor function to unregister
194 void (*remove_crypter
)(crypto_factory_t
*this, crypter_constructor_t create
);
197 * Unregister a aead constructor.
199 * @param create constructor function to unregister
201 void (*remove_aead
)(crypto_factory_t
*this, aead_constructor_t create
);
204 * Register a aead constructor.
206 * @param algo algorithm to constructor
207 * @param key size key size to peform benchmarking for
208 * @param plugin_name plugin that registered this algorithm
209 * @param create constructor function for that algorithm
210 * @return TRUE if registered, FALSE if test vector failed
212 bool (*add_aead
)(crypto_factory_t
*this, encryption_algorithm_t algo
,
213 size_t key_size
, const char *plugin_name
,
214 aead_constructor_t create
);
217 * Register a signer constructor.
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
224 bool (*add_signer
)(crypto_factory_t
*this, integrity_algorithm_t algo
,
225 const char *plugin_name
, signer_constructor_t create
);
228 * Unregister a signer constructor.
230 * @param create constructor function to unregister
232 void (*remove_signer
)(crypto_factory_t
*this, signer_constructor_t create
);
235 * Register a hasher constructor.
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
242 bool (*add_hasher
)(crypto_factory_t
*this, hash_algorithm_t algo
,
243 const char *plugin_name
, hasher_constructor_t create
);
246 * Unregister a hasher constructor.
248 * @param create constructor function to unregister
250 void (*remove_hasher
)(crypto_factory_t
*this, hasher_constructor_t create
);
253 * Register a prf constructor.
255 * @param algo algorithm to constructor
256 * @param plugin_name plugin that registered this algorithm
257 * @param create constructor function for that algorithm
258 * @return TRUE if registered, FALSE if test vector failed
260 bool (*add_prf
)(crypto_factory_t
*this, pseudo_random_function_t algo
,
261 const char *plugin_name
, prf_constructor_t create
);
264 * Unregister a prf constructor.
266 * @param create constructor function to unregister
268 void (*remove_prf
)(crypto_factory_t
*this, prf_constructor_t create
);
271 * Register an xof constructor.
273 * @param algo algorithm to constructor
274 * @param plugin_name plugin that registered this algorithm
275 * @param create constructor function for that algorithm
276 * @return TRUE if registered, FALSE if test vector failed
278 bool (*add_xof
)(crypto_factory_t
*this, ext_out_function_t algo
,
279 const char *plugin_name
, xof_constructor_t create
);
282 * Unregister an xof constructor.
284 * @param create constructor function to unregister
286 void (*remove_xof
)(crypto_factory_t
*this, xof_constructor_t create
);
289 * Register a source of randomness.
291 * @param quality quality of randomness this RNG serves
292 * @param plugin_name plugin that registered this algorithm
293 * @param create constructor function for such a quality
294 * @return TRUE if registered, FALSE if test vector failed
296 bool (*add_rng
)(crypto_factory_t
*this, rng_quality_t quality
,
297 const char *plugin_name
, rng_constructor_t create
);
300 * Unregister a source of randomness.
302 * @param create constructor function to unregister
304 void (*remove_rng
)(crypto_factory_t
*this, rng_constructor_t create
);
307 * Register a nonce generator.
309 * @param plugin_name plugin that registered this algorithm
310 * @param create constructor function for that nonce generator
311 * @return TRUE if registered, FALSE if test vector failed
313 bool (*add_nonce_gen
)(crypto_factory_t
*this, const char *plugin_name
,
314 nonce_gen_constructor_t create
);
317 * Unregister a nonce generator.
319 * @param create constructor function to unregister
321 void (*remove_nonce_gen
)(crypto_factory_t
*this,
322 nonce_gen_constructor_t create
);
325 * Register a diffie hellman constructor.
327 * @param group dh group to constructor
328 * @param plugin_name plugin that registered this algorithm
329 * @param create constructor function for that algorithm
330 * @return TRUE if registered, FALSE if test vector failed
332 bool (*add_dh
)(crypto_factory_t
*this, diffie_hellman_group_t group
,
333 const char *plugin_name
, dh_constructor_t create
);
336 * Unregister a diffie hellman constructor.
338 * @param create constructor function to unregister
340 void (*remove_dh
)(crypto_factory_t
*this, dh_constructor_t create
);
343 * Create an enumerator over all registered crypter algorithms.
345 * @return enumerator over encryption_algorithm_t, plugin
347 enumerator_t
* (*create_crypter_enumerator
)(crypto_factory_t
*this);
350 * Create an enumerator over all registered aead algorithms.
352 * @return enumerator over encryption_algorithm_t, plugin
354 enumerator_t
* (*create_aead_enumerator
)(crypto_factory_t
*this);
357 * Create an enumerator over all registered signer algorithms.
359 * @return enumerator over integrity_algorithm_t, plugin
361 enumerator_t
* (*create_signer_enumerator
)(crypto_factory_t
*this);
364 * Create an enumerator over all registered hasher algorithms.
366 * @return enumerator over hash_algorithm_t, plugin
368 enumerator_t
* (*create_hasher_enumerator
)(crypto_factory_t
*this);
371 * Create an enumerator over all registered PRFs.
373 * @return enumerator over pseudo_random_function_t, plugin
375 enumerator_t
* (*create_prf_enumerator
)(crypto_factory_t
*this);
378 * Create an enumerator over all registered XOFs.
380 * @return enumerator over ext_out_function_t, plugin
382 enumerator_t
* (*create_xof_enumerator
)(crypto_factory_t
*this);
385 * Create an enumerator over all registered diffie hellman groups.
387 * @return enumerator over diffie_hellman_group_t, plugin
389 enumerator_t
* (*create_dh_enumerator
)(crypto_factory_t
*this);
392 * Create an enumerator over all registered random generators.
394 * @return enumerator over rng_quality_t, plugin
396 enumerator_t
* (*create_rng_enumerator
)(crypto_factory_t
*this);
399 * Create an enumerator over all registered nonce generators.
401 * @return enumerator over plugin
403 enumerator_t
* (*create_nonce_gen_enumerator
)(crypto_factory_t
*this);
406 * Add a test vector to the crypto factory.
408 * @param type type of the test vector
409 * @param vector pointer to a test vector, defined in crypto_tester.h
411 void (*add_test_vector
)(crypto_factory_t
*this, transform_type_t type
,
415 * Create an enumerator verifying transforms using known test vectors.
417 * The resulting enumerator enumerates over an u_int with the type
418 * specific transform identifier, the plugin name providing the transform,
419 * and a boolean value indicating success/failure for the given transform.
421 * @param type transform type to test
422 * @return enumerator over (u_int, char*, bool)
424 enumerator_t
* (*create_verify_enumerator
)(crypto_factory_t
*this,
425 transform_type_t type
);
428 * Destroy a crypto_factory instance.
430 void (*destroy
)(crypto_factory_t
*this);
434 * Create a crypto_factory instance.
436 crypto_factory_t
*crypto_factory_create();
438 #endif /** CRYPTO_FACTORY_H_ @}*/