testutils.h 5.71 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
6
7
#if HAVE_CONFIG_H
# include "config.h"
#endif

8
#include "nettle-types.h"
Niels Möller's avatar
Niels Möller committed
9

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

15
#if WITH_HOGWEED
16
# include "rsa.h"
17
# include "dsa-compat.h"
Niels Möller's avatar
Niels Möller committed
18
# include "ecc-curve.h"
Niels Möller's avatar
Niels Möller committed
19
# include "ecc.h"
Niels Möller's avatar
Niels Möller committed
20
# include "ecc-internal.h"
Niels Möller's avatar
Niels Möller committed
21
# include "ecdsa.h"
Niels Möller's avatar
Niels Möller committed
22
# include "gmp-glue.h"
23

Niels Möller's avatar
Niels Möller committed
24
/* Undo dsa-compat name mangling */
25
26
#undef dsa_generate_keypair
#define dsa_generate_keypair nettle_dsa_generate_keypair
27
#endif
28
29
30

#include "nettle-meta.h"

31
32
33
/* Forward declare */
struct nettle_aead;

Niels Möller's avatar
Niels Möller committed
34
35
36
37
#ifdef __cplusplus
extern "C" {
#endif

38
39
40
void
die(const char *format, ...) PRINTF_STYLE (1, 2) NORETURN;

41
42
43
void *
xalloc(size_t size);

44
45
struct tstring {
  struct tstring *next;
Niels Möller's avatar
Niels Möller committed
46
  size_t length;
47
48
49
50
  uint8_t data[1];
};

struct tstring *
Niels Möller's avatar
Niels Möller committed
51
tstring_alloc (size_t length);
52
53
54
55
56

void
tstring_clear(void);

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

59
60
struct tstring *
tstring_hex(const char *hex);
61

62
63
void
tstring_print_hex(const struct tstring *s);
Niels Möller's avatar
Niels Möller committed
64

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

67
void
Niels Möller's avatar
Niels Möller committed
68
print_hex(size_t length, const uint8_t *data);
69

Niels Möller's avatar
Niels Möller committed
70
/* The main program */
71
void
Niels Möller's avatar
Niels Möller committed
72
73
74
75
test_main(void);

extern int verbose;

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* 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,				\
}
104
105

/* Test functions deallocate their inputs when finished.*/
106
107
void
test_cipher(const struct nettle_cipher *cipher,
108
109
110
	    const struct tstring *key,
	    const struct tstring *cleartext,
	    const struct tstring *ciphertext);
111
112
113

void
test_cipher_cbc(const struct nettle_cipher *cipher,
114
115
116
117
		const struct tstring *key,
		const struct tstring *cleartext,
		const struct tstring *ciphertext,
		const struct tstring *iv);
118

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

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

132
133
void
test_aead(const struct nettle_aead *aead,
134
	  nettle_hash_update_func *set_nonce,
135
136
137
138
	  const struct tstring *key,
	  const struct tstring *authtext,
	  const struct tstring *cleartext,
	  const struct tstring *ciphertext,
139
	  const struct tstring *nonce,
140
	  const struct tstring *digest);
141

142
143
void
test_hash(const struct nettle_hash *hash,
144
145
	  const struct tstring *msg,
	  const struct tstring *digest);
Niels Möller's avatar
Niels Möller committed
146

147
148
void
test_hash_large(const struct nettle_hash *hash,
Niels Möller's avatar
Niels Möller committed
149
		size_t count, size_t length,
150
		uint8_t c,
151
		const struct tstring *digest);
152

Niels Möller's avatar
Niels Möller committed
153
154
void
test_armor(const struct nettle_armor *armor,
Niels Möller's avatar
Niels Möller committed
155
           size_t data_length,
Niels Möller's avatar
Niels Möller committed
156
157
158
           const uint8_t *data,
           const uint8_t *ascii);

159
#if WITH_HOGWEED
Niels Möller's avatar
Niels Möller committed
160
161
162
mp_limb_t *
xalloc_limbs (mp_size_t n);

163
164
165
void
write_mpn (FILE *f, int base, const mp_limb_t *xp, mp_size_t n);

166
167
168
169
void
test_rsa_set_key_1(struct rsa_public_key *pub,
		   struct rsa_private_key *key);

170
171
172
173
174
175
176
177
178
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
179

180
181
182
183
184
void
test_rsa_sha256(struct rsa_public_key *pub,
		struct rsa_private_key *key,
		mpz_t expected);

185
186
187
188
189
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
190
191
192
193
void
test_rsa_key(struct rsa_public_key *pub,
	     struct rsa_private_key *key);

Niels Möller's avatar
Niels Möller committed
194
void
195
test_dsa160(const struct dsa_public_key *pub,
196
197
	    const struct dsa_private_key *key,
	    const struct dsa_signature *expected);
198
199
200

void
test_dsa256(const struct dsa_public_key *pub,
201
202
	    const struct dsa_private_key *key,
	    const struct dsa_signature *expected);
Niels Möller's avatar
Niels Möller committed
203

204
#if 0
Niels Möller's avatar
Niels Möller committed
205
206
207
208
209
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);
210
#endif
Niels Möller's avatar
Niels Möller committed
211
212

void
213
214
test_dsa_verify(const struct dsa_params *params,
		const mpz_t pub,
Niels Möller's avatar
Niels Möller committed
215
216
217
218
		const struct nettle_hash *hash,
		struct tstring *msg,
		const struct dsa_signature *ref);

219
void
220
221
222
test_dsa_key(const struct dsa_params *params,
	     const mpz_t pub,
	     const mpz_t key,
223
	     unsigned q_size);
224

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

227
228
229
230
231
232
233
234
235
236
237
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
238
239
240
241
void
test_ecc_mul_a (unsigned curve, unsigned n, const mp_limb_t *p);

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

244
#endif /* WITH_HOGWEED */
245
  
246
247
/* LDATA needs to handle NUL characters. */
#define LLENGTH(x) (sizeof(x) - 1)
248
#define LDATA(x) LLENGTH(x), x
Niels Möller's avatar
Niels Möller committed
249
#define LDUP(x) strlen(x), strdup(x)
250

251
#define SHEX(x) (tstring_hex(x))
252
253
254
#define SDATA(x) ((const struct tstring *)tstring_data(LLENGTH(x), x))
#define H(x) (SHEX(x)->data)

255
256
257
258
#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
259

260
261
262
#define ASSERT(x) do {							\
    if (!(x))								\
      {									\
263
264
	fprintf(stderr, "Assert failed: %s:%d: %s\n", \
		__FILE__, __LINE__, #x);					\
265
266
267
	FAIL();								\
      }									\
  } while(0)
Niels Möller's avatar
Niels Möller committed
268

269
270
271
272
#ifdef __cplusplus
}
#endif

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