3 * $Id: rijndael-mktab.c,v 1.2 2000/06/18 23:12:15 mdw Exp $
5 * Build precomputed tables for the Rijndael block cipher
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: rijndael-mktab.c,v $
33 * Revision 1.2 2000/06/18 23:12:15 mdw
34 * Change typesetting of Galois Field names.
36 * Revision 1.1 2000/06/17 11:56:07 mdw
41 /*----- Header files ------------------------------------------------------*/
47 #include <mLib/bits.h>
49 /*----- Magic variables ---------------------------------------------------*/
51 static octet s[256], si[256];
52 static uint32 t[4][256], ti[4][256];
53 static uint32 u[4][256];
56 /*----- Main code ---------------------------------------------------------*/
60 * Arguments: @unsigned x, y@ = polynomials over %$\gf{2^8}$%
61 * @unsigned m@ = modulus
63 * Returns: The product of two polynomials.
65 * Use: Computes a product of polynomials, quite slowly.
68 static unsigned mul(unsigned x, unsigned y, unsigned m)
73 for (i = 0; i < 8; i++) {
89 * This is built from inversion in the multiplicative group of
90 * %$\gf{2^8}[x]/(p(x))$%, where %$p(x) = x^8 + x^4 + x^3 + x + 1$%, followed
91 * by an affine transformation treating inputs as vectors over %$\gf{2}$%.
92 * The result is a horrible function.
94 * The inversion is done slightly sneakily, by building log and antilog
95 * tables. Let %$a$% be an element of the finite field. If the inverse of
96 * %$a$% is %$a^{-1}$%, then %$\log a a^{-1} = 0$%. Hence
97 * %$\log a = -\log a^{-1}$%. This saves fiddling about with Euclidean
103 static void sbox(void)
105 octet log[256], alog[256];
110 /* --- Find a suitable generator, and build log tables --- */
113 for (g = 2; g < 256; g++) {
115 for (i = 0; i < 256; i++) {
118 x = mul(x, g, S_MOD);
119 if (x == 1 && i != 254)
125 fprintf(stderr, "couldn't find generator\n");
129 /* --- Now grind through and do the affine transform --- *
131 * The matrix multiply is an AND and a parity op. The add is an XOR.
134 for (i = 0; i < 256; i++) {
137 unsigned v = i ? alog[255 - log[i]] : 0;
139 assert(i == 0 || mul(i, v, S_MOD) == 1);
142 for (j = 0; j < 8; j++) {
148 x = (x << 1) | (r & 1);
159 * Construct the t tables for doing the round function efficiently.
162 static void tbox(void)
166 for (i = 0; i < 256; i++) {
170 /* --- Build a forwards t-box entry --- */
173 b = a << 1; if (b & 0x100) b ^= S_MOD;
175 w = (b << 0) | (a << 8) | (a << 16) | (c << 24);
177 t[1][i] = ROL32(w, 8);
178 t[2][i] = ROL32(w, 16);
179 t[3][i] = ROL32(w, 24);
181 /* --- Build a backwards t-box entry --- */
183 a = mul(si[i], 0x0e, S_MOD);
184 b = mul(si[i], 0x09, S_MOD);
185 c = mul(si[i], 0x0d, S_MOD);
186 d = mul(si[i], 0x0b, S_MOD);
187 w = (a << 0) | (b << 8) | (c << 16) | (d << 24);
189 ti[1][i] = ROL32(w, 8);
190 ti[2][i] = ROL32(w, 16);
191 ti[3][i] = ROL32(w, 24);
197 * Construct the tables for performing the decryption key schedule.
200 static void ubox(void)
204 for (i = 0; i < 256; i++) {
207 a = mul(i, 0x0e, S_MOD);
208 b = mul(i, 0x09, S_MOD);
209 c = mul(i, 0x0d, S_MOD);
210 d = mul(i, 0x0b, S_MOD);
211 w = (a << 0) | (b << 8) | (c << 16) | (d << 24);
213 u[1][i] = ROL32(w, 8);
214 u[2][i] = ROL32(w, 16);
215 u[3][i] = ROL32(w, 24);
219 /* --- Round constants --- */
226 for (i = 0; i < sizeof(rc); i++) {
243 * Rijndael tables [generated]\n\
246 #ifndef CATACOMB_RIJNDAEL_TAB_H\n\
247 #define CATACOMB_RIJNDAEL_TAB_H\n\
250 /* --- Write out the S-box --- */
254 /* --- The byte substitution and its inverse --- */\n\
256 #define RIJNDAEL_S { \\\n\
258 for (i = 0; i < 256; i++) {
259 printf("0x%02x", s[i]);
261 fputs(" \\\n}\n\n", stdout);
263 fputs(", \\\n ", stdout);
269 #define RIJNDAEL_SI { \\\n\
271 for (i = 0; i < 256; i++) {
272 printf("0x%02x", si[i]);
274 fputs(" \\\n}\n\n", stdout);
276 fputs(", \\\n ", stdout);
281 /* --- Write out the big t tables --- */
285 /* --- The big round tables --- */\n\
287 #define RIJNDAEL_T { \\\n\
289 for (j = 0; j < 4; j++) {
290 for (i = 0; i < 256; i++) {
291 printf("0x%08x", t[j][i]);
294 fputs(" } \\\n}\n\n", stdout);
299 } else if (i % 4 == 3)
300 fputs(", \\\n ", stdout);
307 #define RIJNDAEL_TI { \\\n\
309 for (j = 0; j < 4; j++) {
310 for (i = 0; i < 256; i++) {
311 printf("0x%08x", ti[j][i]);
314 fputs(" } \\\n}\n\n", stdout);
319 } else if (i % 4 == 3)
320 fputs(", \\\n ", stdout);
326 /* --- Write out the big u tables --- */
330 /* --- The decryption key schedule tables --- */\n\
332 #define RIJNDAEL_U { \\\n\
334 for (j = 0; j < 4; j++) {
335 for (i = 0; i < 256; i++) {
336 printf("0x%08x", u[j][i]);
339 fputs(" } \\\n}\n\n", stdout);
344 } else if (i % 4 == 3)
345 fputs(", \\\n ", stdout);
351 /* --- Round constants --- */
355 /* --- The round constants --- */\n\
357 #define RIJNDAEL_RCON { \\\n\
359 for (i = 0; i < sizeof(rc); i++) {
360 printf("0x%02x", rc[i]);
361 if (i == sizeof(rc) - 1)
362 fputs(" \\\n}\n\n", stdout);
364 fputs(", \\\n ", stdout);
373 if (fclose(stdout)) {
374 fprintf(stderr, "error writing data\n");
381 /*----- That's all, folks -------------------------------------------------*/