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