Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp551843rwb; Tue, 27 Sep 2022 00:58:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4ZHT3AakzuE0JRSsu0TUEx+bCaCNKevF8mtxAftsncZgHZiStrl7n25JSaJm85YwHUaIlw X-Received: by 2002:a63:4408:0:b0:439:befa:3d47 with SMTP id r8-20020a634408000000b00439befa3d47mr22714389pga.64.1664265500138; Tue, 27 Sep 2022 00:58:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664265500; cv=none; d=google.com; s=arc-20160816; b=pxvAsF3y6oiygWaSFayg6nsVNFMNxM+M9rU+R6l9kF7FeUNdWPBDTJEMANGW18Z5eh r1zq187DRJ0TWVwyP7g8uf9b+9EdjTs1ZvphrKSqrpYx8iYeZLQln+8psbc0ILq9SU3b 5jNN5Hxm73TGvCrbjPq5f6xJZnz+396pnuWtb1fEWJ1FEFsthSo2TfqmW2zbNA5ifHNx 24UIrqwqGOeAWB9Oba5QmXMQ3n9DS1WZ5v9rfM9goTWUW91mCsMsuqXESbFHrTKcaFWM ooyyWTrlg28hZaeYZfzoPmdCwGD2g4fRDRPGvzZerMLeGBA4JFgYQUKHJjX1mUVJuCKt jZCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=LTH5XZUoNfw5j5g91Ov715oBhaysSZASayzIPMZPNtU=; b=N50tERWBqnqe5KwmwKeVcoAfV2ab5lGSUn+TyK2S184rVO7GNogFtNdoVvKfi5E/FB wenTfrPh1C2i9aZrVFOfdzK7hC+ovvxccuA2l8HN2eJyrdwUrNu19Xg1BnXZjC+2+l0I uHXKJRvKS5hjuee/2ZPpyTYmHNqDnLiwF8iWWBAR50kqZsp9dAsry6mJkySEUhd9NVM1 Qya4LBFvkRA8QcKRKTbC/9m0ol93fOd9xv1p8nkCWQ46YlvUaAFz7P5RPVTeuUPDB5mQ GVxhtSDGzdlmq8U1z4C2zoRj95gv/hja7feYlY/0hU3p/9kMrAQLda+xJvbwEUt8WM2R 56Jw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20210112.gappssmtp.com header.s=20210112 header.b=lDy9N5u3; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s9-20020a632c09000000b0043f06af782esi738861pgs.304.2022.09.27.00.58.08; Tue, 27 Sep 2022 00:58:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20210112.gappssmtp.com header.s=20210112 header.b=lDy9N5u3; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231437AbiI0H4X (ORCPT + 99 others); Tue, 27 Sep 2022 03:56:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231360AbiI0Hz5 (ORCPT ); Tue, 27 Sep 2022 03:55:57 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E86ADAB4DF for ; Tue, 27 Sep 2022 00:55:43 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id d12-20020a05600c3acc00b003b4c12e47f3so4952782wms.4 for ; Tue, 27 Sep 2022 00:55:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=LTH5XZUoNfw5j5g91Ov715oBhaysSZASayzIPMZPNtU=; b=lDy9N5u3q0mT2cwLyphmqftoyVJuKq0rsENhPUgJwacRs225h0DKbzz8pao3e47XSU WTyoES77IkrhL3tcwXG7a4+LU3TBU7wvhAx/Ca+M8zYS2SdPJoGkEW+UzlbQQWqsI5RF jaap4fCW4O5EmpzV0Ycb4x/4ez5tGA+oIpSWpsQqP35K3OaBvaiq4VzkthCUxuGXxiz6 iNp742QOPH9/geZOxwN5QoADGNUOgIbZjJWOYk21B2UvymXznhnW4wWy7Tw1qqAbDZjp Wy2wen51FkD4h/QvbW/tjQQeMZottNX4te6fLlKXApMbznNgfvpDNRt5PyBTIBgzfDyx CLlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=LTH5XZUoNfw5j5g91Ov715oBhaysSZASayzIPMZPNtU=; b=jU8sQTbWLGpGpGTmv09+VXgLGwbBjCC/N8Anjqn0VFquVWiJrVmF1O0414Hh9lbz0H jDmUA6MfzpLUA04+Lhe78ApcgcDdLRPPk5yUhkhvztK7N8Ain2lPJ8am5zpPxPQJlXiJ 0NH0uhXeQXq4h29Q7yrraGDm00xNIKfZww46Ryc8mVrJu7+r238Td7nkfjYIZ0IFIwpQ S88/x7wrfudp/8UjQRI/Vm5Jefnr2/GJY6pD/yLs01ji+DaL+ivdpcKIozLD0nAEuaW7 iOaYD7n8zZ5xFcTNnYKUqqhZ7aqMx5x2CmJMPGFEJdWEoanJdC3dR+bC5Ki9JBv6kYso 3TYQ== X-Gm-Message-State: ACrzQf0/y/IqDBlB5wr2EtH5RNrZBTAK0UIdNExjDhIstUl8MmMm9dqR IsVgcBlPq5i5D/rij5Cntn8AGA== X-Received: by 2002:a05:600c:4256:b0:3b4:7cfc:a626 with SMTP id r22-20020a05600c425600b003b47cfca626mr1643360wmm.187.1664265341998; Tue, 27 Sep 2022 00:55:41 -0700 (PDT) Received: from localhost.localdomain (laubervilliers-658-1-213-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.googlemail.com with ESMTPSA id x8-20020adfdcc8000000b0022afbd02c69sm1076654wrm.56.2022.09.27.00.55.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Sep 2022 00:55:41 -0700 (PDT) From: Corentin Labbe To: heiko@sntech.de, ardb@kernel.org, davem@davemloft.net, herbert@gondor.apana.org.au, krzysztof.kozlowski+dt@linaro.org, mturquette@baylibre.com, robh+dt@kernel.org, sboyd@kernel.org Cc: linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org, linux-clk@vger.kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rockchip@lists.infradead.org, Corentin Labbe , John Keeping Subject: [PATCH v10 08/33] crypto: rockchip: better handle cipher key Date: Tue, 27 Sep 2022 07:54:46 +0000 Message-Id: <20220927075511.3147847-9-clabbe@baylibre.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220927075511.3147847-1-clabbe@baylibre.com> References: <20220927075511.3147847-1-clabbe@baylibre.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The key should not be set in hardware too much in advance, this will fail it 2 TFM with different keys generate alternative requests. The key should be stored and used just before doing cipher operations. Fixes: ce0183cb6464b ("crypto: rockchip - switch to skcipher API") Reviewed-by: John Keeping Signed-off-by: Corentin Labbe --- drivers/crypto/rockchip/rk3288_crypto.h | 1 + drivers/crypto/rockchip/rk3288_crypto_skcipher.c | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/crypto/rockchip/rk3288_crypto.h b/drivers/crypto/rockchip/rk3288_crypto.h index 8b1e15d8ddc6..540b81a14b9b 100644 --- a/drivers/crypto/rockchip/rk3288_crypto.h +++ b/drivers/crypto/rockchip/rk3288_crypto.h @@ -245,6 +245,7 @@ struct rk_ahash_rctx { struct rk_cipher_ctx { struct rk_crypto_info *dev; unsigned int keylen; + u8 key[AES_MAX_KEY_SIZE]; u8 iv[AES_BLOCK_SIZE]; struct crypto_skcipher *fallback_tfm; }; diff --git a/drivers/crypto/rockchip/rk3288_crypto_skcipher.c b/drivers/crypto/rockchip/rk3288_crypto_skcipher.c index eac5bba66e25..1ef94f8db2c5 100644 --- a/drivers/crypto/rockchip/rk3288_crypto_skcipher.c +++ b/drivers/crypto/rockchip/rk3288_crypto_skcipher.c @@ -95,7 +95,7 @@ static int rk_aes_setkey(struct crypto_skcipher *cipher, keylen != AES_KEYSIZE_256) return -EINVAL; ctx->keylen = keylen; - memcpy_toio(ctx->dev->reg + RK_CRYPTO_AES_KEY_0, key, keylen); + memcpy(ctx->key, key, keylen); return crypto_skcipher_setkey(ctx->fallback_tfm, key, keylen); } @@ -111,7 +111,7 @@ static int rk_des_setkey(struct crypto_skcipher *cipher, return err; ctx->keylen = keylen; - memcpy_toio(ctx->dev->reg + RK_CRYPTO_TDES_KEY1_0, key, keylen); + memcpy(ctx->key, key, keylen); return crypto_skcipher_setkey(ctx->fallback_tfm, key, keylen); } @@ -127,7 +127,8 @@ static int rk_tdes_setkey(struct crypto_skcipher *cipher, return err; ctx->keylen = keylen; - memcpy_toio(ctx->dev->reg + RK_CRYPTO_TDES_KEY1_0, key, keylen); + memcpy(ctx->key, key, keylen); + return crypto_skcipher_setkey(ctx->fallback_tfm, key, keylen); } @@ -283,6 +284,7 @@ static void rk_ablk_hw_init(struct rk_crypto_info *dev) RK_CRYPTO_TDES_BYTESWAP_IV; CRYPTO_WRITE(dev, RK_CRYPTO_TDES_CTRL, rctx->mode); memcpy_toio(dev->reg + RK_CRYPTO_TDES_IV_0, req->iv, ivsize); + memcpy_toio(ctx->dev->reg + RK_CRYPTO_TDES_KEY1_0, ctx->key, ctx->keylen); conf_reg = RK_CRYPTO_DESSEL; } else { rctx->mode |= RK_CRYPTO_AES_FIFO_MODE | @@ -295,6 +297,7 @@ static void rk_ablk_hw_init(struct rk_crypto_info *dev) rctx->mode |= RK_CRYPTO_AES_256BIT_key; CRYPTO_WRITE(dev, RK_CRYPTO_AES_CTRL, rctx->mode); memcpy_toio(dev->reg + RK_CRYPTO_AES_IV_0, req->iv, ivsize); + memcpy_toio(ctx->dev->reg + RK_CRYPTO_AES_KEY_0, ctx->key, ctx->keylen); } conf_reg |= RK_CRYPTO_BYTESWAP_BTFIFO | RK_CRYPTO_BYTESWAP_BRFIFO; @@ -484,6 +487,7 @@ static void rk_ablk_exit_tfm(struct crypto_skcipher *tfm) { struct rk_cipher_ctx *ctx = crypto_skcipher_ctx(tfm); + memzero_explicit(ctx->key, ctx->keylen); free_page((unsigned long)ctx->dev->addr_vir); crypto_free_skcipher(ctx->fallback_tfm); } -- 2.35.1