1261272Sdim/*-
2261272Sdim * SPDX-License-Identifier: BSD-2-Clause
3353358Sdim *
4353358Sdim * Copyright (c) 2003 Poul-Henning Kamp
5353358Sdim * All rights reserved.
6261272Sdim *
7261272Sdim * Redistribution and use in source and binary forms, with or without
8261272Sdim * modification, are permitted provided that the following conditions
9314564Sdim * are met:
10261272Sdim * 1. Redistributions of source code must retain the above copyright
11314564Sdim *    notice, this list of conditions and the following disclaimer.
12314564Sdim * 2. Redistributions in binary form must reproduce the above copyright
13261272Sdim *    notice, this list of conditions and the following disclaimer in the
14314564Sdim *    documentation and/or other materials provided with the distribution.
15261272Sdim *
16314564Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17314564Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18314564Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19261272Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20314564Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21261272Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22341825Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23341825Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24341825Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25341825Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26341825Sdim * SUCH DAMAGE.
27314564Sdim */
28314564Sdim
29341825Sdim#include <sys/types.h>
30341825Sdim
31341825Sdim#include <err.h>
32341825Sdim#include <md5.h>
33341825Sdim#include <stdio.h>
34341825Sdim#include <string.h>
35341825Sdim#include <unistd.h>
36341825Sdim
37341825Sdim#include "crypt.h"
38341825Sdim
39261272Sdim/*
40261272Sdim * UNIX password
41276792Sdim */
42
43int
44crypt_md5(const char *pw, const char *salt, char *buffer)
45{
46	MD5_CTX	ctx,ctx1;
47	unsigned long l;
48	int sl, pl;
49	u_int i;
50	u_char final[MD5_SIZE];
51	const char *ep;
52	static const char *magic = "$1$";
53
54	/* If the salt starts with the magic string, skip that. */
55	if (!strncmp(salt, magic, strlen(magic)))
56		salt += strlen(magic);
57
58	/* It stops at the first '$', max 8 chars */
59	for (ep = salt; *ep && *ep != '$' && ep < salt + 8; ep++)
60		continue;
61
62	/* get the length of the true salt */
63	sl = ep - salt;
64
65	MD5Init(&ctx);
66
67	/* The password first, since that is what is most unknown */
68	MD5Update(&ctx, (const u_char *)pw, strlen(pw));
69
70	/* Then our magic string */
71	MD5Update(&ctx, (const u_char *)magic, strlen(magic));
72
73	/* Then the raw salt */
74	MD5Update(&ctx, (const u_char *)salt, (u_int)sl);
75
76	/* Then just as many characters of the MD5(pw,salt,pw) */
77	MD5Init(&ctx1);
78	MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
79	MD5Update(&ctx1, (const u_char *)salt, (u_int)sl);
80	MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
81	MD5Final(final, &ctx1);
82	for(pl = (int)strlen(pw); pl > 0; pl -= MD5_SIZE)
83		MD5Update(&ctx, (const u_char *)final,
84		    (u_int)(pl > MD5_SIZE ? MD5_SIZE : pl));
85
86	/* Don't leave anything around in vm they could use. */
87	memset(final, 0, sizeof(final));
88
89	/* Then something really weird... */
90	for (i = strlen(pw); i; i >>= 1)
91		if(i & 1)
92		    MD5Update(&ctx, (const u_char *)final, 1);
93		else
94		    MD5Update(&ctx, (const u_char *)pw, 1);
95
96	/* Now make the output string */
97	buffer = stpcpy(buffer, magic);
98	buffer = stpncpy(buffer, salt, (u_int)sl);
99	*buffer++ = '$';
100
101	MD5Final(final, &ctx);
102
103	/*
104	 * and now, just to make sure things don't run too fast
105	 * On a 60 Mhz Pentium this takes 34 msec, so you would
106	 * need 30 seconds to build a 1000 entry dictionary...
107	 */
108	for(i = 0; i < 1000; i++) {
109		MD5Init(&ctx1);
110		if(i & 1)
111			MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
112		else
113			MD5Update(&ctx1, (const u_char *)final, MD5_SIZE);
114
115		if(i % 3)
116			MD5Update(&ctx1, (const u_char *)salt, (u_int)sl);
117
118		if(i % 7)
119			MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
120
121		if(i & 1)
122			MD5Update(&ctx1, (const u_char *)final, MD5_SIZE);
123		else
124			MD5Update(&ctx1, (const u_char *)pw, strlen(pw));
125		MD5Final(final, &ctx1);
126	}
127
128	l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
129	_crypt_to64(buffer, l, 4); buffer += 4;
130	l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
131	_crypt_to64(buffer, l, 4); buffer += 4;
132	l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
133	_crypt_to64(buffer, l, 4); buffer += 4;
134	l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
135	_crypt_to64(buffer, l, 4); buffer += 4;
136	l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
137	_crypt_to64(buffer, l, 4); buffer += 4;
138	l = final[11];
139	_crypt_to64(buffer, l, 2); buffer += 2;
140	*buffer = '\0';
141
142	/* Don't leave anything around in vm they could use. */
143	memset(final, 0, sizeof(final));
144
145	return (0);
146}
147