mknewpc2.c (6191B)
1 /* 2 * mknewpc2.c 3 * 4 * Generate PC-2 tables for DES-150 library 5 * 6 * This Source Code Form is subject to the terms of the Mozilla Public 7 * License, v. 2.0. If a copy of the MPL was not distributed with this 8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 9 10 typedef unsigned char BYTE; 11 typedef unsigned int HALF; 12 13 #define DES_ENCRYPT 0 14 #define DES_DECRYPT 1 15 16 /* two 28-bit registers defined in key schedule production process */ 17 static HALF C0, D0; 18 19 static HALF L0, R0; 20 21 /* key schedule, 16 internal keys, each with 8 6-bit parts */ 22 static BYTE KS[8][16]; 23 24 /* 25 * This table takes the 56 bits in C0 and D0 and shows show they are 26 * permuted into the 8 6-bit parts of the key in the key schedule. 27 * The bits of C0 are numbered left to right, 1-28. 28 * The bits of D0 are numbered left to right, 29-56. 29 * Zeros in this table represent bits that are always zero. 30 * Note that all the bits in the first 4 rows come from C0, 31 * and all the bits in the second 4 rows come from D0. 32 */ 33 static const BYTE PC2[64] = { 34 14, 17, 11, 24, 1, 5, 0, 0, /* S1 */ 35 3, 28, 15, 6, 21, 10, 0, 0, /* S2 */ 36 23, 19, 12, 4, 26, 8, 0, 0, /* S3 */ 37 16, 7, 27, 20, 13, 2, 0, 0, /* S4 */ 38 39 41, 52, 31, 37, 47, 55, 0, 0, /* S5 */ 40 30, 40, 51, 45, 33, 48, 0, 0, /* S6 */ 41 44, 49, 39, 56, 34, 53, 0, 0, /* S7 */ 42 46, 42, 50, 36, 29, 32, 0, 0 /* S8 */ 43 }; 44 45 /* This table represents the same info as PC2, except that 46 * The bits of C0 and D0 are each numbered right to left, 0-27. 47 * -1 values indicate bits that are always zero. 48 * As before all the bits in the first 4 rows come from C0, 49 * and all the bits in the second 4 rows come from D0. 50 */ 51 static signed char PC2a[64] = { 52 /* bits of C0 */ 53 14, 11, 17, 4, 27, 23, -1, -1, /* S1 */ 54 25, 0, 13, 22, 7, 18, -1, -1, /* S2 */ 55 5, 9, 16, 24, 2, 20, -1, -1, /* S3 */ 56 12, 21, 1, 8, 15, 26, -1, -1, /* S4 */ 57 /* bits of D0 */ 58 15, 4, 25, 19, 9, 1, -1, -1, /* S5 */ 59 26, 16, 5, 11, 23, 8, -1, -1, /* S6 */ 60 12, 7, 17, 0, 22, 3, -1, -1, /* S7 */ 61 10, 14, 6, 20, 27, 24, -1, -1 /* S8 */ 62 }; 63 64 /* This table represents the same info as PC2a, except that 65 * The order of of the rows has been changed to increase the efficiency 66 * with which the key sechedule is created. 67 * Fewer shifts and ANDs are required to make the KS from these. 68 */ 69 static const signed char PC2b[64] = { 70 /* bits of C0 */ 71 14, 11, 17, 4, 27, 23, -1, -1, /* S1 */ 72 5, 9, 16, 24, 2, 20, -1, -1, /* S3 */ 73 25, 0, 13, 22, 7, 18, -1, -1, /* S2 */ 74 12, 21, 1, 8, 15, 26, -1, -1, /* S4 */ 75 /* bits of D0 */ 76 26, 16, 5, 11, 23, 8, -1, -1, /* S6 */ 77 10, 14, 6, 20, 27, 24, -1, -1, /* S8 */ 78 15, 4, 25, 19, 9, 1, -1, -1, /* S5 */ 79 12, 7, 17, 0, 22, 3, -1, -1 /* S7 */ 80 }; 81 82 /* Only 24 of the 28 bits in C0 and D0 are used in PC2. 83 * The used bits of C0 and D0 are grouped into 4 groups of 6, 84 * so that the PC2 permutation can be accomplished with 4 lookups 85 * in tables of 64 entries. 86 * The following table shows how the bits of C0 and D0 are grouped 87 * into indexes for the respective table lookups. 88 * Bits are numbered right-to-left, 0-27, as in PC2b. 89 */ 90 static BYTE NDX[48] = { 91 /* Bits of C0 */ 92 27, 26, 25, 24, 23, 22, /* C0 table 0 */ 93 18, 17, 16, 15, 14, 13, /* C0 table 1 */ 94 9, 8, 7, 2, 1, 0, /* C0 table 2 */ 95 5, 4, 21, 20, 12, 11, /* C0 table 3 */ 96 /* bits of D0 */ 97 27, 26, 25, 24, 23, 22, /* D0 table 0 */ 98 20, 19, 17, 16, 15, 14, /* D0 table 1 */ 99 12, 11, 10, 9, 8, 7, /* D0 table 2 */ 100 6, 5, 4, 3, 1, 0 /* D0 table 3 */ 101 }; 102 103 /* Here's the code that does that grouping. 104 left = PC2LOOKUP(0, 0, ((c0 >> 22) & 0x3F) ); 105 left |= PC2LOOKUP(0, 1, ((c0 >> 13) & 0x3F) ); 106 left |= PC2LOOKUP(0, 2, ((c0 >> 4) & 0x38) | (c0 & 0x7) ); 107 left |= PC2LOOKUP(0, 3, ((c0>>18)&0xC) | ((c0>>11)&0x3) | (c0&0x30)); 108 109 right = PC2LOOKUP(1, 0, ((d0 >> 22) & 0x3F) ); 110 right |= PC2LOOKUP(1, 1, ((d0 >> 15) & 0x30) | ((d0 >> 14) & 0xf) ); 111 right |= PC2LOOKUP(1, 2, ((d0 >> 7) & 0x3F) ); 112 right |= PC2LOOKUP(1, 3, ((d0 >> 1) & 0x3C) | (d0 & 0x3)); 113 */ 114 115 void 116 make_pc2a(void) 117 { 118 119 int i, j; 120 121 for (i = 0; i < 64; ++i) { 122 j = PC2[i]; 123 if (j == 0) 124 j = -1; 125 else if (j < 29) 126 j = 28 - j; 127 else 128 j = 56 - j; 129 PC2a[i] = j; 130 } 131 for (i = 0; i < 64; i += 8) { 132 printf("%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,\n", 133 PC2a[i + 0], PC2a[i + 1], PC2a[i + 2], PC2a[i + 3], 134 PC2a[i + 4], PC2a[i + 5], PC2a[i + 6], PC2a[i + 7]); 135 } 136 } 137 138 HALF PC2cd0[64]; 139 140 HALF PC_2H[8][64]; 141 142 void 143 mktable() 144 { 145 int i; 146 int table; 147 const BYTE* ndx = NDX; 148 HALF mask; 149 150 mask = 0x80000000; 151 for (i = 0; i < 32; ++i, mask >>= 1) { 152 int bit = PC2b[i]; 153 if (bit < 0) 154 continue; 155 PC2cd0[bit + 32] = mask; 156 } 157 158 mask = 0x80000000; 159 for (i = 32; i < 64; ++i, mask >>= 1) { 160 int bit = PC2b[i]; 161 if (bit < 0) 162 continue; 163 PC2cd0[bit] = mask; 164 } 165 166 #if DEBUG 167 for (i = 0; i < 64; ++i) { 168 printf("0x%08x,\n", PC2cd0[i]); 169 } 170 #endif 171 for (i = 0; i < 24; ++i) { 172 NDX[i] += 32; /* because c0 is the upper half */ 173 } 174 175 for (table = 0; table < 8; ++table) { 176 HALF bitvals[6]; 177 for (i = 0; i < 6; ++i) { 178 bitvals[5 - i] = PC2cd0[*ndx++]; 179 } 180 for (i = 0; i < 64; ++i) { 181 int j; 182 int k = 0; 183 HALF value = 0; 184 185 for (j = i; j; j >>= 1, ++k) { 186 if (j & 1) { 187 value |= bitvals[k]; 188 } 189 } 190 PC_2H[table][i] = value; 191 } 192 printf("/* table %d */ {\n", table); 193 for (i = 0; i < 64; i += 4) { 194 printf(" 0x%08x, 0x%08x, 0x%08x, 0x%08x, \n", 195 PC_2H[table][i], PC_2H[table][i + 1], 196 PC_2H[table][i + 2], PC_2H[table][i + 3]); 197 } 198 printf(" },\n"); 199 } 200 } 201 202 int 203 main(void) 204 { 205 /* make_pc2a(); */ 206 mktable(); 207 return 0; 208 }