Commit | Line | Data |
---|---|---|
d03ab969 | 1 | /* -*-c-*- |
2 | * | |
b817bfc6 | 3 | * $Id: md4.c,v 1.4 2004/04/08 01:36:15 mdw Exp $ |
d03ab969 | 4 | * |
5 | * The MD4 message digest function | |
6 | * | |
7 | * (c) 1999 Straylight/Edgeware | |
8 | */ | |
9 | ||
45c0fd36 | 10 | /*----- Licensing notice --------------------------------------------------* |
d03ab969 | 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. | |
45c0fd36 | 18 | * |
d03ab969 | 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. | |
45c0fd36 | 23 | * |
d03ab969 | 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 | ||
d03ab969 | 30 | /*----- Header files ------------------------------------------------------*/ |
31 | ||
32 | #include <mLib/bits.h> | |
33 | ||
dc2f0497 | 34 | #include "ghash.h" |
35 | #include "ghash-def.h" | |
d03ab969 | 36 | #include "hash.h" |
37 | #include "md4.h" | |
38 | ||
39 | /*----- Main code ---------------------------------------------------------*/ | |
40 | ||
41 | /* --- @md4_compress@ --- * | |
42 | * | |
43 | * Arguments: @md4_ctx *ctx@ = pointer to context block | |
44 | * @const void *sbuf@ = pointer to buffer of appropriate size | |
45 | * | |
46 | * Returns: --- | |
47 | * | |
48 | * Use: RIPEMD-160 compression function. | |
49 | */ | |
50 | ||
51 | void md4_compress(md4_ctx *ctx, const void *sbuf) | |
52 | { | |
53 | uint32 a, b, c, d; | |
54 | uint32 buf[16]; | |
55 | ||
56 | /* --- Fetch the chaining variables --- */ | |
57 | ||
58 | a = ctx->a; | |
59 | b = ctx->b; | |
60 | c = ctx->c; | |
61 | d = ctx->d; | |
62 | ||
63 | /* --- Fetch the buffer contents --- */ | |
64 | ||
65 | { | |
66 | int i; | |
67 | const octet *p; | |
68 | ||
69 | for (i = 0, p = sbuf; i < 16; i++, p += 4) | |
70 | buf[i] = LOAD32_L(p); | |
71 | } | |
72 | ||
73 | /* --- Definitions for round functions --- */ | |
74 | ||
75 | #define F(x, y, z) (((x) & (y)) | (~(x) & (z))) | |
76 | #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) | |
77 | #define H(x, y, z) ((x) ^ (y) ^ (z)) | |
78 | ||
79 | #define T(w, x, y, z, i, r, k, f) do { \ | |
80 | uint32 _t = w + f(x, y, z) + buf[i] + k; \ | |
81 | w = ROL32(_t, r); \ | |
82 | } while (0) | |
83 | ||
84 | #define FF(w, x, y, z, i, r) T(w, x, y, z, i, r, 0x00000000, F) | |
85 | #define GG(w, x, y, z, i, r) T(w, x, y, z, i, r, 0x5a827999, G) | |
86 | #define HH(w, x, y, z, i, r) T(w, x, y, z, i, r, 0x6ed9eba1, H) | |
87 | ||
88 | /* --- The main compression function --- */ | |
89 | ||
90 | FF(a, b, c, d, 0, 3); | |
91 | FF(d, a, b, c, 1, 7); | |
92 | FF(c, d, a, b, 2, 11); | |
93 | FF(b, c, d, a, 3, 19); | |
94 | FF(a, b, c, d, 4, 3); | |
95 | FF(d, a, b, c, 5, 7); | |
96 | FF(c, d, a, b, 6, 11); | |
97 | FF(b, c, d, a, 7, 19); | |
98 | FF(a, b, c, d, 8, 3); | |
99 | FF(d, a, b, c, 9, 7); | |
100 | FF(c, d, a, b, 10, 11); | |
101 | FF(b, c, d, a, 11, 19); | |
102 | FF(a, b, c, d, 12, 3); | |
103 | FF(d, a, b, c, 13, 7); | |
104 | FF(c, d, a, b, 14, 11); | |
105 | FF(b, c, d, a, 15, 19); | |
106 | ||
107 | GG(a, b, c, d, 0, 3); | |
108 | GG(d, a, b, c, 4, 5); | |
109 | GG(c, d, a, b, 8, 9); | |
110 | GG(b, c, d, a, 12, 13); | |
111 | GG(a, b, c, d, 1, 3); | |
112 | GG(d, a, b, c, 5, 5); | |
113 | GG(c, d, a, b, 9, 9); | |
114 | GG(b, c, d, a, 13, 13); | |
115 | GG(a, b, c, d, 2, 3); | |
116 | GG(d, a, b, c, 6, 5); | |
117 | GG(c, d, a, b, 10, 9); | |
118 | GG(b, c, d, a, 14, 13); | |
119 | GG(a, b, c, d, 3, 3); | |
120 | GG(d, a, b, c, 7, 5); | |
121 | GG(c, d, a, b, 11, 9); | |
122 | GG(b, c, d, a, 15, 13); | |
123 | ||
124 | HH(a, b, c, d, 0, 3); | |
125 | HH(d, a, b, c, 8, 9); | |
126 | HH(c, d, a, b, 4, 11); | |
127 | HH(b, c, d, a, 12, 15); | |
128 | HH(a, b, c, d, 2, 3); | |
129 | HH(d, a, b, c, 10, 9); | |
130 | HH(c, d, a, b, 6, 11); | |
131 | HH(b, c, d, a, 14, 15); | |
132 | HH(a, b, c, d, 1, 3); | |
133 | HH(d, a, b, c, 9, 9); | |
134 | HH(c, d, a, b, 5, 11); | |
135 | HH(b, c, d, a, 13, 15); | |
136 | HH(a, b, c, d, 3, 3); | |
137 | HH(d, a, b, c, 11, 9); | |
138 | HH(c, d, a, b, 7, 11); | |
139 | HH(b, c, d, a, 15, 15); | |
140 | ||
141 | /* --- Update the chaining variables --- */ | |
142 | ||
143 | ctx->a += a; | |
144 | ctx->b += b; | |
145 | ctx->c += c; | |
146 | ctx->d += d; | |
147 | } | |
148 | ||
149 | /* --- @md4_init@ --- * | |
150 | * | |
151 | * Arguments: @md4_ctx *ctx@ = pointer to context block to initialize | |
152 | * | |
153 | * Returns: --- | |
154 | * | |
155 | * Use: Initializes a context block ready for hashing. | |
156 | */ | |
157 | ||
158 | void md4_init(md4_ctx *ctx) | |
159 | { | |
160 | ctx->a = 0x67452301; | |
161 | ctx->b = 0xefcdab89; | |
162 | ctx->c = 0x98badcfe; | |
163 | ctx->d = 0x10325476; | |
164 | ctx->off = 0; | |
dc2f0497 | 165 | ctx->nl = ctx->nh = 0; |
d03ab969 | 166 | } |
167 | ||
168 | /* --- @md4_set@ --- * | |
169 | * | |
170 | * Arguments: @md4_ctx *ctx@ = pointer to context block | |
171 | * @const void *buf@ = pointer to state buffer | |
172 | * @unsigned long count@ = current count of bytes processed | |
173 | * | |
174 | * Returns: --- | |
175 | * | |
176 | * Use: Initializes a context block from a given state. This is | |
177 | * useful in cases where the initial hash state is meant to be | |
178 | * secret, e.g., for NMAC and HMAC support. | |
179 | */ | |
180 | ||
181 | void md4_set(md4_ctx *ctx, const void *buf, unsigned long count) | |
182 | { | |
183 | const octet *p = buf; | |
45c0fd36 MW |
184 | ctx->a = LOAD32_L(p + 0); |
185 | ctx->b = LOAD32_L(p + 4); | |
186 | ctx->c = LOAD32_L(p + 8); | |
d03ab969 | 187 | ctx->d = LOAD32_L(p + 12); |
188 | ctx->off = 0; | |
dc2f0497 | 189 | ctx->nl = U32(count); |
5d59cd77 | 190 | ctx->nh = U32(((count & ~MASK32) >> 16) >> 16); |
d03ab969 | 191 | } |
192 | ||
193 | /* --- @md4_hash@ --- * | |
194 | * | |
195 | * Arguments: @md4_ctx *ctx@ = pointer to context block | |
196 | * @const void *buf@ = buffer of data to hash | |
197 | * @size_t sz@ = size of buffer to hash | |
198 | * | |
199 | * Returns: --- | |
200 | * | |
201 | * Use: Hashes a buffer of data. The buffer may be of any size and | |
202 | * alignment. | |
203 | */ | |
204 | ||
205 | void md4_hash(md4_ctx *ctx, const void *buf, size_t sz) | |
206 | { | |
207 | HASH_BUFFER(MD4, md4, ctx, buf, sz); | |
208 | } | |
209 | ||
210 | /* --- @md4_done@ --- * | |
211 | * | |
212 | * Arguments: @md4_ctx *ctx@ = pointer to context block | |
213 | * @void *hash@ = pointer to output buffer | |
214 | * | |
215 | * Returns: --- | |
216 | * | |
217 | * Use: Returns the hash of the data read so far. | |
218 | */ | |
219 | ||
220 | void md4_done(md4_ctx *ctx, void *hash) | |
221 | { | |
222 | octet *p = hash; | |
223 | HASH_MD5STRENGTH(MD4, md4, ctx); | |
45c0fd36 MW |
224 | STORE32_L(p + 0, ctx->a); |
225 | STORE32_L(p + 4, ctx->b); | |
226 | STORE32_L(p + 8, ctx->c); | |
d03ab969 | 227 | STORE32_L(p + 12, ctx->d); |
228 | } | |
229 | ||
230 | /* --- @md4_state@ --- * | |
231 | * | |
232 | * Arguments: @md4_ctx *ctx@ = pointer to context | |
233 | * @void *state@ = pointer to buffer for current state | |
234 | * | |
235 | * Returns: Number of bytes written to the hash function so far. | |
236 | * | |
237 | * Use: Returns the current state of the hash function such that | |
238 | * it can be passed to @md4_set@. | |
239 | */ | |
240 | ||
241 | unsigned long md4_state(md4_ctx *ctx, void *state) | |
242 | { | |
243 | octet *p = state; | |
45c0fd36 MW |
244 | STORE32_L(p + 0, ctx->a); |
245 | STORE32_L(p + 4, ctx->b); | |
246 | STORE32_L(p + 8, ctx->c); | |
d03ab969 | 247 | STORE32_L(p + 12, ctx->d); |
dc2f0497 | 248 | return (ctx->nl | ((ctx->nh >> 16) >> 16)); |
d03ab969 | 249 | } |
250 | ||
dc2f0497 | 251 | /* --- Generic interface --- */ |
252 | ||
253 | GHASH_DEF(MD4, md4) | |
254 | ||
d03ab969 | 255 | /* --- Test rig --- */ |
256 | ||
257 | HASH_TEST(MD4, md4) | |
258 | ||
259 | /*----- That's all, folks -------------------------------------------------*/ |