This source file includes following definitions.
- md5_init_ctx
- set_uint32
- md5_read_ctx
- md5_finish_ctx
- md5_buffer
- md5_process_bytes
- md5_process_block
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #include <config.h>
23
24
25 #if HAVE_OPENSSL_MD5
26 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
27 #endif
28 #include "md5.h"
29
30 #include <stdalign.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <sys/types.h>
34
35 #ifdef _LIBC
36 # include <endian.h>
37 # if __BYTE_ORDER == __BIG_ENDIAN
38 # define WORDS_BIGENDIAN 1
39 # endif
40
41
42 # define md5_init_ctx __md5_init_ctx
43 # define md5_process_block __md5_process_block
44 # define md5_process_bytes __md5_process_bytes
45 # define md5_finish_ctx __md5_finish_ctx
46 # define md5_read_ctx __md5_read_ctx
47 # define md5_buffer __md5_buffer
48 #endif
49
50 #include <byteswap.h>
51 #ifdef WORDS_BIGENDIAN
52 # define SWAP(n) bswap_32 (n)
53 #else
54 # define SWAP(n) (n)
55 #endif
56
57 #if ! HAVE_OPENSSL_MD5
58
59
60
61 static const unsigned char fillbuf[64] = { 0x80, 0 };
62
63
64
65
66 void
67 md5_init_ctx (struct md5_ctx *ctx)
68 {
69 ctx->A = 0x67452301;
70 ctx->B = 0xefcdab89;
71 ctx->C = 0x98badcfe;
72 ctx->D = 0x10325476;
73
74 ctx->total[0] = ctx->total[1] = 0;
75 ctx->buflen = 0;
76 }
77
78
79
80
81 static void
82 set_uint32 (char *cp, uint32_t v)
83 {
84 memcpy (cp, &v, sizeof v);
85 }
86
87
88
89 void *
90 md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
91 {
92 char *r = resbuf;
93 set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
94 set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
95 set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
96 set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
97
98 return resbuf;
99 }
100
101
102
103 void *
104 md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
105 {
106
107 uint32_t bytes = ctx->buflen;
108 size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
109
110
111 ctx->total[0] += bytes;
112 if (ctx->total[0] < bytes)
113 ++ctx->total[1];
114
115
116 ctx->buffer[size - 2] = SWAP (ctx->total[0] << 3);
117 ctx->buffer[size - 1] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
118
119 memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
120
121
122 md5_process_block (ctx->buffer, size * 4, ctx);
123
124 return md5_read_ctx (ctx, resbuf);
125 }
126
127
128
129
130
131 void *
132 md5_buffer (const char *buffer, size_t len, void *resblock)
133 {
134 struct md5_ctx ctx;
135
136
137 md5_init_ctx (&ctx);
138
139
140 md5_process_bytes (buffer, len, &ctx);
141
142
143 return md5_finish_ctx (&ctx, resblock);
144 }
145
146
147 void
148 md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
149 {
150
151
152 if (ctx->buflen != 0)
153 {
154 size_t left_over = ctx->buflen;
155 size_t add = 128 - left_over > len ? len : 128 - left_over;
156
157 memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
158 ctx->buflen += add;
159
160 if (ctx->buflen > 64)
161 {
162 md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
163
164 ctx->buflen &= 63;
165
166
167 memcpy (ctx->buffer,
168 &((char *) ctx->buffer)[(left_over + add) & ~63],
169 ctx->buflen);
170 }
171
172 buffer = (const char *) buffer + add;
173 len -= add;
174 }
175
176
177 if (len >= 64)
178 {
179 #if !(_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
180 # define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0)
181 if (UNALIGNED_P (buffer))
182 while (len > 64)
183 {
184 md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
185 buffer = (const char *) buffer + 64;
186 len -= 64;
187 }
188 else
189 #endif
190 {
191 md5_process_block (buffer, len & ~63, ctx);
192 buffer = (const char *) buffer + (len & ~63);
193 len &= 63;
194 }
195 }
196
197
198 if (len > 0)
199 {
200 size_t left_over = ctx->buflen;
201
202 memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
203 left_over += len;
204 if (left_over >= 64)
205 {
206 md5_process_block (ctx->buffer, 64, ctx);
207 left_over -= 64;
208
209
210 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
211 }
212 ctx->buflen = left_over;
213 }
214 }
215
216
217
218
219
220
221 #define FF(b, c, d) (d ^ (b & (c ^ d)))
222 #define FG(b, c, d) FF (d, b, c)
223 #define FH(b, c, d) (b ^ c ^ d)
224 #define FI(b, c, d) (c ^ (b | ~d))
225
226
227
228
229 void
230 md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
231 {
232 uint32_t correct_words[16];
233 const uint32_t *words = buffer;
234 size_t nwords = len / sizeof (uint32_t);
235 const uint32_t *endp = words + nwords;
236 uint32_t A = ctx->A;
237 uint32_t B = ctx->B;
238 uint32_t C = ctx->C;
239 uint32_t D = ctx->D;
240 uint32_t lolen = len;
241
242
243
244
245 ctx->total[0] += lolen;
246 ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
247
248
249
250 while (words < endp)
251 {
252 uint32_t *cwp = correct_words;
253 uint32_t A_save = A;
254 uint32_t B_save = B;
255 uint32_t C_save = C;
256 uint32_t D_save = D;
257
258
259
260
261
262
263
264
265 #define OP(a, b, c, d, s, T) \
266 do \
267 { \
268 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
269 ++words; \
270 CYCLIC (a, s); \
271 a += b; \
272 } \
273 while (0)
274
275
276
277 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
278
279
280
281
282
283
284
285
286
287
288
289
290 OP (A, B, C, D, 7, 0xd76aa478);
291 OP (D, A, B, C, 12, 0xe8c7b756);
292 OP (C, D, A, B, 17, 0x242070db);
293 OP (B, C, D, A, 22, 0xc1bdceee);
294 OP (A, B, C, D, 7, 0xf57c0faf);
295 OP (D, A, B, C, 12, 0x4787c62a);
296 OP (C, D, A, B, 17, 0xa8304613);
297 OP (B, C, D, A, 22, 0xfd469501);
298 OP (A, B, C, D, 7, 0x698098d8);
299 OP (D, A, B, C, 12, 0x8b44f7af);
300 OP (C, D, A, B, 17, 0xffff5bb1);
301 OP (B, C, D, A, 22, 0x895cd7be);
302 OP (A, B, C, D, 7, 0x6b901122);
303 OP (D, A, B, C, 12, 0xfd987193);
304 OP (C, D, A, B, 17, 0xa679438e);
305 OP (B, C, D, A, 22, 0x49b40821);
306
307
308
309
310 #undef OP
311 #define OP(f, a, b, c, d, k, s, T) \
312 do \
313 { \
314 a += f (b, c, d) + correct_words[k] + T; \
315 CYCLIC (a, s); \
316 a += b; \
317 } \
318 while (0)
319
320
321 OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
322 OP (FG, D, A, B, C, 6, 9, 0xc040b340);
323 OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
324 OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
325 OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
326 OP (FG, D, A, B, C, 10, 9, 0x02441453);
327 OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
328 OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
329 OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
330 OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
331 OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
332 OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
333 OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
334 OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
335 OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
336 OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
337
338
339 OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
340 OP (FH, D, A, B, C, 8, 11, 0x8771f681);
341 OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
342 OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
343 OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
344 OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
345 OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
346 OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
347 OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
348 OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
349 OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
350 OP (FH, B, C, D, A, 6, 23, 0x04881d05);
351 OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
352 OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
353 OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
354 OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
355
356
357 OP (FI, A, B, C, D, 0, 6, 0xf4292244);
358 OP (FI, D, A, B, C, 7, 10, 0x432aff97);
359 OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
360 OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
361 OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
362 OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
363 OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
364 OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
365 OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
366 OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
367 OP (FI, C, D, A, B, 6, 15, 0xa3014314);
368 OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
369 OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
370 OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
371 OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
372 OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
373
374
375 A += A_save;
376 B += B_save;
377 C += C_save;
378 D += D_save;
379 }
380
381
382 ctx->A = A;
383 ctx->B = B;
384 ctx->C = C;
385 ctx->D = D;
386 }
387
388 #endif
389
390
391
392
393
394
395