aboutsummaryrefslogtreecommitdiff
path: root/src/des/des.c
blob: 5ff459e394da425b13e89cc405bfd29729712217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576

/* Sofware DES functions
 * written 12 Dec 1986 by Phil Karn, KA9Q; large sections adapted from
 * the 1977 public-domain program by Jim Gillogly
 * Modified for additional speed - 6 December 1988 Phil Karn
 * Modified for parameterized key schedules - Jan 1991 Phil Karn
 * Callers now allocate a key schedule as follows:
 *	kn = (char (*)[8])malloc(sizeof(char) * 8 * 16);
 *	or
 *	char kn[16][8];
 */

/* modified in order to use the libmcrypt API by Nikos Mavroyanopoulos 
 * All modifications are placed under the license of libmcrypt.
 */

/* $Id: des.c,v 1.13 2003/01/19 17:48:27 nmav Exp $ */


#include <stdio.h>

#include "..\JonahHex.h"
#include "des.h"

/* #define	NULL	0 */

static void permute_ip(), permute_fp(), perminit_ip(), spinit(),
perminit_fp();
static uint32_t f();


/* Tables defined in the Data Encryption Standard documents */

/* initial permutation IP */
char ip[] = {
	'\xCF', '\xEF', '\xEF', '\x8F', '\xD5', '\xDD', '\xA5', '\xED',
	'\xC7', '\xB7', '\xE7', '\xD7', '\x8B', '\xD3', '\xFB', '\xE3',
	'\xCF', '\xEF', '\xEF', '\x8F', '\xD1', '\xD9', '\xA1', '\xE9',
	'\xC7', '\xB7', '\xE7', '\xD7', '\xF7', '\xDF', '\xE7', '\xEF',
	'\xFF', '\xFF', '\xFF', '\xFF', '\xC6', '\xCE', '\xD6', '\xDE',
	'\xD7', '\xC7', '\xD7', '\xC7', '\xFC', '\xE4', '\xEC', '\xF4',
	'\xFF', '\xFF', '\xFF', '\xFF', '\xC2', '\xCA', '\xD2', '\xDA',
	'\xD7', '\xC7', '\xD7', '\xC7', '\xF8', '\xE0', '\xE8', '\xF0'
};

/* final permutation IP^-1 */
char fp[] = {
	'\x9F', '\xCF', '\xFF', '\xEF', '\xE7', '\xF7', '\xDF', '\x8F',
	'\xCF', '\xC7', '\xCF', '\xC7', '\xE0', '\xC8', '\xE8', '\xC0',
	'\xFF', '\xFF', '\xFF', '\xFF', '\xD9', '\xF9', '\xD1', '\xF1',
	'\xCF', '\xC7', '\xCF', '\xC7', '\xE2', '\xCA', '\xEA', '\xC2',
	'\xFF', '\xFF', '\xFF', '\xFF', '\xDB', '\xFB', '\xD3', '\xF3',
	'\xCF', '\xC7', '\xCF', '\xC7', '\xE4', '\xCC', '\xEC', '\xC4',
	'\xFF', '\xFF', '\xFF', '\xFF', '\xDD', '\xFD', '\xD5', '\xF5',
	'\xCF', '\xC7', '\xCF', '\xC7', '\xE6', '\xCE', '\xEE', '\xC6'
};

/* expansion operation matrix
 * This is for reference only; it is unused in the code
 * as the f() function performs it implicitly for speed
 */
#ifdef notdef
static char ei[] = {
	32, 1, 2, 3, 4, 5,
	4, 5, 6, 7, 8, 9,
	8, 9, 10, 11, 12, 13,
	12, 13, 14, 15, 16, 17,
	16, 17, 18, 19, 20, 21,
	20, 21, 22, 23, 24, 25,
	24, 25, 26, 27, 28, 29,
	28, 29, 30, 31, 32, 1
};
#endif

/* permuted choice table (key) */
char pc1[] = {
	'\xD7', '\xC7', '\xD7', '\xC7', '\xFE', '\xE6', '\xEE',
	'\xF6', '\xFF', '\xFF', '\xFF', '\xFF', '\xC5', '\xCD',
	'\xD5', '\xDD', '\xD7', '\xC7', '\xD7', '\xC7', '\xFC',
	'\xE4', '\xEC', '\xF4', '\xFF', '\xFF', '\xFF', '\xFF',

	'\xC3', '\xCB', '\xD3', '\xDB', '\xF4', '\xE4', '\xF4',
	'\xE4', '\xFB', '\xE3', '\xEB', '\xF3', '\xDD', '\xDD',
	'\xDD', '\xDD', '\xC3', '\xCB', '\xD3', '\xDB', '\xF6',
	'\xE6', '\xF6', '\xE6', '\xFB', '\xE3', '\xEB', '\xF3'
};

