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