summaryrefslogtreecommitdiff
path: root/src/libstrongswan/crypto/crypto_factory.h
blob: 281dc256f314e16dd3176cb1fad0297123bf690d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * Copyright (C) 2008 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

/**
 * @defgroup crypto_factory crypto_factory
 * @{ @ingroup crypto
 */

#ifndef CRYPTO_FACTORY_H_
#define CRYPTO_FACTORY_H_

typedef struct crypto_factory_t crypto_factory_t;

#include <library.h>
#include <collections/enumerator.h>
#include <crypto/crypters/crypter.h>
#include <crypto/aead.h>
#include <crypto/signers/signer.h>
#include <crypto/hashers/hasher.h>
#include <crypto/prfs/prf.h>
#include <crypto/rngs/rng.h>
#include <crypto/nonce_gen.h>
#include <crypto/diffie_hellman.h>
#include <crypto/transform.h>

#define CRYPTO_MAX_ALG_LINE          120   /* characters */

/**
 * Constructor function for crypters
 */
typedef crypter_t* (*crypter_constructor_t)(encryption_algorithm_t algo,
											size_t key_size);
/**
 * Constructor function for aead transforms
 */
typedef aead_t* (*aead_constructor_t)(encryption_algorithm_t algo,
									  size_t key_size);
/**
 * Constructor function for signers
 */
typedef signer_t* (*signer_constructor_t)(integrity_algorithm_t algo);

/**
 * Constructor function for hashers
 */
typedef hasher_t* (*hasher_constructor_t)(hash_algorithm_t algo);

/**
 * Constructor function for pseudo random functions
 */
typedef prf_t* (*prf_constructor_t)(pseudo_random_function_t algo);

/**
 * Constructor function for source of randomness
 */
typedef rng_t* (*rng_constructor_t)(rng_quality_t quality);

/**
 * Constructor function for nonce generators
 */
typedef nonce_gen_t* (*nonce_gen_constructor_t)();

/**
 * Constructor function for diffie hellman
 *
 * The DH constructor accepts additional arguments for:
 * - MODP_CUSTOM: chunk_t generator, chunk_t prime
 */
typedef diffie_hellman_t* (*dh_constructor_t)(diffie_hellman_group_t group, ...);

/**
 * Handles crypto modules and creates instances.
 */
struct crypto_factory_t {

	/**
	 * Create a crypter instance.
	 *
	 * @param algo			encryption algorithm
	 * @param key_size		length of the key in bytes
	 * @return				crypter_t instance, NULL if not supported
	 */
	crypter_t* (*create_crypter)(crypto_factory_t *this,
								 encryption_algorithm_t algo, size_t key_size);

	/**
	 * Create a aead instance.
	 *
	 * @param algo			encryption algorithm
	 * @param key_size		length of the key in bytes
	 * @return				aead_t instance, NULL if not supported
	 */
	aead_t* (*create_aead)(crypto_factory_t *this,
						   encryption_algorithm_t algo, size_t key_size);

	/**
	 * Create a symmetric signer instance.
	 *
	 * @param algo			MAC algorithm to use
	 * @return				signer_t instance, NULL if not supported
	 */
	signer_t* (*create_signer)(crypto_factory_t *this,
							   integrity_algorithm_t algo);

	/**
	 * Create a hasher instance.
	 *
	 * @param algo			hash algorithm
	 * @return				hasher_t instance, NULL if not supported
	 */
	hasher_t* (*create_hasher)(crypto_factory_t *this, hash_algorithm_t algo);

	/**
	 * Create a pseudo random function instance.
	 *
	 * @param algo			PRF algorithm to use
	 * @return				prf_t instance, NULL if not supported
	 */
	prf_t* (*create_prf)(crypto_factory_t *this, pseudo_random_function_t algo);

	/**
	 * Create a source of randomness.
	 *
	 * @param quality		required randomness quality
	 * @return				rng_t instance, NULL if no RNG with such a quality
	 */
	rng_t* (*create_rng)(crypto_factory_t *this, rng_quality_t quality);

