|
|
1.1 root 1: /*
2: * $Source: /mit/kerberos/src/lib/des/RCS/misc.c,v $
3: * $Author: jtkohl $
4: *
5: * Copyright 1988 by the Massachusetts Institute of Technology.
6: *
7: * For copying and distribution information,
8: * please seethe file <mit-copyright.h>.
9: *
10: * This file contains most of the routines needed by the various
11: * make_foo programs, to account for bit- and byte-ordering on
12: * different machine types. It also contains other routines useful in
13: * generating the intermediate source files.
14: */
15:
16: #include <mit-copyright.h>
17: #include <stdio.h>
18: #include "des_internal.h"
19:
20: /*
21: * The DES algorithm is defined in terms of MSBFIRST, so sometimes,
22: * e.g. VAXes, we need to fix it up. ANSI order means the DES
23: * MSBFIRST order.
24: */
25:
26: #if 0 /* These don't seem to get used anywhere.... */
27: void swap_bits(array)
28: char *array;
29: {
30: #ifdef MSBFIRST
31: /* just return */
32: return;
33: #else /* LSBFIRST */
34: register old,new,i,j;
35:
36: /* for an eight byte block-- */
37: /* flips the bit order within each byte from 0 lsb to 0 msb */
38: for (i = 0; i<=7; i++) {
39: old = *array;
40: new = 0;
41: for (j = 0; j<=7; j++) {
42: new |= old & 01; /* copy a bit */
43: if (j < 7) {
44: /* rotate in opposite directions */
45: old = old >> 1;
46: new = new << 1;
47: }
48: }
49: *array++ = new;
50: }
51: #endif /* MSBFIRST */
52: }
53:
54: unsigned long long_swap_bits(x)
55: unsigned long x;
56: {
57: #ifdef MSBFIRST
58: return x;
59: #else
60: char *array = (char *) &x;
61: register old,new,i,j;
62:
63: /* flips the bit order within each byte from 0 lsb to 0 msb */
64: for (i = 0; i <= (sizeof(long)-1); i++) {
65: old = *array;
66: new = 0;
67: for (j = 0; j<=7; j++) {
68: if (old & 01)
69: new = new | 01;
70: if (j < 7) {
71: old = old >> 1;
72: new = new << 1;
73: }
74: }
75: *array++ = new;
76: }
77: return x;
78: #endif /* LSBFIRST */
79: }
80: #endif /* 0 */
81:
82: unsigned long swap_six_bits_to_ansi(old)
83: unsigned long old;
84: {
85: register unsigned long new, j;
86:
87: /* flips the bit order within each byte from 0 lsb to 0 msb */
88: new = 0;
89: for (j = 0; j<=5; j++) {
90: new |= old & 01; /* copy a bit */
91: if (j < 5) {
92: /* rotate in opposite directions */
93: old = old >> 1;
94: new = new << 1;
95: }
96: }
97: return new;
98: }
99:
100: unsigned long swap_four_bits_to_ansi(old)
101: unsigned long old;
102: {
103: register unsigned long new,j;
104:
105: /* flips the bit order within each byte from 0 lsb to 0 msb */
106: new = 0;
107: for (j = 0; j<=3; j++) {
108: new |= (old & 01); /* copy a bit */
109: if (j < 3) {
110: old = old >> 1;
111: new = new << 1;
112: }
113: }
114: return new;
115: }
116:
117: unsigned long swap_bit_pos_1(x)
118: unsigned long x;
119: {
120: /*
121: * This corrects for the bit ordering of the algorithm, e.g.
122: * bit 0 ==> msb, bit 7 lsb.
123: *
124: * given the number of a bit position, >=1, flips the bit order
125: * each byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
126: */
127: register y,z;
128:
129: /* always do it, only used by des_make_key_perm.c so far */
130: y = (x-1)/8;
131: z = (x-1)%8;
132:
133: x = (8-z) + (y*8);
134:
135: return x;
136: }
137:
138: unsigned long swap_bit_pos_0(x)
139: unsigned long x;
140: {
141: /* zero based version */
142:
143: /*
144: * This corrects for the bit ordering of the algorithm, e.g.
145: * bit 0 ==> msb, bit 7 lsb.
146: */
147:
148: #ifdef MSBFIRST
149: return x;
150: #else /* LSBFIRST */
151: register y,z;
152:
153: /*
154: * given the number of a bit position, >=0, flips the bit order
155: * each byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
156: */
157: y = x/8;
158: z = x%8;
159:
160: x = (7-z) + (y*8);
161:
162: return x;
163: #endif /* LSBFIRST */
164: }
165:
166: unsigned long swap_bit_pos_0_to_ansi(x)
167: unsigned long x;
168: {
169: /* zero based version */
170:
171: /*
172: * This corrects for the bit ordering of the algorithm, e.g.
173: * bit 0 ==> msb, bit 7 lsb.
174: */
175:
176: register y,z;
177: /*
178: * given the number of a bit position, >=0, flips the bit order each
179: * byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
180: */
181: y = x/8;
182: z = x%8;
183:
184: x = (7-z) + (y*8);
185:
186: return x;
187: }
188:
189: unsigned long rev_swap_bit_pos_0(x)
190: unsigned long x;
191: {
192: /* zero based version */
193:
194: /*
195: * This corrects for the bit ordering of the algorithm, e.g.
196: * bit 0 ==> msb, bit 7 lsb.
197: *
198: * Role of LSB and MSB flipped from the swap_bit_pos_0()
199: */
200:
201: #ifdef LSBFIRST
202: return x;
203: #else /* MSBFIRST */
204:
205: register y,z;
206:
207: /*
208: * given the number of a bit position, >=0, flips the bit order each
209: * byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
210: */
211: y = x/8;
212: z = x%8;
213:
214: x = (7-z) + (y*8);
215:
216: return x;
217: #endif /* MSBFIRST */
218: }
219:
220: unsigned long swap_byte_bits(x)
221: unsigned long x;
222: {
223: #ifdef MSBFIRST
224: return x;
225: #else /* LSBFIRST */
226:
227: char *array = (char *) &x;
228: register unsigned long old,new,j;
229:
230: /* flips the bit order within each byte from 0 lsb to 0 msb */
231: old = *array;
232: new = 0;
233: for (j = 0; j<=7; j++) {
234: new |= (old & 01); /* copy a bit */
235: if (j < 7) {
236: old = old >> 1;
237: new = new << 1;
238: }
239: }
240: return new;
241: #endif /* LSBFIRST */
242: }
243:
244: swap_long_bytes_bit_number(x)
245: unsigned long x;
246: {
247: /*
248: * given a bit number (0-31) from a vax, swap the byte part of the
249: * bit number to change the byte ordering to mSBFIRST type
250: */
251: #ifdef LSBFIRST
252: return x;
253: #else /* MSBFIRST */
254: unsigned long y,z;
255:
256: y = x/8; /* initial byte component */
257: z = x%8; /* bit within byte */
258:
259: x = (3-y)*8 +z;
260: return x;
261: #endif /* MSBFIRST */
262: }
263:
264: void test_set(stream, src, testbit, dest, setbit)
265: FILE *stream;
266: const char *src;
267: int testbit;
268: const char *dest;
269: int setbit;
270: {
271: #ifdef DES_SHIFT_SHIFT
272: if (testbit == setbit)
273: fprintf(stream, " %s |= %s & (1<<%2d);\n",
274: dest, src, testbit);
275: else
276: fprintf(stream, " %s |= (%s & (1<<%2d)) %s %2d;\n",
277: dest, src, testbit,
278: (testbit < setbit) ? "<<" : ">>",
279: abs(testbit - setbit));
280: #else
281: fprintf(stream,
282: " if (%s & (1<<%2d)) %s |= 1<<%2d;\n",
283: src, testbit, dest, setbit);
284: #endif
285: }
286:
287: extern void gen PROTOTYPE((FILE * stream));
288: int des_debug;
289: char const *whoami;
290:
291: main(argc, argv)
292: int argc;
293: char *argv[];
294: {
295: char *filename;
296: char *arg;
297: FILE * stream;
298:
299: whoami = argv[0];
300: filename = (char *)NULL;
301:
302: while (argc--, *++argv) {
303: arg = *argv;
304: if (*arg == '-') {
305: if (!strcmp(arg, "-d") && !strcmp(arg, "-debug"))
306: des_debug++;
307: else {
308: fprintf(stderr, "%s: unknown control argument %s\n",
309: whoami, arg);
310: goto usage;
311: }
312: }
313: else if (filename) {
314: fprintf(stderr,
315: "%s: multiple file names provided: %s, %s\n",
316: whoami, filename, arg);
317: goto usage;
318: }
319: else
320: filename = arg;
321: }
322:
323: if (!filename) {
324: fprintf(stderr, "%s: no file name provided\n", whoami);
325: goto usage;
326: }
327:
328: stream = fopen(filename, "w");
329: if (!stream) {
330: perror(filename);
331: usage:
332: fprintf(stderr, "usage: %s [-debug] filename\n", whoami);
333: exit(1);
334: }
335:
336: fputs(
337: "/* This file is automatically generated. Do not edit it. */\n",
338: stream);
339:
340: /* This routine will generate the contents of the file. */
341: gen(stream);
342: if (fclose(stream) == EOF) {
343: perror(filename);
344: exit(1);
345: }
346: exit(0);
347: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.