d03ab969 |
1 | /* -*-c-*- |
2 | * |
79ba130c |
3 | * $Id: hash.h,v 1.2 1999/12/10 23:16:40 mdw Exp $ |
d03ab969 |
4 | * |
5 | * Generic handling for message digest functions |
6 | * |
7 | * (c) 1998 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
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. |
18 | * |
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. |
23 | * |
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, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: hash.h,v $ |
79ba130c |
33 | * Revision 1.2 1999/12/10 23:16:40 mdw |
34 | * Split mode macros into interface and implementation. |
35 | * |
d03ab969 |
36 | * Revision 1.1 1999/09/03 08:41:12 mdw |
37 | * Initial import. |
38 | * |
39 | */ |
40 | |
79ba130c |
41 | #ifndef CATACOMB_HASH_H |
42 | #define CATACOMB_HASH_H |
d03ab969 |
43 | |
44 | #ifdef __cplusplus |
45 | extern "C" { |
46 | #endif |
47 | |
48 | /*----- Header files ------------------------------------------------------*/ |
49 | |
50 | #include <string.h> |
51 | |
52 | #include <mLib/bits.h> |
53 | |
54 | /*----- Macros ------------------------------------------------------------*/ |
55 | |
56 | /* --- @HASH_BUFFER@ --- * |
57 | * |
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 |
62 | * |
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). |
69 | */ |
70 | |
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); \ |
75 | \ |
79ba130c |
76 | /* --- Add on the size done so far --- * \ |
77 | * \ |
78 | * Messy, because trapping overflow is difficult when you don't know \ |
79 | * how many bits you've actually got. \ |
80 | */ \ |
d03ab969 |
81 | \ |
79ba130c |
82 | { \ |
83 | uint32 _l = U32(_bsz), _h = (_bsz >> 16) >> 16; \ |
84 | _bctx->nh += _h; \ |
85 | _bctx->nl += _l; \ |
86 | if (_bctx->nl < _l || _bctx->nl & ~MASK32) \ |
87 | _bctx->nh++; \ |
88 | } \ |
d03ab969 |
89 | \ |
90 | /* --- Handle very small contributions --- */ \ |
91 | \ |
92 | if (_bctx->off + _bsz < PRE##_BUFSZ) { \ |
93 | memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz); \ |
94 | _bctx->off += _bsz; \ |
95 | } else { \ |
96 | \ |
97 | /* --- Handle an initial partial buffer --- */ \ |
98 | \ |
99 | if (_bctx->off) { \ |
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; \ |
104 | } \ |
105 | \ |
106 | /* --- Do whole buffers while we can --- */ \ |
107 | \ |
108 | while (_bsz >= PRE##_BUFSZ) { \ |
109 | pre##_compress(_bctx, _bbuf); \ |
110 | _bsz -= PRE##_BUFSZ; _bbuf += PRE##_BUFSZ; \ |
111 | } \ |
112 | \ |
113 | /* --- And wrap up at the end --- */ \ |
114 | \ |
115 | if (_bsz) \ |
116 | memcpy(_bctx->buf, _bbuf, _bsz); \ |
117 | _bctx->off = _bsz; \ |
118 | } \ |
119 | } while (0) |
120 | |
121 | /* --- @HASH_PAD@ --- * |
122 | * |
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 |
128 | * |
129 | * Use: Does padding for message digest functions. |
130 | */ |
131 | |
132 | #define HASH_PAD(PRE, pre, ictx, term, pad, diff) do { \ |
133 | pre##_ctx *_pctx = (ictx); \ |
134 | \ |
135 | _pctx->buf[_pctx->off] = term; \ |
136 | _pctx->off++; \ |
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); \ |
142 | } else \ |
143 | memset(_pctx->buf + _pctx->off, pad, \ |
144 | PRE##_BUFSZ - _pctx->off - diff); \ |
145 | } while (0) |
146 | |
147 | /* --- @HASH_MD5STRENGTH@ --- * |
148 | * |
149 | * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions |
150 | * @ictx@ = pointer to context block for the hash |
151 | * |
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. |
155 | */ |
156 | |
157 | #define HASH_MD5STRENGTH(PRE, pre, ictx) do { \ |
158 | pre##_ctx *_mctx = (ictx); \ |
159 | HASH_PAD(PRE, pre, _mctx, 0x80u, 0, 8); \ |
79ba130c |
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)); \ |
d03ab969 |
163 | pre##_compress(_mctx, _mctx->buf); \ |
164 | } while (0) |
165 | |
166 | /* --- @HASH_TEST@ --- * |
167 | * |
168 | * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions |
169 | * |
170 | * Use: Standard test rig for hash functions. |
171 | */ |
172 | |
173 | #ifdef TEST_RIG |
174 | |
175 | #include <mLib/quis.h> |
176 | #include <mLib/testrig.h> |
177 | |
178 | #define HASH_TEST(PRE, pre) \ |
179 | \ |
180 | static int verify(dstr *v) \ |
181 | { \ |
182 | pre##_ctx ctx; \ |
183 | int ok = 1; \ |
184 | int i; \ |
185 | octet *p; \ |
186 | int szs[] = { 1, 7, 192, -1, 0 }, *ip; \ |
187 | size_t sz; \ |
188 | dstr d; \ |
189 | \ |
190 | dstr_create(&d); \ |
191 | dstr_ensure(&d, PRE##_HASHSZ); \ |
192 | d.len = PRE##_HASHSZ; \ |
193 | \ |
194 | for (ip = szs; *ip; ip++) { \ |
195 | i = *ip; \ |
196 | sz = v[0].len; \ |
197 | if (i == -1) \ |
198 | i = sz; \ |
199 | if (i > sz) \ |
200 | continue; \ |
201 | p = (octet *)v[0].buf; \ |
202 | pre##_init(&ctx); \ |
203 | while (sz) { \ |
204 | if (i > sz) \ |
205 | i = sz; \ |
206 | pre##_hash(&ctx, p, i); \ |
207 | p += i; \ |
208 | sz -= i; \ |
209 | } \ |
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 = ", \ |
213 | *ip, v[0].buf); \ |
214 | type_hex.dump(&v[1], stdout); \ |
215 | fputs("\n\tcomputed = ", stdout); \ |
216 | type_hex.dump(&d, stdout); \ |
217 | putchar('\n'); \ |
218 | ok = 0; \ |
219 | } \ |
220 | } \ |
221 | \ |
222 | dstr_destroy(&d); \ |
223 | return (ok); \ |
224 | } \ |
225 | \ |
226 | static test_chunk defs[] = { \ |
227 | { #pre, verify, { &type_string, &type_hex, 0 } }, \ |
228 | { 0, 0, { 0 } } \ |
229 | }; \ |
230 | \ |
231 | int main(int argc, char *argv[]) \ |
232 | { \ |
233 | ego(argv[0]); \ |
234 | test_run(argc, argv, defs, SRCDIR"/tests/" #pre); \ |
235 | return (0); \ |
236 | } |
237 | |
238 | #else |
239 | # define HASH_TEST(PRE, pre) |
240 | #endif |
241 | |
242 | /*----- That's all, folks -------------------------------------------------*/ |
243 | |
244 | #ifdef __cplusplus |
245 | } |
246 | #endif |
247 | |
248 | #endif |