~ubuntu-branches/ubuntu/lucid/seamonkey/lucid-security

« back to all changes in this revision

Viewing changes to security/nss-fips/lib/freebl/rijndael_tables.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabien Tassin
  • Date: 2008-07-29 21:29:02 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080729212902-spm9kpvchp9udwbw
Tags: 1.1.11+nobinonly-0ubuntu1
* New security upstream release: 1.1.11 (LP: #218534)
  Fixes USN-602-1, USN-619-1, USN-623-1 and USN-629-1
* Refresh diverged patch:
  - update debian/patches/80_security_build.patch
* Fix FTBFS with missing -lfontconfig
  - add debian/patches/11_fix_ftbfs_with_fontconfig.patch
  - update debian/patches/series
* Build with default gcc (hardy: 4.2, intrepid: 4.3)
  - update debian/rules
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
4
 * The contents of this file are subject to the Mozilla Public License Version
 
5
 * 1.1 (the "License"); you may not use this file except in compliance with
 
6
 * the License. You may obtain a copy of the License at
 
7
 * http://www.mozilla.org/MPL/
 
8
 *
 
9
 * Software distributed under the License is distributed on an "AS IS" basis,
 
10
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
11
 * for the specific language governing rights and limitations under the
 
12
 * License.
 
13
 *
 
14
 * The Original Code is the Netscape security libraries.
 
15
 *
 
16
 * The Initial Developer of the Original Code is
 
17
 * Netscape Communications Corporation.
 
18
 * Portions created by the Initial Developer are Copyright (C) 1994-2000
 
19
 * the Initial Developer. All Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 *
 
23
 * Alternatively, the contents of this file may be used under the terms of
 
24
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
25
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
26
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
27
 * of those above. If you wish to allow use of your version of this file only
 
28
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
29
 * use your version of this file under the terms of the MPL, indicate your
 
30
 * decision by deleting the provisions above and replace them with the notice
 
31
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
32
 * the provisions above, a recipient may use your version of this file under
 
33
 * the terms of any one of the MPL, the GPL or the LGPL.
 
34
 *
 
35
 * ***** END LICENSE BLOCK ***** */
 
36
/* $Id: rijndael_tables.c,v 1.4 2004/04/27 23:04:36 gerv%gerv.net Exp $ */
 
37
 
 
38
#include "stdio.h"
 
39
#include "prtypes.h"
 
40
#include "blapi.h"
 
41
 
 
42
/*
 
43
 * what follows is code thrown together to generate the myriad of tables
 
44
 * used by Rijndael, the AES cipher.
 
45
 */
 
46
 
 
47
 
 
48
#define WORD_LE(b0, b1, b2, b3) \
 
49
    (((b3) << 24) | ((b2) << 16) | ((b1) << 8) | b0)
 
50
 
 
51
#define WORD_BE(b0, b1, b2, b3) \
 
52
    (((b0) << 24) | ((b1) << 16) | ((b2) << 8) | b3)
 
53
 
 
54
static const PRUint8 __S[256] = 
 
55
{
 
56
 99, 124, 119, 123, 242, 107, 111, 197,  48,   1, 103,  43, 254, 215, 171, 118, 
 
57
202, 130, 201, 125, 250,  89,  71, 240, 173, 212, 162, 175, 156, 164, 114, 192, 
 
58
183, 253, 147,  38,  54,  63, 247, 204,  52, 165, 229, 241, 113, 216,  49,  21, 
 
59
  4, 199,  35, 195,  24, 150,   5, 154,   7,  18, 128, 226, 235,  39, 178, 117, 
 
60
  9, 131,  44,  26,  27, 110,  90, 160,  82,  59, 214, 179,  41, 227,  47, 132, 
 
61
 83, 209,   0, 237,  32, 252, 177,  91, 106, 203, 190,  57,  74,  76,  88, 207, 
 
62
208, 239, 170, 251,  67,  77,  51, 133,  69, 249,   2, 127,  80,  60, 159, 168, 
 
63
 81, 163,  64, 143, 146, 157,  56, 245, 188, 182, 218,  33,  16, 255, 243, 210, 
 
64
205,  12,  19, 236,  95, 151,  68,  23, 196, 167, 126,  61, 100,  93,  25, 115, 
 
65
 96, 129,  79, 220,  34,  42, 144, 136,  70, 238, 184,  20, 222,  94,  11, 219, 
 
66
224,  50,  58,  10,  73,   6,  36,  92, 194, 211, 172,  98, 145, 149, 228, 121, 
 
67
231, 200,  55, 109, 141, 213,  78, 169, 108,  86, 244, 234, 101, 122, 174,   8, 
 
68
186, 120,  37,  46,  28, 166, 180, 198, 232, 221, 116,  31,  75, 189, 139, 138, 
 
69
112,  62, 181, 102,  72,   3, 246,  14,  97,  53,  87, 185, 134, 193,  29, 158, 
 
70
225, 248, 152,  17, 105, 217, 142, 148, 155,  30, 135, 233, 206,  85,  40, 223, 
 
71
140, 161, 137,  13, 191, 230,  66, 104,  65, 153,  45,  15, 176,  84, 187,  22, 
 
72
};
 
73
 
 
74
static const PRUint8 __SInv[256] = 
 
75
{
 
76
 82,   9, 106, 213,  48,  54, 165,  56, 191,  64, 163, 158, 129, 243, 215, 251, 
 
77
124, 227,  57, 130, 155,  47, 255, 135,  52, 142,  67,  68, 196, 222, 233, 203, 
 
78
 84, 123, 148,  50, 166, 194,  35,  61, 238,  76, 149,  11,  66, 250, 195,  78, 
 
79
  8,  46, 161, 102,  40, 217,  36, 178, 118,  91, 162,  73, 109, 139, 209,  37, 
 
80
114, 248, 246, 100, 134, 104, 152,  22, 212, 164,  92, 204,  93, 101, 182, 146, 
 
81
108, 112,  72,  80, 253, 237, 185, 218,  94,  21,  70,  87, 167, 141, 157, 132, 
 
82
144, 216, 171,   0, 140, 188, 211,  10, 247, 228,  88,   5, 184, 179,  69,   6, 
 
83
208,  44,  30, 143, 202,  63,  15,   2, 193, 175, 189,   3,   1,  19, 138, 107, 
 
84
 58, 145,  17,  65,  79, 103, 220, 234, 151, 242, 207, 206, 240, 180, 230, 115, 
 
85
150, 172, 116,  34, 231, 173,  53, 133, 226, 249,  55, 232,  28, 117, 223, 110, 
 
86
 71, 241,  26, 113,  29,  41, 197, 137, 111, 183,  98,  14, 170,  24, 190,  27, 
 
87
252,  86,  62,  75, 198, 210, 121,  32, 154, 219, 192, 254, 120, 205,  90, 244, 
 
88
 31, 221, 168,  51, 136,   7, 199,  49, 177,  18,  16,  89,  39, 128, 236,  95, 
 
89
 96,  81, 127, 169,  25, 181,  74,  13,  45, 229, 122, 159, 147, 201, 156, 239, 
 
90
160, 224,  59,  77, 174,  42, 245, 176, 200, 235, 187,  60, 131,  83, 153,  97, 
 
91
 23,  43,   4, 126, 186, 119, 214,  38, 225, 105,  20,  99,  85,  33,  12, 125
 
92
};
 
93
 
 
94
/* GF_MULTIPLY
 
95
 *
 
96
 * multiply two bytes represented in GF(2**8), mod (x**4 + 1)
 
97
 */
 
98
PRUint8 gf_multiply(PRUint8 a, PRUint8 b)
 
99
{
 
100
    PRUint8 res = 0;
 
101
    while (b > 0) {
 
102
        res = (b & 0x01) ? res ^ a : res;
 
103
        a = (a & 0x80) ? ((a << 1) ^ 0x1b) : (a << 1);
 
104
        b >>= 1;
 
105
    }
 
106
    return res;
 
107
}
 
108
 
 
109
void
 
110
make_T_Table(char *table, const PRUint8 Sx[256], FILE *file,
 
111
             unsigned char m0, unsigned char m1, 
 
112
             unsigned char m2, unsigned char m3)
 
113
{
 
114
    PRUint32 Ti;
 
115
    int i;
 
116
    fprintf(file, "#ifdef IS_LITTLE_ENDIAN\n");
 
117
    fprintf(file, "static const PRUint32 _T%s[256] = \n{\n", table);
 
118
    for (i=0; i<256; i++) {
 
119
        Ti = WORD_LE( gf_multiply(Sx[i], m0),
 
120
                      gf_multiply(Sx[i], m1),
 
121
                      gf_multiply(Sx[i], m2),
 
122
                      gf_multiply(Sx[i], m3) );
 
123
        if (Ti == 0)
 
124
            fprintf(file, "0x00000000%c%c", (i==255)?' ':',',
 
125
                                            (i%6==5)?'\n':' ');
 
126
        else
 
127
            fprintf(file, "%#.8x%c%c", Ti, (i==255)?' ':',',
 
128
                                           (i%6==5)?'\n':' ');
 
129
    }
 
130
    fprintf(file, "\n};\n");
 
131
    fprintf(file, "#else\n");
 
132
    fprintf(file, "static const PRUint32 _T%s[256] = \n{\n", table);
 
133
    for (i=0; i<256; i++) {
 
134
        Ti = WORD_BE( gf_multiply(Sx[i], m0),
 
135
                      gf_multiply(Sx[i], m1),
 
136
                      gf_multiply(Sx[i], m2),
 
137
                      gf_multiply(Sx[i], m3) );
 
138
        if (Ti == 0)
 
139
            fprintf(file, "0x00000000%c%c", (i==255)?' ':',',
 
140
                                            (i%6==5)?'\n':' ');
 
141
        else
 
142
            fprintf(file, "%#.8x%c%c", Ti, (i==255)?' ':',',
 
143
                                           (i%6==5)?'\n':' ');
 
144
    }
 
145
    fprintf(file, "\n};\n");
 
146
    fprintf(file, "#endif\n\n");
 
147
}
 
148
 
 
149
void make_InvMixCol_Table(int num, FILE *file, PRUint8 m0, PRUint8 m1, PRUint8 m2, PRUint8 m3)
 
150
{
 
151
    PRUint16 i;
 
152
    PRUint8 b0, b1, b2, b3;
 
153
    fprintf(file, "#ifdef IS_LITTLE_ENDIAN\n");
 
154
    fprintf(file, "static const PRUint32 _IMXC%d[256] = \n{\n", num);
 
155
    for (i=0; i<256; i++) {
 
156
        b0 = gf_multiply(i, m0);
 
157
        b1 = gf_multiply(i, m1);
 
158
        b2 = gf_multiply(i, m2);
 
159
        b3 = gf_multiply(i, m3);
 
160
        fprintf(file, "0x%.2x%.2x%.2x%.2x%c%c", b3, b2, b1, b0, (i==255)?' ':',', (i%6==5)?'\n':' ');
 
161
    }
 
162
    fprintf(file, "\n};\n");
 
163
    fprintf(file, "#else\n");
 
164
    fprintf(file, "static const PRUint32 _IMXC%d[256] = \n{\n", num);
 
165
    for (i=0; i<256; i++) {
 
166
        b0 = gf_multiply(i, m0);
 
167
        b1 = gf_multiply(i, m1);
 
168
        b2 = gf_multiply(i, m2);
 
169
        b3 = gf_multiply(i, m3);
 
170
        fprintf(file, "0x%.2x%.2x%.2x%.2x%c%c", b0, b1, b2, b3, (i==255)?' ':',', (i%6==5)?'\n':' ');
 
171
    }
 
172
    fprintf(file, "\n};\n");
 
173
    fprintf(file, "#endif\n\n");
 
174
}
 
175
 
 
176
int main()
 
177
{
 
178
    int i, j;
 
179
    PRUint8 cur, last;
 
180
    PRUint32 tmp;
 
181
    FILE *optfile;
 
182
    optfile = fopen("rijndael32.tab", "w");
 
183
    /* output S, if there are no T tables */
 
184
    fprintf(optfile, "#ifndef RIJNDAEL_INCLUDE_TABLES\n");
 
185
    fprintf(optfile, "static const PRUint8 _S[256] = \n{\n");
 
186
    for (i=0; i<256; i++) {
 
187
        fprintf(optfile, "%3d%c%c", __S[i],(i==255)?' ':',', 
 
188
                                    (i%16==15)?'\n':' ');
 
189
    }
 
190
    fprintf(optfile, "};\n#endif /* not RIJNDAEL_INCLUDE_TABLES */\n\n");
 
191
    /* output S**-1 */
 
192
    fprintf(optfile, "static const PRUint8 _SInv[256] = \n{\n");
 
193
    for (i=0; i<256; i++) {
 
194
        fprintf(optfile, "%3d%c%c", __SInv[i],(i==255)?' ':',', 
 
195
                                    (i%16==15)?'\n':' ');
 
196
    }
 
197
    fprintf(optfile, "};\n\n");
 
198
    fprintf(optfile, "#ifdef RIJNDAEL_INCLUDE_TABLES\n");
 
199
    /* The 32-bit word tables for optimized implementation */
 
200
    /* T0 = [ S[a] * 02, S[a], S[a], S[a] * 03 ] */
 
201
    make_T_Table("0", __S, optfile, 0x02, 0x01, 0x01, 0x03);
 
202
    /* T1 = [ S[a] * 03, S[a] * 02, S[a], S[a] ] */
 
203
    make_T_Table("1", __S, optfile, 0x03, 0x02, 0x01, 0x01);
 
204
    /* T2 = [ S[a], S[a] * 03, S[a] * 02, S[a] ] */
 
205
    make_T_Table("2", __S, optfile, 0x01, 0x03, 0x02, 0x01);
 
206
    /* T3 = [ S[a], S[a], S[a] * 03, S[a] * 02 ] */
 
207
    make_T_Table("3", __S, optfile, 0x01, 0x01, 0x03, 0x02);
 
208
    /* TInv0 = [ Si[a] * 0E, Si[a] * 09, Si[a] * 0D, Si[a] * 0B ] */
 
209
    make_T_Table("Inv0", __SInv, optfile, 0x0e, 0x09, 0x0d, 0x0b);
 
210
    /* TInv1 = [ Si[a] * 0B, Si[a] * 0E, Si[a] * 09, Si[a] * 0D ] */
 
211
    make_T_Table("Inv1", __SInv, optfile, 0x0b, 0x0e, 0x09, 0x0d);
 
212
    /* TInv2 = [ Si[a] * 0D, Si[a] * 0B, Si[a] * 0E, Si[a] * 09 ] */
 
213
    make_T_Table("Inv2", __SInv, optfile, 0x0d, 0x0b, 0x0e, 0x09);
 
214
    /* TInv3 = [ Si[a] * 09, Si[a] * 0D, Si[a] * 0B, Si[a] * 0E ] */
 
215
    make_T_Table("Inv3", __SInv, optfile, 0x09, 0x0d, 0x0b, 0x0e);
 
216
    /* byte multiply tables for inverse key expansion (mimics InvMixColumn) */
 
217
    make_InvMixCol_Table(0, optfile, 0x0e, 0x09, 0x0d, 0x0b);
 
218
    make_InvMixCol_Table(1, optfile, 0x0b, 0x0E, 0x09, 0x0d);
 
219
    make_InvMixCol_Table(2, optfile, 0x0d, 0x0b, 0x0e, 0x09);
 
220
    make_InvMixCol_Table(3, optfile, 0x09, 0x0d, 0x0b, 0x0e);
 
221
    fprintf(optfile, "#endif /* RIJNDAEL_INCLUDE_TABLES */\n\n");
 
222
    /* round constants for key expansion */
 
223
    fprintf(optfile, "#ifdef IS_LITTLE_ENDIAN\n");
 
224
    fprintf(optfile, "static const PRUint32 Rcon[30] = {\n");
 
225
    cur = 0x01;
 
226
    for (i=0; i<30; i++) {
 
227
        fprintf(optfile, "%#.8x%c%c", WORD_LE(cur, 0, 0, 0), 
 
228
                                        (i==29)?' ':',', (i%6==5)?'\n':' ');
 
229
        last = cur;
 
230
        cur = gf_multiply(last, 0x02);
 
231
    }
 
232
    fprintf(optfile, "};\n");
 
233
    fprintf(optfile, "#else\n");
 
234
    fprintf(optfile, "static const PRUint32 Rcon[30] = {\n");
 
235
    cur = 0x01;
 
236
    for (i=0; i<30; i++) {
 
237
        fprintf(optfile, "%#.8x%c%c", WORD_BE(cur, 0, 0, 0), 
 
238
                                        (i==29)?' ':',', (i%6==5)?'\n':' ');
 
239
        last = cur;
 
240
        cur = gf_multiply(last, 0x02);
 
241
    }
 
242
    fprintf(optfile, "};\n");
 
243
    fprintf(optfile, "#endif\n\n");
 
244
    fclose(optfile);
 
245
    return 0;
 
246
}