xref: /linux/fs/smb/client/cifsencrypt.c (revision c94cd9508b1335b949fd13ebd269313c65492df0)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  *
4  *   Encryption and hashing operations relating to NTLM, NTLMv2.  See MS-NLMP
5  *   for more detailed information
6  *
7  *   Copyright (C) International Business Machines  Corp., 2005,2013
8  *   Author(s): Steve French (sfrench@us.ibm.com)
9  *
10  */
11 
12 #include <linux/fs.h>
13 #include <linux/slab.h>
14 #include "cifspdu.h"
15 #include "cifsglob.h"
16 #include "cifs_debug.h"
17 #include "cifs_unicode.h"
18 #include "cifsproto.h"
19 #include "ntlmssp.h"
20 #include <linux/ctype.h>
21 #include <linux/random.h>
22 #include <linux/highmem.h>
23 #include <linux/fips.h>
24 #include <linux/iov_iter.h>
25 #include "../common/arc4.h"
26 #include <crypto/aead.h>
27 
28 static size_t cifs_shash_step(void *iter_base, size_t progress, size_t len,
29 			      void *priv, void *priv2)
30 {
31 	struct shash_desc *shash = priv;
32 	int ret, *pret = priv2;
33 
34 	ret = crypto_shash_update(shash, iter_base, len);
35 	if (ret < 0) {
36 		*pret = ret;
37 		return len;
38 	}
39 	return 0;
40 }
41 
42 /*
43  * Pass the data from an iterator into a hash.
44  */
45 static int cifs_shash_iter(const struct iov_iter *iter, size_t maxsize,
46 			   struct shash_desc *shash)
47 {
48 	struct iov_iter tmp_iter = *iter;
49 	int err = -EIO;
50 
51 	if (iterate_and_advance_kernel(&tmp_iter, maxsize, shash, &err,
52 				       cifs_shash_step) != maxsize)
53 		return err;
54 	return 0;
55 }
56 
57 int __cifs_calc_signature(struct smb_rqst *rqst,
58 			  struct TCP_Server_Info *server, char *signature,
59 			  struct shash_desc *shash)
60 {
61 	int i;
62 	ssize_t rc;
63 	struct kvec *iov = rqst->rq_iov;
64 	int n_vec = rqst->rq_nvec;
65 
66 	/* iov[0] is actual data and not the rfc1002 length for SMB2+ */
67 	if (!is_smb1(server)) {
68 		if (iov[0].iov_len <= 4)
69 			return -EIO;
70 		i = 0;
71 	} else {
72 		if (n_vec < 2 || iov[0].iov_len != 4)
73 			return -EIO;
74 		i = 1; /* skip rfc1002 length */
75 	}
76 
77 	for (; i < n_vec; i++) {
78 		if (iov[i].iov_len == 0)
79 			continue;
80 		if (iov[i].iov_base == NULL) {
81 			cifs_dbg(VFS, "null iovec entry\n");
82 			return -EIO;
83 		}
84 
85 		rc = crypto_shash_update(shash,
86 					 iov[i].iov_base, iov[i].iov_len);
87 		if (rc) {
88 			cifs_dbg(VFS, "%s: Could not update with payload\n",
89 				 __func__);
90 			return rc;
91 		}
92 	}
93 
94 	rc = cifs_shash_iter(&rqst->rq_iter, iov_iter_count(&rqst->rq_iter), shash);
95 	if (rc < 0)
96 		return rc;
97 
98 	rc = crypto_shash_final(shash, signature);
99 	if (rc)
100 		cifs_dbg(VFS, "%s: Could not generate hash\n", __func__);
101 
102 	return rc;
103 }
104 
105 /*
106  * Calculate and return the CIFS signature based on the mac key and SMB PDU.
107  * The 16 byte signature must be allocated by the caller. Note we only use the
108  * 1st eight bytes and that the smb header signature field on input contains
109  * the sequence number before this function is called. Also, this function
110  * should be called with the server->srv_mutex held.
111  */
112 static int cifs_calc_signature(struct smb_rqst *rqst,
113 			struct TCP_Server_Info *server, char *signature)
114 {
115 	int rc;
116 
117 	if (!rqst->rq_iov || !signature || !server)
118 		return -EINVAL;
119 
120 	rc = cifs_alloc_hash("md5", &server->secmech.md5);
121 	if (rc)
122 		return -1;
123 
124 	rc = crypto_shash_init(server->secmech.md5);
125 	if (rc) {
126 		cifs_dbg(VFS, "%s: Could not init md5\n", __func__);
127 		return rc;
128 	}
129 
130 	rc = crypto_shash_update(server->secmech.md5,
131 		server->session_key.response, server->session_key.len);
132 	if (rc) {
133 		cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
134 		return rc;
135 	}
136 
137 	return __cifs_calc_signature(rqst, server, signature, server->secmech.md5);
138 }
139 
140 /* must be called with server->srv_mutex held */
141 int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,
142 		   __u32 *pexpected_response_sequence_number)
143 {
144 	int rc = 0;
145 	char smb_signature[20];
146 	struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
147 
148 	if (rqst->rq_iov[0].iov_len != 4 ||
149 	    rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
150 		return -EIO;
151 
152 	if ((cifs_pdu == NULL) || (server == NULL))
153 		return -EINVAL;
154 
155 	spin_lock(&server->srv_lock);
156 	if (!(cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) ||
157 	    server->tcpStatus == CifsNeedNegotiate) {
158 		spin_unlock(&server->srv_lock);
159 		return rc;
160 	}
161 	spin_unlock(&server->srv_lock);
162 
163 	if (!server->session_estab) {
164 		memcpy(cifs_pdu->Signature.SecuritySignature, "BSRSPYL", 8);
165 		return rc;
166 	}
167 
168 	cifs_pdu->Signature.Sequence.SequenceNumber =
169 				cpu_to_le32(server->sequence_number);
170 	cifs_pdu->Signature.Sequence.Reserved = 0;
171 
172 	*pexpected_response_sequence_number = ++server->sequence_number;
173 	++server->sequence_number;
174 
175 	rc = cifs_calc_signature(rqst, server, smb_signature);
176 	if (rc)
177 		memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
178 	else
179 		memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
180 
181 	return rc;
182 }
183 
184 int cifs_sign_smbv(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
185 		   __u32 *pexpected_response_sequence)
186 {
187 	struct smb_rqst rqst = { .rq_iov = iov,
188 				 .rq_nvec = n_vec };
189 
190 	return cifs_sign_rqst(&rqst, server, pexpected_response_sequence);
191 }
192 
193 /* must be called with server->srv_mutex held */
194 int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
195 		  __u32 *pexpected_response_sequence_number)
196 {
197 	struct kvec iov[2];
198 
199 	iov[0].iov_base = cifs_pdu;
200 	iov[0].iov_len = 4;
201 	iov[1].iov_base = (char *)cifs_pdu + 4;
202 	iov[1].iov_len = be32_to_cpu(cifs_pdu->smb_buf_length);
203 
204 	return cifs_sign_smbv(iov, 2, server,
205 			      pexpected_response_sequence_number);
206 }
207 
208 int cifs_verify_signature(struct smb_rqst *rqst,
209 			  struct TCP_Server_Info *server,
210 			  __u32 expected_sequence_number)
211 {
212 	unsigned int rc;
213 	char server_response_sig[8];
214 	char what_we_think_sig_should_be[20];
215 	struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
216 
217 	if (rqst->rq_iov[0].iov_len != 4 ||
218 	    rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base)
219 		return -EIO;
220 
221 	if (cifs_pdu == NULL || server == NULL)
222 		return -EINVAL;
223 
224 	if (!server->session_estab)
225 		return 0;
226 
227 	if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
228 		struct smb_com_lock_req *pSMB =
229 			(struct smb_com_lock_req *)cifs_pdu;
230 		if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
231 			return 0;
232 	}
233 
234 	/* BB what if signatures are supposed to be on for session but
235 	   server does not send one? BB */
236 
237 	/* Do not need to verify session setups with signature "BSRSPYL "  */
238 	if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
239 		cifs_dbg(FYI, "dummy signature received for smb command 0x%x\n",
240 			 cifs_pdu->Command);
241 
242 	/* save off the origiginal signature so we can modify the smb and check
243 		its signature against what the server sent */
244 	memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
245 
246 	cifs_pdu->Signature.Sequence.SequenceNumber =
247 					cpu_to_le32(expected_sequence_number);
248 	cifs_pdu->Signature.Sequence.Reserved = 0;
249 
250 	cifs_server_lock(server);
251 	rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
252 	cifs_server_unlock(server);
253 
254 	if (rc)
255 		return rc;
256 
257 /*	cifs_dump_mem("what we think it should be: ",
258 		      what_we_think_sig_should_be, 16); */
259 
260 	if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
261 		return -EACCES;
262 	else
263 		return 0;
264 
265 }
266 
267 /* Build a proper attribute value/target info pairs blob.
268  * Fill in netbios and dns domain name and workstation name
269  * and client time (total five av pairs and + one end of fields indicator.
270  * Allocate domain name which gets freed when session struct is deallocated.
271  */
272 static int
273 build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp)
274 {
275 	unsigned int dlen;
276 	unsigned int size = 2 * sizeof(struct ntlmssp2_name);
277 	char *defdmname = "WORKGROUP";
278 	unsigned char *blobptr;
279 	struct ntlmssp2_name *attrptr;
280 
281 	if (!ses->domainName) {
282 		ses->domainName = kstrdup(defdmname, GFP_KERNEL);
283 		if (!ses->domainName)
284 			return -ENOMEM;
285 	}
286 
287 	dlen = strlen(ses->domainName);
288 
289 	/*
290 	 * The length of this blob is two times the size of a
291 	 * structure (av pair) which holds name/size
292 	 * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) +
293 	 * unicode length of a netbios domain name
294 	 */
295 	kfree_sensitive(ses->auth_key.response);
296 	ses->auth_key.len = size + 2 * dlen;
297 	ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
298 	if (!ses->auth_key.response) {
299 		ses->auth_key.len = 0;
300 		return -ENOMEM;
301 	}
302 
303 	blobptr = ses->auth_key.response;
304 	attrptr = (struct ntlmssp2_name *) blobptr;
305 
306 	/*
307 	 * As defined in MS-NTLM 3.3.2, just this av pair field
308 	 * is sufficient as part of the temp
309 	 */
310 	attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
311 	attrptr->length = cpu_to_le16(2 * dlen);
312 	blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
313 	cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
314 
315 	return 0;
316 }
317 
318 /* Server has provided av pairs/target info in the type 2 challenge
319  * packet and we have plucked it and stored within smb session.
320  * We parse that blob here to find netbios domain name to be used
321  * as part of ntlmv2 authentication (in Target String), if not already
322  * specified on the command line.
323  * If this function returns without any error but without fetching
324  * domain name, authentication may fail against some server but
325  * may not fail against other (those who are not very particular
326  * about target string i.e. for some, just user name might suffice.
327  */
328 static int
329 find_domain_name(struct cifs_ses *ses, const struct nls_table *nls_cp)
330 {
331 	unsigned int attrsize;
332 	unsigned int type;
333 	unsigned int onesize = sizeof(struct ntlmssp2_name);
334 	unsigned char *blobptr;
335 	unsigned char *blobend;
336 	struct ntlmssp2_name *attrptr;
337 
338 	if (!ses->auth_key.len || !ses->auth_key.response)
339 		return 0;
340 
341 	blobptr = ses->auth_key.response;
342 	blobend = blobptr + ses->auth_key.len;
343 
344 	while (blobptr + onesize < blobend) {
345 		attrptr = (struct ntlmssp2_name *) blobptr;
346 		type = le16_to_cpu(attrptr->type);
347 		if (type == NTLMSSP_AV_EOL)
348 			break;
349 		blobptr += 2; /* advance attr type */
350 		attrsize = le16_to_cpu(attrptr->length);
351 		blobptr += 2; /* advance attr size */
352 		if (blobptr + attrsize > blobend)
353 			break;
354 		if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
355 			if (!attrsize || attrsize >= CIFS_MAX_DOMAINNAME_LEN)
356 				break;
357 			if (!ses->domainName) {
358 				ses->domainName =
359 					kmalloc(attrsize + 1, GFP_KERNEL);
360 				if (!ses->domainName)
361 						return -ENOMEM;
362 				cifs_from_utf16(ses->domainName,
363 					(__le16 *)blobptr, attrsize, attrsize,
364 					nls_cp, NO_MAP_UNI_RSVD);
365 				break;
366 			}
367 		}
368 		blobptr += attrsize; /* advance attr  value */
369 	}
370 
371 	return 0;
372 }
373 
374 /* Server has provided av pairs/target info in the type 2 challenge
375  * packet and we have plucked it and stored within smb session.
376  * We parse that blob here to find the server given timestamp
377  * as part of ntlmv2 authentication (or local current time as
378  * default in case of failure)
379  */
380 static __le64
381 find_timestamp(struct cifs_ses *ses)
382 {
383 	unsigned int attrsize;
384 	unsigned int type;
385 	unsigned int onesize = sizeof(struct ntlmssp2_name);
386 	unsigned char *blobptr;
387 	unsigned char *blobend;
388 	struct ntlmssp2_name *attrptr;
389 	struct timespec64 ts;
390 
391 	if (!ses->auth_key.len || !ses->auth_key.response)
392 		return 0;
393 
394 	blobptr = ses->auth_key.response;
395 	blobend = blobptr + ses->auth_key.len;
396 
397 	while (blobptr + onesize < blobend) {
398 		attrptr = (struct ntlmssp2_name *) blobptr;
399 		type = le16_to_cpu(attrptr->type);
400 		if (type == NTLMSSP_AV_EOL)
401 			break;
402 		blobptr += 2; /* advance attr type */
403 		attrsize = le16_to_cpu(attrptr->length);
404 		blobptr += 2; /* advance attr size */
405 		if (blobptr + attrsize > blobend)
406 			break;
407 		if (type == NTLMSSP_AV_TIMESTAMP) {
408 			if (attrsize == sizeof(u64))
409 				return *((__le64 *)blobptr);
410 		}
411 		blobptr += attrsize; /* advance attr value */
412 	}
413 
414 	ktime_get_real_ts64(&ts);
415 	return cpu_to_le64(cifs_UnixTimeToNT(ts));
416 }
417 
418 static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
419 			    const struct nls_table *nls_cp)
420 {
421 	int rc = 0;
422 	int len;
423 	char nt_hash[CIFS_NTHASH_SIZE];
424 	__le16 *user;
425 	wchar_t *domain;
426 	wchar_t *server;
427 
428 	if (!ses->server->secmech.hmacmd5) {
429 		cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
430 		return -1;
431 	}
432 
433 	/* calculate md4 hash of password */
434 	E_md4hash(ses->password, nt_hash, nls_cp);
435 
436 	rc = crypto_shash_setkey(ses->server->secmech.hmacmd5->tfm, nt_hash,
437 				CIFS_NTHASH_SIZE);
438 	if (rc) {
439 		cifs_dbg(VFS, "%s: Could not set NT Hash as a key\n", __func__);
440 		return rc;
441 	}
442 
443 	rc = crypto_shash_init(ses->server->secmech.hmacmd5);
444 	if (rc) {
445 		cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
446 		return rc;
447 	}
448 
449 	/* convert ses->user_name to unicode */
450 	len = ses->user_name ? strlen(ses->user_name) : 0;
451 	user = kmalloc(2 + (len * 2), GFP_KERNEL);
452 	if (user == NULL) {
453 		rc = -ENOMEM;
454 		return rc;
455 	}
456 
457 	if (len) {
458 		len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
459 		UniStrupr(user);
460 	} else {
461 		*(u16 *)user = 0;
462 	}
463 
464 	rc = crypto_shash_update(ses->server->secmech.hmacmd5,
465 				(char *)user, 2 * len);
466 	kfree(user);
467 	if (rc) {
468 		cifs_dbg(VFS, "%s: Could not update with user\n", __func__);
469 		return rc;
470 	}
471 
472 	/* convert ses->domainName to unicode and uppercase */
473 	if (ses->domainName) {
474 		len = strlen(ses->domainName);
475 
476 		domain = kmalloc(2 + (len * 2), GFP_KERNEL);
477 		if (domain == NULL) {
478 			rc = -ENOMEM;
479 			return rc;
480 		}
481 		len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
482 				      nls_cp);
483 		rc =
484 		crypto_shash_update(ses->server->secmech.hmacmd5,
485 					(char *)domain, 2 * len);
486 		kfree(domain);
487 		if (rc) {
488 			cifs_dbg(VFS, "%s: Could not update with domain\n",
489 				 __func__);
490 			return rc;
491 		}
492 	} else {
493 		/* We use ses->ip_addr if no domain name available */
494 		len = strlen(ses->ip_addr);
495 
496 		server = kmalloc(2 + (len * 2), GFP_KERNEL);
497 		if (server == NULL) {
498 			rc = -ENOMEM;
499 			return rc;
500 		}
501 		len = cifs_strtoUTF16((__le16 *)server, ses->ip_addr, len,
502 					nls_cp);
503 		rc =
504 		crypto_shash_update(ses->server->secmech.hmacmd5,
505 					(char *)server, 2 * len);
506 		kfree(server);
507 		if (rc) {
508 			cifs_dbg(VFS, "%s: Could not update with server\n",
509 				 __func__);
510 			return rc;
511 		}
512 	}
513 
514 	rc = crypto_shash_final(ses->server->secmech.hmacmd5,
515 					ntlmv2_hash);
516 	if (rc)
517 		cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
518 
519 	return rc;
520 }
521 
522 static int
523 CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
524 {
525 	int rc;
526 	struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *)
527 	    (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
528 	unsigned int hash_len;
529 
530 	/* The MD5 hash starts at challenge_key.key */
531 	hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE +
532 		offsetof(struct ntlmv2_resp, challenge.key[0]));
533 
534 	if (!ses->server->secmech.hmacmd5) {
535 		cifs_dbg(VFS, "%s: can't generate ntlmv2 hash\n", __func__);
536 		return -1;
537 	}
538 
539 	rc = crypto_shash_setkey(ses->server->secmech.hmacmd5->tfm,
540 				 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
541 	if (rc) {
542 		cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
543 			 __func__);
544 		return rc;
545 	}
546 
547 	rc = crypto_shash_init(ses->server->secmech.hmacmd5);
548 	if (rc) {
549 		cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
550 		return rc;
551 	}
552 
553 	if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED)
554 		memcpy(ntlmv2->challenge.key,
555 		       ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
556 	else
557 		memcpy(ntlmv2->challenge.key,
558 		       ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
559 	rc = crypto_shash_update(ses->server->secmech.hmacmd5,
560 				 ntlmv2->challenge.key, hash_len);
561 	if (rc) {
562 		cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
563 		return rc;
564 	}
565 
566 	/* Note that the MD5 digest over writes anon.challenge_key.key */
567 	rc = crypto_shash_final(ses->server->secmech.hmacmd5,
568 				ntlmv2->ntlmv2_hash);
569 	if (rc)
570 		cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
571 
572 	return rc;
573 }
574 
575 int
576 setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
577 {
578 	int rc;
579 	int baselen;
580 	unsigned int tilen;
581 	struct ntlmv2_resp *ntlmv2;
582 	char ntlmv2_hash[16];
583 	unsigned char *tiblob = NULL; /* target info blob */
584 	__le64 rsp_timestamp;
585 
586 	if (nls_cp == NULL) {
587 		cifs_dbg(VFS, "%s called with nls_cp==NULL\n", __func__);
588 		return -EINVAL;
589 	}
590 
591 	if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) {
592 		if (!ses->domainName) {
593 			if (ses->domainAuto) {
594 				rc = find_domain_name(ses, nls_cp);
595 				if (rc) {
596 					cifs_dbg(VFS, "error %d finding domain name\n",
597 						 rc);
598 					goto setup_ntlmv2_rsp_ret;
599 				}
600 			} else {
601 				ses->domainName = kstrdup("", GFP_KERNEL);
602 			}
603 		}
604 	} else {
605 		rc = build_avpair_blob(ses, nls_cp);
606 		if (rc) {
607 			cifs_dbg(VFS, "error %d building av pair blob\n", rc);
608 			goto setup_ntlmv2_rsp_ret;
609 		}
610 	}
611 
612 	/* Must be within 5 minutes of the server (or in range +/-2h
613 	 * in case of Mac OS X), so simply carry over server timestamp
614 	 * (as Windows 7 does)
615 	 */
616 	rsp_timestamp = find_timestamp(ses);
617 
618 	baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
619 	tilen = ses->auth_key.len;
620 	tiblob = ses->auth_key.response;
621 
622 	ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
623 	if (!ses->auth_key.response) {
624 		rc = -ENOMEM;
625 		ses->auth_key.len = 0;
626 		goto setup_ntlmv2_rsp_ret;
627 	}
628 	ses->auth_key.len += baselen;
629 
630 	ntlmv2 = (struct ntlmv2_resp *)
631 			(ses->auth_key.response + CIFS_SESS_KEY_SIZE);
632 	ntlmv2->blob_signature = cpu_to_le32(0x00000101);
633 	ntlmv2->reserved = 0;
634 	ntlmv2->time = rsp_timestamp;
635 
636 	get_random_bytes(&ntlmv2->client_chal, sizeof(ntlmv2->client_chal));
637 	ntlmv2->reserved2 = 0;
638 
639 	memcpy(ses->auth_key.response + baselen, tiblob, tilen);
640 
641 	cifs_server_lock(ses->server);
642 
643 	rc = cifs_alloc_hash("hmac(md5)", &ses->server->secmech.hmacmd5);
644 	if (rc) {
645 		goto unlock;
646 	}
647 
648 	/* calculate ntlmv2_hash */
649 	rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
650 	if (rc) {
651 		cifs_dbg(VFS, "Could not get v2 hash rc %d\n", rc);
652 		goto unlock;
653 	}
654 
655 	/* calculate first part of the client response (CR1) */
656 	rc = CalcNTLMv2_response(ses, ntlmv2_hash);
657 	if (rc) {
658 		cifs_dbg(VFS, "Could not calculate CR1 rc: %d\n", rc);
659 		goto unlock;
660 	}
661 
662 	/* now calculate the session key for NTLMv2 */
663 	rc = crypto_shash_setkey(ses->server->secmech.hmacmd5->tfm,
664 		ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
665 	if (rc) {
666 		cifs_dbg(VFS, "%s: Could not set NTLMV2 Hash as a key\n",
667 			 __func__);
668 		goto unlock;
669 	}
670 
671 	rc = crypto_shash_init(ses->server->secmech.hmacmd5);
672 	if (rc) {
673 		cifs_dbg(VFS, "%s: Could not init hmacmd5\n", __func__);
674 		goto unlock;
675 	}
676 
677 	rc = crypto_shash_update(ses->server->secmech.hmacmd5,
678 		ntlmv2->ntlmv2_hash,
679 		CIFS_HMAC_MD5_HASH_SIZE);
680 	if (rc) {
681 		cifs_dbg(VFS, "%s: Could not update with response\n", __func__);
682 		goto unlock;
683 	}
684 
685 	rc = crypto_shash_final(ses->server->secmech.hmacmd5,
686 		ses->auth_key.response);
687 	if (rc)
688 		cifs_dbg(VFS, "%s: Could not generate md5 hash\n", __func__);
689 
690 unlock:
691 	cifs_server_unlock(ses->server);
692 setup_ntlmv2_rsp_ret:
693 	kfree_sensitive(tiblob);
694 
695 	return rc;
696 }
697 
698 int
699 calc_seckey(struct cifs_ses *ses)
700 {
701 	unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
702 	struct arc4_ctx *ctx_arc4;
703 
704 	if (fips_enabled)
705 		return -ENODEV;
706 
707 	get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
708 
709 	ctx_arc4 = kmalloc(sizeof(*ctx_arc4), GFP_KERNEL);
710 	if (!ctx_arc4) {
711 		cifs_dbg(VFS, "Could not allocate arc4 context\n");
712 		return -ENOMEM;
713 	}
714 
715 	cifs_arc4_setkey(ctx_arc4, ses->auth_key.response, CIFS_SESS_KEY_SIZE);
716 	cifs_arc4_crypt(ctx_arc4, ses->ntlmssp->ciphertext, sec_key,
717 			CIFS_CPHTXT_SIZE);
718 
719 	/* make secondary_key/nonce as session key */
720 	memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
721 	/* and make len as that of session key only */
722 	ses->auth_key.len = CIFS_SESS_KEY_SIZE;
723 
724 	memzero_explicit(sec_key, CIFS_SESS_KEY_SIZE);
725 	kfree_sensitive(ctx_arc4);
726 	return 0;
727 }
728 
729 void
730 cifs_crypto_secmech_release(struct TCP_Server_Info *server)
731 {
732 	cifs_free_hash(&server->secmech.aes_cmac);
733 	cifs_free_hash(&server->secmech.hmacsha256);
734 	cifs_free_hash(&server->secmech.md5);
735 	cifs_free_hash(&server->secmech.sha512);
736 	cifs_free_hash(&server->secmech.hmacmd5);
737 
738 	if (server->secmech.enc) {
739 		crypto_free_aead(server->secmech.enc);
740 		server->secmech.enc = NULL;
741 	}
742 
743 	if (server->secmech.dec) {
744 		crypto_free_aead(server->secmech.dec);
745 		server->secmech.dec = NULL;
746 	}
747 }
748