security/nss/lib/freebl/mksp.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * mksp.c
michael@0 3 *
michael@0 4 * Generate SP tables for DES-150 library
michael@0 5 *
michael@0 6 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 7 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 9
michael@0 10 #include <stdio.h>
michael@0 11
michael@0 12 /*
michael@0 13 * sboxes - the tables for the s-box functions
michael@0 14 * from FIPS 46, pages 15-16.
michael@0 15 */
michael@0 16 unsigned char S[8][64] = {
michael@0 17 /* Func S1 = */ {
michael@0 18 14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
michael@0 19 3, 10, 10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
michael@0 20 4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
michael@0 21 15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13
michael@0 22 },
michael@0 23 /* Func S2 = */ {
michael@0 24 15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
michael@0 25 9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
michael@0 26 0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
michael@0 27 5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9
michael@0 28 },
michael@0 29 /* Func S3 = */ {
michael@0 30 10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
michael@0 31 1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
michael@0 32 13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
michael@0 33 11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12
michael@0 34 },
michael@0 35 /* Func S4 = */ {
michael@0 36 7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
michael@0 37 1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
michael@0 38 10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
michael@0 39 15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14
michael@0 40 },
michael@0 41 /* Func S5 = */ {
michael@0 42 2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
michael@0 43 8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
michael@0 44 4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
michael@0 45 15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3
michael@0 46 },
michael@0 47 /* Func S6 = */ {
michael@0 48 12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
michael@0 49 0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
michael@0 50 9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
michael@0 51 7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13
michael@0 52 },
michael@0 53 /* Func S7 = */ {
michael@0 54 4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
michael@0 55 3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
michael@0 56 1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
michael@0 57 10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12
michael@0 58 },
michael@0 59 /* Func S8 = */ {
michael@0 60 13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
michael@0 61 10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
michael@0 62 7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
michael@0 63 0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11
michael@0 64 }
michael@0 65 };
michael@0 66
michael@0 67 /*
michael@0 68 * Permutation function for results from s-boxes
michael@0 69 * from FIPS 46 pages 12 and 16.
michael@0 70 * P =
michael@0 71 */
michael@0 72 unsigned char P[32] = {
michael@0 73 16, 7, 20, 21, 29, 12, 28, 17,
michael@0 74 1, 15, 23, 26, 5, 18, 31, 10,
michael@0 75 2, 8, 24, 14, 32, 27, 3, 9,
michael@0 76 19, 13, 30, 6, 22, 11, 4, 25
michael@0 77 };
michael@0 78
michael@0 79 unsigned int Pinv[32];
michael@0 80 unsigned int SP[8][64];
michael@0 81
michael@0 82 void
michael@0 83 makePinv(void)
michael@0 84 {
michael@0 85 int i;
michael@0 86 unsigned int Pi = 0x80000000;
michael@0 87 for (i = 0; i < 32; ++i) {
michael@0 88 int j = 32 - P[i];
michael@0 89 Pinv[j] = Pi;
michael@0 90 Pi >>= 1;
michael@0 91 }
michael@0 92 }
michael@0 93
michael@0 94 void
michael@0 95 makeSP(void)
michael@0 96 {
michael@0 97 int box;
michael@0 98 for (box = 0; box < 8; ++box) {
michael@0 99 int item;
michael@0 100 printf("/* box S%d */ {\n", box + 1);
michael@0 101 for (item = 0; item < 64; ++item ) {
michael@0 102 unsigned int s = S[box][item];
michael@0 103 unsigned int val = 0;
michael@0 104 unsigned int bitnum = (7-box) * 4;
michael@0 105 for (; s; s >>= 1, ++bitnum) {
michael@0 106 if (s & 1) {
michael@0 107 val |= Pinv[bitnum];
michael@0 108 }
michael@0 109 }
michael@0 110 val = (val << 3) | (val >> 29);
michael@0 111 SP[box][item] = val;
michael@0 112 }
michael@0 113 for (item = 0; item < 64; item += 4) {
michael@0 114 printf("\t0x%08x, 0x%08x, 0x%08x, 0x%08x,\n",
michael@0 115 SP[box][item], SP[box][item+1], SP[box][item+2], SP[box][item+3]);
michael@0 116 }
michael@0 117 printf(" },\n");
michael@0 118 }
michael@0 119 }
michael@0 120
michael@0 121 int
michael@0 122 main()
michael@0 123 {
michael@0 124 makePinv();
michael@0 125 makeSP();
michael@0 126 return 0;
michael@0 127 }

mercurial