3 * $Id: md2.c,v 1.2 2004/04/02 01:03:49 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
36 * Revision 1.1 2001/02/21 20:03:22 mdw
37 * Added support for MD2 hash function.
41 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/bits.h>
46 #include "ghash-def.h"
51 /*----- Tables ------------------------------------------------------------*/
53 static const octet s
[256] = MD2_S
;
55 /*----- Main code ---------------------------------------------------------*/
57 /* --- @md2_compress@ --- *
59 * Arguments: @md2_ctx *ctx@ = pointer to context block
60 * @const void *sbuf@ = pointer to buffer of appropriate size
64 * Use: MD2 compression and checksum function.
67 void md2_compress(md2_ctx
*ctx
, const void *sbuf
)
74 /* --- Handy macro for doing something lots of times --- */
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); \
83 /* --- Do the hashing first to avoid corrupting the checksum --- */
85 memcpy(x
, sbuf
, sizeof(x
));
86 #define X(z, i) y[i] = z[i] ^ ctx->h[i]
91 for (i
= 0; i
< 18; i
++) {
92 #define X(z, i) t = z[i] ^= s[t];
100 /* --- Now compute the checksum --- */
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
));
110 /* --- @md2_init@ --- *
112 * Arguments: @md2_ctx *ctx@ = pointer to context block to initialize
116 * Use: Initializes a context block ready for hashing.
119 void md2_init(md2_ctx
*ctx
)
121 memset(ctx
->c
, 0, sizeof(ctx
->c
));
122 memset(ctx
->h
, 0, sizeof(ctx
->h
));
126 /* --- @md2_set@ --- *
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
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.
139 void md2_set(md2_ctx
*ctx
, const void *buf
, unsigned long count
)
141 const octet
*p
= buf
;
142 memcpy(ctx
->h
, p
, MD2_BUFSZ
);
143 memcpy(ctx
->c
, p
+ MD2_BUFSZ
, MD2_BUFSZ
);
147 /* --- @md2_hash@ --- *
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
155 * Use: Hashes a buffer of data. The buffer may be of any size and
159 void md2_hash(md2_ctx
*ctx
, const void *buf
, size_t sz
)
161 const octet
*bbuf
= (octet
*)buf
;
163 /* --- Code automatically expanded from @HASH_BUFFER@ and tidied --- */
165 if (ctx
->off
+ sz
< MD2_BUFSZ
) {
166 memcpy(ctx
->buf
+ ctx
->off
, bbuf
, sz
);
170 size_t s
= MD2_BUFSZ
- ctx
->off
;
171 memcpy(ctx
->buf
+ ctx
->off
, bbuf
, s
);
172 md2_compress(ctx
, ctx
->buf
);
176 while (sz
>= MD2_BUFSZ
) {
177 md2_compress(ctx
, bbuf
);
178 sz
-= MD2_BUFSZ
; bbuf
+= MD2_BUFSZ
;
181 memcpy(ctx
->buf
, bbuf
, sz
);
186 /* --- @md2_done@ --- *
188 * Arguments: @md2_ctx *ctx@ = pointer to context block
189 * @void *hash@ = pointer to output buffer
193 * Use: Returns the hash of the data read so far.
196 void md2_done(md2_ctx
*ctx
, void *hash
)
198 unsigned pad
= MD2_BUFSZ
- ctx
->off
;
200 for (i
= ctx
->off
; i
< MD2_BUFSZ
; i
++)
202 md2_compress(ctx
, ctx
->buf
);
203 md2_compress(ctx
, ctx
->c
);
204 memcpy(hash
, ctx
->h
, MD2_BUFSZ
);
207 /* --- @md2_state@ --- *
209 * Arguments: @md2_ctx *ctx@ = pointer to context
210 * @void *state@ = pointer to buffer for current state
212 * Returns: Number of bytes written to the hash function so far.
214 * Use: Returns the current state of the hash function such that
215 * it can be passed to @md2_set@.
218 unsigned long md2_state(md2_ctx
*ctx
, void *state
)
221 memcpy(p
, ctx
->h
, MD2_BUFSZ
);
222 memcpy(p
+ MD2_BUFSZ
, ctx
->c
, MD2_BUFSZ
);
226 /* --- Generic interface --- */
230 /* --- Test code --- */
234 /*----- That's all, folks -------------------------------------------------*/