58786b6553c37892f41d7dc1d7a52afa663cd6cb
[platform/upstream/nettle.git] / 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 char *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_ctr(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_stream(const struct nettle_cipher *cipher,
134                    const struct tstring *key,
135                    const struct tstring *cleartext,
136                    const struct tstring *ciphertext);
137
138 void
139 test_aead(const struct nettle_aead *aead,
140           nettle_hash_update_func *set_nonce,
141           const struct tstring *key,
142           const struct tstring *authtext,
143           const struct tstring *cleartext,
144           const struct tstring *ciphertext,
145           const struct tstring *nonce,
146           const struct tstring *digest);
147
148 void
149 test_hash(const struct nettle_hash *hash,
150           const struct tstring *msg,
151           const struct tstring *digest);
152
153 void
154 test_hash_large(const struct nettle_hash *hash,
155                 size_t count, size_t length,
156                 uint8_t c,
157                 const struct tstring *digest);
158
159 void
160 test_armor(const struct nettle_armor *armor,
161            size_t data_length,
162            const uint8_t *data,
163            const uint8_t *ascii);
164
165 #if WITH_HOGWEED
166 #ifndef mpn_zero_p
167 int
168 mpn_zero_p (mp_srcptr ap, mp_size_t n);
169 #endif
170
171 void
172 mpn_out_str (FILE *f, int base, const mp_limb_t *xp, mp_size_t xn);
173
174 #if NETTLE_USE_MINI_GMP
175 typedef struct knuth_lfib_ctx gmp_randstate_t[1];
176
177 void gmp_randinit_default (struct knuth_lfib_ctx *ctx);
178 #define gmp_randclear(state)
179 void mpz_urandomb (mpz_t r, struct knuth_lfib_ctx *ctx, mp_bitcnt_t bits);
180 /* This is cheating */
181 #define mpz_rrandomb mpz_urandomb
182
183 #endif /* NETTLE_USE_MINI_GMP */
184
185 mp_limb_t *
186 xalloc_limbs (mp_size_t n);
187
188 void
189 write_mpn (FILE *f, int base, const mp_limb_t *xp, mp_size_t n);
190
191 void
192 test_rsa_set_key_1(struct rsa_public_key *pub,
193                    struct rsa_private_key *key);
194
195 void
196 test_rsa_md5(struct rsa_public_key *pub,
197              struct rsa_private_key *key,
198              mpz_t expected);
199
200 void
201 test_rsa_sha1(struct rsa_public_key *pub,
202               struct rsa_private_key *key,
203               mpz_t expected);
204
205 void
206 test_rsa_sha256(struct rsa_public_key *pub,
207                 struct rsa_private_key *key,
208                 mpz_t expected);
209
210 void
211 test_rsa_sha512(struct rsa_public_key *pub,
212                 struct rsa_private_key *key,
213                 mpz_t expected);
214
215 void
216 test_rsa_key(struct rsa_public_key *pub,
217              struct rsa_private_key *key);
218
219 void
220 test_dsa160(const struct dsa_public_key *pub,
221             const struct dsa_private_key *key,
222             const struct dsa_signature *expected);
223
224 void
225 test_dsa256(const struct dsa_public_key *pub,
226             const struct dsa_private_key *key,
227             const struct dsa_signature *expected);
228
229 #if 0
230 void
231 test_dsa_sign(const struct dsa_public_key *pub,
232               const struct dsa_private_key *key,
233               const struct nettle_hash *hash,
234               const struct dsa_signature *expected);
235 #endif
236
237 void
238 test_dsa_verify(const struct dsa_params *params,
239                 const mpz_t pub,
240                 const struct nettle_hash *hash,
241                 struct tstring *msg,
242                 const struct dsa_signature *ref);
243
244 void
245 test_dsa_key(const struct dsa_params *params,
246              const mpz_t pub,
247              const mpz_t key,
248              unsigned q_size);
249
250 extern const struct ecc_curve * const ecc_curves[];
251
252 struct ecc_ref_point
253 {
254   const char *x;
255   const char *y;
256 };
257
258 void
259 test_ecc_point (const struct ecc_curve *ecc,
260                 const struct ecc_ref_point *ref,
261                 const mp_limb_t *p);
262
263 void
264 test_ecc_mul_a (unsigned curve, unsigned n, const mp_limb_t *p);
265
266 void
267 test_ecc_mul_h (unsigned curve, unsigned n, const mp_limb_t *p);
268
269 #endif /* WITH_HOGWEED */
270   
271 /* LDATA needs to handle NUL characters. */
272 #define LLENGTH(x) (sizeof(x) - 1)
273 #define LDATA(x) LLENGTH(x), x
274 #define LDUP(x) strlen(x), strdup(x)
275
276 #define SHEX(x) (tstring_hex(x))
277 #define SDATA(x) ((const struct tstring *)tstring_data(LLENGTH(x), x))
278 #define H(x) (SHEX(x)->data)
279
280 #define MEMEQ(length, a, b) (!memcmp((a), (b), (length)))
281
282 #define FAIL() abort()
283 #define SKIP() exit(77)
284
285 #define ASSERT(x) do {                                                  \
286     if (!(x))                                                           \
287       {                                                                 \
288         fprintf(stderr, "Assert failed: %s:%d: %s\n", \
289                 __FILE__, __LINE__, #x);                                        \
290         FAIL();                                                         \
291       }                                                                 \
292   } while(0)
293
294 #ifdef __cplusplus
295 }
296 #endif
297
298 #endif /* NETTLE_TESTUTILS_H_INCLUDED */