1 | /* |
---|
2 | * makePC1 - build custom permutted choice 1 tables |
---|
3 | */ |
---|
4 | |
---|
5 | #include <stdio.h> |
---|
6 | #include <sys/types.h> |
---|
7 | |
---|
8 | #include "ntp_stdlib.h" |
---|
9 | |
---|
10 | #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0) |
---|
11 | |
---|
12 | char *progname; |
---|
13 | int debug; |
---|
14 | |
---|
15 | static void permute P((u_char *, u_int32 *, u_int32 *)); |
---|
16 | static void doit P((void)); |
---|
17 | |
---|
18 | /* |
---|
19 | * main - parse arguments and handle options |
---|
20 | */ |
---|
21 | void |
---|
22 | main(argc, argv) |
---|
23 | int argc; |
---|
24 | char *argv[]; |
---|
25 | { |
---|
26 | int c; |
---|
27 | int errflg = 0; |
---|
28 | extern int ntp_optind; |
---|
29 | extern char *ntp_optarg; |
---|
30 | |
---|
31 | progname = argv[0]; |
---|
32 | while ((c = ntp_getopt(argc, argv, "d")) != EOF) |
---|
33 | switch (c) { |
---|
34 | case 'd': |
---|
35 | ++debug; |
---|
36 | break; |
---|
37 | default: |
---|
38 | errflg++; |
---|
39 | break; |
---|
40 | } |
---|
41 | if (errflg) { |
---|
42 | (void) fprintf(stderr, "usage: %s [-d]\n", progname); |
---|
43 | exit(2); |
---|
44 | } |
---|
45 | doit(); |
---|
46 | exit(0); |
---|
47 | } |
---|
48 | |
---|
49 | /* |
---|
50 | * Permuted choice 1 table, to produce the initial C. This table |
---|
51 | * has had 1 subtracted from it to give it a zero base. |
---|
52 | */ |
---|
53 | static u_char PC1_C[28] = { |
---|
54 | 56, 48, 40, 32, 24, 16, 8, |
---|
55 | 0, 57, 49, 41, 33, 25, 17, |
---|
56 | 9, 1, 58, 50, 42, 34, 26, |
---|
57 | 18, 10, 2, 59, 51, 43, 35 |
---|
58 | }; |
---|
59 | |
---|
60 | /* |
---|
61 | * Permuted choice 1 table, to produce the initial D. Again, 1 has |
---|
62 | * been subtracted to match C language zero base arrays. |
---|
63 | */ |
---|
64 | static u_char PC1_D[28] = { |
---|
65 | 62, 54, 46, 38, 30, 22, 14, |
---|
66 | 6, 61, 53, 45, 37, 29, 21, |
---|
67 | 13, 5, 60, 52, 44, 36, 28, |
---|
68 | 20, 12, 4, 27, 19, 11, 3 |
---|
69 | }; |
---|
70 | |
---|
71 | /* |
---|
72 | * permute - produce c and d for the given bits |
---|
73 | */ |
---|
74 | static void |
---|
75 | permute(bits, cp, dp) |
---|
76 | u_char *bits; |
---|
77 | u_int32 *cp; |
---|
78 | u_int32 *dp; |
---|
79 | { |
---|
80 | register int i; |
---|
81 | register u_int32 mask; |
---|
82 | u_char c[28]; |
---|
83 | u_char d[28]; |
---|
84 | |
---|
85 | memset((char *)c, 0, sizeof c); |
---|
86 | memset((char *)d, 0, sizeof d); |
---|
87 | |
---|
88 | for (i = 0; i < 28; i++) { |
---|
89 | c[i] = bits[PC1_C[i]]; |
---|
90 | d[i] = bits[PC1_D[i]]; |
---|
91 | } |
---|
92 | |
---|
93 | mask = 0x10000000; |
---|
94 | *cp = *dp = 0; |
---|
95 | for (i = 0; i < 28; i++) { |
---|
96 | mask >>= 1; |
---|
97 | if (c[i]) |
---|
98 | *cp |= mask; |
---|
99 | if (d[i]) |
---|
100 | *dp |= mask; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | /* |
---|
106 | * bits from the left part of the key used to form the C subkey |
---|
107 | */ |
---|
108 | static int lc3[4] = { 0, 8, 16, 24 }; |
---|
109 | |
---|
110 | /* |
---|
111 | * bits from the left part of the key used to form the D subkey |
---|
112 | */ |
---|
113 | static int ld4[4] = { 3, 11, 19, 27 }; |
---|
114 | |
---|
115 | /* |
---|
116 | * bits from the right part of the key used to form the C subkey |
---|
117 | */ |
---|
118 | static int rc4[4] = { 32, 40, 48, 56 }; |
---|
119 | |
---|
120 | /* |
---|
121 | * bits from the right part of the key used to form the D subkey |
---|
122 | */ |
---|
123 | static int rd3[4] = { 36, 44, 52, 60 }; |
---|
124 | |
---|
125 | static u_int32 PC_CL[8]; |
---|
126 | static u_int32 PC_DL[16]; |
---|
127 | static u_int32 PC_CR[16]; |
---|
128 | static u_int32 PC_DR[8]; |
---|
129 | |
---|
130 | |
---|
131 | /* |
---|
132 | * doit - compute and print the four PC1 tables |
---|
133 | */ |
---|
134 | static void |
---|
135 | doit() |
---|
136 | { |
---|
137 | int i; |
---|
138 | int comb; |
---|
139 | u_int32 c; |
---|
140 | u_int32 d; |
---|
141 | u_char bits[64]; |
---|
142 | |
---|
143 | memset((char *)bits, 0, sizeof bits); |
---|
144 | |
---|
145 | printf("static u_int32 PC1_CL[8] = {"); |
---|
146 | for (i = 0; i < 4; i++) { |
---|
147 | for (comb = 0; comb < 8; comb++) { |
---|
148 | if (comb & 0x4) |
---|
149 | bits[lc3[i]] = 1; |
---|
150 | if (comb & 0x2) |
---|
151 | bits[lc3[i]+1] = 1; |
---|
152 | if (comb & 0x1) |
---|
153 | bits[lc3[i]+2] = 1; |
---|
154 | permute(bits, &c, &d); |
---|
155 | bits[lc3[i]] = 0; |
---|
156 | bits[lc3[i]+1] = 0; |
---|
157 | bits[lc3[i]+2] = 0; |
---|
158 | if (d != 0) { |
---|
159 | (void) fprintf(stderr, |
---|
160 | "Error PC_CL i %d comb %d\n", i, comb); |
---|
161 | } |
---|
162 | if (i == 0) { |
---|
163 | PC_CL[comb] = c; |
---|
164 | if ((comb & 0x3) == 0) |
---|
165 | printf("\n\t0x%08x,", c); |
---|
166 | else if (comb == 7) |
---|
167 | printf(" 0x%08x\n};\n\n", c); |
---|
168 | else |
---|
169 | printf(" 0x%08x,", c); |
---|
170 | } else { |
---|
171 | if (c != PC_CL[comb] << i) |
---|
172 | (void) fprintf(stderr, |
---|
173 | "Error PC_CL 0x%08x c 0x%08x\n", |
---|
174 | PC_CL[comb], c); |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | printf("static u_int32 PC1_DL[16] = {"); |
---|
180 | for (i = 0; i < 4; i++) { |
---|
181 | for (comb = 0; comb < 16; comb++) { |
---|
182 | if (comb & 0x8) |
---|
183 | bits[ld4[i]] = 1; |
---|
184 | if (comb & 0x4) |
---|
185 | bits[ld4[i]+1] = 1; |
---|
186 | if (comb & 0x2) |
---|
187 | bits[ld4[i]+2] = 1; |
---|
188 | if (comb & 0x1) |
---|
189 | bits[ld4[i]+3] = 1; |
---|
190 | permute(bits, &c, &d); |
---|
191 | bits[ld4[i]] = 0; |
---|
192 | bits[ld4[i]+1] = 0; |
---|
193 | bits[ld4[i]+2] = 0; |
---|
194 | bits[ld4[i]+3] = 0; |
---|
195 | if (c != 0) { |
---|
196 | (void) fprintf(stderr, |
---|
197 | "Error PC_DL i %d comb %d\n", i, comb); |
---|
198 | } |
---|
199 | if (i == 0) { |
---|
200 | PC_DL[comb] = d; |
---|
201 | if ((comb & 0x3) == 0) |
---|
202 | printf("\n\t0x%08x,", d); |
---|
203 | else if (comb == 15) |
---|
204 | printf(" 0x%08x\n};\n\n", d); |
---|
205 | else |
---|
206 | printf(" 0x%08x,", d); |
---|
207 | } else { |
---|
208 | if (d != PC_DL[comb] << i) |
---|
209 | (void) fprintf(stderr, |
---|
210 | "Error PC_DL 0x%08x c 0x%08x\n", |
---|
211 | PC_DL[comb], d); |
---|
212 | } |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | printf("static u_int32 PC1_CR[16] = {"); |
---|
217 | for (i = 0; i < 4; i++) { |
---|
218 | for (comb = 0; comb < 16; comb++) { |
---|
219 | if (comb & 0x8) |
---|
220 | bits[rc4[i]] = 1; |
---|
221 | if (comb & 0x4) |
---|
222 | bits[rc4[i]+1] = 1; |
---|
223 | if (comb & 0x2) |
---|
224 | bits[rc4[i]+2] = 1; |
---|
225 | if (comb & 0x1) |
---|
226 | bits[rc4[i]+3] = 1; |
---|
227 | permute(bits, &c, &d); |
---|
228 | bits[rc4[i]] = 0; |
---|
229 | bits[rc4[i]+1] = 0; |
---|
230 | bits[rc4[i]+2] = 0; |
---|
231 | bits[rc4[i]+3] = 0; |
---|
232 | if (d != 0) { |
---|
233 | (void) fprintf(stderr, |
---|
234 | "Error PC_CR i %d comb %d\n", i, comb); |
---|
235 | } |
---|
236 | if (i == 0) { |
---|
237 | PC_CR[comb] = c; |
---|
238 | if ((comb & 0x3) == 0) |
---|
239 | printf("\n\t0x%08x,", c); |
---|
240 | else if (comb == 15) |
---|
241 | printf(" 0x%08x\n};\n\n", c); |
---|
242 | else |
---|
243 | printf(" 0x%08x,", c); |
---|
244 | } else { |
---|
245 | if (c != PC_CR[comb] << i) |
---|
246 | (void) fprintf(stderr, |
---|
247 | "Error PC_CR 0x%08x c 0x%08x\n", |
---|
248 | PC_CR[comb], c); |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | printf("static u_int32 PC1_DR[8] = {"); |
---|
254 | for (i = 0; i < 4; i++) { |
---|
255 | for (comb = 0; comb < 8; comb++) { |
---|
256 | if (comb & 0x4) |
---|
257 | bits[rd3[i]] = 1; |
---|
258 | if (comb & 0x2) |
---|
259 | bits[rd3[i]+1] = 1; |
---|
260 | if (comb & 0x1) |
---|
261 | bits[rd3[i]+2] = 1; |
---|
262 | permute(bits, &c, &d); |
---|
263 | bits[rd3[i]] = 0; |
---|
264 | bits[rd3[i]+1] = 0; |
---|
265 | bits[rd3[i]+2] = 0; |
---|
266 | if (c != 0) { |
---|
267 | (void) fprintf(stderr, |
---|
268 | "Error PC_DR i %d comb %d\n", i, comb); |
---|
269 | } |
---|
270 | if (i == 0) { |
---|
271 | PC_DR[comb] = d; |
---|
272 | if ((comb & 0x3) == 0) |
---|
273 | printf("\n\t0x%08x,", d); |
---|
274 | else if (comb == 7) |
---|
275 | printf(" 0x%08x\n};\n\n", d); |
---|
276 | else |
---|
277 | printf(" 0x%08x,", d); |
---|
278 | } else { |
---|
279 | if (d != PC_DR[comb] << i) |
---|
280 | (void) fprintf(stderr, |
---|
281 | "Error PC_DR 0x%08x c 0x%08x\n", |
---|
282 | PC_DR[comb], d); |
---|
283 | } |
---|
284 | } |
---|
285 | } |
---|
286 | } |
---|