2 * md5 - RSA Data Security, Inc. MD5 Message-Digest Algorithm
4 * @(#) $Revision: 13.3 $
5 * @(#) $Id: md5.c,v 13.3 2006/08/14 11:25:24 chongo Exp $
6 * @(#) $Source: /usr/local/src/cmd/hash/RCS/md5.c,v $
8 * This file was written by RSA Data Security.
10 * This file was modified by Landon Curt Noll.
12 * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO
13 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MER-
14 * CHANTABILITY AND FITNESS. IN NO EVENT SHALL LANDON CURT
15 * NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
16 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
17 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
18 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
19 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * chongo (was here) /\oo/\
22 * http://www.isthe.com/chongo/index.html
24 * Share and enjoy! :-)
26 * See md5drvr.c for version and modification history.
30 ***********************************************************************
31 ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
33 ** License to copy and use this software is granted provided that **
34 ** it is identified as the "RSA Data Security, Inc. MD5 Message- **
35 ** Digest Algorithm" in all material mentioning or referencing this **
36 ** software or this function. **
38 ** License is also granted to make and use derivative works **
39 ** provided that such works are identified as "derived from the RSA **
40 ** Data Security, Inc. MD5 Message-Digest Algorithm" in all **
41 ** material mentioning or referencing the derived work. **
43 ** RSA Data Security, Inc. makes no representations concerning **
44 ** either the merchantability of this software or the suitability **
45 ** of this software for any particular purpose. It is provided "as **
46 ** is" without express or implied warranty of any kind. **
48 ** These notices must be retained in any copies of any part of this **
49 ** documentation and/or software. **
50 ***********************************************************************
54 ***********************************************************************
55 ** Message-digest routines: **
56 ** To form the message digest for a message M **
57 ** (1) Initialize a context buffer md5Ctx using MD5Init **
58 ** (2) Call MD5Update on md5Ctx and M **
59 ** (3) Call MD5Final on md5Ctx **
60 ** The message digest is now in md5Ctx->digest[0...15] **
61 ***********************************************************************
71 /* almost zero fill */
72 static BYTE PADDING
[MD5_CHUNKSIZE
] = {
73 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
84 * The F, G, H and I are basic MD5 functions. The following
85 * identity saves one boolean operation.
87 * F: (((x) & (y)) | (~(x) & (z))) == ((z) ^ ((x) & ((y) ^ (z))))
88 * G: (((x) & (z)) | ((y) & ~(z))) == ((y) ^ ((z) & ((x) ^ (y))))
90 /* F, G, H and I are basic MD5 functions */
91 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
92 #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
93 #define H(x, y, z) ((x) ^ (y) ^ (z))
94 #define I(x, y, z) ((y) ^ ((x) | (~z)))
96 /* rotate a 32 bit value */
97 #define ROT(X,n) (((X)<<(n)) | ((X)>>(32-(n))))
99 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
100 /* Rotation is separate from addition to prevent recomputation */
105 #define FF(a, b, c, d, x, s, ac) \
106 {(a) += F((b), (c), (d)) + (x) + (ULONG)(ac); \
107 (a) = ROT((a), (s)); \
114 #define GG(a, b, c, d, x, s, ac) \
115 {(a) += G((b), (c), (d)) + (x) + (ULONG)(ac); \
116 (a) = ROT((a), (s)); \
123 #define HH(a, b, c, d, x, s, ac) \
124 {(a) += H((b), (c), (d)) + (x) + (ULONG)(ac); \
125 (a) = ROT((a), (s)); \
132 #define II(a, b, c, d, x, s, ac) \
133 {(a) += I((b), (c), (d)) + (x) + (ULONG)(ac); \
134 (a) = ROT((a), (s)); \
140 * MD5Init - initiale the message-digest context
142 * The routine MD5Init initializes the message-digest context
143 * md5Ctx. All fields are set to zero.
146 MD5Init(MD5_CTX
*md5Ctx
)
148 /* load magic initialization constants */
149 md5Ctx
->buf
[0] = (ULONG
)0x67452301;
150 md5Ctx
->buf
[1] = (ULONG
)0xefcdab89;
151 md5Ctx
->buf
[2] = (ULONG
)0x98badcfe;
152 md5Ctx
->buf
[3] = (ULONG
)0x10325476;
154 /* Initialise bit count */
155 md5Ctx
->countLo
= 0L;
156 md5Ctx
->countHi
= 0L;
157 md5Ctx
->datalen
= 0L;
162 * MD5Update - update message-digest context
164 * The routine MD5Update updates the message-digest context to
165 * account for the presence of each of the characters inBuf[0..inLen-1]
166 * in the message whose digest is being computed.
169 MD5Update(MD5_CTX
*md5Ctx
, BYTE
*inBuf
, UINT count
)
171 ULONG datalen
= md5Ctx
->datalen
;
172 #if BYTE_ORDER == BIG_ENDIAN
173 ULONG in
[MD5_CHUNKWORDS
]; /* aligned buffer */
179 * Catch the case of a non-empty data inBuf
183 /* determine the size we need to copy */
184 ULONG cpylen
= MD5_CHUNKSIZE
- datalen
;
186 /* case: new data will not fill the inBuf */
187 if (cpylen
> count
) {
188 memcpy((char *)(md5Ctx
->in_byte
)+datalen
, (char *)inBuf
, count
);
189 md5Ctx
->datalen
= datalen
+count
;
192 /* case: inBuf will be filled */
194 memcpy((char *)(md5Ctx
->in_byte
)+datalen
, (char *)inBuf
, cpylen
);
195 #if BYTE_ORDER == BIG_ENDIAN
196 /* byte swap data into little endian order */
197 for (cnt
=0; cnt
< MD5_CHUNKWORDS
; ++cnt
) {
198 tmp
= (md5Ctx
->in_ulong
[cnt
] << 16) |
199 (md5Ctx
->in_ulong
[cnt
] >> 16);
200 in
[cnt
] = ((tmp
& 0xFF00FF00L
) >> 8) |
201 ((tmp
& 0x00FF00FFL
) << 8);
203 MD5Transform(md5Ctx
->buf
, in
);
205 MD5Transform(md5Ctx
->buf
, md5Ctx
->in_ulong
);
214 * Process data in MD5_CHUNKSIZE chunks
216 if (count
>= MD5_CHUNKSIZE
) {
217 MD5fullUpdate(md5Ctx
, inBuf
, count
);
218 inBuf
+= (count
/MD5_CHUNKSIZE
)*MD5_CHUNKSIZE
;
219 count
%= MD5_CHUNKSIZE
;
223 * Handle any remaining bytes of data.
224 * This should only happen once on the final lot of data
227 memcpy(md5Ctx
->in_byte
, inBuf
, count
);
229 md5Ctx
->datalen
= count
;
234 * MD5fullUpdate - update message-digest context with a full set of chunks
236 * The routine MD5Update updates the message-digest context to
237 * account for the presence of each of the characters inBuf[0..inLen-1]
238 * in the message whose digest is being computed.
240 * Unlike MD5Update, this routine will assume that the input length is
241 * a mulitple of MD5_CHUNKSIZE bytes long.
244 MD5fullUpdate(MD5_CTX
*md5Ctx
, BYTE
*inBuf
, UINT count
)
246 #if BYTE_ORDER == BIG_ENDIAN
247 ULONG in
[MD5_CHUNKWORDS
]; /* aligned buffer */
253 * Process data in MD5_CHUNKSIZE chunks
255 while (count
>= MD5_CHUNKSIZE
) {
256 #if defined(MUST_ALIGN)
257 memcpy(in
, inBuf
, MD5_CHUNKSIZE
);
258 #if BYTE_ORDER == BIG_ENDIAN
259 /* byte swap data into little endian order */
260 for (cnt
=0; cnt
< MD5_CHUNKWORDS
; ++cnt
) {
261 tmp
= (in
[cnt
] << 16) | (in
[cnt
] >> 16);
262 in
[cnt
] = ((tmp
& 0xFF00FF00L
) >> 8) | ((tmp
& 0x00FF00FFL
) << 8);
265 MD5Transform(md5Ctx
->buf
, in
);
267 #if BYTE_ORDER == LITTLE_ENDIAN
268 MD5Transform(md5Ctx
->buf
, (ULONG
*)inBuf
);
270 /* byte swap data into little endian order */
271 for (cnt
=0; cnt
< MD5_CHUNKWORDS
; ++cnt
) {
272 tmp
= (((ULONG
*)inBuf
)[cnt
] << 16) | (((ULONG
*)inBuf
)[cnt
] >> 16);
273 in
[cnt
] = ((tmp
& 0xFF00FF00L
) >> 8) | ((tmp
& 0x00FF00FFL
) << 8);
275 MD5Transform(md5Ctx
->buf
, in
);
278 inBuf
+= MD5_CHUNKSIZE
;
279 count
-= MD5_CHUNKSIZE
;
285 * MD5Final - terminate the message-digest computation
287 * The routine MD5Final terminates the message-digest computation and
288 * ends with the desired message digest in md5Ctx->digest[0...15].
291 MD5Final(MD5_CTX
*md5Ctx
)
294 int count
= md5Ctx
->datalen
;
295 ULONG lowBitcount
= md5Ctx
->countLo
;
296 ULONG highBitcount
= md5Ctx
->countHi
;
297 #if BYTE_ORDER == BIG_ENDIAN
302 /* pad out to 56 mod MD5_CHUNKSIZE */
303 padLen
= (count
< 56) ? (56 - count
) : (120 - count
);
304 MD5Update(md5Ctx
, PADDING
, padLen
);
306 /* append length in bits and transform */
307 #if BYTE_ORDER == BIG_ENDIAN
308 for (i
= 0; i
< 14; i
++) {
309 tmp
= (md5Ctx
->in_ulong
[i
] << 16) | (md5Ctx
->in_ulong
[i
] >> 16);
310 md5Ctx
->in_ulong
[i
] = ((tmp
& 0xFF00FF00L
) >> 8) |
311 ((tmp
& 0x00FF00FFL
) << 8);
314 md5Ctx
->in_ulong
[MD5_LOW
] = (lowBitcount
<< 3);
315 md5Ctx
->in_ulong
[MD5_HIGH
] = (highBitcount
<< 3) | (lowBitcount
>> 29);
316 MD5Transform(md5Ctx
->buf
, md5Ctx
->in_ulong
);
318 /* store buffer in digest */
319 #if BYTE_ORDER == LITTLE_ENDIAN
320 memcpy((char *)md5Ctx
->digest
, (char *)md5Ctx
->buf
, sizeof(md5Ctx
->digest
));
322 for (i
= 0, ii
= 0; i
< MD5_DIGESTWORDS
; i
++, ii
+= 4) {
323 md5Ctx
->digest
[ii
] = (BYTE
)(md5Ctx
->buf
[i
] & 0xFF);
324 md5Ctx
->digest
[ii
+1] = (BYTE
)((md5Ctx
->buf
[i
] >> 8) & 0xFF);
325 md5Ctx
->digest
[ii
+2] = (BYTE
)((md5Ctx
->buf
[i
] >> 16) & 0xFF);
326 md5Ctx
->digest
[ii
+3] = (BYTE
)((md5Ctx
->buf
[i
] >> 24) & 0xFF);
333 * Basic MD5 step. Transforms buf based on in.
336 MD5Transform(ULONG
*buf
, ULONG
*in
)
338 ULONG a
= buf
[0], b
= buf
[1], c
= buf
[2], d
= buf
[3];
341 FF( a
, b
, c
, d
, in
[ 0], S11
, 3614090360UL); /* 1 */
342 FF( d
, a
, b
, c
, in
[ 1], S12
, 3905402710UL); /* 2 */
343 FF( c
, d
, a
, b
, in
[ 2], S13
, 606105819UL); /* 3 */
344 FF( b
, c
, d
, a
, in
[ 3], S14
, 3250441966UL); /* 4 */
345 FF( a
, b
, c
, d
, in
[ 4], S11
, 4118548399UL); /* 5 */
346 FF( d
, a
, b
, c
, in
[ 5], S12
, 1200080426UL); /* 6 */
347 FF( c
, d
, a
, b
, in
[ 6], S13
, 2821735955UL); /* 7 */
348 FF( b
, c
, d
, a
, in
[ 7], S14
, 4249261313UL); /* 8 */
349 FF( a
, b
, c
, d
, in
[ 8], S11
, 1770035416UL); /* 9 */
350 FF( d
, a
, b
, c
, in
[ 9], S12
, 2336552879UL); /* 10 */
351 FF( c
, d
, a
, b
, in
[10], S13
, 4294925233UL); /* 11 */
352 FF( b
, c
, d
, a
, in
[11], S14
, 2304563134UL); /* 12 */
353 FF( a
, b
, c
, d
, in
[12], S11
, 1804603682UL); /* 13 */
354 FF( d
, a
, b
, c
, in
[13], S12
, 4254626195UL); /* 14 */
355 FF( c
, d
, a
, b
, in
[14], S13
, 2792965006UL); /* 15 */
356 FF( b
, c
, d
, a
, in
[15], S14
, 1236535329UL); /* 16 */
359 GG( a
, b
, c
, d
, in
[ 1], S21
, 4129170786UL); /* 17 */
360 GG( d
, a
, b
, c
, in
[ 6], S22
, 3225465664UL); /* 18 */
361 GG( c
, d
, a
, b
, in
[11], S23
, 643717713UL); /* 19 */
362 GG( b
, c
, d
, a
, in
[ 0], S24
, 3921069994UL); /* 20 */
363 GG( a
, b
, c
, d
, in
[ 5], S21
, 3593408605UL); /* 21 */
364 GG( d
, a
, b
, c
, in
[10], S22
, 38016083UL); /* 22 */
365 GG( c
, d
, a
, b
, in
[15], S23
, 3634488961UL); /* 23 */
366 GG( b
, c
, d
, a
, in
[ 4], S24
, 3889429448UL); /* 24 */
367 GG( a
, b
, c
, d
, in
[ 9], S21
, 568446438UL); /* 25 */
368 GG( d
, a
, b
, c
, in
[14], S22
, 3275163606UL); /* 26 */
369 GG( c
, d
, a
, b
, in
[ 3], S23
, 4107603335UL); /* 27 */
370 GG( b
, c
, d
, a
, in
[ 8], S24
, 1163531501UL); /* 28 */
371 GG( a
, b
, c
, d
, in
[13], S21
, 2850285829UL); /* 29 */
372 GG( d
, a
, b
, c
, in
[ 2], S22
, 4243563512UL); /* 30 */
373 GG( c
, d
, a
, b
, in
[ 7], S23
, 1735328473UL); /* 31 */
374 GG( b
, c
, d
, a
, in
[12], S24
, 2368359562UL); /* 32 */
377 HH( a
, b
, c
, d
, in
[ 5], S31
, 4294588738UL); /* 33 */
378 HH( d
, a
, b
, c
, in
[ 8], S32
, 2272392833UL); /* 34 */
379 HH( c
, d
, a
, b
, in
[11], S33
, 1839030562UL); /* 35 */
380 HH( b
, c
, d
, a
, in
[14], S34
, 4259657740UL); /* 36 */
381 HH( a
, b
, c
, d
, in
[ 1], S31
, 2763975236UL); /* 37 */
382 HH( d
, a
, b
, c
, in
[ 4], S32
, 1272893353UL); /* 38 */
383 HH( c
, d
, a
, b
, in
[ 7], S33
, 4139469664UL); /* 39 */
384 HH( b
, c
, d
, a
, in
[10], S34
, 3200236656UL); /* 40 */
385 HH( a
, b
, c
, d
, in
[13], S31
, 681279174UL); /* 41 */
386 HH( d
, a
, b
, c
, in
[ 0], S32
, 3936430074UL); /* 42 */
387 HH( c
, d
, a
, b
, in
[ 3], S33
, 3572445317UL); /* 43 */
388 HH( b
, c
, d
, a
, in
[ 6], S34
, 76029189UL); /* 44 */
389 HH( a
, b
, c
, d
, in
[ 9], S31
, 3654602809UL); /* 45 */
390 HH( d
, a
, b
, c
, in
[12], S32
, 3873151461UL); /* 46 */
391 HH( c
, d
, a
, b
, in
[15], S33
, 530742520UL); /* 47 */
392 HH( b
, c
, d
, a
, in
[ 2], S34
, 3299628645UL); /* 48 */
395 II( a
, b
, c
, d
, in
[ 0], S41
, 4096336452UL); /* 49 */
396 II( d
, a
, b
, c
, in
[ 7], S42
, 1126891415UL); /* 50 */
397 II( c
, d
, a
, b
, in
[14], S43
, 2878612391UL); /* 51 */
398 II( b
, c
, d
, a
, in
[ 5], S44
, 4237533241UL); /* 52 */
399 II( a
, b
, c
, d
, in
[12], S41
, 1700485571UL); /* 53 */
400 II( d
, a
, b
, c
, in
[ 3], S42
, 2399980690UL); /* 54 */
401 II( c
, d
, a
, b
, in
[10], S43
, 4293915773UL); /* 55 */
402 II( b
, c
, d
, a
, in
[ 1], S44
, 2240044497UL); /* 56 */
403 II( a
, b
, c
, d
, in
[ 8], S41
, 1873313359UL); /* 57 */
404 II( d
, a
, b
, c
, in
[15], S42
, 4264355552UL); /* 58 */
405 II( c
, d
, a
, b
, in
[ 6], S43
, 2734768916UL); /* 59 */
406 II( b
, c
, d
, a
, in
[13], S44
, 1309151649UL); /* 60 */
407 II( a
, b
, c
, d
, in
[ 4], S41
, 4149444226UL); /* 61 */
408 II( d
, a
, b
, c
, in
[11], S42
, 3174756917UL); /* 62 */
409 II( c
, d
, a
, b
, in
[ 2], S43
, 718787259UL); /* 63 */
410 II( b
, c
, d
, a
, in
[ 9], S44
, 3951481745UL); /* 64 */