aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@kernel.org>2025-06-30 09:06:44 -0700
committerEric Biggers <ebiggers@kernel.org>2025-07-04 10:23:11 -0700
commit773d2b99bb76131f65f98828221142ccd2cec856 (patch)
treea0fecf44b1001680a51c80ed40d1051b7000c8bb /lib
parentlib/crypto: sha256: Consolidate into single module (diff)
downloadlinux-773d2b99bb76131f65f98828221142ccd2cec856.tar.gz
linux-773d2b99bb76131f65f98828221142ccd2cec856.zip
lib/crypto: sha256: Sync sha256_update() with sha512_update()
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. Acked-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20250630160645.3198-14-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/crypto/sha256.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c
index 68936d5cd774..808438d4f427 100644
--- a/lib/crypto/sha256.c
+++ b/lib/crypto/sha256.c
@@ -10,7 +10,6 @@
*/
#include <crypto/hmac.h>
-#include <crypto/internal/blockhash.h>
#include <crypto/sha2.h>
#include <linux/export.h>
#include <linux/kernel.h>
@@ -180,8 +179,31 @@ 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);