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