1178476Sjb/*- 2178476Sjb * Copyright (c) 2003 Michael Bretterklieber 3178476Sjb * All rights reserved. 4178476Sjb * 5178476Sjb * Redistribution and use in source and binary forms, with or without 6178476Sjb * modification, are permitted provided that the following conditions 7178476Sjb * are met: 8178476Sjb * 1. Redistributions of source code must retain the above copyright 9178476Sjb * notice, this list of conditions and the following disclaimer. 10178476Sjb * 2. Redistributions in binary form must reproduce the above copyright 11178476Sjb * notice, this list of conditions and the following disclaimer in the 12178476Sjb * documentation and/or other materials provided with the distribution. 13178476Sjb * 14178476Sjb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15178476Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16178476Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17178476Sjb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18178476Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19178476Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20178476Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21178476Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22178476Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23178476Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24178476Sjb * SUCH DAMAGE. 25178476Sjb */ 26178476Sjb 27178476Sjb#include <sys/cdefs.h> 28178476Sjb__FBSDID("$FreeBSD: releng/11.0/lib/libcrypt/crypt-nthash.c 115720 2003-06-02 19:29:27Z markm $"); 29178476Sjb 30178476Sjb#include <sys/types.h> 31178476Sjb 32178476Sjb#include <netinet/in.h> 33178476Sjb 34178476Sjb#include <ctype.h> 35178476Sjb#include <err.h> 36178476Sjb#include <md4.h> 37178476Sjb#include <stdarg.h> 38178476Sjb#include <stdio.h> 39178476Sjb#include <string.h> 40178476Sjb#include <unistd.h> 41178476Sjb 42178476Sjb#include "crypt.h" 43178476Sjb 44/* 45 * NT HASH = md4(str2unicode(pw)) 46 */ 47 48/* ARGSUSED */ 49char * 50crypt_nthash(const char *pw, const char *salt __unused) 51{ 52 size_t unipwLen; 53 int i, j; 54 static char hexconvtab[] = "0123456789abcdef"; 55 static const char *magic = "$3$"; 56 static char passwd[120]; 57 u_int16_t unipw[128]; 58 char final[MD4_SIZE*2 + 1]; 59 u_char hash[MD4_SIZE]; 60 const char *s; 61 MD4_CTX ctx; 62 63 bzero(unipw, sizeof(unipw)); 64 /* convert to unicode (thanx Archie) */ 65 unipwLen = 0; 66 for (s = pw; unipwLen < sizeof(unipw) / 2 && *s; s++) 67 unipw[unipwLen++] = htons(*s << 8); 68 69 /* Compute MD4 of Unicode password */ 70 MD4Init(&ctx); 71 MD4Update(&ctx, (u_char *)unipw, unipwLen*sizeof(u_int16_t)); 72 MD4Final(hash, &ctx); 73 74 for (i = j = 0; i < MD4_SIZE; i++) { 75 final[j++] = hexconvtab[hash[i] >> 4]; 76 final[j++] = hexconvtab[hash[i] & 15]; 77 } 78 final[j] = '\0'; 79 80 strcpy(passwd, magic); 81 strcat(passwd, "$"); 82 strncat(passwd, final, MD4_SIZE*2); 83 84 /* Don't leave anything around in vm they could use. */ 85 memset(final, 0, sizeof(final)); 86 87 return (passwd); 88} 89