summaryrefslogtreecommitdiffstats
path: root/src/lib/crypto/des/misc.c
blob: 283720f4d74f8d34d0b15b6df9120f90badd16e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * $Source$
 * $Author$
 *
 * Copyright 1988, 1990 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 * 
 *
 * This file contains most of the routines needed by the various
 * make_foo programs, to account for bit- and byte-ordering on
 * different machine types.  It also contains other routines useful in
 * generating the intermediate source files.
 */

#if !defined(lint) && !defined(SABER)
static char rcsid_misc_c[] =
"$Id$";
#endif	/* !lint & !SABER */

#include <krb5/krb5.h>
#include <krb5/ext-proto.h>
#include <stdio.h>

#include "des_int.h"

/*
 * The DES algorithm is defined in terms of MSBFIRST, so sometimes,
 * e.g.  VAXes, we need to fix it up.  ANSI order means the DES
 * MSBFIRST order.
 */

#if 0 /* These don't seem to get used anywhere.... */
void swap_bits(array)
    char *array;
{
#ifdef MSBFIRST
    /* just return */
    return;
#else /* LSBFIRST */
    register old,new,i,j;

    /* for an eight byte block-- */
    /* flips the bit order within each byte from 0 lsb to 0 msb */
    for (i = 0; i<=7; i++) {
        old = *array;
        new = 0;
        for (j = 0; j<=7; j++) {
            new |= old & 01;    /* copy a bit */
            if (j < 7) {
                /* rotate in opposite directions */
                old = old >> 1;
                new = new << 1;
            }
        }
        *array++ = new;
    }
#endif /* MSBFIRST */
}

unsigned long long_swap_bits(x)
    unsigned long x;
{
#ifdef MSBFIRST
    return x;
#else
    char *array = (char *) &x;
    register old,new,i,j;

    /* flips the bit order within each byte from 0 lsb to 0 msb */
    for (i = 0; i <= (sizeof(long)-1); i++) {
        old = *array;
        new = 0;
        for (j = 0; j<=7; j++) {
            if (old & 01)
                new = new | 01;
            if (j < 7) {
                old = old >> 1;
                new = new << 1;
            }
        }
        *array++ = new;
    }
    return x;
#endif /* LSBFIRST */
}
#endif /* 0 */

unsigned long swap_six_bits_to_ansi(old)
    unsigned long old;
{
    register unsigned long new, j;

    /* flips the bit order within each byte from 0 lsb to 0 msb */
    new = 0;
    for (j = 0; j<=5; j++) {
        new |= old & 01;        /* copy a bit */
        if (j < 5) {
            /* rotate in opposite directions */
            old = old >> 1;
            new = new << 1;
        }
    }
    return new;
}

unsigned long swap_four_bits_to_ansi(old)
    unsigned long old;
{
    register unsigned long new,j;

    /* flips the bit order within each byte from 0 lsb to 0 msb */
    new = 0;
    for (j = 0; j<=3; j++) {
        new |= (old & 01);      /* copy a bit */
        if (j < 3) {
            old = old >> 1;
            new = new << 1;
        }
    }
    return new;
}

unsigned long swap_bit_pos_1(x)
    unsigned long x;
{
    /*
     * This corrects for the bit ordering of the algorithm, e.g.
     * bit 0 ==> msb, bit 7 lsb.
     *
     * given the number of a bit position, >=1, flips the bit order
     * each byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
     */
    register y,z;

    /* always do it, only used by mit_des_make_key_perm.c so far */
    y = (x-1)/8;
    z = (x-1)%8;

    x = (8-z) + (y*8);

    return x;
}

unsigned long swap_bit_pos_0(x)
    unsigned long x;
{
    /*  zero based version */

    /*
     * This corrects for the bit ordering of the algorithm, e.g.
     * bit 0 ==> msb, bit 7 lsb.
     */

#ifdef MSBFIRST
    return x;
#else /* LSBFIRST */
    register y,z;

    /*
     * given the number of a bit position, >=0, flips the bit order
     * each byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
     */
    y = x/8;
    z = x%8;

    x = (7-z) + (y*8);

    return x;
#endif /* LSBFIRST */
}

