Makefile :
[kai/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-1997
6    Modified by Jeremy Allison 1995.
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 extern int DEBUGLEVEL;
26
27 #include "byteorder.h"
28
29 /*
30    This implements the X/Open SMB password encryption
31    It takes a password, a 8 byte "crypt key" and puts 24 bytes of 
32    encrypted password into p24 */
33 void SMBencrypt(uchar *passwd, uchar *c8, uchar *p24)
34 {
35   uchar p14[15], p21[21];
36
37   memset(p21,'\0',21);
38   memset(p14,'\0',14);
39   StrnCpy((char *)p14,(char *)passwd,14);
40
41   strupper((char *)p14);
42   E_P16(p14, p21); 
43   E_P24(p21, c8, p24);
44 }
45
46 /* Routines for Windows NT MD4 Hash functions. */
47 static int _my_wcslen(int16 *str)
48 {
49         int len = 0;
50         while(*str++ != 0)
51                 len++;
52         return len;
53 }
54
55 /*
56  * Convert a string into an NT UNICODE string.
57  * Note that regardless of processor type 
58  * this must be in intel (little-endian)
59  * format.
60  */
61  
62 static int _my_mbstowcs(int16 *dst, uchar *src, int len)
63 {
64         int i;
65         int16 val;
66  
67         for(i = 0; i < len; i++) {
68                 val = *src;
69                 SSVAL(dst,0,val);
70                 dst++;
71                 src++;
72                 if(val == 0)
73                         break;
74         }
75         return i;
76 }
77
78 /* 
79  * Creates the MD4 Hash of the users password in NT UNICODE.
80  */
81  
82 void E_md4hash(uchar *passwd, uchar *p16)
83 {
84         int len;
85         int16 wpwd[129];
86         
87         /* Password cannot be longer than 128 characters */
88         len = strlen((char *)passwd);
89         if(len > 128)
90                 len = 128;
91         /* Password must be converted to NT unicode */
92         _my_mbstowcs(wpwd, passwd, len);
93         wpwd[len] = 0; /* Ensure string is null terminated */
94         /* Calculate length in bytes */
95         len = _my_wcslen(wpwd) * sizeof(int16);
96
97         mdfour(p16, (unsigned char *)wpwd, len);
98 }
99
100 /* Does the NT MD4 hash then des encryption. */
101  
102 void SMBNTencrypt(uchar *passwd, uchar *c8, uchar *p24)
103 {
104         uchar p21[21];
105  
106         memset(p21,'\0',21);
107  
108         E_md4hash(passwd, p21);    
109         E_P24(p21, c8, p24);
110 }
111
112 /* Does both the NT and LM owfs of a user's password */
113
114 void nt_lm_owf_gen(char *pwd, char nt_p16[16], char p16[16])
115 {
116         char passwd[129];
117         strncpy(passwd, pwd, 129);
118
119         /* Calculate the MD4 hash (NT compatible) of the password */
120         memset(nt_p16, '\0', 16);
121         E_md4hash((uchar *)passwd, nt_p16);
122
123         /* Mangle the passwords into Lanman format */
124         passwd[14] = '\0';
125         strupper(passwd);
126
127         /* Calculate the SMB (lanman) hash functions of the password */
128
129         memset(p16, '\0', 16);
130         E_P16((uchar *) passwd, p16);
131
132         /* clear out local copy of user's password (just being paranoid). */
133         bzero(passwd, sizeof(passwd));
134 }
135