Make tables of standard encryption schemes etc.
[u/mdw/catacomb] / hash.h
1 /* -*-c-*-
2 *
3 * $Id: hash.h,v 1.4 2004/03/21 22:42:27 mdw Exp $
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 $
33 * Revision 1.4 2004/03/21 22:42:27 mdw
34 * Test hashing on long strings.
35 *
36 * Revision 1.3 2000/06/17 11:23:27 mdw
37 * Portability fix for broken compilers.
38 *
39 * Revision 1.2 1999/12/10 23:16:40 mdw
40 * Split mode macros into interface and implementation.
41 *
42 * Revision 1.1 1999/09/03 08:41:12 mdw
43 * Initial import.
44 *
45 */
46
47 #ifndef CATACOMB_HASH_H
48 #define CATACOMB_HASH_H
49
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53
54 /*----- Header files ------------------------------------------------------*/
55
56 #include <string.h>
57
58 #include <mLib/bits.h>
59
60 /*----- Macros ------------------------------------------------------------*/
61
62 /* --- @HASH_BUFFER@ --- *
63 *
64 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
65 * @ictx@ = pointer to context block for the hash
66 * @ibuf@ = pointer to input data to hash
67 * @isz@ = size of buffer
68 *
69 * Use: Handles buffering of input data to a hash function. The
70 * hash's compression function is called when the buffer is
71 * full. Note that the compression function can be called on
72 * data which is at odd alignments; it is expected to cope
73 * gracefully with this (possibly by copying the data into its
74 * internal buffer before starting).
75 */
76
77 #define HASH_BUFFER(PRE, pre, ictx, ibuf, isz) do { \
78 pre##_ctx *_bctx = (ictx); \
79 size_t _bsz = (isz); \
80 const octet *_bbuf = (octet *)(ibuf); \
81 \
82 /* --- Add on the size done so far --- * \
83 * \
84 * Messy, because trapping overflow is difficult when you don't know \
85 * how many bits you've actually got. \
86 */ \
87 \
88 { \
89 uint32 _l = U32(_bsz); \
90 uint32 _h = ((_bsz & ~MASK32) >> 16) >> 16; \
91 _bctx->nh += _h; \
92 _bctx->nl += _l; \
93 if (_bctx->nl < _l || _bctx->nl & ~MASK32) \
94 _bctx->nh++; \
95 } \
96 \
97 /* --- Handle very small contributions --- */ \
98 \
99 if (_bctx->off + _bsz < PRE##_BUFSZ) { \
100 memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz); \
101 _bctx->off += _bsz; \
102 } else { \
103 \
104 /* --- Handle an initial partial buffer --- */ \
105 \
106 if (_bctx->off) { \
107 size_t s = PRE##_BUFSZ - _bctx->off; \
108 memcpy(_bctx->buf + _bctx->off, _bbuf, s); \
109 pre##_compress(_bctx, _bctx->buf); \
110 _bsz -= s; _bbuf += s; \
111 } \
112 \
113 /* --- Do whole buffers while we can --- */ \
114 \
115 while (_bsz >= PRE##_BUFSZ) { \
116 pre##_compress(_bctx, _bbuf); \
117 _bsz -= PRE##_BUFSZ; _bbuf += PRE##_BUFSZ; \
118 } \
119 \
120 /* --- And wrap up at the end --- */ \
121 \
122 if (_bsz) \
123 memcpy(_bctx->buf, _bbuf, _bsz); \
124 _bctx->off = _bsz; \
125 } \
126 } while (0)
127
128 /* --- @HASH_PAD@ --- *
129 *
130 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
131 * @ictx@ = pointer to context block for the hash
132 * @term@ = terminator character to write following the data
133 * @pad@ = pad character to fill with
134 * @diff@ = size of space to leave at the end of the last block
135 *
136 * Use: Does padding for message digest functions.
137 */
138
139 #define HASH_PAD(PRE, pre, ictx, term, pad, diff) do { \
140 pre##_ctx *_pctx = (ictx); \
141 \
142 _pctx->buf[_pctx->off] = term; \
143 _pctx->off++; \
144 if (_pctx->off > PRE##_BUFSZ - diff) { \
145 if (_pctx->off < PRE##_BUFSZ) \
146 memset(_pctx->buf + _pctx->off, pad, PRE##_BUFSZ - _pctx->off); \
147 pre##_compress(_pctx, _pctx->buf); \
148 memset(_pctx->buf, pad, PRE##_BUFSZ - diff); \
149 } else \
150 memset(_pctx->buf + _pctx->off, pad, \
151 PRE##_BUFSZ - _pctx->off - diff); \
152 } while (0)
153
154 /* --- @HASH_MD5STRENGTH@ --- *
155 *
156 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
157 * @ictx@ = pointer to context block for the hash
158 *
159 * Use: Does MD5-style MD strengthening. The data is terminated
160 * by a single set bit, padded with zero bits, and then a 64-
161 * bit length is written, little-end first.
162 */
163
164 #define HASH_MD5STRENGTH(PRE, pre, ictx) do { \
165 pre##_ctx *_mctx = (ictx); \
166 HASH_PAD(PRE, pre, _mctx, 0x80u, 0, 8); \
167 STORE32_L(_mctx->buf + PRE##_BUFSZ - 8, _mctx->nl << 3); \
168 STORE32_L(_mctx->buf + PRE##_BUFSZ - 4, \
169 (_mctx->nl >> 29) | (_mctx->nh << 3)); \
170 pre##_compress(_mctx, _mctx->buf); \
171 } while (0)
172
173 /* --- @HASH_TEST@ --- *
174 *
175 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
176 *
177 * Use: Standard test rig for hash functions.
178 */
179
180 #ifdef TEST_RIG
181
182 #include <mLib/quis.h>
183 #include <mLib/testrig.h>
184
185 #define HASH_BUFLEN 100000
186
187 #define HASH_TEST(PRE, pre) \
188 \
189 static int verify(dstr *v) \
190 { \
191 pre##_ctx ctx; \
192 int ok = 1; \
193 int i; \
194 octet *p; \
195 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
196 size_t sz; \
197 dstr d; \
198 \
199 dstr_create(&d); \
200 dstr_ensure(&d, PRE##_HASHSZ); \
201 d.len = PRE##_HASHSZ; \
202 \
203 for (ip = szs; *ip; ip++) { \
204 i = *ip; \
205 sz = v[0].len; \
206 if (i == -1) \
207 i = sz; \
208 if (i > sz) \
209 continue; \
210 p = (octet *)v[0].buf; \
211 pre##_init(&ctx); \
212 while (sz) { \
213 if (i > sz) \
214 i = sz; \
215 pre##_hash(&ctx, p, i); \
216 p += i; \
217 sz -= i; \
218 } \
219 pre##_done(&ctx, d.buf); \
220 if (memcmp(d.buf, v[1].buf, PRE##_HASHSZ) != 0) { \
221 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\texpected = ", \
222 *ip, v[0].buf); \
223 type_hex.dump(&v[1], stdout); \
224 fputs("\n\tcomputed = ", stdout); \
225 type_hex.dump(&d, stdout); \
226 putchar('\n'); \
227 ok = 0; \
228 } \
229 } \
230 \
231 dstr_destroy(&d); \
232 return (ok); \
233 } \
234 \
235 static int verifyrep(dstr *v) \
236 { \
237 pre##_ctx ctx; \
238 size_t len = v[0].len; \
239 int n = *(int *)v[1].buf; \
240 int nd = 0; \
241 int nn = len; \
242 int ok = 1; \
243 octet *p, *q; \
244 dstr d = DSTR_INIT; \
245 \
246 while (nn < HASH_BUFLEN && (n & 1) == 0) { nd++; nn <<= 1; n >>= 1; } \
247 p = xmalloc(nn); \
248 memcpy(p, v[0].buf, len); \
249 q = p + len; \
250 while (nd--) { memcpy(q, p, len); q += len; len <<= 1; } \
251 \
252 dstr_ensure(&d, PRE##_HASHSZ); \
253 d.len = PRE##_HASHSZ; \
254 pre##_init(&ctx); \
255 while (n--) pre##_hash(&ctx, p, len); \
256 pre##_done(&ctx, d.buf); \
257 \
258 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
259 printf("\nfail:\n\tinput = `%s'\n\treps = `%i'\n\texpected = ", \
260 v[0].buf, *(int *)v[1].buf); \
261 type_hex.dump(&v[2], stdout); \
262 fputs("\n\tcomputed = ", stdout); \
263 type_hex.dump(&d, stdout); \
264 putchar('\n'); \
265 ok = 0; \
266 } \
267 free(p); \
268 dstr_destroy(&d); \
269 return (ok); \
270 } \
271 \
272 static test_chunk defs[] = { \
273 { #pre, verify, { &type_string, &type_hex, 0 } }, \
274 { #pre "-rep", verifyrep, \
275 { &type_string, &type_int, &type_hex, 0 } }, \
276 { 0, 0, { 0 } } \
277 }; \
278 \
279 int main(int argc, char *argv[]) \
280 { \
281 ego(argv[0]); \
282 test_run(argc, argv, defs, SRCDIR"/tests/" #pre); \
283 return (0); \
284 }
285
286 #else
287 # define HASH_TEST(PRE, pre)
288 #endif
289
290 /*----- That's all, folks -------------------------------------------------*/
291
292 #ifdef __cplusplus
293 }
294 #endif
295
296 #endif