Changed the order of arguments in make_oem_passwd_hash(). All the other
[samba.git] / source3 / libsmb / smbencrypt.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB parameters and setup
5    Copyright (C) Andrew Tridgell 1992-1998
6    Modified by Jeremy Allison 1995.
7    Copyright (C) Jeremy Allison 1995-2000.
8    Copyright (C) Luke Kennethc Casson Leighton 1996-2000.
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26
27 extern int DEBUGLEVEL;
28
29 #include "byteorder.h"
30
31 /*
32    This implements the X/Open SMB password encryption
33    It takes a password, a 8 byte "crypt key" and puts 24 bytes of 
34    encrypted password into p24 */
35 void SMBencrypt(uchar *passwd, uchar *c8, uchar *p24)
36 {
37         uchar p14[15], p21[21];
38
39         memset(p21,'\0',21);
40         memset(p14,'\0',14);
41         StrnCpy((char *)p14,(char *)passwd,14);
42
43         strupper((char *)p14);
44         E_P16(p14, p21); 
45
46         SMBOWFencrypt(p21, c8, p24);
47
48 #ifdef DEBUG_PASSWORD
49         DEBUG(100,("SMBencrypt: lm#, challenge, response\n"));
50         dump_data(100, (char *)p21, 16);
51         dump_data(100, (char *)c8, 8);
52         dump_data(100, (char *)p24, 24);
53 #endif
54 }
55
56 /* 
57  * Creates the MD4 Hash of the users password in NT UNICODE.
58  */
59  
60 void E_md4hash(uchar *passwd, uchar *p16)
61 {
62         int len;
63         smb_ucs2_t wpwd[129];
64         
65         /* Password cannot be longer than 128 characters */
66         len = strlen((char *)passwd);
67         if(len > 128)
68                 len = 128;
69         /* Password must be converted to NT unicode - null terminated. */
70         push_ucs2(NULL, wpwd, passwd, 256, STR_UNICODE|STR_NOALIGN|STR_TERMINATE);
71         /* Calculate length in bytes */
72         len = strlen_w(wpwd) * sizeof(int16);
73
74         mdfour(p16, (unsigned char *)wpwd, len);
75 }
76
77 /* Does both the NT and LM owfs of a user's password */
78 void nt_lm_owf_gen(char *pwd, uchar nt_p16[16], uchar p16[16])
79 {
80         char passwd[514];
81
82         memset(passwd,'\0',514);
83         safe_strcpy( passwd, pwd, sizeof(passwd)-1);
84
85         /* Calculate the MD4 hash (NT compatible) of the password */
86         memset(nt_p16, '\0', 16);
87         E_md4hash((uchar *)passwd, nt_p16);
88
89 #ifdef DEBUG_PASSWORD
90         DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
91         dump_data(120, passwd, strlen(passwd));
92         dump_data(100, (char *)nt_p16, 16);
93 #endif
94
95         /* Mangle the passwords into Lanman format */
96         passwd[14] = '\0';
97         strupper(passwd);
98
99         /* Calculate the SMB (lanman) hash functions of the password */
100
101         memset(p16, '\0', 16);
102         E_P16((uchar *) passwd, (uchar *)p16);
103
104 #ifdef DEBUG_PASSWORD
105         DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
106         dump_data(120, passwd, strlen(passwd));
107         dump_data(100, (char *)p16, 16);
108 #endif
109         /* clear out local copy of user's password (just being paranoid). */
110         memset(passwd, '\0', sizeof(passwd));
111 }
112
113 /* Does both the NTLMv2 owfs of a user's password */
114 void ntv2_owf_gen(const uchar owf[16],
115                   const char *user_n, const char *domain_n, uchar kr_buf[16])
116 {
117         pstring user_u;
118         pstring dom_u;
119         HMACMD5Context ctx;
120
121         int user_l = strlen(user_n);
122         int domain_l = strlen(domain_n);
123
124         push_ucs2(NULL, user_u, user_n, (user_l+1)*2, STR_UNICODE|STR_NOALIGN|STR_TERMINATE|STR_UPPER);
125         push_ucs2(NULL, dom_u, domain_n, (domain_l+1)*2, STR_UNICODE|STR_NOALIGN|STR_TERMINATE|STR_UPPER);
126
127         hmac_md5_init_limK_to_64(owf, 16, &ctx);
128         hmac_md5_update(user_u, user_l * 2, &ctx);
129         hmac_md5_update(dom_u, domain_l * 2, &ctx);
130         hmac_md5_final(kr_buf, &ctx);
131
132 #ifdef DEBUG_PASSWORD
133         DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
134         dump_data(100, user_u, user_l * 2);
135         dump_data(100, dom_u, domain_l * 2);
136         dump_data(100, owf, 16);
137         dump_data(100, kr_buf, 16);
138 #endif
139 }
140
141 /* Does the des encryption from the NT or LM MD4 hash. */
142 void SMBOWFencrypt(const uchar passwd[16], const uchar *c8, uchar p24[24])
143 {
144         uchar p21[21];
145  
146         memset(p21,'\0',21);
147  
148         memcpy(p21, passwd, 16);    
149         E_P24(p21, c8, p24);
150 }
151
152 /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
153 void NTLMSSPOWFencrypt(uchar passwd[8], uchar *ntlmchalresp, uchar p24[24])
154 {
155         uchar p21[21];
156  
157         memset(p21,'\0',21);
158         memcpy(p21, passwd, 8);    
159         memset(p21 + 8, 0xbd, 8);    
160
161         E_P24(p21, ntlmchalresp, p24);
162 #ifdef DEBUG_PASSWORD
163         DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));
164         dump_data(100, (char *)p21, 21);
165         dump_data(100, (char *)ntlmchalresp, 8);
166         dump_data(100, (char *)p24, 24);
167 #endif
168 }
169
170
171 /* Does the NT MD4 hash then des encryption. */
172  
173 void SMBNTencrypt(uchar *passwd, uchar *c8, uchar *p24)
174 {
175         uchar p21[21];
176  
177         memset(p21,'\0',21);
178  
179         E_md4hash(passwd, p21);    
180         SMBOWFencrypt(p21, c8, p24);
181
182 #ifdef DEBUG_PASSWORD
183         DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
184         dump_data(100, (char *)p21, 16);
185         dump_data(100, (char *)c8, 8);
186         dump_data(100, (char *)p24, 24);
187 #endif
188 }
189
190 BOOL make_oem_passwd_hash(const char *passwd, uchar old_pw_hash[16], 
191                           BOOL unicode, char data[516])
192 {
193         int new_pw_len = strlen(passwd) * (unicode ? 2 : 1);
194
195         if (new_pw_len > 512)
196         {
197                 DEBUG(0,("make_oem_passwd_hash: new password is too long.\n"));
198                 return False;
199         }
200
201         /*
202          * Now setup the data area.
203          * We need to generate a random fill
204          * for this area to make it harder to
205          * decrypt. JRA.
206          */
207         generate_random_buffer((unsigned char *)data, 516, False);
208         push_string(NULL, &data[512 - new_pw_len], passwd, new_pw_len, 
209                     STR_NOALIGN | (unicode?STR_UNICODE:STR_ASCII));
210         SIVAL(data, 512, new_pw_len);
211
212 #ifdef DEBUG_PASSWORD
213         DEBUG(100,("make_oem_passwd_hash\n"));
214         dump_data(100, data, 516);
215 #endif
216         SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
217
218         return True;
219 }
220
221 /* Does the md5 encryption from the NT hash for NTLMv2. */
222 void SMBOWFencrypt_ntv2(const uchar kr[16],
223                         const uchar * srv_chal, int srv_chal_len,
224                         const uchar * cli_chal, int cli_chal_len,
225                         char resp_buf[16])
226 {
227         HMACMD5Context ctx;
228
229         hmac_md5_init_limK_to_64(kr, 16, &ctx);
230         hmac_md5_update(srv_chal, srv_chal_len, &ctx);
231         hmac_md5_update(cli_chal, cli_chal_len, &ctx);
232         hmac_md5_final(resp_buf, &ctx);
233
234 #ifdef DEBUG_PASSWORD
235         DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, cli_chal, resp_buf\n"));
236         dump_data(100, srv_chal, srv_chal_len);
237         dump_data(100, cli_chal, cli_chal_len);
238         dump_data(100, resp_buf, 16);
239 #endif
240 }
241
242 void SMBsesskeygen_ntv2(const uchar kr[16],
243                         const uchar * nt_resp, char sess_key[16])
244 {
245         HMACMD5Context ctx;
246
247         hmac_md5_init_limK_to_64(kr, 16, &ctx);
248         hmac_md5_update(nt_resp, 16, &ctx);
249         hmac_md5_final(sess_key, &ctx);
250
251 #ifdef DEBUG_PASSWORD
252         DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
253         dump_data(100, sess_key, 16);
254 #endif
255 }
256
257 void SMBsesskeygen_ntv1(const uchar kr[16],
258                         const uchar * nt_resp, char sess_key[16])
259 {
260         mdfour(sess_key, kr, 16);
261
262 #ifdef DEBUG_PASSWORD
263         DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
264         dump_data(100, sess_key, 16);
265 #endif
266 }
267
268 /***********************************************************
269  encode a password buffer
270 ************************************************************/
271 BOOL encode_pw_buffer(char buffer[516], const char *new_pass,
272                       int new_pw_len, BOOL nt_pass_set)
273 {
274         generate_random_buffer(buffer, 516, True);
275
276         if (nt_pass_set) {
277                 new_pw_len *= 2;
278                 push_ucs2(NULL, &buffer[512 - new_pw_len], new_pass,
279                           new_pw_len, 0);
280         } else {
281                 push_ascii(&buffer[512 - new_pw_len], new_pass,
282                            new_pw_len, 0);
283         }
284
285         /* 
286          * The length of the new password is in the last 4 bytes of
287          * the data buffer.
288          */
289         SIVAL(buffer, 512, new_pw_len);
290
291         return True;
292 }
293
294 /***********************************************************
295  decode a password buffer
296 ************************************************************/
297 BOOL decode_pw_buffer(char in_buffer[516], char *new_pwrd,
298                       int new_pwrd_size, uint32 *new_pw_len,
299                       uchar nt_p16[16], uchar p16[16])
300 {
301         int uni_pw_len=0;
302         int byte_len=0;
303         char unicode_passwd[514];
304         char lm_ascii_passwd[514];
305         char passwd[514];
306
307         /*
308           Warning !!! : This function is called from some rpc call.
309           The password IN the buffer is a UNICODE string.
310           The password IN new_pwrd is an ASCII string
311           If you reuse that code somewhere else check first.
312         */
313
314         ZERO_STRUCT(unicode_passwd);
315         ZERO_STRUCT(lm_ascii_passwd);
316         ZERO_STRUCT(passwd);
317
318         memset(nt_p16, '\0', 16);
319         memset(p16, '\0', 16);
320
321         /* The length of the new password is in the last 4 bytes of the data buffer. */
322
323         byte_len = IVAL(in_buffer, 512);
324
325 #ifdef DEBUG_PASSWORD
326         dump_data(100, in_buffer, 516);
327 #endif
328
329         /* Password cannot be longer than 128 characters */
330         if ( (byte_len < 0) || (byte_len > new_pwrd_size - 1)) {
331                 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
332                 return False;
333         }
334         
335         pull_string(NULL, passwd, &in_buffer[512 - byte_len], -1, byte_len, STR_UNICODE);
336         uni_pw_len = byte_len/2;
337
338 #ifdef DEBUG_PASSWORD
339         DEBUG(100,("nt_lm_owf_gen: passwd: "));
340         dump_data(100, (char *)passwd, uni_pw_len);
341         DEBUG(100,("len:%d\n", uni_pw_len));
342 #endif
343         memcpy(unicode_passwd, &in_buffer[512 - byte_len], byte_len);
344                 
345         mdfour(nt_p16, (unsigned char *)unicode_passwd, byte_len);
346         
347 #ifdef DEBUG_PASSWORD
348         DEBUG(100,("nt_lm_owf_gen: nt#:"));
349         dump_data(100, (char *)nt_p16, 16);
350         DEBUG(100,("\n"));
351 #endif
352         
353         /* Mangle the passwords into Lanman format */
354         memcpy(lm_ascii_passwd, passwd, byte_len/2);
355         lm_ascii_passwd[14] = '\0';
356         strupper(lm_ascii_passwd);
357
358         /* Calculate the SMB (lanman) hash functions of the password */
359         E_P16((uchar *) lm_ascii_passwd, (uchar *)p16);
360
361 #ifdef DEBUG_PASSWORD
362         DEBUG(100,("nt_lm_owf_gen: lm#:"));
363         dump_data(100, (char *)p16, 16);
364         DEBUG(100,("\n"));
365 #endif
366
367         /* copy the password and it's length to the return buffer */    
368         *new_pw_len = byte_len/2;
369         memcpy(new_pwrd, passwd, uni_pw_len);
370         new_pwrd[uni_pw_len]='\0';
371         
372         /* clear out local copy of user's password (just being paranoid). */
373         ZERO_STRUCT(unicode_passwd);
374         ZERO_STRUCT(lm_ascii_passwd);
375         ZERO_STRUCT(passwd);
376         
377         return True;
378
379 }
380
381 /* Calculate the NT owfs of a user's password */
382 void nt_owf_genW(const UNISTR2 *pwd, uchar nt_p16[16])
383 {
384         char buf[512];
385         int i;
386
387         for (i = 0; i < MIN(pwd->uni_str_len, sizeof(buf) / 2); i++)
388         {
389                 SIVAL(buf, i * 2, pwd->buffer[i]);
390         }
391         /* Calculate the MD4 hash (NT compatible) of the password */
392         mdfour(nt_p16, buf, pwd->uni_str_len * 2);
393
394         /* clear out local copy of user's password (just being paranoid). */
395         ZERO_STRUCT(buf);
396 }