1 | |
---|
2 | |
---|
3 | /* |
---|
4 | *********************************************************************** |
---|
5 | ** md5.c -- the source code for MD5 routines ** |
---|
6 | ** RSA Data Security, Inc. MD5 Message-Digest Algorithm ** |
---|
7 | ** Created: 2/17/90 RLR ** |
---|
8 | ** Revised: 1/91 SRD,AJ,BSK,JT Reference C ver., 7/10 constant corr. ** |
---|
9 | *********************************************************************** |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | *********************************************************************** |
---|
14 | ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. ** |
---|
15 | ** ** |
---|
16 | ** License to copy and use this software is granted provided that ** |
---|
17 | ** it is identified as the "RSA Data Security, Inc. MD5 Message- ** |
---|
18 | ** Digest Algorithm" in all material mentioning or referencing this ** |
---|
19 | ** software or this function. ** |
---|
20 | ** ** |
---|
21 | ** License is also granted to make and use derivative works ** |
---|
22 | ** provided that such works are identified as "derived from the RSA ** |
---|
23 | ** Data Security, Inc. MD5 Message-Digest Algorithm" in all ** |
---|
24 | ** material mentioning or referencing the derived work. ** |
---|
25 | ** ** |
---|
26 | ** RSA Data Security, Inc. makes no representations concerning ** |
---|
27 | ** either the merchantability of this software or the suitability ** |
---|
28 | ** of this software for any particular purpose. It is provided "as ** |
---|
29 | ** is" without express or implied warranty of any kind. ** |
---|
30 | ** ** |
---|
31 | ** These notices must be retained in any copies of any part of this ** |
---|
32 | ** documentation and/or software. ** |
---|
33 | *********************************************************************** |
---|
34 | */ |
---|
35 | |
---|
36 | #include <string.h> /* memcpy */ |
---|
37 | |
---|
38 | #include "md5.h" |
---|
39 | |
---|
40 | /* |
---|
41 | *********************************************************************** |
---|
42 | ** Message-digest routines: ** |
---|
43 | ** To form the message digest for a message M ** |
---|
44 | ** (1) Initialize a context buffer mdContext using MD5Init ** |
---|
45 | ** (2) Call MD5Update on mdContext and M ** |
---|
46 | ** (3) Call MD5Final on mdContext ** |
---|
47 | ** The message digest is now in mdContext->digest[0...15] ** |
---|
48 | *********************************************************************** |
---|
49 | */ |
---|
50 | |
---|
51 | /* forward declaration */ |
---|
52 | static void Transform (); |
---|
53 | |
---|
54 | static unsigned char PADDING[64] = { |
---|
55 | 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
---|
56 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
---|
57 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
---|
58 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
---|
59 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
---|
60 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
---|
61 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
---|
62 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
---|
63 | }; |
---|
64 | |
---|
65 | /* F, G, H and I are basic MD5 functions */ |
---|
66 | #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) |
---|
67 | #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) |
---|
68 | #define H(x, y, z) ((x) ^ (y) ^ (z)) |
---|
69 | #define I(x, y, z) ((y) ^ ((x) | (~z))) |
---|
70 | |
---|
71 | /* ROTATE_LEFT rotates x left n bits */ |
---|
72 | #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) |
---|
73 | |
---|
74 | /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */ |
---|
75 | /* Rotation is separate from addition to prevent recomputation */ |
---|
76 | #define FF(a, b, c, d, x, s, ac) \ |
---|
77 | {(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ |
---|
78 | (a) = ROTATE_LEFT ((a), (s)); \ |
---|
79 | (a) += (b); \ |
---|
80 | } |
---|
81 | #define GG(a, b, c, d, x, s, ac) \ |
---|
82 | {(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ |
---|
83 | (a) = ROTATE_LEFT ((a), (s)); \ |
---|
84 | (a) += (b); \ |
---|
85 | } |
---|
86 | #define HH(a, b, c, d, x, s, ac) \ |
---|
87 | {(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ |
---|
88 | (a) = ROTATE_LEFT ((a), (s)); \ |
---|
89 | (a) += (b); \ |
---|
90 | } |
---|
91 | #define II(a, b, c, d, x, s, ac) \ |
---|
92 | {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ |
---|
93 | (a) = ROTATE_LEFT ((a), (s)); \ |
---|
94 | (a) += (b); \ |
---|
95 | } |
---|
96 | |
---|
97 | #ifdef __STDC__ |
---|
98 | #define UL(x) x##U |
---|
99 | #else |
---|
100 | #define UL(x) x |
---|
101 | #endif |
---|
102 | |
---|
103 | /* The routine MD5Init initializes the message-digest context |
---|
104 | mdContext. All fields are set to zero. |
---|
105 | */ |
---|
106 | void MD5Init (mdContext) |
---|
107 | MD5_CTX *mdContext; |
---|
108 | { |
---|
109 | mdContext->i[0] = mdContext->i[1] = (UINT4)0; |
---|
110 | |
---|
111 | /* Load magic initialization constants. |
---|
112 | */ |
---|
113 | mdContext->buf[0] = (UINT4)0x67452301; |
---|
114 | mdContext->buf[1] = (UINT4)0xefcdab89; |
---|
115 | mdContext->buf[2] = (UINT4)0x98badcfe; |
---|
116 | mdContext->buf[3] = (UINT4)0x10325476; |
---|
117 | } |
---|
118 | |
---|
119 | /* The routine MD5Update updates the message-digest context to |
---|
120 | account for the presence of each of the characters inBuf[0..inLen-1] |
---|
121 | in the message whose digest is being computed. |
---|
122 | */ |
---|
123 | void MD5Update (mdContext, inBuf, inLen) |
---|
124 | MD5_CTX *mdContext; |
---|
125 | const unsigned char *inBuf; |
---|
126 | unsigned int inLen; |
---|
127 | { |
---|
128 | UINT4 in[16]; |
---|
129 | int mdi; |
---|
130 | unsigned int i, ii; |
---|
131 | |
---|
132 | /* compute number of bytes mod 64 */ |
---|
133 | mdi = (int)((mdContext->i[0] >> 3) & 0x3F); |
---|
134 | |
---|
135 | /* update number of bits */ |
---|
136 | if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0]) |
---|
137 | mdContext->i[1]++; |
---|
138 | mdContext->i[0] += ((UINT4)inLen << 3); |
---|
139 | mdContext->i[1] += ((UINT4)inLen >> 29); |
---|
140 | |
---|
141 | while (inLen--) { |
---|
142 | /* add new character to buffer, increment mdi */ |
---|
143 | mdContext->in[mdi++] = *inBuf++; |
---|
144 | |
---|
145 | /* transform if necessary */ |
---|
146 | if (mdi == 0x40) { |
---|
147 | for (i = 0, ii = 0; i < 16; i++, ii += 4) |
---|
148 | in[i] = (((UINT4)mdContext->in[ii+3]) << 24) | |
---|
149 | (((UINT4)mdContext->in[ii+2]) << 16) | |
---|
150 | (((UINT4)mdContext->in[ii+1]) << 8) | |
---|
151 | ((UINT4)mdContext->in[ii]); |
---|
152 | Transform (mdContext->buf, in); |
---|
153 | mdi = 0; |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | /* The routine MD5Final terminates the message-digest computation and |
---|
159 | ends with the desired message digest in mdContext->digest[0...15]. |
---|
160 | */ |
---|
161 | void MD5Final (hash, mdContext) |
---|
162 | unsigned char hash[]; |
---|
163 | MD5_CTX *mdContext; |
---|
164 | { |
---|
165 | UINT4 in[16]; |
---|
166 | int mdi; |
---|
167 | unsigned int i, ii; |
---|
168 | unsigned int padLen; |
---|
169 | |
---|
170 | /* save number of bits */ |
---|
171 | in[14] = mdContext->i[0]; |
---|
172 | in[15] = mdContext->i[1]; |
---|
173 | |
---|
174 | /* compute number of bytes mod 64 */ |
---|
175 | mdi = (int)((mdContext->i[0] >> 3) & 0x3F); |
---|
176 | |
---|
177 | /* pad out to 56 mod 64 */ |
---|
178 | padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi); |
---|
179 | MD5Update (mdContext, PADDING, padLen); |
---|
180 | |
---|
181 | /* append length in bits and transform */ |
---|
182 | for (i = 0, ii = 0; i < 14; i++, ii += 4) |
---|
183 | in[i] = (((UINT4)mdContext->in[ii+3]) << 24) | |
---|
184 | (((UINT4)mdContext->in[ii+2]) << 16) | |
---|
185 | (((UINT4)mdContext->in[ii+1]) << 8) | |
---|
186 | ((UINT4)mdContext->in[ii]); |
---|
187 | Transform (mdContext->buf, in); |
---|
188 | |
---|
189 | /* store buffer in digest */ |
---|
190 | for (i = 0, ii = 0; i < 4; i++, ii += 4) { |
---|
191 | mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF); |
---|
192 | mdContext->digest[ii+1] = |
---|
193 | (unsigned char)((mdContext->buf[i] >> 8) & 0xFF); |
---|
194 | mdContext->digest[ii+2] = |
---|
195 | (unsigned char)((mdContext->buf[i] >> 16) & 0xFF); |
---|
196 | mdContext->digest[ii+3] = |
---|
197 | (unsigned char)((mdContext->buf[i] >> 24) & 0xFF); |
---|
198 | } |
---|
199 | memcpy(hash, mdContext->digest, 16); |
---|
200 | } |
---|
201 | |
---|
202 | /* Basic MD5 step. Transforms buf based on in. |
---|
203 | */ |
---|
204 | static void Transform (buf, in) |
---|
205 | UINT4 *buf; |
---|
206 | UINT4 *in; |
---|
207 | { |
---|
208 | UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3]; |
---|
209 | |
---|
210 | /* Round 1 */ |
---|
211 | #define S11 7 |
---|
212 | #define S12 12 |
---|
213 | #define S13 17 |
---|
214 | #define S14 22 |
---|
215 | FF ( a, b, c, d, in[ 0], S11, UL(3614090360)); /* 1 */ |
---|
216 | FF ( d, a, b, c, in[ 1], S12, UL(3905402710)); /* 2 */ |
---|
217 | FF ( c, d, a, b, in[ 2], S13, UL( 606105819)); /* 3 */ |
---|
218 | FF ( b, c, d, a, in[ 3], S14, UL(3250441966)); /* 4 */ |
---|
219 | FF ( a, b, c, d, in[ 4], S11, UL(4118548399)); /* 5 */ |
---|
220 | FF ( d, a, b, c, in[ 5], S12, UL(1200080426)); /* 6 */ |
---|
221 | FF ( c, d, a, b, in[ 6], S13, UL(2821735955)); /* 7 */ |
---|
222 | FF ( b, c, d, a, in[ 7], S14, UL(4249261313)); /* 8 */ |
---|
223 | FF ( a, b, c, d, in[ 8], S11, UL(1770035416)); /* 9 */ |
---|
224 | FF ( d, a, b, c, in[ 9], S12, UL(2336552879)); /* 10 */ |
---|
225 | FF ( c, d, a, b, in[10], S13, UL(4294925233)); /* 11 */ |
---|
226 | FF ( b, c, d, a, in[11], S14, UL(2304563134)); /* 12 */ |
---|
227 | FF ( a, b, c, d, in[12], S11, UL(1804603682)); /* 13 */ |
---|
228 | FF ( d, a, b, c, in[13], S12, UL(4254626195)); /* 14 */ |
---|
229 | FF ( c, d, a, b, in[14], S13, UL(2792965006)); /* 15 */ |
---|
230 | FF ( b, c, d, a, in[15], S14, UL(1236535329)); /* 16 */ |
---|
231 | |
---|
232 | /* Round 2 */ |
---|
233 | #define S21 5 |
---|
234 | #define S22 9 |
---|
235 | #define S23 14 |
---|
236 | #define S24 20 |
---|
237 | GG ( a, b, c, d, in[ 1], S21, UL(4129170786)); /* 17 */ |
---|
238 | GG ( d, a, b, c, in[ 6], S22, UL(3225465664)); /* 18 */ |
---|
239 | GG ( c, d, a, b, in[11], S23, UL( 643717713)); /* 19 */ |
---|
240 | GG ( b, c, d, a, in[ 0], S24, UL(3921069994)); /* 20 */ |
---|
241 | GG ( a, b, c, d, in[ 5], S21, UL(3593408605)); /* 21 */ |
---|
242 | GG ( d, a, b, c, in[10], S22, UL( 38016083)); /* 22 */ |
---|
243 | GG ( c, d, a, b, in[15], S23, UL(3634488961)); /* 23 */ |
---|
244 | GG ( b, c, d, a, in[ 4], S24, UL(3889429448)); /* 24 */ |
---|
245 | GG ( a, b, c, d, in[ 9], S21, UL( 568446438)); /* 25 */ |
---|
246 | GG ( d, a, b, c, in[14], S22, UL(3275163606)); /* 26 */ |
---|
247 | GG ( c, d, a, b, in[ 3], S23, UL(4107603335)); /* 27 */ |
---|
248 | GG ( b, c, d, a, in[ 8], S24, UL(1163531501)); /* 28 */ |
---|
249 | GG ( a, b, c, d, in[13], S21, UL(2850285829)); /* 29 */ |
---|
250 | GG ( d, a, b, c, in[ 2], S22, UL(4243563512)); /* 30 */ |
---|
251 | GG ( c, d, a, b, in[ 7], S23, UL(1735328473)); /* 31 */ |
---|
252 | GG ( b, c, d, a, in[12], S24, UL(2368359562)); /* 32 */ |
---|
253 | |
---|
254 | /* Round 3 */ |
---|
255 | #define S31 4 |
---|
256 | #define S32 11 |
---|
257 | #define S33 16 |
---|
258 | #define S34 23 |
---|
259 | HH ( a, b, c, d, in[ 5], S31, UL(4294588738)); /* 33 */ |
---|
260 | HH ( d, a, b, c, in[ 8], S32, UL(2272392833)); /* 34 */ |
---|
261 | HH ( c, d, a, b, in[11], S33, UL(1839030562)); /* 35 */ |
---|
262 | HH ( b, c, d, a, in[14], S34, UL(4259657740)); /* 36 */ |
---|
263 | HH ( a, b, c, d, in[ 1], S31, UL(2763975236)); /* 37 */ |
---|
264 | HH ( d, a, b, c, in[ 4], S32, UL(1272893353)); /* 38 */ |
---|
265 | HH ( c, d, a, b, in[ 7], S33, UL(4139469664)); /* 39 */ |
---|
266 | HH ( b, c, d, a, in[10], S34, UL(3200236656)); /* 40 */ |
---|
267 | HH ( a, b, c, d, in[13], S31, UL( 681279174)); /* 41 */ |
---|
268 | HH ( d, a, b, c, in[ 0], S32, UL(3936430074)); /* 42 */ |
---|
269 | HH ( c, d, a, b, in[ 3], S33, UL(3572445317)); /* 43 */ |
---|
270 | HH ( b, c, d, a, in[ 6], S34, UL( 76029189)); /* 44 */ |
---|
271 | HH ( a, b, c, d, in[ 9], S31, UL(3654602809)); /* 45 */ |
---|
272 | HH ( d, a, b, c, in[12], S32, UL(3873151461)); /* 46 */ |
---|
273 | HH ( c, d, a, b, in[15], S33, UL( 530742520)); /* 47 */ |
---|
274 | HH ( b, c, d, a, in[ 2], S34, UL(3299628645)); /* 48 */ |
---|
275 | |
---|
276 | /* Round 4 */ |
---|
277 | #define S41 6 |
---|
278 | #define S42 10 |
---|
279 | #define S43 15 |
---|
280 | #define S44 21 |
---|
281 | II ( a, b, c, d, in[ 0], S41, UL(4096336452)); /* 49 */ |
---|
282 | II ( d, a, b, c, in[ 7], S42, UL(1126891415)); /* 50 */ |
---|
283 | II ( c, d, a, b, in[14], S43, UL(2878612391)); /* 51 */ |
---|
284 | II ( b, c, d, a, in[ 5], S44, UL(4237533241)); /* 52 */ |
---|
285 | II ( a, b, c, d, in[12], S41, UL(1700485571)); /* 53 */ |
---|
286 | II ( d, a, b, c, in[ 3], S42, UL(2399980690)); /* 54 */ |
---|
287 | II ( c, d, a, b, in[10], S43, UL(4293915773)); /* 55 */ |
---|
288 | II ( b, c, d, a, in[ 1], S44, UL(2240044497)); /* 56 */ |
---|
289 | II ( a, b, c, d, in[ 8], S41, UL(1873313359)); /* 57 */ |
---|
290 | II ( d, a, b, c, in[15], S42, UL(4264355552)); /* 58 */ |
---|
291 | II ( c, d, a, b, in[ 6], S43, UL(2734768916)); /* 59 */ |
---|
292 | II ( b, c, d, a, in[13], S44, UL(1309151649)); /* 60 */ |
---|
293 | II ( a, b, c, d, in[ 4], S41, UL(4149444226)); /* 61 */ |
---|
294 | II ( d, a, b, c, in[11], S42, UL(3174756917)); /* 62 */ |
---|
295 | II ( c, d, a, b, in[ 2], S43, UL( 718787259)); /* 63 */ |
---|
296 | II ( b, c, d, a, in[ 9], S44, UL(3951481745)); /* 64 */ |
---|
297 | |
---|
298 | buf[0] += a; |
---|
299 | buf[1] += b; |
---|
300 | buf[2] += c; |
---|
301 | buf[3] += d; |
---|
302 | } |
---|
303 | |
---|
304 | /* |
---|
305 | *********************************************************************** |
---|
306 | ** End of md5.c ** |
---|
307 | ******************************** (cut) ******************************** |
---|
308 | */ |
---|