smbencrypt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. /*
  172. * Creates the MD4 Hash of the users password in NT UNICODE.
  173. */
  174. int
  175. E_md4hash(const unsigned char *passwd, unsigned char *p16,
  176. const struct nls_table *codepage)
  177. {
  178. int rc;
  179. int len;
  180. __le16 wpwd[129];
  181. /* Password cannot be longer than 128 characters */
  182. if (passwd) /* Password must be converted to NT unicode */
  183. len = cifs_strtoUTF16(wpwd, passwd, 128, codepage);
  184. else {
  185. len = 0;
  186. *wpwd = 0; /* Ensure string is null terminated */
  187. }
  188. rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16));
  189. memset(wpwd, 0, 129 * sizeof(__le16));
  190. return rc;
  191. }
  192. /* Does the NT MD4 hash then des encryption. */
  193. int
  194. SMBNTencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24,
  195. const struct nls_table *codepage)
  196. {
  197. int rc;
  198. unsigned char p16[16], p21[21];
  199. memset(p16, '\0', 16);
  200. memset(p21, '\0', 21);
  201. rc = E_md4hash(passwd, p16, codepage);
  202. if (rc) {
  203. cFYI(1, "%s Can't generate NT hash, error: %d", __func__, rc);
  204. return rc;
  205. }
  206. memcpy(p21, p16, 16);
  207. rc = E_P24(p21, c8, p24);
  208. return rc;
  209. }