Remove the RCSID macro
[openafs.git] / src / des / make_keyperm.c
1 /*
2  * Copyright 1988 by the Massachusetts Institute of Technology.
3  *
4  * For copying and distribution information, please see the file
5  * <mit-cpyright.h>.
6  *
7  * This routine calculates an effective Key schedule set of
8  * permutations for des.  Beginning with the pre-defined key schedule
9  * algorithm, it reduces it to a set of 16 permutations upon the
10  * initial key.  Only needs to execute once to produce a header file.
11  * Note that we subtract one from the values ouput to fix up for C
12  * subscripts starting at 0.
13  */
14
15 #include <afsconfig.h>
16 #include <afs/param.h>
17
18
19 #include <mit-cpyright.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <des.h>
23 #include "des_internal.h"
24 #include "des_prototypes.h"
25
26 char *progname;
27
28 static int key_position[64 + 1];
29 static int C[28 + 1];
30 static int D[28 + 1];
31 static int C_temp, D_temp;
32
33 /*
34  *  CONVENTIONS for numbering the bits
35  *  bit 0 ==> lsb
36  *  L starts at bit 0
37  *  R starts at bit 64
38  *
39  *  BEWARE-- some stuff starts at 0, some at 1;  perhaps some bugs still?
40  */
41
42 /*
43  * Sequence of shifts used for the key schedule.
44  */
45 static int const shift[16 + 1] = { 0,
46     1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,
47 };
48
49 static int const pc_1[56 + 1] = { 0,
50
51     57, 49, 41, 33, 25, 17, 9,
52     1, 58, 50, 42, 34, 26, 18,
53     10, 2, 59, 51, 43, 35, 27,
54     19, 11, 3, 60, 52, 44, 36,
55
56     63, 55, 47, 39, 31, 23, 15,
57     7, 62, 54, 46, 38, 30, 22,
58     14, 6, 61, 53, 45, 37, 29,
59     21, 13, 5, 28, 20, 12, 4,
60 };
61
62
63 /*
64  * Permuted-choice 2, to pick out the bits from
65  * the CD array that generate the key schedule.
66  */
67 static int const pc_2[48 + 1] = { 0,
68
69     14, 17, 11, 24, 1, 5,
70     3, 28, 15, 6, 21, 10,
71     23, 19, 12, 4, 26, 8,
72     16, 7, 27, 20, 13, 2,
73
74     41, 52, 31, 37, 47, 55,
75     30, 40, 51, 45, 33, 48,
76     44, 49, 39, 56, 34, 53,
77     46, 42, 50, 36, 29, 32,
78 };
79
80 static int ks_perm[16 + 1][48 + 1];
81
82 int des_debug;
83
84 void
85 gen(FILE * stream)
86 {
87     /*  Local Declarations */
88     register int i, j, iter;
89
90     /*
91      * initialize the key_position array s.t. key_position[i] = i;
92      * that is, each element is equal to its starting position.
93      *
94      * Also adjust for the bit order within bytes.
95      */
96
97     for (i = 0; i < 65; i++)
98         key_position[i] = swap_bit_pos_1(i);
99
100     fprintf(stream, "static int const key_perm[16][48] = {\n");
101
102     /*
103      * apply pc_1 to initial key_position to create C[0] and D[0]
104      * Start at pc_1[1], not pc_1[0]
105      */
106     for (i = 1; i <= 28; i++) {
107         C[i] = key_position[pc_1[i]];
108         D[i] = key_position[pc_1[i + 28]];
109     }
110
111     /*
112      * major loop over the 16 iterations
113      * start at iter = 1, not zero.
114      */
115     for (iter = 1; iter <= 16; iter++) {
116         if (des_debug) {
117             /*  for debugging */
118             printf("/* DEBUG-- start iteration = %d  shifts = %d", iter,
119                    shift[iter]);
120             printf("\nC array");
121             for (i = 1; i <= 4; i++) {
122                 printf("\n");
123                 for (j = 1; j <= 7; j++)
124                     printf("%d, ", C[(i - 1) * 7 + j]);
125             }
126             printf("\n\nD array");
127             for (i = 1; i <= 4; i++) {
128                 printf("\n");
129                 for (j = 1; j <= 7; j++)
130                     printf("%d, ", D[(i - 1) * 7 + j]);
131             }
132             printf("\n */");
133             fflush(stdout);
134         }
135
136         /* apply the appropriate left shifts */
137         for (i = 1; i <= shift[iter]; i++) {
138             C_temp = C[1];
139             D_temp = D[1];
140             for (j = 1; j <= 27; j++) {
141                 C[j] = C[j + 1];
142                 D[j] = D[j + 1];
143             }
144             C[j] = C_temp;
145             D[j] = D_temp;
146         }
147
148
149         if (des_debug) {
150             /* for debugging */
151             printf("/* DEBUG:\n");
152             printf(" * after shifts, iteration = %d  shifts = %d", iter,
153                    shift[iter]);
154             printf("\nC array");
155             for (i = 1; i <= 4; i++) {
156                 printf("\n");
157                 for (j = 1; j <= 7; j++)
158                     printf("%d, ", C[(i - 1) * 7 + j]);
159             }
160             printf("\n\nD array");
161             for (i = 1; i <= 4; i++) {
162                 printf("\n");
163                 for (j = 1; j <= 7; j++)
164                     printf("%d, ", D[(i - 1) * 7 + j]);
165             }
166             printf("\n */");
167             fflush(stdout);
168         }
169
170         /*
171          * apply pc_2
172          * Start at pc_2[1], not pc_2[0]
173          *
174          * Start stuffing ks_perm[1][1], not ks_perm[0][0]
175          *
176          * Adjust ks_perm for bit order if needed.
177          */
178         for (i = 1; i <= 48; i++) {
179             if (pc_2[i] <= 28)
180                 ks_perm[iter][(i)] = C[pc_2[i]];
181             else
182                 ks_perm[iter][(i)] = D[pc_2[i] - 28];
183         }
184
185         /* now output the resulting key permutation */
186         fprintf(stream, "\n    /* ks permutation iteration = %2d */", iter);
187         for (i = 1; i <= 6; i++) {
188             if (i == 1)
189                 fprintf(stream, "\n    {");
190             fprintf(stream, "\n    ");
191             for (j = 1; j <= 8; j++) {
192                 /*
193                  * IMPORTANT -- subtract one from value to adjust to a
194                  * zero-based subscript for key
195                  */
196                 fprintf(stream, "%d", ks_perm[iter][(i - 1) * 8 + j] - 1);
197                 /* omit last comma */
198                 if ((j != 8) || (i != 6)) {
199                     fprintf(stream, ", ");
200                 }
201             }
202         }
203         if (iter != 16) {
204             fprintf(stream, "\n    }, ");
205         } else {
206             fprintf(stream, "\n    }");
207         }
208     }
209     fprintf(stream, "\n};\n");
210 }