chiark / gitweb /
Initial import.
[catacomb] / hash.h
1 /* -*-c-*-
2  *
3  * $Id: hash.h,v 1.1 1999/09/03 08:41:12 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.1  1999/09/03 08:41:12  mdw
34  * Initial import.
35  *
36  */
37
38 #ifndef HASH_H
39 #define HASH_H
40
41 #ifdef __cplusplus
42   extern "C" {
43 #endif
44
45 /*----- Header files ------------------------------------------------------*/
46
47 #include <string.h>
48
49 #include <mLib/bits.h>
50
51 /*----- Macros ------------------------------------------------------------*/
52
53 /* --- @HASH_BUFFER@ --- *
54  *
55  * Arguments:   @PRE@, @pre@ = prefixes for hash-specific definitions
56  *              @ictx@ = pointer to context block for the hash
57  *              @ibuf@ = pointer to input data to hash
58  *              @isz@ = size of buffer
59  *
60  * Use:         Handles buffering of input data to a hash function.  The
61  *              hash's compression function is called when the buffer is
62  *              full.  Note that the compression function can be called on
63  *              data which is at odd alignments; it is expected to cope
64  *              gracefully with this (possibly by copying the data into its
65  *              internal buffer before starting).
66  */
67
68 #define HASH_BUFFER(PRE, pre, ictx, ibuf, isz) do {                     \
69   pre##_ctx *_bctx = (ictx);                                            \
70   size_t _bsz = (isz);                                                  \
71   const octet *_bbuf = (octet *)(ibuf);                                 \
72                                                                         \
73   /* --- Add on the size done so far --- */                             \
74                                                                         \
75   _bctx->count += _bsz;                                                 \
76                                                                         \
77   /* --- Handle very small contributions --- */                         \
78                                                                         \
79   if (_bctx->off + _bsz < PRE##_BUFSZ) {                                \
80     memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz);                       \
81     _bctx->off += _bsz;                                                 \
82   } else {                                                              \
83                                                                         \
84     /* --- Handle an initial partial buffer --- */                      \
85                                                                         \
86     if (_bctx->off) {                                                   \
87       size_t s = PRE##_BUFSZ - _bctx->off;                              \
88       memcpy(_bctx->buf + _bctx->off, _bbuf, s);                        \
89       pre##_compress(_bctx, _bctx->buf);                                \
90       _bsz -= s; _bbuf += s;                                            \
91     }                                                                   \
92                                                                         \
93     /* --- Do whole buffers while we can --- */                         \
94                                                                         \
95     while (_bsz >= PRE##_BUFSZ) {                                       \
96       pre##_compress(_bctx, _bbuf);                                     \
97       _bsz -= PRE##_BUFSZ; _bbuf += PRE##_BUFSZ;                        \
98     }                                                                   \
99                                                                         \
100     /* --- And wrap up at the end --- */                                \
101                                                                         \
102     if (_bsz)                                                           \
103       memcpy(_bctx->buf, _bbuf, _bsz);                                  \
104     _bctx->off = _bsz;                                                  \
105   }                                                                     \
106 } while (0)
107
108 /* --- @HASH_PAD@ --- *
109  *
110  * Arguments:   @PRE@, @pre@ = prefixes for hash-specific definitions
111  *              @ictx@ = pointer to context block for the hash
112  *              @term@ = terminator character to write following the data
113  *              @pad@ = pad character to fill with
114  *              @diff@ = size of space to leave at the end of the last block
115  *
116  * Use:         Does padding for message digest functions.
117  */
118
119 #define HASH_PAD(PRE, pre, ictx, term, pad, diff) do {                  \
120   pre##_ctx *_pctx = (ictx);                                            \
121                                                                         \
122   _pctx->buf[_pctx->off] = term;                                        \
123   _pctx->off++;                                                         \
124   if (_pctx->off > PRE##_BUFSZ - diff) {                                \
125     if (_pctx->off < PRE##_BUFSZ)                                       \
126       memset(_pctx->buf + _pctx->off, pad, PRE##_BUFSZ - _pctx->off);   \
127     pre##_compress(_pctx, _pctx->buf);                                  \
128     memset(_pctx->buf, pad, PRE##_BUFSZ - diff);                        \
129   } else                                                                \
130     memset(_pctx->buf + _pctx->off, pad,                                \
131            PRE##_BUFSZ - _pctx->off - diff);                            \
132 } while (0)
133
134 /* --- @HASH_MD5STRENGTH@ --- *
135  *
136  * Arguments:   @PRE@, @pre@ = prefixes for hash-specific definitions
137  *              @ictx@ = pointer to context block for the hash
138  *
139  * Use:         Does MD5-style MD strengthening.  The data is terminated
140  *              by a single set bit, padded with zero bits, and then a 64-
141  *              bit length is written, little-end first.
142  */
143
144 #define HASH_MD5STRENGTH(PRE, pre, ictx) do {                           \
145   pre##_ctx *_mctx = (ictx);                                            \
146   HASH_PAD(PRE, pre, _mctx, 0x80u, 0, 8);                               \
147   STORE32_L(_mctx->buf + PRE##_BUFSZ - 8, _mctx->count << 3);           \
148   STORE32_L(_mctx->buf + PRE##_BUFSZ - 4, _mctx->count >> 29);          \
149   pre##_compress(_mctx, _mctx->buf);                                    \
150 } while (0)
151
152 /* --- @HASH_TEST@ --- *
153  *
154  * Arguments:   @PRE@, @pre@ = prefixes for hash-specfic definitions
155  *
156  * Use:         Standard test rig for hash functions.
157  */
158
159 #ifdef TEST_RIG
160
161 #include <mLib/quis.h>
162 #include <mLib/testrig.h>
163
164 #define HASH_TEST(PRE, pre)                                             \
165                                                                         \
166 static int verify(dstr *v)                                              \
167 {                                                                       \
168   pre##_ctx ctx;                                                        \
169   int ok = 1;                                                           \
170   int i;                                                                \
171   octet *p;                                                             \
172   int szs[] = { 1, 7, 192, -1, 0 }, *ip;                                \
173   size_t sz;                                                            \
174   dstr d;                                                               \
175                                                                         \
176   dstr_create(&d);                                                      \
177   dstr_ensure(&d, PRE##_HASHSZ);                                        \
178   d.len = PRE##_HASHSZ;                                                 \
179                                                                         \
180   for (ip = szs; *ip; ip++) {                                           \
181     i = *ip;                                                            \
182     sz = v[0].len;                                                      \
183     if (i == -1)                                                        \
184       i = sz;                                                           \
185     if (i > sz)                                                         \
186       continue;                                                         \
187     p = (octet *)v[0].buf;                                              \
188     pre##_init(&ctx);                                                   \
189     while (sz) {                                                        \
190       if (i > sz)                                                       \
191         i = sz;                                                         \
192       pre##_hash(&ctx, p, i);                                           \
193       p += i;                                                           \
194       sz -= i;                                                          \
195     }                                                                   \
196     pre##_done(&ctx, d.buf);                                            \
197     if (memcmp(d.buf, v[1].buf, PRE##_HASHSZ) != 0) {                   \
198       printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\texpected = ",     \
199              *ip, v[0].buf);                                            \
200       type_hex.dump(&v[1], stdout);                                     \
201       fputs("\n\tcomputed = ", stdout);                                 \
202       type_hex.dump(&d, stdout);                                        \
203       putchar('\n');                                                    \
204       ok = 0;                                                           \
205     }                                                                   \
206   }                                                                     \
207                                                                         \
208   dstr_destroy(&d);                                                     \
209   return (ok);                                                          \
210 }                                                                       \
211                                                                         \
212 static test_chunk defs[] = {                                            \
213   { #pre, verify, { &type_string, &type_hex, 0 } },                     \
214   { 0, 0, { 0 } }                                                       \
215 };                                                                      \
216                                                                         \
217 int main(int argc, char *argv[])                                        \
218 {                                                                       \
219   ego(argv[0]);                                                         \
220   test_run(argc, argv, defs, SRCDIR"/tests/" #pre);                     \
221   return (0);                                                           \
222 }
223
224 #else
225 #  define HASH_TEST(PRE, pre)
226 #endif
227
228 /*----- That's all, folks -------------------------------------------------*/
229
230 #ifdef __cplusplus
231   }
232 #endif
233
234 #endif