1 #ifndef NETTLE_TESTUTILS_H_INCLUDED
2 #define NETTLE_TESTUTILS_H_INCLUDED
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. */
15 #include "nettle-types.h"
20 # include "dsa-compat.h"
21 # include "ecc-curve.h"
23 # include "ecc-internal.h"
25 # include "gmp-glue.h"
26 # if NETTLE_USE_MINI_GMP
27 # include "knuth-lfib.h"
30 /* Undo dsa-compat name mangling */
31 #undef dsa_generate_keypair
32 #define dsa_generate_keypair nettle_dsa_generate_keypair
33 #endif /* WITH_HOGWEED */
35 #include "nettle-meta.h"
45 die(const char *format, ...) PRINTF_STYLE (1, 2) NORETURN;
57 tstring_alloc (size_t length);
63 tstring_data(size_t length, const char *data);
66 tstring_hex(const char *hex);
69 tstring_print_hex(const struct tstring *s);
71 /* Decodes a NUL-terminated hex string. */
74 print_hex(size_t length, const uint8_t *data);
76 /* The main program */
82 /* FIXME: When interface stabilizes, move to nettle-meta.h */
87 /* Size of the context struct */
88 unsigned context_size;
93 /* Suggested key size; other sizes are sometimes possible. */
96 nettle_set_key_func *set_key;
97 nettle_hash_update_func *update;
98 nettle_hash_digest_func *digest;
101 #define _NETTLE_HMAC(name, NAME, keysize) { \
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, \
111 /* Test functions deallocate their inputs when finished.*/
113 test_cipher(const struct nettle_cipher *cipher,
114 const struct tstring *key,
115 const struct tstring *cleartext,
116 const struct tstring *ciphertext);
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);
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);
133 test_cipher_stream(const struct nettle_cipher *cipher,
134 const struct tstring *key,
135 const struct tstring *cleartext,
136 const struct tstring *ciphertext);
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);
149 test_hash(const struct nettle_hash *hash,
150 const struct tstring *msg,
151 const struct tstring *digest);
154 test_hash_large(const struct nettle_hash *hash,
155 size_t count, size_t length,
157 const struct tstring *digest);
160 test_armor(const struct nettle_armor *armor,
163 const uint8_t *ascii);
168 mpn_zero_p (mp_srcptr ap, mp_size_t n);
172 mpn_out_str (FILE *f, int base, const mp_limb_t *xp, mp_size_t xn);
174 #if NETTLE_USE_MINI_GMP
175 typedef struct knuth_lfib_ctx gmp_randstate_t[1];
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
183 #endif /* NETTLE_USE_MINI_GMP */
186 xalloc_limbs (mp_size_t n);
189 write_mpn (FILE *f, int base, const mp_limb_t *xp, mp_size_t n);
192 test_rsa_set_key_1(struct rsa_public_key *pub,
193 struct rsa_private_key *key);
196 test_rsa_md5(struct rsa_public_key *pub,
197 struct rsa_private_key *key,
201 test_rsa_sha1(struct rsa_public_key *pub,
202 struct rsa_private_key *key,
206 test_rsa_sha256(struct rsa_public_key *pub,
207 struct rsa_private_key *key,
211 test_rsa_sha512(struct rsa_public_key *pub,
212 struct rsa_private_key *key,
216 test_rsa_key(struct rsa_public_key *pub,
217 struct rsa_private_key *key);
220 test_dsa160(const struct dsa_public_key *pub,
221 const struct dsa_private_key *key,
222 const struct dsa_signature *expected);
225 test_dsa256(const struct dsa_public_key *pub,
226 const struct dsa_private_key *key,
227 const struct dsa_signature *expected);
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);
238 test_dsa_verify(const struct dsa_params *params,
240 const struct nettle_hash *hash,
242 const struct dsa_signature *ref);
245 test_dsa_key(const struct dsa_params *params,
250 extern const struct ecc_curve * const ecc_curves[];
259 test_ecc_point (const struct ecc_curve *ecc,
260 const struct ecc_ref_point *ref,
264 test_ecc_mul_a (unsigned curve, unsigned n, const mp_limb_t *p);
267 test_ecc_mul_h (unsigned curve, unsigned n, const mp_limb_t *p);
269 #endif /* WITH_HOGWEED */
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)
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)
280 #define MEMEQ(length, a, b) (!memcmp((a), (b), (length)))
282 #define FAIL() abort()
283 #define SKIP() exit(77)
285 #define ASSERT(x) do { \
288 fprintf(stderr, "Assert failed: %s:%d: %s\n", \
289 __FILE__, __LINE__, #x); \
298 #endif /* NETTLE_TESTUTILS_H_INCLUDED */