Skip to content

Commit dc95b53

Browse files
ebiggersherbertx
authored andcommitted
crypto: ccree - convert to use crypto_authenc_extractkeys()
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>
1 parent ab57b33 commit dc95b53

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

drivers/crypto/ccree/cc_aead.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -549,49 +549,46 @@ static int cc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
549549
unsigned int keylen)
550550
{
551551
struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
552-
struct rtattr *rta = (struct rtattr *)key;
553552
struct cc_crypto_req cc_req = {};
554-
struct crypto_authenc_key_param *param;
555553
struct cc_hw_desc desc[MAX_AEAD_SETKEY_SEQ];
556-
int rc = -EINVAL;
557554
unsigned int seq_len = 0;
558555
struct device *dev = drvdata_to_dev(ctx->drvdata);
556+
const u8 *enckey, *authkey;
557+
int rc;
559558

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

563562
/* STAT_PHASE_0: Init and sanity checks */
564563

565564
if (ctx->auth_mode != DRV_HASH_NULL) { /* authenc() alg. */
566-
if (!RTA_OK(rta, keylen))
567-
goto badkey;
568-
if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
569-
goto badkey;
570-
if (RTA_PAYLOAD(rta) < sizeof(*param))
571-
goto badkey;
572-
param = RTA_DATA(rta);
573-
ctx->enc_keylen = be32_to_cpu(param->enckeylen);
574-
key += RTA_ALIGN(rta->rta_len);
575-
keylen -= RTA_ALIGN(rta->rta_len);
576-
if (keylen < ctx->enc_keylen)
565+
struct crypto_authenc_keys keys;
566+
567+
rc = crypto_authenc_extractkeys(&keys, key, keylen);
568+
if (rc)
577569
goto badkey;
578-
ctx->auth_keylen = keylen - ctx->enc_keylen;
570+
enckey = keys.enckey;
571+
authkey = keys.authkey;
572+
ctx->enc_keylen = keys.enckeylen;
573+
ctx->auth_keylen = keys.authkeylen;
579574

580575
if (ctx->cipher_mode == DRV_CIPHER_CTR) {
581576
/* the nonce is stored in bytes at end of key */
577+
rc = -EINVAL;
582578
if (ctx->enc_keylen <
583579
(AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE))
584580
goto badkey;
585581
/* Copy nonce from last 4 bytes in CTR key to
586582
* first 4 bytes in CTR IV
587583
*/
588-
memcpy(ctx->ctr_nonce, key + ctx->auth_keylen +
589-
ctx->enc_keylen - CTR_RFC3686_NONCE_SIZE,
590-
CTR_RFC3686_NONCE_SIZE);
584+
memcpy(ctx->ctr_nonce, enckey + ctx->enc_keylen -
585+
CTR_RFC3686_NONCE_SIZE, CTR_RFC3686_NONCE_SIZE);
591586
/* Set CTR key size */
592587
ctx->enc_keylen -= CTR_RFC3686_NONCE_SIZE;
593588
}
594589
} else { /* non-authenc - has just one key */
590+
enckey = key;
591+
authkey = NULL;
595592
ctx->enc_keylen = keylen;
596593
ctx->auth_keylen = 0;
597594
}
@@ -603,13 +600,14 @@ static int cc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
603600
/* STAT_PHASE_1: Copy key to ctx */
604601

605602
/* Get key material */
606-
memcpy(ctx->enckey, key + ctx->auth_keylen, ctx->enc_keylen);
603+
memcpy(ctx->enckey, enckey, ctx->enc_keylen);
607604
if (ctx->enc_keylen == 24)
608605
memset(ctx->enckey + 24, 0, CC_AES_KEY_SIZE_MAX - 24);
609606
if (ctx->auth_mode == DRV_HASH_XCBC_MAC) {
610-
memcpy(ctx->auth_state.xcbc.xcbc_keys, key, ctx->auth_keylen);
607+
memcpy(ctx->auth_state.xcbc.xcbc_keys, authkey,
608+
ctx->auth_keylen);
611609
} else if (ctx->auth_mode != DRV_HASH_NULL) { /* HMAC */
612-
rc = cc_get_plain_hmac_key(tfm, key, ctx->auth_keylen);
610+
rc = cc_get_plain_hmac_key(tfm, authkey, ctx->auth_keylen);
613611
if (rc)
614612
goto badkey;
615613
}

0 commit comments

Comments
 (0)