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