1*1047Sgtb /*
2*1047Sgtb * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3*1047Sgtb * Use is subject to license terms.
4*1047Sgtb */
5*1047Sgtb
60Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
70Sstevel@tonic-gate
80Sstevel@tonic-gate #include "md5.h"
90Sstevel@tonic-gate
100Sstevel@tonic-gate /*
110Sstevel@tonic-gate **************************************************************************
120Sstevel@tonic-gate ** md5.c -- the source code for MD5 routines **
130Sstevel@tonic-gate ** RSA Data Security, Inc. MD5 Message-Digest Algorithm **
140Sstevel@tonic-gate ** Created: 2/17/90 RLR **
150Sstevel@tonic-gate ** Revised: 1/91 SRD, AJ, BSK, JT Reference C ver., 7/10 constant corr. **
160Sstevel@tonic-gate **************************************************************************
170Sstevel@tonic-gate */
180Sstevel@tonic-gate
190Sstevel@tonic-gate /*
200Sstevel@tonic-gate **************************************************************************
210Sstevel@tonic-gate ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
220Sstevel@tonic-gate ** **
230Sstevel@tonic-gate ** License to copy and use this software is granted provided that **
240Sstevel@tonic-gate ** it is identified as the "RSA Data Security, Inc. MD5 Message- **
250Sstevel@tonic-gate ** Digest Algorithm" in all material mentioning or referencing this **
260Sstevel@tonic-gate ** software or this function. **
270Sstevel@tonic-gate ** **
280Sstevel@tonic-gate ** License is also granted to make and use derivative works **
290Sstevel@tonic-gate ** provided that such works are identified as "derived from the RSA **
300Sstevel@tonic-gate ** Data Security, Inc. MD5 Message-Digest Algorithm" in all **
310Sstevel@tonic-gate ** material mentioning or referencing the derived work. **
320Sstevel@tonic-gate ** **
330Sstevel@tonic-gate ** RSA Data Security, Inc. makes no representations concerning **
340Sstevel@tonic-gate ** either the merchantability of this software or the suitability **
350Sstevel@tonic-gate ** of this software for any particular purpose. It is provided "as **
360Sstevel@tonic-gate ** is" without express or implied warranty of any kind. **
370Sstevel@tonic-gate ** **
380Sstevel@tonic-gate ** These notices must be retained in any copies of any part of this **
390Sstevel@tonic-gate ** documentation and/or software. **
400Sstevel@tonic-gate **************************************************************************
410Sstevel@tonic-gate */
420Sstevel@tonic-gate
430Sstevel@tonic-gate
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate **************************************************************************
460Sstevel@tonic-gate ** Message-digest routines: **
470Sstevel@tonic-gate ** To form the message digest for a message M **
480Sstevel@tonic-gate ** (1) Initialize a context buffer mdContext using MD5Init **
490Sstevel@tonic-gate ** (2) Call MD5Update on mdContext and M **
500Sstevel@tonic-gate ** (3) Call MD5Final on mdContext **
510Sstevel@tonic-gate ** The message digest is now in mdContext->digest[0...15] **
520Sstevel@tonic-gate **************************************************************************
530Sstevel@tonic-gate */
540Sstevel@tonic-gate
550Sstevel@tonic-gate /* forward declaration */
560Sstevel@tonic-gate static void Transform (UINT4 *buf, UINT4 *in);
570Sstevel@tonic-gate
580Sstevel@tonic-gate static unsigned char PADDING[64] = {
590Sstevel@tonic-gate 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
600Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
610Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
620Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
630Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
640Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
650Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
660Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
670Sstevel@tonic-gate };
680Sstevel@tonic-gate
690Sstevel@tonic-gate /* F, G, H and I are basic MD5 functions */
700Sstevel@tonic-gate #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
710Sstevel@tonic-gate #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
720Sstevel@tonic-gate #define H(x, y, z) ((x) ^ (y) ^ (z))
730Sstevel@tonic-gate #define I(x, y, z) ((y) ^ ((x) | (~z)))
740Sstevel@tonic-gate
750Sstevel@tonic-gate /* ROTATE_LEFT rotates x left n bits */
760Sstevel@tonic-gate #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
770Sstevel@tonic-gate
780Sstevel@tonic-gate /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
790Sstevel@tonic-gate /* Rotation is separate from addition to prevent recomputation */
800Sstevel@tonic-gate #define FF(a, b, c, d, x, s, ac) \
810Sstevel@tonic-gate {(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
820Sstevel@tonic-gate (a) = ROTATE_LEFT ((a), (s)); \
830Sstevel@tonic-gate (a) += (b); \
840Sstevel@tonic-gate }
850Sstevel@tonic-gate #define GG(a, b, c, d, x, s, ac) \
860Sstevel@tonic-gate {(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
870Sstevel@tonic-gate (a) = ROTATE_LEFT ((a), (s)); \
880Sstevel@tonic-gate (a) += (b); \
890Sstevel@tonic-gate }
900Sstevel@tonic-gate #define HH(a, b, c, d, x, s, ac) \
910Sstevel@tonic-gate {(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
920Sstevel@tonic-gate (a) = ROTATE_LEFT ((a), (s)); \
930Sstevel@tonic-gate (a) += (b); \
940Sstevel@tonic-gate }
950Sstevel@tonic-gate #define II(a, b, c, d, x, s, ac) \
960Sstevel@tonic-gate {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
970Sstevel@tonic-gate (a) = ROTATE_LEFT ((a), (s)); \
980Sstevel@tonic-gate (a) += (b); \
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate * The routine MD5Init initializes the message-digest context
1030Sstevel@tonic-gate * mdContext. All fields are set to zero.
1040Sstevel@tonic-gate */
MD5Init(MD5_CTX * mdContext)1050Sstevel@tonic-gate void MD5Init (MD5_CTX *mdContext)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate mdContext->i[0] = mdContext->i[1] = (UINT4)0;
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate * Load magic initialization constants.
1110Sstevel@tonic-gate */
1120Sstevel@tonic-gate mdContext->buf[0] = (UINT4)0x67452301;
1130Sstevel@tonic-gate mdContext->buf[1] = (UINT4)0xefcdab89;
1140Sstevel@tonic-gate mdContext->buf[2] = (UINT4)0x98badcfe;
1150Sstevel@tonic-gate mdContext->buf[3] = (UINT4)0x10325476;
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate * The routine MD5Update updates the message-digest context to
1200Sstevel@tonic-gate * account for the presence of each of the characters inBuf[0..inLen-1]
1210Sstevel@tonic-gate * in the message whose digest is being computed.
1220Sstevel@tonic-gate */
MD5Update(MD5_CTX * mdContext,unsigned char * inBuf,unsigned int inLen)1230Sstevel@tonic-gate void MD5Update (MD5_CTX *mdContext, unsigned char *inBuf, unsigned int inLen)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate UINT4 in[16];
1260Sstevel@tonic-gate int mdi;
1270Sstevel@tonic-gate unsigned int i, ii;
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate /* compute number of bytes mod 64 */
1300Sstevel@tonic-gate mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate /* update number of bits */
1330Sstevel@tonic-gate if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])
1340Sstevel@tonic-gate mdContext->i[1]++;
1350Sstevel@tonic-gate mdContext->i[0] += ((UINT4)inLen << 3);
1360Sstevel@tonic-gate mdContext->i[1] += ((UINT4)inLen >> 29);
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate while (inLen--) {
1390Sstevel@tonic-gate /* add new character to buffer, increment mdi */
1400Sstevel@tonic-gate mdContext->in[mdi++] = *inBuf++;
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate /* transform if necessary */
1430Sstevel@tonic-gate if (mdi == 0x40) {
1440Sstevel@tonic-gate for (i = 0, ii = 0; i < 16; i++, ii += 4)
1450Sstevel@tonic-gate in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
1460Sstevel@tonic-gate (((UINT4)mdContext->in[ii+2]) << 16) |
1470Sstevel@tonic-gate (((UINT4)mdContext->in[ii+1]) << 8) |
1480Sstevel@tonic-gate ((UINT4)mdContext->in[ii]);
1490Sstevel@tonic-gate Transform (mdContext->buf, in);
1500Sstevel@tonic-gate mdi = 0;
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate * The routine MD5Final terminates the message-digest computation and
1570Sstevel@tonic-gate * ends with the desired message digest in mdContext->digest[0...15].
1580Sstevel@tonic-gate */
MD5Final(MD5_CTX * mdContext)1590Sstevel@tonic-gate void MD5Final (MD5_CTX *mdContext)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate UINT4 in[16];
1620Sstevel@tonic-gate int mdi;
1630Sstevel@tonic-gate unsigned int i, ii;
1640Sstevel@tonic-gate unsigned int padLen;
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate /* save number of bits */
1670Sstevel@tonic-gate in[14] = mdContext->i[0];
1680Sstevel@tonic-gate in[15] = mdContext->i[1];
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate /* compute number of bytes mod 64 */
1710Sstevel@tonic-gate mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /* pad out to 56 mod 64 */
1740Sstevel@tonic-gate padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
1750Sstevel@tonic-gate MD5Update (mdContext, PADDING, padLen);
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate /* append length in bits and transform */
1780Sstevel@tonic-gate for (i = 0, ii = 0; i < 14; i++, ii += 4)
1790Sstevel@tonic-gate in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
1800Sstevel@tonic-gate (((UINT4)mdContext->in[ii+2]) << 16) |
1810Sstevel@tonic-gate (((UINT4)mdContext->in[ii+1]) << 8) |
1820Sstevel@tonic-gate ((UINT4)mdContext->in[ii]);
1830Sstevel@tonic-gate Transform (mdContext->buf, in);
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate /* store buffer in digest */
1860Sstevel@tonic-gate for (i = 0, ii = 0; i < 4; i++, ii += 4) {
1870Sstevel@tonic-gate mdContext->digest[ii] =
1880Sstevel@tonic-gate (unsigned char)(mdContext->buf[i] & 0xFF);
1890Sstevel@tonic-gate mdContext->digest[ii+1] =
1900Sstevel@tonic-gate (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
1910Sstevel@tonic-gate mdContext->digest[ii+2] =
1920Sstevel@tonic-gate (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
1930Sstevel@tonic-gate mdContext->digest[ii+3] =
1940Sstevel@tonic-gate (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate * Basic MD5 step. Transforms buf based on in.
2000Sstevel@tonic-gate */
Transform(UINT4 * buf,UINT4 * in)2010Sstevel@tonic-gate static void Transform (UINT4 *buf, UINT4 *in)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate /* Round 1 */
2060Sstevel@tonic-gate #define S11 7
2070Sstevel@tonic-gate #define S12 12
2080Sstevel@tonic-gate #define S13 17
2090Sstevel@tonic-gate #define S14 22
210*1047Sgtb FF (a, b, c, d, in[ 0], S11, 3614090360U); /* 1 */
211*1047Sgtb FF (d, a, b, c, in[ 1], S12, 3905402710U); /* 2 */
212*1047Sgtb FF (c, d, a, b, in[ 2], S13, 606105819U); /* 3 */
213*1047Sgtb FF (b, c, d, a, in[ 3], S14, 3250441966U); /* 4 */
214*1047Sgtb FF (a, b, c, d, in[ 4], S11, 4118548399U); /* 5 */
215*1047Sgtb FF (d, a, b, c, in[ 5], S12, 1200080426U); /* 6 */
216*1047Sgtb FF (c, d, a, b, in[ 6], S13, 2821735955U); /* 7 */
217*1047Sgtb FF (b, c, d, a, in[ 7], S14, 4249261313U); /* 8 */
218*1047Sgtb FF (a, b, c, d, in[ 8], S11, 1770035416U); /* 9 */
219*1047Sgtb FF (d, a, b, c, in[ 9], S12, 2336552879U); /* 10 */
220*1047Sgtb FF (c, d, a, b, in[10], S13, 4294925233U); /* 11 */
221*1047Sgtb FF (b, c, d, a, in[11], S14, 2304563134U); /* 12 */
222*1047Sgtb FF (a, b, c, d, in[12], S11, 1804603682U); /* 13 */
223*1047Sgtb FF (d, a, b, c, in[13], S12, 4254626195U); /* 14 */
224*1047Sgtb FF (c, d, a, b, in[14], S13, 2792965006U); /* 15 */
225*1047Sgtb FF (b, c, d, a, in[15], S14, 1236535329U); /* 16 */
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate /* Round 2 */
2280Sstevel@tonic-gate #define S21 5
2290Sstevel@tonic-gate #define S22 9
2300Sstevel@tonic-gate #define S23 14
2310Sstevel@tonic-gate #define S24 20
232*1047Sgtb GG (a, b, c, d, in[ 1], S21, 4129170786U); /* 17 */
233*1047Sgtb GG (d, a, b, c, in[ 6], S22, 3225465664U); /* 18 */
234*1047Sgtb GG (c, d, a, b, in[11], S23, 643717713U); /* 19 */
235*1047Sgtb GG (b, c, d, a, in[ 0], S24, 3921069994U); /* 20 */
236*1047Sgtb GG (a, b, c, d, in[ 5], S21, 3593408605U); /* 21 */
237*1047Sgtb GG (d, a, b, c, in[10], S22, 38016083U); /* 22 */
238*1047Sgtb GG (c, d, a, b, in[15], S23, 3634488961U); /* 23 */
239*1047Sgtb GG (b, c, d, a, in[ 4], S24, 3889429448U); /* 24 */
240*1047Sgtb GG (a, b, c, d, in[ 9], S21, 568446438U); /* 25 */
241*1047Sgtb GG (d, a, b, c, in[14], S22, 3275163606U); /* 26 */
242*1047Sgtb GG (c, d, a, b, in[ 3], S23, 4107603335U); /* 27 */
243*1047Sgtb GG (b, c, d, a, in[ 8], S24, 1163531501U); /* 28 */
244*1047Sgtb GG (a, b, c, d, in[13], S21, 2850285829U); /* 29 */
245*1047Sgtb GG (d, a, b, c, in[ 2], S22, 4243563512U); /* 30 */
246*1047Sgtb GG (c, d, a, b, in[ 7], S23, 1735328473U); /* 31 */
247*1047Sgtb GG (b, c, d, a, in[12], S24, 2368359562U); /* 32 */
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate /* Round 3 */
2500Sstevel@tonic-gate #define S31 4
2510Sstevel@tonic-gate #define S32 11
2520Sstevel@tonic-gate #define S33 16
2530Sstevel@tonic-gate #define S34 23
254*1047Sgtb HH (a, b, c, d, in[ 5], S31, 4294588738U); /* 33 */
255*1047Sgtb HH (d, a, b, c, in[ 8], S32, 2272392833U); /* 34 */
256*1047Sgtb HH (c, d, a, b, in[11], S33, 1839030562U); /* 35 */
257*1047Sgtb HH (b, c, d, a, in[14], S34, 4259657740U); /* 36 */
258*1047Sgtb HH (a, b, c, d, in[ 1], S31, 2763975236U); /* 37 */
259*1047Sgtb HH (d, a, b, c, in[ 4], S32, 1272893353U); /* 38 */
260*1047Sgtb HH (c, d, a, b, in[ 7], S33, 4139469664U); /* 39 */
261*1047Sgtb HH (b, c, d, a, in[10], S34, 3200236656U); /* 40 */
262*1047Sgtb HH (a, b, c, d, in[13], S31, 681279174U); /* 41 */
263*1047Sgtb HH (d, a, b, c, in[ 0], S32, 3936430074U); /* 42 */
264*1047Sgtb HH (c, d, a, b, in[ 3], S33, 3572445317U); /* 43 */
265*1047Sgtb HH (b, c, d, a, in[ 6], S34, 76029189U); /* 44 */
266*1047Sgtb HH (a, b, c, d, in[ 9], S31, 3654602809U); /* 45 */
267*1047Sgtb HH (d, a, b, c, in[12], S32, 3873151461U); /* 46 */
268*1047Sgtb HH (c, d, a, b, in[15], S33, 530742520U); /* 47 */
269*1047Sgtb HH (b, c, d, a, in[ 2], S34, 3299628645U); /* 48 */
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate /* Round 4 */
2720Sstevel@tonic-gate #define S41 6
2730Sstevel@tonic-gate #define S42 10
2740Sstevel@tonic-gate #define S43 15
2750Sstevel@tonic-gate #define S44 21
276*1047Sgtb II (a, b, c, d, in[ 0], S41, 4096336452U); /* 49 */
277*1047Sgtb II (d, a, b, c, in[ 7], S42, 1126891415U); /* 50 */
278*1047Sgtb II (c, d, a, b, in[14], S43, 2878612391U); /* 51 */
279*1047Sgtb II (b, c, d, a, in[ 5], S44, 4237533241U); /* 52 */
280*1047Sgtb II (a, b, c, d, in[12], S41, 1700485571U); /* 53 */
281*1047Sgtb II (d, a, b, c, in[ 3], S42, 2399980690U); /* 54 */
282*1047Sgtb II (c, d, a, b, in[10], S43, 4293915773U); /* 55 */
283*1047Sgtb II (b, c, d, a, in[ 1], S44, 2240044497U); /* 56 */
284*1047Sgtb II (a, b, c, d, in[ 8], S41, 1873313359U); /* 57 */
285*1047Sgtb II (d, a, b, c, in[15], S42, 4264355552U); /* 58 */
286*1047Sgtb II (c, d, a, b, in[ 6], S43, 2734768916U); /* 59 */
287*1047Sgtb II (b, c, d, a, in[13], S44, 1309151649U); /* 60 */
288*1047Sgtb II (a, b, c, d, in[ 4], S41, 4149444226U); /* 61 */
289*1047Sgtb II (d, a, b, c, in[11], S42, 3174756917U); /* 62 */
290*1047Sgtb II (c, d, a, b, in[ 2], S43, 718787259U); /* 63 */
291*1047Sgtb II (b, c, d, a, in[ 9], S44, 3951481745U); /* 64 */
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate buf[0] += a;
2940Sstevel@tonic-gate buf[1] += b;
2950Sstevel@tonic-gate buf[2] += c;
2960Sstevel@tonic-gate buf[3] += d;
2970Sstevel@tonic-gate }
298