math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / symm / whirlpool-mktab.c
CommitLineData
7fcfe7de 1/* -*-c-*-
2 *
7fcfe7de 3 * Generate tables for Whirlpool hash function
4 *
5 * (c) 2005 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
7fcfe7de 9 *
10 * This file is part of Catacomb.
11 *
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.
45c0fd36 16 *
7fcfe7de 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.
45c0fd36 21 *
7fcfe7de 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,
25 * MA 02111-1307, USA.
26 */
27
28/*----- Header files ------------------------------------------------------*/
29
30#include <assert.h>
31#include <stdio.h>
32#include <stdlib.h>
33
34#include <mLib/bits.h>
35
36/*----- Static variables --------------------------------------------------*/
37
38static const octet E[] = {
39 0x1, 0xb, 0x9, 0xc, 0xd, 0x6, 0xf, 0x3,
40 0xe, 0x8, 0x7, 0x4, 0xa, 0x2, 0x5, 0x0
41}, R[] = {
42 0x7, 0xc, 0xb, 0xd, 0xe, 0x4, 0x9, 0xf,
43 0x6, 0x3, 0x8, 0xa, 0x2, 0x5, 0x1, 0x0
44}, C[] = {
45 0x1, 0x1, 0x4, 0x1, 0x8, 0x5, 0x2, 0x9
46};
47
48static octet S[256], T[256][8], log[256], alog[256];
49
50/*----- Main code ---------------------------------------------------------*/
51
52#define S_MOD 0x11d
53
54static void logtable(void)
55{
56 unsigned i, x;
57
58 for (i = 0, x = 1; i < 255; i++) {
59 log[x] = i;
60 alog[i] = x;
61 x <<= 1;
62 if (x & 0x100) x ^= S_MOD;
63 }
64}
65
66static octet mul(octet x, octet y)
67 { if (!x || !y) return (0); return (alog[(log[x] + log[y]) % 255]); }
68
69static void sbox(void)
70{
71 unsigned i, j;
72 octet EI[16];
73 octet l, r, y;
74
75 for (i = 0; i < 16; i++) EI[E[i]] = i;
76 for (i = 0; i < 256; i++) {
77 l = (i >> 4) & 0xf;
78 r = (i >> 0) & 0xf;
79 l = E[l]; r = EI[r];
80 y = R[l ^ r];
81 l = E[l ^ y]; r = EI[r ^ y];
82 S[i] = (l << 4) | r;
83 }
84
85 for (i = 0; i < 256; i++) {
86 for (j = 0; j < 8; j++)
87 T[i][j] = mul(S[i], C[j]);
88 }
89}
90
91static unsigned long w32(int i, int j, int k)
92{
93 kludge64 x;
94 LOAD64_L_(x, T[j]);
95 ROL64_(x, x, i * 8);
96 return (k ? LO64(x) : HI64(x));
97}
98
99int main(void)
100{
101 int i, j;
102
103 puts("\
104/* -*-c-*-\n\
105 *\n\
106 * Whirlpool tables [generated]\n\
107 */\n\
108\n\
e5b61a8d
MW
109#include <mLib/bits.h>\n\
110\n\
111#if defined(HAVE_UINT64)\n\
112# define USE64\n\
113#endif\n\
114\n\
7fcfe7de 115");
116
117 /* --- Write out the S-box --- */
118
119 logtable();
120 sbox();
121 fputs("\
122/* --- The byte substitution --- */\n\
123\n\
e5b61a8d 124const octet whirlpool_s[256] = {\n\
7fcfe7de 125 ", stdout);
126 for (i = 0; i < 256; i++) {
127 printf("0x%02x", S[i]);
128 if (i == 255)
e5b61a8d 129 fputs("\n};\n\n", stdout);
7fcfe7de 130 else if (i % 8 == 7)
e5b61a8d 131 fputs(",\n ", stdout);
7fcfe7de 132 else
133 fputs(", ", stdout);
134 }
135
136 /* --- Write out the key constant tables --- */
137
138 fputs("\
139/* --- The key generation constants --- */\n\
140\n\
e5b61a8d 141const kludge64 whirlpool_c[10] = {\n\
7fcfe7de 142 ", stdout);
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]));
147 if (i == 9)
e5b61a8d 148 fputs("\n};\n\n", stdout);
7fcfe7de 149 else if (i % 2 == 1)
e5b61a8d 150 fputs(",\n ", stdout);
7fcfe7de 151 else
152 fputs(", ", stdout);
153 }
154
155 /* --- Write out the big T tables --- */
156
157 fputs("\
e5b61a8d
MW
158#ifdef USE64\n\
159\n\
7fcfe7de 160/* --- The 64-bit big round tables --- */\n\
161\n\
e5b61a8d 162const kludge64 whirlpool_t[8][256] = {\n\
7fcfe7de 163 { ", stdout);
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));
167 if (i == 255) {
168 if (j == 7)
e5b61a8d 169 fputs(" }\n};\n\n", stdout);
7fcfe7de 170 else
e5b61a8d 171 fputs(" },\n\n { ", stdout);
7fcfe7de 172 } else if (i % 2 == 1)
e5b61a8d 173 fputs(",\n ", stdout);
7fcfe7de 174 else
175 fputs(", ", stdout);
176 }
177 }
178
179 /* --- Write out the smaller U and V tables --- */
180
181 fputs("\
e5b61a8d
MW
182#else\n\
183\n\
7fcfe7de 184/* --- The 32-bit round tables --- */\n\
185\n\
e5b61a8d 186const uint32 whirlpool_u[4][256] = {\n\
7fcfe7de 187 { ", stdout);
188 for (j = 0; j < 4; j++) {
189 for (i = 0; i < 256; i++) {
190 printf("0x%08lx", w32(j, i, 1));
191 if (i == 255) {
192 if (j == 3)
e5b61a8d 193 fputs(" }\n};\n\n", stdout);
7fcfe7de 194 else
e5b61a8d 195 fputs(" },\n\n { ", stdout);
7fcfe7de 196 } else if (i % 4 == 3)
e5b61a8d 197 fputs(",\n ", stdout);
7fcfe7de 198 else
199 fputs(", ", stdout);
200 }
201 }
202
203 fputs("\
e5b61a8d 204const uint32 whirlpool_v[4][256] = {\n\
7fcfe7de 205 { ", stdout);
206 for (j = 0; j < 4; j++) {
207 for (i = 0; i < 256; i++) {
208 printf("0x%08lx", w32(j, i, 0));
209 if (i == 255) {
210 if (j == 3)
e5b61a8d 211 fputs(" }\n};\n\n", stdout);
7fcfe7de 212 else
e5b61a8d 213 fputs(" },\n\n { ", stdout);
7fcfe7de 214 } else if (i % 4 == 3)
e5b61a8d 215 fputs(",\n ", stdout);
7fcfe7de 216 else
217 fputs(", ", stdout);
218 }
219 }
220
221 /* --- Done --- */
222
223 puts("#endif");
224
225 if (fclose(stdout)) {
226 fprintf(stderr, "error writing data\n");
227 exit(EXIT_FAILURE);
228 }
229
45c0fd36 230 return (0);
7fcfe7de 231}
232
233/*----- That's all, folks -------------------------------------------------*/