1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2002-2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate * Portable C version of des() and des_key() functions.
31*0Sstevel@tonic-gate * This version is very similar to that in Part V of Applied Cryptography
32*0Sstevel@tonic-gate * by Bruce Schneier.
33*0Sstevel@tonic-gate *
34*0Sstevel@tonic-gate * This information is in the public domain 12/15/95 P. Karn
35*0Sstevel@tonic-gate */
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate #include <sys/types.h>
38*0Sstevel@tonic-gate #include <sys/sysmacros.h>
39*0Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
40*0Sstevel@tonic-gate #include <sys/systm.h>
41*0Sstevel@tonic-gate #else
42*0Sstevel@tonic-gate #include <strings.h>
43*0Sstevel@tonic-gate #endif
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate #include "des.h"
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate /*
48*0Sstevel@tonic-gate * Combined SP lookup table, linked in
49*0Sstevel@tonic-gate * For best results, ensure that this is aligned on a 32-bit boundary;
50*0Sstevel@tonic-gate */
51*0Sstevel@tonic-gate static uint32_t Spbox[8][64] = {
52*0Sstevel@tonic-gate 0x01010400U, 0x00000000U, 0x00010000U, 0x01010404U,
53*0Sstevel@tonic-gate 0x01010004U, 0x00010404U, 0x00000004U, 0x00010000U,
54*0Sstevel@tonic-gate 0x00000400U, 0x01010400U, 0x01010404U, 0x00000400U,
55*0Sstevel@tonic-gate 0x01000404U, 0x01010004U, 0x01000000U, 0x00000004U,
56*0Sstevel@tonic-gate 0x00000404U, 0x01000400U, 0x01000400U, 0x00010400U,
57*0Sstevel@tonic-gate 0x00010400U, 0x01010000U, 0x01010000U, 0x01000404U,
58*0Sstevel@tonic-gate 0x00010004U, 0x01000004U, 0x01000004U, 0x00010004U,
59*0Sstevel@tonic-gate 0x00000000U, 0x00000404U, 0x00010404U, 0x01000000U,
60*0Sstevel@tonic-gate 0x00010000U, 0x01010404U, 0x00000004U, 0x01010000U,
61*0Sstevel@tonic-gate 0x01010400U, 0x01000000U, 0x01000000U, 0x00000400U,
62*0Sstevel@tonic-gate 0x01010004U, 0x00010000U, 0x00010400U, 0x01000004U,
63*0Sstevel@tonic-gate 0x00000400U, 0x00000004U, 0x01000404U, 0x00010404U,
64*0Sstevel@tonic-gate 0x01010404U, 0x00010004U, 0x01010000U, 0x01000404U,
65*0Sstevel@tonic-gate 0x01000004U, 0x00000404U, 0x00010404U, 0x01010400U,
66*0Sstevel@tonic-gate 0x00000404U, 0x01000400U, 0x01000400U, 0x00000000U,
67*0Sstevel@tonic-gate 0x00010004U, 0x00010400U, 0x00000000U, 0x01010004U,
68*0Sstevel@tonic-gate 0x80108020U, 0x80008000U, 0x00008000U, 0x00108020U,
69*0Sstevel@tonic-gate 0x00100000U, 0x00000020U, 0x80100020U, 0x80008020U,
70*0Sstevel@tonic-gate 0x80000020U, 0x80108020U, 0x80108000U, 0x80000000U,
71*0Sstevel@tonic-gate 0x80008000U, 0x00100000U, 0x00000020U, 0x80100020U,
72*0Sstevel@tonic-gate 0x00108000U, 0x00100020U, 0x80008020U, 0x00000000U,
73*0Sstevel@tonic-gate 0x80000000U, 0x00008000U, 0x00108020U, 0x80100000U,
74*0Sstevel@tonic-gate 0x00100020U, 0x80000020U, 0x00000000U, 0x00108000U,
75*0Sstevel@tonic-gate 0x00008020U, 0x80108000U, 0x80100000U, 0x00008020U,
76*0Sstevel@tonic-gate 0x00000000U, 0x00108020U, 0x80100020U, 0x00100000U,
77*0Sstevel@tonic-gate 0x80008020U, 0x80100000U, 0x80108000U, 0x00008000U,
78*0Sstevel@tonic-gate 0x80100000U, 0x80008000U, 0x00000020U, 0x80108020U,
79*0Sstevel@tonic-gate 0x00108020U, 0x00000020U, 0x00008000U, 0x80000000U,
80*0Sstevel@tonic-gate 0x00008020U, 0x80108000U, 0x00100000U, 0x80000020U,
81*0Sstevel@tonic-gate 0x00100020U, 0x80008020U, 0x80000020U, 0x00100020U,
82*0Sstevel@tonic-gate 0x00108000U, 0x00000000U, 0x80008000U, 0x00008020U,
83*0Sstevel@tonic-gate 0x80000000U, 0x80100020U, 0x80108020U, 0x00108000U,
84*0Sstevel@tonic-gate 0x00000208U, 0x08020200U, 0x00000000U, 0x08020008U,
85*0Sstevel@tonic-gate 0x08000200U, 0x00000000U, 0x00020208U, 0x08000200U,
86*0Sstevel@tonic-gate 0x00020008U, 0x08000008U, 0x08000008U, 0x00020000U,
87*0Sstevel@tonic-gate 0x08020208U, 0x00020008U, 0x08020000U, 0x00000208U,
88*0Sstevel@tonic-gate 0x08000000U, 0x00000008U, 0x08020200U, 0x00000200U,
89*0Sstevel@tonic-gate 0x00020200U, 0x08020000U, 0x08020008U, 0x00020208U,
90*0Sstevel@tonic-gate 0x08000208U, 0x00020200U, 0x00020000U, 0x08000208U,
91*0Sstevel@tonic-gate 0x00000008U, 0x08020208U, 0x00000200U, 0x08000000U,
92*0Sstevel@tonic-gate 0x08020200U, 0x08000000U, 0x00020008U, 0x00000208U,
93*0Sstevel@tonic-gate 0x00020000U, 0x08020200U, 0x08000200U, 0x00000000U,
94*0Sstevel@tonic-gate 0x00000200U, 0x00020008U, 0x08020208U, 0x08000200U,
95*0Sstevel@tonic-gate 0x08000008U, 0x00000200U, 0x00000000U, 0x08020008U,
96*0Sstevel@tonic-gate 0x08000208U, 0x00020000U, 0x08000000U, 0x08020208U,
97*0Sstevel@tonic-gate 0x00000008U, 0x00020208U, 0x00020200U, 0x08000008U,
98*0Sstevel@tonic-gate 0x08020000U, 0x08000208U, 0x00000208U, 0x08020000U,
99*0Sstevel@tonic-gate 0x00020208U, 0x00000008U, 0x08020008U, 0x00020200U,
100*0Sstevel@tonic-gate 0x00802001U, 0x00002081U, 0x00002081U, 0x00000080U,
101*0Sstevel@tonic-gate 0x00802080U, 0x00800081U, 0x00800001U, 0x00002001U,
102*0Sstevel@tonic-gate 0x00000000U, 0x00802000U, 0x00802000U, 0x00802081U,
103*0Sstevel@tonic-gate 0x00000081U, 0x00000000U, 0x00800080U, 0x00800001U,
104*0Sstevel@tonic-gate 0x00000001U, 0x00002000U, 0x00800000U, 0x00802001U,
105*0Sstevel@tonic-gate 0x00000080U, 0x00800000U, 0x00002001U, 0x00002080U,
106*0Sstevel@tonic-gate 0x00800081U, 0x00000001U, 0x00002080U, 0x00800080U,
107*0Sstevel@tonic-gate 0x00002000U, 0x00802080U, 0x00802081U, 0x00000081U,
108*0Sstevel@tonic-gate 0x00800080U, 0x00800001U, 0x00802000U, 0x00802081U,
109*0Sstevel@tonic-gate 0x00000081U, 0x00000000U, 0x00000000U, 0x00802000U,
110*0Sstevel@tonic-gate 0x00002080U, 0x00800080U, 0x00800081U, 0x00000001U,
111*0Sstevel@tonic-gate 0x00802001U, 0x00002081U, 0x00002081U, 0x00000080U,
112*0Sstevel@tonic-gate 0x00802081U, 0x00000081U, 0x00000001U, 0x00002000U,
113*0Sstevel@tonic-gate 0x00800001U, 0x00002001U, 0x00802080U, 0x00800081U,
114*0Sstevel@tonic-gate 0x00002001U, 0x00002080U, 0x00800000U, 0x00802001U,
115*0Sstevel@tonic-gate 0x00000080U, 0x00800000U, 0x00002000U, 0x00802080U,
116*0Sstevel@tonic-gate 0x00000100U, 0x02080100U, 0x02080000U, 0x42000100U,
117*0Sstevel@tonic-gate 0x00080000U, 0x00000100U, 0x40000000U, 0x02080000U,
118*0Sstevel@tonic-gate 0x40080100U, 0x00080000U, 0x02000100U, 0x40080100U,
119*0Sstevel@tonic-gate 0x42000100U, 0x42080000U, 0x00080100U, 0x40000000U,
120*0Sstevel@tonic-gate 0x02000000U, 0x40080000U, 0x40080000U, 0x00000000U,
121*0Sstevel@tonic-gate 0x40000100U, 0x42080100U, 0x42080100U, 0x02000100U,
122*0Sstevel@tonic-gate 0x42080000U, 0x40000100U, 0x00000000U, 0x42000000U,
123*0Sstevel@tonic-gate 0x02080100U, 0x02000000U, 0x42000000U, 0x00080100U,
124*0Sstevel@tonic-gate 0x00080000U, 0x42000100U, 0x00000100U, 0x02000000U,
125*0Sstevel@tonic-gate 0x40000000U, 0x02080000U, 0x42000100U, 0x40080100U,
126*0Sstevel@tonic-gate 0x02000100U, 0x40000000U, 0x42080000U, 0x02080100U,
127*0Sstevel@tonic-gate 0x40080100U, 0x00000100U, 0x02000000U, 0x42080000U,
128*0Sstevel@tonic-gate 0x42080100U, 0x00080100U, 0x42000000U, 0x42080100U,
129*0Sstevel@tonic-gate 0x02080000U, 0x00000000U, 0x40080000U, 0x42000000U,
130*0Sstevel@tonic-gate 0x00080100U, 0x02000100U, 0x40000100U, 0x00080000U,
131*0Sstevel@tonic-gate 0x00000000U, 0x40080000U, 0x02080100U, 0x40000100U,
132*0Sstevel@tonic-gate 0x20000010U, 0x20400000U, 0x00004000U, 0x20404010U,
133*0Sstevel@tonic-gate 0x20400000U, 0x00000010U, 0x20404010U, 0x00400000U,
134*0Sstevel@tonic-gate 0x20004000U, 0x00404010U, 0x00400000U, 0x20000010U,
135*0Sstevel@tonic-gate 0x00400010U, 0x20004000U, 0x20000000U, 0x00004010U,
136*0Sstevel@tonic-gate 0x00000000U, 0x00400010U, 0x20004010U, 0x00004000U,
137*0Sstevel@tonic-gate 0x00404000U, 0x20004010U, 0x00000010U, 0x20400010U,
138*0Sstevel@tonic-gate 0x20400010U, 0x00000000U, 0x00404010U, 0x20404000U,
139*0Sstevel@tonic-gate 0x00004010U, 0x00404000U, 0x20404000U, 0x20000000U,
140*0Sstevel@tonic-gate 0x20004000U, 0x00000010U, 0x20400010U, 0x00404000U,
141*0Sstevel@tonic-gate 0x20404010U, 0x00400000U, 0x00004010U, 0x20000010U,
142*0Sstevel@tonic-gate 0x00400000U, 0x20004000U, 0x20000000U, 0x00004010U,
143*0Sstevel@tonic-gate 0x20000010U, 0x20404010U, 0x00404000U, 0x20400000U,
144*0Sstevel@tonic-gate 0x00404010U, 0x20404000U, 0x00000000U, 0x20400010U,
145*0Sstevel@tonic-gate 0x00000010U, 0x00004000U, 0x20400000U, 0x00404010U,
146*0Sstevel@tonic-gate 0x00004000U, 0x00400010U, 0x20004010U, 0x00000000U,
147*0Sstevel@tonic-gate 0x20404000U, 0x20000000U, 0x00400010U, 0x20004010U,
148*0Sstevel@tonic-gate 0x00200000U, 0x04200002U, 0x04000802U, 0x00000000U,
149*0Sstevel@tonic-gate 0x00000800U, 0x04000802U, 0x00200802U, 0x04200800U,
150*0Sstevel@tonic-gate 0x04200802U, 0x00200000U, 0x00000000U, 0x04000002U,
151*0Sstevel@tonic-gate 0x00000002U, 0x04000000U, 0x04200002U, 0x00000802U,
152*0Sstevel@tonic-gate 0x04000800U, 0x00200802U, 0x00200002U, 0x04000800U,
153*0Sstevel@tonic-gate 0x04000002U, 0x04200000U, 0x04200800U, 0x00200002U,
154*0Sstevel@tonic-gate 0x04200000U, 0x00000800U, 0x00000802U, 0x04200802U,
155*0Sstevel@tonic-gate 0x00200800U, 0x00000002U, 0x04000000U, 0x00200800U,
156*0Sstevel@tonic-gate 0x04000000U, 0x00200800U, 0x00200000U, 0x04000802U,
157*0Sstevel@tonic-gate 0x04000802U, 0x04200002U, 0x04200002U, 0x00000002U,
158*0Sstevel@tonic-gate 0x00200002U, 0x04000000U, 0x04000800U, 0x00200000U,
159*0Sstevel@tonic-gate 0x04200800U, 0x00000802U, 0x00200802U, 0x04200800U,
160*0Sstevel@tonic-gate 0x00000802U, 0x04000002U, 0x04200802U, 0x04200000U,
161*0Sstevel@tonic-gate 0x00200800U, 0x00000000U, 0x00000002U, 0x04200802U,
162*0Sstevel@tonic-gate 0x00000000U, 0x00200802U, 0x04200000U, 0x00000800U,
163*0Sstevel@tonic-gate 0x04000002U, 0x04000800U, 0x00000800U, 0x00200002U,
164*0Sstevel@tonic-gate 0x10001040U, 0x00001000U, 0x00040000U, 0x10041040U,
165*0Sstevel@tonic-gate 0x10000000U, 0x10001040U, 0x00000040U, 0x10000000U,
166*0Sstevel@tonic-gate 0x00040040U, 0x10040000U, 0x10041040U, 0x00041000U,
167*0Sstevel@tonic-gate 0x10041000U, 0x00041040U, 0x00001000U, 0x00000040U,
168*0Sstevel@tonic-gate 0x10040000U, 0x10000040U, 0x10001000U, 0x00001040U,
169*0Sstevel@tonic-gate 0x00041000U, 0x00040040U, 0x10040040U, 0x10041000U,
170*0Sstevel@tonic-gate 0x00001040U, 0x00000000U, 0x00000000U, 0x10040040U,
171*0Sstevel@tonic-gate 0x10000040U, 0x10001000U, 0x00041040U, 0x00040000U,
172*0Sstevel@tonic-gate 0x00041040U, 0x00040000U, 0x10041000U, 0x00001000U,
173*0Sstevel@tonic-gate 0x00000040U, 0x10040040U, 0x00001000U, 0x00041040U,
174*0Sstevel@tonic-gate 0x10001000U, 0x00000040U, 0x10000040U, 0x10040000U,
175*0Sstevel@tonic-gate 0x10040040U, 0x10000000U, 0x00040000U, 0x10001040U,
176*0Sstevel@tonic-gate 0x00000000U, 0x10041040U, 0x00040040U, 0x10000040U,
177*0Sstevel@tonic-gate 0x10040000U, 0x10001000U, 0x10001040U, 0x00000000U,
178*0Sstevel@tonic-gate 0x10041040U, 0x00041000U, 0x00041000U, 0x00001040U,
179*0Sstevel@tonic-gate 0x00001040U, 0x00040040U, 0x10000000U, 0x10041000U,
180*0Sstevel@tonic-gate };
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate /*
183*0Sstevel@tonic-gate * Primitive function F.
184*0Sstevel@tonic-gate * Input is r, subkey array in keys, output is XORed into l.
185*0Sstevel@tonic-gate * Each round consumes eight 6-bit subkeys, one for
186*0Sstevel@tonic-gate * each of the 8 S-boxes, 2 longs for each round.
187*0Sstevel@tonic-gate * Each long contains four 6-bit subkeys, each taking up a byte.
188*0Sstevel@tonic-gate * The first long contains, from high to low end, the subkeys for
189*0Sstevel@tonic-gate * S-boxes 1, 3, 5 & 7; the second contains the subkeys for S-boxes
190*0Sstevel@tonic-gate * 2, 4, 6 & 8 (using the origin-1 S-box numbering in the standard,
191*0Sstevel@tonic-gate * not the origin-0 numbering used elsewhere in this code)
192*0Sstevel@tonic-gate * See comments elsewhere about the pre-rotated values of r and Spbox.
193*0Sstevel@tonic-gate */
194*0Sstevel@tonic-gate #define F(l, r, key) {\
195*0Sstevel@tonic-gate work = ((r >> 4) | (r << 28)) ^ (key)[0];\
196*0Sstevel@tonic-gate l ^= Spbox[6][work & 0x3f];\
197*0Sstevel@tonic-gate l ^= Spbox[4][(work >> 8) & 0x3f];\
198*0Sstevel@tonic-gate l ^= Spbox[2][(work >> 16) & 0x3f];\
199*0Sstevel@tonic-gate l ^= Spbox[0][(work >> 24) & 0x3f];\
200*0Sstevel@tonic-gate work = r ^ (key)[1];\
201*0Sstevel@tonic-gate l ^= Spbox[7][work & 0x3f];\
202*0Sstevel@tonic-gate l ^= Spbox[5][(work >> 8) & 0x3f];\
203*0Sstevel@tonic-gate l ^= Spbox[3][(work >> 16) & 0x3f];\
204*0Sstevel@tonic-gate l ^= Spbox[1][(work >> 24) & 0x3f];\
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate /* Encrypt or decrypt a block of data in ECB mode */
208*0Sstevel@tonic-gate void
des(void * cookie,uint8_t * block)209*0Sstevel@tonic-gate des(void *cookie, uint8_t *block)
210*0Sstevel@tonic-gate {
211*0Sstevel@tonic-gate uint32_t *ks = (uint32_t *)cookie;
212*0Sstevel@tonic-gate uint32_t left;
213*0Sstevel@tonic-gate uint32_t right;
214*0Sstevel@tonic-gate uint32_t work;
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate /* Read input block and place in left/right in big-endian order */
217*0Sstevel@tonic-gate left = ((uint32_t)block[0] << 24) |
218*0Sstevel@tonic-gate ((uint32_t)block[1] << 16) |
219*0Sstevel@tonic-gate ((uint32_t)block[2] << 8) |
220*0Sstevel@tonic-gate (uint32_t)block[3];
221*0Sstevel@tonic-gate right = ((uint32_t)block[4] << 24) |
222*0Sstevel@tonic-gate ((uint32_t)block[5] << 16) |
223*0Sstevel@tonic-gate ((uint32_t)block[6] << 8) |
224*0Sstevel@tonic-gate (uint32_t)block[7];
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate /*
227*0Sstevel@tonic-gate * Hoey's clever initial permutation algorithm, from Outerbridge
228*0Sstevel@tonic-gate * (see Schneier p 478)
229*0Sstevel@tonic-gate *
230*0Sstevel@tonic-gate * The convention here is the same as Outerbridge: rotate each
231*0Sstevel@tonic-gate * register left by 1 bit, i.e., so that "left" contains permuted
232*0Sstevel@tonic-gate * input bits 2, 3, 4, ... 1 and "right" contains 33, 34, 35, ... 32
233*0Sstevel@tonic-gate * (using origin-1 numbering as in the FIPS). This allows us to avoid
234*0Sstevel@tonic-gate * one of the two rotates that would otherwise be required in each of
235*0Sstevel@tonic-gate * the 16 rounds.
236*0Sstevel@tonic-gate */
237*0Sstevel@tonic-gate work = ((left >> 4) ^ right) & 0x0f0f0f0f;
238*0Sstevel@tonic-gate right ^= work;
239*0Sstevel@tonic-gate left ^= work << 4;
240*0Sstevel@tonic-gate work = ((left >> 16) ^ right) & 0xffff;
241*0Sstevel@tonic-gate right ^= work;
242*0Sstevel@tonic-gate left ^= work << 16;
243*0Sstevel@tonic-gate work = ((right >> 2) ^ left) & 0x33333333;
244*0Sstevel@tonic-gate left ^= work;
245*0Sstevel@tonic-gate right ^= (work << 2);
246*0Sstevel@tonic-gate work = ((right >> 8) ^ left) & 0xff00ff;
247*0Sstevel@tonic-gate left ^= work;
248*0Sstevel@tonic-gate right ^= (work << 8);
249*0Sstevel@tonic-gate right = (right << 1) | (right >> 31);
250*0Sstevel@tonic-gate work = (left ^ right) & 0xaaaaaaaa;
251*0Sstevel@tonic-gate left ^= work;
252*0Sstevel@tonic-gate right ^= work;
253*0Sstevel@tonic-gate left = (left << 1) | (left >> 31);
254*0Sstevel@tonic-gate
255*0Sstevel@tonic-gate /* First key */
256*0Sstevel@tonic-gate F(left, right, ks);
257*0Sstevel@tonic-gate F(right, left, ks + 2);
258*0Sstevel@tonic-gate F(left, right, ks + 4);
259*0Sstevel@tonic-gate F(right, left, ks + 6);
260*0Sstevel@tonic-gate F(left, right, ks + 8);
261*0Sstevel@tonic-gate F(right, left, ks + 10);
262*0Sstevel@tonic-gate F(left, right, ks + 12);
263*0Sstevel@tonic-gate F(right, left, ks + 14);
264*0Sstevel@tonic-gate F(left, right, ks + 16);
265*0Sstevel@tonic-gate F(right, left, ks + 18);
266*0Sstevel@tonic-gate F(left, right, ks + 20);
267*0Sstevel@tonic-gate F(right, left, ks + 22);
268*0Sstevel@tonic-gate F(left, right, ks + 24);
269*0Sstevel@tonic-gate F(right, left, ks + 26);
270*0Sstevel@tonic-gate F(left, right, ks + 28);
271*0Sstevel@tonic-gate F(right, left, ks + 30);
272*0Sstevel@tonic-gate
273*0Sstevel@tonic-gate /* Inverse permutation, also from Hoey via Outerbridge and Schneier */
274*0Sstevel@tonic-gate right = (right << 31) | (right >> 1);
275*0Sstevel@tonic-gate work = (left ^ right) & 0xaaaaaaaa;
276*0Sstevel@tonic-gate left ^= work;
277*0Sstevel@tonic-gate right ^= work;
278*0Sstevel@tonic-gate left = (left >> 1) | (left << 31);
279*0Sstevel@tonic-gate work = ((left >> 8) ^ right) & 0xff00ff;
280*0Sstevel@tonic-gate right ^= work;
281*0Sstevel@tonic-gate left ^= work << 8;
282*0Sstevel@tonic-gate work = ((left >> 2) ^ right) & 0x33333333;
283*0Sstevel@tonic-gate right ^= work;
284*0Sstevel@tonic-gate left ^= work << 2;
285*0Sstevel@tonic-gate work = ((right >> 16) ^ left) & 0xffff;
286*0Sstevel@tonic-gate left ^= work;
287*0Sstevel@tonic-gate right ^= work << 16;
288*0Sstevel@tonic-gate work = ((right >> 4) ^ left) & 0x0f0f0f0f;
289*0Sstevel@tonic-gate left ^= work;
290*0Sstevel@tonic-gate right ^= work << 4;
291*0Sstevel@tonic-gate
292*0Sstevel@tonic-gate /* Put the block back into the user's buffer with final swap */
293*0Sstevel@tonic-gate block[0] = right >> 24;
294*0Sstevel@tonic-gate block[1] = right >> 16;
295*0Sstevel@tonic-gate block[2] = right >> 8;
296*0Sstevel@tonic-gate block[3] = right;
297*0Sstevel@tonic-gate block[4] = left >> 24;
298*0Sstevel@tonic-gate block[5] = left >> 16;
299*0Sstevel@tonic-gate block[6] = left >> 8;
300*0Sstevel@tonic-gate block[7] = left;
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate
303*0Sstevel@tonic-gate /* Key schedule-related tables from FIPS-46 */
304*0Sstevel@tonic-gate
305*0Sstevel@tonic-gate /* permuted choice table (key) */
306*0Sstevel@tonic-gate static unsigned char pc1[] = {
307*0Sstevel@tonic-gate 57, 49, 41, 33, 25, 17, 9,
308*0Sstevel@tonic-gate 1, 58, 50, 42, 34, 26, 18,
309*0Sstevel@tonic-gate 10, 2, 59, 51, 43, 35, 27,
310*0Sstevel@tonic-gate 19, 11, 3, 60, 52, 44, 36,
311*0Sstevel@tonic-gate 63, 55, 47, 39, 31, 23, 15,
312*0Sstevel@tonic-gate 7, 62, 54, 46, 38, 30, 22,
313*0Sstevel@tonic-gate 14, 6, 61, 53, 45, 37, 29,
314*0Sstevel@tonic-gate 21, 13, 5, 28, 20, 12, 4
315*0Sstevel@tonic-gate };
316*0Sstevel@tonic-gate
317*0Sstevel@tonic-gate /* number left rotations of pc1 */
318*0Sstevel@tonic-gate static unsigned char totrot[] = {
319*0Sstevel@tonic-gate 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28
320*0Sstevel@tonic-gate };
321*0Sstevel@tonic-gate
322*0Sstevel@tonic-gate /* permuted choice key (table) */
323*0Sstevel@tonic-gate static unsigned char pc2[] = {
324*0Sstevel@tonic-gate 14, 17, 11, 24, 1, 5,
325*0Sstevel@tonic-gate 3, 28, 15, 6, 21, 10,
326*0Sstevel@tonic-gate 23, 19, 12, 4, 26, 8,
327*0Sstevel@tonic-gate 16, 7, 27, 20, 13, 2,
328*0Sstevel@tonic-gate 41, 52, 31, 37, 47, 55,
329*0Sstevel@tonic-gate 30, 40, 51, 45, 33, 48,
330*0Sstevel@tonic-gate 44, 49, 39, 56, 34, 53,
331*0Sstevel@tonic-gate 46, 42, 50, 36, 29, 32
332*0Sstevel@tonic-gate };
333*0Sstevel@tonic-gate
334*0Sstevel@tonic-gate /* End of DES-defined tables */
335*0Sstevel@tonic-gate
336*0Sstevel@tonic-gate
337*0Sstevel@tonic-gate /* bit 0 is left-most in byte */
338*0Sstevel@tonic-gate static int bytebit[] = {
339*0Sstevel@tonic-gate 0200, 0100, 040, 020, 010, 04, 02, 01
340*0Sstevel@tonic-gate };
341*0Sstevel@tonic-gate
342*0Sstevel@tonic-gate /*
343*0Sstevel@tonic-gate * Generate key schedule for encryption or decryption
344*0Sstevel@tonic-gate * depending on the value of "decrypt"
345*0Sstevel@tonic-gate */
346*0Sstevel@tonic-gate void
des_key(DES_KS k,const unsigned char * key,int decrypt)347*0Sstevel@tonic-gate des_key(DES_KS k, const unsigned char *key, int decrypt)
348*0Sstevel@tonic-gate {
349*0Sstevel@tonic-gate unsigned char pc1m[56]; /* place to modify pc1 into */
350*0Sstevel@tonic-gate unsigned char pcr[56]; /* place to rotate pc1 into */
351*0Sstevel@tonic-gate int i;
352*0Sstevel@tonic-gate int j;
353*0Sstevel@tonic-gate int l;
354*0Sstevel@tonic-gate int m;
355*0Sstevel@tonic-gate unsigned char ks[8];
356*0Sstevel@tonic-gate
357*0Sstevel@tonic-gate for (j = 0; j < 56; j++) { /* convert pc1 to bits of key */
358*0Sstevel@tonic-gate l = pc1[j] - 1; /* integer bit location */
359*0Sstevel@tonic-gate m = l & 07; /* find bit */
360*0Sstevel@tonic-gate pc1m[j] = (key[l >>3 ] /* find which key byte l is in */
361*0Sstevel@tonic-gate & bytebit[m]) /* and which bit of that byte */
362*0Sstevel@tonic-gate ? 1 : 0; /* and store 1-bit result */
363*0Sstevel@tonic-gate }
364*0Sstevel@tonic-gate for (i = 0; i < 16; i++) { /* key chunk for each iteration */
365*0Sstevel@tonic-gate bzero(ks, sizeof (ks)); /* Clear key schedule */
366*0Sstevel@tonic-gate for (j = 0; j < 56; j++) /* rotate pc1 the right amount */
367*0Sstevel@tonic-gate pcr[j] = pc1m[(l = j + totrot[decrypt ? 15 - i : i]) <
368*0Sstevel@tonic-gate (j < 28 ? 28 : 56) ? l : l - 28];
369*0Sstevel@tonic-gate /* rotate left and right halves independently */
370*0Sstevel@tonic-gate for (j = 0; j < 48; j++) { /* select bits individually */
371*0Sstevel@tonic-gate /* check bit that goes to ks[j] */
372*0Sstevel@tonic-gate if (pcr[pc2[j] - 1]) {
373*0Sstevel@tonic-gate /* mask it in if it's there */
374*0Sstevel@tonic-gate l = j % 6;
375*0Sstevel@tonic-gate ks[j/6] |= bytebit[l] >> 2;
376*0Sstevel@tonic-gate }
377*0Sstevel@tonic-gate }
378*0Sstevel@tonic-gate /* Now convert to packed odd/even interleaved form */
379*0Sstevel@tonic-gate k[i][0] = ((uint32_t)ks[0] << 24) |
380*0Sstevel@tonic-gate ((uint32_t)ks[2] << 16) |
381*0Sstevel@tonic-gate ((uint32_t)ks[4] << 8) |
382*0Sstevel@tonic-gate ((uint32_t)ks[6]);
383*0Sstevel@tonic-gate k[i][1] = ((uint32_t)ks[1] << 24) |
384*0Sstevel@tonic-gate ((uint32_t)ks[3] << 16) |
385*0Sstevel@tonic-gate ((uint32_t)ks[5] << 8) |
386*0Sstevel@tonic-gate ((uint32_t)ks[7]);
387*0Sstevel@tonic-gate }
388*0Sstevel@tonic-gate }
389