| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id$ |
| 4 | * |
| 5 | * Implementation of the Whirlpool 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 | /*----- Notes on the Whirlpool hash function ------------------------------* |
| 31 | * |
| 32 | * Whirlpool was designed by Paulo Barreto and Vincent Rijmen. Its |
| 33 | * compression function is based on similar ideas to Rijndael (also |
| 34 | * codesigned by Rijmen). |
| 35 | * |
| 36 | * Whirlpool256 is simply Whirlpool with its final output truncated to 256 |
| 37 | * bits. This is, I hope, about as good as a 256-bit hash function can get. |
| 38 | * It isn't vulnerable to the Kelsey-Schneier generic second-preimage attack |
| 39 | * against MD hash functions because of its larger internal state (see also |
| 40 | * Lucks). |
| 41 | */ |
| 42 | |
| 43 | #ifndef CATACOMB_WHIRLPOOL_H |
| 44 | #define CATACOMB_WHIRLPOOL_H |
| 45 | #define CATACOMB_WHIRLPOOL256_H |
| 46 | |
| 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
| 51 | /*----- Header files ------------------------------------------------------*/ |
| 52 | |
| 53 | #include <mLib/bits.h> |
| 54 | |
| 55 | #ifndef CATACOMB_GHASH_H |
| 56 | # include "ghash.h" |
| 57 | #endif |
| 58 | |
| 59 | /*----- Magic numbers -----------------------------------------------------*/ |
| 60 | |
| 61 | #define WHIRLPOOL_BUFSZ 64 |
| 62 | #define WHIRLPOOL_HASHSZ 64 |
| 63 | #define WHIRLPOOL_STATESZ 64 |
| 64 | |
| 65 | #define WHIRLPOOL256_BUFSZ 64 |
| 66 | #define WHIRLPOOL256_HASHSZ 32 |
| 67 | #define WHIRLPOOL256_STATESZ 64 |
| 68 | |
| 69 | /*----- Data structures ---------------------------------------------------*/ |
| 70 | |
| 71 | typedef struct whirlpool_ctx { |
| 72 | kludge64 s[8]; /* Chaining variables */ |
| 73 | uint32 nh, nl; /* Byte count so far */ |
| 74 | unsigned off; /* Offset into buffer */ |
| 75 | octet buf[WHIRLPOOL_BUFSZ]; /* Accumulation buffer */ |
| 76 | } whirlpool_ctx, whirlpool256_ctx; |
| 77 | |
| 78 | /*----- Functions provided ------------------------------------------------*/ |
| 79 | |
| 80 | /* --- @whirlpool_compress@, @whirlpool256_compress@ --- * |
| 81 | * |
| 82 | * Arguments: @whirlpool_ctx *ctx@ = pointer to context block |
| 83 | * @const void *sbuf@ = pointer to buffer of appropriate size |
| 84 | * |
| 85 | * Returns: --- |
| 86 | * |
| 87 | * Use: SHA-512 compression function. |
| 88 | */ |
| 89 | |
| 90 | extern void whirlpool_compress(whirlpool_ctx */*ctx*/, const void */*sbuf*/); |
| 91 | #define whirlpool256_compress whirlpool_compress |
| 92 | |
| 93 | /* --- @whirlpool_init@, @whirlpool256_init@ --- * |
| 94 | * |
| 95 | * Arguments: @whirlpool_ctx *ctx@ = pointer to context block to initialize |
| 96 | * |
| 97 | * Returns: --- |
| 98 | * |
| 99 | * Use: Initializes a context block ready for hashing. |
| 100 | */ |
| 101 | |
| 102 | extern void whirlpool_init(whirlpool_ctx */*ctx*/); |
| 103 | #define whirlpool256_init whirlpool_init |
| 104 | |
| 105 | /* --- @whirlpool_set@, @whirlpool256_set@ --- * |
| 106 | * |
| 107 | * Arguments: @whirlpool_ctx *ctx@ = pointer to context block |
| 108 | * @const void *buf@ = pointer to state buffer |
| 109 | * @unsigned long count@ = current count of bytes processed |
| 110 | * |
| 111 | * Returns: --- |
| 112 | * |
| 113 | * Use: Initializes a context block from a given state. This is |
| 114 | * useful in cases where the initial hash state is meant to be |
| 115 | * secret, e.g., for NMAC and HMAC support. |
| 116 | */ |
| 117 | |
| 118 | extern void whirlpool_set(whirlpool_ctx */*ctx*/, const void */*buf*/, |
| 119 | unsigned long /*count*/); |
| 120 | #define whirlpool256_set whirlpool_set |
| 121 | |
| 122 | /* --- @whirlpool_hash@, @whirlpool256_hash@ --- * |
| 123 | * |
| 124 | * Arguments: @whirlpool_ctx *ctx@ = pointer to context block |
| 125 | * @const void *buf@ = buffer of data to hash |
| 126 | * @size_t sz@ = size of buffer to hash |
| 127 | * |
| 128 | * Returns: --- |
| 129 | * |
| 130 | * Use: Hashes a buffer of data. The buffer may be of any size and |
| 131 | * alignment. |
| 132 | */ |
| 133 | |
| 134 | extern void whirlpool_hash(whirlpool_ctx */*ctx*/, |
| 135 | const void */*buf*/, size_t /*sz*/); |
| 136 | #define whirlpool256_hash whirlpool_hash |
| 137 | |
| 138 | /* --- @whirlpool_done@, @whirlpool256_done@ --- * |
| 139 | * |
| 140 | * Arguments: @whirlpool_ctx *ctx@ = pointer to context block |
| 141 | * @void *hash@ = pointer to output buffer |
| 142 | * |
| 143 | * Returns: --- |
| 144 | * |
| 145 | * Use: Returns the hash of the data read so far. |
| 146 | */ |
| 147 | |
| 148 | extern void whirlpool_done(whirlpool_ctx */*ctx*/, void */*hash*/); |
| 149 | extern void whirlpool256_done(whirlpool_ctx */*ctx*/, void */*hash*/); |
| 150 | |
| 151 | /* --- @whirlpool_state@, @whirlpool256_state@ --- * |
| 152 | * |
| 153 | * Arguments: @whirlpool_ctx *ctx@ = pointer to context |
| 154 | * @void *state@ = pointer to buffer for current state |
| 155 | * |
| 156 | * Returns: Number of bytes written to the hash function so far. |
| 157 | * |
| 158 | * Use: Returns the current state of the hash function such that |
| 159 | * it can be passed to @whirlpool_set@. |
| 160 | */ |
| 161 | |
| 162 | extern unsigned long whirlpool_state(whirlpool_ctx */*ctx*/, |
| 163 | void */*state*/); |
| 164 | #define whirlpool256_state whirlpool_state |
| 165 | |
| 166 | /*----- Generic hash interface --------------------------------------------*/ |
| 167 | |
| 168 | extern const gchash whirlpool; |
| 169 | extern const gchash whirlpool256; |
| 170 | |
| 171 | /*----- That's all, folks -------------------------------------------------*/ |
| 172 | |
| 173 | #ifdef __cplusplus |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | #endif |