a huge pile of changes :-)
[nivanova/samba-autobuild/.git] / source3 / libsmb / smbencrypt.c
1 #ifdef SMB_PASSWD
2 /* 
3    Unix SMB/Netbios implementation.
4    Version 1.9.
5    SMB parameters and setup
6    Copyright (C) Andrew Tridgell 1992-1995
7    Modified by Jeremy Allison 1995.
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "loadparm.h"
26 #include "des.h"
27 #include "md4.h"
28
29 extern int DEBUGLEVEL;
30
31 #include "byteorder.h"
32
33 void str_to_key(uchar *str,uchar *key)
34 {
35   void des_set_odd_parity(des_cblock *);
36   int i;
37
38   key[0] = str[0]>>1;
39   key[1] = ((str[0]&0x01)<<6) | (str[1]>>2);
40   key[2] = ((str[1]&0x03)<<5) | (str[2]>>3);
41   key[3] = ((str[2]&0x07)<<4) | (str[3]>>4);
42   key[4] = ((str[3]&0x0F)<<3) | (str[4]>>5);
43   key[5] = ((str[4]&0x1F)<<2) | (str[5]>>6);
44   key[6] = ((str[5]&0x3F)<<1) | (str[6]>>7);
45   key[7] = str[6]&0x7F;
46   for (i=0;i<8;i++) {
47     key[i] = (key[i]<<1);
48   }
49   des_set_odd_parity((des_cblock *)key);
50 }
51
52 void D1(uchar *k, uchar *d, uchar *out)
53 {
54   des_key_schedule ks;
55   des_cblock deskey;
56
57   str_to_key(k,(uchar *)deskey);
58   des_set_key(deskey,ks);
59   des_ecb_encrypt(d, out, ks, DES_DECRYPT);
60 }
61
62 void E1(uchar *k, uchar *d, uchar *out)
63 {
64   des_key_schedule ks;
65   des_cblock deskey;
66
67   str_to_key(k,(uchar *)deskey);
68   des_set_key(deskey,ks);
69   des_ecb_encrypt(d, out, ks, DES_ENCRYPT);
70 }
71  
72 void E_P16(uchar *p14,uchar *p16)
73 {
74   uchar sp7[7];
75   /* the following constant makes us compatible with other
76   implementations. Note that publishing this constant does not reduce the
77   security of the encryption mechanism */
78   uchar sp8[] = {0xAA,0xD3,0xB4,0x35,0xB5,0x14,0x4,0xEE};
79   uchar x[8];
80
81   memset(sp7,'\0',7);
82
83   D1(sp7, sp8, x);
84   E1(p14, x, p16);
85   E1(p14+7, x, p16+8);
86 }
87
88 void E_P24(uchar *p21, uchar *c8, uchar *p24)
89 {
90   E1(p21, c8, p24);
91   E1(p21+7, c8, p24+8);
92   E1(p21+14, c8, p24+16);
93 }
94
95
96 /*
97    This implements the X/Open SMB password encryption
98    It takes a password, a 8 byte "crypt key" and puts 24 bytes of 
99    encrypted password into p24 */
100 void SMBencrypt(uchar *passwd, uchar *c8, uchar *p24)
101 {
102   uchar p14[15], p21[21];
103
104   memset(p21,'\0',21);
105   memset(p14,'\0',14);
106   StrnCpy((char *)p14,(char *)passwd,14);
107
108   strupper((char *)p14);
109   E_P16(p14, p21); 
110   E_P24(p21, c8, p24);
111 }
112
113 /* Routines for Windows NT MD4 Hash functions. */
114 static int _my_wcslen(int16 *str)
115 {
116         int len = 0;
117         while(*str++ != 0)
118                 len++;
119         return len;
120 }
121
122 /*
123  * Convert a string into an NT UNICODE string.
124  * Note that regardless of processor type 
125  * this must be in intel (little-endian)
126  * format.
127  */
128  
129 static int _my_mbstowcs(int16 *dst, uchar *src, int len)
130 {
131         int i;
132         int16 val;
133  
134         for(i = 0; i < len; i++) {
135                 val = *src;
136                 SSVAL(dst,0,val);
137                 dst++;
138                 src++;
139                 if(val == 0)
140                         break;
141         }
142         return i;
143 }
144
145 /* 
146  * Creates the MD4 Hash of the users password in NT UNICODE.
147  */
148  
149 void E_md4hash(uchar *passwd, uchar *p16)
150 {
151         int i, len;
152         int16 wpwd[129];
153         MDstruct MD;
154  
155         /* Password cannot be longer than 128 characters */
156         len = strlen((char *)passwd);
157         if(len > 128)
158                 len = 128;
159         /* Password must be converted to NT unicode */
160         _my_mbstowcs( wpwd, passwd, len);
161         wpwd[len] = 0; /* Ensure string is null terminated */
162         /* Calculate length in bytes */
163         len = _my_wcslen(wpwd) * sizeof(int16);
164  
165         MDbegin(&MD);
166         for(i = 0; i + 64 <= len; i += 64)
167                 MDupdate(&MD,wpwd + (i/2), 512);
168         MDupdate(&MD,wpwd + (i/2),(len-i)*8);
169         SIVAL(p16,0,MD.buffer[0]);
170         SIVAL(p16,4,MD.buffer[1]);
171         SIVAL(p16,8,MD.buffer[2]);
172         SIVAL(p16,12,MD.buffer[3]);
173 }
174
175 /* Does the NT MD4 hash then des encryption. */
176  
177 void SMBNTencrypt(uchar *passwd, uchar *c8, uchar *p24)
178 {
179         uchar p21[21];
180  
181         memset(p21,'\0',21);
182  
183         E_md4hash(passwd, p21);    
184         E_P24(p21, c8, p24);
185 }
186
187 #else
188  void smbencrypt_dummy(void){}
189 #endif