@@ -31,6 +31,11 @@ struct skcipher_sg_list {
31
31
struct scatterlist sg [0 ];
32
32
};
33
33
34
+ struct skcipher_tfm {
35
+ struct crypto_skcipher * skcipher ;
36
+ bool has_key ;
37
+ };
38
+
34
39
struct skcipher_ctx {
35
40
struct list_head tsgl ;
36
41
struct af_alg_sgl rsgl ;
@@ -750,17 +755,41 @@ static struct proto_ops algif_skcipher_ops = {
750
755
751
756
static void * skcipher_bind (const char * name , u32 type , u32 mask )
752
757
{
753
- return crypto_alloc_skcipher (name , type , mask );
758
+ struct skcipher_tfm * tfm ;
759
+ struct crypto_skcipher * skcipher ;
760
+
761
+ tfm = kzalloc (sizeof (* tfm ), GFP_KERNEL );
762
+ if (!tfm )
763
+ return ERR_PTR (- ENOMEM );
764
+
765
+ skcipher = crypto_alloc_skcipher (name , type , mask );
766
+ if (IS_ERR (skcipher )) {
767
+ kfree (tfm );
768
+ return ERR_CAST (skcipher );
769
+ }
770
+
771
+ tfm -> skcipher = skcipher ;
772
+
773
+ return tfm ;
754
774
}
755
775
756
776
static void skcipher_release (void * private )
757
777
{
758
- crypto_free_skcipher (private );
778
+ struct skcipher_tfm * tfm = private ;
779
+
780
+ crypto_free_skcipher (tfm -> skcipher );
781
+ kfree (tfm );
759
782
}
760
783
761
784
static int skcipher_setkey (void * private , const u8 * key , unsigned int keylen )
762
785
{
763
- return crypto_skcipher_setkey (private , key , keylen );
786
+ struct skcipher_tfm * tfm = private ;
787
+ int err ;
788
+
789
+ err = crypto_skcipher_setkey (tfm -> skcipher , key , keylen );
790
+ tfm -> has_key = !err ;
791
+
792
+ return err ;
764
793
}
765
794
766
795
static void skcipher_wait (struct sock * sk )
@@ -792,20 +821,25 @@ static int skcipher_accept_parent(void *private, struct sock *sk)
792
821
{
793
822
struct skcipher_ctx * ctx ;
794
823
struct alg_sock * ask = alg_sk (sk );
795
- unsigned int len = sizeof (* ctx ) + crypto_skcipher_reqsize (private );
824
+ struct skcipher_tfm * tfm = private ;
825
+ struct crypto_skcipher * skcipher = tfm -> skcipher ;
826
+ unsigned int len = sizeof (* ctx ) + crypto_skcipher_reqsize (skcipher );
827
+
828
+ if (!tfm -> has_key )
829
+ return - ENOKEY ;
796
830
797
831
ctx = sock_kmalloc (sk , len , GFP_KERNEL );
798
832
if (!ctx )
799
833
return - ENOMEM ;
800
834
801
- ctx -> iv = sock_kmalloc (sk , crypto_skcipher_ivsize (private ),
835
+ ctx -> iv = sock_kmalloc (sk , crypto_skcipher_ivsize (skcipher ),
802
836
GFP_KERNEL );
803
837
if (!ctx -> iv ) {
804
838
sock_kfree_s (sk , ctx , len );
805
839
return - ENOMEM ;
806
840
}
807
841
808
- memset (ctx -> iv , 0 , crypto_skcipher_ivsize (private ));
842
+ memset (ctx -> iv , 0 , crypto_skcipher_ivsize (skcipher ));
809
843
810
844
INIT_LIST_HEAD (& ctx -> tsgl );
811
845
ctx -> len = len ;
@@ -818,7 +852,7 @@ static int skcipher_accept_parent(void *private, struct sock *sk)
818
852
819
853
ask -> private = ctx ;
820
854
821
- skcipher_request_set_tfm (& ctx -> req , private );
855
+ skcipher_request_set_tfm (& ctx -> req , skcipher );
822
856
skcipher_request_set_callback (& ctx -> req , CRYPTO_TFM_REQ_MAY_BACKLOG ,
823
857
af_alg_complete , & ctx -> completion );
824
858
0 commit comments