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