unsigned long swap_bit_pos_0_to_ansi(x)
    unsigned long x;
{
    /* zero based version */

    /*
     * This corrects for the bit ordering of the algorithm, e.g.
     * bit 0 ==> msb, bit 7 lsb.
     */

    register y,z;
    /*
     * given the number of a bit position, >=0, flips the bit order each
     * byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
     */
    y = x/8;
    z = x%8;

    x = (7-z) + (y*8);

    return x;
}

unsigned long rev_swap_bit_pos_0(x)
    unsigned long x;
{
    /* zero based version */

    /*
     * This corrects for the bit ordering of the algorithm, e.g.
     *  bit 0 ==> msb, bit 7 lsb.
     *
     * Role of LSB and MSB flipped from the swap_bit_pos_0()
     */

#ifdef LSBFIRST
    return x;
#else /* MSBFIRST */

    register y,z;

    /*
     * given the number of a bit position, >=0, flips the bit order each
     * byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
     */
    y = x/8;
    z = x%8;

    x = (7-z) + (y*8);

    return x;
#endif /* MSBFIRST */
}

unsigned long swap_byte_bits(x)
    unsigned long x;
{
#ifdef MSBFIRST
    return x;
#else /* LSBFIRST */

    char *array = (char *) &x;
    register unsigned long old,new,j;

    /* flips the bit order within each byte from 0 lsb to 0 msb */
    old = *array;
    new = 0;
    for (j = 0; j<=7; j++) {
        new |= (old & 01);      /* copy a bit */
        if (j < 7) {
            old = old >> 1;
            new = new << 1;
        }
    }
    return new;
#endif /* LSBFIRST */
}

unsigned long
swap_long_bytes_bit_number(x)
    unsigned long x;
{
    /*
     * given a bit number (0-31) from a vax, swap the byte part of the
     * bit number to change the byte ordering to mSBFIRST type
     */
#ifdef LSBFIRST
    return x;
#else /* MSBFIRST */
    unsigned long y,z;

    y = x/8;                    /* initial byte component */
    z = x%8;                    /* bit within byte */

    x = (3-y)*8 +z;
    return x;
#endif /* MSBFIRST */
}

void test_set(stream, src, testbit, dest, setbit)
    FILE *stream;
    const char *src;
    int testbit;
    const char *dest;
    int setbit;
{
#ifdef DES_SHIFT_SHIFT
    if (testbit == setbit)
        fprintf(stream, "    %s |=  %s & (1<<%2d);\n",
                dest, src, testbit);
    else
        fprintf(stream, "    %s |= (%s & (1<<%2d)) %s %2d;\n",
                dest, src, testbit,
                (testbit < setbit) ? "<<" : ">>",
                abs(testbit - setbit));
#else
    fprintf(stream,
            "    if (%s & (1<<%2d))  %s |= 1<<%2d;\n",
            src, testbit, dest, setbit);
#endif
}

extern void gen PROTOTYPE((FILE *));
int mit_des_debug;
char const *whoami;

void
main(argc, argv)
    int argc;
    char *argv[];
{
    char *filename;
    char *arg;
    FILE *stream = 0;

    whoami = argv[0];
    filename = (char *)NULL;

    while (argc--, *++argv) {
        arg = *argv;
        if (*arg == '-') {
	    if (!strcmp(arg, "-d") || !strcmp(arg, "-debug"))
                mit_des_debug++;
            else {
                fprintf(stderr, "%s: unknown control argument %s\n",
                        whoami, arg);
                goto usage;
            }
        }
        else if (filename) {
            fprintf(stderr,
                    "%s: multiple file names provided: %s, %s\n",
                    whoami, filename, arg);
            goto usage;
        }
        else
            filename = arg;
    }

    if (!filename) {
        fprintf(stderr, "%s: no file name provided\n", whoami);
        goto usage;
    }

    stream = fopen(filename, "w");
    if (!stream) {
        perror(filename);
    usage:
        fprintf(stderr, "usage: %s [-debug] filename\n", whoami);
        exit(1);
    }

    fputs(
      "/* This file is automatically generated.  Do not edit it. */\n",
          stream);

    /* This routine will generate the contents of the file. */
    gen(stream);
    if (fclose(stream) == EOF) {
        perror(filename);
        exit(1);
    }
    exit(0);
}