3 * Generate tables for Whirlpool hash function
5 * (c) 2005 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 ------------------------------------------------------*/
34 #include <mLib/bits.h>
36 /*----- Static variables --------------------------------------------------*/
38 static const octet E[] = {
39 0x1, 0xb, 0x9, 0xc, 0xd, 0x6, 0xf, 0x3,
40 0xe, 0x8, 0x7, 0x4, 0xa, 0x2, 0x5, 0x0
42 0x7, 0xc, 0xb, 0xd, 0xe, 0x4, 0x9, 0xf,
43 0x6, 0x3, 0x8, 0xa, 0x2, 0x5, 0x1, 0x0
45 0x1, 0x1, 0x4, 0x1, 0x8, 0x5, 0x2, 0x9
48 static octet S[256], T[256][8], log[256], alog[256];
50 /*----- Main code ---------------------------------------------------------*/
54 static void logtable(void)
58 for (i = 0, x = 1; i < 255; i++) {
62 if (x & 0x100) x ^= S_MOD;
66 static octet mul(octet x, octet y)
67 { if (!x || !y) return (0); return (alog[(log[x] + log[y]) % 255]); }
69 static void sbox(void)
75 for (i = 0; i < 16; i++) EI[E[i]] = i;
76 for (i = 0; i < 256; i++) {
81 l = E[l ^ y]; r = EI[r ^ y];
85 for (i = 0; i < 256; i++) {
86 for (j = 0; j < 8; j++)
87 T[i][j] = mul(S[i], C[j]);
91 static unsigned long w32(int i, int j, int k)
96 return (k ? LO64(x) : HI64(x));
106 * Whirlpool tables [generated]\n\
109 #include <mLib/bits.h>\n\
111 #if defined(HAVE_UINT64)\n\
117 /* --- Write out the S-box --- */
122 /* --- The byte substitution --- */\n\
124 const octet whirlpool_s[256] = {\n\
126 for (i = 0; i < 256; i++) {
127 printf("0x%02x", S[i]);
129 fputs("\n};\n\n", stdout);
131 fputs(",\n ", stdout);
136 /* --- Write out the key constant tables --- */
139 /* --- The key generation constants --- */\n\
141 const kludge64 whirlpool_c[10] = {\n\
143 for (i = 0; i < 10; i++) {
144 printf("X64(%08lx, %08lx)",
145 (unsigned long)LOAD32_L(&S[i * 8 + 4]),
146 (unsigned long)LOAD32_L(&S[i * 8 + 0]));
148 fputs("\n};\n\n", stdout);
150 fputs(",\n ", stdout);
155 /* --- Write out the big T tables --- */
160 /* --- The 64-bit big round tables --- */\n\
162 const kludge64 whirlpool_t[8][256] = {\n\
164 for (j = 0; j < 8; j++) {
165 for (i = 0; i < 256; i++) {
166 printf("X64(%08lx, %08lx)", w32(j, i, 0), w32(j, i, 1));
169 fputs(" }\n};\n\n", stdout);
171 fputs(" },\n\n { ", stdout);
172 } else if (i % 2 == 1)
173 fputs(",\n ", stdout);
179 /* --- Write out the smaller U and V tables --- */
184 /* --- The 32-bit round tables --- */\n\
186 const uint32 whirlpool_u[4][256] = {\n\
188 for (j = 0; j < 4; j++) {
189 for (i = 0; i < 256; i++) {
190 printf("0x%08lx", w32(j, i, 1));
193 fputs(" }\n};\n\n", stdout);
195 fputs(" },\n\n { ", stdout);
196 } else if (i % 4 == 3)
197 fputs(",\n ", stdout);
204 const uint32 whirlpool_v[4][256] = {\n\
206 for (j = 0; j < 4; j++) {
207 for (i = 0; i < 256; i++) {
208 printf("0x%08lx", w32(j, i, 0));
211 fputs(" }\n};\n\n", stdout);
213 fputs(" },\n\n { ", stdout);
214 } else if (i % 4 == 3)
215 fputs(",\n ", stdout);
225 if (fclose(stdout)) {
226 fprintf(stderr, "error writing data\n");
233 /*----- That's all, folks -------------------------------------------------*/