chiark / gitweb /
dc9ad024d14f70a500e30e5e25742c547278969f
[catacomb] / symm / hash.h
1 /* -*-c-*-
2  *
3  * Generic handling for message digest functions
4  *
5  * (c) 1998 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16  *
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.
21  *
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
28 #ifndef CATACOMB_HASH_H
29 #define CATACOMB_HASH_H
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                                                                         \
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    */                                                                   \
68                                                                         \
69   {                                                                     \
70     uint32 _l = U32(_bsz);                                              \
71     uint32 _h = ((_bsz & ~MASK32) >> 16) >> 16;                         \
72     _bctx->nh += _h;                                                    \
73     _bctx->nl += _l;                                                    \
74     if (_bctx->nl < _l || _bctx->nl & ~MASK32)                          \
75       _bctx->nh++;                                                      \
76   }                                                                     \
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);                               \
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));                      \
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
166 #define HASH_BUFLEN 100000
167
168 #define HASH_VERIFY(PRE, pre) HASH_VERIFYX(PRE, pre, #pre)
169
170 #define HASH_VERIFYX(PRE, pre, name)                                    \
171                                                                         \
172 static int vrf_##pre(dstr *v, const test_type *msgty)                   \
173 {                                                                       \
174   pre##_ctx ctx;                                                        \
175   int ok = 1;                                                           \
176   int i;                                                                \
177   octet *p;                                                             \
178   int szs[] = { 1, 7, 192, -1, 0 }, *ip;                                \
179   size_t sz;                                                            \
180   dstr d;                                                               \
181                                                                         \
182   dstr_create(&d);                                                      \
183   dstr_ensure(&d, PRE##_HASHSZ);                                        \
184   d.len = PRE##_HASHSZ;                                                 \
185                                                                         \
186   for (ip = szs; *ip; ip++) {                                           \
187     i = *ip;                                                            \
188     sz = v[0].len;                                                      \
189     if (i == -1)                                                        \
190       i = sz;                                                           \
191     if (i > sz)                                                         \
192       continue;                                                         \
193     p = (octet *)v[0].buf;                                              \
194     pre##_init(&ctx);                                                   \
195     while (sz) {                                                        \
196       if (i > sz)                                                       \
197         i = sz;                                                         \
198       pre##_hash(&ctx, p, i);                                           \
199       p += i;                                                           \
200       sz -= i;                                                          \
201     }                                                                   \
202     pre##_done(&ctx, d.buf);                                            \
203     if (memcmp(d.buf, v[1].buf, PRE##_HASHSZ) != 0) {                   \
204       printf("\nfail:\n\tstep = %i\n\tinput = ", *ip);                  \
205       msgty->dump(&v[0], stdout);                                       \
206       printf("\n\texpected = ");                                        \
207       type_hex.dump(&v[1], stdout);                                     \
208       fputs("\n\tcomputed = ", stdout);                                 \
209       type_hex.dump(&d, stdout);                                        \
210       putchar('\n');                                                    \
211       ok = 0;                                                           \
212     }                                                                   \
213   }                                                                     \
214                                                                         \
215   dstr_destroy(&d);                                                     \
216   return (ok);                                                          \
217 }                                                                       \
218                                                                         \
219 static int vrf_##pre##_hex(dstr *v)                                     \
220   { return vrf_##pre(v, &type_hex); }                                   \
221 static int vrf_##pre##_lit(dstr *v)                                     \
222   { return vrf_##pre(v, &type_string); }                                \
223                                                                         \
224 static int vrf_##pre##_rep(dstr *v)                                     \
225 {                                                                       \
226   pre##_ctx ctx;                                                        \
227   size_t len = v[0].len;                                                \
228   int n = *(int *)v[1].buf;                                             \
229   int nd = 0;                                                           \
230   int nn = len;                                                         \
231   int ok = 1;                                                           \
232   octet *p, *q;                                                         \
233   dstr d = DSTR_INIT;                                                   \
234                                                                         \
235   while (nn < HASH_BUFLEN && (n & 1) == 0) { nd++; nn <<= 1; n >>= 1; } \
236   p = xmalloc(nn);                                                      \
237   memcpy(p, v[0].buf, len);                                             \
238   q = p + len;                                                          \
239   while (nd--) { memcpy(q, p, len); q += len; len <<= 1; }              \
240                                                                         \
241   dstr_ensure(&d, PRE##_HASHSZ);                                        \
242   d.len = PRE##_HASHSZ;                                                 \
243   pre##_init(&ctx);                                                     \
244   while (n--) pre##_hash(&ctx, p, len);                                 \
245   pre##_done(&ctx, d.buf);                                              \
246                                                                         \
247   if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) {                     \
248     printf("\nfail:\n\tinput = `%s'\n\treps = `%i'\n\texpected = ",     \
249              v[0].buf, *(int *)v[1].buf);                               \
250     type_hex.dump(&v[2], stdout);                                       \
251     fputs("\n\tcomputed = ", stdout);                                   \
252     type_hex.dump(&d, stdout);                                          \
253     putchar('\n');                                                      \
254     ok = 0;                                                             \
255   }                                                                     \
256   xfree(p);                                                             \
257   dstr_destroy(&d);                                                     \
258   return (ok);                                                          \
259 }
260
261 #define HASH_TESTDEFS(PRE, pre) HASH_TESTDEFSX(PRE, pre, #pre)
262
263 #define HASH_TESTDEFSX(PRE, pre, name)                                  \
264   { name, vrf_##pre##_lit, { &type_string, &type_hex, 0 } },            \
265   { name "-hex", vrf_##pre##_hex, { &type_hex, &type_hex, 0 } },        \
266   { name "-rep", vrf_##pre##_rep,                                       \
267     { &type_string, &type_int, &type_hex, 0 } },
268
269 #define HASH_TESTX(PRE, pre, name, fname)                               \
270                                                                         \
271 HASH_VERIFYX(PRE, pre, name)                                            \
272                                                                         \
273 static test_chunk defs[] = {                                            \
274   HASH_TESTDEFSX(PRE, pre, name)                                        \
275   { 0, 0, { 0 } }                                                       \
276 };                                                                      \
277                                                                         \
278 int main(int argc, char *argv[])                                        \
279 {                                                                       \
280   ego(argv[0]);                                                         \
281   test_run(argc, argv, defs, SRCDIR"/t/" fname);                        \
282   return (0);                                                           \
283 }
284
285 #else
286 #  define HASH_TESTX(PRE, pre, name, fname)
287 #endif
288
289 #define HASH_TEST(PRE, pre) HASH_TESTX(PRE, pre, #pre, #pre)
290
291 /*----- That's all, folks -------------------------------------------------*/
292
293 #ifdef __cplusplus
294   }
295 #endif
296
297 #endif