Pull fix from branch_1_1
[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/autoconf.h>
27 #include        <stdio.h>
28 #include        <stdlib.h>
29 #include        <string.h>
30 #include        <ctype.h>
31
32 #include        <freeradius-devel/md4.h>
33
34 #include        "smbdes.h"
35
36 static const char * hex = "0123456789ABCDEF";
37
38 static void tohex (const unsigned char * src, size_t len, char *dst)
39 {
40         int i;
41         for (i=0; i<len; i++) {
42                 dst[(i*2)] = hex[(src[i] >> 4)];
43                 dst[(i*2) + 1] = hex[(src[i]&0x0F)];
44         }
45         dst[(i*2)] = 0;
46 }
47
48 static void ntpwdhash (char *szHash, const char *szPassword)
49 {
50         char szUnicodePass[513];
51         char nPasswordLen;
52         int i;
53
54         /*
55          *      NT passwords are unicode.  Convert plain text password
56          *      to unicode by inserting a zero every other byte
57          */
58         nPasswordLen = strlen(szPassword);
59         for (i = 0; i < nPasswordLen; i++) {
60                 szUnicodePass[i << 1] = szPassword[i];
61                 szUnicodePass[(i << 1) + 1] = 0;
62         }
63
64         /* Encrypt Unicode password to a 16-byte MD4 hash */
65         md4_calc(szHash, szUnicodePass, (nPasswordLen<<1) );
66 }
67
68
69
70 int main (int argc, char *argv[])
71 {
72         int i, l;
73         char password[1024];
74         char hash[16];
75         char ntpass[33];
76         char lmpass[33];
77
78         fprintf(stderr, "LM Hash                         \tNT Hash\n");
79         fprintf(stderr, "--------------------------------\t--------------------------------\n");
80         fflush(stderr);
81         for (i = 1; i < argc; i++ ) {
82                 l = strlen(password);
83                 if (l && password[l-1] == '\n') password [l-1] = 0;
84                 smbdes_lmpwdhash(argv[i], hash);
85                 tohex (hash, 16, lmpass);
86                 ntpwdhash (hash, argv[i]);
87                 tohex (hash, 16, ntpass);
88                 printf("%s\t%s\n", lmpass, ntpass);
89         }
90         return 0;
91 }