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