1 | /* |
---|
2 | * makeIPFP - make fast DES IP and FP 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 | u_int32 IPL[256]; |
---|
13 | u_int32 FPL[256]; |
---|
14 | |
---|
15 | char *progname; |
---|
16 | int debug; |
---|
17 | |
---|
18 | static void perm P((u_char *, u_char *, u_int32 *, u_int32 *)); |
---|
19 | static void doit P((void)); |
---|
20 | |
---|
21 | /* |
---|
22 | * main - parse arguments and handle options |
---|
23 | */ |
---|
24 | void |
---|
25 | main(argc, argv) |
---|
26 | int argc; |
---|
27 | char *argv[]; |
---|
28 | { |
---|
29 | int c; |
---|
30 | int errflg = 0; |
---|
31 | extern int ntp_optind; |
---|
32 | extern char *ntp_optarg; |
---|
33 | |
---|
34 | progname = argv[0]; |
---|
35 | while ((c = ntp_getopt(argc, argv, "d")) != EOF) |
---|
36 | switch (c) { |
---|
37 | case 'd': |
---|
38 | ++debug; |
---|
39 | break; |
---|
40 | default: |
---|
41 | errflg++; |
---|
42 | break; |
---|
43 | } |
---|
44 | if (errflg) { |
---|
45 | (void) fprintf(stderr, "usage: %s [-d]\n", progname); |
---|
46 | exit(2); |
---|
47 | } |
---|
48 | doit(); |
---|
49 | exit(0); |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | /* |
---|
54 | * Initial permutation table |
---|
55 | */ |
---|
56 | u_char IP[64] = { |
---|
57 | 58, 50, 42, 34, 26, 18, 10, 2, |
---|
58 | 60, 52, 44, 36, 28, 20, 12, 4, |
---|
59 | 62, 54, 46, 38, 30, 22, 14, 6, |
---|
60 | 64, 56, 48, 40, 32, 24, 16, 8, |
---|
61 | 57, 49, 41, 33, 25, 17, 9, 1, |
---|
62 | 59, 51, 43, 35, 27, 19, 11, 3, |
---|
63 | 61, 53, 45, 37, 29, 21, 13, 5, |
---|
64 | 63, 55, 47, 39, 31, 23, 15, 7 |
---|
65 | }; |
---|
66 | |
---|
67 | /* |
---|
68 | * Inverse initial permutation table |
---|
69 | */ |
---|
70 | u_char FP[64] = { |
---|
71 | 40, 8, 48, 16, 56, 24, 64, 32, |
---|
72 | 39, 7, 47, 15, 55, 23, 63, 31, |
---|
73 | 38, 6, 46, 14, 54, 22, 62, 30, |
---|
74 | 37, 5, 45, 13, 53, 21, 61, 29, |
---|
75 | 36, 4, 44, 12, 52, 20, 60, 28, |
---|
76 | 35, 3, 43, 11, 51, 19, 59, 27, |
---|
77 | 34, 2, 42, 10, 50, 18, 58, 26, |
---|
78 | 33, 1, 41, 9, 49, 17, 57, 25 |
---|
79 | }; |
---|
80 | |
---|
81 | |
---|
82 | /* |
---|
83 | * Bit order after the operation |
---|
84 | * |
---|
85 | * ((left & 0x55555555) << 1) | (right & 0x55555555) |
---|
86 | */ |
---|
87 | u_char IPLbits[32] = { |
---|
88 | 2, 34, 4, 36, 6, 38, 8, 40, |
---|
89 | 10, 42, 12, 44, 14, 46, 16, 48, |
---|
90 | 18, 50, 20, 52, 22, 54, 24, 56, |
---|
91 | 26, 58, 28, 60, 30, 62, 32, 64 |
---|
92 | }; |
---|
93 | |
---|
94 | |
---|
95 | /* |
---|
96 | * Bit order after the operation |
---|
97 | * |
---|
98 | * (left & 0xaaaaaaaa) | ((right & 0xaaaaaaaa) >> 1) |
---|
99 | */ |
---|
100 | u_char IPRbits[32] = { |
---|
101 | 1, 33, 3, 35, 5, 37, 7, 39, |
---|
102 | 9, 41, 11, 43, 13, 45, 15, 47, |
---|
103 | 17, 49, 19, 51, 21, 53, 23, 55, |
---|
104 | 25, 57, 27, 59, 29, 61, 31, 63 |
---|
105 | }; |
---|
106 | |
---|
107 | |
---|
108 | /* |
---|
109 | * Bit order after the operation |
---|
110 | * |
---|
111 | * ((left & 0x0f0f0f0f) << 4) | (right & 0x0f0f0f0f) |
---|
112 | */ |
---|
113 | u_char FPLbits[32] = { |
---|
114 | 5, 6, 7, 8, 37, 38, 39, 40, |
---|
115 | 13, 14, 15, 16, 45, 46, 47, 48, |
---|
116 | 21, 22, 23, 24, 53, 54, 55, 56, |
---|
117 | 29, 30, 31, 32, 61, 62, 63, 64 |
---|
118 | }; |
---|
119 | |
---|
120 | |
---|
121 | /* |
---|
122 | * Bit order after the operation |
---|
123 | * |
---|
124 | * (left & 0xf0f0f0f0) | ((right & 0xf0f0f0f0) >> 4) |
---|
125 | */ |
---|
126 | u_char FPRbits[32] = { |
---|
127 | 1, 2, 3, 4, 33, 34, 35, 36, |
---|
128 | 9, 10, 11, 12, 41, 42, 43, 44, |
---|
129 | 17, 18, 19, 20, 49, 50, 51, 52, |
---|
130 | 25, 26, 27, 28, 57, 58, 59, 60 |
---|
131 | }; |
---|
132 | |
---|
133 | |
---|
134 | /* |
---|
135 | * perm - do a permutation with the given table |
---|
136 | */ |
---|
137 | static void |
---|
138 | perm(databits, permtab, leftp, rightp) |
---|
139 | u_char *databits; |
---|
140 | u_char *permtab; |
---|
141 | u_int32 *leftp; |
---|
142 | u_int32 *rightp; |
---|
143 | { |
---|
144 | register u_int32 left; |
---|
145 | register u_int32 right; |
---|
146 | register u_char *PT; |
---|
147 | register u_char *bits; |
---|
148 | register int i; |
---|
149 | |
---|
150 | left = right = 0; |
---|
151 | PT = permtab; |
---|
152 | bits = databits; |
---|
153 | |
---|
154 | for (i = 0; i < 32; i++) { |
---|
155 | left <<= 1; |
---|
156 | if (bits[PT[i]-1]) |
---|
157 | left |= 1; |
---|
158 | } |
---|
159 | |
---|
160 | for (i = 32; i < 64; i++) { |
---|
161 | right <<= 1; |
---|
162 | if (bits[PT[i]-1]) |
---|
163 | right |= 1; |
---|
164 | } |
---|
165 | |
---|
166 | *leftp = left; |
---|
167 | *rightp = right; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | /* |
---|
172 | * doit - make up the tables |
---|
173 | */ |
---|
174 | static void |
---|
175 | doit() |
---|
176 | { |
---|
177 | u_char bits[64]; |
---|
178 | u_int32 left; |
---|
179 | u_int32 right; |
---|
180 | int tabno; |
---|
181 | int i; |
---|
182 | int ind0, ind1, ind2, ind3; |
---|
183 | int ind4, ind5, ind6, ind7; |
---|
184 | int octbits; |
---|
185 | |
---|
186 | memset((char *)bits, 0, sizeof bits); |
---|
187 | |
---|
188 | /* |
---|
189 | * Do the rounds for the IP table. We save the results of |
---|
190 | * this as well as printing them. Note that this is the |
---|
191 | * left-half table, the right half table will be identical. |
---|
192 | */ |
---|
193 | printf("static u_int32 IP[256] = {"); |
---|
194 | for (tabno = 0; tabno < 4; tabno++) { |
---|
195 | i = tabno * 8; |
---|
196 | ind7 = IPLbits[i] - 1; |
---|
197 | ind6 = IPLbits[i+1] - 1; |
---|
198 | ind5 = IPLbits[i+2] - 1; |
---|
199 | ind4 = IPLbits[i+3] - 1; |
---|
200 | ind3 = IPLbits[i+4] - 1; |
---|
201 | ind2 = IPLbits[i+5] - 1; |
---|
202 | ind1 = IPLbits[i+6] - 1; |
---|
203 | ind0 = IPLbits[i+7] - 1; |
---|
204 | for (octbits = 0; octbits < 256; octbits++) { |
---|
205 | if (octbits & (1 << 7)) |
---|
206 | bits[ind7] = 1; |
---|
207 | if (octbits & (1 << 6)) |
---|
208 | bits[ind6] = 1; |
---|
209 | if (octbits & (1 << 5)) |
---|
210 | bits[ind5] = 1; |
---|
211 | if (octbits & (1 << 4)) |
---|
212 | bits[ind4] = 1; |
---|
213 | if (octbits & (1 << 3)) |
---|
214 | bits[ind3] = 1; |
---|
215 | if (octbits & (1 << 2)) |
---|
216 | bits[ind2] = 1; |
---|
217 | if (octbits & (1 << 1)) |
---|
218 | bits[ind1] = 1; |
---|
219 | if (octbits & 1) |
---|
220 | bits[ind0] = 1; |
---|
221 | perm(bits, IP, &left, &right); |
---|
222 | bits[ind7] = 0; |
---|
223 | bits[ind6] = 0; |
---|
224 | bits[ind5] = 0; |
---|
225 | bits[ind4] = 0; |
---|
226 | bits[ind3] = 0; |
---|
227 | bits[ind2] = 0; |
---|
228 | bits[ind1] = 0; |
---|
229 | bits[ind0] = 0; |
---|
230 | if (right != 0) { |
---|
231 | fprintf(stderr, |
---|
232 | "IP tabno %d oct %d right not zero\n", |
---|
233 | tabno, octbits); |
---|
234 | exit(1); |
---|
235 | } |
---|
236 | if (tabno > 0) { |
---|
237 | if ((IPL[octbits] << tabno) != left) { |
---|
238 | fprintf(stderr, |
---|
239 | "IP tabno %d oct %d IP %d left %d, IP != left\n", |
---|
240 | tabno, octbits, IPL[octbits], left); |
---|
241 | exit (1); |
---|
242 | } |
---|
243 | } else { |
---|
244 | IPL[octbits] = left; |
---|
245 | if (octbits == 255) { |
---|
246 | printf(" 0x%08x", left); |
---|
247 | } else if (octbits & 0x3) { |
---|
248 | printf(" 0x%08x,", left); |
---|
249 | } else { |
---|
250 | printf("\n\t0x%08x,", left); |
---|
251 | } |
---|
252 | } |
---|
253 | } |
---|
254 | if (tabno == 0) |
---|
255 | printf("\n};\n\n"); |
---|
256 | } |
---|
257 | |
---|
258 | /* |
---|
259 | * Next is the FP table, in big endian order |
---|
260 | */ |
---|
261 | printf("#if BYTE_ORDER == LITTLE_ENDIAN\nstatic u_int32 FP[256] = {"); |
---|
262 | for (tabno = 3; tabno >= 0; tabno--) { |
---|
263 | i = tabno * 8; |
---|
264 | ind7 = FPLbits[i] - 1; |
---|
265 | ind6 = FPLbits[i+1] - 1; |
---|
266 | ind5 = FPLbits[i+2] - 1; |
---|
267 | ind4 = FPLbits[i+3] - 1; |
---|
268 | ind3 = FPLbits[i+4] - 1; |
---|
269 | ind2 = FPLbits[i+5] - 1; |
---|
270 | ind1 = FPLbits[i+6] - 1; |
---|
271 | ind0 = FPLbits[i+7] - 1; |
---|
272 | for (octbits = 0; octbits < 256; octbits++) { |
---|
273 | if (octbits & (1 << 7)) |
---|
274 | bits[ind7] = 1; |
---|
275 | if (octbits & (1 << 6)) |
---|
276 | bits[ind6] = 1; |
---|
277 | if (octbits & (1 << 5)) |
---|
278 | bits[ind5] = 1; |
---|
279 | if (octbits & (1 << 4)) |
---|
280 | bits[ind4] = 1; |
---|
281 | if (octbits & (1 << 3)) |
---|
282 | bits[ind3] = 1; |
---|
283 | if (octbits & (1 << 2)) |
---|
284 | bits[ind2] = 1; |
---|
285 | if (octbits & (1 << 1)) |
---|
286 | bits[ind1] = 1; |
---|
287 | if (octbits & 1) |
---|
288 | bits[ind0] = 1; |
---|
289 | perm(bits, FP, &left, &right); |
---|
290 | bits[ind7] = 0; |
---|
291 | bits[ind6] = 0; |
---|
292 | bits[ind5] = 0; |
---|
293 | bits[ind4] = 0; |
---|
294 | bits[ind3] = 0; |
---|
295 | bits[ind2] = 0; |
---|
296 | bits[ind1] = 0; |
---|
297 | bits[ind0] = 0; |
---|
298 | if (right != 0) { |
---|
299 | fprintf(stderr, |
---|
300 | "FP tabno %d oct %d right not zero\n", |
---|
301 | tabno, octbits); |
---|
302 | exit(1); |
---|
303 | } |
---|
304 | if (tabno != 3) { |
---|
305 | if ((FPL[octbits] << ((3-tabno)<<1)) != left) { |
---|
306 | fprintf(stderr, |
---|
307 | "FP tabno %d oct %d FP %x left %x, FP != left\n", |
---|
308 | tabno, octbits, FPL[octbits], left); |
---|
309 | exit (1); |
---|
310 | } |
---|
311 | } else { |
---|
312 | FPL[octbits] = left; |
---|
313 | if (octbits == 255) { |
---|
314 | printf(" 0x%08x", left); |
---|
315 | } else if (octbits & 0x3) { |
---|
316 | printf(" 0x%08x,", left); |
---|
317 | } else { |
---|
318 | printf("\n\t0x%08x,", left); |
---|
319 | } |
---|
320 | } |
---|
321 | } |
---|
322 | if (tabno == 3) |
---|
323 | printf("\n};\n"); |
---|
324 | } |
---|
325 | |
---|
326 | /* |
---|
327 | * Now reouput the FP table in order appropriate for little |
---|
328 | * endian machines |
---|
329 | */ |
---|
330 | printf("#else\nstatic u_int32 FP[256] = {"); |
---|
331 | for (octbits = 0; octbits < 256; octbits++) { |
---|
332 | left = ((FPL[octbits] >> 24) & 0x000000ff) |
---|
333 | | ((FPL[octbits] >> 8) & 0x0000ff00) |
---|
334 | | ((FPL[octbits] << 8) & 0x00ff0000) |
---|
335 | | ((FPL[octbits] << 24) & 0xff000000); |
---|
336 | if (octbits == 255) { |
---|
337 | printf(" 0x%08x", left); |
---|
338 | } else if (octbits & 0x3) { |
---|
339 | printf(" 0x%08x,", left); |
---|
340 | } else { |
---|
341 | printf("\n\t0x%08x,", left); |
---|
342 | } |
---|
343 | } |
---|
344 | printf("\n};\n#endif\n"); |
---|
345 | } |
---|