2 * Copyright 1988 by the Massachusetts Institute of Technology.
4 * For copying and distribution information, please see the file
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.
15 #include <afsconfig.h>
16 #include <afs/param.h>
21 #include <mit-cpyright.h>
25 #include "des_internal.h"
26 #include "des_prototypes.h"
30 static int key_position[64 + 1];
33 static int C_temp, D_temp;
36 * CONVENTIONS for numbering the bits
41 * BEWARE-- some stuff starts at 0, some at 1; perhaps some bugs still?
45 * Sequence of shifts used for the key schedule.
47 static int const shift[16 + 1] = { 0,
48 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,
51 static int const pc_1[64 + 1] = { 0,
53 57, 49, 41, 33, 25, 17, 9,
54 1, 58, 50, 42, 34, 26, 18,
55 10, 2, 59, 51, 43, 35, 27,
56 19, 11, 3, 60, 52, 44, 36,
58 63, 55, 47, 39, 31, 23, 15,
59 7, 62, 54, 46, 38, 30, 22,
60 14, 6, 61, 53, 45, 37, 29,
61 21, 13, 5, 28, 20, 12, 4,
66 * Permuted-choice 2, to pick out the bits from
67 * the CD array that generate the key schedule.
69 static int const pc_2[48 + 1] = { 0,
76 41, 52, 31, 37, 47, 55,
77 30, 40, 51, 45, 33, 48,
78 44, 49, 39, 56, 34, 53,
79 46, 42, 50, 36, 29, 32,
82 static int ks_perm[16 + 1][48 + 1];
89 /* Local Declarations */
90 register int i, j, iter;
93 * initialize the key_position array s.t. key_position[i] = i;
94 * that is, each element is equal to its starting position.
96 * Also adjust for the bit order within bytes.
99 for (i = 0; i < 65; i++)
100 key_position[i] = swap_bit_pos_1(i);
102 fprintf(stream, "static int const key_perm[16][48] = {\n");
105 * apply pc_1 to initial key_position to create C[0] and D[0]
106 * Start at pc_1[1], not pc_1[0]
108 for (i = 1; i <= 28; i++) {
109 C[i] = key_position[pc_1[i]];
110 D[i] = key_position[pc_1[i + 28]];
114 * major loop over the 16 iterations
115 * start at iter = 1, not zero.
117 for (iter = 1; iter <= 16; iter++) {
120 printf("/* DEBUG-- start iteration = %d shifts = %d", iter,
123 for (i = 1; i <= 4; i++) {
125 for (j = 1; j <= 7; j++)
126 printf("%d, ", C[(i - 1) * 7 + j]);
128 printf("\n\nD array");
129 for (i = 1; i <= 4; i++) {
131 for (j = 1; j <= 7; j++)
132 printf("%d, ", D[(i - 1) * 7 + j]);
138 /* apply the appropriate left shifts */
139 for (i = 1; i <= shift[iter]; i++) {
142 for (j = 1; j <= 27; j++) {
153 printf("/* DEBUG:\n");
154 printf(" * after shifts, iteration = %d shifts = %d", iter,
157 for (i = 1; i <= 4; i++) {
159 for (j = 1; j <= 7; j++)
160 printf("%d, ", C[(i - 1) * 7 + j]);
162 printf("\n\nD array");
163 for (i = 1; i <= 4; i++) {
165 for (j = 1; j <= 7; j++)
166 printf("%d, ", D[(i - 1) * 7 + j]);
174 * Start at pc_2[1], not pc_2[0]
176 * Start stuffing ks_perm[1][1], not ks_perm[0][0]
178 * Adjust ks_perm for bit order if needed.
180 for (i = 1; i <= 48; i++) {
182 ks_perm[iter][(i)] = C[pc_2[i]];
184 ks_perm[iter][(i)] = D[pc_2[i] - 28];
187 /* now output the resulting key permutation */
188 fprintf(stream, "\n /* ks permutation iteration = %2d */", iter);
189 for (i = 1; i <= 6; i++) {
191 fprintf(stream, "\n {");
192 fprintf(stream, "\n ");
193 for (j = 1; j <= 8; j++) {
195 * IMPORTANT -- subtract one from value to adjust to a
196 * zero-based subscript for key
198 fprintf(stream, "%d", ks_perm[iter][(i - 1) * 8 + j] - 1);
199 /* omit last comma */
200 if ((j != 8) || (i != 6)) {
201 fprintf(stream, ", ");
206 fprintf(stream, "\n }, ");
208 fprintf(stream, "\n }");
211 fprintf(stream, "\n};\n");