security/nss/lib/freebl/mknewpc2.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/mknewpc2.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,210 @@
     1.4 +/*
     1.5 + *  mknewpc2.c
     1.6 + *
     1.7 + *  Generate PC-2 tables for DES-150 library
     1.8 + *
     1.9 + * This Source Code Form is subject to the terms of the Mozilla Public
    1.10 + * License, v. 2.0. If a copy of the MPL was not distributed with this
    1.11 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.12 +
    1.13 +typedef unsigned char BYTE;
    1.14 +typedef unsigned int  HALF;
    1.15 +
    1.16 +#define DES_ENCRYPT 0
    1.17 +#define DES_DECRYPT 1
    1.18 +
    1.19 +/* two 28-bit registers defined in key schedule production process */
    1.20 +static HALF C0, D0;
    1.21 +
    1.22 +static HALF L0, R0;
    1.23 +
    1.24 +/* key schedule, 16 internal keys, each with 8 6-bit parts */
    1.25 +static BYTE KS [8] [16];
    1.26 +
    1.27 +
    1.28 +/*
    1.29 + * This table takes the 56 bits in C0 and D0 and shows show they are 
    1.30 + * permuted into the 8 6-bit parts of the key in the key schedule.
    1.31 + * The bits of C0 are numbered left to right, 1-28.
    1.32 + * The bits of D0 are numbered left to right, 29-56.
    1.33 + * Zeros in this table represent bits that are always zero.
    1.34 + * Note that all the bits in the first  4 rows come from C0, 
    1.35 + *       and all the bits in the second 4 rows come from D0.
    1.36 + */
    1.37 +static const BYTE PC2[64] = {
    1.38 +    14, 17, 11, 24,  1,  5,  0,  0,	/* S1 */
    1.39 +     3, 28, 15,  6, 21, 10,  0,  0,	/* S2 */
    1.40 +    23, 19, 12,  4, 26,  8,  0,  0,	/* S3 */
    1.41 +    16,  7, 27, 20, 13,  2,  0,  0,	/* S4 */
    1.42 +
    1.43 +    41, 52, 31, 37, 47, 55,  0,  0,	/* S5 */
    1.44 +    30, 40, 51, 45, 33, 48,  0,  0,	/* S6 */
    1.45 +    44, 49, 39, 56, 34, 53,  0,  0,	/* S7 */
    1.46 +    46, 42, 50, 36, 29, 32,  0,  0	/* S8 */
    1.47 +};
    1.48 +
    1.49 +/* This table represents the same info as PC2, except that 
    1.50 + * The bits of C0 and D0 are each numbered right to left, 0-27.
    1.51 + * -1 values indicate bits that are always zero.
    1.52 + * As before all the bits in the first  4 rows come from C0, 
    1.53 + *       and all the bits in the second 4 rows come from D0.
    1.54 + */
    1.55 +static       signed char PC2a[64] = {
    1.56 +/* bits of C0 */
    1.57 +    14, 11, 17,  4, 27, 23, -1, -1,	/* S1 */
    1.58 +    25,  0, 13, 22,  7, 18, -1, -1,	/* S2 */
    1.59 +     5,  9, 16, 24,  2, 20, -1, -1,	/* S3 */
    1.60 +    12, 21,  1,  8, 15, 26, -1, -1,	/* S4 */
    1.61 +/* bits of D0 */
    1.62 +    15,  4, 25, 19,  9,  1, -1, -1,	/* S5 */
    1.63 +    26, 16,  5, 11, 23,  8, -1, -1,	/* S6 */
    1.64 +    12,  7, 17,  0, 22,  3, -1, -1,	/* S7 */
    1.65 +    10, 14,  6, 20, 27, 24, -1, -1 	/* S8 */
    1.66 +};
    1.67 +
    1.68 +/* This table represents the same info as PC2a, except that 
    1.69 + * The order of of the rows has been changed to increase the efficiency
    1.70 + * with which the key sechedule is created.
    1.71 + * Fewer shifts and ANDs are required to make the KS from these.
    1.72 + */
    1.73 +static const signed char PC2b[64] = {
    1.74 +/* bits of C0 */
    1.75 +    14, 11, 17,  4, 27, 23, -1, -1,	/* S1 */
    1.76 +     5,  9, 16, 24,  2, 20, -1, -1,	/* S3 */
    1.77 +    25,  0, 13, 22,  7, 18, -1, -1,	/* S2 */
    1.78 +    12, 21,  1,  8, 15, 26, -1, -1,	/* S4 */
    1.79 +/* bits of D0 */
    1.80 +    26, 16,  5, 11, 23,  8, -1, -1,	/* S6 */
    1.81 +    10, 14,  6, 20, 27, 24, -1, -1,	/* S8 */
    1.82 +    15,  4, 25, 19,  9,  1, -1, -1,	/* S5 */
    1.83 +    12,  7, 17,  0, 22,  3, -1, -1 	/* S7 */
    1.84 +};
    1.85 +
    1.86 +/* Only 24 of the 28 bits in C0 and D0 are used in PC2.
    1.87 + * The used bits of C0 and D0 are grouped into 4 groups of 6,
    1.88 + * so that the PC2 permutation can be accomplished with 4 lookups
    1.89 + * in tables of 64 entries.
    1.90 + * The following table shows how the bits of C0 and D0 are grouped
    1.91 + * into indexes for the respective table lookups.  
    1.92 + * Bits are numbered right-to-left, 0-27, as in PC2b.
    1.93 + */
    1.94 +static BYTE NDX[48] = {
    1.95 +/* Bits of C0 */
    1.96 +    27, 26, 25, 24, 23, 22,	/* C0 table 0 */
    1.97 +    18, 17, 16, 15, 14, 13,	/* C0 table 1 */
    1.98 +     9,  8,  7,  2,  1,  0,	/* C0 table 2 */
    1.99 +     5,  4, 21, 20, 12, 11,	/* C0 table 3 */
   1.100 +/* bits of D0 */
   1.101 +    27, 26, 25, 24, 23, 22,	/* D0 table 0 */
   1.102 +    20, 19, 17, 16, 15, 14,	/* D0 table 1 */
   1.103 +    12, 11, 10,  9,  8,  7,	/* D0 table 2 */
   1.104 +     6,  5,  4,  3,  1,  0	/* D0 table 3 */
   1.105 +};
   1.106 +
   1.107 +/* Here's the code that does that grouping. 
   1.108 +	left   = PC2LOOKUP(0, 0, ((c0 >> 22) & 0x3F) );
   1.109 +	left  |= PC2LOOKUP(0, 1, ((c0 >> 13) & 0x3F) );
   1.110 +	left  |= PC2LOOKUP(0, 2, ((c0 >>  4) & 0x38) | (c0 & 0x7) );
   1.111 +	left  |= PC2LOOKUP(0, 3, ((c0>>18)&0xC) | ((c0>>11)&0x3) | (c0&0x30));
   1.112 +
   1.113 +	right  = PC2LOOKUP(1, 0, ((d0 >> 22) & 0x3F) );
   1.114 +	right |= PC2LOOKUP(1, 1, ((d0 >> 15) & 0x30) | ((d0 >> 14) & 0xf) );
   1.115 +	right |= PC2LOOKUP(1, 2, ((d0 >>  7) & 0x3F) );
   1.116 +	right |= PC2LOOKUP(1, 3, ((d0 >>  1) & 0x3C) | (d0 & 0x3));
   1.117 +*/
   1.118 +
   1.119 +void
   1.120 +make_pc2a( void )
   1.121 +{
   1.122 +
   1.123 +    int i, j;
   1.124 +
   1.125 +    for ( i = 0; i < 64; ++i ) {
   1.126 +	j = PC2[i];
   1.127 +	if (j == 0)
   1.128 +	    j = -1;
   1.129 +	else if ( j < 29 )
   1.130 +	    j = 28 - j ;
   1.131 +	else
   1.132 +	    j = 56 - j;
   1.133 +	PC2a[i] = j;
   1.134 +    }
   1.135 +    for ( i = 0; i < 64; i += 8 ) {
   1.136 +	printf("%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,\n",
   1.137 +		PC2a[i+0],PC2a[i+1],PC2a[i+2],PC2a[i+3],
   1.138 +		PC2a[i+4],PC2a[i+5],PC2a[i+6],PC2a[i+7] );
   1.139 +    }
   1.140 +}
   1.141 +
   1.142 +HALF PC2cd0[64];
   1.143 +
   1.144 +HALF PC_2H[8][64];
   1.145 +
   1.146 +void
   1.147 +mktable( )
   1.148 +{
   1.149 +    int i;
   1.150 +    int table;
   1.151 +    const BYTE * ndx   = NDX;
   1.152 +    HALF         mask;
   1.153 +
   1.154 +    mask  = 0x80000000;
   1.155 +    for (i = 0; i < 32; ++i, mask >>= 1) {
   1.156 +	int bit = PC2b[i];
   1.157 +	if (bit < 0)
   1.158 +	    continue;
   1.159 +	PC2cd0[bit + 32] = mask;
   1.160 +    }
   1.161 +
   1.162 +    mask  = 0x80000000;
   1.163 +    for (i = 32; i < 64; ++i, mask >>= 1) {
   1.164 +	int bit = PC2b[i];
   1.165 +	if (bit < 0)
   1.166 +	    continue;
   1.167 +	PC2cd0[bit] = mask;
   1.168 +    }
   1.169 +
   1.170 +#if DEBUG
   1.171 +    for (i = 0; i < 64; ++i) {
   1.172 +    	printf("0x%08x,\n", PC2cd0[i]);
   1.173 +    }
   1.174 +#endif
   1.175 +    for (i = 0; i < 24; ++i) {
   1.176 +    	NDX[i] += 32;	/* because c0 is the upper half */
   1.177 +    }
   1.178 +
   1.179 +    for (table = 0; table < 8; ++table) {
   1.180 +	HALF bitvals[6];
   1.181 +    	for (i = 0; i < 6; ++i) {
   1.182 +	    bitvals[5-i] = PC2cd0[*ndx++];
   1.183 +	}
   1.184 +	for (i = 0; i < 64; ++i) {
   1.185 +	    int  j;
   1.186 +	    int  k     = 0;
   1.187 +	    HALF value = 0;
   1.188 +
   1.189 +	    for (j = i; j; j >>= 1, ++k) {
   1.190 +	    	if (j & 1) {
   1.191 +		    value |= bitvals[k];
   1.192 +		}
   1.193 +	    }
   1.194 +	    PC_2H[table][i] = value;
   1.195 +	}
   1.196 +	printf("/* table %d */ {\n", table );
   1.197 +	for (i = 0; i < 64; i += 4) {
   1.198 +	    printf("    0x%08x, 0x%08x, 0x%08x, 0x%08x, \n",
   1.199 +		    PC_2H[table][i],   PC_2H[table][i+1],
   1.200 +		    PC_2H[table][i+2], PC_2H[table][i+3]);
   1.201 +	}
   1.202 +	printf("  },\n");
   1.203 +    }
   1.204 +}
   1.205 +
   1.206 +
   1.207 +int
   1.208 +main(void)
   1.209 +{
   1.210 +/*   make_pc2a(); */
   1.211 +   mktable();
   1.212 +   return 0;
   1.213 +}

mercurial