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 <mit-cpyright.h>
18 #include "des_internal.h"
21 extern char *errmsg();
22 extern afs_int32 swap_bit_pos_1();
23 extern afs_int32 swap_bit_pos_0();
30 int key_position[64+1];
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 int const shift[16+1] = { 0,
48 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1,
51 int const pc_1[64+1] = { 0,
66 * Permuted-choice 2, to pick out the bits from
67 * the CD array that generate the key schedule.
69 int const pc_2[48+1] = { 0,
82 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.
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++) {
121 "/* DEBUG-- start iteration = %d shifts = %d",
124 for (i = 1; i <=4 ; i++) {
126 for (j = 1; j<=7; j++)
127 printf("%d, ",C[(i-1)*7+j]);
129 printf("\n\nD array");
130 for (i = 1; i <=4 ; i++) {
132 for (j = 1; j<=7; j++)
133 printf("%d, ",D[(i-1)*7+j]);
139 /* apply the appropriate left shifts */
140 for (i = 1; i <= shift[iter]; i++) {
143 for (j =1; j<=27; j++) {
154 printf("/* DEBUG:\n");
155 printf(" * after shifts, iteration = %d shifts = %d",
158 for (i = 1; i <=4 ; i++) {
160 for (j = 1; j<=7; j++)
161 printf("%d, ",C[(i-1)*7+j]);
163 printf("\n\nD array");
164 for (i = 1; i <=4 ; i++) {
166 for (j = 1; j<=7; j++)
167 printf("%d, ",D[(i-1)*7+j]);
175 * Start at pc_2[1], not pc_2[0]
177 * Start stuffing ks_perm[1][1], not ks_perm[0][0]
179 * Adjust ks_perm for bit order if needed.
181 for (i = 1; i <= 48; i++) {
183 ks_perm[iter][(i)] = C[pc_2[i]];
185 ks_perm[iter][(i)] = D[pc_2[i]-28];
188 /* now output the resulting key permutation */
189 fprintf(stream, " /* ks permutation iteration = %2d */",
191 for (i = 1; i <= 6; i++) {
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) || (iter != 16)) {
201 fprintf(stream,", ");
206 fprintf(stream,"\n};\n");