summaryrefslogtreecommitdiffstats
path: root/usr.sbin/xntpd/authstuff/makePC1.c
blob: 8a49ec902e9438cc7e8cac12091efd04c13d51e4 (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
/*
 * makePC1 - build custom permutted choice 1 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	permute	P((u_char *, u_long *, 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 1 table, to produce the initial C.  This table
 * has had 1 subtracted from it to give it a zero base.
 */
static u_char PC1_C[28] = {
	56, 48, 40, 32, 24, 16,  8,
	 0, 57, 49, 41, 33, 25, 17,
	 9,  1, 58, 50, 42, 34, 26,
	18, 10,  2, 59, 51, 43, 35
};

/*
 * Permuted choice 1 table, to produce the initial D.  Again, 1 has
 * been subtracted to match C language zero base arrays.
 */
static u_char PC1_D[28] = {
	62, 54, 46, 38, 30, 22, 14,
	 6, 61, 53, 45, 37, 29, 21,
	13,  5, 60, 52, 44, 36, 28,
	20, 12,  4, 27, 19, 11,  3
};

/*
 * permute - produce c and d for the given bits
 */
static void
permute(bits, cp, dp)
	u_char *bits;
	u_long *cp;
	u_long *dp;
{
	register int i;
	register u_long mask;
	u_char c[28];
	u_char d[28];

	memset((char *)c, 0, sizeof c);
	memset((char *)d, 0, sizeof d);

	for (i = 0; i < 28; i++) {
		c[i] = bits[PC1_C[i]];
		d[i] = bits[PC1_D[i]];
	}

	mask = 0x10000000;
	*cp = *dp = 0;
	for (i = 0; i < 28; i++) {
		mask >>= 1;
		if (c[i])
			*cp |= mask;
		if (d[i])
			*dp |= mask;
	}
}


/*
 * bits from the left part of the key used to form the C subkey
 */
static	int	lc3[4] = { 0, 8, 16, 24 };

/*
 * bits from the left part of the key used to form the D subkey
 */
static	int	ld4[4] = { 3, 11, 19, 27 };

/*
 * bits from the right part of the key used to form the C subkey
 */
static	int	rc4[4] = { 32, 40, 48, 56 };

/*
 * bits from the right part of the key used to form the D subkey
 */
static	int	rd3[4] = { 36, 44, 52, 60 };

static	u_long	PC_CL[8];
static	u_long	PC_DL[16];
static	u_long	PC_CR[16];
static	u_long	PC_DR[8];


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

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

	printf("static u_long PC1_CL[8] = {");
	for (i = 0; i < 4; i++) {
		for (comb = 0; comb < 8; comb++) {
			if (comb & 0x4)
				bits[lc3[i]] = 1;
			if (comb & 0x2)
				bits[lc3[i]+1] = 1;
			if (comb & 0x1)
				bits[lc3[i]+2] = 1;
			permute(bits, &c, &d);
			bits[lc3[i]] = 0;
			bits[lc3[i]+1] = 0;
			bits[lc3[i]+2] = 0;
			if (d != 0) {
				(void) fprintf(stderr,
				    "Error PC_CL i %d comb %d\n", i, comb);
			}
			if (i == 0) {
				PC_CL[comb] = c;
				if ((comb & 0x3) == 0)
					printf("\n\t0x%08x,", c);
				else if (comb == 7)
					printf(" 0x%08x\n};\n\n", c);
				else
					printf(" 0x%08x,", c);
			} else {
				if (c != PC_CL[comb] << i)
					(void) fprintf(stderr,
					    "Error PC_CL 0x%08x c 0x%08x\n",
					    PC_CL[comb], c);
			}
		}
	}

	printf("static u_long PC1_DL[16] = {");
	for (i = 0; i < 4; i++) {
		for (comb = 0; comb < 16; comb++) {
			if (comb & 0x8)
				bits[ld4[i]] = 1;
			if (comb & 0x4)
				bits[ld4[i]+1] = 1;
			if (comb & 0x2)
				bits[ld4[i]+2] = 1;
			if (comb & 0x1)
				bits[ld4[i]+3] = 1;
			permute(bits, &c, &d);
			bits[ld4[i]] = 0;
			bits[ld4[i]+1] = 0;
			bits[ld4[i]+2] = 0;
			bits[ld4[i]+3] = 0;
			if (c != 0) {
				(void) fprintf(stderr,
				    "Error PC_DL i %d comb %d\n", i, comb);
			}
			if (i == 0) {
				PC_DL[comb] = d;
				if ((comb & 0x3) == 0)
					printf("\n\t0x%08x,", d);
				else if (comb == 15)
					printf(" 0x%08x\n};\n\n", d);
				else
					printf(" 0x%08x,", d);
			} else {
				if (d != PC_DL[comb] << i)
					(void) fprintf(stderr,
					    "Error PC_DL 0x%08x c 0x%08x\n",
					    PC_DL[comb], d);
			}
		}
	}

	printf("static u_long PC1_CR[16] = {");
	for (i = 0; i < 4; i++) {
		for (comb = 0; comb < 16; comb++) {
			if (comb & 0x8)
				bits[rc4[i]] = 1;
			if (comb & 0x4)
				bits[rc4[i]+1] = 1;
			if (comb & 0x2)
				bits[rc4[i]+2] = 1;
			if (comb & 0x1)
				bits[rc4[i]+3] = 1;
			permute(bits, &c, &d);
			bits[rc4[i]] = 0;
			bits[rc4[i]+1] = 0;
			bits[rc4[i]+2] = 0;
			bits[rc4[i]+3] = 0;
			if (d != 0) {
				(void) fprintf(stderr,
				    "Error PC_CR i %d comb %d\n", i, comb);
			}
			if (i == 0) {
				PC_CR[comb] = c;
				if ((comb & 0x3) == 0)
					printf("\n\t0x%08x,", c);
				else if (comb == 15)
					printf(" 0x%08x\n};\n\n", c);
				else
					printf(" 0x%08x,", c);
			} else {
				if (c != PC_CR[comb] << i)
					(void) fprintf(stderr,
					    "Error PC_CR 0x%08x c 0x%08x\n",
					    PC_CR[comb], c);
			}
		}
	}

	printf("static u_long PC1_DR[8] = {");
	for (i = 0; i < 4; i++) {
		for (comb = 0; comb < 8; comb++) {
			if (comb & 0x4)
				bits[rd3[i]] = 1;
			if (comb & 0x2)
				bits[rd3[i]+1] = 1;
			if (comb & 0x1)
				bits[rd3[i]+2] = 1;
			permute(bits, &c, &d);
			bits[rd3[i]] = 0;
			bits[rd3[i]+1] = 0;
			bits[rd3[i]+2] = 0;
			if (c != 0) {
				(void) fprintf(stderr,
				    "Error PC_DR i %d comb %d\n", i, comb);
			}
			if (i == 0) {
				PC_DR[comb] = d;
				if ((comb & 0x3) == 0)
					printf("\n\t0x%08x,", d);
				else if (comb == 7)
					printf(" 0x%08x\n};\n\n", d);
				else
					printf(" 0x%08x,", d);
			} else {
				if (d != PC_DR[comb] << i)
					(void) fprintf(stderr,
					    "Error PC_DR 0x%08x c 0x%08x\n",
					    PC_DR[comb], d);
			}
		}
	}
}
OpenPOWER on IntegriCloud