2 * Copyright 1988 by the Massachusetts Institute of Technology.
4 * For copying and distribution information,
5 * please seethe file <mit-cpyright.h>.
7 * This file contains most of the routines needed by the various
8 * make_foo programs, to account for bit- and byte-ordering on
9 * different machine types. It also contains other routines useful in
10 * generating the intermediate source files.
13 #include <afsconfig.h>
14 #include <afs/param.h>
17 #include <mit-cpyright.h>
22 #include "des_internal.h"
23 #include "des_prototypes.h"
28 * The DES algorithm is defined in terms of MSBFIRST, so sometimes,
29 * e.g. VAXes, we need to fix it up. ANSI order means the DES
33 #if 0 /* These don't seem to get used anywhere.... */
35 swap_bits(char *array)
41 register int old, new, i, j;
43 /* for an eight byte block-- */
44 /* flips the bit order within each byte from 0 lsb to 0 msb */
45 for (i = 0; i <= 7; i++) {
48 for (j = 0; j <= 7; j++) {
49 new |= old & 01; /* copy a bit */
51 /* rotate in opposite directions */
62 long_swap_bits(afs_uint32 x)
67 char *array = (char *)&x;
68 register int old, new, i, j;
70 /* flips the bit order within each byte from 0 lsb to 0 msb */
71 for (i = 0; i <= (sizeof(afs_int32) - 1); i++) {
74 for (j = 0; j <= 7; j++) {
90 swap_six_bits_to_ansi(afs_uint32 old)
92 register afs_uint32 new, j;
94 /* flips the bit order within each byte from 0 lsb to 0 msb */
96 for (j = 0; j <= 5; j++) {
97 new |= old & 01; /* copy a bit */
99 /* rotate in opposite directions */
108 swap_four_bits_to_ansi(afs_uint32 old)
110 register afs_uint32 new, j;
112 /* flips the bit order within each byte from 0 lsb to 0 msb */
114 for (j = 0; j <= 3; j++) {
115 new |= (old & 01); /* copy a bit */
125 swap_bit_pos_1(afs_uint32 x)
128 * This corrects for the bit ordering of the algorithm, e.g.
129 * bit 0 ==> msb, bit 7 lsb.
131 * given the number of a bit position, >=1, flips the bit order
132 * each byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
136 /* always do it, only used by des_make_key_perm.c so far */
140 x = (8 - z) + (y * 8);
146 swap_bit_pos_0(afs_uint32 x)
148 /* zero based version */
151 * This corrects for the bit ordering of the algorithm, e.g.
152 * bit 0 ==> msb, bit 7 lsb.
161 * given the number of a bit position, >=0, flips the bit order
162 * each byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
167 x = (7 - z) + (y * 8);
170 #endif /* LSBFIRST */
174 swap_bit_pos_0_to_ansi(afs_uint32 x)
176 /* zero based version */
179 * This corrects for the bit ordering of the algorithm, e.g.
180 * bit 0 ==> msb, bit 7 lsb.
185 * given the number of a bit position, >=0, flips the bit order each
186 * byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
191 x = (7 - z) + (y * 8);
197 rev_swap_bit_pos_0(afs_uint32 x)
199 /* zero based version */
202 * This corrects for the bit ordering of the algorithm, e.g.
203 * bit 0 ==> msb, bit 7 lsb.
205 * Role of LSB and MSB flipped from the swap_bit_pos_0()
215 * given the number of a bit position, >=0, flips the bit order each
216 * byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
221 x = (7 - z) + (y * 8);
224 #endif /* MSBFIRST */
228 swap_byte_bits(afs_uint32 x)
234 char *array = (char *)&x;
235 register afs_uint32 old, new, j;
237 /* flips the bit order within each byte from 0 lsb to 0 msb */
240 for (j = 0; j <= 7; j++) {
241 new |= (old & 01); /* copy a bit */
248 #endif /* LSBFIRST */
252 swap_long_bytes_bit_number(afs_uint32 x)
255 * given a bit number (0-31) from a vax, swap the byte part of the
256 * bit number to change the byte ordering to mSBFIRST type
263 y = x / 8; /* initial byte component */
264 z = x % 8; /* bit within byte */
268 #endif /* MSBFIRST */
271 #if !defined(KERNEL) && defined(AFS_DARWIN80_ENV)
272 char *_darwin_whichstr[] = {
273 "#if defined(__ppc__) || defined(__ppc64__)\n",
274 "#elif defined(__i386__) || defined(__amd64__)\n",
275 "#else\n#error architecture unsupported\n#endif\n"
277 int _darwin_which = 1;
280 _darwin_swap_long_bytes_bit_number(afs_uint32 x)
283 * given a bit number (0-31) from a vax, swap the byte part of the
284 * bit number to change the byte ordering to mSBFIRST type
292 y = x / 8; /* initial byte component */
293 z = x % 8; /* bit within byte */
298 #endif /* !KERNEL && AFS_DARWIN80_ENV */
301 test_set(FILE * stream, const char *src, int testbit, const char *dest,
304 #ifdef DES_SHIFT_SHIFT
305 if (testbit == setbit)
306 fprintf(stream, " %s |= %s & (1<<%2d);\n", dest, src, testbit);
308 fprintf(stream, " %s |= (%s & (1<<%2d)) %s %2d;\n", dest, src,
309 testbit, (testbit < setbit) ? "<<" : ">>",
310 abs(testbit - setbit));
312 fprintf(stream, " if (%s & (1<<%2d)) %s |= 1<<%2d;\n", src, testbit,