smbencrypt.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. crypto_shash_update(&sdescmd4->shash, link_str, link_len);
  138. rc = crypto_shash_final(&sdescmd4->shash, md4_hash);
  139. mdfour_err:
  140. crypto_free_shash(md4);
  141. kfree(sdescmd4);
  142. return rc;
  143. }
  144. /*
  145. This implements the X/Open SMB password encryption
  146. It takes a password, a 8 byte "crypt key" and puts 24 bytes of
  147. encrypted password into p24 */
  148. /* Note that password must be uppercased and null terminated */
  149. int
  150. SMBencrypt(unsigned char *passwd, const unsigned char *c8, unsigned char *p24)
  151. {
  152. int rc;
  153. unsigned char p14[14], p16[16], p21[21];
  154. memset(p14, '\0', 14);
  155. memset(p16, '\0', 16);
  156. memset(p21, '\0', 21);
  157. memcpy(p14, passwd, 14);
  158. rc = E_P16(p14, p16);
  159. if (rc)
  160. return rc;
  161. memcpy(p21, p16, 16);
  162. rc = E_P24(p21, c8, p24);
  163. return rc;
  164. }
  165. /* Routines for Windows NT MD4 Hash functions. */
  166. static int
  167. _my_wcslen(__u16 *str)
  168. {
  169. int len = 0;
  170. while (*str++ != 0)
  171. len++;
  172. return len;
  173. }
  174. /*
  175. * Convert a string into an NT UNICODE string.
  176. * Note that regardless of processor type
  177. * this must be in intel (little-endian)
  178. * format.
  179. */
  180. static int
  181. _my_mbstowcs(__u16 *dst, const unsigned char *src, int len)
  182. { /* BB not a very good conversion routine - change/fix */
  183. int i;
  184. __u16 val;
  185. for (i = 0; i < len; i++) {
  186. val = *src;
  187. SSVAL(dst, 0, val);
  188. dst++;
  189. src++;
  190. if (val == 0)
  191. break;
  192. }
  193. return i;
  194. }
  195. /*
  196. * Creates the MD4 Hash of the users password in NT UNICODE.
  197. */
  198. int
  199. E_md4hash(const unsigned char *passwd, unsigned char *p16)
  200. {
  201. int rc;
  202. int len;
  203. __u16 wpwd[129];
  204. /* Password cannot be longer than 128 characters */
  205. if (passwd) {
  206. len = strlen((char *) passwd);
  207. if (len > 128)
  208. len = 128;
  209. /* Password must be converted to NT unicode */
  210. _my_mbstowcs(wpwd, passwd, len);
  211. } else
  212. len = 0;
  213. wpwd[len] = 0; /* Ensure string is null terminated */
  214. /* Calculate length in bytes */
  215. len = _my_wcslen(wpwd) * sizeof(__u16);
  216. rc = mdfour(p16, (unsigned char *) wpwd, len);
  217. memset(wpwd, 0, 129 * 2);
  218. return rc;
  219. }
  220. #if 0 /* currently unused */
  221. /* Does both the NT and LM owfs of a user's password */
  222. static void
  223. nt_lm_owf_gen(char *pwd, unsigned char nt_p16[16], unsigned char p16[16])
  224. {
  225. char passwd[514];
  226. memset(passwd, '\0', 514);
  227. if (strlen(pwd) < 513)
  228. strcpy(passwd, pwd);
  229. else
  230. memcpy(passwd, pwd, 512);
  231. /* Calculate the MD4 hash (NT compatible) of the password */
  232. memset(nt_p16, '\0', 16);
  233. E_md4hash(passwd, nt_p16);
  234. /* Mangle the passwords into Lanman format */
  235. passwd[14] = '\0';
  236. /* strupper(passwd); */
  237. /* Calculate the SMB (lanman) hash functions of the password */
  238. memset(p16, '\0', 16);
  239. E_P16((unsigned char *) passwd, (unsigned char *) p16);
  240. /* clear out local copy of user's password (just being paranoid). */
  241. memset(passwd, '\0', sizeof(passwd));
  242. }
  243. #endif
  244. /* Does the NTLMv2 owfs of a user's password */
  245. #if 0 /* function not needed yet - but will be soon */
  246. static void
  247. ntv2_owf_gen(const unsigned char owf[16], const char *user_n,
  248. const char *domain_n, unsigned char kr_buf[16],
  249. const struct nls_table *nls_codepage)
  250. {
  251. wchar_t *user_u;
  252. wchar_t *dom_u;
  253. int user_l, domain_l;
  254. struct HMACMD5Context ctx;
  255. /* might as well do one alloc to hold both (user_u and dom_u) */
  256. user_u = kmalloc(2048 * sizeof(wchar_t), GFP_KERNEL);
  257. if (user_u == NULL)
  258. return;
  259. dom_u = user_u + 1024;
  260. /* push_ucs2(NULL, user_u, user_n, (user_l+1)*2,
  261. STR_UNICODE|STR_NOALIGN|STR_TERMINATE|STR_UPPER);
  262. push_ucs2(NULL, dom_u, domain_n, (domain_l+1)*2,
  263. STR_UNICODE|STR_NOALIGN|STR_TERMINATE|STR_UPPER); */
  264. /* BB user and domain may need to be uppercased */
  265. user_l = cifs_strtoUCS(user_u, user_n, 511, nls_codepage);
  266. domain_l = cifs_strtoUCS(dom_u, domain_n, 511, nls_codepage);
  267. user_l++; /* trailing null */
  268. domain_l++;
  269. hmac_md5_init_limK_to_64(owf, 16, &ctx);
  270. hmac_md5_update((const unsigned char *) user_u, user_l * 2, &ctx);
  271. hmac_md5_update((const unsigned char *) dom_u, domain_l * 2, &ctx);
  272. hmac_md5_final(kr_buf, &ctx);
  273. kfree(user_u);
  274. }
  275. #endif
  276. /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
  277. #if 0 /* currently unused */
  278. static void
  279. NTLMSSPOWFencrypt(unsigned char passwd[8],
  280. unsigned char *ntlmchalresp, unsigned char p24[24])
  281. {
  282. unsigned char p21[21];
  283. memset(p21, '\0', 21);
  284. memcpy(p21, passwd, 8);
  285. memset(p21 + 8, 0xbd, 8);
  286. E_P24(p21, ntlmchalresp, p24);
  287. }
  288. #endif
  289. /* Does the NT MD4 hash then des encryption. */
  290. int
  291. SMBNTencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24)
  292. {
  293. int rc;
  294. unsigned char p16[16], p21[21];
  295. memset(p16, '\0', 16);
  296. memset(p21, '\0', 21);
  297. rc = E_md4hash(passwd, p16);
  298. if (rc) {
  299. cFYI(1, "%s Can't generate NT hash, error: %d", __func__, rc);
  300. return rc;
  301. }
  302. memcpy(p21, p16, 16);
  303. rc = E_P24(p21, c8, p24);
  304. return rc;
  305. }
  306. /* Does the md5 encryption from the NT hash for NTLMv2. */
  307. /* These routines will be needed later */
  308. #if 0
  309. static void
  310. SMBOWFencrypt_ntv2(const unsigned char kr[16],
  311. const struct data_blob *srv_chal,
  312. const struct data_blob *cli_chal, unsigned char resp_buf[16])
  313. {
  314. struct HMACMD5Context ctx;
  315. hmac_md5_init_limK_to_64(kr, 16, &ctx);
  316. hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);
  317. hmac_md5_update(cli_chal->data, cli_chal->length, &ctx);
  318. hmac_md5_final(resp_buf, &ctx);
  319. }
  320. static void
  321. SMBsesskeygen_ntv2(const unsigned char kr[16],
  322. const unsigned char *nt_resp, __u8 sess_key[16])
  323. {
  324. struct HMACMD5Context ctx;
  325. hmac_md5_init_limK_to_64(kr, 16, &ctx);
  326. hmac_md5_update(nt_resp, 16, &ctx);
  327. hmac_md5_final((unsigned char *) sess_key, &ctx);
  328. }
  329. static void
  330. SMBsesskeygen_ntv1(const unsigned char kr[16],
  331. const unsigned char *nt_resp, __u8 sess_key[16])
  332. {
  333. mdfour((unsigned char *) sess_key, (unsigned char *) kr, 16);
  334. }
  335. #endif