3 * The Tiger hash function
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
30 #include <mLib/bits.h>
32 #include "ghash-def.h"
35 #include "tiger-tab.h"
36 #include "tiger-base.h"
38 /*----- S-boxes -----------------------------------------------------------*/
40 static const kludge64 s[4][256] = TIGER_S;
42 /*----- Main code ---------------------------------------------------------*/
44 /* --- @tiger_compress@ --- *
46 * Arguments: @tiger_ctx *ctx@ = pointer to context block
47 * @const void *sbuf@ = pointer to buffer of appropriate size
51 * Use: Tiger compression function.
54 void tiger_compress(tiger_ctx *ctx, const void *sbuf)
59 for (i = 0, p = sbuf; i < 8; i++, p += 8)
61 TIGER_CORE(ctx->a, ctx->b, ctx->c, x);
64 /* --- @tiger_init@ --- *
66 * Arguments: @tiger_ctx *ctx@ = pointer to context block to initialize
70 * Use: Initializes a context block ready for hashing.
73 void tiger_init(tiger_ctx *ctx)
75 SET64(ctx->a, 0x01234567, 0x89abcdef);
76 SET64(ctx->b, 0xfedcba98, 0x76543210);
77 SET64(ctx->c, 0xf096a5b4, 0xc3b2e187);
79 ctx->nl = ctx->nh = 0;
82 /* --- @tiger_set@ --- *
84 * Arguments: @tiger_ctx *ctx@ = pointer to context block
85 * @const void *buf@ = pointer to state buffer
86 * @unsigned long count@ = current count of bytes processed
90 * Use: Initializes a context block from a given state. This is
91 * useful in cases where the initial hash state is meant to be
92 * secret, e.g., for NMAC and HMAC support.
95 void tiger_set(tiger_ctx *ctx, const void *buf, unsigned long count)
98 LOAD64_L_(ctx->a, p + 0);
99 LOAD64_L_(ctx->b, p + 8);
100 LOAD64_L_(ctx->c, p + 16);
102 ctx->nl = U32(count);
103 ctx->nh = U32(((count & ~MASK32) >> 16) >> 16);
106 /* --- @tiger_hash@ --- *
108 * Arguments: @tiger_ctx *ctx@ = pointer to context block
109 * @const void *buf@ = buffer of data to hash
110 * @size_t sz@ = size of buffer to hash
114 * Use: Hashes a buffer of data. The buffer may be of any size and
118 void tiger_hash(tiger_ctx *ctx, const void *buf, size_t sz)
120 HASH_BUFFER(TIGER, tiger, ctx, buf, sz);
123 /* --- @tiger_done@ --- *
125 * Arguments: @tiger_ctx *ctx@ = pointer to context block
126 * @void *hash@ = pointer to output buffer
130 * Use: Returns the hash of the data read so far.
133 void tiger_done(tiger_ctx *ctx, void *hash)
136 HASH_PAD(TIGER, tiger, ctx, 0x01u, 0, 8);
137 STORE32_L(ctx->buf + TIGER_BUFSZ - 8, ctx->nl << 3);
138 STORE32_L(ctx->buf + TIGER_BUFSZ - 4, (ctx->nl >> 29) | (ctx->nh << 3));
139 tiger_compress(ctx, ctx->buf);
140 STORE64_L_(p + 0, ctx->a);
141 STORE64_L_(p + 8, ctx->b);
142 STORE64_L_(p + 16, ctx->c);
145 /* --- @tiger_state@ --- *
147 * Arguments: @tiger_ctx *ctx@ = pointer to context
148 * @void *state@ = pointer to buffer for current state
150 * Returns: Number of bytes written to the hash function so far.
152 * Use: Returns the current state of the hash function such that
153 * it can be passed to @tiger_set@.
156 unsigned long tiger_state(tiger_ctx *ctx, void *state)
159 STORE64_L_(p + 0, ctx->a);
160 STORE64_L_(p + 8, ctx->b);
161 STORE64_L_(p + 16, ctx->c);
162 return (ctx->nl | ((ctx->nh << 16) << 16));
165 /* --- Generic interface --- */
167 GHASH_DEF(TIGER, tiger)
169 /* --- Test code --- */
171 HASH_TEST(TIGER, tiger)
173 /*----- That's all, folks -------------------------------------------------*/