xref: /netbsd-src/lib/libcrypt/md5crypt.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: md5crypt.c,v 1.4 2001/09/10 12:33:25 ad 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.4 2001/09/10 12:33:25 ad Exp $");
19 #endif /* not lint */
20 
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <md5.h>
25 #include <string.h>
26 
27 #define MD5_MAGIC	"$1$"
28 #define MD5_MAGIC_LEN	3
29 
30 char	*__md5crypt(const char *pw, const char *salt);	/* XXX */
31 
32 static unsigned char itoa64[] =		/* 0 ... 63 => ascii - 64 */
33 	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
34 
35 static void to64(char *, u_int32_t, int);
36 
37 static void
38 to64(char *s, u_int32_t v, int n)
39 {
40 
41 	while (--n >= 0) {
42 		*s++ = itoa64[v & 0x3f];
43 		v >>= 6;
44 	}
45 }
46 
47 /*
48  * MD5 password encryption.
49  */
50 char *
51 __md5crypt(const char *pw, const char *salt)
52 {
53 	static char passwd[120], *p;
54 	const char *sp, *ep;
55 	unsigned char final[16];
56 	unsigned int i, sl, pwl;
57 	MD5_CTX	ctx, ctx1;
58 	u_int32_t l;
59 	int pl;
60 
61 	pwl = strlen(pw);
62 
63 	/* Refine the salt first */
64 	sp = salt;
65 
66 	/* If it starts with the magic string, then skip that */
67 	if (strncmp(sp, MD5_MAGIC, MD5_MAGIC_LEN) == 0)
68 		sp += MD5_MAGIC_LEN;
69 
70 	/* It stops at the first '$', max 8 chars */
71 	for (ep = sp; *ep != '\0' && *ep != '$' && ep < (sp + 8); ep++)
72 		continue;
73 
74 	/* get the length of the true salt */
75 	sl = ep - sp;
76 
77 	MD5Init(&ctx);
78 
79 	/* The password first, since that is what is most unknown */
80 	MD5Update(&ctx, (const unsigned char *)pw, pwl);
81 
82 	/* Then our magic string */
83 	MD5Update(&ctx, (const unsigned char *)MD5_MAGIC, MD5_MAGIC_LEN);
84 
85 	/* Then the raw salt */
86 	MD5Update(&ctx, (const unsigned char *)sp, sl);
87 
88 	/* Then just as many characters of the MD5(pw,salt,pw) */
89 	MD5Init(&ctx1);
90 	MD5Update(&ctx1, (const unsigned char *)pw, pwl);
91 	MD5Update(&ctx1, (const unsigned char *)sp, sl);
92 	MD5Update(&ctx1, (const unsigned char *)pw, pwl);
93 	MD5Final(final, &ctx1);
94 
95 	for (pl = pwl; pl > 0; pl -= 16)
96 		MD5Update(&ctx, final, (unsigned int)(pl > 16 ? 16 : pl));
97 
98 	/* Don't leave anything around in vm they could use. */
99 	memset(final, 0, sizeof(final));
100 
101 	/* Then something really weird... */
102 	for (i = pwl; i != 0; i >>= 1)
103 		if ((i & 1) != 0)
104 		    MD5Update(&ctx, final, 1);
105 		else
106 		    MD5Update(&ctx, (const unsigned char *)pw, 1);
107 
108 	/* Now make the output string */
109 	memcpy(passwd, MD5_MAGIC, MD5_MAGIC_LEN);
110 	strlcpy(passwd + MD5_MAGIC_LEN, sp, sl + 1);
111 	strcat(passwd, "$");
112 
113 	MD5Final(final, &ctx);
114 
115 	/*
116 	 * And now, just to make sure things don't run too fast. On a 60 MHz
117 	 * Pentium this takes 34 msec, so you would need 30 seconds to build
118 	 * a 1000 entry dictionary...
119 	 */
120 	for (i = 0; i < 1000; i++) {
121 		MD5Init(&ctx1);
122 
123 		if ((i & 1) != 0)
124 			MD5Update(&ctx1, (const unsigned char *)pw, pwl);
125 		else
126 			MD5Update(&ctx1, final, 16);
127 
128 		if ((i % 3) != 0)
129 			MD5Update(&ctx1, (const unsigned char *)sp, sl);
130 
131 		if ((i % 7) != 0)
132 			MD5Update(&ctx1, (const unsigned char *)pw, pwl);
133 
134 		if ((i & 1) != 0)
135 			MD5Update(&ctx1, final, 16);
136 		else
137 			MD5Update(&ctx1, (const unsigned char *)pw, pwl);
138 
139 		MD5Final(final, &ctx1);
140 	}
141 
142 	p = passwd + sl + MD5_MAGIC_LEN + 1;
143 
144 	l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4;
145 	l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4;
146 	l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4;
147 	l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4;
148 	l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4;
149 	l =		       final[11]		; to64(p,l,2); p += 2;
150 	*p = '\0';
151 
152 	/* Don't leave anything around in vm they could use. */
153 	memset(final, 0, sizeof(final));
154 	return (passwd);
155 }
156