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