chiark / gitweb /
Add some more vectors, and a whinge about how Skipjack test vectors are.
[catacomb] / tiger.c
1 /* -*-c-*-
2  *
3  * $Id: tiger.c,v 1.1 2000/07/15 10:02:43 mdw Exp $
4  *
5  * 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.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 "ghash-def.h"
43 #include "hash.h"
44 #include "tiger.h"
45 #include "tiger-tab.h"
46 #include "tiger-base.h"
47
48 /*----- S-boxes -----------------------------------------------------------*/
49
50 static const kludge64 s[4][256] = TIGER_S;
51
52 /*----- Main code ---------------------------------------------------------*/
53
54 /* --- @tiger_compress@ --- *
55  *
56  * Arguments:   @tiger_ctx *ctx@ = pointer to context block
57  *              @const void *sbuf@ = pointer to buffer of appropriate size
58  *
59  * Returns:     ---
60  *
61  * Use:         Tiger compression function.
62  */
63
64 void tiger_compress(tiger_ctx *ctx, const void *sbuf)
65 {
66   kludge64 x[8];
67   int i;
68   const octet *p;
69   for (i = 0, p = sbuf; i < 8; i++, p += 8)
70     LOAD64_L_(x[i], p);
71   TIGER_CORE(ctx->a, ctx->b, ctx->c, x);
72 }
73
74 /* --- @tiger_init@ --- *
75  *
76  * Arguments:   @tiger_ctx *ctx@ = pointer to context block to initialize
77  *
78  * Returns:     ---
79  *
80  * Use:         Initializes a context block ready for hashing.
81  */
82
83 void tiger_init(tiger_ctx *ctx)
84 {
85   SET64(ctx->a, 0x01234567, 0x89abcdef);
86   SET64(ctx->b, 0xfedcba98, 0x76543210);
87   SET64(ctx->c, 0xf096a5b4, 0xc3b2e187);
88   ctx->off = 0;
89   ctx->nl = ctx->nh = 0;
90 }
91
92 /* --- @tiger_set@ --- *
93  *
94  * Arguments:   @tiger_ctx *ctx@ = pointer to context block
95  *              @const void *buf@ = pointer to state buffer
96  *              @unsigned long count@ = current count of bytes processed
97  *
98  * Returns:     ---
99  *
100  * Use:         Initializes a context block from a given state.  This is
101  *              useful in cases where the initial hash state is meant to be
102  *              secret, e.g., for NMAC and HMAC support.
103  */
104
105 void tiger_set(tiger_ctx *ctx, const void *buf, unsigned long count)
106 {
107   const octet *p = buf;
108   LOAD64_L_(ctx->a, p +  0);
109   LOAD64_L_(ctx->b, p +  8);
110   LOAD64_L_(ctx->c, p + 16);
111   ctx->off = 0;
112   ctx->nl = U32(count);
113   ctx->nh = U32(((count & ~MASK32) >> 16) >> 16);
114 }
115
116 /* --- @tiger_hash@ --- *
117  *
118  * Arguments:   @tiger_ctx *ctx@ = pointer to context block
119  *              @const void *buf@ = buffer of data to hash
120  *              @size_t sz@ = size of buffer to hash
121  *
122  * Returns:     ---
123  *
124  * Use:         Hashes a buffer of data.  The buffer may be of any size and
125  *              alignment.
126  */
127
128 void tiger_hash(tiger_ctx *ctx, const void *buf, size_t sz)
129 {
130   HASH_BUFFER(TIGER, tiger, ctx, buf, sz);
131 }
132
133 /* --- @tiger_done@ --- *
134  *
135  * Arguments:   @tiger_ctx *ctx@ = pointer to context block
136  *              @void *hash@ = pointer to output buffer
137  *
138  * Returns:     ---
139  *
140  * Use:         Returns the hash of the data read so far.
141  */
142
143 void tiger_done(tiger_ctx *ctx, void *hash)
144 {
145   octet *p = hash;
146   HASH_PAD(TIGER, tiger, ctx, 0x01u, 0, 8);
147   STORE32_L(ctx->buf + TIGER_BUFSZ - 8, ctx->nl << 3);
148   STORE32_L(ctx->buf + TIGER_BUFSZ - 4, (ctx->nl >> 29) | (ctx->nh << 3));
149   tiger_compress(ctx, ctx->buf);
150   STORE64_L_(p +  0, ctx->a);
151   STORE64_L_(p +  8, ctx->b);
152   STORE64_L_(p + 16, ctx->c);
153 }
154
155 /* --- @tiger_state@ --- *
156  *
157  * Arguments:   @tiger_ctx *ctx@ = pointer to context
158  *              @void *state@ = pointer to buffer for current state
159  *
160  * Returns:     Number of bytes written to the hash function so far.
161  *
162  * Use:         Returns the current state of the hash function such that
163  *              it can be passed to @tiger_set@.
164  */
165
166 unsigned long tiger_state(tiger_ctx *ctx, void *state)
167 {
168   octet *p = state;
169   STORE64_L_(p +  0, ctx->a);
170   STORE64_L_(p +  8, ctx->b);
171   STORE64_L_(p + 16, ctx->c);
172   return (ctx->nl | ((ctx->nh << 16) << 16));
173 }
174
175 /* --- Generic interface --- */
176
177 GHASH_DEF(TIGER, tiger)
178
179 /* --- Test code --- */
180
181 HASH_TEST(TIGER, tiger)
182
183 /*----- That's all, folks -------------------------------------------------*/