23 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/random.h>
41 static int cifs_calc_signature(
struct smb_rqst *rqst,
52 if (!server->
secmech.sdescmd5) {
53 cERROR(1,
"%s: Can't generate signature", __func__);
57 rc = crypto_shash_init(&server->
secmech.sdescmd5->shash);
59 cERROR(1,
"%s: Could not init md5", __func__);
66 cERROR(1,
"%s: Could not update with response", __func__);
70 for (i = 0; i < n_vec; i++) {
74 cERROR(1,
"null iovec entry");
91 cERROR(1,
"%s: Could not update with payload",
103 p_iov.iov_base, p_iov.iov_len);
109 cERROR(1,
"%s: Could not generate md5 hash", __func__);
116 __u32 *pexpected_response_sequence_number)
119 char smb_signature[20];
136 cifs_pdu->Signature.Sequence.
Reserved = 0;
141 rc = cifs_calc_signature(rqst, server, smb_signature);
151 __u32 *pexpected_response_sequence)
156 return cifs_sign_rqst(&rqst, server, pexpected_response_sequence);
161 __u32 *pexpected_response_sequence_number)
169 pexpected_response_sequence_number);
174 __u32 expected_sequence_number)
177 char server_response_sig[8];
178 char what_we_think_sig_should_be[20];
199 cFYI(1,
"dummy signature received for smb command 0x%x",
208 cifs_pdu->Signature.Sequence.
Reserved = 0;
211 rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
220 if (
memcmp(server_response_sig, what_we_think_sig_should_be, 8))
239 cERROR(1,
"NTLM can't allocate (%u bytes) memory", temp_len);
247 cFYI(1,
"%s Can't generate NTLM response, error: %d",
254 cFYI(1,
"%s Can't generate NT hash, error: %d", __func__, rc);
260 cFYI(1,
"%s Can't generate NTLM session key, error: %d",
266 #ifdef CONFIG_CIFS_WEAK_PW_HASH
267 int calc_lanman_hash(
const char *
password,
const char *cryptkey,
bool encrypt,
268 char *lnm_session_key)
280 memcpy(lnm_session_key, password_with_pad,
297 password_with_pad[i] =
toupper(password_with_pad[i]);
299 rc =
SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
315 char *defdmname =
"WORKGROUP";
316 unsigned char *blobptr;
337 cERROR(1,
"Challenge target info allocation failure");
350 blobptr = (
unsigned char *)attrptr +
sizeof(
struct ntlmssp2_name);
369 unsigned int attrsize;
372 unsigned char *blobptr;
373 unsigned char *blobend;
380 blobend = blobptr + ses->
auth_key.len;
382 while (blobptr + onesize < blobend) {
390 if (blobptr + attrsize > blobend)
401 (
__le16 *)blobptr, attrsize, attrsize,
422 if (!ses->
server->secmech.sdeschmacmd5) {
423 cERROR(1,
"calc_ntlmv2_hash: can't generate ntlmv2 hash");
433 cERROR(1,
"%s: Could not set NT Hash as a key", __func__);
437 rc = crypto_shash_init(&ses->
server->secmech.sdeschmacmd5->shash);
439 cERROR(1,
"calc_ntlmv2_hash: could not init hmacmd5");
447 cERROR(1,
"calc_ntlmv2_hash: user mem alloc failure");
460 (
char *)user, 2 * len);
463 cERROR(1,
"%s: Could not update with user", __func__);
472 if (domain ==
NULL) {
473 cERROR(1,
"calc_ntlmv2_hash: domain mem alloc failure");
481 (
char *)domain, 2 * len);
484 cERROR(1,
"%s: Could not update with domain",
492 if (server ==
NULL) {
493 cERROR(1,
"calc_ntlmv2_hash: server mem alloc failure");
501 (
char *)server, 2 * len);
504 cERROR(1,
"%s: Could not update with server",
513 cERROR(1,
"%s: Could not generate md5 hash", __func__);
519 CalcNTLMv2_response(
const struct cifs_ses *ses,
char *ntlmv2_hash)
524 if (!ses->
server->secmech.sdeschmacmd5) {
525 cERROR(1,
"calc_ntlmv2_hash: can't generate ntlmv2 hash");
532 cERROR(1,
"%s: Could not set NTLMV2 Hash as a key", __func__);
536 rc = crypto_shash_init(&ses->
server->secmech.sdeschmacmd5->shash);
538 cERROR(1,
"CalcNTLMv2_response: could not init hmacmd5");
551 cERROR(1,
"%s: Could not update with response", __func__);
558 cERROR(1,
"%s: Could not generate md5 hash", __func__);
571 char ntlmv2_hash[16];
572 unsigned char *tiblob =
NULL;
576 rc = find_domain_name(ses, nls_cp);
578 cERROR(1,
"error %d finding domain name", rc);
579 goto setup_ntlmv2_rsp_ret;
583 rc = build_avpair_blob(ses, nls_cp);
585 cERROR(1,
"error %d building av pair blob", rc);
586 goto setup_ntlmv2_rsp_ret;
598 cERROR(1,
"%s: Can't allocate auth blob", __func__);
599 goto setup_ntlmv2_rsp_ret;
614 rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
616 cERROR(1,
"could not get v2 hash rc %d", rc);
617 goto setup_ntlmv2_rsp_ret;
621 rc = CalcNTLMv2_response(ses, ntlmv2_hash);
623 cERROR(1,
"Could not calculate CR1 rc: %d", rc);
624 goto setup_ntlmv2_rsp_ret;
631 cERROR(1,
"%s: Could not set NTLMV2 Hash as a key", __func__);
632 goto setup_ntlmv2_rsp_ret;
635 rc = crypto_shash_init(&ses->
server->secmech.sdeschmacmd5->shash);
637 cERROR(1,
"%s: Could not init hmacmd5", __func__);
638 goto setup_ntlmv2_rsp_ret;
645 cERROR(1,
"%s: Could not update with response", __func__);
646 goto setup_ntlmv2_rsp_ret;
652 cERROR(1,
"%s: Could not generate md5 hash", __func__);
654 setup_ntlmv2_rsp_ret:
672 if (IS_ERR(tfm_arc4)) {
673 rc = PTR_ERR(tfm_arc4);
674 cERROR(1,
"could not allocate crypto API arc4");
680 rc = crypto_blkcipher_setkey(tfm_arc4, ses->
auth_key.response,
683 cERROR(1,
"%s: Could not set response as a key", __func__);
692 cERROR(1,
"could not encrypt session key rc: %d", rc);
693 crypto_free_blkcipher(tfm_arc4);
702 crypto_free_blkcipher(tfm_arc4);
710 if (server->
secmech.hmacsha256)
711 crypto_free_shash(server->
secmech.hmacsha256);
714 crypto_free_shash(server->
secmech.md5);
717 crypto_free_shash(server->
secmech.hmacmd5);
733 if (IS_ERR(server->
secmech.hmacmd5)) {
734 cERROR(1,
"could not allocate crypto hmacmd5");
735 return PTR_ERR(server->
secmech.hmacmd5);
739 if (IS_ERR(server->
secmech.md5)) {
740 cERROR(1,
"could not allocate crypto md5");
741 rc = PTR_ERR(server->
secmech.md5);
742 goto crypto_allocate_md5_fail;
746 if (IS_ERR(server->
secmech.hmacsha256)) {
747 cERROR(1,
"could not allocate crypto hmacsha256\n");
748 rc = PTR_ERR(server->
secmech.hmacsha256);
749 goto crypto_allocate_hmacsha256_fail;
753 crypto_shash_descsize(server->secmech.hmacmd5);
755 if (!server->secmech.sdeschmacmd5) {
756 cERROR(1,
"cifs_crypto_shash_allocate: can't alloc hmacmd5");
758 goto crypto_allocate_hmacmd5_sdesc_fail;
760 server->secmech.sdeschmacmd5->shash.
tfm = server->secmech.hmacmd5;
761 server->secmech.sdeschmacmd5->shash.
flags = 0x0;
764 crypto_shash_descsize(server->secmech.md5);
766 if (!server->secmech.sdescmd5) {
767 cERROR(1,
"cifs_crypto_shash_allocate: can't alloc md5");
769 goto crypto_allocate_md5_sdesc_fail;
771 server->secmech.sdescmd5->shash.
tfm = server->secmech.md5;
772 server->secmech.sdescmd5->shash.
flags = 0x0;
775 crypto_shash_descsize(server->secmech.hmacsha256);
777 if (!server->secmech.sdeschmacsha256) {
778 cERROR(1,
"%s: Can't alloc hmacsha256\n", __func__);
780 goto crypto_allocate_hmacsha256_sdesc_fail;
782 server->secmech.sdeschmacsha256->shash.
tfm = server->secmech.hmacsha256;
783 server->secmech.sdeschmacsha256->shash.
flags = 0x0;
787 crypto_allocate_hmacsha256_sdesc_fail:
788 kfree(server->secmech.sdescmd5);
790 crypto_allocate_md5_sdesc_fail:
791 kfree(server->secmech.sdeschmacmd5);
793 crypto_allocate_hmacmd5_sdesc_fail:
794 crypto_free_shash(server->secmech.hmacsha256);
796 crypto_allocate_hmacsha256_fail:
797 crypto_free_shash(server->secmech.md5);
799 crypto_allocate_md5_fail:
800 crypto_free_shash(server->secmech.hmacmd5);