security/nss/lib/freebl/mksp.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/mksp.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/*
     1.5 + *  mksp.c
     1.6 + *
     1.7 + *  Generate SP 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 +#include <stdio.h>
    1.14 +
    1.15 +/*
    1.16 + * sboxes - the tables for the s-box functions
    1.17 + *        from FIPS 46, pages 15-16.
    1.18 + */
    1.19 +unsigned char S[8][64] = {
    1.20 +/* Func S1 = */ {
    1.21 +	14,  0,  4, 15, 13,  7,  1,  4,  2, 14, 15,  2, 11, 13,  8,  1,
    1.22 +	 3, 10, 10,  6,  6, 12, 12, 11,  5,  9,  9,  5,  0,  3,  7,  8,
    1.23 +	 4, 15,  1, 12, 14,  8,  8,  2, 13,  4,  6,  9,  2,  1, 11,  7,
    1.24 +	15,  5, 12, 11,  9,  3,  7, 14,  3, 10, 10,  0,  5,  6,  0, 13
    1.25 +    },
    1.26 +/* Func S2 = */ {
    1.27 +	15,  3,  1, 13,  8,  4, 14,  7,  6, 15, 11,  2,  3,  8,  4, 14,
    1.28 +	 9, 12,  7,  0,  2,  1, 13, 10, 12,  6,  0,  9,  5, 11, 10,  5,
    1.29 +	 0, 13, 14,  8,  7, 10, 11,  1, 10,  3,  4, 15, 13,  4,  1,  2,
    1.30 +	 5, 11,  8,  6, 12,  7,  6, 12,  9,  0,  3,  5,  2, 14, 15,  9
    1.31 +    },
    1.32 +/* Func S3 = */ {
    1.33 +	10, 13,  0,  7,  9,  0, 14,  9,  6,  3,  3,  4, 15,  6,  5, 10,
    1.34 +	 1,  2, 13,  8, 12,  5,  7, 14, 11, 12,  4, 11,  2, 15,  8,  1,
    1.35 +	13,  1,  6, 10,  4, 13,  9,  0,  8,  6, 15,  9,  3,  8,  0,  7,
    1.36 +	11,  4,  1, 15,  2, 14, 12,  3,  5, 11, 10,  5, 14,  2,  7, 12
    1.37 +    },
    1.38 +/* Func S4 = */ {
    1.39 +	 7, 13, 13,  8, 14, 11,  3,  5,  0,  6,  6, 15,  9,  0, 10,  3,
    1.40 +	 1,  4,  2,  7,  8,  2,  5, 12, 11,  1, 12, 10,  4, 14, 15,  9,
    1.41 +	10,  3,  6, 15,  9,  0,  0,  6, 12, 10, 11,  1,  7, 13, 13,  8,
    1.42 +	15,  9,  1,  4,  3,  5, 14, 11,  5, 12,  2,  7,  8,  2,  4, 14
    1.43 +    },
    1.44 +/* Func S5 = */ {
    1.45 +	 2, 14, 12, 11,  4,  2,  1, 12,  7,  4, 10,  7, 11, 13,  6,  1,
    1.46 +	 8,  5,  5,  0,  3, 15, 15, 10, 13,  3,  0,  9, 14,  8,  9,  6,
    1.47 +	 4, 11,  2,  8,  1, 12, 11,  7, 10,  1, 13, 14,  7,  2,  8, 13,
    1.48 +	15,  6,  9, 15, 12,  0,  5,  9,  6, 10,  3,  4,  0,  5, 14,  3
    1.49 +    },
    1.50 +/* Func S6 = */ {
    1.51 +	12, 10,  1, 15, 10,  4, 15,  2,  9,  7,  2, 12,  6,  9,  8,  5,
    1.52 +	 0,  6, 13,  1,  3, 13,  4, 14, 14,  0,  7, 11,  5,  3, 11,  8,
    1.53 +	 9,  4, 14,  3, 15,  2,  5, 12,  2,  9,  8,  5, 12, 15,  3, 10,
    1.54 +	 7, 11,  0, 14,  4,  1, 10,  7,  1,  6, 13,  0, 11,  8,  6, 13
    1.55 +    },
    1.56 +/* Func S7 = */ {
    1.57 +	 4, 13, 11,  0,  2, 11, 14,  7, 15,  4,  0,  9,  8,  1, 13, 10,
    1.58 +	 3, 14, 12,  3,  9,  5,  7, 12,  5,  2, 10, 15,  6,  8,  1,  6,
    1.59 +	 1,  6,  4, 11, 11, 13, 13,  8, 12,  1,  3,  4,  7, 10, 14,  7,
    1.60 +	10,  9, 15,  5,  6,  0,  8, 15,  0, 14,  5,  2,  9,  3,  2, 12
    1.61 +    },
    1.62 +/* Func S8 = */ {
    1.63 +	13,  1,  2, 15,  8, 13,  4,  8,  6, 10, 15,  3, 11,  7,  1,  4,
    1.64 +	10, 12,  9,  5,  3,  6, 14, 11,  5,  0,  0, 14, 12,  9,  7,  2,
    1.65 +	 7,  2, 11,  1,  4, 14,  1,  7,  9,  4, 12, 10, 14,  8,  2, 13,
    1.66 +	 0, 15,  6, 12, 10,  9, 13,  0, 15,  3,  3,  5,  5,  6,  8, 11
    1.67 +    }
    1.68 +};
    1.69 +
    1.70 +/*
    1.71 + * Permutation function for results from s-boxes
    1.72 + *   from FIPS 46 pages 12 and 16.
    1.73 + * P = 
    1.74 + */
    1.75 +unsigned char P[32] = {
    1.76 +	16,   7,  20,  21,  29,  12,  28,  17,
    1.77 +	 1,  15,  23,  26,   5,  18,  31,  10,
    1.78 +	 2,   8,  24,  14,  32,  27,   3,   9,
    1.79 +	19,  13,  30,   6,  22,  11,   4,  25
    1.80 +};
    1.81 +
    1.82 +unsigned int Pinv[32];
    1.83 +unsigned int SP[8][64];
    1.84 +
    1.85 +void
    1.86 +makePinv(void)
    1.87 +{
    1.88 +    int i;
    1.89 +    unsigned int Pi = 0x80000000;
    1.90 +    for (i = 0; i < 32; ++i) {
    1.91 +    	int j = 32 - P[i];
    1.92 +	Pinv[j] = Pi;
    1.93 +	Pi >>= 1;
    1.94 +    }
    1.95 +}
    1.96 +
    1.97 +void
    1.98 +makeSP(void)
    1.99 +{
   1.100 +    int box;
   1.101 +    for (box = 0; box < 8; ++box) {
   1.102 +	int item;
   1.103 +	printf("/* box S%d */ {\n", box + 1);
   1.104 +    	for (item = 0; item < 64; ++item ) {
   1.105 +	    unsigned int s = S[box][item];
   1.106 +	    unsigned int val = 0;
   1.107 +	    unsigned int bitnum = (7-box) * 4;
   1.108 +	    for (; s; s >>= 1, ++bitnum) {
   1.109 +		if (s & 1) {
   1.110 +		    val |= Pinv[bitnum];
   1.111 +		}
   1.112 +	    }
   1.113 +	    val = (val << 3) | (val >> 29);
   1.114 +	    SP[box][item] = val;
   1.115 +	}
   1.116 +	for (item = 0; item < 64; item += 4) {
   1.117 +	    printf("\t0x%08x, 0x%08x, 0x%08x, 0x%08x,\n",
   1.118 +	    SP[box][item], SP[box][item+1], SP[box][item+2], SP[box][item+3]);
   1.119 +	}
   1.120 +	printf("    },\n");
   1.121 +    }
   1.122 +}
   1.123 +
   1.124 +int
   1.125 +main()
   1.126 +{
   1.127 +    makePinv();
   1.128 +    makeSP();
   1.129 +    return 0;
   1.130 +}

mercurial