xref: /illumos-gate/usr/src/common/crypto/des/des_ks.c (revision 694c35faa87b858ecdadfe4fc592615f4eefbb07)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 
29 static uint64_t pc1_table[2][128]=
30 {
31 /* BEGIN CSTYLED */
32 {
33 0x0000000000000000ULL, 0x0000000000100000ULL, 0x0000000000001000ULL, 0x0000000000101000ULL,
34 0x0000000000000010ULL, 0x0000000000100010ULL, 0x0000000000001010ULL, 0x0000000000101010ULL,
35 0x0000000000000001ULL, 0x0000000000100001ULL, 0x0000000000001001ULL, 0x0000000000101001ULL,
36 0x0000000000000011ULL, 0x0000000000100011ULL, 0x0000000000001011ULL, 0x0000000000101011ULL,
37 0x0000000100000000ULL, 0x0000000100100000ULL, 0x0000000100001000ULL, 0x0000000100101000ULL,
38 0x0000000100000010ULL, 0x0000000100100010ULL, 0x0000000100001010ULL, 0x0000000100101010ULL,
39 0x0000000100000001ULL, 0x0000000100100001ULL, 0x0000000100001001ULL, 0x0000000100101001ULL,
40 0x0000000100000011ULL, 0x0000000100100011ULL, 0x0000000100001011ULL, 0x0000000100101011ULL,
41 0x0000010000000000ULL, 0x0000010000100000ULL, 0x0000010000001000ULL, 0x0000010000101000ULL,
42 0x0000010000000010ULL, 0x0000010000100010ULL, 0x0000010000001010ULL, 0x0000010000101010ULL,
43 0x0000010000000001ULL, 0x0000010000100001ULL, 0x0000010000001001ULL, 0x0000010000101001ULL,
44 0x0000010000000011ULL, 0x0000010000100011ULL, 0x0000010000001011ULL, 0x0000010000101011ULL,
45 0x0000010100000000ULL, 0x0000010100100000ULL, 0x0000010100001000ULL, 0x0000010100101000ULL,
46 0x0000010100000010ULL, 0x0000010100100010ULL, 0x0000010100001010ULL, 0x0000010100101010ULL,
47 0x0000010100000001ULL, 0x0000010100100001ULL, 0x0000010100001001ULL, 0x0000010100101001ULL,
48 0x0000010100000011ULL, 0x0000010100100011ULL, 0x0000010100001011ULL, 0x0000010100101011ULL,
49 0x0001000000000000ULL, 0x0001000000100000ULL, 0x0001000000001000ULL, 0x0001000000101000ULL,
50 0x0001000000000010ULL, 0x0001000000100010ULL, 0x0001000000001010ULL, 0x0001000000101010ULL,
51 0x0001000000000001ULL, 0x0001000000100001ULL, 0x0001000000001001ULL, 0x0001000000101001ULL,
52 0x0001000000000011ULL, 0x0001000000100011ULL, 0x0001000000001011ULL, 0x0001000000101011ULL,
53 0x0001000100000000ULL, 0x0001000100100000ULL, 0x0001000100001000ULL, 0x0001000100101000ULL,
54 0x0001000100000010ULL, 0x0001000100100010ULL, 0x0001000100001010ULL, 0x0001000100101010ULL,
55 0x0001000100000001ULL, 0x0001000100100001ULL, 0x0001000100001001ULL, 0x0001000100101001ULL,
56 0x0001000100000011ULL, 0x0001000100100011ULL, 0x0001000100001011ULL, 0x0001000100101011ULL,
57 0x0001010000000000ULL, 0x0001010000100000ULL, 0x0001010000001000ULL, 0x0001010000101000ULL,
58 0x0001010000000010ULL, 0x0001010000100010ULL, 0x0001010000001010ULL, 0x0001010000101010ULL,
59 0x0001010000000001ULL, 0x0001010000100001ULL, 0x0001010000001001ULL, 0x0001010000101001ULL,
60 0x0001010000000011ULL, 0x0001010000100011ULL, 0x0001010000001011ULL, 0x0001010000101011ULL,
61 0x0001010100000000ULL, 0x0001010100100000ULL, 0x0001010100001000ULL, 0x0001010100101000ULL,
62 0x0001010100000010ULL, 0x0001010100100010ULL, 0x0001010100001010ULL, 0x0001010100101010ULL,
63 0x0001010100000001ULL, 0x0001010100100001ULL, 0x0001010100001001ULL, 0x0001010100101001ULL,
64 0x0001010100000011ULL, 0x0001010100100011ULL, 0x0001010100001011ULL, 0x0001010100101011ULL
65 },
66 {
67 0x0000000000000000ULL, 0x0000000001000000ULL, 0x0000000000010000ULL, 0x0000000001010000ULL,
68 0x0000000000000100ULL, 0x0000000001000100ULL, 0x0000000000010100ULL, 0x0000000001010100ULL,
69 0x0000000010000000ULL, 0x0000000011000000ULL, 0x0000000010010000ULL, 0x0000000011010000ULL,
70 0x0000000010000100ULL, 0x0000000011000100ULL, 0x0000000010010100ULL, 0x0000000011010100ULL,
71 0x0000001000000000ULL, 0x0000001001000000ULL, 0x0000001000010000ULL, 0x0000001001010000ULL,
72 0x0000001000000100ULL, 0x0000001001000100ULL, 0x0000001000010100ULL, 0x0000001001010100ULL,
73 0x0000001010000000ULL, 0x0000001011000000ULL, 0x0000001010010000ULL, 0x0000001011010000ULL,
74 0x0000001010000100ULL, 0x0000001011000100ULL, 0x0000001010010100ULL, 0x0000001011010100ULL,
75 0x0000100000000000ULL, 0x0000100001000000ULL, 0x0000100000010000ULL, 0x0000100001010000ULL,
76 0x0000100000000100ULL, 0x0000100001000100ULL, 0x0000100000010100ULL, 0x0000100001010100ULL,
77 0x0000100010000000ULL, 0x0000100011000000ULL, 0x0000100010010000ULL, 0x0000100011010000ULL,
78 0x0000100010000100ULL, 0x0000100011000100ULL, 0x0000100010010100ULL, 0x0000100011010100ULL,
79 0x0000101000000000ULL, 0x0000101001000000ULL, 0x0000101000010000ULL, 0x0000101001010000ULL,
80 0x0000101000000100ULL, 0x0000101001000100ULL, 0x0000101000010100ULL, 0x0000101001010100ULL,
81 0x0000101010000000ULL, 0x0000101011000000ULL, 0x0000101010010000ULL, 0x0000101011010000ULL,
82 0x0000101010000100ULL, 0x0000101011000100ULL, 0x0000101010010100ULL, 0x0000101011010100ULL,
83 0x0010000000000000ULL, 0x0010000001000000ULL, 0x0010000000010000ULL, 0x0010000001010000ULL,
84 0x0010000000000100ULL, 0x0010000001000100ULL, 0x0010000000010100ULL, 0x0010000001010100ULL,
85 0x0010000010000000ULL, 0x0010000011000000ULL, 0x0010000010010000ULL, 0x0010000011010000ULL,
86 0x0010000010000100ULL, 0x0010000011000100ULL, 0x0010000010010100ULL, 0x0010000011010100ULL,
87 0x0010001000000000ULL, 0x0010001001000000ULL, 0x0010001000010000ULL, 0x0010001001010000ULL,
88 0x0010001000000100ULL, 0x0010001001000100ULL, 0x0010001000010100ULL, 0x0010001001010100ULL,
89 0x0010001010000000ULL, 0x0010001011000000ULL, 0x0010001010010000ULL, 0x0010001011010000ULL,
90 0x0010001010000100ULL, 0x0010001011000100ULL, 0x0010001010010100ULL, 0x0010001011010100ULL,
91 0x0010100000000000ULL, 0x0010100001000000ULL, 0x0010100000010000ULL, 0x0010100001010000ULL,
92 0x0010100000000100ULL, 0x0010100001000100ULL, 0x0010100000010100ULL, 0x0010100001010100ULL,
93 0x0010100010000000ULL, 0x0010100011000000ULL, 0x0010100010010000ULL, 0x0010100011010000ULL,
94 0x0010100010000100ULL, 0x0010100011000100ULL, 0x0010100010010100ULL, 0x0010100011010100ULL,
95 0x0010101000000000ULL, 0x0010101001000000ULL, 0x0010101000010000ULL, 0x0010101001010000ULL,
96 0x0010101000000100ULL, 0x0010101001000100ULL, 0x0010101000010100ULL, 0x0010101001010100ULL,
97 0x0010101010000000ULL, 0x0010101011000000ULL, 0x0010101010010000ULL, 0x0010101011010000ULL,
98 0x0010101010000100ULL, 0x0010101011000100ULL, 0x0010101010010100ULL, 0x0010101011010100ULL
99 }
100 /* END CSTYLED */
101 };
102 
103 
104 static uint32_t pc2_1_table[4][64] =
105 {
106 {
107 0x00000000, 0x00004000, 0x04000000, 0x04004000,
108 0x00000100, 0x00004100, 0x04000100, 0x04004100,
109 0x00020000, 0x00024000, 0x04020000, 0x04024000,
110 0x00020100, 0x00024100, 0x04020100, 0x04024100,
111 0x00000001, 0x00004001, 0x04000001, 0x04004001,
112 0x00000101, 0x00004101, 0x04000101, 0x04004101,
113 0x00020001, 0x00024001, 0x04020001, 0x04024001,
114 0x00020101, 0x00024101, 0x04020101, 0x04024101,
115 0x08000000, 0x08004000, 0x0c000000, 0x0c004000,
116 0x08000100, 0x08004100, 0x0c000100, 0x0c004100,
117 0x08020000, 0x08024000, 0x0c020000, 0x0c024000,
118 0x08020100, 0x08024100, 0x0c020100, 0x0c024100,
119 0x08000001, 0x08004001, 0x0c000001, 0x0c004001,
120 0x08000101, 0x08004101, 0x0c000101, 0x0c004101,
121 0x08020001, 0x08024001, 0x0c020001, 0x0c024001,
122 0x08020101, 0x08024101, 0x0c020101, 0x0c024101
123 },
124 {
125 0x00000000, 0x00000200, 0x20000000, 0x20000200,
126 0x00001000, 0x00001200, 0x20001000, 0x20001200,
127 0x00000000, 0x00000200, 0x20000000, 0x20000200,
128 0x00001000, 0x00001200, 0x20001000, 0x20001200,
129 0x00000040, 0x00000240, 0x20000040, 0x20000240,
130 0x00001040, 0x00001240, 0x20001040, 0x20001240,
131 0x00000040, 0x00000240, 0x20000040, 0x20000240,
132 0x00001040, 0x00001240, 0x20001040, 0x20001240,
133 0x00000010, 0x00000210, 0x20000010, 0x20000210,
134 0x00001010, 0x00001210, 0x20001010, 0x20001210,
135 0x00000010, 0x00000210, 0x20000010, 0x20000210,
136 0x00001010, 0x00001210, 0x20001010, 0x20001210,
137 0x00000050, 0x00000250, 0x20000050, 0x20000250,
138 0x00001050, 0x00001250, 0x20001050, 0x20001250,
139 0x00000050, 0x00000250, 0x20000050, 0x20000250,
140 0x00001050, 0x00001250, 0x20001050, 0x20001250
141 },
142 {
143 0x00000000, 0x00000000, 0x40000000, 0x40000000,
144 0x00000020, 0x00000020, 0x40000020, 0x40000020,
145 0x00008000, 0x00008000, 0x40008000, 0x40008000,
146 0x00008020, 0x00008020, 0x40008020, 0x40008020,
147 0x80000000, 0x80000000, 0xc0000000, 0xc0000000,
148 0x80000020, 0x80000020, 0xc0000020, 0xc0000020,
149 0x80008000, 0x80008000, 0xc0008000, 0xc0008000,
150 0x80008020, 0x80008020, 0xc0008020, 0xc0008020,
151 0x00000002, 0x00000002, 0x40000002, 0x40000002,
152 0x00000022, 0x00000022, 0x40000022, 0x40000022,
153 0x00008002, 0x00008002, 0x40008002, 0x40008002,
154 0x00008022, 0x00008022, 0x40008022, 0x40008022,
155 0x80000002, 0x80000002, 0xc0000002, 0xc0000002,
156 0x80000022, 0x80000022, 0xc0000022, 0xc0000022,
157 0x80008002, 0x80008002, 0xc0008002, 0xc0008002,
158 0x80008022, 0x80008022, 0xc0008022, 0xc0008022
159 },
160 {
161 0x00000000, 0x10000000, 0x00000800, 0x10000800,
162 0x00000000, 0x10000000, 0x00000800, 0x10000800,
163 0x00002000, 0x10002000, 0x00002800, 0x10002800,
164 0x00002000, 0x10002000, 0x00002800, 0x10002800,
165 0x00000004, 0x10000004, 0x00000804, 0x10000804,
166 0x00000004, 0x10000004, 0x00000804, 0x10000804,
167 0x00002004, 0x10002004, 0x00002804, 0x10002804,
168 0x00002004, 0x10002004, 0x00002804, 0x10002804,
169 0x00000400, 0x10000400, 0x00000c00, 0x10000c00,
170 0x00000400, 0x10000400, 0x00000c00, 0x10000c00,
171 0x00002400, 0x10002400, 0x00002c00, 0x10002c00,
172 0x00002400, 0x10002400, 0x00002c00, 0x10002c00,
173 0x00000404, 0x10000404, 0x00000c04, 0x10000c04,
174 0x00000404, 0x10000404, 0x00000c04, 0x10000c04,
175 0x00002404, 0x10002404, 0x00002c04, 0x10002c04,
176 0x00002404, 0x10002404, 0x00002c04, 0x10002c04
177 }
178 };
179 
180 static uint32_t pc2_1_tail_table[8] =
181 {
182 0x00000000, 0x00010000, 0x00000008, 0x00010008,
183 0x00000080, 0x00010080, 0x00000088, 0x00010088
184 };
185 
186 static uint32_t pc2_2_table[5][32] =
187 {
188 {
189 0x00000000, 0x00010000, 0x00000008, 0x00010008,
190 0x20000000, 0x20010000, 0x20000008, 0x20010008,
191 0x00100000, 0x00110000, 0x00100008, 0x00110008,
192 0x20100000, 0x20110000, 0x20100008, 0x20110008,
193 0x00000010, 0x00010010, 0x00000018, 0x00010018,
194 0x20000010, 0x20010010, 0x20000018, 0x20010018,
195 0x00100010, 0x00110010, 0x00100018, 0x00110018,
196 0x20100010, 0x20110010, 0x20100018, 0x20110018
197 },
198 {
199 0x00000000, 0x00000000, 0x10000000, 0x10000000,
200 0x00000020, 0x00000020, 0x10000020, 0x10000020,
201 0x00000000, 0x00000000, 0x10000000, 0x10000000,
202 0x00000020, 0x00000020, 0x10000020, 0x10000020,
203 0x00000400, 0x00000400, 0x10000400, 0x10000400,
204 0x00000420, 0x00000420, 0x10000420, 0x10000420,
205 0x00000400, 0x00000400, 0x10000400, 0x10000400,
206 0x00000420, 0x00000420, 0x10000420, 0x10000420
207 },
208 {
209 0x00000000, 0x00000000, 0x00000080, 0x00000080,
210 0x80000000, 0x80000000, 0x80000080, 0x80000080,
211 0x00080000, 0x00080000, 0x00080080, 0x00080080,
212 0x80080000, 0x80080000, 0x80080080, 0x80080080,
213 0x00001000, 0x00001000, 0x00001080, 0x00001080,
214 0x80001000, 0x80001000, 0x80001080, 0x80001080,
215 0x00081000, 0x00081000, 0x00081080, 0x00081080,
216 0x80081000, 0x80081000, 0x80081080, 0x80081080
217 },
218 {
219 0x00000000, 0x00008000, 0x08000000, 0x08008000,
220 0x00000100, 0x00008100, 0x08000100, 0x08008100,
221 0x00020000, 0x00028000, 0x08020000, 0x08028000,
222 0x00020100, 0x00028100, 0x08020100, 0x08028100,
223 0x00004000, 0x0000c000, 0x08004000, 0x0800c000,
224 0x00004100, 0x0000c100, 0x08004100, 0x0800c100,
225 0x00024000, 0x0002c000, 0x08024000, 0x0802c000,
226 0x00024100, 0x0002c100, 0x08024100, 0x0802c100
227 },
228 {
229 0x00000000, 0x00000200, 0x40000000, 0x40000200,
230 0x00040000, 0x00040200, 0x40040000, 0x40040200,
231 0x00000040, 0x00000240, 0x40000040, 0x40000240,
232 0x00040040, 0x00040240, 0x40040040, 0x40040240,
233 0x00002000, 0x00002200, 0x40002000, 0x40002200,
234 0x00042000, 0x00042200, 0x40042000, 0x40042200,
235 0x00002040, 0x00002240, 0x40002040, 0x40002240,
236 0x00042040, 0x00042240, 0x40042040, 0x40042240
237 }
238 };
239 
240 static uint32_t pc2_2_tail_table[4] =
241 {
242 0x00000000, 0x00000800, 0x04000000, 0x04000800
243 };
244 
245 static int ss[16] = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};
246 
247 
248 /*
249  * This routine initializes the key schedule.
250  */
251 void
des_ks(uint64_t * ks,uint64_t key)252 des_ks(uint64_t *ks, uint64_t key)
253 {
254 	int i;
255 	uint32_t upper, lower;
256 	uint64_t c, d;
257 
258 	c = pc1_table[0][key >> 57] |
259 		((pc1_table[0][(key >> 49) & 127ULL]) << 1) |
260 		((pc1_table[0][(key >> 41) & 127ULL]) << 2) |
261 		((pc1_table[0][(key >> 33) & 127ULL]) << 3) |
262 		pc1_table[1][(key >> 25) & 127ULL] |
263 		((pc1_table[1][(key >> 17) & 127ULL]) << 1) |
264 		((pc1_table[1][(key >> 9) & 127ULL]) << 2) |
265 		((pc1_table[1][(key >> 1) & 127ULL]) << 3);
266 
267 	d = (c & 0xfffffffULL) | ((c & 0xfffffffULL) << 28);
268 	c = (c & 0xfffffff0000000ULL) | ((c & 0xfffffff0000000ULL) >> 28);
269 
270 	for (i = 0; i < 16; i++) {
271 		c = c << ss[i];
272 		d = d << ss[i];
273 
274 		upper = pc2_1_table[0][(c >> 50) & 63] |
275 			pc2_1_table[1][(c >> 44) & 63] |
276 			pc2_1_table[2][(c >> 38) & 63] |
277 			pc2_1_table[3][(c >> 32) & 63] |
278 			pc2_1_tail_table[(c >> 28) & 7];
279 
280 		lower = pc2_2_table[0][(d >> 51) & 31] |
281 			pc2_2_table[1][(d >> 46) & 31] |
282 			pc2_2_table[2][(d >> 41) & 31] |
283 			pc2_2_table[3][(d >> 36) & 31] |
284 			pc2_2_table[4][(d >> 31) & 31] |
285 			pc2_2_tail_table[(d >> 28) & 3];
286 		ks[i] = ((uint64_t)upper)<<32 | ((uint64_t)lower);
287 	}
288 }
289