3 * $Id: twofish-mktab.c,v 1.2 2000/06/18 23:12:15 mdw Exp $
5 * Build constant tables for Twofish
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: twofish-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 12:10:17 mdw
41 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/bits.h>
48 /*----- Data structures ---------------------------------------------------*/
50 typedef struct { octet t[4][16]; } t_tab;
51 typedef struct { octet q[256]; } q_tab;
53 /*----- Various Twofish tables --------------------------------------------*/
55 /* --- The t-tables --- */
57 static const t_tab qt0 = {{
58 { 0x8, 0x1, 0x7, 0xd, 0x6, 0xf, 0x3, 0x2,
59 0x0, 0xb, 0x5, 0x9, 0xe, 0xc, 0xa, 0x4 },
60 { 0xe, 0xc, 0xb, 0x8, 0x1, 0x2, 0x3, 0x5,
61 0xf, 0x4, 0xa, 0x6, 0x7, 0x0, 0x9, 0xd },
62 { 0xb, 0xa, 0x5, 0xe, 0x6, 0xd, 0x9, 0x0,
63 0xc, 0x8, 0xf, 0x3, 0x2, 0x4, 0x7, 0x1 },
64 { 0xd, 0x7, 0xf, 0x4, 0x1, 0x2, 0x6, 0xe,
65 0x9, 0xb, 0x3, 0x0, 0x8, 0x5, 0xc, 0xa }
68 static const t_tab qt1 = {{
69 { 0x2, 0x8, 0xb, 0xd, 0xf, 0x7, 0x6, 0xe,
70 0x3, 0x1, 0x9, 0x4, 0x0, 0xa, 0xc, 0x5 },
71 { 0x1, 0xe, 0x2, 0xb, 0x4, 0xc, 0x3, 0x7,
72 0x6, 0xd, 0xa, 0x5, 0xf, 0x9, 0x0, 0x8 },
73 { 0x4, 0xc, 0x7, 0x5, 0x1, 0x6, 0x9, 0xa,
74 0x0, 0xe, 0xd, 0x8, 0x2, 0xb, 0x3, 0xf },
75 { 0xb, 0x9, 0x5, 0x1, 0xc, 0x3, 0xd, 0xe,
76 0x6, 0x4, 0x7, 0xf, 0x2, 0x0, 0x8, 0xa }
81 /* --- The MDS and Reed-Solomon matrices --- */
83 static const octet mds[16] = {
84 0x01, 0xef, 0x5b, 0x5b,
85 0x5b, 0xef, 0xef, 0x01,
86 0xef, 0x5b, 0x01, 0xef,
87 0xef, 0x01, 0xef, 0x5b
90 static const octet rs[32] = {
91 0x01, 0xa4, 0x55, 0x87, 0x5a, 0x58, 0xdb, 0x9e,
92 0xa4, 0x56, 0x82, 0xf3, 0x1e, 0xc6, 0x68, 0xe5,
93 0x02, 0xa1, 0xfc, 0xc1, 0x47, 0xae, 0x3d, 0x19,
94 0xa4, 0x55, 0x87, 0x5a, 0x58, 0xdb, 0x9e, 0x03
97 /*----- Magic macros ------------------------------------------------------*/
99 #define ROR4(x) ((((x) >> 1) | ((x) << 3)) & 15)
101 /*----- Building and printing @q@ tables ----------------------------------*/
105 * Arguments: @q_tab *q@ = pointer to output @q@ table
106 * @const t_tab *t@ = pointer to input @t@ table
107 * @const char *name@ = name of @q@ table
111 * Use: Constructs a 256-entry @q@-table.
114 static void mkq(q_tab *q, const t_tab *t, const char *name)
119 /* --- Ensure the t-table is well-formed --- */
121 for (i = 0; i < 4; i++) {
125 for (j = 0; j < 16; j++) {
127 fprintf(stderr, "duplicate %i in %s[%i] (col %i and %i)\n",
128 t->t[i][j], name, i, j, f[t->t[i][j]]);
138 /* --- Construct the @q@ table --- */
140 for (i = 0; i < 256; i++) {
141 int a = i >> 4, b = i & 15;
142 int aa = t->t[0][a ^ b], bb = t->t[1][a ^ ((a << 3) & 15) ^ ROR4(b)];
143 a = t->t[2][aa ^ bb], b = t->t[3][aa ^ ((aa << 3) & 15) ^ ROR4(bb)];
144 q->q[i] = a | (b << 4);
147 /* Consider testing @q@ for linear and differential properties here */
150 /* --- @printq@ --- *
152 * Arguments: @const q_tab *t@ = pointer to table
153 * @const char *name@ = pointer to table name
157 * Use: Prints a q table.
160 static void printq(const q_tab *q, const char *name)
166 #define TWOFISH_%s { \\\n\
169 for (i = 0; i < 256; i++) {
170 printf("0x%02x", q->q[i]);
173 fputs(" \\\n}\n\n", stdout);
175 fputs(", \\\n ", stdout);
181 /*----- %$\gf{2^8}$% arithmetic -------------------------------------------*/
183 #define MDS_MOD 0x169
188 * Arguments: @unsigned x, y@ = polynomials over %$\gf{2^8}$%
189 * @unsigned m@ = modulus
191 * Returns: The product of two polynomials.
193 * Use: Computes a product of polynomials, quite slowly.
196 static unsigned mul(unsigned x, unsigned y, unsigned m)
201 for (i = 0; i < 8; i++) {
215 * Arguments: @octet *d@ = destination vector
216 * @const octet *p@ = matrix of bytes
217 * @const octet *q@ = vector from somewhere else
218 * @size_t r@ = size of destination or number of rows in matrix
219 * @size_t n@ = length of row and vector
220 * @unsigned m@ = modulus polynomial
224 * Use: Computes an inner product of matrices over the finite field
225 * %$\gf{2^8}[x]/(m(x))$%. This isn't particularly rapid.
228 static void mmul(octet *d, const octet *p, const octet *q,
229 size_t r, size_t n, unsigned m)
236 for (i = 0; i < n; i++)
237 a ^= mul(*p++, *qq++, m);
249 * Use: Prints the MDS/q table.
252 static void qmds(void)
256 static const q_tab *q[4] = { &q1, &q0, &q1, &q0 };
258 for (i = 0; i < 4; i++) {
259 octet in[4] = { 0, 0, 0, 0 };
262 for (j = 0; j < 256; j++) {
264 mmul(out, mds, in, 4, 4, MDS_MOD);
265 t[i][j] = LOAD32_L(out);
270 /* --- Expanded MDS tables --- *\n\
272 * The table contains output vectors for computing the result of pushing\n\
273 * bytes through appropriate @q@ tables and the MDS matrix.\n\
276 #define TWOFISH_QMDS { \\");
277 for (i = 0; i < 4; i++) {
278 fputs(" { ", stdout);
279 for (j = 0; j < 256; j++) {
280 printf("0x%08lx", (unsigned long)t[i][j]);
287 } else if (j % 4 == 3)
288 fputs(", \\\n ", stdout);
303 * Use: Produces the log and antilog tables for doing the RS
304 * arithmetic efficiently.
307 static void rslog(void)
316 for (i = 0; i < 256; i++) {
325 for (i = 0; i < 32; i++) {
326 if (rslog[rs[i]] > x)
331 /* --- Reed-Solomon log tables --- *\n\
333 * The Reed-Solomon multiplies are accelerated by using log tables.\n\
336 #define TWOFISH_RSLOG { \\\n\
339 for (i = 0; i < 256; i++) {
340 printf("0x%02x", rslog[i]);
344 fputs(", \\\n ", stdout);
350 #define TWOFISH_RSEXP { \\\n\
353 for (i = 0; i < 255 + x + 1; i++) {
354 printf("0x%02x", rsexp[i % 255]);
358 fputs(", \\\n ", stdout);
364 /* --- Reed-Solomon matrix with log entries --- */\n\
366 #define TWOFISH_RS { \\\n\
369 for (i = 0; i < 32; i++) {
370 printf("0x%02x", rslog[rs[i]]);
374 fputs(", \\\n ", stdout);
380 /*----- Main program ------------------------------------------------------*/
389 * Twofish q tables [generated]\n\
392 #ifndef CATACOMB_TWOFISH_TAB_H
393 #define CATACOMB_TWOFISH_TAB_H
397 /* --- The q tables --- */
400 /* --- Precomputed @q@ tables --- */\n\
402 mkq(&q0, &qt0, "qt0");
403 mkq(&q1, &qt1, "qt1");
407 /* --- The MDS/q tables --- */
416 if (fclose(stdout)) {
417 fprintf(stderr, "error writing data\n");
424 /*----- That's all, folks -------------------------------------------------*/