smbencrypt.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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/slab.h>
  24. #include <linux/fs.h>
  25. #include <linux/string.h>
  26. #include <linux/kernel.h>
  27. #include <linux/random.h>
  28. #include "cifs_unicode.h"
  29. #include "cifspdu.h"
  30. #include "cifsglob.h"
  31. #include "cifs_debug.h"
  32. #include "cifsproto.h"
  33. #ifndef false
  34. #define false 0
  35. #endif
  36. #ifndef true
  37. #define true 1
  38. #endif
  39. /* following came from the other byteorder.h to avoid include conflicts */
  40. #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
  41. #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
  42. #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((__u16)(val)))
  43. static void
  44. str_to_key(unsigned char *str, unsigned char *key)
  45. {
  46. int i;
  47. key[0] = str[0] >> 1;
  48. key[1] = ((str[0] & 0x01) << 6) | (str[1] >> 2);
  49. key[2] = ((str[1] & 0x03) << 5) | (str[2] >> 3);
  50. key[3] = ((str[2] & 0x07) << 4) | (str[3] >> 4);
  51. key[4] = ((str[3] & 0x0F) << 3) | (str[4] >> 5);
  52. key[5] = ((str[4] & 0x1F) << 2) | (str[5] >> 6);
  53. key[6] = ((str[5] & 0x3F) << 1) | (str[6] >> 7);
  54. key[7] = str[6] & 0x7F;
  55. for (i = 0; i < 8; i++)
  56. key[i] = (key[i] << 1);
  57. }
  58. static int
  59. smbhash(unsigned char *out, const unsigned char *in, unsigned char *key)
  60. {
  61. int rc;
  62. unsigned char key2[8];
  63. struct crypto_blkcipher *tfm_des;
  64. struct scatterlist sgin, sgout;
  65. struct blkcipher_desc desc;
  66. str_to_key(key, key2);
  67. tfm_des = crypto_alloc_blkcipher("ecb(des)", 0, CRYPTO_ALG_ASYNC);
  68. if (IS_ERR(tfm_des)) {
  69. rc = PTR_ERR(tfm_des);
  70. cERROR(1, "could not allocate des crypto API\n");
  71. goto smbhash_err;
  72. }
  73. desc.tfm = tfm_des;
  74. crypto_blkcipher_setkey(tfm_des, key2, 8);
  75. sg_init_one(&sgin, in, 8);
  76. sg_init_one(&sgout, out, 8);
  77. rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, 8);
  78. if (rc)
  79. cERROR(1, "could not encrypt crypt key rc: %d\n", rc);
  80. crypto_free_blkcipher(tfm_des);
  81. smbhash_err:
  82. return rc;
  83. }
  84. static int
  85. E_P16(unsigned char *p14, unsigned char *p16)
  86. {
  87. int rc;
  88. unsigned char sp8[8] =
  89. { 0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 };
  90. rc = smbhash(p16, sp8, p14);
  91. if (rc)
  92. return rc;
  93. rc = smbhash(p16 + 8, sp8, p14 + 7);
  94. return rc;
  95. }
  96. static int
  97. E_P24(unsigned char *p21, const unsigned char *c8, unsigned char *p24)
  98. {
  99. int rc;
  100. rc = smbhash(p24, c8, p21);
  101. if (rc)
  102. return rc;
  103. rc = smbhash(p24 + 8, c8, p21 + 7);
  104. if (rc)
  105. return rc;
  106. rc = smbhash(p24 + 16, c8, p21 + 14);
  107. return rc;
  108. }
  109. /* produce a md4 message digest from data of length n bytes */
  110. int
  111. mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len)
  112. {
  113. int rc;
  114. unsigned int size;
  115. struct crypto_shash *md4;
  116. struct sdesc *sdescmd4;
  117. md4 = crypto_alloc_shash("md4", 0, 0);
  118. if (IS_ERR(md4)) {
  119. rc = PTR_ERR(md4);
  120. cERROR(1, "%s: Crypto md4 allocation error %d\n", __func__, rc);
  121. return rc;
  122. }
  123. size = sizeof(struct shash_desc) + crypto_shash_descsize(md4);
  124. sdescmd4 = kmalloc(size, GFP_KERNEL);
  125. if (!sdescmd4) {
  126. rc = -ENOMEM;
  127. cERROR(1, "%s: Memory allocation failure\n", __func__);
  128. goto mdfour_err;
  129. }
  130. sdescmd4->shash.tfm = md4;
  131. sdescmd4->shash.flags = 0x0;
  132. rc = crypto_shash_init(&sdescmd4->shash);
  133. if (rc) {
  134. cERROR(1, "%s: Could not init md4 shash\n", __func__);
  135. goto mdfour_err;
  136. }
  137. rc = crypto_shash_update(&sdescmd4->shash, link_str, link_len);
  138. if (rc) {
  139. cERROR(1, "%s: Could not update with link_str\n", __func__);
  140. goto mdfour_err;
  141. }
  142. rc = crypto_shash_final(&sdescmd4->shash, md4_hash);
  143. if (rc)
  144. cERROR(1, "%s: Could not genereate md4 hash\n", __func__);
  145. mdfour_err:
  146. crypto_free_shash(md4);
  147. kfree(sdescmd4);
  148. return rc;
  149. }
  150. /*
  151. This implements the X/Open SMB password encryption
  152. It takes a password, a 8 byte "crypt key" and puts 24 bytes of
  153. encrypted password into p24 */
  154. /* Note that password must be uppercased and null terminated */
  155. int
  156. SMBencrypt(unsigned char *passwd, const unsigned char *c8, unsigned char *p24)
  157. {
  158. int rc;
  159. unsigned char p14[14], p16[16], p21[21];
  160. memset(p14, '\0', 14);
  161. memset(p16, '\0', 16);
  162. memset(p21, '\0', 21);
  163. memcpy(p14, passwd, 14);
  164. rc = E_P16(p14, p16);
  165. if (rc)
  166. return rc;
  167. memcpy(p21, p16, 16);
  168. rc = E_P24(p21, c8, p24);
  169. return rc;
  170. }
  171. /* Routines for Windows NT MD4 Hash functions. */
  172. static int
  173. _my_wcslen(__u16 *str)
  174. {
  175. int len = 0;
  176. while (*str++ != 0)
  177. len++;
  178. return len;
  179. }
  180. /*
  181. * Convert a string into an NT UNICODE string.
  182. * Note that regardless of processor type
  183. * this must be in intel (little-endian)
  184. * format.
  185. */
  186. static int
  187. _my_mbstowcs(__u16 *dst, const unsigned char *src, int len)
  188. { /* BB not a very good conversion routine - change/fix */
  189. int i;
  190. __u16 val;
  191. for (i = 0; i < len; i++) {
  192. val = *src;
  193. SSVAL(dst, 0, val);
  194. dst++;
  195. src++;
  196. if (val == 0)
  197. break;
  198. }
  199. return i;
  200. }
  201. /*
  202. * Creates the MD4 Hash of the users password in NT UNICODE.
  203. */
  204. int
  205. E_md4hash(const unsigned char *passwd, unsigned char *p16)
  206. {
  207. int rc;
  208. int len;
  209. __u16 wpwd[129];
  210. /* Password cannot be longer than 128 characters */
  211. if (passwd) {
  212. len = strlen((char *) passwd);
  213. if (len > 128)
  214. len = 128;
  215. /* Password must be converted to NT unicode */
  216. _my_mbstowcs(wpwd, passwd, len);
  217. } else
  218. len = 0;
  219. wpwd[len] = 0; /* Ensure string is null terminated */
  220. /* Calculate length in bytes */
  221. len = _my_wcslen(wpwd) * sizeof(__u16);
  222. rc = mdfour(p16, (unsigned char *) wpwd, len);
  223. memset(wpwd, 0, 129 * 2);
  224. return rc;
  225. }
  226. #if 0 /* currently unused */
  227. /* Does both the NT and LM owfs of a user's password */
  228. static void
  229. nt_lm_owf_gen(char *pwd, unsigned char nt_p16[16], unsigned char p16[16])
  230. {
  231. char passwd[514];
  232. memset(passwd, '\0', 514);
  233. if (strlen(pwd) < 513)
  234. strcpy(passwd, pwd);
  235. else
  236. memcpy(passwd, pwd, 512);
  237. /* Calculate the MD4 hash (NT compatible) of the password */
  238. memset(nt_p16, '\0', 16);
  239. E_md4hash(passwd, nt_p16);
  240. /* Mangle the passwords into Lanman format */
  241. passwd[14] = '\0';
  242. /* strupper(passwd); */
  243. /* Calculate the SMB (lanman) hash functions of the password */
  244. memset(p16, '\0', 16);
  245. E_P16((unsigned char *) passwd, (unsigned char *) p16);
  246. /* clear out local copy of user's password (just being paranoid). */
  247. memset(passwd, '\0', sizeof(passwd));
  248. }
  249. #endif
  250. /* Does the NTLMv2 owfs of a user's password */
  251. #if 0 /* function not needed yet - but will be soon */
  252. static void
  253. ntv2_owf_gen(const unsigned char owf[16], const char *user_n,
  254. const char *domain_n, unsigned char kr_buf[16],
  255. const struct nls_table *nls_codepage)
  256. {
  257. wchar_t *user_u;
  258. wchar_t *dom_u;
  259. int user_l, domain_l;
  260. struct HMACMD5Context ctx;
  261. /* might as well do one alloc to hold both (user_u and dom_u) */
  262. user_u = kmalloc(2048 * sizeof(wchar_t), GFP_KERNEL);
  263. if (user_u == NULL)
  264. return;
  265. dom_u = user_u + 1024;
  266. /* push_ucs2(NULL, user_u, user_n, (user_l+1)*2,
  267. STR_UNICODE|STR_NOALIGN|STR_TERMINATE|STR_UPPER);
  268. push_ucs2(NULL, dom_u, domain_n, (domain_l+1)*2,
  269. STR_UNICODE|STR_NOALIGN|STR_TERMINATE|STR_UPPER); */
  270. /* BB user and domain may need to be uppercased */
  271. user_l = cifs_strtoUCS(user_u, user_n, 511, nls_codepage);
  272. domain_l = cifs_strtoUCS(dom_u, domain_n, 511, nls_codepage);
  273. user_l++; /* trailing null */
  274. domain_l++;
  275. hmac_md5_init_limK_to_64(owf, 16, &ctx);
  276. hmac_md5_update((const unsigned char *) user_u, user_l * 2, &ctx);
  277. hmac_md5_update((const unsigned char *) dom_u, domain_l * 2, &ctx);
  278. hmac_md5_final(kr_buf, &ctx);
  279. kfree(user_u);
  280. }
  281. #endif
  282. /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
  283. #if 0 /* currently unused */
  284. static void
  285. NTLMSSPOWFencrypt(unsigned char passwd[8],
  286. unsigned char *ntlmchalresp, unsigned char p24[24])
  287. {
  288. unsigned char p21[21];
  289. memset(p21, '\0', 21);
  290. memcpy(p21, passwd, 8);
  291. memset(p21 + 8, 0xbd, 8);
  292. E_P24(p21, ntlmchalresp, p24);
  293. }
  294. #endif
  295. /* Does the NT MD4 hash then des encryption. */
  296. int
  297. SMBNTencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24)
  298. {
  299. int rc;
  300. unsigned char p16[16], p21[21];
  301. memset(p16, '\0', 16);
  302. memset(p21, '\0', 21);
  303. rc = E_md4hash(passwd, p16);
  304. if (rc) {
  305. cFYI(1, "%s Can't generate NT hash, error: %d", __func__, rc);
  306. return rc;
  307. }
  308. memcpy(p21, p16, 16);
  309. rc = E_P24(p21, c8, p24);
  310. return rc;
  311. }
  312. /* Does the md5 encryption from the NT hash for NTLMv2. */
  313. /* These routines will be needed later */
  314. #if 0
  315. static void
  316. SMBOWFencrypt_ntv2(const unsigned char kr[16],
  317. const struct data_blob *srv_chal,
  318. const struct data_blob *cli_chal, unsigned char resp_buf[16])
  319. {
  320. struct HMACMD5Context ctx;
  321. hmac_md5_init_limK_to_64(kr, 16, &ctx);
  322. hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);
  323. hmac_md5_update(cli_chal->data, cli_chal->length, &ctx);
  324. hmac_md5_final(resp_buf, &ctx);
  325. }
  326. static void
  327. SMBsesskeygen_ntv2(const unsigned char kr[16],
  328. const unsigned char *nt_resp, __u8 sess_key[16])
  329. {
  330. struct HMACMD5Context ctx;
  331. hmac_md5_init_limK_to_64(kr, 16, &ctx);
  332. hmac_md5_update(nt_resp, 16, &ctx);
  333. hmac_md5_final((unsigned char *) sess_key, &ctx);
  334. }
  335. static void
  336. SMBsesskeygen_ntv1(const unsigned char kr[16],
  337. const unsigned char *nt_resp, __u8 sess_key[16])
  338. {
  339. mdfour((unsigned char *) sess_key, (unsigned char *) kr, 16);
  340. }
  341. #endif