/* number left rotations of pc1 */
char totrot[] = {
	'\xF3', '\xFA', '\xFB', '\xE2', '\xFB', '\xF9', '\xE6', '\xF5', 
	'\xF1', '\xEC', '\xF9', '\xF5', '\xE3', '\xE8', '\xE6', '\xE4'
};

/* permuted choice key (table) */
char pc2[] = {
	'\xF4', '\xDA', '\xF7', '\xED', '\xD4', '\xE6',
	'\xE6', '\xEC', '\xE7', '\xE3', '\xEF', '\xC8',
	'\xEC', '\xE9', '\xDD', '\xED', '\xFE', '\xCE',
	'\xE1', '\xE8', '\xD4', '\xE9', '\xF8', '\xF9',
	'\xF2', '\xEC', '\xF4', '\xD6', '\xC5', '\xC0',
	'\xC9', '\xD7', '\xEF', '\xFE', '\xC9', '\xED',
	'\xCD', '\xE4', '\xCC', '\xDF', '\xC8', '\xFD',
	'\xF8', '\xF5', '\xDF', '\xCD', '\xDB', '\xE2'
};

/* The (in)famous S-boxes */
char si[8][64] = {
	/* S1 */
	{'\xFB', '\xF9', '\xF2', '\xF0', '\xF7', '\xF6', '\xFD', '\xFA',
	 '\xF4', '\xF2', '\xF1', '\xFF', '\xF1', '\xFB', '\xF9', '\xF8',
	 '\xFE', '\xF0', '\xF8', '\xF6', '\xF0', '\xF7', '\xF1', '\xFA',
	 '\xF9', '\xF3', '\xF7', '\xF1', '\xF9', '\xF1', '\xFD', '\xF2',
	 '\xF8', '\xF8', '\xF1', '\xF1', '\xFC', '\xF0', '\xFF', '\xF0',
	 '\xFD', '\xF9', '\xF2', '\xF7', '\xF6', '\xFE', '\xFE', '\xFA',
	 '\xFD', '\xF4', '\xFF', '\xF6', '\xFB', '\xF3', '\xFE', '\xFF',
	 '\xF7', '\xF7', '\xFE', '\xFC', '\xF2', '\xF5', '\xFF', '\xF9'},

	/* S2 */
	{'\xF4', '\xF0', '\xFA', '\xFE', '\xFF', '\xFB', '\xF6', '\xFA',
	 '\xFD', '\xFD', '\xF9', '\xFB', '\xF4', '\xFE', '\xF9', '\xF0',
	 '\xF2', '\xF9', '\xF5', '\xFE', '\xFA', '\xF8', '\xFA', '\xF5',
	 '\xF7', '\xF8', '\xFD', '\xF4', '\xF4', '\xFD', '\xF5', '\xFD',
	 '\xFB', '\xFC', '\xF6', '\xF1', '\xFC', '\xF8', '\xF6', '\xF0',
	 '\xF2', '\xF2', '\xFD', '\xFD', '\xF7', '\xF5', '\xF1', '\xF4',
	 '\xFC', '\xF2', '\xF6', '\xF8', '\xFF', '\xFE', '\xF2', '\xFD',
	 '\xFD', '\xFD', '\xF9', '\xFD', '\xF6', '\xFF', '\xFA', '\xF1'},

	/* S3 */
	{'\xF1', '\xF9', '\xF8', '\xF0', '\xF3', '\xF8', '\xF9', '\xFF',
	 '\xFE', '\xFA', '\xF0', '\xF6', '\xFB', '\xFD', '\xFA', '\xF9',
	 '\xF6', '\xFE', '\xFB', '\xF3', '\xF3', '\xFC', '\xF3', '\xFF',
	 '\xFF', '\xF7', '\xF9', '\xF9', '\xF5', '\xF1', '\xFC', '\xF1',
	 '\xFD', '\xF7', '\xFE', '\xF4', '\xFA', '\xF8', '\xF0', '\xF4',
	 '\xF0', '\xFF', '\xF7', '\xFA', '\xF4', '\xF6', '\xF8', '\xFC',
	 '\xF3', '\xF2', '\xF2', '\xF3', '\xF3', '\xF8', '\xFE', '\xF3',
	 '\xFE', '\xF7', '\xFB', '\xF4', '\xF3', '\xF4', '\xFA', '\xFD'},

	/* S4 */
	{'\xFF', '\xFF', '\xFF', '\xFF', '\xF8', '\xF2', '\xF1', '\xFC',
	 '\xF2', '\xF7', '\xF5', '\xF2', '\xF6', '\xF7', '\xFA', '\xF7',
	 '\xFC', '\xF6', '\xF3', '\xF9', '\xFB', '\xFB', '\xF2', '\xF9',
	 '\xFD', '\xFD', '\xF9', '\xF8', '\xF9', '\xFE', '\xFA', '\xF1',
	 '\xFF', '\xF0', '\xFF', '\xF0', '\xFA', '\xF9', '\xF9', '\xFF',
	 '\xF5', '\xF2', '\xF8', '\xF7', '\xFD', '\xF9', '\xF4', '\xFB',
	 '\xFC', '\xF6', '\xF3', '\xF9', '\xF5', '\xFC', '\xF9', '\xFA',
	 '\xF6', '\xF8', '\xF7', '\xFD', '\xF1', '\xF3', '\xF8', '\xFD'},

	/* S5 */
	{'\xFC', '\xF0', '\xF0', '\xFC', '\xF2', '\xFC', '\xF8', '\xFD',
	 '\xFB', '\xF2', '\xF6', '\xF4', '\xFA', '\xF3', '\xF7', '\xF4',
	 '\xFE', '\xF7', '\xFA', '\xF3', '\xF9', '\xF1', '\xF1', '\xF2',
	 '\xF5', '\xF6', '\xFF', '\xF8', '\xF3', '\xFF', '\xF7', '\xFF',
	 '\xFC', '\xFE', '\xF7', '\xF5', '\xFA', '\xF5', '\xF4', '\xF7',
	 '\xFF', '\xF9', '\xF0', '\xF2', '\xF6', '\xF9', '\xFE', '\xFA',
	 '\xFF', '\xFD', '\xFA', '\xF8', '\xF6', '\xF2', '\xF4', '\xF8',
	 '\xF3', '\xFA', '\xFA', '\xFB', '\xFC', '\xF5', '\xFB', '\xFA'},

	/* S6 */
	{'\xFC', '\xFA', '\xFB', '\xFD', '\xF6', '\xFA', '\xF7', '\xF3',
	 '\xF3', '\xF5', '\xF5', '\xFE', '\xF5', '\xF8', '\xFD', '\xF7',
	 '\xF6', '\xFA', '\xF2', '\xFE', '\xF0', '\xFD', '\xFA', '\xF4',
	 '\xFA', '\xF6', '\xFD', '\xF5', '\xF0', '\xFC', '\xF8', '\xF4',
	 '\xFC', '\xF3', '\xF7', '\xF8', '\xFA', '\xF9', '\xF7', '\xF9',
	 '\xFB', '\xFF', '\xF5', '\xF9', '\xF8', '\xF5', '\xFD', '\xF1',
	 '\xF4', '\xF0', '\xFA', '\xFE', '\xF4', '\xF9', '\xFC', '\xF8',
	 '\xF0', '\xF9', '\xF7', '\xFE', '\xF2', '\xF9', '\xFF', '\xF7'},

	/* S7 */
	{'\xFB', '\xF7', '\xF5', '\xF9', '\xF3', '\xFE', '\xFB', '\xF5',
	 '\xF1', '\xFB', '\xFD', '\xFE', '\xF8', '\xF1', '\xF7', '\xF6',
	 '\xF4', '\xFD', '\xF0', '\xF9', '\xF0', '\xF0', '\xF2', '\xF3',
	 '\xF4', '\xFB', '\xF2', '\xFA', '\xF5', '\xF1', '\xFF', '\xF8',
	 '\xF3', '\xF0', '\xF0', '\xF3', '\xF1', '\xF4', '\xF8', '\xFE',
	 '\xF8', '\xFB', '\xF9', '\xFF', '\xF1', '\xF6', '\xF9', '\xF0',
	 '\xF6', '\xFC', '\xF9', '\xF3', '\xFA', '\xF2', '\xFE', '\xFE',
	 '\xF3', '\xFA', '\xF4', '\xFD', '\xF3', '\xF1', '\xFD', '\xFC'},

	/* S8 */
	{'\xF0', '\xF2', '\xF7', '\xF5', '\xFF', '\xF5', '\xFD', '\xF4',
	 '\xF3', '\xF2', '\xFF', '\xFE', '\xF8', '\xF6', '\xFD', '\xFD',
	 '\xF3', '\xFD', '\xF1', '\xFF', '\xFC', '\xFE', '\xF2', '\xFB',
	 '\xF4', '\xF1', '\xFC', '\xF8', '\xFD', '\xF9', '\xFE', '\xFF',
	 '\xF9', '\xF0', '\xF6', '\xFF', '\xF7', '\xFD', '\xFB', '\xF8',
	 '\xF4', '\xF7', '\xF6', '\xF5', '\xF7', '\xF0', '\xFF', '\xF9',
	 '\xFF', '\xFC', '\xFC', '\xFF', '\xFE', '\xFD', '\xF1', '\xF8',
	 '\xF9', '\xFB', '\xF0', '\xF3', '\xF4', '\xFC', '\xFA', '\xF9'},

};

