desdata.c 4.36 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
/* desdata.c
 *
Niels Möller's avatar
Niels Möller committed
3
 * Generate tables used by des.c and desCode.h.
Niels Möller's avatar
Niels Möller committed
4
 *
5
 */
Niels Möller's avatar
Niels Möller committed
6

Niels Möller's avatar
Niels Möller committed
7
8
9
/*
 *	des - fast & portable DES encryption & decryption.
 *	Copyright (C) 1992  Dana L. How
Niels Möller's avatar
Niels Möller committed
10
 *	Please see the file `descore.README' for the complete copyright notice.
Niels Möller's avatar
Niels Möller committed
11
12
13
 *
 */

Niels Möller's avatar
Niels Möller committed
14
15
#include <stdio.h>

Niels Möller's avatar
Niels Möller committed
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
#include	"desinfo.h"


/* list of weak and semi-weak keys

	 +0   +1   +2   +3   +4   +5   +6   +7
	0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01
	0x01 0x1f 0x01 0x1f 0x01 0x0e 0x01 0x0e
	0x01 0xe0 0x01 0xe0 0x01 0xf1 0x01 0xf1
	0x01 0xfe 0x01 0xfe 0x01 0xfe 0x01 0xfe
	0x1f 0x01 0x1f 0x01 0x0e 0x01 0x0e 0x01
	0x1f 0x1f 0x1f 0x1f 0x0e 0x0e 0x0e 0x0e
	0x1f 0xe0 0x1f 0xe0 0x0e 0xf1 0x0e 0xf1
	0x1f 0xfe 0x1f 0xfe 0x0e 0xfe 0x0e 0xfe
	0xe0 0x01 0xe0 0x01 0xf1 0x01 0xf1 0x01
	0xe0 0x1f 0xe0 0x1f 0xf1 0x0e 0xf1 0x0e
	0xe0 0xe0 0xe0 0xe0 0xf1 0xf1 0xf1 0xf1
	0xe0 0xfe 0xe0 0xfe 0xf1 0xfe 0xf1 0xfe
	0xfe 0x01 0xfe 0x01 0xfe 0x01 0xfe 0x01
	0xfe 0x1f 0xfe 0x1f 0xfe 0x0e 0xfe 0x0e
	0xfe 0xe0 0xfe 0xe0 0xfe 0xf1 0xfe 0xf1
	0xfe 0xfe 0xfe 0xfe 0xfe 0xfe 0xfe 0xfe
 */

/* key bit order in each method pair: bits 31->00 of 1st, bits 31->00 of 2nd */
/* this does not reflect the rotate of the 2nd word */

#define	S(box,bit)	(box*6+bit)
int korder[] = {
	S(7, 5), S(7, 4), S(7, 3), S(7, 2), S(7, 1), S(7, 0),
	S(5, 5), S(5, 4), S(5, 3), S(5, 2), S(5, 1), S(5, 0),
	S(3, 5), S(3, 4), S(3, 3), S(3, 2), S(3, 1), S(3, 0),
	S(1, 5), S(1, 4), S(1, 3), S(1, 2), S(1, 1), S(1, 0),
	S(6, 5), S(6, 4), S(6, 3), S(6, 2), S(6, 1), S(6, 0),
	S(4, 5), S(4, 4), S(4, 3), S(4, 2), S(4, 1), S(4, 0),
	S(2, 5), S(2, 4), S(2, 3), S(2, 2), S(2, 1), S(2, 0),
	S(0, 5), S(0, 4), S(0, 3), S(0, 2), S(0, 1), S(0, 0),
};

/* the order in which the algorithm accesses the s boxes */

int sorder[] = {
	7, 5, 3, 1, 6, 4, 2, 0,
};

