xref: /freebsd/sbin/ipf/ipftest/md5.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
141edb306SCy Schubert 
241edb306SCy Schubert 
341edb306SCy Schubert 
441edb306SCy Schubert /*
541edb306SCy Schubert  ***********************************************************************
641edb306SCy Schubert  ** md5.c -- the source code for MD5 routines                         **
741edb306SCy Schubert  ** RSA Data Security, Inc. MD5 Message-Digest Algorithm              **
841edb306SCy Schubert  ** Created: 2/17/90 RLR                                              **
941edb306SCy Schubert  ** Revised: 1/91 SRD,AJ,BSK,JT Reference C ver., 7/10 constant corr. **
1041edb306SCy Schubert  ***********************************************************************
1141edb306SCy Schubert  */
1241edb306SCy Schubert 
1341edb306SCy Schubert /*
1441edb306SCy Schubert  ***********************************************************************
1541edb306SCy Schubert  ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.  **
1641edb306SCy Schubert  **                                                                   **
1741edb306SCy Schubert  ** License to copy and use this software is granted provided that    **
1841edb306SCy Schubert  ** it is identified as the "RSA Data Security, Inc. MD5 Message-     **
1941edb306SCy Schubert  ** Digest Algorithm" in all material mentioning or referencing this  **
2041edb306SCy Schubert  ** software or this function.                                        **
2141edb306SCy Schubert  **                                                                   **
2241edb306SCy Schubert  ** License is also granted to make and use derivative works          **
2341edb306SCy Schubert  ** provided that such works are identified as "derived from the RSA  **
2441edb306SCy Schubert  ** Data Security, Inc. MD5 Message-Digest Algorithm" in all          **
2541edb306SCy Schubert  ** material mentioning or referencing the derived work.              **
2641edb306SCy Schubert  **                                                                   **
2741edb306SCy Schubert  ** RSA Data Security, Inc. makes no representations concerning       **
2841edb306SCy Schubert  ** either the merchantability of this software or the suitability    **
2941edb306SCy Schubert  ** of this software for any particular purpose.  It is provided "as  **
3041edb306SCy Schubert  ** is" without express or implied warranty of any kind.              **
3141edb306SCy Schubert  **                                                                   **
3241edb306SCy Schubert  ** These notices must be retained in any copies of any part of this  **
3341edb306SCy Schubert  ** documentation and/or software.                                    **
3441edb306SCy Schubert  ***********************************************************************
3541edb306SCy Schubert  */
3641edb306SCy Schubert 
3741edb306SCy Schubert # if defined(_KERNEL)
3841edb306SCy Schubert #  include <sys/systm.h>
3941edb306SCy Schubert # else
4041edb306SCy Schubert #  include <string.h>
4141edb306SCy Schubert # endif
4241edb306SCy Schubert 
4341edb306SCy Schubert #include "md5.h"
4441edb306SCy Schubert 
4541edb306SCy Schubert /*
4641edb306SCy Schubert  ***********************************************************************
4741edb306SCy Schubert  **  Message-digest routines:                                         **
4841edb306SCy Schubert  **  To form the message digest for a message M                       **
4941edb306SCy Schubert  **    (1) Initialize a context buffer mdContext using MD5Init        **
5041edb306SCy Schubert  **    (2) Call MD5Update on mdContext and M                          **
5141edb306SCy Schubert  **    (3) Call MD5Final on mdContext                                 **
5241edb306SCy Schubert  **  The message digest is now in mdContext->digest[0...15]           **
5341edb306SCy Schubert  ***********************************************************************
5441edb306SCy Schubert  */
5541edb306SCy Schubert 
5641edb306SCy Schubert /* forward declaration */
5741edb306SCy Schubert static void Transform(UINT4 *, UINT4 *);
5841edb306SCy Schubert 
5941edb306SCy Schubert static unsigned char PADDING[64] = {
6041edb306SCy Schubert   0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6141edb306SCy Schubert   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6241edb306SCy Schubert   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6341edb306SCy Schubert   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6441edb306SCy Schubert   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6541edb306SCy Schubert   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6641edb306SCy Schubert   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6741edb306SCy Schubert   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
6841edb306SCy Schubert };
6941edb306SCy Schubert 
7041edb306SCy Schubert /* F, G, H and I are basic MD5 functions */
7141edb306SCy Schubert #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
7241edb306SCy Schubert #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
7341edb306SCy Schubert #define H(x, y, z) ((x) ^ (y) ^ (z))
7441edb306SCy Schubert #define I(x, y, z) ((y) ^ ((x) | (~z)))
7541edb306SCy Schubert 
7641edb306SCy Schubert /* ROTATE_LEFT rotates x left n bits */
7741edb306SCy Schubert #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
7841edb306SCy Schubert 
7941edb306SCy Schubert /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
8041edb306SCy Schubert /* Rotation is separate from addition to prevent recomputation */
8141edb306SCy Schubert #define FF(a, b, c, d, x, s, ac) \
8241edb306SCy Schubert   {(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
8341edb306SCy Schubert    (a) = ROTATE_LEFT ((a), (s)); \
8441edb306SCy Schubert    (a) += (b); \
8541edb306SCy Schubert   }
8641edb306SCy Schubert #define GG(a, b, c, d, x, s, ac) \
8741edb306SCy Schubert   {(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
8841edb306SCy Schubert    (a) = ROTATE_LEFT ((a), (s)); \
8941edb306SCy Schubert    (a) += (b); \
9041edb306SCy Schubert   }
9141edb306SCy Schubert #define HH(a, b, c, d, x, s, ac) \
9241edb306SCy Schubert   {(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
9341edb306SCy Schubert    (a) = ROTATE_LEFT ((a), (s)); \
9441edb306SCy Schubert    (a) += (b); \
9541edb306SCy Schubert   }
9641edb306SCy Schubert #define II(a, b, c, d, x, s, ac) \
9741edb306SCy Schubert   {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
9841edb306SCy Schubert    (a) = ROTATE_LEFT ((a), (s)); \
9941edb306SCy Schubert    (a) += (b); \
10041edb306SCy Schubert   }
10141edb306SCy Schubert 
10241edb306SCy Schubert #define UL(x)	x##U
10341edb306SCy Schubert 
10441edb306SCy Schubert /* The routine MD5Init initializes the message-digest context
10541edb306SCy Schubert    mdContext. All fields are set to zero.
10641edb306SCy Schubert  */
MD5Init(MD5_CTX * mdContext)107*efeb8bffSCy Schubert void MD5Init (MD5_CTX *mdContext)
10841edb306SCy Schubert {
10941edb306SCy Schubert   mdContext->i[0] = mdContext->i[1] = (UINT4)0;
11041edb306SCy Schubert 
11141edb306SCy Schubert   /* Load magic initialization constants.
11241edb306SCy Schubert    */
11341edb306SCy Schubert   mdContext->buf[0] = (UINT4)0x67452301;
11441edb306SCy Schubert   mdContext->buf[1] = (UINT4)0xefcdab89;
11541edb306SCy Schubert   mdContext->buf[2] = (UINT4)0x98badcfe;
11641edb306SCy Schubert   mdContext->buf[3] = (UINT4)0x10325476;
11741edb306SCy Schubert }
11841edb306SCy Schubert 
11941edb306SCy Schubert /* The routine MD5Update updates the message-digest context to
12041edb306SCy Schubert    account for the presence of each of the characters inBuf[0..inLen-1]
12141edb306SCy Schubert    in the message whose digest is being computed.
12241edb306SCy Schubert  */
MD5Update(MD5_CTX * mdContext,unsigned char * inBuf,unsigned int inLen)123*efeb8bffSCy Schubert void MD5Update (MD5_CTX *mdContext, unsigned char *inBuf, unsigned int inLen)
12441edb306SCy Schubert {
12541edb306SCy Schubert   UINT4 in[16];
12641edb306SCy Schubert   int mdi;
12741edb306SCy Schubert   unsigned int i, ii;
12841edb306SCy Schubert 
12941edb306SCy Schubert   /* compute number of bytes mod 64 */
13041edb306SCy Schubert   mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
13141edb306SCy Schubert 
13241edb306SCy Schubert   /* update number of bits */
13341edb306SCy Schubert   if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])
13441edb306SCy Schubert     mdContext->i[1]++;
13541edb306SCy Schubert   mdContext->i[0] += ((UINT4)inLen << 3);
13641edb306SCy Schubert   mdContext->i[1] += ((UINT4)inLen >> 29);
13741edb306SCy Schubert 
13841edb306SCy Schubert   while (inLen--) {
13941edb306SCy Schubert     /* add new character to buffer, increment mdi */
14041edb306SCy Schubert     mdContext->in[mdi++] = *inBuf++;
14141edb306SCy Schubert 
14241edb306SCy Schubert     /* transform if necessary */
14341edb306SCy Schubert     if (mdi == 0x40) {
14441edb306SCy Schubert       for (i = 0, ii = 0; i < 16; i++, ii += 4)
14541edb306SCy Schubert 	in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
14641edb306SCy Schubert 		(((UINT4)mdContext->in[ii+2]) << 16) |
14741edb306SCy Schubert 		(((UINT4)mdContext->in[ii+1]) << 8) |
14841edb306SCy Schubert 		((UINT4)mdContext->in[ii]);
14941edb306SCy Schubert       Transform (mdContext->buf, in);
15041edb306SCy Schubert       mdi = 0;
15141edb306SCy Schubert     }
15241edb306SCy Schubert   }
15341edb306SCy Schubert }
15441edb306SCy Schubert 
15541edb306SCy Schubert /* The routine MD5Final terminates the message-digest computation and
15641edb306SCy Schubert    ends with the desired message digest in mdContext->digest[0...15].
15741edb306SCy Schubert  */
MD5Final(unsigned char hash[],MD5_CTX * mdContext)158*efeb8bffSCy Schubert void MD5Final (unsigned char hash[], MD5_CTX *mdContext)
15941edb306SCy Schubert {
16041edb306SCy Schubert   UINT4 in[16];
16141edb306SCy Schubert   int mdi;
16241edb306SCy Schubert   unsigned int i, ii;
16341edb306SCy Schubert   unsigned int padLen;
16441edb306SCy Schubert 
16541edb306SCy Schubert   /* save number of bits */
16641edb306SCy Schubert   in[14] = mdContext->i[0];
16741edb306SCy Schubert   in[15] = mdContext->i[1];
16841edb306SCy Schubert 
16941edb306SCy Schubert   /* compute number of bytes mod 64 */
17041edb306SCy Schubert   mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
17141edb306SCy Schubert 
17241edb306SCy Schubert   /* pad out to 56 mod 64 */
17341edb306SCy Schubert   padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
17441edb306SCy Schubert   MD5Update (mdContext, PADDING, padLen);
17541edb306SCy Schubert 
17641edb306SCy Schubert   /* append length in bits and transform */
17741edb306SCy Schubert   for (i = 0, ii = 0; i < 14; i++, ii += 4)
17841edb306SCy Schubert     in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
17941edb306SCy Schubert 	    (((UINT4)mdContext->in[ii+2]) << 16) |
18041edb306SCy Schubert 	    (((UINT4)mdContext->in[ii+1]) << 8) |
18141edb306SCy Schubert 	    ((UINT4)mdContext->in[ii]);
18241edb306SCy Schubert   Transform (mdContext->buf, in);
18341edb306SCy Schubert 
18441edb306SCy Schubert   /* store buffer in digest */
18541edb306SCy Schubert   for (i = 0, ii = 0; i < 4; i++, ii += 4) {
18641edb306SCy Schubert     mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
18741edb306SCy Schubert     mdContext->digest[ii+1] =
18841edb306SCy Schubert       (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
18941edb306SCy Schubert     mdContext->digest[ii+2] =
19041edb306SCy Schubert       (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
19141edb306SCy Schubert     mdContext->digest[ii+3] =
19241edb306SCy Schubert       (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
19341edb306SCy Schubert   }
19441edb306SCy Schubert   bcopy((char *)mdContext->digest, (char *)hash, 16);
19541edb306SCy Schubert }
19641edb306SCy Schubert 
19741edb306SCy Schubert /* Basic MD5 step. Transforms buf based on in.
19841edb306SCy Schubert  */
Transform(UINT4 * buf,UINT4 * in)199*efeb8bffSCy Schubert static void Transform (UINT4 *buf, UINT4 *in)
20041edb306SCy Schubert {
20141edb306SCy Schubert   UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
20241edb306SCy Schubert 
20341edb306SCy Schubert   /* Round 1 */
20441edb306SCy Schubert #define S11 7
20541edb306SCy Schubert #define S12 12
20641edb306SCy Schubert #define S13 17
20741edb306SCy Schubert #define S14 22
20841edb306SCy Schubert   FF ( a, b, c, d, in[ 0], S11, UL(3614090360)); /* 1 */
20941edb306SCy Schubert   FF ( d, a, b, c, in[ 1], S12, UL(3905402710)); /* 2 */
21041edb306SCy Schubert   FF ( c, d, a, b, in[ 2], S13, UL( 606105819)); /* 3 */
21141edb306SCy Schubert   FF ( b, c, d, a, in[ 3], S14, UL(3250441966)); /* 4 */
21241edb306SCy Schubert   FF ( a, b, c, d, in[ 4], S11, UL(4118548399)); /* 5 */
21341edb306SCy Schubert   FF ( d, a, b, c, in[ 5], S12, UL(1200080426)); /* 6 */
21441edb306SCy Schubert   FF ( c, d, a, b, in[ 6], S13, UL(2821735955)); /* 7 */
21541edb306SCy Schubert   FF ( b, c, d, a, in[ 7], S14, UL(4249261313)); /* 8 */
21641edb306SCy Schubert   FF ( a, b, c, d, in[ 8], S11, UL(1770035416)); /* 9 */
21741edb306SCy Schubert   FF ( d, a, b, c, in[ 9], S12, UL(2336552879)); /* 10 */
21841edb306SCy Schubert   FF ( c, d, a, b, in[10], S13, UL(4294925233)); /* 11 */
21941edb306SCy Schubert   FF ( b, c, d, a, in[11], S14, UL(2304563134)); /* 12 */
22041edb306SCy Schubert   FF ( a, b, c, d, in[12], S11, UL(1804603682)); /* 13 */
22141edb306SCy Schubert   FF ( d, a, b, c, in[13], S12, UL(4254626195)); /* 14 */
22241edb306SCy Schubert   FF ( c, d, a, b, in[14], S13, UL(2792965006)); /* 15 */
22341edb306SCy Schubert   FF ( b, c, d, a, in[15], S14, UL(1236535329)); /* 16 */
22441edb306SCy Schubert 
22541edb306SCy Schubert   /* Round 2 */
22641edb306SCy Schubert #define S21 5
22741edb306SCy Schubert #define S22 9
22841edb306SCy Schubert #define S23 14
22941edb306SCy Schubert #define S24 20
23041edb306SCy Schubert   GG ( a, b, c, d, in[ 1], S21, UL(4129170786)); /* 17 */
23141edb306SCy Schubert   GG ( d, a, b, c, in[ 6], S22, UL(3225465664)); /* 18 */
23241edb306SCy Schubert   GG ( c, d, a, b, in[11], S23, UL( 643717713)); /* 19 */
23341edb306SCy Schubert   GG ( b, c, d, a, in[ 0], S24, UL(3921069994)); /* 20 */
23441edb306SCy Schubert   GG ( a, b, c, d, in[ 5], S21, UL(3593408605)); /* 21 */
23541edb306SCy Schubert   GG ( d, a, b, c, in[10], S22, UL(  38016083)); /* 22 */
23641edb306SCy Schubert   GG ( c, d, a, b, in[15], S23, UL(3634488961)); /* 23 */
23741edb306SCy Schubert   GG ( b, c, d, a, in[ 4], S24, UL(3889429448)); /* 24 */
23841edb306SCy Schubert   GG ( a, b, c, d, in[ 9], S21, UL( 568446438)); /* 25 */
23941edb306SCy Schubert   GG ( d, a, b, c, in[14], S22, UL(3275163606)); /* 26 */
24041edb306SCy Schubert   GG ( c, d, a, b, in[ 3], S23, UL(4107603335)); /* 27 */
24141edb306SCy Schubert   GG ( b, c, d, a, in[ 8], S24, UL(1163531501)); /* 28 */
24241edb306SCy Schubert   GG ( a, b, c, d, in[13], S21, UL(2850285829)); /* 29 */
24341edb306SCy Schubert   GG ( d, a, b, c, in[ 2], S22, UL(4243563512)); /* 30 */
24441edb306SCy Schubert   GG ( c, d, a, b, in[ 7], S23, UL(1735328473)); /* 31 */
24541edb306SCy Schubert   GG ( b, c, d, a, in[12], S24, UL(2368359562)); /* 32 */
24641edb306SCy Schubert 
24741edb306SCy Schubert   /* Round 3 */
24841edb306SCy Schubert #define S31 4
24941edb306SCy Schubert #define S32 11
25041edb306SCy Schubert #define S33 16
25141edb306SCy Schubert #define S34 23
25241edb306SCy Schubert   HH ( a, b, c, d, in[ 5], S31, UL(4294588738)); /* 33 */
25341edb306SCy Schubert   HH ( d, a, b, c, in[ 8], S32, UL(2272392833)); /* 34 */
25441edb306SCy Schubert   HH ( c, d, a, b, in[11], S33, UL(1839030562)); /* 35 */
25541edb306SCy Schubert   HH ( b, c, d, a, in[14], S34, UL(4259657740)); /* 36 */
25641edb306SCy Schubert   HH ( a, b, c, d, in[ 1], S31, UL(2763975236)); /* 37 */
25741edb306SCy Schubert   HH ( d, a, b, c, in[ 4], S32, UL(1272893353)); /* 38 */
25841edb306SCy Schubert   HH ( c, d, a, b, in[ 7], S33, UL(4139469664)); /* 39 */
25941edb306SCy Schubert   HH ( b, c, d, a, in[10], S34, UL(3200236656)); /* 40 */
26041edb306SCy Schubert   HH ( a, b, c, d, in[13], S31, UL( 681279174)); /* 41 */
26141edb306SCy Schubert   HH ( d, a, b, c, in[ 0], S32, UL(3936430074)); /* 42 */
26241edb306SCy Schubert   HH ( c, d, a, b, in[ 3], S33, UL(3572445317)); /* 43 */
26341edb306SCy Schubert   HH ( b, c, d, a, in[ 6], S34, UL(  76029189)); /* 44 */
26441edb306SCy Schubert   HH ( a, b, c, d, in[ 9], S31, UL(3654602809)); /* 45 */
26541edb306SCy Schubert   HH ( d, a, b, c, in[12], S32, UL(3873151461)); /* 46 */
26641edb306SCy Schubert   HH ( c, d, a, b, in[15], S33, UL( 530742520)); /* 47 */
26741edb306SCy Schubert   HH ( b, c, d, a, in[ 2], S34, UL(3299628645)); /* 48 */
26841edb306SCy Schubert 
26941edb306SCy Schubert   /* Round 4 */
27041edb306SCy Schubert #define S41 6
27141edb306SCy Schubert #define S42 10
27241edb306SCy Schubert #define S43 15
27341edb306SCy Schubert #define S44 21
27441edb306SCy Schubert   II ( a, b, c, d, in[ 0], S41, UL(4096336452)); /* 49 */
27541edb306SCy Schubert   II ( d, a, b, c, in[ 7], S42, UL(1126891415)); /* 50 */
27641edb306SCy Schubert   II ( c, d, a, b, in[14], S43, UL(2878612391)); /* 51 */
27741edb306SCy Schubert   II ( b, c, d, a, in[ 5], S44, UL(4237533241)); /* 52 */
27841edb306SCy Schubert   II ( a, b, c, d, in[12], S41, UL(1700485571)); /* 53 */
27941edb306SCy Schubert   II ( d, a, b, c, in[ 3], S42, UL(2399980690)); /* 54 */
28041edb306SCy Schubert   II ( c, d, a, b, in[10], S43, UL(4293915773)); /* 55 */
28141edb306SCy Schubert   II ( b, c, d, a, in[ 1], S44, UL(2240044497)); /* 56 */
28241edb306SCy Schubert   II ( a, b, c, d, in[ 8], S41, UL(1873313359)); /* 57 */
28341edb306SCy Schubert   II ( d, a, b, c, in[15], S42, UL(4264355552)); /* 58 */
28441edb306SCy Schubert   II ( c, d, a, b, in[ 6], S43, UL(2734768916)); /* 59 */
28541edb306SCy Schubert   II ( b, c, d, a, in[13], S44, UL(1309151649)); /* 60 */
28641edb306SCy Schubert   II ( a, b, c, d, in[ 4], S41, UL(4149444226)); /* 61 */
28741edb306SCy Schubert   II ( d, a, b, c, in[11], S42, UL(3174756917)); /* 62 */
28841edb306SCy Schubert   II ( c, d, a, b, in[ 2], S43, UL( 718787259)); /* 63 */
28941edb306SCy Schubert   II ( b, c, d, a, in[ 9], S44, UL(3951481745)); /* 64 */
29041edb306SCy Schubert 
29141edb306SCy Schubert   buf[0] += a;
29241edb306SCy Schubert   buf[1] += b;
29341edb306SCy Schubert   buf[2] += c;
29441edb306SCy Schubert   buf[3] += d;
29541edb306SCy Schubert }
29641edb306SCy Schubert 
29741edb306SCy Schubert /*
29841edb306SCy Schubert  ***********************************************************************
29941edb306SCy Schubert  ** End of md5.c                                                      **
30041edb306SCy Schubert  ******************************** (cut) ********************************
30141edb306SCy Schubert  */
302