3 * Generic handling for message digest functions
5 * (c) 1998 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 #ifndef CATACOMB_HASH_H
29 #define CATACOMB_HASH_H
35 /*----- Header files ------------------------------------------------------*/
39 #include <mLib/bits.h>
41 /*----- Macros ------------------------------------------------------------*/
43 /* --- @HASH_BUFFER@ --- *
45 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
46 * @ictx@ = pointer to context block for the hash
47 * @ibuf@ = pointer to input data to hash
48 * @isz@ = size of buffer
50 * Use: Handles buffering of input data to a hash function. The
51 * hash's compression function is called when the buffer is
52 * full. Note that the compression function can be called on
53 * data which is at odd alignments; it is expected to cope
54 * gracefully with this (possibly by copying the data into its
55 * internal buffer before starting).
58 #define HASH_BUFFER(PRE, pre, ictx, ibuf, isz) do { \
59 pre##_ctx *_bctx = (ictx); \
60 size_t _bsz = (isz); \
61 const octet *_bbuf = (octet *)(ibuf); \
63 /* --- Add on the size done so far --- * \
65 * Messy, because trapping overflow is difficult when you don't know \
66 * how many bits you've actually got. \
70 uint32 _l = U32(_bsz); \
71 uint32 _h = ((_bsz & ~(size_t)MASK32) >> 16) >> 16; \
74 if (_bctx->nl < _l || _bctx->nl & ~(uint32)MASK32) \
78 /* --- Handle very small contributions --- */ \
80 if (_bctx->off + _bsz < PRE##_BUFSZ) { \
81 memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz); \
85 /* --- Handle an initial partial buffer --- */ \
88 size_t s = PRE##_BUFSZ - _bctx->off; \
89 memcpy(_bctx->buf + _bctx->off, _bbuf, s); \
90 pre##_compress(_bctx, _bctx->buf); \
91 _bsz -= s; _bbuf += s; \
94 /* --- Do whole buffers while we can --- */ \
96 while (_bsz >= PRE##_BUFSZ) { \
97 pre##_compress(_bctx, _bbuf); \
98 _bsz -= PRE##_BUFSZ; _bbuf += PRE##_BUFSZ; \
101 /* --- And wrap up at the end --- */ \
104 memcpy(_bctx->buf, _bbuf, _bsz); \
109 /* --- @HASH_PAD@ --- *
111 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
112 * @ictx@ = pointer to context block for the hash
113 * @term@ = terminator character to write following the data
114 * @pad@ = pad character to fill with
115 * @diff@ = size of space to leave at the end of the last block
117 * Use: Does padding for message digest functions.
120 #define HASH_PAD(PRE, pre, ictx, term, pad, diff) do { \
121 pre##_ctx *_pctx = (ictx); \
123 _pctx->buf[_pctx->off] = term; \
125 if (_pctx->off > PRE##_BUFSZ - diff) { \
126 if (_pctx->off < PRE##_BUFSZ) \
127 memset(_pctx->buf + _pctx->off, pad, PRE##_BUFSZ - _pctx->off); \
128 pre##_compress(_pctx, _pctx->buf); \
129 memset(_pctx->buf, pad, PRE##_BUFSZ - diff); \
131 memset(_pctx->buf + _pctx->off, pad, \
132 PRE##_BUFSZ - _pctx->off - diff); \
135 /* --- @HASH_MD5STRENGTH@ --- *
137 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
138 * @ictx@ = pointer to context block for the hash
140 * Use: Does MD5-style MD strengthening. The data is terminated
141 * by a single set bit, padded with zero bits, and then a 64-
142 * bit length is written, little-end first.
145 #define HASH_MD5STRENGTH(PRE, pre, ictx) do { \
146 pre##_ctx *_mctx = (ictx); \
147 HASH_PAD(PRE, pre, _mctx, 0x80u, 0, 8); \
148 STORE32_L(_mctx->buf + PRE##_BUFSZ - 8, _mctx->nl << 3); \
149 STORE32_L(_mctx->buf + PRE##_BUFSZ - 4, \
150 (_mctx->nl >> 29) | (_mctx->nh << 3)); \
151 pre##_compress(_mctx, _mctx->buf); \
154 /* --- @HASH_TEST@ --- *
156 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
158 * Use: Standard test rig for hash functions.
163 #include <mLib/quis.h>
164 #include <mLib/testrig.h>
166 #define HASH_BUFLEN 100000
168 #define HASH_VERIFY(PRE, pre) HASH_VERIFYX(PRE, pre, #pre)
170 #define HASH_VERIFYX(PRE, pre, name) \
172 static int vrf_##pre(dstr *v, const test_type *msgty) \
178 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
183 dstr_ensure(&d, PRE##_HASHSZ); \
184 d.len = PRE##_HASHSZ; \
186 for (ip = szs; *ip; ip++) { \
193 p = (octet *)v[0].buf; \
198 pre##_hash(&ctx, p, i); \
202 pre##_done(&ctx, d.buf); \
203 if (memcmp(d.buf, v[1].buf, PRE##_HASHSZ) != 0) { \
204 printf("\nfail:\n\tstep = %i\n\tinput = ", *ip); \
205 msgty->dump(&v[0], stdout); \
206 printf("\n\texpected = "); \
207 type_hex.dump(&v[1], stdout); \
208 fputs("\n\tcomputed = ", stdout); \
209 type_hex.dump(&d, stdout); \
219 static int vrf_##pre##_hex(dstr *v) \
220 { return vrf_##pre(v, &type_hex); } \
221 static int vrf_##pre##_lit(dstr *v) \
222 { return vrf_##pre(v, &type_string); } \
224 static int vrf_##pre##_rep(dstr *v) \
227 size_t len = v[0].len; \
228 int n = *(int *)v[1].buf; \
233 dstr d = DSTR_INIT; \
235 while (nn < HASH_BUFLEN && (n & 1) == 0) { nd++; nn <<= 1; n >>= 1; } \
237 memcpy(p, v[0].buf, len); \
239 while (nd--) { memcpy(q, p, len); q += len; len <<= 1; } \
241 dstr_ensure(&d, PRE##_HASHSZ); \
242 d.len = PRE##_HASHSZ; \
244 while (n--) pre##_hash(&ctx, p, len); \
245 pre##_done(&ctx, d.buf); \
247 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
248 printf("\nfail:\n\tinput = `%s'\n\treps = `%i'\n\texpected = ", \
249 v[0].buf, *(int *)v[1].buf); \
250 type_hex.dump(&v[2], stdout); \
251 fputs("\n\tcomputed = ", stdout); \
252 type_hex.dump(&d, stdout); \
261 #define HASH_TESTDEFS(PRE, pre) HASH_TESTDEFSX(PRE, pre, #pre)
263 #define HASH_TESTDEFSX(PRE, pre, name) \
264 { name, vrf_##pre##_lit, { &type_string, &type_hex, 0 } }, \
265 { name "-hex", vrf_##pre##_hex, { &type_hex, &type_hex, 0 } }, \
266 { name "-rep", vrf_##pre##_rep, \
267 { &type_string, &type_int, &type_hex, 0 } },
269 #define HASH_TESTX(PRE, pre, name, fname) \
271 HASH_VERIFYX(PRE, pre, name) \
273 static test_chunk defs[] = { \
274 HASH_TESTDEFSX(PRE, pre, name) \
278 int main(int argc, char *argv[]) \
281 test_run(argc, argv, defs, SRCDIR"/t/" fname); \
286 # define HASH_TESTX(PRE, pre, name, fname)
289 #define HASH_TEST(PRE, pre) HASH_TESTX(PRE, pre, #pre, #pre)
291 /*----- That's all, folks -------------------------------------------------*/