Changeset View
Changeset View
Standalone View
Standalone View
lib/libcrypt/crypt-nthash.c
Show All 40 Lines | |||||
#include "crypt.h" | #include "crypt.h" | ||||
/* | /* | ||||
* NT HASH = md4(str2unicode(pw)) | * NT HASH = md4(str2unicode(pw)) | ||||
*/ | */ | ||||
/* ARGSUSED */ | /* ARGSUSED */ | ||||
char * | int | ||||
crypt_nthash(const char *pw, const char *salt __unused) | crypt_nthash(const char *pw, const char *salt __unused, char *buffer) | ||||
{ | { | ||||
size_t unipwLen; | size_t unipwLen; | ||||
int i, j; | int i; | ||||
static char hexconvtab[] = "0123456789abcdef"; | static const char hexconvtab[] = "0123456789abcdef"; | ||||
static const char *magic = "$3$"; | static const char *magic = "$3$"; | ||||
static char passwd[120]; | |||||
u_int16_t unipw[128]; | u_int16_t unipw[128]; | ||||
char final[MD4_SIZE*2 + 1]; | |||||
u_char hash[MD4_SIZE]; | u_char hash[MD4_SIZE]; | ||||
const char *s; | const char *s; | ||||
MD4_CTX ctx; | MD4_CTX ctx; | ||||
bzero(unipw, sizeof(unipw)); | bzero(unipw, sizeof(unipw)); | ||||
/* convert to unicode (thanx Archie) */ | /* convert to unicode (thanx Archie) */ | ||||
unipwLen = 0; | unipwLen = 0; | ||||
for (s = pw; unipwLen < sizeof(unipw) / 2 && *s; s++) | for (s = pw; unipwLen < sizeof(unipw) / 2 && *s; s++) | ||||
unipw[unipwLen++] = htons(*s << 8); | unipw[unipwLen++] = htons(*s << 8); | ||||
/* Compute MD4 of Unicode password */ | /* Compute MD4 of Unicode password */ | ||||
MD4Init(&ctx); | MD4Init(&ctx); | ||||
MD4Update(&ctx, (u_char *)unipw, unipwLen*sizeof(u_int16_t)); | MD4Update(&ctx, (u_char *)unipw, unipwLen*sizeof(u_int16_t)); | ||||
MD4Final(hash, &ctx); | MD4Final(hash, &ctx); | ||||
for (i = j = 0; i < MD4_SIZE; i++) { | buffer = stpcpy(buffer, magic); | ||||
final[j++] = hexconvtab[hash[i] >> 4]; | *buffer++ = '$'; | ||||
final[j++] = hexconvtab[hash[i] & 15]; | for (i = 0; i < MD4_SIZE; i++) { | ||||
*buffer++ = hexconvtab[hash[i] >> 4]; | |||||
*buffer++ = hexconvtab[hash[i] & 15]; | |||||
} | } | ||||
final[j] = '\0'; | *buffer = '\0'; | ||||
strcpy(passwd, magic); | return (0); | ||||
strcat(passwd, "$"); | |||||
strncat(passwd, final, MD4_SIZE*2); | |||||
/* Don't leave anything around in vm they could use. */ | |||||
memset(final, 0, sizeof(final)); | |||||
return (passwd); | |||||
} | } |