blob: af520522ef20e1acb47999a4200b50d6b3dfffbf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/cifsencrypt.c
3 *
Steve French12b3b8f2006-02-09 21:12:47 +00004 * Copyright (C) International Business Machines Corp., 2005,2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "cifspdu.h"
Steve Frenchffdd6e42007-06-24 21:15:44 +000025#include "cifsglob.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "cifs_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "cifs_unicode.h"
28#include "cifsproto.h"
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -050029#include "ntlmssp.h"
Steve French7c7b25b2006-06-01 19:20:10 +000030#include <linux/ctype.h>
Steve French6d027cf2006-06-05 16:26:05 +000031#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Jeff Layton157c2492011-04-02 07:34:30 -040033/*
34 * Calculate and return the CIFS signature based on the mac key and SMB PDU.
35 * The 16 byte signature must be allocated by the caller. Note we only use the
36 * 1st eight bytes and that the smb header signature field on input contains
37 * the sequence number before this function is called. Also, this function
38 * should be called with the server->srv_mutex held.
39 */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -070040static int cifs_calc_signature(struct smb_rqst *rqst,
Jeff Layton826a95e2011-10-11 06:41:32 -040041 struct TCP_Server_Info *server, char *signature)
Steve French84afc292005-12-02 13:32:45 -080042{
Steve Frenche9917a02006-03-31 21:22:00 +000043 int i;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -050044 int rc;
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -070045 struct kvec *iov = rqst->rq_iov;
46 int n_vec = rqst->rq_nvec;
Steve French84afc292005-12-02 13:32:45 -080047
Shirish Pargaonkar21e73392010-10-21 06:42:55 -050048 if (iov == NULL || signature == NULL || server == NULL)
Steve Frenche9917a02006-03-31 21:22:00 +000049 return -EINVAL;
Steve French84afc292005-12-02 13:32:45 -080050
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -050051 if (!server->secmech.sdescmd5) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -040052 cERROR(1, "%s: Can't generate signature", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -050053 return -1;
54 }
55
56 rc = crypto_shash_init(&server->secmech.sdescmd5->shash);
57 if (rc) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -040058 cERROR(1, "%s: Could not init md5", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -050059 return rc;
60 }
61
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050062 rc = crypto_shash_update(&server->secmech.sdescmd5->shash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -050063 server->session_key.response, server->session_key.len);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050064 if (rc) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -040065 cERROR(1, "%s: Could not update with response", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050066 return rc;
67 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -050068
Steve French50c2f752007-07-13 00:33:32 +000069 for (i = 0; i < n_vec; i++) {
Jeff Layton745542e2007-11-03 04:34:04 +000070 if (iov[i].iov_len == 0)
71 continue;
Steve Frenchffdd6e42007-06-24 21:15:44 +000072 if (iov[i].iov_base == NULL) {
Steve French56234e22010-09-08 20:57:05 +000073 cERROR(1, "null iovec entry");
Steve Frenche9917a02006-03-31 21:22:00 +000074 return -EIO;
Jeff Layton745542e2007-11-03 04:34:04 +000075 }
Steve Frenchffdd6e42007-06-24 21:15:44 +000076 /* The first entry includes a length field (which does not get
Steve Frenche9917a02006-03-31 21:22:00 +000077 signed that occupies the first 4 bytes before the header */
Steve Frenchffdd6e42007-06-24 21:15:44 +000078 if (i == 0) {
Steve French63d25832007-11-05 21:46:10 +000079 if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
Steve Frenche9917a02006-03-31 21:22:00 +000080 break; /* nothing to sign or corrupt header */
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050081 rc =
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -050082 crypto_shash_update(&server->secmech.sdescmd5->shash,
83 iov[i].iov_base + 4, iov[i].iov_len - 4);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050084 } else {
85 rc =
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -050086 crypto_shash_update(&server->secmech.sdescmd5->shash,
87 iov[i].iov_base, iov[i].iov_len);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050088 }
89 if (rc) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -040090 cERROR(1, "%s: Could not update with payload",
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050091 __func__);
92 return rc;
93 }
Steve Frenche9917a02006-03-31 21:22:00 +000094 }
Steve French84afc292005-12-02 13:32:45 -080095
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -050096 rc = crypto_shash_final(&server->secmech.sdescmd5->shash, signature);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050097 if (rc)
Jeff Laytonac3aa2f2012-07-23 13:14:28 -040098 cERROR(1, "%s: Could not generate md5 hash", __func__);
Steve French84afc292005-12-02 13:32:45 -080099
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500100 return rc;
Steve French84afc292005-12-02 13:32:45 -0800101}
102
Jeff Laytona0f8b4f2011-01-07 11:30:28 -0500103/* must be called with server->srv_mutex held */
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700104int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,
Steve French63d25832007-11-05 21:46:10 +0000105 __u32 *pexpected_response_sequence_number)
Steve French84afc292005-12-02 13:32:45 -0800106{
107 int rc = 0;
108 char smb_signature[20];
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700109 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Steve French84afc292005-12-02 13:32:45 -0800110
Steve Frenchffdd6e42007-06-24 21:15:44 +0000111 if ((cifs_pdu == NULL) || (server == NULL))
Steve French84afc292005-12-02 13:32:45 -0800112 return -EINVAL;
113
Jeff Layton998d6fc2011-07-26 12:21:17 -0400114 if (!(cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) ||
115 server->tcpStatus == CifsNeedNegotiate)
Steve French84afc292005-12-02 13:32:45 -0800116 return rc;
117
Jeff Layton998d6fc2011-07-26 12:21:17 -0400118 if (!server->session_estab) {
Jeff Laytonb4dacbc2011-10-11 06:41:32 -0400119 memcpy(cifs_pdu->Signature.SecuritySignature, "BSRSPYL", 8);
Jeff Layton998d6fc2011-07-26 12:21:17 -0400120 return rc;
121 }
122
Steve Frenchffdd6e42007-06-24 21:15:44 +0000123 cifs_pdu->Signature.Sequence.SequenceNumber =
Steve French84afc292005-12-02 13:32:45 -0800124 cpu_to_le32(server->sequence_number);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000125 cifs_pdu->Signature.Sequence.Reserved = 0;
Steve French84afc292005-12-02 13:32:45 -0800126
Steve Frenchffdd6e42007-06-24 21:15:44 +0000127 *pexpected_response_sequence_number = server->sequence_number++;
128 server->sequence_number++;
Steve French84afc292005-12-02 13:32:45 -0800129
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700130 rc = cifs_calc_signature(rqst, server, smb_signature);
Steve Frenchffdd6e42007-06-24 21:15:44 +0000131 if (rc)
132 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
133 else
134 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
Steve French84afc292005-12-02 13:32:45 -0800135
Steve Frenchffdd6e42007-06-24 21:15:44 +0000136 return rc;
Steve French84afc292005-12-02 13:32:45 -0800137}
138
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700139int cifs_sign_smbv(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
140 __u32 *pexpected_response_sequence)
141{
142 struct smb_rqst rqst = { .rq_iov = iov,
143 .rq_nvec = n_vec };
144
145 return cifs_sign_rqst(&rqst, server, pexpected_response_sequence);
146}
147
Jeff Layton826a95e2011-10-11 06:41:32 -0400148/* must be called with server->srv_mutex held */
149int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
150 __u32 *pexpected_response_sequence_number)
151{
152 struct kvec iov;
153
154 iov.iov_base = cifs_pdu;
155 iov.iov_len = be32_to_cpu(cifs_pdu->smb_buf_length) + 4;
156
Jeff Layton762a4202012-07-23 13:28:38 -0400157 return cifs_sign_smbv(&iov, 1, server,
Jeff Layton826a95e2011-10-11 06:41:32 -0400158 pexpected_response_sequence_number);
159}
160
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700161int cifs_verify_signature(struct smb_rqst *rqst,
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500162 struct TCP_Server_Info *server,
Steve Frenchffdd6e42007-06-24 21:15:44 +0000163 __u32 expected_sequence_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Steve Frenchc8e56f12010-09-08 21:10:58 +0000165 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 char server_response_sig[8];
167 char what_we_think_sig_should_be[20];
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700168 struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500170 if (cifs_pdu == NULL || server == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return -EINVAL;
172
Jeff Layton9c4843e2011-06-06 15:40:23 -0400173 if (!server->session_estab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 0;
175
176 if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
Steve French50c2f752007-07-13 00:33:32 +0000177 struct smb_com_lock_req *pSMB =
Steve Frenchffdd6e42007-06-24 21:15:44 +0000178 (struct smb_com_lock_req *)cifs_pdu;
179 if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
181 }
182
Steve French50c2f752007-07-13 00:33:32 +0000183 /* BB what if signatures are supposed to be on for session but
184 server does not send one? BB */
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* Do not need to verify session setups with signature "BSRSPYL " */
Steve French50c2f752007-07-13 00:33:32 +0000187 if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
Joe Perchesb6b38f72010-04-21 03:50:45 +0000188 cFYI(1, "dummy signature received for smb command 0x%x",
189 cifs_pdu->Command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /* save off the origiginal signature so we can modify the smb and check
192 its signature against what the server sent */
Steve French50c2f752007-07-13 00:33:32 +0000193 memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Steve French50c2f752007-07-13 00:33:32 +0000195 cifs_pdu->Signature.Sequence.SequenceNumber =
196 cpu_to_le32(expected_sequence_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 cifs_pdu->Signature.Sequence.Reserved = 0;
198
Jeff Layton157c2492011-04-02 07:34:30 -0400199 mutex_lock(&server->srv_mutex);
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -0700200 rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
Jeff Layton157c2492011-04-02 07:34:30 -0400201 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Steve French50c2f752007-07-13 00:33:32 +0000203 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return rc;
205
Steve French50c2f752007-07-13 00:33:32 +0000206/* cifs_dump_mem("what we think it should be: ",
207 what_we_think_sig_should_be, 16); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Steve French50c2f752007-07-13 00:33:32 +0000209 if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return -EACCES;
211 else
212 return 0;
213
214}
215
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500216/* first calculate 24 bytes ntlm response and then 16 byte session key */
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500217int setup_ntlm_response(struct cifs_ses *ses, const struct nls_table *nls_cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600219 int rc = 0;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500220 unsigned int temp_len = CIFS_SESS_KEY_SIZE + CIFS_AUTH_RESP_SIZE;
221 char temp_key[CIFS_SESS_KEY_SIZE];
222
223 if (!ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EINVAL;
225
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500226 ses->auth_key.response = kmalloc(temp_len, GFP_KERNEL);
227 if (!ses->auth_key.response) {
228 cERROR(1, "NTLM can't allocate (%u bytes) memory", temp_len);
229 return -ENOMEM;
230 }
231 ses->auth_key.len = temp_len;
232
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600233 rc = SMBNTencrypt(ses->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500234 ses->auth_key.response + CIFS_SESS_KEY_SIZE, nls_cp);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600235 if (rc) {
236 cFYI(1, "%s Can't generate NTLM response, error: %d",
237 __func__, rc);
238 return rc;
239 }
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500240
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500241 rc = E_md4hash(ses->password, temp_key, nls_cp);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600242 if (rc) {
243 cFYI(1, "%s Can't generate NT hash, error: %d", __func__, rc);
244 return rc;
245 }
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500246
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600247 rc = mdfour(ses->auth_key.response, temp_key, CIFS_SESS_KEY_SIZE);
248 if (rc)
249 cFYI(1, "%s Can't generate NTLM session key, error: %d",
250 __func__, rc);
251
252 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Steve French7c7b25b2006-06-01 19:20:10 +0000255#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French43988d72011-04-19 18:23:31 +0000256int calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500257 char *lnm_session_key)
Steve French7c7b25b2006-06-01 19:20:10 +0000258{
259 int i;
Steve French43988d72011-04-19 18:23:31 +0000260 int rc;
Steve French7c7b25b2006-06-01 19:20:10 +0000261 char password_with_pad[CIFS_ENCPWD_SIZE];
262
263 memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500264 if (password)
265 strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
Steve French7c7b25b2006-06-01 19:20:10 +0000266
Jeff Layton04912d62010-04-24 07:57:45 -0400267 if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500268 memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE);
269 memcpy(lnm_session_key, password_with_pad,
270 CIFS_ENCPWD_SIZE);
Steve French43988d72011-04-19 18:23:31 +0000271 return 0;
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500272 }
Steve Frenchbdc4bf6e2006-06-02 22:57:13 +0000273
Steve French7c7b25b2006-06-01 19:20:10 +0000274 /* calculate old style session key */
275 /* calling toupper is less broken than repeatedly
276 calling nls_toupper would be since that will never
277 work for UTF8, but neither handles multibyte code pages
278 but the only alternative would be converting to UCS-16 (Unicode)
279 (using a routine something like UniStrupr) then
280 uppercasing and then converting back from Unicode - which
281 would only worth doing it if we knew it were utf8. Basically
282 utf8 and other multibyte codepages each need their own strupper
283 function since a byte at a time will ont work. */
284
Shirish Pargaonkaref571ca2008-07-24 15:56:05 +0000285 for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
Steve French7c7b25b2006-06-01 19:20:10 +0000286 password_with_pad[i] = toupper(password_with_pad[i]);
Steve French7c7b25b2006-06-01 19:20:10 +0000287
Steve French43988d72011-04-19 18:23:31 +0000288 rc = SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500289
Steve French43988d72011-04-19 18:23:31 +0000290 return rc;
Steve French7c7b25b2006-06-01 19:20:10 +0000291}
292#endif /* CIFS_WEAK_PW_HASH */
293
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500294/* Build a proper attribute value/target info pairs blob.
295 * Fill in netbios and dns domain name and workstation name
296 * and client time (total five av pairs and + one end of fields indicator.
297 * Allocate domain name which gets freed when session struct is deallocated.
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500298 */
299static int
Steve French96daf2b2011-05-27 04:34:02 +0000300build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500301{
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500302 unsigned int dlen;
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500303 unsigned int size = 2 * sizeof(struct ntlmssp2_name);
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500304 char *defdmname = "WORKGROUP";
305 unsigned char *blobptr;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500306 struct ntlmssp2_name *attrptr;
307
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500308 if (!ses->domainName) {
309 ses->domainName = kstrdup(defdmname, GFP_KERNEL);
310 if (!ses->domainName)
311 return -ENOMEM;
312 }
313
314 dlen = strlen(ses->domainName);
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500315
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500316 /*
317 * The length of this blob is two times the size of a
318 * structure (av pair) which holds name/size
319 * ( for NTLMSSP_AV_NB_DOMAIN_NAME followed by NTLMSSP_AV_EOL ) +
320 * unicode length of a netbios domain name
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500321 */
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500322 ses->auth_key.len = size + 2 * dlen;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500323 ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
324 if (!ses->auth_key.response) {
325 ses->auth_key.len = 0;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500326 cERROR(1, "Challenge target info allocation failure");
327 return -ENOMEM;
328 }
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500329
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500330 blobptr = ses->auth_key.response;
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500331 attrptr = (struct ntlmssp2_name *) blobptr;
332
Shirish Pargaonkarcfbd6f82011-08-24 23:05:46 -0500333 /*
334 * As defined in MS-NTLM 3.3.2, just this av pair field
335 * is sufficient as part of the temp
336 */
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500337 attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
338 attrptr->length = cpu_to_le16(2 * dlen);
339 blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
Steve Frenchacbbb762012-01-18 22:32:33 -0600340 cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500341
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500342 return 0;
343}
344
345/* Server has provided av pairs/target info in the type 2 challenge
346 * packet and we have plucked it and stored within smb session.
347 * We parse that blob here to find netbios domain name to be used
348 * as part of ntlmv2 authentication (in Target String), if not already
349 * specified on the command line.
350 * If this function returns without any error but without fetching
351 * domain name, authentication may fail against some server but
352 * may not fail against other (those who are not very particular
353 * about target string i.e. for some, just user name might suffice.
354 */
355static int
Steve French96daf2b2011-05-27 04:34:02 +0000356find_domain_name(struct cifs_ses *ses, const struct nls_table *nls_cp)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500357{
358 unsigned int attrsize;
359 unsigned int type;
360 unsigned int onesize = sizeof(struct ntlmssp2_name);
361 unsigned char *blobptr;
362 unsigned char *blobend;
363 struct ntlmssp2_name *attrptr;
364
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500365 if (!ses->auth_key.len || !ses->auth_key.response)
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500366 return 0;
367
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500368 blobptr = ses->auth_key.response;
369 blobend = blobptr + ses->auth_key.len;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500370
371 while (blobptr + onesize < blobend) {
372 attrptr = (struct ntlmssp2_name *) blobptr;
373 type = le16_to_cpu(attrptr->type);
374 if (type == NTLMSSP_AV_EOL)
375 break;
376 blobptr += 2; /* advance attr type */
377 attrsize = le16_to_cpu(attrptr->length);
378 blobptr += 2; /* advance attr size */
379 if (blobptr + attrsize > blobend)
380 break;
381 if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
382 if (!attrsize)
383 break;
384 if (!ses->domainName) {
385 ses->domainName =
386 kmalloc(attrsize + 1, GFP_KERNEL);
387 if (!ses->domainName)
388 return -ENOMEM;
Steve Frenchacbbb762012-01-18 22:32:33 -0600389 cifs_from_utf16(ses->domainName,
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500390 (__le16 *)blobptr, attrsize, attrsize,
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -0500391 nls_cp, false);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500392 break;
393 }
394 }
395 blobptr += attrsize; /* advance attr value */
396 }
397
398 return 0;
399}
400
Steve French96daf2b2011-05-27 04:34:02 +0000401static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
Steve French50c2f752007-07-13 00:33:32 +0000402 const struct nls_table *nls_cp)
Steve Frencha8ee0342006-06-05 23:34:19 +0000403{
404 int rc = 0;
405 int len;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500406 char nt_hash[CIFS_NTHASH_SIZE];
Steve French50c2f752007-07-13 00:33:32 +0000407 wchar_t *user;
408 wchar_t *domain;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500409 wchar_t *server;
Steve Frenchc8e56f12010-09-08 21:10:58 +0000410
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500411 if (!ses->server->secmech.sdeschmacmd5) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400412 cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash");
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500413 return -1;
414 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000415
416 /* calculate md4 hash of password */
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500417 E_md4hash(ses->password, nt_hash, nls_cp);
Steve Frencha8ee0342006-06-05 23:34:19 +0000418
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500419 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500420 CIFS_NTHASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500421 if (rc) {
422 cERROR(1, "%s: Could not set NT Hash as a key", __func__);
423 return rc;
424 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500425
426 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
427 if (rc) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400428 cERROR(1, "calc_ntlmv2_hash: could not init hmacmd5");
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500429 return rc;
430 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000431
Steve French8727c8a2011-02-25 01:11:56 -0600432 /* convert ses->user_name to unicode and uppercase */
Jeff Layton04febab2012-01-17 16:09:15 -0500433 len = ses->user_name ? strlen(ses->user_name) : 0;
Steve French1717ffc2006-06-08 05:41:32 +0000434 user = kmalloc(2 + (len * 2), GFP_KERNEL);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500435 if (user == NULL) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400436 cERROR(1, "calc_ntlmv2_hash: user mem alloc failure");
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500437 rc = -ENOMEM;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500438 return rc;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500439 }
Jeff Layton04febab2012-01-17 16:09:15 -0500440
441 if (len) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600442 len = cifs_strtoUTF16((__le16 *)user, ses->user_name, len, nls_cp);
Jeff Layton04febab2012-01-17 16:09:15 -0500443 UniStrupr(user);
444 } else {
445 memset(user, '\0', 2);
446 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500447
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500448 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500449 (char *)user, 2 * len);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500450 kfree(user);
451 if (rc) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400452 cERROR(1, "%s: Could not update with user", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500453 return rc;
454 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000455
456 /* convert ses->domainName to unicode and uppercase */
Steve French50c2f752007-07-13 00:33:32 +0000457 if (ses->domainName) {
Steve French1717ffc2006-06-08 05:41:32 +0000458 len = strlen(ses->domainName);
Steve Frencha8ee0342006-06-05 23:34:19 +0000459
Steve French50c2f752007-07-13 00:33:32 +0000460 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500461 if (domain == NULL) {
462 cERROR(1, "calc_ntlmv2_hash: domain mem alloc failure");
463 rc = -ENOMEM;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500464 return rc;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500465 }
Steve Frenchacbbb762012-01-18 22:32:33 -0600466 len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
467 nls_cp);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500468 rc =
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500469 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
470 (char *)domain, 2 * len);
Steve French1717ffc2006-06-08 05:41:32 +0000471 kfree(domain);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500472 if (rc) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400473 cERROR(1, "%s: Could not update with domain",
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500474 __func__);
475 return rc;
476 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500477 } else if (ses->serverName) {
478 len = strlen(ses->serverName);
479
480 server = kmalloc(2 + (len * 2), GFP_KERNEL);
481 if (server == NULL) {
482 cERROR(1, "calc_ntlmv2_hash: server mem alloc failure");
483 rc = -ENOMEM;
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500484 return rc;
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500485 }
Steve Frenchacbbb762012-01-18 22:32:33 -0600486 len = cifs_strtoUTF16((__le16 *)server, ses->serverName, len,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500487 nls_cp);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500488 rc =
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500489 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
490 (char *)server, 2 * len);
491 kfree(server);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500492 if (rc) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400493 cERROR(1, "%s: Could not update with server",
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500494 __func__);
495 return rc;
496 }
Steve French1717ffc2006-06-08 05:41:32 +0000497 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500498
499 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500500 ntlmv2_hash);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500501 if (rc)
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400502 cERROR(1, "%s: Could not generate md5 hash", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500503
Steve Frencha8ee0342006-06-05 23:34:19 +0000504 return rc;
505}
506
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500507static int
Steve French96daf2b2011-05-27 04:34:02 +0000508CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500509{
510 int rc;
511 unsigned int offset = CIFS_SESS_KEY_SIZE + 8;
512
513 if (!ses->server->secmech.sdeschmacmd5) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400514 cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash");
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500515 return -1;
516 }
517
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500518 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500519 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500520 if (rc) {
521 cERROR(1, "%s: Could not set NTLMV2 Hash as a key", __func__);
522 return rc;
523 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500524
525 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
526 if (rc) {
527 cERROR(1, "CalcNTLMv2_response: could not init hmacmd5");
528 return rc;
529 }
530
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500531 if (ses->server->secType == RawNTLMSSP)
532 memcpy(ses->auth_key.response + offset,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500533 ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500534 else
535 memcpy(ses->auth_key.response + offset,
536 ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500537 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500538 ses->auth_key.response + offset, ses->auth_key.len - offset);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500539 if (rc) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400540 cERROR(1, "%s: Could not update with response", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500541 return rc;
542 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500543
544 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
545 ses->auth_key.response + CIFS_SESS_KEY_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500546 if (rc)
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400547 cERROR(1, "%s: Could not generate md5 hash", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500548
549 return rc;
550}
551
552
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500553int
Steve French96daf2b2011-05-27 04:34:02 +0000554setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
Steve French9fbc5902010-08-20 20:42:26 +0000555{
Steve Frenchc8e56f12010-09-08 21:10:58 +0000556 int rc;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500557 int baselen;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500558 unsigned int tilen;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500559 struct ntlmv2_resp *buf;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500560 char ntlmv2_hash[16];
561 unsigned char *tiblob = NULL; /* target info blob */
Steve French6d027cf2006-06-05 16:26:05 +0000562
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500563 if (ses->server->secType == RawNTLMSSP) {
564 if (!ses->domainName) {
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -0500565 rc = find_domain_name(ses, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500566 if (rc) {
567 cERROR(1, "error %d finding domain name", rc);
568 goto setup_ntlmv2_rsp_ret;
569 }
570 }
571 } else {
Shirish Pargaonkar9daa42e2010-10-10 13:21:05 -0500572 rc = build_avpair_blob(ses, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500573 if (rc) {
574 cERROR(1, "error %d building av pair blob", rc);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500575 goto setup_ntlmv2_rsp_ret;
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500576 }
577 }
Steve Frencha8ee0342006-06-05 23:34:19 +0000578
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500579 baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500580 tilen = ses->auth_key.len;
581 tiblob = ses->auth_key.response;
582
583 ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500584 if (!ses->auth_key.response) {
585 rc = ENOMEM;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500586 ses->auth_key.len = 0;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500587 cERROR(1, "%s: Can't allocate auth blob", __func__);
588 goto setup_ntlmv2_rsp_ret;
589 }
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500590 ses->auth_key.len += baselen;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500591
592 buf = (struct ntlmv2_resp *)
593 (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
594 buf->blob_signature = cpu_to_le32(0x00000101);
595 buf->reserved = 0;
596 buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
597 get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
598 buf->reserved2 = 0;
599
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500600 memcpy(ses->auth_key.response + baselen, tiblob, tilen);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500601
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -0500602 /* calculate ntlmv2_hash */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500603 rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500604 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000605 cERROR(1, "could not get v2 hash rc %d", rc);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500606 goto setup_ntlmv2_rsp_ret;
607 }
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -0500608
609 /* calculate first part of the client response (CR1) */
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500610 rc = CalcNTLMv2_response(ses, ntlmv2_hash);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500611 if (rc) {
612 cERROR(1, "Could not calculate CR1 rc: %d", rc);
613 goto setup_ntlmv2_rsp_ret;
614 }
Steve Frenchb609f062007-07-09 07:55:14 +0000615
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500616 /* now calculate the session key for NTLMv2 */
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500617 rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500618 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500619 if (rc) {
620 cERROR(1, "%s: Could not set NTLMV2 Hash as a key", __func__);
621 goto setup_ntlmv2_rsp_ret;
622 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500623
624 rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
625 if (rc) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400626 cERROR(1, "%s: Could not init hmacmd5", __func__);
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500627 goto setup_ntlmv2_rsp_ret;
628 }
629
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500630 rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500631 ses->auth_key.response + CIFS_SESS_KEY_SIZE,
632 CIFS_HMAC_MD5_HASH_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500633 if (rc) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400634 cERROR(1, "%s: Could not update with response", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500635 goto setup_ntlmv2_rsp_ret;
636 }
Shirish Pargaonkar307fbd32010-10-21 14:25:17 -0500637
638 rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
639 ses->auth_key.response);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500640 if (rc)
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400641 cERROR(1, "%s: Could not generate md5 hash", __func__);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500642
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500643setup_ntlmv2_rsp_ret:
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500644 kfree(tiblob);
Shirish Pargaonkar2b149f12010-09-18 22:02:18 -0500645
646 return rc;
Steve French6d027cf2006-06-05 16:26:05 +0000647}
648
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500649int
Steve French96daf2b2011-05-27 04:34:02 +0000650calc_seckey(struct cifs_ses *ses)
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500651{
652 int rc;
653 struct crypto_blkcipher *tfm_arc4;
654 struct scatterlist sgin, sgout;
655 struct blkcipher_desc desc;
656 unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
657
658 get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
659
660 tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
Shirish Pargaonkar7a8587e2011-01-29 13:54:58 -0600661 if (IS_ERR(tfm_arc4)) {
662 rc = PTR_ERR(tfm_arc4);
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400663 cERROR(1, "could not allocate crypto API arc4");
Shirish Pargaonkar7a8587e2011-01-29 13:54:58 -0600664 return rc;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500665 }
666
667 desc.tfm = tfm_arc4;
668
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500669 rc = crypto_blkcipher_setkey(tfm_arc4, ses->auth_key.response,
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500670 CIFS_SESS_KEY_SIZE);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500671 if (rc) {
672 cERROR(1, "%s: Could not set response as a key", __func__);
673 return rc;
674 }
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500675
676 sg_init_one(&sgin, sec_key, CIFS_SESS_KEY_SIZE);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -0500677 sg_init_one(&sgout, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500678
679 rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
680 if (rc) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400681 cERROR(1, "could not encrypt session key rc: %d", rc);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500682 crypto_free_blkcipher(tfm_arc4);
683 return rc;
684 }
685
686 /* make secondary_key/nonce as session key */
687 memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
688 /* and make len as that of session key only */
689 ses->auth_key.len = CIFS_SESS_KEY_SIZE;
690
691 crypto_free_blkcipher(tfm_arc4);
692
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500693 return rc;
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500694}
695
696void
697cifs_crypto_shash_release(struct TCP_Server_Info *server)
698{
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700699 if (server->secmech.hmacsha256)
700 crypto_free_shash(server->secmech.hmacsha256);
701
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500702 if (server->secmech.md5)
703 crypto_free_shash(server->secmech.md5);
704
705 if (server->secmech.hmacmd5)
706 crypto_free_shash(server->secmech.hmacmd5);
707
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700708 kfree(server->secmech.sdeschmacsha256);
709
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500710 kfree(server->secmech.sdeschmacmd5);
711
712 kfree(server->secmech.sdescmd5);
713}
714
715int
716cifs_crypto_shash_allocate(struct TCP_Server_Info *server)
717{
718 int rc;
719 unsigned int size;
720
721 server->secmech.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600722 if (IS_ERR(server->secmech.hmacmd5)) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400723 cERROR(1, "could not allocate crypto hmacmd5");
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500724 return PTR_ERR(server->secmech.hmacmd5);
725 }
726
727 server->secmech.md5 = crypto_alloc_shash("md5", 0, 0);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600728 if (IS_ERR(server->secmech.md5)) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400729 cERROR(1, "could not allocate crypto md5");
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500730 rc = PTR_ERR(server->secmech.md5);
731 goto crypto_allocate_md5_fail;
732 }
733
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700734 server->secmech.hmacsha256 = crypto_alloc_shash("hmac(sha256)", 0, 0);
735 if (IS_ERR(server->secmech.hmacsha256)) {
736 cERROR(1, "could not allocate crypto hmacsha256\n");
737 rc = PTR_ERR(server->secmech.hmacsha256);
738 goto crypto_allocate_hmacsha256_fail;
739 }
740
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500741 size = sizeof(struct shash_desc) +
742 crypto_shash_descsize(server->secmech.hmacmd5);
743 server->secmech.sdeschmacmd5 = kmalloc(size, GFP_KERNEL);
744 if (!server->secmech.sdeschmacmd5) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400745 cERROR(1, "cifs_crypto_shash_allocate: can't alloc hmacmd5");
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500746 rc = -ENOMEM;
747 goto crypto_allocate_hmacmd5_sdesc_fail;
748 }
749 server->secmech.sdeschmacmd5->shash.tfm = server->secmech.hmacmd5;
750 server->secmech.sdeschmacmd5->shash.flags = 0x0;
751
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500752 size = sizeof(struct shash_desc) +
753 crypto_shash_descsize(server->secmech.md5);
754 server->secmech.sdescmd5 = kmalloc(size, GFP_KERNEL);
755 if (!server->secmech.sdescmd5) {
Jeff Laytonac3aa2f2012-07-23 13:14:28 -0400756 cERROR(1, "cifs_crypto_shash_allocate: can't alloc md5");
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500757 rc = -ENOMEM;
758 goto crypto_allocate_md5_sdesc_fail;
759 }
760 server->secmech.sdescmd5->shash.tfm = server->secmech.md5;
761 server->secmech.sdescmd5->shash.flags = 0x0;
762
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700763 size = sizeof(struct shash_desc) +
764 crypto_shash_descsize(server->secmech.hmacsha256);
765 server->secmech.sdeschmacsha256 = kmalloc(size, GFP_KERNEL);
766 if (!server->secmech.sdeschmacsha256) {
767 cERROR(1, "%s: Can't alloc hmacsha256\n", __func__);
768 rc = -ENOMEM;
769 goto crypto_allocate_hmacsha256_sdesc_fail;
770 }
771 server->secmech.sdeschmacsha256->shash.tfm = server->secmech.hmacsha256;
772 server->secmech.sdeschmacsha256->shash.flags = 0x0;
773
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500774 return 0;
775
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700776crypto_allocate_hmacsha256_sdesc_fail:
777 kfree(server->secmech.sdescmd5);
778
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500779crypto_allocate_md5_sdesc_fail:
780 kfree(server->secmech.sdeschmacmd5);
781
782crypto_allocate_hmacmd5_sdesc_fail:
Pavel Shilovsky3c1bf7e2012-09-18 16:20:30 -0700783 crypto_free_shash(server->secmech.hmacsha256);
784
785crypto_allocate_hmacsha256_fail:
Shirish Pargaonkard2b91522010-10-21 14:25:08 -0500786 crypto_free_shash(server->secmech.md5);
787
788crypto_allocate_md5_fail:
789 crypto_free_shash(server->secmech.hmacmd5);
790
791 return rc;
792}