1*da217e55Szrj /*
2*da217e55Szrj * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
3*da217e55Szrj *
4*da217e55Szrj * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
5*da217e55Szrj * rights reserved.
6*da217e55Szrj *
7*da217e55Szrj * License to copy and use this software is granted provided that it
8*da217e55Szrj * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
9*da217e55Szrj * Algorithm" in all material mentioning or referencing this software
10*da217e55Szrj * or this function.
11*da217e55Szrj *
12*da217e55Szrj * License is also granted to make and use derivative works provided
13*da217e55Szrj * that such works are identified as "derived from the RSA Data
14*da217e55Szrj * Security, Inc. MD5 Message-Digest Algorithm" in all material
15*da217e55Szrj * mentioning or referencing the derived work.
16*da217e55Szrj *
17*da217e55Szrj * RSA Data Security, Inc. makes no representations concerning either
18*da217e55Szrj * the merchantability of this software or the suitability of this
19*da217e55Szrj * software for any particular purpose. It is provided "as is"
20*da217e55Szrj * without express or implied warranty of any kind.
21*da217e55Szrj *
22*da217e55Szrj * These notices must be retained in any copies of any part of this
23*da217e55Szrj * documentation and/or software.
24*da217e55Szrj *
25*da217e55Szrj * This code is the same as the code published by RSA Inc. It has been
26*da217e55Szrj * edited for clarity, style and inlineability.
27*da217e55Szrj */
28*da217e55Szrj
29*da217e55Szrj /*
30*da217e55Szrj * This header shall not have include guards, be included only locally
31*da217e55Szrj * and not export/pass MD5_CTX unless WITH_OPENSSL.
32*da217e55Szrj */
33*da217e55Szrj
34*da217e55Szrj #include <sys/cdefs.h>
35*da217e55Szrj #include <sys/types.h>
36*da217e55Szrj #include <string.h>
37*da217e55Szrj #include <machine/endian.h>
38*da217e55Szrj #include <sys/endian.h>
39*da217e55Szrj
40*da217e55Szrj #ifdef WITH_OPENSSL
41*da217e55Szrj #include <openssl/md5.h>
42*da217e55Szrj #else
43*da217e55Szrj /* XXX must match <openssl/md5.h> !!! */
44*da217e55Szrj #define MD5_CBLOCK 64
45*da217e55Szrj #define MD5_DIGEST_LENGTH 16
46*da217e55Szrj #define MD5_LBLOCK (MD5_CBLOCK/4)
47*da217e55Szrj #define MD5_LONG unsigned int
48*da217e55Szrj typedef struct MD5state_st {
49*da217e55Szrj MD5_LONG A,B,C,D;
50*da217e55Szrj MD5_LONG Nl,Nh;
51*da217e55Szrj MD5_LONG data[MD5_LBLOCK];
52*da217e55Szrj unsigned int num;
53*da217e55Szrj } MD5_CTX;
54*da217e55Szrj #endif
55*da217e55Szrj
56*da217e55Szrj #define MD5_BLOCK_LENGTH MD5_CBLOCK
57*da217e55Szrj #define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1)
58*da217e55Szrj
59*da217e55Szrj #if (BYTE_ORDER == LITTLE_ENDIAN)
60*da217e55Szrj #define _md5_Encode memcpy
61*da217e55Szrj #define _md5_Decode memcpy
62*da217e55Szrj #else
63*da217e55Szrj
64*da217e55Szrj /*
65*da217e55Szrj * Encodes input (u_int32_t) into output (unsigned char). Assumes len is
66*da217e55Szrj * a multiple of 4.
67*da217e55Szrj */
68*da217e55Szrj static void
_md5_Encode(unsigned char * output,u_int32_t * input,unsigned int len)69*da217e55Szrj _md5_Encode (unsigned char *output, u_int32_t *input, unsigned int len)
70*da217e55Szrj {
71*da217e55Szrj unsigned int i;
72*da217e55Szrj u_int32_t *op = (u_int32_t *)output;
73*da217e55Szrj
74*da217e55Szrj for (i = 0; i < len / 4; i++)
75*da217e55Szrj op[i] = htole32(input[i]);
76*da217e55Szrj }
77*da217e55Szrj
78*da217e55Szrj /*
79*da217e55Szrj * Decodes input (unsigned char) into output (u_int32_t). Assumes len is
80*da217e55Szrj * a multiple of 4.
81*da217e55Szrj */
82*da217e55Szrj static void
_md5_Decode(u_int32_t * output,const unsigned char * input,unsigned int len)83*da217e55Szrj _md5_Decode (u_int32_t *output, const unsigned char *input, unsigned int len)
84*da217e55Szrj {
85*da217e55Szrj unsigned int i;
86*da217e55Szrj const u_int32_t *ip = (const u_int32_t *)input;
87*da217e55Szrj
88*da217e55Szrj for (i = 0; i < len / 4; i++)
89*da217e55Szrj output[i] = le32toh(ip[i]);
90*da217e55Szrj }
91*da217e55Szrj #endif
92*da217e55Szrj
93*da217e55Szrj static unsigned char _md5_PADDING[64] = {
94*da217e55Szrj 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95*da217e55Szrj 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96*da217e55Szrj 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
97*da217e55Szrj };
98*da217e55Szrj
99*da217e55Szrj /* F, G, H and I are basic MD5 functions. */
100*da217e55Szrj #define _md5_F(x, y, z) (((x) & (y)) | ((~x) & (z)))
101*da217e55Szrj #define _md5_G(x, y, z) (((x) & (z)) | ((y) & (~z)))
102*da217e55Szrj #define _md5_H(x, y, z) ((x) ^ (y) ^ (z))
103*da217e55Szrj #define _md5_I(x, y, z) ((y) ^ ((x) | (~z)))
104*da217e55Szrj
105*da217e55Szrj /* ROTATE_LEFT rotates x left n bits. */
106*da217e55Szrj #define _md5_ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
107*da217e55Szrj
108*da217e55Szrj /*
109*da217e55Szrj * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
110*da217e55Szrj * Rotation is separate from addition to prevent recomputation.
111*da217e55Szrj */
112*da217e55Szrj #define _md5_FF(a, b, c, d, x, s, ac) { \
113*da217e55Szrj (a) += _md5_F ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
114*da217e55Szrj (a) = _md5_ROTATE_LEFT ((a), (s)); \
115*da217e55Szrj (a) += (b); \
116*da217e55Szrj }
117*da217e55Szrj #define _md5_GG(a, b, c, d, x, s, ac) { \
118*da217e55Szrj (a) += _md5_G ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
119*da217e55Szrj (a) = _md5_ROTATE_LEFT ((a), (s)); \
120*da217e55Szrj (a) += (b); \
121*da217e55Szrj }
122*da217e55Szrj #define _md5_HH(a, b, c, d, x, s, ac) { \
123*da217e55Szrj (a) += _md5_H ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
124*da217e55Szrj (a) = _md5_ROTATE_LEFT ((a), (s)); \
125*da217e55Szrj (a) += (b); \
126*da217e55Szrj }
127*da217e55Szrj #define _md5_II(a, b, c, d, x, s, ac) { \
128*da217e55Szrj (a) += _md5_I ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
129*da217e55Szrj (a) = _md5_ROTATE_LEFT ((a), (s)); \
130*da217e55Szrj (a) += (b); \
131*da217e55Szrj }
132*da217e55Szrj
133*da217e55Szrj /* MD5 basic transformation. Transforms state based on block. */
134*da217e55Szrj static void
MD5Transform(u_int32_t * state,const unsigned char * block)135*da217e55Szrj MD5Transform (u_int32_t *state, const unsigned char *block)
136*da217e55Szrj {
137*da217e55Szrj u_int32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
138*da217e55Szrj
139*da217e55Szrj _md5_Decode (x, block, 64);
140*da217e55Szrj
141*da217e55Szrj /* Round 1 */
142*da217e55Szrj #define _md5_S11 7
143*da217e55Szrj #define _md5_S12 12
144*da217e55Szrj #define _md5_S13 17
145*da217e55Szrj #define _md5_S14 22
146*da217e55Szrj _md5_FF (a, b, c, d, x[ 0], _md5_S11, 0xd76aa478); /* 1 */
147*da217e55Szrj _md5_FF (d, a, b, c, x[ 1], _md5_S12, 0xe8c7b756); /* 2 */
148*da217e55Szrj _md5_FF (c, d, a, b, x[ 2], _md5_S13, 0x242070db); /* 3 */
149*da217e55Szrj _md5_FF (b, c, d, a, x[ 3], _md5_S14, 0xc1bdceee); /* 4 */
150*da217e55Szrj _md5_FF (a, b, c, d, x[ 4], _md5_S11, 0xf57c0faf); /* 5 */
151*da217e55Szrj _md5_FF (d, a, b, c, x[ 5], _md5_S12, 0x4787c62a); /* 6 */
152*da217e55Szrj _md5_FF (c, d, a, b, x[ 6], _md5_S13, 0xa8304613); /* 7 */
153*da217e55Szrj _md5_FF (b, c, d, a, x[ 7], _md5_S14, 0xfd469501); /* 8 */
154*da217e55Szrj _md5_FF (a, b, c, d, x[ 8], _md5_S11, 0x698098d8); /* 9 */
155*da217e55Szrj _md5_FF (d, a, b, c, x[ 9], _md5_S12, 0x8b44f7af); /* 10 */
156*da217e55Szrj _md5_FF (c, d, a, b, x[10], _md5_S13, 0xffff5bb1); /* 11 */
157*da217e55Szrj _md5_FF (b, c, d, a, x[11], _md5_S14, 0x895cd7be); /* 12 */
158*da217e55Szrj _md5_FF (a, b, c, d, x[12], _md5_S11, 0x6b901122); /* 13 */
159*da217e55Szrj _md5_FF (d, a, b, c, x[13], _md5_S12, 0xfd987193); /* 14 */
160*da217e55Szrj _md5_FF (c, d, a, b, x[14], _md5_S13, 0xa679438e); /* 15 */
161*da217e55Szrj _md5_FF (b, c, d, a, x[15], _md5_S14, 0x49b40821); /* 16 */
162*da217e55Szrj
163*da217e55Szrj /* Round 2 */
164*da217e55Szrj #define _md5_S21 5
165*da217e55Szrj #define _md5_S22 9
166*da217e55Szrj #define _md5_S23 14
167*da217e55Szrj #define _md5_S24 20
168*da217e55Szrj _md5_GG (a, b, c, d, x[ 1], _md5_S21, 0xf61e2562); /* 17 */
169*da217e55Szrj _md5_GG (d, a, b, c, x[ 6], _md5_S22, 0xc040b340); /* 18 */
170*da217e55Szrj _md5_GG (c, d, a, b, x[11], _md5_S23, 0x265e5a51); /* 19 */
171*da217e55Szrj _md5_GG (b, c, d, a, x[ 0], _md5_S24, 0xe9b6c7aa); /* 20 */
172*da217e55Szrj _md5_GG (a, b, c, d, x[ 5], _md5_S21, 0xd62f105d); /* 21 */
173*da217e55Szrj _md5_GG (d, a, b, c, x[10], _md5_S22, 0x2441453); /* 22 */
174*da217e55Szrj _md5_GG (c, d, a, b, x[15], _md5_S23, 0xd8a1e681); /* 23 */
175*da217e55Szrj _md5_GG (b, c, d, a, x[ 4], _md5_S24, 0xe7d3fbc8); /* 24 */
176*da217e55Szrj _md5_GG (a, b, c, d, x[ 9], _md5_S21, 0x21e1cde6); /* 25 */
177*da217e55Szrj _md5_GG (d, a, b, c, x[14], _md5_S22, 0xc33707d6); /* 26 */
178*da217e55Szrj _md5_GG (c, d, a, b, x[ 3], _md5_S23, 0xf4d50d87); /* 27 */
179*da217e55Szrj _md5_GG (b, c, d, a, x[ 8], _md5_S24, 0x455a14ed); /* 28 */
180*da217e55Szrj _md5_GG (a, b, c, d, x[13], _md5_S21, 0xa9e3e905); /* 29 */
181*da217e55Szrj _md5_GG (d, a, b, c, x[ 2], _md5_S22, 0xfcefa3f8); /* 30 */
182*da217e55Szrj _md5_GG (c, d, a, b, x[ 7], _md5_S23, 0x676f02d9); /* 31 */
183*da217e55Szrj _md5_GG (b, c, d, a, x[12], _md5_S24, 0x8d2a4c8a); /* 32 */
184*da217e55Szrj
185*da217e55Szrj /* Round 3 */
186*da217e55Szrj #define _md5_S31 4
187*da217e55Szrj #define _md5_S32 11
188*da217e55Szrj #define _md5_S33 16
189*da217e55Szrj #define _md5_S34 23
190*da217e55Szrj _md5_HH (a, b, c, d, x[ 5], _md5_S31, 0xfffa3942); /* 33 */
191*da217e55Szrj _md5_HH (d, a, b, c, x[ 8], _md5_S32, 0x8771f681); /* 34 */
192*da217e55Szrj _md5_HH (c, d, a, b, x[11], _md5_S33, 0x6d9d6122); /* 35 */
193*da217e55Szrj _md5_HH (b, c, d, a, x[14], _md5_S34, 0xfde5380c); /* 36 */
194*da217e55Szrj _md5_HH (a, b, c, d, x[ 1], _md5_S31, 0xa4beea44); /* 37 */
195*da217e55Szrj _md5_HH (d, a, b, c, x[ 4], _md5_S32, 0x4bdecfa9); /* 38 */
196*da217e55Szrj _md5_HH (c, d, a, b, x[ 7], _md5_S33, 0xf6bb4b60); /* 39 */
197*da217e55Szrj _md5_HH (b, c, d, a, x[10], _md5_S34, 0xbebfbc70); /* 40 */
198*da217e55Szrj _md5_HH (a, b, c, d, x[13], _md5_S31, 0x289b7ec6); /* 41 */
199*da217e55Szrj _md5_HH (d, a, b, c, x[ 0], _md5_S32, 0xeaa127fa); /* 42 */
200*da217e55Szrj _md5_HH (c, d, a, b, x[ 3], _md5_S33, 0xd4ef3085); /* 43 */
201*da217e55Szrj _md5_HH (b, c, d, a, x[ 6], _md5_S34, 0x4881d05); /* 44 */
202*da217e55Szrj _md5_HH (a, b, c, d, x[ 9], _md5_S31, 0xd9d4d039); /* 45 */
203*da217e55Szrj _md5_HH (d, a, b, c, x[12], _md5_S32, 0xe6db99e5); /* 46 */
204*da217e55Szrj _md5_HH (c, d, a, b, x[15], _md5_S33, 0x1fa27cf8); /* 47 */
205*da217e55Szrj _md5_HH (b, c, d, a, x[ 2], _md5_S34, 0xc4ac5665); /* 48 */
206*da217e55Szrj
207*da217e55Szrj /* Round 4 */
208*da217e55Szrj #define _md5_S41 6
209*da217e55Szrj #define _md5_S42 10
210*da217e55Szrj #define _md5_S43 15
211*da217e55Szrj #define _md5_S44 21
212*da217e55Szrj _md5_II (a, b, c, d, x[ 0], _md5_S41, 0xf4292244); /* 49 */
213*da217e55Szrj _md5_II (d, a, b, c, x[ 7], _md5_S42, 0x432aff97); /* 50 */
214*da217e55Szrj _md5_II (c, d, a, b, x[14], _md5_S43, 0xab9423a7); /* 51 */
215*da217e55Szrj _md5_II (b, c, d, a, x[ 5], _md5_S44, 0xfc93a039); /* 52 */
216*da217e55Szrj _md5_II (a, b, c, d, x[12], _md5_S41, 0x655b59c3); /* 53 */
217*da217e55Szrj _md5_II (d, a, b, c, x[ 3], _md5_S42, 0x8f0ccc92); /* 54 */
218*da217e55Szrj _md5_II (c, d, a, b, x[10], _md5_S43, 0xffeff47d); /* 55 */
219*da217e55Szrj _md5_II (b, c, d, a, x[ 1], _md5_S44, 0x85845dd1); /* 56 */
220*da217e55Szrj _md5_II (a, b, c, d, x[ 8], _md5_S41, 0x6fa87e4f); /* 57 */
221*da217e55Szrj _md5_II (d, a, b, c, x[15], _md5_S42, 0xfe2ce6e0); /* 58 */
222*da217e55Szrj _md5_II (c, d, a, b, x[ 6], _md5_S43, 0xa3014314); /* 59 */
223*da217e55Szrj _md5_II (b, c, d, a, x[13], _md5_S44, 0x4e0811a1); /* 60 */
224*da217e55Szrj _md5_II (a, b, c, d, x[ 4], _md5_S41, 0xf7537e82); /* 61 */
225*da217e55Szrj _md5_II (d, a, b, c, x[11], _md5_S42, 0xbd3af235); /* 62 */
226*da217e55Szrj _md5_II (c, d, a, b, x[ 2], _md5_S43, 0x2ad7d2bb); /* 63 */
227*da217e55Szrj _md5_II (b, c, d, a, x[ 9], _md5_S44, 0xeb86d391); /* 64 */
228*da217e55Szrj
229*da217e55Szrj state[0] += a;
230*da217e55Szrj state[1] += b;
231*da217e55Szrj state[2] += c;
232*da217e55Szrj state[3] += d;
233*da217e55Szrj
234*da217e55Szrj /* Zeroize sensitive information. */
235*da217e55Szrj memset ((void *)x, 0, sizeof (x));
236*da217e55Szrj }
237*da217e55Szrj
238*da217e55Szrj /* MD5 initialization. Begins an MD5 operation, writing a new context. */
239*da217e55Szrj static int
MD5Init(MD5_CTX * context)240*da217e55Szrj MD5Init (MD5_CTX *context)
241*da217e55Szrj {
242*da217e55Szrj
243*da217e55Szrj context->Nl = context->Nh = 0;
244*da217e55Szrj
245*da217e55Szrj /* Load magic initialization constants. */
246*da217e55Szrj context->A = 0x67452301;
247*da217e55Szrj context->B = 0xefcdab89;
248*da217e55Szrj context->C = 0x98badcfe;
249*da217e55Szrj context->D = 0x10325476;
250*da217e55Szrj return 1;
251*da217e55Szrj }
252*da217e55Szrj /*
253*da217e55Szrj * MD5 block update operation. Continues an MD5 message-digest
254*da217e55Szrj * operation, processing another message block, and updating the
255*da217e55Szrj * context.
256*da217e55Szrj */
257*da217e55Szrj static void
MD5Update(MD5_CTX * context,const void * in,unsigned int inputLen)258*da217e55Szrj MD5Update (MD5_CTX *context, const void *in, unsigned int inputLen)
259*da217e55Szrj {
260*da217e55Szrj unsigned int i, idx, partLen;
261*da217e55Szrj const unsigned char *input = in;
262*da217e55Szrj
263*da217e55Szrj /* Compute number of bytes mod 64 */
264*da217e55Szrj idx = (unsigned int)((context->Nl >> 3) & 0x3F);
265*da217e55Szrj
266*da217e55Szrj /* Update number of bits */
267*da217e55Szrj if ((context->Nl += ((u_int32_t)inputLen << 3))
268*da217e55Szrj < ((u_int32_t)inputLen << 3))
269*da217e55Szrj context->Nh++;
270*da217e55Szrj context->Nh += ((u_int32_t)inputLen >> 29);
271*da217e55Szrj
272*da217e55Szrj partLen = 64 - idx;
273*da217e55Szrj
274*da217e55Szrj /* Transform as many times as possible. */
275*da217e55Szrj if (inputLen >= partLen) {
276*da217e55Szrj memcpy(&((char *)context->data)[idx], (const void *)input,
277*da217e55Szrj partLen);
278*da217e55Szrj MD5Transform (&context->A, (unsigned char *)context->data);
279*da217e55Szrj
280*da217e55Szrj for (i = partLen; i + 63 < inputLen; i += 64)
281*da217e55Szrj MD5Transform (&context->A, &input[i]);
282*da217e55Szrj
283*da217e55Szrj idx = 0;
284*da217e55Szrj }
285*da217e55Szrj else
286*da217e55Szrj i = 0;
287*da217e55Szrj
288*da217e55Szrj /* Buffer remaining input */
289*da217e55Szrj memcpy (&((char *)context->data)[idx], (const void *)&input[i],
290*da217e55Szrj inputLen-i);
291*da217e55Szrj }
292*da217e55Szrj
293*da217e55Szrj /*
294*da217e55Szrj * MD5 padding. Adds padding followed by original length.
295*da217e55Szrj */
296*da217e55Szrj static void
MD5Pad(MD5_CTX * context)297*da217e55Szrj MD5Pad (MD5_CTX *context)
298*da217e55Szrj {
299*da217e55Szrj unsigned char bits[8];
300*da217e55Szrj unsigned int idx, padLen;
301*da217e55Szrj
302*da217e55Szrj /* Save number of bits */
303*da217e55Szrj _md5_Encode (bits, &context->Nl, 8);
304*da217e55Szrj
305*da217e55Szrj /* Pad out to 56 mod 64. */
306*da217e55Szrj idx = (unsigned int)((context->Nl >> 3) & 0x3f);
307*da217e55Szrj padLen = (idx < 56) ? (56 - idx) : (120 - idx);
308*da217e55Szrj MD5Update (context, _md5_PADDING, padLen);
309*da217e55Szrj
310*da217e55Szrj /* Append length (before padding) */
311*da217e55Szrj MD5Update (context, bits, 8);
312*da217e55Szrj }
313*da217e55Szrj
314*da217e55Szrj /*
315*da217e55Szrj * MD5 finalization. Ends an MD5 message-digest operation, writing the
316*da217e55Szrj * the message digest and zeroizing the context.
317*da217e55Szrj */
318*da217e55Szrj static void
MD5Final(unsigned char digest[16],MD5_CTX * context)319*da217e55Szrj MD5Final (unsigned char digest[16], MD5_CTX *context)
320*da217e55Szrj {
321*da217e55Szrj /* Do padding. */
322*da217e55Szrj MD5Pad (context);
323*da217e55Szrj
324*da217e55Szrj /* Store state in digest */
325*da217e55Szrj _md5_Encode (digest, &context->A, 16);
326*da217e55Szrj
327*da217e55Szrj /* Zeroize sensitive information. */
328*da217e55Szrj memset ((void *)context, 0, sizeof (*context));
329*da217e55Szrj }
330