2 * Copyright (C) 2008 Martin Willi
3 * Copyright (C) 2016-2019 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/drbgs/drbg.h>
37 #include <crypto/nonce_gen.h>
38 #include <crypto/diffie_hellman.h>
39 #include <crypto/transform.h>
41 #define CRYPTO_MAX_ALG_LINE 120 /* characters */
44 * Constructor function for crypters
46 typedef crypter_t
* (*crypter_constructor_t
)(encryption_algorithm_t algo
,
49 * Constructor function for aead transforms
51 typedef aead_t
* (*aead_constructor_t
)(encryption_algorithm_t algo
,
52 size_t key_size
, size_t salt_size
);
54 * Constructor function for signers
56 typedef signer_t
* (*signer_constructor_t
)(integrity_algorithm_t algo
);
59 * Constructor function for hashers
61 typedef hasher_t
* (*hasher_constructor_t
)(hash_algorithm_t algo
);
64 * Constructor function for pseudo random functions
66 typedef prf_t
* (*prf_constructor_t
)(pseudo_random_function_t algo
);
69 * Constructor function for extended output functions
71 typedef xof_t
* (*xof_constructor_t
)(ext_out_function_t algo
);
74 * Constructor function for deterministic random bit generators
76 typedef drbg_t
* (*drbg_constructor_t
)(drbg_type_t type
, uint32_t strength
,
77 rng_t
*entropy
, chunk_t personalization_str
);
80 * Constructor function for source of randomness
82 typedef rng_t
* (*rng_constructor_t
)(rng_quality_t quality
);
85 * Constructor function for nonce generators
87 typedef nonce_gen_t
* (*nonce_gen_constructor_t
)();
90 * Constructor function for diffie hellman
92 * The DH constructor accepts additional arguments for:
93 * - MODP_CUSTOM: chunk_t generator, chunk_t prime
95 typedef diffie_hellman_t
* (*dh_constructor_t
)(diffie_hellman_group_t group
, ...);
98 * Handles crypto modules and creates instances.
100 struct crypto_factory_t
{
103 * Create a crypter instance.
105 * @param algo encryption algorithm
106 * @param key_size length of the key in bytes
107 * @return crypter_t instance, NULL if not supported
109 crypter_t
* (*create_crypter
)(crypto_factory_t
*this,
110 encryption_algorithm_t algo
, size_t key_size
);
113 * Create a aead instance.
115 * @param algo encryption algorithm
116 * @param key_size length of the key in bytes
117 * @param salt_size size of salt, implicit part of the nonce
118 * @return aead_t instance, NULL if not supported
120 aead_t
* (*create_aead
)(crypto_factory_t
*this,
121 encryption_algorithm_t algo
,
122 size_t key_size
, size_t salt_size
);
125 * Create a symmetric signer instance.
127 * @param algo MAC algorithm to use
128 * @return signer_t instance, NULL if not supported
130 signer_t
* (*create_signer
)(crypto_factory_t
*this,
131 integrity_algorithm_t algo
);
134 * Create a hasher instance.
136 * @param algo hash algorithm
137 * @return hasher_t instance, NULL if not supported
139 hasher_t
* (*create_hasher
)(crypto_factory_t
*this, hash_algorithm_t algo
);
142 * Create a pseudo random function instance.
144 * @param algo PRF algorithm to use
145 * @return prf_t instance, NULL if not supported
147 prf_t
* (*create_prf
)(crypto_factory_t
*this, pseudo_random_function_t algo
);
150 * Create an extended output function instance.
152 * @param algo XOF algorithm to use
153 * @return xof_t instance, NULL if not supported
155 xof_t
* (*create_xof
)(crypto_factory_t
*this, ext_out_function_t algo
);
158 * Create a deterministic random bit generator instance.
160 * @param type DRBG type to use
161 * @param strength security strength in bits
162 * @param entropy entropy source to be used (adopted)
163 * @param personalization_str optional personalization string
164 * @return drbg_t instance, NULL if not supported
166 drbg_t
* (*create_drbg
)(crypto_factory_t
*this, drbg_type_t type
,
167 uint32_t strength
, rng_t
*entropy
,
168 chunk_t personalization_str
);
171 * Create a source of randomness.
173 * @param quality required randomness quality
174 * @return rng_t instance, NULL if no RNG with such a quality
176 rng_t
* (*create_rng
)(crypto_factory_t
*this, rng_quality_t quality
);
179 * Create a nonce generator instance.
181 * @return nonce_gen_t instance, NULL if not supported
183 nonce_gen_t
* (*create_nonce_gen
)(crypto_factory_t
*this);
186 * Create a diffie hellman instance.
188 * Additional arguments are passed to the DH constructor.
190 * @param group diffie hellman group
191 * @return diffie_hellman_t instance, NULL if not supported
193 diffie_hellman_t
* (*create_dh
)(crypto_factory_t
*this,
194 diffie_hellman_group_t group
, ...);
197 * Register a crypter constructor.
199 * @param algo algorithm to constructor
200 * @param key size key size to perform benchmarking for
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
205 bool (*add_crypter
)(crypto_factory_t
*this, encryption_algorithm_t algo
,
206 size_t key_size
, const char *plugin_name
,
207 crypter_constructor_t create
);
210 * Unregister a crypter constructor.
212 * @param create constructor function to unregister
214 void (*remove_crypter
)(crypto_factory_t
*this, crypter_constructor_t create
);
217 * Unregister a aead constructor.
219 * @param create constructor function to unregister
221 void (*remove_aead
)(crypto_factory_t
*this, aead_constructor_t create
);
224 * Register a aead constructor.
226 * @param algo algorithm to constructor
227 * @param key size key size to perform benchmarking for
228 * @param plugin_name plugin that registered this algorithm
229 * @param create constructor function for that algorithm
230 * @return TRUE if registered, FALSE if test vector failed
232 bool (*add_aead
)(crypto_factory_t
*this, encryption_algorithm_t algo
,
233 size_t key_size
, const char *plugin_name
,
234 aead_constructor_t create
);
237 * Register a signer constructor.
239 * @param algo algorithm to constructor
240 * @param plugin_name plugin that registered this algorithm
241 * @param create constructor function for that algorithm
242 * @return TRUE if registered, FALSE if test vector failed
244 bool (*add_signer
)(crypto_factory_t
*this, integrity_algorithm_t algo
,
245 const char *plugin_name
, signer_constructor_t create
);
248 * Unregister a signer constructor.
250 * @param create constructor function to unregister
252 void (*remove_signer
)(crypto_factory_t
*this, signer_constructor_t create
);
255 * Register a hasher constructor.
257 * @param algo algorithm to constructor
258 * @param plugin_name plugin that registered this algorithm
259 * @param create constructor function for that algorithm
260 * @return TRUE if registered, FALSE if test vector failed
262 bool (*add_hasher
)(crypto_factory_t
*this, hash_algorithm_t algo
,
263 const char *plugin_name
, hasher_constructor_t create
);
266 * Unregister a hasher constructor.
268 * @param create constructor function to unregister
270 void (*remove_hasher
)(crypto_factory_t
*this, hasher_constructor_t create
);
273 * Register a prf constructor.
275 * @param algo algorithm to constructor
276 * @param plugin_name plugin that registered this algorithm
277 * @param create constructor function for that algorithm
278 * @return TRUE if registered, FALSE if test vector failed
280 bool (*add_prf
)(crypto_factory_t
*this, pseudo_random_function_t algo
,
281 const char *plugin_name
, prf_constructor_t create
);
284 * Unregister a prf constructor.
286 * @param create constructor function to unregister
288 void (*remove_prf
)(crypto_factory_t
*this, prf_constructor_t create
);
291 * Register an xof constructor.
293 * @param algo algorithm to constructor
294 * @param plugin_name plugin that registered this algorithm
295 * @param create constructor function for that algorithm
296 * @return TRUE if registered, FALSE if test vector failed
298 bool (*add_xof
)(crypto_factory_t
*this, ext_out_function_t algo
,
299 const char *plugin_name
, xof_constructor_t create
);
302 * Unregister an xof constructor.
304 * @param create constructor function to unregister
306 void (*remove_xof
)(crypto_factory_t
*this, xof_constructor_t create
);
309 * Register a drbg constructor.
311 * @param type type to constructor
312 * @param plugin_name plugin that registered this algorithm
313 * @param create constructor function for that algorithm
314 * @return TRUE if registered, FALSE if test vector failed
316 bool (*add_drbg
)(crypto_factory_t
*this, drbg_type_t type
,
317 const char *plugin_name
, drbg_constructor_t create
);
320 * Unregister a drbg constructor.
322 * @param create constructor function to unregister
324 void (*remove_drbg
)(crypto_factory_t
*this, drbg_constructor_t create
);
327 * Register a source of randomness.
329 * @param quality quality of randomness this RNG serves
330 * @param plugin_name plugin that registered this algorithm
331 * @param create constructor function for such a quality
332 * @return TRUE if registered, FALSE if test vector failed
334 bool (*add_rng
)(crypto_factory_t
*this, rng_quality_t quality
,
335 const char *plugin_name
, rng_constructor_t create
);
338 * Unregister a source of randomness.
340 * @param create constructor function to unregister
342 void (*remove_rng
)(crypto_factory_t
*this, rng_constructor_t create
);
345 * Register a nonce generator.
347 * @param plugin_name plugin that registered this algorithm
348 * @param create constructor function for that nonce generator
349 * @return TRUE if registered, FALSE if test vector failed
351 bool (*add_nonce_gen
)(crypto_factory_t
*this, const char *plugin_name
,
352 nonce_gen_constructor_t create
);
355 * Unregister a nonce generator.
357 * @param create constructor function to unregister
359 void (*remove_nonce_gen
)(crypto_factory_t
*this,
360 nonce_gen_constructor_t create
);
363 * Register a diffie hellman constructor.
365 * @param group dh group to constructor
366 * @param plugin_name plugin that registered this algorithm
367 * @param create constructor function for that algorithm
368 * @return TRUE if registered, FALSE if test vector failed
370 bool (*add_dh
)(crypto_factory_t
*this, diffie_hellman_group_t group
,
371 const char *plugin_name
, dh_constructor_t create
);
374 * Unregister a diffie hellman constructor.
376 * @param create constructor function to unregister
378 void (*remove_dh
)(crypto_factory_t
*this, dh_constructor_t create
);
381 * Create an enumerator over all registered crypter algorithms.
383 * @return enumerator over encryption_algorithm_t, plugin
385 enumerator_t
* (*create_crypter_enumerator
)(crypto_factory_t
*this);
388 * Create an enumerator over all registered aead algorithms.
390 * @return enumerator over encryption_algorithm_t, plugin
392 enumerator_t
* (*create_aead_enumerator
)(crypto_factory_t
*this);
395 * Create an enumerator over all registered signer algorithms.
397 * @return enumerator over integrity_algorithm_t, plugin
399 enumerator_t
* (*create_signer_enumerator
)(crypto_factory_t
*this);
402 * Create an enumerator over all registered hasher algorithms.
404 * @return enumerator over hash_algorithm_t, plugin
406 enumerator_t
* (*create_hasher_enumerator
)(crypto_factory_t
*this);
409 * Create an enumerator over all registered PRFs.
411 * @return enumerator over pseudo_random_function_t, plugin
413 enumerator_t
* (*create_prf_enumerator
)(crypto_factory_t
*this);
416 * Create an enumerator over all registered XOFs.
418 * @return enumerator over ext_out_function_t, plugin
420 enumerator_t
* (*create_xof_enumerator
)(crypto_factory_t
*this);
423 * Create an enumerator over all registered DRBGs.
425 * @return enumerator over drbg_type_t, plugin
427 enumerator_t
* (*create_drbg_enumerator
)(crypto_factory_t
*this);
430 * Create an enumerator over all registered diffie hellman groups.
432 * @return enumerator over diffie_hellman_group_t, plugin
434 enumerator_t
* (*create_dh_enumerator
)(crypto_factory_t
*this);
437 * Create an enumerator over all registered random generators.
439 * @return enumerator over rng_quality_t, plugin
441 enumerator_t
* (*create_rng_enumerator
)(crypto_factory_t
*this);
444 * Create an enumerator over all registered nonce generators.
446 * @return enumerator over plugin
448 enumerator_t
* (*create_nonce_gen_enumerator
)(crypto_factory_t
*this);
451 * Add a test vector to the crypto factory.
453 * @param type type of the test vector
454 * @param vector pointer to a test vector, defined in crypto_tester.h
456 void (*add_test_vector
)(crypto_factory_t
*this, transform_type_t type
,
460 * Create an enumerator verifying transforms using known test vectors.
462 * The resulting enumerator enumerates over an u_int with the type
463 * specific transform identifier, the plugin name providing the transform,
464 * and a boolean value indicating success/failure for the given transform.
466 * @param type transform type to test
467 * @return enumerator over (u_int, char*, bool)
469 enumerator_t
* (*create_verify_enumerator
)(crypto_factory_t
*this,
470 transform_type_t type
);
473 * Destroy a crypto_factory instance.
475 void (*destroy
)(crypto_factory_t
*this);
479 * Create a crypto_factory instance.
481 crypto_factory_t
*crypto_factory_create();
483 #endif /** CRYPTO_FACTORY_H_ @}*/