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