3 * $Id: hash.h,v 1.2 1999/12/10 23:16:40 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.2 1999/12/10 23:16:40 mdw
34 * Split mode macros into interface and implementation.
36 * Revision 1.1 1999/09/03 08:41:12 mdw
41 #ifndef CATACOMB_HASH_H
42 #define CATACOMB_HASH_H
48 /*----- Header files ------------------------------------------------------*/
52 #include <mLib/bits.h>
54 /*----- Macros ------------------------------------------------------------*/
56 /* --- @HASH_BUFFER@ --- *
58 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
59 * @ictx@ = pointer to context block for the hash
60 * @ibuf@ = pointer to input data to hash
61 * @isz@ = size of buffer
63 * Use: Handles buffering of input data to a hash function. The
64 * hash's compression function is called when the buffer is
65 * full. Note that the compression function can be called on
66 * data which is at odd alignments; it is expected to cope
67 * gracefully with this (possibly by copying the data into its
68 * internal buffer before starting).
71 #define HASH_BUFFER(PRE, pre, ictx, ibuf, isz) do { \
72 pre##_ctx *_bctx = (ictx); \
73 size_t _bsz = (isz); \
74 const octet *_bbuf = (octet *)(ibuf); \
76 /* --- Add on the size done so far --- * \
78 * Messy, because trapping overflow is difficult when you don't know \
79 * how many bits you've actually got. \
83 uint32 _l = U32(_bsz), _h = (_bsz >> 16) >> 16; \
86 if (_bctx->nl < _l || _bctx->nl & ~MASK32) \
90 /* --- Handle very small contributions --- */ \
92 if (_bctx->off + _bsz < PRE##_BUFSZ) { \
93 memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz); \
97 /* --- Handle an initial partial buffer --- */ \
100 size_t s = PRE##_BUFSZ - _bctx->off; \
101 memcpy(_bctx->buf + _bctx->off, _bbuf, s); \
102 pre##_compress(_bctx, _bctx->buf); \
103 _bsz -= s; _bbuf += s; \
106 /* --- Do whole buffers while we can --- */ \
108 while (_bsz >= PRE##_BUFSZ) { \
109 pre##_compress(_bctx, _bbuf); \
110 _bsz -= PRE##_BUFSZ; _bbuf += PRE##_BUFSZ; \
113 /* --- And wrap up at the end --- */ \
116 memcpy(_bctx->buf, _bbuf, _bsz); \
121 /* --- @HASH_PAD@ --- *
123 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
124 * @ictx@ = pointer to context block for the hash
125 * @term@ = terminator character to write following the data
126 * @pad@ = pad character to fill with
127 * @diff@ = size of space to leave at the end of the last block
129 * Use: Does padding for message digest functions.
132 #define HASH_PAD(PRE, pre, ictx, term, pad, diff) do { \
133 pre##_ctx *_pctx = (ictx); \
135 _pctx->buf[_pctx->off] = term; \
137 if (_pctx->off > PRE##_BUFSZ - diff) { \
138 if (_pctx->off < PRE##_BUFSZ) \
139 memset(_pctx->buf + _pctx->off, pad, PRE##_BUFSZ - _pctx->off); \
140 pre##_compress(_pctx, _pctx->buf); \
141 memset(_pctx->buf, pad, PRE##_BUFSZ - diff); \
143 memset(_pctx->buf + _pctx->off, pad, \
144 PRE##_BUFSZ - _pctx->off - diff); \
147 /* --- @HASH_MD5STRENGTH@ --- *
149 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
150 * @ictx@ = pointer to context block for the hash
152 * Use: Does MD5-style MD strengthening. The data is terminated
153 * by a single set bit, padded with zero bits, and then a 64-
154 * bit length is written, little-end first.
157 #define HASH_MD5STRENGTH(PRE, pre, ictx) do { \
158 pre##_ctx *_mctx = (ictx); \
159 HASH_PAD(PRE, pre, _mctx, 0x80u, 0, 8); \
160 STORE32_L(_mctx->buf + PRE##_BUFSZ - 8, _mctx->nl << 3); \
161 STORE32_L(_mctx->buf + PRE##_BUFSZ - 4, \
162 (_mctx->nl >> 29) | (_mctx->nh << 3)); \
163 pre##_compress(_mctx, _mctx->buf); \
166 /* --- @HASH_TEST@ --- *
168 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
170 * Use: Standard test rig for hash functions.
175 #include <mLib/quis.h>
176 #include <mLib/testrig.h>
178 #define HASH_TEST(PRE, pre) \
180 static int verify(dstr *v) \
186 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
191 dstr_ensure(&d, PRE##_HASHSZ); \
192 d.len = PRE##_HASHSZ; \
194 for (ip = szs; *ip; ip++) { \
201 p = (octet *)v[0].buf; \
206 pre##_hash(&ctx, p, i); \
210 pre##_done(&ctx, d.buf); \
211 if (memcmp(d.buf, v[1].buf, PRE##_HASHSZ) != 0) { \
212 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\texpected = ", \
214 type_hex.dump(&v[1], stdout); \
215 fputs("\n\tcomputed = ", stdout); \
216 type_hex.dump(&d, stdout); \
226 static test_chunk defs[] = { \
227 { #pre, verify, { &type_string, &type_hex, 0 } }, \
231 int main(int argc, char *argv[]) \
234 test_run(argc, argv, defs, SRCDIR"/tests/" #pre); \
239 # define HASH_TEST(PRE, pre)
242 /*----- That's all, folks -------------------------------------------------*/