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