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