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