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 2004 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 #include <sys/types.h>
30 #include <sys/systm.h>
31 #include <sys/ddi.h>
32 #include <sys/sysmacros.h>
33 #include <sys/strsun.h>
34 #include <sys/note.h>
35 #include <sys/crypto/common.h>
36 #include "des_impl.h"
37 #ifndef	_KERNEL
38 #include <strings.h>
39 #include <stdlib.h>
40 #endif	/* !_KERNEL */
41 
42 /* EXPORT DELETE START */
43 
44 typedef struct keysched_s {
45 	uint64_t ksch_encrypt[16];
46 	uint64_t ksch_decrypt[16];
47 } keysched_t;
48 
49 typedef struct keysched3_s {
50 	uint64_t ksch_encrypt[48];
51 	uint64_t ksch_decrypt[48];
52 } keysched3_t;
53 
54 static void fix_des_parity(uint64_t *);
55 
56 #ifndef sun4u
57 
58 static const uint64_t sbox_table[8][64]=
59 {
60 {
61 0x0000140140020000, 0x0000000000000000, 0x0000000140000000, 0x0000140140020020,
62 0x0000140140000020, 0x0000000140020020, 0x0000000000000020, 0x0000000140000000,
63 0x0000000000020000, 0x0000140140020000, 0x0000140140020020, 0x0000000000020000,
64 0x0000140000020020, 0x0000140140000020, 0x0000140000000000, 0x0000000000000020,
65 0x0000000000020020, 0x0000140000020000, 0x0000140000020000, 0x0000000140020000,
66 0x0000000140020000, 0x0000140140000000, 0x0000140140000000, 0x0000140000020020,
67 0x0000000140000020, 0x0000140000000020, 0x0000140000000020, 0x0000000140000020,
68 0x0000000000000000, 0x0000000000020020, 0x0000000140020020, 0x0000140000000000,
69 0x0000000140000000, 0x0000140140020020, 0x0000000000000020, 0x0000140140000000,
70 0x0000140140020000, 0x0000140000000000, 0x0000140000000000, 0x0000000000020000,
71 0x0000140140000020, 0x0000000140000000, 0x0000000140020000, 0x0000140000000020,
72 0x0000000000020000, 0x0000000000000020, 0x0000140000020020, 0x0000000140020020,
73 0x0000140140020020, 0x0000000140000020, 0x0000140140000000, 0x0000140000020020,
74 0x0000140000000020, 0x0000000000020020, 0x0000000140020020, 0x0000140140020000,
75 0x0000000000020020, 0x0000140000020000, 0x0000140000020000, 0x0000000000000000,
76 0x0000000140000020, 0x0000000140020000, 0x0000000000000000, 0x0000140140000020
77 },
78 {
79 0x2000005020000500, 0x2000000020000000, 0x0000000020000000, 0x0000005020000500,
80 0x0000005000000000, 0x0000000000000500, 0x2000005000000500, 0x2000000020000500,
81 0x2000000000000500, 0x2000005020000500, 0x2000005020000000, 0x2000000000000000,
82 0x2000000020000000, 0x0000005000000000, 0x0000000000000500, 0x2000005000000500,
83 0x0000005020000000, 0x0000005000000500, 0x2000000020000500, 0x0000000000000000,
84 0x2000000000000000, 0x0000000020000000, 0x0000005020000500, 0x2000005000000000,
85 0x0000005000000500, 0x2000000000000500, 0x0000000000000000, 0x0000005020000000,
86 0x0000000020000500, 0x2000005020000000, 0x2000005000000000, 0x0000000020000500,
87 0x0000000000000000, 0x0000005020000500, 0x2000005000000500, 0x0000005000000000,
88 0x2000000020000500, 0x2000005000000000, 0x2000005020000000, 0x0000000020000000,
89 0x2000005000000000, 0x2000000020000000, 0x0000000000000500, 0x2000005020000500,
90 0x0000005020000500, 0x0000000000000500, 0x0000000020000000, 0x2000000000000000,
91 0x0000000020000500, 0x2000005020000000, 0x0000005000000000, 0x2000000000000500,
92 0x0000005000000500, 0x2000000020000500, 0x2000000000000500, 0x0000005000000500,
93 0x0000005020000000, 0x0000000000000000, 0x2000000020000000, 0x0000000020000500,
94 0x2000000000000000, 0x2000005000000500, 0x2000005020000500, 0x0000005020000000
95 },
96 {
97 0x0000000000014040, 0x0000800280014000, 0x0000000000000000, 0x0000800280000040,
98 0x0000800000014000, 0x0000000000000000, 0x0000000280014040, 0x0000800000014000,
99 0x0000000280000040, 0x0000800000000040, 0x0000800000000040, 0x0000000280000000,
100 0x0000800280014040, 0x0000000280000040, 0x0000800280000000, 0x0000000000014040,
101 0x0000800000000000, 0x0000000000000040, 0x0000800280014000, 0x0000000000014000,
102 0x0000000280014000, 0x0000800280000000, 0x0000800280000040, 0x0000000280014040,
103 0x0000800000014040, 0x0000000280014000, 0x0000000280000000, 0x0000800000014040,
104 0x0000000000000040, 0x0000800280014040, 0x0000000000014000, 0x0000800000000000,
105 0x0000800280014000, 0x0000800000000000, 0x0000000280000040, 0x0000000000014040,
106 0x0000000280000000, 0x0000800280014000, 0x0000800000014000, 0x0000000000000000,
107 0x0000000000014000, 0x0000000280000040, 0x0000800280014040, 0x0000800000014000,
108 0x0000800000000040, 0x0000000000014000, 0x0000000000000000, 0x0000800280000040,
109 0x0000800000014040, 0x0000000280000000, 0x0000800000000000, 0x0000800280014040,
110 0x0000000000000040, 0x0000000280014040, 0x0000000280014000, 0x0000800000000040,
111 0x0000800280000000, 0x0000800000014040, 0x0000000000014040, 0x0000800280000000,
112 0x0000000280014040, 0x0000000000000040, 0x0000800280000040, 0x0000000280014000
113 },
114 {
115 0x4000020008100008, 0x4000000008101008, 0x4000000008101008, 0x0000000000001000,
116 0x0000020008101000, 0x4000020000001008, 0x4000020000000008, 0x4000000008100008,
117 0x0000000000000000, 0x0000020008100000, 0x0000020008100000, 0x4000020008101008,
118 0x4000000000001008, 0x0000000000000000, 0x0000020000001000, 0x4000020000000008,
119 0x4000000000000008, 0x0000000008100000, 0x0000020000000000, 0x4000020008100008,
120 0x0000000000001000, 0x0000020000000000, 0x4000000008100008, 0x0000000008101000,
121 0x4000020000001008, 0x4000000000000008, 0x0000000008101000, 0x0000020000001000,
122 0x0000000008100000, 0x0000020008101000, 0x4000020008101008, 0x4000000000001008,
123 0x0000020000001000, 0x4000020000000008, 0x0000020008100000, 0x4000020008101008,
124 0x4000000000001008, 0x0000000000000000, 0x0000000000000000, 0x0000020008100000,
125 0x0000000008101000, 0x0000020000001000, 0x4000020000001008, 0x4000000000000008,
126 0x4000020008100008, 0x4000000008101008, 0x4000000008101008, 0x0000000000001000,
127 0x4000020008101008, 0x4000000000001008, 0x4000000000000008, 0x0000000008100000,
128 0x4000020000000008, 0x4000000008100008, 0x0000020008101000, 0x4000020000001008,
129 0x4000000008100008, 0x0000000008101000, 0x0000020000000000, 0x4000020008100008,
130 0x0000000000001000, 0x0000020000000000, 0x0000000008100000, 0x0000020008101000
131 },
132 {
133 0x000000000000a000, 0x000028080000a000, 0x0000280800000000, 0x100028000000a000,
134 0x0000000800000000, 0x000000000000a000, 0x1000000000000000, 0x0000280800000000,
135 0x100000080000a000, 0x0000000800000000, 0x000028000000a000, 0x100000080000a000,
136 0x100028000000a000, 0x1000280800000000, 0x000000080000a000, 0x1000000000000000,
137 0x0000280000000000, 0x1000000800000000, 0x1000000800000000, 0x0000000000000000,
138 0x100000000000a000, 0x100028080000a000, 0x100028080000a000, 0x000028000000a000,
139 0x1000280800000000, 0x100000000000a000, 0x0000000000000000, 0x1000280000000000,
140 0x000028080000a000, 0x0000280000000000, 0x1000280000000000, 0x000000080000a000,
141 0x0000000800000000, 0x100028000000a000, 0x000000000000a000, 0x0000280000000000,
142 0x1000000000000000, 0x0000280800000000, 0x100028000000a000, 0x100000080000a000,
143 0x000028000000a000, 0x1000000000000000, 0x1000280800000000, 0x000028080000a000,
144 0x100000080000a000, 0x000000000000a000, 0x0000280000000000, 0x1000280800000000,
145 0x100028080000a000, 0x000000080000a000, 0x1000280000000000, 0x100028080000a000,
146 0x0000280800000000, 0x0000000000000000, 0x1000000800000000, 0x1000280000000000,
147 0x000000080000a000, 0x000028000000a000, 0x100000000000a000, 0x0000000800000000,
148 0x0000000000000000, 0x1000000800000000, 0x000028080000a000, 0x100000000000a000
149 },
150 {
151 0x0802000000000280, 0x0802010000000000, 0x0000000010000000, 0x0802010010000280,
152 0x0802010000000000, 0x0000000000000280, 0x0802010010000280, 0x0000010000000000,
153 0x0802000010000000, 0x0000010010000280, 0x0000010000000000, 0x0802000000000280,
154 0x0000010000000280, 0x0802000010000000, 0x0802000000000000, 0x0000000010000280,
155 0x0000000000000000, 0x0000010000000280, 0x0802000010000280, 0x0000000010000000,
156 0x0000010010000000, 0x0802000010000280, 0x0000000000000280, 0x0802010000000280,
157 0x0802010000000280, 0x0000000000000000, 0x0000010010000280, 0x0802010010000000,
158 0x0000000010000280, 0x0000010010000000, 0x0802010010000000, 0x0802000000000000,
159 0x0802000010000000, 0x0000000000000280, 0x0802010000000280, 0x0000010010000000,
160 0x0802010010000280, 0x0000010000000000, 0x0000000010000280, 0x0802000000000280,
161 0x0000010000000000, 0x0802000010000000, 0x0802000000000000, 0x0000000010000280,
162 0x0802000000000280, 0x0802010010000280, 0x0000010010000000, 0x0802010000000000,
163 0x0000010010000280, 0x0802010010000000, 0x0000000000000000, 0x0802010000000280,
164 0x0000000000000280, 0x0000000010000000, 0x0802010000000000, 0x0000010010000280,
165 0x0000000010000000, 0x0000010000000280, 0x0802000010000280, 0x0000000000000000,
166 0x0802010010000000, 0x0802000000000000, 0x0000010000000280, 0x0802000010000280
167 },
168 {
169 0x000000a000000000, 0x800040a000000010, 0x8000400000040010, 0x0000000000000000,
170 0x0000000000040000, 0x8000400000040010, 0x800000a000040010, 0x000040a000040000,
171 0x800040a000040010, 0x000000a000000000, 0x0000000000000000, 0x8000400000000010,
172 0x8000000000000010, 0x0000400000000000, 0x800040a000000010, 0x8000000000040010,
173 0x0000400000040000, 0x800000a000040010, 0x800000a000000010, 0x0000400000040000,
174 0x8000400000000010, 0x000040a000000000, 0x000040a000040000, 0x800000a000000010,
175 0x000040a000000000, 0x0000000000040000, 0x8000000000040010, 0x800040a000040010,
176 0x000000a000040000, 0x8000000000000010, 0x0000400000000000, 0x000000a000040000,
177 0x0000400000000000, 0x000000a000040000, 0x000000a000000000, 0x8000400000040010,
178 0x8000400000040010, 0x800040a000000010, 0x800040a000000010, 0x8000000000000010,
179 0x800000a000000010, 0x0000400000000000, 0x0000400000040000, 0x000000a000000000,
180 0x000040a000040000, 0x8000000000040010, 0x800000a000040010, 0x000040a000040000,
181 0x8000000000040010, 0x8000400000000010, 0x800040a000040010, 0x000040a000000000,
182 0x000000a000040000, 0x0000000000000000, 0x8000000000000010, 0x800040a000040010,
183 0x0000000000000000, 0x800000a000040010, 0x000040a000000000, 0x0000000000040000,
184 0x8000400000000010, 0x0000400000040000, 0x0000000000040000, 0x800000a000000010
185 },
186 {
187 0x0401000004080800, 0x0000000004080000, 0x0000000400000000, 0x0401000404080800,
188 0x0401000000000000, 0x0401000004080800, 0x0000000000000800, 0x0401000000000000,
189 0x0000000400000800, 0x0401000400000000, 0x0401000404080800, 0x0000000404080000,
190 0x0401000404080000, 0x0000000404080800, 0x0000000004080000, 0x0000000000000800,
191 0x0401000400000000, 0x0401000000000800, 0x0401000004080000, 0x0000000004080800,
192 0x0000000404080000, 0x0000000400000800, 0x0401000400000800, 0x0401000404080000,
193 0x0000000004080800, 0x0000000000000000, 0x0000000000000000, 0x0401000400000800,
194 0x0401000000000800, 0x0401000004080000, 0x0000000404080800, 0x0000000400000000,
195 0x0000000404080800, 0x0000000400000000, 0x0401000404080000, 0x0000000004080000,
196 0x0000000000000800, 0x0401000400000800, 0x0000000004080000, 0x0000000404080800,
197 0x0401000004080000, 0x0000000000000800, 0x0401000000000800, 0x0401000400000000,
198 0x0401000400000800, 0x0401000000000000, 0x0000000400000000, 0x0401000004080800,
199 0x0000000000000000, 0x0401000404080800, 0x0000000400000800, 0x0401000000000800,
200 0x0401000400000000, 0x0401000004080000, 0x0401000004080800, 0x0000000000000000,
201 0x0401000404080800, 0x0000000404080000, 0x0000000404080000, 0x0000000004080800,
202 0x0000000004080800, 0x0000000400000800, 0x0401000000000000, 0x0401000404080000
203 }
204 };
205 
206 
207 static const uint64_t ip_table[2][256]=
208 {
209 {
210 0x0000000000000000, 0x0000000000000400, 0x0080000000000280, 0x0080000000000680,
211 0x0000000000400000, 0x0000000000400400, 0x0080000000400280, 0x0080000000400680,
212 0x0000000000280000, 0x0000000000280400, 0x0080000000280280, 0x0080000000280680,
213 0x0000000000680000, 0x0000000000680400, 0x0080000000680280, 0x0080000000680680,
214 0x0000000400000000, 0x0000000400000400, 0x0080000400000280, 0x0080000400000680,
215 0x0000000400400000, 0x0000000400400400, 0x0080000400400280, 0x0080000400400680,
216 0x0000000400280000, 0x0000000400280400, 0x0080000400280280, 0x0080000400280680,
217 0x0000000400680000, 0x0000000400680400, 0x0080000400680280, 0x0080000400680680,
218 0x0000000280000000, 0x0000000280000400, 0x0080000280000280, 0x0080000280000680,
219 0x0000000280400000, 0x0000000280400400, 0x0080000280400280, 0x0080000280400680,
220 0x0000000280280000, 0x0000000280280400, 0x0080000280280280, 0x0080000280280680,
221 0x0000000280680000, 0x0000000280680400, 0x0080000280680280, 0x0080000280680680,
222 0x0000000680000000, 0x0000000680000400, 0x0080000680000280, 0x0080000680000680,
223 0x0000000680400000, 0x0000000680400400, 0x0080000680400280, 0x0080000680400680,
224 0x0000000680280000, 0x0000000680280400, 0x0080000680280280, 0x0080000680280680,
225 0x0000000680680000, 0x0000000680680400, 0x0080000680680280, 0x0080000680680680,
226 0x0000400000000000, 0x0000400000000400, 0x0080400000000280, 0x0080400000000680,
227 0x0000400000400000, 0x0000400000400400, 0x0080400000400280, 0x0080400000400680,
228 0x0000400000280000, 0x0000400000280400, 0x0080400000280280, 0x0080400000280680,
229 0x0000400000680000, 0x0000400000680400, 0x0080400000680280, 0x0080400000680680,
230 0x0000400400000000, 0x0000400400000400, 0x0080400400000280, 0x0080400400000680,
231 0x0000400400400000, 0x0000400400400400, 0x0080400400400280, 0x0080400400400680,
232 0x0000400400280000, 0x0000400400280400, 0x0080400400280280, 0x0080400400280680,
233 0x0000400400680000, 0x0000400400680400, 0x0080400400680280, 0x0080400400680680,
234 0x0000400280000000, 0x0000400280000400, 0x0080400280000280, 0x0080400280000680,
235 0x0000400280400000, 0x0000400280400400, 0x0080400280400280, 0x0080400280400680,
236 0x0000400280280000, 0x0000400280280400, 0x0080400280280280, 0x0080400280280680,
237 0x0000400280680000, 0x0000400280680400, 0x0080400280680280, 0x0080400280680680,
238 0x0000400680000000, 0x0000400680000400, 0x0080400680000280, 0x0080400680000680,
239 0x0000400680400000, 0x0000400680400400, 0x0080400680400280, 0x0080400680400680,
240 0x0000400680280000, 0x0000400680280400, 0x0080400680280280, 0x0080400680280680,
241 0x0000400680680000, 0x0000400680680400, 0x0080400680680280, 0x0080400680680680,
242 0x0000280000000000, 0x0000280000000400, 0x0080280000000280, 0x0080280000000680,
243 0x0000280000400000, 0x0000280000400400, 0x0080280000400280, 0x0080280000400680,
244 0x0000280000280000, 0x0000280000280400, 0x0080280000280280, 0x0080280000280680,
245 0x0000280000680000, 0x0000280000680400, 0x0080280000680280, 0x0080280000680680,
246 0x0000280400000000, 0x0000280400000400, 0x0080280400000280, 0x0080280400000680,
247 0x0000280400400000, 0x0000280400400400, 0x0080280400400280, 0x0080280400400680,
248 0x0000280400280000, 0x0000280400280400, 0x0080280400280280, 0x0080280400280680,
249 0x0000280400680000, 0x0000280400680400, 0x0080280400680280, 0x0080280400680680,
250 0x0000280280000000, 0x0000280280000400, 0x0080280280000280, 0x0080280280000680,
251 0x0000280280400000, 0x0000280280400400, 0x0080280280400280, 0x0080280280400680,
252 0x0000280280280000, 0x0000280280280400, 0x0080280280280280, 0x0080280280280680,
253 0x0000280280680000, 0x0000280280680400, 0x0080280280680280, 0x0080280280680680,
254 0x0000280680000000, 0x0000280680000400, 0x0080280680000280, 0x0080280680000680,
255 0x0000280680400000, 0x0000280680400400, 0x0080280680400280, 0x0080280680400680,
256 0x0000280680280000, 0x0000280680280400, 0x0080280680280280, 0x0080280680280680,
257 0x0000280680680000, 0x0000280680680400, 0x0080280680680280, 0x0080280680680680,
258 0x0000680000000000, 0x0000680000000400, 0x0080680000000280, 0x0080680000000680,
259 0x0000680000400000, 0x0000680000400400, 0x0080680000400280, 0x0080680000400680,
260 0x0000680000280000, 0x0000680000280400, 0x0080680000280280, 0x0080680000280680,
261 0x0000680000680000, 0x0000680000680400, 0x0080680000680280, 0x0080680000680680,
262 0x0000680400000000, 0x0000680400000400, 0x0080680400000280, 0x0080680400000680,
263 0x0000680400400000, 0x0000680400400400, 0x0080680400400280, 0x0080680400400680,
264 0x0000680400280000, 0x0000680400280400, 0x0080680400280280, 0x0080680400280680,
265 0x0000680400680000, 0x0000680400680400, 0x0080680400680280, 0x0080680400680680,
266 0x0000680280000000, 0x0000680280000400, 0x0080680280000280, 0x0080680280000680,
267 0x0000680280400000, 0x0000680280400400, 0x0080680280400280, 0x0080680280400680,
268 0x0000680280280000, 0x0000680280280400, 0x0080680280280280, 0x0080680280280680,
269 0x0000680280680000, 0x0000680280680400, 0x0080680280680280, 0x0080680280680680,
270 0x0000680680000000, 0x0000680680000400, 0x0080680680000280, 0x0080680680000680,
271 0x0000680680400000, 0x0000680680400400, 0x0080680680400280, 0x0080680680400680,
272 0x0000680680280000, 0x0000680680280400, 0x0080680680280280, 0x0080680680280680,
273 0x0000680680680000, 0x0000680680680400, 0x0080680680680280, 0x0080680680680680
274 },
275 {
276 0x0000000000000000, 0x0000000000005000, 0x0000000000000800, 0x0000000000005800,
277 0x0000000005000000, 0x0000000005005000, 0x0000000005000800, 0x0000000005005800,
278 0x0000000000800000, 0x0000000000805000, 0x0000000000800800, 0x0000000000805800,
279 0x0000000005800000, 0x0000000005805000, 0x0000000005800800, 0x0000000005805800,
280 0x0000005000000000, 0x0000005000005000, 0x0000005000000800, 0x0000005000005800,
281 0x0000005005000000, 0x0000005005005000, 0x0000005005000800, 0x0000005005005800,
282 0x0000005000800000, 0x0000005000805000, 0x0000005000800800, 0x0000005000805800,
283 0x0000005005800000, 0x0000005005805000, 0x0000005005800800, 0x0000005005805800,
284 0x0000000800000000, 0x0000000800005000, 0x0000000800000800, 0x0000000800005800,
285 0x0000000805000000, 0x0000000805005000, 0x0000000805000800, 0x0000000805005800,
286 0x0000000800800000, 0x0000000800805000, 0x0000000800800800, 0x0000000800805800,
287 0x0000000805800000, 0x0000000805805000, 0x0000000805800800, 0x0000000805805800,
288 0x0000005800000000, 0x0000005800005000, 0x0000005800000800, 0x0000005800005800,
289 0x0000005805000000, 0x0000005805005000, 0x0000005805000800, 0x0000005805005800,
290 0x0000005800800000, 0x0000005800805000, 0x0000005800800800, 0x0000005800805800,
291 0x0000005805800000, 0x0000005805805000, 0x0000005805800800, 0x0000005805805800,
292 0x0005000000000004, 0x0005000000005004, 0x0005000000000804, 0x0005000000005804,
293 0x0005000005000004, 0x0005000005005004, 0x0005000005000804, 0x0005000005005804,
294 0x0005000000800004, 0x0005000000805004, 0x0005000000800804, 0x0005000000805804,
295 0x0005000005800004, 0x0005000005805004, 0x0005000005800804, 0x0005000005805804,
296 0x0005005000000004, 0x0005005000005004, 0x0005005000000804, 0x0005005000005804,
297 0x0005005005000004, 0x0005005005005004, 0x0005005005000804, 0x0005005005005804,
298 0x0005005000800004, 0x0005005000805004, 0x0005005000800804, 0x0005005000805804,
299 0x0005005005800004, 0x0005005005805004, 0x0005005005800804, 0x0005005005805804,
300 0x0005000800000004, 0x0005000800005004, 0x0005000800000804, 0x0005000800005804,
301 0x0005000805000004, 0x0005000805005004, 0x0005000805000804, 0x0005000805005804,
302 0x0005000800800004, 0x0005000800805004, 0x0005000800800804, 0x0005000800805804,
303 0x0005000805800004, 0x0005000805805004, 0x0005000805800804, 0x0005000805805804,
304 0x0005005800000004, 0x0005005800005004, 0x0005005800000804, 0x0005005800005804,
305 0x0005005805000004, 0x0005005805005004, 0x0005005805000804, 0x0005005805005804,
306 0x0005005800800004, 0x0005005800805004, 0x0005005800800804, 0x0005005800805804,
307 0x0005005805800004, 0x0005005805805004, 0x0005005805800804, 0x0005005805805804,
308 0x0000800000000000, 0x0000800000005000, 0x0000800000000800, 0x0000800000005800,
309 0x0000800005000000, 0x0000800005005000, 0x0000800005000800, 0x0000800005005800,
310 0x0000800000800000, 0x0000800000805000, 0x0000800000800800, 0x0000800000805800,
311 0x0000800005800000, 0x0000800005805000, 0x0000800005800800, 0x0000800005805800,
312 0x0000805000000000, 0x0000805000005000, 0x0000805000000800, 0x0000805000005800,
313 0x0000805005000000, 0x0000805005005000, 0x0000805005000800, 0x0000805005005800,
314 0x0000805000800000, 0x0000805000805000, 0x0000805000800800, 0x0000805000805800,
315 0x0000805005800000, 0x0000805005805000, 0x0000805005800800, 0x0000805005805800,
316 0x0000800800000000, 0x0000800800005000, 0x0000800800000800, 0x0000800800005800,
317 0x0000800805000000, 0x0000800805005000, 0x0000800805000800, 0x0000800805005800,
318 0x0000800800800000, 0x0000800800805000, 0x0000800800800800, 0x0000800800805800,
319 0x0000800805800000, 0x0000800805805000, 0x0000800805800800, 0x0000800805805800,
320 0x0000805800000000, 0x0000805800005000, 0x0000805800000800, 0x0000805800005800,
321 0x0000805805000000, 0x0000805805005000, 0x0000805805000800, 0x0000805805005800,
322 0x0000805800800000, 0x0000805800805000, 0x0000805800800800, 0x0000805800805800,
323 0x0000805805800000, 0x0000805805805000, 0x0000805805800800, 0x0000805805805800,
324 0x0005800000000004, 0x0005800000005004, 0x0005800000000804, 0x0005800000005804,
325 0x0005800005000004, 0x0005800005005004, 0x0005800005000804, 0x0005800005005804,
326 0x0005800000800004, 0x0005800000805004, 0x0005800000800804, 0x0005800000805804,
327 0x0005800005800004, 0x0005800005805004, 0x0005800005800804, 0x0005800005805804,
328 0x0005805000000004, 0x0005805000005004, 0x0005805000000804, 0x0005805000005804,
329 0x0005805005000004, 0x0005805005005004, 0x0005805005000804, 0x0005805005005804,
330 0x0005805000800004, 0x0005805000805004, 0x0005805000800804, 0x0005805000805804,
331 0x0005805005800004, 0x0005805005805004, 0x0005805005800804, 0x0005805005805804,
332 0x0005800800000004, 0x0005800800005004, 0x0005800800000804, 0x0005800800005804,
333 0x0005800805000004, 0x0005800805005004, 0x0005800805000804, 0x0005800805005804,
334 0x0005800800800004, 0x0005800800805004, 0x0005800800800804, 0x0005800800805804,
335 0x0005800805800004, 0x0005800805805004, 0x0005800805800804, 0x0005800805805804,
336 0x0005805800000004, 0x0005805800005004, 0x0005805800000804, 0x0005805800005804,
337 0x0005805805000004, 0x0005805805005004, 0x0005805805000804, 0x0005805805005804,
338 0x0005805800800004, 0x0005805800805004, 0x0005805800800804, 0x0005805800805804,
339 0x0005805805800004, 0x0005805805805004, 0x0005805805800804, 0x0005805805805804
340 }
341 };
342 
343 static const uint32_t fp_table[256]=
344 {
345 0x00000000, 0x80000000, 0x00800000, 0x80800000,
346 0x00008000, 0x80008000, 0x00808000, 0x80808000,
347 0x00000080, 0x80000080, 0x00800080, 0x80800080,
348 0x00008080, 0x80008080, 0x00808080, 0x80808080,
349 0x40000000, 0xc0000000, 0x40800000, 0xc0800000,
350 0x40008000, 0xc0008000, 0x40808000, 0xc0808000,
351 0x40000080, 0xc0000080, 0x40800080, 0xc0800080,
352 0x40008080, 0xc0008080, 0x40808080, 0xc0808080,
353 0x00400000, 0x80400000, 0x00c00000, 0x80c00000,
354 0x00408000, 0x80408000, 0x00c08000, 0x80c08000,
355 0x00400080, 0x80400080, 0x00c00080, 0x80c00080,
356 0x00408080, 0x80408080, 0x00c08080, 0x80c08080,
357 0x40400000, 0xc0400000, 0x40c00000, 0xc0c00000,
358 0x40408000, 0xc0408000, 0x40c08000, 0xc0c08000,
359 0x40400080, 0xc0400080, 0x40c00080, 0xc0c00080,
360 0x40408080, 0xc0408080, 0x40c08080, 0xc0c08080,
361 0x00004000, 0x80004000, 0x00804000, 0x80804000,
362 0x0000c000, 0x8000c000, 0x0080c000, 0x8080c000,
363 0x00004080, 0x80004080, 0x00804080, 0x80804080,
364 0x0000c080, 0x8000c080, 0x0080c080, 0x8080c080,
365 0x40004000, 0xc0004000, 0x40804000, 0xc0804000,
366 0x4000c000, 0xc000c000, 0x4080c000, 0xc080c000,
367 0x40004080, 0xc0004080, 0x40804080, 0xc0804080,
368 0x4000c080, 0xc000c080, 0x4080c080, 0xc080c080,
369 0x00404000, 0x80404000, 0x00c04000, 0x80c04000,
370 0x0040c000, 0x8040c000, 0x00c0c000, 0x80c0c000,
371 0x00404080, 0x80404080, 0x00c04080, 0x80c04080,
372 0x0040c080, 0x8040c080, 0x00c0c080, 0x80c0c080,
373 0x40404000, 0xc0404000, 0x40c04000, 0xc0c04000,
374 0x4040c000, 0xc040c000, 0x40c0c000, 0xc0c0c000,
375 0x40404080, 0xc0404080, 0x40c04080, 0xc0c04080,
376 0x4040c080, 0xc040c080, 0x40c0c080, 0xc0c0c080,
377 0x00000040, 0x80000040, 0x00800040, 0x80800040,
378 0x00008040, 0x80008040, 0x00808040, 0x80808040,
379 0x000000c0, 0x800000c0, 0x008000c0, 0x808000c0,
380 0x000080c0, 0x800080c0, 0x008080c0, 0x808080c0,
381 0x40000040, 0xc0000040, 0x40800040, 0xc0800040,
382 0x40008040, 0xc0008040, 0x40808040, 0xc0808040,
383 0x400000c0, 0xc00000c0, 0x408000c0, 0xc08000c0,
384 0x400080c0, 0xc00080c0, 0x408080c0, 0xc08080c0,
385 0x00400040, 0x80400040, 0x00c00040, 0x80c00040,
386 0x00408040, 0x80408040, 0x00c08040, 0x80c08040,
387 0x004000c0, 0x804000c0, 0x00c000c0, 0x80c000c0,
388 0x004080c0, 0x804080c0, 0x00c080c0, 0x80c080c0,
389 0x40400040, 0xc0400040, 0x40c00040, 0xc0c00040,
390 0x40408040, 0xc0408040, 0x40c08040, 0xc0c08040,
391 0x404000c0, 0xc04000c0, 0x40c000c0, 0xc0c000c0,
392 0x404080c0, 0xc04080c0, 0x40c080c0, 0xc0c080c0,
393 0x00004040, 0x80004040, 0x00804040, 0x80804040,
394 0x0000c040, 0x8000c040, 0x0080c040, 0x8080c040,
395 0x000040c0, 0x800040c0, 0x008040c0, 0x808040c0,
396 0x0000c0c0, 0x8000c0c0, 0x0080c0c0, 0x8080c0c0,
397 0x40004040, 0xc0004040, 0x40804040, 0xc0804040,
398 0x4000c040, 0xc000c040, 0x4080c040, 0xc080c040,
399 0x400040c0, 0xc00040c0, 0x408040c0, 0xc08040c0,
400 0x4000c0c0, 0xc000c0c0, 0x4080c0c0, 0xc080c0c0,
401 0x00404040, 0x80404040, 0x00c04040, 0x80c04040,
402 0x0040c040, 0x8040c040, 0x00c0c040, 0x80c0c040,
403 0x004040c0, 0x804040c0, 0x00c040c0, 0x80c040c0,
404 0x0040c0c0, 0x8040c0c0, 0x00c0c0c0, 0x80c0c0c0,
405 0x40404040, 0xc0404040, 0x40c04040, 0xc0c04040,
406 0x4040c040, 0xc040c040, 0x40c0c040, 0xc0c0c040,
407 0x404040c0, 0xc04040c0, 0x40c040c0, 0xc0c040c0,
408 0x4040c0c0, 0xc040c0c0, 0x40c0c0c0, 0xc0c0c0c0
409 };
410 
411 static const uint64_t all_a = 0xaaaaaaaaaaaaaaaaull;
412 static const uint64_t all_5 = 0x5555555555555555ull;
413 static const uint64_t top_1 = 0xfc000000000000ull;
414 static const uint64_t mid_4 = 0x3fffffc000000ull;
415 static const uint64_t low_3 = 0x3ffff00ull;
416 
417 
418 static void
419 des_ip(uint64_t *l, uint64_t *r, uint64_t pt)
420 {
421 	uint64_t a, b;
422 
423 	a = pt & all_a;
424 	b = pt & all_5;
425 	a = a | (a << 7);
426 	b = b | (b >> 7);
427 
428 	b = (ip_table[0][(b >> 48) & 255ull]) |
429 		(ip_table[1][(b >> 32) & 255ull]) |
430 		(ip_table[0][(b >> 16) & 255ull] << 6) |
431 		(ip_table[1][b & 255ull] << 6);
432 
433 	a = (ip_table[0][(a >> 56) & 255]) |
434 		(ip_table[1][(a >> 40) & 255]) |
435 		(ip_table[0][(a >> 24) & 255] << 6) |
436 		(ip_table[1][(a >> 8) & 255] << 6);
437 
438 	*l = ((b & top_1) << 8) |
439 		(b & mid_4) |
440 		((b & low_3) >> 5);
441 
442 	*r = ((a & top_1) << 8) |
443 		(a & mid_4) |
444 		((a & low_3) >> 5);
445 }
446 
447 
448 static uint64_t
449 des_fp(uint64_t l, uint64_t r)
450 {
451 	uint32_t upper, lower;
452 
453 	lower = fp_table[((l >> 55) & 240) | ((r >> 59) & 15)] |
454 		(fp_table[((l >> 35) & 240) | ((r>>39) & 15)] >> 2) |
455 		(fp_table[((l >> 23) & 240) | ((r >> 27) & 15)] >> 4) |
456 		(fp_table[((l >> 6) & 240) | ((r >> 10) & 15)] >> 6);
457 
458 	upper = fp_table[((l >> 41) & 240) | ((r >> 45) & 15)] |
459 		(fp_table[((l >> 29) & 240) | ((r >> 33) & 15)] >> 2) |
460 		(fp_table[((l >> 12) & 240) | ((r >> 16) & 15)] >> 4) |
461 		(fp_table[(l & 240) | (r >> 4) & 15] >> 6);
462 
463 	return ((((uint64_t)upper) << 32) | (uint64_t)lower);
464 
465 }
466 
467 uint64_t
468 des_crypt_impl(uint64_t *ks, uint64_t block, int one_or_three)
469 {
470 	int i, j;
471 	uint64_t l, r, t;
472 
473 	des_ip(&l, &r, block);
474 	for (j = 0; j < one_or_three; j++) {
475 		for (i = j * 16; i < (j + 1) * 16; i++) {
476 			t = r ^ ks[i];
477 			t = sbox_table[0][t >> 58] |
478 				sbox_table[1][(t >> 44) & 63] |
479 				sbox_table[2][(t >> 38) & 63] |
480 				sbox_table[3][(t >> 32) & 63] |
481 				sbox_table[4][(t >> 26) & 63] |
482 				sbox_table[5][(t >> 15) & 63] |
483 				sbox_table[6][(t >> 9) & 63] |
484 				sbox_table[7][(t >> 3) & 63];
485 			t = t^l;
486 			l = r;
487 			r = t;
488 		}
489 		r = l;
490 		l = t;
491 	}
492 
493 	return (des_fp(l, r));
494 }
495 #endif /* !sun4u */
496 
497 /* EXPORT DELETE END */
498 
499 void
500 des3_crunch_block(void *cookie, uint8_t block[DES_BLOCK_LEN],
501     uint8_t out_block[DES_BLOCK_LEN], boolean_t decrypt)
502 {
503 /* EXPORT DELETE START */
504 	keysched3_t *ksch = (keysched3_t *)cookie;
505 
506 	/*
507 	 * The code below, that is always executed on LITTLE_ENDIAN machines,
508 	 * reverses bytes in the block.  On BIG_ENDIAN, the same code
509 	 * copies the block without reversing bytes.
510 	 */
511 #ifdef _BIG_ENDIAN
512 	if (IS_P2ALIGNED(block, sizeof (uint64_t)) &&
513 	    IS_P2ALIGNED(out_block, sizeof (uint64_t))) {
514 		if (decrypt == B_TRUE)
515 			/* LINTED */
516 			*(uint64_t *)out_block = des_crypt_impl(
517 			    ksch->ksch_decrypt,
518 			    /* LINTED */
519 			    *(uint64_t *)block, 3);
520 		else
521 			/* LINTED */
522 			*(uint64_t *)out_block = des_crypt_impl(
523 			    ksch->ksch_encrypt,
524 			    /* LINTED */
525 			    *(uint64_t *)block, 3);
526 	} else {
527 #endif
528 		uint64_t tmp;
529 
530 		tmp = (((uint64_t)block[0] << 56) | ((uint64_t)block[1] << 48) |
531 		    ((uint64_t)block[2] << 40) | ((uint64_t)block[3] << 32) |
532 		    ((uint64_t)block[4] << 24) | ((uint64_t)block[5] << 16) |
533 		    ((uint64_t)block[6] << 8) | (uint64_t)block[7]);
534 
535 		if (decrypt == B_TRUE)
536 			tmp = des_crypt_impl(ksch->ksch_decrypt, tmp, 3);
537 		else
538 			tmp = des_crypt_impl(ksch->ksch_encrypt, tmp, 3);
539 
540 		out_block[0] = tmp >> 56;
541 		out_block[1] = tmp >> 48;
542 		out_block[2] = tmp >> 40;
543 		out_block[3] = tmp >> 32;
544 		out_block[4] = tmp >> 24;
545 		out_block[5] = tmp >> 16;
546 		out_block[6] = tmp >> 8;
547 		out_block[7] = (uint8_t)tmp;
548 #ifdef _BIG_ENDIAN
549 	}
550 #endif
551 /* EXPORT DELETE END */
552 }
553 
554 void
555 des_crunch_block(void *cookie, uint8_t block[DES_BLOCK_LEN],
556     uint8_t out_block[DES_BLOCK_LEN], boolean_t decrypt)
557 {
558 /* EXPORT DELETE START */
559 	keysched_t *ksch = (keysched_t *)cookie;
560 
561 	/*
562 	 * The code below, that is always executed on LITTLE_ENDIAN machines,
563 	 * reverses bytes in the block.  On BIG_ENDIAN, the same code
564 	 * copies the block without reversing bytes.
565 	 */
566 #ifdef _BIG_ENDIAN
567 	if (IS_P2ALIGNED(block, sizeof (uint64_t)) &&
568 	    IS_P2ALIGNED(out_block, sizeof (uint64_t))) {
569 		if (decrypt == B_TRUE)
570 			/* LINTED */
571 			*(uint64_t *)out_block = des_crypt_impl(
572 			    ksch->ksch_decrypt,
573 			    /* LINTED */
574 			    *(uint64_t *)block, 1);
575 		else
576 			/* LINTED */
577 			*(uint64_t *)out_block = des_crypt_impl(
578 			    ksch->ksch_encrypt,
579 			    /* LINTED */
580 			    *(uint64_t *)block, 1);
581 
582 	} else {
583 #endif
584 		uint64_t tmp;
585 
586 		tmp = (((uint64_t)block[0] << 56) | ((uint64_t)block[1] << 48) |
587 		    ((uint64_t)block[2] << 40) | ((uint64_t)block[3] << 32) |
588 		    ((uint64_t)block[4] << 24) | ((uint64_t)block[5] << 16) |
589 		    ((uint64_t)block[6] << 8) | (uint64_t)block[7]);
590 
591 		if (decrypt == B_TRUE)
592 			tmp = des_crypt_impl(ksch->ksch_decrypt, tmp, 1);
593 		else
594 			tmp = des_crypt_impl(ksch->ksch_encrypt, tmp, 1);
595 
596 		out_block[0] = tmp >> 56;
597 		out_block[1] = tmp >> 48;
598 		out_block[2] = tmp >> 40;
599 		out_block[3] = tmp >> 32;
600 		out_block[4] = tmp >> 24;
601 		out_block[5] = tmp >> 16;
602 		out_block[6] = tmp >> 8;
603 		out_block[7] = (uint8_t)tmp;
604 #ifdef _BIG_ENDIAN
605 	}
606 #endif
607 /* EXPORT DELETE END */
608 }
609 
610 static boolean_t
611 keycheck(uint8_t *key, uint8_t *corrected_key)
612 {
613 /* EXPORT DELETE START */
614 	uint64_t key_so_far;
615 	uint_t i;
616 	/*
617 	 * Table of weak and semi-weak keys.  Fortunately, weak keys are
618 	 * endian-independent, and some semi-weak keys can be paired up in
619 	 * endian-opposite order.  Since keys are stored as uint64_t's,
620 	 * use the ifdef _LITTLE_ENDIAN where appropriate.
621 	 */
622 	static uint64_t des_weak_keys[] = {
623 		/* Really weak keys.  Byte-order independent values. */
624 		0x0101010101010101,
625 		0x1f1f1f1f0e0e0e0e,
626 		0xe0e0e0e0f1f1f1f1,
627 		0xfefefefefefefefe,
628 
629 		/* Semi-weak (and a few possibly-weak) keys. */
630 
631 		/* Byte-order independent semi-weak keys. */
632 		0x01fe01fe01fe01fe,	0xfe01fe01fe01fe01,
633 
634 		/* Byte-order dependent semi-weak keys. */
635 #ifdef _LITTLE_ENDIAN
636 		0xf10ef10ee01fe01f,	0x0ef10ef11fe01fe0,
637 		0x01f101f101e001e0,	0xf101f101e001e001,
638 		0x0efe0efe1ffe1ffe,	0xfe0efe0efe1ffe1f,
639 		0x010e010e011f011f,	0x0e010e011f011f01,
640 		0xf1fef1fee0fee0fe,	0xfef1fef1fee0fee0,
641 #else	/* Big endian */
642 		0x1fe01fe00ef10ef1,	0xe01fe01ff10ef10e,
643 		0x01e001e001f101f1,	0xe001e001f101f101,
644 		0x1ffe1ffe0efe0efe,	0xfe1ffe1ffe0efe0e,
645 		0x011f011f010e010e,	0x1f011f010e010e01,
646 		0xe0fee0fef1fef1fe,	0xfee0fee0fef1fef1,
647 #endif
648 
649 		/* We'll save the other possibly-weak keys for the future. */
650 	};
651 
652 	if (key == NULL)
653 		return (B_FALSE);
654 
655 	/*
656 	 * The code below reverses the bytes on LITTLE_ENDIAN machines.
657 	 * On BIG_ENDIAN, the same code copies without reversing
658 	 * the bytes.
659 	 */
660 	key_so_far = (((uint64_t)key[0] << 56) | ((uint64_t)key[1] << 48) |
661 	    ((uint64_t)key[2] << 40) | ((uint64_t)key[3] << 32) |
662 	    ((uint64_t)key[4] << 24) | ((uint64_t)key[5] << 16) |
663 	    ((uint64_t)key[6] << 8) | (uint64_t)key[7]);
664 
665 	/*
666 	 * Fix parity.
667 	 */
668 	fix_des_parity(&key_so_far);
669 	key_so_far ^= 0x0101010101010101;
670 
671 	/* Do weak key check itself. */
672 	for (i = 0; i < (sizeof (des_weak_keys) / sizeof (uint64_t)); i++)
673 		if (key_so_far == des_weak_keys[i]) {
674 			return (B_FALSE);
675 		}
676 
677 	if (corrected_key != NULL) {
678 		/*
679 		 * The code below reverses the bytes on LITTLE_ENDIAN machines.
680 		 * On BIG_ENDIAN, the same code copies without reversing
681 		 * the bytes.
682 		 */
683 		corrected_key[0] = key_so_far >> 56;
684 		corrected_key[1] = key_so_far >> 48;
685 		corrected_key[2] = key_so_far >> 40;
686 		corrected_key[3] = key_so_far >> 32;
687 		corrected_key[4] = key_so_far >> 24;
688 		corrected_key[5] = key_so_far >> 16;
689 		corrected_key[6] = key_so_far >> 8;
690 		corrected_key[7] = (uint8_t)key_so_far;
691 	}
692 /* EXPORT DELETE END */
693 	return (B_TRUE);
694 }
695 
696 static boolean_t
697 des3_keycheck(uint8_t *key, uint8_t *corrected_key)
698 {
699 /* EXPORT DELETE START */
700 	uint64_t aligned_key[DES3_KEYSIZE / sizeof (uint64_t)];
701 	uint64_t key_so_far, scratch, *currentkey;
702 	uint_t j, num_weakkeys = 0;
703 
704 	if (key == NULL) {
705 		return (B_FALSE);
706 	}
707 
708 	if (!IS_P2ALIGNED(key, sizeof (uint64_t))) {
709 		bcopy(key, aligned_key, DES3_KEYSIZE);
710 		currentkey = (uint64_t *)aligned_key;
711 	} else {
712 		/* LINTED */
713 		currentkey = (uint64_t *)key;
714 	}
715 
716 	for (j = 0; j < 3; j++) {
717 		key_so_far = currentkey[j];
718 
719 		if (!keycheck((uint8_t *)&key_so_far, (uint8_t *)&scratch)) {
720 			if (++num_weakkeys > 1) {
721 				return (B_FALSE);
722 			}
723 			/*
724 			 * We found a weak key, but since
725 			 * we've only found one weak key,
726 			 * we can not reject the whole 3DES
727 			 * set of keys as weak.
728 			 *
729 			 * Break from the weak key loop
730 			 * (since this DES key is weak) and
731 			 * continue on.
732 			 */
733 		}
734 
735 		currentkey[j] = scratch;
736 	}
737 
738 	/*
739 	 * Perform key equivalence checks, now that parity is properly set.
740 	 * All three keys must be unique.
741 	 */
742 	if (currentkey[0] == currentkey[1] || currentkey[1] == currentkey[2] ||
743 	    currentkey[2] == currentkey[0])
744 		return (B_FALSE);
745 
746 	if (corrected_key != NULL) {
747 		bcopy(currentkey, corrected_key, DES3_KEYSIZE);
748 	}
749 /* EXPORT DELETE END */
750 	return (B_TRUE);
751 }
752 
753 boolean_t
754 des_keycheck(uint8_t *key, des_strength_t strength, uint8_t *corrected_key)
755 {
756 	if (strength == DES) {
757 		return (keycheck(key, corrected_key));
758 	} else if (strength == DES3) {
759 		return (des3_keycheck(key, corrected_key));
760 	} else {
761 		return (B_FALSE);
762 	}
763 }
764 
765 /*
766  * Initialize key schedule for DES, DES2, and DES3
767  */
768 void
769 des_init_keysched(uint8_t *cipherKey, des_strength_t strength, void *ks)
770 {
771 /* EXPORT DELETE START */
772 	uint64_t *encryption_ks;
773 	uint64_t *decryption_ks;
774 	uint64_t keysched[48];
775 	uint64_t key_uint64[3];
776 	uint64_t tmp;
777 	uint_t keysize, i, j;
778 
779 	switch (strength) {
780 	case DES:
781 		keysize = DES_KEYSIZE;
782 		encryption_ks = ((keysched_t *)ks)->ksch_encrypt;
783 		decryption_ks = ((keysched_t *)ks)->ksch_decrypt;
784 		break;
785 	case DES2:
786 		keysize = DES2_KEYSIZE;
787 		encryption_ks = ((keysched3_t *)ks)->ksch_encrypt;
788 		decryption_ks = ((keysched3_t *)ks)->ksch_decrypt;
789 		break;
790 	case DES3:
791 		keysize = DES3_KEYSIZE;
792 		encryption_ks = ((keysched3_t *)ks)->ksch_encrypt;
793 		decryption_ks = ((keysched3_t *)ks)->ksch_decrypt;
794 	}
795 
796 	/*
797 	 * The code below, that is always executed on LITTLE_ENDIAN machines,
798 	 * reverses every 8 bytes in the key.  On BIG_ENDIAN, the same code
799 	 * copies the key without reversing bytes.
800 	 */
801 #ifdef _BIG_ENDIAN
802 	if (IS_P2ALIGNED(cipherKey, sizeof (uint64_t))) {
803 		for (i = 0, j = 0; j < keysize; i++, j += 8) {
804 			/* LINTED: pointer alignment */
805 			key_uint64[i] = *((uint64_t *)&cipherKey[j]);
806 		}
807 	} else {
808 #endif
809 	{
810 		for (i = 0, j = 0; j < keysize; i++, j += 8) {
811 			key_uint64[i] = (((uint64_t)cipherKey[j] << 56) |
812 			    ((uint64_t)cipherKey[j + 1] << 48) |
813 			    ((uint64_t)cipherKey[j + 2] << 40) |
814 			    ((uint64_t)cipherKey[j + 3] << 32) |
815 			    ((uint64_t)cipherKey[j + 4] << 24) |
816 			    ((uint64_t)cipherKey[j + 5] << 16) |
817 			    ((uint64_t)cipherKey[j + 6] << 8) |
818 			    (uint64_t)cipherKey[j + 7]);
819 		}
820 	}
821 #ifdef _BIG_ENDIAN
822 	}
823 #endif
824 
825 	switch (strength) {
826 	case DES:
827 		des_ks(keysched, key_uint64[0]);
828 		break;
829 
830 	case DES2:
831 		/* DES2 is just DES3 with the first and third keys the same */
832 		bcopy(key_uint64, key_uint64 + 2, DES_KEYSIZE);
833 		/* FALLTHRU */
834 	case DES3:
835 		des_ks(keysched, key_uint64[0]);
836 		des_ks(keysched + 16, key_uint64[1]);
837 		for (i = 0; i < 8; i++) {
838 			tmp = keysched[16+i];
839 			keysched[16+i] = keysched[31-i];
840 			keysched[31-i] = tmp;
841 		}
842 		des_ks(keysched+32, key_uint64[2]);
843 		keysize = DES3_KEYSIZE;
844 	}
845 
846 	/* save the encryption keyschedule */
847 	bcopy(keysched, encryption_ks, keysize * 16);
848 
849 	/* reverse the key schedule */
850 	for (i = 0; i < keysize; i++) {
851 		tmp = keysched[i];
852 		keysched[i] = keysched[2 * keysize - 1 - i];
853 		keysched[2 * keysize -1 -i] = tmp;
854 	}
855 
856 	/* save the decryption keyschedule */
857 	bcopy(keysched, decryption_ks, keysize * 16);
858 /* EXPORT DELETE END */
859 }
860 
861 /*
862  * Allocate key schedule.
863  */
864 /*ARGSUSED*/
865 void *
866 des_alloc_keysched(size_t *keysched_size, des_strength_t strength, int kmflag)
867 {
868 	void *keysched;
869 
870 /* EXPORT DELETE START */
871 
872 	size_t size;
873 
874 	switch (strength) {
875 	case DES:
876 		size = sizeof (keysched_t);
877 		break;
878 	case DES2:
879 	case DES3:
880 		size = sizeof (keysched3_t);
881 	}
882 
883 #ifdef	_KERNEL
884 	keysched = (keysched_t *)kmem_alloc(size, kmflag);
885 #else	/* !_KERNEL */
886 	keysched = (keysched_t *)malloc(size);
887 #endif	/* _KERNEL */
888 
889 	if (keysched == NULL)
890 		return (NULL);
891 
892 	if (keysched_size != NULL)
893 		*keysched_size = size;
894 
895 /* EXPORT DELETE END */
896 
897 	return (keysched);
898 }
899 
900 /*
901  * Replace the LSB of each byte by the xor of the other
902  * 7 bits.  The tricky thing is that the original contents of the LSBs
903  * are nullifed by including them twice in the xor computation.
904  */
905 static void
906 fix_des_parity(uint64_t *keyp)
907 {
908 /* EXPORT DELETE START */
909 	uint64_t k = *keyp;
910 	k ^= k >> 1;
911 	k ^= k >> 2;
912 	k ^= k >> 4;
913 	*keyp ^= (k & 0x0101010101010101LL);
914 /* EXPORT DELETE END */
915 }
916