xref: /freebsd/lib/libcrypt/crypt-nthash.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1bf513f69SMark Murray /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro 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/types.h>
30bf513f69SMark Murray 
31bf513f69SMark Murray #include <netinet/in.h>
32bf513f69SMark Murray 
33bf513f69SMark Murray #include <ctype.h>
34bf513f69SMark Murray #include <err.h>
35bf513f69SMark Murray #include <md4.h>
36bf513f69SMark Murray #include <stdarg.h>
37bf513f69SMark Murray #include <stdio.h>
38bf513f69SMark Murray #include <string.h>
39bf513f69SMark Murray #include <unistd.h>
40bf513f69SMark Murray 
41bf513f69SMark Murray #include "crypt.h"
42bf513f69SMark Murray 
43bf513f69SMark Murray /*
44bf513f69SMark Murray  * NT HASH = md4(str2unicode(pw))
45bf513f69SMark Murray  */
46bf513f69SMark Murray 
47bf513f69SMark Murray /* ARGSUSED */
485f521d7bSEd Schouten int
crypt_nthash(const char * pw,const char * salt __unused,char * buffer)495f521d7bSEd Schouten crypt_nthash(const char *pw, const char *salt __unused, char *buffer)
50bf513f69SMark Murray {
51bf513f69SMark Murray 	size_t unipwLen;
525f521d7bSEd Schouten 	int i;
535f521d7bSEd Schouten 	static const char hexconvtab[] = "0123456789abcdef";
54bf513f69SMark Murray 	static const char *magic = "$3$";
55bf513f69SMark Murray 	u_int16_t unipw[128];
56bf513f69SMark Murray 	u_char hash[MD4_SIZE];
57bf513f69SMark Murray 	const char *s;
58bf513f69SMark Murray 	MD4_CTX	ctx;
59bf513f69SMark Murray 
60bf513f69SMark Murray 	bzero(unipw, sizeof(unipw));
61bf513f69SMark Murray 	/* convert to unicode (thanx Archie) */
62bf513f69SMark Murray 	unipwLen = 0;
63bf513f69SMark Murray 	for (s = pw; unipwLen < sizeof(unipw) / 2 && *s; s++)
64bf513f69SMark Murray 		unipw[unipwLen++] = htons(*s << 8);
65bf513f69SMark Murray 
66bf513f69SMark Murray 	/* Compute MD4 of Unicode password */
67bf513f69SMark Murray 	MD4Init(&ctx);
68bf513f69SMark Murray 	MD4Update(&ctx, (u_char *)unipw, unipwLen*sizeof(u_int16_t));
69bf513f69SMark Murray 	MD4Final(hash, &ctx);
70bf513f69SMark Murray 
715f521d7bSEd Schouten 	buffer = stpcpy(buffer, magic);
725f521d7bSEd Schouten 	*buffer++ = '$';
735f521d7bSEd Schouten 	for (i = 0; i < MD4_SIZE; i++) {
745f521d7bSEd Schouten 		*buffer++ = hexconvtab[hash[i] >> 4];
755f521d7bSEd Schouten 		*buffer++ = hexconvtab[hash[i] & 15];
76bf513f69SMark Murray 	}
775f521d7bSEd Schouten 	*buffer = '\0';
78bf513f69SMark Murray 
795f521d7bSEd Schouten 	return (0);
80bf513f69SMark Murray }
81