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 */ 49bf513f69SMark Murray char * 50bf513f69SMark Murray crypt_nthash(const char *pw, const char *salt __unused) 51bf513f69SMark Murray { 52bf513f69SMark Murray size_t unipwLen; 53bf513f69SMark Murray int i, j; 54bf513f69SMark Murray static char hexconvtab[] = "0123456789abcdef"; 55bf513f69SMark Murray static const char *magic = "$3$"; 56bf513f69SMark Murray static char passwd[120]; 57bf513f69SMark Murray u_int16_t unipw[128]; 58bf513f69SMark Murray char final[MD4_SIZE*2 + 1]; 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 74bf513f69SMark Murray for (i = j = 0; i < MD4_SIZE; i++) { 75bf513f69SMark Murray final[j++] = hexconvtab[hash[i] >> 4]; 76bf513f69SMark Murray final[j++] = hexconvtab[hash[i] & 15]; 77bf513f69SMark Murray } 78bf513f69SMark Murray final[j] = '\0'; 79bf513f69SMark Murray 80bf513f69SMark Murray strcpy(passwd, magic); 81bf513f69SMark Murray strcat(passwd, "$"); 82bf513f69SMark Murray strncat(passwd, final, MD4_SIZE*2); 83bf513f69SMark Murray 84bf513f69SMark Murray /* Don't leave anything around in vm they could use. */ 85bf513f69SMark Murray memset(final, 0, sizeof(final)); 86bf513f69SMark Murray 87bf513f69SMark Murray return (passwd); 88bf513f69SMark Murray } 89