chiark / gitweb /
symm/poly1305.c: Fix daft typo in banner comment.
[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 /*----- Static variables --------------------------------------------------*/
38
39 static kludge64 tiger_s[4][256];
40
41 /*----- Main code ---------------------------------------------------------*/
42
43 /* --- The basic Tiger compression function --- */
44
45 static void tiger(kludge64 *x, kludge64 *ss)
46 {
47   TIGER_CORE(ss[0], ss[1], ss[2], x);
48 }
49
50 /* --- The S-box generator --- */
51
52 void gen(const char *buf, unsigned passes)
53 {
54   kludge64 x[8], ss[3];
55   unsigned i, j, k, b;
56   unsigned q, n;
57   uint32 t;
58   const char *p;
59
60   for (i = 0; i < 256; i++) {
61     for (j = 0; j < 4; j++) {
62       uint32 z = 0x01010101 * i;
63       SET64(tiger_s[j][i], z, z);
64     }
65   }
66
67   SET64(ss[0], 0x01234567, 0x89abcdef);
68   SET64(ss[1], 0xfedcba98, 0x76543210);
69   SET64(ss[2], 0xf096a5b4, 0xc3b2e187);
70
71   q = 2;
72   for (i = 0; i < passes; i++) {
73     for (j = 0; j < 256; j++) {
74       for (k = 0; k < 4; k++) {
75         q++;
76         if (q == 3) {
77           q = 0;
78           for (p = buf, n = 0; n < 8; n++, p += 8)
79             LOAD64_L_(x[n], p);
80           tiger(x, ss);
81         }
82         for (b = 0; b < 32; b += 8) {
83           n = U8(LO64(ss[q]) >> b);
84           t = (LO64(tiger_s[k][j]) ^ LO64(tiger_s[k][n])) & (0xff << b);
85           SET64(tiger_s[k][j], HI64(tiger_s[k][j]), LO64(tiger_s[k][j]) ^ t);
86           SET64(tiger_s[k][n], HI64(tiger_s[k][n]), LO64(tiger_s[k][n]) ^ t);
87         }
88         for (b = 0; b < 32; b += 8) {
89           n = U8(HI64(ss[q]) >> b);
90           t = (HI64(tiger_s[k][j]) ^ HI64(tiger_s[k][n])) & (0xff << b);
91           SET64(tiger_s[k][j], HI64(tiger_s[k][j]) ^ t, LO64(tiger_s[k][j]));
92           SET64(tiger_s[k][n], HI64(tiger_s[k][n]) ^ t, LO64(tiger_s[k][n]));
93         }
94       }
95     }
96   }
97 }
98
99 int main(void)
100 {
101   unsigned i, j;
102
103   gen("Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham", 5);
104
105   fputs("\
106 /* -*-c-*-\n\
107  *\n\
108  * S-boxes for Tiger [generated]\n\
109  */\n\
110 \n\
111 #include <mLib/bits.h>\n\
112 \n\
113 const kludge64 tiger_s[4][256] = {\n\
114   { ", stdout);
115
116   for (i = 0; i < 4; i++) {
117     for (j = 0; j < 256; j++) {
118       printf("X64(%08lx, %08lx)",
119              (unsigned long)HI64(tiger_s[i][j]),
120              (unsigned long)LO64(tiger_s[i][j]));
121       if (j == 255) {
122         if (i == 3)
123           fputs(" }\n};\n", stdout);
124         else
125           fputs(" },\n\n  { ", stdout);
126       } else if (j % 2 == 1)
127         fputs(",\n    ", stdout);
128       else
129         fputs(", ", stdout);
130     }
131   }
132
133   if (fclose(stdout)) {
134     fprintf(stderr, "error writing data\n");
135     exit(EXIT_FAILURE);
136   }
137
138   return (0);
139 }
140
141 /*----- That's all, folks -------------------------------------------------*/