Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / testsuite / testutils.h
1 #ifndef NETTLE_TESTUTILS_H_INCLUDED
2 #define NETTLE_TESTUTILS_H_INCLUDED
3
4 #if HAVE_CONFIG_H
5 # include "config.h"
6 #endif
7
8 #include "nettle-types.h"
9
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 #if HAVE_LIBGMP
16 # include "bignum.h"
17 #endif
18
19 #if WITH_HOGWEED
20 # include "rsa.h"
21 # include "dsa.h"
22 # include "ecc-curve.h"
23 # include "ecc.h"
24 # include "ecc-internal.h"
25 # include "ecdsa.h"
26 # include "gmp-glue.h"
27 #endif
28
29 #include "nettle-meta.h"
30
31 /* Forward declare */
32 struct nettle_aead;
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 void
39 die(const char *format, ...) PRINTF_STYLE (1, 2) NORETURN;
40
41 void *
42 xalloc(size_t size);
43
44 struct tstring {
45   struct tstring *next;
46   unsigned length;
47   uint8_t data[1];
48 };
49
50 struct tstring *
51 tstring_alloc (unsigned length);
52
53 void
54 tstring_clear(void);
55
56 struct tstring *
57 tstring_data(unsigned length, const char *data);
58
59 struct tstring *
60 tstring_hex(const char *hex);
61
62 void
63 tstring_print_hex(const struct tstring *s);
64
65 /* Decodes a NUL-terminated hex string. */
66
67 void
68 print_hex(unsigned length, const uint8_t *data);
69
70 /* The main program */
71 void
72 test_main(void);
73
74 extern int verbose;
75
76 /* FIXME: When interface stabilizes, move to nettle-meta.h */
77 struct nettle_mac
78 {
79   const char *name;
80
81   /* Size of the context struct */
82   unsigned context_size;
83
84   /* Size of digests */
85   unsigned digest_size;
86
87   /* Suggested key size; other sizes are sometimes possible. */
88   unsigned key_size;
89   
90   nettle_set_key_func *set_key;
91   nettle_hash_update_func *update;
92   nettle_hash_digest_func *digest;
93 };
94
95 #define _NETTLE_HMAC(name, NAME, keysize) {     \
96   #name,                                        \
97   sizeof(struct hmac_##name##_ctx),             \
98   NAME##_DIGEST_SIZE,                           \
99   NAME##_DIGEST_SIZE,                           \
100   hmac_##name##_set_key,                        \
101   hmac_##name##_update,                         \
102   hmac_##name##_digest,                         \
103 }
104
105 /* Test functions deallocate their inputs when finished.*/
106 void
107 test_cipher(const struct nettle_cipher *cipher,
108             const struct tstring *key,
109             const struct tstring *cleartext,
110             const struct tstring *ciphertext);
111
112 void
113 test_cipher_cbc(const struct nettle_cipher *cipher,
114                 const struct tstring *key,
115                 const struct tstring *cleartext,
116                 const struct tstring *ciphertext,
117                 const struct tstring *iv);
118
119 void
120 test_cipher_ctr(const struct nettle_cipher *cipher,
121                 const struct tstring *key,
122                 const struct tstring *cleartext,
123                 const struct tstring *ciphertext,
124                 const struct tstring *iv);
125
126 void
127 test_cipher_stream(const struct nettle_cipher *cipher,
128                    const struct tstring *key,
129                    const struct tstring *cleartext,
130                    const struct tstring *ciphertext);
131
132 void
133 test_aead(const struct nettle_aead *aead,
134           const struct tstring *key,
135           const struct tstring *authtext,
136           const struct tstring *cleartext,
137           const struct tstring *ciphertext,
138           const struct tstring *iv,
139           const struct tstring *digest);
140
141 void
142 test_hash(const struct nettle_hash *hash,
143           const struct tstring *msg,
144           const struct tstring *digest);
145
146 void
147 test_hash_large(const struct nettle_hash *hash,
148                 unsigned count, unsigned length,
149                 uint8_t c,
150                 const struct tstring *digest);
151
152 void
153 test_armor(const struct nettle_armor *armor,
154            unsigned data_length,
155            const uint8_t *data,
156            const uint8_t *ascii);
157
158 #if WITH_HOGWEED
159 mp_limb_t *
160 xalloc_limbs (mp_size_t n);
161
162 void
163 test_rsa_set_key_1(struct rsa_public_key *pub,
164                    struct rsa_private_key *key);
165
166 void
167 test_rsa_md5(struct rsa_public_key *pub,
168              struct rsa_private_key *key,
169              mpz_t expected);
170
171 void
172 test_rsa_sha1(struct rsa_public_key *pub,
173               struct rsa_private_key *key,
174               mpz_t expected);
175
176 void
177 test_rsa_sha256(struct rsa_public_key *pub,
178                 struct rsa_private_key *key,
179                 mpz_t expected);
180
181 void
182 test_rsa_sha512(struct rsa_public_key *pub,
183                 struct rsa_private_key *key,
184                 mpz_t expected);
185
186 void
187 test_rsa_key(struct rsa_public_key *pub,
188              struct rsa_private_key *key);
189
190 void
191 test_dsa160(const struct dsa_public_key *pub,
192             const struct dsa_private_key *key,
193             const struct dsa_signature *expected);
194
195 void
196 test_dsa256(const struct dsa_public_key *pub,
197             const struct dsa_private_key *key,
198             const struct dsa_signature *expected);
199
200 void
201 test_dsa_key(struct dsa_public_key *pub,
202              struct dsa_private_key *key,
203              unsigned q_size);
204
205 extern const struct ecc_curve * const ecc_curves[];
206
207 void
208 test_ecc_mul_a (unsigned curve, unsigned n, const mp_limb_t *p);
209
210 void
211 test_ecc_mul_j (unsigned curve, unsigned n, const mp_limb_t *p);
212
213 #endif /* WITH_HOGWEED */
214   
215 /* LDATA needs to handle NUL characters. */
216 #define LLENGTH(x) (sizeof(x) - 1)
217 #define LDATA(x) LLENGTH(x), x
218 #define LDUP(x) strlen(x), strdup(x)
219
220 #define SHEX(x) (tstring_hex(x))
221 #define SDATA(x) ((const struct tstring *)tstring_data(LLENGTH(x), x))
222 #define H(x) (SHEX(x)->data)
223
224 #define MEMEQ(length, a, b) (!memcmp((a), (b), (length)))
225
226 #define FAIL() abort()
227 #define SKIP() exit(77)
228
229 #define ASSERT(x) do {                                                  \
230     if (!(x))                                                           \
231       {                                                                 \
232         fprintf(stderr, "Assert failed %d: %s\n", __LINE__, #x);        \
233         FAIL();                                                         \
234       }                                                                 \
235   } while(0)
236
237 #ifdef __cplusplus
238 }
239 #endif
240
241 #endif /* NETTLE_TESTUTILS_H_INCLUDED */