xref: /freebsd/lib/libcrypt/crypt-md5.c (revision 1f4aad4d1c613bd7ecdddf758e7beeda53b1b57a)
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $FreeBSD$
10  *
11  */
12 
13 #if defined(LIBC_SCCS) && !defined(lint)
14 static const char rcsid[] = \
15 "$FreeBSD$";
16 #endif /* LIBC_SCCS and not lint */
17 
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <md5.h>
22 #include <err.h>
23 #include "crypt.h"
24 
25 #ifdef __PIC__
26 #include <dlfcn.h>
27 
28 #define	MD5Init(ctx)			dl_MD5Init(ctx)
29 #define	MD5Update(ctx, data, len)	dl_MD5Update(ctx, data, len)
30 #define	MD5Final(dgst, ctx)		dl_MD5Final(dgst, ctx)
31 
32 static void (*dl_MD5Init)(MD5_CTX *);
33 static void (*dl_MD5Update)(MD5_CTX *, const unsigned char *, unsigned int);
34 static void (*dl_MD5Final)(unsigned char digest[16], MD5_CTX *);
35 #endif
36 
37 /*
38  * UNIX password
39  */
40 
41 char *
42 crypt_md5(pw, salt)
43 	const char *pw;
44 	const char *salt;
45 {
46 	static char	*magic = "$1$";	/*
47 					 * This string is magic for
48 					 * this algorithm.  Having
49 					 * it this way, we can get
50 					 * get better later on
51 					 */
52 	static char     passwd[120], *p;
53 	static const char *sp,*ep;
54 	unsigned char	final[MD5_SIZE];
55 	int sl,pl,i;
56 	MD5_CTX	ctx,ctx1;
57 	unsigned long l;
58 #ifdef __PIC__
59 	void *libmd;
60 #endif
61 
62 	/* Refine the Salt first */
63 	sp = salt;
64 
65 	/* If it starts with the magic string, then skip that */
66 	if(!strncmp(sp,magic,strlen(magic)))
67 		sp += strlen(magic);
68 
69 	/* It stops at the first '$', max 8 chars */
70 	for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++)
71 		continue;
72 
73 	/* get the length of the true salt */
74 	sl = ep - sp;
75 
76 #ifdef __PIC__
77 	libmd = dlopen("libmd.so", RTLD_NOW);
78 	if (libmd == NULL)
79 		return NULL;
80 	dl_MD5Init = dlsym(libmd, "MD5Init");
81 	if (dl_MD5Init == NULL) {
82 		warnx("libcrypt-md5: looking for MD5Init: %s\n", dlerror());
83 		dlclose(libmd);
84 		return NULL;
85 	}
86 	dl_MD5Update = dlsym(libmd, "MD5Update");
87 	if (dl_MD5Update == NULL) {
88 		warnx("libcrypt-md5: looking for MD5Update: %s\n", dlerror());
89 		dlclose(libmd);
90 		return NULL;
91 	}
92 	dl_MD5Final = dlsym(libmd, "MD5Final");
93 	if (dl_MD5Final == NULL) {
94 		warnx("libcrypt-md5: looking for MD5Final: %s\n", dlerror());
95 		dlclose(libmd);
96 		return NULL;
97 	}
98 #endif
99 	MD5Init(&ctx);
100 
101 	/* The password first, since that is what is most unknown */
102 	MD5Update(&ctx,pw,strlen(pw));
103 
104 	/* Then our magic string */
105 	MD5Update(&ctx,magic,strlen(magic));
106 
107 	/* Then the raw salt */
108 	MD5Update(&ctx,sp,sl);
109 
110 	/* Then just as many characters of the MD5(pw,salt,pw) */
111 	MD5Init(&ctx1);
112 	MD5Update(&ctx1,pw,strlen(pw));
113 	MD5Update(&ctx1,sp,sl);
114 	MD5Update(&ctx1,pw,strlen(pw));
115 	MD5Final(final,&ctx1);
116 	for(pl = strlen(pw); pl > 0; pl -= MD5_SIZE)
117 		MD5Update(&ctx,final,pl>MD5_SIZE ? MD5_SIZE : pl);
118 
119 	/* Don't leave anything around in vm they could use. */
120 	memset(final,0,sizeof final);
121 
122 	/* Then something really weird... */
123 	for (i = strlen(pw); i ; i >>= 1)
124 		if(i&1)
125 		    MD5Update(&ctx, final, 1);
126 		else
127 		    MD5Update(&ctx, pw, 1);
128 
129 	/* Now make the output string */
130 	strcpy(passwd,magic);
131 	strncat(passwd,sp,sl);
132 	strcat(passwd,"$");
133 
134 	MD5Final(final,&ctx);
135 
136 	/*
137 	 * and now, just to make sure things don't run too fast
138 	 * On a 60 Mhz Pentium this takes 34 msec, so you would
139 	 * need 30 seconds to build a 1000 entry dictionary...
140 	 */
141 	for(i=0;i<1000;i++) {
142 		MD5Init(&ctx1);
143 		if(i & 1)
144 			MD5Update(&ctx1,pw,strlen(pw));
145 		else
146 			MD5Update(&ctx1,final,MD5_SIZE);
147 
148 		if(i % 3)
149 			MD5Update(&ctx1,sp,sl);
150 
151 		if(i % 7)
152 			MD5Update(&ctx1,pw,strlen(pw));
153 
154 		if(i & 1)
155 			MD5Update(&ctx1,final,MD5_SIZE);
156 		else
157 			MD5Update(&ctx1,pw,strlen(pw));
158 		MD5Final(final,&ctx1);
159 	}
160 
161 #ifdef __PIC__
162 	dlclose(libmd);
163 #endif
164 	p = passwd + strlen(passwd);
165 
166 	l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
167 	_crypt_to64(p,l,4); p += 4;
168 	l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
169 	_crypt_to64(p,l,4); p += 4;
170 	l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
171 	_crypt_to64(p,l,4); p += 4;
172 	l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
173 	_crypt_to64(p,l,4); p += 4;
174 	l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
175 	_crypt_to64(p,l,4); p += 4;
176 	l =                    final[11]                ;
177 	_crypt_to64(p,l,2); p += 2;
178 	*p = '\0';
179 
180 	/* Don't leave anything around in vm they could use. */
181 	memset(final,0,sizeof final);
182 
183 	return passwd;
184 }
185 
186