int
62
main(int argc, char **argv)
Niels Möller's avatar
Niels Möller committed
63
{
64
	unsigned long d, i, j, k, l, m, n, s; /* Always at least 32 bits */
Niels Möller's avatar
Niels Möller committed
65
66
	char b[256], ksr[56];

67
68
69
	if (argc <= 1)
		return 1;

Niels Möller's avatar
Niels Möller committed
70
71
	switch ( argv[1][0] ) {

72
73
default: 
	return 1;
Niels Möller's avatar
Niels Möller committed
74
75
76
77
78
	/*
	 * <<< make the key parity table >>>
	 */

case 'p':
Niels Möller's avatar
Niels Möller committed
79
	printf(
Niels Möller's avatar
Niels Möller committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"/* automagically produced - do not fuss with this information */\n\n");

	/* store parity information */
	for ( i = 0; i < 256; i++ ) {
		j  = i;
		j ^= j >> 4;	/* bits 3-0 have pairs */
		j ^= j << 2;	/* bits 3-2 have quads */
		j ^= j << 1;	/* bit  3 has the entire eight (no cox) */
		b[i] = 8 & ~j;	/* 0 is okay and 8 is bad parity */
	}

	/* only these characters can appear in a weak key */
	b[0x01] = 1;
	b[0x0e] = 2;
	b[0x1f] = 3;
	b[0xe0] = 4;
	b[0xf1] = 5;
	b[0xfe] = 6;

	/* print it out */
	for ( i = 0; i < 256; i++ ) {
Niels Möller's avatar
Niels Möller committed
101
		printf("%d,", b[i]);
Niels Möller's avatar
Niels Möller committed
102
		if ( (i & 31) == 31 )
Niels Möller's avatar
Niels Möller committed
103
			printf("\n");
Niels Möller's avatar
Niels Möller committed
104
105
106
107
108
109
110
111
112
113
	}

	break;


	/*
	 * <<< make the key usage table >>>
	 */

case 'r':
Niels Möller's avatar
Niels Möller committed
114
	printf("/* automagically made - do not fuss with this */\n\n");
Niels Möller's avatar
Niels Möller committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

	/* KL specifies the initial key bit positions */
	for (i = 0; i < 56; i++)
		ksr[i] = (KL[i] - 1) ^ 7;

	for (i = 0; i < 16; i++) {

		/* apply the appropriate number of left shifts */
		for (j = 0; j < KS[i]; j++) {
			m = ksr[ 0];
			n = ksr[28];
			for (k = 0; k < 27; k++)
				ksr[k     ] = ksr[k +  1],
				ksr[k + 28] = ksr[k + 29];
			ksr[27] = m;
			ksr[55] = n;
		}

		/* output the key bit numbers */
		for (j = 0; j < 48; j++) {
			m = ksr[KC[korder[j]] - 1];
			m = (m / 8) * 7 + (m % 8) - 1;
			m = 55 - m;
Niels Möller's avatar
Niels Möller committed
138
			printf(" %2ld,", (long) m);
Niels Möller's avatar
Niels Möller committed
139
			if ((j % 12) == 11)
Niels Möller's avatar
Niels Möller committed
140
				printf("\n");
Niels Möller's avatar
Niels Möller committed
141
		}
Niels Möller's avatar
Niels Möller committed
142
		printf("\n");
Niels Möller's avatar
Niels Möller committed
143
144
145
146
147
148
149
150
151
152
	}

	break;


	/*
	 * <<< make the keymap table >>>
	 */

case 'k':
Niels Möller's avatar
Niels Möller committed
153
	printf("/* automagically made - do not fuss with this */\n\n");
Niels Möller's avatar
Niels Möller committed
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

	for ( i = 0; i <= 7 ; i++ ) {
		s = sorder[i];
		for ( d = 0; d <= 63; d++ ) {
			/* flip bits */
			k =	((d << 5) & 32) |
				((d << 3) & 16) |
				((d << 1) &  8) |
				((d >> 1) &  4) |
				((d >> 3) &  2) |
				((d >> 5) &  1) ;
			/* more bit twiddling */
			l =	((k << 0) & 32) |	/* overlap bit */
				((k << 4) & 16) |	/* overlap bit */
				((k >> 1) & 15) ;	/* unique bits */
			/* look up s box value */
			m = SB[s][l];
			/* flip bits */
			n =	((m << 3) &  8) |
				((m << 1) &  4) |
				((m >> 1) &  2) |
				((m >> 3) &  1) ;
			/* put in correct nybble */
			n <<= (s << 2);
			/* perform p permutation */
			for ( m = j = 0; j < 32; j++ )
				if ( n & (1 << (SP[j] - 1)) )
181
					m |= (1UL << j);
Niels Möller's avatar
Niels Möller committed
182
			/* rotate right (alg keeps everything rotated by 1) */
183
			m = (m >> 1) | ((m & 1) << 31);
Niels Möller's avatar
Niels Möller committed
184
			/* print it out */
185
			printf(" 0x%08lx,", m);
Niels Möller's avatar
Niels Möller committed
186
			if ( ( d & 3 ) == 3 )
Niels Möller's avatar
Niels Möller committed
187
				printf("\n");
Niels Möller's avatar
Niels Möller committed
188
		}
Niels Möller's avatar
Niels Möller committed
189
		printf("\n");
Niels Möller's avatar
Niels Möller committed
190
191
192
193
194
195
196
197
	}

	break;

	}

	return 0;
}