5a9726126c1599337910a4830ebd755e527ba6c6
[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 signer_test_vector_t signer_test_vector_t;
30 typedef struct hasher_test_vector_t hasher_test_vector_t;
31 typedef struct prf_test_vector_t prf_test_vector_t;
32 typedef struct rng_test_vector_t rng_test_vector_t;
33
34 struct crypter_test_vector_t {
35 /** encryption algorithm this vector tests */
36 encryption_algorithm_t alg;
37 /** key length to use, in bytes */
38 size_t key_size;
39 /** encryption key of test vector */
40 u_char *key;
41 /** initialization vector, using crypters blocksize bytes */
42 u_char *iv;
43 /** length of plain and cipher text */
44 size_t len;
45 /** plain text */
46 u_char *plain;
47 /** cipher text */
48 u_char *cipher;
49 };
50
51 struct signer_test_vector_t {
52 /** signer algorithm this test vector tests */
53 pseudo_random_function_t alg;
54 /** key to use, with a length the algorithm expects */
55 u_char *key;
56 /** size of the input data */
57 size_t len;
58 /** input data */
59 u_char *data;
60 /** expected output, with ouput size of the tested algorithm */
61 u_char *mac;
62 };
63
64 struct hasher_test_vector_t {
65 /** hash algorithm this test vector tests */
66 hash_algorithm_t alg;
67 /** length of the input data */
68 size_t len;
69 /** input data */
70 u_char *data;
71 /** expected hash, with hash size of the tested algorithm */
72 u_char *hash;
73 };
74
75 struct prf_test_vector_t {
76 /** prf algorithm this test vector tests */
77 pseudo_random_function_t alg;
78 /** key length to use, in bytes */
79 size_t key_size;
80 /** key to use */
81 u_char *key;
82 /** size of the seed data */
83 size_t len;
84 /** seed data */
85 u_char *seed;
86 /** expected output, with block size of the tested algorithm */
87 u_char *out;
88 };
89
90 /**
91 * Test vector for a RNG.
92 *
93 * Contains a callback function to analyze the output of a RNG,
94 */
95 struct rng_test_vector_t {
96 /** quality of random data this test vector tests */
97 rng_quality_t quality;
98 /** callback function to test RNG output, returns TRUE if data ok */
99 bool (*test)(void *user, chunk_t data);
100 /** number of bytes the function requests */
101 size_t len;
102 /** user data passed back to the test() function on invocation */
103 void *user;
104 };
105
106 /**
107 * Cryptographic primitive testing framework.
108 */
109 struct crypto_tester_t {
110
111 /**
112 * Test a crypter algorithm, optionally using a specified key size.
113 *
114 * @param alg algorithm to test
115 * @param key_size key size to test, 0 for all
116 * @param create constructor function for the crypter
117 * @return TRUE if test passed
118 */
119 bool (*test_crypter)(crypto_tester_t *this, encryption_algorithm_t alg,
120 size_t key_size, crypter_constructor_t create);
121 /**
122 * Test a signer algorithm.
123 *
124 * @param alg algorithm to test
125 * @param create constructor function for the signer
126 * @return TRUE if test passed
127 */
128 bool (*test_signer)(crypto_tester_t *this, integrity_algorithm_t alg,
129 signer_constructor_t create);
130 /**
131 * Test a hasher algorithm.
132 *
133 * @param alg algorithm to test
134 * @param create constructor function for the hasher
135 * @return TRUE if test passed
136 */
137 bool (*test_hasher)(crypto_tester_t *this, hash_algorithm_t alg,
138 hasher_constructor_t create);
139 /**
140 * Test a PRF algorithm.
141 *
142 * @param alg algorithm to test
143 * @param create constructor function for the PRF
144 * @return TRUE if test passed
145 */
146 bool (*test_prf)(crypto_tester_t *this, pseudo_random_function_t alg,
147 prf_constructor_t create);
148 /**
149 * Test a RNG implementation.
150 *
151 * @param alg algorithm to test
152 * @param create constructor function for the RNG
153 * @return TRUE if test passed
154 */
155 bool (*test_rng)(crypto_tester_t *this, rng_quality_t quality,
156 rng_constructor_t create);
157 /**
158 * Add a test vector to test a crypter.
159 *
160 * @param vector pointer to test vector
161 */
162 void (*add_crypter_vector)(crypto_tester_t *this,
163 crypter_test_vector_t *vector);
164 /**
165 * Add a test vector to test a signer.
166 *
167 * @param vector pointer to test vector
168 */
169 void (*add_signer_vector)(crypto_tester_t *this,
170 signer_test_vector_t *vector);
171 /**
172 * Add a test vector to test a hasher.
173 *
174 * @param vector pointer to test vector
175 */
176 void (*add_hasher_vector)(crypto_tester_t *this,
177 hasher_test_vector_t *vector);
178 /**
179 * Add a test vector to test a PRF.
180 *
181 * @param vector pointer to test vector
182 */
183 void (*add_prf_vector)(crypto_tester_t *this, prf_test_vector_t *vector);
184
185 /**
186 * Add a test vector to test a RNG.
187 *
188 * @param vector pointer to test vector
189 */
190 void (*add_rng_vector)(crypto_tester_t *this, rng_test_vector_t *vector);
191
192 /**
193 * Destroy a crypto_tester_t.
194 */
195 void (*destroy)(crypto_tester_t *this);
196 };
197
198 /**
199 * Create a crypto_tester instance.
200 */
201 crypto_tester_t *crypto_tester_create();
202
203 #endif /* CRYPTO_TESTER_ @}*/