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 original 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 #define AV_TYPE(av) (le16_to_cpu(av->type)) 319 #define AV_LEN(av) (le16_to_cpu(av->length)) 320 #define AV_DATA_PTR(av) ((void *)av->data) 321 322 #define av_for_each_entry(ses, av) \ 323 for (av = NULL; (av = find_next_av(ses, av));) 324 325 static struct ntlmssp2_name *find_next_av(struct cifs_ses *ses, 326 struct ntlmssp2_name *av) 327 { 328 u16 len; 329 u8 *end; 330 331 end = (u8 *)ses->auth_key.response + ses->auth_key.len; 332 if (!av) { 333 if (unlikely(!ses->auth_key.response || !ses->auth_key.len)) 334 return NULL; 335 av = (void *)ses->auth_key.response; 336 } else { 337 av = (void *)((u8 *)av + sizeof(*av) + AV_LEN(av)); 338 } 339 340 if ((u8 *)av + sizeof(*av) > end) 341 return NULL; 342 343 len = AV_LEN(av); 344 if (AV_TYPE(av) == NTLMSSP_AV_EOL) 345 return NULL; 346 if (!len || (u8 *)av + sizeof(*av) + len > end) 347 return NULL; 348 return av; 349 } 350 351 /* 352 * Check if server has provided av pair of @type in the NTLMSSP 353 * CHALLENGE_MESSAGE blob. 354 */ 355 static int find_av_name(struct cifs_ses *ses, u16 type, char **name, u16 maxlen) 356 { 357 const struct nls_table *nlsc = ses->local_nls; 358 struct ntlmssp2_name *av; 359 u16 len, nlen; 360 361 if (*name) 362 return 0; 363 364 av_for_each_entry(ses, av) { 365 len = AV_LEN(av); 366 if (AV_TYPE(av) != type) 367 continue; 368 if (!IS_ALIGNED(len, sizeof(__le16))) { 369 cifs_dbg(VFS | ONCE, "%s: bad length(%u) for type %u\n", 370 __func__, len, type); 371 continue; 372 } 373 nlen = len / sizeof(__le16); 374 if (nlen <= maxlen) { 375 ++nlen; 376 *name = kmalloc(nlen, GFP_KERNEL); 377 if (!*name) 378 return -ENOMEM; 379 cifs_from_utf16(*name, AV_DATA_PTR(av), nlen, 380 len, nlsc, NO_MAP_UNI_RSVD); 381 break; 382 } 383 } 384 return 0; 385 } 386 387 /* Server has provided av pairs/target info in the type 2 challenge 388 * packet and we have plucked it and stored within smb session. 389 * We parse that blob here to find the server given timestamp 390 * as part of ntlmv2 authentication (or local current time as 391 * default in case of failure) 392 */ 393 static __le64 find_timestamp(struct cifs_ses *ses) 394 { 395 struct ntlmssp2_name *av; 396 struct timespec64 ts; 397 398 av_for_each_entry(ses, av) { 399 if (AV_TYPE(av) == NTLMSSP_AV_TIMESTAMP && 400 AV_LEN(av) == sizeof(u64)) 401 return *((__le64 *)AV_DATA_PTR(av)); 402 } 403 ktime_get_real_ts64(&ts); 404 return cpu_to_le64(cifs_UnixTimeToNT(ts)); 405 } 406 407 static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash, 408 const struct nls_table *nls_cp, struct shash_desc *hmacmd5) 409 { 410 int rc = 0; 411 int len; 412 char nt_hash[CIFS_NTHASH_SIZE]; 413 __le16 *user; 414 wchar_t *domain; 415 wchar_t *server; 416 417 /* calculate md4 hash of password */ 418 E_md4hash(ses->password, nt_hash, nls_cp); 419 420 rc = crypto_shash_setkey(hmacmd5->tfm, nt_hash, CIFS_NTHASH_SIZE); 421 if (rc) { 422 cifs_dbg(VFS, "%s: Could not set NT hash as a key, rc=%d\n", __func__, rc); 423 return rc; 424 } 425 426 rc = crypto_shash_init(hmacmd5); 427 if (rc) { 428 cifs_dbg(VFS, "%s: Could not init HMAC-MD5, rc=%d\n", __func__, rc); 429 return rc; 430 } 431 432 /* convert ses->user_name to unicode */ 433 len = ses->user_name ? strlen(ses->user_name) : 0; 434 user = kmalloc(2 + (len * 2), GFP_KERNEL); 435 if (user == NULL) 436 return -ENOMEM; 437 438 if (len) { 439 len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp); 440 UniStrupr(user); 441 } else { 442 *(u16 *)user = 0; 443 } 444 445 rc = crypto_shash_update(hmacmd5, (char *)user, 2 * len); 446 kfree(user); 447 if (rc) { 448 cifs_dbg(VFS, "%s: Could not update with user, rc=%d\n", __func__, rc); 449 return rc; 450 } 451 452 /* convert ses->domainName to unicode and uppercase */ 453 if (ses->domainName) { 454 len = strlen(ses->domainName); 455 456 domain = kmalloc(2 + (len * 2), GFP_KERNEL); 457 if (domain == NULL) 458 return -ENOMEM; 459 460 len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len, 461 nls_cp); 462 rc = crypto_shash_update(hmacmd5, (char *)domain, 2 * len); 463 kfree(domain); 464 if (rc) { 465 cifs_dbg(VFS, "%s: Could not update with domain, rc=%d\n", __func__, rc); 466 return rc; 467 } 468 } else { 469 /* We use ses->ip_addr if no domain name available */ 470 len = strlen(ses->ip_addr); 471 472 server = kmalloc(2 + (len * 2), GFP_KERNEL); 473 if (server == NULL) 474 return -ENOMEM; 475 476 len = cifs_strtoUTF16((__le16 *)server, ses->ip_addr, len, nls_cp); 477 rc = crypto_shash_update(hmacmd5, (char *)server, 2 * len); 478 kfree(server); 479 if (rc) { 480 cifs_dbg(VFS, "%s: Could not update with server, rc=%d\n", __func__, rc); 481 return rc; 482 } 483 } 484 485 rc = crypto_shash_final(hmacmd5, ntlmv2_hash); 486 if (rc) 487 cifs_dbg(VFS, "%s: Could not generate MD5 hash, rc=%d\n", __func__, rc); 488 489 return rc; 490 } 491 492 static int 493 CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash, struct shash_desc *hmacmd5) 494 { 495 int rc; 496 struct ntlmv2_resp *ntlmv2 = (struct ntlmv2_resp *) 497 (ses->auth_key.response + CIFS_SESS_KEY_SIZE); 498 unsigned int hash_len; 499 500 /* The MD5 hash starts at challenge_key.key */ 501 hash_len = ses->auth_key.len - (CIFS_SESS_KEY_SIZE + 502 offsetof(struct ntlmv2_resp, challenge.key[0])); 503 504 rc = crypto_shash_setkey(hmacmd5->tfm, ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE); 505 if (rc) { 506 cifs_dbg(VFS, "%s: Could not set NTLMv2 hash as a key, rc=%d\n", __func__, rc); 507 return rc; 508 } 509 510 rc = crypto_shash_init(hmacmd5); 511 if (rc) { 512 cifs_dbg(VFS, "%s: Could not init HMAC-MD5, rc=%d\n", __func__, rc); 513 return rc; 514 } 515 516 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) 517 memcpy(ntlmv2->challenge.key, ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE); 518 else 519 memcpy(ntlmv2->challenge.key, ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE); 520 521 rc = crypto_shash_update(hmacmd5, ntlmv2->challenge.key, hash_len); 522 if (rc) { 523 cifs_dbg(VFS, "%s: Could not update with response, rc=%d\n", __func__, rc); 524 return rc; 525 } 526 527 /* Note that the MD5 digest over writes anon.challenge_key.key */ 528 rc = crypto_shash_final(hmacmd5, ntlmv2->ntlmv2_hash); 529 if (rc) 530 cifs_dbg(VFS, "%s: Could not generate MD5 hash, rc=%d\n", __func__, rc); 531 532 return rc; 533 } 534 535 int 536 setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp) 537 { 538 struct shash_desc *hmacmd5 = NULL; 539 int rc; 540 int baselen; 541 unsigned int tilen; 542 struct ntlmv2_resp *ntlmv2; 543 char ntlmv2_hash[16]; 544 unsigned char *tiblob = NULL; /* target info blob */ 545 __le64 rsp_timestamp; 546 547 if (nls_cp == NULL) { 548 cifs_dbg(VFS, "%s called with nls_cp==NULL\n", __func__); 549 return -EINVAL; 550 } 551 552 if (ses->server->negflavor == CIFS_NEGFLAVOR_EXTENDED) { 553 if (!ses->domainName) { 554 if (ses->domainAuto) { 555 /* 556 * Domain (workgroup) hasn't been specified in 557 * mount options, so try to find it in 558 * CHALLENGE_MESSAGE message and then use it as 559 * part of NTLMv2 authentication. 560 */ 561 rc = find_av_name(ses, NTLMSSP_AV_NB_DOMAIN_NAME, 562 &ses->domainName, 563 CIFS_MAX_DOMAINNAME_LEN); 564 if (rc) 565 goto setup_ntlmv2_rsp_ret; 566 } else { 567 ses->domainName = kstrdup("", GFP_KERNEL); 568 if (!ses->domainName) { 569 rc = -ENOMEM; 570 goto setup_ntlmv2_rsp_ret; 571 } 572 } 573 } 574 rc = find_av_name(ses, NTLMSSP_AV_DNS_DOMAIN_NAME, 575 &ses->dns_dom, CIFS_MAX_DOMAINNAME_LEN); 576 if (rc) 577 goto setup_ntlmv2_rsp_ret; 578 } else { 579 rc = build_avpair_blob(ses, nls_cp); 580 if (rc) { 581 cifs_dbg(VFS, "error %d building av pair blob\n", rc); 582 goto setup_ntlmv2_rsp_ret; 583 } 584 } 585 586 /* Must be within 5 minutes of the server (or in range +/-2h 587 * in case of Mac OS X), so simply carry over server timestamp 588 * (as Windows 7 does) 589 */ 590 rsp_timestamp = find_timestamp(ses); 591 592 baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp); 593 tilen = ses->auth_key.len; 594 tiblob = ses->auth_key.response; 595 596 ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL); 597 if (!ses->auth_key.response) { 598 rc = -ENOMEM; 599 ses->auth_key.len = 0; 600 goto setup_ntlmv2_rsp_ret; 601 } 602 ses->auth_key.len += baselen; 603 604 ntlmv2 = (struct ntlmv2_resp *) 605 (ses->auth_key.response + CIFS_SESS_KEY_SIZE); 606 ntlmv2->blob_signature = cpu_to_le32(0x00000101); 607 ntlmv2->reserved = 0; 608 ntlmv2->time = rsp_timestamp; 609 610 get_random_bytes(&ntlmv2->client_chal, sizeof(ntlmv2->client_chal)); 611 ntlmv2->reserved2 = 0; 612 613 memcpy(ses->auth_key.response + baselen, tiblob, tilen); 614 615 cifs_server_lock(ses->server); 616 617 rc = cifs_alloc_hash("hmac(md5)", &hmacmd5); 618 if (rc) { 619 cifs_dbg(VFS, "Could not allocate HMAC-MD5, rc=%d\n", rc); 620 goto unlock; 621 } 622 623 /* calculate ntlmv2_hash */ 624 rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp, hmacmd5); 625 if (rc) { 626 cifs_dbg(VFS, "Could not get NTLMv2 hash, rc=%d\n", rc); 627 goto unlock; 628 } 629 630 /* calculate first part of the client response (CR1) */ 631 rc = CalcNTLMv2_response(ses, ntlmv2_hash, hmacmd5); 632 if (rc) { 633 cifs_dbg(VFS, "Could not calculate CR1, rc=%d\n", rc); 634 goto unlock; 635 } 636 637 /* now calculate the session key for NTLMv2 */ 638 rc = crypto_shash_setkey(hmacmd5->tfm, ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE); 639 if (rc) { 640 cifs_dbg(VFS, "%s: Could not set NTLMv2 hash as a key, rc=%d\n", __func__, rc); 641 goto unlock; 642 } 643 644 rc = crypto_shash_init(hmacmd5); 645 if (rc) { 646 cifs_dbg(VFS, "%s: Could not init HMAC-MD5, rc=%d\n", __func__, rc); 647 goto unlock; 648 } 649 650 rc = crypto_shash_update(hmacmd5, ntlmv2->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE); 651 if (rc) { 652 cifs_dbg(VFS, "%s: Could not update with response, rc=%d\n", __func__, rc); 653 goto unlock; 654 } 655 656 rc = crypto_shash_final(hmacmd5, ses->auth_key.response); 657 if (rc) 658 cifs_dbg(VFS, "%s: Could not generate MD5 hash, rc=%d\n", __func__, rc); 659 unlock: 660 cifs_server_unlock(ses->server); 661 cifs_free_hash(&hmacmd5); 662 setup_ntlmv2_rsp_ret: 663 kfree_sensitive(tiblob); 664 665 return rc; 666 } 667 668 int 669 calc_seckey(struct cifs_ses *ses) 670 { 671 unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */ 672 struct arc4_ctx *ctx_arc4; 673 674 if (fips_enabled) 675 return -ENODEV; 676 677 get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE); 678 679 ctx_arc4 = kmalloc(sizeof(*ctx_arc4), GFP_KERNEL); 680 if (!ctx_arc4) { 681 cifs_dbg(VFS, "Could not allocate arc4 context\n"); 682 return -ENOMEM; 683 } 684 685 cifs_arc4_setkey(ctx_arc4, ses->auth_key.response, CIFS_SESS_KEY_SIZE); 686 cifs_arc4_crypt(ctx_arc4, ses->ntlmssp->ciphertext, sec_key, 687 CIFS_CPHTXT_SIZE); 688 689 /* make secondary_key/nonce as session key */ 690 memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE); 691 /* and make len as that of session key only */ 692 ses->auth_key.len = CIFS_SESS_KEY_SIZE; 693 694 memzero_explicit(sec_key, CIFS_SESS_KEY_SIZE); 695 kfree_sensitive(ctx_arc4); 696 return 0; 697 } 698 699 void 700 cifs_crypto_secmech_release(struct TCP_Server_Info *server) 701 { 702 cifs_free_hash(&server->secmech.aes_cmac); 703 cifs_free_hash(&server->secmech.hmacsha256); 704 cifs_free_hash(&server->secmech.md5); 705 cifs_free_hash(&server->secmech.sha512); 706 707 if (server->secmech.enc) { 708 crypto_free_aead(server->secmech.enc); 709 server->secmech.enc = NULL; 710 } 711 if (server->secmech.dec) { 712 crypto_free_aead(server->secmech.dec); 713 server->secmech.dec = NULL; 714 } 715 } 716