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