summaryrefslogtreecommitdiffstats
path: root/src/lib/crypto/des/make_kp.c
blob: 92bb1d65de94a393760d40dc3f2077d821a9f9b8 (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
/*
 * $Source$
 * $Author$
 * $Locker$
 *
 * Copyright 1988,1990 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America is assumed
 *   to 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 routine calculates an effective Key schedule set of
 * permutations for des.  Beginning with the pre-defined key schedule
 * algorithm, it reduces it to a set of 16 permutations upon the
 * initial key.  Only needs to execute once to produce a header file.
 * Note that we subtract one from the values ouput to fix up for C
 * subscripts starting at 0.
 */

#include <krb5/krb5.h>
#include <stdio.h>
#include "des_int.h"

#ifndef lint
static char rcsid_make_kp[]=
"$Id$";
#endif /* lint */

char *progname;
extern int errno;
int sflag;
int vflag;
int dflag;
int pid;
int child_status;

int key_position[64+1];
int C[28+1];
int D[28+1];
int C_temp, D_temp;

/*
 *  CONVENTIONS for numbering the bits
 *  bit 0 ==> lsb
 *  L starts at bit 0
 *  R starts at bit 64
 *
 *  BEWARE-- some stuff starts at 0, some at 1;  perhaps some bugs still?
 */

/*
 * Sequence of shifts used for the key schedule.
 */
int  shift[16+1] = { 0,
    1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1,
};

int pc_1[64+1] = { 0,

    57,49,41,33,25,17, 9,
     1,58,50,42,34,26,18,
    10, 2,59,51,43,35,27,
    19,11, 3,60,52,44,36,

    63,55,47,39,31,23,15,
     7,62,54,46,38,30,22,
    14, 6,61,53,45,37,29,
    21,13, 5,28,20,12, 4,
};


/*
 * Permuted-choice 2, to pick out the bits from
 * the CD array that generate the key schedule.
 */
int  pc_2[48+1] = { 0,

    14,17,11,24, 1, 5,
     3,28,15, 6,21,10,
    23,19,12, 4,26, 8,
    16, 7,27,20,13, 2,

    41,52,31,37,47,55,
    30,40,51,45,33,48,
    44,49,39,56,34,53,
    46,42,50,36,29,32,
};

int ks_perm[16+1][48+1];

int mit_des_debug;

void gen(stream)
    FILE *stream;
{
    /*  Local Declarations */
    register i, j, iter;

    /*
     * initialize the key_position array s.t. key_position[i] = i;
     * that is, each element is equal to its starting position.
     *
     * Also adjust for the bit order within bytes.
     */

    for (i=0; i<65; i++)
        key_position[i]= swap_bit_pos_1(i);

    fprintf(stream,"static int const key_perm[16][48] = {\n");

    /*
     * apply pc_1 to initial key_position to create C[0] and D[0]
     * Start at pc_1[1], not pc_1[0]
     */
    for (i=1; i<=28; i++) {
        C[i] = key_position[pc_1[i]];
        D[i] = key_position[pc_1[i+28]];
    }

    /*
     * major loop over the 16 iterations
     * start at iter = 1, not zero.
     */
    for (iter = 1; iter <= 16; iter++) {
        if (mit_des_debug) {
            /*  for debugging */
            printf(
                    "/* DEBUG-- start iteration = %d  shifts = %d",
                    iter, shift[iter]);
            printf("\nC array");
            for (i = 1; i <=4 ; i++) {
                printf("\n");
                for (j = 1; j<=7; j++)
                    printf("%d, ",C[(i-1)*7+j]);
            }
            printf("\n\nD array");
            for (i = 1; i <=4 ; i++) {
                printf("\n");
                for (j = 1; j<=7; j++)
                    printf("%d, ",D[(i-1)*7+j]);
            }
            printf("\n */");
            fflush(stdout);
        }

        /* apply the appropriate left shifts */
        for (i = 1; i <= shift[iter]; i++) {
            C_temp = C[1];
            D_temp = D[1];
            for (j =1; j<=27; j++) {
                C[j] = C[j+1];
                D[j] = D[j+1];
            }
            C[j] = C_temp;
            D[j] = D_temp;
        }


        if (mit_des_debug) {
            /* for debugging */
            printf("/* DEBUG:\n");
            printf(" * after shifts, iteration = %d  shifts = %d",
                    iter, shift[iter]);
            printf("\nC array");
            for (i = 1; i <=4 ; i++) {
                printf("\n");
                for (j = 1; j<=7; j++)
                    printf("%d, ",C[(i-1)*7+j]);
            }
            printf("\n\nD array");
            for (i = 1; i <=4 ; i++) {
                printf("\n");
                for (j = 1; j<=7; j++)
                    printf("%d, ",D[(i-1)*7+j]);
            }
            printf("\n */");
            fflush(stdout);
        }

        /*
         * apply pc_2
         * Start at pc_2[1], not pc_2[0]
         *
         * Start stuffing ks_perm[1][1], not ks_perm[0][0]
         *
         * Adjust ks_perm for bit order if needed.
         */
        for (i = 1; i <= 48; i++) {
            if (pc_2[i] <= 28)
                ks_perm[iter][(i)] = C[pc_2[i]];
            else
                ks_perm[iter][(i)] = D[pc_2[i]-28];
        }

        /* now output the resulting key permutation */
        fprintf(stream, "    /* ks permutation iteration = %2d */",
                iter);
        for (i = 1; i <= 6; i++) {
            fprintf(stream, "\n    ");
            for (j = 1; j <= 8; j++) {
                /*
                 * IMPORTANT -- subtract one from value to adjust to a
                 * zero-based subscript for key
                 */
                fprintf(stream, "%d", ks_perm[iter][(i-1)*8+j]-1);
                /* omit last comma */
                if ((j != 8) || (i != 6) || (iter != 16)) {
                    fprintf(stream,", ");
                }
            }
        }
    }
    fprintf(stream,"\n};\n");
}