xref: /titanic_52/usr/src/common/net/wanboot/crypt/des3_test.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * NIST tests for 3DES certification.
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * Using the values for td[], encrypts plain text using the provided
33*7c478bd9Sstevel@tonic-gate  * key and verifies the result against the cipher value. Then decrypts
34*7c478bd9Sstevel@tonic-gate  * the cipher and compares the result against the plain value.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * Also, gk[] and bk[] are used to test the 3DES keycheck algorithm.
37*7c478bd9Sstevel@tonic-gate  * Each key in gk[] should pass the keycheck and every key in bk[] should
38*7c478bd9Sstevel@tonic-gate  * fail the keycheck.
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include <stdio.h>
42*7c478bd9Sstevel@tonic-gate #include <strings.h>
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include "des3.h"
45*7c478bd9Sstevel@tonic-gate #include "des.h"
46*7c478bd9Sstevel@tonic-gate #include "des3_test.h"
47*7c478bd9Sstevel@tonic-gate #include "cmn_test.h"
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate typedef struct test_data {
50*7c478bd9Sstevel@tonic-gate 	char key[DES_KEY_SIZE * 2];
51*7c478bd9Sstevel@tonic-gate 	char plain[DES3_BLOCK_SIZE * 2];
52*7c478bd9Sstevel@tonic-gate 	char cipher[DES3_BLOCK_SIZE * 2];
53*7c478bd9Sstevel@tonic-gate } test_data_t;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate static test_data_t td[] = {
56*7c478bd9Sstevel@tonic-gate 	{ "0000000000000000", "0000000000000000", "8CA64DE9C1B123A7" },
57*7c478bd9Sstevel@tonic-gate 	{ "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "7359B2163E4EDC58" },
58*7c478bd9Sstevel@tonic-gate 	{ "3000000000000000", "1000000000000001", "958E6E627A05557B" },
59*7c478bd9Sstevel@tonic-gate 	{ "1111111111111111", "1111111111111111", "F40379AB9E0EC533" },
60*7c478bd9Sstevel@tonic-gate 	{ "0123456789ABCDEF", "1111111111111111", "17668DFC7292532D" },
61*7c478bd9Sstevel@tonic-gate 	{ "1111111111111111", "0123456789ABCDEF", "8A5AE1F81AB8F2DD" },
62*7c478bd9Sstevel@tonic-gate 	{ "0000000000000000", "0000000000000000", "8CA64DE9C1B123A7" },
63*7c478bd9Sstevel@tonic-gate 	{ "FEDCBA9876543210", "0123456789ABCDEF", "ED39D950FA74BCC4" },
64*7c478bd9Sstevel@tonic-gate 	{ "7CA110454A1A6E57", "01A1D6D039776742", "690F5B0D9A26939B" },
65*7c478bd9Sstevel@tonic-gate 	{ "0131D9619DC1376E", "5CD54CA83DEF57DA", "7A389D10354BD271" },
66*7c478bd9Sstevel@tonic-gate 	{ "07A1133E4A0B2686", "0248D43806F67172", "868EBB51CAB4599A" },
67*7c478bd9Sstevel@tonic-gate 	{ "3849674C2602319E", "51454B582DDF440A", "7178876E01F19B2A" },
68*7c478bd9Sstevel@tonic-gate 	{ "04B915BA43FEB5B6", "42FD443059577FA2", "AF37FB421F8C4095" },
69*7c478bd9Sstevel@tonic-gate 	{ "0113B970FD34F2CE", "059B5E0851CF143A", "86A560F10EC6D85B" },
70*7c478bd9Sstevel@tonic-gate 	{ "0170F175468FB5E6", "0756D8E0774761D2", "0CD3DA020021DC09" },
71*7c478bd9Sstevel@tonic-gate 	{ "43297FAD38E373FE", "762514B829BF486A", "EA676B2CB7DB2B7A" },
72*7c478bd9Sstevel@tonic-gate 	{ "07A7137045DA2A16", "3BDD119049372802", "DFD64A815CAF1A0F" },
73*7c478bd9Sstevel@tonic-gate 	{ "04689104C2FD3B2F", "26955F6835AF609A", "5C513C9C4886C088" },
74*7c478bd9Sstevel@tonic-gate 	{ "37D06BB516CB7546", "164D5E404F275232", "0A2AEEAE3FF4AB77" },
75*7c478bd9Sstevel@tonic-gate 	{ "1F08260D1AC2465E", "6B056E18759F5CCA", "EF1BF03E5DFA575A" },
76*7c478bd9Sstevel@tonic-gate 	{ "584023641ABA6176", "004BD6EF09176062", "88BF0DB6D70DEE56" },
77*7c478bd9Sstevel@tonic-gate 	{ "025816164629B007", "480D39006EE762F2", "A1F9915541020B56" },
78*7c478bd9Sstevel@tonic-gate 	{ "49793EBC79B3258F", "437540C8698F3CFA", "6FBF1CAFCFFD0556" },
79*7c478bd9Sstevel@tonic-gate 	{ "4FB05E1515AB73A7", "072D43A077075292", "2F22E49BAB7CA1AC" },
80*7c478bd9Sstevel@tonic-gate 	{ "49E95D6D4CA229BF", "02FE55778117F12A", "5A6B612CC26CCE4A" },
81*7c478bd9Sstevel@tonic-gate 	{ "018310DC409B26D6", "1D9D5C5018F728C2", "5F4C038ED12B2E41" },
82*7c478bd9Sstevel@tonic-gate 	{ "1C587F1C13924FEF", "305532286D6F295A", "63FAC0D034D9F793" },
83*7c478bd9Sstevel@tonic-gate 	{ "0101010101010101", "0123456789ABCDEF", "617B3A0CE8F07100" },
84*7c478bd9Sstevel@tonic-gate 	{ "1F1F1F1F0E0E0E0E", "0123456789ABCDEF", "DB958605F8C8C606" },
85*7c478bd9Sstevel@tonic-gate 	{ "E0FEE0FEF1FEF1FE", "0123456789ABCDEF", "EDBFD1C66C29CCC7" },
86*7c478bd9Sstevel@tonic-gate 	{ "0000000000000000", "FFFFFFFFFFFFFFFF", "355550B2150E2451" },
87*7c478bd9Sstevel@tonic-gate 	{ "FFFFFFFFFFFFFFFF", "0000000000000000", "CAAAAF4DEAF1DBAE" },
88*7c478bd9Sstevel@tonic-gate 	{ "0123456789ABCDEF", "0000000000000000", "D5D44FF720683D0D" },
89*7c478bd9Sstevel@tonic-gate 	{ "FEDCBA9876543210", "FFFFFFFFFFFFFFFF", "2A2BB008DF97C2F2" }
90*7c478bd9Sstevel@tonic-gate };
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate typedef struct test_keys {
93*7c478bd9Sstevel@tonic-gate 	char key1[DES_KEY_SIZE * 2];
94*7c478bd9Sstevel@tonic-gate 	char key2[DES_KEY_SIZE * 2];
95*7c478bd9Sstevel@tonic-gate 	char key3[DES_KEY_SIZE * 2];
96*7c478bd9Sstevel@tonic-gate } test_keys_t;
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate static test_keys_t gk[] = {
99*7c478bd9Sstevel@tonic-gate 	{ "A0CB0D98FE752301", "105237EFCBA00DFE", "8CA64DE9C1B123A7" }
100*7c478bd9Sstevel@tonic-gate };
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate static test_keys_t bk[] = {
103*7c478bd9Sstevel@tonic-gate 	{ "A0CB0D98FE752301", "A0CB0D98FE752301", "8CA64DE9C1B123A7" },
104*7c478bd9Sstevel@tonic-gate 	{ "FFFFFFFFFFFFFFFF", "0101010101010101", "E0E0E0E0F1F1F1F1" }
105*7c478bd9Sstevel@tonic-gate };
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate int
108*7c478bd9Sstevel@tonic-gate des3test(void)
109*7c478bd9Sstevel@tonic-gate {
110*7c478bd9Sstevel@tonic-gate 	void *d3h;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	unsigned char key[DES3_KEY_SIZE];
113*7c478bd9Sstevel@tonic-gate 	unsigned char plain[DES3_BLOCK_SIZE];
114*7c478bd9Sstevel@tonic-gate 	unsigned char cipher[DES3_BLOCK_SIZE];
115*7c478bd9Sstevel@tonic-gate 	unsigned char work[DES3_BLOCK_SIZE];
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	int fail;
118*7c478bd9Sstevel@tonic-gate 	int num;
119*7c478bd9Sstevel@tonic-gate 	int i;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	if (des3_init(&d3h) != 0) {
122*7c478bd9Sstevel@tonic-gate 		(void) printf("Error initializing DES3\n");
123*7c478bd9Sstevel@tonic-gate 		return (-1);
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	num = sizeof (td) / sizeof (test_data_t);
127*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
128*7c478bd9Sstevel@tonic-gate 		fail = 0;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 		(void) printf("NIST Test #%d ", i+1);
131*7c478bd9Sstevel@tonic-gate 		getxdata(key, td[i].key, DES_KEY_SIZE);
132*7c478bd9Sstevel@tonic-gate 		bcopy(key, &key[8], DES_KEY_SIZE); /* K1=K2=K3 for test */
133*7c478bd9Sstevel@tonic-gate 		bcopy(key, &key[16], DES_KEY_SIZE);
134*7c478bd9Sstevel@tonic-gate 		des3_key(d3h, key);
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 		getxdata(plain, td[i].plain, DES3_BLOCK_SIZE);
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 		getxdata(cipher, td[i].cipher, DES3_BLOCK_SIZE);
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 		bcopy(plain, work, DES3_BLOCK_SIZE);
141*7c478bd9Sstevel@tonic-gate 		des3_encrypt(d3h, work);
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 		if (bcmp(work, cipher, DES3_BLOCK_SIZE) != 0) {
144*7c478bd9Sstevel@tonic-gate 			(void) printf("FAILED [Encrypt]");
145*7c478bd9Sstevel@tonic-gate 			(void) printf(" c: ");
146*7c478bd9Sstevel@tonic-gate 			putxdata(work, DES3_BLOCK_SIZE);
147*7c478bd9Sstevel@tonic-gate 			fail++;
148*7c478bd9Sstevel@tonic-gate 		}
149*7c478bd9Sstevel@tonic-gate 		des3_decrypt(d3h, work);
150*7c478bd9Sstevel@tonic-gate 		if (bcmp(work, plain, DES3_BLOCK_SIZE) != 0) {
151*7c478bd9Sstevel@tonic-gate 			(void) printf("FAILED [Decrypt]");
152*7c478bd9Sstevel@tonic-gate 			(void) printf(" p: ");
153*7c478bd9Sstevel@tonic-gate 			putxdata(work, DES3_BLOCK_SIZE);
154*7c478bd9Sstevel@tonic-gate 			fail++;
155*7c478bd9Sstevel@tonic-gate 		}
156*7c478bd9Sstevel@tonic-gate 		if (fail == 0)
157*7c478bd9Sstevel@tonic-gate 			(void) printf("PASSED");
158*7c478bd9Sstevel@tonic-gate 		(void) printf("\n");
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	des3_fini(d3h);
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	return (fail);
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate int
167*7c478bd9Sstevel@tonic-gate des3_keytest(void)
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate 	unsigned char key[DES_KEY_SIZE * 3];
170*7c478bd9Sstevel@tonic-gate 	int num;
171*7c478bd9Sstevel@tonic-gate 	int testnum = 0;
172*7c478bd9Sstevel@tonic-gate 	int fail = 0;
173*7c478bd9Sstevel@tonic-gate 	int i;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	num = sizeof (gk) / sizeof (test_keys_t);
176*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
177*7c478bd9Sstevel@tonic-gate 		getxdata(key, gk[i].key1, DES_KEY_SIZE);
178*7c478bd9Sstevel@tonic-gate 		getxdata(&key[8], gk[i].key2, DES_KEY_SIZE);
179*7c478bd9Sstevel@tonic-gate 		getxdata(&key[16], gk[i].key3, DES_KEY_SIZE);
180*7c478bd9Sstevel@tonic-gate 		(void) printf("Keycheck Test #%d ", testnum);
181*7c478bd9Sstevel@tonic-gate 		if (des3_keycheck(key)) {
182*7c478bd9Sstevel@tonic-gate 			(void) printf("PASSED\n", testnum);
183*7c478bd9Sstevel@tonic-gate 		} else {
184*7c478bd9Sstevel@tonic-gate 			fail++;
185*7c478bd9Sstevel@tonic-gate 			(void) printf("FAILED\n", testnum);
186*7c478bd9Sstevel@tonic-gate 		}
187*7c478bd9Sstevel@tonic-gate 		testnum++;
188*7c478bd9Sstevel@tonic-gate 	}
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	num = sizeof (bk) / sizeof (test_keys_t);
191*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
192*7c478bd9Sstevel@tonic-gate 		getxdata(key, bk[i].key1, DES_KEY_SIZE);
193*7c478bd9Sstevel@tonic-gate 		getxdata(&key[8], bk[i].key2, DES_KEY_SIZE);
194*7c478bd9Sstevel@tonic-gate 		getxdata(&key[16], bk[i].key3, DES_KEY_SIZE);
195*7c478bd9Sstevel@tonic-gate 		(void) printf("Keycheck Test #%d ", testnum);
196*7c478bd9Sstevel@tonic-gate 		if (!des3_keycheck(key)) {
197*7c478bd9Sstevel@tonic-gate 			(void) printf("PASSED\n", testnum);
198*7c478bd9Sstevel@tonic-gate 		} else {
199*7c478bd9Sstevel@tonic-gate 			fail++;
200*7c478bd9Sstevel@tonic-gate 			(void) printf("FAILED\n", testnum);
201*7c478bd9Sstevel@tonic-gate 		}
202*7c478bd9Sstevel@tonic-gate 		testnum++;
203*7c478bd9Sstevel@tonic-gate 	}
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	return (fail);
206*7c478bd9Sstevel@tonic-gate }
207