testutils.h 6.59 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
#ifndef NETTLE_TESTUTILS_H_INCLUDED
#define NETTLE_TESTUTILS_H_INCLUDED

4
5
/* config.h should usually be first in each .c file. This is an
   exception, include it here to reduce clutter in the test cases. */
6
7
8
9
#if HAVE_CONFIG_H
# include "config.h"
#endif

10
#include <stdarg.h>
11
#include <stdlib.h>
12
#include <stdio.h>
13
#include <string.h>
14

15
16
17
#include "nettle-types.h"
#include "version.h"

18
#if WITH_HOGWEED
19
# include "rsa.h"
20
# include "dsa-compat.h"
Niels Möller's avatar
Niels Möller committed
21
# include "ecc-curve.h"
Niels Möller's avatar
Niels Möller committed
22
# include "ecc.h"
Niels Möller's avatar
Niels Möller committed
23
# include "ecc-internal.h"
Niels Möller's avatar
Niels Möller committed
24
# include "ecdsa.h"
Niels Möller's avatar
Niels Möller committed
25
# include "gmp-glue.h"
26
27
28
# if NETTLE_USE_MINI_GMP
#  include "knuth-lfib.h"
# endif
29

Niels Möller's avatar
Niels Möller committed
30
/* Undo dsa-compat name mangling */
31
32
#undef dsa_generate_keypair
#define dsa_generate_keypair nettle_dsa_generate_keypair
33
#endif /* WITH_HOGWEED */
34
35
36

#include "nettle-meta.h"

37
38
39
/* Forward declare */
struct nettle_aead;

