dd299a178ed170bdab92026a4ce72d95e78c86b5
[freeradius.git] / src / modules / rlm_mschap / smbencrypt.c
1 /*
2  * smbencrypt.c Produces LM-Password and NT-Password from
3  *              cleartext password
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  *
19  * Copyright 2002  3APA3A for FreeRADIUS project
20    Copyright 2006  The FreeRADIUS server project
21  */
22
23 #include <freeradius-devel/ident.h>
24 RCSID("$Id$")
25
26 #include        <freeradius-devel/libradius.h>
27 #include        <freeradius-devel/md4.h>
28 #include        <ctype.h>
29
30
31 #include        "smbdes.h"
32
33 static const char * hex = "0123456789ABCDEF";
34
35 static void tohex (const unsigned char * src, size_t len, char *dst)
36 {
37         int i;
38         for (i=0; i<len; i++) {
39                 dst[(i*2)] = hex[(src[i] >> 4)];
40                 dst[(i*2) + 1] = hex[(src[i]&0x0F)];
41         }
42         dst[(i*2)] = 0;
43 }
44
45 static void ntpwdhash (char *szHash, const char *szPassword)
46 {
47         char szUnicodePass[513];
48         char nPasswordLen;
49         int i;
50
51         /*
52          *      NT passwords are unicode.  Convert plain text password
53          *      to unicode by inserting a zero every other byte
54          */
55         nPasswordLen = strlen(szPassword);
56         for (i = 0; i < nPasswordLen; i++) {
57                 szUnicodePass[i << 1] = szPassword[i];
58                 szUnicodePass[(i << 1) + 1] = 0;
59         }
60
61         /* Encrypt Unicode password to a 16-byte MD4 hash */
62         md4_calc(szHash, szUnicodePass, (nPasswordLen<<1) );
63 }
64
65
66
67 int main (int argc, char *argv[])
68 {
69         int i, l;
70         char password[1024];
71         char hash[16];
72         char ntpass[33];
73         char lmpass[33];
74
75         fprintf(stderr, "LM Hash                         \tNT Hash\n");
76         fprintf(stderr, "--------------------------------\t--------------------------------\n");
77         fflush(stderr);
78         for (i = 1; i < argc; i++ ) {
79                 l = strlen(password);
80                 if (l && password[l-1] == '\n') password [l-1] = 0;
81                 smbdes_lmpwdhash(argv[i], hash);
82                 tohex (hash, 16, lmpass);
83                 ntpwdhash (hash, argv[i]);
84                 tohex (hash, 16, ntpass);
85                 printf("%s\t%s\n", lmpass, ntpass);
86         }
87         return 0;
88 }