3 * $Id: hash.h,v 1.1 1999/09/03 08:41:12 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.1 1999/09/03 08:41:12 mdw
45 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/bits.h>
51 /*----- Macros ------------------------------------------------------------*/
53 /* --- @HASH_BUFFER@ --- *
55 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
56 * @ictx@ = pointer to context block for the hash
57 * @ibuf@ = pointer to input data to hash
58 * @isz@ = size of buffer
60 * Use: Handles buffering of input data to a hash function. The
61 * hash's compression function is called when the buffer is
62 * full. Note that the compression function can be called on
63 * data which is at odd alignments; it is expected to cope
64 * gracefully with this (possibly by copying the data into its
65 * internal buffer before starting).
68 #define HASH_BUFFER(PRE, pre, ictx, ibuf, isz) do { \
69 pre##_ctx *_bctx = (ictx); \
70 size_t _bsz = (isz); \
71 const octet *_bbuf = (octet *)(ibuf); \
73 /* --- Add on the size done so far --- */ \
75 _bctx->count += _bsz; \
77 /* --- Handle very small contributions --- */ \
79 if (_bctx->off + _bsz < PRE##_BUFSZ) { \
80 memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz); \
84 /* --- Handle an initial partial buffer --- */ \
87 size_t s = PRE##_BUFSZ - _bctx->off; \
88 memcpy(_bctx->buf + _bctx->off, _bbuf, s); \
89 pre##_compress(_bctx, _bctx->buf); \
90 _bsz -= s; _bbuf += s; \
93 /* --- Do whole buffers while we can --- */ \
95 while (_bsz >= PRE##_BUFSZ) { \
96 pre##_compress(_bctx, _bbuf); \
97 _bsz -= PRE##_BUFSZ; _bbuf += PRE##_BUFSZ; \
100 /* --- And wrap up at the end --- */ \
103 memcpy(_bctx->buf, _bbuf, _bsz); \
108 /* --- @HASH_PAD@ --- *
110 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
111 * @ictx@ = pointer to context block for the hash
112 * @term@ = terminator character to write following the data
113 * @pad@ = pad character to fill with
114 * @diff@ = size of space to leave at the end of the last block
116 * Use: Does padding for message digest functions.
119 #define HASH_PAD(PRE, pre, ictx, term, pad, diff) do { \
120 pre##_ctx *_pctx = (ictx); \
122 _pctx->buf[_pctx->off] = term; \
124 if (_pctx->off > PRE##_BUFSZ - diff) { \
125 if (_pctx->off < PRE##_BUFSZ) \
126 memset(_pctx->buf + _pctx->off, pad, PRE##_BUFSZ - _pctx->off); \
127 pre##_compress(_pctx, _pctx->buf); \
128 memset(_pctx->buf, pad, PRE##_BUFSZ - diff); \
130 memset(_pctx->buf + _pctx->off, pad, \
131 PRE##_BUFSZ - _pctx->off - diff); \
134 /* --- @HASH_MD5STRENGTH@ --- *
136 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
137 * @ictx@ = pointer to context block for the hash
139 * Use: Does MD5-style MD strengthening. The data is terminated
140 * by a single set bit, padded with zero bits, and then a 64-
141 * bit length is written, little-end first.
144 #define HASH_MD5STRENGTH(PRE, pre, ictx) do { \
145 pre##_ctx *_mctx = (ictx); \
146 HASH_PAD(PRE, pre, _mctx, 0x80u, 0, 8); \
147 STORE32_L(_mctx->buf + PRE##_BUFSZ - 8, _mctx->count << 3); \
148 STORE32_L(_mctx->buf + PRE##_BUFSZ - 4, _mctx->count >> 29); \
149 pre##_compress(_mctx, _mctx->buf); \
152 /* --- @HASH_TEST@ --- *
154 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
156 * Use: Standard test rig for hash functions.
161 #include <mLib/quis.h>
162 #include <mLib/testrig.h>
164 #define HASH_TEST(PRE, pre) \
166 static int verify(dstr *v) \
172 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
177 dstr_ensure(&d, PRE##_HASHSZ); \
178 d.len = PRE##_HASHSZ; \
180 for (ip = szs; *ip; ip++) { \
187 p = (octet *)v[0].buf; \
192 pre##_hash(&ctx, p, i); \
196 pre##_done(&ctx, d.buf); \
197 if (memcmp(d.buf, v[1].buf, PRE##_HASHSZ) != 0) { \
198 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\texpected = ", \
200 type_hex.dump(&v[1], stdout); \
201 fputs("\n\tcomputed = ", stdout); \
202 type_hex.dump(&d, stdout); \
212 static test_chunk defs[] = { \
213 { #pre, verify, { &type_string, &type_hex, 0 } }, \
217 int main(int argc, char *argv[]) \
220 test_run(argc, argv, defs, SRCDIR"/tests/" #pre); \
225 # define HASH_TEST(PRE, pre)
228 /*----- That's all, folks -------------------------------------------------*/