11
11
#include <crypto/xts.h>
12
12
#include <crypto/gcm.h>
13
13
#include <crypto/scatterwalk.h>
14
- #include <linux/rtnetlink.h>
15
14
#include <linux/sort.h>
16
15
#include <linux/module.h>
17
16
#include "otx2_cptvf.h"
@@ -55,6 +54,8 @@ static struct cpt_device_table se_devices = {
55
54
.count = ATOMIC_INIT (0 )
56
55
};
57
56
57
+ static struct otx2_cpt_sdesc * alloc_sdesc (struct crypto_shash * alg );
58
+
58
59
static inline int get_se_device (struct pci_dev * * pdev , int * cpu_num )
59
60
{
60
61
int count ;
@@ -598,40 +599,56 @@ static int cpt_aead_init(struct crypto_aead *atfm, u8 cipher_type, u8 mac_type)
598
599
ctx -> cipher_type = cipher_type ;
599
600
ctx -> mac_type = mac_type ;
600
601
602
+ switch (ctx -> mac_type ) {
603
+ case OTX2_CPT_SHA1 :
604
+ ctx -> hashalg = crypto_alloc_shash ("sha1" , 0 , 0 );
605
+ break ;
606
+
607
+ case OTX2_CPT_SHA256 :
608
+ ctx -> hashalg = crypto_alloc_shash ("sha256" , 0 , 0 );
609
+ break ;
610
+
611
+ case OTX2_CPT_SHA384 :
612
+ ctx -> hashalg = crypto_alloc_shash ("sha384" , 0 , 0 );
613
+ break ;
614
+
615
+ case OTX2_CPT_SHA512 :
616
+ ctx -> hashalg = crypto_alloc_shash ("sha512" , 0 , 0 );
617
+ break ;
618
+ }
619
+
620
+ if (IS_ERR (ctx -> hashalg ))
621
+ return PTR_ERR (ctx -> hashalg );
622
+
623
+ if (ctx -> hashalg ) {
624
+ ctx -> sdesc = alloc_sdesc (ctx -> hashalg );
625
+ if (!ctx -> sdesc ) {
626
+ crypto_free_shash (ctx -> hashalg );
627
+ return - ENOMEM ;
628
+ }
629
+ }
630
+
601
631
/*
602
632
* When selected cipher is NULL we use HMAC opcode instead of
603
633
* FLEXICRYPTO opcode therefore we don't need to use HASH algorithms
604
634
* for calculating ipad and opad
605
635
*/
606
- if (ctx -> cipher_type != OTX2_CPT_CIPHER_NULL ) {
607
- switch (ctx -> mac_type ) {
608
- case OTX2_CPT_SHA1 :
609
- ctx -> hashalg = crypto_alloc_shash ("sha1" , 0 ,
610
- CRYPTO_ALG_ASYNC );
611
- if (IS_ERR (ctx -> hashalg ))
612
- return PTR_ERR (ctx -> hashalg );
613
- break ;
614
-
615
- case OTX2_CPT_SHA256 :
616
- ctx -> hashalg = crypto_alloc_shash ("sha256" , 0 ,
617
- CRYPTO_ALG_ASYNC );
618
- if (IS_ERR (ctx -> hashalg ))
619
- return PTR_ERR (ctx -> hashalg );
620
- break ;
636
+ if (ctx -> cipher_type != OTX2_CPT_CIPHER_NULL && ctx -> hashalg ) {
637
+ int ss = crypto_shash_statesize (ctx -> hashalg );
621
638
622
- case OTX2_CPT_SHA384 :
623
- ctx -> hashalg = crypto_alloc_shash ( "sha384" , 0 ,
624
- CRYPTO_ALG_ASYNC );
625
- if ( IS_ERR ( ctx -> hashalg ))
626
- return PTR_ERR ( ctx -> hashalg ) ;
627
- break ;
639
+ ctx -> ipad = kzalloc ( ss , GFP_KERNEL );
640
+ if (! ctx -> ipad ) {
641
+ kfree ( ctx -> sdesc );
642
+ crypto_free_shash ( ctx -> hashalg );
643
+ return - ENOMEM ;
644
+ }
628
645
629
- case OTX2_CPT_SHA512 :
630
- ctx -> hashalg = crypto_alloc_shash ( "sha512" , 0 ,
631
- CRYPTO_ALG_ASYNC );
632
- if ( IS_ERR ( ctx -> hashalg ))
633
- return PTR_ERR (ctx -> hashalg );
634
- break ;
646
+ ctx -> opad = kzalloc ( ss , GFP_KERNEL );
647
+ if (! ctx -> opad ) {
648
+ kfree ( ctx -> ipad );
649
+ kfree ( ctx -> sdesc );
650
+ crypto_free_shash (ctx -> hashalg );
651
+ return - ENOMEM ;
635
652
}
636
653
}
637
654
switch (ctx -> cipher_type ) {
@@ -713,8 +730,7 @@ static void otx2_cpt_aead_exit(struct crypto_aead *tfm)
713
730
714
731
kfree (ctx -> ipad );
715
732
kfree (ctx -> opad );
716
- if (ctx -> hashalg )
717
- crypto_free_shash (ctx -> hashalg );
733
+ crypto_free_shash (ctx -> hashalg );
718
734
kfree (ctx -> sdesc );
719
735
720
736
if (ctx -> fbk_cipher ) {
@@ -788,30 +804,27 @@ static inline void swap_data64(void *buf, u32 len)
788
804
cpu_to_be64s (src );
789
805
}
790
806
791
- static int copy_pad (u8 mac_type , u8 * out_pad , u8 * in_pad )
807
+ static int swap_pad (u8 mac_type , u8 * pad )
792
808
{
793
809
struct sha512_state * sha512 ;
794
810
struct sha256_state * sha256 ;
795
811
struct sha1_state * sha1 ;
796
812
797
813
switch (mac_type ) {
798
814
case OTX2_CPT_SHA1 :
799
- sha1 = (struct sha1_state * ) in_pad ;
815
+ sha1 = (struct sha1_state * )pad ;
800
816
swap_data32 (sha1 -> state , SHA1_DIGEST_SIZE );
801
- memcpy (out_pad , & sha1 -> state , SHA1_DIGEST_SIZE );
802
817
break ;
803
818
804
819
case OTX2_CPT_SHA256 :
805
- sha256 = (struct sha256_state * ) in_pad ;
820
+ sha256 = (struct sha256_state * )pad ;
806
821
swap_data32 (sha256 -> state , SHA256_DIGEST_SIZE );
807
- memcpy (out_pad , & sha256 -> state , SHA256_DIGEST_SIZE );
808
822
break ;
809
823
810
824
case OTX2_CPT_SHA384 :
811
825
case OTX2_CPT_SHA512 :
812
- sha512 = (struct sha512_state * ) in_pad ;
826
+ sha512 = (struct sha512_state * )pad ;
813
827
swap_data64 (sha512 -> state , SHA512_DIGEST_SIZE );
814
- memcpy (out_pad , & sha512 -> state , SHA512_DIGEST_SIZE );
815
828
break ;
816
829
817
830
default :
@@ -821,55 +834,54 @@ static int copy_pad(u8 mac_type, u8 *out_pad, u8 *in_pad)
821
834
return 0 ;
822
835
}
823
836
824
- static int aead_hmac_init (struct crypto_aead * cipher )
837
+ static int aead_hmac_init (struct crypto_aead * cipher ,
838
+ struct crypto_authenc_keys * keys )
825
839
{
826
840
struct otx2_cpt_aead_ctx * ctx = crypto_aead_ctx_dma (cipher );
827
- int state_size = crypto_shash_statesize (ctx -> hashalg );
828
841
int ds = crypto_shash_digestsize (ctx -> hashalg );
829
842
int bs = crypto_shash_blocksize (ctx -> hashalg );
830
- int authkeylen = ctx -> auth_key_len ;
843
+ int authkeylen = keys -> authkeylen ;
831
844
u8 * ipad = NULL , * opad = NULL ;
832
- int ret = 0 , icount = 0 ;
845
+ int icount = 0 ;
846
+ int ret ;
833
847
834
- ctx -> sdesc = alloc_sdesc (ctx -> hashalg );
835
- if (!ctx -> sdesc )
836
- return - ENOMEM ;
848
+ if (authkeylen > bs ) {
849
+ ret = crypto_shash_digest (& ctx -> sdesc -> shash , keys -> authkey ,
850
+ authkeylen , ctx -> key );
851
+ if (ret )
852
+ goto calc_fail ;
837
853
838
- ctx -> ipad = kzalloc (bs , GFP_KERNEL );
839
- if (!ctx -> ipad ) {
840
- ret = - ENOMEM ;
841
- goto calc_fail ;
842
- }
854
+ authkeylen = ds ;
855
+ } else
856
+ memcpy (ctx -> key , keys -> authkey , authkeylen );
843
857
844
- ctx -> opad = kzalloc (bs , GFP_KERNEL );
845
- if (!ctx -> opad ) {
846
- ret = - ENOMEM ;
847
- goto calc_fail ;
848
- }
858
+ ctx -> enc_key_len = keys -> enckeylen ;
859
+ ctx -> auth_key_len = authkeylen ;
849
860
850
- ipad = kzalloc (state_size , GFP_KERNEL );
851
- if (!ipad ) {
852
- ret = - ENOMEM ;
853
- goto calc_fail ;
854
- }
861
+ if (ctx -> cipher_type == OTX2_CPT_CIPHER_NULL )
862
+ return keys -> enckeylen ? - EINVAL : 0 ;
855
863
856
- opad = kzalloc (state_size , GFP_KERNEL );
857
- if (!opad ) {
858
- ret = - ENOMEM ;
859
- goto calc_fail ;
864
+ switch (keys -> enckeylen ) {
865
+ case AES_KEYSIZE_128 :
866
+ ctx -> key_type = OTX2_CPT_AES_128_BIT ;
867
+ break ;
868
+ case AES_KEYSIZE_192 :
869
+ ctx -> key_type = OTX2_CPT_AES_192_BIT ;
870
+ break ;
871
+ case AES_KEYSIZE_256 :
872
+ ctx -> key_type = OTX2_CPT_AES_256_BIT ;
873
+ break ;
874
+ default :
875
+ /* Invalid key length */
876
+ return - EINVAL ;
860
877
}
861
878
862
- if (authkeylen > bs ) {
863
- ret = crypto_shash_digest (& ctx -> sdesc -> shash , ctx -> key ,
864
- authkeylen , ipad );
865
- if (ret )
866
- goto calc_fail ;
879
+ memcpy (ctx -> key + authkeylen , keys -> enckey , keys -> enckeylen );
867
880
868
- authkeylen = ds ;
869
- } else {
870
- memcpy (ipad , ctx -> key , authkeylen );
871
- }
881
+ ipad = ctx -> ipad ;
882
+ opad = ctx -> opad ;
872
883
884
+ memcpy (ipad , ctx -> key , authkeylen );
873
885
memset (ipad + authkeylen , 0 , bs - authkeylen );
874
886
memcpy (opad , ipad , bs );
875
887
@@ -887,121 +899,35 @@ static int aead_hmac_init(struct crypto_aead *cipher)
887
899
crypto_shash_init (& ctx -> sdesc -> shash );
888
900
crypto_shash_update (& ctx -> sdesc -> shash , ipad , bs );
889
901
crypto_shash_export (& ctx -> sdesc -> shash , ipad );
890
- ret = copy_pad (ctx -> mac_type , ctx -> ipad , ipad );
902
+ ret = swap_pad (ctx -> mac_type , ipad );
891
903
if (ret )
892
904
goto calc_fail ;
893
905
894
906
/* OPAD Calculation */
895
907
crypto_shash_init (& ctx -> sdesc -> shash );
896
908
crypto_shash_update (& ctx -> sdesc -> shash , opad , bs );
897
909
crypto_shash_export (& ctx -> sdesc -> shash , opad );
898
- ret = copy_pad (ctx -> mac_type , ctx -> opad , opad );
899
- if (ret )
900
- goto calc_fail ;
901
-
902
- kfree (ipad );
903
- kfree (opad );
904
-
905
- return 0 ;
910
+ ret = swap_pad (ctx -> mac_type , opad );
906
911
907
912
calc_fail :
908
- kfree (ctx -> ipad );
909
- ctx -> ipad = NULL ;
910
- kfree (ctx -> opad );
911
- ctx -> opad = NULL ;
912
- kfree (ipad );
913
- kfree (opad );
914
- kfree (ctx -> sdesc );
915
- ctx -> sdesc = NULL ;
916
-
917
913
return ret ;
918
914
}
919
915
920
916
static int otx2_cpt_aead_cbc_aes_sha_setkey (struct crypto_aead * cipher ,
921
917
const unsigned char * key ,
922
918
unsigned int keylen )
923
919
{
924
- struct otx2_cpt_aead_ctx * ctx = crypto_aead_ctx_dma (cipher );
925
- struct crypto_authenc_key_param * param ;
926
- int enckeylen = 0 , authkeylen = 0 ;
927
- struct rtattr * rta = (void * )key ;
928
-
929
- if (!RTA_OK (rta , keylen ))
930
- return - EINVAL ;
920
+ struct crypto_authenc_keys authenc_keys ;
931
921
932
- if (rta -> rta_type != CRYPTO_AUTHENC_KEYA_PARAM )
933
- return - EINVAL ;
934
-
935
- if (RTA_PAYLOAD (rta ) < sizeof (* param ))
936
- return - EINVAL ;
937
-
938
- param = RTA_DATA (rta );
939
- enckeylen = be32_to_cpu (param -> enckeylen );
940
- key += RTA_ALIGN (rta -> rta_len );
941
- keylen -= RTA_ALIGN (rta -> rta_len );
942
- if (keylen < enckeylen )
943
- return - EINVAL ;
944
-
945
- if (keylen > OTX2_CPT_MAX_KEY_SIZE )
946
- return - EINVAL ;
947
-
948
- authkeylen = keylen - enckeylen ;
949
- memcpy (ctx -> key , key , keylen );
950
-
951
- switch (enckeylen ) {
952
- case AES_KEYSIZE_128 :
953
- ctx -> key_type = OTX2_CPT_AES_128_BIT ;
954
- break ;
955
- case AES_KEYSIZE_192 :
956
- ctx -> key_type = OTX2_CPT_AES_192_BIT ;
957
- break ;
958
- case AES_KEYSIZE_256 :
959
- ctx -> key_type = OTX2_CPT_AES_256_BIT ;
960
- break ;
961
- default :
962
- /* Invalid key length */
963
- return - EINVAL ;
964
- }
965
-
966
- ctx -> enc_key_len = enckeylen ;
967
- ctx -> auth_key_len = authkeylen ;
968
-
969
- return aead_hmac_init (cipher );
922
+ return crypto_authenc_extractkeys (& authenc_keys , key , keylen ) ?:
923
+ aead_hmac_init (cipher , & authenc_keys );
970
924
}
971
925
972
926
static int otx2_cpt_aead_ecb_null_sha_setkey (struct crypto_aead * cipher ,
973
927
const unsigned char * key ,
974
928
unsigned int keylen )
975
929
{
976
- struct otx2_cpt_aead_ctx * ctx = crypto_aead_ctx_dma (cipher );
977
- struct crypto_authenc_key_param * param ;
978
- struct rtattr * rta = (void * )key ;
979
- int enckeylen = 0 ;
980
-
981
- if (!RTA_OK (rta , keylen ))
982
- return - EINVAL ;
983
-
984
- if (rta -> rta_type != CRYPTO_AUTHENC_KEYA_PARAM )
985
- return - EINVAL ;
986
-
987
- if (RTA_PAYLOAD (rta ) < sizeof (* param ))
988
- return - EINVAL ;
989
-
990
- param = RTA_DATA (rta );
991
- enckeylen = be32_to_cpu (param -> enckeylen );
992
- key += RTA_ALIGN (rta -> rta_len );
993
- keylen -= RTA_ALIGN (rta -> rta_len );
994
- if (enckeylen != 0 )
995
- return - EINVAL ;
996
-
997
- if (keylen > OTX2_CPT_MAX_KEY_SIZE )
998
- return - EINVAL ;
999
-
1000
- memcpy (ctx -> key , key , keylen );
1001
- ctx -> enc_key_len = enckeylen ;
1002
- ctx -> auth_key_len = keylen ;
1003
-
1004
- return 0 ;
930
+ return otx2_cpt_aead_cbc_aes_sha_setkey (cipher , key , keylen );
1005
931
}
1006
932
1007
933
static int otx2_cpt_aead_gcm_aes_setkey (struct crypto_aead * cipher ,
0 commit comments