Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3697015pxb; Mon, 24 Jan 2022 15:38:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJzb386xCSfj36smsEQl3aPMXb/TBdxg1ms48+3wSOfU2mwO6Z+IZ8XkXVPtPqXmPgyZt7gD X-Received: by 2002:a17:903:300c:b0:14b:61:b1a9 with SMTP id o12-20020a170903300c00b0014b0061b1a9mr16479799pla.137.1643067537267; Mon, 24 Jan 2022 15:38:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643067537; cv=none; d=google.com; s=arc-20160816; b=N5AmVY6p0vtklpYkvHLdzrrbPQcoQ7lAw++5JlaG/tVu3Uea4/eieKRXBONPrlmEkO EEPoMJGCebQvilu3Uo83fiyIY5oR5TrG0PCJipmmVyBGTIsAGHf+kaLcy6RCtTUKKnTY 3sDaA1dTUM9DTMeM/+xQG59AkGANZ7uSl8YpRJLYKzd0cGtI80TZsqD5yFbTkpK64NP8 kwOnoUGZI6KMA3tbek2IhbMtqpSgxys1lECERbpXayG9t9aAo4Hkjf3kuEq+yJ+/I3rU oqJTBLwI/5MTBYH1IDgS1i19LVkCzKCHkaS5yZczrPp+TTq3r//yzb52a3e7YddDiqho doOA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=gnpGP8X8i7tZpBj4b21v28S6gXYSpgF7/BbaYn4Zmdo=; b=Irx9CH7nm0hqs8lg8fM7WZuis1zBstwLnjxxh9wZQ8GJ7h3tclLiB1g81nnqrbd3v4 gK4YYyX8bdEOYkOimCYvWB9Csx6+hM4dbTTjAWcl9v1m+FGEXKxssgJhxSm/K9s+4Pci FCSzM2OXbXKmAbhnQ/hgCR4Bm/g3dej2OtyP4Iit0yMrxDHZR2/NBxX/saoMyTAfeTiI lnXBJEopzJHeVG8m2I8cJ2GdsE3ipcO9g+i6DyCuSpa0oI4VDtcZWGdroTTSGO/MM+UN OUoLNjMS1lpwCgQKgVlKNiv4o8uTiPVaR1rfvlj4NQX0L0U5o36jjo9NOwTdnRbRD3/8 ZCKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jH6hmUVD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s192si6353937pgs.424.2022.01.24.15.38.45; Mon, 24 Jan 2022 15:38:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jH6hmUVD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2359804AbiAXXe7 (ORCPT + 99 others); Mon, 24 Jan 2022 18:34:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1838084AbiAXWpw (ORCPT ); Mon, 24 Jan 2022 17:45:52 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF01EC0550F3; Mon, 24 Jan 2022 13:06:22 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 3A796B81142; Mon, 24 Jan 2022 21:06:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A4F4C340E8; Mon, 24 Jan 2022 21:06:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643058380; bh=f21MzGFvooadYexLUcwxb1T6t4IyhWjyJE0ILpkgpd8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jH6hmUVDhaWDu7YwmiQg+VHVb1c7XtnKVGbc/arLMIRuf+5/bJErMC324d3rfwlrj QjARalrp/S6m8nvVieTNArxCcErgfLT6OXF8Az6HPPiHrYIWRwnDa1o6WdYvNkGCLC rMTf9uC45wpcvKICEeBenvtve1SqYEzOzOlgsDKo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nicolas Toromanoff , Herbert Xu , Sasha Levin Subject: [PATCH 5.16 0271/1039] crypto: stm32/cryp - fix CTR counter carry Date: Mon, 24 Jan 2022 19:34:20 +0100 Message-Id: <20220124184134.410347685@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184125.121143506@linuxfoundation.org> References: <20220124184125.121143506@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nicolas Toromanoff [ Upstream commit 41c76690b0990efacd15d35cfb4e77318cd80ebb ] STM32 CRYP hardware doesn't manage CTR counter bigger than max U32, as a workaround, at each block the current IV is saved, if the saved IV lower u32 is 0xFFFFFFFF, the full IV is manually incremented, and set in hardware. Fixes: bbb2832620ac ("crypto: stm32 - Fix sparse warnings") Signed-off-by: Nicolas Toromanoff Signed-off-by: Herbert Xu Signed-off-by: Sasha Levin --- drivers/crypto/stm32/stm32-cryp.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/drivers/crypto/stm32/stm32-cryp.c b/drivers/crypto/stm32/stm32-cryp.c index 7389a0536ff02..d13b262b36252 100644 --- a/drivers/crypto/stm32/stm32-cryp.c +++ b/drivers/crypto/stm32/stm32-cryp.c @@ -163,7 +163,7 @@ struct stm32_cryp { struct scatter_walk in_walk; struct scatter_walk out_walk; - u32 last_ctr[4]; + __be32 last_ctr[4]; u32 gcm_ctr; }; @@ -1217,27 +1217,26 @@ static void stm32_cryp_check_ctr_counter(struct stm32_cryp *cryp) { u32 cr; - if (unlikely(cryp->last_ctr[3] == 0xFFFFFFFF)) { - cryp->last_ctr[3] = 0; - cryp->last_ctr[2]++; - if (!cryp->last_ctr[2]) { - cryp->last_ctr[1]++; - if (!cryp->last_ctr[1]) - cryp->last_ctr[0]++; - } + if (unlikely(cryp->last_ctr[3] == cpu_to_be32(0xFFFFFFFF))) { + /* + * In this case, we need to increment manually the ctr counter, + * as HW doesn't handle the U32 carry. + */ + crypto_inc((u8 *)cryp->last_ctr, sizeof(cryp->last_ctr)); cr = stm32_cryp_read(cryp, CRYP_CR); stm32_cryp_write(cryp, CRYP_CR, cr & ~CR_CRYPEN); - stm32_cryp_hw_write_iv(cryp, (__be32 *)cryp->last_ctr); + stm32_cryp_hw_write_iv(cryp, cryp->last_ctr); stm32_cryp_write(cryp, CRYP_CR, cr); } - cryp->last_ctr[0] = stm32_cryp_read(cryp, CRYP_IV0LR); - cryp->last_ctr[1] = stm32_cryp_read(cryp, CRYP_IV0RR); - cryp->last_ctr[2] = stm32_cryp_read(cryp, CRYP_IV1LR); - cryp->last_ctr[3] = stm32_cryp_read(cryp, CRYP_IV1RR); + /* The IV registers are BE */ + cryp->last_ctr[0] = cpu_to_be32(stm32_cryp_read(cryp, CRYP_IV0LR)); + cryp->last_ctr[1] = cpu_to_be32(stm32_cryp_read(cryp, CRYP_IV0RR)); + cryp->last_ctr[2] = cpu_to_be32(stm32_cryp_read(cryp, CRYP_IV1LR)); + cryp->last_ctr[3] = cpu_to_be32(stm32_cryp_read(cryp, CRYP_IV1RR)); } static bool stm32_cryp_irq_read_data(struct stm32_cryp *cryp) -- 2.34.1