summaryrefslogtreecommitdiffstats
path: root/usr.sbin/xntpd/authstuff/makePC2.c
blob: e5121fd08364bfd22b7be65dc19b625c0f8a23d6 (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
/*
 * makePC2 - build custom permutted choice 2 tables
 */

#include <stdio.h>
#include <sys/types.h>

#include "ntp_stdlib.h"

#define	STREQ(a, b)	(*(a) == *(b) && strcmp((a), (b)) == 0)

char *progname;
int debug;

static	void	permc	P((u_char *, u_long *));
static	void	permd	P((u_char *, u_long *));
static	void	doit	P((void));

/*
 * main - parse arguments and handle options
 */
void
main(argc, argv)
int argc;
char *argv[];
{
	int c;
	int errflg = 0;
	extern int ntp_optind;
	extern char *ntp_optarg;

	progname = argv[0];
	while ((c = ntp_getopt(argc, argv, "d")) != EOF)
		switch (c) {
		case 'd':
			++debug;
			break;
		default:
			errflg++;
			break;
		}
	if (errflg) {
		(void) fprintf(stderr, "usage: %s [-d]\n", progname);
		exit(2);
	}
	doit();
	exit(0);
}

/*
 * Permuted choice 2 table.  This actually produces the low order 24
 * bits of the subkey Ki from the 28 bit value of Ci.  This has had
 * 1 subtracted from it to give a zero base.
 */
static u_char PC2_C[24] = {
	13, 16, 10, 23,  0,  4,
	 2, 27, 14,  5, 20,  9,
	22, 18, 11,  3, 25,  7,
	15,  6, 26, 19, 12,  1
};

/*
 * Permuted choice 2 table, operating on the 28 Di bits to produce the
 * high order 24 bits of subkey Ki.  This has had 29 subtracted from
 * it to give it a zero base into our D bit array.
 */
static u_char PC2_D[24] = {
	12, 23,  2,  8, 18, 26,
	 1, 11, 22, 16,  4, 19,
	15, 20, 10, 27,  5, 24,
	17, 13, 21,  7,  0,  3
};

u_long masks[4] = { 0x40000000, 0x400000, 0x4000, 0x40 };


/*
 * permc - permute C, producing a four byte result
 */
static void
permc(bits, resp)
	u_char *bits;
	u_long *resp;
{
	register int part;
	register int i;
	register u_long mask;
	u_char res[24];

	memset((char *)res, 0, sizeof res);

	for (i = 0; i < 24; i++) {
		res[i] = bits[PC2_C[i]];
	}

	*resp = 0;
	for (part = 0; part < 4; part++) {
		mask = masks[part];
		for (i = part*6; i < (part+1)*6; i++) {
			mask >>= 1;
			if (res[i])
				*resp |= mask;
		}
	}
}

/*
 * permd - permute D, producing a four byte result
 */
static void
permd(bits, resp)
	u_char *bits;
	u_long *resp;
{
	register int part;
	register int i;
	register u_long mask;
	u_char res[24];

	memset((char *)res, 0, sizeof res);

	for (i = 0; i < 24; i++) {
		res[i] = bits[PC2_D[i]];
	}

	*resp = 0;
	for (part = 0; part < 4; part++) {
		mask = masks[part];
		for (i = part*6; i < (part+1)*6; i++) {
			mask >>= 1;
			if (res[i])
				*resp |= mask;
		}
	}
}


/*
 * bits used for each round in C
 */
static	int	cbits[4][6] = {
	0, 1, 2, 3, 4, 5,
	6, 7, 9, 10, 11, 12,
	13, 14, 15, 16, 22, 23,
	18, 19, 20, 25, 26, 27
};


/*
 * bits used for each round in D
 */
static	int	dbits[4][6] = {
	0, 1, 2, 3, 4, 5,
	7, 8, 10, 11, 12, 13,
	15, 16, 17, 18, 19, 20,
	21, 22, 23, 24, 26, 27
};


/*
 * doit - compute and print the four PC1 tables
 */
static void
doit()
{
	int i;
	int comb;
	u_long res;
	u_char bits[28];

	memset((char *)bits, 0, sizeof bits);

	printf("static u_long PC2_C[4][64] = {");
	for (i = 0; i < 4; i++) {
		for (comb = 0; comb < 64; comb++) {
			if (comb & 0x20)
				bits[cbits[i][0]] = 1;
			if (comb & 0x10)
				bits[cbits[i][1]] = 1;
			if (comb & 0x8)
				bits[cbits[i][2]] = 1;
			if (comb & 0x4)
				bits[cbits[i][3]] = 1;
			if (comb & 0x2)
				bits[cbits[i][4]] = 1;
			if (comb & 0x1)
				bits[cbits[i][5]] = 1;
			permc(bits, &res);
			bits[cbits[i][0]] = 0;
			bits[cbits[i][1]] = 0;
			bits[cbits[i][2]] = 0;
			bits[cbits[i][3]] = 0;
			bits[cbits[i][4]] = 0;
			bits[cbits[i][5]] = 0;
			if ((comb & 0x3) == 0)
				printf("\n\t0x%08x,", res);
			else if (comb == 63 && i == 3)
				printf(" 0x%08x\n};\n\n", res);
			else if (comb == 63)
				printf(" 0x%08x,\n", res);
			else
				printf(" 0x%08x,", res);
		}
	}

	printf("static u_long PC2_D[4][64] = {");
	for (i = 0; i < 4; i++) {
		for (comb = 0; comb < 64; comb++) {
			if (comb & 0x20)
				bits[dbits[i][0]] = 1;
			if (comb & 0x10)
				bits[dbits[i][1]] = 1;
			if (comb & 0x8)
				bits[dbits[i][2]] = 1;
			if (comb & 0x4)
				bits[dbits[i][3]] = 1;
			if (comb & 0x2)
				bits[dbits[i][4]] = 1;
			if (comb & 0x1)
				bits[dbits[i][5]] = 1;
			permd(bits, &res);
			bits[dbits[i][0]] = 0;
			bits[dbits[i][1]] = 0;
			bits[dbits[i][2]] = 0;
			bits[dbits[i][3]] = 0;
			bits[dbits[i][4]] = 0;
			bits[dbits[i][5]] = 0;
			if ((comb & 0x3) == 0)
				printf("\n\t0x%08x,", res);
			else if (comb == 63 && i == 3)
				printf(" 0x%08x\n};\n\n", res);
			else if (comb == 63)
				printf(" 0x%08x,\n", res);
			else
				printf(" 0x%08x,", res);
		}
	}
}
OpenPOWER on IntegriCloud