Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2849832pxb; Mon, 4 Apr 2022 00:06:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwBZDaBrybCndYJPryTLFqPQvGVR+k4RY/4QIervCT/7QReMYBa5hSjxZYmI4+dHQExtbjF X-Received: by 2002:a05:6a00:2403:b0:4fd:e84a:4563 with SMTP id z3-20020a056a00240300b004fde84a4563mr8438494pfh.60.1649055995668; Mon, 04 Apr 2022 00:06:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649055995; cv=none; d=google.com; s=arc-20160816; b=Cx0k+XiEimDFjQEXHFMT8G8aLYwHdOr1RlCHjzILzngFspc0jeZTo9A0KHwMXQSKmO CZSckz5xprXFLqw3RxSvvPNkeUnRkm5YRYdZlst9r0MP+oWiRWZfTU6jLAbYfvLriOMr EUFDW0GGtcSMHLWSS7cMnympIdk9Tjm7XT0bLHLO54pqC7R79hjG2HMdJ6lcTkL/DX/K fCVW8X7RKe4ZGpDjEnpfcR1BFA9n7MfFHPJgLlcHciFO78B7gL/GHvL0lrXuNqBM20WS xcIAjXvjStpRWI8Y8CssIgEDIHU+lXzrz9QebXbxwzg6f6jCKP7DVojFW4rzO6NKbM9f yH3A== 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=V+7KNurryNyAJClOH+iG6roylpIcQjX+XN2mzXKtw2Y=; b=SS+wGmhtA2O05nx0GCsCXfSHjGS22h9P/dGb33vJpAHmzyYj5Ez8Enj1SBfjRdjkL7 yfG+c8QazDP3REGu2s+42CJXT5zphv7aBpK0dmKAZMtwgvJ54zk+Sqvuy59WETft77zf 7ULoEUYnzfkIST6P5WOZZCyRIapaipVzmdfTPLBFSr4z8LQ0qIAHe7achDeT0KSi3YY+ E4R5ul/J7yv9c8tey1Z4q2IprVDx4I85U5rYdGIy+Cf2iguapPi23RYH8xhWzPDq+18u aDgHr1GMbIjNkSXSNSseCbIX9cwXcrvThXO5EcafHmGHtGDQT6EODswPENabZn/V977e ZQbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20210112.gappssmtp.com header.s=20210112 header.b=AVUH0oDl; 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 kk11-20020a17090b4a0b00b001bd14e030e4si9112922pjb.188.2022.04.04.00.06.04; Mon, 04 Apr 2022 00:06:35 -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=AVUH0oDl; 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 S1351978AbiDAUU1 (ORCPT + 99 others); Fri, 1 Apr 2022 16:20:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352002AbiDAUUW (ORCPT ); Fri, 1 Apr 2022 16:20:22 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0BB5B98 for ; Fri, 1 Apr 2022 13:18:18 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id r7so2426612wmq.2 for ; Fri, 01 Apr 2022 13:18:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=V+7KNurryNyAJClOH+iG6roylpIcQjX+XN2mzXKtw2Y=; b=AVUH0oDlWjvPJIa3QazfvV/LG9kR2GMXH1677fU+OsNtQ7/wk3NkzZ7wzwAVjhAVtO pB9mG7HL0g0oVD6oS8njtnKSjVwN6Mle0MgC48J8xG325O5wDZG7g+QT7qor3rF5loej 1Al2wIYyvcC5sP871VOVxuNa8ZLL1P+2hymRTWCi+25fvM18qIo8zgK7xKg72NdhYJnO wnxmBPsvdFvluXVnAPGB/zHR+sB4IlYzOR35s1q9OauXlsI7CVWG4jFkMxt3hTyfvXU9 mPp3qki7pXAWWB1ZOwxb5bp1SgSC29XfbrjmGtzrBlCYFHwmfWSPHp74yLEdGcP4SXpo DgvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=V+7KNurryNyAJClOH+iG6roylpIcQjX+XN2mzXKtw2Y=; b=wVCotpftWBUZSu1/prfzNcwC3+ZZAXYk4t2zgXiaG7SNHDUSgo8GkG1CvVHheF2Fpy LvbIcA7VjUC8iucy7+PzWxHbMz4kvkuNXJM41z1DiF6yBhuU0CQ2FaqBCnZzPwda97nx ZcE9DFh1vqsp+FN1Qt3Rd1k6lTlz3wJnzkQyVmTrFjZXOM/0XyJR134jRcb0gPcSXpeh KazGjPE/Y02Slc3563zJjdxfFL3mDUMHgF8WTjt1UFLl6DUeruBW8sWGj4P6+IkPBh3S BM/3R6gQifHVUKG6Q/wxhQ8kG6fj0jmd9d18Y/o5/KP4jvEQT09OhCuJzbnjBTrwGEH8 2Kqg== X-Gm-Message-State: AOAM533OIFttPaCQ966C5FlWbjDRJqq+WM3IyhlvARf47hp+R70Z7/ue DHBtZTuLY715Sv4hOSX4ugUZXQ== X-Received: by 2002:a7b:c40f:0:b0:389:f3ad:5166 with SMTP id k15-20020a7bc40f000000b00389f3ad5166mr9949057wmi.63.1648844296777; Fri, 01 Apr 2022 13:18:16 -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 j16-20020a05600c191000b0038ca3500494sm17823838wmq.27.2022.04.01.13.18.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Apr 2022 13:18:16 -0700 (PDT) From: Corentin Labbe To: heiko@sntech.de, herbert@gondor.apana.org.au, krzk+dt@kernel.org, robh+dt@kernel.org Cc: devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rockchip@lists.infradead.org, Corentin Labbe Subject: [PATCH v4 08/33] crypto: rockchip: better handle cipher key Date: Fri, 1 Apr 2022 20:17:39 +0000 Message-Id: <20220401201804.2867154-9-clabbe@baylibre.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220401201804.2867154-1-clabbe@baylibre.com> References: <20220401201804.2867154-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, T_SCC_BODY_TEXT_LINE 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") 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..826508e4a0c3 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; + u32 key[AES_MAX_KEY_SIZE / 4]; 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 c6b601086c04..5a9ba7c0206d 100644 --- a/drivers/crypto/rockchip/rk3288_crypto_skcipher.c +++ b/drivers/crypto/rockchip/rk3288_crypto_skcipher.c @@ -103,7 +103,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); } @@ -119,7 +119,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); } @@ -135,7 +135,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); } @@ -291,6 +292,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 | @@ -303,6 +305,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; @@ -492,6 +495,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