Update manuals.
[u/mdw/catacomb] / md2.c
1 /* -*-c-*-
2 *
3 * $Id: md2.c,v 1.2 2004/04/02 01:03:49 mdw Exp $
4 *
5 * The MD2 message digest function
6 *
7 * (c) 2001 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: md2.c,v $
33 * Revision 1.2 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
35 *
36 * Revision 1.1 2001/02/21 20:03:22 mdw
37 * Added support for MD2 hash function.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include <mLib/bits.h>
44
45 #include "ghash.h"
46 #include "ghash-def.h"
47 #include "hash.h"
48 #include "md2.h"
49 #include "md2-tab.h"
50
51 /*----- Tables ------------------------------------------------------------*/
52
53 static const octet s[256] = MD2_S;
54
55 /*----- Main code ---------------------------------------------------------*/
56
57 /* --- @md2_compress@ --- *
58 *
59 * Arguments: @md2_ctx *ctx@ = pointer to context block
60 * @const void *sbuf@ = pointer to buffer of appropriate size
61 *
62 * Returns: ---
63 *
64 * Use: MD2 compression and checksum function.
65 */
66
67 void md2_compress(md2_ctx *ctx, const void *sbuf)
68 {
69 unsigned i;
70 unsigned t;
71 octet x[MD2_BUFSZ];
72 octet y[MD2_BUFSZ];
73
74 /* --- Handy macro for doing something lots of times --- */
75
76 #define DO(what, where) do { \
77 what(where, 0); what(where, 1); what(where, 2); what(where, 3); \
78 what(where, 4); what(where, 5); what(where, 6); what(where, 7); \
79 what(where, 8); what(where, 9); what(where, 10); what(where, 11); \
80 what(where, 12); what(where, 13); what(where, 14); what(where, 15); \
81 } while (0)
82
83 /* --- Do the hashing first to avoid corrupting the checksum --- */
84
85 memcpy(x, sbuf, sizeof(x));
86 #define X(z, i) y[i] = z[i] ^ ctx->h[i]
87 DO(X, x);
88 #undef X
89
90 t = 0;
91 for (i = 0; i < 18; i++) {
92 #define X(z, i) t = z[i] ^= s[t];
93 DO(X, ctx->h);
94 DO(X, x);
95 DO(X, y);
96 #undef X
97 t = U8(t + i);
98 }
99
100 /* --- Now compute the checksum --- */
101
102 t = ctx->c[MD2_BUFSZ - 1];
103 #define X(z, i) t = ctx->c[i] ^= s[z[i] ^ t]
104 DO(X, ((const octet *)sbuf));
105 #undef X
106
107 #undef DO
108 }
109
110 /* --- @md2_init@ --- *
111 *
112 * Arguments: @md2_ctx *ctx@ = pointer to context block to initialize
113 *
114 * Returns: ---
115 *
116 * Use: Initializes a context block ready for hashing.
117 */
118
119 void md2_init(md2_ctx *ctx)
120 {
121 memset(ctx->c, 0, sizeof(ctx->c));
122 memset(ctx->h, 0, sizeof(ctx->h));
123 ctx->off = 0;
124 }
125
126 /* --- @md2_set@ --- *
127 *
128 * Arguments: @md2_ctx *ctx@ = pointer to context block
129 * @const void *buf@ = pointer to state buffer
130 * @unsigned long count@ = current count of bytes processed
131 *
132 * Returns: ---
133 *
134 * Use: Initializes a context block from a given state. This is
135 * useful in cases where the initial hash state is meant to be
136 * secret, e.g., for NMAC and HMAC support.
137 */
138
139 void md2_set(md2_ctx *ctx, const void *buf, unsigned long count)
140 {
141 const octet *p = buf;
142 memcpy(ctx->h, p, MD2_BUFSZ);
143 memcpy(ctx->c, p + MD2_BUFSZ, MD2_BUFSZ);
144 ctx->off = 0;
145 }
146
147 /* --- @md2_hash@ --- *
148 *
149 * Arguments: @md2_ctx *ctx@ = pointer to context block
150 * @const void *buf@ = buffer of data to hash
151 * @size_t sz@ = size of buffer to hash
152 *
153 * Returns: ---
154 *
155 * Use: Hashes a buffer of data. The buffer may be of any size and
156 * alignment.
157 */
158
159 void md2_hash(md2_ctx *ctx, const void *buf, size_t sz)
160 {
161 const octet *bbuf = (octet *)buf;
162
163 /* --- Code automatically expanded from @HASH_BUFFER@ and tidied --- */
164
165 if (ctx->off + sz < MD2_BUFSZ) {
166 memcpy(ctx->buf + ctx->off, bbuf, sz);
167 ctx->off += sz;
168 } else {
169 if (ctx->off) {
170 size_t s = MD2_BUFSZ - ctx->off;
171 memcpy(ctx->buf + ctx->off, bbuf, s);
172 md2_compress(ctx, ctx->buf);
173 sz -= s;
174 bbuf += s;
175 }
176 while (sz >= MD2_BUFSZ) {
177 md2_compress(ctx, bbuf);
178 sz -= MD2_BUFSZ; bbuf += MD2_BUFSZ;
179 }
180 if (sz)
181 memcpy(ctx->buf, bbuf, sz);
182 ctx->off = sz;
183 }
184 }
185
186 /* --- @md2_done@ --- *
187 *
188 * Arguments: @md2_ctx *ctx@ = pointer to context block
189 * @void *hash@ = pointer to output buffer
190 *
191 * Returns: ---
192 *
193 * Use: Returns the hash of the data read so far.
194 */
195
196 void md2_done(md2_ctx *ctx, void *hash)
197 {
198 unsigned pad = MD2_BUFSZ - ctx->off;
199 unsigned i;
200 for (i = ctx->off; i < MD2_BUFSZ; i++)
201 ctx->buf[i] = pad;
202 md2_compress(ctx, ctx->buf);
203 md2_compress(ctx, ctx->c);
204 memcpy(hash, ctx->h, MD2_BUFSZ);
205 }
206
207 /* --- @md2_state@ --- *
208 *
209 * Arguments: @md2_ctx *ctx@ = pointer to context
210 * @void *state@ = pointer to buffer for current state
211 *
212 * Returns: Number of bytes written to the hash function so far.
213 *
214 * Use: Returns the current state of the hash function such that
215 * it can be passed to @md2_set@.
216 */
217
218 unsigned long md2_state(md2_ctx *ctx, void *state)
219 {
220 octet *p = state;
221 memcpy(p, ctx->h, MD2_BUFSZ);
222 memcpy(p + MD2_BUFSZ, ctx->c, MD2_BUFSZ);
223 return (0);
224 }
225
226 /* --- Generic interface --- */
227
228 GHASH_DEF(MD2, md2)
229
230 /* --- Test code --- */
231
232 HASH_TEST(MD2, md2)
233
234 /*----- That's all, folks -------------------------------------------------*/