3 * $Id: square-mktab.c,v 1.2 2000/08/04 18:03:19 mdw Exp $
5 * Build precomputed tables for the Square 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: square-mktab.c,v $
33 * Revision 1.2 2000/08/04 18:03:19 mdw
34 * Fix comment describing the field in which inversion is done.
36 * Revision 1.1 2000/07/27 18:10:27 mdw
37 * Build precomuted tables for Square.
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^7+x^6+x^5+x^4+x^2+1$%,
91 * followed by an affine transformation treating inputs as vectors over
92 * %$\gf{2}$%. 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
++) {
136 octet m
[] = { 0xd6, 0x7b, 0x3d, 0x1f, 0x0f, 0x05, 0x03, 0x01 };
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);
158 * Construct the t tables for doing the round function efficiently.
161 static void tbox(void)
165 for (i
= 0; i
< 256; i
++) {
169 /* --- Build a forwards t-box entry --- */
172 b
= a
<< 1; if (b
& 0x100) b
^= S_MOD
;
174 w
= (b
<< 0) | (a
<< 8) | (a
<< 16) | (c
<< 24);
176 t
[1][i
] = ROL32(w
, 8);
177 t
[2][i
] = ROL32(w
, 16);
178 t
[3][i
] = ROL32(w
, 24);
180 /* --- Build a backwards t-box entry --- */
182 a
= mul(si
[i
], 0x0e, S_MOD
);
183 b
= mul(si
[i
], 0x09, S_MOD
);
184 c
= mul(si
[i
], 0x0d, S_MOD
);
185 d
= mul(si
[i
], 0x0b, S_MOD
);
186 w
= (a
<< 0) | (b
<< 8) | (c
<< 16) | (d
<< 24);
188 ti
[1][i
] = ROL32(w
, 8);
189 ti
[2][i
] = ROL32(w
, 16);
190 ti
[3][i
] = ROL32(w
, 24);
196 * Construct the tables for performing the key schedule.
199 static void ubox(void)
203 for (i
= 0; i
< 256; i
++) {
207 b
= a
<< 1; if (b
& 0x100) b
^= S_MOD
;
209 w
= (b
<< 0) | (a
<< 8) | (a
<< 16) | (c
<< 24);
211 u
[1][i
] = ROL32(w
, 8);
212 u
[2][i
] = ROL32(w
, 16);
213 u
[3][i
] = ROL32(w
, 24);
217 /* --- Round constants --- */
224 for (i
= 0; i
< sizeof(rc
); i
++) {
241 * Square tables [generated]\n\
244 #ifndef CATACOMB_SQUARE_TAB_H\n\
245 #define CATACOMB_SQUARE_TAB_H\n\
248 /* --- Write out the S-box --- */
252 /* --- The byte substitution and its inverse --- */\n\
254 #define SQUARE_S { \\\n\
256 for (i
= 0; i
< 256; i
++) {
257 printf("0x%02x", s
[i
]);
259 fputs(" \\\n}\n\n", stdout
);
261 fputs(", \\\n ", stdout
);
267 #define SQUARE_SI { \\\n\
269 for (i
= 0; i
< 256; i
++) {
270 printf("0x%02x", si
[i
]);
272 fputs(" \\\n}\n\n", stdout
);
274 fputs(", \\\n ", stdout
);
279 /* --- Write out the big t tables --- */
283 /* --- The big round tables --- */\n\
285 #define SQUARE_T { \\\n\
287 for (j
= 0; j
< 4; j
++) {
288 for (i
= 0; i
< 256; i
++) {
289 printf("0x%08x", t
[j
][i
]);
292 fputs(" } \\\n}\n\n", stdout
);
297 } else if (i
% 4 == 3)
298 fputs(", \\\n ", stdout
);
305 #define SQUARE_TI { \\\n\
307 for (j
= 0; j
< 4; j
++) {
308 for (i
= 0; i
< 256; i
++) {
309 printf("0x%08x", ti
[j
][i
]);
312 fputs(" } \\\n}\n\n", stdout
);
317 } else if (i
% 4 == 3)
318 fputs(", \\\n ", stdout
);
324 /* --- Write out the big u tables --- */
328 /* --- The key schedule tables --- */\n\
330 #define SQUARE_U { \\\n\
332 for (j
= 0; j
< 4; j
++) {
333 for (i
= 0; i
< 256; i
++) {
334 printf("0x%08x", u
[j
][i
]);
337 fputs(" } \\\n}\n\n", stdout
);
342 } else if (i
% 4 == 3)
343 fputs(", \\\n ", stdout
);
349 /* --- Round constants --- */
353 /* --- The round constants --- */\n\
355 #define SQUARE_RCON { \\\n\
357 for (i
= 0; i
< sizeof(rc
); i
++) {
358 printf("0x%02x", rc
[i
]);
359 if (i
== sizeof(rc
) - 1)
360 fputs(" \\\n}\n\n", stdout
);
362 fputs(", \\\n ", stdout
);
371 if (fclose(stdout
)) {
372 fprintf(stderr
, "error writing data\n");
379 /*----- That's all, folks -------------------------------------------------*/