catcrypt: Implement symmetric key-encapsulation and signature schemes.
[u/mdw/catacomb] / tiger-base.h
1 /* -*-c-*-
2 *
3 * $Id: tiger-base.h,v 1.2 2004/04/08 01:36:15 mdw Exp $
4 *
5 * Common definitions 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 #ifndef CATACOMB_TIGER_BASE_H
31 #define CATACOMB_TIGER_BASE_H
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #include <mLib/bits.h>
40
41 /*----- Macros provided ---------------------------------------------------*/
42
43 /* --- The guts of a single round --- */
44
45 #define TIGER_ROUND(a, b, c, x, n, op) do { \
46 kludge64 _t; \
47 XOR64(c, c, x); \
48 _t = s[0][U8(LO64(c) >> 0)]; \
49 XOR64(_t, _t, s[1][U8(LO64(c) >> 16)]); \
50 XOR64(_t, _t, s[2][U8(HI64(c) >> 0)]); \
51 XOR64(_t, _t, s[3][U8(HI64(c) >> 16)]); \
52 SUB64(a, a, _t); \
53 _t = s[3][U8(LO64(c) >> 8)]; \
54 XOR64(_t, _t, s[2][U8(LO64(c) >> 24)]); \
55 XOR64(_t, _t, s[1][U8(HI64(c) >> 8)]); \
56 XOR64(_t, _t, s[0][U8(HI64(c) >> 24)]); \
57 ADD64(b, b, _t); \
58 LSL64_(_t, b, n); \
59 op##64(b, _t, b); \
60 } while (0)
61
62 /* --- One pass over the buffer --- */
63
64 #define TIGER_PASS(a, b, c, x, n, op) do { \
65 TIGER_ROUND(a, b, c, x[0], n, op); \
66 TIGER_ROUND(b, c, a, x[1], n, op); \
67 TIGER_ROUND(c, a, b, x[2], n, op); \
68 TIGER_ROUND(a, b, c, x[3], n, op); \
69 TIGER_ROUND(b, c, a, x[4], n, op); \
70 TIGER_ROUND(c, a, b, x[5], n, op); \
71 TIGER_ROUND(a, b, c, x[6], n, op); \
72 TIGER_ROUND(b, c, a, x[7], n, op); \
73 } while (0)
74
75 /* --- A step in the `key schedule' --- */
76
77 #define TIGER_KSTEP(a, b, c, d, op, n) do { \
78 kludge64 _u; \
79 XOR64(b, b, a); \
80 ADD64(c, c, b); \
81 CPL64(_u, b); op##64_(_u, _u, n); XOR64(_u, _u, c); SUB64(d, d, _u); \
82 } while (0)
83
84 /* --- The `key schedule' -- mangle the buffer --- */
85
86 #define TIGER_KSCHED(x) do { \
87 kludge64 _t; \
88 \
89 SET64(_t, 0xa5a5a5a5, 0xa5a5a5a5); \
90 XOR64(_t, _t, x[7]); SUB64(x[0], x[0], _t); \
91 TIGER_KSTEP(x[0], x[1], x[2], x[3], LSL, 19); \
92 TIGER_KSTEP(x[3], x[4], x[5], x[6], LSR, 23); \
93 TIGER_KSTEP(x[6], x[7], x[0], x[1], LSL, 19); \
94 TIGER_KSTEP(x[1], x[2], x[3], x[4], LSR, 23); \
95 XOR64(x[5], x[5], x[4]); \
96 ADD64(x[6], x[6], x[5]); \
97 SET64(_t, 0x01234567, 0x89abcdef); \
98 XOR64(_t, _t, x[6]); SUB64(x[7], x[7], _t); \
99 } while (0)
100
101 /* --- The Tiger compression function --- */
102
103 #define TIGER_CORE(a, b, c, x) do { \
104 kludge64 _a, _b, _c; \
105 _a = a, _b = b, _c = c; \
106 TIGER_PASS(_a, _b, _c, x, 2, ADD); \
107 TIGER_KSCHED(x); \
108 TIGER_PASS(_c, _a, _b, x, 3, SUB); \
109 TIGER_KSCHED(x); \
110 TIGER_PASS(_b, _c, _a, x, 3, ADD); \
111 XOR64(a, _a, a); SUB64(b, _b, b); ADD64(c, _c, c); \
112 } while (0)
113
114 /*----- That's all, folks -------------------------------------------------*/
115
116 #ifdef __cplusplus
117 }
118 #endif
119
120 #endif