aboutsummaryrefslogtreecommitdiffstats
path: root/fs/cifs/cifsencrypt.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/cifs/cifsencrypt.c')
-rw-r--r--fs/cifs/cifsencrypt.c416
1 files changed, 294 insertions, 122 deletions
diff --git a/fs/cifs/cifsencrypt.c b/fs/cifs/cifsencrypt.c
index 847628dfdc44..051d00011ca3 100644
--- a/fs/cifs/cifsencrypt.c
+++ b/fs/cifs/cifsencrypt.c
@@ -27,6 +27,7 @@
27#include "md5.h" 27#include "md5.h"
28#include "cifs_unicode.h" 28#include "cifs_unicode.h"
29#include "cifsproto.h" 29#include "cifsproto.h"
30#include "ntlmssp.h"
30#include <linux/ctype.h> 31#include <linux/ctype.h>
31#include <linux/random.h> 32#include <linux/random.h>
32 33
@@ -42,21 +43,44 @@ extern void SMBencrypt(unsigned char *passwd, const unsigned char *c8,
42 unsigned char *p24); 43 unsigned char *p24);
43 44
44static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu, 45static int cifs_calculate_signature(const struct smb_hdr *cifs_pdu,
45 const struct mac_key *key, char *signature) 46 struct TCP_Server_Info *server, char *signature)
46{ 47{
47 struct MD5Context context; 48 int rc = 0;
49 struct {
50 struct shash_desc shash;
51 char ctx[crypto_shash_descsize(server->ntlmssp.md5)];
52 } sdesc;
48 53
49 if ((cifs_pdu == NULL) || (signature == NULL) || (key == NULL)) 54 if (cifs_pdu == NULL || server == NULL || signature == NULL)
50 return -EINVAL; 55 return -EINVAL;
51 56
52 cifs_MD5_init(&context); 57 sdesc.shash.tfm = server->ntlmssp.md5;
53 cifs_MD5_update(&context, (char *)&key->data, key->len); 58 sdesc.shash.flags = 0x0;
54 cifs_MD5_update(&context, cifs_pdu->Protocol, cifs_pdu->smb_buf_length); 59
60 rc = crypto_shash_init(&sdesc.shash);
61 if (rc) {
62 cERROR(1, "could not initialize master crypto API hmacmd5\n");
63 return rc;
64 }
65
66 if (server->secType == RawNTLMSSP)
67 crypto_shash_update(&sdesc.shash,
68 server->session_key.data.ntlmv2.key,
69 CIFS_NTLMV2_SESSKEY_SIZE);
70 else
71 crypto_shash_update(&sdesc.shash,
72 (char *)&server->session_key.data,
73 server->session_key.len);
74
75 crypto_shash_update(&sdesc.shash,
76 cifs_pdu->Protocol, cifs_pdu->smb_buf_length);
77
78 rc = crypto_shash_final(&sdesc.shash, signature);
55 79
56 cifs_MD5_final(signature, &context);
57 return 0; 80 return 0;
58} 81}
59 82
83
60int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server, 84int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
61 __u32 *pexpected_response_sequence_number) 85 __u32 *pexpected_response_sequence_number)
62{ 86{
@@ -78,8 +102,7 @@ int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
78 server->sequence_number++; 102 server->sequence_number++;
79 spin_unlock(&GlobalMid_Lock); 103 spin_unlock(&GlobalMid_Lock);
80 104
81 rc = cifs_calculate_signature(cifs_pdu, &server->mac_signing_key, 105 rc = cifs_calculate_signature(cifs_pdu, server, smb_signature);
82 smb_signature);
83 if (rc) 106 if (rc)
84 memset(cifs_pdu->Signature.SecuritySignature, 0, 8); 107 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
85 else 108 else
@@ -89,16 +112,36 @@ int cifs_sign_smb(struct smb_hdr *cifs_pdu, struct TCP_Server_Info *server,
89} 112}
90 113
91static int cifs_calc_signature2(const struct kvec *iov, int n_vec, 114static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
92 const struct mac_key *key, char *signature) 115 struct TCP_Server_Info *server, char *signature)
93{ 116{
94 struct MD5Context context;
95 int i; 117 int i;
118 int rc = 0;
119 struct {
120 struct shash_desc shash;
121 char ctx[crypto_shash_descsize(server->ntlmssp.md5)];
122 } sdesc;
96 123
97 if ((iov == NULL) || (signature == NULL) || (key == NULL)) 124 if (iov == NULL || server == NULL || signature == NULL)
98 return -EINVAL; 125 return -EINVAL;
99 126
100 cifs_MD5_init(&context); 127 sdesc.shash.tfm = server->ntlmssp.md5;
101 cifs_MD5_update(&context, (char *)&key->data, key->len); 128 sdesc.shash.flags = 0x0;
129
130 rc = crypto_shash_init(&sdesc.shash);
131 if (rc) {
132 cERROR(1, "could not initialize master crypto API hmacmd5\n");
133 return rc;
134 }
135
136 if (server->secType == RawNTLMSSP)
137 crypto_shash_update(&sdesc.shash,
138 server->session_key.data.ntlmv2.key,
139 CIFS_NTLMV2_SESSKEY_SIZE);
140 else
141 crypto_shash_update(&sdesc.shash,
142 (char *)&server->session_key.data,
143 server->session_key.len);
144
102 for (i = 0; i < n_vec; i++) { 145 for (i = 0; i < n_vec; i++) {
103 if (iov[i].iov_len == 0) 146 if (iov[i].iov_len == 0)
104 continue; 147 continue;
@@ -111,18 +154,18 @@ static int cifs_calc_signature2(const struct kvec *iov, int n_vec,
111 if (i == 0) { 154 if (i == 0) {
112 if (iov[0].iov_len <= 8) /* cmd field at offset 9 */ 155 if (iov[0].iov_len <= 8) /* cmd field at offset 9 */
113 break; /* nothing to sign or corrupt header */ 156 break; /* nothing to sign or corrupt header */
114 cifs_MD5_update(&context, iov[0].iov_base+4, 157 crypto_shash_update(&sdesc.shash,
115 iov[0].iov_len-4); 158 iov[i].iov_base + 4, iov[i].iov_len - 4);
116 } else 159 } else
117 cifs_MD5_update(&context, iov[i].iov_base, iov[i].iov_len); 160 crypto_shash_update(&sdesc.shash,
161 iov[i].iov_base, iov[i].iov_len);
118 } 162 }
119 163
120 cifs_MD5_final(signature, &context); 164 rc = crypto_shash_final(&sdesc.shash, signature);
121 165
122 return 0; 166 return 0;
123} 167}
124 168
125
126int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server, 169int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
127 __u32 *pexpected_response_sequence_number) 170 __u32 *pexpected_response_sequence_number)
128{ 171{
@@ -145,8 +188,7 @@ int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
145 server->sequence_number++; 188 server->sequence_number++;
146 spin_unlock(&GlobalMid_Lock); 189 spin_unlock(&GlobalMid_Lock);
147 190
148 rc = cifs_calc_signature2(iov, n_vec, &server->mac_signing_key, 191 rc = cifs_calc_signature2(iov, n_vec, server, smb_signature);
149 smb_signature);
150 if (rc) 192 if (rc)
151 memset(cifs_pdu->Signature.SecuritySignature, 0, 8); 193 memset(cifs_pdu->Signature.SecuritySignature, 0, 8);
152 else 194 else
@@ -156,14 +198,14 @@ int cifs_sign_smb2(struct kvec *iov, int n_vec, struct TCP_Server_Info *server,
156} 198}
157 199
158int cifs_verify_signature(struct smb_hdr *cifs_pdu, 200int cifs_verify_signature(struct smb_hdr *cifs_pdu,
159 const struct mac_key *mac_key, 201 struct TCP_Server_Info *server,
160 __u32 expected_sequence_number) 202 __u32 expected_sequence_number)
161{ 203{
162 unsigned int rc; 204 int rc;
163 char server_response_sig[8]; 205 char server_response_sig[8];
164 char what_we_think_sig_should_be[20]; 206 char what_we_think_sig_should_be[20];
165 207
166 if ((cifs_pdu == NULL) || (mac_key == NULL)) 208 if (cifs_pdu == NULL || server == NULL)
167 return -EINVAL; 209 return -EINVAL;
168 210
169 if (cifs_pdu->Command == SMB_COM_NEGOTIATE) 211 if (cifs_pdu->Command == SMB_COM_NEGOTIATE)
@@ -192,7 +234,7 @@ int cifs_verify_signature(struct smb_hdr *cifs_pdu,
192 cpu_to_le32(expected_sequence_number); 234 cpu_to_le32(expected_sequence_number);
193 cifs_pdu->Signature.Sequence.Reserved = 0; 235 cifs_pdu->Signature.Sequence.Reserved = 0;
194 236
195 rc = cifs_calculate_signature(cifs_pdu, mac_key, 237 rc = cifs_calculate_signature(cifs_pdu, server,
196 what_we_think_sig_should_be); 238 what_we_think_sig_should_be);
197 239
198 if (rc) 240 if (rc)
@@ -209,7 +251,7 @@ int cifs_verify_signature(struct smb_hdr *cifs_pdu,
209} 251}
210 252
211/* We fill in key by putting in 40 byte array which was allocated by caller */ 253/* We fill in key by putting in 40 byte array which was allocated by caller */
212int cifs_calculate_mac_key(struct mac_key *key, const char *rn, 254int cifs_calculate_session_key(struct session_key *key, const char *rn,
213 const char *password) 255 const char *password)
214{ 256{
215 char temp_key[16]; 257 char temp_key[16];
@@ -223,63 +265,6 @@ int cifs_calculate_mac_key(struct mac_key *key, const char *rn,
223 return 0; 265 return 0;
224} 266}
225 267
226int CalcNTLMv2_partial_mac_key(struct cifsSesInfo *ses,
227 const struct nls_table *nls_info)
228{
229 char temp_hash[16];
230 struct HMACMD5Context ctx;
231 char *ucase_buf;
232 __le16 *unicode_buf;
233 unsigned int i, user_name_len, dom_name_len;
234
235 if (ses == NULL)
236 return -EINVAL;
237
238 E_md4hash(ses->password, temp_hash);
239
240 hmac_md5_init_limK_to_64(temp_hash, 16, &ctx);
241 user_name_len = strlen(ses->userName);
242 if (user_name_len > MAX_USERNAME_SIZE)
243 return -EINVAL;
244 if (ses->domainName == NULL)
245 return -EINVAL; /* BB should we use CIFS_LINUX_DOM */
246 dom_name_len = strlen(ses->domainName);
247 if (dom_name_len > MAX_USERNAME_SIZE)
248 return -EINVAL;
249
250 ucase_buf = kmalloc((MAX_USERNAME_SIZE+1), GFP_KERNEL);
251 if (ucase_buf == NULL)
252 return -ENOMEM;
253 unicode_buf = kmalloc((MAX_USERNAME_SIZE+1)*4, GFP_KERNEL);
254 if (unicode_buf == NULL) {
255 kfree(ucase_buf);
256 return -ENOMEM;
257 }
258
259 for (i = 0; i < user_name_len; i++)
260 ucase_buf[i] = nls_info->charset2upper[(int)ses->userName[i]];
261 ucase_buf[i] = 0;
262 user_name_len = cifs_strtoUCS(unicode_buf, ucase_buf,
263 MAX_USERNAME_SIZE*2, nls_info);
264 unicode_buf[user_name_len] = 0;
265 user_name_len++;
266
267 for (i = 0; i < dom_name_len; i++)
268 ucase_buf[i] = nls_info->charset2upper[(int)ses->domainName[i]];
269 ucase_buf[i] = 0;
270 dom_name_len = cifs_strtoUCS(unicode_buf+user_name_len, ucase_buf,
271 MAX_USERNAME_SIZE*2, nls_info);
272
273 unicode_buf[user_name_len + dom_name_len] = 0;
274 hmac_md5_update((const unsigned char *) unicode_buf,
275 (user_name_len+dom_name_len)*2, &ctx);
276
277 hmac_md5_final(ses->server->ntlmv2_hash, &ctx);
278 kfree(ucase_buf);
279 kfree(unicode_buf);
280 return 0;
281}
282
283#ifdef CONFIG_CIFS_WEAK_PW_HASH 268#ifdef CONFIG_CIFS_WEAK_PW_HASH
284void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt, 269void calc_lanman_hash(const char *password, const char *cryptkey, bool encrypt,
285 char *lnm_session_key) 270 char *lnm_session_key)
@@ -324,21 +309,29 @@ static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
324{ 309{
325 int rc = 0; 310 int rc = 0;
326 int len; 311 int len;
327 char nt_hash[16]; 312 char nt_hash[CIFS_NTHASH_SIZE];
328 struct HMACMD5Context *pctxt;
329 wchar_t *user; 313 wchar_t *user;
330 wchar_t *domain; 314 wchar_t *domain;
331 315 wchar_t *server;
332 pctxt = kmalloc(sizeof(struct HMACMD5Context), GFP_KERNEL); 316 struct {
333 317 struct shash_desc shash;
334 if (pctxt == NULL) 318 char ctx[crypto_shash_descsize(ses->server->ntlmssp.hmacmd5)];
335 return -ENOMEM; 319 } sdesc;
336 320
337 /* calculate md4 hash of password */ 321 /* calculate md4 hash of password */
338 E_md4hash(ses->password, nt_hash); 322 E_md4hash(ses->password, nt_hash);
339 323
340 /* convert Domainname to unicode and uppercase */ 324 sdesc.shash.tfm = ses->server->ntlmssp.hmacmd5;
341 hmac_md5_init_limK_to_64(nt_hash, 16, pctxt); 325 sdesc.shash.flags = 0x0;
326
327 crypto_shash_setkey(ses->server->ntlmssp.hmacmd5, nt_hash,
328 CIFS_NTHASH_SIZE);
329
330 rc = crypto_shash_init(&sdesc.shash);
331 if (rc) {
332 cERROR(1, "could not initialize master crypto API hmacmd5\n");
333 return rc;
334 }
342 335
343 /* convert ses->userName to unicode and uppercase */ 336 /* convert ses->userName to unicode and uppercase */
344 len = strlen(ses->userName); 337 len = strlen(ses->userName);
@@ -347,7 +340,8 @@ static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
347 goto calc_exit_2; 340 goto calc_exit_2;
348 len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp); 341 len = cifs_strtoUCS((__le16 *)user, ses->userName, len, nls_cp);
349 UniStrupr(user); 342 UniStrupr(user);
350 hmac_md5_update((char *)user, 2*len, pctxt); 343
344 crypto_shash_update(&sdesc.shash, (char *)user, 2 * len);
351 345
352 /* convert ses->domainName to unicode and uppercase */ 346 /* convert ses->domainName to unicode and uppercase */
353 if (ses->domainName) { 347 if (ses->domainName) {
@@ -363,65 +357,243 @@ static int calc_ntlmv2_hash(struct cifsSesInfo *ses,
363 Maybe converting the domain name earlier makes sense */ 357 Maybe converting the domain name earlier makes sense */
364 /* UniStrupr(domain); */ 358 /* UniStrupr(domain); */
365 359
366 hmac_md5_update((char *)domain, 2*len, pctxt); 360 crypto_shash_update(&sdesc.shash, (char *)domain, 2 * len);
367 361
368 kfree(domain); 362 kfree(domain);
363 } else if (ses->serverName) {
364 len = strlen(ses->serverName);
365
366 server = kmalloc(2 + (len * 2), GFP_KERNEL);
367 if (server == NULL)
368 goto calc_exit_1;
369 len = cifs_strtoUCS((__le16 *)server, ses->serverName, len,
370 nls_cp);
371 /* the following line was removed since it didn't work well
372 with lower cased domain name that passed as an option.
373 Maybe converting the domain name earlier makes sense */
374 /* UniStrupr(domain); */
375
376 crypto_shash_update(&sdesc.shash, (char *)server, 2 * len);
377
378 kfree(server);
369 } 379 }
370calc_exit_1: 380calc_exit_1:
371 kfree(user); 381 kfree(user);
372calc_exit_2: 382calc_exit_2:
373 /* BB FIXME what about bytes 24 through 40 of the signing key? 383 /* BB FIXME what about bytes 24 through 40 of the signing key?
374 compare with the NTLM example */ 384 compare with the NTLM example */
375 hmac_md5_final(ses->server->ntlmv2_hash, pctxt); 385 rc = crypto_shash_final(&sdesc.shash, ses->server->ntlmv2_hash);
376 386
377 kfree(pctxt);
378 return rc; 387 return rc;
379} 388}
380 389
381void setup_ntlmv2_rsp(struct cifsSesInfo *ses, char *resp_buf, 390static int
382 const struct nls_table *nls_cp) 391find_domain_name(struct cifsSesInfo *ses)
392{
393 int rc = 0;
394 unsigned int attrsize;
395 unsigned int type;
396 unsigned char *blobptr;
397 struct ntlmssp2_name *attrptr;
398
399 if (ses->server->tiblob) {
400 blobptr = ses->server->tiblob;
401 attrptr = (struct ntlmssp2_name *) blobptr;
402
403 while ((type = attrptr->type) != 0) {
404 blobptr += 2; /* advance attr type */
405 attrsize = attrptr->length;
406 blobptr += 2; /* advance attr size */
407 if (type == NTLMSSP_AV_NB_DOMAIN_NAME) {
408 if (!ses->domainName) {
409 ses->domainName =
410 kmalloc(attrptr->length + 1,
411 GFP_KERNEL);
412 if (!ses->domainName)
413 return -ENOMEM;
414 cifs_from_ucs2(ses->domainName,
415 (__le16 *)blobptr,
416 attrptr->length,
417 attrptr->length,
418 load_nls_default(), false);
419 }
420 }
421 blobptr += attrsize; /* advance attr value */
422 attrptr = (struct ntlmssp2_name *) blobptr;
423 }
424 } else {
425 ses->server->tilen = 2 * sizeof(struct ntlmssp2_name);
426 ses->server->tiblob = kmalloc(ses->server->tilen, GFP_KERNEL);
427 if (!ses->server->tiblob) {
428 ses->server->tilen = 0;
429 cERROR(1, "Challenge target info allocation failure");
430 return -ENOMEM;
431 }
432 memset(ses->server->tiblob, 0x0, ses->server->tilen);
433 attrptr = (struct ntlmssp2_name *) ses->server->tiblob;
434 attrptr->type = cpu_to_le16(NTLMSSP_DOMAIN_TYPE);
435 }
436
437 return rc;
438}
439
440static int
441CalcNTLMv2_response(const struct TCP_Server_Info *server,
442 char *v2_session_response)
383{ 443{
384 int rc; 444 int rc;
445 struct {
446 struct shash_desc shash;
447 char ctx[crypto_shash_descsize(server->ntlmssp.hmacmd5)];
448 } sdesc;
449
450 sdesc.shash.tfm = server->ntlmssp.hmacmd5;
451 sdesc.shash.flags = 0x0;
452
453 crypto_shash_setkey(server->ntlmssp.hmacmd5, server->ntlmv2_hash,
454 CIFS_HMAC_MD5_HASH_SIZE);
455
456 rc = crypto_shash_init(&sdesc.shash);
457 if (rc) {
458 cERROR(1, "could not initialize master crypto API hmacmd5\n");
459 return rc;
460 }
461
462 memcpy(v2_session_response + CIFS_SERVER_CHALLENGE_SIZE,
463 server->cryptKey, CIFS_SERVER_CHALLENGE_SIZE);
464 crypto_shash_update(&sdesc.shash,
465 v2_session_response + CIFS_SERVER_CHALLENGE_SIZE,
466 sizeof(struct ntlmv2_resp) - CIFS_SERVER_CHALLENGE_SIZE);
467
468 if (server->tilen)
469 crypto_shash_update(&sdesc.shash,
470 server->tiblob, server->tilen);
471
472 rc = crypto_shash_final(&sdesc.shash, v2_session_response);
473
474 return rc;
475}
476
477int
478setup_ntlmv2_rsp(struct cifsSesInfo *ses, char *resp_buf,
479 const struct nls_table *nls_cp)
480{
481 int rc = 0;
385 struct ntlmv2_resp *buf = (struct ntlmv2_resp *)resp_buf; 482 struct ntlmv2_resp *buf = (struct ntlmv2_resp *)resp_buf;
386 struct HMACMD5Context context; 483 struct {
484 struct shash_desc shash;
485 char ctx[crypto_shash_descsize(ses->server->ntlmssp.hmacmd5)];
486 } sdesc;
387 487
388 buf->blob_signature = cpu_to_le32(0x00000101); 488 buf->blob_signature = cpu_to_le32(0x00000101);
389 buf->reserved = 0; 489 buf->reserved = 0;
390 buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); 490 buf->time = cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
391 get_random_bytes(&buf->client_chal, sizeof(buf->client_chal)); 491 get_random_bytes(&buf->client_chal, sizeof(buf->client_chal));
392 buf->reserved2 = 0; 492 buf->reserved2 = 0;
393 buf->names[0].type = cpu_to_le16(NTLMSSP_DOMAIN_TYPE); 493
394 buf->names[0].length = 0; 494 if (!ses->domainName) {
395 buf->names[1].type = 0; 495 rc = find_domain_name(ses);
396 buf->names[1].length = 0; 496 if (rc) {
497 cERROR(1, "could not get domain/server name rc %d", rc);
498 return rc;
499 }
500 }
397 501
398 /* calculate buf->ntlmv2_hash */ 502 /* calculate buf->ntlmv2_hash */
399 rc = calc_ntlmv2_hash(ses, nls_cp); 503 rc = calc_ntlmv2_hash(ses, nls_cp);
400 if (rc) 504 if (rc) {
505 cERROR(1, "could not get v2 hash rc %d", rc);
506 return rc;
507 }
508 rc = CalcNTLMv2_response(ses->server, resp_buf);
509 if (rc) {
401 cERROR(1, "could not get v2 hash rc %d", rc); 510 cERROR(1, "could not get v2 hash rc %d", rc);
402 CalcNTLMv2_response(ses, resp_buf); 511 return rc;
512 }
513
514 crypto_shash_setkey(ses->server->ntlmssp.hmacmd5,
515 ses->server->ntlmv2_hash, CIFS_HMAC_MD5_HASH_SIZE);
516
517 sdesc.shash.tfm = ses->server->ntlmssp.hmacmd5;
518 sdesc.shash.flags = 0x0;
519
520 rc = crypto_shash_init(&sdesc.shash);
521 if (rc) {
522 cERROR(1, "could not initialize master crypto API hmacmd5\n");
523 return rc;
524 }
525
526 crypto_shash_update(&sdesc.shash, resp_buf, CIFS_HMAC_MD5_HASH_SIZE);
403 527
404 /* now calculate the MAC key for NTLMv2 */ 528 rc = crypto_shash_final(&sdesc.shash,
405 hmac_md5_init_limK_to_64(ses->server->ntlmv2_hash, 16, &context); 529 ses->server->session_key.data.ntlmv2.key);
406 hmac_md5_update(resp_buf, 16, &context);
407 hmac_md5_final(ses->server->mac_signing_key.data.ntlmv2.key, &context);
408 530
409 memcpy(&ses->server->mac_signing_key.data.ntlmv2.resp, resp_buf, 531 memcpy(&ses->server->session_key.data.ntlmv2.resp, resp_buf,
410 sizeof(struct ntlmv2_resp)); 532 sizeof(struct ntlmv2_resp));
411 ses->server->mac_signing_key.len = 16 + sizeof(struct ntlmv2_resp); 533 ses->server->session_key.len = 16 + sizeof(struct ntlmv2_resp);
534
535 return rc;
412} 536}
413 537
414void CalcNTLMv2_response(const struct cifsSesInfo *ses, 538int
415 char *v2_session_response) 539calc_seckey(struct TCP_Server_Info *server)
416{ 540{
417 struct HMACMD5Context context; 541 int rc;
418 /* rest of v2 struct already generated */ 542 unsigned char sec_key[CIFS_NTLMV2_SESSKEY_SIZE];
419 memcpy(v2_session_response + 8, ses->server->cryptKey, 8); 543 struct crypto_blkcipher *tfm_arc4;
420 hmac_md5_init_limK_to_64(ses->server->ntlmv2_hash, 16, &context); 544 struct scatterlist sgin, sgout;
545 struct blkcipher_desc desc;
546
547 get_random_bytes(sec_key, CIFS_NTLMV2_SESSKEY_SIZE);
548
549 tfm_arc4 = crypto_alloc_blkcipher("ecb(arc4)",
550 0, CRYPTO_ALG_ASYNC);
551 if (!tfm_arc4 || IS_ERR(tfm_arc4)) {
552 cERROR(1, "could not allocate " "master crypto API arc4\n");
553 return 1;
554 }
555
556 crypto_blkcipher_setkey(tfm_arc4,
557 server->session_key.data.ntlmv2.key, CIFS_CPHTXT_SIZE);
558 sg_init_one(&sgin, sec_key, CIFS_CPHTXT_SIZE);
559 sg_init_one(&sgout, server->ntlmssp.ciphertext, CIFS_CPHTXT_SIZE);
560 rc = crypto_blkcipher_encrypt(&desc, &sgout, &sgin, CIFS_CPHTXT_SIZE);
561
562 if (!rc)
563 memcpy(server->session_key.data.ntlmv2.key,
564 sec_key, CIFS_NTLMV2_SESSKEY_SIZE);
421 565
422 hmac_md5_update(v2_session_response+8, 566 crypto_free_blkcipher(tfm_arc4);
423 sizeof(struct ntlmv2_resp) - 8, &context);
424 567
425 hmac_md5_final(v2_session_response, &context); 568 return 0;
426/* cifs_dump_mem("v2_sess_rsp: ", v2_session_response, 32); */ 569}
570
571void
572cifs_crypto_shash_release(struct TCP_Server_Info *server)
573{
574 if (server->ntlmssp.md5)
575 crypto_free_shash(server->ntlmssp.md5);
576
577 if (server->ntlmssp.hmacmd5)
578 crypto_free_shash(server->ntlmssp.hmacmd5);
579}
580
581int
582cifs_crypto_shash_allocate(struct TCP_Server_Info *server)
583{
584 server->ntlmssp.hmacmd5 = crypto_alloc_shash("hmac(md5)", 0, 0);
585 if (!server->ntlmssp.hmacmd5 ||
586 IS_ERR(server->ntlmssp.hmacmd5)) {
587 cERROR(1, "could not allocate master crypto API hmacmd5\n");
588 return 1;
589 }
590
591 server->ntlmssp.md5 = crypto_alloc_shash("md5", 0, 0);
592 if (!server->ntlmssp.md5 || IS_ERR(server->ntlmssp.md5)) {
593 crypto_free_shash(server->ntlmssp.hmacmd5);
594 cERROR(1, "could not allocate master crypto API md5\n");
595 return 1;
596 }
597
598 return 0;
427} 599}