Lines Matching +full:user +full:- +full:challenge

1 // SPDX-License-Identifier: LGPL-2.1
4 * Encryption and hashing operations relating to NTLM, NTLMv2. See MS-NLMP
28 * Hash data from a BVEC-type iterator.
33 const struct bio_vec *bv = iter->bvec; in cifs_shash_bvec()
34 unsigned long start = iter->iov_offset; in cifs_shash_bvec()
39 for (i = 0; i < iter->nr_segs; i++) { in cifs_shash_bvec()
44 start -= len; in cifs_shash_bvec()
48 len = min_t(size_t, maxsize, len - start); in cifs_shash_bvec()
57 maxsize -= len; in cifs_shash_bvec()
67 * Hash data from a KVEC-type iterator.
72 const struct kvec *kv = iter->kvec; in cifs_shash_kvec()
73 unsigned long start = iter->iov_offset; in cifs_shash_kvec()
77 for (i = 0; i < iter->nr_segs; i++) { in cifs_shash_kvec()
82 start -= len; in cifs_shash_kvec()
86 len = min_t(size_t, maxsize, len - start); in cifs_shash_kvec()
90 maxsize -= len; in cifs_shash_kvec()
101 * Hash data from an XARRAY-type iterator.
108 loff_t start = iter->xarray_start + iter->iov_offset; in cifs_shash_xarray()
117 last = (start + maxsize - 1) / PAGE_SIZE; in cifs_shash_xarray()
119 nr = xa_extract(iter->xarray, (void **)folios, index, last, in cifs_shash_xarray()
122 return -EIO; in cifs_shash_xarray()
127 foffset = start - folio_pos(folio); in cifs_shash_xarray()
130 len = min_t(size_t, maxsize, PAGE_SIZE - offset); in cifs_shash_xarray()
136 maxsize -= len; in cifs_shash_xarray()
167 return -EIO; in cifs_shash_iter()
177 struct kvec *iov = rqst->rq_iov; in __cifs_calc_signature()
178 int n_vec = rqst->rq_nvec; in __cifs_calc_signature()
183 return -EIO; in __cifs_calc_signature()
187 return -EIO; in __cifs_calc_signature()
196 return -EIO; in __cifs_calc_signature()
208 rc = cifs_shash_iter(&rqst->rq_iter, iov_iter_count(&rqst->rq_iter), shash); in __cifs_calc_signature()
224 * should be called with the server->srv_mutex held.
231 if (!rqst->rq_iov || !signature || !server) in cifs_calc_signature()
232 return -EINVAL; in cifs_calc_signature()
234 rc = cifs_alloc_hash("md5", &server->secmech.md5); in cifs_calc_signature()
236 return -1; in cifs_calc_signature()
238 rc = crypto_shash_init(server->secmech.md5); in cifs_calc_signature()
244 rc = crypto_shash_update(server->secmech.md5, in cifs_calc_signature()
245 server->session_key.response, server->session_key.len); in cifs_calc_signature()
251 return __cifs_calc_signature(rqst, server, signature, server->secmech.md5); in cifs_calc_signature()
254 /* must be called with server->srv_mutex held */
260 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base; in cifs_sign_rqst()
262 if (rqst->rq_iov[0].iov_len != 4 || in cifs_sign_rqst()
263 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base) in cifs_sign_rqst()
264 return -EIO; in cifs_sign_rqst()
267 return -EINVAL; in cifs_sign_rqst()
269 spin_lock(&server->srv_lock); in cifs_sign_rqst()
270 if (!(cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) || in cifs_sign_rqst()
271 server->tcpStatus == CifsNeedNegotiate) { in cifs_sign_rqst()
272 spin_unlock(&server->srv_lock); in cifs_sign_rqst()
275 spin_unlock(&server->srv_lock); in cifs_sign_rqst()
277 if (!server->session_estab) { in cifs_sign_rqst()
278 memcpy(cifs_pdu->Signature.SecuritySignature, "BSRSPYL", 8); in cifs_sign_rqst()
282 cifs_pdu->Signature.Sequence.SequenceNumber = in cifs_sign_rqst()
283 cpu_to_le32(server->sequence_number); in cifs_sign_rqst()
284 cifs_pdu->Signature.Sequence.Reserved = 0; in cifs_sign_rqst()
286 *pexpected_response_sequence_number = ++server->sequence_number; in cifs_sign_rqst()
287 ++server->sequence_number; in cifs_sign_rqst()
291 memset(cifs_pdu->Signature.SecuritySignature, 0, 8); in cifs_sign_rqst()
293 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8); in cifs_sign_rqst()
307 /* must be called with server->srv_mutex held */
316 iov[1].iov_len = be32_to_cpu(cifs_pdu->smb_buf_length); in cifs_sign_smb()
329 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base; in cifs_verify_signature()
331 if (rqst->rq_iov[0].iov_len != 4 || in cifs_verify_signature()
332 rqst->rq_iov[0].iov_base + 4 != rqst->rq_iov[1].iov_base) in cifs_verify_signature()
333 return -EIO; in cifs_verify_signature()
336 return -EINVAL; in cifs_verify_signature()
338 if (!server->session_estab) in cifs_verify_signature()
341 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) { in cifs_verify_signature()
344 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE) in cifs_verify_signature()
352 if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0) in cifs_verify_signature()
354 cifs_pdu->Command); in cifs_verify_signature()
358 memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8); in cifs_verify_signature()
360 cifs_pdu->Signature.Sequence.SequenceNumber = in cifs_verify_signature()
362 cifs_pdu->Signature.Sequence.Reserved = 0; in cifs_verify_signature()
375 return -EACCES; in cifs_verify_signature()
395 if (!ses->domainName) { in build_avpair_blob()
396 ses->domainName = kstrdup(defdmname, GFP_KERNEL); in build_avpair_blob()
397 if (!ses->domainName) in build_avpair_blob()
398 return -ENOMEM; in build_avpair_blob()
401 dlen = strlen(ses->domainName); in build_avpair_blob()
409 kfree_sensitive(ses->auth_key.response); in build_avpair_blob()
410 ses->auth_key.len = size + 2 * dlen; in build_avpair_blob()
411 ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL); in build_avpair_blob()
412 if (!ses->auth_key.response) { in build_avpair_blob()
413 ses->auth_key.len = 0; in build_avpair_blob()
414 return -ENOMEM; in build_avpair_blob()
417 blobptr = ses->auth_key.response; in build_avpair_blob()
421 * As defined in MS-NTLM 3.3.2, just this av pair field in build_avpair_blob()
424 attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME); in build_avpair_blob()
425 attrptr->length = cpu_to_le16(2 * dlen); in build_avpair_blob()
427 cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp); in build_avpair_blob()
432 /* Server has provided av pairs/target info in the type 2 challenge
440 * about target string i.e. for some, just user name might suffice.
452 if (!ses->auth_key.len || !ses->auth_key.response) in find_domain_name()
455 blobptr = ses->auth_key.response; in find_domain_name()
456 blobend = blobptr + ses->auth_key.len; in find_domain_name()
460 type = le16_to_cpu(attrptr->type); in find_domain_name()
464 attrsize = le16_to_cpu(attrptr->length); in find_domain_name()
471 if (!ses->domainName) { in find_domain_name()
472 ses->domainName = in find_domain_name()
474 if (!ses->domainName) in find_domain_name()
475 return -ENOMEM; in find_domain_name()
476 cifs_from_utf16(ses->domainName, in find_domain_name()
488 /* Server has provided av pairs/target info in the type 2 challenge
505 if (!ses->auth_key.len || !ses->auth_key.response) in find_timestamp()
508 blobptr = ses->auth_key.response; in find_timestamp()
509 blobend = blobptr + ses->auth_key.len; in find_timestamp()
513 type = le16_to_cpu(attrptr->type); in find_timestamp()
517 attrsize = le16_to_cpu(attrptr->length); in find_timestamp()
538 __le16 *user; in calc_ntlmv2_hash() local
542 if (!ses->server->secmech.hmacmd5) { in calc_ntlmv2_hash()
544 return -1; in calc_ntlmv2_hash()
548 E_md4hash(ses->password, nt_hash, nls_cp); in calc_ntlmv2_hash()
550 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5->tfm, nt_hash, in calc_ntlmv2_hash()
557 rc = crypto_shash_init(ses->server->secmech.hmacmd5); in calc_ntlmv2_hash()
563 /* convert ses->user_name to unicode */ in calc_ntlmv2_hash()
564 len = ses->user_name ? strlen(ses->user_name) : 0; in calc_ntlmv2_hash()
565 user = kmalloc(2 + (len * 2), GFP_KERNEL); in calc_ntlmv2_hash()
566 if (user == NULL) { in calc_ntlmv2_hash()
567 rc = -ENOMEM; in calc_ntlmv2_hash()
572 len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp); in calc_ntlmv2_hash()
573 UniStrupr(user); in calc_ntlmv2_hash()
575 *(u16 *)user = 0; in calc_ntlmv2_hash()
578 rc = crypto_shash_update(ses->server->secmech.hmacmd5, in calc_ntlmv2_hash()
579 (char *)user, 2 * len); in calc_ntlmv2_hash()
580 kfree(user); in calc_ntlmv2_hash()
582 cifs_dbg(VFS, "%s: Could not update with user\n", __func__); in calc_ntlmv2_hash()
586 /* convert ses->domainName to unicode and uppercase */ in calc_ntlmv2_hash()
587 if (ses->domainName) { in calc_ntlmv2_hash()
588 len = strlen(ses->domainName); in calc_ntlmv2_hash()
592 rc = -ENOMEM; in calc_ntlmv2_hash()
595 len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len, in calc_ntlmv2_hash()
598 crypto_shash_update(ses->server->secmech.hmacmd5, in calc_ntlmv2_hash()
607 /* We use ses->ip_addr if no domain name available */ in calc_ntlmv2_hash()
608 len = strlen(ses->ip_addr); in calc_ntlmv2_hash()
612 rc = -ENOMEM; in calc_ntlmv2_hash()
615 len = cifs_strtoUTF16((__le16 *)server, ses->ip_addr, len, in calc_ntlmv2_hash()
618 crypto_shash_update(ses->server->secmech.hmacmd5, in calc_ntlmv2_hash()
628 rc = crypto_shash_final(ses->server->secmech.hmacmd5, in calc_ntlmv2_hash()
641 (ses->auth_key.response + CIFS_SESS_KEY_SIZE); in CalcNTLMv2_response()
645 hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE + in CalcNTLMv2_response()
646 offsetof(struct ntlmv2_resp, challenge.key[0])); in CalcNTLMv2_response()
648 if (!ses->server->secmech.hmacmd5) { in CalcNTLMv2_response()
650 return -1; in CalcNTLMv2_response()
653 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5->tfm, in CalcNTLMv2_response()
661 rc = crypto_shash_init(ses->server->secmech.hmacmd5); in CalcNTLMv2_response()
667 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) in CalcNTLMv2_response()
668 memcpy(ntlmv2->challenge.key, in CalcNTLMv2_response()
669 ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE); in CalcNTLMv2_response()
671 memcpy(ntlmv2->challenge.key, in CalcNTLMv2_response()
672 ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE); in CalcNTLMv2_response()
673 rc = crypto_shash_update(ses->server->secmech.hmacmd5, in CalcNTLMv2_response()
674 ntlmv2->challenge.key, hash_len); in CalcNTLMv2_response()
681 rc = crypto_shash_final(ses->server->secmech.hmacmd5, in CalcNTLMv2_response()
682 ntlmv2->ntlmv2_hash); in CalcNTLMv2_response()
702 return -EINVAL; in setup_ntlmv2_rsp()
705 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) { in setup_ntlmv2_rsp()
706 if (!ses->domainName) { in setup_ntlmv2_rsp()
707 if (ses->domainAuto) { in setup_ntlmv2_rsp()
715 ses->domainName = kstrdup("", GFP_KERNEL); in setup_ntlmv2_rsp()
726 /* Must be within 5 minutes of the server (or in range +/-2h in setup_ntlmv2_rsp()
733 tilen = ses->auth_key.len; in setup_ntlmv2_rsp()
734 tiblob = ses->auth_key.response; in setup_ntlmv2_rsp()
736 ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL); in setup_ntlmv2_rsp()
737 if (!ses->auth_key.response) { in setup_ntlmv2_rsp()
738 rc = -ENOMEM; in setup_ntlmv2_rsp()
739 ses->auth_key.len = 0; in setup_ntlmv2_rsp()
742 ses->auth_key.len += baselen; in setup_ntlmv2_rsp()
745 (ses->auth_key.response + CIFS_SESS_KEY_SIZE); in setup_ntlmv2_rsp()
746 ntlmv2->blob_signature = cpu_to_le32(0x00000101); in setup_ntlmv2_rsp()
747 ntlmv2->reserved = 0; in setup_ntlmv2_rsp()
748 ntlmv2->time = rsp_timestamp; in setup_ntlmv2_rsp()
750 get_random_bytes(&ntlmv2->client_chal, sizeof(ntlmv2->client_chal)); in setup_ntlmv2_rsp()
751 ntlmv2->reserved2 = 0; in setup_ntlmv2_rsp()
753 memcpy(ses->auth_key.response + baselen, tiblob, tilen); in setup_ntlmv2_rsp()
755 cifs_server_lock(ses->server); in setup_ntlmv2_rsp()
757 rc = cifs_alloc_hash("hmac(md5)", &ses->server->secmech.hmacmd5); in setup_ntlmv2_rsp()
777 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5->tfm, in setup_ntlmv2_rsp()
785 rc = crypto_shash_init(ses->server->secmech.hmacmd5); in setup_ntlmv2_rsp()
791 rc = crypto_shash_update(ses->server->secmech.hmacmd5, in setup_ntlmv2_rsp()
792 ntlmv2->ntlmv2_hash, in setup_ntlmv2_rsp()
799 rc = crypto_shash_final(ses->server->secmech.hmacmd5, in setup_ntlmv2_rsp()
800 ses->auth_key.response); in setup_ntlmv2_rsp()
805 cifs_server_unlock(ses->server); in setup_ntlmv2_rsp()
819 return -ENODEV; in calc_seckey()
826 return -ENOMEM; in calc_seckey()
829 cifs_arc4_setkey(ctx_arc4, ses->auth_key.response, CIFS_SESS_KEY_SIZE); in calc_seckey()
830 cifs_arc4_crypt(ctx_arc4, ses->ntlmssp->ciphertext, sec_key, in calc_seckey()
834 memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE); in calc_seckey()
836 ses->auth_key.len = CIFS_SESS_KEY_SIZE; in calc_seckey()
846 cifs_free_hash(&server->secmech.aes_cmac); in cifs_crypto_secmech_release()
847 cifs_free_hash(&server->secmech.hmacsha256); in cifs_crypto_secmech_release()
848 cifs_free_hash(&server->secmech.md5); in cifs_crypto_secmech_release()
849 cifs_free_hash(&server->secmech.sha512); in cifs_crypto_secmech_release()
850 cifs_free_hash(&server->secmech.hmacmd5); in cifs_crypto_secmech_release()
852 if (server->secmech.enc) { in cifs_crypto_secmech_release()
853 crypto_free_aead(server->secmech.enc); in cifs_crypto_secmech_release()
854 server->secmech.enc = NULL; in cifs_crypto_secmech_release()
857 if (server->secmech.dec) { in cifs_crypto_secmech_release()
858 crypto_free_aead(server->secmech.dec); in cifs_crypto_secmech_release()
859 server->secmech.dec = NULL; in cifs_crypto_secmech_release()