3 * The MD2 message digest function
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
30 #include <mLib/bits.h>
33 #include "ghash-def.h"
38 /*----- Tables ------------------------------------------------------------*/
40 static const octet s
[256] = MD2_S
;
42 /*----- Main code ---------------------------------------------------------*/
44 /* --- @md2_compress@ --- *
46 * Arguments: @md2_ctx *ctx@ = pointer to context block
47 * @const void *sbuf@ = pointer to buffer of appropriate size
51 * Use: MD2 compression and checksum function.
54 void md2_compress(md2_ctx
*ctx
, const void *sbuf
)
61 /* --- Handy macro for doing something lots of times --- */
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); \
70 /* --- Do the hashing first to avoid corrupting the checksum --- */
72 memcpy(x
, sbuf
, sizeof(x
));
73 #define X(z, i) y[i] = z[i] ^ ctx->h[i]
78 for (i
= 0; i
< 18; i
++) {
79 #define X(z, i) t = z[i] ^= s[t];
87 /* --- Now compute the checksum --- */
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
));
97 /* --- @md2_init@ --- *
99 * Arguments: @md2_ctx *ctx@ = pointer to context block to initialize
103 * Use: Initializes a context block ready for hashing.
106 void md2_init(md2_ctx
*ctx
)
108 memset(ctx
->c
, 0, sizeof(ctx
->c
));
109 memset(ctx
->h
, 0, sizeof(ctx
->h
));
113 /* --- @md2_set@ --- *
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
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.
126 void md2_set(md2_ctx
*ctx
, const void *buf
, unsigned long count
)
128 const octet
*p
= buf
;
129 memcpy(ctx
->h
, p
, MD2_BUFSZ
);
130 memcpy(ctx
->c
, p
+ MD2_BUFSZ
, MD2_BUFSZ
);
134 /* --- @md2_hash@ --- *
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
142 * Use: Hashes a buffer of data. The buffer may be of any size and
146 void md2_hash(md2_ctx
*ctx
, const void *buf
, size_t sz
)
148 const octet
*bbuf
= (octet
*)buf
;
150 /* --- Code automatically expanded from @HASH_BUFFER@ and tidied --- */
152 if (ctx
->off
+ sz
< MD2_BUFSZ
) {
153 memcpy(ctx
->buf
+ ctx
->off
, bbuf
, sz
);
157 size_t s
= MD2_BUFSZ
- ctx
->off
;
158 memcpy(ctx
->buf
+ ctx
->off
, bbuf
, s
);
159 md2_compress(ctx
, ctx
->buf
);
163 while (sz
>= MD2_BUFSZ
) {
164 md2_compress(ctx
, bbuf
);
165 sz
-= MD2_BUFSZ
; bbuf
+= MD2_BUFSZ
;
168 memcpy(ctx
->buf
, bbuf
, sz
);
173 /* --- @md2_done@ --- *
175 * Arguments: @md2_ctx *ctx@ = pointer to context block
176 * @void *hash@ = pointer to output buffer
180 * Use: Returns the hash of the data read so far.
183 void md2_done(md2_ctx
*ctx
, void *hash
)
185 unsigned pad
= MD2_BUFSZ
- ctx
->off
;
187 for (i
= ctx
->off
; i
< MD2_BUFSZ
; i
++)
189 md2_compress(ctx
, ctx
->buf
);
190 md2_compress(ctx
, ctx
->c
);
191 memcpy(hash
, ctx
->h
, MD2_BUFSZ
);
194 /* --- @md2_state@ --- *
196 * Arguments: @md2_ctx *ctx@ = pointer to context
197 * @void *state@ = pointer to buffer for current state
199 * Returns: Number of bytes written to the hash function so far.
201 * Use: Returns the current state of the hash function such that
202 * it can be passed to @md2_set@.
205 unsigned long md2_state(md2_ctx
*ctx
, void *state
)
208 memcpy(p
, ctx
->h
, MD2_BUFSZ
);
209 memcpy(p
+ MD2_BUFSZ
, ctx
->c
, MD2_BUFSZ
);
213 /* --- Generic interface --- */
217 /* --- Test code --- */
221 /*----- That's all, folks -------------------------------------------------*/