1 /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
\r
4 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
\r
7 License to copy and use this software is granted provided that it
\r
8 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
\r
9 Algorithm" in all material mentioning or referencing this software
\r
12 License is also granted to make and use derivative works provided
\r
13 that such works are identified as "derived from the RSA Data
\r
14 Security, Inc. MD5 Message-Digest Algorithm" in all material
\r
15 mentioning or referencing the derived work.
\r
17 RSA Data Security, Inc. makes no representations concerning either
\r
18 the merchantability of this software or the suitability of this
\r
19 software for any particular purpose. It is provided "as is"
\r
20 without express or implied warranty of any kind.
\r
22 These notices must be retained in any copies of any part of this
\r
23 documentation and/or software.
\r
28 /* Constants for MD5Transform routine.
\r
49 static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
\r
50 static void Encode PROTO_LIST
\r
51 ((unsigned char *, UINT4 *, unsigned int));
\r
52 static void Decode PROTO_LIST
\r
53 ((UINT4 *, unsigned char *, unsigned int));
\r
54 static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
\r
55 static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
\r
57 static unsigned char PADDING[64] = {
\r
58 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
\r
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
\r
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
\r
63 /* F, G, H and I are basic MD5 functions.
\r
65 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
\r
66 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
\r
67 #define H(x, y, z) ((x) ^ (y) ^ (z))
\r
68 #define I(x, y, z) ((y) ^ ((x) | (~z)))
\r
70 /* ROTATE_LEFT rotates x left n bits.
\r
72 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
\r
74 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
\r
75 Rotation is separate from addition to prevent recomputation.
\r
77 #define FF(a, b, c, d, x, s, ac) { \
\r
78 (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
\r
79 (a) = ROTATE_LEFT ((a), (s)); \
\r
82 #define GG(a, b, c, d, x, s, ac) { \
\r
83 (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
\r
84 (a) = ROTATE_LEFT ((a), (s)); \
\r
87 #define HH(a, b, c, d, x, s, ac) { \
\r
88 (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
\r
89 (a) = ROTATE_LEFT ((a), (s)); \
\r
92 #define II(a, b, c, d, x, s, ac) { \
\r
93 (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
\r
94 (a) = ROTATE_LEFT ((a), (s)); \
\r
98 /* MD5 initialization. Begins an MD5 operation, writing a new context.
\r
100 void MD5Init (context)
\r
101 MD5_CTX *context; /* context */
\r
103 context->count[0] = context->count[1] = 0;
\r
104 /* Load magic initialization constants.
\r
106 context->state[0] = 0x67452301;
\r
107 context->state[1] = 0xefcdab89;
\r
108 context->state[2] = 0x98badcfe;
\r
109 context->state[3] = 0x10325476;
\r
112 /* MD5 block update operation. Continues an MD5 message-digest
\r
113 operation, processing another message block, and updating the
\r
116 void MD5Update (context, input, inputLen)
\r
117 MD5_CTX *context; /* context */
\r
118 unsigned char *input; /* input block */
\r
119 unsigned int inputLen; /* length of input block */
\r
121 unsigned int i, index, partLen;
\r
123 /* Compute number of bytes mod 64 */
\r
124 index = (unsigned int)((context->count[0] >> 3) & 0x3F);
\r
126 /* Update number of bits */
\r
127 if ((context->count[0] += ((UINT4)inputLen << 3))
\r
128 < ((UINT4)inputLen << 3))
\r
129 context->count[1]++;
\r
130 context->count[1] += ((UINT4)inputLen >> 29);
\r
132 partLen = 64 - index;
\r
134 /* Transform as many times as possible.
\r
136 if (inputLen >= partLen) {
\r
138 ((POINTER)&context->buffer[index], (POINTER)input, partLen);
\r
139 MD5Transform (context->state, context->buffer);
\r
141 for (i = partLen; i + 63 < inputLen; i += 64)
\r
142 MD5Transform (context->state, &input[i]);
\r
149 /* Buffer remaining input */
\r
151 ((POINTER)&context->buffer[index], (POINTER)&input[i],
\r
155 /* MD5 finalization. Ends an MD5 message-digest operation, writing the
\r
156 the message digest and zeroizing the context.
\r
158 void MD5Final (digest, context)
\r
159 unsigned char digest[16]; /* message digest */
\r
160 MD5_CTX *context; /* context */
\r
162 unsigned char bits[8];
\r
163 unsigned int index, padLen;
\r
165 /* Save number of bits */
\r
166 Encode (bits, context->count, 8);
\r
168 /* Pad out to 56 mod 64.
\r
170 index = (unsigned int)((context->count[0] >> 3) & 0x3f);
\r
171 padLen = (index < 56) ? (56 - index) : (120 - index);
\r
172 MD5Update (context, PADDING, padLen);
\r
174 /* Append length (before padding) */
\r
175 MD5Update (context, bits, 8);
\r
177 /* Store state in digest */
\r
178 Encode (digest, context->state, 16);
\r
180 /* Zeroize sensitive information.
\r
182 MD5_memset ((POINTER)context, 0, sizeof (*context));
\r
185 /* MD5 basic transformation. Transforms state based on block.
\r
187 static void MD5Transform (state, block)
\r
189 unsigned char block[64];
\r
191 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
\r
193 Decode (x, block, 64);
\r
196 FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
\r
197 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
\r
198 FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
\r
199 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
\r
200 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
\r
201 FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
\r
202 FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
\r
203 FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
\r
204 FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
\r
205 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
\r
206 FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
\r
207 FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
\r
208 FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
\r
209 FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
\r
210 FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
\r
211 FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
\r
214 GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
\r
215 GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
\r
216 GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
\r
217 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
\r
218 GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
\r
219 GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
\r
220 GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
\r
221 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
\r
222 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
\r
223 GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
\r
224 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
\r
225 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
\r
226 GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
\r
227 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
\r
228 GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
\r
229 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
\r
232 HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
\r
233 HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
\r
234 HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
\r
235 HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
\r
236 HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
\r
237 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
\r
238 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
\r
239 HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
\r
240 HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
\r
241 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
\r
242 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
\r
243 HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
\r
244 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
\r
245 HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
\r
246 HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
\r
247 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
\r
250 II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
\r
251 II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
\r
252 II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
\r
253 II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
\r
254 II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
\r
255 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
\r
256 II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
\r
257 II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
\r
258 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
\r
259 II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
\r
260 II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
\r
261 II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
\r
262 II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
\r
263 II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
\r
264 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
\r
265 II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
\r
272 /* Zeroize sensitive information.
\r
274 MD5_memset ((POINTER)x, 0, sizeof (x));
\r
277 /* Encodes input (UINT4) into output (unsigned char). Assumes len is
\r
280 static void Encode (output, input, len)
\r
281 unsigned char *output;
\r
287 for (i = 0, j = 0; j < len; i++, j += 4) {
\r
288 output[j] = (unsigned char)(input[i] & 0xff);
\r
289 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
\r
290 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
\r
291 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
\r
295 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
\r
298 static void Decode (output, input, len)
\r
300 unsigned char *input;
\r
305 for (i = 0, j = 0; j < len; i++, j += 4)
\r
306 output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
\r
307 (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
\r
310 /* Note: Replace "for loop" with standard memcpy if possible.
\r
313 static void MD5_memcpy (output, input, len)
\r
320 for (i = 0; i < len; i++)
\r
321 output[i] = input[i];
\r
324 /* Note: Replace "for loop" with standard memset if possible.
\r
326 static void MD5_memset (output, value, len)
\r
333 for (i = 0; i < len; i++)
\r
334 ((char *)output)[i] = (char)value;
\r