2 * This code implements the MD5 message-digest algorithm.
3 * The algorithm is due to Ron Rivest. This code was
4 * written by Colin Plumb in 1993, no copyright is claimed.
5 * This code is in the public domain; do with it what you wish.
7 * ----------------------------------------------------------------------------
8 * The md5_crypt() function was taken from freeBSD's libcrypt and contains
10 * "THE BEER-WARE LICENSE" (Revision 42):
11 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
12 * can do whatever you want with this stuff. If we meet some day, and you think
13 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
15 * $FreeBSD: src/lib/libcrypt/crypt.c,v 1.7.2.1 1999/08/29 14:56:33 peter Exp $
17 * ----------------------------------------------------------------------------
18 * On April 19th, 2001 md5_crypt() was modified to make it reentrant
19 * by Erik Andersen <andersen@uclibc.org>
22 #include "../globals.h"
23 #include "../oscam-string.h"
27 #if !defined(WITH_SSL) && !defined(WITH_LIBCRYPTO)
30 #define byteReverse(a, b)
33 * Note: This code is harmless on little-endian machines.
34 * The ifdefs are just a small optimization
36 static void byteReverse(unsigned char *buf
, unsigned int longs
)
41 t
= (uint32_t)((unsigned int)buf
[3] << 8 | buf
[2]) << 16 |
42 ((unsigned int)buf
[1] << 8 | buf
[0]);
50 /* The four core functions - F1 is optimized somewhat */
52 /* #define F1(x, y, z) (x & y | ~x & z) */
53 #define F1(x, y, z) (z ^ (x & (y ^ z)))
54 #define F2(x, y, z) F1(z, x, y)
55 #define F3(x, y, z) (x ^ y ^ z)
56 #define F4(x, y, z) (y ^ (x | ~z))
58 /* This is the central step in the MD5 algorithm. */
59 #define MD5STEP(f, w, x, y, z, data, s) \
60 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
63 * The core of the MD5 algorithm, this alters an existing MD5 hash to
64 * reflect the addition of 16 longwords of new data. MD5_Update blocks
65 * the data and converts bytes into longwords for this routine.
67 static void MD5_Transform(uint32_t buf
[4], uint32_t in
[16])
74 MD5STEP(F1
, a
, b
, c
, d
, in
[ 0] + 0xd76aa478, 7);
75 MD5STEP(F1
, d
, a
, b
, c
, in
[ 1] + 0xe8c7b756, 12);
76 MD5STEP(F1
, c
, d
, a
, b
, in
[ 2] + 0x242070db, 17);
77 MD5STEP(F1
, b
, c
, d
, a
, in
[ 3] + 0xc1bdceee, 22);
78 MD5STEP(F1
, a
, b
, c
, d
, in
[ 4] + 0xf57c0faf, 7);
79 MD5STEP(F1
, d
, a
, b
, c
, in
[ 5] + 0x4787c62a, 12);
80 MD5STEP(F1
, c
, d
, a
, b
, in
[ 6] + 0xa8304613, 17);
81 MD5STEP(F1
, b
, c
, d
, a
, in
[ 7] + 0xfd469501, 22);
82 MD5STEP(F1
, a
, b
, c
, d
, in
[ 8] + 0x698098d8, 7);
83 MD5STEP(F1
, d
, a
, b
, c
, in
[ 9] + 0x8b44f7af, 12);
84 MD5STEP(F1
, c
, d
, a
, b
, in
[10] + 0xffff5bb1, 17);
85 MD5STEP(F1
, b
, c
, d
, a
, in
[11] + 0x895cd7be, 22);
86 MD5STEP(F1
, a
, b
, c
, d
, in
[12] + 0x6b901122, 7);
87 MD5STEP(F1
, d
, a
, b
, c
, in
[13] + 0xfd987193, 12);
88 MD5STEP(F1
, c
, d
, a
, b
, in
[14] + 0xa679438e, 17);
89 MD5STEP(F1
, b
, c
, d
, a
, in
[15] + 0x49b40821, 22);
91 MD5STEP(F2
, a
, b
, c
, d
, in
[ 1] + 0xf61e2562, 5);
92 MD5STEP(F2
, d
, a
, b
, c
, in
[ 6] + 0xc040b340, 9);
93 MD5STEP(F2
, c
, d
, a
, b
, in
[11] + 0x265e5a51, 14);
94 MD5STEP(F2
, b
, c
, d
, a
, in
[ 0] + 0xe9b6c7aa, 20);
95 MD5STEP(F2
, a
, b
, c
, d
, in
[ 5] + 0xd62f105d, 5);
96 MD5STEP(F2
, d
, a
, b
, c
, in
[10] + 0x02441453, 9);
97 MD5STEP(F2
, c
, d
, a
, b
, in
[15] + 0xd8a1e681, 14);
98 MD5STEP(F2
, b
, c
, d
, a
, in
[ 4] + 0xe7d3fbc8, 20);
99 MD5STEP(F2
, a
, b
, c
, d
, in
[ 9] + 0x21e1cde6, 5);
100 MD5STEP(F2
, d
, a
, b
, c
, in
[14] + 0xc33707d6, 9);
101 MD5STEP(F2
, c
, d
, a
, b
, in
[ 3] + 0xf4d50d87, 14);
102 MD5STEP(F2
, b
, c
, d
, a
, in
[ 8] + 0x455a14ed, 20);
103 MD5STEP(F2
, a
, b
, c
, d
, in
[13] + 0xa9e3e905, 5);
104 MD5STEP(F2
, d
, a
, b
, c
, in
[ 2] + 0xfcefa3f8, 9);
105 MD5STEP(F2
, c
, d
, a
, b
, in
[ 7] + 0x676f02d9, 14);
106 MD5STEP(F2
, b
, c
, d
, a
, in
[12] + 0x8d2a4c8a, 20);
108 MD5STEP(F3
, a
, b
, c
, d
, in
[ 5] + 0xfffa3942, 4);
109 MD5STEP(F3
, d
, a
, b
, c
, in
[ 8] + 0x8771f681, 11);
110 MD5STEP(F3
, c
, d
, a
, b
, in
[11] + 0x6d9d6122, 16);
111 MD5STEP(F3
, b
, c
, d
, a
, in
[14] + 0xfde5380c, 23);
112 MD5STEP(F3
, a
, b
, c
, d
, in
[ 1] + 0xa4beea44, 4);
113 MD5STEP(F3
, d
, a
, b
, c
, in
[ 4] + 0x4bdecfa9, 11);
114 MD5STEP(F3
, c
, d
, a
, b
, in
[ 7] + 0xf6bb4b60, 16);
115 MD5STEP(F3
, b
, c
, d
, a
, in
[10] + 0xbebfbc70, 23);
116 MD5STEP(F3
, a
, b
, c
, d
, in
[13] + 0x289b7ec6, 4);
117 MD5STEP(F3
, d
, a
, b
, c
, in
[ 0] + 0xeaa127fa, 11);
118 MD5STEP(F3
, c
, d
, a
, b
, in
[ 3] + 0xd4ef3085, 16);
119 MD5STEP(F3
, b
, c
, d
, a
, in
[ 6] + 0x04881d05, 23);
120 MD5STEP(F3
, a
, b
, c
, d
, in
[ 9] + 0xd9d4d039, 4);
121 MD5STEP(F3
, d
, a
, b
, c
, in
[12] + 0xe6db99e5, 11);
122 MD5STEP(F3
, c
, d
, a
, b
, in
[15] + 0x1fa27cf8, 16);
123 MD5STEP(F3
, b
, c
, d
, a
, in
[ 2] + 0xc4ac5665, 23);
125 MD5STEP(F4
, a
, b
, c
, d
, in
[ 0] + 0xf4292244, 6);
126 MD5STEP(F4
, d
, a
, b
, c
, in
[ 7] + 0x432aff97, 10);
127 MD5STEP(F4
, c
, d
, a
, b
, in
[14] + 0xab9423a7, 15);
128 MD5STEP(F4
, b
, c
, d
, a
, in
[ 5] + 0xfc93a039, 21);
129 MD5STEP(F4
, a
, b
, c
, d
, in
[12] + 0x655b59c3, 6);
130 MD5STEP(F4
, d
, a
, b
, c
, in
[ 3] + 0x8f0ccc92, 10);
131 MD5STEP(F4
, c
, d
, a
, b
, in
[10] + 0xffeff47d, 15);
132 MD5STEP(F4
, b
, c
, d
, a
, in
[ 1] + 0x85845dd1, 21);
133 MD5STEP(F4
, a
, b
, c
, d
, in
[ 8] + 0x6fa87e4f, 6);
134 MD5STEP(F4
, d
, a
, b
, c
, in
[15] + 0xfe2ce6e0, 10);
135 MD5STEP(F4
, c
, d
, a
, b
, in
[ 6] + 0xa3014314, 15);
136 MD5STEP(F4
, b
, c
, d
, a
, in
[13] + 0x4e0811a1, 21);
137 MD5STEP(F4
, a
, b
, c
, d
, in
[ 4] + 0xf7537e82, 6);
138 MD5STEP(F4
, d
, a
, b
, c
, in
[11] + 0xbd3af235, 10);
139 MD5STEP(F4
, c
, d
, a
, b
, in
[ 2] + 0x2ad7d2bb, 15);
140 MD5STEP(F4
, b
, c
, d
, a
, in
[ 9] + 0xeb86d391, 21);
149 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
150 * initialization constants.
152 void MD5_Init(MD5_CTX
*ctx
)
154 ctx
->buf
[0] = 0x67452301;
155 ctx
->buf
[1] = 0xefcdab89;
156 ctx
->buf
[2] = 0x98badcfe;
157 ctx
->buf
[3] = 0x10325476;
164 * Update context to reflect the concatenation of another buffer full
167 void MD5_Update(MD5_CTX
*ctx
, const unsigned char *buf
, unsigned int len
)
171 /* Update bitcount */
174 if((ctx
->bits
[0] = t
+ ((uint32_t) len
<< 3)) < t
)
175 { ctx
->bits
[1]++; } /* Carry from low to high */
176 ctx
->bits
[1] += len
>> 29;
178 t
= (t
>> 3) & 0x3f; /* Bytes already in shsInfo->data */
180 /* Handle any leading odd-sized chunks */
184 unsigned char *p
= ((unsigned char *)ctx
->in
) + t
;
192 byteReverse((unsigned char *)ctx
->in
, 16);
193 MD5_Transform(ctx
->buf
, ctx
->in
);
198 /* Process data in 64-byte chunks */
201 memcpy(ctx
->in
, buf
, 64);
202 byteReverse((unsigned char *)ctx
->in
, 16);
203 MD5_Transform(ctx
->buf
, ctx
->in
);
208 /* Handle any remaining bytes of data. */
209 memcpy(ctx
->in
, buf
, len
);
213 * Final wrapup - pad to 64-byte boundary with the bit pattern
214 * 1 0* (64-bit count of bits processed, MSB-first)
216 void MD5_Final(unsigned char digest
[MD5_DIGEST_LENGTH
], MD5_CTX
*ctx
)
221 /* Compute number of bytes mod 64 */
222 count
= (ctx
->bits
[0] >> 3) & 0x3F;
224 /* Set the first char of padding to 0x80. This is safe since there is
225 always at least one byte free */
226 p
= ((unsigned char *)ctx
->in
) + count
;
229 /* Bytes of padding needed to make 64 bytes */
230 count
= 64 - 1 - count
;
232 /* Pad out to 56 mod 64 */
235 /* Two lots of padding: Pad the first block to 64 bytes */
237 byteReverse((unsigned char *)ctx
->in
, 16);
238 MD5_Transform(ctx
->buf
, ctx
->in
);
240 /* Now fill the next block with 56 bytes */
241 memset(ctx
->in
, 0, 56);
245 /* Pad block to 56 bytes */
246 memset(p
, 0, count
- 8);
248 byteReverse((unsigned char *)ctx
->in
, 14);
250 /* Append length in bits and transform */
251 uint32_t *c
= ctx
->in
;
252 c
[14] = ctx
->bits
[0];
253 c
[15] = ctx
->bits
[1];
255 MD5_Transform(ctx
->buf
, ctx
->in
);
256 byteReverse((unsigned char *) ctx
->buf
, 4);
258 memcpy(digest
, ctx
->buf
, 16);
259 memset(ctx
, 0, sizeof(struct MD5Context
)); /* In case it's sensitive */
262 unsigned char *MD5(const unsigned char *input
, unsigned long len
, unsigned char *output
)
266 MD5_Update(&ctx
, input
, len
);
267 MD5_Final(output
, &ctx
);
268 memset(&ctx
, 0, sizeof(ctx
)); /* security consideration */
273 /* This string is magic for this algorithm. Having
274 it this way, we can get better later on */
275 static const char __md5__magic
[] = "$1$";
277 /* 0 ... 63 => ascii - 64 */
278 static const unsigned char __md5_itoa64
[] =
279 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
281 static void __md5_to64(char *s
, unsigned long v
, int n
)
285 *s
++ = __md5_itoa64
[v
& 0x3f];
293 * Use MD5 for what it is best at...
296 char *__md5_crypt(const char *pw
, const char *salt
, char *passwd
)
301 unsigned char final
[17]; /* final[16] exists only to aid in looping */
302 int sl
, pl
, i
, __md5__magic_len
, pw_len
;
306 /* Refine the Salt first */
309 /* If it starts with the magic string, then skip that */
310 __md5__magic_len
= cs_strlen(__md5__magic
);
311 if(!strncmp(sp
, __md5__magic
, __md5__magic_len
))
312 { sp
+= __md5__magic_len
; }
314 /* It stops at the first '$', max 8 chars */
315 for(ep
= sp
; *ep
&& *ep
!= '$' && ep
< (sp
+ 8); ep
++)
318 /* get the length of the true salt */
323 /* The password first, since that is what is most unknown */
324 pw_len
= cs_strlen(pw
);
325 MD5_Update(&ctx
, (const unsigned char *)pw
, pw_len
);
327 /* Then our magic string */
328 MD5_Update(&ctx
, (const unsigned char *)__md5__magic
, __md5__magic_len
);
330 /* Then the raw salt */
331 MD5_Update(&ctx
, (const unsigned char *)sp
, sl
);
333 /* Then just as many characters of the MD5(pw,salt,pw) */
335 MD5_Update(&ctx1
, (const unsigned char *)pw
, pw_len
);
336 MD5_Update(&ctx1
, (const unsigned char *)sp
, sl
);
337 MD5_Update(&ctx1
, (const unsigned char *)pw
, pw_len
);
338 MD5_Final(final
, &ctx1
);
339 for(pl
= pw_len
; pl
> 0; pl
-= 16)
340 { MD5_Update(&ctx
, (const unsigned char *)final
, pl
> 16 ? 16 : pl
); }
342 /* Don't leave anything around in vm they could use. */
343 memset(final
, 0, sizeof final
);
345 /* Then something really weird... */
346 for(i
= pw_len
; i
; i
>>= 1)
348 MD5_Update(&ctx
, ((i
& 1) ? final
: (const unsigned char *) pw
), 1);
351 /* Now make the output string */
352 strncpy(passwd
, __md5__magic
, 4); // This should be safe
353 strncat(passwd
, sp
, sl
);
356 MD5_Final(final
, &ctx
);
359 * and now, just to make sure things don't run too fast
360 * On a 60 Mhz Pentium this takes 34 msec, so you would
361 * need 30 seconds to build a 1000 entry dictionary...
363 for(i
= 0; i
< 1000; i
++)
367 { MD5_Update(&ctx1
, (const unsigned char *)pw
, pw_len
); }
369 { MD5_Update(&ctx1
, (const unsigned char *)final
, 16); }
372 { MD5_Update(&ctx1
, (const unsigned char *)sp
, sl
); }
375 { MD5_Update(&ctx1
, (const unsigned char *)pw
, pw_len
); }
378 { MD5_Update(&ctx1
, (const unsigned char *)final
, 16); }
380 { MD5_Update(&ctx1
, (const unsigned char *)pw
, pw_len
); }
381 MD5_Final(final
, &ctx1
);
384 p
= passwd
+ cs_strlen(passwd
);
386 final
[16] = final
[5];
387 for(i
= 0 ; i
< 5 ; i
++)
389 l
= (final
[i
] << 16) | (final
[i
+ 6] << 8) | final
[i
+ 12];
398 /* Don't leave anything around in vm they could use. */
399 memset(final
, 0, sizeof final
);