xref: /openbsd/regress/lib/libcrypto/rc4/rc4_test.c (revision e04c7e8e)
1*e04c7e8eStb /*	$OpenBSD: rc4_test.c,v 1.2 2022/09/05 21:34:23 tb Exp $ */
29db53cc8Stb /*
39db53cc8Stb  * Copyright (c) 2022 Joshua Sing <joshua@hypera.dev>
49db53cc8Stb  *
59db53cc8Stb  * Permission to use, copy, modify, and distribute this software for any
69db53cc8Stb  * purpose with or without fee is hereby granted, provided that the above
79db53cc8Stb  * copyright notice and this permission notice appear in all copies.
89db53cc8Stb  *
99db53cc8Stb  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
109db53cc8Stb  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119db53cc8Stb  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
129db53cc8Stb  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
139db53cc8Stb  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149db53cc8Stb  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
159db53cc8Stb  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
169db53cc8Stb  */
179db53cc8Stb 
189db53cc8Stb #include <openssl/evp.h>
199db53cc8Stb #include <openssl/rc4.h>
209db53cc8Stb 
219db53cc8Stb #include <stdint.h>
229db53cc8Stb #include <string.h>
239db53cc8Stb 
249db53cc8Stb struct rc4_test {
259db53cc8Stb 	const uint8_t key[32];
269db53cc8Stb 	const int key_len;
279db53cc8Stb 	const int len;
289db53cc8Stb 	const uint8_t in[512];
299db53cc8Stb 	const uint8_t out[512];
309db53cc8Stb };
319db53cc8Stb 
329db53cc8Stb static const struct rc4_test rc4_tests[] = {
339db53cc8Stb 	/*
349db53cc8Stb 	 * Test vectors from RFC 6229, with 40 and 128-bit keys.
359db53cc8Stb 	 * Note that this only uses the first 32 bytes of each test vector due
369db53cc8Stb 	 * to stream offsets.
379db53cc8Stb 	 */
389db53cc8Stb 	{
399db53cc8Stb 		.key = {
409db53cc8Stb 			0x01, 0x02, 0x03, 0x04, 0x05,
419db53cc8Stb 		},
429db53cc8Stb 		.key_len = 5,
439db53cc8Stb 		.len = 32,
449db53cc8Stb 		.in = {
459db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
469db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
479db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
489db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
499db53cc8Stb 		},
509db53cc8Stb 		.out = {
519db53cc8Stb 			0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27,
529db53cc8Stb 			0xcc, 0xc3, 0x52, 0x4a, 0x0a, 0x11, 0x18, 0xa8,
539db53cc8Stb 			0x69, 0x82, 0x94, 0x4f, 0x18, 0xfc, 0x82, 0xd5,
549db53cc8Stb 			0x89, 0xc4, 0x03, 0xa4, 0x7a, 0x0d, 0x09, 0x19,
559db53cc8Stb 		},
569db53cc8Stb 	},
579db53cc8Stb 	{
589db53cc8Stb 		.key = {
599db53cc8Stb 			0x83, 0x32, 0x22, 0x77, 0x2a,
609db53cc8Stb 		},
619db53cc8Stb 		.key_len = 5,
629db53cc8Stb 		.len = 32,
639db53cc8Stb 		.in = {
649db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
659db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
669db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
679db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
689db53cc8Stb 		},
699db53cc8Stb 		.out = {
709db53cc8Stb 			0x80, 0xad, 0x97, 0xbd, 0xc9, 0x73, 0xdf, 0x8a,
719db53cc8Stb 			0x2e, 0x87, 0x9e, 0x92, 0xa4, 0x97, 0xef, 0xda,
729db53cc8Stb 			0x20, 0xf0, 0x60, 0xc2, 0xf2, 0xe5, 0x12, 0x65,
739db53cc8Stb 			0x01, 0xd3, 0xd4, 0xfe, 0xa1, 0x0d, 0x5f, 0xc0,
749db53cc8Stb 		},
759db53cc8Stb 	},
769db53cc8Stb 	{
779db53cc8Stb 		.key = {
789db53cc8Stb 			0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
799db53cc8Stb 			0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
809db53cc8Stb 		},
819db53cc8Stb 		.key_len = 16,
829db53cc8Stb 		.len = 32,
839db53cc8Stb 		.in = {
849db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
859db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
869db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
879db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
889db53cc8Stb 		},
899db53cc8Stb 		.out = {
909db53cc8Stb 			0x9a, 0xc7, 0xcc, 0x9a, 0x60, 0x9d, 0x1e, 0xf7,
919db53cc8Stb 			0xb2, 0x93, 0x28, 0x99, 0xcd, 0xe4, 0x1b, 0x97,
929db53cc8Stb 			0x52, 0x48, 0xc4, 0x95, 0x90, 0x14, 0x12, 0x6a,
939db53cc8Stb 			0x6e, 0x8a, 0x84, 0xf1, 0x1d, 0x1a, 0x9e, 0x1c,
949db53cc8Stb 		},
959db53cc8Stb 	},
969db53cc8Stb 	{
979db53cc8Stb 		.key = {
989db53cc8Stb 			0xeb, 0xb4, 0x62, 0x27, 0xc6, 0xcc, 0x8b, 0x37,
999db53cc8Stb 			0x64, 0x19, 0x10, 0x83, 0x32, 0x22, 0x77, 0x2a,
1009db53cc8Stb 		},
1019db53cc8Stb 		.key_len = 16,
1029db53cc8Stb 		.len = 32,
1039db53cc8Stb 		.in = {
1049db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1059db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1069db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1079db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1089db53cc8Stb 		},
1099db53cc8Stb 		.out = {
1109db53cc8Stb 			0x72, 0x0c, 0x94, 0xb6, 0x3e, 0xdf, 0x44, 0xe1,
1119db53cc8Stb 			0x31, 0xd9, 0x50, 0xca, 0x21, 0x1a, 0x5a, 0x30,
1129db53cc8Stb 			0xc3, 0x66, 0xfd, 0xea, 0xcf, 0x9c, 0xa8, 0x04,
1139db53cc8Stb 			0x36, 0xbe, 0x7c, 0x35, 0x84, 0x24, 0xd2, 0x0b,
1149db53cc8Stb 		},
1159db53cc8Stb 	},
1169db53cc8Stb 
1179db53cc8Stb 	/*
1189db53cc8Stb 	 * Test vectors from the original cypherpunk posting of ARC4:
1199db53cc8Stb 	 * https://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0?pli=1
1209db53cc8Stb 	 */
1219db53cc8Stb 	{
1229db53cc8Stb 		.key = {
1239db53cc8Stb 			0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
1249db53cc8Stb 		},
1259db53cc8Stb 		.key_len = 8,
1269db53cc8Stb 		.len = 8,
1279db53cc8Stb 		.in = {
1289db53cc8Stb 			0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
1299db53cc8Stb 		},
1309db53cc8Stb 		.out = {
1319db53cc8Stb 			0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96,
1329db53cc8Stb 		},
1339db53cc8Stb 	},
1349db53cc8Stb 	{
1359db53cc8Stb 		.key = {
1369db53cc8Stb 			0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
1379db53cc8Stb 		},
1389db53cc8Stb 		.key_len = 8,
1399db53cc8Stb 		.len = 8,
1409db53cc8Stb 		.in = {
1419db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1429db53cc8Stb 		},
1439db53cc8Stb 		.out = {
1449db53cc8Stb 			0x74, 0x94, 0xc2, 0xe7, 0x10, 0x4b, 0x08, 0x79,
1459db53cc8Stb 		},
1469db53cc8Stb 	},
1479db53cc8Stb 	{
1489db53cc8Stb 		.key = {
1499db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1509db53cc8Stb 		},
1519db53cc8Stb 		.key_len = 8,
1529db53cc8Stb 		.len = 8,
1539db53cc8Stb 		.in = {
1549db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1559db53cc8Stb 		},
1569db53cc8Stb 		.out = {
1579db53cc8Stb 			0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a,
1589db53cc8Stb 		},
1599db53cc8Stb 	},
1609db53cc8Stb 	{
1619db53cc8Stb 		.key = {
1629db53cc8Stb 			0xef, 0x01, 0x23, 0x45,
1639db53cc8Stb 		},
1649db53cc8Stb 		.key_len = 4,
1659db53cc8Stb 		.len = 10,
1669db53cc8Stb 		.in = {
1679db53cc8Stb 			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1689db53cc8Stb 			0x00, 0x00,
1699db53cc8Stb 		},
1709db53cc8Stb 		.out = {
1719db53cc8Stb 			0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf,
1729db53cc8Stb 			0xbd, 0x61,
1739db53cc8Stb 		},
1749db53cc8Stb 	},
1759db53cc8Stb 	{
1769db53cc8Stb 		.key = {
1779db53cc8Stb 			0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
1789db53cc8Stb 		},
1799db53cc8Stb 		.key_len = 8,
1809db53cc8Stb 		.len = 10,
1819db53cc8Stb 		.in = {
1829db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1839db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1849db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1859db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1869db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1879db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1889db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1899db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1909db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1919db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1929db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1939db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1949db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1959db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1969db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1979db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1989db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1999db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2009db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2019db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2029db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2039db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2049db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2059db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2069db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2079db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2089db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2099db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2109db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2119db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2129db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2139db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2149db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2159db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2169db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2179db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2189db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2199db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2209db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2219db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2229db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2239db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2249db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2259db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2269db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2279db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2289db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2299db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2309db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2319db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2329db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2339db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2349db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2359db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2369db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2379db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2389db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2399db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2409db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2419db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2429db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2439db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2449db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2459db53cc8Stb 			0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2469db53cc8Stb 		},
2479db53cc8Stb 		.out = {
2489db53cc8Stb 			0x75, 0x95, 0xc3, 0xe6, 0x11, 0x4a, 0x09, 0x78,
2499db53cc8Stb 			0x0c, 0x4a, 0xd4, 0x52, 0x33, 0x8e, 0x1f, 0xfd,
2509db53cc8Stb 			0x9a, 0x1b, 0xe9, 0x49, 0x8f, 0x81, 0x3d, 0x76,
2519db53cc8Stb 			0x53, 0x34, 0x49, 0xb6, 0x77, 0x8d, 0xca, 0xd8,
2529db53cc8Stb 			0xc7, 0x8a, 0x8d, 0x2b, 0xa9, 0xac, 0x66, 0x08,
2539db53cc8Stb 			0x5d, 0x0e, 0x53, 0xd5, 0x9c, 0x26, 0xc2, 0xd1,
2549db53cc8Stb 			0xc4, 0x90, 0xc1, 0xeb, 0xbe, 0x0c, 0xe6, 0x6d,
2559db53cc8Stb 			0x1b, 0x6b, 0x1b, 0x13, 0xb6, 0xb9, 0x19, 0xb8,
2569db53cc8Stb 			0x47, 0xc2, 0x5a, 0x91, 0x44, 0x7a, 0x95, 0xe7,
2579db53cc8Stb 			0x5e, 0x4e, 0xf1, 0x67, 0x79, 0xcd, 0xe8, 0xbf,
2589db53cc8Stb 			0x0a, 0x95, 0x85, 0x0e, 0x32, 0xaf, 0x96, 0x89,
2599db53cc8Stb 			0x44, 0x4f, 0xd3, 0x77, 0x10, 0x8f, 0x98, 0xfd,
2609db53cc8Stb 			0xcb, 0xd4, 0xe7, 0x26, 0x56, 0x75, 0x00, 0x99,
2619db53cc8Stb 			0x0b, 0xcc, 0x7e, 0x0c, 0xa3, 0xc4, 0xaa, 0xa3,
2629db53cc8Stb 			0x04, 0xa3, 0x87, 0xd2, 0x0f, 0x3b, 0x8f, 0xbb,
2639db53cc8Stb 			0xcd, 0x42, 0xa1, 0xbd, 0x31, 0x1d, 0x7a, 0x43,
2649db53cc8Stb 			0x03, 0xdd, 0xa5, 0xab, 0x07, 0x88, 0x96, 0xae,
2659db53cc8Stb 			0x80, 0xc1, 0x8b, 0x0a, 0xf6, 0x6d, 0xff, 0x31,
2669db53cc8Stb 			0x96, 0x16, 0xeb, 0x78, 0x4e, 0x49, 0x5a, 0xd2,
2679db53cc8Stb 			0xce, 0x90, 0xd7, 0xf7, 0x72, 0xa8, 0x17, 0x47,
2689db53cc8Stb 			0xb6, 0x5f, 0x62, 0x09, 0x3b, 0x1e, 0x0d, 0xb9,
2699db53cc8Stb 			0xe5, 0xba, 0x53, 0x2f, 0xaf, 0xec, 0x47, 0x50,
2709db53cc8Stb 			0x83, 0x23, 0xe6, 0x71, 0x32, 0x7d, 0xf9, 0x44,
2719db53cc8Stb 			0x44, 0x32, 0xcb, 0x73, 0x67, 0xce, 0xc8, 0x2f,
2729db53cc8Stb 			0x5d, 0x44, 0xc0, 0xd0, 0x0b, 0x67, 0xd6, 0x50,
2739db53cc8Stb 			0xa0, 0x75, 0xcd, 0x4b, 0x70, 0xde, 0xdd, 0x77,
2749db53cc8Stb 			0xeb, 0x9b, 0x10, 0x23, 0x1b, 0x6b, 0x5b, 0x74,
2759db53cc8Stb 			0x13, 0x47, 0x39, 0x6d, 0x62, 0x89, 0x74, 0x21,
2769db53cc8Stb 			0xd4, 0x3d, 0xf9, 0xb4, 0x2e, 0x44, 0x6e, 0x35,
2779db53cc8Stb 			0x8e, 0x9c, 0x11, 0xa9, 0xb2, 0x18, 0x4e, 0xcb,
2789db53cc8Stb 			0xef, 0x0c, 0xd8, 0xe7, 0xa8, 0x77, 0xef, 0x96,
2799db53cc8Stb 			0x8f, 0x13, 0x90, 0xec, 0x9b, 0x3d, 0x35, 0xa5,
2809db53cc8Stb 			0x58, 0x5c, 0xb0, 0x09, 0x29, 0x0e, 0x2f, 0xcd,
2819db53cc8Stb 			0xe7, 0xb5, 0xec, 0x66, 0xd9, 0x08, 0x4b, 0xe4,
2829db53cc8Stb 			0x40, 0x55, 0xa6, 0x19, 0xd9, 0xdd, 0x7f, 0xc3,
2839db53cc8Stb 			0x16, 0x6f, 0x94, 0x87, 0xf7, 0xcb, 0x27, 0x29,
2849db53cc8Stb 			0x12, 0x42, 0x64, 0x45, 0x99, 0x85, 0x14, 0xc1,
2859db53cc8Stb 			0x5d, 0x53, 0xa1, 0x8c, 0x86, 0x4c, 0xe3, 0xa2,
2869db53cc8Stb 			0xb7, 0x55, 0x57, 0x93, 0x98, 0x81, 0x26, 0x52,
2879db53cc8Stb 			0x0e, 0xac, 0xf2, 0xe3, 0x06, 0x6e, 0x23, 0x0c,
2889db53cc8Stb 			0x91, 0xbe, 0xe4, 0xdd, 0x53, 0x04, 0xf5, 0xfd,
2899db53cc8Stb 			0x04, 0x05, 0xb3, 0x5b, 0xd9, 0x9c, 0x73, 0x13,
2909db53cc8Stb 			0x5d, 0x3d, 0x9b, 0xc3, 0x35, 0xee, 0x04, 0x9e,
2919db53cc8Stb 			0xf6, 0x9b, 0x38, 0x67, 0xbf, 0x2d, 0x7b, 0xd1,
2929db53cc8Stb 			0xea, 0xa5, 0x95, 0xd8, 0xbf, 0xc0, 0x06, 0x6f,
2939db53cc8Stb 			0xf8, 0xd3, 0x15, 0x09, 0xeb, 0x0c, 0x6c, 0xaa,
2949db53cc8Stb 			0x00, 0x6c, 0x80, 0x7a, 0x62, 0x3e, 0xf8, 0x4c,
2959db53cc8Stb 			0x3d, 0x33, 0xc1, 0x95, 0xd2, 0x3e, 0xe3, 0x20,
2969db53cc8Stb 			0xc4, 0x0d, 0xe0, 0x55, 0x81, 0x57, 0xc8, 0x22,
2979db53cc8Stb 			0xd4, 0xb8, 0xc5, 0x69, 0xd8, 0x49, 0xae, 0xd5,
2989db53cc8Stb 			0x9d, 0x4e, 0x0f, 0xd7, 0xf3, 0x79, 0x58, 0x6b,
2999db53cc8Stb 			0x4b, 0x7f, 0xf6, 0x84, 0xed, 0x6a, 0x18, 0x9f,
3009db53cc8Stb 			0x74, 0x86, 0xd4, 0x9b, 0x9c, 0x4b, 0xad, 0x9b,
3019db53cc8Stb 			0xa2, 0x4b, 0x96, 0xab, 0xf9, 0x24, 0x37, 0x2c,
3029db53cc8Stb 			0x8a, 0x8f, 0xff, 0xb1, 0x0d, 0x55, 0x35, 0x49,
3039db53cc8Stb 			0x00, 0xa7, 0x7a, 0x3d, 0xb5, 0xf2, 0x05, 0xe1,
3049db53cc8Stb 			0xb9, 0x9f, 0xcd, 0x86, 0x60, 0x86, 0x3a, 0x15,
3059db53cc8Stb 			0x9a, 0xd4, 0xab, 0xe4, 0x0f, 0xa4, 0x89, 0x34,
3069db53cc8Stb 			0x16, 0x3d, 0xdd, 0xe5, 0x42, 0xa6, 0x58, 0x55,
3079db53cc8Stb 			0x40, 0xfd, 0x68, 0x3c, 0xbf, 0xd8, 0xc0, 0x0f,
3089db53cc8Stb 			0x12, 0x12, 0x9a, 0x28, 0x4d, 0xea, 0xcc, 0x4c,
3099db53cc8Stb 			0xde, 0xfe, 0x58, 0xbe, 0x71, 0x37, 0x54, 0x1c,
3109db53cc8Stb 			0x04, 0x71, 0x26, 0xc8, 0xd4, 0x9e, 0x27, 0x55,
3119db53cc8Stb 			0xab, 0x18, 0x1a, 0xb7, 0xe9, 0x40, 0xb0, 0xc0,
3129db53cc8Stb 		},
3139db53cc8Stb 	},
3149db53cc8Stb };
3159db53cc8Stb 
3169db53cc8Stb #define N_RC4_TESTS (sizeof(rc4_tests) / sizeof(rc4_tests[0]))
3179db53cc8Stb 
3189db53cc8Stb static int
3199db53cc8Stb rc4_test(void)
3209db53cc8Stb {
3219db53cc8Stb 	const struct rc4_test *rt;
3229db53cc8Stb 	RC4_KEY key;
3239db53cc8Stb 	EVP_CIPHER_CTX *ctx = NULL;
3249db53cc8Stb 	const EVP_CIPHER *cipher;
3259db53cc8Stb 	uint8_t out[512];
3269db53cc8Stb 	int in_len, out_len;
3279db53cc8Stb 	size_t i;
3289db53cc8Stb 	int j;
3299db53cc8Stb 	int failed = 1;
3309db53cc8Stb 
3319db53cc8Stb 	if ((ctx = EVP_CIPHER_CTX_new()) == NULL) {
3329db53cc8Stb 		fprintf(stderr, "FAIL: EVP_CIPHER_CTX_new() failed\n");
3339db53cc8Stb 		goto failed;
3349db53cc8Stb 	}
3359db53cc8Stb 
3369db53cc8Stb 	for (i = 0; i < N_RC4_TESTS; i++) {
3379db53cc8Stb 		rt = &rc4_tests[i];
3389db53cc8Stb 
3399db53cc8Stb 		/* Encryption */
3409db53cc8Stb 		memset(out, 0, sizeof(out));
3419db53cc8Stb 		RC4_set_key(&key, rt->key_len, rt->key);
3429db53cc8Stb 		RC4(&key, rt->len, rt->in, out);
3439db53cc8Stb 
3449db53cc8Stb 		if (memcmp(rt->out, out, rt->len) != 0) {
3459db53cc8Stb 			fprintf(stderr, "FAIL: encryption mismatch\n");
3469db53cc8Stb 			goto failed;
3479db53cc8Stb 		}
3489db53cc8Stb 
3499db53cc8Stb 		/* Decryption */
3509db53cc8Stb 		memset(out, 0, sizeof(out));
3519db53cc8Stb 		RC4_set_key(&key, rt->key_len, rt->key);
3529db53cc8Stb 		RC4(&key, rt->len, rt->out, out);
3539db53cc8Stb 
3549db53cc8Stb 		if (memcmp(rt->in, out, rt->len) != 0) {
3559db53cc8Stb 			fprintf(stderr, "FAIL: decryption mismatch\n");
3569db53cc8Stb 			goto failed;
3579db53cc8Stb 		}
3589db53cc8Stb 
3599db53cc8Stb 		/*
3609db53cc8Stb 		 * EVP tests
3619db53cc8Stb 		 */
3629db53cc8Stb 		if (rt->key_len == 5) {
3639db53cc8Stb 			cipher = EVP_rc4_40();
3649db53cc8Stb 		} else if (rt->key_len == 16) {
3659db53cc8Stb 			cipher = EVP_rc4();
3669db53cc8Stb 		} else {
3679db53cc8Stb 			/* EVP does not support this key length */
3689db53cc8Stb 			continue;
3699db53cc8Stb 		}
3709db53cc8Stb 
3719db53cc8Stb 		/* EVP encryption */
3729db53cc8Stb 		memset(out, 0, sizeof(out));
3739db53cc8Stb 		if (!EVP_EncryptInit(ctx, cipher, rt->key, NULL)) {
3749db53cc8Stb 			fprintf(stderr, "FAIL: EVP_EncryptInit failed\n");
3759db53cc8Stb 			goto failed;
3769db53cc8Stb 		}
3779db53cc8Stb 
3789db53cc8Stb 		for (j = 0; j < rt->len;) {
3799db53cc8Stb 			in_len = arc4random_uniform(sizeof(rt->len / 2));
3809db53cc8Stb 			if (in_len > rt->len - j)
3819db53cc8Stb 				in_len = rt->len - j;
3829db53cc8Stb 
3839db53cc8Stb 			if (!EVP_EncryptUpdate(ctx, out + j, &out_len,
3849db53cc8Stb 			    rt->in + j, in_len)) {
3859db53cc8Stb 				fprintf(stderr,
3869db53cc8Stb 				    "FAIL: EVP_EncryptUpdate failed\n");
3879db53cc8Stb 				goto failed;
3889db53cc8Stb 			}
3899db53cc8Stb 
3909db53cc8Stb 			j += in_len;
3919db53cc8Stb 		}
3929db53cc8Stb 
3939db53cc8Stb 		if (!EVP_EncryptFinal_ex(ctx, out, &out_len)) {
3949db53cc8Stb 			fprintf(stderr, "FAIL: EVP_EncryptFinal_ex failed\n");
3959db53cc8Stb 			goto failed;
3969db53cc8Stb 		}
3979db53cc8Stb 
398*e04c7e8eStb 		if (!EVP_CIPHER_CTX_reset(ctx)) {
399*e04c7e8eStb 			fprintf(stderr, "FAIL: EVP_CIPHER_CTX_reset failed\n");
400*e04c7e8eStb 			goto failed;
401*e04c7e8eStb 		}
402*e04c7e8eStb 
4039db53cc8Stb 		if (memcmp(rt->out, out, rt->len) != 0) {
4049db53cc8Stb 			fprintf(stderr, "FAIL: EVP encryption mismatch\n");
4059db53cc8Stb 			goto failed;
4069db53cc8Stb 		}
4079db53cc8Stb 
4089db53cc8Stb 		/* EVP decryption */
4099db53cc8Stb 		memset(out, 0, sizeof(out));
4109db53cc8Stb 		if (!EVP_DecryptInit(ctx, cipher, rt->key, NULL)) {
4119db53cc8Stb 			fprintf(stderr, "FAIL: EVP_DecryptInit failed\n");
4129db53cc8Stb 			goto failed;
4139db53cc8Stb 		}
4149db53cc8Stb 
4159db53cc8Stb 		for (j = 0; j < rt->len;) {
4169db53cc8Stb 			in_len = arc4random_uniform(sizeof(rt->len / 2));
4179db53cc8Stb 			if (in_len > rt->len - j)
4189db53cc8Stb 				in_len = rt->len - j;
4199db53cc8Stb 
4209db53cc8Stb 			if (!EVP_DecryptUpdate(ctx, out + j, &out_len,
4219db53cc8Stb 			    rt->in + j, in_len)) {
4229db53cc8Stb 				fprintf(stderr,
4239db53cc8Stb 				    "FAIL: EVP_DecryptUpdate failed\n");
4249db53cc8Stb 				goto failed;
4259db53cc8Stb 			}
4269db53cc8Stb 
4279db53cc8Stb 			j += in_len;
4289db53cc8Stb 		}
4299db53cc8Stb 
4309db53cc8Stb 		if (!EVP_DecryptFinal_ex(ctx, out, &out_len)) {
4319db53cc8Stb 			fprintf(stderr, "FAIL: EVP_DecryptFinal_ex failed\n");
4329db53cc8Stb 			goto failed;
4339db53cc8Stb 		}
4349db53cc8Stb 
435*e04c7e8eStb 		if (!EVP_CIPHER_CTX_reset(ctx)) {
436*e04c7e8eStb 			fprintf(stderr, "FAIL: EVP_CIPHER_CTX_reset failed\n");
437*e04c7e8eStb 			goto failed;
438*e04c7e8eStb 		}
439*e04c7e8eStb 
4409db53cc8Stb 		if (memcmp(rt->out, out, rt->len) != 0) {
4419db53cc8Stb 			fprintf(stderr, "FAIL: EVP decryption mismatch\n");
4429db53cc8Stb 			goto failed;
4439db53cc8Stb 		}
4449db53cc8Stb 	}
4459db53cc8Stb 
4469db53cc8Stb 	failed = 0;
4479db53cc8Stb 
4489db53cc8Stb  failed:
4499db53cc8Stb 	EVP_CIPHER_CTX_free(ctx);
4509db53cc8Stb 	return failed;
4519db53cc8Stb }
4529db53cc8Stb 
4539db53cc8Stb int
4549db53cc8Stb main(int argc, char **argv)
4559db53cc8Stb {
4569db53cc8Stb 	int failed = 0;
4579db53cc8Stb 
4589db53cc8Stb 	failed |= rc4_test();
4599db53cc8Stb 
4609db53cc8Stb 	return failed;
4619db53cc8Stb }
462