PageRenderTime 46ms CodeModel.GetById 18ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/security/nss/lib/freebl/mksp.c

http://github.com/zpao/v8monkey
C | 159 lines | 103 code | 7 blank | 49 comment | 6 complexity | 507be97a02aab34f78df211d74dff3d8 MD5 | raw file
  1/*
  2 *  mksp.c
  3 *
  4 *  Generate SP tables for DES-150 library
  5 *
  6 * ***** BEGIN LICENSE BLOCK *****
  7 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  8 *
  9 * The contents of this file are subject to the Mozilla Public License Version
 10 * 1.1 (the "License"); you may not use this file except in compliance with
 11 * the License. You may obtain a copy of the License at
 12 * http://www.mozilla.org/MPL/
 13 *
 14 * Software distributed under the License is distributed on an "AS IS" basis,
 15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 16 * for the specific language governing rights and limitations under the
 17 * License.
 18 *
 19 * The Original Code is the DES-150 library.
 20 *
 21 * The Initial Developer of the Original Code is
 22 * Nelson B. Bolyard, nelsonb@iname.com.
 23 * Portions created by the Initial Developer are Copyright (C) 1990
 24 * the Initial Developer. All Rights Reserved.
 25 *
 26 * Contributor(s):
 27 *
 28 * Alternatively, the contents of this file may be used under the terms of
 29 * either the GNU General Public License Version 2 or later (the "GPL"), or
 30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 31 * in which case the provisions of the GPL or the LGPL are applicable instead
 32 * of those above. If you wish to allow use of your version of this file only
 33 * under the terms of either the GPL or the LGPL, and not to allow others to
 34 * use your version of this file under the terms of the MPL, indicate your
 35 * decision by deleting the provisions above and replace them with the notice
 36 * and other provisions required by the GPL or the LGPL. If you do not delete
 37 * the provisions above, a recipient may use your version of this file under
 38 * the terms of any one of the MPL, the GPL or the LGPL.
 39 *
 40 * ***** END LICENSE BLOCK ***** */
 41
 42#include <stdio.h>
 43
 44/*
 45 * sboxes - the tables for the s-box functions
 46 *        from FIPS 46, pages 15-16.
 47 */
 48unsigned char S[8][64] = {
 49/* Func S1 = */ {
 50	14,  0,  4, 15, 13,  7,  1,  4,  2, 14, 15,  2, 11, 13,  8,  1,
 51	 3, 10, 10,  6,  6, 12, 12, 11,  5,  9,  9,  5,  0,  3,  7,  8,
 52	 4, 15,  1, 12, 14,  8,  8,  2, 13,  4,  6,  9,  2,  1, 11,  7,
 53	15,  5, 12, 11,  9,  3,  7, 14,  3, 10, 10,  0,  5,  6,  0, 13
 54    },
 55/* Func S2 = */ {
 56	15,  3,  1, 13,  8,  4, 14,  7,  6, 15, 11,  2,  3,  8,  4, 14,
 57	 9, 12,  7,  0,  2,  1, 13, 10, 12,  6,  0,  9,  5, 11, 10,  5,
 58	 0, 13, 14,  8,  7, 10, 11,  1, 10,  3,  4, 15, 13,  4,  1,  2,
 59	 5, 11,  8,  6, 12,  7,  6, 12,  9,  0,  3,  5,  2, 14, 15,  9
 60    },
 61/* Func S3 = */ {
 62	10, 13,  0,  7,  9,  0, 14,  9,  6,  3,  3,  4, 15,  6,  5, 10,
 63	 1,  2, 13,  8, 12,  5,  7, 14, 11, 12,  4, 11,  2, 15,  8,  1,
 64	13,  1,  6, 10,  4, 13,  9,  0,  8,  6, 15,  9,  3,  8,  0,  7,
 65	11,  4,  1, 15,  2, 14, 12,  3,  5, 11, 10,  5, 14,  2,  7, 12
 66    },
 67/* Func S4 = */ {
 68	 7, 13, 13,  8, 14, 11,  3,  5,  0,  6,  6, 15,  9,  0, 10,  3,
 69	 1,  4,  2,  7,  8,  2,  5, 12, 11,  1, 12, 10,  4, 14, 15,  9,
 70	10,  3,  6, 15,  9,  0,  0,  6, 12, 10, 11,  1,  7, 13, 13,  8,
 71	15,  9,  1,  4,  3,  5, 14, 11,  5, 12,  2,  7,  8,  2,  4, 14
 72    },
 73/* Func S5 = */ {
 74	 2, 14, 12, 11,  4,  2,  1, 12,  7,  4, 10,  7, 11, 13,  6,  1,
 75	 8,  5,  5,  0,  3, 15, 15, 10, 13,  3,  0,  9, 14,  8,  9,  6,
 76	 4, 11,  2,  8,  1, 12, 11,  7, 10,  1, 13, 14,  7,  2,  8, 13,
 77	15,  6,  9, 15, 12,  0,  5,  9,  6, 10,  3,  4,  0,  5, 14,  3
 78    },
 79/* Func S6 = */ {
 80	12, 10,  1, 15, 10,  4, 15,  2,  9,  7,  2, 12,  6,  9,  8,  5,
 81	 0,  6, 13,  1,  3, 13,  4, 14, 14,  0,  7, 11,  5,  3, 11,  8,
 82	 9,  4, 14,  3, 15,  2,  5, 12,  2,  9,  8,  5, 12, 15,  3, 10,
 83	 7, 11,  0, 14,  4,  1, 10,  7,  1,  6, 13,  0, 11,  8,  6, 13
 84    },
 85/* Func S7 = */ {
 86	 4, 13, 11,  0,  2, 11, 14,  7, 15,  4,  0,  9,  8,  1, 13, 10,
 87	 3, 14, 12,  3,  9,  5,  7, 12,  5,  2, 10, 15,  6,  8,  1,  6,
 88	 1,  6,  4, 11, 11, 13, 13,  8, 12,  1,  3,  4,  7, 10, 14,  7,
 89	10,  9, 15,  5,  6,  0,  8, 15,  0, 14,  5,  2,  9,  3,  2, 12
 90    },
 91/* Func S8 = */ {
 92	13,  1,  2, 15,  8, 13,  4,  8,  6, 10, 15,  3, 11,  7,  1,  4,
 93	10, 12,  9,  5,  3,  6, 14, 11,  5,  0,  0, 14, 12,  9,  7,  2,
 94	 7,  2, 11,  1,  4, 14,  1,  7,  9,  4, 12, 10, 14,  8,  2, 13,
 95	 0, 15,  6, 12, 10,  9, 13,  0, 15,  3,  3,  5,  5,  6,  8, 11
 96    }
 97};
 98
 99/*
100 * Permutation function for results from s-boxes
101 *   from FIPS 46 pages 12 and 16.
102 * P = 
103 */
104unsigned char P[32] = {
105	16,   7,  20,  21,  29,  12,  28,  17,
106	 1,  15,  23,  26,   5,  18,  31,  10,
107	 2,   8,  24,  14,  32,  27,   3,   9,
108	19,  13,  30,   6,  22,  11,   4,  25
109};
110
111unsigned int Pinv[32];
112unsigned int SP[8][64];
113
114void
115makePinv(void)
116{
117    int i;
118    unsigned int Pi = 0x80000000;
119    for (i = 0; i < 32; ++i) {
120    	int j = 32 - P[i];
121	Pinv[j] = Pi;
122	Pi >>= 1;
123    }
124}
125
126void
127makeSP(void)
128{
129    int box;
130    for (box = 0; box < 8; ++box) {
131	int item;
132	printf("/* box S%d */ {\n", box + 1);
133    	for (item = 0; item < 64; ++item ) {
134	    unsigned int s = S[box][item];
135	    unsigned int val = 0;
136	    unsigned int bitnum = (7-box) * 4;
137	    for (; s; s >>= 1, ++bitnum) {
138		if (s & 1) {
139		    val |= Pinv[bitnum];
140		}
141	    }
142	    val = (val << 3) | (val >> 29);
143	    SP[box][item] = val;
144	}
145	for (item = 0; item < 64; item += 4) {
146	    printf("\t0x%08x, 0x%08x, 0x%08x, 0x%08x,\n",
147	    SP[box][item], SP[box][item+1], SP[box][item+2], SP[box][item+3]);
148	}
149	printf("    },\n");
150    }
151}
152
153int
154main()
155{
156    makePinv();
157    makeSP();
158    return 0;
159}