	/**
	 * Create a nonce generator instance.
	 *
	 * @return				nonce_gen_t instance, NULL if not supported
	 */
	nonce_gen_t* (*create_nonce_gen)(crypto_factory_t *this);

	/**
	 * Create a diffie hellman instance.
	 *
	 * Additional arguments are passed to the DH constructor.
	 *
	 * @param group			diffie hellman group
	 * @return				diffie_hellman_t instance, NULL if not supported
	 */
	diffie_hellman_t* (*create_dh)(crypto_factory_t *this,
								   diffie_hellman_group_t group, ...);

	/**
	 * Register a crypter constructor.
	 *
	 * @param algo			algorithm to constructor
	 * @param plugin_name	plugin that registered this algorithm
	 * @param create		constructor function for that algorithm
	 * @return				TRUE if registered, FALSE if test vector failed
	 */
	bool (*add_crypter)(crypto_factory_t *this, encryption_algorithm_t algo,
						const char *plugin_name, crypter_constructor_t create);

	/**
	 * Unregister a crypter constructor.
	 *
	 * @param create		constructor function to unregister
	 */
	void (*remove_crypter)(crypto_factory_t *this, crypter_constructor_t create);

	/**
	 * Unregister a aead constructor.
	 *
	 * @param create		constructor function to unregister
	 */
	void (*remove_aead)(crypto_factory_t *this, aead_constructor_t create);

	/**
	 * Register a aead constructor.
	 *
	 * @param algo			algorithm to constructor
	 * @param plugin_name	plugin that registered this algorithm
	 * @param create		constructor function for that algorithm
	 * @return				TRUE if registered, FALSE if test vector failed
	 */
	bool (*add_aead)(crypto_factory_t *this, encryption_algorithm_t algo,
					 const char *plugin_name, aead_constructor_t create);

	/**
	 * Register a signer constructor.
	 *
	 * @param algo			algorithm to constructor
	 * @param plugin_name	plugin that registered this algorithm
	 * @param create		constructor function for that algorithm
	 * @return				TRUE if registered, FALSE if test vector failed
	 */
	bool (*add_signer)(crypto_factory_t *this, integrity_algorithm_t algo,
					    const char *plugin_name, signer_constructor_t create);

	/**
	 * Unregister a signer constructor.
	 *
	 * @param create		constructor function to unregister
	 */
	void (*remove_signer)(crypto_factory_t *this, signer_constructor_t create);

	/**
	 * Register a hasher constructor.
	 *
	 * @param algo			algorithm to constructor
	 * @param plugin_name	plugin that registered this algorithm
	 * @param create		constructor function for that algorithm
	 * @return				TRUE if registered, FALSE if test vector failed
	 */
	bool (*add_hasher)(crypto_factory_t *this, hash_algorithm_t algo,
					   const char *plugin_name, hasher_constructor_t create);

	/**
	 * Unregister a hasher constructor.
	 *
	 * @param create		constructor function to unregister
	 */
	void (*remove_hasher)(crypto_factory_t *this, hasher_constructor_t create);

	/**
	 * Register a prf constructor.
	 *
	 * @param algo			algorithm to constructor
	 * @param plugin_name	plugin that registered this algorithm
	 * @param create		constructor function for that algorithm
	 * @return				TRUE if registered, FALSE if test vector failed
	 */
	bool (*add_prf)(crypto_factory_t *this, pseudo_random_function_t algo,
					const char *plugin_name, prf_constructor_t create);

	/**
	 * Unregister a prf constructor.
	 *
	 * @param create		constructor function to unregister
	 */
	void (*remove_prf)(crypto_factory_t *this, prf_constructor_t create);

	/**
	 * Register a source of randomness.
	 *
	 * @param quality		quality of randomness this RNG serves
	 * @param plugin_name	plugin that registered this algorithm
	 * @param create		constructor function for such a quality
	 * @return				TRUE if registered, FALSE if test vector failed
	 */
	bool (*add_rng)(crypto_factory_t *this, rng_quality_t quality,
					const char *plugin_name, rng_constructor_t create);

