3 Unix SMB/Netbios implementation.
5 SMB parameters and setup
6 Copyright (C) Andrew Tridgell 1992-1995
7 Modified by Jeremy Allison 1995.
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.
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.
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.
28 extern int DEBUGLEVEL
;
30 #include "byteorder.h"
32 void str_to_key(uchar
*str
,uchar
*key
)
34 void des_set_odd_parity(des_cblock
*);
38 key
[1] = ((str
[0]&0x01)<<6) | (str
[1]>>2);
39 key
[2] = ((str
[1]&0x03)<<5) | (str
[2]>>3);
40 key
[3] = ((str
[2]&0x07)<<4) | (str
[3]>>4);
41 key
[4] = ((str
[3]&0x0F)<<3) | (str
[4]>>5);
42 key
[5] = ((str
[4]&0x1F)<<2) | (str
[5]>>6);
43 key
[6] = ((str
[5]&0x3F)<<1) | (str
[6]>>7);
48 des_set_odd_parity((des_cblock
*)key
);
51 void D1(uchar
*k
, uchar
*d
, uchar
*out
)
56 str_to_key(k
,(uchar
*)deskey
);
57 des_set_key(deskey
,ks
);
58 des_ecb_encrypt(d
, out
, ks
, DES_DECRYPT
);
61 void E1(uchar
*k
, uchar
*d
, uchar
*out
)
66 str_to_key(k
,(uchar
*)deskey
);
67 des_set_key(deskey
,ks
);
68 des_ecb_encrypt(d
, out
, ks
, DES_ENCRYPT
);
71 void E_P16(uchar
*p14
,uchar
*p16
)
74 /* the following constant makes us compatible with other
75 implementations. Note that publishing this constant does not reduce the
76 security of the encryption mechanism */
77 uchar sp8
[] = {0xAA,0xD3,0xB4,0x35,0xB5,0x14,0x4,0xEE};
87 void E_P24(uchar
*p21
, uchar
*c8
, uchar
*p24
)
91 E1(p21
+14, c8
, p24
+16);
96 This implements the X/Open SMB password encryption
97 It takes a password, a 8 byte "crypt key" and puts 24 bytes of
98 encrypted password into p24 */
99 void SMBencrypt(uchar
*passwd
, uchar
*c8
, uchar
*p24
)
101 uchar p14
[15], p21
[21];
105 StrnCpy((char *)p14
,(char *)passwd
,14);
107 strupper((char *)p14
);
112 /* Routines for Windows NT MD4 Hash functions. */
113 static int _my_wcslen(int16
*str
)
122 * Convert a string into an NT UNICODE string.
123 * Note that regardless of processor type
124 * this must be in intel (little-endian)
128 static int _my_mbstowcs(int16
*dst
, uchar
*src
, int len
)
133 for(i
= 0; i
< len
; i
++) {
145 * Creates the MD4 Hash of the users password in NT UNICODE.
148 void E_md4hash(uchar
*passwd
, uchar
*p16
)
154 /* Password cannot be longer than 128 characters */
155 len
= strlen((char *)passwd
);
158 /* Password must be converted to NT unicode */
159 _my_mbstowcs( wpwd
, passwd
, len
);
160 wpwd
[len
] = 0; /* Ensure string is null terminated */
161 /* Calculate length in bytes */
162 len
= _my_wcslen(wpwd
) * sizeof(int16
);
165 for(i
= 0; i
+ 64 <= len
; i
+= 64)
166 MDupdate(&MD
,wpwd
+ (i
/2), 512);
167 MDupdate(&MD
,wpwd
+ (i
/2),(len
-i
)*8);
168 SIVAL(p16
,0,MD
.buffer
[0]);
169 SIVAL(p16
,4,MD
.buffer
[1]);
170 SIVAL(p16
,8,MD
.buffer
[2]);
171 SIVAL(p16
,12,MD
.buffer
[3]);
174 /* Does the NT MD4 hash then des encryption. */
176 void SMBNTencrypt(uchar
*passwd
, uchar
*c8
, uchar
*p24
)
182 E_md4hash(passwd
, p21
);
187 void smbencrypt_dummy(void){}