Back to index

lightning-sunbird  0.9+nobinonly
mksp.c
Go to the documentation of this file.
00001 /*
00002  *  mksp.c
00003  *
00004  *  Generate SP 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 #include <stdio.h>
00043 
00044 /*
00045  * sboxes - the tables for the s-box functions
00046  *        from FIPS 46, pages 15-16.
00047  */
00048 unsigned char S[8][64] = {
00049 /* Func S1 = */ {
00050        14,  0,  4, 15, 13,  7,  1,  4,  2, 14, 15,  2, 11, 13,  8,  1,
00051         3, 10, 10,  6,  6, 12, 12, 11,  5,  9,  9,  5,  0,  3,  7,  8,
00052         4, 15,  1, 12, 14,  8,  8,  2, 13,  4,  6,  9,  2,  1, 11,  7,
00053        15,  5, 12, 11,  9,  3,  7, 14,  3, 10, 10,  0,  5,  6,  0, 13
00054     },
00055 /* Func S2 = */ {
00056        15,  3,  1, 13,  8,  4, 14,  7,  6, 15, 11,  2,  3,  8,  4, 14,
00057         9, 12,  7,  0,  2,  1, 13, 10, 12,  6,  0,  9,  5, 11, 10,  5,
00058         0, 13, 14,  8,  7, 10, 11,  1, 10,  3,  4, 15, 13,  4,  1,  2,
00059         5, 11,  8,  6, 12,  7,  6, 12,  9,  0,  3,  5,  2, 14, 15,  9
00060     },
00061 /* Func S3 = */ {
00062        10, 13,  0,  7,  9,  0, 14,  9,  6,  3,  3,  4, 15,  6,  5, 10,
00063         1,  2, 13,  8, 12,  5,  7, 14, 11, 12,  4, 11,  2, 15,  8,  1,
00064        13,  1,  6, 10,  4, 13,  9,  0,  8,  6, 15,  9,  3,  8,  0,  7,
00065        11,  4,  1, 15,  2, 14, 12,  3,  5, 11, 10,  5, 14,  2,  7, 12
00066     },
00067 /* Func S4 = */ {
00068         7, 13, 13,  8, 14, 11,  3,  5,  0,  6,  6, 15,  9,  0, 10,  3,
00069         1,  4,  2,  7,  8,  2,  5, 12, 11,  1, 12, 10,  4, 14, 15,  9,
00070        10,  3,  6, 15,  9,  0,  0,  6, 12, 10, 11,  1,  7, 13, 13,  8,
00071        15,  9,  1,  4,  3,  5, 14, 11,  5, 12,  2,  7,  8,  2,  4, 14
00072     },
00073 /* Func S5 = */ {
00074         2, 14, 12, 11,  4,  2,  1, 12,  7,  4, 10,  7, 11, 13,  6,  1,
00075         8,  5,  5,  0,  3, 15, 15, 10, 13,  3,  0,  9, 14,  8,  9,  6,
00076         4, 11,  2,  8,  1, 12, 11,  7, 10,  1, 13, 14,  7,  2,  8, 13,
00077        15,  6,  9, 15, 12,  0,  5,  9,  6, 10,  3,  4,  0,  5, 14,  3
00078     },
00079 /* Func S6 = */ {
00080        12, 10,  1, 15, 10,  4, 15,  2,  9,  7,  2, 12,  6,  9,  8,  5,
00081         0,  6, 13,  1,  3, 13,  4, 14, 14,  0,  7, 11,  5,  3, 11,  8,
00082         9,  4, 14,  3, 15,  2,  5, 12,  2,  9,  8,  5, 12, 15,  3, 10,
00083         7, 11,  0, 14,  4,  1, 10,  7,  1,  6, 13,  0, 11,  8,  6, 13
00084     },
00085 /* Func S7 = */ {
00086         4, 13, 11,  0,  2, 11, 14,  7, 15,  4,  0,  9,  8,  1, 13, 10,
00087         3, 14, 12,  3,  9,  5,  7, 12,  5,  2, 10, 15,  6,  8,  1,  6,
00088         1,  6,  4, 11, 11, 13, 13,  8, 12,  1,  3,  4,  7, 10, 14,  7,
00089        10,  9, 15,  5,  6,  0,  8, 15,  0, 14,  5,  2,  9,  3,  2, 12
00090     },
00091 /* Func S8 = */ {
00092        13,  1,  2, 15,  8, 13,  4,  8,  6, 10, 15,  3, 11,  7,  1,  4,
00093        10, 12,  9,  5,  3,  6, 14, 11,  5,  0,  0, 14, 12,  9,  7,  2,
00094         7,  2, 11,  1,  4, 14,  1,  7,  9,  4, 12, 10, 14,  8,  2, 13,
00095         0, 15,  6, 12, 10,  9, 13,  0, 15,  3,  3,  5,  5,  6,  8, 11
00096     }
00097 };
00098 
00099 /*
00100  * Permutation function for results from s-boxes
00101  *   from FIPS 46 pages 12 and 16.
00102  * P = 
00103  */
00104 unsigned char P[32] = {
00105        16,   7,  20,  21,  29,  12,  28,  17,
00106         1,  15,  23,  26,   5,  18,  31,  10,
00107         2,   8,  24,  14,  32,  27,   3,   9,
00108        19,  13,  30,   6,  22,  11,   4,  25
00109 };
00110 
00111 unsigned int Pinv[32];
00112 unsigned int SP[8][64];
00113 
00114 void
00115 makePinv(void)
00116 {
00117     int i;
00118     unsigned int Pi = 0x80000000;
00119     for (i = 0; i < 32; ++i) {
00120        int j = 32 - P[i];
00121        Pinv[j] = Pi;
00122        Pi >>= 1;
00123     }
00124 }
00125 
00126 void
00127 makeSP(void)
00128 {
00129     int box;
00130     for (box = 0; box < 8; ++box) {
00131        int item;
00132        printf("/* box S%d */ {\n", box + 1);
00133        for (item = 0; item < 64; ++item ) {
00134            unsigned int s = S[box][item];
00135            unsigned int val = 0;
00136            unsigned int bitnum = (7-box) * 4;
00137            for (; s; s >>= 1, ++bitnum) {
00138               if (s & 1) {
00139                   val |= Pinv[bitnum];
00140               }
00141            }
00142            val = (val << 3) | (val >> 29);
00143            SP[box][item] = val;
00144        }
00145        for (item = 0; item < 64; item += 4) {
00146            printf("\t0x%08x, 0x%08x, 0x%08x, 0x%08x,\n",
00147            SP[box][item], SP[box][item+1], SP[box][item+2], SP[box][item+3]);
00148        }
00149        printf("    },\n");
00150     }
00151 }
00152 
00153 int
00154 main()
00155 {
00156     makePinv();
00157     makeSP();
00158     return 0;
00159 }