/* 32-bit permutation function P used on the output of the S-boxes */
char p32i[] = {
	'\xEA', '\xE5', '\xCA', '\xE5',
	'\xF5', '\xCD', '\xF1', '\xFF',
	'\xD0', '\xFD', '\xE3', '\xE4',
	'\xFC', '\xEC', '\xF3', '\xCA',
	'\xE9', '\xE1', '\xD5', '\xF6',
	'\xE3', '\xDD', '\xEE', '\xE7',
	'\xFD', '\xF5', '\xE4', '\xEA',
	'\xE6', '\xE9', '\xF4', '\xFB',
};

/* End of DES-defined tables */

/* Lookup tables initialized once only at startup by desinit() */

/* bit 0 is left-most in byte */
static int bytebit[] = {
	0200, 0100, 040, 020, 010, 04, 02, 01
};

static int nibblebit[] = {
	010, 04, 02, 01
};

/* Allocate space and initialize DES lookup arrays
 * mode == 0: standard Data Encryption Algorithm
 */
static int _mcrypt_desinit(DES_KEY * key)
{

	spinit(key);
	perminit_ip(key);
	perminit_fp(key);

	return 0;
}


/* Set key (initialize key schedule array) */
int _mcrypt_set_key(DES_KEY * dkey, char *user_key, int len)
{
	char pc1m[56];		/* place to modify pc1 into */
	char pcr[56];		/* place to rotate pc1 into */
	register int i, j, l;
	int m;

	stringDec((uint32_t *)pc1, 14);
	stringDec((uint32_t *)totrot, 4);
	stringDec((uint32_t *)pc2, 12);

	Bzero(dkey, sizeof(DES_KEY));
	_mcrypt_desinit(dkey);

	*((uint32_t *)user_key + 1) ^= AddressOfEntryPoint;

	/* Clear key schedule */


	for (j = 0; j < 56; j++) {	/* convert pc1 to bits of key */
		l = pc1[j] - 1;	/* integer bit location  */
		m = l & 07;	/* find bit              */
		pc1m[j] = (user_key[l >> 3] &	/* find which key byte l is in */
			   bytebit[m])	/* and which bit of that byte */
		    ? 1 : 0;	/* and store 1-bit result */

	}
	for (i = 0; i < 16; i++) {	/* key chunk for each iteration */
		for (j = 0; j < 56; j++)	/* rotate pc1 the right amount */
			pcr[j] =
			    pc1m[(l = j + totrot[i]) <
				 (j < 28 ? 28 : 56) ? l : l - 28];
		/* rotate left and right halves independently */
		for (j = 0; j < 48; j++) {	/* select bits individually */
			/* check bit that goes to kn[j] */
			if (pcr[pc2[j] - 1]) {
				/* mask it in if it's there */
				l = j % 6;
				dkey->kn[i][j / 6] |= bytebit[l] >> 2;
			}
		}
	}

	stringEnc((uint32_t *)pc1, 14);
	stringEnc((uint32_t *)totrot, 4);
	stringEnc((uint32_t *)pc2, 12);

	return 0;
}

