symm/...: Reformat encryption mode loops and related code.
[catacomb] / symm / hash.h
CommitLineData
d03ab969 1/* -*-c-*-
2 *
d03ab969 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); \
0fee61eb
MW
62 size_t _s; \
63 uint32 _l, _h; \
d03ab969 64 \
79ba130c 65 /* --- Add on the size done so far --- * \
66 * \
67 * Messy, because trapping overflow is difficult when you don't know \
68 * how many bits you've actually got. \
69 */ \
d03ab969 70 \
0fee61eb
MW
71 _l = U32(_bsz); _h = ((_bsz & ~(size_t)MASK32) >> 16) >> 16; \
72 _bctx->nl += _l; if (_bctx->nl < _l || _bctx->nl & ~(uint32)MASK32) _h++; \
73 _bctx->nh += _h; \
d03ab969 74 \
75 /* --- Handle very small contributions --- */ \
76 \
0fee61eb
MW
77 if (_bctx->off + _bsz < PRE##_BUFSZ) \
78 { memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz); _bctx->off += _bsz; } \
79 else { \
d03ab969 80 \
81 /* --- Handle an initial partial buffer --- */ \
82 \
83 if (_bctx->off) { \
0fee61eb
MW
84 _s = PRE##_BUFSZ - _bctx->off; \
85 memcpy(_bctx->buf + _bctx->off, _bbuf, _s); \
d03ab969 86 pre##_compress(_bctx, _bctx->buf); \
0fee61eb 87 _bsz -= _s; _bbuf += _s; \
d03ab969 88 } \
89 \
90 /* --- Do whole buffers while we can --- */ \
91 \
92 while (_bsz >= PRE##_BUFSZ) { \
93 pre##_compress(_bctx, _bbuf); \
94 _bsz -= PRE##_BUFSZ; _bbuf += PRE##_BUFSZ; \
95 } \
96 \
97 /* --- And wrap up at the end --- */ \
98 \
0fee61eb 99 if (_bsz) memcpy(_bctx->buf, _bbuf, _bsz); \
d03ab969 100 _bctx->off = _bsz; \
101 } \
102} while (0)
103
104/* --- @HASH_PAD@ --- *
105 *
106 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
107 * @ictx@ = pointer to context block for the hash
108 * @term@ = terminator character to write following the data
109 * @pad@ = pad character to fill with
110 * @diff@ = size of space to leave at the end of the last block
111 *
112 * Use: Does padding for message digest functions.
113 */
114
115#define HASH_PAD(PRE, pre, ictx, term, pad, diff) do { \
116 pre##_ctx *_pctx = (ictx); \
117 \
118 _pctx->buf[_pctx->off] = term; \
119 _pctx->off++; \
120 if (_pctx->off > PRE##_BUFSZ - diff) { \
121 if (_pctx->off < PRE##_BUFSZ) \
122 memset(_pctx->buf + _pctx->off, pad, PRE##_BUFSZ - _pctx->off); \
123 pre##_compress(_pctx, _pctx->buf); \
124 memset(_pctx->buf, pad, PRE##_BUFSZ - diff); \
125 } else \
126 memset(_pctx->buf + _pctx->off, pad, \
127 PRE##_BUFSZ - _pctx->off - diff); \
128} while (0)
129
130/* --- @HASH_MD5STRENGTH@ --- *
131 *
132 * Arguments: @PRE@, @pre@ = prefixes for hash-specific definitions
133 * @ictx@ = pointer to context block for the hash
134 *
135 * Use: Does MD5-style MD strengthening. The data is terminated
136 * by a single set bit, padded with zero bits, and then a 64-
137 * bit length is written, little-end first.
138 */
139
140#define HASH_MD5STRENGTH(PRE, pre, ictx) do { \
141 pre##_ctx *_mctx = (ictx); \
142 HASH_PAD(PRE, pre, _mctx, 0x80u, 0, 8); \
79ba130c 143 STORE32_L(_mctx->buf + PRE##_BUFSZ - 8, _mctx->nl << 3); \
144 STORE32_L(_mctx->buf + PRE##_BUFSZ - 4, \
145 (_mctx->nl >> 29) | (_mctx->nh << 3)); \
d03ab969 146 pre##_compress(_mctx, _mctx->buf); \
147} while (0)
148
149/* --- @HASH_TEST@ --- *
150 *
151 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
152 *
153 * Use: Standard test rig for hash functions.
154 */
155
156#ifdef TEST_RIG
157
158#include <mLib/quis.h>
159#include <mLib/testrig.h>
160
e9926004 161#define HASH_BUFLEN 100000
162
4328f746
MW
163#define HASH_VERIFY(PRE, pre) HASH_VERIFYX(PRE, pre, #pre)
164
165#define HASH_VERIFYX(PRE, pre, name) \
d03ab969 166 \
cf9d9d38 167static int vrf_##pre(dstr *v, const test_type *msgty) \
d03ab969 168{ \
169 pre##_ctx ctx; \
170 int ok = 1; \
171 int i; \
172 octet *p; \
173 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
174 size_t sz; \
175 dstr d; \
176 \
177 dstr_create(&d); \
178 dstr_ensure(&d, PRE##_HASHSZ); \
179 d.len = PRE##_HASHSZ; \
180 \
181 for (ip = szs; *ip; ip++) { \
182 i = *ip; \
183 sz = v[0].len; \
184 if (i == -1) \
185 i = sz; \
186 if (i > sz) \
187 continue; \
188 p = (octet *)v[0].buf; \
189 pre##_init(&ctx); \
190 while (sz) { \
191 if (i > sz) \
192 i = sz; \
193 pre##_hash(&ctx, p, i); \
194 p += i; \
195 sz -= i; \
196 } \
197 pre##_done(&ctx, d.buf); \
198 if (memcmp(d.buf, v[1].buf, PRE##_HASHSZ) != 0) { \
cf9d9d38
MW
199 printf("\nfail:\n\tstep = %i\n\tinput = ", *ip); \
200 msgty->dump(&v[0], stdout); \
201 printf("\n\texpected = "); \
d03ab969 202 type_hex.dump(&v[1], stdout); \
203 fputs("\n\tcomputed = ", stdout); \
204 type_hex.dump(&d, stdout); \
205 putchar('\n'); \
206 ok = 0; \
207 } \
208 } \
209 \
210 dstr_destroy(&d); \
211 return (ok); \
212} \
213 \
cf9d9d38
MW
214static int vrf_##pre##_hex(dstr *v) \
215 { return vrf_##pre(v, &type_hex); } \
216static int vrf_##pre##_lit(dstr *v) \
217 { return vrf_##pre(v, &type_string); } \
218 \
4328f746 219static int vrf_##pre##_rep(dstr *v) \
e9926004 220{ \
221 pre##_ctx ctx; \
222 size_t len = v[0].len; \
223 int n = *(int *)v[1].buf; \
224 int nd = 0; \
225 int nn = len; \
226 int ok = 1; \
227 octet *p, *q; \
228 dstr d = DSTR_INIT; \
229 \
230 while (nn < HASH_BUFLEN && (n & 1) == 0) { nd++; nn <<= 1; n >>= 1; } \
231 p = xmalloc(nn); \
232 memcpy(p, v[0].buf, len); \
233 q = p + len; \
234 while (nd--) { memcpy(q, p, len); q += len; len <<= 1; } \
235 \
236 dstr_ensure(&d, PRE##_HASHSZ); \
237 d.len = PRE##_HASHSZ; \
238 pre##_init(&ctx); \
239 while (n--) pre##_hash(&ctx, p, len); \
240 pre##_done(&ctx, d.buf); \
241 \
242 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
243 printf("\nfail:\n\tinput = `%s'\n\treps = `%i'\n\texpected = ", \
244 v[0].buf, *(int *)v[1].buf); \
245 type_hex.dump(&v[2], stdout); \
246 fputs("\n\tcomputed = ", stdout); \
247 type_hex.dump(&d, stdout); \
248 putchar('\n'); \
249 ok = 0; \
250 } \
12ed8a1f 251 xfree(p); \
e9926004 252 dstr_destroy(&d); \
253 return (ok); \
4328f746
MW
254}
255
256#define HASH_TESTDEFS(PRE, pre) HASH_TESTDEFSX(PRE, pre, #pre)
257
258#define HASH_TESTDEFSX(PRE, pre, name) \
cf9d9d38
MW
259 { name, vrf_##pre##_lit, { &type_string, &type_hex, 0 } }, \
260 { name "-hex", vrf_##pre##_hex, { &type_hex, &type_hex, 0 } }, \
4328f746
MW
261 { name "-rep", vrf_##pre##_rep, \
262 { &type_string, &type_int, &type_hex, 0 } },
263
264#define HASH_TESTX(PRE, pre, name, fname) \
265 \
266HASH_VERIFYX(PRE, pre, name) \
e9926004 267 \
d03ab969 268static test_chunk defs[] = { \
4328f746 269 HASH_TESTDEFSX(PRE, pre, name) \
d03ab969 270 { 0, 0, { 0 } } \
271}; \
272 \
273int main(int argc, char *argv[]) \
274{ \
275 ego(argv[0]); \
aaae9cab 276 test_run(argc, argv, defs, SRCDIR"/t/" fname); \
d03ab969 277 return (0); \
278}
279
280#else
aaae9cab 281# define HASH_TESTX(PRE, pre, name, fname)
d03ab969 282#endif
283
aaae9cab
MW
284#define HASH_TEST(PRE, pre) HASH_TESTX(PRE, pre, #pre, #pre)
285
d03ab969 286/*----- That's all, folks -------------------------------------------------*/
287
288#ifdef __cplusplus
289 }
290#endif
291
292#endif