2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dknet.dk> wrote this file. As long as you retain this
5 * notice you can do whatever you want with this stuff. If we meet some
6 * day, and you think this stuff is worth it, you can buy me a beer in
7 * return. Poul-Henning Kamp
8 * ----------------------------------------------------------------------------
13 #if defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT)
14 #include <sys/types.h>
18 #include <openssl/md5.h>
20 /* 0 ... 63 => ascii - 64 */
21 static unsigned char itoa64[] =
22 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
24 static char *magic = "$1$";
27 to64(unsigned long v, int n)
35 memset(buf, '\0', sizeof(buf));
37 *s++ = itoa64[v&0x3f];
45 is_md5_salt(const char *salt)
47 return (strncmp(salt, magic, strlen(magic)) == 0);
51 md5_crypt(const char *pw, const char *salt)
53 static char passwd[120], salt_copy[9], *p;
54 static const char *sp, *ep;
55 unsigned char final[16];
60 /* Refine the Salt first */
63 /* If it starts with the magic string, then skip that */
64 if(strncmp(sp, magic, strlen(magic)) == 0)
67 /* It stops at the first '$', max 8 chars */
68 for (ep = sp; *ep != '$'; ep++) {
69 if (*ep == '\0' || ep >= (sp + 8))
73 /* get the length of the true salt */
77 memcpy(salt_copy, sp, sl);
82 /* The password first, since that is what is most unknown */
83 MD5_Update(&ctx, pw, strlen(pw));
85 /* Then our magic string */
86 MD5_Update(&ctx, magic, strlen(magic));
88 /* Then the raw salt */
89 MD5_Update(&ctx, sp, sl);
91 /* Then just as many characters of the MD5(pw, salt, pw) */
93 MD5_Update(&ctx1, pw, strlen(pw));
94 MD5_Update(&ctx1, sp, sl);
95 MD5_Update(&ctx1, pw, strlen(pw));
96 MD5_Final(final, &ctx1);
98 for(pl = strlen(pw); pl > 0; pl -= 16)
99 MD5_Update(&ctx, final, pl > 16 ? 16 : pl);
101 /* Don't leave anything around in vm they could use. */
102 memset(final, '\0', sizeof final);
104 /* Then something really weird... */
105 for (j = 0, i = strlen(pw); i != 0; i >>= 1)
107 MD5_Update(&ctx, final + j, 1);
109 MD5_Update(&ctx, pw + j, 1);
111 /* Now make the output string */
112 snprintf(passwd, sizeof(passwd), "%s%s$", magic, salt_copy);
114 MD5_Final(final, &ctx);
117 * and now, just to make sure things don't run too fast
118 * On a 60 Mhz Pentium this takes 34 msec, so you would
119 * need 30 seconds to build a 1000 entry dictionary...
121 for(i = 0; i < 1000; i++) {
124 MD5_Update(&ctx1, pw, strlen(pw));
126 MD5_Update(&ctx1, final, 16);
129 MD5_Update(&ctx1, sp, sl);
132 MD5_Update(&ctx1, pw, strlen(pw));
135 MD5_Update(&ctx1, final, 16);
137 MD5_Update(&ctx1, pw, strlen(pw));
139 MD5_Final(final, &ctx1);
142 p = passwd + strlen(passwd);
144 l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
145 strlcat(passwd, to64(l, 4), sizeof(passwd));
146 l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
147 strlcat(passwd, to64(l, 4), sizeof(passwd));
148 l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
149 strlcat(passwd, to64(l, 4), sizeof(passwd));
150 l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
151 strlcat(passwd, to64(l, 4), sizeof(passwd));
152 l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
153 strlcat(passwd, to64(l, 4), sizeof(passwd));
155 strlcat(passwd, to64(l, 2), sizeof(passwd));
157 /* Don't leave anything around in vm they could use. */
158 memset(final, 0, sizeof(final));
159 memset(salt_copy, 0, sizeof(salt_copy));
160 memset(&ctx, 0, sizeof(ctx));
161 memset(&ctx1, 0, sizeof(ctx1));
167 #endif /* defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT) */