xof: Defined Extended Output Functions
[strongswan.git] / src / libstrongswan / crypto / crypto_tester.h
1 /*
2 * Copyright (C) 2009 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_tester crypto_tester
18 * @{ @ingroup crypto
19 */
20
21 #ifndef CRYPTO_TESTER_H_
22 #define CRYPTO_TESTER_H_
23
24 typedef struct crypto_tester_t crypto_tester_t;
25
26 #include <crypto/crypto_factory.h>
27
28 typedef struct crypter_test_vector_t crypter_test_vector_t;
29 typedef struct aead_test_vector_t aead_test_vector_t;
30 typedef struct signer_test_vector_t signer_test_vector_t;
31 typedef struct hasher_test_vector_t hasher_test_vector_t;
32 typedef struct prf_test_vector_t prf_test_vector_t;
33 typedef struct xof_test_vector_t xof_test_vector_t;
34 typedef struct rng_test_vector_t rng_test_vector_t;
35 typedef struct dh_test_vector_t dh_test_vector_t;
36
37 struct crypter_test_vector_t {
38 /** encryption algorithm this vector tests */
39 encryption_algorithm_t alg;
40 /** key length to use, in bytes */
41 size_t key_size;
42 /** encryption key of test vector */
43 u_char *key;
44 /** initialization vector, using crypters blocksize bytes */
45 u_char *iv;
46 /** length of plain and cipher text */
47 size_t len;
48 /** plain text */
49 u_char *plain;
50 /** cipher text */
51 u_char *cipher;
52 };
53
54 struct aead_test_vector_t {
55 /** encryption algorithm this vector tests */
56 encryption_algorithm_t alg;
57 /** key length to use, in bytes */
58 size_t key_size;
59 /** salt length to use, in bytes */
60 size_t salt_size;
61 /** encryption key of test vector */
62 u_char *key;
63 /** initialization vector, using crypters blocksize bytes */
64 u_char *iv;
65 /** length of associated data */
66 size_t alen;
67 /** associated data */
68 u_char *adata;
69 /** length of plain text */
70 size_t len;
71 /** plain text */
72 u_char *plain;
73 /** cipher text */
74 u_char *cipher;
75 };
76
77 struct signer_test_vector_t {
78 /** signer algorithm this test vector tests */
79 integrity_algorithm_t alg;
80 /** key to use, with a length the algorithm expects */
81 u_char *key;
82 /** size of the input data */
83 size_t len;
84 /** input data */
85 u_char *data;
86 /** expected output, with ouput size of the tested algorithm */
87 u_char *mac;
88 };
89
90 struct hasher_test_vector_t {
91 /** hash algorithm this test vector tests */
92 hash_algorithm_t alg;
93 /** length of the input data */
94 size_t len;
95 /** input data */
96 u_char *data;
97 /** expected hash, with hash size of the tested algorithm */
98 u_char *hash;
99 };
100
101 struct prf_test_vector_t {
102 /** prf algorithm this test vector tests */
103 pseudo_random_function_t alg;
104 /** is this PRF stateful? */
105 bool stateful;
106 /** key length to use, in bytes */
107 size_t key_size;
108 /** key to use */
109 u_char *key;
110 /** size of the seed data */
111 size_t len;
112 /** seed data */
113 u_char *seed;
114 /** expected output, with block size of the tested algorithm */
115 u_char *out;
116 };
117
118 struct xof_test_vector_t {
119 /** xof algorithm this test vector tests */
120 ext_out_function_t alg;
121 /** size of the seed data */
122 size_t len;
123 /** seed data */
124 u_char *seed;
125 /** size of the output */
126 size_t out_len;
127 /** expected output of size*/
128 u_char *out;
129 };
130
131 /**
132 * Test vector for a RNG.
133 *
134 * Contains a callback function to analyze the output of a RNG,
135 */
136 struct rng_test_vector_t {
137 /** quality of random data this test vector tests */
138 rng_quality_t quality;
139 /** callback function to test RNG output, returns TRUE if data ok */
140 bool (*test)(void *user, chunk_t data);
141 /** number of bytes the function requests */
142 size_t len;
143 /** user data passed back to the test() function on invocation */
144 void *user;
145 };
146
147 struct dh_test_vector_t {
148 /** diffie hellman group to test */
149 diffie_hellman_group_t group;
150 /** private value of alice */
151 u_char *priv_a;
152 /** private value of bob */
153 u_char *priv_b;
154 /** length of private values */
155 size_t priv_len;
156 /** expected public value of alice */
157 u_char *pub_a;
158 /** expected public value of bob */
159 u_char *pub_b;
160 /** size of public values */
161 size_t pub_len;
162 /** expected shared secret */
163 u_char *shared;
164 /** size of shared secret */
165 size_t shared_len;
166 };
167
168 /**
169 * Cryptographic primitive testing framework.
170 */
171 struct crypto_tester_t {
172
173 /**
174 * Test a crypter algorithm, optionally using a specified key size.
175 *
176 * @param alg algorithm to test
177 * @param key_size key size to test, 0 for default
178 * @param create constructor function for the crypter
179 * @param speed speed test result, NULL to omit
180 * @return TRUE if test passed
181 */
182 bool (*test_crypter)(crypto_tester_t *this, encryption_algorithm_t alg,
183 size_t key_size, crypter_constructor_t create,
184 u_int *speed, const char *plugin_name);
185
186 /**
187 * Test an aead algorithm, optionally using a specified key size.
188 *
189 * @param alg algorithm to test
190 * @param key_size key size to test, 0 for default
191 * @param salt_size salt length to test, 0 for default
192 * @param create constructor function for the aead transform
193 * @param speed speed test result, NULL to omit
194 * @return TRUE if test passed
195 */
196 bool (*test_aead)(crypto_tester_t *this, encryption_algorithm_t alg,
197 size_t key_size, size_t salt_size,
198 aead_constructor_t create,
199 u_int *speed, const char *plugin_name);
200 /**
201 * Test a signer algorithm.
202 *
203 * @param alg algorithm to test
204 * @param create constructor function for the signer
205 * @param speed speed test result, NULL to omit
206 * @return TRUE if test passed
207 */
208 bool (*test_signer)(crypto_tester_t *this, integrity_algorithm_t alg,
209 signer_constructor_t create,
210 u_int *speed, const char *plugin_name);
211 /**
212 * Test a hasher algorithm.
213 *
214 * @param alg algorithm to test
215 * @param create constructor function for the hasher
216 * @param speed speed test result, NULL to omit
217 * @return TRUE if test passed
218 */
219 bool (*test_hasher)(crypto_tester_t *this, hash_algorithm_t alg,
220 hasher_constructor_t create,
221 u_int *speed, const char *plugin_name);
222 /**
223 * Test a PRF algorithm.
224 *
225 * @param alg algorithm to test
226 * @param create constructor function for the PRF
227 * @param speed speed test result, NULL to omit
228 * @return TRUE if test passed
229 */
230 bool (*test_prf)(crypto_tester_t *this, pseudo_random_function_t alg,
231 prf_constructor_t create,
232 u_int *speed, const char *plugin_name);
233 /**
234 * Test an XOF algorithm.
235 *
236 * @param alg algorithm to test
237 * @param create constructor function for the XOF
238 * @param speed speed test result, NULL to omit
239 * @return TRUE if test passed
240 */
241 bool (*test_xof)(crypto_tester_t *this, ext_out_function_t alg,
242 xof_constructor_t create,
243 u_int *speed, const char *plugin_name);
244 /**
245 * Test a RNG implementation.
246 *
247 * @param alg algorithm to test
248 * @param create constructor function for the RNG
249 * @param speed speed test result, NULL to omit
250 * @return TRUE if test passed
251 */
252 bool (*test_rng)(crypto_tester_t *this, rng_quality_t quality,
253 rng_constructor_t create,
254 u_int *speed, const char *plugin_name);
255 /**
256 * Test a Diffie-Hellman implementation.
257 *
258 * @param group group to test
259 * @param create constructor function for the DH backend
260 * @param speed speeed test result, NULL to omit
261 * @return TRUE if test passed
262 */
263 bool (*test_dh)(crypto_tester_t *this, diffie_hellman_group_t group,
264 dh_constructor_t create,
265 u_int *speed, const char *plugin_name);
266
267 /**
268 * Add a test vector to test a crypter.
269 *
270 * @param vector pointer to test vector
271 */
272 void (*add_crypter_vector)(crypto_tester_t *this,
273 crypter_test_vector_t *vector);
274 /**
275 * Add a test vector to test an aead transform.
276 *
277 * @param vector pointer to test vector
278 */
279 void (*add_aead_vector)(crypto_tester_t *this,
280 aead_test_vector_t *vector);
281 /**
282 * Add a test vector to test a signer.
283 *
284 * @param vector pointer to test vector
285 */
286 void (*add_signer_vector)(crypto_tester_t *this,
287 signer_test_vector_t *vector);
288 /**
289 * Add a test vector to test a hasher.
290 *
291 * @param vector pointer to test vector
292 */
293 void (*add_hasher_vector)(crypto_tester_t *this,
294 hasher_test_vector_t *vector);
295 /**
296 * Add a test vector to test a PRF.
297 *
298 * @param vector pointer to test vector
299 */
300 void (*add_prf_vector)(crypto_tester_t *this, prf_test_vector_t *vector);
301
302 /**
303 * Add a test vector to test an XOF.
304 *
305 * @param vector pointer to test vector
306 */
307 void (*add_xof_vector)(crypto_tester_t *this, xof_test_vector_t *vector);
308
309 /**
310 * Add a test vector to test a RNG.
311 *
312 * @param vector pointer to test vector
313 */
314 void (*add_rng_vector)(crypto_tester_t *this, rng_test_vector_t *vector);
315
316 /**
317 * Add a test vector to test a Diffie-Hellman backend.
318 *
319 * @param vector pointer to test vector
320 */
321 void (*add_dh_vector)(crypto_tester_t *this, dh_test_vector_t *vector);
322
323 /**
324 * Destroy a crypto_tester_t.
325 */
326 void (*destroy)(crypto_tester_t *this);
327 };
328
329 /**
330 * Create a crypto_tester instance.
331 */
332 crypto_tester_t *crypto_tester_create();
333
334 #endif /** CRYPTO_TESTER_H_ @}*/