1/*
2 * Copyright (c) 2011-12 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 * Copyright (c) 2006 Kungliga Tekniska Högskolan
26 * (Royal Institute of Technology, Stockholm, Sweden).
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * 1. Redistributions of source code must retain the above copyright
34 *    notice, this list of conditions and the following disclaimer.
35 *
36 * 2. Redistributions in binary form must reproduce the above copyright
37 *    notice, this list of conditions and the following disclaimer in the
38 *    documentation and/or other materials provided with the distribution.
39 *
40 * 3. Neither the name of the Institute nor the names of its contributors
41 *    may be used to endorse or promote products derived from this software
42 *    without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57#include "cs-config.h"
58
59#include <sys/types.h>
60#include <limits.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64
65#include "rk-getarg.h"
66
67#include "rk-roken.h"
68
69#include "cs-evp.h"
70#include "cs-evp-hcrypto.h"
71#include "cs-evp-cc.h"
72
73#include "rk-hex.h"
74
75#include <err.h>
76
77struct tests {
78	const char *	name;
79	void *		key;
80	size_t		keysize;
81	void *		iv;
82	size_t		datasize;
83	void *		indata;
84	void *		outdata;
85	void *		outiv;
86};
87
88struct tests aes_tests[] =
89{
90	{ "aes-256",
91	  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
92	  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
93	  32,
94	  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
95	  16,
96	  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
97	  "\xdc\x95\xc0\x78\xa2\x40\x89\x89\xad\x48\xa2\x14\x92\x84\x20\x87" }
98};
99
100struct tests aes_cfb_tests[] =
101{
102	{ "aes-cfb8-128",
103	  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
104	  16,
105	  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
106	  16,
107	  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
108	  "\x66\xe9\x4b\xd4\xef\x8a\x2c\x3b\x88\x4c\xfa\x59\xca\x34\x2b\x2e" }
109};
110
111struct tests rc2_40_tests[] =
112{
113	{ "rc2-40",
114	  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
115	  16,
116	  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
117	  16,
118	  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
119	  "\xc0\xb8\xff\xa5\xd6\xeb\xc9\x62\xcc\x52\x5f\xfe\x9a\x3c\x97\xe6" }
120};
121
122struct tests des_ede3_tests[] =
123{
124	{ "des-ede3",
125	  "\x19\x17\xff\xe6\xbb\x77\x2e\xfc"
126	  "\x29\x76\x43\xbc\x63\x56\x7e\x9a"
127	  "\x00\x2e\x4d\x43\x1d\x5f\xfd\x58",
128	  24,
129	  "\xbf\x9a\x12\xb7\x26\x69\xfd\x05",
130	  16,
131	  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
132	  "\x55\x95\x97\x76\xa9\x6c\x66\x40\x64\xc7\xf4\x1c\x21\xb7\x14\x1b" }
133};
134
135struct tests camellia128_tests[] =
136{
137	{ "camellia128",
138	  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
139	  16,
140	  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
141	  16,
142	  "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
143	  "\x07\x92\x3A\x39\xEB\x0A\x81\x7D\x1C\x4D\x87\xBD\xB8\x2D\x1F\x1C",
144	  NULL }
145};
146
147struct tests rc4_tests[] =
148{
149	{
150		"rc4 8",
151		"\x01\x23\x45\x67\x89\xAB\xCD\xEF",
152		8,
153		NULL,
154		8,
155		"\x00\x00\x00\x00\x00\x00\x00\x00",
156		"\x74\x94\xC2\xE7\x10\x4B\x08\x79",
157		NULL
158	},
159	{
160		"rc4 5",
161		"\x61\x8a\x63\xd2\xfb",
162		5,
163		NULL,
164		5,
165		"\xdc\xee\x4c\xf9\x2c",
166		"\xf1\x38\x29\xc9\xde",
167		NULL
168	},
169	{
170		"rc4 309",
171		"\x29\x04\x19\x72\xfb\x42\xba\x5f\xc7\x12\x77\x12\xf1\x38\x29\xc9",
172		16,
173		NULL,
174		309,
175		"\x52\x75\x69\x73\x6c\x69\x6e\x6e"
176		"\x75\x6e\x20\x6c\x61\x75\x6c\x75"
177		"\x20\x6b\x6f\x72\x76\x69\x73\x73"
178		"\x73\x61\x6e\x69\x2c\x20\x74\xe4"
179		"\x68\x6b\xe4\x70\xe4\x69\x64\x65"
180		"\x6e\x20\x70\xe4\xe4\x6c\x6c\xe4"
181		"\x20\x74\xe4\x79\x73\x69\x6b\x75"
182		"\x75\x2e\x20\x4b\x65\x73\xe4\x79"
183		"\xf6\x6e\x20\x6f\x6e\x20\x6f\x6e"
184		"\x6e\x69\x20\x6f\x6d\x61\x6e\x61"
185		"\x6e\x69\x2c\x20\x6b\x61\x73\x6b"
186		"\x69\x73\x61\x76\x75\x75\x6e\x20"
187		"\x6c\x61\x61\x6b\x73\x6f\x74\x20"
188		"\x76\x65\x72\x68\x6f\x75\x75\x2e"
189		"\x20\x45\x6e\x20\x6d\x61\x20\x69"
190		"\x6c\x6f\x69\x74\x73\x65\x2c\x20"
191		"\x73\x75\x72\x65\x20\x68\x75\x6f"
192		"\x6b\x61\x61\x2c\x20\x6d\x75\x74"
193		"\x74\x61\x20\x6d\x65\x74\x73\xe4"
194		"\x6e\x20\x74\x75\x6d\x6d\x75\x75"
195		"\x73\x20\x6d\x75\x6c\x6c\x65\x20"
196		"\x74\x75\x6f\x6b\x61\x61\x2e\x20"
197		"\x50\x75\x75\x6e\x74\x6f\x20\x70"
198		"\x69\x6c\x76\x65\x6e\x2c\x20\x6d"
199		"\x69\x20\x68\x75\x6b\x6b\x75\x75"
200		"\x2c\x20\x73\x69\x69\x6e\x74\x6f"
201		"\x20\x76\x61\x72\x61\x6e\x20\x74"
202		"\x75\x75\x6c\x69\x73\x65\x6e\x2c"
203		"\x20\x6d\x69\x20\x6e\x75\x6b\x6b"
204		"\x75\x75\x2e\x20\x54\x75\x6f\x6b"
205		"\x73\x75\x74\x20\x76\x61\x6e\x61"
206		"\x6d\x6f\x6e\x20\x6a\x61\x20\x76"
207		"\x61\x72\x6a\x6f\x74\x20\x76\x65"
208		"\x65\x6e\x2c\x20\x6e\x69\x69\x73"
209		"\x74\xe4\x20\x73\x79\x64\xe4\x6d"
210		"\x65\x6e\x69\x20\x6c\x61\x75\x6c"
211		"\x75\x6e\x20\x74\x65\x65\x6e\x2e"
212		"\x20\x2d\x20\x45\x69\x6e\x6f\x20"
213		"\x4c\x65\x69\x6e\x6f",
214		"\x35\x81\x86\x99\x90\x01\xe6\xb5"
215		"\xda\xf0\x5e\xce\xeb\x7e\xee\x21"
216		"\xe0\x68\x9c\x1f\x00\xee\xa8\x1f"
217		"\x7d\xd2\xca\xae\xe1\xd2\x76\x3e"
218		"\x68\xaf\x0e\xad\x33\xd6\x6c\x26"
219		"\x8b\xc9\x46\xc4\x84\xfb\xe9\x4c"
220		"\x5f\x5e\x0b\x86\xa5\x92\x79\xe4"
221		"\xf8\x24\xe7\xa6\x40\xbd\x22\x32"
222		"\x10\xb0\xa6\x11\x60\xb7\xbc\xe9"
223		"\x86\xea\x65\x68\x80\x03\x59\x6b"
224		"\x63\x0a\x6b\x90\xf8\xe0\xca\xf6"
225		"\x91\x2a\x98\xeb\x87\x21\x76\xe8"
226		"\x3c\x20\x2c\xaa\x64\x16\x6d\x2c"
227		"\xce\x57\xff\x1b\xca\x57\xb2\x13"
228		"\xf0\xed\x1a\xa7\x2f\xb8\xea\x52"
229		"\xb0\xbe\x01\xcd\x1e\x41\x28\x67"
230		"\x72\x0b\x32\x6e\xb3\x89\xd0\x11"
231		"\xbd\x70\xd8\xaf\x03\x5f\xb0\xd8"
232		"\x58\x9d\xbc\xe3\xc6\x66\xf5\xea"
233		"\x8d\x4c\x79\x54\xc5\x0c\x3f\x34"
234		"\x0b\x04\x67\xf8\x1b\x42\x59\x61"
235		"\xc1\x18\x43\x07\x4d\xf6\x20\xf2"
236		"\x08\x40\x4b\x39\x4c\xf9\xd3\x7f"
237		"\xf5\x4b\x5f\x1a\xd8\xf6\xea\x7d"
238		"\xa3\xc5\x61\xdf\xa7\x28\x1f\x96"
239		"\x44\x63\xd2\xcc\x35\xa4\xd1\xb0"
240		"\x34\x90\xde\xc5\x1b\x07\x11\xfb"
241		"\xd6\xf5\x5f\x79\x23\x4d\x5b\x7c"
242		"\x76\x66\x22\xa6\x6d\xe9\x2b\xe9"
243		"\x96\x46\x1d\x5e\x4d\xc8\x78\xef"
244		"\x9b\xca\x03\x05\x21\xe8\x35\x1e"
245		"\x4b\xae\xd2\xfd\x04\xf9\x46\x73"
246		"\x68\xc4\xad\x6a\xc1\x86\xd0\x82"
247		"\x45\xb2\x63\xa2\x66\x6d\x1f\x6c"
248		"\x54\x20\xf1\x59\x9d\xfd\x9f\x43"
249		"\x89\x21\xc2\xf5\xa4\x63\x93\x8c"
250		"\xe0\x98\x22\x65\xee\xf7\x01\x79"
251		"\xbc\x55\x3f\x33\x9e\xb1\xa4\xc1"
252		"\xaf\x5f\x6a\x54\x7f"
253	}
254};
255
256
257static int
258test_cipher(int i, const EVP_CIPHER *c, struct tests *t)
259{
260	EVP_CIPHER_CTX ectx;
261	EVP_CIPHER_CTX dctx;
262	void *d;
263
264	if (c == NULL) {
265		printf("%s not supported\n", t->name);
266		return (0);
267	}
268
269	EVP_CIPHER_CTX_init(&ectx);
270	EVP_CIPHER_CTX_init(&dctx);
271
272	if (EVP_CipherInit_ex(&ectx, c, NULL, NULL, NULL, 1) != 1) {
273		errx(1, "%s: %d EVP_CipherInit_ex einit", t->name, i);
274	}
275	if (EVP_CipherInit_ex(&dctx, c, NULL, NULL, NULL, 0) != 1) {
276		errx(1, "%s: %d EVP_CipherInit_ex dinit", t->name, i);
277	}
278
279	EVP_CIPHER_CTX_set_key_length(&ectx, t->keysize);
280	EVP_CIPHER_CTX_set_key_length(&dctx, t->keysize);
281
282	if (EVP_CipherInit_ex(&ectx, NULL, NULL, t->key, t->iv, 1) != 1) {
283		errx(1, "%s: %d EVP_CipherInit_ex encrypt", t->name, i);
284	}
285	if (EVP_CipherInit_ex(&dctx, NULL, NULL, t->key, t->iv, 0) != 1) {
286		errx(1, "%s: %d EVP_CipherInit_ex decrypt", t->name, i);
287	}
288
289	d = emalloc(t->datasize);
290
291	if (!EVP_Cipher(&ectx, d, t->indata, t->datasize)) {
292		return (1);
293	}
294
295	if (memcmp(d, t->outdata, t->datasize) != 0) {
296		char *s, *s2;
297		hex_encode(d, t->datasize, &s);
298		hex_encode(t->outdata, t->datasize, &s2);
299		errx(1, "%s: %d encrypt not the same: %s != %s", t->name, i, s, s2);
300	}
301
302	if (!EVP_Cipher(&dctx, d, d, t->datasize)) {
303		return (1);
304	}
305
306	if (memcmp(d, t->indata, t->datasize) != 0) {
307		char *s;
308		hex_encode(d, t->datasize, &s);
309		errx(1, "%s: %d decrypt not the same: %s", t->name, i, s);
310	}
311	if (t->outiv) {
312		/* XXXX check  */
313	}
314
315	EVP_CIPHER_CTX_cleanup(&ectx);
316	EVP_CIPHER_CTX_cleanup(&dctx);
317	free(d);
318
319	printf("%s passed\n", t->name);
320	return (0);
321}
322
323
324#include "cs-aes.h"
325
326static int
327test_aes(int i, struct tests *t)
328{
329	AES_KEY ekey, dkey;
330	void *d;
331
332	if (AES_set_encrypt_key(t->key, (t->keysize * 8), &ekey) != 0) {
333		errx(1, "%s: %d AES_set_encrypt_key()", t->name, i);
334	}
335	if (AES_set_decrypt_key(t->key, (t->keysize * 8), &dkey) != 0) {
336		errx(1, "%s: %d AES_set_decrypt_key()", t->name, i);
337	}
338
339	d = emalloc(t->datasize);
340
341	AES_encrypt(t->indata, d, &ekey);
342	if (memcmp(d, t->outdata, t->datasize) != 0) {
343		char *s, *s2;
344		hex_encode(d, t->datasize, &s);
345		hex_encode(t->outdata, t->datasize, &s2);
346		errx(1, "%s: %d encrypt not the same: %s != %s", t->name, i, s, s2);
347	}
348
349	AES_decrypt(d, d, &dkey);
350	if (memcmp(d, t->indata, t->datasize) != 0) {
351		char *s;
352		hex_encode(d, t->datasize, &s);
353		errx(1, "%s: %d decrypt not the same: %s", t->name, i, s);
354	}
355
356	free(d);
357	printf("AES_%s passed\n", t->name);
358	return (0);
359}
360
361
362static int version_flag;
363static int help_flag;
364
365static struct getargs args[] =
366{
367	{ "version", 0, arg_flag, &version_flag,
368	  "print version", NULL },
369	{ "help",    0, arg_flag, &help_flag,
370	  NULL, NULL }
371};
372
373static void
374usage(int ret)
375{
376	arg_printusage(args,
377	    sizeof(args)/sizeof(*args),
378	    NULL,
379	    "");
380	exit(ret);
381}
382
383
384int
385main(int argc, char **argv)
386{
387	int ret = 0;
388	int i, idx = 0;
389
390	setprogname(argv[0]);
391
392	if (getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &idx)) {
393		usage(1);
394	}
395
396	if (help_flag) {
397		usage(0);
398	}
399
400	if (version_flag) {
401		print_version(NULL);
402		exit(0);
403	}
404
405	argc -= idx;
406	argv += idx;
407
408#ifndef __APPLE__
409	/* hcrypto */
410	for (i = 0; i < sizeof(aes_tests)/sizeof(aes_tests[0]); i++) {
411		ret += test_cipher(i, EVP_hcrypto_aes_256_cbc(), &aes_tests[i]);
412	}
413	for (i = 0; i < sizeof(aes_cfb_tests)/sizeof(aes_cfb_tests[0]); i++) {
414		ret += test_cipher(i, EVP_hcrypto_aes_128_cfb8(), &aes_cfb_tests[i]);
415	}
416
417	for (i = 0; i < sizeof(rc2_40_tests)/sizeof(rc2_40_tests[0]); i++) {
418		ret += test_cipher(i, EVP_hcrypto_rc2_40_cbc(), &rc2_40_tests[i]);
419	}
420	for (i = 0; i < sizeof(des_ede3_tests)/sizeof(des_ede3_tests[0]); i++) {
421		ret += test_cipher(i, EVP_hcrypto_des_ede3_cbc(), &des_ede3_tests[i]);
422	}
423	for (i = 0; i < sizeof(camellia128_tests)/sizeof(camellia128_tests[0]); i++) {
424		ret += test_cipher(i, EVP_hcrypto_camellia_128_cbc(),
425			&camellia128_tests[i]);
426	}
427	for (i = 0; i < sizeof(rc4_tests)/sizeof(rc4_tests[0]); i++) {
428		ret += test_cipher(i, EVP_hcrypto_rc4(), &rc4_tests[i]);
429	}
430
431#else
432	/* Common Crypto */
433	for (i = 0; i < sizeof(aes_tests)/sizeof(aes_tests[0]); i++) {
434		ret += test_cipher(i, EVP_cc_aes_256_ecb(), &aes_tests[i]);
435	}
436	for (i = 0; i < sizeof(aes_tests)/sizeof(aes_tests[0]); i++) {
437		ret += test_aes(i, &aes_tests[i]);
438	}
439
440	/*
441	 * for (i = 0; i < sizeof(aes_cfb_tests)/sizeof(aes_cfb_tests[0]); i++)
442	 *  ret += test_cipher(i, EVP_cc_aes_128_cfb8(), &aes_cfb_tests[i]);
443	 */
444	for (i = 0; i < sizeof(rc2_40_tests)/sizeof(rc2_40_tests[0]); i++) {
445		ret += test_cipher(i, EVP_cc_rc2_40_cbc(), &rc2_40_tests[i]);
446	}
447	for (i = 0; i < sizeof(des_ede3_tests)/sizeof(des_ede3_tests[0]); i++) {
448		ret += test_cipher(i, EVP_cc_des_ede3_cbc(), &des_ede3_tests[i]);
449	}
450	for (i = 0; i < sizeof(camellia128_tests)/sizeof(camellia128_tests[0]); i++) {
451		ret += test_cipher(i, EVP_cc_camellia_128_cbc(),
452			&camellia128_tests[i]);
453	}
454	for (i = 0; i < sizeof(rc4_tests)/sizeof(rc4_tests[0]); i++) {
455		ret += test_cipher(i, EVP_cc_rc4(), &rc4_tests[i]);
456	}
457#endif
458
459	return (ret);
460}
461