/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2003 Michael Bretterklieber4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/types.h>2930#include <netinet/in.h>3132#include <ctype.h>33#include <err.h>34#include <md4.h>35#include <stdarg.h>36#include <stdio.h>37#include <string.h>38#include <unistd.h>3940#include "crypt.h"4142/*43* NT HASH = md4(str2unicode(pw))44*/4546/* ARGSUSED */47int48crypt_nthash(const char *pw, const char *salt __unused, char *buffer)49{50size_t unipwLen;51int i;52static const char hexconvtab[] = "0123456789abcdef";53static const char *magic = "$3$";54u_int16_t unipw[128];55u_char hash[MD4_SIZE];56const char *s;57MD4_CTX ctx;5859bzero(unipw, sizeof(unipw));60/* convert to unicode (thanx Archie) */61unipwLen = 0;62for (s = pw; unipwLen < sizeof(unipw) / 2 && *s; s++)63unipw[unipwLen++] = htons(*s << 8);6465/* Compute MD4 of Unicode password */66MD4Init(&ctx);67MD4Update(&ctx, (u_char *)unipw, unipwLen*sizeof(u_int16_t));68MD4Final(hash, &ctx);6970buffer = stpcpy(buffer, magic);71*buffer++ = '$';72for (i = 0; i < MD4_SIZE; i++) {73*buffer++ = hexconvtab[hash[i] >> 4];74*buffer++ = hexconvtab[hash[i] & 15];75}76*buffer = '\0';7778return (0);79}808182