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