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