add cfb8-aes modes
[heimdal.git] / lib / hcrypto / test_cipher.c
blob341fcadf16a693c9759ff23fae86abda651997f0
1 /*
2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include <config.h>
36 #define HC_DEPRECATED_CRYPTO
38 #include <sys/types.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <getarg.h>
44 #include <roken.h>
46 #include <evp.h>
47 #include <evp-hcrypto.h>
48 #include <evp-cc.h>
49 #include <hex.h>
50 #include <err.h>
52 struct tests {
53 const char *name;
54 void *key;
55 size_t keysize;
56 void *iv;
57 size_t datasize;
58 void *indata;
59 void *outdata;
60 void *outiv;
63 struct tests aes_tests[] = {
64 { "aes-256",
65 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
66 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
67 32,
68 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
69 16,
70 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
71 "\xdc\x95\xc0\x78\xa2\x40\x89\x89\xad\x48\xa2\x14\x92\x84\x20\x87"
75 struct tests aes_cfb_tests[] = {
76 { "aes-cfb8-128",
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 16,
81 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
82 "\x66\xe9\x4b\xd4\xef\x8a\x2c\x3b\x88\x4c\xfa\x59\xca\x34\x2b\x2e"
86 struct tests rc2_40_tests[] = {
87 { "rc2-40",
88 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
89 16,
90 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
91 16,
92 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
93 "\xc0\xb8\xff\xa5\xd6\xeb\xc9\x62\xcc\x52\x5f\xfe\x9a\x3c\x97\xe6"
97 struct tests des_ede3_tests[] = {
98 { "des-ede3",
99 "\x19\x17\xff\xe6\xbb\x77\x2e\xfc"
100 "\x29\x76\x43\xbc\x63\x56\x7e\x9a"
101 "\x00\x2e\x4d\x43\x1d\x5f\xfd\x58",
103 "\xbf\x9a\x12\xb7\x26\x69\xfd\x05",
105 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
106 "\x55\x95\x97\x76\xa9\x6c\x66\x40\x64\xc7\xf4\x1c\x21\xb7\x14\x1b"
110 struct tests camellia128_tests[] = {
111 { "camellia128",
112 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
114 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
116 "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
117 "\x07\x92\x3A\x39\xEB\x0A\x81\x7D\x1C\x4D\x87\xBD\xB8\x2D\x1F\x1C",
118 NULL
122 struct tests rc4_tests[] = {
124 "rc4 8",
125 "\x01\x23\x45\x67\x89\xAB\xCD\xEF",
127 NULL,
129 "\x00\x00\x00\x00\x00\x00\x00\x00",
130 "\x74\x94\xC2\xE7\x10\x4B\x08\x79",
131 NULL
134 "rc4 5",
135 "\x61\x8a\x63\xd2\xfb",
137 NULL,
139 "\xdc\xee\x4c\xf9\x2c",
140 "\xf1\x38\x29\xc9\xde",
141 NULL
144 "rc4 309",
145 "\x29\x04\x19\x72\xfb\x42\xba\x5f\xc7\x12\x77\x12\xf1\x38\x29\xc9",
147 NULL,
148 309,
149 "\x52\x75\x69\x73\x6c\x69\x6e\x6e"
150 "\x75\x6e\x20\x6c\x61\x75\x6c\x75"
151 "\x20\x6b\x6f\x72\x76\x69\x73\x73"
152 "\x73\x61\x6e\x69\x2c\x20\x74\xe4"
153 "\x68\x6b\xe4\x70\xe4\x69\x64\x65"
154 "\x6e\x20\x70\xe4\xe4\x6c\x6c\xe4"
155 "\x20\x74\xe4\x79\x73\x69\x6b\x75"
156 "\x75\x2e\x20\x4b\x65\x73\xe4\x79"
157 "\xf6\x6e\x20\x6f\x6e\x20\x6f\x6e"
158 "\x6e\x69\x20\x6f\x6d\x61\x6e\x61"
159 "\x6e\x69\x2c\x20\x6b\x61\x73\x6b"
160 "\x69\x73\x61\x76\x75\x75\x6e\x20"
161 "\x6c\x61\x61\x6b\x73\x6f\x74\x20"
162 "\x76\x65\x72\x68\x6f\x75\x75\x2e"
163 "\x20\x45\x6e\x20\x6d\x61\x20\x69"
164 "\x6c\x6f\x69\x74\x73\x65\x2c\x20"
165 "\x73\x75\x72\x65\x20\x68\x75\x6f"
166 "\x6b\x61\x61\x2c\x20\x6d\x75\x74"
167 "\x74\x61\x20\x6d\x65\x74\x73\xe4"
168 "\x6e\x20\x74\x75\x6d\x6d\x75\x75"
169 "\x73\x20\x6d\x75\x6c\x6c\x65\x20"
170 "\x74\x75\x6f\x6b\x61\x61\x2e\x20"
171 "\x50\x75\x75\x6e\x74\x6f\x20\x70"
172 "\x69\x6c\x76\x65\x6e\x2c\x20\x6d"
173 "\x69\x20\x68\x75\x6b\x6b\x75\x75"
174 "\x2c\x20\x73\x69\x69\x6e\x74\x6f"
175 "\x20\x76\x61\x72\x61\x6e\x20\x74"
176 "\x75\x75\x6c\x69\x73\x65\x6e\x2c"
177 "\x20\x6d\x69\x20\x6e\x75\x6b\x6b"
178 "\x75\x75\x2e\x20\x54\x75\x6f\x6b"
179 "\x73\x75\x74\x20\x76\x61\x6e\x61"
180 "\x6d\x6f\x6e\x20\x6a\x61\x20\x76"
181 "\x61\x72\x6a\x6f\x74\x20\x76\x65"
182 "\x65\x6e\x2c\x20\x6e\x69\x69\x73"
183 "\x74\xe4\x20\x73\x79\x64\xe4\x6d"
184 "\x65\x6e\x69\x20\x6c\x61\x75\x6c"
185 "\x75\x6e\x20\x74\x65\x65\x6e\x2e"
186 "\x20\x2d\x20\x45\x69\x6e\x6f\x20"
187 "\x4c\x65\x69\x6e\x6f",
188 "\x35\x81\x86\x99\x90\x01\xe6\xb5"
189 "\xda\xf0\x5e\xce\xeb\x7e\xee\x21"
190 "\xe0\x68\x9c\x1f\x00\xee\xa8\x1f"
191 "\x7d\xd2\xca\xae\xe1\xd2\x76\x3e"
192 "\x68\xaf\x0e\xad\x33\xd6\x6c\x26"
193 "\x8b\xc9\x46\xc4\x84\xfb\xe9\x4c"
194 "\x5f\x5e\x0b\x86\xa5\x92\x79\xe4"
195 "\xf8\x24\xe7\xa6\x40\xbd\x22\x32"
196 "\x10\xb0\xa6\x11\x60\xb7\xbc\xe9"
197 "\x86\xea\x65\x68\x80\x03\x59\x6b"
198 "\x63\x0a\x6b\x90\xf8\xe0\xca\xf6"
199 "\x91\x2a\x98\xeb\x87\x21\x76\xe8"
200 "\x3c\x20\x2c\xaa\x64\x16\x6d\x2c"
201 "\xce\x57\xff\x1b\xca\x57\xb2\x13"
202 "\xf0\xed\x1a\xa7\x2f\xb8\xea\x52"
203 "\xb0\xbe\x01\xcd\x1e\x41\x28\x67"
204 "\x72\x0b\x32\x6e\xb3\x89\xd0\x11"
205 "\xbd\x70\xd8\xaf\x03\x5f\xb0\xd8"
206 "\x58\x9d\xbc\xe3\xc6\x66\xf5\xea"
207 "\x8d\x4c\x79\x54\xc5\x0c\x3f\x34"
208 "\x0b\x04\x67\xf8\x1b\x42\x59\x61"
209 "\xc1\x18\x43\x07\x4d\xf6\x20\xf2"
210 "\x08\x40\x4b\x39\x4c\xf9\xd3\x7f"
211 "\xf5\x4b\x5f\x1a\xd8\xf6\xea\x7d"
212 "\xa3\xc5\x61\xdf\xa7\x28\x1f\x96"
213 "\x44\x63\xd2\xcc\x35\xa4\xd1\xb0"
214 "\x34\x90\xde\xc5\x1b\x07\x11\xfb"
215 "\xd6\xf5\x5f\x79\x23\x4d\x5b\x7c"
216 "\x76\x66\x22\xa6\x6d\xe9\x2b\xe9"
217 "\x96\x46\x1d\x5e\x4d\xc8\x78\xef"
218 "\x9b\xca\x03\x05\x21\xe8\x35\x1e"
219 "\x4b\xae\xd2\xfd\x04\xf9\x46\x73"
220 "\x68\xc4\xad\x6a\xc1\x86\xd0\x82"
221 "\x45\xb2\x63\xa2\x66\x6d\x1f\x6c"
222 "\x54\x20\xf1\x59\x9d\xfd\x9f\x43"
223 "\x89\x21\xc2\xf5\xa4\x63\x93\x8c"
224 "\xe0\x98\x22\x65\xee\xf7\x01\x79"
225 "\xbc\x55\x3f\x33\x9e\xb1\xa4\xc1"
226 "\xaf\x5f\x6a\x54\x7f"
231 static int
232 test_cipher(int i, const EVP_CIPHER *c, struct tests *t)
234 EVP_CIPHER_CTX ectx;
235 EVP_CIPHER_CTX dctx;
236 void *d;
238 if (c == NULL) {
239 printf("%s not supported\n", t->name);
240 return 0;
243 EVP_CIPHER_CTX_init(&ectx);
244 EVP_CIPHER_CTX_init(&dctx);
246 if (EVP_CipherInit_ex(&ectx, c, NULL, NULL, NULL, 1) != 1)
247 errx(1, "%s: %d EVP_CipherInit_ex einit", t->name, i);
248 if (EVP_CipherInit_ex(&dctx, c, NULL, NULL, NULL, 0) != 1)
249 errx(1, "%s: %d EVP_CipherInit_ex dinit", t->name, i);
251 EVP_CIPHER_CTX_set_key_length(&ectx, t->keysize);
252 EVP_CIPHER_CTX_set_key_length(&dctx, t->keysize);
254 if (EVP_CipherInit_ex(&ectx, NULL, NULL, t->key, t->iv, 1) != 1)
255 errx(1, "%s: %d EVP_CipherInit_ex encrypt", t->name, i);
256 if (EVP_CipherInit_ex(&dctx, NULL, NULL, t->key, t->iv, 0) != 1)
257 errx(1, "%s: %d EVP_CipherInit_ex decrypt", t->name, i);
259 d = emalloc(t->datasize);
261 if (!EVP_Cipher(&ectx, d, t->indata, t->datasize))
262 return 1;
264 if (memcmp(d, t->outdata, t->datasize) != 0) {
265 char *s, *s2;
266 hex_encode(d, t->datasize, &s);
267 hex_encode(t->outdata, t->datasize, &s2);
268 errx(1, "%s: %d encrypt not the same: %s != %s", t->name, i, s, s2);
271 if (!EVP_Cipher(&dctx, d, d, t->datasize))
272 return 1;
274 if (memcmp(d, t->indata, t->datasize) != 0) {
275 char *s;
276 hex_encode(d, t->datasize, &s);
277 errx(1, "%s: %d decrypt not the same: %s", t->name, i, s);
279 if (t->outiv)
280 /* XXXX check */;
282 EVP_CIPHER_CTX_cleanup(&ectx);
283 EVP_CIPHER_CTX_cleanup(&dctx);
284 free(d);
286 return 0;
289 static int version_flag;
290 static int help_flag;
292 static struct getargs args[] = {
293 { "version", 0, arg_flag, &version_flag,
294 "print version", NULL },
295 { "help", 0, arg_flag, &help_flag,
296 NULL, NULL }
299 static void
300 usage (int ret)
302 arg_printusage (args,
303 sizeof(args)/sizeof(*args),
304 NULL,
305 "");
306 exit (ret);
310 main(int argc, char **argv)
312 int ret = 0;
313 int i, idx = 0;
315 setprogname(argv[0]);
317 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &idx))
318 usage(1);
320 if (help_flag)
321 usage(0);
323 if(version_flag){
324 print_version(NULL);
325 exit(0);
328 argc -= idx;
329 argv += idx;
331 /* hcrypto */
332 for (i = 0; i < sizeof(aes_tests)/sizeof(aes_tests[0]); i++)
333 ret += test_cipher(i, EVP_hcrypto_aes_256_cbc(), &aes_tests[i]);
334 for (i = 0; i < sizeof(aes_cfb_tests)/sizeof(aes_cfb_tests[0]); i++)
335 ret += test_cipher(i, EVP_hcrypto_aes_128_cfb8(), &aes_cfb_tests[i]);
337 for (i = 0; i < sizeof(rc2_40_tests)/sizeof(rc2_40_tests[0]); i++)
338 ret += test_cipher(i, EVP_hcrypto_rc2_40_cbc(), &rc2_40_tests[i]);
339 for (i = 0; i < sizeof(des_ede3_tests)/sizeof(des_ede3_tests[0]); i++)
340 ret += test_cipher(i, EVP_hcrypto_des_ede3_cbc(), &des_ede3_tests[i]);
341 for (i = 0; i < sizeof(camellia128_tests)/sizeof(camellia128_tests[0]); i++)
342 ret += test_cipher(i, EVP_hcrypto_camellia_128_cbc(),
343 &camellia128_tests[i]);
344 for (i = 0; i < sizeof(rc4_tests)/sizeof(rc4_tests[0]); i++)
345 ret += test_cipher(i, EVP_hcrypto_rc4(), &rc4_tests[i]);
347 /* Common Crypto */
348 #ifdef __APPLE__
349 for (i = 0; i < sizeof(aes_tests)/sizeof(aes_tests[0]); i++)
350 ret += test_cipher(i, EVP_cc_aes_256_cbc(), &aes_tests[i]);
351 #if 0
352 for (i = 0; i < sizeof(aes_cfb_tests)/sizeof(aes_cfb_tests[0]); i++)
353 ret += test_cipher(i, EVP_cc_aes_128_cfb8(), &aes_cfb_tests[i]);
354 #endif
355 for (i = 0; i < sizeof(rc2_40_tests)/sizeof(rc2_40_tests[0]); i++)
356 ret += test_cipher(i, EVP_cc_rc2_40_cbc(), &rc2_40_tests[i]);
357 for (i = 0; i < sizeof(des_ede3_tests)/sizeof(des_ede3_tests[0]); i++)
358 ret += test_cipher(i, EVP_cc_des_ede3_cbc(), &des_ede3_tests[i]);
359 for (i = 0; i < sizeof(camellia128_tests)/sizeof(camellia128_tests[0]); i++)
360 ret += test_cipher(i, EVP_cc_camellia_128_cbc(),
361 &camellia128_tests[i]);
362 for (i = 0; i < sizeof(rc4_tests)/sizeof(rc4_tests[0]); i++)
363 ret += test_cipher(i, EVP_cc_rc4(), &rc4_tests[i]);
364 #endif
366 return ret;