xref: /freebsd-14.2/lib/libcrypt/crypt-md5.c (revision 1af027e5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003 Poul-Henning Kamp
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
31 
32 #include <err.h>
33 #include <md5.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <unistd.h>
38 
39 #include "crypt.h"
40 
41 /*
42  * UNIX password
43  */
44 
45 int
crypt_md5(const char * pw,const char * salt,char * buffer)46 crypt_md5(const char *pw, const char *salt, char *buffer)
47 {
48 	MD5_CTX	ctx,ctx1;
49 	unsigned long l;
50 	int sl, pl;
51 	u_int i;
52 	u_char final[MD5_SIZE];
53 	const char *ep;
54 	static const char *magic = "$1$";
55 
56 	/* If the salt starts with the magic string, skip that. */
57 	if (!strncmp(salt, magic, strlen(magic)))
58 		salt += strlen(magic);
59 
60 	/* It stops at the first '$', max 8 chars */
61 	for (ep = salt; *ep && *ep != '$' && ep < salt + 8; ep++)
62 		continue;
63 
64 	/* get the length of the true salt */
65 	sl = ep - salt;
66 
67 	MD5Init(&ctx);
68 
69 	/* The password first, since that is what is most unknown */
70 	MD5Update(&ctx, (const u_char *)pw, strlen(pw));
71 
72 	/* Then our magic string */
73 	MD5Update(&ctx, (const u_char *)magic, strlen(magic));
74 
75 	/* Then the raw salt */
76 	MD5Update(&ctx, (const u_char *)salt, (u_int)sl);
77 
78 	/* Then just as many characters of the MD5(pw,salt,pw) */
79 	MD5Init(&ctx1);
80 	MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
81 	MD5Update(&ctx1, (const u_char *)salt, (u_int)sl);
82 	MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
83 	MD5Final(final, &ctx1);
84 	for(pl = (int)strlen(pw); pl > 0; pl -= MD5_SIZE)
85 		MD5Update(&ctx, (const u_char *)final,
86 		    (u_int)(pl > MD5_SIZE ? MD5_SIZE : pl));
87 
88 	/* Don't leave anything around in vm they could use. */
89 	explicit_bzero(final, sizeof(final));
90 
91 	/* Then something really weird... */
92 	for (i = strlen(pw); i; i >>= 1)
93 		if(i & 1)
94 		    MD5Update(&ctx, (const u_char *)final, 1);
95 		else
96 		    MD5Update(&ctx, (const u_char *)pw, 1);
97 
98 	/* Now make the output string */
99 	buffer = stpcpy(buffer, magic);
100 	buffer = stpncpy(buffer, salt, (u_int)sl);
101 	*buffer++ = '$';
102 
103 	MD5Final(final, &ctx);
104 
105 	/*
106 	 * and now, just to make sure things don't run too fast
107 	 * On a 60 Mhz Pentium this takes 34 msec, so you would
108 	 * need 30 seconds to build a 1000 entry dictionary...
109 	 */
110 	for(i = 0; i < 1000; i++) {
111 		MD5Init(&ctx1);
112 		if(i & 1)
113 			MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
114 		else
115 			MD5Update(&ctx1, (const u_char *)final, MD5_SIZE);
116 
117 		if(i % 3)
118 			MD5Update(&ctx1, (const u_char *)salt, (u_int)sl);
119 
120 		if(i % 7)
121 			MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
122 
123 		if(i & 1)
124 			MD5Update(&ctx1, (const u_char *)final, MD5_SIZE);
125 		else
126 			MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
127 		MD5Final(final, &ctx1);
128 	}
129 
130 	l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
131 	_crypt_to64(buffer, l, 4); buffer += 4;
132 	l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
133 	_crypt_to64(buffer, l, 4); buffer += 4;
134 	l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
135 	_crypt_to64(buffer, l, 4); buffer += 4;
136 	l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
137 	_crypt_to64(buffer, l, 4); buffer += 4;
138 	l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
139 	_crypt_to64(buffer, l, 4); buffer += 4;
140 	l = final[11];
141 	_crypt_to64(buffer, l, 2); buffer += 2;
142 	*buffer = '\0';
143 
144 	/* Don't leave anything around in vm they could use. */
145 	explicit_bzero(final, sizeof(final));
146 
147 	return (0);
148 }
149