michael@0: /* michael@0: * mknewpc2.c michael@0: * michael@0: * Generate PC-2 tables for DES-150 library michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: typedef unsigned char BYTE; michael@0: typedef unsigned int HALF; michael@0: michael@0: #define DES_ENCRYPT 0 michael@0: #define DES_DECRYPT 1 michael@0: michael@0: /* two 28-bit registers defined in key schedule production process */ michael@0: static HALF C0, D0; michael@0: michael@0: static HALF L0, R0; michael@0: michael@0: /* key schedule, 16 internal keys, each with 8 6-bit parts */ michael@0: static BYTE KS [8] [16]; michael@0: michael@0: michael@0: /* michael@0: * This table takes the 56 bits in C0 and D0 and shows show they are michael@0: * permuted into the 8 6-bit parts of the key in the key schedule. michael@0: * The bits of C0 are numbered left to right, 1-28. michael@0: * The bits of D0 are numbered left to right, 29-56. michael@0: * Zeros in this table represent bits that are always zero. michael@0: * Note that all the bits in the first 4 rows come from C0, michael@0: * and all the bits in the second 4 rows come from D0. michael@0: */ michael@0: static const BYTE PC2[64] = { michael@0: 14, 17, 11, 24, 1, 5, 0, 0, /* S1 */ michael@0: 3, 28, 15, 6, 21, 10, 0, 0, /* S2 */ michael@0: 23, 19, 12, 4, 26, 8, 0, 0, /* S3 */ michael@0: 16, 7, 27, 20, 13, 2, 0, 0, /* S4 */ michael@0: michael@0: 41, 52, 31, 37, 47, 55, 0, 0, /* S5 */ michael@0: 30, 40, 51, 45, 33, 48, 0, 0, /* S6 */ michael@0: 44, 49, 39, 56, 34, 53, 0, 0, /* S7 */ michael@0: 46, 42, 50, 36, 29, 32, 0, 0 /* S8 */ michael@0: }; michael@0: michael@0: /* This table represents the same info as PC2, except that michael@0: * The bits of C0 and D0 are each numbered right to left, 0-27. michael@0: * -1 values indicate bits that are always zero. michael@0: * As before all the bits in the first 4 rows come from C0, michael@0: * and all the bits in the second 4 rows come from D0. michael@0: */ michael@0: static signed char PC2a[64] = { michael@0: /* bits of C0 */ michael@0: 14, 11, 17, 4, 27, 23, -1, -1, /* S1 */ michael@0: 25, 0, 13, 22, 7, 18, -1, -1, /* S2 */ michael@0: 5, 9, 16, 24, 2, 20, -1, -1, /* S3 */ michael@0: 12, 21, 1, 8, 15, 26, -1, -1, /* S4 */ michael@0: /* bits of D0 */ michael@0: 15, 4, 25, 19, 9, 1, -1, -1, /* S5 */ michael@0: 26, 16, 5, 11, 23, 8, -1, -1, /* S6 */ michael@0: 12, 7, 17, 0, 22, 3, -1, -1, /* S7 */ michael@0: 10, 14, 6, 20, 27, 24, -1, -1 /* S8 */ michael@0: }; michael@0: michael@0: /* This table represents the same info as PC2a, except that michael@0: * The order of of the rows has been changed to increase the efficiency michael@0: * with which the key sechedule is created. michael@0: * Fewer shifts and ANDs are required to make the KS from these. michael@0: */ michael@0: static const signed char PC2b[64] = { michael@0: /* bits of C0 */ michael@0: 14, 11, 17, 4, 27, 23, -1, -1, /* S1 */ michael@0: 5, 9, 16, 24, 2, 20, -1, -1, /* S3 */ michael@0: 25, 0, 13, 22, 7, 18, -1, -1, /* S2 */ michael@0: 12, 21, 1, 8, 15, 26, -1, -1, /* S4 */ michael@0: /* bits of D0 */ michael@0: 26, 16, 5, 11, 23, 8, -1, -1, /* S6 */ michael@0: 10, 14, 6, 20, 27, 24, -1, -1, /* S8 */ michael@0: 15, 4, 25, 19, 9, 1, -1, -1, /* S5 */ michael@0: 12, 7, 17, 0, 22, 3, -1, -1 /* S7 */ michael@0: }; michael@0: michael@0: /* Only 24 of the 28 bits in C0 and D0 are used in PC2. michael@0: * The used bits of C0 and D0 are grouped into 4 groups of 6, michael@0: * so that the PC2 permutation can be accomplished with 4 lookups michael@0: * in tables of 64 entries. michael@0: * The following table shows how the bits of C0 and D0 are grouped michael@0: * into indexes for the respective table lookups. michael@0: * Bits are numbered right-to-left, 0-27, as in PC2b. michael@0: */ michael@0: static BYTE NDX[48] = { michael@0: /* Bits of C0 */ michael@0: 27, 26, 25, 24, 23, 22, /* C0 table 0 */ michael@0: 18, 17, 16, 15, 14, 13, /* C0 table 1 */ michael@0: 9, 8, 7, 2, 1, 0, /* C0 table 2 */ michael@0: 5, 4, 21, 20, 12, 11, /* C0 table 3 */ michael@0: /* bits of D0 */ michael@0: 27, 26, 25, 24, 23, 22, /* D0 table 0 */ michael@0: 20, 19, 17, 16, 15, 14, /* D0 table 1 */ michael@0: 12, 11, 10, 9, 8, 7, /* D0 table 2 */ michael@0: 6, 5, 4, 3, 1, 0 /* D0 table 3 */ michael@0: }; michael@0: michael@0: /* Here's the code that does that grouping. michael@0: left = PC2LOOKUP(0, 0, ((c0 >> 22) & 0x3F) ); michael@0: left |= PC2LOOKUP(0, 1, ((c0 >> 13) & 0x3F) ); michael@0: left |= PC2LOOKUP(0, 2, ((c0 >> 4) & 0x38) | (c0 & 0x7) ); michael@0: left |= PC2LOOKUP(0, 3, ((c0>>18)&0xC) | ((c0>>11)&0x3) | (c0&0x30)); michael@0: michael@0: right = PC2LOOKUP(1, 0, ((d0 >> 22) & 0x3F) ); michael@0: right |= PC2LOOKUP(1, 1, ((d0 >> 15) & 0x30) | ((d0 >> 14) & 0xf) ); michael@0: right |= PC2LOOKUP(1, 2, ((d0 >> 7) & 0x3F) ); michael@0: right |= PC2LOOKUP(1, 3, ((d0 >> 1) & 0x3C) | (d0 & 0x3)); michael@0: */ michael@0: michael@0: void michael@0: make_pc2a( void ) michael@0: { michael@0: michael@0: int i, j; michael@0: michael@0: for ( i = 0; i < 64; ++i ) { michael@0: j = PC2[i]; michael@0: if (j == 0) michael@0: j = -1; michael@0: else if ( j < 29 ) michael@0: j = 28 - j ; michael@0: else michael@0: j = 56 - j; michael@0: PC2a[i] = j; michael@0: } michael@0: for ( i = 0; i < 64; i += 8 ) { michael@0: printf("%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,\n", michael@0: PC2a[i+0],PC2a[i+1],PC2a[i+2],PC2a[i+3], michael@0: PC2a[i+4],PC2a[i+5],PC2a[i+6],PC2a[i+7] ); michael@0: } michael@0: } michael@0: michael@0: HALF PC2cd0[64]; michael@0: michael@0: HALF PC_2H[8][64]; michael@0: michael@0: void michael@0: mktable( ) michael@0: { michael@0: int i; michael@0: int table; michael@0: const BYTE * ndx = NDX; michael@0: HALF mask; michael@0: michael@0: mask = 0x80000000; michael@0: for (i = 0; i < 32; ++i, mask >>= 1) { michael@0: int bit = PC2b[i]; michael@0: if (bit < 0) michael@0: continue; michael@0: PC2cd0[bit + 32] = mask; michael@0: } michael@0: michael@0: mask = 0x80000000; michael@0: for (i = 32; i < 64; ++i, mask >>= 1) { michael@0: int bit = PC2b[i]; michael@0: if (bit < 0) michael@0: continue; michael@0: PC2cd0[bit] = mask; michael@0: } michael@0: michael@0: #if DEBUG michael@0: for (i = 0; i < 64; ++i) { michael@0: printf("0x%08x,\n", PC2cd0[i]); michael@0: } michael@0: #endif michael@0: for (i = 0; i < 24; ++i) { michael@0: NDX[i] += 32; /* because c0 is the upper half */ michael@0: } michael@0: michael@0: for (table = 0; table < 8; ++table) { michael@0: HALF bitvals[6]; michael@0: for (i = 0; i < 6; ++i) { michael@0: bitvals[5-i] = PC2cd0[*ndx++]; michael@0: } michael@0: for (i = 0; i < 64; ++i) { michael@0: int j; michael@0: int k = 0; michael@0: HALF value = 0; michael@0: michael@0: for (j = i; j; j >>= 1, ++k) { michael@0: if (j & 1) { michael@0: value |= bitvals[k]; michael@0: } michael@0: } michael@0: PC_2H[table][i] = value; michael@0: } michael@0: printf("/* table %d */ {\n", table ); michael@0: for (i = 0; i < 64; i += 4) { michael@0: printf(" 0x%08x, 0x%08x, 0x%08x, 0x%08x, \n", michael@0: PC_2H[table][i], PC_2H[table][i+1], michael@0: PC_2H[table][i+2], PC_2H[table][i+3]); michael@0: } michael@0: printf(" },\n"); michael@0: } michael@0: } michael@0: michael@0: michael@0: int michael@0: main(void) michael@0: { michael@0: /* make_pc2a(); */ michael@0: mktable(); michael@0: return 0; michael@0: }