3 * $Id: hash.h,v 1.4 2004/03/21 22:42:27 mdw Exp $
5 * Generic handling for message digest functions
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 2004/03/21 22:42:27 mdw
34 * Test hashing on long strings.
36 * Revision 1.3 2000/06/17 11:23:27 mdw
37 * Portability fix for broken compilers.
39 * Revision 1.2 1999/12/10 23:16:40 mdw
40 * Split mode macros into interface and implementation.
42 * Revision 1.1 1999/09/03 08:41:12 mdw
47 #ifndef CATACOMB_HASH_H
48 #define CATACOMB_HASH_H
54 /*----- Header files ------------------------------------------------------*/
58 #include <mLib/bits.h>
60 /*----- Macros ------------------------------------------------------------*/
62 /* --- @HASH_BUFFER@ --- *
64 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
65 * @ictx@ = pointer to context block for the hash
66 * @ibuf@ = pointer to input data to hash
67 * @isz@ = size of buffer
69 * Use: Handles buffering of input data to a hash function. The
70 * hash's compression function is called when the buffer is
71 * full. Note that the compression function can be called on
72 * data which is at odd alignments; it is expected to cope
73 * gracefully with this (possibly by copying the data into its
74 * internal buffer before starting).
77 #define HASH_BUFFER(PRE, pre, ictx, ibuf, isz) do { \
78 pre##_ctx *_bctx = (ictx); \
79 size_t _bsz = (isz); \
80 const octet *_bbuf = (octet *)(ibuf); \
82 /* --- Add on the size done so far --- * \
84 * Messy, because trapping overflow is difficult when you don't know \
85 * how many bits you've actually got. \
89 uint32 _l = U32(_bsz); \
90 uint32 _h = ((_bsz & ~MASK32) >> 16) >> 16; \
93 if (_bctx->nl < _l || _bctx->nl & ~MASK32) \
97 /* --- Handle very small contributions --- */ \
99 if (_bctx->off + _bsz < PRE##_BUFSZ) { \
100 memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz); \
101 _bctx->off += _bsz; \
104 /* --- Handle an initial partial buffer --- */ \
107 size_t s = PRE##_BUFSZ - _bctx->off; \
108 memcpy(_bctx->buf + _bctx->off, _bbuf, s); \
109 pre##_compress(_bctx, _bctx->buf); \
110 _bsz -= s; _bbuf += s; \
113 /* --- Do whole buffers while we can --- */ \
115 while (_bsz >= PRE##_BUFSZ) { \
116 pre##_compress(_bctx, _bbuf); \
117 _bsz -= PRE##_BUFSZ; _bbuf += PRE##_BUFSZ; \
120 /* --- And wrap up at the end --- */ \
123 memcpy(_bctx->buf, _bbuf, _bsz); \
128 /* --- @HASH_PAD@ --- *
130 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
131 * @ictx@ = pointer to context block for the hash
132 * @term@ = terminator character to write following the data
133 * @pad@ = pad character to fill with
134 * @diff@ = size of space to leave at the end of the last block
136 * Use: Does padding for message digest functions.
139 #define HASH_PAD(PRE, pre, ictx, term, pad, diff) do { \
140 pre##_ctx *_pctx = (ictx); \
142 _pctx->buf[_pctx->off] = term; \
144 if (_pctx->off > PRE##_BUFSZ - diff) { \
145 if (_pctx->off < PRE##_BUFSZ) \
146 memset(_pctx->buf + _pctx->off, pad, PRE##_BUFSZ - _pctx->off); \
147 pre##_compress(_pctx, _pctx->buf); \
148 memset(_pctx->buf, pad, PRE##_BUFSZ - diff); \
150 memset(_pctx->buf + _pctx->off, pad, \
151 PRE##_BUFSZ - _pctx->off - diff); \
154 /* --- @HASH_MD5STRENGTH@ --- *
156 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
157 * @ictx@ = pointer to context block for the hash
159 * Use: Does MD5-style MD strengthening. The data is terminated
160 * by a single set bit, padded with zero bits, and then a 64-
161 * bit length is written, little-end first.
164 #define HASH_MD5STRENGTH(PRE, pre, ictx) do { \
165 pre##_ctx *_mctx = (ictx); \
166 HASH_PAD(PRE, pre, _mctx, 0x80u, 0, 8); \
167 STORE32_L(_mctx->buf + PRE##_BUFSZ - 8, _mctx->nl << 3); \
168 STORE32_L(_mctx->buf + PRE##_BUFSZ - 4, \
169 (_mctx->nl >> 29) | (_mctx->nh << 3)); \
170 pre##_compress(_mctx, _mctx->buf); \
173 /* --- @HASH_TEST@ --- *
175 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
177 * Use: Standard test rig for hash functions.
182 #include <mLib/quis.h>
183 #include <mLib/testrig.h>
185 #define HASH_BUFLEN 100000
187 #define HASH_TEST(PRE, pre) \
189 static int verify(dstr *v) \
195 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
200 dstr_ensure(&d, PRE##_HASHSZ); \
201 d.len = PRE##_HASHSZ; \
203 for (ip = szs; *ip; ip++) { \
210 p = (octet *)v[0].buf; \
215 pre##_hash(&ctx, p, i); \
219 pre##_done(&ctx, d.buf); \
220 if (memcmp(d.buf, v[1].buf, PRE##_HASHSZ) != 0) { \
221 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\texpected = ", \
223 type_hex.dump(&v[1], stdout); \
224 fputs("\n\tcomputed = ", stdout); \
225 type_hex.dump(&d, stdout); \
235 static int verifyrep(dstr *v) \
238 size_t len = v[0].len; \
239 int n = *(int *)v[1].buf; \
244 dstr d = DSTR_INIT; \
246 while (nn < HASH_BUFLEN && (n & 1) == 0) { nd++; nn <<= 1; n >>= 1; } \
248 memcpy(p, v[0].buf, len); \
250 while (nd--) { memcpy(q, p, len); q += len; len <<= 1; } \
252 dstr_ensure(&d, PRE##_HASHSZ); \
253 d.len = PRE##_HASHSZ; \
255 while (n--) pre##_hash(&ctx, p, len); \
256 pre##_done(&ctx, d.buf); \
258 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
259 printf("\nfail:\n\tinput = `%s'\n\treps = `%i'\n\texpected = ", \
260 v[0].buf, *(int *)v[1].buf); \
261 type_hex.dump(&v[2], stdout); \
262 fputs("\n\tcomputed = ", stdout); \
263 type_hex.dump(&d, stdout); \
272 static test_chunk defs[] = { \
273 { #pre, verify, { &type_string, &type_hex, 0 } }, \
274 { #pre "-rep", verifyrep, \
275 { &type_string, &type_int, &type_hex, 0 } }, \
279 int main(int argc, char *argv[]) \
282 test_run(argc, argv, defs, SRCDIR"/tests/" #pre); \
287 # define HASH_TEST(PRE, pre)
290 /*----- That's all, folks -------------------------------------------------*/