3 * Build constant tables for Twofish
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
33 #include <mLib/bits.h>
35 /*----- Data structures ---------------------------------------------------*/
37 typedef struct { octet t
[4][16]; } t_tab
;
38 typedef struct { octet q
[256]; } q_tab
;
40 /*----- Various Twofish tables --------------------------------------------*/
42 /* --- The t-tables --- */
44 static const t_tab qt0
= {{
45 { 0x8, 0x1, 0x7, 0xd, 0x6, 0xf, 0x3, 0x2,
46 0x0, 0xb, 0x5, 0x9, 0xe, 0xc, 0xa, 0x4 },
47 { 0xe, 0xc, 0xb, 0x8, 0x1, 0x2, 0x3, 0x5,
48 0xf, 0x4, 0xa, 0x6, 0x7, 0x0, 0x9, 0xd },
49 { 0xb, 0xa, 0x5, 0xe, 0x6, 0xd, 0x9, 0x0,
50 0xc, 0x8, 0xf, 0x3, 0x2, 0x4, 0x7, 0x1 },
51 { 0xd, 0x7, 0xf, 0x4, 0x1, 0x2, 0x6, 0xe,
52 0x9, 0xb, 0x3, 0x0, 0x8, 0x5, 0xc, 0xa }
55 static const t_tab qt1
= {{
56 { 0x2, 0x8, 0xb, 0xd, 0xf, 0x7, 0x6, 0xe,
57 0x3, 0x1, 0x9, 0x4, 0x0, 0xa, 0xc, 0x5 },
58 { 0x1, 0xe, 0x2, 0xb, 0x4, 0xc, 0x3, 0x7,
59 0x6, 0xd, 0xa, 0x5, 0xf, 0x9, 0x0, 0x8 },
60 { 0x4, 0xc, 0x7, 0x5, 0x1, 0x6, 0x9, 0xa,
61 0x0, 0xe, 0xd, 0x8, 0x2, 0xb, 0x3, 0xf },
62 { 0xb, 0x9, 0x5, 0x1, 0xc, 0x3, 0xd, 0xe,
63 0x6, 0x4, 0x7, 0xf, 0x2, 0x0, 0x8, 0xa }
68 /* --- The MDS and Reed-Solomon matrices --- */
70 static const octet mds
[16] = {
71 0x01, 0xef, 0x5b, 0x5b,
72 0x5b, 0xef, 0xef, 0x01,
73 0xef, 0x5b, 0x01, 0xef,
74 0xef, 0x01, 0xef, 0x5b
77 static const octet rs
[32] = {
78 0x01, 0xa4, 0x55, 0x87, 0x5a, 0x58, 0xdb, 0x9e,
79 0xa4, 0x56, 0x82, 0xf3, 0x1e, 0xc6, 0x68, 0xe5,
80 0x02, 0xa1, 0xfc, 0xc1, 0x47, 0xae, 0x3d, 0x19,
81 0xa4, 0x55, 0x87, 0x5a, 0x58, 0xdb, 0x9e, 0x03
84 /*----- Magic macros ------------------------------------------------------*/
86 #define ROR4(x) ((((x) >> 1) | ((x) << 3)) & 15)
88 /*----- Building and printing @q@ tables ----------------------------------*/
92 * Arguments: @q_tab *q@ = pointer to output @q@ table
93 * @const t_tab *t@ = pointer to input @t@ table
94 * @const char *name@ = name of @q@ table
98 * Use: Constructs a 256-entry @q@-table.
101 static void mkq(q_tab
*q
, const t_tab
*t
, const char *name
)
106 /* --- Ensure the t-table is well-formed --- */
108 for (i
= 0; i
< 4; i
++) {
112 for (j
= 0; j
< 16; j
++) {
114 fprintf(stderr
, "duplicate %i in %s[%i] (col %i and %i)\n",
115 t
->t
[i
][j
], name
, i
, j
, f
[t
->t
[i
][j
]]);
125 /* --- Construct the @q@ table --- */
127 for (i
= 0; i
< 256; i
++) {
128 int a
= i
>> 4, b
= i
& 15;
129 int aa
= t
->t
[0][a
^ b
], bb
= t
->t
[1][a
^ ((a
<< 3) & 15) ^ ROR4(b
)];
130 a
= t
->t
[2][aa
^ bb
], b
= t
->t
[3][aa
^ ((aa
<< 3) & 15) ^ ROR4(bb
)];
131 q
->q
[i
] = a
| (b
<< 4);
134 /* Consider testing @q@ for linear and differential properties here */
137 /* --- @printq@ --- *
139 * Arguments: @const q_tab *t@ = pointer to table
140 * @const char *name@ = pointer to table name
144 * Use: Prints a q table.
147 static void printq(const q_tab
*q
, const char *name
)
153 const octet twofish_%s[256] = {\n\
156 for (i
= 0; i
< 256; i
++) {
157 printf("0x%02x", q
->q
[i
]);
160 fputs("\n};\n\n", stdout
);
162 fputs(",\n ", stdout
);
168 /*----- %$\gf{2^8}$% arithmetic -------------------------------------------*/
170 #define MDS_MOD 0x169
175 * Arguments: @unsigned x, y@ = polynomials over %$\gf{2^8}$%
176 * @unsigned m@ = modulus
178 * Returns: The product of two polynomials.
180 * Use: Computes a product of polynomials, quite slowly.
183 static unsigned mul(unsigned x
, unsigned y
, unsigned m
)
188 for (i
= 0; i
< 8; i
++) {
202 * Arguments: @octet *d@ = destination vector
203 * @const octet *p@ = matrix of bytes
204 * @const octet *q@ = vector from somewhere else
205 * @size_t r@ = size of destination or number of rows in matrix
206 * @size_t n@ = length of row and vector
207 * @unsigned m@ = modulus polynomial
211 * Use: Computes an inner product of matrices over the finite field
212 * %$\gf{2^8}[x]/(m(x))$%. This isn't particularly rapid.
215 static void mmul(octet
*d
, const octet
*p
, const octet
*q
,
216 size_t r
, size_t n
, unsigned m
)
223 for (i
= 0; i
< n
; i
++)
224 a
^= mul(*p
++, *qq
++, m
);
236 * Use: Prints the MDS/q table.
239 static void qmds(void)
243 static const q_tab
*q
[4] = { &q1
, &q0
, &q1
, &q0
};
245 for (i
= 0; i
< 4; i
++) {
246 octet in
[4] = { 0, 0, 0, 0 };
249 for (j
= 0; j
< 256; j
++) {
251 mmul(out
, mds
, in
, 4, 4, MDS_MOD
);
252 t
[i
][j
] = LOAD32_L(out
);
257 /* --- Expanded MDS tables --- *\n\
259 * The table contains output vectors for computing the result of pushing\n\
260 * bytes through appropriate @q@ tables and the MDS matrix.\n\
263 const uint32 twofish_qmds[4][256] = {\
265 for (i
= 0; i
< 4; i
++) {
266 fputs(" { ", stdout
);
267 for (j
= 0; j
< 256; j
++) {
268 printf("0x%08lx", (unsigned long)t
[i
][j
]);
274 } else if (j
% 4 == 3)
275 fputs(",\n ", stdout
);
290 * Use: Produces the log and antilog tables for doing the RS
291 * arithmetic efficiently.
294 static void rslog(void)
303 for (i
= 0; i
< 255; i
++) {
312 for (i
= 0; i
< 32; i
++) {
313 if (rslog
[rs
[i
]] > x
)
318 /* --- Reed-Solomon log tables --- *\n\
320 * The Reed-Solomon multiplies are accelerated by using log tables.\n\
323 const octet twofish_rslog[256] = {\n\
326 for (i
= 0; i
< 256; i
++) {
327 printf("0x%02x", rslog
[i
]);
331 fputs(",\n ", stdout
);
337 const octet twofish_rsexp[%d] = {\n\
340 for (i
= 0; i
< 255 + x
+ 1; i
++) {
341 printf("0x%02x", rsexp
[i
% 255]);
345 fputs(",\n ", stdout
);
351 /* --- Reed-Solomon matrix with log entries --- */\n\
353 const octet twofish_rs[32] = {\n\
356 for (i
= 0; i
< 32; i
++) {
357 printf("0x%02x", rslog
[rs
[i
]]);
361 fputs(",\n ", stdout
);
367 /*----- Main program ------------------------------------------------------*/
376 * Twofish q tables [generated]\n\
379 #include <mLib/bits.h>\n\
383 /* --- The q tables --- */
386 /* --- Precomputed @q@ tables --- */\n\
388 mkq(&q0
, &qt0
, "qt0");
389 mkq(&q1
, &qt1
, "qt1");
393 /* --- The MDS/q tables --- */
400 if (fclose(stdout
)) {
401 fprintf(stderr
, "error writing data\n");
408 /*----- That's all, folks -------------------------------------------------*/