1 /* fcrypt.c */
2 /* Copyright (C) 1993 Eric Young - see README for more details */
3 /* #include <stdio.h> */
4
5 /* Eric Young.
6 * This version of crypt has been developed from my MIT compatable
7 * DES library.
8 * The library is available at pub/DES at ftp.psy.uq.oz.au
9 * eay@psych.psy.uq.oz.au
10 */
11
12 #include "fcrypt.h"
13
14 typedef struct des_ks_struct
15 {
16 union {
17 des_cblock _;
18 /* make sure things are correct size on machines with
19 * 8 byte longs */
20 unsigned long pad[2];
21 } ks;
22 #define _ ks._
23 } des_key_schedule[16];
24
25 #define DES_KEY_SZ (sizeof(des_cblock))
26 #define DES_ENCRYPT 1
27 #define DES_DECRYPT 0
28
29 #define ITERATIONS 16
30 #define HALF_ITERATIONS 8
31
32 #define c2l(c,l) (l =((unsigned long)(*((c)++))) , \
33 l|=((unsigned long)(*((c)++)))<< 8, \
34 l|=((unsigned long)(*((c)++)))<<16, \
35 l|=((unsigned long)(*((c)++)))<<24)
36
37 #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
38 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
39 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
40 *((c)++)=(unsigned char)(((l)>>24)&0xff))
41
42 static unsigned long SPtrans[8][64]=
43 {
44 {
45 /* nibble 0 */
46 0x00820200, 0x00020000, 0x80800000, 0x80820200,
47 0x00800000, 0x80020200, 0x80020000, 0x80800000,
48 0x80020200, 0x00820200, 0x00820000, 0x80000200,
49 0x80800200, 0x00800000, 0x00000000, 0x80020000,
50 0x00020000, 0x80000000, 0x00800200, 0x00020200,
51 0x80820200, 0x00820000, 0x80000200, 0x00800200,
52 0x80000000, 0x00000200, 0x00020200, 0x80820000,
53 0x00000200, 0x80800200, 0x80820000, 0x00000000,
54 0x00000000, 0x80820200, 0x00800200, 0x80020000,
55 0x00820200, 0x00020000, 0x80000200, 0x00800200,
56 0x80820000, 0x00000200, 0x00020200, 0x80800000,
57 0x80020200, 0x80000000, 0x80800000, 0x00820000,
58 0x80820200, 0x00020200, 0x00820000, 0x80800200,
59 0x00800000, 0x80000200, 0x80020000, 0x00000000,
60 0x00020000, 0x00800000, 0x80800200, 0x00820200,
61 0x80000000, 0x80820000, 0x00000200, 0x80020200
62 },{
63 /* nibble 1 */
64 0x10042004, 0x00000000, 0x00042000, 0x10040000,
65 0x10000004, 0x00002004, 0x10002000, 0x00042000,
66 0x00002000, 0x10040004, 0x00000004, 0x10002000,
67 0x00040004, 0x10042000, 0x10040000, 0x00000004,
68 0x00040000, 0x10002004, 0x10040004, 0x00002000,
69 0x00042004, 0x10000000, 0x00000000, 0x00040004,
70 0x10002004, 0x00042004, 0x10042000, 0x10000004,
71 0x10000000, 0x00040000, 0x00002004, 0x10042004,
72 0x00040004, 0x10042000, 0x10002000, 0x00042004,
73 0x10042004, 0x00040004, 0x10000004, 0x00000000,
74 0x10000000, 0x00002004, 0x00040000, 0x10040004,
75 0x00002000, 0x10000000, 0x00042004, 0x10002004,
76 0x10042000, 0x00002000, 0x00000000, 0x10000004,
77 0x00000004, 0x10042004, 0x00042000, 0x10040000,
78 0x10040004, 0x00040000, 0x00002004, 0x10002000,
79 0x10002004, 0x00000004, 0x10040000, 0x00042000
80 },{
81 /* nibble 2 */
82 0x41000000, 0x01010040, 0x00000040, 0x41000040,
83 0x40010000, 0x01000000, 0x41000040, 0x00010040,
84 0x01000040, 0x00010000, 0x01010000, 0x40000000,
85 0x41010040, 0x40000040, 0x40000000, 0x41010000,
86 0x00000000, 0x40010000, 0x01010040, 0x00000040,
87 0x40000040, 0x41010040, 0x00010000, 0x41000000,
88 0x41010000, 0x01000040, 0x40010040, 0x01010000,
89 0x00010040, 0x00000000, 0x01000000, 0x40010040,
90 0x01010040, 0x00000040, 0x40000000, 0x00010000,
91 0x40000040, 0x40010000, 0x01010000, 0x41000040,
92 0x00000000, 0x01010040, 0x00010040, 0x41010000,
93 0x40010000, 0x01000000, 0x41010040, 0x40000000,
94 0x40010040, 0x41000000, 0x01000000, 0x41010040,
95 0x00010000, 0x01000040, 0x41000040, 0x00010040,
96 0x01000040, 0x00000000, 0x41010000, 0x40000040,
97 0x41000000, 0x40010040, 0x00000040, 0x01010000
98 },{
99 /* nibble 3 */
100 0x00100402, 0x04000400, 0x00000002, 0x04100402,
101 0x00000000, 0x04100000, 0x04000402, 0x00100002,
102 0x04100400, 0x04000002, 0x04000000, 0x00000402,
103 0x04000002, 0x00100402, 0x00100000, 0x04000000,
104 0x04100002, 0x00100400, 0x00000400, 0x00000002,
105 0x00100400, 0x04000402, 0x04100000, 0x00000400,
106 0x00000402, 0x00000000, 0x00100002, 0x04100400,
107 0x04000400, 0x04100002, 0x04100402, 0x00100000,
108 0x04100002, 0x00000402, 0x00100000, 0x04000002,
109 0x00100400, 0x04000400, 0x00000002, 0x04100000,
110 0x04000402, 0x00000000, 0x00000400, 0x00100002,
111 0x00000000, 0x04100002, 0x04100400, 0x00000400,
112 0x04000000, 0x04100402, 0x00100402, 0x00100000,
113 0x04100402, 0x00000002, 0x04000400, 0x00100402,
114 0x00100002, 0x00100400, 0x04100000, 0x04000402,
115 0x00000402, 0x04000000, 0x04000002, 0x04100400
116 },{
117 /* nibble 4 */
118 0x02000000, 0x00004000, 0x00000100, 0x02004108,
119 0x02004008, 0x02000100, 0x00004108, 0x02004000,
120 0x00004000, 0x00000008, 0x02000008, 0x00004100,
121 0x02000108, 0x02004008, 0x02004100, 0x00000000,
122 0x00004100, 0x02000000, 0x00004008, 0x00000108,
123 0x02000100, 0x00004108, 0x00000000, 0x02000008,
124 0x00000008, 0x02000108, 0x02004108, 0x00004008,
125 0x02004000, 0x00000100, 0x00000108, 0x02004100,
126 0x02004100, 0x02000108, 0x00004008, 0x02004000,
127 0x00004000, 0x00000008, 0x02000008, 0x02000100,
128 0x02000000, 0x00004100, 0x02004108, 0x00000000,
129 0x00004108, 0x02000000, 0x00000100, 0x00004008,
130 0x02000108, 0x00000100, 0x00000000, 0x02004108,
131 0x02004008, 0x02004100, 0x00000108, 0x00004000,
132 0x00004100, 0x02004008, 0x02000100, 0x00000108,
133 0x00000008, 0x00004108, 0x02004000, 0x02000008
134 },{
135 /* nibble 5 */
136 0x20000010, 0x00080010, 0x00000000, 0x20080800,
137 0x00080010, 0x00000800, 0x20000810, 0x00080000,
138 0x00000810, 0x20080810, 0x00080800, 0x20000000,
139 0x20000800, 0x20000010, 0x20080000, 0x00080810,
140 0x00080000, 0x20000810, 0x20080010, 0x00000000,
141 0x00000800, 0x00000010, 0x20080800, 0x20080010,
142 0x20080810, 0x20080000, 0x20000000, 0x00000810,
143 0x00000010, 0x00080800, 0x00080810, 0x20000800,
144 0x00000810, 0x20000000, 0x20000800, 0x00080810,
145 0x20080800, 0x00080010, 0x00000000, 0x20000800,
146 0x20000000, 0x00000800, 0x20080010, 0x00080000,
147 0x00080010, 0x20080810, 0x00080800, 0x00000010,
148 0x20080810, 0x00080800, 0x00080000, 0x20000810,
149 0x20000010, 0x20080000, 0x00080810, 0x00000000,
150 0x00000800, 0x20000010, 0x20000810, 0x20080800,
151 0x20080000, 0x00000810, 0x00000010, 0x20080010
152 },{
153 /* nibble 6 */
154 0x00001000, 0x00000080, 0x00400080, 0x00400001,
155 0x00401081, 0x00001001, 0x00001080, 0x00000000,
156 0x00400000, 0x00400081, 0x00000081, 0x00401000,
157 0x00000001, 0x00401080, 0x00401000, 0x00000081,
158 0x00400081, 0x00001000, 0x00001001, 0x00401081,
159 0x00000000, 0x00400080, 0x00400001, 0x00001080,
160 0x00401001, 0x00001081, 0x00401080, 0x00000001,
161 0x00001081, 0x00401001, 0x00000080, 0x00400000,
162 0x00001081, 0x00401000, 0x00401001, 0x00000081,
163 0x00001000, 0x00000080, 0x00400000, 0x00401001,
164 0x00400081, 0x00001081, 0x00001080, 0x00000000,
165 0x00000080, 0x00400001, 0x00000001, 0x00400080,
166 0x00000000, 0x00400081, 0x00400080, 0x00001080,
167 0x00000081, 0x00001000, 0x00401081, 0x00400000,
168 0x00401080, 0x00000001, 0x00001001, 0x00401081,
169 0x00400001, 0x00401080, 0x00401000, 0x00001001
170 },{
171 /* nibble 7 */
172 0x08200020, 0x08208000, 0x00008020, 0x00000000,
173 0x08008000, 0x00200020, 0x08200000, 0x08208020,
174 0x00000020, 0x08000000, 0x00208000, 0x00008020,
175 0x00208020, 0x08008020, 0x08000020, 0x08200000,
176 0x00008000, 0x00208020, 0x00200020, 0x08008000,
177 0x08208020, 0x08000020, 0x00000000, 0x00208000,
178 0x08000000, 0x00200000, 0x08008020, 0x08200020,
179 0x00200000, 0x00008000, 0x08208000, 0x00000020,
180 0x00200000, 0x00008000, 0x08000020, 0x08208020,
181 0x00008020, 0x08000000, 0x00000000, 0x00208000,
182 0x08200020, 0x08008020, 0x08008000, 0x00200020,
183 0x08208000, 0x00000020, 0x00200020, 0x08008000,
184 0x08208020, 0x00200000, 0x08200000, 0x08000020,
185 0x00208000, 0x00008020, 0x08008020, 0x08200000,
186 0x00000020, 0x08208000, 0x00208020, 0x00000000,
187 0x08000000, 0x08200020, 0x00008000, 0x00208020
188 }
189 };
190 static unsigned long skb[8][64]={
191 {
192 /* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
193 0x00000000,0x00000010,0x20000000,0x20000010,
194 0x00010000,0x00010010,0x20010000,0x20010010,
195 0x00000800,0x00000810,0x20000800,0x20000810,
196 0x00010800,0x00010810,0x20010800,0x20010810,
197 0x00000020,0x00000030,0x20000020,0x20000030,
198 0x00010020,0x00010030,0x20010020,0x20010030,
199 0x00000820,0x00000830,0x20000820,0x20000830,
200 0x00010820,0x00010830,0x20010820,0x20010830,
201 0x00080000,0x00080010,0x20080000,0x20080010,
202 0x00090000,0x00090010,0x20090000,0x20090010,
203 0x00080800,0x00080810,0x20080800,0x20080810,
204 0x00090800,0x00090810,0x20090800,0x20090810,
205 0x00080020,0x00080030,0x20080020,0x20080030,
206 0x00090020,0x00090030,0x20090020,0x20090030,
207 0x00080820,0x00080830,0x20080820,0x20080830,
208 0x00090820,0x00090830,0x20090820,0x20090830},{
209 /* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */
210 0x00000000,0x02000000,0x00002000,0x02002000,
211 0x00200000,0x02200000,0x00202000,0x02202000,
212 0x00000004,0x02000004,0x00002004,0x02002004,
213 0x00200004,0x02200004,0x00202004,0x02202004,
214 0x00000400,0x02000400,0x00002400,0x02002400,
215 0x00200400,0x02200400,0x00202400,0x02202400,
216 0x00000404,0x02000404,0x00002404,0x02002404,
217 0x00200404,0x02200404,0x00202404,0x02202404,
218 0x10000000,0x12000000,0x10002000,0x12002000,
219 0x10200000,0x12200000,0x10202000,0x12202000,
220 0x10000004,0x12000004,0x10002004,0x12002004,
221 0x10200004,0x12200004,0x10202004,0x12202004,
222 0x10000400,0x12000400,0x10002400,0x12002400,
223 0x10200400,0x12200400,0x10202400,0x12202400,
224 0x10000404,0x12000404,0x10002404,0x12002404,
225 0x10200404,0x12200404,0x10202404,0x12202404}, {
226 /* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */
227 0x00000000,0x00000001,0x00040000,0x00040001,
228 0x01000000,0x01000001,0x01040000,0x01040001,
229 0x00000002,0x00000003,0x00040002,0x00040003,
230 0x01000002,0x01000003,0x01040002,0x01040003,
231 0x00000200,0x00000201,0x00040200,0x00040201,
232 0x01000200,0x01000201,0x01040200,0x01040201,
233 0x00000202,0x00000203,0x00040202,0x00040203,
234 0x01000202,0x01000203,0x01040202,0x01040203,
235 0x08000000,0x08000001,0x08040000,0x08040001,
236 0x09000000,0x09000001,0x09040000,0x09040001,
237 0x08000002,0x08000003,0x08040002,0x08040003,
238 0x09000002,0x09000003,0x09040002,0x09040003,
239 0x08000200,0x08000201,0x08040200,0x08040201,
240 0x09000200,0x09000201,0x09040200,0x09040201,
241 0x08000202,0x08000203,0x08040202,0x08040203,
242 0x09000202,0x09000203,0x09040202,0x09040203},{
243 /* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */
244 0x00000000,0x00100000,0x00000100,0x00100100,
245 0x00000008,0x00100008,0x00000108,0x00100108,
246 0x00001000,0x00101000,0x00001100,0x00101100,
247 0x00001008,0x00101008,0x00001108,0x00101108,
248 0x04000000,0x04100000,0x04000100,0x04100100,
249 0x04000008,0x04100008,0x04000108,0x04100108,
250 0x04001000,0x04101000,0x04001100,0x04101100,
251 0x04001008,0x04101008,0x04001108,0x04101108,
252 0x00020000,0x00120000,0x00020100,0x00120100,
253 0x00020008,0x00120008,0x00020108,0x00120108,
254 0x00021000,0x00121000,0x00021100,0x00121100,
255 0x00021008,0x00121008,0x00021108,0x00121108,
256 0x04020000,0x04120000,0x04020100,0x04120100,
257 0x04020008,0x04120008,0x04020108,0x04120108,
258 0x04021000,0x04121000,0x04021100,0x04121100,
259 0x04021008,0x04121008,0x04021108,0x04121108},{
260 /* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
261 0x00000000,0x10000000,0x00010000,0x10010000,
262 0x00000004,0x10000004,0x00010004,0x10010004,
263 0x20000000,0x30000000,0x20010000,0x30010000,
264 0x20000004,0x30000004,0x20010004,0x30010004,
265 0x00100000,0x10100000,0x00110000,0x10110000,
266 0x00100004,0x10100004,0x00110004,0x10110004,
267 0x20100000,0x30100000,0x20110000,0x30110000,
268 0x20100004,0x30100004,0x20110004,0x30110004,
269 0x00001000,0x10001000,0x00011000,0x10011000,
270 0x00001004,0x10001004,0x00011004,0x10011004,
271 0x20001000,0x30001000,0x20011000,0x30011000,
272 0x20001004,0x30001004,0x20011004,0x30011004,
273 0x00101000,0x10101000,0x00111000,0x10111000,
274 0x00101004,0x10101004,0x00111004,0x10111004,
275 0x20101000,0x30101000,0x20111000,0x30111000,
276 0x20101004,0x30101004,0x20111004,0x30111004}, {
277 /* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */
278 0x00000000,0x08000000,0x00000008,0x08000008,
279 0x00000400,0x08000400,0x00000408,0x08000408,
280 0x00020000,0x08020000,0x00020008,0x08020008,
281 0x00020400,0x08020400,0x00020408,0x08020408,
282 0x00000001,0x08000001,0x00000009,0x08000009,
283 0x00000401,0x08000401,0x00000409,0x08000409,
284 0x00020001,0x08020001,0x00020009,0x08020009,
285 0x00020401,0x08020401,0x00020409,0x08020409,
286 0x02000000,0x0A000000,0x02000008,0x0A000008,
287 0x02000400,0x0A000400,0x02000408,0x0A000408,
288 0x02020000,0x0A020000,0x02020008,0x0A020008,
289 0x02020400,0x0A020400,0x02020408,0x0A020408,
290 0x02000001,0x0A000001,0x02000009,0x0A000009,
291 0x02000401,0x0A000401,0x02000409,0x0A000409,
292 0x02020001,0x0A020001,0x02020009,0x0A020009,
293 0x02020401,0x0A020401,0x02020409,0x0A020409},{
294 /* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */
295 0x00000000,0x00000100,0x00080000,0x00080100,
296 0x01000000,0x01000100,0x01080000,0x01080100,
297 0x00000010,0x00000110,0x00080010,0x00080110,
298 0x01000010,0x01000110,0x01080010,0x01080110,
299 0x00200000,0x00200100,0x00280000,0x00280100,
300 0x01200000,0x01200100,0x01280000,0x01280100,
301 0x00200010,0x00200110,0x00280010,0x00280110,
302 0x01200010,0x01200110,0x01280010,0x01280110,
303 0x00000200,0x00000300,0x00080200,0x00080300,
304 0x01000200,0x01000300,0x01080200,0x01080300,
305 0x00000210,0x00000310,0x00080210,0x00080310,
306 0x01000210,0x01000310,0x01080210,0x01080310,
307 0x00200200,0x00200300,0x00280200,0x00280300,
308 0x01200200,0x01200300,0x01280200,0x01280300,
309 0x00200210,0x00200310,0x00280210,0x00280310,
310 0x01200210,0x01200310,0x01280210,0x01280310},{
311 /* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */
312 0x00000000,0x04000000,0x00040000,0x04040000,
313 0x00000002,0x04000002,0x00040002,0x04040002,
314 0x00002000,0x04002000,0x00042000,0x04042000,
315 0x00002002,0x04002002,0x00042002,0x04042002,
316 0x00000020,0x04000020,0x00040020,0x04040020,
317 0x00000022,0x04000022,0x00040022,0x04040022,
318 0x00002020,0x04002020,0x00042020,0x04042020,
319 0x00002022,0x04002022,0x00042022,0x04042022,
320 0x00000800,0x04000800,0x00040800,0x04040800,
321 0x00000802,0x04000802,0x00040802,0x04040802,
322 0x00002800,0x04002800,0x00042800,0x04042800,
323 0x00002802,0x04002802,0x00042802,0x04042802,
324 0x00000820,0x04000820,0x00040820,0x04040820,
325 0x00000822,0x04000822,0x00040822,0x04040822,
326 0x00002820,0x04002820,0x00042820,0x04042820,
327 0x00002822,0x04002822,0x00042822,0x04042822}
328 };
329
330 /* See ecb_encrypt.c for a pseudo description of these macros. */
331 #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
332 (b)^=(t),\
333 (a)^=((t)<<(n)))
334
335 #define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
336 (a)=(a)^(t)^(t>>(16-(n))))\
337
338 static char shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
339
340 static int
des_set_key(des_cblock * key,des_key_schedule schedule)341 des_set_key(des_cblock *key, des_key_schedule schedule)
342 {
343 register unsigned long c,d,t,s;
344 register unsigned char *in;
345 register unsigned long *k;
346 register int i;
347
348 k=(unsigned long *)schedule;
349 in=(unsigned char *)key;
350
351 c2l(in,c);
352 c2l(in,d);
353
354 /* I now do it in 47 simple operations :-)
355 * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
356 * for the inspiration. :-) */
357 PERM_OP (d,c,t,4,0x0f0f0f0f);
358 HPERM_OP(c,t,-2,0xcccc0000);
359 HPERM_OP(d,t,-2,0xcccc0000);
360 PERM_OP (d,c,t,1,0x55555555);
361 PERM_OP (c,d,t,8,0x00ff00ff);
362 PERM_OP (d,c,t,1,0x55555555);
363 d= (((d&0x000000ff)<<16)| (d&0x0000ff00) |
364 ((d&0x00ff0000)>>16)|((c&0xf0000000)>>4));
365 c&=0x0fffffff;
366
367 for (i=0; i<ITERATIONS; i++)
368 {
369 if (shifts2[i])
370 { c=((c>>2)|(c<<26)); d=((d>>2)|(d<<26)); }
371 else
372 { c=((c>>1)|(c<<27)); d=((d>>1)|(d<<27)); }
373 c&=0x0fffffff;
374 d&=0x0fffffff;
375 /* could be a few less shifts but I am to lazy at this
376 * point in time to investigate */
377 s= skb[0][ (c )&0x3f ]|
378 skb[1][((c>> 6)&0x03)|((c>> 7)&0x3c)]|
379 skb[2][((c>>13)&0x0f)|((c>>14)&0x30)]|
380 skb[3][((c>>20)&0x01)|((c>>21)&0x06) |
381 ((c>>22)&0x38)];
382 t= skb[4][ (d )&0x3f ]|
383 skb[5][((d>> 7)&0x03)|((d>> 8)&0x3c)]|
384 skb[6][ (d>>15)&0x3f ]|
385 skb[7][((d>>21)&0x0f)|((d>>22)&0x30)];
386
387 /* table contained 0213 4657 */
388 *(k++)=((t<<16)|(s&0x0000ffff))&0xffffffff;
389 s= ((s>>16)|(t&0xffff0000));
390
391 s=(s<<4)|(s>>28);
392 *(k++)=s&0xffffffff;
393 }
394 return(0);
395 }
396
397 /******************************************************************
398 * modified stuff for crypt.
399 ******************************************************************/
400
401 /* The changes to this macro may help or hinder, depending on the
402 * compiler and the achitecture. gcc2 always seems to do well :-).
403 * Inspired by Dana How <how@isl.stanford.edu>
404 * DO NOT use the alternative version on machines with 8 byte longs.
405 */
406 #ifdef ALT_ECB
407 #define D_ENCRYPT(L,R,S) \
408 v=(R^(R>>16)); \
409 u=(v&E0); \
410 v=(v&E1); \
411 u=((u^(u<<16))^R^s[S ])<<2; \
412 t=(v^(v<<16))^R^s[S+1]; \
413 t=(t>>2)|(t<<30); \
414 L^= \
415 *(unsigned long *)(des_SP+0x0100+((t )&0xfc))+ \
416 *(unsigned long *)(des_SP+0x0300+((t>> 8)&0xfc))+ \
417 *(unsigned long *)(des_SP+0x0500+((t>>16)&0xfc))+ \
418 *(unsigned long *)(des_SP+0x0700+((t>>24)&0xfc))+ \
419 *(unsigned long *)(des_SP+ ((u )&0xfc))+ \
420 *(unsigned long *)(des_SP+0x0200+((u>> 8)&0xfc))+ \
421 *(unsigned long *)(des_SP+0x0400+((u>>16)&0xfc))+ \
422 *(unsigned long *)(des_SP+0x0600+((u>>24)&0xfc));
423 #else /* original version */
424 #define D_ENCRYPT(L,R,S) \
425 v=(R^(R>>16)); \
426 u=(v&E0); \
427 v=(v&E1); \
428 u=(u^(u<<16))^R^s[S ]; \
429 t=(v^(v<<16))^R^s[S+1]; \
430 t=(t>>4)|(t<<28); \
431 L^= SPtrans[1][(t )&0x3f]| \
432 SPtrans[3][(t>> 8)&0x3f]| \
433 SPtrans[5][(t>>16)&0x3f]| \
434 SPtrans[7][(t>>24)&0x3f]| \
435 SPtrans[0][(u )&0x3f]| \
436 SPtrans[2][(u>> 8)&0x3f]| \
437 SPtrans[4][(u>>16)&0x3f]| \
438 SPtrans[6][(u>>24)&0x3f];
439 #endif
440
441 void
crypt_rounds(des_cblock key,unsigned long nrounds,unsigned long saltnum,des_cblock block)442 crypt_rounds(
443 des_cblock key,
444 unsigned long nrounds,
445 unsigned long saltnum,
446 des_cblock block)
447 {
448 unsigned char *cp;
449 des_key_schedule ks;
450 register unsigned long l,r,t,u,v;
451 #ifdef ALT_ECB
452 register unsigned char *des_SP=(unsigned char *)SPtrans;
453 #endif
454 register unsigned long *s;
455 register int i;
456 register unsigned long E0,E1;
457
458 E0 = (saltnum & 0x003f) | ((saltnum >> 4) & 0x3f00);
459 E1 = ((saltnum >> 2) & 0x03f0) | ((saltnum >> 6) & 0xf000) |
460 ((saltnum >> 22) & 0x0003);
461
462 des_set_key((des_cblock *)key, ks);
463
464 cp = block;
465 c2l(cp, l);
466 c2l(cp, r);
467
468 PERM_OP(r,l,t, 4,0x0f0f0f0f);
469 PERM_OP(l,r,t,16,0x0000ffff);
470 PERM_OP(r,l,t, 2,0x33333333);
471 PERM_OP(l,r,t, 8,0x00ff00ff);
472 PERM_OP(r,l,t, 1,0x55555555);
473
474 t=(l<<1)|(l>>31);
475 l=(r<<1)|(r>>31);
476 r=t;
477 /* clear the top bits on machines with 8byte longs */
478 l&=0xffffffff;
479 r&=0xffffffff;
480
481 s=(unsigned long *)ks;
482
483 while(nrounds--)
484 {
485 for (i=0; i<(ITERATIONS*2); i+=4)
486 {
487 D_ENCRYPT(l,r, i); /* 1 */
488 D_ENCRYPT(r,l, i+2); /* 2 */
489 }
490 t=l;
491 l=r;
492 r=t;
493 }
494 t=r;
495 r=(l>>1)|(l<<31);
496 l=(t>>1)|(t<<31);
497 /* clear the top bits on machines with 8byte longs */
498 l&=0xffffffff;
499 r&=0xffffffff;
500
501 PERM_OP(r,l,t, 1,0x55555555);
502 PERM_OP(l,r,t, 8,0x00ff00ff);
503 PERM_OP(r,l,t, 2,0x33333333);
504 PERM_OP(l,r,t,16,0x0000ffff);
505 PERM_OP(r,l,t, 4,0x0f0f0f0f);
506
507 cp = block;
508 l2c(l, cp);
509 l2c(r, cp);
510 }
511
512 unsigned char base64_char_to_val[128]={
513 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
514 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
515 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
516 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
517 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
518 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
519 0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
520 0x0A,0x0B,0x05,0x06,0x07,0x08,0x09,0x0A,
521 0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,
522 0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,
523 0x1B,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,
524 0x23,0x24,0x25,0x20,0x21,0x22,0x23,0x24,
525 0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,
526 0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,
527 0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,
528 0x3D,0x3E,0x3F,0x00,0x00,0x00,0x00,0x00,
529 };
530
531 unsigned char base64_val_to_char[64]={
532 0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
533 0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
534 0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
535 0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
536 0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
537 0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
538 0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
539 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
540 };
541
542 unsigned long
base64_to_int12(const char * base64)543 base64_to_int12(
544 const char *base64)
545 {
546 return ((unsigned long)base64_char_to_val[base64[0] & 0x7f]) |
547 (((unsigned long)base64_char_to_val[base64[1] & 0x7f]) << 6);
548 }
549
550 void
int12_to_base64(unsigned long val,char * base64)551 int12_to_base64(
552 unsigned long val,
553 char *base64)
554 {
555 base64[0] = base64_val_to_char[val & 0x3f];
556 base64[1] = base64_val_to_char[(val >> 6) & 0x3f];
557 base64[2] = 0;
558 }
559
560 unsigned long
base64_to_int24(const char * base64)561 base64_to_int24(
562 const char *base64)
563 {
564 return ((unsigned long)base64_char_to_val[base64[0] & 0x7f]) |
565 (((unsigned long)base64_char_to_val[base64[1] & 0x7f]) << 6) |
566 (((unsigned long)base64_char_to_val[base64[2] & 0x7f]) << 12) |
567 (((unsigned long)base64_char_to_val[base64[3] & 0x7f]) << 18);
568 }
569
570 void
int24_to_base64(unsigned long val,char * base64)571 int24_to_base64(
572 unsigned long val,
573 char *base64)
574 {
575 base64[0] = base64_val_to_char[val & 0x3f];
576 base64[1] = base64_val_to_char[(val >> 6) & 0x3f];
577 base64[2] = base64_val_to_char[(val >> 12) & 0x3f];
578 base64[3] = base64_val_to_char[(val >> 18) & 0x3f];
579 base64[4] = 0;
580 }
581
582 void
block_to_base64(des_cblock block,char * base64)583 block_to_base64(
584 des_cblock block,
585 char *base64)
586 {
587 base64[0] = base64_val_to_char[block[0] >> 2];
588 base64[1] = base64_val_to_char[((block[0]<<4) & 0x30) | (block[1]>>4)];
589 base64[2] = base64_val_to_char[((block[1]<<2) & 0x3c) | (block[2]>>6)];
590 base64[3] = base64_val_to_char[block[2] & 0x3f];
591 base64[4] = base64_val_to_char[block[3] >> 2];
592 base64[5] = base64_val_to_char[((block[3]<<4) & 0x30) | (block[4]>>4)];
593 base64[6] = base64_val_to_char[((block[4]<<2) & 0x3c) | (block[5]>>6)];
594 base64[7] = base64_val_to_char[block[5] & 0x3f];
595 base64[8] = base64_val_to_char[block[6] >> 2];
596 base64[9] = base64_val_to_char[((block[6]<<4) & 0x30) | (block[7]>>4)];
597 base64[10] = base64_val_to_char[(block[7]<<2) & 0x3c];
598 base64[11] = 0;
599 }
600
601 void
base64_to_block(des_cblock block,const char * base64)602 base64_to_block(
603 des_cblock block,
604 const char *base64)
605 {
606 unsigned char bb[11];
607 int i;
608 for(i = 0; i != 11; i++)
609 bb[i] = base64_char_to_val[base64[i] & 0x7f];
610 block[0] = (bb[0] << 2) | (bb[1] >> 4);
611 block[1] = ((bb[1] << 4) & 0xf0) | (bb[2] >> 2);
612 block[2] = ((bb[2] << 6) & 0xc0) | bb[3];
613 block[3] = (bb[4] << 2) | (bb[5] >> 4);
614 block[4] = ((bb[5] << 4) & 0xf0) | (bb[6] >> 2);
615 block[5] = ((bb[6] << 6) & 0xc0) | bb[7];
616 block[6] = (bb[8] << 2) | (bb[9] >> 4);
617 block[7] = ((bb[9] << 4) & 0xf0) | (bb[10] >> 2);
618 }
619
620 void
trad_password_to_key(des_cblock key,const char * password,size_t passwordlen)621 trad_password_to_key(
622 des_cblock key,
623 const char *password,
624 size_t passwordlen)
625 {
626 int i;
627 if(passwordlen > 8) passwordlen = 8;
628 for (i=0; i<passwordlen; i++)
629 key[i] = ((unsigned char)password[i]) << 1;
630 for (; i<8; i++)
631 key[i] = 0;
632 }
633
634 void
ext_password_to_key(des_cblock key,const char * password,size_t passwordlen)635 ext_password_to_key(
636 des_cblock key,
637 const char *password,
638 size_t passwordlen)
639 {
640 int i;
641 trad_password_to_key(key, password, passwordlen);
642 while(passwordlen > 8)
643 {
644 password += 8;
645 passwordlen -= 8;
646 crypt_rounds(key, 1, 0, key);
647 for(i=0; i<8 && i<passwordlen; i++)
648 key[i] ^= ((unsigned char)password[i]) << 1;
649 }
650 }
651
652 void
des_fcrypt(const char * password,size_t passwordlen,const char * salt,size_t saltlen,char * outbuf)653 des_fcrypt(
654 const char *password,
655 size_t passwordlen,
656 const char *salt,
657 size_t saltlen,
658 char *outbuf)
659 {
660 unsigned long saltnum;
661 unsigned long nrounds;
662 des_cblock block, key;
663
664 if(saltlen == 0)
665 {
666 /* empty salt string indicates no password required */
667 outbuf[0] = 0;
668 return;
669 }
670
671 if(salt[0] == '_' && saltlen >= 9)
672 {
673 nrounds = base64_to_int24(salt + 1);
674 saltnum = base64_to_int24(salt + 5);
675 ext_password_to_key(key, password, passwordlen);
676 outbuf[0] = '_';
677 int24_to_base64(nrounds, outbuf + 1);
678 int24_to_base64(saltnum, outbuf + 5);
679 outbuf += 9;
680 }
681 else
682 {
683 nrounds = 25;
684 saltnum = base64_to_int12(salt);
685 trad_password_to_key(key, password, passwordlen);
686 int12_to_base64(saltnum, outbuf);
687 outbuf += 2;
688 }
689
690 memset(block, 0, sizeof(block));
691 crypt_rounds(key, nrounds, saltnum, block);
692 block_to_base64(block, outbuf);
693 }
694
695