smbencrypt.c 9.7 KB

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