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"
37 /*----- Tables ------------------------------------------------------------*/
39 extern const octet md2_s
[256];
41 /*----- Main code ---------------------------------------------------------*/
43 /* --- @md2_compress@ --- *
45 * Arguments: @md2_ctx *ctx@ = pointer to context block
46 * @const void *sbuf@ = pointer to buffer of appropriate size
50 * Use: MD2 compression and checksum function.
53 void md2_compress(md2_ctx
*ctx
, const void *sbuf
)
60 /* --- Handy macro for doing something lots of times --- */
62 #define DO(what, where) do { \
63 what(where, 0); what(where, 1); what(where, 2); what(where, 3); \
64 what(where, 4); what(where, 5); what(where, 6); what(where, 7); \
65 what(where, 8); what(where, 9); what(where, 10); what(where, 11); \
66 what(where, 12); what(where, 13); what(where, 14); what(where, 15); \
69 /* --- Do the hashing first to avoid corrupting the checksum --- */
71 memcpy(x
, sbuf
, sizeof(x
));
72 #define X(z, i) y[i] = z[i] ^ ctx->h[i]
77 for (i
= 0; i
< 18; i
++) {
78 #define X(z, i) t = z[i] ^= md2_s[t];
86 /* --- Now compute the checksum --- */
88 t
= ctx
->c
[MD2_BUFSZ
- 1];
89 #define X(z, i) t = ctx->c[i] ^= md2_s[z[i] ^ t]
90 DO(X
, ((const octet
*)sbuf
));
96 /* --- @md2_init@ --- *
98 * Arguments: @md2_ctx *ctx@ = pointer to context block to initialize
102 * Use: Initializes a context block ready for hashing.
105 void md2_init(md2_ctx
*ctx
)
107 memset(ctx
->c
, 0, sizeof(ctx
->c
));
108 memset(ctx
->h
, 0, sizeof(ctx
->h
));
112 /* --- @md2_set@ --- *
114 * Arguments: @md2_ctx *ctx@ = pointer to context block
115 * @const void *buf@ = pointer to state buffer
116 * @unsigned long count@ = current count of bytes processed
120 * Use: Initializes a context block from a given state. This is
121 * useful in cases where the initial hash state is meant to be
122 * secret, e.g., for NMAC and HMAC support.
125 void md2_set(md2_ctx
*ctx
, const void *buf
, unsigned long count
)
127 const octet
*p
= buf
;
128 memcpy(ctx
->h
, p
, MD2_BUFSZ
);
129 memcpy(ctx
->c
, p
+ MD2_BUFSZ
, MD2_BUFSZ
);
133 /* --- @md2_hash@ --- *
135 * Arguments: @md2_ctx *ctx@ = pointer to context block
136 * @const void *buf@ = buffer of data to hash
137 * @size_t sz@ = size of buffer to hash
141 * Use: Hashes a buffer of data. The buffer may be of any size and
145 void md2_hash(md2_ctx
*ctx
, const void *buf
, size_t sz
)
147 const octet
*bbuf
= (octet
*)buf
;
149 /* --- Code automatically expanded from @HASH_BUFFER@ and tidied --- */
151 if (ctx
->off
+ sz
< MD2_BUFSZ
) {
152 memcpy(ctx
->buf
+ ctx
->off
, bbuf
, sz
);
156 size_t s
= MD2_BUFSZ
- ctx
->off
;
157 memcpy(ctx
->buf
+ ctx
->off
, bbuf
, s
);
158 md2_compress(ctx
, ctx
->buf
);
162 while (sz
>= MD2_BUFSZ
) {
163 md2_compress(ctx
, bbuf
);
164 sz
-= MD2_BUFSZ
; bbuf
+= MD2_BUFSZ
;
167 memcpy(ctx
->buf
, bbuf
, sz
);
172 /* --- @md2_done@ --- *
174 * Arguments: @md2_ctx *ctx@ = pointer to context block
175 * @void *hash@ = pointer to output buffer
179 * Use: Returns the hash of the data read so far.
182 void md2_done(md2_ctx
*ctx
, void *hash
)
184 unsigned pad
= MD2_BUFSZ
- ctx
->off
;
186 for (i
= ctx
->off
; i
< MD2_BUFSZ
; i
++)
188 md2_compress(ctx
, ctx
->buf
);
189 md2_compress(ctx
, ctx
->c
);
190 memcpy(hash
, ctx
->h
, MD2_BUFSZ
);
193 /* --- @md2_state@ --- *
195 * Arguments: @md2_ctx *ctx@ = pointer to context
196 * @void *state@ = pointer to buffer for current state
198 * Returns: Number of bytes written to the hash function so far.
200 * Use: Returns the current state of the hash function such that
201 * it can be passed to @md2_set@.
204 unsigned long md2_state(md2_ctx
*ctx
, void *state
)
207 memcpy(p
, ctx
->h
, MD2_BUFSZ
);
208 memcpy(p
+ MD2_BUFSZ
, ctx
->c
, MD2_BUFSZ
);
212 /* --- Generic interface --- */
216 /* --- Test code --- */
220 /*----- That's all, folks -------------------------------------------------*/