xref: /netbsd-src/lib/libcrypt/md5crypt.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /*	$NetBSD: md5crypt.c,v 1.7 2003/08/06 08:37:19 jdolecek Exp $	*/
2 
3 /*
4  * ----------------------------------------------------------------------------
5  * "THE BEER-WARE LICENSE" (Revision 42):
6  * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
7  * can do whatever you want with this stuff. If we meet some day, and you think
8  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
9  * ----------------------------------------------------------------------------
10  *
11  * from FreeBSD: crypt.c,v 1.5 1996/10/14 08:34:02 phk Exp
12  * via OpenBSD: md5crypt.c,v 1.9 1997/07/23 20:58:27 kstailey Exp
13  *
14  */
15 
16 #include <sys/cdefs.h>
17 #if !defined(lint)
18 __RCSID("$NetBSD: md5crypt.c,v 1.7 2003/08/06 08:37:19 jdolecek Exp $");
19 #endif /* not lint */
20 
21 /*
22  * NOTE: We are also built for inclusion in libcrypto; when built for that
23  * environment, use the libcrypto versions of the MD5 routines, so save
24  * having to pull two versions into the same program.
25  */
26 
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <string.h>
30 #ifdef libcrypto
31 #include <openssl/md5.h>
32 #else
33 #include <md5.h>
34 #endif
35 #include <string.h>
36 
37 #define MD5_MAGIC	"$1$"
38 #define MD5_MAGIC_LEN	3
39 
40 char	*__md5crypt(const char *pw, const char *salt);	/* XXX */
41 
42 static const unsigned char itoa64[] =		/* 0 ... 63 => ascii - 64 */
43 	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
44 
45 #ifdef libcrypto
46 #define	INIT(x)			MD5_Init((x))
47 #define	UPDATE(x, b, l)		MD5_Update((x), (b), (l))
48 #define	FINAL(v, x)		MD5_Final((v), (x))
49 #else
50 #define	INIT(x)			MD5Init((x))
51 #define	UPDATE(x, b, l)		MD5Update((x), (b), (l))
52 #define	FINAL(v, x)		MD5Final((v), (x))
53 #endif
54 
55 static void
56 to64(char *s, u_int32_t v, int n)
57 {
58 
59 	while (--n >= 0) {
60 		*s++ = itoa64[v & 0x3f];
61 		v >>= 6;
62 	}
63 }
64 
65 /*
66  * MD5 password encryption.
67  */
68 char *
69 __md5crypt(const char *pw, const char *salt)
70 {
71 	static char passwd[120], *p;
72 	const char *sp, *ep;
73 	unsigned char final[16];
74 	unsigned int i, sl, pwl;
75 	MD5_CTX	ctx, ctx1;
76 	u_int32_t l;
77 	int pl;
78 
79 	pwl = strlen(pw);
80 
81 	/* Refine the salt first */
82 	sp = salt;
83 
84 	/* If it starts with the magic string, then skip that */
85 	if (strncmp(sp, MD5_MAGIC, MD5_MAGIC_LEN) == 0)
86 		sp += MD5_MAGIC_LEN;
87 
88 	/* It stops at the first '$', max 8 chars */
89 	for (ep = sp; *ep != '\0' && *ep != '$' && ep < (sp + 8); ep++)
90 		continue;
91 
92 	/* get the length of the true salt */
93 	sl = ep - sp;
94 
95 	INIT(&ctx);
96 
97 	/* The password first, since that is what is most unknown */
98 	UPDATE(&ctx, (const unsigned char *)pw, pwl);
99 
100 	/* Then our magic string */
101 	UPDATE(&ctx, (const unsigned char *)MD5_MAGIC, MD5_MAGIC_LEN);
102 
103 	/* Then the raw salt */
104 	UPDATE(&ctx, (const unsigned char *)sp, sl);
105 
106 	/* Then just as many characters of the MD5(pw,salt,pw) */
107 	INIT(&ctx1);
108 	UPDATE(&ctx1, (const unsigned char *)pw, pwl);
109 	UPDATE(&ctx1, (const unsigned char *)sp, sl);
110 	UPDATE(&ctx1, (const unsigned char *)pw, pwl);
111 	FINAL(final, &ctx1);
112 
113 	for (pl = pwl; pl > 0; pl -= 16)
114 		UPDATE(&ctx, final, (unsigned int)(pl > 16 ? 16 : pl));
115 
116 	/* Don't leave anything around in vm they could use. */
117 	memset(final, 0, sizeof(final));
118 
119 	/* Then something really weird... */
120 	for (i = pwl; i != 0; i >>= 1)
121 		if ((i & 1) != 0)
122 		    UPDATE(&ctx, final, 1);
123 		else
124 		    UPDATE(&ctx, (const unsigned char *)pw, 1);
125 
126 	/* Now make the output string */
127 	memcpy(passwd, MD5_MAGIC, MD5_MAGIC_LEN);
128 	strlcpy(passwd + MD5_MAGIC_LEN, sp, sl + 1);
129 	strlcat(passwd, "$", sizeof(passwd));
130 
131 	FINAL(final, &ctx);
132 
133 	/*
134 	 * And now, just to make sure things don't run too fast. On a 60 MHz
135 	 * Pentium this takes 34 msec, so you would need 30 seconds to build
136 	 * a 1000 entry dictionary...
137 	 */
138 	for (i = 0; i < 1000; i++) {
139 		INIT(&ctx1);
140 
141 		if ((i & 1) != 0)
142 			UPDATE(&ctx1, (const unsigned char *)pw, pwl);
143 		else
144 			UPDATE(&ctx1, final, 16);
145 
146 		if ((i % 3) != 0)
147 			UPDATE(&ctx1, (const unsigned char *)sp, sl);
148 
149 		if ((i % 7) != 0)
150 			UPDATE(&ctx1, (const unsigned char *)pw, pwl);
151 
152 		if ((i & 1) != 0)
153 			UPDATE(&ctx1, final, 16);
154 		else
155 			UPDATE(&ctx1, (const unsigned char *)pw, pwl);
156 
157 		FINAL(final, &ctx1);
158 	}
159 
160 	p = passwd + sl + MD5_MAGIC_LEN + 1;
161 
162 	l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4;
163 	l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4;
164 	l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4;
165 	l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4;
166 	l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4;
167 	l =		       final[11]		; to64(p,l,2); p += 2;
168 	*p = '\0';
169 
170 	/* Don't leave anything around in vm they could use. */
171 	memset(final, 0, sizeof(final));
172 	return (passwd);
173 }
174