cfb8: Fix decrypt path
[gd/nettle] / testsuite / testutils.h
1 #ifndef NETTLE_TESTUTILS_H_INCLUDED
2 #define NETTLE_TESTUTILS_H_INCLUDED
3
4 /* config.h should usually be first in each .c file. This is an
5    exception, include it here to reduce clutter in the test cases. */
6 #if HAVE_CONFIG_H
7 # include "config.h"
8 #endif
9
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 #include "nettle-types.h"
16 #include "version.h"
17
18 #if WITH_HOGWEED
19 # include "rsa.h"
20 # include "dsa-compat.h"
21 # include "ecc-curve.h"
22 # include "ecc.h"
23 # include "ecc-internal.h"
24 # include "ecdsa.h"
25 # include "gmp-glue.h"
26 # if NETTLE_USE_MINI_GMP
27 #  include "knuth-lfib.h"
28 # endif
29
30 /* Undo dsa-compat name mangling */
31 #undef dsa_generate_keypair
32 #define dsa_generate_keypair nettle_dsa_generate_keypair
33 #endif /* WITH_HOGWEED */
34
35 #include "nettle-meta.h"
36
37 /* Forward declare */
38 struct nettle_aead;
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 void
45 die(const char *format, ...) PRINTF_STYLE (1, 2) NORETURN;
46
47 void *
48 xalloc(size_t size);
49
50 struct tstring {
51   struct tstring *next;
52   size_t length;
53   uint8_t data[1];
54 };
55
56 struct tstring *
57 tstring_alloc (size_t length);
58
59 void
60 tstring_clear(void);
61
62 struct tstring *
63 tstring_data(size_t length, const uint8_t *data);
64
65 struct tstring *
66 tstring_hex(const char *hex);
67
68 void
69 tstring_print_hex(const struct tstring *s);
70
71 /* Decodes a NUL-terminated hex string. */
72
73 void
74 print_hex(size_t length, const uint8_t *data);
75
76 /* The main program */
77 void
78 test_main(void);
79
80 extern int verbose;
81
82 /* FIXME: When interface stabilizes, move to nettle-meta.h */
83 struct nettle_mac
84 {
85   const char *name;
86
87   /* Size of the context struct */
88   unsigned context_size;
89
90   /* Size of digests */
91   unsigned digest_size;
92
93   /* Suggested key size; other sizes are sometimes possible. */
94   unsigned key_size;
95   
96   nettle_set_key_func *set_key;
97   nettle_hash_update_func *update;
98   nettle_hash_digest_func *digest;
99 };
100
101 #define _NETTLE_HMAC(name, NAME, keysize) {     \
102   #name,                                        \
103   sizeof(struct hmac_##name##_ctx),             \
104   NAME##_DIGEST_SIZE,                           \
105   NAME##_DIGEST_SIZE,                           \
106   hmac_##name##_set_key,                        \
107   hmac_##name##_update,                         \
108   hmac_##name##_digest,                         \
109 }
110
111 /* Test functions deallocate their inputs when finished.*/
112 void
113 test_cipher(const struct nettle_cipher *cipher,
114             const struct tstring *key,
115             const struct tstring *cleartext,
116             const struct tstring *ciphertext);
117
118 void
119 test_cipher_cbc(const struct nettle_cipher *cipher,
120                 const struct tstring *key,
121                 const struct tstring *cleartext,
122                 const struct tstring *ciphertext,
123                 const struct tstring *iv);
124
125 void
126 test_cipher_cfb(const struct nettle_cipher *cipher,
127                 const struct tstring *key,
128                 const struct tstring *cleartext,
129                 const struct tstring *ciphertext,
130                 const struct tstring *iv);
131
132 void
133 test_cipher_cfb8(const struct nettle_cipher *cipher,
134                  const struct tstring *key,
135                  const struct tstring *cleartext,
136                  const struct tstring *ciphertext,
137                  const struct tstring *iv);
138
139 void
140 test_cipher_ctr(const struct nettle_cipher *cipher,
141                 const struct tstring *key,
142                 const struct tstring *cleartext,
143                 const struct tstring *ciphertext,
144                 const struct tstring *iv);
145
146 void
147 test_cipher_stream(const struct nettle_cipher *cipher,
148                    const struct tstring *key,
149                    const struct tstring *cleartext,
150                    const struct tstring *ciphertext);
151
152 void
153 test_aead(const struct nettle_aead *aead,
154           nettle_hash_update_func *set_nonce,
155           const struct tstring *key,
156           const struct tstring *authtext,
157           const struct tstring *cleartext,
158           const struct tstring *ciphertext,
159           const struct tstring *nonce,
160           const struct tstring *digest);
161
162 void
163 test_hash(const struct nettle_hash *hash,
164           const struct tstring *msg,
165           const struct tstring *digest);
166
167 void
168 test_hash_large(const struct nettle_hash *hash,
169                 size_t count, size_t length,
170                 uint8_t c,
171                 const struct tstring *digest);
172
173 void
174 test_mac(const struct nettle_mac *mac,
175          const struct tstring *key,
176          const struct tstring *msg,
177          const struct tstring *digest);
178
179 void
180 test_armor(const struct nettle_armor *armor,
181            size_t data_length,
182            const uint8_t *data,
183            const char *ascii);
184
185 #if WITH_HOGWEED
186
187 #if NETTLE_USE_MINI_GMP
188 typedef struct knuth_lfib_ctx gmp_randstate_t[1];
189
190 void gmp_randinit_default (struct knuth_lfib_ctx *ctx);
191 #define gmp_randclear(state)
192 void mpz_urandomb (mpz_t r, struct knuth_lfib_ctx *ctx, mp_bitcnt_t bits);
193 /* This is cheating */
194 #define mpz_rrandomb mpz_urandomb
195
196 /* mini-gmp defines this function (in the GMP library, it was added in
197    gmp in version 6.1.0). */
198 #define mpn_zero_p mpn_zero_p
199
200 #endif /* NETTLE_USE_MINI_GMP */
201
202 #ifndef mpn_zero_p
203 int
204 mpn_zero_p (mp_srcptr ap, mp_size_t n);
205 #endif
206
207 void
208 mpn_out_str (FILE *f, int base, const mp_limb_t *xp, mp_size_t xn);
209
210 mp_limb_t *
211 xalloc_limbs (mp_size_t n);
212
213 void
214 write_mpn (FILE *f, int base, const mp_limb_t *xp, mp_size_t n);
215
216 void
217 test_rsa_set_key_1(struct rsa_public_key *pub,
218                    struct rsa_private_key *key);
219
220 void
221 test_rsa_md5(struct rsa_public_key *pub,
222              struct rsa_private_key *key,
223              mpz_t expected);
224
225 void
226 test_rsa_sha1(struct rsa_public_key *pub,
227               struct rsa_private_key *key,
228               mpz_t expected);
229
230 void
231 test_rsa_sha256(struct rsa_public_key *pub,
232                 struct rsa_private_key *key,
233                 mpz_t expected);
234
235 void
236 test_rsa_sha512(struct rsa_public_key *pub,
237                 struct rsa_private_key *key,
238                 mpz_t expected);
239
240 void
241 test_rsa_key(struct rsa_public_key *pub,
242              struct rsa_private_key *key);
243
244 void
245 test_dsa160(const struct dsa_public_key *pub,
246             const struct dsa_private_key *key,
247             const struct dsa_signature *expected);
248
249 void
250 test_dsa256(const struct dsa_public_key *pub,
251             const struct dsa_private_key *key,
252             const struct dsa_signature *expected);
253
254 #if 0
255 void
256 test_dsa_sign(const struct dsa_public_key *pub,
257               const struct dsa_private_key *key,
258               const struct nettle_hash *hash,
259               const struct dsa_signature *expected);
260 #endif
261
262 void
263 test_dsa_verify(const struct dsa_params *params,
264                 const mpz_t pub,
265                 const struct nettle_hash *hash,
266                 struct tstring *msg,
267                 const struct dsa_signature *ref);
268
269 void
270 test_dsa_key(const struct dsa_params *params,
271              const mpz_t pub,
272              const mpz_t key,
273              unsigned q_size);
274
275 extern const struct ecc_curve * const ecc_curves[];
276
277 struct ecc_ref_point
278 {
279   const char *x;
280   const char *y;
281 };
282
283 void
284 test_ecc_point (const struct ecc_curve *ecc,
285                 const struct ecc_ref_point *ref,
286                 const mp_limb_t *p);
287
288 void
289 test_ecc_mul_a (unsigned curve, unsigned n, const mp_limb_t *p);
290
291 void
292 test_ecc_mul_h (unsigned curve, unsigned n, const mp_limb_t *p);
293
294 #endif /* WITH_HOGWEED */
295
296 /* String literal of type unsigned char. The GNUC version is safer. */
297 #if __GNUC__
298 #define US(s) ({ static const unsigned char us_s[] = s; us_s; })
299 #else
300 #define US(s) ((const uint8_t *) (s))
301 #endif
302   
303 /* LDATA needs to handle NUL characters. */
304 #define LLENGTH(x) (sizeof(x) - 1)
305 #define LDATA(x) LLENGTH(x), US(x)
306 #define LDUP(x) strlen(x), strdup(x)
307
308 #define SHEX(x) (tstring_hex(x))
309 #define SDATA(x) ((const struct tstring *)tstring_data(LLENGTH(x), US(x)))
310 #define H(x) (SHEX(x)->data)
311
312 #define MEMEQ(length, a, b) (!memcmp((a), (b), (length)))
313
314 #define FAIL() abort()
315 #define SKIP() exit(77)
316
317 #define ASSERT(x) do {                                                  \
318     if (!(x))                                                           \
319       {                                                                 \
320         fprintf(stderr, "Assert failed: %s:%d: %s\n", \
321                 __FILE__, __LINE__, #x);                                        \
322         FAIL();                                                         \
323       }                                                                 \
324   } while(0)
325
326 #ifdef __cplusplus
327 }
328 #endif
329
330 #endif /* NETTLE_TESTUTILS_H_INCLUDED */