50static void MD5Transform(uint32_t [4],
unsigned char [64]);
51static void Encode(
unsigned char *,uint32_t *,
unsigned int);
52static void Decode(uint32_t *,
unsigned char *,
unsigned int);
54static unsigned char PADDING[64] = {
55 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
61#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
62#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
63#define H(x, y, z) ((x) ^ (y) ^ (z))
64#define I(x, y, z) ((y) ^ ((x) | (~z)))
67#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
72#define FF(a, b, c, d, x, s, ac) { \
73 (a) += F ((b), (c), (d)) + (x) + (ac); \
74 (a) = ROTATE_LEFT ((a), (s)); \
77#define GG(a, b, c, d, x, s, ac) { \
78 (a) += G ((b), (c), (d)) + (x) + (ac); \
79 (a) = ROTATE_LEFT ((a), (s)); \
82#define HH(a, b, c, d, x, s, ac) { \
83 (a) += H ((b), (c), (d)) + (x) + (ac); \
84 (a) = ROTATE_LEFT ((a), (s)); \
87#define II(a, b, c, d, x, s, ac) { \
88 (a) += I ((b), (c), (d)) + (x) + (ac); \
89 (a) = ROTATE_LEFT ((a), (s)); \
99 context->
state[0] = 0x67452301;
100 context->
state[1] = 0xefcdab89;
101 context->
state[2] = 0x98badcfe;
102 context->
state[3] = 0x10325476;
110 uint32_t i, index, partLen;
113 index = (context->
count[0] >> 3) & 0x3F;
116 if ((context->
count[0] += inputLen << 3)
119 context->
count[1] += (inputLen >> 29);
121 partLen = 64 - index;
124 if (inputLen >= partLen) {
125 byte_copy((
char *)&context->
buffer[index],partLen,(
char *)input);
128 for (i = partLen; i + 63 < inputLen; i += 64)
129 MD5Transform (context->
state, &input[i]);
137 byte_copy((
char *)&context->
buffer[index],inputLen - i,(
char *)&input[i]);
145 unsigned char bits[8];
146 uint32_t index, padLen;
149 Encode(bits, context->
count, 8);
152 index = (context->
count[0] >> 3) & 0x3f;
153 padLen = (index < 56) ? (56 - index) : (120 - index);
160 Encode(digest, context->
state, 16);
163 byte_zero((
char *)context,
sizeof(context));
168static void MD5Transform (uint32_t state[4],
unsigned char block[64])
170 uint32_t a = state[0],
b = state[1], c = state[2],
d = state[3], x[16];
175 FF (a,
b, c,
d, x[ 0],
S11, 0xd76aa478);
176 FF (
d, a,
b, c, x[ 1],
S12, 0xe8c7b756);
177 FF (c,
d, a,
b, x[ 2],
S13, 0x242070db);
178 FF (
b, c,
d, a, x[ 3],
S14, 0xc1bdceee);
179 FF (a,
b, c,
d, x[ 4],
S11, 0xf57c0faf);
180 FF (
d, a,
b, c, x[ 5],
S12, 0x4787c62a);
181 FF (c,
d, a,
b, x[ 6],
S13, 0xa8304613);
182 FF (
b, c,
d, a, x[ 7],
S14, 0xfd469501);
183 FF (a,
b, c,
d, x[ 8],
S11, 0x698098d8);
184 FF (
d, a,
b, c, x[ 9],
S12, 0x8b44f7af);
185 FF (c,
d, a,
b, x[10],
S13, 0xffff5bb1);
186 FF (
b, c,
d, a, x[11],
S14, 0x895cd7be);
187 FF (a,
b, c,
d, x[12],
S11, 0x6b901122);
188 FF (
d, a,
b, c, x[13],
S12, 0xfd987193);
189 FF (c,
d, a,
b, x[14],
S13, 0xa679438e);
190 FF (
b, c,
d, a, x[15],
S14, 0x49b40821);
193 GG (a,
b, c,
d, x[ 1],
S21, 0xf61e2562);
194 GG (
d, a,
b, c, x[ 6],
S22, 0xc040b340);
195 GG (c,
d, a,
b, x[11],
S23, 0x265e5a51);
196 GG (
b, c,
d, a, x[ 0],
S24, 0xe9b6c7aa);
197 GG (a,
b, c,
d, x[ 5],
S21, 0xd62f105d);
198 GG (
d, a,
b, c, x[10],
S22, 0x02441453);
199 GG (c,
d, a,
b, x[15],
S23, 0xd8a1e681);
200 GG (
b, c,
d, a, x[ 4],
S24, 0xe7d3fbc8);
201 GG (a,
b, c,
d, x[ 9],
S21, 0x21e1cde6);
202 GG (
d, a,
b, c, x[14],
S22, 0xc33707d6);
203 GG (c,
d, a,
b, x[ 3],
S23, 0xf4d50d87);
204 GG (
b, c,
d, a, x[ 8],
S24, 0x455a14ed);
205 GG (a,
b, c,
d, x[13],
S21, 0xa9e3e905);
206 GG (
d, a,
b, c, x[ 2],
S22, 0xfcefa3f8);
207 GG (c,
d, a,
b, x[ 7],
S23, 0x676f02d9);
208 GG (
b, c,
d, a, x[12],
S24, 0x8d2a4c8a);
211 HH (a,
b, c,
d, x[ 5],
S31, 0xfffa3942);
212 HH (
d, a,
b, c, x[ 8],
S32, 0x8771f681);
213 HH (c,
d, a,
b, x[11],
S33, 0x6d9d6122);
214 HH (
b, c,
d, a, x[14],
S34, 0xfde5380c);
215 HH (a,
b, c,
d, x[ 1],
S31, 0xa4beea44);
216 HH (
d, a,
b, c, x[ 4],
S32, 0x4bdecfa9);
217 HH (c,
d, a,
b, x[ 7],
S33, 0xf6bb4b60);
218 HH (
b, c,
d, a, x[10],
S34, 0xbebfbc70);
219 HH (a,
b, c,
d, x[13],
S31, 0x289b7ec6);
220 HH (
d, a,
b, c, x[ 0],
S32, 0xeaa127fa);
221 HH (c,
d, a,
b, x[ 3],
S33, 0xd4ef3085);
222 HH (
b, c,
d, a, x[ 6],
S34, 0x04881d05);
223 HH (a,
b, c,
d, x[ 9],
S31, 0xd9d4d039);
224 HH (
d, a,
b, c, x[12],
S32, 0xe6db99e5);
225 HH (c,
d, a,
b, x[15],
S33, 0x1fa27cf8);
226 HH (
b, c,
d, a, x[ 2],
S34, 0xc4ac5665);
229 II (a,
b, c,
d, x[ 0],
S41, 0xf4292244);
230 II (
d, a,
b, c, x[ 7],
S42, 0x432aff97);
231 II (c,
d, a,
b, x[14],
S43, 0xab9423a7);
232 II (
b, c,
d, a, x[ 5],
S44, 0xfc93a039);
233 II (a,
b, c,
d, x[12],
S41, 0x655b59c3);
234 II (
d, a,
b, c, x[ 3],
S42, 0x8f0ccc92);
235 II (c,
d, a,
b, x[10],
S43, 0xffeff47d);
236 II (
b, c,
d, a, x[ 1],
S44, 0x85845dd1);
237 II (a,
b, c,
d, x[ 8],
S41, 0x6fa87e4f);
238 II (
d, a,
b, c, x[15],
S42, 0xfe2ce6e0);
239 II (c,
d, a,
b, x[ 6],
S43, 0xa3014314);
240 II (
b, c,
d, a, x[13],
S44, 0x4e0811a1);
241 II (a,
b, c,
d, x[ 4],
S41, 0xf7537e82);
242 II (
d, a,
b, c, x[11],
S42, 0xbd3af235);
243 II (c,
d, a,
b, x[ 2],
S43, 0x2ad7d2bb);
244 II (
b, c,
d, a, x[ 9],
S44, 0xeb86d391);
252 byte_zero((
char *)x,
sizeof(x));
258static void Encode(
unsigned char *output,uint32_t *input,
unsigned int len)
262 for (i = 0,
j = 0;
j < len; i++,
j += 4) {
263 output[
j] = (
unsigned char) (input[i] & 0xff);
264 output[
j+1] = (
unsigned char) ((input[i] >> 8) & 0xff);
265 output[
j+2] = (
unsigned char) ((input[i] >> 16) & 0xff);
266 output[
j+3] = (
unsigned char) ((input[i] >> 24) & 0xff);
273static void Decode(uint32_t *output,
unsigned char *input,
unsigned int len)
277 for (i = 0,
j = 0;
j < len; i++,
j += 4)
278 output[i] = ((uint32_t)input[
j]) | (((uint32_t)input[
j+1]) << 8) |
279 (((uint32_t)input[
j+2]) << 16) | (((uint32_t)input[
j+3]) << 24);
void MD5Init(MD5_CTX *context)
#define FF(a, b, c, d, x, s, ac)
#define GG(a, b, c, d, x, s, ac)
void MD5Update(MD5_CTX *context, unsigned char *input, uint32_t inputLen)
#define HH(a, b, c, d, x, s, ac)
void MD5Final(unsigned char digest[16], MD5_CTX *context)
#define II(a, b, c, d, x, s, ac)