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 #ifndef CATACOMB_HASH_H
31 #define CATACOMB_HASH_H
37 /*----- Header files ------------------------------------------------------*/
41 #include <mLib/bits.h>
43 /*----- Macros ------------------------------------------------------------*/
45 /* --- @HASH_BUFFER@ --- *
47 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
48 * @ictx@ = pointer to context block for the hash
49 * @ibuf@ = pointer to input data to hash
50 * @isz@ = size of buffer
52 * Use: Handles buffering of input data to a hash function. The
53 * hash's compression function is called when the buffer is
54 * full. Note that the compression function can be called on
55 * data which is at odd alignments; it is expected to cope
56 * gracefully with this (possibly by copying the data into its
57 * internal buffer before starting).
60 #define HASH_BUFFER(PRE, pre, ictx, ibuf, isz) do { \
61 pre##_ctx *_bctx = (ictx); \
62 size_t _bsz = (isz); \
63 const octet *_bbuf = (octet *)(ibuf); \
65 /* --- Add on the size done so far --- * \
67 * Messy, because trapping overflow is difficult when you don't know \
68 * how many bits you've actually got. \
72 uint32 _l = U32(_bsz); \
73 uint32 _h = ((_bsz & ~MASK32) >> 16) >> 16; \
76 if (_bctx->nl < _l || _bctx->nl & ~MASK32) \
80 /* --- Handle very small contributions --- */ \
82 if (_bctx->off + _bsz < PRE##_BUFSZ) { \
83 memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz); \
87 /* --- Handle an initial partial buffer --- */ \
90 size_t s = PRE##_BUFSZ - _bctx->off; \
91 memcpy(_bctx->buf + _bctx->off, _bbuf, s); \
92 pre##_compress(_bctx, _bctx->buf); \
93 _bsz -= s; _bbuf += s; \
96 /* --- Do whole buffers while we can --- */ \
98 while (_bsz >= PRE##_BUFSZ) { \
99 pre##_compress(_bctx, _bbuf); \
100 _bsz -= PRE##_BUFSZ; _bbuf += PRE##_BUFSZ; \
103 /* --- And wrap up at the end --- */ \
106 memcpy(_bctx->buf, _bbuf, _bsz); \
111 /* --- @HASH_PAD@ --- *
113 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
114 * @ictx@ = pointer to context block for the hash
115 * @term@ = terminator character to write following the data
116 * @pad@ = pad character to fill with
117 * @diff@ = size of space to leave at the end of the last block
119 * Use: Does padding for message digest functions.
122 #define HASH_PAD(PRE, pre, ictx, term, pad, diff) do { \
123 pre##_ctx *_pctx = (ictx); \
125 _pctx->buf[_pctx->off] = term; \
127 if (_pctx->off > PRE##_BUFSZ - diff) { \
128 if (_pctx->off < PRE##_BUFSZ) \
129 memset(_pctx->buf + _pctx->off, pad, PRE##_BUFSZ - _pctx->off); \
130 pre##_compress(_pctx, _pctx->buf); \
131 memset(_pctx->buf, pad, PRE##_BUFSZ - diff); \
133 memset(_pctx->buf + _pctx->off, pad, \
134 PRE##_BUFSZ - _pctx->off - diff); \
137 /* --- @HASH_MD5STRENGTH@ --- *
139 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
140 * @ictx@ = pointer to context block for the hash
142 * Use: Does MD5-style MD strengthening. The data is terminated
143 * by a single set bit, padded with zero bits, and then a 64-
144 * bit length is written, little-end first.
147 #define HASH_MD5STRENGTH(PRE, pre, ictx) do { \
148 pre##_ctx *_mctx = (ictx); \
149 HASH_PAD(PRE, pre, _mctx, 0x80u, 0, 8); \
150 STORE32_L(_mctx->buf + PRE##_BUFSZ - 8, _mctx->nl << 3); \
151 STORE32_L(_mctx->buf + PRE##_BUFSZ - 4, \
152 (_mctx->nl >> 29) | (_mctx->nh << 3)); \
153 pre##_compress(_mctx, _mctx->buf); \
156 /* --- @HASH_TEST@ --- *
158 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
160 * Use: Standard test rig for hash functions.
165 #include <mLib/quis.h>
166 #include <mLib/testrig.h>
168 #define HASH_BUFLEN 100000
170 #define HASH_TEST(PRE, pre) \
172 static int verify(dstr *v) \
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 = `%s'\n\texpected = ", \
206 type_hex.dump(&v[1], stdout); \
207 fputs("\n\tcomputed = ", stdout); \
208 type_hex.dump(&d, stdout); \
218 static int verifyrep(dstr *v) \
221 size_t len = v[0].len; \
222 int n = *(int *)v[1].buf; \
227 dstr d = DSTR_INIT; \
229 while (nn < HASH_BUFLEN && (n & 1) == 0) { nd++; nn <<= 1; n >>= 1; } \
231 memcpy(p, v[0].buf, len); \
233 while (nd--) { memcpy(q, p, len); q += len; len <<= 1; } \
235 dstr_ensure(&d, PRE##_HASHSZ); \
236 d.len = PRE##_HASHSZ; \
238 while (n--) pre##_hash(&ctx, p, len); \
239 pre##_done(&ctx, d.buf); \
241 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
242 printf("\nfail:\n\tinput = `%s'\n\treps = `%i'\n\texpected = ", \
243 v[0].buf, *(int *)v[1].buf); \
244 type_hex.dump(&v[2], stdout); \
245 fputs("\n\tcomputed = ", stdout); \
246 type_hex.dump(&d, stdout); \
255 static test_chunk defs[] = { \
256 { #pre, verify, { &type_string, &type_hex, 0 } }, \
257 { #pre "-rep", verifyrep, \
258 { &type_string, &type_int, &type_hex, 0 } }, \
262 int main(int argc, char *argv[]) \
265 test_run(argc, argv, defs, SRCDIR"/tests/" #pre); \
270 # define HASH_TEST(PRE, pre)
273 /*----- That's all, folks -------------------------------------------------*/