cifsencrypt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*
  2. * fs/cifs/cifsencrypt.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2005,2006
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/slab.h>
  23. #include "cifspdu.h"
  24. #include "cifsglob.h"
  25. #include "cifs_debug.h"
  26. #include "md5.h"
  27. #include "cifs_unicode.h"
  28. #include "cifsproto.h"
  29. #include "ntlmssp.h"
  30. #include <linux/ctype.h>
  31. #include <linux/random.h>
  32. /* Calculate and return the CIFS signature based on the mac key and SMB PDU */
  33. /* the 16 byte signature must be allocated by the caller */
  34. /* Note we only use the 1st eight bytes */
  35. /* Note that the smb header signature field on input contains the
  36. sequence number before this function is called */
  37. extern void mdfour(unsigned char *out, unsigned char *in, int n);
  38. extern void E_md4hash(const unsigned char *passwd, unsigned char *p16);
  39. extern void SMBencrypt(unsigned char *passwd, const unsigned char *c8,
  40. unsigned char *p24);
  41. static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu,
  42. struct TCP_Server_Info *server, char *signature)
  43. {
  44. int rc;
  45. if (cifs_pdu == NULL || server == NULL || signature == NULL)
  46. return -EINVAL;
  47. if (!server->ntlmssp.sdescmd5) {
  48. cERROR(1,
  49. "cifs_calculate_signature: can't generate signature\n");
  50. return -1;
  51. }
  52. rc = crypto_shash_init(&server->ntlmssp.sdescmd5->shash);
  53. if (rc) {
  54. cERROR(1, "cifs_calculate_signature: oould not init md5\n");
  55. return rc;
  56. }
  57. if (server->secType == RawNTLMSSP)
  58. crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
  59. server->session_key.data.ntlmv2.key,
  60. CIFS_NTLMV2_SESSKEY_SIZE);
  61. else
  62. crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
  63. (char *)&server->session_key.data,
  64. server->session_key.len);
  65. crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
  66. cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
  67. rc = crypto_shash_final(&server->ntlmssp.sdescmd5->shash, signature);
  68. return rc;
  69. }
  70. int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
  71. __u32 *pexpected_response_sequence_number)
  72. {
  73. int rc = 0;
  74. char smb_signature[20];
  75. if ((cifs_pdu == NULL) || (server == NULL))
  76. return -EINVAL;
  77. if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
  78. return rc;
  79. spin_lock(&GlobalMid_Lock);
  80. cifs_pdu->Signature.Sequence.SequenceNumber =
  81. cpu_to_le32(server->sequence_number);
  82. cifs_pdu->Signature.Sequence.Reserved = 0;
  83. *pexpected_response_sequence_number = server->sequence_number++;
  84. server->sequence_number++;
  85. spin_unlock(&GlobalMid_Lock);
  86. rc = cifs_calculate_signature(cifs_pdu, server, smb_signature);
  87. if (rc)
  88. memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
  89. else
  90. memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
  91. return rc;
  92. }
  93. static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
  94. struct TCP_Server_Info *server, char *signature)
  95. {
  96. int i;
  97. int rc;
  98. if (iov == NULL || server == NULL || signature == NULL)
  99. return -EINVAL;
  100. if (!server->ntlmssp.sdescmd5) {
  101. cERROR(1, "cifs_calc_signature2: can't generate signature\n");
  102. return -1;
  103. }
  104. rc = crypto_shash_init(&server->ntlmssp.sdescmd5->shash);
  105. if (rc) {
  106. cERROR(1, "cifs_calc_signature2: oould not init md5\n");
  107. return rc;
  108. }
  109. if (server->secType == RawNTLMSSP)
  110. crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
  111. server->session_key.data.ntlmv2.key,
  112. CIFS_NTLMV2_SESSKEY_SIZE);
  113. else
  114. crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
  115. (char *)&server->session_key.data,
  116. server->session_key.len);
  117. for (i = 0; i < n_vec; i++) {
  118. if (iov[i].iov_len == 0)
  119. continue;
  120. if (iov[i].iov_base == NULL) {
  121. cERROR(1, "cifs_calc_signature2: null iovec entry");
  122. return -EIO;
  123. }
  124. /* The first entry includes a length field (which does not get
  125. signed that occupies the first 4 bytes before the header */
  126. if (i == 0) {
  127. if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
  128. break; /* nothing to sign or corrupt header */
  129. crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
  130. iov[i].iov_base + 4, iov[i].iov_len - 4);
  131. } else
  132. crypto_shash_update(&server->ntlmssp.sdescmd5->shash,
  133. iov[i].iov_base, iov[i].iov_len);
  134. }
  135. rc = crypto_shash_final(&server->ntlmssp.sdescmd5->shash, signature);
  136. return rc;
  137. }
  138. int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
  139. __u32 *pexpected_response_sequence_number)
  140. {
  141. int rc = 0;
  142. char smb_signature[20];
  143. struct smb_hdr *cifs_pdu = iov[0].iov_base;
  144. if ((cifs_pdu == NULL) || (server == NULL))
  145. return -EINVAL;
  146. if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
  147. return rc;
  148. spin_lock(&GlobalMid_Lock);
  149. cifs_pdu->Signature.Sequence.SequenceNumber =
  150. cpu_to_le32(server->sequence_number);
  151. cifs_pdu->Signature.Sequence.Reserved = 0;
  152. *pexpected_response_sequence_number = server->sequence_number++;
  153. server->sequence_number++;
  154. spin_unlock(&GlobalMid_Lock);
  155. rc = cifs_calc_signature2(iov, n_vec, server, smb_signature);
  156. if (rc)
  157. memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
  158. else
  159. memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
  160. return rc;
  161. }
  162. int cifs_verify_signature(struct smb_hdr *cifs_pdu,
  163. struct TCP_Server_Info *server,
  164. __u32 expected_sequence_number)
  165. {
  166. int rc;
  167. char server_response_sig[8];
  168. char what_we_think_sig_should_be[20];
  169. if (cifs_pdu == NULL || server == NULL)
  170. return -EINVAL;
  171. if (cifs_pdu->Command == SMB_COM_NEGOTIATE)
  172. return 0;
  173. if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
  174. struct smb_com_lock_req *pSMB =
  175. (struct smb_com_lock_req *)cifs_pdu;
  176. if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
  177. return 0;
  178. }
  179. /* BB what if signatures are supposed to be on for session but
  180. server does not send one? BB */
  181. /* Do not need to verify session setups with signature "BSRSPYL " */
  182. if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
  183. cFYI(1, "dummy signature received for smb command 0x%x",
  184. cifs_pdu->Command);
  185. /* save off the origiginal signature so we can modify the smb and check
  186. its signature against what the server sent */
  187. memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
  188. cifs_pdu->Signature.Sequence.SequenceNumber =
  189. cpu_to_le32(expected_sequence_number);
  190. cifs_pdu->Signature.Sequence.Reserved = 0;
  191. rc = cifs_calculate_signature(cifs_pdu, server,
  192. what_we_think_sig_should_be);
  193. if (rc)
  194. return rc;
  195. /* cifs_dump_mem("what we think it should be: ",
  196. what_we_think_sig_should_be, 16); */
  197. if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
  198. return -EACCES;
  199. else
  200. return 0;
  201. }
  202. /* We fill in key by putting in 40 byte array which was allocated by caller */
  203. int cifs_calculate_session_key(struct session_key *key, const char *rn,
  204. const char *password)
  205. {
  206. char temp_key[16];
  207. if ((key == NULL) || (rn == NULL))
  208. return -EINVAL;
  209. E_md4hash(password, temp_key);
  210. mdfour(key->data.ntlm, temp_key, 16);
  211. memcpy(key->data.ntlm+16, rn, CIFS_SESS_KEY_SIZE);
  212. key->len = 40;
  213. return 0;
  214. }
  215. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  216. void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
  217. char *lnm_session_key)
  218. {
  219. int i;
  220. char password_with_pad[CIFS_ENCPWD_SIZE];
  221. memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
  222. if (password)
  223. strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
  224. if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
  225. memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE);
  226. memcpy(lnm_session_key, password_with_pad,
  227. CIFS_ENCPWD_SIZE);
  228. return;
  229. }
  230. /* calculate old style session key */
  231. /* calling toupper is less broken than repeatedly
  232. calling nls_toupper would be since that will never
  233. work for UTF8, but neither handles multibyte code pages
  234. but the only alternative would be converting to UCS-16 (Unicode)
  235. (using a routine something like UniStrupr) then
  236. uppercasing and then converting back from Unicode - which
  237. would only worth doing it if we knew it were utf8. Basically
  238. utf8 and other multibyte codepages each need their own strupper
  239. function since a byte at a time will ont work. */
  240. for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
  241. password_with_pad[i] = toupper(password_with_pad[i]);
  242. SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
  243. /* clear password before we return/free memory */
  244. memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
  245. }
  246. #endif /* CIFS_WEAK_PW_HASH */
  247. static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
  248. const struct nls_table *nls_cp)
  249. {
  250. int rc = 0;
  251. int len;
  252. char nt_hash[CIFS_NTHASH_SIZE];
  253. wchar_t *user;
  254. wchar_t *domain;
  255. wchar_t *server;
  256. if (!ses->server->ntlmssp.sdeschmacmd5) {
  257. cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
  258. return -1;
  259. }
  260. /* calculate md4 hash of password */
  261. E_md4hash(ses->password, nt_hash);
  262. crypto_shash_setkey(ses->server->ntlmssp.hmacmd5, nt_hash,
  263. CIFS_NTHASH_SIZE);
  264. rc = crypto_shash_init(&ses->server->ntlmssp.sdeschmacmd5->shash);
  265. if (rc) {
  266. cERROR(1, "calc_ntlmv2_hash: could not init hmacmd5\n");
  267. return rc;
  268. }
  269. /* convert ses->userName to unicode and uppercase */
  270. len = strlen(ses->userName);
  271. user = kmalloc(2 + (len * 2), GFP_KERNEL);
  272. if (user == NULL) {
  273. cERROR(1, "calc_ntlmv2_hash: user mem alloc failure\n");
  274. rc = -ENOMEM;
  275. goto calc_exit_2;
  276. }
  277. len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp);
  278. UniStrupr(user);
  279. crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash,
  280. (char *)user, 2 * len);
  281. /* convert ses->domainName to unicode and uppercase */
  282. if (ses->domainName) {
  283. len = strlen(ses->domainName);
  284. domain = kmalloc(2 + (len * 2), GFP_KERNEL);
  285. if (domain == NULL) {
  286. cERROR(1, "calc_ntlmv2_hash: domain mem alloc failure");
  287. rc = -ENOMEM;
  288. goto calc_exit_1;
  289. }
  290. len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len,
  291. nls_cp);
  292. /* the following line was removed since it didn't work well
  293. with lower cased domain name that passed as an option.
  294. Maybe converting the domain name earlier makes sense */
  295. /* UniStrupr(domain); */
  296. crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash,
  297. (char *)domain, 2 * len);
  298. kfree(domain);
  299. } else if (ses->serverName) {
  300. len = strlen(ses->serverName);
  301. server = kmalloc(2 + (len * 2), GFP_KERNEL);
  302. if (server == NULL) {
  303. cERROR(1, "calc_ntlmv2_hash: server mem alloc failure");
  304. rc = -ENOMEM;
  305. goto calc_exit_1;
  306. }
  307. len = cifs_strtoUCS((__le16 *)server, ses->serverName, len,
  308. nls_cp);
  309. /* the following line was removed since it didn't work well
  310. with lower cased domain name that passed as an option.
  311. Maybe converting the domain name earlier makes sense */
  312. /* UniStrupr(domain); */
  313. crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash,
  314. (char *)server, 2 * len);
  315. kfree(server);
  316. }
  317. rc = crypto_shash_final(&ses->server->ntlmssp.sdeschmacmd5->shash,
  318. ses->server->ntlmv2_hash);
  319. calc_exit_1:
  320. kfree(user);
  321. calc_exit_2:
  322. /* BB FIXME what about bytes 24 through 40 of the signing key?
  323. compare with the NTLM example */
  324. return rc;
  325. }
  326. static int
  327. find_domain_name(struct cifsSesInfo *ses)
  328. {
  329. int rc = 0;
  330. unsigned int attrsize;
  331. unsigned int type;
  332. unsigned char *blobptr;
  333. struct ntlmssp2_name *attrptr;
  334. if (ses->server->tiblob) {
  335. blobptr = ses->server->tiblob;
  336. attrptr = (struct ntlmssp2_name *) blobptr;
  337. while ((type = attrptr->type) != 0) {
  338. blobptr += 2; /* advance attr type */
  339. attrsize = attrptr->length;
  340. blobptr += 2; /* advance attr size */
  341. if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
  342. if (!ses->domainName) {
  343. ses->domainName =
  344. kmalloc(attrptr->length + 1,
  345. GFP_KERNEL);
  346. if (!ses->domainName)
  347. return -ENOMEM;
  348. cifs_from_ucs2(ses->domainName,
  349. (__le16 *)blobptr,
  350. attrptr->length,
  351. attrptr->length,
  352. load_nls_default(), false);
  353. }
  354. }
  355. blobptr += attrsize; /* advance attr value */
  356. attrptr = (struct ntlmssp2_name *) blobptr;
  357. }
  358. } else {
  359. ses->server->tilen = 2 * sizeof(struct ntlmssp2_name);
  360. ses->server->tiblob = kmalloc(ses->server->tilen, GFP_KERNEL);
  361. if (!ses->server->tiblob) {
  362. ses->server->tilen = 0;
  363. cERROR(1, "Challenge target info allocation failure");
  364. return -ENOMEM;
  365. }
  366. memset(ses->server->tiblob, 0x0, ses->server->tilen);
  367. attrptr = (struct ntlmssp2_name *) ses->server->tiblob;
  368. attrptr->type = cpu_to_le16(NTLMSSP_DOMAIN_TYPE);
  369. }
  370. return rc;
  371. }
  372. static int
  373. CalcNTLMv2_response(const struct TCP_Server_Info *server,
  374. char *v2_session_response)
  375. {
  376. int rc;
  377. if (!server->ntlmssp.sdeschmacmd5) {
  378. cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
  379. return -1;
  380. }
  381. crypto_shash_setkey(server->ntlmssp.hmacmd5, server->ntlmv2_hash,
  382. CIFS_HMAC_MD5_HASH_SIZE);
  383. rc = crypto_shash_init(&server->ntlmssp.sdeschmacmd5->shash);
  384. if (rc) {
  385. cERROR(1, "CalcNTLMv2_response: could not init hmacmd5");
  386. return rc;
  387. }
  388. memcpy(v2_session_response + CIFS_SERVER_CHALLENGE_SIZE,
  389. server->cryptKey, CIFS_SERVER_CHALLENGE_SIZE);
  390. crypto_shash_update(&server->ntlmssp.sdeschmacmd5->shash,
  391. v2_session_response + CIFS_SERVER_CHALLENGE_SIZE,
  392. sizeof(struct ntlmv2_resp) - CIFS_SERVER_CHALLENGE_SIZE);
  393. if (server->tilen)
  394. crypto_shash_update(&server->ntlmssp.sdeschmacmd5->shash,
  395. server->tiblob, server->tilen);
  396. rc = crypto_shash_final(&server->ntlmssp.sdeschmacmd5->shash,
  397. v2_session_response);
  398. return rc;
  399. }
  400. int
  401. setup_ntlmv2_rsp(struct cifsSesInfo *ses, char *resp_buf,
  402. const struct nls_table *nls_cp)
  403. {
  404. int rc = 0;
  405. struct ntlmv2_resp *buf = (struct ntlmv2_resp *)resp_buf;
  406. buf->blob_signature = cpu_to_le32(0x00000101);
  407. buf->reserved = 0;
  408. buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
  409. get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
  410. buf->reserved2 = 0;
  411. if (!ses->domainName) {
  412. rc = find_domain_name(ses);
  413. if (rc) {
  414. cERROR(1, "could not get domain/server name rc %d", rc);
  415. return rc;
  416. }
  417. }
  418. /* calculate buf->ntlmv2_hash */
  419. rc = calc_ntlmv2_hash(ses, nls_cp);
  420. if (rc) {
  421. cERROR(1, "could not get v2 hash rc %d", rc);
  422. return rc;
  423. }
  424. rc = CalcNTLMv2_response(ses->server, resp_buf);
  425. if (rc) {
  426. cERROR(1, "could not get v2 hash rc %d", rc);
  427. return rc;
  428. }
  429. if (!ses->server->ntlmssp.sdeschmacmd5) {
  430. cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
  431. return -1;
  432. }
  433. crypto_shash_setkey(ses->server->ntlmssp.hmacmd5,
  434. ses->server->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
  435. rc = crypto_shash_init(&ses->server->ntlmssp.sdeschmacmd5->shash);
  436. if (rc) {
  437. cERROR(1, "setup_ntlmv2_rsp: could not init hmacmd5\n");
  438. return rc;
  439. }
  440. crypto_shash_update(&ses->server->ntlmssp.sdeschmacmd5->shash,
  441. resp_buf, CIFS_HMAC_MD5_HASH_SIZE);
  442. rc = crypto_shash_final(&ses->server->ntlmssp.sdeschmacmd5->shash,
  443. ses->server->session_key.data.ntlmv2.key);
  444. memcpy(&ses->server->session_key.data.ntlmv2.resp, resp_buf,
  445. sizeof(struct ntlmv2_resp));
  446. ses->server->session_key.len = 16 + sizeof(struct ntlmv2_resp);
  447. return rc;
  448. }
  449. int
  450. calc_seckey(struct TCP_Server_Info *server)
  451. {
  452. int rc;
  453. unsigned char sec_key[CIFS_NTLMV2_SESSKEY_SIZE];
  454. struct crypto_blkcipher *tfm_arc4;
  455. struct scatterlist sgin, sgout;
  456. struct blkcipher_desc desc;
  457. get_random_bytes(sec_key, CIFS_NTLMV2_SESSKEY_SIZE);
  458. tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)",
  459. 0, CRYPTO_ALG_ASYNC);
  460. if (!tfm_arc4 || IS_ERR(tfm_arc4)) {
  461. cERROR(1, "could not allocate " "master crypto API arc4\n");
  462. return 1;
  463. }
  464. desc.tfm = tfm_arc4;
  465. crypto_blkcipher_setkey(tfm_arc4,
  466. server->session_key.data.ntlmv2.key, CIFS_CPHTXT_SIZE);
  467. sg_init_one(&sgin, sec_key, CIFS_CPHTXT_SIZE);
  468. sg_init_one(&sgout, server->ntlmssp.ciphertext, CIFS_CPHTXT_SIZE);
  469. rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
  470. if (!rc)
  471. memcpy(server->session_key.data.ntlmv2.key,
  472. sec_key, CIFS_NTLMV2_SESSKEY_SIZE);
  473. crypto_free_blkcipher(tfm_arc4);
  474. return 0;
  475. }
  476. void
  477. cifs_crypto_shash_release(struct TCP_Server_Info *server)
  478. {
  479. if (server->ntlmssp.md5)
  480. crypto_free_shash(server->ntlmssp.md5);
  481. if (server->ntlmssp.hmacmd5)
  482. crypto_free_shash(server->ntlmssp.hmacmd5);
  483. kfree(server->ntlmssp.sdeschmacmd5);
  484. kfree(server->ntlmssp.sdescmd5);
  485. }
  486. int
  487. cifs_crypto_shash_allocate(struct TCP_Server_Info *server)
  488. {
  489. int rc;
  490. unsigned int size;
  491. server->ntlmssp.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0);
  492. if (!server->ntlmssp.hmacmd5 ||
  493. IS_ERR(server->ntlmssp.hmacmd5)) {
  494. cERROR(1, "could not allocate crypto hmacmd5\n");
  495. return 1;
  496. }
  497. server->ntlmssp.md5 = crypto_alloc_shash("md5", 0, 0);
  498. if (!server->ntlmssp.md5 || IS_ERR(server->ntlmssp.md5)) {
  499. cERROR(1, "could not allocate crypto md5\n");
  500. rc = 1;
  501. goto cifs_crypto_shash_allocate_ret1;
  502. }
  503. size = sizeof(struct shash_desc) +
  504. crypto_shash_descsize(server->ntlmssp.hmacmd5);
  505. server->ntlmssp.sdeschmacmd5 = kmalloc(size, GFP_KERNEL);
  506. if (!server->ntlmssp.sdeschmacmd5) {
  507. cERROR(1, "cifs_crypto_shash_allocate: can't alloc hmacmd5\n");
  508. rc = -ENOMEM;
  509. goto cifs_crypto_shash_allocate_ret2;
  510. }
  511. server->ntlmssp.sdeschmacmd5->shash.tfm = server->ntlmssp.hmacmd5;
  512. server->ntlmssp.sdeschmacmd5->shash.flags = 0x0;
  513. size = sizeof(struct shash_desc) +
  514. crypto_shash_descsize(server->ntlmssp.md5);
  515. server->ntlmssp.sdescmd5 = kmalloc(size, GFP_KERNEL);
  516. if (!server->ntlmssp.sdescmd5) {
  517. cERROR(1, "cifs_crypto_shash_allocate: can't alloc md5\n");
  518. rc = -ENOMEM;
  519. goto cifs_crypto_shash_allocate_ret3;
  520. }
  521. server->ntlmssp.sdescmd5->shash.tfm = server->ntlmssp.md5;
  522. server->ntlmssp.sdescmd5->shash.flags = 0x0;
  523. return 0;
  524. cifs_crypto_shash_allocate_ret3:
  525. kfree(server->ntlmssp.sdeschmacmd5);
  526. cifs_crypto_shash_allocate_ret2:
  527. crypto_free_shash(server->ntlmssp.md5);
  528. cifs_crypto_shash_allocate_ret1:
  529. crypto_free_shash(server->ntlmssp.hmacmd5);
  530. return rc;
  531. }