Initialize Tizen 2.3
[external/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 <string.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13
14 #if HAVE_LIBGMP
15 # include "bignum.h"
16 #endif
17
18 #if WITH_HOGWEED
19 # include "rsa.h"
20 # include "dsa.h"
21 #endif
22
23 #include "nettle-meta.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 void *
30 xalloc(size_t size);
31
32 /* Decodes a NUL-terminated hex string. */
33
34 unsigned
35 decode_hex_length(const char *hex);
36
37 int
38 decode_hex(uint8_t *dst, const char *hex);
39
40 /* Allocates space */
41 const uint8_t *
42 decode_hex_dup(const char *hex);
43
44 void
45 print_hex(unsigned length, const uint8_t *data);
46
47 /* The main program */
48 int
49 test_main(void);
50
51 extern int verbose;
52
53 /* FIXME: When interface stabilizes, move to nettle-meta.h */
54 struct nettle_mac
55 {
56   const char *name;
57
58   /* Size of the context struct */
59   unsigned context_size;
60
61   /* Size of digests */
62   unsigned digest_size;
63
64   /* Suggested key size; other sizes are sometimes possible. */
65   unsigned key_size;
66   
67   nettle_set_key_func *set_key;
68   nettle_hash_update_func *update;
69   nettle_hash_digest_func *digest;
70 };
71
72 #define _NETTLE_HMAC(name, NAME, keysize) {     \
73   #name,                                        \
74   sizeof(struct hmac_##name##_ctx),             \
75   NAME##_DIGEST_SIZE,                           \
76   NAME##_DIGEST_SIZE,                           \
77   hmac_##name##_set_key,                        \
78   hmac_##name##_update,                         \
79   hmac_##name##_digest,                         \
80 }
81  
82 void
83 test_cipher(const struct nettle_cipher *cipher,
84             unsigned key_length,
85             const uint8_t *key,
86             unsigned length,
87             const uint8_t *cleartext,
88             const uint8_t *ciphertext);
89
90 void
91 test_cipher_cbc(const struct nettle_cipher *cipher,
92                 unsigned key_length,
93                 const uint8_t *key,
94                 unsigned length,
95                 const uint8_t *cleartext,
96                 const uint8_t *ciphertext,
97                 const uint8_t *iv);
98
99 void
100 test_cipher_ctr(const struct nettle_cipher *cipher,
101                 unsigned key_length,
102                 const uint8_t *key,
103                 unsigned length,
104                 const uint8_t *cleartext,
105                 const uint8_t *ciphertext,
106                 const uint8_t *iv);
107
108 void
109 test_cipher_stream(const struct nettle_cipher *cipher,
110                    unsigned key_length,
111                    const uint8_t *key,
112                    unsigned length,
113                    const uint8_t *cleartext,
114                    const uint8_t *ciphertext);
115
116 void
117 test_hash(const struct nettle_hash *hash,
118           unsigned length,
119           const uint8_t *data,
120           const uint8_t *digest);
121
122 void
123 test_hash_large(const struct nettle_hash *hash,
124                 unsigned count, unsigned length,
125                 uint8_t c,
126                 const uint8_t *digest);
127
128 void
129 test_mac(const struct nettle_mac *mac,
130          unsigned key_length, const uint8_t *key,
131          unsigned msg_length, const uint8_t *msg,
132          const uint8_t *digest);
133
134 void
135 test_armor(const struct nettle_armor *armor,
136            unsigned data_length,
137            const uint8_t *data,
138            const uint8_t *ascii);
139
140 #if WITH_HOGWEED
141 void
142 test_rsa_set_key_1(struct rsa_public_key *pub,
143                    struct rsa_private_key *key);
144
145 void
146 test_rsa_md5(struct rsa_public_key *pub,
147              struct rsa_private_key *key,
148              mpz_t expected);
149
150 void
151 test_rsa_sha1(struct rsa_public_key *pub,
152               struct rsa_private_key *key,
153               mpz_t expected);
154
155 void
156 test_rsa_sha256(struct rsa_public_key *pub,
157                 struct rsa_private_key *key,
158                 mpz_t expected);
159
160 void
161 test_rsa_sha512(struct rsa_public_key *pub,
162                 struct rsa_private_key *key,
163                 mpz_t expected);
164
165 void
166 test_rsa_key(struct rsa_public_key *pub,
167              struct rsa_private_key *key);
168
169 void
170 test_dsa160(const struct dsa_public_key *pub,
171             const struct dsa_private_key *key,
172             const struct dsa_signature *expected);
173
174 void
175 test_dsa256(const struct dsa_public_key *pub,
176             const struct dsa_private_key *key,
177             const struct dsa_signature *expected);
178
179 void
180 test_dsa_key(struct dsa_public_key *pub,
181              struct dsa_private_key *key,
182              unsigned q_size);
183
184 #endif /* WITH_HOGWEED */
185
186 #ifdef __cplusplus
187 }
188 #endif
189
190 #define H2(d, s) decode_hex((d), (s))
191 #define H(x) decode_hex_dup(x)
192 #define HL(x) decode_hex_length(x), decode_hex_dup(x)
193
194 /* LDATA needs to handle NUL characters. */
195 #define LLENGTH(x) (sizeof(x) - 1)
196 #define LDATA(x) (sizeof(x) - 1), x
197 #define LDUP(x) strlen(x), strdup(x)
198
199 #define MEMEQ(length, a, b) (!memcmp((a), (b), (length)))
200 #define MEMEQH(length, a, b) \
201 ((length) == decode_hex_length((b)) \
202  && !memcmp((a), decode_hex_dup((b)), (length)))
203
204 #define FAIL() abort()
205 #define SKIP() exit(77)
206 #define SUCCESS() return EXIT_SUCCESS
207
208 #define ASSERT(x) do { if (!(x)) FAIL(); } while(0)
209
210 #endif /* NETTLE_TESTUTILS_H_INCLUDED */