OSDN Git Service

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