linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Corentin Labbe <clabbe@baylibre.com>
To: herbert@gondor.apana.org.au, jernej.skrabec@gmail.com,
	samuel@sholland.org, wens@csie.org
Cc: linux-arm-kernel@lists.infradead.org,
	linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-sunxi@lists.linux.dev, Corentin Labbe <clabbe@baylibre.com>
Subject: [PATCH v2 03/19] crypto: sun4i-ss: do not allocate backup IV on requests
Date: Mon,  2 May 2022 20:19:13 +0000	[thread overview]
Message-ID: <20220502201929.843194-4-clabbe@baylibre.com> (raw)
In-Reply-To: <20220502201929.843194-1-clabbe@baylibre.com>

Instead of allocate memory on each requests, it is easier to
pre-allocate buffer for backup IV.
This made error path easier.
Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
---
 .../allwinner/sun4i-ss/sun4i-ss-cipher.c      | 22 +++++++------------
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h  |  1 +
 2 files changed, 9 insertions(+), 14 deletions(-)

diff --git a/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c b/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c
index 8dc2a475c601..a8c784acce13 100644
--- a/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c
+++ b/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c
@@ -20,7 +20,6 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 	unsigned int ivsize = crypto_skcipher_ivsize(tfm);
 	struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
 	u32 mode = ctx->mode;
-	void *backup_iv = NULL;
 	/* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
 	u32 rx_cnt = SS_RX_DEFAULT;
 	u32 tx_cnt = 0;
@@ -47,10 +46,8 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 	}
 
 	if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {
-		backup_iv = kzalloc(ivsize, GFP_KERNEL);
-		if (!backup_iv)
-			return -ENOMEM;
-		scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
+		scatterwalk_map_and_copy(ctx->backup_iv, areq->src,
+					 areq->cryptlen - ivsize, ivsize, 0);
 	}
 
 	if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {
@@ -133,8 +130,8 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 
 	if (areq->iv) {
 		if (mode & SS_DECRYPTION) {
-			memcpy(areq->iv, backup_iv, ivsize);
-			kfree_sensitive(backup_iv);
+			memcpy(areq->iv, ctx->backup_iv, ivsize);
+			memzero_explicit(ctx->backup_iv, ivsize);
 		} else {
 			scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,
 						 ivsize, 0);
@@ -198,7 +195,6 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 	unsigned int ileft = areq->cryptlen;
 	unsigned int oleft = areq->cryptlen;
 	unsigned int todo;
-	void *backup_iv = NULL;
 	struct sg_mapping_iter mi, mo;
 	unsigned long pi = 0, po = 0; /* progress for in and out */
 	bool miter_err;
@@ -242,10 +238,8 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 		return sun4i_ss_cipher_poll_fallback(areq);
 
 	if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {
-		backup_iv = kzalloc(ivsize, GFP_KERNEL);
-		if (!backup_iv)
-			return -ENOMEM;
-		scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
+		scatterwalk_map_and_copy(ctx->backup_iv, areq->src,
+					 areq->cryptlen - ivsize, ivsize, 0);
 	}
 
 	if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {
@@ -382,8 +376,8 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 	}
 	if (areq->iv) {
 		if (mode & SS_DECRYPTION) {
-			memcpy(areq->iv, backup_iv, ivsize);
-			kfree_sensitive(backup_iv);
+			memcpy(areq->iv, ctx->backup_iv, ivsize);
+			memzero_explicit(ctx->backup_iv, ivsize);
 		} else {
 			scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,
 						 ivsize, 0);
diff --git a/drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h b/drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h
index 0fee6f4e2d90..ba59c7a48825 100644
--- a/drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h
+++ b/drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h
@@ -183,6 +183,7 @@ struct sun4i_tfm_ctx {
 
 struct sun4i_cipher_req_ctx {
 	u32 mode;
+	u8 backup_iv[AES_BLOCK_SIZE];
 	struct skcipher_request fallback_req;   // keep at the end
 };
 
-- 
2.35.1


  parent reply	other threads:[~2022-05-02 20:20 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-02 20:19 [PATCH v2 00/19] crypto: allwinner: lots of fixes Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 01/19] crypto: sun8i-ce: Fix minor style issue Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 02/19] crypto: sun8i-ce: do not allocate memory when handling requests Corentin Labbe
2022-05-02 20:19 ` Corentin Labbe [this message]
2022-05-02 20:19 ` [PATCH v2 04/19] crypto: sun8i-ss: rework handling of IV Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 05/19] crypto: sun8i-ss: handle zero sized sg Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 06/19] crypto: sun8i-ss: remove redundant test Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 07/19] crypto: sun8i-ss: test error before assigning Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 08/19] crypto: sun8i-ss: use sg_nents_for_len Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 09/19] crypto: sun8i-ss: do not allocate memory when handling hash requests Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 10/19] crypto: sun8i-ss: do not zeroize all pad Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 11/19] crypto: sun8i-ss: handle requests if last block is not modulo 64 Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 12/19] crypto: sun8i-ss: rework debugging Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 13/19] crypto: sun8i-ss: Add function for handling hash padding Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 14/19] crypto: sun8i-ss: add hmac(sha1) Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 15/19] crypto: sun8i-ss: do not fallback if cryptlen is less than sg length Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 16/19] crypto: sun8i-ce: Add function for handling hash padding Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 17/19] crypto: sun8i-ce: use sg_nents_for_len Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 18/19] crypto: sun8i-ce: rework debugging Corentin Labbe
2022-05-02 20:19 ` [PATCH v2 19/19] crypto: sun8i-ce: do not fallback if cryptlen is less than sg length Corentin Labbe
2022-05-13  9:34 ` [PATCH v2 00/19] crypto: allwinner: lots of fixes Herbert Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220502201929.843194-4-clabbe@baylibre.com \
    --to=clabbe@baylibre.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=jernej.skrabec@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sunxi@lists.linux.dev \
    --cc=samuel@sholland.org \
    --cc=wens@csie.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).