3 * $Id: md2.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
5 * The MD2 message digest function
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/bits.h>
35 #include "ghash-def.h"
40 /*----- Tables ------------------------------------------------------------*/
42 static const octet s[256] = MD2_S;
44 /*----- Main code ---------------------------------------------------------*/
46 /* --- @md2_compress@ --- *
48 * Arguments: @md2_ctx *ctx@ = pointer to context block
49 * @const void *sbuf@ = pointer to buffer of appropriate size
53 * Use: MD2 compression and checksum function.
56 void md2_compress(md2_ctx *ctx, const void *sbuf)
63 /* --- Handy macro for doing something lots of times --- */
65 #define DO(what, where) do { \
66 what(where, 0); what(where, 1); what(where, 2); what(where, 3); \
67 what(where, 4); what(where, 5); what(where, 6); what(where, 7); \
68 what(where, 8); what(where, 9); what(where, 10); what(where, 11); \
69 what(where, 12); what(where, 13); what(where, 14); what(where, 15); \
72 /* --- Do the hashing first to avoid corrupting the checksum --- */
74 memcpy(x, sbuf, sizeof(x));
75 #define X(z, i) y[i] = z[i] ^ ctx->h[i]
80 for (i = 0; i < 18; i++) {
81 #define X(z, i) t = z[i] ^= s[t];
89 /* --- Now compute the checksum --- */
91 t = ctx->c[MD2_BUFSZ - 1];
92 #define X(z, i) t = ctx->c[i] ^= s[z[i] ^ t]
93 DO(X, ((const octet *)sbuf));
99 /* --- @md2_init@ --- *
101 * Arguments: @md2_ctx *ctx@ = pointer to context block to initialize
105 * Use: Initializes a context block ready for hashing.
108 void md2_init(md2_ctx *ctx)
110 memset(ctx->c, 0, sizeof(ctx->c));
111 memset(ctx->h, 0, sizeof(ctx->h));
115 /* --- @md2_set@ --- *
117 * Arguments: @md2_ctx *ctx@ = pointer to context block
118 * @const void *buf@ = pointer to state buffer
119 * @unsigned long count@ = current count of bytes processed
123 * Use: Initializes a context block from a given state. This is
124 * useful in cases where the initial hash state is meant to be
125 * secret, e.g., for NMAC and HMAC support.
128 void md2_set(md2_ctx *ctx, const void *buf, unsigned long count)
130 const octet *p = buf;
131 memcpy(ctx->h, p, MD2_BUFSZ);
132 memcpy(ctx->c, p + MD2_BUFSZ, MD2_BUFSZ);
136 /* --- @md2_hash@ --- *
138 * Arguments: @md2_ctx *ctx@ = pointer to context block
139 * @const void *buf@ = buffer of data to hash
140 * @size_t sz@ = size of buffer to hash
144 * Use: Hashes a buffer of data. The buffer may be of any size and
148 void md2_hash(md2_ctx *ctx, const void *buf, size_t sz)
150 const octet *bbuf = (octet *)buf;
152 /* --- Code automatically expanded from @HASH_BUFFER@ and tidied --- */
154 if (ctx->off + sz < MD2_BUFSZ) {
155 memcpy(ctx->buf + ctx->off, bbuf, sz);
159 size_t s = MD2_BUFSZ - ctx->off;
160 memcpy(ctx->buf + ctx->off, bbuf, s);
161 md2_compress(ctx, ctx->buf);
165 while (sz >= MD2_BUFSZ) {
166 md2_compress(ctx, bbuf);
167 sz -= MD2_BUFSZ; bbuf += MD2_BUFSZ;
170 memcpy(ctx->buf, bbuf, sz);
175 /* --- @md2_done@ --- *
177 * Arguments: @md2_ctx *ctx@ = pointer to context block
178 * @void *hash@ = pointer to output buffer
182 * Use: Returns the hash of the data read so far.
185 void md2_done(md2_ctx *ctx, void *hash)
187 unsigned pad = MD2_BUFSZ - ctx->off;
189 for (i = ctx->off; i < MD2_BUFSZ; i++)
191 md2_compress(ctx, ctx->buf);
192 md2_compress(ctx, ctx->c);
193 memcpy(hash, ctx->h, MD2_BUFSZ);
196 /* --- @md2_state@ --- *
198 * Arguments: @md2_ctx *ctx@ = pointer to context
199 * @void *state@ = pointer to buffer for current state
201 * Returns: Number of bytes written to the hash function so far.
203 * Use: Returns the current state of the hash function such that
204 * it can be passed to @md2_set@.
207 unsigned long md2_state(md2_ctx *ctx, void *state)
210 memcpy(p, ctx->h, MD2_BUFSZ);
211 memcpy(p + MD2_BUFSZ, ctx->c, MD2_BUFSZ);
215 /* --- Generic interface --- */
219 /* --- Test code --- */
223 /*----- That's all, folks -------------------------------------------------*/