[PATCH v2 5/5] crypto: hisilicon/sec2 - restore iv for ctr mode

From: ZongYu Wu

Date: Thu May 28 2026 - 08:03:27 EST


From: Wenkai Lin <linwenkai6@xxxxxxxxxxxxx>

Upon termination of the CTR task, the initial vector (IV) is
restored to guarantee valid IV availability for subsequent tasks.

Fixes: 7b44c0eecd6a ("crypto: hisilicon/sec - add new skcipher mode for SEC")
Signed-off-by: Wenkai Lin <linwenkai6@xxxxxxxxxxxxx>
Signed-off-by: Zongyu Wu <wuzongyu1@xxxxxxxxxx>
---
drivers/crypto/hisilicon/sec2/sec_crypto.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/crypto/hisilicon/sec2/sec_crypto.c b/drivers/crypto/hisilicon/sec2/sec_crypto.c
index 901abe8761e5..03f873f68828 100644
--- a/drivers/crypto/hisilicon/sec2/sec_crypto.c
+++ b/drivers/crypto/hisilicon/sec2/sec_crypto.c
@@ -1970,6 +1970,7 @@ static int sec_request_init(struct sec_ctx *ctx, struct sec_req *req)

static int sec_process(struct sec_ctx *ctx, struct sec_req *req)
{
+ bool need_copy_iv = false;
int ret;

ret = sec_request_init(ctx, req);
@@ -1982,8 +1983,10 @@ static int sec_process(struct sec_ctx *ctx, struct sec_req *req)

/* Output IV as decrypto */
if (!req->c_req.encrypt && (ctx->c_ctx.c_mode == SEC_CMODE_CBC ||
- ctx->c_ctx.c_mode == SEC_CMODE_CTR))
+ ctx->c_ctx.c_mode == SEC_CMODE_CTR)) {
+ need_copy_iv = true;
sec_update_iv(req, ctx->alg_type);
+ }

ret = ctx->req_op->bd_send(ctx, req);
if (likely(ret == -EINPROGRESS || ret == -EBUSY))
@@ -1992,7 +1995,7 @@ static int sec_process(struct sec_ctx *ctx, struct sec_req *req)
dev_err_ratelimited(ctx->dev, "send sec request failed!\n");

/* As failing, restore the IV from user */
- if (ctx->c_ctx.c_mode == SEC_CMODE_CBC && !req->c_req.encrypt) {
+ if (need_copy_iv) {
if (ctx->alg_type == SEC_SKCIPHER)
memcpy(req->c_req.sk_req->iv, req->c_req.c_ivin,
ctx->c_ctx.ivsize);
--
2.33.0