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>
19 #include <mit-cpyright.h>
24 #include "des_internal.h"
25 #include "des_prototypes.h"
30 * The DES algorithm is defined in terms of MSBFIRST, so sometimes,
31 * e.g. VAXes, we need to fix it up. ANSI order means the DES
35 #if 0 /* These don't seem to get used anywhere.... */
37 swap_bits(char *array)
43 register int old, new, i, j;
45 /* for an eight byte block-- */
46 /* flips the bit order within each byte from 0 lsb to 0 msb */
47 for (i = 0; i <= 7; i++) {
50 for (j = 0; j <= 7; j++) {
51 new |= old & 01; /* copy a bit */
53 /* rotate in opposite directions */
64 long_swap_bits(afs_uint32 x)
69 char *array = (char *)&x;
70 register int old, new, i, j;
72 /* flips the bit order within each byte from 0 lsb to 0 msb */
73 for (i = 0; i <= (sizeof(afs_int32) - 1); i++) {
76 for (j = 0; j <= 7; j++) {
92 swap_six_bits_to_ansi(afs_uint32 old)
94 register afs_uint32 new, j;
96 /* flips the bit order within each byte from 0 lsb to 0 msb */
98 for (j = 0; j <= 5; j++) {
99 new |= old & 01; /* copy a bit */
101 /* rotate in opposite directions */
110 swap_four_bits_to_ansi(afs_uint32 old)
112 register afs_uint32 new, j;
114 /* flips the bit order within each byte from 0 lsb to 0 msb */
116 for (j = 0; j <= 3; j++) {
117 new |= (old & 01); /* copy a bit */
127 swap_bit_pos_1(afs_uint32 x)
130 * This corrects for the bit ordering of the algorithm, e.g.
131 * bit 0 ==> msb, bit 7 lsb.
133 * given the number of a bit position, >=1, flips the bit order
134 * each byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
138 /* always do it, only used by des_make_key_perm.c so far */
142 x = (8 - z) + (y * 8);
148 swap_bit_pos_0(afs_uint32 x)
150 /* zero based version */
153 * This corrects for the bit ordering of the algorithm, e.g.
154 * bit 0 ==> msb, bit 7 lsb.
163 * given the number of a bit position, >=0, flips the bit order
164 * each byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
169 x = (7 - z) + (y * 8);
172 #endif /* LSBFIRST */
176 swap_bit_pos_0_to_ansi(afs_uint32 x)
178 /* zero based version */
181 * This corrects for the bit ordering of the algorithm, e.g.
182 * bit 0 ==> msb, bit 7 lsb.
187 * given the number of a bit position, >=0, flips the bit order each
188 * byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
193 x = (7 - z) + (y * 8);
199 rev_swap_bit_pos_0(afs_uint32 x)
201 /* zero based version */
204 * This corrects for the bit ordering of the algorithm, e.g.
205 * bit 0 ==> msb, bit 7 lsb.
207 * Role of LSB and MSB flipped from the swap_bit_pos_0()
217 * given the number of a bit position, >=0, flips the bit order each
218 * byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
223 x = (7 - z) + (y * 8);
226 #endif /* MSBFIRST */
230 swap_byte_bits(afs_uint32 x)
236 char *array = (char *)&x;
237 register afs_uint32 old, new, j;
239 /* flips the bit order within each byte from 0 lsb to 0 msb */
242 for (j = 0; j <= 7; j++) {
243 new |= (old & 01); /* copy a bit */
250 #endif /* LSBFIRST */
254 swap_long_bytes_bit_number(afs_uint32 x)
257 * given a bit number (0-31) from a vax, swap the byte part of the
258 * bit number to change the byte ordering to mSBFIRST type
265 y = x / 8; /* initial byte component */
266 z = x % 8; /* bit within byte */
270 #endif /* MSBFIRST */
274 test_set(FILE * stream, const char *src, int testbit, const char *dest,
277 #ifdef DES_SHIFT_SHIFT
278 if (testbit == setbit)
279 fprintf(stream, " %s |= %s & (1<<%2d);\n", dest, src, testbit);
281 fprintf(stream, " %s |= (%s & (1<<%2d)) %s %2d;\n", dest, src,
282 testbit, (testbit < setbit) ? "<<" : ">>",
283 abs(testbit - setbit));
285 fprintf(stream, " if (%s & (1<<%2d)) %s |= 1<<%2d;\n", src, testbit,