3 * $Id: md2.c,v 1.1 2001/02/21 20:03:22 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.1 2001/02/21 20:03:22 mdw
34 * Added support for MD2 hash function.
38 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/bits.h>
43 #include "ghash-def.h"
48 /*----- Tables ------------------------------------------------------------*/
50 static octet s
[256] = MD2_S
;
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @md2_compress@ --- *
56 * Arguments: @md2_ctx *ctx@ = pointer to context block
57 * @const void *sbuf@ = pointer to buffer of appropriate size
61 * Use: MD2 compression and checksum function.
64 void md2_compress(md2_ctx
*ctx
, const void *sbuf
)
71 /* --- Handy macro for doing something lots of times --- */
73 #define DO(what, where) do { \
74 what(where, 0); what(where, 1); what(where, 2); what(where, 3); \
75 what(where, 4); what(where, 5); what(where, 6); what(where, 7); \
76 what(where, 8); what(where, 9); what(where, 10); what(where, 11); \
77 what(where, 12); what(where, 13); what(where, 14); what(where, 15); \
80 /* --- Do the hashing first to avoid corrupting the checksum --- */
82 memcpy(x
, sbuf
, sizeof(x
));
83 #define X(z, i) y[i] = z[i] ^ ctx->h[i]
88 for (i
= 0; i
< 18; i
++) {
89 #define X(z, i) t = z[i] ^= s[t];
97 /* --- Now compute the checksum --- */
99 t
= ctx
->c
[MD2_BUFSZ
- 1];
100 #define X(z, i) t = ctx->c[i] ^= s[z[i] ^ t]
101 DO(X
, ((const octet
*)sbuf
));
107 /* --- @md2_init@ --- *
109 * Arguments: @md2_ctx *ctx@ = pointer to context block to initialize
113 * Use: Initializes a context block ready for hashing.
116 void md2_init(md2_ctx
*ctx
)
118 memset(ctx
->c
, 0, sizeof(ctx
->c
));
119 memset(ctx
->h
, 0, sizeof(ctx
->h
));
123 /* --- @md2_set@ --- *
125 * Arguments: @md2_ctx *ctx@ = pointer to context block
126 * @const void *buf@ = pointer to state buffer
127 * @unsigned long count@ = current count of bytes processed
131 * Use: Initializes a context block from a given state. This is
132 * useful in cases where the initial hash state is meant to be
133 * secret, e.g., for NMAC and HMAC support.
136 void md2_set(md2_ctx
*ctx
, const void *buf
, unsigned long count
)
138 const octet
*p
= buf
;
139 memcpy(ctx
->h
, p
, MD2_BUFSZ
);
140 memcpy(ctx
->c
, p
+ MD2_BUFSZ
, MD2_BUFSZ
);
144 /* --- @md2_hash@ --- *
146 * Arguments: @md2_ctx *ctx@ = pointer to context block
147 * @const void *buf@ = buffer of data to hash
148 * @size_t sz@ = size of buffer to hash
152 * Use: Hashes a buffer of data. The buffer may be of any size and
156 void md2_hash(md2_ctx
*ctx
, const void *buf
, size_t sz
)
158 const octet
*bbuf
= (octet
*)buf
;
160 /* --- Code automatically expanded from @HASH_BUFFER@ and tidied --- */
162 if (ctx
->off
+ sz
< MD2_BUFSZ
) {
163 memcpy(ctx
->buf
+ ctx
->off
, bbuf
, sz
);
167 size_t s
= MD2_BUFSZ
- ctx
->off
;
168 memcpy(ctx
->buf
+ ctx
->off
, bbuf
, s
);
169 md2_compress(ctx
, ctx
->buf
);
173 while (sz
>= MD2_BUFSZ
) {
174 md2_compress(ctx
, bbuf
);
175 sz
-= MD2_BUFSZ
; bbuf
+= MD2_BUFSZ
;
178 memcpy(ctx
->buf
, bbuf
, sz
);
183 /* --- @md2_done@ --- *
185 * Arguments: @md2_ctx *ctx@ = pointer to context block
186 * @void *hash@ = pointer to output buffer
190 * Use: Returns the hash of the data read so far.
193 void md2_done(md2_ctx
*ctx
, void *hash
)
195 unsigned pad
= MD2_BUFSZ
- ctx
->off
;
197 for (i
= ctx
->off
; i
< MD2_BUFSZ
; i
++)
199 md2_compress(ctx
, ctx
->buf
);
200 md2_compress(ctx
, ctx
->c
);
201 memcpy(hash
, ctx
->h
, MD2_BUFSZ
);
204 /* --- @md2_state@ --- *
206 * Arguments: @md2_ctx *ctx@ = pointer to context
207 * @void *state@ = pointer to buffer for current state
209 * Returns: Number of bytes written to the hash function so far.
211 * Use: Returns the current state of the hash function such that
212 * it can be passed to @md2_set@.
215 unsigned long md2_state(md2_ctx
*ctx
, void *state
)
218 memcpy(p
, ctx
->h
, MD2_BUFSZ
);
219 memcpy(p
+ MD2_BUFSZ
, ctx
->c
, MD2_BUFSZ
);
223 /* --- Generic interface --- */
227 /* --- Test code --- */
231 /*----- That's all, folks -------------------------------------------------*/