From nobody Fri Nov 22 21:13:12 2024 Received: from szxga06-in.huawei.com (szxga06-in.huawei.com [45.249.212.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB5F761FEB; Fri, 15 Nov 2024 10:21:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731666108; cv=none; b=be/gtbskotvmbfVJnF8BNG691WtSlcZSPKwON/Geh2tFRAX4h2geR1tH172Qd7VZsh4m3Oro7Sai/GCdrXVWgomrdO7FedYPkfKDKp05wQ41owU7OVDcVYpzTHF9RsbyszMwJrHT64Fbjk32tpnRBlhwX711yPiTg+XPCWPWjgk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731666108; c=relaxed/simple; bh=GEwtf78cD4y+rCJ1s3uaU/k4waiB1eqwemrGCaeuoyo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WbK8og4hQZGaXH42wq0tqhGoCChcMzZh0erY7AGPieD0SwiRPftOYTi1oXHAWC4WmpPe+0N2xQfbW7mJG44zn0MMbxSwCc5wk2+vaQTs8XVWXN8BEtFK7Mno5k8wy23Kpx727c72F5K2DIpkuIRMX03KRtBtzUJ4wzordZJ+vS0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.234]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4XqY3P5mSgz1wwcn; Fri, 15 Nov 2024 18:21:53 +0800 (CST) Received: from kwepemd200024.china.huawei.com (unknown [7.221.188.85]) by mail.maildlp.com (Postfix) with ESMTPS id D515D1401F4; Fri, 15 Nov 2024 18:21:41 +0800 (CST) Received: from localhost.huawei.com (10.90.30.45) by kwepemd200024.china.huawei.com (7.221.188.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 15 Nov 2024 18:21:41 +0800 From: Chenghai Huang To: , CC: , , , , Subject: [PATCH v4 1/2] crypto: hisilicon/sec2 - fix for aead icv error Date: Fri, 15 Nov 2024 18:21:38 +0800 Message-ID: <20241115102139.3793659-2-huangchenghai2@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241115102139.3793659-1-huangchenghai2@huawei.com> References: <20241115102139.3793659-1-huangchenghai2@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemd200024.china.huawei.com (7.221.188.85) Content-Type: text/plain; charset="utf-8" From: Wenkai Lin When the AEAD algorithm is used for encryption or decryption, the input authentication length varies, the hardware needs to obtain the input length to pass the integrity check verification. Currently, the driver uses a fixed authentication length,which causes decryption failure, so the length configuration is modified. In addition, the step of setting the auth length is unnecessary, so it was deleted from the setkey function. Fixes: 2f072d75d1ab ("crypto: hisilicon - Add aead support on SEC2") Signed-off-by: Wenkai Lin Signed-off-by: Chenghai Huang --- drivers/crypto/hisilicon/sec2/sec.h | 1 - drivers/crypto/hisilicon/sec2/sec_crypto.c | 101 +++++++++------------ drivers/crypto/hisilicon/sec2/sec_crypto.h | 11 --- 3 files changed, 44 insertions(+), 69 deletions(-) diff --git a/drivers/crypto/hisilicon/sec2/sec.h b/drivers/crypto/hisilicon= /sec2/sec.h index 356188bee6fb..70c3bdedb6ba 100644 --- a/drivers/crypto/hisilicon/sec2/sec.h +++ b/drivers/crypto/hisilicon/sec2/sec.h @@ -90,7 +90,6 @@ struct sec_auth_ctx { dma_addr_t a_key_dma; u8 *a_key; u8 a_key_len; - u8 mac_len; u8 a_alg; bool fallback; struct crypto_shash *hash_tfm; diff --git a/drivers/crypto/hisilicon/sec2/sec_crypto.c b/drivers/crypto/hi= silicon/sec2/sec_crypto.c index ae9ebbb4103d..8db995279545 100644 --- a/drivers/crypto/hisilicon/sec2/sec_crypto.c +++ b/drivers/crypto/hisilicon/sec2/sec_crypto.c @@ -948,15 +948,14 @@ static int sec_aead_mac_init(struct sec_aead_req *req) struct aead_request *aead_req =3D req->aead_req; struct crypto_aead *tfm =3D crypto_aead_reqtfm(aead_req); size_t authsize =3D crypto_aead_authsize(tfm); - u8 *mac_out =3D req->out_mac; struct scatterlist *sgl =3D aead_req->src; + u8 *mac_out =3D req->out_mac; size_t copy_size; off_t skip_size; =20 /* Copy input mac */ skip_size =3D aead_req->assoclen + aead_req->cryptlen - authsize; - copy_size =3D sg_pcopy_to_buffer(sgl, sg_nents(sgl), mac_out, - authsize, skip_size); + copy_size =3D sg_pcopy_to_buffer(sgl, sg_nents(sgl), mac_out, authsize, s= kip_size); if (unlikely(copy_size !=3D authsize)) return -EINVAL; =20 @@ -1139,7 +1138,6 @@ static int sec_aead_fallback_setkey(struct sec_auth_c= tx *a_ctx, static int sec_aead_setkey(struct crypto_aead *tfm, const u8 *key, const u32 keylen, const enum sec_hash_alg a_alg, const enum sec_calg c_alg, - const enum sec_mac_len mac_len, const enum sec_cmode c_mode) { struct sec_ctx *ctx =3D crypto_aead_ctx(tfm); @@ -1151,7 +1149,6 @@ static int sec_aead_setkey(struct crypto_aead *tfm, c= onst u8 *key, =20 ctx->a_ctx.a_alg =3D a_alg; ctx->c_ctx.c_alg =3D c_alg; - ctx->a_ctx.mac_len =3D mac_len; c_ctx->c_mode =3D c_mode; =20 if (c_mode =3D=3D SEC_CMODE_CCM || c_mode =3D=3D SEC_CMODE_GCM) { @@ -1187,10 +1184,9 @@ static int sec_aead_setkey(struct crypto_aead *tfm, = const u8 *key, goto bad_key; } =20 - if ((ctx->a_ctx.mac_len & SEC_SQE_LEN_RATE_MASK) || - (ctx->a_ctx.a_key_len & SEC_SQE_LEN_RATE_MASK)) { + if (ctx->a_ctx.a_key_len & SEC_SQE_LEN_RATE_MASK) { ret =3D -EINVAL; - dev_err(dev, "MAC or AUTH key length error!\n"); + dev_err(dev, "AUTH key length error!\n"); goto bad_key; } =20 @@ -1202,27 +1198,19 @@ static int sec_aead_setkey(struct crypto_aead *tfm,= const u8 *key, } =20 =20 -#define GEN_SEC_AEAD_SETKEY_FUNC(name, aalg, calg, maclen, cmode) \ -static int sec_setkey_##name(struct crypto_aead *tfm, const u8 *key, \ - u32 keylen) \ -{ \ - return sec_aead_setkey(tfm, key, keylen, aalg, calg, maclen, cmode);\ -} - -GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha1, SEC_A_HMAC_SHA1, - SEC_CALG_AES, SEC_HMAC_SHA1_MAC, SEC_CMODE_CBC) -GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha256, SEC_A_HMAC_SHA256, - SEC_CALG_AES, SEC_HMAC_SHA256_MAC, SEC_CMODE_CBC) -GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha512, SEC_A_HMAC_SHA512, - SEC_CALG_AES, SEC_HMAC_SHA512_MAC, SEC_CMODE_CBC) -GEN_SEC_AEAD_SETKEY_FUNC(aes_ccm, 0, SEC_CALG_AES, - SEC_HMAC_CCM_MAC, SEC_CMODE_CCM) -GEN_SEC_AEAD_SETKEY_FUNC(aes_gcm, 0, SEC_CALG_AES, - SEC_HMAC_GCM_MAC, SEC_CMODE_GCM) -GEN_SEC_AEAD_SETKEY_FUNC(sm4_ccm, 0, SEC_CALG_SM4, - SEC_HMAC_CCM_MAC, SEC_CMODE_CCM) -GEN_SEC_AEAD_SETKEY_FUNC(sm4_gcm, 0, SEC_CALG_SM4, - SEC_HMAC_GCM_MAC, SEC_CMODE_GCM) +#define GEN_SEC_AEAD_SETKEY_FUNC(name, aalg, calg, cmode) \ +static int sec_setkey_##name(struct crypto_aead *tfm, const u8 *key, u32 k= eylen) \ +{ \ + return sec_aead_setkey(tfm, key, keylen, aalg, calg, cmode); \ +} + +GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha1, SEC_A_HMAC_SHA1, SEC_CALG_AES, SEC_= CMODE_CBC) +GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha256, SEC_A_HMAC_SHA256, SEC_CALG_AES, = SEC_CMODE_CBC) +GEN_SEC_AEAD_SETKEY_FUNC(aes_cbc_sha512, SEC_A_HMAC_SHA512, SEC_CALG_AES, = SEC_CMODE_CBC) +GEN_SEC_AEAD_SETKEY_FUNC(aes_ccm, 0, SEC_CALG_AES, SEC_CMODE_CCM) +GEN_SEC_AEAD_SETKEY_FUNC(aes_gcm, 0, SEC_CALG_AES, SEC_CMODE_GCM) +GEN_SEC_AEAD_SETKEY_FUNC(sm4_ccm, 0, SEC_CALG_SM4, SEC_CMODE_CCM) +GEN_SEC_AEAD_SETKEY_FUNC(sm4_gcm, 0, SEC_CALG_SM4, SEC_CMODE_GCM) =20 static int sec_aead_sgl_map(struct sec_ctx *ctx, struct sec_req *req) { @@ -1470,9 +1458,10 @@ static void sec_skcipher_callback(struct sec_ctx *ct= x, struct sec_req *req, static void set_aead_auth_iv(struct sec_ctx *ctx, struct sec_req *req) { struct aead_request *aead_req =3D req->aead_req.aead_req; - struct sec_cipher_req *c_req =3D &req->c_req; + struct crypto_aead *tfm =3D crypto_aead_reqtfm(aead_req); + size_t authsize =3D crypto_aead_authsize(tfm); struct sec_aead_req *a_req =3D &req->aead_req; - size_t authsize =3D ctx->a_ctx.mac_len; + struct sec_cipher_req *c_req =3D &req->c_req; u32 data_size =3D aead_req->cryptlen; u8 flage =3D 0; u8 cm, cl; @@ -1513,10 +1502,8 @@ static void set_aead_auth_iv(struct sec_ctx *ctx, st= ruct sec_req *req) static void sec_aead_set_iv(struct sec_ctx *ctx, struct sec_req *req) { struct aead_request *aead_req =3D req->aead_req.aead_req; - struct crypto_aead *tfm =3D crypto_aead_reqtfm(aead_req); - size_t authsize =3D crypto_aead_authsize(tfm); - struct sec_cipher_req *c_req =3D &req->c_req; struct sec_aead_req *a_req =3D &req->aead_req; + struct sec_cipher_req *c_req =3D &req->c_req; =20 memcpy(c_req->c_ivin, aead_req->iv, ctx->c_ctx.ivsize); =20 @@ -1524,15 +1511,11 @@ static void sec_aead_set_iv(struct sec_ctx *ctx, st= ruct sec_req *req) /* * CCM 16Byte Cipher_IV: {1B_Flage,13B_IV,2B_counter}, * the counter must set to 0x01 + * CCM 16Byte Auth_IV: {1B_AFlage,13B_IV,2B_Ptext_length} */ - ctx->a_ctx.mac_len =3D authsize; - /* CCM 16Byte Auth_IV: {1B_AFlage,13B_IV,2B_Ptext_length} */ set_aead_auth_iv(ctx, req); - } - - /* GCM 12Byte Cipher_IV =3D=3D Auth_IV */ - if (ctx->c_ctx.c_mode =3D=3D SEC_CMODE_GCM) { - ctx->a_ctx.mac_len =3D authsize; + } else if (ctx->c_ctx.c_mode =3D=3D SEC_CMODE_GCM) { + /* GCM 12Byte Cipher_IV =3D=3D Auth_IV */ memcpy(a_req->a_ivin, c_req->c_ivin, SEC_AIV_SIZE); } } @@ -1542,9 +1525,11 @@ static void sec_auth_bd_fill_xcm(struct sec_auth_ctx= *ctx, int dir, { struct sec_aead_req *a_req =3D &req->aead_req; struct aead_request *aq =3D a_req->aead_req; + struct crypto_aead *tfm =3D crypto_aead_reqtfm(aq); + size_t authsize =3D crypto_aead_authsize(tfm); =20 /* C_ICV_Len is MAC size, 0x4 ~ 0x10 */ - sec_sqe->type2.icvw_kmode |=3D cpu_to_le16((u16)ctx->mac_len); + sec_sqe->type2.icvw_kmode |=3D cpu_to_le16((u16)authsize); =20 /* mode set to CCM/GCM, don't set {A_Alg, AKey_Len, MAC_Len} */ sec_sqe->type2.a_key_addr =3D sec_sqe->type2.c_key_addr; @@ -1568,9 +1553,11 @@ static void sec_auth_bd_fill_xcm_v3(struct sec_auth_= ctx *ctx, int dir, { struct sec_aead_req *a_req =3D &req->aead_req; struct aead_request *aq =3D a_req->aead_req; + struct crypto_aead *tfm =3D crypto_aead_reqtfm(aq); + size_t authsize =3D crypto_aead_authsize(tfm); =20 /* C_ICV_Len is MAC size, 0x4 ~ 0x10 */ - sqe3->c_icv_key |=3D cpu_to_le16((u16)ctx->mac_len << SEC_MAC_OFFSET_V3); + sqe3->c_icv_key |=3D cpu_to_le16((u16)authsize << SEC_MAC_OFFSET_V3); =20 /* mode set to CCM/GCM, don't set {A_Alg, AKey_Len, MAC_Len} */ sqe3->a_key_addr =3D sqe3->c_key_addr; @@ -1594,11 +1581,12 @@ static void sec_auth_bd_fill_ex(struct sec_auth_ctx= *ctx, int dir, struct sec_aead_req *a_req =3D &req->aead_req; struct sec_cipher_req *c_req =3D &req->c_req; struct aead_request *aq =3D a_req->aead_req; + struct crypto_aead *tfm =3D crypto_aead_reqtfm(aq); + size_t authsize =3D crypto_aead_authsize(tfm); =20 sec_sqe->type2.a_key_addr =3D cpu_to_le64(ctx->a_key_dma); =20 - sec_sqe->type2.mac_key_alg =3D - cpu_to_le32(ctx->mac_len / SEC_SQE_LEN_RATE); + sec_sqe->type2.mac_key_alg =3D cpu_to_le32(authsize / SEC_SQE_LEN_RATE); =20 sec_sqe->type2.mac_key_alg |=3D cpu_to_le32((u32)((ctx->a_key_len) / @@ -1648,11 +1636,13 @@ static void sec_auth_bd_fill_ex_v3(struct sec_auth_= ctx *ctx, int dir, struct sec_aead_req *a_req =3D &req->aead_req; struct sec_cipher_req *c_req =3D &req->c_req; struct aead_request *aq =3D a_req->aead_req; + struct crypto_aead *tfm =3D crypto_aead_reqtfm(aq); + size_t authsize =3D crypto_aead_authsize(tfm); =20 sqe3->a_key_addr =3D cpu_to_le64(ctx->a_key_dma); =20 sqe3->auth_mac_key |=3D - cpu_to_le32((u32)(ctx->mac_len / + cpu_to_le32((u32)(authsize / SEC_SQE_LEN_RATE) << SEC_MAC_OFFSET_V3); =20 sqe3->auth_mac_key |=3D @@ -1703,9 +1693,9 @@ static void sec_aead_callback(struct sec_ctx *c, stru= ct sec_req *req, int err) { struct aead_request *a_req =3D req->aead_req.aead_req; struct crypto_aead *tfm =3D crypto_aead_reqtfm(a_req); + size_t authsize =3D crypto_aead_authsize(tfm); struct sec_aead_req *aead_req =3D &req->aead_req; struct sec_cipher_req *c_req =3D &req->c_req; - size_t authsize =3D crypto_aead_authsize(tfm); struct sec_qp_ctx *qp_ctx =3D req->qp_ctx; struct aead_request *backlog_aead_req; struct sec_req *backlog_req; @@ -1718,10 +1708,8 @@ static void sec_aead_callback(struct sec_ctx *c, str= uct sec_req *req, int err) if (!err && c_req->encrypt) { struct scatterlist *sgl =3D a_req->dst; =20 - sz =3D sg_pcopy_from_buffer(sgl, sg_nents(sgl), - aead_req->out_mac, - authsize, a_req->cryptlen + - a_req->assoclen); + sz =3D sg_pcopy_from_buffer(sgl, sg_nents(sgl), aead_req->out_mac, + authsize, a_req->cryptlen + a_req->assoclen); if (unlikely(sz !=3D authsize)) { dev_err(c->dev, "copy out mac err!\n"); err =3D -EINVAL; @@ -2233,7 +2221,7 @@ static int sec_aead_spec_check(struct sec_ctx *ctx, s= truct sec_req *sreq) { struct aead_request *req =3D sreq->aead_req.aead_req; struct crypto_aead *tfm =3D crypto_aead_reqtfm(req); - size_t authsize =3D crypto_aead_authsize(tfm); + size_t sz =3D crypto_aead_authsize(tfm); u8 c_mode =3D ctx->c_ctx.c_mode; struct device *dev =3D ctx->dev; int ret; @@ -2244,9 +2232,8 @@ static int sec_aead_spec_check(struct sec_ctx *ctx, s= truct sec_req *sreq) return -EINVAL; } =20 - if (unlikely((c_mode =3D=3D SEC_CMODE_GCM && authsize < DES_BLOCK_SIZE) || - (c_mode =3D=3D SEC_CMODE_CCM && (authsize < MIN_MAC_LEN || - authsize & MAC_LEN_MASK)))) { + if (unlikely((c_mode =3D=3D SEC_CMODE_GCM && sz < DES_BLOCK_SIZE) || + (c_mode =3D=3D SEC_CMODE_CCM && (sz < MIN_MAC_LEN || sz & MAC_LEN_M= ASK)))) { dev_err(dev, "aead input mac length error!\n"); return -EINVAL; } @@ -2266,7 +2253,7 @@ static int sec_aead_spec_check(struct sec_ctx *ctx, s= truct sec_req *sreq) if (sreq->c_req.encrypt) sreq->c_req.c_len =3D req->cryptlen; else - sreq->c_req.c_len =3D req->cryptlen - authsize; + sreq->c_req.c_len =3D req->cryptlen - sz; if (c_mode =3D=3D SEC_CMODE_CBC) { if (unlikely(sreq->c_req.c_len & (AES_BLOCK_SIZE - 1))) { dev_err(dev, "aead crypto length error!\n"); @@ -2292,7 +2279,7 @@ static int sec_aead_param_check(struct sec_ctx *ctx, = struct sec_req *sreq) =20 if (ctx->sec->qm.ver =3D=3D QM_HW_V2) { if (unlikely(!req->cryptlen || (!sreq->c_req.encrypt && - req->cryptlen <=3D authsize))) { + req->cryptlen <=3D authsize))) { ctx->a_ctx.fallback =3D true; return -EINVAL; } diff --git a/drivers/crypto/hisilicon/sec2/sec_crypto.h b/drivers/crypto/hi= silicon/sec2/sec_crypto.h index 27a0ee5ad913..04725b514382 100644 --- a/drivers/crypto/hisilicon/sec2/sec_crypto.h +++ b/drivers/crypto/hisilicon/sec2/sec_crypto.h @@ -23,17 +23,6 @@ enum sec_hash_alg { SEC_A_HMAC_SHA512 =3D 0x15, }; =20 -enum sec_mac_len { - SEC_HMAC_CCM_MAC =3D 16, - SEC_HMAC_GCM_MAC =3D 16, - SEC_SM3_MAC =3D 32, - SEC_HMAC_SM3_MAC =3D 32, - SEC_HMAC_MD5_MAC =3D 16, - SEC_HMAC_SHA1_MAC =3D 20, - SEC_HMAC_SHA256_MAC =3D 32, - SEC_HMAC_SHA512_MAC =3D 64, -}; - enum sec_cmode { SEC_CMODE_ECB =3D 0x0, SEC_CMODE_CBC =3D 0x1, --=20 2.33.0 From nobody Fri Nov 22 21:13:12 2024 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0766E13D52B; Fri, 15 Nov 2024 10:21:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731666108; cv=none; b=AEVZOSsmONxkiTk/aXhxe2bM+WSDy3E0YTsM5ohH6uUUAXyd1QRJFzxgOVhuEgjxiaNhU2EWnP+cUz9mWTL7b6QAlKOfSlJmUed7mvrGsNaOIY0xqMoAmwShM9W/nhJLsCHJsiCSZ8sMpPKXLLY/mEJ4HTvayw2g/WNEjHBu+M8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731666108; c=relaxed/simple; bh=6BQaJXZKOiOBwXYWSwcZkOiPe81Q04tYPz74uEAa5X0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=OcipL1XCX4He+3Jf/oTEFUYL1ve5ANlCPQqgnt1ZZvPE+5NyOTMQnubElN4MpYr1j2wccvAZaq6KSAFHrNM3zdfAMyxyh+wlAbdxksVWN/Z8diC+HSiqMPCATuHqEREVNKKZLI83z5WthCvLtE88SnWty2+gvPfJAZP37ihEsq4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4XqY0x5bhtz10W2Q; Fri, 15 Nov 2024 18:19:45 +0800 (CST) Received: from kwepemd200024.china.huawei.com (unknown [7.221.188.85]) by mail.maildlp.com (Postfix) with ESMTPS id 37A671400DD; Fri, 15 Nov 2024 18:21:42 +0800 (CST) Received: from localhost.huawei.com (10.90.30.45) by kwepemd200024.china.huawei.com (7.221.188.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 15 Nov 2024 18:21:41 +0800 From: Chenghai Huang To: , CC: , , , , Subject: [PATCH v4 2/2] crypto: hisilicon/sec2 - fix for aead invalid authsize Date: Fri, 15 Nov 2024 18:21:39 +0800 Message-ID: <20241115102139.3793659-3-huangchenghai2@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241115102139.3793659-1-huangchenghai2@huawei.com> References: <20241115102139.3793659-1-huangchenghai2@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemd200024.china.huawei.com (7.221.188.85) Content-Type: text/plain; charset="utf-8" From: Wenkai Lin When the digest alg is HMAC-SHAx or another, the authsize may be less than 4 bytes and mac_len of the BD is set to zero, the hardware considers it a BD configuration error and reports a ras error, so the sec driver needs to switch to software calculation in this case, this patch add a check for it and remove unnecessary check that has been done by crypto. Fixes: 2f072d75d1ab ("crypto: hisilicon - Add aead support on SEC2") Signed-off-by: Wenkai Lin Signed-off-by: Chenghai Huang --- drivers/crypto/hisilicon/sec2/sec.h | 1 + drivers/crypto/hisilicon/sec2/sec_crypto.c | 64 ++++++++++++---------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/drivers/crypto/hisilicon/sec2/sec.h b/drivers/crypto/hisilicon= /sec2/sec.h index 70c3bdedb6ba..df2c930df2fb 100644 --- a/drivers/crypto/hisilicon/sec2/sec.h +++ b/drivers/crypto/hisilicon/sec2/sec.h @@ -37,6 +37,7 @@ struct sec_aead_req { u8 *a_ivin; dma_addr_t a_ivin_dma; struct aead_request *aead_req; + bool fallback; }; =20 /* SEC request of Crypto */ diff --git a/drivers/crypto/hisilicon/sec2/sec_crypto.c b/drivers/crypto/hi= silicon/sec2/sec_crypto.c index 8db995279545..c3a474e18593 100644 --- a/drivers/crypto/hisilicon/sec2/sec_crypto.c +++ b/drivers/crypto/hisilicon/sec2/sec_crypto.c @@ -1119,10 +1119,7 @@ static int sec_aead_setauthsize(struct crypto_aead *= aead, unsigned int authsize) struct sec_ctx *ctx =3D crypto_tfm_ctx(tfm); struct sec_auth_ctx *a_ctx =3D &ctx->a_ctx; =20 - if (unlikely(a_ctx->fallback_aead_tfm)) - return crypto_aead_setauthsize(a_ctx->fallback_aead_tfm, authsize); - - return 0; + return crypto_aead_setauthsize(a_ctx->fallback_aead_tfm, authsize); } =20 static int sec_aead_fallback_setkey(struct sec_auth_ctx *a_ctx, @@ -1159,13 +1156,7 @@ static int sec_aead_setkey(struct crypto_aead *tfm, = const u8 *key, } memcpy(c_ctx->c_key, key, keylen); =20 - if (unlikely(a_ctx->fallback_aead_tfm)) { - ret =3D sec_aead_fallback_setkey(a_ctx, tfm, key, keylen); - if (ret) - return ret; - } - - return 0; + return sec_aead_fallback_setkey(a_ctx, tfm, key, keylen); } =20 ret =3D crypto_authenc_extractkeys(&keys, key, keylen); @@ -1190,6 +1181,12 @@ static int sec_aead_setkey(struct crypto_aead *tfm, = const u8 *key, goto bad_key; } =20 + ret =3D sec_aead_fallback_setkey(a_ctx, tfm, key, keylen); + if (ret) { + dev_err(dev, "set sec fallback key err!\n"); + goto bad_key; + } + return 0; =20 bad_key: @@ -1917,8 +1914,10 @@ static void sec_aead_exit(struct crypto_aead *tfm) =20 static int sec_aead_ctx_init(struct crypto_aead *tfm, const char *hash_nam= e) { + struct aead_alg *alg =3D crypto_aead_alg(tfm); struct sec_ctx *ctx =3D crypto_aead_ctx(tfm); - struct sec_auth_ctx *auth_ctx =3D &ctx->a_ctx; + struct sec_auth_ctx *a_ctx =3D &ctx->a_ctx; + const char *aead_name =3D alg->base.cra_name; int ret; =20 ret =3D sec_aead_init(tfm); @@ -1927,12 +1926,22 @@ static int sec_aead_ctx_init(struct crypto_aead *tf= m, const char *hash_name) return ret; } =20 - auth_ctx->hash_tfm =3D crypto_alloc_shash(hash_name, 0, 0); - if (IS_ERR(auth_ctx->hash_tfm)) { + a_ctx->hash_tfm =3D crypto_alloc_shash(hash_name, 0, 0); + if (IS_ERR(a_ctx->hash_tfm)) { dev_err(ctx->dev, "aead alloc shash error!\n"); sec_aead_exit(tfm); - return PTR_ERR(auth_ctx->hash_tfm); + return PTR_ERR(a_ctx->hash_tfm); + } + + a_ctx->fallback_aead_tfm =3D crypto_alloc_aead(aead_name, 0, + CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC); + if (IS_ERR(a_ctx->fallback_aead_tfm)) { + dev_err(ctx->dev, "aead driver alloc fallback tfm error!\n"); + crypto_free_shash(ctx->a_ctx.hash_tfm); + sec_aead_exit(tfm); + return PTR_ERR(a_ctx->fallback_aead_tfm); } + a_ctx->fallback =3D false; =20 return 0; } @@ -1941,6 +1950,7 @@ static void sec_aead_ctx_exit(struct crypto_aead *tfm) { struct sec_ctx *ctx =3D crypto_aead_ctx(tfm); =20 + crypto_free_aead(ctx->a_ctx.fallback_aead_tfm); crypto_free_shash(ctx->a_ctx.hash_tfm); sec_aead_exit(tfm); } @@ -2226,15 +2236,15 @@ static int sec_aead_spec_check(struct sec_ctx *ctx,= struct sec_req *sreq) struct device *dev =3D ctx->dev; int ret; =20 - if (unlikely(req->cryptlen + req->assoclen > MAX_INPUT_DATA_LEN || - req->assoclen > SEC_MAX_AAD_LEN)) { - dev_err(dev, "aead input spec error!\n"); + /* Hardware does not handle cases where authsize is less than 4 bytes */ + if (unlikely(sz < MIN_MAC_LEN)) { + sreq->aead_req.fallback =3D true; return -EINVAL; } =20 - if (unlikely((c_mode =3D=3D SEC_CMODE_GCM && sz < DES_BLOCK_SIZE) || - (c_mode =3D=3D SEC_CMODE_CCM && (sz < MIN_MAC_LEN || sz & MAC_LEN_M= ASK)))) { - dev_err(dev, "aead input mac length error!\n"); + if (unlikely(req->cryptlen + req->assoclen > MAX_INPUT_DATA_LEN || + req->assoclen > SEC_MAX_AAD_LEN)) { + dev_err(dev, "aead input spec error!\n"); return -EINVAL; } =20 @@ -2280,7 +2290,7 @@ static int sec_aead_param_check(struct sec_ctx *ctx, = struct sec_req *sreq) if (ctx->sec->qm.ver =3D=3D QM_HW_V2) { if (unlikely(!req->cryptlen || (!sreq->c_req.encrypt && req->cryptlen <=3D authsize))) { - ctx->a_ctx.fallback =3D true; + sreq->aead_req.fallback =3D true; return -EINVAL; } } @@ -2308,16 +2318,9 @@ static int sec_aead_soft_crypto(struct sec_ctx *ctx, bool encrypt) { struct sec_auth_ctx *a_ctx =3D &ctx->a_ctx; - struct device *dev =3D ctx->dev; struct aead_request *subreq; int ret; =20 - /* Kunpeng920 aead mode not support input 0 size */ - if (!a_ctx->fallback_aead_tfm) { - dev_err(dev, "aead fallback tfm is NULL!\n"); - return -EINVAL; - } - subreq =3D aead_request_alloc(a_ctx->fallback_aead_tfm, GFP_KERNEL); if (!subreq) return -ENOMEM; @@ -2349,10 +2352,11 @@ static int sec_aead_crypto(struct aead_request *a_r= eq, bool encrypt) req->aead_req.aead_req =3D a_req; req->c_req.encrypt =3D encrypt; req->ctx =3D ctx; + req->aead_req.fallback =3D false; =20 ret =3D sec_aead_param_check(ctx, req); if (unlikely(ret)) { - if (ctx->a_ctx.fallback) + if (req->aead_req.fallback) return sec_aead_soft_crypto(ctx, a_req, encrypt); return -EINVAL; } --=20 2.33.0