CIFS: SMBD: Support page offset in memory registration
[linux-2.6-block.git] / fs / cifs / cifsencrypt.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/cifsencrypt.c
3 *
8b7a4544
SF
4 * Encryption and hashing operations relating to NTLM, NTLMv2. See MS-NLMP
5 * for more detailed information
6 *
95dc8dd1 7 * Copyright (C) International Business Machines Corp., 2005,2013
1da177e4
LT
8 * Author(s): Steve French (sfrench@us.ibm.com)
9 *
10 * This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/fs.h>
5a0e3ad6 26#include <linux/slab.h>
1da177e4 27#include "cifspdu.h"
ffdd6e4d 28#include "cifsglob.h"
1da177e4 29#include "cifs_debug.h"
1da177e4
LT
30#include "cifs_unicode.h"
31#include "cifsproto.h"
2b149f11 32#include "ntlmssp.h"
7c7b25bc 33#include <linux/ctype.h>
6d027cfd 34#include <linux/random.h>
fb308a6f 35#include <linux/highmem.h>
9651ddba 36#include <crypto/skcipher.h>
026e93dc 37#include <crypto/aead.h>
1da177e4 38
16c568ef 39int __cifs_calc_signature(struct smb_rqst *rqst,
57f933ce 40 int start,
16c568ef
AV
41 struct TCP_Server_Info *server, char *signature,
42 struct shash_desc *shash)
84afc29b 43{
e9917a00 44 int i;
307fbd31 45 int rc;
bf5ea0e2
JL
46 struct kvec *iov = rqst->rq_iov;
47 int n_vec = rqst->rq_nvec;
84afc29b 48
57f933ce 49 for (i = start; i < n_vec; i++) {
745542e2
JL
50 if (iov[i].iov_len == 0)
51 continue;
ffdd6e4d 52 if (iov[i].iov_base == NULL) {
f96637be 53 cifs_dbg(VFS, "null iovec entry\n");
e9917a00 54 return -EIO;
745542e2 55 }
738f9de5
PS
56 if (i == 1 && iov[1].iov_len <= 4)
57 break; /* nothing to sign or corrupt header */
58 rc = crypto_shash_update(shash,
59 iov[i].iov_base, iov[i].iov_len);
14cae324 60 if (rc) {
f96637be
JP
61 cifs_dbg(VFS, "%s: Could not update with payload\n",
62 __func__);
14cae324
SP
63 return rc;
64 }
e9917a00 65 }
84afc29b 66
fb308a6f
JL
67 /* now hash over the rq_pages array */
68 for (i = 0; i < rqst->rq_npages; i++) {
16c568ef
AV
69 void *kaddr = kmap(rqst->rq_pages[i]);
70 size_t len = rqst->rq_pagesz;
71
72 if (i == rqst->rq_npages - 1)
73 len = rqst->rq_tailsz;
74
75 crypto_shash_update(shash, kaddr, len);
fb308a6f 76
fb308a6f
JL
77 kunmap(rqst->rq_pages[i]);
78 }
79
16c568ef 80 rc = crypto_shash_final(shash, signature);
14cae324 81 if (rc)
16c568ef 82 cifs_dbg(VFS, "%s: Could not generate hash\n", __func__);
84afc29b 83
307fbd31 84 return rc;
84afc29b
SF
85}
86
16c568ef
AV
87/*
88 * Calculate and return the CIFS signature based on the mac key and SMB PDU.
89 * The 16 byte signature must be allocated by the caller. Note we only use the
90 * 1st eight bytes and that the smb header signature field on input contains
91 * the sequence number before this function is called. Also, this function
92 * should be called with the server->srv_mutex held.
93 */
94static int cifs_calc_signature(struct smb_rqst *rqst,
95 struct TCP_Server_Info *server, char *signature)
96{
97 int rc;
98
99 if (!rqst->rq_iov || !signature || !server)
100 return -EINVAL;
101
82fb82be
AA
102 rc = cifs_alloc_hash("md5", &server->secmech.md5,
103 &server->secmech.sdescmd5);
104 if (rc)
105 return -1;
16c568ef
AV
106
107 rc = crypto_shash_init(&server->secmech.sdescmd5->shash);
108 if (rc) {
109 cifs_dbg(VFS, "%s: Could not init md5\n", __func__);
110 return rc;
111 }
112
113 rc = crypto_shash_update(&server->secmech.sdescmd5->shash,
114 server->session_key.response, server->session_key.len);
115 if (rc) {
116 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
117 return rc;
118 }
119
57f933ce 120 return __cifs_calc_signature(rqst, 1, server, signature,
16c568ef
AV
121 &server->secmech.sdescmd5->shash);
122}
123
a0f8b4fb 124/* must be called with server->srv_mutex held */
bf5ea0e2 125int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,
63d2583f 126 __u32 *pexpected_response_sequence_number)
84afc29b
SF
127{
128 int rc = 0;
129 char smb_signature[20];
bf5ea0e2 130 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
84afc29b 131
738f9de5
PS
132 if (rqst->rq_iov[0].iov_len != 4 ||
133 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
134 return -EIO;
135
ffdd6e4d 136 if ((cifs_pdu == NULL) || (server == NULL))
84afc29b
SF
137 return -EINVAL;
138
998d6fcb
JL
139 if (!(cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) ||
140 server->tcpStatus == CifsNeedNegotiate)
84afc29b
SF
141 return rc;
142
998d6fcb 143 if (!server->session_estab) {
b4dacbc2 144 memcpy(cifs_pdu->Signature.SecuritySignature, "BSRSPYL", 8);
998d6fcb
JL
145 return rc;
146 }
147
ffdd6e4d 148 cifs_pdu->Signature.Sequence.SequenceNumber =
84afc29b 149 cpu_to_le32(server->sequence_number);
ffdd6e4d 150 cifs_pdu->Signature.Sequence.Reserved = 0;
84afc29b 151
0124cc45
JL
152 *pexpected_response_sequence_number = ++server->sequence_number;
153 ++server->sequence_number;
84afc29b 154
bf5ea0e2 155 rc = cifs_calc_signature(rqst, server, smb_signature);
ffdd6e4d
SF
156 if (rc)
157 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
158 else
159 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
84afc29b 160
ffdd6e4d 161 return rc;
84afc29b
SF
162}
163
bf5ea0e2
JL
164int cifs_sign_smbv(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
165 __u32 *pexpected_response_sequence)
166{
167 struct smb_rqst rqst = { .rq_iov = iov,
168 .rq_nvec = n_vec };
169
170 return cifs_sign_rqst(&rqst, server, pexpected_response_sequence);
171}
172
826a95e4
JL
173/* must be called with server->srv_mutex held */
174int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
175 __u32 *pexpected_response_sequence_number)
176{
738f9de5 177 struct kvec iov[2];
826a95e4 178
738f9de5
PS
179 iov[0].iov_base = cifs_pdu;
180 iov[0].iov_len = 4;
181 iov[1].iov_base = (char *)cifs_pdu + 4;
182 iov[1].iov_len = be32_to_cpu(cifs_pdu->smb_buf_length);
826a95e4 183
738f9de5 184 return cifs_sign_smbv(iov, 2, server,
826a95e4
JL
185 pexpected_response_sequence_number);
186}
187
bf5ea0e2 188int cifs_verify_signature(struct smb_rqst *rqst,
21e73393 189 struct TCP_Server_Info *server,
ffdd6e4d 190 __u32 expected_sequence_number)
1da177e4 191{
c8e56f1f 192 unsigned int rc;
1da177e4
LT
193 char server_response_sig[8];
194 char what_we_think_sig_should_be[20];
bf5ea0e2 195 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
1da177e4 196
738f9de5
PS
197 if (rqst->rq_iov[0].iov_len != 4 ||
198 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
199 return -EIO;
200
21e73393 201 if (cifs_pdu == NULL || server == NULL)
1da177e4
LT
202 return -EINVAL;
203
9c4843ea 204 if (!server->session_estab)
1da177e4
LT
205 return 0;
206
207 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
50c2f753 208 struct smb_com_lock_req *pSMB =
ffdd6e4d
SF
209 (struct smb_com_lock_req *)cifs_pdu;
210 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
1da177e4
LT
211 return 0;
212 }
213
50c2f753
SF
214 /* BB what if signatures are supposed to be on for session but
215 server does not send one? BB */
216
1da177e4 217 /* Do not need to verify session setups with signature "BSRSPYL " */
50c2f753 218 if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
f96637be
JP
219 cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
220 cifs_pdu->Command);
1da177e4
LT
221
222 /* save off the origiginal signature so we can modify the smb and check
223 its signature against what the server sent */
50c2f753 224 memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
1da177e4 225
50c2f753
SF
226 cifs_pdu->Signature.Sequence.SequenceNumber =
227 cpu_to_le32(expected_sequence_number);
1da177e4
LT
228 cifs_pdu->Signature.Sequence.Reserved = 0;
229
157c2491 230 mutex_lock(&server->srv_mutex);
bf5ea0e2 231 rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
157c2491 232 mutex_unlock(&server->srv_mutex);
1da177e4 233
50c2f753 234 if (rc)
1da177e4
LT
235 return rc;
236
50c2f753
SF
237/* cifs_dump_mem("what we think it should be: ",
238 what_we_think_sig_should_be, 16); */
1da177e4 239
50c2f753 240 if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
1da177e4
LT
241 return -EACCES;
242 else
243 return 0;
244
245}
246
21e73393 247/* first calculate 24 bytes ntlm response and then 16 byte session key */
9ef5992e 248int setup_ntlm_response(struct cifs_ses *ses, const struct nls_table *nls_cp)
1da177e4 249{
ee2c9258 250 int rc = 0;
21e73393
SP
251 unsigned int temp_len = CIFS_SESS_KEY_SIZE + CIFS_AUTH_RESP_SIZE;
252 char temp_key[CIFS_SESS_KEY_SIZE];
253
254 if (!ses)
1da177e4
LT
255 return -EINVAL;
256
21e73393 257 ses->auth_key.response = kmalloc(temp_len, GFP_KERNEL);
f96637be 258 if (!ses->auth_key.response)
21e73393 259 return -ENOMEM;
f96637be 260
21e73393
SP
261 ses->auth_key.len = temp_len;
262
ee2c9258 263 rc = SMBNTencrypt(ses->password, ses->server->cryptkey,
9ef5992e 264 ses->auth_key.response + CIFS_SESS_KEY_SIZE, nls_cp);
ee2c9258 265 if (rc) {
f96637be
JP
266 cifs_dbg(FYI, "%s Can't generate NTLM response, error: %d\n",
267 __func__, rc);
ee2c9258
SP
268 return rc;
269 }
270
9ef5992e 271 rc = E_md4hash(ses->password, temp_key, nls_cp);
ee2c9258 272 if (rc) {
f96637be
JP
273 cifs_dbg(FYI, "%s Can't generate NT hash, error: %d\n",
274 __func__, rc);
ee2c9258
SP
275 return rc;
276 }
21e73393 277
ee2c9258
SP
278 rc = mdfour(ses->auth_key.response, temp_key, CIFS_SESS_KEY_SIZE);
279 if (rc)
f96637be
JP
280 cifs_dbg(FYI, "%s Can't generate NTLM session key, error: %d\n",
281 __func__, rc);
21e73393 282
ee2c9258 283 return rc;
1da177e4
LT
284}
285
7c7b25bc 286#ifdef CONFIG_CIFS_WEAK_PW_HASH
43988d76 287int calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
4e53a3fb 288 char *lnm_session_key)
7c7b25bc
SF
289{
290 int i;
43988d76 291 int rc;
97f4b727 292 char password_with_pad[CIFS_ENCPWD_SIZE] = {0};
7c7b25bc 293
4e53a3fb
JL
294 if (password)
295 strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
296
04912d6a 297 if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
4e53a3fb
JL
298 memcpy(lnm_session_key, password_with_pad,
299 CIFS_ENCPWD_SIZE);
43988d76 300 return 0;
4e53a3fb 301 }
bdc4bf6e 302
7c7b25bc
SF
303 /* calculate old style session key */
304 /* calling toupper is less broken than repeatedly
305 calling nls_toupper would be since that will never
306 work for UTF8, but neither handles multibyte code pages
307 but the only alternative would be converting to UCS-16 (Unicode)
308 (using a routine something like UniStrupr) then
309 uppercasing and then converting back from Unicode - which
310 would only worth doing it if we knew it were utf8. Basically
311 utf8 and other multibyte codepages each need their own strupper
312 function since a byte at a time will ont work. */
313
ef571cad 314 for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
7c7b25bc 315 password_with_pad[i] = toupper(password_with_pad[i]);
7c7b25bc 316
43988d76 317 rc = SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
4e53a3fb 318
43988d76 319 return rc;
7c7b25bc
SF
320}
321#endif /* CIFS_WEAK_PW_HASH */
322
9daa42e2
SP
323/* Build a proper attribute value/target info pairs blob.
324 * Fill in netbios and dns domain name and workstation name
325 * and client time (total five av pairs and + one end of fields indicator.
326 * Allocate domain name which gets freed when session struct is deallocated.
2b149f11
SP
327 */
328static int
96daf2b0 329build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp)
2b149f11 330{
9daa42e2 331 unsigned int dlen;
cfbd6f84 332 unsigned int size = 2 * sizeof(struct ntlmssp2_name);
9daa42e2
SP
333 char *defdmname = "WORKGROUP";
334 unsigned char *blobptr;
2b149f11
SP
335 struct ntlmssp2_name *attrptr;
336
9daa42e2
SP
337 if (!ses->domainName) {
338 ses->domainName = kstrdup(defdmname, GFP_KERNEL);
339 if (!ses->domainName)
340 return -ENOMEM;
341 }
342
343 dlen = strlen(ses->domainName);
9daa42e2 344
cfbd6f84
SP
345 /*
346 * The length of this blob is two times the size of a
347 * structure (av pair) which holds name/size
348 * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) +
349 * unicode length of a netbios domain name
9daa42e2 350 */
cfbd6f84 351 ses->auth_key.len = size + 2 * dlen;
d3686d54
SP
352 ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
353 if (!ses->auth_key.response) {
354 ses->auth_key.len = 0;
2b149f11
SP
355 return -ENOMEM;
356 }
9daa42e2 357
d3686d54 358 blobptr = ses->auth_key.response;
9daa42e2
SP
359 attrptr = (struct ntlmssp2_name *) blobptr;
360
cfbd6f84
SP
361 /*
362 * As defined in MS-NTLM 3.3.2, just this av pair field
363 * is sufficient as part of the temp
364 */
9daa42e2
SP
365 attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
366 attrptr->length = cpu_to_le16(2 * dlen);
367 blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
acbbb76a 368 cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
9daa42e2 369
2b149f11
SP
370 return 0;
371}
372
373/* Server has provided av pairs/target info in the type 2 challenge
374 * packet and we have plucked it and stored within smb session.
375 * We parse that blob here to find netbios domain name to be used
376 * as part of ntlmv2 authentication (in Target String), if not already
377 * specified on the command line.
378 * If this function returns without any error but without fetching
379 * domain name, authentication may fail against some server but
380 * may not fail against other (those who are not very particular
381 * about target string i.e. for some, just user name might suffice.
382 */
383static int
96daf2b0 384find_domain_name(struct cifs_ses *ses, const struct nls_table *nls_cp)
2b149f11
SP
385{
386 unsigned int attrsize;
387 unsigned int type;
388 unsigned int onesize = sizeof(struct ntlmssp2_name);
389 unsigned char *blobptr;
390 unsigned char *blobend;
391 struct ntlmssp2_name *attrptr;
392
d3686d54 393 if (!ses->auth_key.len || !ses->auth_key.response)
2b149f11
SP
394 return 0;
395
d3686d54
SP
396 blobptr = ses->auth_key.response;
397 blobend = blobptr + ses->auth_key.len;
2b149f11
SP
398
399 while (blobptr + onesize < blobend) {
400 attrptr = (struct ntlmssp2_name *) blobptr;
401 type = le16_to_cpu(attrptr->type);
402 if (type == NTLMSSP_AV_EOL)
403 break;
404 blobptr += 2; /* advance attr type */
405 attrsize = le16_to_cpu(attrptr->length);
406 blobptr += 2; /* advance attr size */
407 if (blobptr + attrsize > blobend)
408 break;
409 if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
057d6332 410 if (!attrsize || attrsize >= CIFS_MAX_DOMAINNAME_LEN)
2b149f11
SP
411 break;
412 if (!ses->domainName) {
413 ses->domainName =
414 kmalloc(attrsize + 1, GFP_KERNEL);
415 if (!ses->domainName)
416 return -ENOMEM;
acbbb76a 417 cifs_from_utf16(ses->domainName,
2b149f11 418 (__le16 *)blobptr, attrsize, attrsize,
b693855f 419 nls_cp, NO_MAP_UNI_RSVD);
2b149f11
SP
420 break;
421 }
422 }
423 blobptr += attrsize; /* advance attr value */
424 }
425
426 return 0;
427}
428
98ce94c8
PS
429/* Server has provided av pairs/target info in the type 2 challenge
430 * packet and we have plucked it and stored within smb session.
431 * We parse that blob here to find the server given timestamp
432 * as part of ntlmv2 authentication (or local current time as
433 * default in case of failure)
434 */
435static __le64
436find_timestamp(struct cifs_ses *ses)
437{
438 unsigned int attrsize;
439 unsigned int type;
440 unsigned int onesize = sizeof(struct ntlmssp2_name);
441 unsigned char *blobptr;
442 unsigned char *blobend;
443 struct ntlmssp2_name *attrptr;
e37fea58 444 struct timespec ts;
98ce94c8
PS
445
446 if (!ses->auth_key.len || !ses->auth_key.response)
447 return 0;
448
449 blobptr = ses->auth_key.response;
450 blobend = blobptr + ses->auth_key.len;
451
452 while (blobptr + onesize < blobend) {
453 attrptr = (struct ntlmssp2_name *) blobptr;
454 type = le16_to_cpu(attrptr->type);
455 if (type == NTLMSSP_AV_EOL)
456 break;
457 blobptr += 2; /* advance attr type */
458 attrsize = le16_to_cpu(attrptr->length);
459 blobptr += 2; /* advance attr size */
460 if (blobptr + attrsize > blobend)
461 break;
462 if (type == NTLMSSP_AV_TIMESTAMP) {
463 if (attrsize == sizeof(u64))
464 return *((__le64 *)blobptr);
465 }
466 blobptr += attrsize; /* advance attr value */
467 }
468
e37fea58
DD
469 ktime_get_real_ts(&ts);
470 return cpu_to_le64(cifs_UnixTimeToNT(ts));
98ce94c8
PS
471}
472
96daf2b0 473static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
50c2f753 474 const struct nls_table *nls_cp)
a8ee0344
SF
475{
476 int rc = 0;
477 int len;
307fbd31 478 char nt_hash[CIFS_NTHASH_SIZE];
fdf96a90 479 __le16 *user;
50c2f753 480 wchar_t *domain;
307fbd31 481 wchar_t *server;
a8ee0344 482
307fbd31 483 if (!ses->server->secmech.sdeschmacmd5) {
f96637be 484 cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
307fbd31
SP
485 return -1;
486 }
56234e27 487
c8e56f1f 488 /* calculate md4 hash of password */
9ef5992e 489 E_md4hash(ses->password, nt_hash, nls_cp);
9fbc5908 490
14cae324 491 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash,
307fbd31 492 CIFS_NTHASH_SIZE);
14cae324 493 if (rc) {
f96637be 494 cifs_dbg(VFS, "%s: Could not set NT Hash as a key\n", __func__);
14cae324
SP
495 return rc;
496 }
307fbd31
SP
497
498 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
499 if (rc) {
f96637be 500 cifs_dbg(VFS, "%s: could not init hmacmd5\n", __func__);
307fbd31
SP
501 return rc;
502 }
a8ee0344 503
fdf96a90 504 /* convert ses->user_name to unicode */
04febabc 505 len = ses->user_name ? strlen(ses->user_name) : 0;
1717ffc5 506 user = kmalloc(2 + (len * 2), GFP_KERNEL);
307fbd31 507 if (user == NULL) {
307fbd31 508 rc = -ENOMEM;
14cae324 509 return rc;
307fbd31 510 }
04febabc
JL
511
512 if (len) {
fdf96a90 513 len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
04febabc
JL
514 UniStrupr(user);
515 } else {
516 memset(user, '\0', 2);
517 }
307fbd31 518
14cae324 519 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
307fbd31 520 (char *)user, 2 * len);
14cae324
SP
521 kfree(user);
522 if (rc) {
f96637be 523 cifs_dbg(VFS, "%s: Could not update with user\n", __func__);
14cae324
SP
524 return rc;
525 }
a8ee0344
SF
526
527 /* convert ses->domainName to unicode and uppercase */
50c2f753 528 if (ses->domainName) {
1717ffc5 529 len = strlen(ses->domainName);
a8ee0344 530
50c2f753 531 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
307fbd31 532 if (domain == NULL) {
307fbd31 533 rc = -ENOMEM;
14cae324 534 return rc;
307fbd31 535 }
acbbb76a
SF
536 len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
537 nls_cp);
14cae324 538 rc =
307fbd31
SP
539 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
540 (char *)domain, 2 * len);
1717ffc5 541 kfree(domain);
14cae324 542 if (rc) {
f96637be
JP
543 cifs_dbg(VFS, "%s: Could not update with domain\n",
544 __func__);
14cae324
SP
545 return rc;
546 }
8b7a4544
SF
547 } else {
548 /* We use ses->serverName if no domain name available */
307fbd31
SP
549 len = strlen(ses->serverName);
550
551 server = kmalloc(2 + (len * 2), GFP_KERNEL);
552 if (server == NULL) {
307fbd31 553 rc = -ENOMEM;
14cae324 554 return rc;
307fbd31 555 }
acbbb76a 556 len = cifs_strtoUTF16((__le16 *)server, ses->serverName, len,
307fbd31 557 nls_cp);
14cae324 558 rc =
307fbd31
SP
559 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
560 (char *)server, 2 * len);
561 kfree(server);
14cae324 562 if (rc) {
f96637be
JP
563 cifs_dbg(VFS, "%s: Could not update with server\n",
564 __func__);
14cae324
SP
565 return rc;
566 }
1717ffc5 567 }
307fbd31
SP
568
569 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
d3686d54 570 ntlmv2_hash);
14cae324 571 if (rc)
f96637be 572 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
307fbd31 573
307fbd31
SP
574 return rc;
575}
576
577static int
96daf2b0 578CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
307fbd31
SP
579{
580 int rc;
2c957ddf
TG
581 struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *)
582 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
583 unsigned int hash_len;
584
585 /* The MD5 hash starts at challenge_key.key */
586 hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE +
587 offsetof(struct ntlmv2_resp, challenge.key[0]));
307fbd31
SP
588
589 if (!ses->server->secmech.sdeschmacmd5) {
f96637be 590 cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
307fbd31
SP
591 return -1;
592 }
593
14cae324 594 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
2c957ddf 595 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
14cae324 596 if (rc) {
f96637be
JP
597 cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
598 __func__);
14cae324
SP
599 return rc;
600 }
307fbd31
SP
601
602 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
603 if (rc) {
f96637be 604 cifs_dbg(VFS, "%s: could not init hmacmd5\n", __func__);
307fbd31
SP
605 return rc;
606 }
607
3f618223 608 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED)
2c957ddf
TG
609 memcpy(ntlmv2->challenge.key,
610 ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
d3ba50b1 611 else
2c957ddf
TG
612 memcpy(ntlmv2->challenge.key,
613 ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
14cae324 614 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
2c957ddf 615 ntlmv2->challenge.key, hash_len);
14cae324 616 if (rc) {
f96637be 617 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
14cae324
SP
618 return rc;
619 }
307fbd31 620
2c957ddf 621 /* Note that the MD5 digest over writes anon.challenge_key.key */
307fbd31 622 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
2c957ddf 623 ntlmv2->ntlmv2_hash);
14cae324 624 if (rc)
f96637be 625 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
9fbc5908
SF
626
627 return rc;
628}
629
2b149f11 630int
96daf2b0 631setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
9fbc5908 632{
c8e56f1f 633 int rc;
21e73393 634 int baselen;
d3686d54 635 unsigned int tilen;
2c957ddf 636 struct ntlmv2_resp *ntlmv2;
d3686d54
SP
637 char ntlmv2_hash[16];
638 unsigned char *tiblob = NULL; /* target info blob */
98ce94c8 639 __le64 rsp_timestamp;
6d027cfd 640
3f618223 641 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) {
2b149f11 642 if (!ses->domainName) {
39566443
GP
643 if (ses->domainAuto) {
644 rc = find_domain_name(ses, nls_cp);
645 if (rc) {
646 cifs_dbg(VFS, "error %d finding domain name\n",
647 rc);
648 goto setup_ntlmv2_rsp_ret;
649 }
650 } else {
651 ses->domainName = kstrdup("", GFP_KERNEL);
2b149f11
SP
652 }
653 }
654 } else {
9daa42e2 655 rc = build_avpair_blob(ses, nls_cp);
2b149f11 656 if (rc) {
f96637be 657 cifs_dbg(VFS, "error %d building av pair blob\n", rc);
d3686d54 658 goto setup_ntlmv2_rsp_ret;
2b149f11
SP
659 }
660 }
a8ee0344 661
98ce94c8
PS
662 /* Must be within 5 minutes of the server (or in range +/-2h
663 * in case of Mac OS X), so simply carry over server timestamp
664 * (as Windows 7 does)
665 */
666 rsp_timestamp = find_timestamp(ses);
667
21e73393 668 baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
d3686d54
SP
669 tilen = ses->auth_key.len;
670 tiblob = ses->auth_key.response;
671
672 ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
21e73393 673 if (!ses->auth_key.response) {
4b550af5 674 rc = -ENOMEM;
d3686d54 675 ses->auth_key.len = 0;
21e73393
SP
676 goto setup_ntlmv2_rsp_ret;
677 }
d3686d54 678 ses->auth_key.len += baselen;
21e73393 679
2c957ddf 680 ntlmv2 = (struct ntlmv2_resp *)
21e73393 681 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
2c957ddf
TG
682 ntlmv2->blob_signature = cpu_to_le32(0x00000101);
683 ntlmv2->reserved = 0;
98ce94c8
PS
684 ntlmv2->time = rsp_timestamp;
685
2c957ddf
TG
686 get_random_bytes(&ntlmv2->client_chal, sizeof(ntlmv2->client_chal));
687 ntlmv2->reserved2 = 0;
21e73393 688
d3686d54 689 memcpy(ses->auth_key.response + baselen, tiblob, tilen);
21e73393 690
bd975d1e
RV
691 mutex_lock(&ses->server->srv_mutex);
692
82fb82be
AA
693 rc = cifs_alloc_hash("hmac(md5)",
694 &ses->server->secmech.hmacmd5,
695 &ses->server->secmech.sdeschmacmd5);
95dc8dd1 696 if (rc) {
bd975d1e 697 goto unlock;
95dc8dd1
SF
698 }
699
f7c5445a 700 /* calculate ntlmv2_hash */
d3686d54 701 rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
2b149f11 702 if (rc) {
f96637be 703 cifs_dbg(VFS, "could not get v2 hash rc %d\n", rc);
bd975d1e 704 goto unlock;
2b149f11 705 }
f7c5445a
SP
706
707 /* calculate first part of the client response (CR1) */
d3686d54 708 rc = CalcNTLMv2_response(ses, ntlmv2_hash);
307fbd31 709 if (rc) {
f96637be 710 cifs_dbg(VFS, "Could not calculate CR1 rc: %d\n", rc);
bd975d1e 711 goto unlock;
307fbd31 712 }
b609f06a 713
5d0d2882 714 /* now calculate the session key for NTLMv2 */
14cae324 715 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
d3686d54 716 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
14cae324 717 if (rc) {
f96637be
JP
718 cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
719 __func__);
bd975d1e 720 goto unlock;
14cae324 721 }
307fbd31
SP
722
723 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
724 if (rc) {
f96637be 725 cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
bd975d1e 726 goto unlock;
307fbd31
SP
727 }
728
14cae324 729 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
2c957ddf 730 ntlmv2->ntlmv2_hash,
307fbd31 731 CIFS_HMAC_MD5_HASH_SIZE);
14cae324 732 if (rc) {
f96637be 733 cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
bd975d1e 734 goto unlock;
14cae324 735 }
307fbd31
SP
736
737 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
738 ses->auth_key.response);
14cae324 739 if (rc)
f96637be 740 cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
2b149f11 741
bd975d1e
RV
742unlock:
743 mutex_unlock(&ses->server->srv_mutex);
2b149f11 744setup_ntlmv2_rsp_ret:
d3686d54 745 kfree(tiblob);
2b149f11
SP
746
747 return rc;
6d027cfd
SF
748}
749
d2b91521 750int
96daf2b0 751calc_seckey(struct cifs_ses *ses)
d2b91521
SP
752{
753 int rc;
9651ddba 754 struct crypto_skcipher *tfm_arc4;
d2b91521 755 struct scatterlist sgin, sgout;
9651ddba 756 struct skcipher_request *req;
5f4b5569
SP
757 unsigned char *sec_key;
758
759 sec_key = kmalloc(CIFS_SESS_KEY_SIZE, GFP_KERNEL);
760 if (sec_key == NULL)
761 return -ENOMEM;
d2b91521
SP
762
763 get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
764
9651ddba 765 tfm_arc4 = crypto_alloc_skcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
7a8587e7
SP
766 if (IS_ERR(tfm_arc4)) {
767 rc = PTR_ERR(tfm_arc4);
f96637be 768 cifs_dbg(VFS, "could not allocate crypto API arc4\n");
5f4b5569 769 goto out;
d2b91521
SP
770 }
771
9651ddba 772 rc = crypto_skcipher_setkey(tfm_arc4, ses->auth_key.response,
d2b91521 773 CIFS_SESS_KEY_SIZE);
14cae324 774 if (rc) {
f96637be
JP
775 cifs_dbg(VFS, "%s: Could not set response as a key\n",
776 __func__);
9651ddba
HX
777 goto out_free_cipher;
778 }
779
780 req = skcipher_request_alloc(tfm_arc4, GFP_KERNEL);
781 if (!req) {
782 rc = -ENOMEM;
783 cifs_dbg(VFS, "could not allocate crypto API arc4 request\n");
784 goto out_free_cipher;
14cae324 785 }
d2b91521
SP
786
787 sg_init_one(&sgin, sec_key, CIFS_SESS_KEY_SIZE);
d3686d54 788 sg_init_one(&sgout, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
d2b91521 789
9651ddba
HX
790 skcipher_request_set_callback(req, 0, NULL, NULL);
791 skcipher_request_set_crypt(req, &sgin, &sgout, CIFS_CPHTXT_SIZE, NULL);
792
793 rc = crypto_skcipher_encrypt(req);
794 skcipher_request_free(req);
d2b91521 795 if (rc) {
f96637be 796 cifs_dbg(VFS, "could not encrypt session key rc: %d\n", rc);
9651ddba 797 goto out_free_cipher;
d2b91521
SP
798 }
799
800 /* make secondary_key/nonce as session key */
801 memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
802 /* and make len as that of session key only */
803 ses->auth_key.len = CIFS_SESS_KEY_SIZE;
804
9651ddba
HX
805out_free_cipher:
806 crypto_free_skcipher(tfm_arc4);
5f4b5569
SP
807out:
808 kfree(sec_key);
14cae324 809 return rc;
d2b91521
SP
810}
811
812void
026e93dc 813cifs_crypto_secmech_release(struct TCP_Server_Info *server)
d2b91521 814{
95dc8dd1 815 if (server->secmech.cmacaes) {
429b46f4 816 crypto_free_shash(server->secmech.cmacaes);
95dc8dd1
SF
817 server->secmech.cmacaes = NULL;
818 }
429b46f4 819
95dc8dd1 820 if (server->secmech.hmacsha256) {
3c1bf7e4 821 crypto_free_shash(server->secmech.hmacsha256);
95dc8dd1
SF
822 server->secmech.hmacsha256 = NULL;
823 }
3c1bf7e4 824
95dc8dd1 825 if (server->secmech.md5) {
d2b91521 826 crypto_free_shash(server->secmech.md5);
95dc8dd1
SF
827 server->secmech.md5 = NULL;
828 }
d2b91521 829
70e80655 830 if (server->secmech.sha512) {
5fcd7f3f
AA
831 crypto_free_shash(server->secmech.sha512);
832 server->secmech.sha512 = NULL;
833 }
834
95dc8dd1 835 if (server->secmech.hmacmd5) {
d2b91521 836 crypto_free_shash(server->secmech.hmacmd5);
95dc8dd1
SF
837 server->secmech.hmacmd5 = NULL;
838 }
d2b91521 839
026e93dc
PS
840 if (server->secmech.ccmaesencrypt) {
841 crypto_free_aead(server->secmech.ccmaesencrypt);
842 server->secmech.ccmaesencrypt = NULL;
843 }
844
845 if (server->secmech.ccmaesdecrypt) {
846 crypto_free_aead(server->secmech.ccmaesdecrypt);
847 server->secmech.ccmaesdecrypt = NULL;
848 }
849
429b46f4 850 kfree(server->secmech.sdesccmacaes);
95dc8dd1 851 server->secmech.sdesccmacaes = NULL;
3c1bf7e4 852 kfree(server->secmech.sdeschmacsha256);
95dc8dd1 853 server->secmech.sdeschmacsha256 = NULL;
d2b91521 854 kfree(server->secmech.sdeschmacmd5);
95dc8dd1 855 server->secmech.sdeschmacmd5 = NULL;
d2b91521 856 kfree(server->secmech.sdescmd5);
95dc8dd1 857 server->secmech.sdescmd5 = NULL;
5fcd7f3f
AA
858 kfree(server->secmech.sdescsha512);
859 server->secmech.sdescsha512 = NULL;
d2b91521 860}