9abecfb6065d4033b21716e1b7f0b8e5d24dc4fa
2 * This is an OpenSSL-compatible implementation of the RSA Data Security,
3 * Inc. MD5 Message-Digest Algorithm.
5 * Written by Solar Designer <solar at openwall.com> in 2001, and placed
6 * in the public domain. There's absolutely no warranty.
8 * This differs from Colin Plumb's older public domain implementation in
9 * that no 32-bit integer data type is required, there's no compile-time
10 * endianness configuration, and the function prototypes match OpenSSL's.
11 * The primary goals are portability and ease of use.
13 * This implementation is meant to be fast, but not as fast as possible.
14 * Some known optimizations are not included to reduce source code size
15 * and avoid compile-time configuration.
25 * The basic MD5 functions.
27 * F is optimized compared to its RFC 1321 definition just like in Colin
28 * Plumb's implementation.
30 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
31 #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
32 #define H(x, y, z) ((x) ^ (y) ^ (z))
33 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
36 * The MD5 transformation for all four rounds.
38 #define STEP(f, a, b, c, d, x, t, s) \
39 (a) += f((b), (c), (d)) + (x) + (t); \
40 (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
44 * SET reads 4 input bytes in little-endian byte order and stores them
45 * in a properly aligned word in host byte order.
47 * The check for little-endian architectures which tolerate unaligned
48 * memory accesses is just an optimization. Nothing will break if it
51 #if defined(__i386__) || defined(__vax__)
53 (*(MD5_u32plus *)&ptr[(n) * 4])
59 (MD5_u32plus)ptr[(n) * 4] | \
60 ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
61 ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
62 ((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
68 * This processes one or more 64-byte data blocks, but does NOT update
69 * the bit counters. There're no alignment requirements.
71 static void *body(MD5_CTX
*ctx
, void *data
, unsigned long size
)
74 MD5_u32plus a
, b
, c
, d
;
75 MD5_u32plus saved_a
, saved_b
, saved_c
, saved_d
;
91 STEP(F
, a
, b
, c
, d
, SET(0), 0xd76aa478, 7)
92 STEP(F
, d
, a
, b
, c
, SET(1), 0xe8c7b756, 12)
93 STEP(F
, c
, d
, a
, b
, SET(2), 0x242070db, 17)
94 STEP(F
, b
, c
, d
, a
, SET(3), 0xc1bdceee, 22)
95 STEP(F
, a
, b
, c
, d
, SET(4), 0xf57c0faf, 7)
96 STEP(F
, d
, a
, b
, c
, SET(5), 0x4787c62a, 12)
97 STEP(F
, c
, d
, a
, b
, SET(6), 0xa8304613, 17)
98 STEP(F
, b
, c
, d
, a
, SET(7), 0xfd469501, 22)
99 STEP(F
, a
, b
, c
, d
, SET(8), 0x698098d8, 7)
100 STEP(F
, d
, a
, b
, c
, SET(9), 0x8b44f7af, 12)
101 STEP(F
, c
, d
, a
, b
, SET(10), 0xffff5bb1, 17)
102 STEP(F
, b
, c
, d
, a
, SET(11), 0x895cd7be, 22)
103 STEP(F
, a
, b
, c
, d
, SET(12), 0x6b901122, 7)
104 STEP(F
, d
, a
, b
, c
, SET(13), 0xfd987193, 12)
105 STEP(F
, c
, d
, a
, b
, SET(14), 0xa679438e, 17)
106 STEP(F
, b
, c
, d
, a
, SET(15), 0x49b40821, 22)
109 STEP(G
, a
, b
, c
, d
, GET(1), 0xf61e2562, 5)
110 STEP(G
, d
, a
, b
, c
, GET(6), 0xc040b340, 9)
111 STEP(G
, c
, d
, a
, b
, GET(11), 0x265e5a51, 14)
112 STEP(G
, b
, c
, d
, a
, GET(0), 0xe9b6c7aa, 20)
113 STEP(G
, a
, b
, c
, d
, GET(5), 0xd62f105d, 5)
114 STEP(G
, d
, a
, b
, c
, GET(10), 0x02441453, 9)
115 STEP(G
, c
, d
, a
, b
, GET(15), 0xd8a1e681, 14)
116 STEP(G
, b
, c
, d
, a
, GET(4), 0xe7d3fbc8, 20)
117 STEP(G
, a
, b
, c
, d
, GET(9), 0x21e1cde6, 5)
118 STEP(G
, d
, a
, b
, c
, GET(14), 0xc33707d6, 9)
119 STEP(G
, c
, d
, a
, b
, GET(3), 0xf4d50d87, 14)
120 STEP(G
, b
, c
, d
, a
, GET(8), 0x455a14ed, 20)
121 STEP(G
, a
, b
, c
, d
, GET(13), 0xa9e3e905, 5)
122 STEP(G
, d
, a
, b
, c
, GET(2), 0xfcefa3f8, 9)
123 STEP(G
, c
, d
, a
, b
, GET(7), 0x676f02d9, 14)
124 STEP(G
, b
, c
, d
, a
, GET(12), 0x8d2a4c8a, 20)
127 STEP(H
, a
, b
, c
, d
, GET(5), 0xfffa3942, 4)
128 STEP(H
, d
, a
, b
, c
, GET(8), 0x8771f681, 11)
129 STEP(H
, c
, d
, a
, b
, GET(11), 0x6d9d6122, 16)
130 STEP(H
, b
, c
, d
, a
, GET(14), 0xfde5380c, 23)
131 STEP(H
, a
, b
, c
, d
, GET(1), 0xa4beea44, 4)
132 STEP(H
, d
, a
, b
, c
, GET(4), 0x4bdecfa9, 11)
133 STEP(H
, c
, d
, a
, b
, GET(7), 0xf6bb4b60, 16)
134 STEP(H
, b
, c
, d
, a
, GET(10), 0xbebfbc70, 23)
135 STEP(H
, a
, b
, c
, d
, GET(13), 0x289b7ec6, 4)
136 STEP(H
, d
, a
, b
, c
, GET(0), 0xeaa127fa, 11)
137 STEP(H
, c
, d
, a
, b
, GET(3), 0xd4ef3085, 16)
138 STEP(H
, b
, c
, d
, a
, GET(6), 0x04881d05, 23)
139 STEP(H
, a
, b
, c
, d
, GET(9), 0xd9d4d039, 4)
140 STEP(H
, d
, a
, b
, c
, GET(12), 0xe6db99e5, 11)
141 STEP(H
, c
, d
, a
, b
, GET(15), 0x1fa27cf8, 16)
142 STEP(H
, b
, c
, d
, a
, GET(2), 0xc4ac5665, 23)
145 STEP(I
, a
, b
, c
, d
, GET(0), 0xf4292244, 6)
146 STEP(I
, d
, a
, b
, c
, GET(7), 0x432aff97, 10)
147 STEP(I
, c
, d
, a
, b
, GET(14), 0xab9423a7, 15)
148 STEP(I
, b
, c
, d
, a
, GET(5), 0xfc93a039, 21)
149 STEP(I
, a
, b
, c
, d
, GET(12), 0x655b59c3, 6)
150 STEP(I
, d
, a
, b
, c
, GET(3), 0x8f0ccc92, 10)
151 STEP(I
, c
, d
, a
, b
, GET(10), 0xffeff47d, 15)
152 STEP(I
, b
, c
, d
, a
, GET(1), 0x85845dd1, 21)
153 STEP(I
, a
, b
, c
, d
, GET(8), 0x6fa87e4f, 6)
154 STEP(I
, d
, a
, b
, c
, GET(15), 0xfe2ce6e0, 10)
155 STEP(I
, c
, d
, a
, b
, GET(6), 0xa3014314, 15)
156 STEP(I
, b
, c
, d
, a
, GET(13), 0x4e0811a1, 21)
157 STEP(I
, a
, b
, c
, d
, GET(4), 0xf7537e82, 6)
158 STEP(I
, d
, a
, b
, c
, GET(11), 0xbd3af235, 10)
159 STEP(I
, c
, d
, a
, b
, GET(2), 0x2ad7d2bb, 15)
160 STEP(I
, b
, c
, d
, a
, GET(9), 0xeb86d391, 21)
168 } while (size
-= 64);
178 void MD5_Init(MD5_CTX
*ctx
)
189 void MD5_Update(MD5_CTX
*ctx
, void *data
, unsigned long size
)
191 MD5_u32plus saved_lo
;
192 unsigned long used
, free
;
195 if ((ctx
->lo
= (saved_lo
+ size
) & 0x1fffffff) < saved_lo
)
197 ctx
->hi
+= size
>> 29;
199 used
= saved_lo
& 0x3f;
205 memcpy(&ctx
->buffer
[used
], data
, size
);
209 memcpy(&ctx
->buffer
[used
], data
, free
);
210 data
= (unsigned char *)data
+ free
;
212 body(ctx
, ctx
->buffer
, 64);
216 data
= body(ctx
, data
, size
& ~(unsigned long)0x3f);
220 memcpy(ctx
->buffer
, data
, size
);
223 void MD5_Final(unsigned char *result
, MD5_CTX
*ctx
)
225 unsigned long used
, free
;
227 used
= ctx
->lo
& 0x3f;
229 ctx
->buffer
[used
++] = 0x80;
234 memset(&ctx
->buffer
[used
], 0, free
);
235 body(ctx
, ctx
->buffer
, 64);
240 memset(&ctx
->buffer
[used
], 0, free
- 8);
243 ctx
->buffer
[56] = ctx
->lo
;
244 ctx
->buffer
[57] = ctx
->lo
>> 8;
245 ctx
->buffer
[58] = ctx
->lo
>> 16;
246 ctx
->buffer
[59] = ctx
->lo
>> 24;
247 ctx
->buffer
[60] = ctx
->hi
;
248 ctx
->buffer
[61] = ctx
->hi
>> 8;
249 ctx
->buffer
[62] = ctx
->hi
>> 16;
250 ctx
->buffer
[63] = ctx
->hi
>> 24;
252 body(ctx
, ctx
->buffer
, 64);
255 result
[1] = ctx
->a
>> 8;
256 result
[2] = ctx
->a
>> 16;
257 result
[3] = ctx
->a
>> 24;
259 result
[5] = ctx
->b
>> 8;
260 result
[6] = ctx
->b
>> 16;
261 result
[7] = ctx
->b
>> 24;
263 result
[9] = ctx
->c
>> 8;
264 result
[10] = ctx
->c
>> 16;
265 result
[11] = ctx
->c
>> 24;
267 result
[13] = ctx
->d
>> 8;
268 result
[14] = ctx
->d
>> 16;
269 result
[15] = ctx
->d
>> 24;
271 memset(ctx
, 0, sizeof(*ctx
));