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