annotate src/md5/md5.c @ 285:bdcc2b42eb0f

Heavily reworked man/masqmail.8 I hope the new version is clearer.
author markus schnalke <meillo@marmaro.de>
date Tue, 07 Dec 2010 17:10:07 -0300 (2010-12-07)
parents 10da50168dab
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
209
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
1 /*
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
2 * This is an OpenSSL-compatible implementation of the RSA Data Security,
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
3 * Inc. MD5 Message-Digest Algorithm (RFC 1321).
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
4 *
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
5 * Written by Solar Designer <solar at openwall.com> in 2001, and placed
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
6 * in the public domain. There's absolutely no warranty.
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
7 *
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
8 * This differs from Colin Plumb's older public domain implementation in
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
9 * that no 32-bit integer data type is required, there's no compile-time
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
10 * endianness configuration, and the function prototypes match OpenSSL's.
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
11 * The primary goals are portability and ease of use.
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
12 *
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
13 * This implementation is meant to be fast, but not as fast as possible.
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
14 * Some known optimizations are not included to reduce source code size
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
15 * and avoid compile-time configuration.
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
16 */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
17
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
18 #ifndef HAVE_OPENSSL
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
19
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
20 #include <string.h>
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
21
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
22 #include "md5.h"
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
23
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
24 /*
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
25 * The basic MD5 functions.
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
26 *
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
27 * F and G are optimized compared to their RFC 1321 definitions for
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
28 * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
29 * implementation.
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
30 */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
31 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
32 #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
33 #define H(x, y, z) ((x) ^ (y) ^ (z))
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
34 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
35
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
36 /*
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
37 * The MD5 transformation for all four rounds.
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
38 */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
39 #define STEP(f, a, b, c, d, x, t, s) \
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
40 (a) += f((b), (c), (d)) + (x) + (t); \
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
41 (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
42 (a) += (b);
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
43
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
44 /*
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
45 * SET reads 4 input bytes in little-endian byte order and stores them
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
46 * in a properly aligned word in host byte order.
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
47 *
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
48 * The check for little-endian architectures that tolerate unaligned
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
49 * memory accesses is just an optimization. Nothing will break if it
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
50 * doesn't work.
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
51 */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
52 #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
53 #define SET(n) \
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
54 (*(MD5_u32plus *)&ptr[(n) * 4])
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
55 #define GET(n) \
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
56 SET(n)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
57 #else
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
58 #define SET(n) \
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
59 (ctx->block[(n)] = \
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
60 (MD5_u32plus)ptr[(n) * 4] | \
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
61 ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
62 ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
63 ((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
64 #define GET(n) \
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
65 (ctx->block[(n)])
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
66 #endif
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
67
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
68 /*
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
69 * This processes one or more 64-byte data blocks, but does NOT update
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
70 * the bit counters. There are no alignment requirements.
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
71 */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
72 static void *body(MD5_CTX *ctx, void *data, unsigned long size)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
73 {
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
74 unsigned char *ptr;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
75 MD5_u32plus a, b, c, d;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
76 MD5_u32plus saved_a, saved_b, saved_c, saved_d;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
77
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
78 ptr = data;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
79
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
80 a = ctx->a;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
81 b = ctx->b;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
82 c = ctx->c;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
83 d = ctx->d;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
84
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
85 do {
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
86 saved_a = a;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
87 saved_b = b;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
88 saved_c = c;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
89 saved_d = d;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
90
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
91 /* Round 1 */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
92 STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
93 STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
94 STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
95 STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
96 STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
97 STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
98 STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
99 STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
100 STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
101 STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
102 STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
103 STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
104 STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
105 STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
106 STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
107 STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
108
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
109 /* Round 2 */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
110 STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
111 STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
112 STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
113 STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
114 STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
115 STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
116 STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
117 STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
118 STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
119 STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
120 STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
121 STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
122 STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
123 STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
124 STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
125 STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
126
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
127 /* Round 3 */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
128 STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
129 STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
130 STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
131 STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
132 STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
133 STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
134 STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
135 STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
136 STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
137 STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
138 STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
139 STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
140 STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
141 STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
142 STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
143 STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
144
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
145 /* Round 4 */
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
146 STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
147 STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
148 STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
149 STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
150 STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
151 STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
152 STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
153 STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
154 STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
155 STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
156 STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
157 STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
158 STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
159 STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
160 STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
161 STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
162
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
163 a += saved_a;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
164 b += saved_b;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
165 c += saved_c;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
166 d += saved_d;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
167
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
168 ptr += 64;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
169 } while (size -= 64);
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
170
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
171 ctx->a = a;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
172 ctx->b = b;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
173 ctx->c = c;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
174 ctx->d = d;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
175
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
176 return ptr;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
177 }
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
178
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
179 void MD5_Init(MD5_CTX *ctx)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
180 {
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
181 ctx->a = 0x67452301;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
182 ctx->b = 0xefcdab89;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
183 ctx->c = 0x98badcfe;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
184 ctx->d = 0x10325476;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
185
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
186 ctx->lo = 0;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
187 ctx->hi = 0;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
188 }
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
189
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
190 void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
191 {
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
192 MD5_u32plus saved_lo;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
193 unsigned long used, free;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
194
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
195 saved_lo = ctx->lo;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
196 if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
197 ctx->hi++;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
198 ctx->hi += size >> 29;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
199
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
200 used = saved_lo & 0x3f;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
201
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
202 if (used) {
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
203 free = 64 - used;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
204
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
205 if (size < free) {
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
206 memcpy(&ctx->buffer[used], data, size);
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
207 return;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
208 }
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
209
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
210 memcpy(&ctx->buffer[used], data, free);
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
211 data = (unsigned char *)data + free;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
212 size -= free;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
213 body(ctx, ctx->buffer, 64);
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
214 }
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
215
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
216 if (size >= 64) {
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
217 data = body(ctx, data, size & ~(unsigned long)0x3f);
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
218 size &= 0x3f;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
219 }
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
220
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
221 memcpy(ctx->buffer, data, size);
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
222 }
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
223
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
224 void MD5_Final(unsigned char *result, MD5_CTX *ctx)
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
225 {
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
226 unsigned long used, free;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
227
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
228 used = ctx->lo & 0x3f;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
229
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
230 ctx->buffer[used++] = 0x80;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
231
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
232 free = 64 - used;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
233
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
234 if (free < 8) {
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
235 memset(&ctx->buffer[used], 0, free);
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
236 body(ctx, ctx->buffer, 64);
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
237 used = 0;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
238 free = 64;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
239 }
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
240
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
241 memset(&ctx->buffer[used], 0, free - 8);
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
242
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
243 ctx->lo <<= 3;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
244 ctx->buffer[56] = ctx->lo;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
245 ctx->buffer[57] = ctx->lo >> 8;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
246 ctx->buffer[58] = ctx->lo >> 16;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
247 ctx->buffer[59] = ctx->lo >> 24;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
248 ctx->buffer[60] = ctx->hi;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
249 ctx->buffer[61] = ctx->hi >> 8;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
250 ctx->buffer[62] = ctx->hi >> 16;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
251 ctx->buffer[63] = ctx->hi >> 24;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
252
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
253 body(ctx, ctx->buffer, 64);
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
254
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
255 result[0] = ctx->a;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
256 result[1] = ctx->a >> 8;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
257 result[2] = ctx->a >> 16;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
258 result[3] = ctx->a >> 24;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
259 result[4] = ctx->b;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
260 result[5] = ctx->b >> 8;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
261 result[6] = ctx->b >> 16;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
262 result[7] = ctx->b >> 24;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
263 result[8] = ctx->c;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
264 result[9] = ctx->c >> 8;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
265 result[10] = ctx->c >> 16;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
266 result[11] = ctx->c >> 24;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
267 result[12] = ctx->d;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
268 result[13] = ctx->d >> 8;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
269 result[14] = ctx->d >> 16;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
270 result[15] = ctx->d >> 24;
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
271
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
272 memset(ctx, 0, sizeof(*ctx));
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
273 }
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
274
10da50168dab replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
275 #endif