1
1
/*
2
2
* fs/cifs/cifsencrypt.c
3
3
*
4
- * Copyright (C) International Business Machines Corp., 2005,2006
4
+ * Copyright (C) International Business Machines Corp., 2005,2013
5
5
* Author(s): Steve French ([email protected] )
6
6
*
7
7
* This library is free software; you can redistribute it and/or modify
31
31
#include <linux/random.h>
32
32
#include <linux/highmem.h>
33
33
34
+ static int
35
+ cifs_crypto_shash_md5_allocate (struct TCP_Server_Info * server )
36
+ {
37
+ int rc ;
38
+ unsigned int size ;
39
+
40
+ if (server -> secmech .sdescmd5 != NULL )
41
+ return 0 ; /* already allocated */
42
+
43
+ server -> secmech .md5 = crypto_alloc_shash ("md5" , 0 , 0 );
44
+ if (IS_ERR (server -> secmech .md5 )) {
45
+ cifs_dbg (VFS , "could not allocate crypto md5\n" );
46
+ return PTR_ERR (server -> secmech .md5 );
47
+ }
48
+
49
+ size = sizeof (struct shash_desc ) +
50
+ crypto_shash_descsize (server -> secmech .md5 );
51
+ server -> secmech .sdescmd5 = kmalloc (size , GFP_KERNEL );
52
+ if (!server -> secmech .sdescmd5 ) {
53
+ rc = - ENOMEM ;
54
+ crypto_free_shash (server -> secmech .md5 );
55
+ server -> secmech .md5 = NULL ;
56
+ return rc ;
57
+ }
58
+ server -> secmech .sdescmd5 -> shash .tfm = server -> secmech .md5 ;
59
+ server -> secmech .sdescmd5 -> shash .flags = 0x0 ;
60
+
61
+ return 0 ;
62
+ }
63
+
34
64
/*
35
65
* Calculate and return the CIFS signature based on the mac key and SMB PDU.
36
66
* The 16 byte signature must be allocated by the caller. Note we only use the
@@ -50,8 +80,11 @@ static int cifs_calc_signature(struct smb_rqst *rqst,
50
80
return - EINVAL ;
51
81
52
82
if (!server -> secmech .sdescmd5 ) {
53
- cifs_dbg (VFS , "%s: Can't generate signature\n" , __func__ );
54
- return -1 ;
83
+ rc = cifs_crypto_shash_md5_allocate (server );
84
+ if (rc ) {
85
+ cifs_dbg (VFS , "%s: Can't alloc md5 crypto\n" , __func__ );
86
+ return -1 ;
87
+ }
55
88
}
56
89
57
90
rc = crypto_shash_init (& server -> secmech .sdescmd5 -> shash );
@@ -556,6 +589,33 @@ CalcNTLMv2_response(const struct cifs_ses *ses, char *ntlmv2_hash)
556
589
return rc ;
557
590
}
558
591
592
+ static int crypto_hmacmd5_alloc (struct TCP_Server_Info * server )
593
+ {
594
+ unsigned int size ;
595
+
596
+ /* check if already allocated */
597
+ if (server -> secmech .sdeschmacmd5 )
598
+ return 0 ;
599
+
600
+ server -> secmech .hmacmd5 = crypto_alloc_shash ("hmac(md5)" , 0 , 0 );
601
+ if (IS_ERR (server -> secmech .hmacmd5 )) {
602
+ cifs_dbg (VFS , "could not allocate crypto hmacmd5\n" );
603
+ return PTR_ERR (server -> secmech .hmacmd5 );
604
+ }
605
+
606
+ size = sizeof (struct shash_desc ) +
607
+ crypto_shash_descsize (server -> secmech .hmacmd5 );
608
+ server -> secmech .sdeschmacmd5 = kmalloc (size , GFP_KERNEL );
609
+ if (!server -> secmech .sdeschmacmd5 ) {
610
+ crypto_free_shash (server -> secmech .hmacmd5 );
611
+ server -> secmech .hmacmd5 = NULL ;
612
+ return - ENOMEM ;
613
+ }
614
+ server -> secmech .sdeschmacmd5 -> shash .tfm = server -> secmech .hmacmd5 ;
615
+ server -> secmech .sdeschmacmd5 -> shash .flags = 0x0 ;
616
+
617
+ return 0 ;
618
+ }
559
619
560
620
int
561
621
setup_ntlmv2_rsp (struct cifs_ses * ses , const struct nls_table * nls_cp )
@@ -606,6 +666,12 @@ setup_ntlmv2_rsp(struct cifs_ses *ses, const struct nls_table *nls_cp)
606
666
607
667
memcpy (ses -> auth_key .response + baselen , tiblob , tilen );
608
668
669
+ rc = crypto_hmacmd5_alloc (ses -> server );
670
+ if (rc ) {
671
+ cifs_dbg (VFS , "could not crypto alloc hmacmd5 rc %d\n" , rc );
672
+ goto setup_ntlmv2_rsp_ret ;
673
+ }
674
+
609
675
/* calculate ntlmv2_hash */
610
676
rc = calc_ntlmv2_hash (ses , ntlmv2_hash , nls_cp );
611
677
if (rc ) {
@@ -705,123 +771,32 @@ calc_seckey(struct cifs_ses *ses)
705
771
void
706
772
cifs_crypto_shash_release (struct TCP_Server_Info * server )
707
773
{
708
- if (server -> secmech .cmacaes )
774
+ if (server -> secmech .cmacaes ) {
709
775
crypto_free_shash (server -> secmech .cmacaes );
776
+ server -> secmech .cmacaes = NULL ;
777
+ }
710
778
711
- if (server -> secmech .hmacsha256 )
779
+ if (server -> secmech .hmacsha256 ) {
712
780
crypto_free_shash (server -> secmech .hmacsha256 );
781
+ server -> secmech .hmacsha256 = NULL ;
782
+ }
713
783
714
- if (server -> secmech .md5 )
784
+ if (server -> secmech .md5 ) {
715
785
crypto_free_shash (server -> secmech .md5 );
786
+ server -> secmech .md5 = NULL ;
787
+ }
716
788
717
- if (server -> secmech .hmacmd5 )
789
+ if (server -> secmech .hmacmd5 ) {
718
790
crypto_free_shash (server -> secmech .hmacmd5 );
791
+ server -> secmech .hmacmd5 = NULL ;
792
+ }
719
793
720
794
kfree (server -> secmech .sdesccmacaes );
721
-
795
+ server -> secmech . sdesccmacaes = NULL ;
722
796
kfree (server -> secmech .sdeschmacsha256 );
723
-
797
+ server -> secmech . sdeschmacsha256 = NULL ;
724
798
kfree (server -> secmech .sdeschmacmd5 );
725
-
799
+ server -> secmech . sdeschmacmd5 = NULL ;
726
800
kfree (server -> secmech .sdescmd5 );
727
- }
728
-
729
- int
730
- cifs_crypto_shash_allocate (struct TCP_Server_Info * server )
731
- {
732
- int rc ;
733
- unsigned int size ;
734
-
735
- server -> secmech .hmacmd5 = crypto_alloc_shash ("hmac(md5)" , 0 , 0 );
736
- if (IS_ERR (server -> secmech .hmacmd5 )) {
737
- cifs_dbg (VFS , "could not allocate crypto hmacmd5\n" );
738
- return PTR_ERR (server -> secmech .hmacmd5 );
739
- }
740
-
741
- server -> secmech .md5 = crypto_alloc_shash ("md5" , 0 , 0 );
742
- if (IS_ERR (server -> secmech .md5 )) {
743
- cifs_dbg (VFS , "could not allocate crypto md5\n" );
744
- rc = PTR_ERR (server -> secmech .md5 );
745
- goto crypto_allocate_md5_fail ;
746
- }
747
-
748
- server -> secmech .hmacsha256 = crypto_alloc_shash ("hmac(sha256)" , 0 , 0 );
749
- if (IS_ERR (server -> secmech .hmacsha256 )) {
750
- cifs_dbg (VFS , "could not allocate crypto hmacsha256\n" );
751
- rc = PTR_ERR (server -> secmech .hmacsha256 );
752
- goto crypto_allocate_hmacsha256_fail ;
753
- }
754
-
755
- server -> secmech .cmacaes = crypto_alloc_shash ("cmac(aes)" , 0 , 0 );
756
- if (IS_ERR (server -> secmech .cmacaes )) {
757
- cifs_dbg (VFS , "could not allocate crypto cmac-aes" );
758
- rc = PTR_ERR (server -> secmech .cmacaes );
759
- goto crypto_allocate_cmacaes_fail ;
760
- }
761
-
762
- size = sizeof (struct shash_desc ) +
763
- crypto_shash_descsize (server -> secmech .hmacmd5 );
764
- server -> secmech .sdeschmacmd5 = kmalloc (size , GFP_KERNEL );
765
- if (!server -> secmech .sdeschmacmd5 ) {
766
- rc = - ENOMEM ;
767
- goto crypto_allocate_hmacmd5_sdesc_fail ;
768
- }
769
- server -> secmech .sdeschmacmd5 -> shash .tfm = server -> secmech .hmacmd5 ;
770
- server -> secmech .sdeschmacmd5 -> shash .flags = 0x0 ;
771
-
772
- size = sizeof (struct shash_desc ) +
773
- crypto_shash_descsize (server -> secmech .md5 );
774
- server -> secmech .sdescmd5 = kmalloc (size , GFP_KERNEL );
775
- if (!server -> secmech .sdescmd5 ) {
776
- rc = - ENOMEM ;
777
- goto crypto_allocate_md5_sdesc_fail ;
778
- }
779
- server -> secmech .sdescmd5 -> shash .tfm = server -> secmech .md5 ;
780
- server -> secmech .sdescmd5 -> shash .flags = 0x0 ;
781
-
782
- size = sizeof (struct shash_desc ) +
783
- crypto_shash_descsize (server -> secmech .hmacsha256 );
784
- server -> secmech .sdeschmacsha256 = kmalloc (size , GFP_KERNEL );
785
- if (!server -> secmech .sdeschmacsha256 ) {
786
- rc = - ENOMEM ;
787
- goto crypto_allocate_hmacsha256_sdesc_fail ;
788
- }
789
- server -> secmech .sdeschmacsha256 -> shash .tfm = server -> secmech .hmacsha256 ;
790
- server -> secmech .sdeschmacsha256 -> shash .flags = 0x0 ;
791
-
792
- size = sizeof (struct shash_desc ) +
793
- crypto_shash_descsize (server -> secmech .cmacaes );
794
- server -> secmech .sdesccmacaes = kmalloc (size , GFP_KERNEL );
795
- if (!server -> secmech .sdesccmacaes ) {
796
- cifs_dbg (VFS , "%s: Can't alloc cmacaes\n" , __func__ );
797
- rc = - ENOMEM ;
798
- goto crypto_allocate_cmacaes_sdesc_fail ;
799
- }
800
- server -> secmech .sdesccmacaes -> shash .tfm = server -> secmech .cmacaes ;
801
- server -> secmech .sdesccmacaes -> shash .flags = 0x0 ;
802
-
803
- return 0 ;
804
-
805
- crypto_allocate_cmacaes_sdesc_fail :
806
- kfree (server -> secmech .sdeschmacsha256 );
807
-
808
- crypto_allocate_hmacsha256_sdesc_fail :
809
- kfree (server -> secmech .sdescmd5 );
810
-
811
- crypto_allocate_md5_sdesc_fail :
812
- kfree (server -> secmech .sdeschmacmd5 );
813
-
814
- crypto_allocate_hmacmd5_sdesc_fail :
815
- crypto_free_shash (server -> secmech .cmacaes );
816
-
817
- crypto_allocate_cmacaes_fail :
818
- crypto_free_shash (server -> secmech .hmacsha256 );
819
-
820
- crypto_allocate_hmacsha256_fail :
821
- crypto_free_shash (server -> secmech .md5 );
822
-
823
- crypto_allocate_md5_fail :
824
- crypto_free_shash (server -> secmech .hmacmd5 );
825
-
826
- return rc ;
801
+ server -> secmech .sdescmd5 = NULL ;
827
802
}
0 commit comments