cdb9b47ba57afa10ecb0a97ca5f11ec94cd0340b
[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/signers/signer.h>
29 #include <crypto/hashers/hasher.h>
30 #include <crypto/prfs/prf.h>
31 #include <crypto/rngs/rng.h>
32 #include <crypto/diffie_hellman.h>
33
34 /**
35 * Constructor function for crypters
36 */
37 typedef crypter_t* (*crypter_constructor_t)(encryption_algorithm_t algo,
38 size_t key_size);
39 /**
40 * Constructor function for signers
41 */
42 typedef signer_t* (*signer_constructor_t)(integrity_algorithm_t algo);
43
44 /**
45 * Constructor function for hashers
46 */
47 typedef hasher_t* (*hasher_constructor_t)(hash_algorithm_t algo);
48
49 /**
50 * Constructor function for pseudo random functions
51 */
52 typedef prf_t* (*prf_constructor_t)(pseudo_random_function_t algo);
53
54 /**
55 * Constructor function for source of randomness
56 */
57 typedef rng_t* (*rng_constructor_t)(rng_quality_t quality);
58
59 /**
60 * Constructor function for diffie hellman
61 */
62 typedef diffie_hellman_t* (*dh_constructor_t)(diffie_hellman_group_t group);
63
64 /**
65 * Handles crypto modules and creates instances.
66 */
67 struct crypto_factory_t {
68
69 /**
70 * Create a crypter instance.
71 *
72 * @param algo encryption algorithm
73 * @param key_size length of the key in bytes
74 * @return crypter_t instance, NULL if not supported
75 */
76 crypter_t* (*create_crypter)(crypto_factory_t *this,
77 encryption_algorithm_t algo, size_t key_size);
78
79 /**
80 * Create a symmetric signer instance.
81 *
82 * @param algo MAC algorithm to use
83 * @return signer_t instance, NULL if not supported
84 */
85 signer_t* (*create_signer)(crypto_factory_t *this,
86 integrity_algorithm_t algo);
87
88 /**
89 * Create a hasher instance.
90 *
91 * @param algo hash algorithm
92 * @return hasher_t instance, NULL if not supported
93 */
94 hasher_t* (*create_hasher)(crypto_factory_t *this, hash_algorithm_t algo);
95
96 /**
97 * Create a pseudo random function instance.
98 *
99 * @param algo PRF algorithm to use
100 * @return prf_t instance, NULL if not supported
101 */
102 prf_t* (*create_prf)(crypto_factory_t *this, pseudo_random_function_t algo);
103
104 /**
105 * Create a source of randomness.
106 *
107 * @param quality required randomness quality
108 * @return rng_t instance, NULL if no RNG with such a quality
109 */
110 rng_t* (*create_rng)(crypto_factory_t *this, rng_quality_t quality);
111
112 /**
113 * Create a diffie hellman instance.
114 *
115 * @param group diffie hellman group
116 * @return diffie_hellman_t instance, NULL if not supported
117 */
118 diffie_hellman_t* (*create_dh)(crypto_factory_t *this,
119 diffie_hellman_group_t group);
120
121 /**
122 * Register a crypter constructor.
123 *
124 * @param algo algorithm to constructor
125 * @param create constructor function for that algorithm
126 * @return
127 */
128 void (*add_crypter)(crypto_factory_t *this, encryption_algorithm_t algo,
129 crypter_constructor_t create);
130
131 /**
132 * Unregister a crypter constructor.
133 *
134 * @param create constructor function to unregister
135 */
136 void (*remove_crypter)(crypto_factory_t *this, crypter_constructor_t create);
137
138 /**
139 * Register a signer constructor.
140 *
141 * @param algo algorithm to constructor
142 * @param create constructor function for that algorithm
143 * @return
144 */
145 void (*add_signer)(crypto_factory_t *this, integrity_algorithm_t algo,
146 signer_constructor_t create);
147
148 /**
149 * Unregister a signer constructor.
150 *
151 * @param create constructor function to unregister
152 */
153 void (*remove_signer)(crypto_factory_t *this, signer_constructor_t create);
154
155 /**
156 * Register a hasher constructor.
157 *
158 * The first added hasher is the preferred hasher returned on
159 * create_hasher(HASH_PREFERRED).
160 *
161 * @param algo algorithm to constructor
162 * @param create constructor function for that algorithm
163 * @return
164 */
165 void (*add_hasher)(crypto_factory_t *this, hash_algorithm_t algo,
166 hasher_constructor_t create);
167
168 /**
169 * Unregister a hasher constructor.
170 *
171 * @param create constructor function to unregister
172 */
173 void (*remove_hasher)(crypto_factory_t *this, hasher_constructor_t create);
174
175 /**
176 * Register a prf constructor.
177 *
178 * @param algo algorithm to constructor
179 * @param create constructor function for that algorithm
180 * @return
181 */
182 void (*add_prf)(crypto_factory_t *this, pseudo_random_function_t algo,
183 prf_constructor_t create);
184
185 /**
186 * Unregister a prf constructor.
187 *
188 * @param create constructor function to unregister
189 */
190 void (*remove_prf)(crypto_factory_t *this, prf_constructor_t create);
191
192 /**
193 * Register a source of randomness.
194 *
195 * @param quality quality of randomness this RNG serves
196 * @param create constructor function for such a quality
197 */
198 void (*add_rng)(crypto_factory_t *this, rng_quality_t quality, rng_constructor_t create);
199
200 /**
201 * Unregister a source of randomness.
202 *
203 * @param create constructor function to unregister
204 */
205 void (*remove_rng)(crypto_factory_t *this, rng_constructor_t create);
206
207 /**
208 * Register a diffie hellman constructor.
209 *
210 * @param group dh group to constructor
211 * @param create constructor function for that algorithm
212 * @return
213 */
214 void (*add_dh)(crypto_factory_t *this, diffie_hellman_group_t group,
215 dh_constructor_t create);
216
217 /**
218 * Unregister a diffie hellman constructor.
219 *
220 * @param create constructor function to unregister
221 */
222 void (*remove_dh)(crypto_factory_t *this, dh_constructor_t create);
223
224 /**
225 * Create an enumerator over all registered crypter algorithms.
226 *
227 * @return enumerator over encryption_algorithm_t
228 */
229 enumerator_t* (*create_crypter_enumerator)(crypto_factory_t *this);
230
231 /**
232 * Create an enumerator over all registered signer algorithms.
233 *
234 * @return enumerator over integrity_algorithm_t
235 */
236 enumerator_t* (*create_signer_enumerator)(crypto_factory_t *this);
237
238 /**
239 * Create an enumerator over all registered hasher algorithms.
240 *
241 * @return enumerator over hash_algorithm_t
242 */
243 enumerator_t* (*create_hasher_enumerator)(crypto_factory_t *this);
244
245 /**
246 * Create an enumerator over all registered PRFs.
247 *
248 * @return enumerator over pseudo_random_function_t
249 */
250 enumerator_t* (*create_prf_enumerator)(crypto_factory_t *this);
251
252 /**
253 * Create an enumerator over all registered diffie hellman groups.
254 *
255 * @return enumerator over diffie_hellman_group_t
256 */
257 enumerator_t* (*create_dh_enumerator)(crypto_factory_t *this);
258
259 /**
260 * Destroy a crypto_factory instance.
261 */
262 void (*destroy)(crypto_factory_t *this);
263 };
264
265 /**
266 * Create a crypto_factory instance.
267 */
268 crypto_factory_t *crypto_factory_create();
269
270 #endif /* CRYPTO_FACTORY_H_ @}*/