Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp4807735ybi; Tue, 28 May 2019 02:53:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqygdMQ0C991AyqNmdzEZnrmeUjiuCWyegvyk8GidN0xzNFqc5mpD0FYg8fsgz1ngOs75VXW X-Received: by 2002:a17:902:b089:: with SMTP id p9mr16565671plr.38.1559037205565; Tue, 28 May 2019 02:53:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559037205; cv=none; d=google.com; s=arc-20160816; b=TZojjnDMAF5wX7TRHXOU45AXR60DHP+I9uE/O1PbEbI9/cKSqmNaygz/o/u5yRJFNT exymEPlVlqtDKUHz+8FwKHGqg0noLG/Q1vowFCpiZOX/BfR4Y85cTKfLh5atKtpYk/vx SwN+zGWpd3rvi1B7dGpJdxRDTcfXIkdT/UHXU23kzmvq26E3QdQzzDEy6q9AmRGgsL9L nPp6JuPczN/vuF9/RvaAXBSStkVRoAPPhACAaQQL2+LqZ88oq0Bgrr+xr0cfcghOZYOe kS8AvM4oeVCIMsU+dKsbPYWrraAIiGrme7RuApUnG30KKB0UowDm+ec5ClYBwA9LJ13T HyfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=St3p0ku6IZQeSmkzKH5KUogc00emSwKNrHibB3Tgx6M=; b=ZXsmbmjgqONssyW8v9SYrJ1bgdYdcyH9vGgU3ywnejIx3O455Wlu1/i+KsYn5doxmG dyqmOzBOT5T5rukpYTw0a+fg+e47oqSMP5f/riFCXsoM7DiAjKTAKXqd/tZ15bywvcSi tdJy4sPYglxze2McJTZPaiICdsLJvBh5n/ovSEgWKGXoYz/BaNCPaV81yoPTjOrLR4I9 aS5ctKo80FNLEn/UXFLy4l7uXP1w4G1vAJ4CiMeho8fOYhOAQBYuoJmi4IE+eMW5m/7z nqu//3CV/LyP0B5RYR2v7cozI8sM+HMFWXHAmm5P+by6hWa8ek9FdEW3YYgcGPIvA05p Zdug== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o61si23033655pld.66.2019.05.28.02.53.11; Tue, 28 May 2019 02:53:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726418AbfE1JwR (ORCPT + 99 others); Tue, 28 May 2019 05:52:17 -0400 Received: from inva020.nxp.com ([92.121.34.13]:44018 "EHLO inva020.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726362AbfE1JwR (ORCPT ); Tue, 28 May 2019 05:52:17 -0400 Received: from inva020.nxp.com (localhost [127.0.0.1]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 6AC421A0DFB; Tue, 28 May 2019 11:52:15 +0200 (CEST) Received: from inva024.eu-rdc02.nxp.com (inva024.eu-rdc02.nxp.com [134.27.226.22]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 5E3381A02B9; Tue, 28 May 2019 11:52:15 +0200 (CEST) Received: from lorenz.ea.freescale.net (lorenz.ea.freescale.net [10.171.71.5]) by inva024.eu-rdc02.nxp.com (Postfix) with ESMTP id 11CFF205F4; Tue, 28 May 2019 11:52:15 +0200 (CEST) From: Iuliana Prodan To: Herbert Xu , Horia Geanta , Aymen Sghaier Cc: "David S. Miller" , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-imx Subject: [PATCH v5 2/2] crypto: caam - strip input without changing crypto request Date: Tue, 28 May 2019 12:52:11 +0300 Message-Id: <1559037131-4601-2-git-send-email-iuliana.prodan@nxp.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1559037131-4601-1-git-send-email-iuliana.prodan@nxp.com> References: <1559037131-4601-1-git-send-email-iuliana.prodan@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org For rsa and pkcs1pad, CAAM expects an input of modulus size. For this we strip the leading zeros in case the size is more than modulus. This commit avoids modifying the crypto request while stripping zeros from input, to comply with the crypto API requirement. This is done by adding a fixup input pointer and length. Signed-off-by: Iuliana Prodan Reviewed-by: Horia Geanta --- Changes since V4: - added Reviewed-by; - changed version to be in sync with patch 1/2. --- drivers/crypto/caam/caampkc.c | 39 ++++++++++++++++++++++++++------------- drivers/crypto/caam/caampkc.h | 7 ++++++- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c index 2bd3a17..19b02c1 100644 --- a/drivers/crypto/caam/caampkc.c +++ b/drivers/crypto/caam/caampkc.c @@ -32,8 +32,10 @@ static u8 *zero_buffer; static void rsa_io_unmap(struct device *dev, struct rsa_edesc *edesc, struct akcipher_request *req) { + struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); + dma_unmap_sg(dev, req->dst, edesc->dst_nents, DMA_FROM_DEVICE); - dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE); + dma_unmap_sg(dev, req_ctx->fixup_src, edesc->src_nents, DMA_TO_DEVICE); if (edesc->sec4_sg_bytes) dma_unmap_single(dev, edesc->sec4_sg_dma, edesc->sec4_sg_bytes, @@ -251,17 +253,21 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req, if (lzeros < 0) return ERR_PTR(lzeros); - req->src_len -= lzeros; - req->src = scatterwalk_ffwd(req_ctx->src, req->src, lzeros); + req_ctx->fixup_src = scatterwalk_ffwd(req_ctx->src, req->src, + lzeros); + req_ctx->fixup_src_len = req->src_len - lzeros; } else { /* * input src is less then n key modulus, * so there will be zero padding */ diff_size = key->n_sz - req->src_len; + req_ctx->fixup_src = req->src; + req_ctx->fixup_src_len = req->src_len; } - src_nents = sg_nents_for_len(req->src, req->src_len); + src_nents = sg_nents_for_len(req_ctx->fixup_src, + req_ctx->fixup_src_len); dst_nents = sg_nents_for_len(req->dst, req->dst_len); if (!diff_size && src_nents == 1) @@ -282,7 +288,7 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req, if (!edesc) return ERR_PTR(-ENOMEM); - sgc = dma_map_sg(dev, req->src, src_nents, DMA_TO_DEVICE); + sgc = dma_map_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE); if (unlikely(!sgc)) { dev_err(dev, "unable to map source\n"); goto src_fail; @@ -300,8 +306,8 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req, 0); if (sec4_sg_index) - sg_to_sec4_sg_last(req->src, src_nents, edesc->sec4_sg + - !!diff_size, 0); + sg_to_sec4_sg_last(req_ctx->fixup_src, src_nents, + edesc->sec4_sg + !!diff_size, 0); if (dst_nents > 1) sg_to_sec4_sg_last(req->dst, dst_nents, @@ -332,7 +338,7 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req, sec4_sg_fail: dma_unmap_sg(dev, req->dst, dst_nents, DMA_FROM_DEVICE); dst_fail: - dma_unmap_sg(dev, req->src, src_nents, DMA_TO_DEVICE); + dma_unmap_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE); src_fail: kfree(edesc); return ERR_PTR(-ENOMEM); @@ -342,6 +348,7 @@ static int set_rsa_pub_pdb(struct akcipher_request *req, struct rsa_edesc *edesc) { struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); + struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); struct caam_rsa_key *key = &ctx->key; struct device *dev = ctx->dev; @@ -366,7 +373,7 @@ static int set_rsa_pub_pdb(struct akcipher_request *req, pdb->f_dma = edesc->sec4_sg_dma; sec4_sg_index += edesc->src_nents; } else { - pdb->f_dma = sg_dma_address(req->src); + pdb->f_dma = sg_dma_address(req_ctx->fixup_src); } if (edesc->dst_nents > 1) { @@ -378,7 +385,7 @@ static int set_rsa_pub_pdb(struct akcipher_request *req, } pdb->sgf |= (key->e_sz << RSA_PDB_E_SHIFT) | key->n_sz; - pdb->f_len = req->src_len; + pdb->f_len = req_ctx->fixup_src_len; return 0; } @@ -411,7 +418,9 @@ static int set_rsa_priv_f1_pdb(struct akcipher_request *req, pdb->g_dma = edesc->sec4_sg_dma; sec4_sg_index += edesc->src_nents; } else { - pdb->g_dma = sg_dma_address(req->src); + struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); + + pdb->g_dma = sg_dma_address(req_ctx->fixup_src); } if (edesc->dst_nents > 1) { @@ -474,7 +483,9 @@ static int set_rsa_priv_f2_pdb(struct akcipher_request *req, pdb->g_dma = edesc->sec4_sg_dma; sec4_sg_index += edesc->src_nents; } else { - pdb->g_dma = sg_dma_address(req->src); + struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); + + pdb->g_dma = sg_dma_address(req_ctx->fixup_src); } if (edesc->dst_nents > 1) { @@ -561,7 +572,9 @@ static int set_rsa_priv_f3_pdb(struct akcipher_request *req, pdb->g_dma = edesc->sec4_sg_dma; sec4_sg_index += edesc->src_nents; } else { - pdb->g_dma = sg_dma_address(req->src); + struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); + + pdb->g_dma = sg_dma_address(req_ctx->fixup_src); } if (edesc->dst_nents > 1) { diff --git a/drivers/crypto/caam/caampkc.h b/drivers/crypto/caam/caampkc.h index 5ac7201..2c488c9 100644 --- a/drivers/crypto/caam/caampkc.h +++ b/drivers/crypto/caam/caampkc.h @@ -95,14 +95,19 @@ struct caam_rsa_ctx { struct caam_rsa_key key; struct device *dev; dma_addr_t padding_dma; + }; /** * caam_rsa_req_ctx - per request context. - * @src: input scatterlist (stripped of leading zeros) + * @src : input scatterlist (stripped of leading zeros) + * @fixup_src : input scatterlist (that might be stripped of leading zeros) + * @fixup_src_len : length of the fixup_src input scatterlist */ struct caam_rsa_req_ctx { struct scatterlist src[2]; + struct scatterlist *fixup_src; + unsigned int fixup_src_len; }; /** -- 2.1.0