From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel <ardb@kernel.org>,
"Jason A . Donenfeld" <Jason@zx2c4.com>,
linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org,
linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org,
linux-s390@vger.kernel.org, sparclinux@vger.kernel.org,
x86@kernel.org, Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH 17/18] lib/crypto: sha256: Sync sha256_update() with sha512_update()
Date: Wed, 25 Jun 2025 00:08:18 -0700 [thread overview]
Message-ID: <20250625070819.1496119-18-ebiggers@kernel.org> (raw)
In-Reply-To: <20250625070819.1496119-1-ebiggers@kernel.org>
The BLOCK_HASH_UPDATE_BLOCKS macro is difficult to read. For now, let's
just write the update explicitly in the straightforward way, mirroring
sha512_update(). It's possible that we'll bring back a macro for this
later, but it needs to be properly justified and hopefully a bit more
readable.
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
lib/crypto/sha256.c | 28 +++++++++++++++++++++++++---
1 file changed, 25 insertions(+), 3 deletions(-)
diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c
index 0de49bf8e8b8b..c93bf4699160c 100644
--- a/lib/crypto/sha256.c
+++ b/lib/crypto/sha256.c
@@ -8,11 +8,10 @@
* Copyright (c) 2014 Red Hat Inc.
* Copyright 2025 Google LLC
*/
#include <crypto/hmac.h>
-#include <crypto/internal/blockhash.h>
#include <crypto/sha2.h>
#include <linux/export.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/unaligned.h>
@@ -177,12 +176,35 @@ EXPORT_SYMBOL_GPL(sha256_init);
void __sha256_update(struct __sha256_ctx *ctx, const u8 *data, size_t len)
{
size_t partial = ctx->bytecount % SHA256_BLOCK_SIZE;
ctx->bytecount += len;
- BLOCK_HASH_UPDATE_BLOCKS(sha256_blocks, &ctx->state, data, len,
- SHA256_BLOCK_SIZE, ctx->buf, partial);
+
+ if (partial + len >= SHA256_BLOCK_SIZE) {
+ size_t nblocks;
+
+ if (partial) {
+ size_t l = SHA256_BLOCK_SIZE - partial;
+
+ memcpy(&ctx->buf[partial], data, l);
+ data += l;
+ len -= l;
+
+ sha256_blocks(&ctx->state, ctx->buf, 1);
+ }
+
+ nblocks = len / SHA256_BLOCK_SIZE;
+ len %= SHA256_BLOCK_SIZE;
+
+ if (nblocks) {
+ sha256_blocks(&ctx->state, data, nblocks);
+ data += nblocks * SHA256_BLOCK_SIZE;
+ }
+ partial = 0;
+ }
+ if (len)
+ memcpy(&ctx->buf[partial], data, len);
}
EXPORT_SYMBOL(__sha256_update);
static void __sha256_final(struct __sha256_ctx *ctx,
u8 *out, size_t digest_size)
--
2.50.0
WARNING: multiple messages have this Message-ID (diff)
From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel <ardb@kernel.org>,
"Jason A . Donenfeld" <Jason@zx2c4.com>,
linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org,
linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org,
linux-s390@vger.kernel.org, sparclinux@vger.kernel.org,
x86@kernel.org, Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH 17/18] lib/crypto: sha256: Sync sha256_update() with sha512_update()
Date: Wed, 25 Jun 2025 00:08:18 -0700 [thread overview]
Message-ID: <20250625070819.1496119-18-ebiggers@kernel.org> (raw)
In-Reply-To: <20250625070819.1496119-1-ebiggers@kernel.org>
The BLOCK_HASH_UPDATE_BLOCKS macro is difficult to read. For now, let's
just write the update explicitly in the straightforward way, mirroring
sha512_update(). It's possible that we'll bring back a macro for this
later, but it needs to be properly justified and hopefully a bit more
readable.
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
lib/crypto/sha256.c | 28 +++++++++++++++++++++++++---
1 file changed, 25 insertions(+), 3 deletions(-)
diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c
index 0de49bf8e8b8b..c93bf4699160c 100644
--- a/lib/crypto/sha256.c
+++ b/lib/crypto/sha256.c
@@ -8,11 +8,10 @@
* Copyright (c) 2014 Red Hat Inc.
* Copyright 2025 Google LLC
*/
#include <crypto/hmac.h>
-#include <crypto/internal/blockhash.h>
#include <crypto/sha2.h>
#include <linux/export.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/unaligned.h>
@@ -177,12 +176,35 @@ EXPORT_SYMBOL_GPL(sha256_init);
void __sha256_update(struct __sha256_ctx *ctx, const u8 *data, size_t len)
{
size_t partial = ctx->bytecount % SHA256_BLOCK_SIZE;
ctx->bytecount += len;
- BLOCK_HASH_UPDATE_BLOCKS(sha256_blocks, &ctx->state, data, len,
- SHA256_BLOCK_SIZE, ctx->buf, partial);
+
+ if (partial + len >= SHA256_BLOCK_SIZE) {
+ size_t nblocks;
+
+ if (partial) {
+ size_t l = SHA256_BLOCK_SIZE - partial;
+
+ memcpy(&ctx->buf[partial], data, l);
+ data += l;
+ len -= l;
+
+ sha256_blocks(&ctx->state, ctx->buf, 1);
+ }
+
+ nblocks = len / SHA256_BLOCK_SIZE;
+ len %= SHA256_BLOCK_SIZE;
+
+ if (nblocks) {
+ sha256_blocks(&ctx->state, data, nblocks);
+ data += nblocks * SHA256_BLOCK_SIZE;
+ }
+ partial = 0;
+ }
+ if (len)
+ memcpy(&ctx->buf[partial], data, len);
}
EXPORT_SYMBOL(__sha256_update);
static void __sha256_final(struct __sha256_ctx *ctx,
u8 *out, size_t digest_size)
--
2.50.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2025-06-25 7:52 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-25 7:08 [PATCH 00/18] SHA-256 library improvements Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 01/18] libceph: Rename hmac_sha256() to ceph_hmac_sha256() Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 02/18] cxl/test: Simplify fw_buf_checksum_show() Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 03/18] crypto: sha512 - Use the correct legacy export format Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 04/18] lib/crypto: sha512: Reorder some code in sha512.c Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 05/18] lib/crypto: sha512: Do not include <crypto/internal/sha2.h> Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 06/18] lib/crypto: sha512: Fix a grammatical error in kerneldoc comments Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 07/18] lib/crypto: sha256: Reorder some code Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 08/18] lib/crypto: sha256: Remove sha256_blocks_simd() Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 09/18] lib/crypto: sha256: Add sha224() and sha224_update() Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 10/18] lib/crypto: sha256: Make library API use strongly-typed contexts Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 11/18] lib/crypto: sha256: Propagate sha256_block_state type to implementations Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 12/18] lib/crypto: sha256: Add HMAC-SHA224 and HMAC-SHA256 support Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 13/18] crypto: sha256 - Wrap library and add HMAC support Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 14/18] crypto: sha256 - Use same state format as legacy drivers Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 15/18] lib/crypto: sha512: Remove sha256_is_arch_optimized() Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` [PATCH 16/18] lib/crypto: sha256: Consolidate into single module Eric Biggers
2025-06-25 7:08 ` Eric Biggers
2025-06-25 7:08 ` Eric Biggers [this message]
2025-06-25 7:08 ` [PATCH 17/18] lib/crypto: sha256: Sync sha256_update() with sha512_update() Eric Biggers
2025-06-25 7:08 ` [PATCH 18/18] lib/crypto: sha256: Document the SHA-224 and SHA-256 API Eric Biggers
2025-06-25 7:08 ` Eric Biggers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250625070819.1496119-18-ebiggers@kernel.org \
--to=ebiggers@kernel.org \
--cc=Jason@zx2c4.com \
--cc=ardb@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mips@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=linux-s390@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=sparclinux@vger.kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.