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 uint8_t *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_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);
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);
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);
147 test_cipher_stream(const struct nettle_cipher *cipher,
148 const struct tstring *key,
149 const struct tstring *cleartext,
150 const struct tstring *ciphertext);
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);
163 test_hash(const struct nettle_hash *hash,
164 const struct tstring *msg,
165 const struct tstring *digest);
168 test_hash_large(const struct nettle_hash *hash,
169 size_t count, size_t length,
171 const struct tstring *digest);
174 test_mac(const struct nettle_mac *mac,
175 const struct tstring *key,
176 const struct tstring *msg,
177 const struct tstring *digest);
180 test_armor(const struct nettle_armor *armor,
187 #if NETTLE_USE_MINI_GMP
188 typedef struct knuth_lfib_ctx gmp_randstate_t[1];
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
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
200 #endif /* NETTLE_USE_MINI_GMP */
204 mpn_zero_p (mp_srcptr ap, mp_size_t n);
208 mpn_out_str (FILE *f, int base, const mp_limb_t *xp, mp_size_t xn);
211 xalloc_limbs (mp_size_t n);
214 write_mpn (FILE *f, int base, const mp_limb_t *xp, mp_size_t n);
217 test_rsa_set_key_1(struct rsa_public_key *pub,
218 struct rsa_private_key *key);
221 test_rsa_md5(struct rsa_public_key *pub,
222 struct rsa_private_key *key,
226 test_rsa_sha1(struct rsa_public_key *pub,
227 struct rsa_private_key *key,
231 test_rsa_sha256(struct rsa_public_key *pub,
232 struct rsa_private_key *key,
236 test_rsa_sha512(struct rsa_public_key *pub,
237 struct rsa_private_key *key,
241 test_rsa_key(struct rsa_public_key *pub,
242 struct rsa_private_key *key);
245 test_dsa160(const struct dsa_public_key *pub,
246 const struct dsa_private_key *key,
247 const struct dsa_signature *expected);
250 test_dsa256(const struct dsa_public_key *pub,
251 const struct dsa_private_key *key,
252 const struct dsa_signature *expected);
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);
263 test_dsa_verify(const struct dsa_params *params,
265 const struct nettle_hash *hash,
267 const struct dsa_signature *ref);
270 test_dsa_key(const struct dsa_params *params,
275 extern const struct ecc_curve * const ecc_curves[];
284 test_ecc_point (const struct ecc_curve *ecc,
285 const struct ecc_ref_point *ref,
289 test_ecc_mul_a (unsigned curve, unsigned n, const mp_limb_t *p);
292 test_ecc_mul_h (unsigned curve, unsigned n, const mp_limb_t *p);
294 #endif /* WITH_HOGWEED */
296 /* String literal of type unsigned char. The GNUC version is safer. */
298 #define US(s) ({ static const unsigned char us_s[] = s; us_s; })
300 #define US(s) ((const uint8_t *) (s))
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)
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)
312 #define MEMEQ(length, a, b) (!memcmp((a), (b), (length)))
314 #define FAIL() abort()
315 #define SKIP() exit(77)
317 #define ASSERT(x) do { \
320 fprintf(stderr, "Assert failed: %s:%d: %s\n", \
321 __FILE__, __LINE__, #x); \
330 #endif /* NETTLE_TESTUTILS_H_INCLUDED */