1/*-
2 * Copyright (c) 2003 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <sys/types.h>
31
32#include <err.h>
33#include <md5.h>
34#include <stdio.h>
35#include <string.h>
36#include <unistd.h>
37
38#include "crypt.h"
39
40/*
41 * UNIX password
42 */
43
44char *
45crypt_md5(const char *pw, const char *salt)
46{
47	MD5_CTX	ctx,ctx1;
48	unsigned long l;
49	int sl, pl;
50	u_int i;
51	u_char final[MD5_SIZE];
52	static const char *sp, *ep;
53	static char passwd[120], *p;
54	static const char *magic = "$1$";
55
56	/* Refine the Salt first */
57	sp = salt;
58
59	/* If it starts with the magic string, then skip that */
60	if(!strncmp(sp, magic, strlen(magic)))
61		sp += strlen(magic);
62
63	/* It stops at the first '$', max 8 chars */
64	for(ep = sp; *ep && *ep != '$' && ep < (sp + 8); ep++)
65		continue;
66
67	/* get the length of the true salt */
68	sl = ep - sp;
69
70	MD5Init(&ctx);
71
72	/* The password first, since that is what is most unknown */
73	MD5Update(&ctx, (const u_char *)pw, strlen(pw));
74
75	/* Then our magic string */
76	MD5Update(&ctx, (const u_char *)magic, strlen(magic));
77
78	/* Then the raw salt */
79	MD5Update(&ctx, (const u_char *)sp, (u_int)sl);
80
81	/* Then just as many characters of the MD5(pw,salt,pw) */
82	MD5Init(&ctx1);
83	MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
84	MD5Update(&ctx1, (const u_char *)sp, (u_int)sl);
85	MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
86	MD5Final(final, &ctx1);
87	for(pl = (int)strlen(pw); pl > 0; pl -= MD5_SIZE)
88		MD5Update(&ctx, (const u_char *)final,
89		    (u_int)(pl > MD5_SIZE ? MD5_SIZE : pl));
90
91	/* Don't leave anything around in vm they could use. */
92	memset(final, 0, sizeof(final));
93
94	/* Then something really weird... */
95	for (i = strlen(pw); i; i >>= 1)
96		if(i & 1)
97		    MD5Update(&ctx, (const u_char *)final, 1);
98		else
99		    MD5Update(&ctx, (const u_char *)pw, 1);
100
101	/* Now make the output string */
102	strcpy(passwd, magic);
103	strncat(passwd, sp, (u_int)sl);
104	strcat(passwd, "$");
105
106	MD5Final(final, &ctx);
107
108	/*
109	 * and now, just to make sure things don't run too fast
110	 * On a 60 Mhz Pentium this takes 34 msec, so you would
111	 * need 30 seconds to build a 1000 entry dictionary...
112	 */
113	for(i = 0; i < 1000; i++) {
114		MD5Init(&ctx1);
115		if(i & 1)
116			MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
117		else
118			MD5Update(&ctx1, (const u_char *)final, MD5_SIZE);
119
120		if(i % 3)
121			MD5Update(&ctx1, (const u_char *)sp, (u_int)sl);
122
123		if(i % 7)
124			MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
125
126		if(i & 1)
127			MD5Update(&ctx1, (const u_char *)final, MD5_SIZE);
128		else
129			MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
130		MD5Final(final, &ctx1);
131	}
132
133	p = passwd + strlen(passwd);
134
135	l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
136	_crypt_to64(p, l, 4); p += 4;
137	l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
138	_crypt_to64(p, l, 4); p += 4;
139	l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
140	_crypt_to64(p, l, 4); p += 4;
141	l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
142	_crypt_to64(p, l, 4); p += 4;
143	l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
144	_crypt_to64(p, l, 4); p += 4;
145	l = final[11];
146	_crypt_to64(p, l, 2); p += 2;
147	*p = '\0';
148
149	/* Don't leave anything around in vm they could use. */
150	memset(final, 0, sizeof(final));
151
152	return (passwd);
153}
154