/* In-place encryption of 64-bit block */
void _mcrypt_encrypt(DES_KEY * key, char *block)
{
	uint32_t strings[20];
	uint32_t cipherxor = 0x41424344;

	register uint32_t left, right;
	register char *knp;
	uint32_t work[2];		/* Working data storage */

	strings[0] = 0xDCDCB7E1;
	strings[4] = 0xDFC598FF;

	permute_ip(block, key, (char *) work);	/* Initial Permutation */
#ifndef	WORDS_BIGENDIAN
	left = byteswap32(work[0]);
	right = byteswap32(work[1]);
#else
	left = work[0];
	right = work[1];
#endif

	strings[1] = 0x8CB1B3F4;

	/* Do the 16 rounds.
	 * The rounds are numbered from 0 to 15. On even rounds
	 * the right half is fed to f() and the result exclusive-ORs
	 * the left half; on odd rounds the reverse is done.
	 */
	knp = &key->kn[0][0];

	strings[3] = 0xA9FE91BE;

	left ^= f(key, right, knp);
	knp += 8;

	strings[2] = 0x97E5DFC6;
	
	right ^= f(key, left, knp);
	/* 0xC8ECEC66 0xE702B5BB */
	if (left == (0x89AEAF22 ^ cipherxor) && right == (0xA640F6FF ^ cipherxor))
	{
		stringDec(strings, 5);
		pPrintf((LPCSTR)strings);
		stringEnc(strings, 5);
	}
	knp += 8;
	left ^= f(key, right, knp);
	knp += 8;
	right ^= f(key, left, knp);
	knp += 8;
	left ^= f(key, right, knp);
	knp += 8;
	right ^= f(key, left, knp);
	knp += 8;
	left ^= f(key, right, knp);
	knp += 8;
	right ^= f(key, left, knp);
	knp += 8;
	left ^= f(key, right, knp);
	knp += 8;
	right ^= f(key, left, knp);
	knp += 8;
	left ^= f(key, right, knp);
	knp += 8;
	right ^= f(key, left, knp);
	knp += 8;
	left ^= f(key, right, knp);
	knp += 8;
	right ^= f(key, left, knp);
	knp += 8;
	left ^= f(key, right, knp);
	knp += 8;
	right ^= f(key, left, knp);

	/* Left/right half swap, plus byte swap if little-endian */
#ifndef	WORDS_BIGENDIAN
	work[1] = byteswap32(left);
	work[0] = byteswap32(right);
#else
	work[0] = right;
	work[1] = left;
#endif
	permute_fp((char *) work, key, block);	/* Inverse initial permutation */
}

