Ticket 1551: Update GPL version from 2 to 3
[midnight-commander.git] / src / vfs / smbfs / helpers / libsmb / smbencrypt.c
blobb0d5f44b29ce01c5d0753a3f3e3e020e01aa9485
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 SMB parameters and setup
6 Copyright (C) Andrew Tridgell 1992-1998
8 Copyright (C) 2011
9 The Free Software Foundation, Inc.
11 Modified by Jeremy Allison 1995.
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include "includes.h"
31 extern int DEBUGLEVEL;
33 #include "byteorder.h"
36 This implements the X/Open SMB password encryption
37 It takes a password, a 8 byte "crypt key" and puts 24 bytes of
38 encrypted password into p24 */
39 void SMBencrypt(uchar *passwd, uchar *c8, uchar *p24)
41 uchar p14[15], p21[21];
43 memset(p21,'\0',21);
44 memset(p14,'\0',14);
45 StrnCpy((char *)p14,(char *)passwd,14);
47 strupper((char *)p14);
48 E_P16(p14, p21);
50 SMBOWFencrypt(p21, c8, p24);
52 #ifdef DEBUG_PASSWORD
53 DEBUG(100,("SMBencrypt: lm#, challenge, response\n"));
54 dump_data(100, (char *)p21, 16);
55 dump_data(100, (char *)c8, 8);
56 dump_data(100, (char *)p24, 24);
57 #endif
60 /* Routines for Windows NT MD4 Hash functions. */
61 static int _my_wcslen(int16 *str)
63 int len = 0;
64 while(*str++ != 0)
65 len++;
66 return len;
70 * Convert a string into an NT UNICODE string.
71 * Note that regardless of processor type
72 * this must be in intel (little-endian)
73 * format.
76 static int _my_mbstowcs(int16 *dst, uchar *src, int len)
78 int i;
79 int16 val;
81 for(i = 0; i < len; i++) {
82 val = *src;
83 SSVAL(dst,0,val);
84 dst++;
85 src++;
86 if(val == 0)
87 break;
89 return i;
92 /*
93 * Creates the MD4 Hash of the users password in NT UNICODE.
96 void E_md4hash(uchar *passwd, uchar *p16)
98 int len;
99 int16 wpwd[129];
101 /* Password cannot be longer than 128 characters */
102 len = strlen((char *)passwd);
103 if(len > 128)
104 len = 128;
105 /* Password must be converted to NT unicode */
106 _my_mbstowcs(wpwd, passwd, len);
107 wpwd[len] = 0; /* Ensure string is null terminated */
108 /* Calculate length in bytes */
109 len = _my_wcslen(wpwd) * sizeof(int16);
111 mdfour(p16, (unsigned char *)wpwd, len);
114 /* Does both the NT and LM owfs of a user's password */
115 void nt_lm_owf_gen(char *pwd, uchar nt_p16[16], uchar p16[16])
117 char passwd[130];
119 memset(passwd,'\0',130);
120 safe_strcpy( passwd, pwd, sizeof(passwd)-1);
122 /* Calculate the MD4 hash (NT compatible) of the password */
123 memset(nt_p16, '\0', 16);
124 E_md4hash((uchar *)passwd, nt_p16);
126 #ifdef DEBUG_PASSWORD
127 DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
128 dump_data(120, passwd, strlen(passwd));
129 dump_data(100, (char *)nt_p16, 16);
130 #endif
132 /* Mangle the passwords into Lanman format */
133 passwd[14] = '\0';
134 strupper(passwd);
136 /* Calculate the SMB (lanman) hash functions of the password */
138 memset(p16, '\0', 16);
139 E_P16((uchar *) passwd, (uchar *)p16);
141 #ifdef DEBUG_PASSWORD
142 DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
143 dump_data(120, passwd, strlen(passwd));
144 dump_data(100, (char *)p16, 16);
145 #endif
146 /* clear out local copy of user's password (just being paranoid). */
147 memset(passwd, '\0', sizeof(passwd));
150 /* Does the des encryption from the NT or LM MD4 hash. */
151 void SMBOWFencrypt(uchar passwd[16], uchar *c8, uchar p24[24])
153 uchar p21[21];
155 memset(p21,'\0',21);
157 memcpy(p21, passwd, 16);
158 E_P24(p21, c8, p24);
161 /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
162 void NTLMSSPOWFencrypt(uchar passwd[8], uchar *ntlmchalresp, uchar p24[24])
164 uchar p21[21];
166 memset(p21,'\0',21);
167 memcpy(p21, passwd, 8);
168 memset(p21 + 8, 0xbd, 8);
170 E_P24(p21, ntlmchalresp, p24);
171 #ifdef DEBUG_PASSWORD
172 DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));
173 dump_data(100, (char *)p21, 21);
174 dump_data(100, (char *)ntlmchalresp, 8);
175 dump_data(100, (char *)p24, 24);
176 #endif
180 /* Does the NT MD4 hash then des encryption. */
182 void SMBNTencrypt(uchar *passwd, uchar *c8, uchar *p24)
184 uchar p21[21];
186 memset(p21,'\0',21);
188 E_md4hash(passwd, p21);
189 SMBOWFencrypt(p21, c8, p24);
191 #ifdef DEBUG_PASSWORD
192 DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
193 dump_data(100, (char *)p21, 16);
194 dump_data(100, (char *)c8, 8);
195 dump_data(100, (char *)p24, 24);
196 #endif