d03ab969 |
1 | /* -*-c-*- |
d03ab969 |
2 | * |
3 | * Generic handling for message digest functions |
4 | * |
5 | * (c) 1998 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
d03ab969 |
9 | * |
10 | * This file is part of Catacomb. |
11 | * |
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. |
45c0fd36 |
16 | * |
d03ab969 |
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. |
45c0fd36 |
21 | * |
d03ab969 |
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, |
25 | * MA 02111-1307, USA. |
26 | */ |
27 | |
79ba130c |
28 | #ifndef CATACOMB_HASH_H |
29 | #define CATACOMB_HASH_H |
d03ab969 |
30 | |
31 | #ifdef __cplusplus |
32 | extern "C" { |
33 | #endif |
34 | |
35 | /*----- Header files ------------------------------------------------------*/ |
36 | |
37 | #include <string.h> |
38 | |
39 | #include <mLib/bits.h> |
40 | |
41 | /*----- Macros ------------------------------------------------------------*/ |
42 | |
43 | /* --- @HASH_BUFFER@ --- * |
44 | * |
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 |
49 | * |
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). |
56 | */ |
57 | |
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); \ |
62 | \ |
79ba130c |
63 | /* --- Add on the size done so far --- * \ |
64 | * \ |
65 | * Messy, because trapping overflow is difficult when you don't know \ |
66 | * how many bits you've actually got. \ |
67 | */ \ |
d03ab969 |
68 | \ |
79ba130c |
69 | { \ |
9a707709 |
70 | uint32 _l = U32(_bsz); \ |
71 | uint32 _h = ((_bsz & ~MASK32) >> 16) >> 16; \ |
79ba130c |
72 | _bctx->nh += _h; \ |
73 | _bctx->nl += _l; \ |
74 | if (_bctx->nl < _l || _bctx->nl & ~MASK32) \ |
75 | _bctx->nh++; \ |
76 | } \ |
d03ab969 |
77 | \ |
78 | /* --- Handle very small contributions --- */ \ |
79 | \ |
80 | if (_bctx->off + _bsz < PRE##_BUFSZ) { \ |
81 | memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz); \ |
82 | _bctx->off += _bsz; \ |
83 | } else { \ |
84 | \ |
85 | /* --- Handle an initial partial buffer --- */ \ |
86 | \ |
87 | if (_bctx->off) { \ |
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; \ |
92 | } \ |
93 | \ |
94 | /* --- Do whole buffers while we can --- */ \ |
95 | \ |
96 | while (_bsz >= PRE##_BUFSZ) { \ |
97 | pre##_compress(_bctx, _bbuf); \ |
98 | _bsz -= PRE##_BUFSZ; _bbuf += PRE##_BUFSZ; \ |
99 | } \ |
100 | \ |
101 | /* --- And wrap up at the end --- */ \ |
102 | \ |
103 | if (_bsz) \ |
104 | memcpy(_bctx->buf, _bbuf, _bsz); \ |
105 | _bctx->off = _bsz; \ |
106 | } \ |
107 | } while (0) |
108 | |
109 | /* --- @HASH_PAD@ --- * |
110 | * |
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 |
116 | * |
117 | * Use: Does padding for message digest functions. |
118 | */ |
119 | |
120 | #define HASH_PAD(PRE, pre, ictx, term, pad, diff) do { \ |
121 | pre##_ctx *_pctx = (ictx); \ |
122 | \ |
123 | _pctx->buf[_pctx->off] = term; \ |
124 | _pctx->off++; \ |
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); \ |
130 | } else \ |
131 | memset(_pctx->buf + _pctx->off, pad, \ |
132 | PRE##_BUFSZ - _pctx->off - diff); \ |
133 | } while (0) |
134 | |
135 | /* --- @HASH_MD5STRENGTH@ --- * |
136 | * |
137 | * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions |
138 | * @ictx@ = pointer to context block for the hash |
139 | * |
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. |
143 | */ |
144 | |
145 | #define HASH_MD5STRENGTH(PRE, pre, ictx) do { \ |
146 | pre##_ctx *_mctx = (ictx); \ |
147 | HASH_PAD(PRE, pre, _mctx, 0x80u, 0, 8); \ |
79ba130c |
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)); \ |
d03ab969 |
151 | pre##_compress(_mctx, _mctx->buf); \ |
152 | } while (0) |
153 | |
154 | /* --- @HASH_TEST@ --- * |
155 | * |
156 | * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions |
157 | * |
158 | * Use: Standard test rig for hash functions. |
159 | */ |
160 | |
161 | #ifdef TEST_RIG |
162 | |
163 | #include <mLib/quis.h> |
164 | #include <mLib/testrig.h> |
165 | |
e9926004 |
166 | #define HASH_BUFLEN 100000 |
167 | |
d03ab969 |
168 | #define HASH_TEST(PRE, pre) \ |
169 | \ |
170 | static int verify(dstr *v) \ |
171 | { \ |
172 | pre##_ctx ctx; \ |
173 | int ok = 1; \ |
174 | int i; \ |
175 | octet *p; \ |
176 | int szs[] = { 1, 7, 192, -1, 0 }, *ip; \ |
177 | size_t sz; \ |
178 | dstr d; \ |
179 | \ |
180 | dstr_create(&d); \ |
181 | dstr_ensure(&d, PRE##_HASHSZ); \ |
182 | d.len = PRE##_HASHSZ; \ |
183 | \ |
184 | for (ip = szs; *ip; ip++) { \ |
185 | i = *ip; \ |
186 | sz = v[0].len; \ |
187 | if (i == -1) \ |
188 | i = sz; \ |
189 | if (i > sz) \ |
190 | continue; \ |
191 | p = (octet *)v[0].buf; \ |
192 | pre##_init(&ctx); \ |
193 | while (sz) { \ |
194 | if (i > sz) \ |
195 | i = sz; \ |
196 | pre##_hash(&ctx, p, i); \ |
197 | p += i; \ |
198 | sz -= i; \ |
199 | } \ |
200 | pre##_done(&ctx, d.buf); \ |
201 | if (memcmp(d.buf, v[1].buf, PRE##_HASHSZ) != 0) { \ |
202 | printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\texpected = ", \ |
203 | *ip, v[0].buf); \ |
204 | type_hex.dump(&v[1], stdout); \ |
205 | fputs("\n\tcomputed = ", stdout); \ |
206 | type_hex.dump(&d, stdout); \ |
207 | putchar('\n'); \ |
208 | ok = 0; \ |
209 | } \ |
210 | } \ |
211 | \ |
212 | dstr_destroy(&d); \ |
213 | return (ok); \ |
214 | } \ |
215 | \ |
e9926004 |
216 | static int verifyrep(dstr *v) \ |
217 | { \ |
218 | pre##_ctx ctx; \ |
219 | size_t len = v[0].len; \ |
220 | int n = *(int *)v[1].buf; \ |
221 | int nd = 0; \ |
222 | int nn = len; \ |
223 | int ok = 1; \ |
224 | octet *p, *q; \ |
225 | dstr d = DSTR_INIT; \ |
226 | \ |
227 | while (nn < HASH_BUFLEN && (n & 1) == 0) { nd++; nn <<= 1; n >>= 1; } \ |
228 | p = xmalloc(nn); \ |
229 | memcpy(p, v[0].buf, len); \ |
230 | q = p + len; \ |
231 | while (nd--) { memcpy(q, p, len); q += len; len <<= 1; } \ |
232 | \ |
233 | dstr_ensure(&d, PRE##_HASHSZ); \ |
234 | d.len = PRE##_HASHSZ; \ |
235 | pre##_init(&ctx); \ |
236 | while (n--) pre##_hash(&ctx, p, len); \ |
237 | pre##_done(&ctx, d.buf); \ |
238 | \ |
239 | if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \ |
240 | printf("\nfail:\n\tinput = `%s'\n\treps = `%i'\n\texpected = ", \ |
241 | v[0].buf, *(int *)v[1].buf); \ |
242 | type_hex.dump(&v[2], stdout); \ |
243 | fputs("\n\tcomputed = ", stdout); \ |
244 | type_hex.dump(&d, stdout); \ |
245 | putchar('\n'); \ |
246 | ok = 0; \ |
247 | } \ |
12ed8a1f |
248 | xfree(p); \ |
e9926004 |
249 | dstr_destroy(&d); \ |
250 | return (ok); \ |
251 | } \ |
252 | \ |
d03ab969 |
253 | static test_chunk defs[] = { \ |
254 | { #pre, verify, { &type_string, &type_hex, 0 } }, \ |
e9926004 |
255 | { #pre "-rep", verifyrep, \ |
256 | { &type_string, &type_int, &type_hex, 0 } }, \ |
d03ab969 |
257 | { 0, 0, { 0 } } \ |
258 | }; \ |
259 | \ |
260 | int main(int argc, char *argv[]) \ |
261 | { \ |
262 | ego(argv[0]); \ |
0f00dc4c |
263 | test_run(argc, argv, defs, SRCDIR"/t/" #pre); \ |
d03ab969 |
264 | return (0); \ |
265 | } |
266 | |
267 | #else |
268 | # define HASH_TEST(PRE, pre) |
269 | #endif |
270 | |
271 | /*----- That's all, folks -------------------------------------------------*/ |
272 | |
273 | #ifdef __cplusplus |
274 | } |
275 | #endif |
276 | |
277 | #endif |