1 | #include <stdlib.h>
|
---|
2 | #include <stdio.h>
|
---|
3 | #include <string.h>
|
---|
4 | #include "des.h"
|
---|
5 |
|
---|
6 | #define CRYPT 0
|
---|
7 | #define HASH 1
|
---|
8 |
|
---|
9 | #define F_EURO_S2 0
|
---|
10 | #define F_TRIPLE_DES 1
|
---|
11 |
|
---|
12 | #define TestBit(addr, bit) ((addr) & (1 << bit))
|
---|
13 |
|
---|
14 | static unsigned char PC2[8][6] =
|
---|
15 | {
|
---|
16 | { 14, 17, 11, 24, 1, 5 },
|
---|
17 | { 3, 28, 15, 6, 21, 10 },
|
---|
18 | { 23, 19, 12, 4, 26, 8 },
|
---|
19 | { 16, 7, 27, 20, 13, 2 },
|
---|
20 | { 41, 52, 31, 37, 47, 55 },
|
---|
21 | { 30, 40, 51, 45, 33, 48 },
|
---|
22 | { 44, 49, 39, 56, 34, 53 },
|
---|
23 | { 46, 42, 50, 36, 29, 32 }
|
---|
24 | };
|
---|
25 |
|
---|
26 |
|
---|
27 | static unsigned char E[8][6] =
|
---|
28 | {
|
---|
29 | { 32, 1, 2, 3, 4, 5 },
|
---|
30 | { 4, 5, 6, 7, 8, 9 },
|
---|
31 | { 8, 9, 10, 11, 12, 13 },
|
---|
32 | { 12, 13, 14, 15, 16, 17 },
|
---|
33 | { 16, 17, 18, 19, 20, 21 },
|
---|
34 | { 20, 21, 22, 23, 24, 25 },
|
---|
35 | { 24, 25, 26, 27, 28, 29 },
|
---|
36 | { 28, 29, 30, 31, 32, 1 }
|
---|
37 | };
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 | static unsigned char P[32] =
|
---|
42 | {
|
---|
43 | 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
|
---|
44 | 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
|
---|
45 | };
|
---|
46 |
|
---|
47 |
|
---|
48 | static unsigned char SBOXES[4][64] =
|
---|
49 | {
|
---|
50 | {
|
---|
51 | 0x2e, 0xe0, 0xc4, 0xbf, 0x4d, 0x27, 0x11, 0xc4,
|
---|
52 | 0x72, 0x4e, 0xaf, 0x72, 0xbb, 0xdd, 0x68, 0x11,
|
---|
53 | 0x83, 0x5a, 0x5a, 0x06, 0x36, 0xfc, 0xfc, 0xab,
|
---|
54 | 0xd5, 0x39, 0x09, 0x95, 0xe0, 0x83, 0x97, 0x68,
|
---|
55 | 0x44, 0xbf, 0x21, 0x8c, 0x1e, 0xc8, 0xb8, 0x72,
|
---|
56 | 0xad, 0x14, 0xd6, 0xe9, 0x72, 0x21, 0x8b, 0xd7,
|
---|
57 | 0xff, 0x65, 0x9c, 0xfb, 0xc9, 0x03, 0x57, 0x9e,
|
---|
58 | 0x63, 0xaa, 0x3a, 0x40, 0x05, 0x56, 0xe0, 0x3d
|
---|
59 | },
|
---|
60 | {
|
---|
61 | 0xcf, 0xa3, 0x11, 0xfd, 0xa8, 0x44, 0xfe, 0x27,
|
---|
62 | 0x96, 0x7f, 0x2b, 0xc2, 0x63, 0x98, 0x84, 0x5e,
|
---|
63 | 0x09, 0x6c, 0xd7, 0x10, 0x32, 0xd1, 0x4d, 0xea,
|
---|
64 | 0xec, 0x06, 0x70, 0xb9, 0x55, 0x3b, 0xba, 0x85,
|
---|
65 | 0x90, 0x4d, 0xee, 0x38, 0xf7, 0x2a, 0x5b, 0xc1,
|
---|
66 | 0x2a, 0x93, 0x84, 0x5f, 0xcd, 0xf4, 0x31, 0xa2,
|
---|
67 | 0x75, 0xbb, 0x08, 0xe6, 0x4c, 0x17, 0xa6, 0x7c,
|
---|
68 | 0x19, 0x60, 0xd3, 0x05, 0xb2, 0x8e, 0x6f, 0xd9
|
---|
69 | },
|
---|
70 | {
|
---|
71 | 0x4a, 0xdd, 0xb0, 0x07, 0x29, 0xb0, 0xee, 0x79,
|
---|
72 | 0xf6, 0x43, 0x03, 0x94, 0x8f, 0x16, 0xd5, 0xaa,
|
---|
73 | 0x31, 0xe2, 0xcd, 0x38, 0x9c, 0x55, 0x77, 0xce,
|
---|
74 | 0x5b, 0x2c, 0xa4, 0xfb, 0x62, 0x8f, 0x18, 0x61,
|
---|
75 | 0x1d, 0x61, 0x46, 0xba, 0xb4, 0xdd, 0xd9, 0x80,
|
---|
76 | 0xc8, 0x16, 0x3f, 0x49, 0x73, 0xa8, 0xe0, 0x77,
|
---|
77 | 0xab, 0x94, 0xf1, 0x5f, 0x62, 0x0e, 0x8c, 0xf3,
|
---|
78 | 0x05, 0xeb, 0x5a, 0x25, 0x9e, 0x32, 0x27, 0xcc
|
---|
79 | },
|
---|
80 | {
|
---|
81 | 0xd7, 0x1d, 0x2d, 0xf8, 0x8e, 0xdb, 0x43, 0x85,
|
---|
82 | 0x60, 0xa6, 0xf6, 0x3f, 0xb9, 0x70, 0x1a, 0x43,
|
---|
83 | 0xa1, 0xc4, 0x92, 0x57, 0x38, 0x62, 0xe5, 0xbc,
|
---|
84 | 0x5b, 0x01, 0x0c, 0xea, 0xc4, 0x9e, 0x7f, 0x29,
|
---|
85 | 0x7a, 0x23, 0xb6, 0x1f, 0x49, 0xe0, 0x10, 0x76,
|
---|
86 | 0x9c, 0x4a, 0xcb, 0xa1, 0xe7, 0x8d, 0x2d, 0xd8,
|
---|
87 | 0x0f, 0xf9, 0x61, 0xc4, 0xa3, 0x95, 0xde, 0x0b,
|
---|
88 | 0xf5, 0x3c, 0x32, 0x57, 0x58, 0x62, 0x84, 0xbe
|
---|
89 | }
|
---|
90 | };
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | static unsigned char PC1[][8] =
|
---|
95 | {
|
---|
96 | {57, 49, 41, 33, 25, 17, 9, 1},
|
---|
97 | {58, 50, 42, 34, 26, 18, 10, 2},
|
---|
98 | {59, 51, 43, 35, 27, 19, 11, 3},
|
---|
99 | {60, 52, 44, 36, 63, 55, 47,39},
|
---|
100 | {31, 23, 15, 7, 62, 54, 46,38},
|
---|
101 | {30, 22, 14, 6, 61, 53, 45,37},
|
---|
102 | {29, 21, 13, 5, 28, 20, 12, 4}
|
---|
103 | };
|
---|
104 |
|
---|
105 |
|
---|
106 | void doPC1(unsigned char data[])
|
---|
107 | {
|
---|
108 | unsigned char buf[8];
|
---|
109 | unsigned char i, j;
|
---|
110 |
|
---|
111 | memset(buf, 0, 8);
|
---|
112 |
|
---|
113 | for(j=0; j<7; j++) {
|
---|
114 | for(i=0; i<8; i++) {
|
---|
115 | unsigned char lookup = PC1[j][i];
|
---|
116 | buf[j] |= ((data[(lookup>>3)]>>(8-(lookup & 7))) & 1) << (7-i);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | memcpy(data, buf, 8);
|
---|
121 | }
|
---|
122 |
|
---|
123 | static void doIp(unsigned char data[])
|
---|
124 | {
|
---|
125 | unsigned char j, k;
|
---|
126 | unsigned char val;
|
---|
127 | unsigned char buf[8];
|
---|
128 | unsigned char *p;
|
---|
129 | unsigned char i = 8;
|
---|
130 |
|
---|
131 | for(i=0; i<8; i++)
|
---|
132 | {
|
---|
133 | val = data[i];
|
---|
134 | p = &buf[3];
|
---|
135 | j = 4;
|
---|
136 |
|
---|
137 | do
|
---|
138 | {
|
---|
139 | for(k=0; k<=4; k+=4)
|
---|
140 | {
|
---|
141 | p[k] >>= 1;
|
---|
142 | if(val & 1) p[k] |= 0x80;
|
---|
143 | val >>= 1;
|
---|
144 | }
|
---|
145 | p--;
|
---|
146 | } while(--j);
|
---|
147 | }
|
---|
148 |
|
---|
149 | memcpy(data, buf, 8);
|
---|
150 | }
|
---|
151 |
|
---|
152 | static void doIp_1(unsigned char data[])
|
---|
153 | {
|
---|
154 | unsigned char j, k;
|
---|
155 | unsigned char r = 0;
|
---|
156 | unsigned char buf[8];
|
---|
157 | unsigned char *p;
|
---|
158 | unsigned char i = 8;
|
---|
159 |
|
---|
160 | for(i=0; i<8; i++)
|
---|
161 | {
|
---|
162 | p = &data[3];
|
---|
163 | j = 4;
|
---|
164 |
|
---|
165 | do
|
---|
166 | {
|
---|
167 | for(k=0; k<=4; k+=4)
|
---|
168 | {
|
---|
169 | r >>= 1;
|
---|
170 | if(p[k] & 1) r |= 0x80;
|
---|
171 | p[k] >>= 1;
|
---|
172 | }
|
---|
173 | p--;
|
---|
174 | } while(--j);
|
---|
175 | buf[i] = r;
|
---|
176 | }
|
---|
177 |
|
---|
178 | memcpy(data, buf, 8);
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 |
|
---|
183 | static void makeK(unsigned char *left, unsigned char *right, unsigned char *K)
|
---|
184 | {
|
---|
185 | unsigned char i, j;
|
---|
186 | unsigned char bit, val;
|
---|
187 | unsigned char *p;
|
---|
188 |
|
---|
189 | for(i=0; i<8; i++)
|
---|
190 | {
|
---|
191 | val = 0;
|
---|
192 | for(j=0; j<6; j++)
|
---|
193 | {
|
---|
194 | bit = PC2[i][j];
|
---|
195 | if(bit < 29)
|
---|
196 | {
|
---|
197 | bit = 28-bit;
|
---|
198 | p = left;
|
---|
199 | }
|
---|
200 | else
|
---|
201 | {
|
---|
202 | bit = 56-bit;
|
---|
203 | p = right;
|
---|
204 | }
|
---|
205 | val <<= 1;
|
---|
206 | if( p[bit >> 3] & (1 << (bit & 7)) ) val |= 1;
|
---|
207 | }
|
---|
208 | *K = val;
|
---|
209 | K++;
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | static void rightRot(unsigned char key[])
|
---|
214 | {
|
---|
215 | unsigned char *p = key;
|
---|
216 | unsigned char i = 3;
|
---|
217 | unsigned char carry = 0;
|
---|
218 |
|
---|
219 | carry = 0;
|
---|
220 |
|
---|
221 | if(*p & 1) carry = 0x08;
|
---|
222 |
|
---|
223 | do {
|
---|
224 | *p = (*p >> 1) | ((p[1] & 1) ? 0x80 : 0);
|
---|
225 | p++;
|
---|
226 | } while(--i);
|
---|
227 |
|
---|
228 | *p = (*p >> 1) | carry;
|
---|
229 | }
|
---|
230 |
|
---|
231 | static void rightRotKeys(unsigned char left[], unsigned char right[])
|
---|
232 | {
|
---|
233 | rightRot(left);
|
---|
234 | rightRot(right);
|
---|
235 | }
|
---|
236 |
|
---|
237 | static void leftRot(unsigned char key[])
|
---|
238 | {
|
---|
239 | unsigned char i = 27;
|
---|
240 |
|
---|
241 | do {
|
---|
242 | rightRot(key);
|
---|
243 | } while(--i);
|
---|
244 | }
|
---|
245 |
|
---|
246 | static void leftRotKeys(unsigned char left[], unsigned char right[])
|
---|
247 | {
|
---|
248 | leftRot(left);
|
---|
249 | leftRot(right);
|
---|
250 | }
|
---|
251 |
|
---|
252 | static void desCore(unsigned char data[], unsigned char K[], unsigned char result[])
|
---|
253 | {
|
---|
254 | unsigned char i, j;
|
---|
255 | unsigned char bit, val;
|
---|
256 |
|
---|
257 | memset(result, 0, 4);
|
---|
258 |
|
---|
259 | for(i=0; i<8; i++)
|
---|
260 | {
|
---|
261 | val = 0;
|
---|
262 | for(j=0; j<6; j++)
|
---|
263 | {
|
---|
264 | bit = 32-E[i][j];
|
---|
265 | val <<= 1;
|
---|
266 | if( data[3 - (bit >> 3)] & (1 << (bit & 7)) ) val |= 1;
|
---|
267 | }
|
---|
268 | val ^= K[i];
|
---|
269 | val = SBOXES[i & 3][val];
|
---|
270 | if(i > 3)
|
---|
271 | {
|
---|
272 | val >>= 4;
|
---|
273 | }
|
---|
274 | val &= 0x0f;
|
---|
275 | result[i >> 1] |= (i & 1) ? val : (val << 4);
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | static void permut32(unsigned char data[])
|
---|
280 | {
|
---|
281 | unsigned char i, j;
|
---|
282 | unsigned char bit;
|
---|
283 | unsigned char r[4];
|
---|
284 | unsigned char *p;
|
---|
285 |
|
---|
286 | for(i=0; i<32; i++)
|
---|
287 | {
|
---|
288 | bit = 32-P[i];
|
---|
289 | p = r;
|
---|
290 | for(j=0; j<3; j++)
|
---|
291 | {
|
---|
292 | *p = (*p << 1) | ((p[1] & 0x80) ? 1 : 0);
|
---|
293 | p++;
|
---|
294 | }
|
---|
295 | *p <<= 1;
|
---|
296 | if( data[3 - (bit >> 3)] & (1 << (bit & 7)) ) *p |= 1;
|
---|
297 | }
|
---|
298 |
|
---|
299 | memcpy(data, r, 4);
|
---|
300 | }
|
---|
301 |
|
---|
302 | static void swap(unsigned char left[], unsigned char right[])
|
---|
303 | {
|
---|
304 | unsigned char x[4];
|
---|
305 |
|
---|
306 | memcpy(x, right, 4);
|
---|
307 | memcpy(right, left, 4);
|
---|
308 | memcpy(left, x, 4);
|
---|
309 | }
|
---|
310 |
|
---|
311 | static void desRound(unsigned char left[], unsigned char right[], unsigned char data[], unsigned char mode, unsigned char k8)
|
---|
312 | {
|
---|
313 | unsigned char i;
|
---|
314 | unsigned char K[8];
|
---|
315 | unsigned char r[4];
|
---|
316 | unsigned char tempr[4];
|
---|
317 | unsigned short temp;
|
---|
318 |
|
---|
319 | memcpy(tempr, data+4, 4);
|
---|
320 |
|
---|
321 | /* Viaccess */
|
---|
322 | temp = (short)k8*(short)tempr[0]+(short)k8+(short)tempr[0];
|
---|
323 | tempr[0] = (temp & 0xff) - ((temp>>8) & 0xff);
|
---|
324 | if((temp & 0xff) - (temp>>8) < 0)
|
---|
325 | tempr[0]++;
|
---|
326 |
|
---|
327 | makeK(left, right, K);
|
---|
328 | desCore(tempr, K, r);
|
---|
329 | permut32(r);
|
---|
330 |
|
---|
331 | if(mode & DES_HASH)
|
---|
332 | {
|
---|
333 | i = r[0];
|
---|
334 | r[0] = r[1];
|
---|
335 | r[1] = i;
|
---|
336 | }
|
---|
337 |
|
---|
338 | for(i=0; i<4; i++)
|
---|
339 | {
|
---|
340 | *data ^= r[i];
|
---|
341 | data++;
|
---|
342 | }
|
---|
343 |
|
---|
344 | swap(data-4, data);
|
---|
345 | }
|
---|
346 |
|
---|
347 | void des(unsigned char key[], unsigned char mode, unsigned char data[])
|
---|
348 | {
|
---|
349 | unsigned char i;
|
---|
350 | unsigned char left[8];
|
---|
351 | unsigned char right[8];
|
---|
352 | unsigned char *p = left;
|
---|
353 |
|
---|
354 | short DESShift = (mode & DES_RIGHT) ? 0x8103 : 0xc081;
|
---|
355 |
|
---|
356 | for(i=3; i>0; i--)
|
---|
357 | {
|
---|
358 | *p = (key[i-1] << 4) | (key[i] >> 4);
|
---|
359 | p++;
|
---|
360 | }
|
---|
361 | left[3] = key[0] >> 4;
|
---|
362 | right[0] = key[6];
|
---|
363 | right[1] = key[5];
|
---|
364 | right[2] = key[4];
|
---|
365 | right[3] = key[3] & 0x0f;
|
---|
366 |
|
---|
367 | if(mode & DES_IP) doIp(data);
|
---|
368 |
|
---|
369 | do {
|
---|
370 | if(!(mode & DES_RIGHT))
|
---|
371 | {
|
---|
372 | leftRotKeys(left, right);
|
---|
373 | if(!(DESShift & 0x8000)) leftRotKeys(left, right);
|
---|
374 | }
|
---|
375 | desRound(left, right, data, mode, key[7]);
|
---|
376 |
|
---|
377 | if(mode & DES_RIGHT)
|
---|
378 | {
|
---|
379 | rightRotKeys(left, right);
|
---|
380 | if(!(DESShift & 0x8000)) rightRotKeys(left, right);
|
---|
381 | }
|
---|
382 | DESShift <<= 1;
|
---|
383 | } while(DESShift);
|
---|
384 |
|
---|
385 | swap(data, data+4);
|
---|
386 | if(mode & DES_IP_1) doIp_1(data);
|
---|
387 |
|
---|
388 | }
|
---|
389 |
|
---|
390 | static unsigned char getmask(unsigned char *OutData, unsigned char *Mask, unsigned char I, unsigned char J)
|
---|
391 | {
|
---|
392 | unsigned char K, B, M, M1 , D, DI, MI;
|
---|
393 |
|
---|
394 | K = I ^ J;
|
---|
395 | DI = 7;
|
---|
396 | if ((K & 4) == 4) {
|
---|
397 | K ^= 7;
|
---|
398 | DI ^= 7;
|
---|
399 | }
|
---|
400 | MI = 3;
|
---|
401 | MI &= J;
|
---|
402 | K ^= MI;
|
---|
403 | K += MI;
|
---|
404 | if ((K & 4) == 4) {
|
---|
405 | return 0;
|
---|
406 | }
|
---|
407 | DI ^= J;
|
---|
408 | D = OutData[DI];
|
---|
409 | MI = 0;
|
---|
410 | MI += J;
|
---|
411 | M1 = Mask[MI];
|
---|
412 | MI ^= 4;
|
---|
413 | M = Mask[MI];
|
---|
414 | B = 0;
|
---|
415 | for(K = 0; K <=7; K++)
|
---|
416 | {
|
---|
417 | if ((D & 1) == 1) B += M;
|
---|
418 | D = (D >> 1) + ((B & 1) << 7);
|
---|
419 | B = B >> 1;
|
---|
420 | }
|
---|
421 | return D ^ M1;
|
---|
422 | }
|
---|
423 |
|
---|
424 | static void v2mask(unsigned char *cw, unsigned char *mask)
|
---|
425 | {
|
---|
426 | int i, j;
|
---|
427 |
|
---|
428 | for(i = 7; i >= 0; i--)
|
---|
429 | for(j = 7; j >=4; j--)
|
---|
430 | cw[i] ^= getmask(cw, mask, i, j);
|
---|
431 | for(i = 0; i <= 7; i++)
|
---|
432 | for(j = 0; j <=3; j++)
|
---|
433 | cw[i] ^= getmask(cw, mask, i, j);
|
---|
434 | }
|
---|
435 |
|
---|
436 |
|
---|
437 | static void EuroDes(unsigned char key1[], unsigned char key2[], unsigned char desMode, unsigned char operatingMode, unsigned char data[])
|
---|
438 | {
|
---|
439 | unsigned char mode;
|
---|
440 |
|
---|
441 | if(key1[7]) { /* Viaccess */
|
---|
442 | mode = (operatingMode == HASH) ? DES_ECM_HASH : DES_ECM_CRYPT;
|
---|
443 |
|
---|
444 | if(key2 != NULL)
|
---|
445 | v2mask(data, key2);
|
---|
446 | des(key1, mode, data);
|
---|
447 | if(key2 != NULL)
|
---|
448 | v2mask(data, key2);
|
---|
449 | }
|
---|
450 | else if(TestBit(desMode, F_TRIPLE_DES))
|
---|
451 | {
|
---|
452 | /* Eurocrypt 3-DES */
|
---|
453 | mode = (operatingMode == HASH) ? 0 : DES_RIGHT;
|
---|
454 | des(key1, (unsigned char)(DES_IP | mode), data);
|
---|
455 |
|
---|
456 | mode ^= DES_RIGHT;
|
---|
457 | des(key2, mode, data);
|
---|
458 |
|
---|
459 | mode ^= DES_RIGHT;
|
---|
460 | des(key1, (unsigned char)(mode | DES_IP_1), data);
|
---|
461 | }
|
---|
462 | else
|
---|
463 | {
|
---|
464 | if(TestBit(desMode, F_EURO_S2))
|
---|
465 | {
|
---|
466 | /* Eurocrypt S2 */
|
---|
467 | mode = (operatingMode == HASH) ? DES_ECS2_CRYPT : DES_ECS2_DECRYPT;
|
---|
468 | }
|
---|
469 | else
|
---|
470 | {
|
---|
471 | /* Eurocrypt M */
|
---|
472 | mode = (operatingMode == HASH) ? DES_ECM_HASH : DES_ECM_CRYPT;
|
---|
473 | }
|
---|
474 | des(key1, mode, data);
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 | /*------------------------------------------------------------------------*/
|
---|
479 | static void des_key_parity_adjust(unsigned char *key, unsigned char len)
|
---|
480 | {
|
---|
481 | unsigned char i, j, parity;
|
---|
482 |
|
---|
483 | for (i = 0; i < len; i++)
|
---|
484 | {
|
---|
485 | parity = 1;
|
---|
486 | for (j = 1; j < 8; j++) if ((key[i] >> j) & 0x1) parity = ~parity & 0x01;
|
---|
487 | key[i] |= parity;
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | static unsigned char *des_key_spread(unsigned char *normal, unsigned char *spread)
|
---|
492 | {
|
---|
493 | spread[ 0] = normal[ 0] & 0xfe;
|
---|
494 | spread[ 1] = ((normal[ 0] << 7) | (normal[ 1] >> 1)) & 0xfe;
|
---|
495 | spread[ 2] = ((normal[ 1] << 6) | (normal[ 2] >> 2)) & 0xfe;
|
---|
496 | spread[ 3] = ((normal[ 2] << 5) | (normal[ 3] >> 3)) & 0xfe;
|
---|
497 | spread[ 4] = ((normal[ 3] << 4) | (normal[ 4] >> 4)) & 0xfe;
|
---|
498 | spread[ 5] = ((normal[ 4] << 3) | (normal[ 5] >> 5)) & 0xfe;
|
---|
499 | spread[ 6] = ((normal[ 5] << 2) | (normal[ 6] >> 6)) & 0xfe;
|
---|
500 | spread[ 7] = normal[ 6] << 1;
|
---|
501 | spread[ 8] = normal[ 7] & 0xfe;
|
---|
502 | spread[ 9] = ((normal[ 7] << 7) | (normal[ 8] >> 1)) & 0xfe;
|
---|
503 | spread[10] = ((normal[ 8] << 6) | (normal[ 9] >> 2)) & 0xfe;
|
---|
504 | spread[11] = ((normal[ 9] << 5) | (normal[10] >> 3)) & 0xfe;
|
---|
505 | spread[12] = ((normal[10] << 4) | (normal[11] >> 4)) & 0xfe;
|
---|
506 | spread[13] = ((normal[11] << 3) | (normal[12] >> 5)) & 0xfe;
|
---|
507 | spread[14] = ((normal[12] << 2) | (normal[13] >> 6)) & 0xfe;
|
---|
508 | spread[15] = normal[13] << 1;
|
---|
509 |
|
---|
510 | des_key_parity_adjust(spread, 16);
|
---|
511 | return spread;
|
---|
512 | }
|
---|
513 |
|
---|
514 | static void des_random_get(unsigned char *buffer, unsigned char len)
|
---|
515 | {
|
---|
516 | unsigned char idx = 0;
|
---|
517 | int randomNo = 0;
|
---|
518 |
|
---|
519 | for (idx = 0; idx < len; idx++)
|
---|
520 | {
|
---|
521 | if (!(idx % 3)) randomNo = rand();
|
---|
522 | buffer[idx] = (randomNo >> ((idx % 3) << 3)) & 0xff;
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | #define CWS_NETMSGSIZE 272
|
---|
527 |
|
---|
528 | int des_encrypt(unsigned char *buffer, int len, unsigned char *deskey)
|
---|
529 | {
|
---|
530 | unsigned char checksum = 0;
|
---|
531 | unsigned char noPadBytes;
|
---|
532 | unsigned char padBytes[7];
|
---|
533 | char ivec[8];
|
---|
534 | short i;
|
---|
535 |
|
---|
536 | if (!deskey) return len;
|
---|
537 | noPadBytes = (8 - ((len - 1) % 8)) % 8;
|
---|
538 | if (len + noPadBytes + 1 >= CWS_NETMSGSIZE-8) return -1;
|
---|
539 | des_random_get(padBytes, noPadBytes);
|
---|
540 | for (i = 0; i < noPadBytes; i++) buffer[len++] = padBytes[i];
|
---|
541 | for (i = 2; i < len; i++) checksum ^= buffer[i];
|
---|
542 | buffer[len++] = checksum;
|
---|
543 | des_random_get((unsigned char *)ivec, 8);
|
---|
544 | memcpy(buffer+len, ivec, 8);
|
---|
545 | for (i = 2; i < len; i += 8)
|
---|
546 | {
|
---|
547 | unsigned char j;
|
---|
548 | const unsigned char flags = (1 << F_EURO_S2) | (1 << F_TRIPLE_DES);
|
---|
549 | for(j=0; j<8; j++) buffer[i+j] ^= ivec[j];
|
---|
550 | EuroDes(deskey, deskey+8, flags, HASH, buffer+i);
|
---|
551 | memcpy(ivec, buffer+i, 8);
|
---|
552 | }
|
---|
553 | len += 8;
|
---|
554 | return len;
|
---|
555 | }
|
---|
556 |
|
---|
557 | int des_decrypt(unsigned char *buffer, int len, unsigned char *deskey)
|
---|
558 | {
|
---|
559 | char ivec[8];
|
---|
560 | char nextIvec[8];
|
---|
561 | int i;
|
---|
562 | unsigned char checksum = 0;
|
---|
563 |
|
---|
564 | if (!deskey) return len;
|
---|
565 | if ((len-2) % 8 || (len-2) < 16) return -1;
|
---|
566 | len -= 8;
|
---|
567 | memcpy(nextIvec, buffer+len, 8);
|
---|
568 | for (i = 2; i < len; i += 8)
|
---|
569 | {
|
---|
570 | unsigned char j;
|
---|
571 | const unsigned char flags = (1 << F_EURO_S2) | (1 << F_TRIPLE_DES);
|
---|
572 |
|
---|
573 | memcpy(ivec, nextIvec, 8);
|
---|
574 | memcpy(nextIvec, buffer+i, 8);
|
---|
575 | EuroDes(deskey, deskey+8, flags, CRYPT, buffer+i);
|
---|
576 | for(j=0; j<8; j++)
|
---|
577 | buffer[i+j] ^= ivec[j];
|
---|
578 | }
|
---|
579 | for (i = 2; i < len; i++) checksum ^= buffer[i];
|
---|
580 | if (checksum) return -1;
|
---|
581 | return len;
|
---|
582 | }
|
---|
583 |
|
---|
584 | unsigned char *des_login_key_get(unsigned char *key1, unsigned char *key2, int len, unsigned char *des16)
|
---|
585 | {
|
---|
586 | unsigned char des14[14];
|
---|
587 | int i;
|
---|
588 |
|
---|
589 | memcpy(des14, key1, sizeof(des14));
|
---|
590 | for (i = 0; i < len; i++) des14[i%14] ^= key2[i];
|
---|
591 | des16 = des_key_spread(des14, des16);
|
---|
592 | doPC1(des16);
|
---|
593 | doPC1(des16+8);
|
---|
594 | return des16;
|
---|
595 | }
|
---|