2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
47 crypt_md5(const char *pw, const char *salt, char *buffer)
53 u_char final[MD5_SIZE];
55 static const char *magic = "$1$";
57 /* If the salt starts with the magic string, skip that. */
58 if (!strncmp(salt, magic, strlen(magic)))
59 salt += strlen(magic);
61 /* It stops at the first '$', max 8 chars */
62 for (ep = salt; *ep && *ep != '$' && ep < salt + 8; ep++)
65 /* get the length of the true salt */
70 /* The password first, since that is what is most unknown */
71 MD5Update(&ctx, (const u_char *)pw, strlen(pw));
73 /* Then our magic string */
74 MD5Update(&ctx, (const u_char *)magic, strlen(magic));
76 /* Then the raw salt */
77 MD5Update(&ctx, (const u_char *)salt, (u_int)sl);
79 /* Then just as many characters of the MD5(pw,salt,pw) */
81 MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
82 MD5Update(&ctx1, (const u_char *)salt, (u_int)sl);
83 MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
84 MD5Final(final, &ctx1);
85 for(pl = (int)strlen(pw); pl > 0; pl -= MD5_SIZE)
86 MD5Update(&ctx, (const u_char *)final,
87 (u_int)(pl > MD5_SIZE ? MD5_SIZE : pl));
89 /* Don't leave anything around in vm they could use. */
90 memset(final, 0, sizeof(final));
92 /* Then something really weird... */
93 for (i = strlen(pw); i; i >>= 1)
95 MD5Update(&ctx, (const u_char *)final, 1);
97 MD5Update(&ctx, (const u_char *)pw, 1);
99 /* Now make the output string */
100 buffer = stpcpy(buffer, magic);
101 buffer = stpncpy(buffer, salt, (u_int)sl);
104 MD5Final(final, &ctx);
107 * and now, just to make sure things don't run too fast
108 * On a 60 Mhz Pentium this takes 34 msec, so you would
109 * need 30 seconds to build a 1000 entry dictionary...
111 for(i = 0; i < 1000; i++) {
114 MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
116 MD5Update(&ctx1, (const u_char *)final, MD5_SIZE);
119 MD5Update(&ctx1, (const u_char *)salt, (u_int)sl);
122 MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
125 MD5Update(&ctx1, (const u_char *)final, MD5_SIZE);
127 MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
128 MD5Final(final, &ctx1);
131 l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
132 _crypt_to64(buffer, l, 4); buffer += 4;
133 l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
134 _crypt_to64(buffer, l, 4); buffer += 4;
135 l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
136 _crypt_to64(buffer, l, 4); buffer += 4;
137 l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
138 _crypt_to64(buffer, l, 4); buffer += 4;
139 l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
140 _crypt_to64(buffer, l, 4); buffer += 4;
142 _crypt_to64(buffer, l, 2); buffer += 2;
145 /* Don't leave anything around in vm they could use. */
146 memset(final, 0, sizeof(final));