2 * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION
5 * "Digital Equipment Corporation authorizes the reproduction,
6 * distribution and modification of this software subject to the following
9 * 1. Any partial or whole copy of this software, or any modification
10 * thereof, must include this copyright notice in its entirety.
12 * 2. This software is supplied "as is" with no warranty of any kind,
13 * expressed or implied, for any purpose, including any warranty of fitness
14 * or merchantibility. DIGITAL assumes no responsibility for the use or
15 * reliability of this software, nor promises to provide any form of
16 * support for it on any basis.
18 * 3. Distribution of this software is authorized only if no profit or
19 * remuneration of any kind is received in exchange for such distribution.
21 * 4. This software produces public key authentication certificates
22 * bearing an expiration date established by DIGITAL and RSA Data
23 * Security, Inc. It may cease to generate certificates after the expiration
24 * date. Any modification of this software that changes or defeats
25 * the expiration date or its effect is unauthorized.
27 * 5. Software that will renew or extend the expiration date of
28 * authentication certificates produced by this software may be obtained
29 * from RSA Data Security, Inc., 10 Twin Dolphin Drive, Redwood City, CA
30 * 94065, (415)595-8782, or from DIGITAL"
36 static char copyright[] = "\n Copyright, 1989, 1990, Digital Equipment Corporation \n";
37 static char warning[]= "\n This software is subject to export restrictions under \
38 \n the U.S. Department of State's International Traffic in Arms \
39 \n Regulations (ITAR). This software must not be transmitted \
40 \n in source form outside the United States or to a foreign \
41 \n national in the United States without a valid U.S. State \
42 \n Department export license. ";
46 * Generate the permutation tables for a software implementation of DES.
50 #define FILENAME "tables.h"
51 #define EFILE "endian.h"
53 typedef unsigned long INT32; /* generic type for 32-bit integer */
56 * The following tables are based on those in the Data Encryption
57 * Standard document (FIPS PUB 46).
61 * Initial permutation IP
63 static char ip[] = { 2, 4, 6, 8, 1, 3, 5, 7 };
66 * Final permutation IP^-1
68 static char fp[] = { 8, 7, 6, 5, 4, 3, 2, 1 };
71 * The (in)famous S-boxes
73 static char si[8][64] = {
77 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
78 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
79 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
80 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
85 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
86 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
87 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
88 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
93 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
94 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
95 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
96 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
101 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
102 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
103 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
104 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
109 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
110 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
111 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
112 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
117 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
118 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
119 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
120 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
125 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
126 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
127 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
128 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
133 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
134 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
135 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
136 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
140 * 32-bit permutation function P used on the output of the S-boxes
142 static char p32i[] = {
156 8, 9, 10, 11, 12, 13,
157 12, 13, 14, 15, 16, 17,
158 16, 17, 18, 19, 20, 21,
159 20, 21, 22, 23, 24, 25,
160 24, 25, 26, 27, 28, 29,
161 28, 29, 30, 31, 32, 1
165 * Permuted choice table (key)
167 static char pc1[] = {
168 57, 49, 41, 33, 25, 17, 9,
169 1, 58, 50, 42, 34, 26, 18,
170 10, 2, 59, 51, 43, 35, 27,
171 19, 11, 3, 60, 52, 44, 36,
173 63, 55, 47, 39, 31, 23, 15,
174 7, 62, 54, 46, 38, 30, 22,
175 14, 6, 61, 53, 45, 37, 29,
176 21, 13, 5, 28, 20, 12, 4
180 * Permuted choice key (table)
182 static char pc2[] = {
183 14, 17, 11, 24, 1, 5,
184 3, 28, 15, 6, 21, 10,
185 23, 19, 12, 4, 26, 8,
186 16, 7, 27, 20, 13, 2,
187 41, 52, 31, 37, 47, 55,
188 30, 40, 51, 45, 33, 48,
189 44, 49, 39, 56, 34, 53,
190 46, 42, 50, 36, 29, 32
194 * Note that the Standard is written in Big Endian terms so bit 1 is the
195 * high bit of the byte.
197 static int bytebit[] = {
198 0200, 0100, 040, 020, 010, 04, 02, 01
204 static INT32 iperma[256],ipermb[256];
205 static INT32 fperma[256],fpermb[256];
206 static INT32 sp[8][64];
207 static char pc1bit[56],pc1byte[56];
213 * Initialize a permutation array.
215 static perminit( perma,permb,p )
216 INT32 perma[256],permb[256];
222 * Clear out the permutation array.
224 memset(perma, 0, sizeof(perma));
225 memset(permb, 0, sizeof(permb));
228 * Now produce the table.
230 for (i = 0; i < 4; i++)
231 for (j = 0; j < 256; j++)
233 if ((j & bytebit[p[i] - 1]) != 0)
234 perma[j] |= 1L << (8 * (3 - i));
235 if ((j & bytebit[p[i+4] - 1]) != 0)
236 permb[j] |= 1L << (8 * (3 - i));
240 outperm( perma,permb,name,comment )
241 INT32 perma[256],permb[256];
246 fprintf(tables, "/*\n * %s\n */\n", comment);
247 fprintf(tables, "static INT32 %sa[256] = {", name);
248 for (i = 0; i < 64; i++)
250 fprintf(tables, "\n ");
251 for (j = 0; j < 4; j++)
252 fprintf(tables, "0x%lx, ", (long)perma[(i * 4) + j]);
255 fprintf(tables, "\n};\n\nstatic INT32 %sb[256] = {", name);
256 for (i = 0; i < 64; i++)
258 fprintf(tables, "\n ");
259 for (j = 0; j < 4; j++)
260 fprintf(tables, "0x%lx, ", (long)permb[(i * 4) + j]);
262 fprintf(tables, "\n};\n\n");
266 * Intialize the combined S and P boxes
270 register int i,j,k,l;
272 unsigned char inv[32];
275 * Invert the pbox array.
277 for (i = 0; i < 32; i++)
278 inv[p32i[i] - 1] = i;
281 * Now build a combination of the p and s boxes.
283 for (i = 0; i < 8; i++)
284 for (j = 0; j < 64; j++)
289 * Compute the index into the sbox table. The row number is
290 * formed from bits 0 and 5 (VAX terminology) and the column
291 * number from the middle 4 bits.
293 l = (j & 0x20) | ((j & 1) ? 0x10 : 0) | ((j >> 1) & 0xF);
296 * Each sbox entry gives us 4 bits which must be merged into
299 for (k = 0; k < 4; k++)
301 if ((si[i][l] & (8 >> k)) != 0)
302 perm |= 1L << (31 - inv[(i * 4) + k]);
312 fprintf(tables, "/*\n * Combined s and p boxes\n */\n");
313 fprintf(tables, "static INT32 sp[8][64] = {");
315 for (i = 0; i < 8; i++)
317 fprintf(tables, "\n /*\n * Permuted S box %d\n */", i);
318 for (j = 0; j < 16; j++)
320 fprintf(tables, "\n ");
321 for (k = 0; k < 4; k++)
322 fprintf(tables, "0x%lx, ", (long)(sp[i][(j * 4) + k]));
325 fprintf(tables, "\n};\n\n");
328 choiceinit( pc,bit,byte )
329 char pc[],bit[],byte[];
334 * Convert a permuted choice table from bit addressing to 2 tables
335 * giving the byte and bit within the byte address.
337 for (i = 0; i < 56; i++)
339 bit[i] = bytebit[(pc[i] - 1) & 07];
340 byte[i] = (pc[i] - 1) >> 3;
344 outchoice( bit,byte,name,comment )
345 char bit[],byte[],*name,*comment;
349 fprintf(tables, "/*\n * %s\n */\n", comment);
350 fprintf(tables, "static unsigned char %sbit[56] = {", name);
351 for (i = 0; i < 7; i++)
353 fprintf(tables, "\n ");
354 for (j = 0; j < 8; j++)
355 fprintf(tables, "0x%x, ", bit[(i * 8) + j] & 0xFF);
357 fprintf(tables, "\n};\n\n");
359 fprintf(tables, "static unsigned char %sbyte[56] = {", name);
360 for (i = 0; i < 7; i++)
362 fprintf(tables, "\n ");
363 for (j = 0; j < 8; j++)
364 fprintf(tables, "%2d, ", byte[(i * 8) + j]);
366 fprintf(tables, "\n};\n\n");
373 register char *cp,temp;
392 union { char bytes [ sizeof(long) ];
393 long longs; } testword ;
400 if ((endian = fopen(EFILE, "w")) == NULL)
406 fprintf(endian, "/* endian.h */\n");
407 fprintf(endian, "/*\n * Machine generated on %s */\n", ctime(&now));
408 fprintf(endian, "\n#ifndef SPHINX_ENDIAN\n\n");
409 fprintf(endian, "/*\n");
410 fprintf(endian, " * Little endian machines are DEC/Intel like\n");
411 fprintf(endian, " * Big endian machines are IBM/SPARC/Motorola like\n *\n");
412 fprintf(endian, " * This machine is ");
413 if (testword.bytes[0]) fprintf (endian,"little "); else fprintf(endian,"big ");
414 fprintf(endian, "endian since the value of SPHINX_ENDIAN is %u\n", testword.bytes[0]);
415 fprintf(endian, " *\n */\n\n");
416 fprintf(endian, "\n#define SPHINX_ENDIAN %u\n\n", testword.bytes[0]);
417 fprintf(endian, "\n#endif\n\n\n");
419 if ((tables = fopen(FILENAME, "w")) == NULL)
425 fprintf(tables, "/*\n * Machine generated tables for DES encryption %s */\n\n",
427 if (testword.bytes[0])
428 fprintf(tables, "/*\n * Little Endian (DEC/Intel like)\n */\n\n");
429 else fprintf(tables, "/*\n * Big Endian (IBM/SPARC/Motorola like)\n */\n\n");
431 perminit(iperma, ipermb, ip);
432 perminit(fperma, fpermb, fp);
434 if (testword.bytes[0]) /* little endian */
436 * Perform a byte swap on the final permutation table.
438 for (i = 0; i < 256; i++)
440 fperma[i] = swap(fperma[i]);
441 fpermb[i] = swap(fpermb[i]);
444 outperm(iperma, ipermb, "iperm", "Initial permutation");
445 outperm(fperma, fpermb, "fperm", "Final permutation");
448 choiceinit(pc1, pc1bit, pc1byte);
449 outchoice(pc1bit, pc1byte, "pc1", "Permuted choice 1 table");