2 * Copyright (c) 2003 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
37 static char itoa64[] = /* 0 ... 63 => ascii - 64 */
38 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
41 _crypt_to64(char *s, uint32_t v, int n)
44 *s++ = itoa64[v&0x3f];
50 crypt_md5(const char *pw, const char *salt)
56 uint8_t final[MD5_SIZE];
57 static const char *sp, *ep;
58 static char passwd[120], *p;
59 static const char *magic = "$1$";
61 /* Refine the Salt first */
64 /* If it starts with the magic string, then skip that */
65 if(!strncmp(sp, magic, strlen(magic)))
68 /* It stops at the first '$', max 8 chars */
69 for(ep = sp; *ep && *ep != '$' && ep < (sp + 8); ep++)
72 /* get the length of the true salt */
77 /* The password first, since that is what is most unknown */
78 MD5Update(&ctx, (const uint8_t *)pw, strlen(pw));
80 /* Then our magic string */
81 MD5Update(&ctx, (const uint8_t *)magic, strlen(magic));
83 /* Then the raw salt */
84 MD5Update(&ctx, (const uint8_t *)sp, (uint32_t)sl);
86 /* Then just as many characters of the MD5(pw,salt,pw) */
88 MD5Update(&ctx1, (const uint8_t *)pw, strlen(pw));
89 MD5Update(&ctx1, (const uint8_t *)sp, (uint32_t)sl);
90 MD5Update(&ctx1, (const uint8_t *)pw, strlen(pw));
91 MD5Final(final, &ctx1);
92 for(pl = (int)strlen(pw); pl > 0; pl -= MD5_SIZE)
93 MD5Update(&ctx, (const uint8_t *)final,
94 (uint32_t)(pl > MD5_SIZE ? MD5_SIZE : pl));
96 /* Don't leave anything around in vm they could use. */
97 memset(final, 0, sizeof(final));
99 /* Then something really weird... */
100 for (i = strlen(pw); i; i >>= 1)
102 MD5Update(&ctx, (const uint8_t *)final, 1);
104 MD5Update(&ctx, (const uint8_t *)pw, 1);
106 /* Now make the output string */
107 strcpy(passwd, magic);
108 strncat(passwd, sp, (uint32_t)sl);
111 MD5Final(final, &ctx);
114 * and now, just to make sure things don't run too fast
115 * On a 60 Mhz Pentium this takes 34 msec, so you would
116 * need 30 seconds to build a 1000 entry dictionary...
118 for(i = 0; i < 1000; i++) {
121 MD5Update(&ctx1, (const uint8_t *)pw, strlen(pw));
123 MD5Update(&ctx1, (const uint8_t *)final, MD5_SIZE);
126 MD5Update(&ctx1, (const uint8_t *)sp, (uint32_t)sl);
129 MD5Update(&ctx1, (const uint8_t *)pw, strlen(pw));
132 MD5Update(&ctx1, (const uint8_t *)final, MD5_SIZE);
134 MD5Update(&ctx1, (const uint8_t *)pw, strlen(pw));
135 MD5Final(final, &ctx1);
138 p = passwd + strlen(passwd);
140 l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
141 _crypt_to64(p, l, 4); p += 4;
142 l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
143 _crypt_to64(p, l, 4); p += 4;
144 l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
145 _crypt_to64(p, l, 4); p += 4;
146 l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
147 _crypt_to64(p, l, 4); p += 4;
148 l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
149 _crypt_to64(p, l, 4); p += 4;
151 _crypt_to64(p, l, 2); p += 2;
154 /* Don't leave anything around in vm they could use. */
155 memset(final, 0, sizeof(final));
163 int main(int argc, char *argv[])
165 puts(crypt_md5(argv[1], argv[2]));