math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / symm / md5.c
1 /* -*-c-*-
2 *
3 * The MD5 message digest function
4 *
5 * (c) 1998 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16 *
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.
21 *
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
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 "md5.h"
36
37 /*----- Main code ---------------------------------------------------------*/
38
39 /* --- @md5_compress@ --- *
40 *
41 * Arguments: @md5_ctx *ctx@ = pointer to context block
42 * @const void *sbuf@ = pointer to buffer of appropriate size
43 *
44 * Returns: ---
45 *
46 * Use: MD5 compression function.
47 */
48
49 void md5_compress(md5_ctx *ctx, const void *sbuf)
50 {
51 uint32 a, b, c, d;
52 uint32 buf[16];
53
54 /* --- Fetch the chaining variables --- */
55
56 a = ctx->a;
57 b = ctx->b;
58 c = ctx->c;
59 d = ctx->d;
60
61 /* --- Fetch the buffer contents --- */
62
63 {
64 int i;
65 const octet *p;
66
67 for (i = 0, p = sbuf; i < 16; i++, p += 4)
68 buf[i] = LOAD32_L(p);
69 }
70
71 /* --- Definitions for round functions --- */
72
73 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
74 #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
75 #define H(x, y, z) ((x) ^ (y) ^ (z))
76 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
77
78 #define T(w, x, y, z, i, r, k, f) do { \
79 uint32 _t = w + f(x, y, z) + buf[i] + k; \
80 w = ROL32(_t, r) + x; \
81 } while (0)
82
83 #define FF(w, x, y, z, i, r, k) T(w, x, y, z, i, r, k, F)
84 #define GG(w, x, y, z, i, r, k) T(w, x, y, z, i, r, k, G)
85 #define HH(w, x, y, z, i, r, k) T(w, x, y, z, i, r, k, H)
86 #define II(w, x, y, z, i, r, k) T(w, x, y, z, i, r, k, I)
87
88 /* --- The main compression function --- */
89
90 FF(a, b, c, d, 0, 7, 0xd76aa478);
91 FF(d, a, b, c, 1, 12, 0xe8c7b756);
92 FF(c, d, a, b, 2, 17, 0x242070db);
93 FF(b, c, d, a, 3, 22, 0xc1bdceee);
94 FF(a, b, c, d, 4, 7, 0xf57c0faf);
95 FF(d, a, b, c, 5, 12, 0x4787c62a);
96 FF(c, d, a, b, 6, 17, 0xa8304613);
97 FF(b, c, d, a, 7, 22, 0xfd469501);
98 FF(a, b, c, d, 8, 7, 0x698098d8);
99 FF(d, a, b, c, 9, 12, 0x8b44f7af);
100 FF(c, d, a, b, 10, 17, 0xffff5bb1);
101 FF(b, c, d, a, 11, 22, 0x895cd7be);
102 FF(a, b, c, d, 12, 7, 0x6b901122);
103 FF(d, a, b, c, 13, 12, 0xfd987193);
104 FF(c, d, a, b, 14, 17, 0xa679438e);
105 FF(b, c, d, a, 15, 22, 0x49b40821);
106
107 GG(a, b, c, d, 1, 5, 0xf61e2562);
108 GG(d, a, b, c, 6, 9, 0xc040b340);
109 GG(c, d, a, b, 11, 14, 0x265e5a51);
110 GG(b, c, d, a, 0, 20, 0xe9b6c7aa);
111 GG(a, b, c, d, 5, 5, 0xd62f105d);
112 GG(d, a, b, c, 10, 9, 0x02441453);
113 GG(c, d, a, b, 15, 14, 0xd8a1e681);
114 GG(b, c, d, a, 4, 20, 0xe7d3fbc8);
115 GG(a, b, c, d, 9, 5, 0x21e1cde6);
116 GG(d, a, b, c, 14, 9, 0xc33707d6);
117 GG(c, d, a, b, 3, 14, 0xf4d50d87);
118 GG(b, c, d, a, 8, 20, 0x455a14ed);
119 GG(a, b, c, d, 13, 5, 0xa9e3e905);
120 GG(d, a, b, c, 2, 9, 0xfcefa3f8);
121 GG(c, d, a, b, 7, 14, 0x676f02d9);
122 GG(b, c, d, a, 12, 20, 0x8d2a4c8a);
123
124 HH(a, b, c, d, 5, 4, 0xfffa3942);
125 HH(d, a, b, c, 8, 11, 0x8771f681);
126 HH(c, d, a, b, 11, 16, 0x6d9d6122);
127 HH(b, c, d, a, 14, 23, 0xfde5380c);
128 HH(a, b, c, d, 1, 4, 0xa4beea44);
129 HH(d, a, b, c, 4, 11, 0x4bdecfa9);
130 HH(c, d, a, b, 7, 16, 0xf6bb4b60);
131 HH(b, c, d, a, 10, 23, 0xbebfbc70);
132 HH(a, b, c, d, 13, 4, 0x289b7ec6);
133 HH(d, a, b, c, 0, 11, 0xeaa127fa);
134 HH(c, d, a, b, 3, 16, 0xd4ef3085);
135 HH(b, c, d, a, 6, 23, 0x04881d05);
136 HH(a, b, c, d, 9, 4, 0xd9d4d039);
137 HH(d, a, b, c, 12, 11, 0xe6db99e5);
138 HH(c, d, a, b, 15, 16, 0x1fa27cf8);
139 HH(b, c, d, a, 2, 23, 0xc4ac5665);
140
141 II(a, b, c, d, 0, 6, 0xf4292244);
142 II(d, a, b, c, 7, 10, 0x432aff97);
143 II(c, d, a, b, 14, 15, 0xab9423a7);
144 II(b, c, d, a, 5, 21, 0xfc93a039);
145 II(a, b, c, d, 12, 6, 0x655b59c3);
146 II(d, a, b, c, 3, 10, 0x8f0ccc92);
147 II(c, d, a, b, 10, 15, 0xffeff47d);
148 II(b, c, d, a, 1, 21, 0x85845dd1);
149 II(a, b, c, d, 8, 6, 0x6fa87e4f);
150 II(d, a, b, c, 15, 10, 0xfe2ce6e0);
151 II(c, d, a, b, 6, 15, 0xa3014314);
152 II(b, c, d, a, 13, 21, 0x4e0811a1);
153 II(a, b, c, d, 4, 6, 0xf7537e82);
154 II(d, a, b, c, 11, 10, 0xbd3af235);
155 II(c, d, a, b, 2, 15, 0x2ad7d2bb);
156 II(b, c, d, a, 9, 21, 0xeb86d391);
157
158 /* --- Update the chaining variables --- */
159
160 ctx->a += a;
161 ctx->b += b;
162 ctx->c += c;
163 ctx->d += d;
164 }
165
166 /* --- @md5_init@ --- *
167 *
168 * Arguments: @md5_ctx *ctx@ = pointer to context block to initialize
169 *
170 * Returns: ---
171 *
172 * Use: Initializes a context block ready for hashing.
173 */
174
175 void md5_init(md5_ctx *ctx)
176 {
177 ctx->a = 0x67452301;
178 ctx->b = 0xefcdab89;
179 ctx->c = 0x98badcfe;
180 ctx->d = 0x10325476;
181 ctx->off = 0;
182 ctx->nl = ctx->nh = 0;
183 }
184
185 /* --- @md5_set@ --- *
186 *
187 * Arguments: @md5_ctx *ctx@ = pointer to context block
188 * @const void *buf@ = pointer to state buffer
189 * @unsigned long count@ = current count of bytes processed
190 *
191 * Returns: ---
192 *
193 * Use: Initializes a context block from a given state. This is
194 * useful in cases where the initial hash state is meant to be
195 * secret, e.g., for NMAC and HMAC support.
196 */
197
198 void md5_set(md5_ctx *ctx, const void *buf, unsigned long count)
199 {
200 const octet *p = buf;
201 ctx->a = LOAD32_L(p + 0);
202 ctx->b = LOAD32_L(p + 4);
203 ctx->c = LOAD32_L(p + 8);
204 ctx->d = LOAD32_L(p + 12);
205 ctx->off = 0;
206 ctx->nl = U32(count);
207 ctx->nh = U32(((count & ~MASK32) >> 16) >> 16);
208 }
209
210 /* --- @md5_hash@ --- *
211 *
212 * Arguments: @md5_ctx *ctx@ = pointer to context block
213 * @const void *buf@ = buffer of data to hash
214 * @size_t sz@ = size of buffer to hash
215 *
216 * Returns: ---
217 *
218 * Use: Hashes a buffer of data. The buffer may be of any size and
219 * alignment.
220 */
221
222 void md5_hash(md5_ctx *ctx, const void *buf, size_t sz)
223 {
224 HASH_BUFFER(MD5, md5, ctx, buf, sz);
225 }
226
227 /* --- @md5_done@ --- *
228 *
229 * Arguments: @md5_ctx *ctx@ = pointer to context block
230 * @void *hash@ = pointer to output buffer
231 *
232 * Returns: ---
233 *
234 * Use: Returns the hash of the data read so far.
235 */
236
237 void md5_done(md5_ctx *ctx, void *hash)
238 {
239 octet *p = hash;
240 HASH_MD5STRENGTH(MD5, md5, ctx);
241 STORE32_L(p + 0, ctx->a);
242 STORE32_L(p + 4, ctx->b);
243 STORE32_L(p + 8, ctx->c);
244 STORE32_L(p + 12, ctx->d);
245 }
246
247 /* --- @md5_state@ --- *
248 *
249 * Arguments: @md5_ctx *ctx@ = pointer to context
250 * @void *state@ = pointer to buffer for current state
251 *
252 * Returns: Number of bytes written to the hash function so far.
253 *
254 * Use: Returns the current state of the hash function such that
255 * it can be passed to @md5_set@.
256 */
257
258 unsigned long md5_state(md5_ctx *ctx, void *state)
259 {
260 octet *p = state;
261 STORE32_L(p + 0, ctx->a);
262 STORE32_L(p + 4, ctx->b);
263 STORE32_L(p + 8, ctx->c);
264 STORE32_L(p + 12, ctx->d);
265 return (ctx->nl | ((ctx->nh << 16) << 16));
266 }
267
268 /* --- Generic interface --- */
269
270 GHASH_DEF(MD5, md5)
271
272 /* --- Test code --- */
273
274 HASH_TEST(MD5, md5)
275
276 /*----- That's all, folks -------------------------------------------------*/