smbencrypt.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. SMB parameters and setup
  5. Copyright (C) Andrew Tridgell 1992-2000
  6. Copyright (C) Luke Kenneth Casson Leighton 1996-2000
  7. Modified by Jeremy Allison 1995.
  8. Copyright (C) Andrew Bartlett <abartlet@samba.org> 2002-2003
  9. Modified by Steve French (sfrench@us.ibm.com) 2002-2003
  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. 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. 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. #include <linux/module.h>
  23. #include <linux/fs.h>
  24. #include <linux/string.h>
  25. #include <linux/kernel.h>
  26. #include <linux/random.h>
  27. #include "cifs_unicode.h"
  28. #include "cifspdu.h"
  29. #include "md5.h"
  30. #include "cifs_debug.h"
  31. #include "cifsencrypt.h"
  32. #ifndef FALSE
  33. #define FALSE 0
  34. #endif
  35. #ifndef TRUE
  36. #define TRUE 1
  37. #endif
  38. /* following came from the other byteorder.h to avoid include conflicts */
  39. #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
  40. #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
  41. #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((__u16)(val)))
  42. /*The following definitions come from libsmb/smbencrypt.c */
  43. void SMBencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24);
  44. void E_md4hash(const unsigned char *passwd, unsigned char *p16);
  45. void nt_lm_owf_gen(char *pwd, unsigned char nt_p16[16], unsigned char p16[16]);
  46. static void SMBOWFencrypt(unsigned char passwd[16], unsigned char *c8,
  47. unsigned char p24[24]);
  48. void NTLMSSPOWFencrypt(unsigned char passwd[8],
  49. unsigned char *ntlmchalresp, unsigned char p24[24]);
  50. void SMBNTencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24);
  51. /*
  52. This implements the X/Open SMB password encryption
  53. It takes a password, a 8 byte "crypt key" and puts 24 bytes of
  54. encrypted password into p24 */
  55. /* Note that password must be uppercased and null terminated */
  56. void
  57. SMBencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24)
  58. {
  59. unsigned char p14[15], p21[21];
  60. memset(p21, '\0', 21);
  61. memset(p14, '\0', 14);
  62. strncpy((char *) p14, (char *) passwd, 14);
  63. /* strupper((char *)p14); *//* BB at least uppercase the easy range */
  64. E_P16(p14, p21);
  65. SMBOWFencrypt(p21, c8, p24);
  66. memset(p14,0,15);
  67. memset(p21,0,21);
  68. }
  69. /* Routines for Windows NT MD4 Hash functions. */
  70. static int
  71. _my_wcslen(__u16 * str)
  72. {
  73. int len = 0;
  74. while (*str++ != 0)
  75. len++;
  76. return len;
  77. }
  78. /*
  79. * Convert a string into an NT UNICODE string.
  80. * Note that regardless of processor type
  81. * this must be in intel (little-endian)
  82. * format.
  83. */
  84. static int
  85. _my_mbstowcs(__u16 * dst, const unsigned char *src, int len)
  86. { /* not a very good conversion routine - change/fix */
  87. int i;
  88. __u16 val;
  89. for (i = 0; i < len; i++) {
  90. val = *src;
  91. SSVAL(dst, 0, val);
  92. dst++;
  93. src++;
  94. if (val == 0)
  95. break;
  96. }
  97. return i;
  98. }
  99. /*
  100. * Creates the MD4 Hash of the users password in NT UNICODE.
  101. */
  102. void
  103. E_md4hash(const unsigned char *passwd, unsigned char *p16)
  104. {
  105. int len;
  106. __u16 wpwd[129];
  107. /* Password cannot be longer than 128 characters */
  108. if(passwd) {
  109. len = strlen((char *) passwd);
  110. if (len > 128) {
  111. len = 128;
  112. }
  113. /* Password must be converted to NT unicode */
  114. _my_mbstowcs(wpwd, passwd, len);
  115. } else
  116. len = 0;
  117. wpwd[len] = 0; /* Ensure string is null terminated */
  118. /* Calculate length in bytes */
  119. len = _my_wcslen(wpwd) * sizeof (__u16);
  120. mdfour(p16, (unsigned char *) wpwd, len);
  121. memset(wpwd,0,129 * 2);
  122. }
  123. /* Does both the NT and LM owfs of a user's password */
  124. void
  125. nt_lm_owf_gen(char *pwd, unsigned char nt_p16[16], unsigned char p16[16])
  126. {
  127. char passwd[514];
  128. memset(passwd, '\0', 514);
  129. if (strlen(pwd) < 513)
  130. strcpy(passwd, pwd);
  131. else
  132. memcpy(passwd, pwd, 512);
  133. /* Calculate the MD4 hash (NT compatible) of the password */
  134. memset(nt_p16, '\0', 16);
  135. E_md4hash(passwd, nt_p16);
  136. /* Mangle the passwords into Lanman format */
  137. passwd[14] = '\0';
  138. /* strupper(passwd); */
  139. /* Calculate the SMB (lanman) hash functions of the password */
  140. memset(p16, '\0', 16);
  141. E_P16((unsigned char *) passwd, (unsigned char *) p16);
  142. /* clear out local copy of user's password (just being paranoid). */
  143. memset(passwd, '\0', sizeof (passwd));
  144. }
  145. /* Does the NTLMv2 owfs of a user's password */
  146. #if 0 /* function not needed yet - but will be soon */
  147. static void
  148. ntv2_owf_gen(const unsigned char owf[16], const char *user_n,
  149. const char *domain_n, unsigned char kr_buf[16],
  150. const struct nls_table *nls_codepage)
  151. {
  152. wchar_t * user_u;
  153. wchar_t * dom_u;
  154. int user_l, domain_l;
  155. struct HMACMD5Context ctx;
  156. /* might as well do one alloc to hold both (user_u and dom_u) */
  157. user_u = kmalloc(2048 * sizeof(wchar_t),GFP_KERNEL);
  158. if(user_u == NULL)
  159. return;
  160. dom_u = user_u + 1024;
  161. /* push_ucs2(NULL, user_u, user_n, (user_l+1)*2, STR_UNICODE|STR_NOALIGN|STR_TERMINATE|STR_UPPER);
  162. push_ucs2(NULL, dom_u, domain_n, (domain_l+1)*2, STR_UNICODE|STR_NOALIGN|STR_TERMINATE|STR_UPPER); */
  163. /* BB user and domain may need to be uppercased */
  164. user_l = cifs_strtoUCS(user_u, user_n, 511, nls_codepage);
  165. domain_l = cifs_strtoUCS(dom_u, domain_n, 511, nls_codepage);
  166. user_l++; /* trailing null */
  167. domain_l++;
  168. hmac_md5_init_limK_to_64(owf, 16, &ctx);
  169. hmac_md5_update((const unsigned char *) user_u, user_l * 2, &ctx);
  170. hmac_md5_update((const unsigned char *) dom_u, domain_l * 2, &ctx);
  171. hmac_md5_final(kr_buf, &ctx);
  172. kfree(user_u);
  173. }
  174. #endif
  175. /* Does the des encryption from the NT or LM MD4 hash. */
  176. static void
  177. SMBOWFencrypt(unsigned char passwd[16], unsigned char *c8,
  178. unsigned char p24[24])
  179. {
  180. unsigned char p21[21];
  181. memset(p21, '\0', 21);
  182. memcpy(p21, passwd, 16);
  183. E_P24(p21, c8, p24);
  184. }
  185. /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
  186. void
  187. NTLMSSPOWFencrypt(unsigned char passwd[8],
  188. unsigned char *ntlmchalresp, unsigned char p24[24])
  189. {
  190. unsigned char p21[21];
  191. memset(p21, '\0', 21);
  192. memcpy(p21, passwd, 8);
  193. memset(p21 + 8, 0xbd, 8);
  194. E_P24(p21, ntlmchalresp, p24);
  195. }
  196. /* Does the NT MD4 hash then des encryption. */
  197. void
  198. SMBNTencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24)
  199. {
  200. unsigned char p21[21];
  201. memset(p21, '\0', 21);
  202. E_md4hash(passwd, p21);
  203. SMBOWFencrypt(p21, c8, p24);
  204. }
  205. /* Does the md5 encryption from the NT hash for NTLMv2. */
  206. /* These routines will be needed later */
  207. #if 0
  208. static void
  209. SMBOWFencrypt_ntv2(const unsigned char kr[16],
  210. const struct data_blob * srv_chal,
  211. const struct data_blob * cli_chal, unsigned char resp_buf[16])
  212. {
  213. struct HMACMD5Context ctx;
  214. hmac_md5_init_limK_to_64(kr, 16, &ctx);
  215. hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);
  216. hmac_md5_update(cli_chal->data, cli_chal->length, &ctx);
  217. hmac_md5_final(resp_buf, &ctx);
  218. }
  219. static void
  220. SMBsesskeygen_ntv2(const unsigned char kr[16],
  221. const unsigned char *nt_resp, __u8 sess_key[16])
  222. {
  223. struct HMACMD5Context ctx;
  224. hmac_md5_init_limK_to_64(kr, 16, &ctx);
  225. hmac_md5_update(nt_resp, 16, &ctx);
  226. hmac_md5_final((unsigned char *) sess_key, &ctx);
  227. }
  228. static void
  229. SMBsesskeygen_ntv1(const unsigned char kr[16],
  230. const unsigned char *nt_resp, __u8 sess_key[16])
  231. {
  232. mdfour((unsigned char *) sess_key, (unsigned char *) kr, 16);
  233. }
  234. #endif