From patchwork Wed Oct 12 16:09:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomas Marek X-Patchwork-Id: 614907 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47BA2C4332F for ; Wed, 12 Oct 2022 16:22:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229978AbiJLQWh (ORCPT ); Wed, 12 Oct 2022 12:22:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230008AbiJLQWb (ORCPT ); Wed, 12 Oct 2022 12:22:31 -0400 X-Greylist: delayed 632 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Wed, 12 Oct 2022 09:22:20 PDT Received: from smtp.cesky-hosting.cz (smtp.cesky-hosting.cz [IPv6:2a00:1ed0:2:0:1:5bef:c8ee:1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A12CFDED21; Wed, 12 Oct 2022 09:22:20 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at smtp.cesky-hosting.cz DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=elrest.cz; s=rampa2-202208; t=1665591114; bh=Kxemadi69pFkVKEwR5Ct+aURnZswpdi+grRKg9XSUXM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bAJz+2sz2IMgY6tsPsXGxVq5bdbCyOqe0lEq1PgQ5u3JnITtPyURer2dO+loY7E5x ES28+H560S8Lw3Yr6erJ9FdbqY8sQckLpc2YuRtsT+J911nqp+ERQ3aFJPGDP/oXg7 4jvGrefQ1rnZSwsi6lJYW4AC8ZIQsdPeVv2huIXYWEEL8SOV6M67z+9dm5QpgePIUZ 6/uzYAvHawwfnfHHQmMEFbrDKJFnem3we8VmIab58vwiJL46zo0WjDDbprT6IdSQ8y jWjub3fTeVjqu6L2U/u49zsdHpy1qBqP9VmtcTcvrt6cbQEiVT/B3ztfQgLX+yyu/M 9D3XpiQJPxiqw== X-Thin-Conversation: conversation Received: from localhost.localdomain (unknown [5.181.92.50]) (Authenticated sender: tomas.marek@elrest.cz) by smtp.cesky-hosting.cz (Postfix) with ESMTPSA id 81D451498; Wed, 12 Oct 2022 18:11:54 +0200 (CEST) From: Tomas Marek To: mpm@selenic.com, herbert@gondor.apana.org.au Cc: mcoquelin.stm32@gmail.com, linux-arm-kernel@lists.infradead.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, alexandre.torgue@foss.st.com, oleg.karfich@wago.com, Tomas Marek Subject: [PATCH 1/2] hwrng: stm32 - fix number of returned bytes on read Date: Wed, 12 Oct 2022 18:09:23 +0200 Message-Id: <20221012160924.12226-2-tomas.marek@elrest.cz> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221012160924.12226-1-tomas.marek@elrest.cz> References: <20221012160924.12226-1-tomas.marek@elrest.cz> Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The stm32_rng_read() function uses `retval` variable as a counter of generated random bytes. However, the same variable is used to store a result of the polling function in case the driver is waiting until the TRNG is ready. The TRNG generates random numbers by 16B. One loop read 4B. So, the function calls the polling every 16B, i.e. every 4th loop. The `retval` counter is reset on poll call and only number of bytes read after the last poll call is returned to the caller. The remaining sampled random bytes (for example 48 out of 64 in case 64 bytes are read) are not used. Use different variable to store the polling function result and do not overwrite `retval` counter. Cc: Oleg Karfich Signed-off-by: Tomas Marek --- drivers/char/hw_random/stm32-rng.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/char/hw_random/stm32-rng.c b/drivers/char/hw_random/stm32-rng.c index bc22178f83e8..8eaacefd498b 100644 --- a/drivers/char/hw_random/stm32-rng.c +++ b/drivers/char/hw_random/stm32-rng.c @@ -49,11 +49,13 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) /* Manage timeout which is based on timer and take */ /* care of initial delay time when enabling rng */ if (!sr && wait) { - retval = readl_relaxed_poll_timeout_atomic(priv->base + int ret; + + ret = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, sr, sr, 10, 50000); - if (retval) + if (ret) dev_err((struct device *)priv->rng.priv, "%s: timeout %x!\n", __func__, sr); } From patchwork Wed Oct 12 16:09:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomas Marek X-Patchwork-Id: 614620 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 88207C433FE for ; Wed, 12 Oct 2022 16:22:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229948AbiJLQWj (ORCPT ); Wed, 12 Oct 2022 12:22:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229613AbiJLQWb (ORCPT ); Wed, 12 Oct 2022 12:22:31 -0400 Received: from smtp.cesky-hosting.cz (smtp.cesky-hosting.cz [IPv6:2a00:1ed0:2:0:1:5bef:c8ee:1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F49FF41BF; Wed, 12 Oct 2022 09:22:20 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at smtp.cesky-hosting.cz DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=elrest.cz; s=rampa2-202208; t=1665591117; bh=3PJXsVojE1yIhCWBte/WZxkCcLEvTSwXh4kCjVhKYUs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BM9SRrG5yhctNS3csxOccAcSe9vSkB3S2uNE6KRqTiSY34JigW89/x39nfaRLs6Kf dLdUsEwN9Dh/DTG0XEyco++ecN6flXxf9+UxHPG2iNlYEEMuZOm8BKEH9+HT7moR5f 3+SIWdshFStDyvBTir4GfYUmpMZTWXbXsSYZg3C5Pt+qRoA8q3NAYNJIxcCYhxgjKL 9V+lRum8MNl+/6ZHJdS/qRZvP4g4dQ9nb6ftCi2bTahAJKB1gxCAPjBGtR63Dy6tUq NJ5vJI/+JUHuKiaksFyOdvDS7yF5k7chQ2MrGK7vmdhTC0njx+reJ/Q4zjG5omh2VG c6hwfMij99J+g== X-Thin-Conversation: conversation Received: from localhost.localdomain (unknown [5.181.92.50]) (Authenticated sender: tomas.marek@elrest.cz) by smtp.cesky-hosting.cz (Postfix) with ESMTPSA id 4FB2E1ACF; Wed, 12 Oct 2022 18:11:57 +0200 (CEST) From: Tomas Marek To: mpm@selenic.com, herbert@gondor.apana.org.au Cc: mcoquelin.stm32@gmail.com, linux-arm-kernel@lists.infradead.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, alexandre.torgue@foss.st.com, oleg.karfich@wago.com, Tomas Marek Subject: [PATCH 2/2] hwrng: stm32 - fix read of the last word Date: Wed, 12 Oct 2022 18:09:24 +0200 Message-Id: <20221012160924.12226-3-tomas.marek@elrest.cz> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221012160924.12226-1-tomas.marek@elrest.cz> References: <20221012160924.12226-1-tomas.marek@elrest.cz> Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The stm32_rng_read() function samples TRNG by 4 bytes until at least 5 bytes are free in the input buffer. The last four bytes are never read. For example, 60 bytes are returned in case the input buffer size is 64 bytes. Read until at least 4 bytes are free in the input buffer. Fill the buffer entirely in case the buffer size is divisible by 4. Cc: Oleg Karfich Signed-off-by: Tomas Marek --- drivers/char/hw_random/stm32-rng.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/char/hw_random/stm32-rng.c b/drivers/char/hw_random/stm32-rng.c index 8eaacefd498b..366edda4848b 100644 --- a/drivers/char/hw_random/stm32-rng.c +++ b/drivers/char/hw_random/stm32-rng.c @@ -44,7 +44,7 @@ static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) pm_runtime_get_sync((struct device *) priv->rng.priv); - while (max > sizeof(u32)) { + while (max >= sizeof(u32)) { sr = readl_relaxed(priv->base + RNG_SR); /* Manage timeout which is based on timer and take */ /* care of initial delay time when enabling rng */