Back to index

lightning-sunbird  0.9+nobinonly
mknewpc2.c
Go to the documentation of this file.
00001 /*
00002  *  mknewpc2.c
00003  *
00004  *  Generate PC-2 tables for DES-150 library
00005  *
00006  * ***** BEGIN LICENSE BLOCK *****
00007  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00008  *
00009  * The contents of this file are subject to the Mozilla Public License Version
00010  * 1.1 (the "License"); you may not use this file except in compliance with
00011  * the License. You may obtain a copy of the License at
00012  * http://www.mozilla.org/MPL/
00013  *
00014  * Software distributed under the License is distributed on an "AS IS" basis,
00015  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00016  * for the specific language governing rights and limitations under the
00017  * License.
00018  *
00019  * The Original Code is the DES-150 library.
00020  *
00021  * The Initial Developer of the Original Code is
00022  * Nelson B. Bolyard, nelsonb@iname.com.
00023  * Portions created by the Initial Developer are Copyright (C) 1990
00024  * the Initial Developer. All Rights Reserved.
00025  *
00026  * Contributor(s):
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 typedef unsigned char BYTE;
00043 typedef unsigned int  HALF;
00044 
00045 #define DES_ENCRYPT 0
00046 #define DES_DECRYPT 1
00047 
00048 /* two 28-bit registers defined in key schedule production process */
00049 static HALF C0, D0;
00050 
00051 static HALF L0, R0;
00052 
00053 /* key schedule, 16 internal keys, each with 8 6-bit parts */
00054 static BYTE KS [8] [16];
00055 
00056 
00057 /*
00058  * This table takes the 56 bits in C0 and D0 and shows show they are 
00059  * permuted into the 8 6-bit parts of the key in the key schedule.
00060  * The bits of C0 are numbered left to right, 1-28.
00061  * The bits of D0 are numbered left to right, 29-56.
00062  * Zeros in this table represent bits that are always zero.
00063  * Note that all the bits in the first  4 rows come from C0, 
00064  *       and all the bits in the second 4 rows come from D0.
00065  */
00066 static const BYTE PC2[64] = {
00067     14, 17, 11, 24,  1,  5,  0,  0,       /* S1 */
00068      3, 28, 15,  6, 21, 10,  0,  0,       /* S2 */
00069     23, 19, 12,  4, 26,  8,  0,  0,       /* S3 */
00070     16,  7, 27, 20, 13,  2,  0,  0,       /* S4 */
00071 
00072     41, 52, 31, 37, 47, 55,  0,  0,       /* S5 */
00073     30, 40, 51, 45, 33, 48,  0,  0,       /* S6 */
00074     44, 49, 39, 56, 34, 53,  0,  0,       /* S7 */
00075     46, 42, 50, 36, 29, 32,  0,  0 /* S8 */
00076 };
00077 
00078 /* This table represents the same info as PC2, except that 
00079  * The bits of C0 and D0 are each numbered right to left, 0-27.
00080  * -1 values indicate bits that are always zero.
00081  * As before all the bits in the first  4 rows come from C0, 
00082  *       and all the bits in the second 4 rows come from D0.
00083  */
00084 static       signed char PC2a[64] = {
00085 /* bits of C0 */
00086     14, 11, 17,  4, 27, 23, -1, -1,       /* S1 */
00087     25,  0, 13, 22,  7, 18, -1, -1,       /* S2 */
00088      5,  9, 16, 24,  2, 20, -1, -1,       /* S3 */
00089     12, 21,  1,  8, 15, 26, -1, -1,       /* S4 */
00090 /* bits of D0 */
00091     15,  4, 25, 19,  9,  1, -1, -1,       /* S5 */
00092     26, 16,  5, 11, 23,  8, -1, -1,       /* S6 */
00093     12,  7, 17,  0, 22,  3, -1, -1,       /* S7 */
00094     10, 14,  6, 20, 27, 24, -1, -1        /* S8 */
00095 };
00096 
00097 /* This table represents the same info as PC2a, except that 
00098  * The order of of the rows has been changed to increase the efficiency
00099  * with which the key sechedule is created.
00100  * Fewer shifts and ANDs are required to make the KS from these.
00101  */
00102 static const signed char PC2b[64] = {
00103 /* bits of C0 */
00104     14, 11, 17,  4, 27, 23, -1, -1,       /* S1 */
00105      5,  9, 16, 24,  2, 20, -1, -1,       /* S3 */
00106     25,  0, 13, 22,  7, 18, -1, -1,       /* S2 */
00107     12, 21,  1,  8, 15, 26, -1, -1,       /* S4 */
00108 /* bits of D0 */
00109     26, 16,  5, 11, 23,  8, -1, -1,       /* S6 */
00110     10, 14,  6, 20, 27, 24, -1, -1,       /* S8 */
00111     15,  4, 25, 19,  9,  1, -1, -1,       /* S5 */
00112     12,  7, 17,  0, 22,  3, -1, -1        /* S7 */
00113 };
00114 
00115 /* Only 24 of the 28 bits in C0 and D0 are used in PC2.
00116  * The used bits of C0 and D0 are grouped into 4 groups of 6,
00117  * so that the PC2 permutation can be accomplished with 4 lookups
00118  * in tables of 64 entries.
00119  * The following table shows how the bits of C0 and D0 are grouped
00120  * into indexes for the respective table lookups.  
00121  * Bits are numbered right-to-left, 0-27, as in PC2b.
00122  */
00123 static BYTE NDX[48] = {
00124 /* Bits of C0 */
00125     27, 26, 25, 24, 23, 22, /* C0 table 0 */
00126     18, 17, 16, 15, 14, 13, /* C0 table 1 */
00127      9,  8,  7,  2,  1,  0, /* C0 table 2 */
00128      5,  4, 21, 20, 12, 11, /* C0 table 3 */
00129 /* bits of D0 */
00130     27, 26, 25, 24, 23, 22, /* D0 table 0 */
00131     20, 19, 17, 16, 15, 14, /* D0 table 1 */
00132     12, 11, 10,  9,  8,  7, /* D0 table 2 */
00133      6,  5,  4,  3,  1,  0  /* D0 table 3 */
00134 };
00135 
00136 /* Here's the code that does that grouping. 
00137        left   = PC2LOOKUP(0, 0, ((c0 >> 22) & 0x3F) );
00138        left  |= PC2LOOKUP(0, 1, ((c0 >> 13) & 0x3F) );
00139        left  |= PC2LOOKUP(0, 2, ((c0 >>  4) & 0x38) | (c0 & 0x7) );
00140        left  |= PC2LOOKUP(0, 3, ((c0>>18)&0xC) | ((c0>>11)&0x3) | (c0&0x30));
00141 
00142        right  = PC2LOOKUP(1, 0, ((d0 >> 22) & 0x3F) );
00143        right |= PC2LOOKUP(1, 1, ((d0 >> 15) & 0x30) | ((d0 >> 14) & 0xf) );
00144        right |= PC2LOOKUP(1, 2, ((d0 >>  7) & 0x3F) );
00145        right |= PC2LOOKUP(1, 3, ((d0 >>  1) & 0x3C) | (d0 & 0x3));
00146 */
00147 
00148 void
00149 make_pc2a( void )
00150 {
00151 
00152     int i, j;
00153 
00154     for ( i = 0; i < 64; ++i ) {
00155        j = PC2[i];
00156        if (j == 0)
00157            j = -1;
00158        else if ( j < 29 )
00159            j = 28 - j ;
00160        else
00161            j = 56 - j;
00162        PC2a[i] = j;
00163     }
00164     for ( i = 0; i < 64; i += 8 ) {
00165        printf("%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,\n",
00166               PC2a[i+0],PC2a[i+1],PC2a[i+2],PC2a[i+3],
00167               PC2a[i+4],PC2a[i+5],PC2a[i+6],PC2a[i+7] );
00168     }
00169 }
00170 
00171 HALF PC2cd0[64];
00172 
00173 HALF PC_2H[8][64];
00174 
00175 void
00176 mktable( )
00177 {
00178     int i;
00179     int table;
00180     const BYTE * ndx   = NDX;
00181     HALF         mask;
00182 
00183     mask  = 0x80000000;
00184     for (i = 0; i < 32; ++i, mask >>= 1) {
00185        int bit = PC2b[i];
00186        if (bit < 0)
00187            continue;
00188        PC2cd0[bit + 32] = mask;
00189     }
00190 
00191     mask  = 0x80000000;
00192     for (i = 32; i < 64; ++i, mask >>= 1) {
00193        int bit = PC2b[i];
00194        if (bit < 0)
00195            continue;
00196        PC2cd0[bit] = mask;
00197     }
00198 
00199 #if DEBUG
00200     for (i = 0; i < 64; ++i) {
00201        printf("0x%08x,\n", PC2cd0[i]);
00202     }
00203 #endif
00204     for (i = 0; i < 24; ++i) {
00205        NDX[i] += 32; /* because c0 is the upper half */
00206     }
00207 
00208     for (table = 0; table < 8; ++table) {
00209        HALF bitvals[6];
00210        for (i = 0; i < 6; ++i) {
00211            bitvals[5-i] = PC2cd0[*ndx++];
00212        }
00213        for (i = 0; i < 64; ++i) {
00214            int  j;
00215            int  k     = 0;
00216            HALF value = 0;
00217 
00218            for (j = i; j; j >>= 1, ++k) {
00219               if (j & 1) {
00220                   value |= bitvals[k];
00221               }
00222            }
00223            PC_2H[table][i] = value;
00224        }
00225        printf("/* table %d */ {\n", table );
00226        for (i = 0; i < 64; i += 4) {
00227            printf("    0x%08x, 0x%08x, 0x%08x, 0x%08x, \n",
00228                   PC_2H[table][i],   PC_2H[table][i+1],
00229                   PC_2H[table][i+2], PC_2H[table][i+3]);
00230        }
00231        printf("  },\n");
00232     }
00233 }
00234 
00235 
00236 int
00237 main(void)
00238 {
00239 /*   make_pc2a(); */
00240    mktable();
00241    return 0;
00242 }