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