xref: /freebsd/lib/libcrypt/crypt-nthash.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
1bf513f69SMark Murray /*-
2*5e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*5e53a4f9SPedro F. Giffuni  *
4bf513f69SMark Murray  * Copyright (c) 2003 Michael Bretterklieber
5bf513f69SMark Murray  * All rights reserved.
6bf513f69SMark Murray  *
7bf513f69SMark Murray  * Redistribution and use in source and binary forms, with or without
8bf513f69SMark Murray  * modification, are permitted provided that the following conditions
9bf513f69SMark Murray  * are met:
10bf513f69SMark Murray  * 1. Redistributions of source code must retain the above copyright
11bf513f69SMark Murray  *    notice, this list of conditions and the following disclaimer.
12bf513f69SMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
13bf513f69SMark Murray  *    notice, this list of conditions and the following disclaimer in the
14bf513f69SMark Murray  *    documentation and/or other materials provided with the distribution.
15bf513f69SMark Murray  *
16bf513f69SMark Murray  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17bf513f69SMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bf513f69SMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bf513f69SMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20bf513f69SMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bf513f69SMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bf513f69SMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bf513f69SMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bf513f69SMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bf513f69SMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bf513f69SMark Murray  * SUCH DAMAGE.
27bf513f69SMark Murray  */
28bf513f69SMark Murray 
29bf513f69SMark Murray #include <sys/cdefs.h>
30bf513f69SMark Murray __FBSDID("$FreeBSD$");
31bf513f69SMark Murray 
32bf513f69SMark Murray #include <sys/types.h>
33bf513f69SMark Murray 
34bf513f69SMark Murray #include <netinet/in.h>
35bf513f69SMark Murray 
36bf513f69SMark Murray #include <ctype.h>
37bf513f69SMark Murray #include <err.h>
38bf513f69SMark Murray #include <md4.h>
39bf513f69SMark Murray #include <stdarg.h>
40bf513f69SMark Murray #include <stdio.h>
41bf513f69SMark Murray #include <string.h>
42bf513f69SMark Murray #include <unistd.h>
43bf513f69SMark Murray 
44bf513f69SMark Murray #include "crypt.h"
45bf513f69SMark Murray 
46bf513f69SMark Murray /*
47bf513f69SMark Murray  * NT HASH = md4(str2unicode(pw))
48bf513f69SMark Murray  */
49bf513f69SMark Murray 
50bf513f69SMark Murray /* ARGSUSED */
515f521d7bSEd Schouten int
525f521d7bSEd Schouten crypt_nthash(const char *pw, const char *salt __unused, char *buffer)
53bf513f69SMark Murray {
54bf513f69SMark Murray 	size_t unipwLen;
555f521d7bSEd Schouten 	int i;
565f521d7bSEd Schouten 	static const char hexconvtab[] = "0123456789abcdef";
57bf513f69SMark Murray 	static const char *magic = "$3$";
58bf513f69SMark Murray 	u_int16_t unipw[128];
59bf513f69SMark Murray 	u_char hash[MD4_SIZE];
60bf513f69SMark Murray 	const char *s;
61bf513f69SMark Murray 	MD4_CTX	ctx;
62bf513f69SMark Murray 
63bf513f69SMark Murray 	bzero(unipw, sizeof(unipw));
64bf513f69SMark Murray 	/* convert to unicode (thanx Archie) */
65bf513f69SMark Murray 	unipwLen = 0;
66bf513f69SMark Murray 	for (s = pw; unipwLen < sizeof(unipw) / 2 && *s; s++)
67bf513f69SMark Murray 		unipw[unipwLen++] = htons(*s << 8);
68bf513f69SMark Murray 
69bf513f69SMark Murray 	/* Compute MD4 of Unicode password */
70bf513f69SMark Murray  	MD4Init(&ctx);
71bf513f69SMark Murray 	MD4Update(&ctx, (u_char *)unipw, unipwLen*sizeof(u_int16_t));
72bf513f69SMark Murray 	MD4Final(hash, &ctx);
73bf513f69SMark Murray 
745f521d7bSEd Schouten 	buffer = stpcpy(buffer, magic);
755f521d7bSEd Schouten 	*buffer++ = '$';
765f521d7bSEd Schouten 	for (i = 0; i < MD4_SIZE; i++) {
775f521d7bSEd Schouten 		*buffer++ = hexconvtab[hash[i] >> 4];
785f521d7bSEd Schouten 		*buffer++ = hexconvtab[hash[i] & 15];
79bf513f69SMark Murray 	}
805f521d7bSEd Schouten 	*buffer = '\0';
81bf513f69SMark Murray 
825f521d7bSEd Schouten 	return (0);
83bf513f69SMark Murray }
84