502933071a74400ec8085b78a72d87ae1556f655
[u/mdw/catacomb] / symm / tiger-mktab.c
1 /* -*-c-*-
2 *
3 * Generate S-boxes for the Tiger hash function
4 *
5 * (c) 2000 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16 *
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.
21 *
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 <mLib/bits.h>
31
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include "tiger-base.h"
36
37 /*----- Data structures ---------------------------------------------------*/
38
39 /*----- Static variables --------------------------------------------------*/
40
41 static kludge64 s[4][256];
42
43 /*----- Main code ---------------------------------------------------------*/
44
45 /* --- The basic Tiger compression function --- */
46
47 static void tiger(kludge64 *x, kludge64 *ss)
48 {
49 TIGER_CORE(ss[0], ss[1], ss[2], x);
50 }
51
52 /* --- The S-box generator --- */
53
54 void gen(const char *buf, unsigned passes)
55 {
56 kludge64 x[8], ss[3];
57 unsigned i, j, k, b;
58 unsigned q, n;
59 uint32 t;
60 const char *p;
61
62 for (i = 0; i < 256; i++) {
63 for (j = 0; j < 4; j++) {
64 uint32 z = 0x01010101 * i;
65 SET64(s[j][i], z, z);
66 }
67 }
68
69 SET64(ss[0], 0x01234567, 0x89abcdef);
70 SET64(ss[1], 0xfedcba98, 0x76543210);
71 SET64(ss[2], 0xf096a5b4, 0xc3b2e187);
72
73 q = 2;
74 for (i = 0; i < passes; i++) {
75 for (j = 0; j < 256; j++) {
76 for (k = 0; k < 4; k++) {
77 q++;
78 if (q == 3) {
79 q = 0;
80 for (p = buf, n = 0; n < 8; n++, p += 8)
81 LOAD64_L_(x[n], p);
82 tiger(x, ss);
83 }
84 for (b = 0; b < 32; b += 8) {
85 n = U8(LO64(ss[q]) >> b);
86 t = (LO64(s[k][j]) ^ LO64(s[k][n])) & (0xff << b);
87 SET64(s[k][j], HI64(s[k][j]), LO64(s[k][j]) ^ t);
88 SET64(s[k][n], HI64(s[k][n]), LO64(s[k][n]) ^ t);
89 }
90 for (b = 0; b < 32; b += 8) {
91 n = U8(HI64(ss[q]) >> b);
92 t = (HI64(s[k][j]) ^ HI64(s[k][n])) & (0xff << b);
93 SET64(s[k][j], HI64(s[k][j]) ^ t, LO64(s[k][j]));
94 SET64(s[k][n], HI64(s[k][n]) ^ t, LO64(s[k][n]));
95 }
96 }
97 }
98 }
99 }
100
101 int main(void)
102 {
103 unsigned i, j;
104
105 gen("Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham", 5);
106
107 fputs("\
108 /* -*-c-*-\n\
109 *\n\
110 * S-boxes for Tiger [generated]\n\
111 */\n\
112 \n\
113 #ifndef CATACOMB_TIGER_TAB_H\n\
114 #define CATACOMB_TIGER_TAB_H\n\
115 \n\
116 #define TIGER_S { \\\n\
117 { ", stdout);
118
119 for (i = 0; i < 4; i++) {
120 for (j = 0; j < 256; j++) {
121 #ifdef HAVE_UINT64
122 printf("{ 0x%016llxull }", s[i][j].i);
123 #else
124 printf("{ 0x%08lx, 0x%08lx }",
125 (unsigned long)s[i][j].hi, (unsigned long)s[i][j].lo);
126 #endif
127 if (j == 255) {
128 if (i == 3)
129 fputs(" } \\\n}\n", stdout);
130 else
131 fputs(" }, \\\n\
132 \\\n\
133 { ", stdout);
134 } else if (j % 2 == 1)
135 fputs(", \\\n ", stdout);
136 else
137 fputs(", ", stdout);
138 }
139 }
140
141 fputs("\n#endif\n", stdout);
142
143 if (fclose(stdout)) {
144 fprintf(stderr, "error writing data\n");
145 exit(EXIT_FAILURE);
146 }
147
148 return (0);
149 }
150
151 /*----- That's all, folks -------------------------------------------------*/