17
17
#include <crypto/sha2.h>
18
18
#include <crypto/xts.h>
19
19
#include <crypto/scatterwalk.h>
20
- #include <linux/rtnetlink.h>
21
20
#include <linux/sort.h>
22
21
#include <linux/module.h>
23
22
#include "otx_cptvf.h"
@@ -66,6 +65,8 @@ static struct cpt_device_table ae_devices = {
66
65
.count = ATOMIC_INIT (0 )
67
66
};
68
67
68
+ static struct otx_cpt_sdesc * alloc_sdesc (struct crypto_shash * alg );
69
+
69
70
static inline int get_se_device (struct pci_dev * * pdev , int * cpu_num )
70
71
{
71
72
int count , ret = 0 ;
@@ -509,44 +510,61 @@ static int cpt_aead_init(struct crypto_aead *tfm, u8 cipher_type, u8 mac_type)
509
510
ctx -> cipher_type = cipher_type ;
510
511
ctx -> mac_type = mac_type ;
511
512
513
+ switch (ctx -> mac_type ) {
514
+ case OTX_CPT_SHA1 :
515
+ ctx -> hashalg = crypto_alloc_shash ("sha1" , 0 , 0 );
516
+ break ;
517
+
518
+ case OTX_CPT_SHA256 :
519
+ ctx -> hashalg = crypto_alloc_shash ("sha256" , 0 , 0 );
520
+ break ;
521
+
522
+ case OTX_CPT_SHA384 :
523
+ ctx -> hashalg = crypto_alloc_shash ("sha384" , 0 , 0 );
524
+ break ;
525
+
526
+ case OTX_CPT_SHA512 :
527
+ ctx -> hashalg = crypto_alloc_shash ("sha512" , 0 , 0 );
528
+ break ;
529
+ }
530
+
531
+ if (IS_ERR (ctx -> hashalg ))
532
+ return PTR_ERR (ctx -> hashalg );
533
+
534
+ crypto_aead_set_reqsize_dma (tfm , sizeof (struct otx_cpt_req_ctx ));
535
+
536
+ if (!ctx -> hashalg )
537
+ return 0 ;
538
+
512
539
/*
513
540
* When selected cipher is NULL we use HMAC opcode instead of
514
541
* FLEXICRYPTO opcode therefore we don't need to use HASH algorithms
515
542
* for calculating ipad and opad
516
543
*/
517
544
if (ctx -> cipher_type != OTX_CPT_CIPHER_NULL ) {
518
- switch (ctx -> mac_type ) {
519
- case OTX_CPT_SHA1 :
520
- ctx -> hashalg = crypto_alloc_shash ("sha1" , 0 ,
521
- CRYPTO_ALG_ASYNC );
522
- if (IS_ERR (ctx -> hashalg ))
523
- return PTR_ERR (ctx -> hashalg );
524
- break ;
525
-
526
- case OTX_CPT_SHA256 :
527
- ctx -> hashalg = crypto_alloc_shash ("sha256" , 0 ,
528
- CRYPTO_ALG_ASYNC );
529
- if (IS_ERR (ctx -> hashalg ))
530
- return PTR_ERR (ctx -> hashalg );
531
- break ;
545
+ int ss = crypto_shash_statesize (ctx -> hashalg );
532
546
533
- case OTX_CPT_SHA384 :
534
- ctx -> hashalg = crypto_alloc_shash ("sha384" , 0 ,
535
- CRYPTO_ALG_ASYNC );
536
- if (IS_ERR (ctx -> hashalg ))
537
- return PTR_ERR (ctx -> hashalg );
538
- break ;
547
+ ctx -> ipad = kzalloc (ss , GFP_KERNEL );
548
+ if (!ctx -> ipad ) {
549
+ crypto_free_shash (ctx -> hashalg );
550
+ return - ENOMEM ;
551
+ }
539
552
540
- case OTX_CPT_SHA512 :
541
- ctx -> hashalg = crypto_alloc_shash ("sha512" , 0 ,
542
- CRYPTO_ALG_ASYNC );
543
- if (IS_ERR (ctx -> hashalg ))
544
- return PTR_ERR (ctx -> hashalg );
545
- break ;
553
+ ctx -> opad = kzalloc (ss , GFP_KERNEL );
554
+ if (!ctx -> opad ) {
555
+ kfree (ctx -> ipad );
556
+ crypto_free_shash (ctx -> hashalg );
557
+ return - ENOMEM ;
546
558
}
547
559
}
548
560
549
- crypto_aead_set_reqsize_dma (tfm , sizeof (struct otx_cpt_req_ctx ));
561
+ ctx -> sdesc = alloc_sdesc (ctx -> hashalg );
562
+ if (!ctx -> sdesc ) {
563
+ kfree (ctx -> opad );
564
+ kfree (ctx -> ipad );
565
+ crypto_free_shash (ctx -> hashalg );
566
+ return - ENOMEM ;
567
+ }
550
568
551
569
return 0 ;
552
570
}
@@ -602,8 +620,7 @@ static void otx_cpt_aead_exit(struct crypto_aead *tfm)
602
620
603
621
kfree (ctx -> ipad );
604
622
kfree (ctx -> opad );
605
- if (ctx -> hashalg )
606
- crypto_free_shash (ctx -> hashalg );
623
+ crypto_free_shash (ctx -> hashalg );
607
624
kfree (ctx -> sdesc );
608
625
}
609
626
@@ -699,30 +716,27 @@ static inline void swap_data64(void *buf, u32 len)
699
716
* dst = cpu_to_be64p (src );
700
717
}
701
718
702
- static int copy_pad (u8 mac_type , u8 * out_pad , u8 * in_pad )
719
+ static int swap_pad (u8 mac_type , u8 * pad )
703
720
{
704
721
struct sha512_state * sha512 ;
705
722
struct sha256_state * sha256 ;
706
723
struct sha1_state * sha1 ;
707
724
708
725
switch (mac_type ) {
709
726
case OTX_CPT_SHA1 :
710
- sha1 = (struct sha1_state * ) in_pad ;
727
+ sha1 = (struct sha1_state * )pad ;
711
728
swap_data32 (sha1 -> state , SHA1_DIGEST_SIZE );
712
- memcpy (out_pad , & sha1 -> state , SHA1_DIGEST_SIZE );
713
729
break ;
714
730
715
731
case OTX_CPT_SHA256 :
716
- sha256 = (struct sha256_state * ) in_pad ;
732
+ sha256 = (struct sha256_state * )pad ;
717
733
swap_data32 (sha256 -> state , SHA256_DIGEST_SIZE );
718
- memcpy (out_pad , & sha256 -> state , SHA256_DIGEST_SIZE );
719
734
break ;
720
735
721
736
case OTX_CPT_SHA384 :
722
737
case OTX_CPT_SHA512 :
723
- sha512 = (struct sha512_state * ) in_pad ;
738
+ sha512 = (struct sha512_state * )pad ;
724
739
swap_data64 (sha512 -> state , SHA512_DIGEST_SIZE );
725
- memcpy (out_pad , & sha512 -> state , SHA512_DIGEST_SIZE );
726
740
break ;
727
741
728
742
default :
@@ -732,55 +746,53 @@ static int copy_pad(u8 mac_type, u8 *out_pad, u8 *in_pad)
732
746
return 0 ;
733
747
}
734
748
735
- static int aead_hmac_init (struct crypto_aead * cipher )
749
+ static int aead_hmac_init (struct crypto_aead * cipher ,
750
+ struct crypto_authenc_keys * keys )
736
751
{
737
752
struct otx_cpt_aead_ctx * ctx = crypto_aead_ctx_dma (cipher );
738
- int state_size = crypto_shash_statesize (ctx -> hashalg );
739
753
int ds = crypto_shash_digestsize (ctx -> hashalg );
740
754
int bs = crypto_shash_blocksize (ctx -> hashalg );
741
- int authkeylen = ctx -> auth_key_len ;
755
+ int authkeylen = keys -> authkeylen ;
742
756
u8 * ipad = NULL , * opad = NULL ;
743
- int ret = 0 , icount = 0 ;
757
+ int icount = 0 ;
758
+ int ret ;
744
759
745
- ctx -> sdesc = alloc_sdesc (ctx -> hashalg );
746
- if (!ctx -> sdesc )
747
- return - ENOMEM ;
760
+ if (authkeylen > bs ) {
761
+ ret = crypto_shash_digest (& ctx -> sdesc -> shash , keys -> authkey ,
762
+ authkeylen , ctx -> key );
763
+ if (ret )
764
+ return ret ;
765
+ authkeylen = ds ;
766
+ } else
767
+ memcpy (ctx -> key , keys -> authkey , authkeylen );
748
768
749
- ctx -> ipad = kzalloc (bs , GFP_KERNEL );
750
- if (!ctx -> ipad ) {
751
- ret = - ENOMEM ;
752
- goto calc_fail ;
753
- }
769
+ ctx -> enc_key_len = keys -> enckeylen ;
770
+ ctx -> auth_key_len = authkeylen ;
754
771
755
- ctx -> opad = kzalloc (bs , GFP_KERNEL );
756
- if (!ctx -> opad ) {
757
- ret = - ENOMEM ;
758
- goto calc_fail ;
759
- }
772
+ if (ctx -> cipher_type == OTX_CPT_CIPHER_NULL )
773
+ return keys -> enckeylen ? - EINVAL : 0 ;
760
774
761
- ipad = kzalloc (state_size , GFP_KERNEL );
762
- if (!ipad ) {
763
- ret = - ENOMEM ;
764
- goto calc_fail ;
775
+ switch (keys -> enckeylen ) {
776
+ case AES_KEYSIZE_128 :
777
+ ctx -> key_type = OTX_CPT_AES_128_BIT ;
778
+ break ;
779
+ case AES_KEYSIZE_192 :
780
+ ctx -> key_type = OTX_CPT_AES_192_BIT ;
781
+ break ;
782
+ case AES_KEYSIZE_256 :
783
+ ctx -> key_type = OTX_CPT_AES_256_BIT ;
784
+ break ;
785
+ default :
786
+ /* Invalid key length */
787
+ return - EINVAL ;
765
788
}
766
789
767
- opad = kzalloc (state_size , GFP_KERNEL );
768
- if (!opad ) {
769
- ret = - ENOMEM ;
770
- goto calc_fail ;
771
- }
790
+ memcpy (ctx -> key + authkeylen , keys -> enckey , keys -> enckeylen );
772
791
773
- if (authkeylen > bs ) {
774
- ret = crypto_shash_digest (& ctx -> sdesc -> shash , ctx -> key ,
775
- authkeylen , ipad );
776
- if (ret )
777
- goto calc_fail ;
778
-
779
- authkeylen = ds ;
780
- } else {
781
- memcpy (ipad , ctx -> key , authkeylen );
782
- }
792
+ ipad = ctx -> ipad ;
793
+ opad = ctx -> opad ;
783
794
795
+ memcpy (ipad , ctx -> key , authkeylen );
784
796
memset (ipad + authkeylen , 0 , bs - authkeylen );
785
797
memcpy (opad , ipad , bs );
786
798
@@ -798,91 +810,33 @@ static int aead_hmac_init(struct crypto_aead *cipher)
798
810
crypto_shash_init (& ctx -> sdesc -> shash );
799
811
crypto_shash_update (& ctx -> sdesc -> shash , ipad , bs );
800
812
crypto_shash_export (& ctx -> sdesc -> shash , ipad );
801
- ret = copy_pad (ctx -> mac_type , ctx -> ipad , ipad );
813
+ ret = swap_pad (ctx -> mac_type , ipad );
802
814
if (ret )
803
815
goto calc_fail ;
804
816
805
817
/* OPAD Calculation */
806
818
crypto_shash_init (& ctx -> sdesc -> shash );
807
819
crypto_shash_update (& ctx -> sdesc -> shash , opad , bs );
808
820
crypto_shash_export (& ctx -> sdesc -> shash , opad );
809
- ret = copy_pad (ctx -> mac_type , ctx -> opad , opad );
810
- if (ret )
811
- goto calc_fail ;
812
-
813
- kfree (ipad );
814
- kfree (opad );
815
-
816
- return 0 ;
821
+ ret = swap_pad (ctx -> mac_type , opad );
817
822
818
823
calc_fail :
819
- kfree (ctx -> ipad );
820
- ctx -> ipad = NULL ;
821
- kfree (ctx -> opad );
822
- ctx -> opad = NULL ;
823
- kfree (ipad );
824
- kfree (opad );
825
- kfree (ctx -> sdesc );
826
- ctx -> sdesc = NULL ;
827
-
828
824
return ret ;
829
825
}
830
826
831
827
static int otx_cpt_aead_cbc_aes_sha_setkey (struct crypto_aead * cipher ,
832
828
const unsigned char * key ,
833
829
unsigned int keylen )
834
830
{
835
- struct otx_cpt_aead_ctx * ctx = crypto_aead_ctx_dma (cipher );
836
- struct crypto_authenc_key_param * param ;
837
- int enckeylen = 0 , authkeylen = 0 ;
838
- struct rtattr * rta = (void * )key ;
839
- int status = - EINVAL ;
840
-
841
- if (!RTA_OK (rta , keylen ))
842
- goto badkey ;
843
-
844
- if (rta -> rta_type != CRYPTO_AUTHENC_KEYA_PARAM )
845
- goto badkey ;
846
-
847
- if (RTA_PAYLOAD (rta ) < sizeof (* param ))
848
- goto badkey ;
849
-
850
- param = RTA_DATA (rta );
851
- enckeylen = be32_to_cpu (param -> enckeylen );
852
- key += RTA_ALIGN (rta -> rta_len );
853
- keylen -= RTA_ALIGN (rta -> rta_len );
854
- if (keylen < enckeylen )
855
- goto badkey ;
831
+ struct crypto_authenc_keys authenc_keys ;
832
+ int status ;
856
833
857
- if (keylen > OTX_CPT_MAX_KEY_SIZE )
858
- goto badkey ;
859
-
860
- authkeylen = keylen - enckeylen ;
861
- memcpy (ctx -> key , key , keylen );
862
-
863
- switch (enckeylen ) {
864
- case AES_KEYSIZE_128 :
865
- ctx -> key_type = OTX_CPT_AES_128_BIT ;
866
- break ;
867
- case AES_KEYSIZE_192 :
868
- ctx -> key_type = OTX_CPT_AES_192_BIT ;
869
- break ;
870
- case AES_KEYSIZE_256 :
871
- ctx -> key_type = OTX_CPT_AES_256_BIT ;
872
- break ;
873
- default :
874
- /* Invalid key length */
875
- goto badkey ;
876
- }
877
-
878
- ctx -> enc_key_len = enckeylen ;
879
- ctx -> auth_key_len = authkeylen ;
880
-
881
- status = aead_hmac_init (cipher );
834
+ status = crypto_authenc_extractkeys (& authenc_keys , key , keylen );
882
835
if (status )
883
836
goto badkey ;
884
837
885
- return 0 ;
838
+ status = aead_hmac_init (cipher , & authenc_keys );
839
+
886
840
badkey :
887
841
return status ;
888
842
}
@@ -891,36 +845,7 @@ static int otx_cpt_aead_ecb_null_sha_setkey(struct crypto_aead *cipher,
891
845
const unsigned char * key ,
892
846
unsigned int keylen )
893
847
{
894
- struct otx_cpt_aead_ctx * ctx = crypto_aead_ctx_dma (cipher );
895
- struct crypto_authenc_key_param * param ;
896
- struct rtattr * rta = (void * )key ;
897
- int enckeylen = 0 ;
898
-
899
- if (!RTA_OK (rta , keylen ))
900
- goto badkey ;
901
-
902
- if (rta -> rta_type != CRYPTO_AUTHENC_KEYA_PARAM )
903
- goto badkey ;
904
-
905
- if (RTA_PAYLOAD (rta ) < sizeof (* param ))
906
- goto badkey ;
907
-
908
- param = RTA_DATA (rta );
909
- enckeylen = be32_to_cpu (param -> enckeylen );
910
- key += RTA_ALIGN (rta -> rta_len );
911
- keylen -= RTA_ALIGN (rta -> rta_len );
912
- if (enckeylen != 0 )
913
- goto badkey ;
914
-
915
- if (keylen > OTX_CPT_MAX_KEY_SIZE )
916
- goto badkey ;
917
-
918
- memcpy (ctx -> key , key , keylen );
919
- ctx -> enc_key_len = enckeylen ;
920
- ctx -> auth_key_len = keylen ;
921
- return 0 ;
922
- badkey :
923
- return - EINVAL ;
848
+ return otx_cpt_aead_cbc_aes_sha_setkey (cipher , key , keylen );
924
849
}
925
850
926
851
static int otx_cpt_aead_gcm_aes_setkey (struct crypto_aead * cipher ,
0 commit comments