Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2287413pxp; Mon, 21 Mar 2022 15:58:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx3JYIStyXiBQcIgWlVSBQWtoDERJvZ+zEBUtwTphWY8fsl5HtzLX+th+4vpaQC4FWWivLS X-Received: by 2002:a63:d316:0:b0:380:85b6:d595 with SMTP id b22-20020a63d316000000b0038085b6d595mr19917492pgg.0.1647903485721; Mon, 21 Mar 2022 15:58:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647903485; cv=none; d=google.com; s=arc-20160816; b=x+G5xU0R9Oz8WqAW6xqoPdBrcCeprnYNtqq7jAPpr3jckhEauY9e0+9VY9NwYbCTL8 aBEgRUUmAohSXJaHZT+CozCABr7mO2ZzZTXDljNgJHIt9G7zSEH0oaFCvy0YuthcWZTS QN2K7AT/i4v7iMCRNEuYLPp3kR8rJHWUsR2wTEqMW0ROrkNjnDhq54cR/sN9Ik3/a6ER 0/Kd9jSDW2QkwR5lOvuKKMhVDSQQzSMAca35aOF4ca5z46nn3JTP0GEErIcnvSQgquER JUHoHtA9xOQ6K2EgAD90N3fKlkFd30YBVpSPLO+SbIje+9VqWcOFiZ54YhLb/1pW2hyD PoCA== 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=ci0efpCMY9zTNuOVgDKNG91fdIJJ0F69qN8X/Q/Dc4g=; b=wteRQAJ1SyGtZw4qkb0nSrLMj5qsl7esnHtvv68Xo1law5/giaBBX/GwsNEY8XKXhc 7yq2/cYxr4hx4Aq/Uai+p5/4gWUKLGxwL9pew54Cn18+Eyr9Dg4btqnX++Krla6U3LRA 3oDa8btY2J+Mx3wa9QH94CYVivFg0ejZ+Bj6SIrbKf8yuHlH2XwSNg8IjkHtIB1gHgiC Mf2FZ7w/9eg4fYoXawhG2mrpJ/95QAnhYQEeZ3hBWkdx1B8D/UdAWnDs8kQijIpKcvgE fj70TzFVodNSIoBUym2d1mU12P8N1Vr/uyH5fJJ0Fmpdq+AKp29U0akggScHQc5Cdr3n dXzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20210112.gappssmtp.com header.s=20210112 header.b=fUlJScin; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id e11-20020a63e00b000000b003816043ef2asi14468884pgh.287.2022.03.21.15.58.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 15:58:05 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20210112.gappssmtp.com header.s=20210112 header.b=fUlJScin; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A5FFD366E6D; Mon, 21 Mar 2022 15:10:37 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353106AbiCUUKU (ORCPT + 99 others); Mon, 21 Mar 2022 16:10:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352954AbiCUUJi (ORCPT ); Mon, 21 Mar 2022 16:09:38 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01E9C104297 for ; Mon, 21 Mar 2022 13:07:57 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id r10so22253024wrp.3 for ; Mon, 21 Mar 2022 13:07:56 -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=ci0efpCMY9zTNuOVgDKNG91fdIJJ0F69qN8X/Q/Dc4g=; b=fUlJScinxALBEKGsxDRADRumA9/X76r4caxuw7MfkLlPHpxvicP7Z1x2y7CJtV7tAk 08HcMfjN3qx8ha5ErVUA1JEOtg/wO8FK8wwK7RdaKWzmaeCVBqZHmJCoQcpldbXU5v+Y HPD7qSmBVrpx95StbfZPDTznrB7TN11f5XqoJF/C8HaOmV2OJvOoOlVCuDLVaewm2NSq BXj62hwdSLYjxudkNPULA9U1JzjvuIrXv0tDOv/I6hz33ncEfU0anwuJC9Sl8qW7k6+B Cx5w97iId9pK6D3w1E+GFB/A+75c/k2IA7NHnwUkj3O1lRgO8wCG8JdSXQ0KwvMHMn3d UVhA== 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=ci0efpCMY9zTNuOVgDKNG91fdIJJ0F69qN8X/Q/Dc4g=; b=wm3dTVMwzYJk2SwPkErdPYfo18a8VP9yzWyIcRWMNiVZ9tWs8Bp+xyTU1uooWQsdbF AM0jZQLldW7M9qdS5ZVsWY+zmgJkX1Bdy1raflV6boWCbGebBOM26jSXFzBTq1Ii/P7z uOf0bObTspN3jFMlUl5/ZuLbsAXSl/ek+ofR+hUP4Vg8YchNIfpF43D780VBt6MEHmF9 tQHc2rqoH3NmgR0eLk4lhS2s4WhJfxXN+bPbVc5pgk+uHrvFQLeITW/GOUaVA9A5MH+d yFQedwIF6N2LrprRfuzG13I+CNvPxkU7/HUfHQpfhESQD5fBlFzBsjxHiAaI7XCMqz+Q l3Lw== X-Gm-Message-State: AOAM532e6IbSea2XGl/n0O7dLzn2RhhJD0N/bBxruJ3lp9pJG124aIbG bQFC+dqXfKVt2vAKx826TKRCbA== X-Received: by 2002:a5d:6d8b:0:b0:203:f9b4:be1d with SMTP id l11-20020a5d6d8b000000b00203f9b4be1dmr13153556wrs.298.1647893275492; Mon, 21 Mar 2022 13:07:55 -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 i14-20020a0560001ace00b00203da1fa749sm24426988wry.72.2022.03.21.13.07.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 13:07:55 -0700 (PDT) From: Corentin Labbe To: heiko@sntech.de, herbert@gondor.apana.org.au, krzk+dt@kernel.org, mturquette@baylibre.com, robh+dt@kernel.org, sboyd@kernel.org Cc: devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-clk@vger.kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rockchip@lists.infradead.org, Corentin Labbe Subject: [PATCH v3 08/26] crypto: rockchip: better handle cipher key Date: Mon, 21 Mar 2022 20:07:21 +0000 Message-Id: <20220321200739.3572792-9-clabbe@baylibre.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220321200739.3572792-1-clabbe@baylibre.com> References: <20220321200739.3572792-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,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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-kernel@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.34.1