Skip to content

Commit 311eea7

Browse files
committed
crypto: octeontx - Fix authenc setkey
Use the generic crypto_authenc_extractkeys helper instead of custom parsing code that is slightly broken. Also fix a number of memory leaks by moving memory allocation from setkey to init_tfm (setkey can be called multiple times over the life of a tfm). Finally accept all hash key lengths by running the digest over extra-long keys. Signed-off-by: Herbert Xu <[email protected]>
1 parent 87a3fcf commit 311eea7

File tree

1 file changed

+93
-168
lines changed

1 file changed

+93
-168
lines changed

drivers/crypto/marvell/octeontx/otx_cptvf_algs.c

Lines changed: 93 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <crypto/sha2.h>
1818
#include <crypto/xts.h>
1919
#include <crypto/scatterwalk.h>
20-
#include <linux/rtnetlink.h>
2120
#include <linux/sort.h>
2221
#include <linux/module.h>
2322
#include "otx_cptvf.h"
@@ -66,6 +65,8 @@ static struct cpt_device_table ae_devices = {
6665
.count = ATOMIC_INIT(0)
6766
};
6867

68+
static struct otx_cpt_sdesc *alloc_sdesc(struct crypto_shash *alg);
69+
6970
static inline int get_se_device(struct pci_dev **pdev, int *cpu_num)
7071
{
7172
int count, ret = 0;
@@ -509,44 +510,61 @@ static int cpt_aead_init(struct crypto_aead *tfm, u8 cipher_type, u8 mac_type)
509510
ctx->cipher_type = cipher_type;
510511
ctx->mac_type = mac_type;
511512

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+
512539
/*
513540
* When selected cipher is NULL we use HMAC opcode instead of
514541
* FLEXICRYPTO opcode therefore we don't need to use HASH algorithms
515542
* for calculating ipad and opad
516543
*/
517544
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);
532546

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+
}
539552

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;
546558
}
547559
}
548560

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+
}
550568

551569
return 0;
552570
}
@@ -602,8 +620,7 @@ static void otx_cpt_aead_exit(struct crypto_aead *tfm)
602620

603621
kfree(ctx->ipad);
604622
kfree(ctx->opad);
605-
if (ctx->hashalg)
606-
crypto_free_shash(ctx->hashalg);
623+
crypto_free_shash(ctx->hashalg);
607624
kfree(ctx->sdesc);
608625
}
609626

@@ -699,30 +716,27 @@ static inline void swap_data64(void *buf, u32 len)
699716
*dst = cpu_to_be64p(src);
700717
}
701718

702-
static int copy_pad(u8 mac_type, u8 *out_pad, u8 *in_pad)
719+
static int swap_pad(u8 mac_type, u8 *pad)
703720
{
704721
struct sha512_state *sha512;
705722
struct sha256_state *sha256;
706723
struct sha1_state *sha1;
707724

708725
switch (mac_type) {
709726
case OTX_CPT_SHA1:
710-
sha1 = (struct sha1_state *) in_pad;
727+
sha1 = (struct sha1_state *)pad;
711728
swap_data32(sha1->state, SHA1_DIGEST_SIZE);
712-
memcpy(out_pad, &sha1->state, SHA1_DIGEST_SIZE);
713729
break;
714730

715731
case OTX_CPT_SHA256:
716-
sha256 = (struct sha256_state *) in_pad;
732+
sha256 = (struct sha256_state *)pad;
717733
swap_data32(sha256->state, SHA256_DIGEST_SIZE);
718-
memcpy(out_pad, &sha256->state, SHA256_DIGEST_SIZE);
719734
break;
720735

721736
case OTX_CPT_SHA384:
722737
case OTX_CPT_SHA512:
723-
sha512 = (struct sha512_state *) in_pad;
738+
sha512 = (struct sha512_state *)pad;
724739
swap_data64(sha512->state, SHA512_DIGEST_SIZE);
725-
memcpy(out_pad, &sha512->state, SHA512_DIGEST_SIZE);
726740
break;
727741

728742
default:
@@ -732,55 +746,53 @@ static int copy_pad(u8 mac_type, u8 *out_pad, u8 *in_pad)
732746
return 0;
733747
}
734748

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)
736751
{
737752
struct otx_cpt_aead_ctx *ctx = crypto_aead_ctx_dma(cipher);
738-
int state_size = crypto_shash_statesize(ctx->hashalg);
739753
int ds = crypto_shash_digestsize(ctx->hashalg);
740754
int bs = crypto_shash_blocksize(ctx->hashalg);
741-
int authkeylen = ctx->auth_key_len;
755+
int authkeylen = keys->authkeylen;
742756
u8 *ipad = NULL, *opad = NULL;
743-
int ret = 0, icount = 0;
757+
int icount = 0;
758+
int ret;
744759

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);
748768

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;
754771

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;
760774

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;
765788
}
766789

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);
772791

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;
783794

795+
memcpy(ipad, ctx->key, authkeylen);
784796
memset(ipad + authkeylen, 0, bs - authkeylen);
785797
memcpy(opad, ipad, bs);
786798

@@ -798,91 +810,33 @@ static int aead_hmac_init(struct crypto_aead *cipher)
798810
crypto_shash_init(&ctx->sdesc->shash);
799811
crypto_shash_update(&ctx->sdesc->shash, ipad, bs);
800812
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);
802814
if (ret)
803815
goto calc_fail;
804816

805817
/* OPAD Calculation */
806818
crypto_shash_init(&ctx->sdesc->shash);
807819
crypto_shash_update(&ctx->sdesc->shash, opad, bs);
808820
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);
817822

818823
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-
828824
return ret;
829825
}
830826

831827
static int otx_cpt_aead_cbc_aes_sha_setkey(struct crypto_aead *cipher,
832828
const unsigned char *key,
833829
unsigned int keylen)
834830
{
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;
856833

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);
882835
if (status)
883836
goto badkey;
884837

885-
return 0;
838+
status = aead_hmac_init(cipher, &authenc_keys);
839+
886840
badkey:
887841
return status;
888842
}
@@ -891,36 +845,7 @@ static int otx_cpt_aead_ecb_null_sha_setkey(struct crypto_aead *cipher,
891845
const unsigned char *key,
892846
unsigned int keylen)
893847
{
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);
924849
}
925850

926851
static int otx_cpt_aead_gcm_aes_setkey(struct crypto_aead *cipher,

0 commit comments

Comments
 (0)