	/**
	 * Unregister a source of randomness.
	 *
	 * @param create		constructor function to unregister
	 */
	void (*remove_rng)(crypto_factory_t *this, rng_constructor_t create);

	/**
	 * Register a nonce generator.
	 *
	 * @param plugin_name	plugin that registered this algorithm
	 * @param create		constructor function for that nonce generator
	 * @return				TRUE if registered, FALSE if test vector failed
	 */
	bool (*add_nonce_gen)(crypto_factory_t *this, const char *plugin_name,
						  nonce_gen_constructor_t create);

	/**
	 * Unregister a nonce generator.
	 *
	 * @param create		constructor function to unregister
	 */
	void (*remove_nonce_gen)(crypto_factory_t *this,
							 nonce_gen_constructor_t create);

	/**
	 * Register a diffie hellman constructor.
	 *
	 * @param group			dh group to constructor
	 * @param plugin_name	plugin that registered this algorithm
	 * @param create		constructor function for that algorithm
	 * @return				TRUE if registered, FALSE if test vector failed
	 */
	bool (*add_dh)(crypto_factory_t *this, diffie_hellman_group_t group,
				   const char *plugin_name, dh_constructor_t create);

	/**
	 * Unregister a diffie hellman constructor.
	 *
	 * @param create		constructor function to unregister
	 */
	void (*remove_dh)(crypto_factory_t *this, dh_constructor_t create);

	/**
	 * Create an enumerator over all registered crypter algorithms.
	 *
	 * @return				enumerator over encryption_algorithm_t, plugin
	 */
	enumerator_t* (*create_crypter_enumerator)(crypto_factory_t *this);

	/**
	 * Create an enumerator over all registered aead algorithms.
	 *
	 * @return				enumerator over encryption_algorithm_t, plugin
	 */
	enumerator_t* (*create_aead_enumerator)(crypto_factory_t *this);

	/**
	 * Create an enumerator over all registered signer algorithms.
	 *
	 * @return				enumerator over integrity_algorithm_t, plugin
	 */
	enumerator_t* (*create_signer_enumerator)(crypto_factory_t *this);

	/**
	 * Create an enumerator over all registered hasher algorithms.
	 *
	 * @return				enumerator over hash_algorithm_t, plugin
	 */
	enumerator_t* (*create_hasher_enumerator)(crypto_factory_t *this);

	/**
	 * Create an enumerator over all registered PRFs.
	 *
	 * @return				enumerator over pseudo_random_function_t, plugin
	 */
	enumerator_t* (*create_prf_enumerator)(crypto_factory_t *this);

	/**
	 * Create an enumerator over all registered diffie hellman groups.
	 *
	 * @return				enumerator over diffie_hellman_group_t, plugin
	 */
	enumerator_t* (*create_dh_enumerator)(crypto_factory_t *this);

	/**
	 * Create an enumerator over all registered random generators.
	 *
	 * @return				enumerator over rng_quality_t, plugin
	 */
	enumerator_t* (*create_rng_enumerator)(crypto_factory_t *this);

	/**
	 * Create an enumerator over all registered nonce generators.
	 *
	 * @return				enumerator over plugin
	 */
	enumerator_t* (*create_nonce_gen_enumerator)(crypto_factory_t *this);

	/**
	 * Add a test vector to the crypto factory.
	 *
	 * @param type			type of the test vector
	 * @param vector		pointer to a test vector, defined in crypto_tester.h
	 */
	void (*add_test_vector)(crypto_factory_t *this, transform_type_t type,
							void *vector);

	/**
	 * Get the number of test vector failures encountered during add.
	 *
	 * This counter gets incremented only if transforms get tested during
	 * registration.
	 *
	 * @return				number of failed test vectors
	 */
	u_int (*get_test_vector_failures)(crypto_factory_t *this);

	/**
	 * Destroy a crypto_factory instance.
	 */
	void (*destroy)(crypto_factory_t *this);
};

/**
 * Create a crypto_factory instance.
 */
crypto_factory_t *crypto_factory_create();

#endif /** CRYPTO_FACTORY_H_ @}*/