Commit | Line | Data |
---|---|---|
d03ab969 | 1 | /* -*-c-*- |
d03ab969 | 2 | * |
3 | * The RIPEMD-160 message digest function | |
4 | * | |
5 | * (c) 1998 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
d03ab969 | 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 | * |
d03ab969 | 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 | * |
d03ab969 | 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 | ||
d03ab969 | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
30 | #include <mLib/bits.h> | |
31 | ||
dc2f0497 | 32 | #include "ghash.h" |
33 | #include "ghash-def.h" | |
d03ab969 | 34 | #include "hash.h" |
35 | #include "rmd160.h" | |
36 | ||
37 | /*----- Main code ---------------------------------------------------------*/ | |
38 | ||
39 | /* --- @rmd160_compress@ --- * | |
40 | * | |
41 | * Arguments: @rmd160_ctx *ctx@ = pointer to context block | |
42 | * @const void *sbuf@ = pointer to buffer of appropriate size | |
43 | * | |
44 | * Returns: --- | |
45 | * | |
46 | * Use: RIPEMD-160 compression function. | |
47 | */ | |
48 | ||
49 | void rmd160_compress(rmd160_ctx *ctx, const void *sbuf) | |
50 | { | |
51 | uint32 a, b, c, d, e; | |
52 | uint32 A, B, C, D, E; | |
53 | uint32 buf[16]; | |
54 | ||
55 | /* --- Fetch the chaining variables --- */ | |
56 | ||
57 | a = A = ctx->a; | |
58 | b = B = ctx->b; | |
59 | c = C = ctx->c; | |
60 | d = D = ctx->d; | |
61 | e = E = ctx->e; | |
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) ^ (z)) | |
76 | #define G(x, y, z) (((x) & (y)) | (~(x) & (z))) | |
77 | #define H(x, y, z) (((x) | ~(y)) ^ (z)) | |
78 | #define I(x, y, z) (((x) & (z)) | ((y) & ~(z))) | |
79 | #define J(x, y, z) ((x) ^ ((y) | ~(z))) | |
80 | ||
81 | #define T(v, w, x, y, z, i, r, f, k) do { \ | |
82 | uint32 _t = v + f(w, x, y) + buf[i] + k; \ | |
83 | v = ROL32(_t, r) + z; x = ROL32(x, 10); \ | |
84 | } while (0) | |
85 | ||
86 | #define F1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, F, 0x00000000) | |
87 | #define G1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, G, 0x5a827999) | |
88 | #define H1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, H, 0x6ed9eba1) | |
89 | #define I1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, I, 0x8f1bbcdc) | |
90 | #define J1(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, J, 0xa953fd4e) | |
91 | ||
92 | #define F2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, J, 0x50a28be6) | |
93 | #define G2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, I, 0x5c4dd124) | |
94 | #define H2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, H, 0x6d703ef3) | |
95 | #define I2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, G, 0x7a6d76e9) | |
96 | #define J2(v, w, x, y, z, i, r) T(v, w, x, y, z, i, r, F, 0x00000000) | |
97 | ||
98 | /* --- First the left hand side --- */ | |
99 | ||
100 | F1(a, b, c, d, e, 0, 11); | |
101 | F1(e, a, b, c, d, 1, 14); | |
102 | F1(d, e, a, b, c, 2, 15); | |
103 | F1(c, d, e, a, b, 3, 12); | |
45c0fd36 MW |
104 | F1(b, c, d, e, a, 4, 5); |
105 | F1(a, b, c, d, e, 5, 8); | |
106 | F1(e, a, b, c, d, 6, 7); | |
107 | F1(d, e, a, b, c, 7, 9); | |
d03ab969 | 108 | F1(c, d, e, a, b, 8, 11); |
109 | F1(b, c, d, e, a, 9, 13); | |
110 | F1(a, b, c, d, e, 10, 14); | |
111 | F1(e, a, b, c, d, 11, 15); | |
45c0fd36 MW |
112 | F1(d, e, a, b, c, 12, 6); |
113 | F1(c, d, e, a, b, 13, 7); | |
114 | F1(b, c, d, e, a, 14, 9); | |
115 | F1(a, b, c, d, e, 15, 8); | |
116 | ||
d03ab969 | 117 | G1(e, a, b, c, d, 7, 7); |
118 | G1(d, e, a, b, c, 4, 6); | |
119 | G1(c, d, e, a, b, 13, 8); | |
120 | G1(b, c, d, e, a, 1, 13); | |
121 | G1(a, b, c, d, e, 10, 11); | |
122 | G1(e, a, b, c, d, 6, 9); | |
123 | G1(d, e, a, b, c, 15, 7); | |
124 | G1(c, d, e, a, b, 3, 15); | |
125 | G1(b, c, d, e, a, 12, 7); | |
126 | G1(a, b, c, d, e, 0, 12); | |
127 | G1(e, a, b, c, d, 9, 15); | |
128 | G1(d, e, a, b, c, 5, 9); | |
129 | G1(c, d, e, a, b, 2, 11); | |
130 | G1(b, c, d, e, a, 14, 7); | |
131 | G1(a, b, c, d, e, 11, 13); | |
132 | G1(e, a, b, c, d, 8, 12); | |
45c0fd36 | 133 | |
d03ab969 | 134 | H1(d, e, a, b, c, 3, 11); |
135 | H1(c, d, e, a, b, 10, 13); | |
136 | H1(b, c, d, e, a, 14, 6); | |
137 | H1(a, b, c, d, e, 4, 7); | |
138 | H1(e, a, b, c, d, 9, 14); | |
139 | H1(d, e, a, b, c, 15, 9); | |
140 | H1(c, d, e, a, b, 8, 13); | |
141 | H1(b, c, d, e, a, 1, 15); | |
142 | H1(a, b, c, d, e, 2, 14); | |
143 | H1(e, a, b, c, d, 7, 8); | |
144 | H1(d, e, a, b, c, 0, 13); | |
145 | H1(c, d, e, a, b, 6, 6); | |
146 | H1(b, c, d, e, a, 13, 5); | |
147 | H1(a, b, c, d, e, 11, 12); | |
148 | H1(e, a, b, c, d, 5, 7); | |
149 | H1(d, e, a, b, c, 12, 5); | |
45c0fd36 | 150 | |
d03ab969 | 151 | I1(c, d, e, a, b, 1, 11); |
152 | I1(b, c, d, e, a, 9, 12); | |
153 | I1(a, b, c, d, e, 11, 14); | |
154 | I1(e, a, b, c, d, 10, 15); | |
155 | I1(d, e, a, b, c, 0, 14); | |
156 | I1(c, d, e, a, b, 8, 15); | |
157 | I1(b, c, d, e, a, 12, 9); | |
158 | I1(a, b, c, d, e, 4, 8); | |
159 | I1(e, a, b, c, d, 13, 9); | |
160 | I1(d, e, a, b, c, 3, 14); | |
161 | I1(c, d, e, a, b, 7, 5); | |
162 | I1(b, c, d, e, a, 15, 6); | |
163 | I1(a, b, c, d, e, 14, 8); | |
164 | I1(e, a, b, c, d, 5, 6); | |
165 | I1(d, e, a, b, c, 6, 5); | |
166 | I1(c, d, e, a, b, 2, 12); | |
45c0fd36 | 167 | |
d03ab969 | 168 | J1(b, c, d, e, a, 4, 9); |
169 | J1(a, b, c, d, e, 0, 15); | |
170 | J1(e, a, b, c, d, 5, 5); | |
171 | J1(d, e, a, b, c, 9, 11); | |
172 | J1(c, d, e, a, b, 7, 6); | |
173 | J1(b, c, d, e, a, 12, 8); | |
174 | J1(a, b, c, d, e, 2, 13); | |
175 | J1(e, a, b, c, d, 10, 12); | |
176 | J1(d, e, a, b, c, 14, 5); | |
177 | J1(c, d, e, a, b, 1, 12); | |
178 | J1(b, c, d, e, a, 3, 13); | |
179 | J1(a, b, c, d, e, 8, 14); | |
180 | J1(e, a, b, c, d, 11, 11); | |
181 | J1(d, e, a, b, c, 6, 8); | |
182 | J1(c, d, e, a, b, 15, 5); | |
183 | J1(b, c, d, e, a, 13, 6); | |
184 | ||
185 | /* --- And then the right hand side --- */ | |
186 | ||
187 | F2(A, B, C, D, E, 5, 8); | |
188 | F2(E, A, B, C, D, 14, 9); | |
189 | F2(D, E, A, B, C, 7, 9); | |
190 | F2(C, D, E, A, B, 0, 11); | |
191 | F2(B, C, D, E, A, 9, 13); | |
192 | F2(A, B, C, D, E, 2, 15); | |
193 | F2(E, A, B, C, D, 11, 15); | |
194 | F2(D, E, A, B, C, 4, 5); | |
195 | F2(C, D, E, A, B, 13, 7); | |
196 | F2(B, C, D, E, A, 6, 7); | |
197 | F2(A, B, C, D, E, 15, 8); | |
198 | F2(E, A, B, C, D, 8, 11); | |
199 | F2(D, E, A, B, C, 1, 14); | |
200 | F2(C, D, E, A, B, 10, 14); | |
201 | F2(B, C, D, E, A, 3, 12); | |
202 | F2(A, B, C, D, E, 12, 6); | |
45c0fd36 | 203 | |
d03ab969 | 204 | G2(E, A, B, C, D, 6, 9); |
205 | G2(D, E, A, B, C, 11, 13); | |
206 | G2(C, D, E, A, B, 3, 15); | |
207 | G2(B, C, D, E, A, 7, 7); | |
208 | G2(A, B, C, D, E, 0, 12); | |
209 | G2(E, A, B, C, D, 13, 8); | |
210 | G2(D, E, A, B, C, 5, 9); | |
211 | G2(C, D, E, A, B, 10, 11); | |
212 | G2(B, C, D, E, A, 14, 7); | |
213 | G2(A, B, C, D, E, 15, 7); | |
214 | G2(E, A, B, C, D, 8, 12); | |
215 | G2(D, E, A, B, C, 12, 7); | |
216 | G2(C, D, E, A, B, 4, 6); | |
217 | G2(B, C, D, E, A, 9, 15); | |
218 | G2(A, B, C, D, E, 1, 13); | |
219 | G2(E, A, B, C, D, 2, 11); | |
45c0fd36 | 220 | |
d03ab969 | 221 | H2(D, E, A, B, C, 15, 9); |
222 | H2(C, D, E, A, B, 5, 7); | |
223 | H2(B, C, D, E, A, 1, 15); | |
224 | H2(A, B, C, D, E, 3, 11); | |
225 | H2(E, A, B, C, D, 7, 8); | |
226 | H2(D, E, A, B, C, 14, 6); | |
227 | H2(C, D, E, A, B, 6, 6); | |
228 | H2(B, C, D, E, A, 9, 14); | |
229 | H2(A, B, C, D, E, 11, 12); | |
230 | H2(E, A, B, C, D, 8, 13); | |
231 | H2(D, E, A, B, C, 12, 5); | |
232 | H2(C, D, E, A, B, 2, 14); | |
233 | H2(B, C, D, E, A, 10, 13); | |
234 | H2(A, B, C, D, E, 0, 13); | |
235 | H2(E, A, B, C, D, 4, 7); | |
236 | H2(D, E, A, B, C, 13, 5); | |
45c0fd36 | 237 | |
d03ab969 | 238 | I2(C, D, E, A, B, 8, 15); |
239 | I2(B, C, D, E, A, 6, 5); | |
240 | I2(A, B, C, D, E, 4, 8); | |
241 | I2(E, A, B, C, D, 1, 11); | |
242 | I2(D, E, A, B, C, 3, 14); | |
243 | I2(C, D, E, A, B, 11, 14); | |
244 | I2(B, C, D, E, A, 15, 6); | |
245 | I2(A, B, C, D, E, 0, 14); | |
246 | I2(E, A, B, C, D, 5, 6); | |
247 | I2(D, E, A, B, C, 12, 9); | |
248 | I2(C, D, E, A, B, 2, 12); | |
249 | I2(B, C, D, E, A, 13, 9); | |
250 | I2(A, B, C, D, E, 9, 12); | |
251 | I2(E, A, B, C, D, 7, 5); | |
252 | I2(D, E, A, B, C, 10, 15); | |
253 | I2(C, D, E, A, B, 14, 8); | |
45c0fd36 | 254 | |
d03ab969 | 255 | J2(B, C, D, E, A, 12, 8); |
256 | J2(A, B, C, D, E, 15, 5); | |
257 | J2(E, A, B, C, D, 10, 12); | |
258 | J2(D, E, A, B, C, 4, 9); | |
259 | J2(C, D, E, A, B, 1, 12); | |
260 | J2(B, C, D, E, A, 5, 5); | |
261 | J2(A, B, C, D, E, 8, 14); | |
262 | J2(E, A, B, C, D, 7, 6); | |
263 | J2(D, E, A, B, C, 6, 8); | |
264 | J2(C, D, E, A, B, 2, 13); | |
265 | J2(B, C, D, E, A, 13, 6); | |
266 | J2(A, B, C, D, E, 14, 5); | |
267 | J2(E, A, B, C, D, 0, 15); | |
268 | J2(D, E, A, B, C, 3, 13); | |
269 | J2(C, D, E, A, B, 9, 11); | |
270 | J2(B, C, D, E, A, 11, 11); | |
271 | ||
272 | /* --- Recombine the two halves --- */ | |
273 | ||
274 | { | |
275 | uint32 | |
276 | tmp = ctx->b + c + D; | |
277 | ctx->b = ctx->c + d + E; | |
278 | ctx->c = ctx->d + e + A; | |
279 | ctx->d = ctx->e + a + B; | |
280 | ctx->e = ctx->a + b + C; | |
281 | ctx->a = tmp; | |
282 | } | |
283 | } | |
284 | ||
285 | /* --- @rmd160_init@ --- * | |
286 | * | |
287 | * Arguments: @rmd160_ctx *ctx@ = pointer to context block to initialize | |
288 | * | |
289 | * Returns: --- | |
290 | * | |
291 | * Use: Initializes a context block ready for hashing. | |
292 | */ | |
293 | ||
294 | void rmd160_init(rmd160_ctx *ctx) | |
295 | { | |
296 | ctx->a = 0x67452301; | |
297 | ctx->b = 0xefcdab89; | |
298 | ctx->c = 0x98badcfe; | |
299 | ctx->d = 0x10325476; | |
300 | ctx->e = 0xc3d2e1f0; | |
301 | ctx->off = 0; | |
dc2f0497 | 302 | ctx->nl = ctx->nh = 0; |
d03ab969 | 303 | } |
304 | ||
305 | /* --- @rmd160_set@ --- * | |
306 | * | |
307 | * Arguments: @rmd160_ctx *ctx@ = pointer to context block | |
308 | * @const void *buf@ = pointer to state buffer | |
309 | * @unsigned long count@ = current count of bytes processed | |
310 | * | |
311 | * Returns: --- | |
312 | * | |
313 | * Use: Initializes a context block from a given state. This is | |
314 | * useful in cases where the initial hash state is meant to be | |
315 | * secret, e.g., for NMAC and HMAC support. | |
316 | */ | |
317 | ||
318 | void rmd160_set(rmd160_ctx *ctx, const void *buf, unsigned long count) | |
319 | { | |
320 | const octet *p = buf; | |
45c0fd36 MW |
321 | ctx->a = LOAD32_L(p + 0); |
322 | ctx->b = LOAD32_L(p + 4); | |
323 | ctx->c = LOAD32_L(p + 8); | |
d03ab969 | 324 | ctx->d = LOAD32_L(p + 12); |
325 | ctx->e = LOAD32_L(p + 16); | |
326 | ctx->off = 0; | |
dc2f0497 | 327 | ctx->nl = U32(count); |
e91d142c | 328 | ctx->nh = U32(((count & ~(unsigned long)MASK32) >> 16) >> 16); |
d03ab969 | 329 | } |
330 | ||
331 | /* --- @rmd160_hash@ --- * | |
332 | * | |
333 | * Arguments: @rmd160_ctx *ctx@ = pointer to context block | |
334 | * @const void *buf@ = buffer of data to hash | |
335 | * @size_t sz@ = size of buffer to hash | |
336 | * | |
337 | * Returns: --- | |
338 | * | |
339 | * Use: Hashes a buffer of data. The buffer may be of any size and | |
340 | * alignment. | |
341 | */ | |
342 | ||
343 | void rmd160_hash(rmd160_ctx *ctx, const void *buf, size_t sz) | |
344 | { | |
345 | HASH_BUFFER(RMD160, rmd160, ctx, buf, sz); | |
346 | } | |
347 | ||
348 | /* --- @rmd160_done@ --- * | |
349 | * | |
350 | * Arguments: @rmd160_ctx *ctx@ = pointer to context block | |
351 | * @void *hash@ = pointer to output buffer | |
352 | * | |
353 | * Returns: --- | |
354 | * | |
355 | * Use: Returns the hash of the data read so far. | |
356 | */ | |
357 | ||
358 | void rmd160_done(rmd160_ctx *ctx, void *hash) | |
359 | { | |
360 | octet *p = hash; | |
361 | HASH_MD5STRENGTH(RMD160, rmd160, ctx); | |
45c0fd36 MW |
362 | STORE32_L(p + 0, ctx->a); |
363 | STORE32_L(p + 4, ctx->b); | |
364 | STORE32_L(p + 8, ctx->c); | |
d03ab969 | 365 | STORE32_L(p + 12, ctx->d); |
366 | STORE32_L(p + 16, ctx->e); | |
367 | } | |
368 | ||
369 | /* --- @rmd160_state@ --- * | |
370 | * | |
371 | * Arguments: @rmd160_ctx *ctx@ = pointer to context | |
372 | * @void *state@ = pointer to buffer for current state | |
373 | * | |
374 | * Returns: Number of bytes written to the hash function so far. | |
375 | * | |
376 | * Use: Returns the current state of the hash function such that | |
377 | * it can be passed to @rmd160_set@. | |
378 | */ | |
379 | ||
380 | unsigned long rmd160_state(rmd160_ctx *ctx, void *state) | |
381 | { | |
382 | octet *p = state; | |
45c0fd36 MW |
383 | STORE32_L(p + 0, ctx->a); |
384 | STORE32_L(p + 4, ctx->b); | |
385 | STORE32_L(p + 8, ctx->c); | |
d03ab969 | 386 | STORE32_L(p + 12, ctx->d); |
387 | STORE32_L(p + 16, ctx->e); | |
dc2f0497 | 388 | return (ctx->nl | ((ctx->nh << 16) << 16)); |
d03ab969 | 389 | } |
390 | ||
dc2f0497 | 391 | /* --- Generic interface --- */ |
392 | ||
393 | GHASH_DEF(RMD160, rmd160) | |
394 | ||
d03ab969 | 395 | /* --- Test code --- */ |
396 | ||
397 | HASH_TEST(RMD160, rmd160) | |
398 | ||
399 | /*----- That's all, folks -------------------------------------------------*/ |