2 Unix SMB/Netbios implementation.
4 SMB parameters and setup
6 Copyright (C) Andrew Tridgell 1992-1998
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/>.
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];
45 StrnCpy((char *)p14
,(char *)passwd
,14);
47 strupper((char *)p14
);
50 SMBOWFencrypt(p21
, c8
, p24
);
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);
60 /* Routines for Windows NT MD4 Hash functions. */
61 static int _my_wcslen(int16
*str
)
70 * Convert a string into an NT UNICODE string.
71 * Note that regardless of processor type
72 * this must be in intel (little-endian)
76 static int _my_mbstowcs(int16
*dst
, uchar
*src
, int len
)
81 for(i
= 0; i
< len
; i
++) {
93 * Creates the MD4 Hash of the users password in NT UNICODE.
96 void E_md4hash(uchar
*passwd
, uchar
*p16
)
101 /* Password cannot be longer than 128 characters */
102 len
= strlen((char *)passwd
);
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])
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);
132 /* Mangle the passwords into Lanman format */
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);
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])
157 memcpy(p21
, passwd
, 16);
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])
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);
180 /* Does the NT MD4 hash then des encryption. */
182 void SMBNTencrypt(uchar
*passwd
, uchar
*c8
, uchar
*p24
)
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);