Skip to content

Commit 93242fa

Browse files
ebiggersgregkh
authored andcommitted
crypto: ccree - convert to use crypto_authenc_extractkeys()
commit dc95b53 upstream. Convert the ccree crypto driver to use crypto_authenc_extractkeys() so that it picks up the fix for broken validation of rtattr::rta_len. Fixes: ff27e85 ("crypto: ccree - add AEAD support") Cc: <stable@vger.kernel.org> # v4.17+ Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6590803 commit 93242fa

1 file changed

Lines changed: 19 additions & 21 deletions

File tree

‎drivers/crypto/ccree/cc_aead.c‎

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -540,49 +540,46 @@ static int cc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
540540
unsigned int keylen)
541541
{
542542
struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
543-
struct rtattr *rta = (struct rtattr *)key;
544543
struct cc_crypto_req cc_req = {};
545-
struct crypto_authenc_key_param *param;
546544
struct cc_hw_desc desc[MAX_AEAD_SETKEY_SEQ];
547-
int rc = -EINVAL;
548545
unsigned int seq_len = 0;
549546
struct device *dev = drvdata_to_dev(ctx->drvdata);
547+
const u8 *enckey, *authkey;
548+
int rc;
550549

551550
dev_dbg(dev, "Setting key in context @%p for %s. key=%p keylen=%u\n",
552551
ctx, crypto_tfm_alg_name(crypto_aead_tfm(tfm)), key, keylen);
553552

554553
/* STAT_PHASE_0: Init and sanity checks */
555554

556555
if (ctx->auth_mode != DRV_HASH_NULL) { /* authenc() alg. */
557-
if (!RTA_OK(rta, keylen))
558-
goto badkey;
559-
if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
560-
goto badkey;
561-
if (RTA_PAYLOAD(rta) < sizeof(*param))
562-
goto badkey;
563-
param = RTA_DATA(rta);
564-
ctx->enc_keylen = be32_to_cpu(param->enckeylen);
565-
key += RTA_ALIGN(rta->rta_len);
566-
keylen -= RTA_ALIGN(rta->rta_len);
567-
if (keylen < ctx->enc_keylen)
556+
struct crypto_authenc_keys keys;
557+
558+
rc = crypto_authenc_extractkeys(&keys, key, keylen);
559+
if (rc)
568560
goto badkey;
569-
ctx->auth_keylen = keylen - ctx->enc_keylen;
561+
enckey = keys.enckey;
562+
authkey = keys.authkey;
563+
ctx->enc_keylen = keys.enckeylen;
564+
ctx->auth_keylen = keys.authkeylen;
570565

571566
if (ctx->cipher_mode == DRV_CIPHER_CTR) {
572567
/* the nonce is stored in bytes at end of key */
568+
rc = -EINVAL;
573569
if (ctx->enc_keylen <
574570
(AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE))
575571
goto badkey;
576572
/* Copy nonce from last 4 bytes in CTR key to
577573
* first 4 bytes in CTR IV
578574
*/
579-
memcpy(ctx->ctr_nonce, key + ctx->auth_keylen +
580-
ctx->enc_keylen - CTR_RFC3686_NONCE_SIZE,
581-
CTR_RFC3686_NONCE_SIZE);
575+
memcpy(ctx->ctr_nonce, enckey + ctx->enc_keylen -
576+
CTR_RFC3686_NONCE_SIZE, CTR_RFC3686_NONCE_SIZE);
582577
/* Set CTR key size */
583578
ctx->enc_keylen -= CTR_RFC3686_NONCE_SIZE;
584579
}
585580
} else { /* non-authenc - has just one key */
581+
enckey = key;
582+
authkey = NULL;
586583
ctx->enc_keylen = keylen;
587584
ctx->auth_keylen = 0;
588585
}
@@ -594,13 +591,14 @@ static int cc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
594591
/* STAT_PHASE_1: Copy key to ctx */
595592

596593
/* Get key material */
597-
memcpy(ctx->enckey, key + ctx->auth_keylen, ctx->enc_keylen);
594+
memcpy(ctx->enckey, enckey, ctx->enc_keylen);
598595
if (ctx->enc_keylen == 24)
599596
memset(ctx->enckey + 24, 0, CC_AES_KEY_SIZE_MAX - 24);
600597
if (ctx->auth_mode == DRV_HASH_XCBC_MAC) {
601-
memcpy(ctx->auth_state.xcbc.xcbc_keys, key, ctx->auth_keylen);
598+
memcpy(ctx->auth_state.xcbc.xcbc_keys, authkey,
599+
ctx->auth_keylen);
602600
} else if (ctx->auth_mode != DRV_HASH_NULL) { /* HMAC */
603-
rc = cc_get_plain_hmac_key(tfm, key, ctx->auth_keylen);
601+
rc = cc_get_plain_hmac_key(tfm, authkey, ctx->auth_keylen);
604602
if (rc)
605603
goto badkey;
606604
}

0 commit comments

Comments
 (0)