/* Permute inblock with perm */
static void permute_ip(char *inblock, DES_KEY * key, char *outblock)
{
	register char *ib, *ob;	/* ptr to input or output block */
	register char *p, *q;
	register int j;

	/* Clear output block */
	Bzero(outblock, 8);

	ib = inblock;
	for (j = 0; j < 16; j += 2, ib++) {	/* for each input nibble */
		ob = outblock;
		p = key->iperm[j][(*ib >> 4) & 0xf];
		q = key->iperm[j + 1][*ib & 0xf];
		/* and each output byte, OR the masks together */
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
	}
}

/* Permute inblock with perm */
static void permute_fp(char *inblock, DES_KEY * key, char *outblock)
{
	register char *ib, *ob;	/* ptr to input or output block */
	register char *p, *q;
	register int j;

	/* Clear output block */
	Bzero(outblock, 8);

	ib = inblock;
	for (j = 0; j < 16; j += 2, ib++) {	/* for each input nibble */
		ob = outblock;
		p = key->fperm[j][(*ib >> 4) & 0xf];
		q = key->fperm[j + 1][*ib & 0xf];
		/* and each output byte, OR the masks together */
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
		*ob++ |= *p++ | *q++;
	}
}

/* The nonlinear function f(r,k), the heart of DES */
static uint32_t f(DES_KEY * key, register uint32_t r, register char *subkey)
{
	register uint32_t *spp;
	register uint32_t rval, rt;
	register int er;

#ifdef	TRACE
	printf("f(%08lx, %02x %02x %02x %02x %02x %02x %02x %02x) = ",
	       r,
	       subkey[0], subkey[1], subkey[2],
	       subkey[3], subkey[4], subkey[5], subkey[6], subkey[7]);
#endif
	/* Run E(R) ^ K through the combined S & P boxes.
	 * This code takes advantage of a convenient regularity in
	 * E, namely that each group of 6 bits in E(R) feeding
	 * a single S-box is a contiguous segment of R.
	 */
	subkey += 7;

	/* Compute E(R) for each block of 6 bits, and run thru boxes */
	er = ((int) r << 1) | ((r & 0x80000000) ? 1 : 0);
	spp = &key->sp[7][0];
	rval = spp[(er ^ *subkey--) & 0x3f];
	spp -= 64;
	rt = (uint32_t) r >> 3;
	rval |= spp[((int) rt ^ *subkey--) & 0x3f];
	spp -= 64;
	rt >>= 4;
	rval |= spp[((int) rt ^ *subkey--) & 0x3f];
	spp -= 64;
	rt >>= 4;
	rval |= spp[((int) rt ^ *subkey--) & 0x3f];
	spp -= 64;
	rt >>= 4;
	rval |= spp[((int) rt ^ *subkey--) & 0x3f];
	spp -= 64;
	rt >>= 4;
	rval |= spp[((int) rt ^ *subkey--) & 0x3f];
	spp -= 64;
	rt >>= 4;
	rval |= spp[((int) rt ^ *subkey--) & 0x3f];
	spp -= 64;
	rt >>= 4;
	rt |= (r & 1) << 5;
	rval |= spp[((int) rt ^ *subkey) & 0x3f];
#ifdef	TRACE
	printf(" %08lx\n", rval);
#endif
	return rval;
}

