d3187d77 |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: md2.c,v 1.3 2004/04/08 01:36:15 mdw Exp $ |
d3187d77 |
4 | * |
5 | * The MD2 message digest function |
6 | * |
7 | * (c) 2001 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
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. |
18 | * |
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. |
23 | * |
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, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
d3187d77 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include <mLib/bits.h> |
33 | |
34 | #include "ghash.h" |
35 | #include "ghash-def.h" |
36 | #include "hash.h" |
37 | #include "md2.h" |
38 | #include "md2-tab.h" |
39 | |
40 | /*----- Tables ------------------------------------------------------------*/ |
41 | |
4e66da02 |
42 | static const octet s[256] = MD2_S; |
d3187d77 |
43 | |
44 | /*----- Main code ---------------------------------------------------------*/ |
45 | |
46 | /* --- @md2_compress@ --- * |
47 | * |
48 | * Arguments: @md2_ctx *ctx@ = pointer to context block |
49 | * @const void *sbuf@ = pointer to buffer of appropriate size |
50 | * |
51 | * Returns: --- |
52 | * |
53 | * Use: MD2 compression and checksum function. |
54 | */ |
55 | |
56 | void md2_compress(md2_ctx *ctx, const void *sbuf) |
57 | { |
58 | unsigned i; |
59 | unsigned t; |
60 | octet x[MD2_BUFSZ]; |
61 | octet y[MD2_BUFSZ]; |
62 | |
63 | /* --- Handy macro for doing something lots of times --- */ |
64 | |
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); \ |
70 | } while (0) |
71 | |
72 | /* --- Do the hashing first to avoid corrupting the checksum --- */ |
73 | |
74 | memcpy(x, sbuf, sizeof(x)); |
75 | #define X(z, i) y[i] = z[i] ^ ctx->h[i] |
76 | DO(X, x); |
77 | #undef X |
78 | |
79 | t = 0; |
80 | for (i = 0; i < 18; i++) { |
81 | #define X(z, i) t = z[i] ^= s[t]; |
82 | DO(X, ctx->h); |
83 | DO(X, x); |
84 | DO(X, y); |
85 | #undef X |
86 | t = U8(t + i); |
87 | } |
88 | |
89 | /* --- Now compute the checksum --- */ |
90 | |
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)); |
94 | #undef X |
95 | |
96 | #undef DO |
97 | } |
98 | |
99 | /* --- @md2_init@ --- * |
100 | * |
101 | * Arguments: @md2_ctx *ctx@ = pointer to context block to initialize |
102 | * |
103 | * Returns: --- |
104 | * |
105 | * Use: Initializes a context block ready for hashing. |
106 | */ |
107 | |
108 | void md2_init(md2_ctx *ctx) |
109 | { |
110 | memset(ctx->c, 0, sizeof(ctx->c)); |
111 | memset(ctx->h, 0, sizeof(ctx->h)); |
112 | ctx->off = 0; |
113 | } |
114 | |
115 | /* --- @md2_set@ --- * |
116 | * |
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 |
120 | * |
121 | * Returns: --- |
122 | * |
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. |
126 | */ |
127 | |
128 | void md2_set(md2_ctx *ctx, const void *buf, unsigned long count) |
129 | { |
130 | const octet *p = buf; |
131 | memcpy(ctx->h, p, MD2_BUFSZ); |
132 | memcpy(ctx->c, p + MD2_BUFSZ, MD2_BUFSZ); |
133 | ctx->off = 0; |
134 | } |
135 | |
136 | /* --- @md2_hash@ --- * |
137 | * |
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 |
141 | * |
142 | * Returns: --- |
143 | * |
144 | * Use: Hashes a buffer of data. The buffer may be of any size and |
145 | * alignment. |
146 | */ |
147 | |
148 | void md2_hash(md2_ctx *ctx, const void *buf, size_t sz) |
149 | { |
150 | const octet *bbuf = (octet *)buf; |
151 | |
152 | /* --- Code automatically expanded from @HASH_BUFFER@ and tidied --- */ |
153 | |
154 | if (ctx->off + sz < MD2_BUFSZ) { |
155 | memcpy(ctx->buf + ctx->off, bbuf, sz); |
156 | ctx->off += sz; |
157 | } else { |
158 | if (ctx->off) { |
159 | size_t s = MD2_BUFSZ - ctx->off; |
160 | memcpy(ctx->buf + ctx->off, bbuf, s); |
161 | md2_compress(ctx, ctx->buf); |
162 | sz -= s; |
163 | bbuf += s; |
164 | } |
165 | while (sz >= MD2_BUFSZ) { |
166 | md2_compress(ctx, bbuf); |
167 | sz -= MD2_BUFSZ; bbuf += MD2_BUFSZ; |
168 | } |
169 | if (sz) |
170 | memcpy(ctx->buf, bbuf, sz); |
171 | ctx->off = sz; |
172 | } |
173 | } |
174 | |
175 | /* --- @md2_done@ --- * |
176 | * |
177 | * Arguments: @md2_ctx *ctx@ = pointer to context block |
178 | * @void *hash@ = pointer to output buffer |
179 | * |
180 | * Returns: --- |
181 | * |
182 | * Use: Returns the hash of the data read so far. |
183 | */ |
184 | |
185 | void md2_done(md2_ctx *ctx, void *hash) |
186 | { |
187 | unsigned pad = MD2_BUFSZ - ctx->off; |
188 | unsigned i; |
189 | for (i = ctx->off; i < MD2_BUFSZ; i++) |
190 | ctx->buf[i] = pad; |
191 | md2_compress(ctx, ctx->buf); |
192 | md2_compress(ctx, ctx->c); |
193 | memcpy(hash, ctx->h, MD2_BUFSZ); |
194 | } |
195 | |
196 | /* --- @md2_state@ --- * |
197 | * |
198 | * Arguments: @md2_ctx *ctx@ = pointer to context |
199 | * @void *state@ = pointer to buffer for current state |
200 | * |
201 | * Returns: Number of bytes written to the hash function so far. |
202 | * |
203 | * Use: Returns the current state of the hash function such that |
204 | * it can be passed to @md2_set@. |
205 | */ |
206 | |
207 | unsigned long md2_state(md2_ctx *ctx, void *state) |
208 | { |
209 | octet *p = state; |
210 | memcpy(p, ctx->h, MD2_BUFSZ); |
211 | memcpy(p + MD2_BUFSZ, ctx->c, MD2_BUFSZ); |
212 | return (0); |
213 | } |
214 | |
215 | /* --- Generic interface --- */ |
216 | |
217 | GHASH_DEF(MD2, md2) |
218 | |
219 | /* --- Test code --- */ |
220 | |
221 | HASH_TEST(MD2, md2) |
222 | |
223 | /*----- That's all, folks -------------------------------------------------*/ |