1 | /*
|
---|
2 | * Copyright (c) 2010 Kungliga Tekniska Högskolan
|
---|
3 | * (Royal Institute of Technology, Stockholm, Sweden).
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * 1. Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | *
|
---|
13 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer in the
|
---|
15 | * documentation and/or other materials provided with the distribution.
|
---|
16 | *
|
---|
17 | * 3. Neither the name of the Institute nor the names of its contributors
|
---|
18 | * may be used to endorse or promote products derived from this software
|
---|
19 | * without specific prior written permission.
|
---|
20 | *
|
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
---|
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
---|
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
31 | * SUCH DAMAGE.
|
---|
32 | */
|
---|
33 |
|
---|
34 | #include <config.h>
|
---|
35 |
|
---|
36 | #include <sys/types.h>
|
---|
37 | #include <limits.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <string.h>
|
---|
41 | #include <roken.h>
|
---|
42 |
|
---|
43 | #include <evp.h>
|
---|
44 | #include <hmac.h>
|
---|
45 | #include <err.h>
|
---|
46 |
|
---|
47 | struct tests {
|
---|
48 | const EVP_CIPHER *(*cipher)(void);
|
---|
49 | const char *name;
|
---|
50 | void *key;
|
---|
51 | size_t keysize;
|
---|
52 | void *iv;
|
---|
53 | size_t datasize;
|
---|
54 | void *indata;
|
---|
55 | void *outdata;
|
---|
56 | void *outiv;
|
---|
57 | };
|
---|
58 |
|
---|
59 | struct tests tests[] = {
|
---|
60 | {
|
---|
61 | EVP_aes_256_cbc,
|
---|
62 | "aes-256",
|
---|
63 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
---|
64 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
---|
65 | 32,
|
---|
66 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
---|
67 | 16,
|
---|
68 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
---|
69 | "\xdc\x95\xc0\x78\xa2\x40\x89\x89\xad\x48\xa2\x14\x92\x84\x20\x87"
|
---|
70 | },
|
---|
71 | #if 0
|
---|
72 | {
|
---|
73 | EVP_aes_128_cfb8,
|
---|
74 | "aes-cfb8-128",
|
---|
75 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
---|
76 | 16,
|
---|
77 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
---|
78 | 16,
|
---|
79 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
---|
80 | "\x66\xe9\x4b\xd4\xef\x8a\x2c\x3b\x88\x4c\xfa\x59\xca\x34\x2b\x2e"
|
---|
81 | },
|
---|
82 | #endif
|
---|
83 | {
|
---|
84 | EVP_des_ede3_cbc,
|
---|
85 | "des-ede3",
|
---|
86 | "\x19\x17\xff\xe6\xbb\x77\x2e\xfc"
|
---|
87 | "\x29\x76\x43\xbc\x63\x56\x7e\x9a"
|
---|
88 | "\x00\x2e\x4d\x43\x1d\x5f\xfd\x58",
|
---|
89 | 24,
|
---|
90 | "\xbf\x9a\x12\xb7\x26\x69\xfd\x05",
|
---|
91 | 16,
|
---|
92 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
---|
93 | "\x55\x95\x97\x76\xa9\x6c\x66\x40\x64\xc7\xf4\x1c\x21\xb7\x14\x1b"
|
---|
94 | },
|
---|
95 | #if 0
|
---|
96 | {
|
---|
97 | EVP_camellia_128_cbc,
|
---|
98 | "camellia128",
|
---|
99 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
---|
100 | 16,
|
---|
101 | "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
---|
102 | 16,
|
---|
103 | "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
---|
104 | "\x07\x92\x3A\x39\xEB\x0A\x81\x7D\x1C\x4D\x87\xBD\xB8\x2D\x1F\x1C",
|
---|
105 | NULL
|
---|
106 | },
|
---|
107 | #endif
|
---|
108 | {
|
---|
109 | EVP_rc4,
|
---|
110 | "rc4 8",
|
---|
111 | "\x01\x23\x45\x67\x89\xAB\xCD\xEF",
|
---|
112 | 8,
|
---|
113 | NULL,
|
---|
114 | 8,
|
---|
115 | "\x00\x00\x00\x00\x00\x00\x00\x00",
|
---|
116 | "\x74\x94\xC2\xE7\x10\x4B\x08\x79",
|
---|
117 | NULL
|
---|
118 | },
|
---|
119 | {
|
---|
120 | EVP_rc4,
|
---|
121 | "rc4 5",
|
---|
122 | "\x61\x8a\x63\xd2\xfb",
|
---|
123 | 5,
|
---|
124 | NULL,
|
---|
125 | 5,
|
---|
126 | "\xdc\xee\x4c\xf9\x2c",
|
---|
127 | "\xf1\x38\x29\xc9\xde",
|
---|
128 | NULL
|
---|
129 | },
|
---|
130 | {
|
---|
131 | EVP_rc4,
|
---|
132 | "rc4 309",
|
---|
133 | "\x29\x04\x19\x72\xfb\x42\xba\x5f\xc7\x12\x77\x12\xf1\x38\x29\xc9",
|
---|
134 | 16,
|
---|
135 | NULL,
|
---|
136 | 309,
|
---|
137 | "\x52\x75\x69\x73\x6c\x69\x6e\x6e"
|
---|
138 | "\x75\x6e\x20\x6c\x61\x75\x6c\x75"
|
---|
139 | "\x20\x6b\x6f\x72\x76\x69\x73\x73"
|
---|
140 | "\x73\x61\x6e\x69\x2c\x20\x74\xe4"
|
---|
141 | "\x68\x6b\xe4\x70\xe4\x69\x64\x65"
|
---|
142 | "\x6e\x20\x70\xe4\xe4\x6c\x6c\xe4"
|
---|
143 | "\x20\x74\xe4\x79\x73\x69\x6b\x75"
|
---|
144 | "\x75\x2e\x20\x4b\x65\x73\xe4\x79"
|
---|
145 | "\xf6\x6e\x20\x6f\x6e\x20\x6f\x6e"
|
---|
146 | "\x6e\x69\x20\x6f\x6d\x61\x6e\x61"
|
---|
147 | "\x6e\x69\x2c\x20\x6b\x61\x73\x6b"
|
---|
148 | "\x69\x73\x61\x76\x75\x75\x6e\x20"
|
---|
149 | "\x6c\x61\x61\x6b\x73\x6f\x74\x20"
|
---|
150 | "\x76\x65\x72\x68\x6f\x75\x75\x2e"
|
---|
151 | "\x20\x45\x6e\x20\x6d\x61\x20\x69"
|
---|
152 | "\x6c\x6f\x69\x74\x73\x65\x2c\x20"
|
---|
153 | "\x73\x75\x72\x65\x20\x68\x75\x6f"
|
---|
154 | "\x6b\x61\x61\x2c\x20\x6d\x75\x74"
|
---|
155 | "\x74\x61\x20\x6d\x65\x74\x73\xe4"
|
---|
156 | "\x6e\x20\x74\x75\x6d\x6d\x75\x75"
|
---|
157 | "\x73\x20\x6d\x75\x6c\x6c\x65\x20"
|
---|
158 | "\x74\x75\x6f\x6b\x61\x61\x2e\x20"
|
---|
159 | "\x50\x75\x75\x6e\x74\x6f\x20\x70"
|
---|
160 | "\x69\x6c\x76\x65\x6e\x2c\x20\x6d"
|
---|
161 | "\x69\x20\x68\x75\x6b\x6b\x75\x75"
|
---|
162 | "\x2c\x20\x73\x69\x69\x6e\x74\x6f"
|
---|
163 | "\x20\x76\x61\x72\x61\x6e\x20\x74"
|
---|
164 | "\x75\x75\x6c\x69\x73\x65\x6e\x2c"
|
---|
165 | "\x20\x6d\x69\x20\x6e\x75\x6b\x6b"
|
---|
166 | "\x75\x75\x2e\x20\x54\x75\x6f\x6b"
|
---|
167 | "\x73\x75\x74\x20\x76\x61\x6e\x61"
|
---|
168 | "\x6d\x6f\x6e\x20\x6a\x61\x20\x76"
|
---|
169 | "\x61\x72\x6a\x6f\x74\x20\x76\x65"
|
---|
170 | "\x65\x6e\x2c\x20\x6e\x69\x69\x73"
|
---|
171 | "\x74\xe4\x20\x73\x79\x64\xe4\x6d"
|
---|
172 | "\x65\x6e\x69\x20\x6c\x61\x75\x6c"
|
---|
173 | "\x75\x6e\x20\x74\x65\x65\x6e\x2e"
|
---|
174 | "\x20\x2d\x20\x45\x69\x6e\x6f\x20"
|
---|
175 | "\x4c\x65\x69\x6e\x6f",
|
---|
176 | "\x35\x81\x86\x99\x90\x01\xe6\xb5"
|
---|
177 | "\xda\xf0\x5e\xce\xeb\x7e\xee\x21"
|
---|
178 | "\xe0\x68\x9c\x1f\x00\xee\xa8\x1f"
|
---|
179 | "\x7d\xd2\xca\xae\xe1\xd2\x76\x3e"
|
---|
180 | "\x68\xaf\x0e\xad\x33\xd6\x6c\x26"
|
---|
181 | "\x8b\xc9\x46\xc4\x84\xfb\xe9\x4c"
|
---|
182 | "\x5f\x5e\x0b\x86\xa5\x92\x79\xe4"
|
---|
183 | "\xf8\x24\xe7\xa6\x40\xbd\x22\x32"
|
---|
184 | "\x10\xb0\xa6\x11\x60\xb7\xbc\xe9"
|
---|
185 | "\x86\xea\x65\x68\x80\x03\x59\x6b"
|
---|
186 | "\x63\x0a\x6b\x90\xf8\xe0\xca\xf6"
|
---|
187 | "\x91\x2a\x98\xeb\x87\x21\x76\xe8"
|
---|
188 | "\x3c\x20\x2c\xaa\x64\x16\x6d\x2c"
|
---|
189 | "\xce\x57\xff\x1b\xca\x57\xb2\x13"
|
---|
190 | "\xf0\xed\x1a\xa7\x2f\xb8\xea\x52"
|
---|
191 | "\xb0\xbe\x01\xcd\x1e\x41\x28\x67"
|
---|
192 | "\x72\x0b\x32\x6e\xb3\x89\xd0\x11"
|
---|
193 | "\xbd\x70\xd8\xaf\x03\x5f\xb0\xd8"
|
---|
194 | "\x58\x9d\xbc\xe3\xc6\x66\xf5\xea"
|
---|
195 | "\x8d\x4c\x79\x54\xc5\x0c\x3f\x34"
|
---|
196 | "\x0b\x04\x67\xf8\x1b\x42\x59\x61"
|
---|
197 | "\xc1\x18\x43\x07\x4d\xf6\x20\xf2"
|
---|
198 | "\x08\x40\x4b\x39\x4c\xf9\xd3\x7f"
|
---|
199 | "\xf5\x4b\x5f\x1a\xd8\xf6\xea\x7d"
|
---|
200 | "\xa3\xc5\x61\xdf\xa7\x28\x1f\x96"
|
---|
201 | "\x44\x63\xd2\xcc\x35\xa4\xd1\xb0"
|
---|
202 | "\x34\x90\xde\xc5\x1b\x07\x11\xfb"
|
---|
203 | "\xd6\xf5\x5f\x79\x23\x4d\x5b\x7c"
|
---|
204 | "\x76\x66\x22\xa6\x6d\xe9\x2b\xe9"
|
---|
205 | "\x96\x46\x1d\x5e\x4d\xc8\x78\xef"
|
---|
206 | "\x9b\xca\x03\x05\x21\xe8\x35\x1e"
|
---|
207 | "\x4b\xae\xd2\xfd\x04\xf9\x46\x73"
|
---|
208 | "\x68\xc4\xad\x6a\xc1\x86\xd0\x82"
|
---|
209 | "\x45\xb2\x63\xa2\x66\x6d\x1f\x6c"
|
---|
210 | "\x54\x20\xf1\x59\x9d\xfd\x9f\x43"
|
---|
211 | "\x89\x21\xc2\xf5\xa4\x63\x93\x8c"
|
---|
212 | "\xe0\x98\x22\x65\xee\xf7\x01\x79"
|
---|
213 | "\xbc\x55\x3f\x33\x9e\xb1\xa4\xc1"
|
---|
214 | "\xaf\x5f\x6a\x54\x7f"
|
---|
215 | }
|
---|
216 | };
|
---|
217 |
|
---|
218 | static int
|
---|
219 | test_cipher(struct tests *t)
|
---|
220 | {
|
---|
221 | const EVP_CIPHER *c = t->cipher();
|
---|
222 | EVP_CIPHER_CTX ectx;
|
---|
223 | EVP_CIPHER_CTX dctx;
|
---|
224 | void *d;
|
---|
225 |
|
---|
226 | EVP_CIPHER_CTX_init(&ectx);
|
---|
227 | EVP_CIPHER_CTX_init(&dctx);
|
---|
228 |
|
---|
229 | if (EVP_CipherInit_ex(&ectx, c, NULL, NULL, NULL, 1) != 1)
|
---|
230 | errx(1, "%s: EVP_CipherInit_ex einit", t->name);
|
---|
231 | if (EVP_CipherInit_ex(&dctx, c, NULL, NULL, NULL, 0) != 1)
|
---|
232 | errx(1, "%s: EVP_CipherInit_ex dinit", t->name);
|
---|
233 |
|
---|
234 | EVP_CIPHER_CTX_set_key_length(&ectx, t->keysize);
|
---|
235 | EVP_CIPHER_CTX_set_key_length(&dctx, t->keysize);
|
---|
236 |
|
---|
237 | if (EVP_CipherInit_ex(&ectx, NULL, NULL, t->key, t->iv, 1) != 1)
|
---|
238 | errx(1, "%s: EVP_CipherInit_ex encrypt", t->name);
|
---|
239 | if (EVP_CipherInit_ex(&dctx, NULL, NULL, t->key, t->iv, 0) != 1)
|
---|
240 | errx(1, "%s: EVP_CipherInit_ex decrypt", t->name);
|
---|
241 |
|
---|
242 | d = emalloc(t->datasize);
|
---|
243 |
|
---|
244 | if (!EVP_Cipher(&ectx, d, t->indata, t->datasize))
|
---|
245 | return 1;
|
---|
246 |
|
---|
247 | if (memcmp(d, t->outdata, t->datasize) != 0)
|
---|
248 | errx(1, "%s: encrypt not the same", t->name);
|
---|
249 |
|
---|
250 | if (!EVP_Cipher(&dctx, d, d, t->datasize))
|
---|
251 | return 1;
|
---|
252 |
|
---|
253 | if (memcmp(d, t->indata, t->datasize) != 0)
|
---|
254 | errx(1, "%s: decrypt not the same", t->name);
|
---|
255 |
|
---|
256 | if (t->outiv)
|
---|
257 | /* XXXX check */;
|
---|
258 |
|
---|
259 | EVP_CIPHER_CTX_cleanup(&ectx);
|
---|
260 | EVP_CIPHER_CTX_cleanup(&dctx);
|
---|
261 | free(d);
|
---|
262 |
|
---|
263 | return 0;
|
---|
264 | }
|
---|
265 |
|
---|
266 | static void
|
---|
267 | check_hmac(void)
|
---|
268 | {
|
---|
269 | unsigned char buf[4] = { 0, 0, 0, 0 };
|
---|
270 | char hmackey[] = "hello-world";
|
---|
271 | size_t hmackey_size = sizeof(hmackey);
|
---|
272 | unsigned int hmaclen;
|
---|
273 | unsigned char hmac[EVP_MAX_MD_SIZE];
|
---|
274 | HMAC_CTX c;
|
---|
275 |
|
---|
276 | char answer[20] = "\x2c\xfa\x32\xb7\x2b\x8a\xf6\xdf\xcf\xda"
|
---|
277 | "\x6f\xd1\x52\x4d\x54\x58\x73\x0f\xf3\x24";
|
---|
278 |
|
---|
279 | HMAC_CTX_init(&c);
|
---|
280 | HMAC_Init_ex(&c, hmackey, hmackey_size, EVP_sha1(), NULL);
|
---|
281 | HMAC_Update(&c, buf, sizeof(buf));
|
---|
282 | HMAC_Final(&c, hmac, &hmaclen);
|
---|
283 | HMAC_CTX_cleanup(&c);
|
---|
284 |
|
---|
285 | if (hmaclen != 20)
|
---|
286 | errx(1, "hmaclen = %d\n", (int)hmaclen);
|
---|
287 |
|
---|
288 | if (ct_memcmp(hmac, answer, hmaclen) != 0)
|
---|
289 | errx(1, "wrong answer\n");
|
---|
290 | }
|
---|
291 |
|
---|
292 | void
|
---|
293 | hcrypto_validate(void)
|
---|
294 | {
|
---|
295 | static int validated = 0;
|
---|
296 | unsigned int i;
|
---|
297 |
|
---|
298 | /* its ok to run this twice, do don't check for races */
|
---|
299 | if (validated)
|
---|
300 | return;
|
---|
301 | validated++;
|
---|
302 |
|
---|
303 | for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
|
---|
304 | test_cipher(&tests[i]);
|
---|
305 |
|
---|
306 | check_hmac();
|
---|
307 | }
|
---|