OSDN Git Service

cifs: change signing routines to deal with smb_rqst structs
[uclinux-h8/linux.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 /*
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  */
40 static int cifs_calc_signature(struct smb_rqst *rqst,
41                         struct TCP_Server_Info *server, char *signature)
42 {
43         int i;
44         int rc;
45         struct kvec *iov = rqst->rq_iov;
46         int n_vec = rqst->rq_nvec;
47
48         if (iov == NULL || signature == NULL || server == NULL)
49                 return -EINVAL;
50
51         if (!server->secmech.sdescmd5) {
52                 cERROR(1, "%s: Can't generate signature", __func__);
53                 return -1;
54         }
55
56         rc = crypto_shash_init(&server->secmech.sdescmd5->shash);
57         if (rc) {
58                 cERROR(1, "%s: Could not init md5", __func__);
59                 return rc;
60         }
61
62         rc = crypto_shash_update(&server->secmech.sdescmd5->shash,
63                 server->session_key.response, server->session_key.len);
64         if (rc) {
65                 cERROR(1, "%s: Could not update with response", __func__);
66                 return rc;
67         }
68
69         for (i = 0; i < n_vec; i++) {
70                 if (iov[i].iov_len == 0)
71                         continue;
72                 if (iov[i].iov_base == NULL) {
73                         cERROR(1, "null iovec entry");
74                         return -EIO;
75                 }
76                 /* The first entry includes a length field (which does not get
77                    signed that occupies the first 4 bytes before the header */
78                 if (i == 0) {
79                         if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
80                                 break; /* nothing to sign or corrupt header */
81                         rc =
82                         crypto_shash_update(&server->secmech.sdescmd5->shash,
83                                 iov[i].iov_base + 4, iov[i].iov_len - 4);
84                 } else {
85                         rc =
86                         crypto_shash_update(&server->secmech.sdescmd5->shash,
87                                 iov[i].iov_base, iov[i].iov_len);
88                 }
89                 if (rc) {
90                         cERROR(1, "%s: Could not update with payload",
91                                                         __func__);
92                         return rc;
93                 }
94         }
95
96         rc = crypto_shash_final(&server->secmech.sdescmd5->shash, signature);
97         if (rc)
98                 cERROR(1, "%s: Could not generate md5 hash", __func__);
99
100         return rc;
101 }
102
103 /* must be called with server->srv_mutex held */
104 int cifs_sign_rqst(struct smb_rqst *rqst, struct TCP_Server_Info *server,
105                    __u32 *pexpected_response_sequence_number)
106 {
107         int rc = 0;
108         char smb_signature[20];
109         struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
110
111         if ((cifs_pdu == NULL) || (server == NULL))
112                 return -EINVAL;
113
114         if (!(cifs_pdu->Flags2 & SMBFLG2_SECURITY_SIGNATURE) ||
115             server->tcpStatus == CifsNeedNegotiate)
116                 return rc;
117
118         if (!server->session_estab) {
119                 memcpy(cifs_pdu->Signature.SecuritySignature, "BSRSPYL", 8);
120                 return rc;
121         }
122
123         cifs_pdu->Signature.Sequence.SequenceNumber =
124                                 cpu_to_le32(server->sequence_number);
125         cifs_pdu->Signature.Sequence.Reserved = 0;
126
127         *pexpected_response_sequence_number = server->sequence_number++;
128         server->sequence_number++;
129
130         rc = cifs_calc_signature(rqst, server, smb_signature);
131         if (rc)
132                 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
133         else
134                 memcpy(cifs_pdu->Signature.SecuritySignature, smb_signature, 8);
135
136         return rc;
137 }
138
139 int 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
148 /* must be called with server->srv_mutex held */
149 int 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
157         return cifs_sign_smbv(&iov, 1, server,
158                               pexpected_response_sequence_number);
159 }
160
161 int cifs_verify_signature(struct smb_rqst *rqst,
162                           struct TCP_Server_Info *server,
163                           __u32 expected_sequence_number)
164 {
165         unsigned int rc;
166         char server_response_sig[8];
167         char what_we_think_sig_should_be[20];
168         struct smb_hdr *cifs_pdu = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
169
170         if (cifs_pdu == NULL || server == NULL)
171                 return -EINVAL;
172
173         if (!server->session_estab)
174                 return 0;
175
176         if (cifs_pdu->Command == SMB_COM_LOCKING_ANDX) {
177                 struct smb_com_lock_req *pSMB =
178                         (struct smb_com_lock_req *)cifs_pdu;
179             if (pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE)
180                         return 0;
181         }
182
183         /* BB what if signatures are supposed to be on for session but
184            server does not send one? BB */
185
186         /* Do not need to verify session setups with signature "BSRSPYL "  */
187         if (memcmp(cifs_pdu->Signature.SecuritySignature, "BSRSPYL ", 8) == 0)
188                 cFYI(1, "dummy signature received for smb command 0x%x",
189                         cifs_pdu->Command);
190
191         /* save off the origiginal signature so we can modify the smb and check
192                 its signature against what the server sent */
193         memcpy(server_response_sig, cifs_pdu->Signature.SecuritySignature, 8);
194
195         cifs_pdu->Signature.Sequence.SequenceNumber =
196                                         cpu_to_le32(expected_sequence_number);
197         cifs_pdu->Signature.Sequence.Reserved = 0;
198
199         mutex_lock(&server->srv_mutex);
200         rc = cifs_calc_signature(rqst, server, what_we_think_sig_should_be);
201         mutex_unlock(&server->srv_mutex);
202
203         if (rc)
204                 return rc;
205
206 /*      cifs_dump_mem("what we think it should be: ",
207                       what_we_think_sig_should_be, 16); */
208
209         if (memcmp(server_response_sig, what_we_think_sig_should_be, 8))
210                 return -EACCES;
211         else
212                 return 0;
213
214 }
215
216 /* first calculate 24 bytes ntlm response and then 16 byte session key */
217 int setup_ntlm_response(struct cifs_ses *ses, const struct nls_table *nls_cp)
218 {
219         int rc = 0;
220         unsigned int temp_len = CIFS_SESS_KEY_SIZE + CIFS_AUTH_RESP_SIZE;
221         char temp_key[CIFS_SESS_KEY_SIZE];
222
223         if (!ses)
224                 return -EINVAL;
225
226         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
233         rc = SMBNTencrypt(ses->password, ses->server->cryptkey,
234                         ses->auth_key.response + CIFS_SESS_KEY_SIZE, nls_cp);
235         if (rc) {
236                 cFYI(1, "%s Can't generate NTLM response, error: %d",
237                         __func__, rc);
238                 return rc;
239         }
240
241         rc = E_md4hash(ses->password, temp_key, nls_cp);
242         if (rc) {
243                 cFYI(1, "%s Can't generate NT hash, error: %d", __func__, rc);
244                 return rc;
245         }
246
247         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;
253 }
254
255 #ifdef CONFIG_CIFS_WEAK_PW_HASH
256 int calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
257                         char *lnm_session_key)
258 {
259         int i;
260         int rc;
261         char password_with_pad[CIFS_ENCPWD_SIZE];
262
263         memset(password_with_pad, 0, CIFS_ENCPWD_SIZE);
264         if (password)
265                 strncpy(password_with_pad, password, CIFS_ENCPWD_SIZE);
266
267         if (!encrypt && global_secflags & CIFSSEC_MAY_PLNTXT) {
268                 memset(lnm_session_key, 0, CIFS_SESS_KEY_SIZE);
269                 memcpy(lnm_session_key, password_with_pad,
270                         CIFS_ENCPWD_SIZE);
271                 return 0;
272         }
273
274         /* 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
285         for (i = 0; i < CIFS_ENCPWD_SIZE; i++)
286                 password_with_pad[i] = toupper(password_with_pad[i]);
287
288         rc = SMBencrypt(password_with_pad, cryptkey, lnm_session_key);
289
290         return rc;
291 }
292 #endif /* CIFS_WEAK_PW_HASH */
293
294 /* 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.
298  */
299 static int
300 build_avpair_blob(struct cifs_ses *ses, const struct nls_table *nls_cp)
301 {
302         unsigned int dlen;
303         unsigned int size = 2 * sizeof(struct ntlmssp2_name);
304         char *defdmname = "WORKGROUP";
305         unsigned char *blobptr;
306         struct ntlmssp2_name *attrptr;
307
308         if (!ses->domainName) {
309                 ses->domainName = kstrdup(defdmname, GFP_KERNEL);
310                 if (!ses->domainName)
311                         return -ENOMEM;
312         }
313
314         dlen = strlen(ses->domainName);
315
316         /*
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
321          */
322         ses->auth_key.len = size + 2 * dlen;
323         ses->auth_key.response = kzalloc(ses->auth_key.len, GFP_KERNEL);
324         if (!ses->auth_key.response) {
325                 ses->auth_key.len = 0;
326                 cERROR(1, "Challenge target info allocation failure");
327                 return -ENOMEM;
328         }
329
330         blobptr = ses->auth_key.response;
331         attrptr = (struct ntlmssp2_name *) blobptr;
332
333         /*
334          * As defined in MS-NTLM 3.3.2, just this av pair field
335          * is sufficient as part of the temp
336          */
337         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);
340         cifs_strtoUTF16((__le16 *)blobptr, ses->domainName, dlen, nls_cp);
341
342         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  */
355 static int
356 find_domain_name(struct cifs_ses *ses, const struct nls_table *nls_cp)
357 {
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
365         if (!ses->auth_key.len || !ses->auth_key.response)
366                 return 0;
367
368         blobptr = ses->auth_key.response;
369         blobend = blobptr + ses->auth_key.len;
370
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;
389                                 cifs_from_utf16(ses->domainName,
390                                         (__le16 *)blobptr, attrsize, attrsize,
391                                         nls_cp, false);
392                                 break;
393                         }
394                 }
395                 blobptr += attrsize; /* advance attr  value */
396         }
397
398         return 0;
399 }
400
401 static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
402                             const struct nls_table *nls_cp)
403 {
404         int rc = 0;
405         int len;
406         char nt_hash[CIFS_NTHASH_SIZE];
407         wchar_t *user;
408         wchar_t *domain;
409         wchar_t *server;
410
411         if (!ses->server->secmech.sdeschmacmd5) {
412                 cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash");
413                 return -1;
414         }
415
416         /* calculate md4 hash of password */
417         E_md4hash(ses->password, nt_hash, nls_cp);
418
419         rc = crypto_shash_setkey(ses->server->secmech.hmacmd5, nt_hash,
420                                 CIFS_NTHASH_SIZE);
421         if (rc) {
422                 cERROR(1, "%s: Could not set NT Hash as a key", __func__);
423                 return rc;
424         }
425
426         rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
427         if (rc) {
428                 cERROR(1, "calc_ntlmv2_hash: could not init hmacmd5");
429                 return rc;
430         }
431
432         /* convert ses->user_name to unicode and uppercase */
433         len = ses->user_name ? strlen(ses->user_name) : 0;
434         user = kmalloc(2 + (len * 2), GFP_KERNEL);
435         if (user == NULL) {
436                 cERROR(1, "calc_ntlmv2_hash: user mem alloc failure");
437                 rc = -ENOMEM;
438                 return rc;
439         }
440
441         if (len) {
442                 len = cifs_strtoUTF16((__le16 *)user, ses->user_name, len, nls_cp);
443                 UniStrupr(user);
444         } else {
445                 memset(user, '\0', 2);
446         }
447
448         rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
449                                 (char *)user, 2 * len);
450         kfree(user);
451         if (rc) {
452                 cERROR(1, "%s: Could not update with user", __func__);
453                 return rc;
454         }
455
456         /* convert ses->domainName to unicode and uppercase */
457         if (ses->domainName) {
458                 len = strlen(ses->domainName);
459
460                 domain = kmalloc(2 + (len * 2), GFP_KERNEL);
461                 if (domain == NULL) {
462                         cERROR(1, "calc_ntlmv2_hash: domain mem alloc failure");
463                         rc = -ENOMEM;
464                         return rc;
465                 }
466                 len = cifs_strtoUTF16((__le16 *)domain, ses->domainName, len,
467                                       nls_cp);
468                 rc =
469                 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
470                                         (char *)domain, 2 * len);
471                 kfree(domain);
472                 if (rc) {
473                         cERROR(1, "%s: Could not update with domain",
474                                                                 __func__);
475                         return rc;
476                 }
477         } 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;
484                         return rc;
485                 }
486                 len = cifs_strtoUTF16((__le16 *)server, ses->serverName, len,
487                                         nls_cp);
488                 rc =
489                 crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
490                                         (char *)server, 2 * len);
491                 kfree(server);
492                 if (rc) {
493                         cERROR(1, "%s: Could not update with server",
494                                                                 __func__);
495                         return rc;
496                 }
497         }
498
499         rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
500                                         ntlmv2_hash);
501         if (rc)
502                 cERROR(1, "%s: Could not generate md5 hash", __func__);
503
504         return rc;
505 }
506
507 static int
508 CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
509 {
510         int rc;
511         unsigned int offset = CIFS_SESS_KEY_SIZE + 8;
512
513         if (!ses->server->secmech.sdeschmacmd5) {
514                 cERROR(1, "calc_ntlmv2_hash: can't generate ntlmv2 hash");
515                 return -1;
516         }
517
518         rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
519                                 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
520         if (rc) {
521                 cERROR(1, "%s: Could not set NTLMV2 Hash as a key", __func__);
522                 return rc;
523         }
524
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
531         if (ses->server->secType == RawNTLMSSP)
532                 memcpy(ses->auth_key.response + offset,
533                         ses->ntlmssp->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
534         else
535                 memcpy(ses->auth_key.response + offset,
536                         ses->server->cryptkey, CIFS_SERVER_CHALLENGE_SIZE);
537         rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
538                 ses->auth_key.response + offset, ses->auth_key.len - offset);
539         if (rc) {
540                 cERROR(1, "%s: Could not update with response", __func__);
541                 return rc;
542         }
543
544         rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
545                 ses->auth_key.response + CIFS_SESS_KEY_SIZE);
546         if (rc)
547                 cERROR(1, "%s: Could not generate md5 hash", __func__);
548
549         return rc;
550 }
551
552
553 int
554 setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
555 {
556         int rc;
557         int baselen;
558         unsigned int tilen;
559         struct ntlmv2_resp *buf;
560         char ntlmv2_hash[16];
561         unsigned char *tiblob = NULL; /* target info blob */
562
563         if (ses->server->secType == RawNTLMSSP) {
564                 if (!ses->domainName) {
565                         rc = find_domain_name(ses, nls_cp);
566                         if (rc) {
567                                 cERROR(1, "error %d finding domain name", rc);
568                                 goto setup_ntlmv2_rsp_ret;
569                         }
570                 }
571         } else {
572                 rc = build_avpair_blob(ses, nls_cp);
573                 if (rc) {
574                         cERROR(1, "error %d building av pair blob", rc);
575                         goto setup_ntlmv2_rsp_ret;
576                 }
577         }
578
579         baselen = CIFS_SESS_KEY_SIZE + sizeof(struct ntlmv2_resp);
580         tilen = ses->auth_key.len;
581         tiblob = ses->auth_key.response;
582
583         ses->auth_key.response = kmalloc(baselen + tilen, GFP_KERNEL);
584         if (!ses->auth_key.response) {
585                 rc = ENOMEM;
586                 ses->auth_key.len = 0;
587                 cERROR(1, "%s: Can't allocate auth blob", __func__);
588                 goto setup_ntlmv2_rsp_ret;
589         }
590         ses->auth_key.len += baselen;
591
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
600         memcpy(ses->auth_key.response + baselen, tiblob, tilen);
601
602         /* calculate ntlmv2_hash */
603         rc = calc_ntlmv2_hash(ses, ntlmv2_hash, nls_cp);
604         if (rc) {
605                 cERROR(1, "could not get v2 hash rc %d", rc);
606                 goto setup_ntlmv2_rsp_ret;
607         }
608
609         /* calculate first part of the client response (CR1) */
610         rc = CalcNTLMv2_response(ses, ntlmv2_hash);
611         if (rc) {
612                 cERROR(1, "Could not calculate CR1  rc: %d", rc);
613                 goto setup_ntlmv2_rsp_ret;
614         }
615
616         /* now calculate the session key for NTLMv2 */
617         rc = crypto_shash_setkey(ses->server->secmech.hmacmd5,
618                 ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
619         if (rc) {
620                 cERROR(1, "%s: Could not set NTLMV2 Hash as a key", __func__);
621                 goto setup_ntlmv2_rsp_ret;
622         }
623
624         rc = crypto_shash_init(&ses->server->secmech.sdeschmacmd5->shash);
625         if (rc) {
626                 cERROR(1, "%s: Could not init hmacmd5", __func__);
627                 goto setup_ntlmv2_rsp_ret;
628         }
629
630         rc = crypto_shash_update(&ses->server->secmech.sdeschmacmd5->shash,
631                 ses->auth_key.response + CIFS_SESS_KEY_SIZE,
632                 CIFS_HMAC_MD5_HASH_SIZE);
633         if (rc) {
634                 cERROR(1, "%s: Could not update with response", __func__);
635                 goto setup_ntlmv2_rsp_ret;
636         }
637
638         rc = crypto_shash_final(&ses->server->secmech.sdeschmacmd5->shash,
639                 ses->auth_key.response);
640         if (rc)
641                 cERROR(1, "%s: Could not generate md5 hash", __func__);
642
643 setup_ntlmv2_rsp_ret:
644         kfree(tiblob);
645
646         return rc;
647 }
648
649 int
650 calc_seckey(struct cifs_ses *ses)
651 {
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);
661         if (IS_ERR(tfm_arc4)) {
662                 rc = PTR_ERR(tfm_arc4);
663                 cERROR(1, "could not allocate crypto API arc4");
664                 return rc;
665         }
666
667         desc.tfm = tfm_arc4;
668
669         rc = crypto_blkcipher_setkey(tfm_arc4, ses->auth_key.response,
670                                         CIFS_SESS_KEY_SIZE);
671         if (rc) {
672                 cERROR(1, "%s: Could not set response as a key", __func__);
673                 return rc;
674         }
675
676         sg_init_one(&sgin, sec_key, CIFS_SESS_KEY_SIZE);
677         sg_init_one(&sgout, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
678
679         rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
680         if (rc) {
681                 cERROR(1, "could not encrypt session key rc: %d", rc);
682                 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
693         return rc;
694 }
695
696 void
697 cifs_crypto_shash_release(struct TCP_Server_Info *server)
698 {
699         if (server->secmech.hmacsha256)
700                 crypto_free_shash(server->secmech.hmacsha256);
701
702         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
708         kfree(server->secmech.sdeschmacsha256);
709
710         kfree(server->secmech.sdeschmacmd5);
711
712         kfree(server->secmech.sdescmd5);
713 }
714
715 int
716 cifs_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);
722         if (IS_ERR(server->secmech.hmacmd5)) {
723                 cERROR(1, "could not allocate crypto hmacmd5");
724                 return PTR_ERR(server->secmech.hmacmd5);
725         }
726
727         server->secmech.md5 = crypto_alloc_shash("md5", 0, 0);
728         if (IS_ERR(server->secmech.md5)) {
729                 cERROR(1, "could not allocate crypto md5");
730                 rc = PTR_ERR(server->secmech.md5);
731                 goto crypto_allocate_md5_fail;
732         }
733
734         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
741         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) {
745                 cERROR(1, "cifs_crypto_shash_allocate: can't alloc hmacmd5");
746                 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
752         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) {
756                 cERROR(1, "cifs_crypto_shash_allocate: can't alloc md5");
757                 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
763         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
774         return 0;
775
776 crypto_allocate_hmacsha256_sdesc_fail:
777         kfree(server->secmech.sdescmd5);
778
779 crypto_allocate_md5_sdesc_fail:
780         kfree(server->secmech.sdeschmacmd5);
781
782 crypto_allocate_hmacmd5_sdesc_fail:
783         crypto_free_shash(server->secmech.hmacsha256);
784
785 crypto_allocate_hmacsha256_fail:
786         crypto_free_shash(server->secmech.md5);
787
788 crypto_allocate_md5_fail:
789         crypto_free_shash(server->secmech.hmacmd5);
790
791         return rc;
792 }