Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mjg59/platf...
[pandora-kernel.git] / fs / cifs / cifsencrypt.c
1 /*
2  *   fs/cifs/cifsencrypt.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2005,2006
5  *   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>
23 #include <linux/slab.h>
24 #include "cifspdu.h"
25 #include "cifsglob.h"
26 #include "cifs_debug.h"
27 #include "cifs_unicode.h"
28 #include "cifsproto.h"
29 #include "ntlmssp.h"
30 #include <linux/ctype.h>
31 #include <linux/random.h>
32
33 /* Calculate and return the CIFS signature based on the mac key and SMB PDU */
34 /* the 16 byte signature must be allocated by the caller  */
35 /* Note we only use the 1st eight bytes */
36 /* Note that the smb header signature field on input contains the
37         sequence number before this function is called */
38
39 static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu,
40                                 struct TCP_Server_Info *server, char *signature)
41 {
42         int rc;
43
44         if (cifs_pdu == NULL || signature == NULL || server == NULL)
45                 return -EINVAL;
46
47         if (!server->secmech.sdescmd5) {
48                 cERROR(1, "%s: Can't generate signature\n", __func__);
49                 return -1;
50         }
51
52         rc = crypto_shash_init(&server->secmech.sdescmd5->shash);
53         if (rc) {
54                 cERROR(1, "%s: Oould not init md5\n", __func__);
55                 return rc;
56         }
57
58         crypto_shash_update(&server->secmech.sdescmd5->shash,
59                 server->session_key.response, server->session_key.len);
60
61         crypto_shash_update(&server->secmech.sdescmd5->shash,
62                 cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
63
64         rc = crypto_shash_final(&server->secmech.sdescmd5->shash, signature);
65
66         return 0;
67 }
68
69 /* must be called with server->srv_mutex held */
70 int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
71                   __u32 *pexpected_response_sequence_number)
72 {
73         int rc = 0;
74         char smb_signature[20];
75
76         if ((cifs_pdu == NULL) || (server == NULL))
77                 return -EINVAL;
78
79         if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
80                 return rc;
81
82         cifs_pdu->Signature.Sequence.SequenceNumber =
83                         cpu_to_le32(server->sequence_number);
84         cifs_pdu->Signature.Sequence.Reserved = 0;
85
86         *pexpected_response_sequence_number = server->sequence_number++;
87         server->sequence_number++;
88
89         rc = cifs_calculate_signature(cifs_pdu, server, smb_signature);
90         if (rc)
91                 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
92         else
93                 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
94
95         return rc;
96 }
97
98 static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
99                                 struct TCP_Server_Info *server, char *signature)
100 {
101         int i;
102         int rc;
103
104         if (iov == NULL || signature == NULL || server == NULL)
105                 return -EINVAL;
106
107         if (!server->secmech.sdescmd5) {
108                 cERROR(1, "%s: Can't generate signature\n", __func__);
109                 return -1;
110         }
111
112         rc = crypto_shash_init(&server->secmech.sdescmd5->shash);
113         if (rc) {
114                 cERROR(1, "%s: Oould not init md5\n", __func__);
115                 return rc;
116         }
117
118         crypto_shash_update(&server->secmech.sdescmd5->shash,
119                 server->session_key.response, server->session_key.len);
120
121         for (i = 0; i < n_vec; i++) {
122                 if (iov[i].iov_len == 0)
123                         continue;
124                 if (iov[i].iov_base == NULL) {
125                         cERROR(1, "null iovec entry");
126                         return -EIO;
127                 }
128                 /* The first entry includes a length field (which does not get
129                    signed that occupies the first 4 bytes before the header */
130                 if (i == 0) {
131                         if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
132                                 break; /* nothing to sign or corrupt header */
133                         crypto_shash_update(&server->secmech.sdescmd5->shash,
134                                 iov[i].iov_base + 4, iov[i].iov_len - 4);
135                 } else
136                         crypto_shash_update(&server->secmech.sdescmd5->shash,
137                                 iov[i].iov_base, iov[i].iov_len);
138         }
139
140         rc = crypto_shash_final(&server->secmech.sdescmd5->shash, signature);
141
142         return rc;
143 }
144
145 /* must be called with server->srv_mutex held */
146 int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
147                    __u32 *pexpected_response_sequence_number)
148 {
149         int rc = 0;
150         char smb_signature[20];
151         struct smb_hdr *cifs_pdu = iov[0].iov_base;
152
153         if ((cifs_pdu == NULL) || (server == NULL))
154                 return -EINVAL;
155
156         if ((cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) == 0)
157                 return rc;
158
159         cifs_pdu->Signature.Sequence.SequenceNumber =
160                                 cpu_to_le32(server->sequence_number);
161         cifs_pdu->Signature.Sequence.Reserved = 0;
162
163         *pexpected_response_sequence_number = server->sequence_number++;
164         server->sequence_number++;
165
166         rc = cifs_calc_signature2(iov, n_vec, server, smb_signature);
167         if (rc)
168                 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
169         else
170                 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
171
172         return rc;
173 }
174
175 int cifs_verify_signature(struct smb_hdr *cifs_pdu,
176                           struct TCP_Server_Info *server,
177                           __u32 expected_sequence_number)
178 {
179         unsigned int rc;
180         char server_response_sig[8];
181         char what_we_think_sig_should_be[20];
182
183         if (cifs_pdu == NULL || server == NULL)
184                 return -EINVAL;
185
186         if (cifs_pdu->Command == SMB_COM_NEGOTIATE)
187                 return 0;
188
189         if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
190                 struct smb_com_lock_req *pSMB =
191                         (struct smb_com_lock_req *)cifs_pdu;
192             if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
193                         return 0;
194         }
195
196         /* BB what if signatures are supposed to be on for session but
197            server does not send one? BB */
198
199         /* Do not need to verify session setups with signature "BSRSPYL "  */
200         if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
201                 cFYI(1, "dummy signature received for smb command 0x%x",
202                         cifs_pdu->Command);
203
204         /* save off the origiginal signature so we can modify the smb and check
205                 its signature against what the server sent */
206         memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
207
208         cifs_pdu->Signature.Sequence.SequenceNumber =
209                                         cpu_to_le32(expected_sequence_number);
210         cifs_pdu->Signature.Sequence.Reserved = 0;
211
212         rc = cifs_calculate_signature(cifs_pdu, server,
213                 what_we_think_sig_should_be);
214
215         if (rc)
216                 return rc;
217
218 /*      cifs_dump_mem("what we think it should be: ",
219                       what_we_think_sig_should_be, 16); */
220
221         if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
222                 return -EACCES;
223         else
224                 return 0;
225
226 }
227
228 /* first calculate 24 bytes ntlm response and then 16 byte session key */
229 int setup_ntlm_response(struct cifsSesInfo *ses)
230 {
231         int rc = 0;
232         unsigned int temp_len = CIFS_SESS_KEY_SIZE + CIFS_AUTH_RESP_SIZE;
233         char temp_key[CIFS_SESS_KEY_SIZE];
234
235         if (!ses)
236                 return -EINVAL;
237
238         ses->auth_key.response = kmalloc(temp_len, GFP_KERNEL);
239         if (!ses->auth_key.response) {
240                 cERROR(1, "NTLM can't allocate (%u bytes) memory", temp_len);
241                 return -ENOMEM;
242         }
243         ses->auth_key.len = temp_len;
244
245         rc = SMBNTencrypt(ses->password, ses->server->cryptkey,
246                         ses->auth_key.response + CIFS_SESS_KEY_SIZE);
247         if (rc) {
248                 cFYI(1, "%s Can't generate NTLM response, error: %d",
249                         __func__, rc);
250                 return rc;
251         }
252
253         rc = E_md4hash(ses->password, temp_key);
254         if (rc) {
255                 cFYI(1, "%s Can't generate NT hash, error: %d", __func__, rc);
256                 return rc;
257         }
258
259         rc = mdfour(ses->auth_key.response, temp_key, CIFS_SESS_KEY_SIZE);
260         if (rc)
261                 cFYI(1, "%s Can't generate NTLM session key, error: %d",
262                         __func__, rc);
263
264         return rc;
265 }
266
267 #ifdef CONFIG_CIFS_WEAK_PW_HASH
268 void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
269                         char *lnm_session_key)
270 {
271         int i;
272         char password_with_pad[CIFS_ENCPWD_SIZE];
273
274         memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
275         if (password)
276                 strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
277
278         if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
279                 memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE);
280                 memcpy(lnm_session_key, password_with_pad,
281                         CIFS_ENCPWD_SIZE);
282                 return;
283         }
284
285         /* calculate old style session key */
286         /* calling toupper is less broken than repeatedly
287         calling nls_toupper would be since that will never
288         work for UTF8, but neither handles multibyte code pages
289         but the only alternative would be converting to UCS-16 (Unicode)
290         (using a routine something like UniStrupr) then
291         uppercasing and then converting back from Unicode - which
292         would only worth doing it if we knew it were utf8. Basically
293         utf8 and other multibyte codepages each need their own strupper
294         function since a byte at a time will ont work. */
295
296         for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
297                 password_with_pad[i] = toupper(password_with_pad[i]);
298
299         SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
300
301         /* clear password before we return/free memory */
302         memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
303 }
304 #endif /* CIFS_WEAK_PW_HASH */
305
306 /* Build a proper attribute value/target info pairs blob.
307  * Fill in netbios and dns domain name and workstation name
308  * and client time (total five av pairs and + one end of fields indicator.
309  * Allocate domain name which gets freed when session struct is deallocated.
310  */
311 static int
312 build_avpair_blob(struct cifsSesInfo *ses, const struct nls_table *nls_cp)
313 {
314         unsigned int dlen;
315         unsigned int wlen;
316         unsigned int size = 6 * sizeof(struct ntlmssp2_name);
317         __le64  curtime;
318         char *defdmname = "WORKGROUP";
319         unsigned char *blobptr;
320         struct ntlmssp2_name *attrptr;
321
322         if (!ses->domainName) {
323                 ses->domainName = kstrdup(defdmname, GFP_KERNEL);
324                 if (!ses->domainName)
325                         return -ENOMEM;
326         }
327
328         dlen = strlen(ses->domainName);
329         wlen = strlen(ses->server->hostname);
330
331         /* The length of this blob is a size which is
332          * six times the size of a structure which holds name/size +
333          * two times the unicode length of a domain name +
334          * two times the unicode length of a server name +
335          * size of a timestamp (which is 8 bytes).
336          */
337         ses->auth_key.len = size + 2 * (2 * dlen) + 2 * (2 * wlen) + 8;
338         ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
339         if (!ses->auth_key.response) {
340                 ses->auth_key.len = 0;
341                 cERROR(1, "Challenge target info allocation failure");
342                 return -ENOMEM;
343         }
344
345         blobptr = ses->auth_key.response;
346         attrptr = (struct ntlmssp2_name *) blobptr;
347
348         attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_DOMAIN_NAME);
349         attrptr->length = cpu_to_le16(2 * dlen);
350         blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
351         cifs_strtoUCS((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
352
353         blobptr += 2 * dlen;
354         attrptr = (struct ntlmssp2_name *) blobptr;
355
356         attrptr->type = cpu_to_le16(NTLMSSP_AV_NB_COMPUTER_NAME);
357         attrptr->length = cpu_to_le16(2 * wlen);
358         blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
359         cifs_strtoUCS((__le16 *)blobptr, ses->server->hostname, wlen, nls_cp);
360
361         blobptr += 2 * wlen;
362         attrptr = (struct ntlmssp2_name *) blobptr;
363
364         attrptr->type = cpu_to_le16(NTLMSSP_AV_DNS_DOMAIN_NAME);
365         attrptr->length = cpu_to_le16(2 * dlen);
366         blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
367         cifs_strtoUCS((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
368
369         blobptr += 2 * dlen;
370         attrptr = (struct ntlmssp2_name *) blobptr;
371
372         attrptr->type = cpu_to_le16(NTLMSSP_AV_DNS_COMPUTER_NAME);
373         attrptr->length = cpu_to_le16(2 * wlen);
374         blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
375         cifs_strtoUCS((__le16 *)blobptr, ses->server->hostname, wlen, nls_cp);
376
377         blobptr += 2 * wlen;
378         attrptr = (struct ntlmssp2_name *) blobptr;
379
380         attrptr->type = cpu_to_le16(NTLMSSP_AV_TIMESTAMP);
381         attrptr->length = cpu_to_le16(sizeof(__le64));
382         blobptr = (unsigned char *)attrptr + sizeof(struct ntlmssp2_name);
383         curtime = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
384         memcpy(blobptr, &curtime, sizeof(__le64));
385
386         return 0;
387 }
388
389 /* Server has provided av pairs/target info in the type 2 challenge
390  * packet and we have plucked it and stored within smb session.
391  * We parse that blob here to find netbios domain name to be used
392  * as part of ntlmv2 authentication (in Target String), if not already
393  * specified on the command line.
394  * If this function returns without any error but without fetching
395  * domain name, authentication may fail against some server but
396  * may not fail against other (those who are not very particular
397  * about target string i.e. for some, just user name might suffice.
398  */
399 static int
400 find_domain_name(struct cifsSesInfo *ses, const struct nls_table *nls_cp)
401 {
402         unsigned int attrsize;
403         unsigned int type;
404         unsigned int onesize = sizeof(struct ntlmssp2_name);
405         unsigned char *blobptr;
406         unsigned char *blobend;
407         struct ntlmssp2_name *attrptr;
408
409         if (!ses->auth_key.len || !ses->auth_key.response)
410                 return 0;
411
412         blobptr = ses->auth_key.response;
413         blobend = blobptr + ses->auth_key.len;
414
415         while (blobptr + onesize < blobend) {
416                 attrptr = (struct ntlmssp2_name *) blobptr;
417                 type = le16_to_cpu(attrptr->type);
418                 if (type == NTLMSSP_AV_EOL)
419                         break;
420                 blobptr += 2; /* advance attr type */
421                 attrsize = le16_to_cpu(attrptr->length);
422                 blobptr += 2; /* advance attr size */
423                 if (blobptr + attrsize > blobend)
424                         break;
425                 if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
426                         if (!attrsize)
427                                 break;
428                         if (!ses->domainName) {
429                                 ses->domainName =
430                                         kmalloc(attrsize + 1, GFP_KERNEL);
431                                 if (!ses->domainName)
432                                                 return -ENOMEM;
433                                 cifs_from_ucs2(ses->domainName,
434                                         (__le16 *)blobptr, attrsize, attrsize,
435                                         nls_cp, false);
436                                 break;
437                         }
438                 }
439                 blobptr += attrsize; /* advance attr  value */
440         }
441
442         return 0;
443 }
444
445 static int calc_ntlmv2_hash(struct cifsSesInfo *ses, char *ntlmv2_hash,
446                             const struct nls_table *nls_cp)
447 {
448         int rc = 0;
449         int len;
450         char nt_hash[CIFS_NTHASH_SIZE];
451         wchar_t *user;
452         wchar_t *domain;
453         wchar_t *server;
454
455         if (!ses->server->secmech.sdeschmacmd5) {
456                 cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
457                 return -1;
458         }
459
460         /* calculate md4 hash of password */
461         E_md4hash(ses->password, nt_hash);
462
463         crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash,
464                                 CIFS_NTHASH_SIZE);
465
466         rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
467         if (rc) {
468                 cERROR(1, "calc_ntlmv2_hash: could not init hmacmd5\n");
469                 return rc;
470         }
471
472         /* convert ses->userName to unicode and uppercase */
473         len = strlen(ses->userName);
474         user = kmalloc(2 + (len * 2), GFP_KERNEL);
475         if (user == NULL) {
476                 cERROR(1, "calc_ntlmv2_hash: user mem alloc failure\n");
477                 rc = -ENOMEM;
478                 goto calc_exit_2;
479         }
480         len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp);
481         UniStrupr(user);
482
483         crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
484                                 (char *)user, 2 * len);
485
486         /* convert ses->domainName to unicode and uppercase */
487         if (ses->domainName) {
488                 len = strlen(ses->domainName);
489
490                 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
491                 if (domain == NULL) {
492                         cERROR(1, "calc_ntlmv2_hash: domain mem alloc failure");
493                         rc = -ENOMEM;
494                         goto calc_exit_1;
495                 }
496                 len = cifs_strtoUCS((__le16 *)domain, ses->domainName, len,
497                                         nls_cp);
498                 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
499                                         (char *)domain, 2 * len);
500                 kfree(domain);
501         } else if (ses->serverName) {
502                 len = strlen(ses->serverName);
503
504                 server = kmalloc(2 + (len * 2), GFP_KERNEL);
505                 if (server == NULL) {
506                         cERROR(1, "calc_ntlmv2_hash: server mem alloc failure");
507                         rc = -ENOMEM;
508                         goto calc_exit_1;
509                 }
510                 len = cifs_strtoUCS((__le16 *)server, ses->serverName, len,
511                                         nls_cp);
512                 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
513                                         (char *)server, 2 * len);
514                 kfree(server);
515         }
516
517         rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
518                                         ntlmv2_hash);
519
520 calc_exit_1:
521         kfree(user);
522 calc_exit_2:
523         return rc;
524 }
525
526 static int
527 CalcNTLMv2_response(const struct cifsSesInfo *ses, char *ntlmv2_hash)
528 {
529         int rc;
530         unsigned int offset = CIFS_SESS_KEY_SIZE + 8;
531
532         if (!ses->server->secmech.sdeschmacmd5) {
533                 cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash\n");
534                 return -1;
535         }
536
537         crypto_shash_setkey(ses->server->secmech.hmacmd5,
538                                 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
539
540         rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
541         if (rc) {
542                 cERROR(1, "CalcNTLMv2_response: could not init hmacmd5");
543                 return rc;
544         }
545
546         if (ses->server->secType == RawNTLMSSP)
547                 memcpy(ses->auth_key.response + offset,
548                         ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
549         else
550                 memcpy(ses->auth_key.response + offset,
551                         ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
552         crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
553                 ses->auth_key.response + offset, ses->auth_key.len - offset);
554
555         rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
556                 ses->auth_key.response + CIFS_SESS_KEY_SIZE);
557
558         return rc;
559 }
560
561
562 int
563 setup_ntlmv2_rsp(struct cifsSesInfo *ses, const struct nls_table *nls_cp)
564 {
565         int rc;
566         int baselen;
567         unsigned int tilen;
568         struct ntlmv2_resp *buf;
569         char ntlmv2_hash[16];
570         unsigned char *tiblob = NULL; /* target info blob */
571
572         if (ses->server->secType == RawNTLMSSP) {
573                 if (!ses->domainName) {
574                         rc = find_domain_name(ses, nls_cp);
575                         if (rc) {
576                                 cERROR(1, "error %d finding domain name", rc);
577                                 goto setup_ntlmv2_rsp_ret;
578                         }
579                 }
580         } else {
581                 rc = build_avpair_blob(ses, nls_cp);
582                 if (rc) {
583                         cERROR(1, "error %d building av pair blob", rc);
584                         goto setup_ntlmv2_rsp_ret;
585                 }
586         }
587
588         baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
589         tilen = ses->auth_key.len;
590         tiblob = ses->auth_key.response;
591
592         ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
593         if (!ses->auth_key.response) {
594                 rc = ENOMEM;
595                 ses->auth_key.len = 0;
596                 cERROR(1, "%s: Can't allocate auth blob", __func__);
597                 goto setup_ntlmv2_rsp_ret;
598         }
599         ses->auth_key.len += baselen;
600
601         buf = (struct ntlmv2_resp *)
602                         (ses->auth_key.response + CIFS_SESS_KEY_SIZE);
603         buf->blob_signature = cpu_to_le32(0x00000101);
604         buf->reserved = 0;
605         buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
606         get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
607         buf->reserved2 = 0;
608
609         memcpy(ses->auth_key.response + baselen, tiblob, tilen);
610
611         /* calculate ntlmv2_hash */
612         rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
613         if (rc) {
614                 cERROR(1, "could not get v2 hash rc %d", rc);
615                 goto setup_ntlmv2_rsp_ret;
616         }
617
618         /* calculate first part of the client response (CR1) */
619         rc = CalcNTLMv2_response(ses, ntlmv2_hash);
620         if (rc) {
621                 cERROR(1, "Could not calculate CR1  rc: %d", rc);
622                 goto setup_ntlmv2_rsp_ret;
623         }
624
625         /* now calculate the session key for NTLMv2 */
626         crypto_shash_setkey(ses->server->secmech.hmacmd5,
627                 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
628
629         rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
630         if (rc) {
631                 cERROR(1, "%s: Could not init hmacmd5\n", __func__);
632                 goto setup_ntlmv2_rsp_ret;
633         }
634
635         crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
636                 ses->auth_key.response + CIFS_SESS_KEY_SIZE,
637                 CIFS_HMAC_MD5_HASH_SIZE);
638
639         rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
640                 ses->auth_key.response);
641
642 setup_ntlmv2_rsp_ret:
643         kfree(tiblob);
644
645         return rc;
646 }
647
648 int
649 calc_seckey(struct cifsSesInfo *ses)
650 {
651         int rc;
652         struct crypto_blkcipher *tfm_arc4;
653         struct scatterlist sgin, sgout;
654         struct blkcipher_desc desc;
655         unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
656
657         get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
658
659         tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
660         if (IS_ERR(tfm_arc4)) {
661                 rc = PTR_ERR(tfm_arc4);
662                 cERROR(1, "could not allocate crypto API arc4\n");
663                 return rc;
664         }
665
666         desc.tfm = tfm_arc4;
667
668         crypto_blkcipher_setkey(tfm_arc4, ses->auth_key.response,
669                                         CIFS_SESS_KEY_SIZE);
670
671         sg_init_one(&sgin, sec_key, CIFS_SESS_KEY_SIZE);
672         sg_init_one(&sgout, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
673
674         rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
675         if (rc) {
676                 cERROR(1, "could not encrypt session key rc: %d\n", rc);
677                 crypto_free_blkcipher(tfm_arc4);
678                 return rc;
679         }
680
681         /* make secondary_key/nonce as session key */
682         memcpy(ses->auth_key.response, sec_key, CIFS_SESS_KEY_SIZE);
683         /* and make len as that of session key only */
684         ses->auth_key.len = CIFS_SESS_KEY_SIZE;
685
686         crypto_free_blkcipher(tfm_arc4);
687
688         return 0;
689 }
690
691 void
692 cifs_crypto_shash_release(struct TCP_Server_Info *server)
693 {
694         if (server->secmech.md5)
695                 crypto_free_shash(server->secmech.md5);
696
697         if (server->secmech.hmacmd5)
698                 crypto_free_shash(server->secmech.hmacmd5);
699
700         kfree(server->secmech.sdeschmacmd5);
701
702         kfree(server->secmech.sdescmd5);
703 }
704
705 int
706 cifs_crypto_shash_allocate(struct TCP_Server_Info *server)
707 {
708         int rc;
709         unsigned int size;
710
711         server->secmech.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0);
712         if (IS_ERR(server->secmech.hmacmd5)) {
713                 cERROR(1, "could not allocate crypto hmacmd5\n");
714                 return PTR_ERR(server->secmech.hmacmd5);
715         }
716
717         server->secmech.md5 = crypto_alloc_shash("md5", 0, 0);
718         if (IS_ERR(server->secmech.md5)) {
719                 cERROR(1, "could not allocate crypto md5\n");
720                 rc = PTR_ERR(server->secmech.md5);
721                 goto crypto_allocate_md5_fail;
722         }
723
724         size = sizeof(struct shash_desc) +
725                         crypto_shash_descsize(server->secmech.hmacmd5);
726         server->secmech.sdeschmacmd5 = kmalloc(size, GFP_KERNEL);
727         if (!server->secmech.sdeschmacmd5) {
728                 cERROR(1, "cifs_crypto_shash_allocate: can't alloc hmacmd5\n");
729                 rc = -ENOMEM;
730                 goto crypto_allocate_hmacmd5_sdesc_fail;
731         }
732         server->secmech.sdeschmacmd5->shash.tfm = server->secmech.hmacmd5;
733         server->secmech.sdeschmacmd5->shash.flags = 0x0;
734
735
736         size = sizeof(struct shash_desc) +
737                         crypto_shash_descsize(server->secmech.md5);
738         server->secmech.sdescmd5 = kmalloc(size, GFP_KERNEL);
739         if (!server->secmech.sdescmd5) {
740                 cERROR(1, "cifs_crypto_shash_allocate: can't alloc md5\n");
741                 rc = -ENOMEM;
742                 goto crypto_allocate_md5_sdesc_fail;
743         }
744         server->secmech.sdescmd5->shash.tfm = server->secmech.md5;
745         server->secmech.sdescmd5->shash.flags = 0x0;
746
747         return 0;
748
749 crypto_allocate_md5_sdesc_fail:
750         kfree(server->secmech.sdeschmacmd5);
751
752 crypto_allocate_hmacmd5_sdesc_fail:
753         crypto_free_shash(server->secmech.md5);
754
755 crypto_allocate_md5_fail:
756         crypto_free_shash(server->secmech.hmacmd5);
757
758         return rc;
759 }