xref: /freebsd/crypto/krb5/src/lib/crypto/builtin/des/f_tables.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/builtin/des/f_tables.c */
3 /*
4  * Copyright (C) 1990 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 /* DES implementation donated by Dennis Ferguson */
28 
29 /*
30  * des_tables.c - precomputed tables used for the DES cipher function
31  */
32 
33 /*
34  * Include the header file so something will complain if the
35  * declarations get out of sync
36  */
37 #include "crypto_int.h"
38 #include "des_int.h"
39 #include "f_tables.h"
40 
41 #ifdef K5_BUILTIN_DES
42 
43 /*
44  * These tables may be declared const if you want.  Many compilers
45  * don't support this, though.
46  */
47 
48 /*
49  * The DES algorithm which uses these is intended to be fairly speedy
50  * at the expense of some memory.  All the standard hacks are used.
51  * The S boxes and the P permutation are precomputed into one table.
52  * The E box never actually appears explicitly since it is easy to apply
53  * this algorithmically as needed.  The initial permutation and final
54  * (inverse initial) permutation are computed from tables designed to
55  * permute one byte at a time.  This should run pretty fast on machines
56  * with 32 bit words and bit field/multiple bit shift instructions which
57  * are fast.
58  */
59 
60 /*
61  * The initial permutation array.  This is used to compute both the
62  * left and the right halves of the initial permutation using bytes
63  * from words made from the following operations:
64  *
65  * ((left & 0x55555555) << 1) | (right & 0x55555555)  for left half
66  * (left & 0xaaaaaaaa) | ((right & 0xaaaaaaaa) >> 1)  for right half
67  *
68  * The scheme is that we index into the table using each byte.  The
69  * result from the high order byte is or'd with the result from the
70  * next byte shifted left once is or'd with the result from the next
71  * byte shifted left twice if or'd with the result from the low order
72  * byte shifted left by three.  Clear?
73  */
74 
75 const unsigned DES_INT32 des_IP_table[256] = {
76     0x00000000, 0x00000010, 0x00000001, 0x00000011,
77     0x00001000, 0x00001010, 0x00001001, 0x00001011,
78     0x00000100, 0x00000110, 0x00000101, 0x00000111,
79     0x00001100, 0x00001110, 0x00001101, 0x00001111,
80     0x00100000, 0x00100010, 0x00100001, 0x00100011,
81     0x00101000, 0x00101010, 0x00101001, 0x00101011,
82     0x00100100, 0x00100110, 0x00100101, 0x00100111,
83     0x00101100, 0x00101110, 0x00101101, 0x00101111,
84     0x00010000, 0x00010010, 0x00010001, 0x00010011,
85     0x00011000, 0x00011010, 0x00011001, 0x00011011,
86     0x00010100, 0x00010110, 0x00010101, 0x00010111,
87     0x00011100, 0x00011110, 0x00011101, 0x00011111,
88     0x00110000, 0x00110010, 0x00110001, 0x00110011,
89     0x00111000, 0x00111010, 0x00111001, 0x00111011,
90     0x00110100, 0x00110110, 0x00110101, 0x00110111,
91     0x00111100, 0x00111110, 0x00111101, 0x00111111,
92     0x10000000, 0x10000010, 0x10000001, 0x10000011,
93     0x10001000, 0x10001010, 0x10001001, 0x10001011,
94     0x10000100, 0x10000110, 0x10000101, 0x10000111,
95     0x10001100, 0x10001110, 0x10001101, 0x10001111,
96     0x10100000, 0x10100010, 0x10100001, 0x10100011,
97     0x10101000, 0x10101010, 0x10101001, 0x10101011,
98     0x10100100, 0x10100110, 0x10100101, 0x10100111,
99     0x10101100, 0x10101110, 0x10101101, 0x10101111,
100     0x10010000, 0x10010010, 0x10010001, 0x10010011,
101     0x10011000, 0x10011010, 0x10011001, 0x10011011,
102     0x10010100, 0x10010110, 0x10010101, 0x10010111,
103     0x10011100, 0x10011110, 0x10011101, 0x10011111,
104     0x10110000, 0x10110010, 0x10110001, 0x10110011,
105     0x10111000, 0x10111010, 0x10111001, 0x10111011,
106     0x10110100, 0x10110110, 0x10110101, 0x10110111,
107     0x10111100, 0x10111110, 0x10111101, 0x10111111,
108     0x01000000, 0x01000010, 0x01000001, 0x01000011,
109     0x01001000, 0x01001010, 0x01001001, 0x01001011,
110     0x01000100, 0x01000110, 0x01000101, 0x01000111,
111     0x01001100, 0x01001110, 0x01001101, 0x01001111,
112     0x01100000, 0x01100010, 0x01100001, 0x01100011,
113     0x01101000, 0x01101010, 0x01101001, 0x01101011,
114     0x01100100, 0x01100110, 0x01100101, 0x01100111,
115     0x01101100, 0x01101110, 0x01101101, 0x01101111,
116     0x01010000, 0x01010010, 0x01010001, 0x01010011,
117     0x01011000, 0x01011010, 0x01011001, 0x01011011,
118     0x01010100, 0x01010110, 0x01010101, 0x01010111,
119     0x01011100, 0x01011110, 0x01011101, 0x01011111,
120     0x01110000, 0x01110010, 0x01110001, 0x01110011,
121     0x01111000, 0x01111010, 0x01111001, 0x01111011,
122     0x01110100, 0x01110110, 0x01110101, 0x01110111,
123     0x01111100, 0x01111110, 0x01111101, 0x01111111,
124     0x11000000, 0x11000010, 0x11000001, 0x11000011,
125     0x11001000, 0x11001010, 0x11001001, 0x11001011,
126     0x11000100, 0x11000110, 0x11000101, 0x11000111,
127     0x11001100, 0x11001110, 0x11001101, 0x11001111,
128     0x11100000, 0x11100010, 0x11100001, 0x11100011,
129     0x11101000, 0x11101010, 0x11101001, 0x11101011,
130     0x11100100, 0x11100110, 0x11100101, 0x11100111,
131     0x11101100, 0x11101110, 0x11101101, 0x11101111,
132     0x11010000, 0x11010010, 0x11010001, 0x11010011,
133     0x11011000, 0x11011010, 0x11011001, 0x11011011,
134     0x11010100, 0x11010110, 0x11010101, 0x11010111,
135     0x11011100, 0x11011110, 0x11011101, 0x11011111,
136     0x11110000, 0x11110010, 0x11110001, 0x11110011,
137     0x11111000, 0x11111010, 0x11111001, 0x11111011,
138     0x11110100, 0x11110110, 0x11110101, 0x11110111,
139     0x11111100, 0x11111110, 0x11111101, 0x11111111
140 };
141 
142 /*
143  * The final permutation array.  Like the IP array, used
144  * to compute both the left and right results from the bytes
145  * of words computed from:
146  *
147  * ((left & 0x0f0f0f0f) << 4) | (right & 0x0f0f0f0f)  for left result
148  * (left & 0xf0f0f0f0) | ((right & 0xf0f0f0f0) >> 4)  for right result
149  *
150  * The result from the high order byte is shifted left 6 bits and
151  * or'd with the result from the next byte shifted left 4 bits, which
152  * is or'd with the result from the next byte shifted left 2 bits,
153  * which is or'd with the result from the low byte.
154  */
155 const unsigned DES_INT32 des_FP_table[256] = {
156     0x00000000, 0x02000000, 0x00020000, 0x02020000,
157     0x00000200, 0x02000200, 0x00020200, 0x02020200,
158     0x00000002, 0x02000002, 0x00020002, 0x02020002,
159     0x00000202, 0x02000202, 0x00020202, 0x02020202,
160     0x01000000, 0x03000000, 0x01020000, 0x03020000,
161     0x01000200, 0x03000200, 0x01020200, 0x03020200,
162     0x01000002, 0x03000002, 0x01020002, 0x03020002,
163     0x01000202, 0x03000202, 0x01020202, 0x03020202,
164     0x00010000, 0x02010000, 0x00030000, 0x02030000,
165     0x00010200, 0x02010200, 0x00030200, 0x02030200,
166     0x00010002, 0x02010002, 0x00030002, 0x02030002,
167     0x00010202, 0x02010202, 0x00030202, 0x02030202,
168     0x01010000, 0x03010000, 0x01030000, 0x03030000,
169     0x01010200, 0x03010200, 0x01030200, 0x03030200,
170     0x01010002, 0x03010002, 0x01030002, 0x03030002,
171     0x01010202, 0x03010202, 0x01030202, 0x03030202,
172     0x00000100, 0x02000100, 0x00020100, 0x02020100,
173     0x00000300, 0x02000300, 0x00020300, 0x02020300,
174     0x00000102, 0x02000102, 0x00020102, 0x02020102,
175     0x00000302, 0x02000302, 0x00020302, 0x02020302,
176     0x01000100, 0x03000100, 0x01020100, 0x03020100,
177     0x01000300, 0x03000300, 0x01020300, 0x03020300,
178     0x01000102, 0x03000102, 0x01020102, 0x03020102,
179     0x01000302, 0x03000302, 0x01020302, 0x03020302,
180     0x00010100, 0x02010100, 0x00030100, 0x02030100,
181     0x00010300, 0x02010300, 0x00030300, 0x02030300,
182     0x00010102, 0x02010102, 0x00030102, 0x02030102,
183     0x00010302, 0x02010302, 0x00030302, 0x02030302,
184     0x01010100, 0x03010100, 0x01030100, 0x03030100,
185     0x01010300, 0x03010300, 0x01030300, 0x03030300,
186     0x01010102, 0x03010102, 0x01030102, 0x03030102,
187     0x01010302, 0x03010302, 0x01030302, 0x03030302,
188     0x00000001, 0x02000001, 0x00020001, 0x02020001,
189     0x00000201, 0x02000201, 0x00020201, 0x02020201,
190     0x00000003, 0x02000003, 0x00020003, 0x02020003,
191     0x00000203, 0x02000203, 0x00020203, 0x02020203,
192     0x01000001, 0x03000001, 0x01020001, 0x03020001,
193     0x01000201, 0x03000201, 0x01020201, 0x03020201,
194     0x01000003, 0x03000003, 0x01020003, 0x03020003,
195     0x01000203, 0x03000203, 0x01020203, 0x03020203,
196     0x00010001, 0x02010001, 0x00030001, 0x02030001,
197     0x00010201, 0x02010201, 0x00030201, 0x02030201,
198     0x00010003, 0x02010003, 0x00030003, 0x02030003,
199     0x00010203, 0x02010203, 0x00030203, 0x02030203,
200     0x01010001, 0x03010001, 0x01030001, 0x03030001,
201     0x01010201, 0x03010201, 0x01030201, 0x03030201,
202     0x01010003, 0x03010003, 0x01030003, 0x03030003,
203     0x01010203, 0x03010203, 0x01030203, 0x03030203,
204     0x00000101, 0x02000101, 0x00020101, 0x02020101,
205     0x00000301, 0x02000301, 0x00020301, 0x02020301,
206     0x00000103, 0x02000103, 0x00020103, 0x02020103,
207     0x00000303, 0x02000303, 0x00020303, 0x02020303,
208     0x01000101, 0x03000101, 0x01020101, 0x03020101,
209     0x01000301, 0x03000301, 0x01020301, 0x03020301,
210     0x01000103, 0x03000103, 0x01020103, 0x03020103,
211     0x01000303, 0x03000303, 0x01020303, 0x03020303,
212     0x00010101, 0x02010101, 0x00030101, 0x02030101,
213     0x00010301, 0x02010301, 0x00030301, 0x02030301,
214     0x00010103, 0x02010103, 0x00030103, 0x02030103,
215     0x00010303, 0x02010303, 0x00030303, 0x02030303,
216     0x01010101, 0x03010101, 0x01030101, 0x03030101,
217     0x01010301, 0x03010301, 0x01030301, 0x03030301,
218     0x01010103, 0x03010103, 0x01030103, 0x03030103,
219     0x01010303, 0x03010303, 0x01030303, 0x03030303
220 };
221 
222 
223 /*
224  * The SP table is actually the S boxes and the P permutation
225  * table combined.  This table is actually reordered from the
226  * spec, to match the order of key application we follow.
227  */
228 const unsigned DES_INT32 des_SP_table[8][64] = {
229     {
230         0x00100000, 0x02100001, 0x02000401, 0x00000000, /* 7 */
231         0x00000400, 0x02000401, 0x00100401, 0x02100400,
232         0x02100401, 0x00100000, 0x00000000, 0x02000001,
233         0x00000001, 0x02000000, 0x02100001, 0x00000401,
234         0x02000400, 0x00100401, 0x00100001, 0x02000400,
235         0x02000001, 0x02100000, 0x02100400, 0x00100001,
236         0x02100000, 0x00000400, 0x00000401, 0x02100401,
237         0x00100400, 0x00000001, 0x02000000, 0x00100400,
238         0x02000000, 0x00100400, 0x00100000, 0x02000401,
239         0x02000401, 0x02100001, 0x02100001, 0x00000001,
240         0x00100001, 0x02000000, 0x02000400, 0x00100000,
241         0x02100400, 0x00000401, 0x00100401, 0x02100400,
242         0x00000401, 0x02000001, 0x02100401, 0x02100000,
243         0x00100400, 0x00000000, 0x00000001, 0x02100401,
244         0x00000000, 0x00100401, 0x02100000, 0x00000400,
245         0x02000001, 0x02000400, 0x00000400, 0x00100001,
246     },
247     {
248         0x00808200, 0x00000000, 0x00008000, 0x00808202, /* 1 */
249         0x00808002, 0x00008202, 0x00000002, 0x00008000,
250         0x00000200, 0x00808200, 0x00808202, 0x00000200,
251         0x00800202, 0x00808002, 0x00800000, 0x00000002,
252         0x00000202, 0x00800200, 0x00800200, 0x00008200,
253         0x00008200, 0x00808000, 0x00808000, 0x00800202,
254         0x00008002, 0x00800002, 0x00800002, 0x00008002,
255         0x00000000, 0x00000202, 0x00008202, 0x00800000,
256         0x00008000, 0x00808202, 0x00000002, 0x00808000,
257         0x00808200, 0x00800000, 0x00800000, 0x00000200,
258         0x00808002, 0x00008000, 0x00008200, 0x00800002,
259         0x00000200, 0x00000002, 0x00800202, 0x00008202,
260         0x00808202, 0x00008002, 0x00808000, 0x00800202,
261         0x00800002, 0x00000202, 0x00008202, 0x00808200,
262         0x00000202, 0x00800200, 0x00800200, 0x00000000,
263         0x00008002, 0x00008200, 0x00000000, 0x00808002,
264     },
265     {
266         0x00000104, 0x04010100, 0x00000000, 0x04010004, /* 3 */
267         0x04000100, 0x00000000, 0x00010104, 0x04000100,
268         0x00010004, 0x04000004, 0x04000004, 0x00010000,
269         0x04010104, 0x00010004, 0x04010000, 0x00000104,
270         0x04000000, 0x00000004, 0x04010100, 0x00000100,
271         0x00010100, 0x04010000, 0x04010004, 0x00010104,
272         0x04000104, 0x00010100, 0x00010000, 0x04000104,
273         0x00000004, 0x04010104, 0x00000100, 0x04000000,
274         0x04010100, 0x04000000, 0x00010004, 0x00000104,
275         0x00010000, 0x04010100, 0x04000100, 0x00000000,
276         0x00000100, 0x00010004, 0x04010104, 0x04000100,
277         0x04000004, 0x00000100, 0x00000000, 0x04010004,
278         0x04000104, 0x00010000, 0x04000000, 0x04010104,
279         0x00000004, 0x00010104, 0x00010100, 0x04000004,
280         0x04010000, 0x04000104, 0x00000104, 0x04010000,
281         0x00010104, 0x00000004, 0x04010004, 0x00010100,
282     },
283     {
284         0x00000080, 0x01040080, 0x01040000, 0x21000080, /* 5 */
285         0x00040000, 0x00000080, 0x20000000, 0x01040000,
286         0x20040080, 0x00040000, 0x01000080, 0x20040080,
287         0x21000080, 0x21040000, 0x00040080, 0x20000000,
288         0x01000000, 0x20040000, 0x20040000, 0x00000000,
289         0x20000080, 0x21040080, 0x21040080, 0x01000080,
290         0x21040000, 0x20000080, 0x00000000, 0x21000000,
291         0x01040080, 0x01000000, 0x21000000, 0x00040080,
292         0x00040000, 0x21000080, 0x00000080, 0x01000000,
293         0x20000000, 0x01040000, 0x21000080, 0x20040080,
294         0x01000080, 0x20000000, 0x21040000, 0x01040080,
295         0x20040080, 0x00000080, 0x01000000, 0x21040000,
296         0x21040080, 0x00040080, 0x21000000, 0x21040080,
297         0x01040000, 0x00000000, 0x20040000, 0x21000000,
298         0x00040080, 0x01000080, 0x20000080, 0x00040000,
299         0x00000000, 0x20040000, 0x01040080, 0x20000080,
300     },
301     {
302         0x80401000, 0x80001040, 0x80001040, 0x00000040, /* 4 */
303         0x00401040, 0x80400040, 0x80400000, 0x80001000,
304         0x00000000, 0x00401000, 0x00401000, 0x80401040,
305         0x80000040, 0x00000000, 0x00400040, 0x80400000,
306         0x80000000, 0x00001000, 0x00400000, 0x80401000,
307         0x00000040, 0x00400000, 0x80001000, 0x00001040,
308         0x80400040, 0x80000000, 0x00001040, 0x00400040,
309         0x00001000, 0x00401040, 0x80401040, 0x80000040,
310         0x00400040, 0x80400000, 0x00401000, 0x80401040,
311         0x80000040, 0x00000000, 0x00000000, 0x00401000,
312         0x00001040, 0x00400040, 0x80400040, 0x80000000,
313         0x80401000, 0x80001040, 0x80001040, 0x00000040,
314         0x80401040, 0x80000040, 0x80000000, 0x00001000,
315         0x80400000, 0x80001000, 0x00401040, 0x80400040,
316         0x80001000, 0x00001040, 0x00400000, 0x80401000,
317         0x00000040, 0x00400000, 0x00001000, 0x00401040,
318     },
319     {
320         0x10000008, 0x10200000, 0x00002000, 0x10202008, /* 6 */
321         0x10200000, 0x00000008, 0x10202008, 0x00200000,
322         0x10002000, 0x00202008, 0x00200000, 0x10000008,
323         0x00200008, 0x10002000, 0x10000000, 0x00002008,
324         0x00000000, 0x00200008, 0x10002008, 0x00002000,
325         0x00202000, 0x10002008, 0x00000008, 0x10200008,
326         0x10200008, 0x00000000, 0x00202008, 0x10202000,
327         0x00002008, 0x00202000, 0x10202000, 0x10000000,
328         0x10002000, 0x00000008, 0x10200008, 0x00202000,
329         0x10202008, 0x00200000, 0x00002008, 0x10000008,
330         0x00200000, 0x10002000, 0x10000000, 0x00002008,
331         0x10000008, 0x10202008, 0x00202000, 0x10200000,
332         0x00202008, 0x10202000, 0x00000000, 0x10200008,
333         0x00000008, 0x00002000, 0x10200000, 0x00202008,
334         0x00002000, 0x00200008, 0x10002008, 0x00000000,
335         0x10202000, 0x10000000, 0x00200008, 0x10002008,
336     },
337     {
338         0x08000820, 0x00000800, 0x00020000, 0x08020820, /* 8 */
339         0x08000000, 0x08000820, 0x00000020, 0x08000000,
340         0x00020020, 0x08020000, 0x08020820, 0x00020800,
341         0x08020800, 0x00020820, 0x00000800, 0x00000020,
342         0x08020000, 0x08000020, 0x08000800, 0x00000820,
343         0x00020800, 0x00020020, 0x08020020, 0x08020800,
344         0x00000820, 0x00000000, 0x00000000, 0x08020020,
345         0x08000020, 0x08000800, 0x00020820, 0x00020000,
346         0x00020820, 0x00020000, 0x08020800, 0x00000800,
347         0x00000020, 0x08020020, 0x00000800, 0x00020820,
348         0x08000800, 0x00000020, 0x08000020, 0x08020000,
349         0x08020020, 0x08000000, 0x00020000, 0x08000820,
350         0x00000000, 0x08020820, 0x00020020, 0x08000020,
351         0x08020000, 0x08000800, 0x08000820, 0x00000000,
352         0x08020820, 0x00020800, 0x00020800, 0x00000820,
353         0x00000820, 0x00020020, 0x08000000, 0x08020800,
354     },
355     {
356         0x40084010, 0x40004000, 0x00004000, 0x00084010, /* 2 */
357         0x00080000, 0x00000010, 0x40080010, 0x40004010,
358         0x40000010, 0x40084010, 0x40084000, 0x40000000,
359         0x40004000, 0x00080000, 0x00000010, 0x40080010,
360         0x00084000, 0x00080010, 0x40004010, 0x00000000,
361         0x40000000, 0x00004000, 0x00084010, 0x40080000,
362         0x00080010, 0x40000010, 0x00000000, 0x00084000,
363         0x00004010, 0x40084000, 0x40080000, 0x00004010,
364         0x00000000, 0x00084010, 0x40080010, 0x00080000,
365         0x40004010, 0x40080000, 0x40084000, 0x00004000,
366         0x40080000, 0x40004000, 0x00000010, 0x40084010,
367         0x00084010, 0x00000010, 0x00004000, 0x40000000,
368         0x00004010, 0x40084000, 0x00080000, 0x40000010,
369         0x00080010, 0x40004010, 0x40000010, 0x00080010,
370         0x00084000, 0x00000000, 0x40004000, 0x00004010,
371         0x40000000, 0x40080010, 0x40084010, 0x00084000
372     },
373 };
374 
375 #endif /* K5_BUILTIN_DES */
376