d3187d77 |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: md2.c,v 1.1 2001/02/21 20:03:22 mdw Exp $ |
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 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: md2.c,v $ |
33 | * Revision 1.1 2001/02/21 20:03:22 mdw |
34 | * Added support for MD2 hash function. |
35 | * |
36 | */ |
37 | |
38 | /*----- Header files ------------------------------------------------------*/ |
39 | |
40 | #include <mLib/bits.h> |
41 | |
42 | #include "ghash.h" |
43 | #include "ghash-def.h" |
44 | #include "hash.h" |
45 | #include "md2.h" |
46 | #include "md2-tab.h" |
47 | |
48 | /*----- Tables ------------------------------------------------------------*/ |
49 | |
50 | static octet s[256] = MD2_S; |
51 | |
52 | /*----- Main code ---------------------------------------------------------*/ |
53 | |
54 | /* --- @md2_compress@ --- * |
55 | * |
56 | * Arguments: @md2_ctx *ctx@ = pointer to context block |
57 | * @const void *sbuf@ = pointer to buffer of appropriate size |
58 | * |
59 | * Returns: --- |
60 | * |
61 | * Use: MD2 compression and checksum function. |
62 | */ |
63 | |
64 | void md2_compress(md2_ctx *ctx, const void *sbuf) |
65 | { |
66 | unsigned i; |
67 | unsigned t; |
68 | octet x[MD2_BUFSZ]; |
69 | octet y[MD2_BUFSZ]; |
70 | |
71 | /* --- Handy macro for doing something lots of times --- */ |
72 | |
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); \ |
78 | } while (0) |
79 | |
80 | /* --- Do the hashing first to avoid corrupting the checksum --- */ |
81 | |
82 | memcpy(x, sbuf, sizeof(x)); |
83 | #define X(z, i) y[i] = z[i] ^ ctx->h[i] |
84 | DO(X, x); |
85 | #undef X |
86 | |
87 | t = 0; |
88 | for (i = 0; i < 18; i++) { |
89 | #define X(z, i) t = z[i] ^= s[t]; |
90 | DO(X, ctx->h); |
91 | DO(X, x); |
92 | DO(X, y); |
93 | #undef X |
94 | t = U8(t + i); |
95 | } |
96 | |
97 | /* --- Now compute the checksum --- */ |
98 | |
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)); |
102 | #undef X |
103 | |
104 | #undef DO |
105 | } |
106 | |
107 | /* --- @md2_init@ --- * |
108 | * |
109 | * Arguments: @md2_ctx *ctx@ = pointer to context block to initialize |
110 | * |
111 | * Returns: --- |
112 | * |
113 | * Use: Initializes a context block ready for hashing. |
114 | */ |
115 | |
116 | void md2_init(md2_ctx *ctx) |
117 | { |
118 | memset(ctx->c, 0, sizeof(ctx->c)); |
119 | memset(ctx->h, 0, sizeof(ctx->h)); |
120 | ctx->off = 0; |
121 | } |
122 | |
123 | /* --- @md2_set@ --- * |
124 | * |
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 |
128 | * |
129 | * Returns: --- |
130 | * |
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. |
134 | */ |
135 | |
136 | void md2_set(md2_ctx *ctx, const void *buf, unsigned long count) |
137 | { |
138 | const octet *p = buf; |
139 | memcpy(ctx->h, p, MD2_BUFSZ); |
140 | memcpy(ctx->c, p + MD2_BUFSZ, MD2_BUFSZ); |
141 | ctx->off = 0; |
142 | } |
143 | |
144 | /* --- @md2_hash@ --- * |
145 | * |
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 |
149 | * |
150 | * Returns: --- |
151 | * |
152 | * Use: Hashes a buffer of data. The buffer may be of any size and |
153 | * alignment. |
154 | */ |
155 | |
156 | void md2_hash(md2_ctx *ctx, const void *buf, size_t sz) |
157 | { |
158 | const octet *bbuf = (octet *)buf; |
159 | |
160 | /* --- Code automatically expanded from @HASH_BUFFER@ and tidied --- */ |
161 | |
162 | if (ctx->off + sz < MD2_BUFSZ) { |
163 | memcpy(ctx->buf + ctx->off, bbuf, sz); |
164 | ctx->off += sz; |
165 | } else { |
166 | if (ctx->off) { |
167 | size_t s = MD2_BUFSZ - ctx->off; |
168 | memcpy(ctx->buf + ctx->off, bbuf, s); |
169 | md2_compress(ctx, ctx->buf); |
170 | sz -= s; |
171 | bbuf += s; |
172 | } |
173 | while (sz >= MD2_BUFSZ) { |
174 | md2_compress(ctx, bbuf); |
175 | sz -= MD2_BUFSZ; bbuf += MD2_BUFSZ; |
176 | } |
177 | if (sz) |
178 | memcpy(ctx->buf, bbuf, sz); |
179 | ctx->off = sz; |
180 | } |
181 | } |
182 | |
183 | /* --- @md2_done@ --- * |
184 | * |
185 | * Arguments: @md2_ctx *ctx@ = pointer to context block |
186 | * @void *hash@ = pointer to output buffer |
187 | * |
188 | * Returns: --- |
189 | * |
190 | * Use: Returns the hash of the data read so far. |
191 | */ |
192 | |
193 | void md2_done(md2_ctx *ctx, void *hash) |
194 | { |
195 | unsigned pad = MD2_BUFSZ - ctx->off; |
196 | unsigned i; |
197 | for (i = ctx->off; i < MD2_BUFSZ; i++) |
198 | ctx->buf[i] = pad; |
199 | md2_compress(ctx, ctx->buf); |
200 | md2_compress(ctx, ctx->c); |
201 | memcpy(hash, ctx->h, MD2_BUFSZ); |
202 | } |
203 | |
204 | /* --- @md2_state@ --- * |
205 | * |
206 | * Arguments: @md2_ctx *ctx@ = pointer to context |
207 | * @void *state@ = pointer to buffer for current state |
208 | * |
209 | * Returns: Number of bytes written to the hash function so far. |
210 | * |
211 | * Use: Returns the current state of the hash function such that |
212 | * it can be passed to @md2_set@. |
213 | */ |
214 | |
215 | unsigned long md2_state(md2_ctx *ctx, void *state) |
216 | { |
217 | octet *p = state; |
218 | memcpy(p, ctx->h, MD2_BUFSZ); |
219 | memcpy(p + MD2_BUFSZ, ctx->c, MD2_BUFSZ); |
220 | return (0); |
221 | } |
222 | |
223 | /* --- Generic interface --- */ |
224 | |
225 | GHASH_DEF(MD2, md2) |
226 | |
227 | /* --- Test code --- */ |
228 | |
229 | HASH_TEST(MD2, md2) |
230 | |
231 | /*----- That's all, folks -------------------------------------------------*/ |