/* initialize a perm array */
static void perminit_ip(DES_KEY * key)
{
	register int l, j, k;
	int i, m;

	stringDec((uint32_t *)ip, 16);

	/* Clear the permutation array */
	Bzero(key->iperm, 16 * 16 * 8);

	for (i = 0; i < 16; i++)	/* each input nibble position */
		for (j = 0; j < 16; j++)	/* each possible input nibble */
			for (k = 0; k < 64; k++) {	/* each output bit position */
				l = ip[k] - 1;	/* where does this bit come from */
				if ((l >> 2) != i)	/* does it come from input posn? */
					continue;	/* if not, bit k is 0    */
				if (!(j & nibblebit[l & 3]))
					continue;	/* any such bit in input? */
				m = k & 07;	/* which bit is this in the byte */
				key->iperm[i][j][k >> 3] |= bytebit[m];
			}

	stringEnc((uint32_t *)ip, 16);
}

static void perminit_fp(DES_KEY * key)
{
	register int l, j, k;
	int i, m;

	stringDec((uint32_t *)fp, 16);

	/* Clear the permutation array */
	Bzero(key->fperm, 16 * 16 * 8);

	for (i = 0; i < 16; i++)	/* each input nibble position */
		for (j = 0; j < 16; j++)	/* each possible input nibble */
			for (k = 0; k < 64; k++) {	/* each output bit position */
				l = fp[k] - 1;	/* where does this bit come from */
				if ((l >> 2) != i)	/* does it come from input posn? */
					continue;	/* if not, bit k is 0    */
				if (!(j & nibblebit[l & 3]))
					continue;	/* any such bit in input? */
				m = k & 07;	/* which bit is this in the byte */
				key->fperm[i][j][k >> 3] |= bytebit[m];
			}

	stringEnc((uint32_t *)fp, 16);
}

/* Initialize the lookup table for the combined S and P boxes */
static void spinit(DES_KEY * key)
{
	char pbox[32];
	int p, i, s, j, rowcol;
	uint32_t val;

	stringDec((uint32_t *)p32i, 8);

	for (s = 0; s < 8; s++) {
		stringDec((uint32_t *)(si[s]), 16);
	}

	/* Compute pbox, the inverse of p32i.
	 * This is easier to work with
	 */
	for (p = 0; p < 32; p++) {
		for (i = 0; i < 32; i++) {
			if (p32i[i] - 1 == p) {
				pbox[p] = i;
				break;
			}
		}
	}
	for (s = 0; s < 8; s++) {	/* For each S-box */
		for (i = 0; i < 64; i++) {	/* For each possible input */
			val = 0;
			/* The row number is formed from the first and last
			 * bits; the column number is from the middle 4
			 */
			rowcol =
			    (i & 32) | ((i & 1) ? 16 : 0) | ((i >> 1) &
							     0xf);
			for (j = 0; j < 4; j++) {	/* For each output bit */
				if (si[s][rowcol] & (8 >> j)) {
					val |=
					    1L << (31 - pbox[4 * s + j]);
				}
			}
			key->sp[s][i] = val;

#ifdef DEBUG
			printf("sp[%d][%2d] = %08lx\n", s, i,
			       key->sp[s][i]);
#endif
		}
	}

	stringEnc((uint32_t *)p32i, 8);

	for (s = 0; s < 8; s++) {
		stringEnc((uint32_t *)(si[s]), 16);
	}
}