Niels Möller's avatar
Niels Möller committed
40
41
42
43
#ifdef __cplusplus
extern "C" {
#endif

44
45
46
void
die(const char *format, ...) PRINTF_STYLE (1, 2) NORETURN;

47
48
49
void *
xalloc(size_t size);

50
51
struct tstring {
  struct tstring *next;
Niels Möller's avatar
Niels Möller committed
52
  size_t length;
53
54
55
56
  uint8_t data[1];
};

struct tstring *
Niels Möller's avatar
Niels Möller committed
57
tstring_alloc (size_t length);
58
59
60
61
62

void
tstring_clear(void);

struct tstring *
Niels Möller's avatar
Niels Möller committed
63
tstring_data(size_t length, const char *data);
64

65
66
struct tstring *
tstring_hex(const char *hex);
67

68
69
void
tstring_print_hex(const struct tstring *s);
Niels Möller's avatar
Niels Möller committed
70

71
/* Decodes a NUL-terminated hex string. */
Niels Möller's avatar
Niels Möller committed
72

73
void
Niels Möller's avatar
Niels Möller committed
74
print_hex(size_t length, const uint8_t *data);
75

Niels Möller's avatar
Niels Möller committed
76
/* The main program */
77
void
Niels Möller's avatar
Niels Möller committed
78
79
80
81
test_main(void);

extern int verbose;

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* FIXME: When interface stabilizes, move to nettle-meta.h */
struct nettle_mac
{
  const char *name;

  /* Size of the context struct */
  unsigned context_size;

  /* Size of digests */
  unsigned digest_size;

  /* Suggested key size; other sizes are sometimes possible. */
  unsigned key_size;
  
  nettle_set_key_func *set_key;
  nettle_hash_update_func *update;
  nettle_hash_digest_func *digest;
};

#define _NETTLE_HMAC(name, NAME, keysize) {	\
  #name,					\
  sizeof(struct hmac_##name##_ctx),		\
  NAME##_DIGEST_SIZE,				\
  NAME##_DIGEST_SIZE,				\
  hmac_##name##_set_key,			\
  hmac_##name##_update,				\
  hmac_##name##_digest,				\
}
110
111

/* Test functions deallocate their inputs when finished.*/
112
113
void
test_cipher(const struct nettle_cipher *cipher,
114
115
116
	    const struct tstring *key,
	    const struct tstring *cleartext,
	    const struct tstring *ciphertext);
117
118
119

void
test_cipher_cbc(const struct nettle_cipher *cipher,
120
121
122
123
		const struct tstring *key,
		const struct tstring *cleartext,
		const struct tstring *ciphertext,
		const struct tstring *iv);
124

125
126
void
test_cipher_ctr(const struct nettle_cipher *cipher,
127
128
129
130
		const struct tstring *key,
		const struct tstring *cleartext,
		const struct tstring *ciphertext,
		const struct tstring *iv);
131

132
133
void
test_cipher_stream(const struct nettle_cipher *cipher,
134
135
136
		   const struct tstring *key,
		   const struct tstring *cleartext,
		   const struct tstring *ciphertext);
137

138
139
void
test_aead(const struct nettle_aead *aead,
140
	  nettle_hash_update_func *set_nonce,
141
142
143
144
	  const struct tstring *key,
	  const struct tstring *authtext,
	  const struct tstring *cleartext,
	  const struct tstring *ciphertext,
145
	  const struct tstring *nonce,
146
	  const struct tstring *digest);
147

148
149
void
test_hash(const struct nettle_hash *hash,
150
151
	  const struct tstring *msg,
	  const struct tstring *digest);
Niels Möller's avatar
Niels Möller committed
152

153
154
void
test_hash_large(const struct nettle_hash *hash,
Niels Möller's avatar
Niels Möller committed
155
		size_t count, size_t length,
156
		uint8_t c,
157
		const struct tstring *digest);
158

Niels Möller's avatar
Niels Möller committed
159
160
void
test_armor(const struct nettle_armor *armor,
Niels Möller's avatar
Niels Möller committed
161
           size_t data_length,
Niels Möller's avatar
Niels Möller committed
162
           const uint8_t *data,
163
           const char *ascii);
Niels Möller's avatar
Niels Möller committed
164

165
#if WITH_HOGWEED
166
167
168
169
170
#ifndef mpn_zero_p
int
mpn_zero_p (mp_srcptr ap, mp_size_t n);
#endif

171
172
173
void
mpn_out_str (FILE *f, int base, const mp_limb_t *xp, mp_size_t xn);

174
175
176
177
178
179
180
181
182
183
184
#if NETTLE_USE_MINI_GMP
typedef struct knuth_lfib_ctx gmp_randstate_t[1];

void gmp_randinit_default (struct knuth_lfib_ctx *ctx);
#define gmp_randclear(state)
void mpz_urandomb (mpz_t r, struct knuth_lfib_ctx *ctx, mp_bitcnt_t bits);
/* This is cheating */
#define mpz_rrandomb mpz_urandomb

#endif /* NETTLE_USE_MINI_GMP */

Niels Möller's avatar
Niels Möller committed
185
186
187
mp_limb_t *
xalloc_limbs (mp_size_t n);

188
189
190
void
write_mpn (FILE *f, int base, const mp_limb_t *xp, mp_size_t n);

191
192
193
194
void
test_rsa_set_key_1(struct rsa_public_key *pub,
		   struct rsa_private_key *key);

195
196
197
198
199
200
201
202
203
void
test_rsa_md5(struct rsa_public_key *pub,
	     struct rsa_private_key *key,
	     mpz_t expected);

void
test_rsa_sha1(struct rsa_public_key *pub,
	      struct rsa_private_key *key,
	      mpz_t expected);
Niels Möller's avatar
Niels Möller committed
204

205
206
207
208
209
void
test_rsa_sha256(struct rsa_public_key *pub,
		struct rsa_private_key *key,
		mpz_t expected);

210
211
212
213
214
void
test_rsa_sha512(struct rsa_public_key *pub,
		struct rsa_private_key *key,
		mpz_t expected);

Niels Möller's avatar
Niels Möller committed
215
216
217
218
void
test_rsa_key(struct rsa_public_key *pub,
	     struct rsa_private_key *key);

Niels Möller's avatar
Niels Möller committed
219
void
220
test_dsa160(const struct dsa_public_key *pub,
221
222
	    const struct dsa_private_key *key,
	    const struct dsa_signature *expected);
223
224
225

void
test_dsa256(const struct dsa_public_key *pub,
226
227
	    const struct dsa_private_key *key,
	    const struct dsa_signature *expected);
Niels Möller's avatar
Niels Möller committed
228

229
#if 0
Niels Möller's avatar
Niels Möller committed
230
231
232
233
234
void
test_dsa_sign(const struct dsa_public_key *pub,
	      const struct dsa_private_key *key,
	      const struct nettle_hash *hash,
	      const struct dsa_signature *expected);
235
#endif
Niels Möller's avatar
Niels Möller committed
236
237

void
238
239
test_dsa_verify(const struct dsa_params *params,
		const mpz_t pub,
Niels Möller's avatar
Niels Möller committed
240
241
242
243
		const struct nettle_hash *hash,
		struct tstring *msg,
		const struct dsa_signature *ref);

244
void
245
246
247
test_dsa_key(const struct dsa_params *params,
	     const mpz_t pub,
	     const mpz_t key,
248
	     unsigned q_size);
249

Niels Möller's avatar
Niels Möller committed
250
251
extern const struct ecc_curve * const ecc_curves[];

252
253
254
255
256
257
258
259
260
261
262
struct ecc_ref_point
{
  const char *x;
  const char *y;
};

void
test_ecc_point (const struct ecc_curve *ecc,
		const struct ecc_ref_point *ref,
		const mp_limb_t *p);

Niels Möller's avatar
Niels Möller committed
263
264
265
266
void
test_ecc_mul_a (unsigned curve, unsigned n, const mp_limb_t *p);

void
267
test_ecc_mul_h (unsigned curve, unsigned n, const mp_limb_t *p);
Niels Möller's avatar
Niels Möller committed
268

269
#endif /* WITH_HOGWEED */
270
271
272
273
274
275
276

/* String literal of type unsigned char. The GNUC version is safer. */
#if __GNUC__
#define US(s) ({ static const unsigned char us_s[] = s; us_s; })
#else
#define US(s) ((const uint8_t *) (s))
#endif
277
  
278
279
/* LDATA needs to handle NUL characters. */
#define LLENGTH(x) (sizeof(x) - 1)
280
#define LDATA(x) LLENGTH(x), US(x)
Niels Möller's avatar
Niels Möller committed
281
#define LDUP(x) strlen(x), strdup(x)
282

283
#define SHEX(x) (tstring_hex(x))
284
285
286
#define SDATA(x) ((const struct tstring *)tstring_data(LLENGTH(x), x))
#define H(x) (SHEX(x)->data)

287
288
289
290
#define MEMEQ(length, a, b) (!memcmp((a), (b), (length)))

#define FAIL() abort()
#define SKIP() exit(77)
Niels Möller's avatar
Niels Möller committed
291

292
293
294
#define ASSERT(x) do {							\
    if (!(x))								\
      {									\
295
296
	fprintf(stderr, "Assert failed: %s:%d: %s\n", \
		__FILE__, __LINE__, #x);					\
297
298
299
	FAIL();								\
      }									\
  } while(0)
Niels Möller's avatar
Niels Möller committed
300

301
302
303
304
#ifdef __cplusplus
}
#endif

Niels Möller's avatar
Niels Möller committed
305
#endif /* NETTLE_TESTUTILS_H_INCLUDED */