Skip to contentSkip to conversation

lib/crypto: Add SHA3-224, SHA3-256, SHA3-384, SHA-512, SHAKE128, SHAKE256

PATCH Superseded David Howells 4 participants 9 messages Sep 18, 2025 – Sep 19, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

PATCH 1/1 lib/crypto: Add SHA3-224, SHA3-256, SHA3-384, SHA-512, SHAKE128, SHAKE256

Add SHA3, providing SHA3-224, SHA3-256, SHA3-384, SHA-512, SHAKE128 and SHAKE256 to lib/crypto. Also add kunit tests for each of these.

gen-hash-testvecs.py is also modified to be able to generate SHAKE hashes because Python's hashlib requires the output digest size supplying for those two algorithms as they produce arbitrary length digests.

Notes:

(1) I've left hooks in sha3.c for asm-optimised variants, but as I don't entirely know what those might look like, not having implemented any, the hooks' usability is uncertain.

(2) The SHAKE algorithms will be required for ML-DSA.

cc Eric Biggers <[email protected]>
cc Jason A. Donenfeld <[email protected]>
cc Ard Biesheuvel <[email protected]>
cc Herbert Xu <[email protected]>
cc Stephan Mueller <[email protected]>
---
 include/crypto/sha3.h                     |  104 ++++++
 lib/crypto/Kconfig                        |    7 
 lib/crypto/Makefile                       |    6 
 lib/crypto/sha3.c                         |  508 ++++++++++++++++++++++++++++++
 lib/crypto/tests/Kconfig                  |   12 
 lib/crypto/tests/Makefile                 |    7 
 lib/crypto/tests/sha3_224_kunit.c         |   32 +
 lib/crypto/tests/sha3_224_testvecs.h      |  231 +++++++++++++
 lib/crypto/tests/sha3_256_kunit.c         |   32 +
 lib/crypto/tests/sha3_256_testvecs.h      |  231 +++++++++++++
 lib/crypto/tests/sha3_384_kunit.c         |   32 +
 lib/crypto/tests/sha3_384_testvecs.h      |  281 ++++++++++++++++
 lib/crypto/tests/sha3_512_kunit.c         |   32 +
 lib/crypto/tests/sha3_512_testvecs.h      |  331 +++++++++++++++++++
 lib/crypto/tests/sha3_shake128_kunit.c    |   42 ++
 lib/crypto/tests/sha3_shake128_testvecs.h |  181 ++++++++++
 lib/crypto/tests/sha3_shake256_kunit.c    |   42 ++
 lib/crypto/tests/sha3_shake256_testvecs.h |  231 +++++++++++++
 scripts/crypto/gen-hash-testvecs.py       |    8 
 19 files changed, 2347 insertions(+), 3 deletions(-)
include/crypto/sha3.h +103 -1
index 41e1b83a6d91..8693261e19dc 100644
@@ -23,6 +23,14 @@
 #define SHA3_512_BLOCK_SIZE	(200 - 2 * SHA3_512_DIGEST_SIZE)
 #define SHA3_512_EXPORT_SIZE	SHA3_STATE_SIZE + SHA3_512_BLOCK_SIZE + 1
 
+/* SHAKE128 and SHAKE256 actually have variable digest size, but this
+ * is needed for the kunit tests.
+ */
+#define SHAKE128_DIGEST_SIZE	(128 / 8)
+#define SHAKE128_BLOCK_SIZE	(200 - 2 * SHAKE128_DIGEST_SIZE)
+#define SHAKE256_DIGEST_SIZE	(256 / 8)
+#define SHAKE256_BLOCK_SIZE	(200 - 2 * SHAKE256_DIGEST_SIZE)
+
 #define SHA3_STATE_SIZE		200
 
 struct shash_desc;
@@ -31,6 +39,100 @@ struct sha3_state {
 	u64		st[SHA3_STATE_SIZE / 8];
 };
 
+struct sha3_ctx {
+	struct sha3_state	state;
+	__le64			buf[SHA3_224_BLOCK_SIZE / 8];
+	unsigned short		block_size;
+	unsigned short		digest_size;
+	unsigned short		partial;
+	u8			padding;
+};
+
 int crypto_sha3_init(struct shash_desc *desc);
 
-#endif
+void sha3_init(struct sha3_ctx *ctx);
+
+/**
+ * sha3_224_init() - Initialize a SHA3-224 context for a new message
+ * @ctx: the context to initialize
+ */
+static inline void sha3_224_init(struct sha3_ctx *ctx)
+{
+	sha3_init(ctx);
+	ctx->block_size  = SHA3_224_BLOCK_SIZE;
+	ctx->digest_size = SHA3_224_DIGEST_SIZE;
+	ctx->padding	 = 0x06;
+}
+
+/**
+ * sha3_256_init() - Initialize a SHA3-256 context for a new message
+ * @ctx: the context to initialize
+ */
+static inline void sha3_256_init(struct sha3_ctx *ctx)
+{
+	sha3_init(ctx);
+	ctx->block_size  = SHA3_256_BLOCK_SIZE;
+	ctx->digest_size = SHA3_256_DIGEST_SIZE;
+	ctx->padding	 = 0x06;
+}
+
+/**
+ * sha3_384_init() - Initialize a SHA3-384 context for a new message
+ * @ctx: the context to initialize
+ */
+static inline void sha3_384_init(struct sha3_ctx *ctx)
+{
+	sha3_init(ctx);
+	ctx->block_size  = SHA3_384_BLOCK_SIZE;
+	ctx->digest_size = SHA3_384_DIGEST_SIZE;
+	ctx->padding	 = 0x06;
+}
+
+/**
+ * sha3_512_init() - Initialize a SHA3-512 context for a new message
+ * @ctx: the context to initialize
+ */
+static inline void sha3_512_init(struct sha3_ctx *ctx)
+{
+	sha3_init(ctx);
+	ctx->block_size  = SHA3_512_BLOCK_SIZE;
+	ctx->digest_size = SHA3_512_DIGEST_SIZE;
+	ctx->padding	 = 0x06;
+}
+
+/**
+ * shake128_init() - Initialize a SHAKE128 context for a new message
+ * @ctx: The context to initialize
+ * @digest_size: The size of the digest desired
+ */
+static inline void shake128_init(struct sha3_ctx *ctx, unsigned int digest_size)
+{
+	sha3_init(ctx);
+	ctx->block_size  = SHAKE128_BLOCK_SIZE;
+	ctx->digest_size = digest_size;
+	ctx->padding	 = 0x1f;
+}
+
+/**
+ * shake256_init() - Initialize a SHAKE256 context for a new message
+ * @ctx: the context to initialize
+ * @digest_size: The size of the digest desired
+ */
+static inline void shake256_init(struct sha3_ctx *ctx, unsigned int digest_size)
+{
+	sha3_init(ctx);
+	ctx->block_size  = SHA3_256_BLOCK_SIZE;
+	ctx->digest_size = digest_size;
+	ctx->padding	 = 0x1f;
+}
+
+void sha3_update(struct sha3_ctx *ctx, const u8 *data, unsigned int len);
+int sha3_final(struct sha3_ctx *ctx, u8 *out);
+void sha3_224(const u8 *in, size_t in_len, u8 out[SHA3_224_DIGEST_SIZE]);
+void sha3_256(const u8 *in, size_t in_len, u8 out[SHA3_256_DIGEST_SIZE]);
+void sha3_384(const u8 *in, size_t in_len, u8 out[SHA3_384_DIGEST_SIZE]);
+void sha3_512(const u8 *in, size_t in_len, u8 out[SHA3_512_DIGEST_SIZE]);
+int shake128(const u8 *in, size_t in_len, u8 *out, size_t out_len);
+int shake256(const u8 *in, size_t in_len, u8 *out, size_t out_len);
+
+#endif /* __CRYPTO_SHA3_H__ */
lib/crypto/Kconfig +7 -0
index 1e6b008f8fca..5ea6abe934d6 100644
@@ -191,6 +191,13 @@ config CRYPTO_LIB_SHA512_ARCH
 	default y if SPARC64
 	default y if X86_64
 
+config CRYPTO_LIB_SHA3
+	tristate
+	help
+	  The SHA3 library functions.
+	  Select this if your module uses any of these functions from
+	  <crypto/sha3.h>.
+
 config CRYPTO_LIB_SM3
 	tristate
 
lib/crypto/Makefile +6 -0
index 539d5d59a50e..bf73a2cfef86 100644
@@ -149,6 +149,12 @@ endif # CONFIG_CRYPTO_LIB_SHA512_ARCH
 
 ################################################################################
 
+#obj-$(CONFIG_CRYPTO_LIB_SHA3) += libsha3.o
+#libsha3-y := sha3.o
+obj-$(CONFIG_CRYPTO_SHA3) += sha3.o
+
+################################################################################
+
 obj-$(CONFIG_MPILIB) += mpi/
 
 obj-$(CONFIG_CRYPTO_SELFTESTS_FULL)		+= simd.o
lib/crypto/sha3.c new file +508 -0

508 lines not on this page · raw message

lib/crypto/tests/Kconfig +12 -0
index de7e8babb6af..43b39616dce1 100644
@@ -43,6 +43,18 @@ config CRYPTO_LIB_SHA512_KUNIT_TEST
 	  KUnit tests for the SHA-384 and SHA-512 cryptographic hash functions
 	  and their corresponding HMACs.
 
+config CRYPTO_LIB_SHA3_KUNIT_TEST
+	tristate "KUnit tests for SHA-3" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS
+	select CRYPTO_LIB_BENCHMARK_VISIBLE
+	select CRYPTO_LIB_SHA3
+	help
+	  KUnit tests for the SHA3 cryptographic hash functions, including
+	  SHA3-224, SHA3-256, SHA3-386, SHA3-512, SHAKE128 and SHAKE256.  Note
+	  that whilst the SHAKE* hash functions can support arbitrary-length
+	  digests, these tests only check the nominal digest sizes for now.
+
 config CRYPTO_LIB_BENCHMARK_VISIBLE
 	bool
 
lib/crypto/tests/Makefile +7 -0
index 8601dccd6fdd..efbe1bf2e1a9 100644
@@ -4,3 +4,10 @@ obj-$(CONFIG_CRYPTO_LIB_POLY1305_KUNIT_TEST) += poly1305_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_SHA1_KUNIT_TEST) += sha1_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_SHA256_KUNIT_TEST) += sha224_kunit.o sha256_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_SHA512_KUNIT_TEST) += sha384_kunit.o sha512_kunit.o
+obj-$(CONFIG_CRYPTO_LIB_SHA3_KUNIT_TEST) += \
+	sha3_224_kunit.o \
+	sha3_256_kunit.o \
+	sha3_384_kunit.o \
+	sha3_512_kunit.o \
+	sha3_shake128_kunit.o \
+	sha3_shake256_kunit.o
lib/crypto/tests/sha3_224_kunit.c new file +32 -0
new file mode 100644
index 000000000000..c09aab76660a
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2025 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells ([email protected])
+ */
+#include <crypto/sha3.h>
+#include "sha3_224_testvecs.h"
+
+#define HASH		sha3_224
+#define HASH_CTX	sha3_ctx
+#define HASH_SIZE	SHA3_224_DIGEST_SIZE
+#define HASH_INIT	sha3_224_init
+#define HASH_UPDATE	sha3_update
+#define HASH_FINAL	sha3_final
+#include "hash-test-template.h"
+
+static struct kunit_case hash_test_cases[] = {
+	HASH_KUNIT_CASES,
+	KUNIT_CASE(benchmark_hash),
+	{},
+};
+
+static struct kunit_suite hash_test_suite = {
+	.name = "sha3_224",
+	.test_cases = hash_test_cases,
+	.suite_init = hash_suite_init,
+	.suite_exit = hash_suite_exit,
+};
+kunit_test_suite(hash_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests and benchmark for SHA3-224");
+MODULE_LICENSE("GPL");
lib/crypto/tests/sha3_224_testvecs.h new file +231 -0

231 lines not on this page · raw message

lib/crypto/tests/sha3_256_kunit.c new file +32 -0
new file mode 100644
index 000000000000..04f0ebce6223
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2025 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells ([email protected])
+ */
+#include <crypto/sha3.h>
+#include "sha3_256_testvecs.h"
+
+#define HASH		sha3_256
+#define HASH_CTX	sha3_ctx
+#define HASH_SIZE	SHA3_256_DIGEST_SIZE
+#define HASH_INIT	sha3_256_init
+#define HASH_UPDATE	sha3_update
+#define HASH_FINAL	sha3_final
+#include "hash-test-template.h"
+
+static struct kunit_case hash_test_cases[] = {
+	HASH_KUNIT_CASES,
+	KUNIT_CASE(benchmark_hash),
+	{},
+};
+
+static struct kunit_suite hash_test_suite = {
+	.name = "sha3_256",
+	.test_cases = hash_test_cases,
+	.suite_init = hash_suite_init,
+	.suite_exit = hash_suite_exit,
+};
+kunit_test_suite(hash_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests and benchmark for SHA3-256");
+MODULE_LICENSE("GPL");
lib/crypto/tests/sha3_256_testvecs.h new file +231 -0

231 lines not on this page · raw message

lib/crypto/tests/sha3_384_kunit.c new file +32 -0
new file mode 100644
index 000000000000..5521f4ae3c25
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2025 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells ([email protected])
+ */
+#include <crypto/sha3.h>
+#include "sha3_384_testvecs.h"
+
+#define HASH		sha3_384
+#define HASH_CTX	sha3_ctx
+#define HASH_SIZE	SHA3_384_DIGEST_SIZE
+#define HASH_INIT	sha3_384_init
+#define HASH_UPDATE	sha3_update
+#define HASH_FINAL	sha3_final
+#include "hash-test-template.h"
+
+static struct kunit_case hash_test_cases[] = {
+	HASH_KUNIT_CASES,
+	KUNIT_CASE(benchmark_hash),
+	{},
+};
+
+static struct kunit_suite hash_test_suite = {
+	.name = "sha3_384",
+	.test_cases = hash_test_cases,
+	.suite_init = hash_suite_init,
+	.suite_exit = hash_suite_exit,
+};
+kunit_test_suite(hash_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests and benchmark for SHA3-384");
+MODULE_LICENSE("GPL");
lib/crypto/tests/sha3_384_testvecs.h new file +281 -0

281 lines not on this page · raw message

lib/crypto/tests/sha3_512_kunit.c new file +32 -0
new file mode 100644
index 000000000000..2762dae16aa0
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2025 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells ([email protected])
+ */
+#include <crypto/sha3.h>
+#include "sha3_512_testvecs.h"
+
+#define HASH		sha3_512
+#define HASH_CTX	sha3_ctx
+#define HASH_SIZE	SHA3_512_DIGEST_SIZE
+#define HASH_INIT	sha3_512_init
+#define HASH_UPDATE	sha3_update
+#define HASH_FINAL	sha3_final
+#include "hash-test-template.h"
+
+static struct kunit_case hash_test_cases[] = {
+	HASH_KUNIT_CASES,
+	KUNIT_CASE(benchmark_hash),
+	{},
+};
+
+static struct kunit_suite hash_test_suite = {
+	.name = "sha3_512",
+	.test_cases = hash_test_cases,
+	.suite_init = hash_suite_init,
+	.suite_exit = hash_suite_exit,
+};
+kunit_test_suite(hash_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests and benchmark for SHA-512 and HMAC-SHA512");
+MODULE_LICENSE("GPL");
lib/crypto/tests/sha3_512_testvecs.h new file +331 -0

331 lines not on this page · raw message

lib/crypto/tests/sha3_shake128_kunit.c new file +42 -0

42 lines not on this page · raw message

lib/crypto/tests/sha3_shake128_testvecs.h new file +181 -0

181 lines not on this page · raw message

lib/crypto/tests/sha3_shake256_kunit.c new file +42 -0

42 lines not on this page · raw message

lib/crypto/tests/sha3_shake256_testvecs.h new file +231 -0

231 lines not on this page · raw message

scripts/crypto/gen-hash-testvecs.py +6 -2
index 4ac927d40cf5..f0bc057bd76d 100755
@@ -61,6 +61,10 @@ def hash_update(ctx, data):
     ctx.update(data)
 
 def hash_final(ctx):
+    if ctx.name == "shake_128":
+        return ctx.digest(16)
+    if ctx.name == "shake_256":
+        return ctx.digest(32)
     return ctx.digest()
 
 def compute_hash(alg, data):
@@ -117,7 +121,7 @@ def gen_hmac_testvecs(alg):
         ctx.update(mac)
     print_static_u8_array_definition(
             f'hmac_testvec_consolidated[{alg.upper()}_DIGEST_SIZE]',
-            ctx.digest())
+            hash_final(ctx))
 
 def gen_additional_poly1305_testvecs():
     key = b'\xff' * POLY1305_KEY_SIZE
@@ -143,5 +147,5 @@ print(f'/* This file was generated by: {sys.argv[0]} {" ".join(sys.argv[1:])} */
 gen_unkeyed_testvecs(alg)
 if alg == 'poly1305':
     gen_additional_poly1305_testvecs()
-else:
+elif alg != 'shake128' and alg != 'shake256':
     gen_hmac_testvecs(alg)
#1 lore raw

Am Freitag, 19. September 2025, 00:07:28 Mitteleuropäische Sommerzeit schrieb David Howells:

Hi David,

as you mentioned that this patch as a basis for ML-DSA then may I outline the
following: the ML-DSA code requires a multi-staged squeeze operation. For example:

squeeze(state, 10 bytes);
squeeze(state, 10 bytes);

must be identical to

squeeze(state, 20 bytes);

With this in mind, may I highlight that potentially the following code does not support this notion:

Quoted 11 lines

+/**
+ * sha3_final() - Finish computing a SHA3 message digest of any type
+ * @ctx: the context to finalize; must have been initialized
+ * @out: (output) the resulting message digest
+ *
+ * Finish the computation of a SHA3 message digest of any type and perform the + * "Keccak sponge squeezing" phase. The digest is written to @out buffer and + * the size of the digest is returned. Before returning, the context @ctx is + * cleared so that the caller does not need to do it.
+ */

lib/crypto/sha3.c new file +1 -0
+int sha3_final(struct sha3_ctx *ctx, u8 *out)

lib/crypto/sha3.c line 292

Quoted 10 lines
lib/crypto/sha3.c new file +10 -0
+{
+	struct sha3_state *state = &ctx->state;
+	unsigned int digest_size = ctx->digest_size;
+	unsigned int bsize = ctx->block_size;
+	u8 end_marker = 0x80;
+
+	sha3_absorb_xorle(ctx, &ctx->padding, 1);
+	ctx->partial = bsize - 1;
+	sha3_absorb_xorle(ctx, &end_marker, 1);
+	sha3_keccakf(ctx->state.st);

This logic above should only be invoked for the first squeeze operation.

May I suggest you consider the code at:

https://github.com/smuellerDD/leancrypto/blob/master/hash/src/sha3_c.c#L625

lib/crypto/sha3.c line 304

Quoted 12 lines
lib/crypto/sha3.c new file +12 -0
+
+#ifdef __LITTLE_ENDIAN
+	for (;;) {
+		unsigned int part = umin(digest_size, bsize);
+
+		memcpy(out, state->st, part);
+		digest_size -= part;
+		if (!digest_size)
+			goto done;
+		out += part;
+		sha3_keccakf(ctx->state.st);
+	}

This loop needs to honor a starting offset in case the previous call only requested a subset of the rate.

May I suggest to consider the code at:

https://github.com/smuellerDD/leancrypto/blob/master/hash/src/sha3_c.c#L643

Quoted 15 lines
lib/crypto/sha3.c new file +15 -0
+#else
+	__le64 *digest = (__le64 *)out, *s;
+
+	while (digest_size >= bsize) {
+		for (int i = 0; i < bsize / 8; i++)
+			put_unaligned_le64(state->st[i], digest++);
+		digest_size -= bsize;
+		if (!digest_size)
+			goto done;
+		sha3_keccakf(ctx->state.st);
+	}
+
+	s = state->st;
+	for (; digest_size >= 8; digest_size -= 8)
+		put_unaligned_le64(*s++, digest++);

lib/crypto/sha3.c line 329

Quoted 10 lines
lib/crypto/sha3.c new file +10 -0
+
+	u8 *sc = (u8 *)s;
+	u8 *dc = (u8 *)digest;
+
+	for (; digest_size >= 1; digest_size -= 1)
+		*dc++ = *sc++;
+#endif
+done:
+	digest_size = ctx->digest_size;
+	memzero_explicit(ctx, sizeof(*ctx));

For a multi-stage squeeze, it is perhaps not helpful to zeroize the context here.

Ciao
Stephan

author maintainer
#2 lore raw
Quoted 2 lines · Stephan Müller

For a multi-stage squeeze, it is perhaps not helpful to zeroize the context here.

Yeah - I've seen this now that I'm starting to trawl through your dilithium code, so it will need adjusting.

David

#3 lore raw
Quoted 7 lines · David Howells
Quoted 2 lines · Stephan Müller

For a multi-stage squeeze, it is perhaps not helpful to zeroize the context here.

Yeah - I've seen this now that I'm starting to trawl through your dilithium code, so it will need adjusting.

I strongly suggest creating a test vector where multiple absorb and squeeze operations are done in intermixed order, and then use that test vector in your Kunit tests to ensure changes to the code do not break this fundamental property of the keccak sponge algorithm.

Simo.

Signature · 4 lines
Simo Sorce
Distinguished Engineer
RHEL Crypto Team
Red Hat, Inc
author maintainer
#4 lore raw
Quoted 4 lines · Simo Sorce

I strongly suggest creating a test vector where multiple absorb and squeeze operations are done in intermixed order, and then use that test vector in your Kunit tests to ensure changes to the code do not break this fundamental property of the keccak sponge algorithm.

I'm putting such a beast in the module init function at least.

Annoyingly, Eric's hash-test-template.h makes some unwarranted assumptions about the hashes it is testing - such as the final function zeroing out the hash struct.

David

#5 lore raw

Am Freitag, 19. September 2025, 17:34:36 Mitteleuropäische Sommerzeit schrieb David Howells:

Hi David,

Quoted 11 lines
Quoted 4 lines · Simo Sorce

I strongly suggest creating a test vector where multiple absorb and squeeze operations are done in intermixed order, and then use that test vector in your Kunit tests to ensure changes to the code do not break this fundamental property of the keccak sponge algorithm.

I'm putting such a beast in the module init function at least.

Annoyingly, Eric's hash-test-template.h makes some unwarranted assumptions about the hashes it is testing - such as the final function zeroing out the hash struct.

If it is of help, here is such test that I use:

https://github.com/smuellerDD/leancrypto/blob/master/hash/tests/
shake_squeeze_more_tester.c#L92

Ciao
Stephan

#6 lore raw
In reply to David Howells
kernel test robot noticed the following build warnings:

Hi David,

kernel test robot noticed the following build warnings:

[auto build test WARNING on ebiggers/libcrypto-fixes]
[also build test WARNING on herbert-cryptodev-2.6/master herbert-crypto-2.6/master linus/master v6.17-rc6 next-20250918] [cannot apply to ebiggers/libcrypto-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Howells/lib-crypto-Add-SHA3-224-SHA3-256-SHA3-384-SHA-512-SHAKE128-SHAKE256/20250919-061024
base:   https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git libcrypto-fixes
patch link:    https://lore.kernel.org/r/3605112.1758233248%40warthog.procyon.org.uk
patch subject: [PATCH] lib/crypto: Add SHA3-224, SHA3-256, SHA3-384, SHA-512, SHAKE128, SHAKE256
config: arm-randconfig-001-20250919 (https://download.01.org/0day-ci/archive/20250919/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 12.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250919/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

lib/crypto/sha3.c: In function 'sha3_absorb_xorle':

>> lib/crypto/sha3.c:229:30: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
     229 |                 buf[woff | 7 - boff] ^= *data++;
         |                            ~~^~~~~~

vim +229 lib/crypto/sha3.c

211
212 /*
213 * XOR in partial data that's insufficient to fill a whole block.
214 */
215 static void sha3_absorb_xorle(struct sha3_ctx *ctx, const u8 *data, unsigned int len)
216 {
217 unsigned int partial = ctx->partial;
218 u8 *buf = (u8 *)ctx->state.st;
219
220 #ifdef __LITTLE_ENDIAN
221 buf += partial;
222 for (int i = 0; i < len; i++)
223 *buf++ ^= *data++;
224 #else
225 for (int i = 0; i < len; i++) {
226 unsigned int woff = (partial + i) & ~7;
227 unsigned int boff = (partial + i) & 7;
228

 > 229			buf[woff | 7 - boff] ^= *data++;

230 }
231 #endif
232 ctx->partial += len;
233 }
234

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw
In reply to David Howells
kernel test robot noticed the following build errors:

Hi David,

kernel test robot noticed the following build errors:

[auto build test ERROR on ebiggers/libcrypto-fixes]
[also build test ERROR on herbert-cryptodev-2.6/master herbert-crypto-2.6/master linus/master v6.17-rc6 next-20250918] [cannot apply to ebiggers/libcrypto-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Howells/lib-crypto-Add-SHA3-224-SHA3-256-SHA3-384-SHA-512-SHAKE128-SHAKE256/20250919-061024
base:   https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git libcrypto-fixes
patch link:    https://lore.kernel.org/r/3605112.1758233248%40warthog.procyon.org.uk
patch subject: [PATCH] lib/crypto: Add SHA3-224, SHA3-256, SHA3-384, SHA-512, SHAKE128, SHAKE256
config: s390-randconfig-001-20250919 (https://download.01.org/0day-ci/archive/20250919/[email protected]/config)
compiler: s390-linux-gcc (GCC) 8.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250919/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> arch/s390/crypto/sha3_256_s390.c:22:12: error: conflicting types for 'sha3_256_init'
    static int sha3_256_init(struct shash_desc *desc)
               ^~~~~~~~~~~~~
   In file included from arch/s390/crypto/sha3_256_s390.c:13:
   include/crypto/sha3.h:71:20: note: previous definition of 'sha3_256_init' was here
    static inline void sha3_256_init(struct sha3_ctx *ctx)
                       ^~~~~~~~~~~~~
>> arch/s390/crypto/sha3_256_s390.c:99:12: error: conflicting types for 'sha3_224_init'

static int sha3_224_init(struct shash_desc *desc)
^~~~~~~~~~~~~
In file included from arch/s390/crypto/sha3_256_s390.c:13:
include/crypto/sha3.h:59:20: note: previous definition of 'sha3_224_init' was here
static inline void sha3_224_init(struct sha3_ctx *ctx)
^~~~~~~~~~~~~

vim +/sha3_256_init +22 arch/s390/crypto/sha3_256_s390.c

3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14 21

3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14  @22  static int sha3_256_init(struct shash_desc *desc)
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   23  {
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   24  	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   25  
6f90ba7065515d Herbert Xu        2025-04-18   26  	sctx->first_message_part = test_facility(86);
6f90ba7065515d Herbert Xu        2025-04-18   27  	if (!sctx->first_message_part)
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   28  		memset(sctx->state, 0, sizeof(sctx->state));
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   29  	sctx->count = 0;
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   30  	sctx->func = CPACF_KIMD_SHA3_256;
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   31  
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   32  	return 0;
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   33  }
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   34  
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   35  static int sha3_256_export(struct shash_desc *desc, void *out)
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   36  {
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   37  	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
73c2437109c3ea Herbert Xu        2025-05-23   38  	union {
73c2437109c3ea Herbert Xu        2025-05-23   39  		u8 *u8;
73c2437109c3ea Herbert Xu        2025-05-23   40  		u64 *u64;
73c2437109c3ea Herbert Xu        2025-05-23   41  	} p = { .u8 = out };
73c2437109c3ea Herbert Xu        2025-05-23   42  	int i;
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   43  
6f90ba7065515d Herbert Xu        2025-04-18   44  	if (sctx->first_message_part) {
73c2437109c3ea Herbert Xu        2025-05-23   45  		memset(out, 0, SHA3_STATE_SIZE);
73c2437109c3ea Herbert Xu        2025-05-23   46  		return 0;
6f90ba7065515d Herbert Xu        2025-04-18   47  	}
73c2437109c3ea Herbert Xu        2025-05-23   48  	for (i = 0; i < SHA3_STATE_SIZE / 8; i++)
73c2437109c3ea Herbert Xu        2025-05-23   49  		put_unaligned(le64_to_cpu(sctx->sha3.state[i]), p.u64++);
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   50  	return 0;
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   51  }
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   52  
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   53  static int sha3_256_import(struct shash_desc *desc, const void *in)
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   54  {
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   55  	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
73c2437109c3ea Herbert Xu        2025-05-23   56  	union {
73c2437109c3ea Herbert Xu        2025-05-23   57  		const u8 *u8;
73c2437109c3ea Herbert Xu        2025-05-23   58  		const u64 *u64;
73c2437109c3ea Herbert Xu        2025-05-23   59  	} p = { .u8 = in };
73c2437109c3ea Herbert Xu        2025-05-23   60  	int i;
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   61  
73c2437109c3ea Herbert Xu        2025-05-23   62  	for (i = 0; i < SHA3_STATE_SIZE / 8; i++)
73c2437109c3ea Herbert Xu        2025-05-23   63  		sctx->sha3.state[i] = cpu_to_le64(get_unaligned(p.u64++));
6f90ba7065515d Herbert Xu        2025-04-18   64  	sctx->count = 0;
6f90ba7065515d Herbert Xu        2025-04-18   65  	sctx->first_message_part = 0;
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   66  	sctx->func = CPACF_KIMD_SHA3_256;
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   67  
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   68  	return 0;
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   69  }
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   70  
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   71  static int sha3_224_import(struct shash_desc *desc, const void *in)
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   72  {
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   73  	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   74  
6f90ba7065515d Herbert Xu        2025-04-18   75  	sha3_256_import(desc, in);
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   76  	sctx->func = CPACF_KIMD_SHA3_224;
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   77  	return 0;
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   78  }
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   79  
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   80  static struct shash_alg sha3_256_alg = {
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   81  	.digestsize	=	SHA3_256_DIGEST_SIZE,	   /* = 32 */
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   82  	.init		=	sha3_256_init,
6f90ba7065515d Herbert Xu        2025-04-18   83  	.update		=	s390_sha_update_blocks,
6f90ba7065515d Herbert Xu        2025-04-18   84  	.finup		=	s390_sha_finup,
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   85  	.export		=	sha3_256_export,
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   86  	.import		=	sha3_256_import,
6f90ba7065515d Herbert Xu        2025-04-18   87  	.descsize	=	S390_SHA_CTX_SIZE,
6f90ba7065515d Herbert Xu        2025-04-18   88  	.statesize	=	SHA3_STATE_SIZE,
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   89  	.base		=	{
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   90  		.cra_name	 =	"sha3-256",
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   91  		.cra_driver_name =	"sha3-256-s390",
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   92  		.cra_priority	 =	300,
6f90ba7065515d Herbert Xu        2025-04-18   93  		.cra_flags	 =	CRYPTO_AHASH_ALG_BLOCK_ONLY,
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   94  		.cra_blocksize	 =	SHA3_256_BLOCK_SIZE,
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   95  		.cra_module	 =	THIS_MODULE,
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   96  	}
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   97  };
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14   98  
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14  @99  static int sha3_224_init(struct shash_desc *desc)
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14  100  {
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14  101  	struct s390_sha_ctx *sctx = shash_desc_ctx(desc);
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14  102  
6f90ba7065515d Herbert Xu        2025-04-18  103  	sha3_256_init(desc);
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14  104  	sctx->func = CPACF_KIMD_SHA3_224;
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14  105  	return 0;
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14  106  }
3c2eb6b76cabb7 Joerg Schmidbauer 2019-08-14  107  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#8 lore raw
In reply to David Howells
kernel test robot noticed the following build warnings:

Hi David,

kernel test robot noticed the following build warnings:

[auto build test WARNING on ebiggers/libcrypto-fixes]
[also build test WARNING on herbert-cryptodev-2.6/master herbert-crypto-2.6/master linus/master v6.17-rc6 next-20250918] [cannot apply to ebiggers/libcrypto-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Howells/lib-crypto-Add-SHA3-224-SHA3-256-SHA3-384-SHA-512-SHAKE128-SHAKE256/20250919-061024
base:   https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git libcrypto-fixes
patch link:    https://lore.kernel.org/r/3605112.1758233248%40warthog.procyon.org.uk
patch subject: [PATCH] lib/crypto: Add SHA3-224, SHA3-256, SHA3-384, SHA-512, SHAKE128, SHAKE256
config: arc-randconfig-r112-20250919 (https://download.01.org/0day-ci/archive/20250919/[email protected]/config)
compiler: arc-linux-gcc (GCC) 10.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250919/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> lib/crypto/sha3.c:317:11: sparse: sparse: incorrect type in assignment (different base types) @@     expected restricted __le64 [usertype] *s @@     got unsigned long long * @@
   lib/crypto/sha3.c:317:11: sparse:     expected restricted __le64 [usertype] *s
   lib/crypto/sha3.c:317:11: sparse:     got unsigned long long *
>> lib/crypto/sha3.c:319:36: sparse: sparse: incorrect type in argument 1 (different base types) @@     expected unsigned long long [usertype] val @@     got restricted __le64 [usertype] @@

lib/crypto/sha3.c:319:36: sparse: expected unsigned long long [usertype] val
lib/crypto/sha3.c:319:36: sparse: got restricted __le64 [usertype]

vim +317 lib/crypto/sha3.c

271
272 /**
273 * sha3_final() - Finish computing a SHA3 message digest of any type
274 * @ctx: the context to finalize; must have been initialized
275 * @out: (output) the resulting message digest
276 *
277 * Finish the computation of a SHA3 message digest of any type and perform the
278 * "Keccak sponge squeezing" phase. The digest is written to @out buffer and
279 * the size of the digest is returned. Before returning, the context @ctx is
280 * cleared so that the caller does not need to do it.
281 */
282 int sha3_final(struct sha3_ctx *ctx, u8 *out)
283 {
284 struct sha3_state *state = &ctx->state;
285 unsigned int digest_size = ctx->digest_size;
286 unsigned int bsize = ctx->block_size;
287 u8 end_marker = 0x80;
288
289 sha3_absorb_xorle(ctx, &ctx->padding, 1);
290 ctx->partial = bsize - 1;
291 sha3_absorb_xorle(ctx, &end_marker, 1);
292 sha3_keccakf(ctx->state.st);
293
294 #ifdef __LITTLE_ENDIAN
295 for (;;) {
296 unsigned int part = umin(digest_size, bsize);
297
298 memcpy(out, state->st, part);
299 digest_size -= part;
300 if (!digest_size)
301 goto done;
302 out += part;
303 sha3_keccakf(ctx->state.st);
304 }
305 #else
306 __le64 *digest = (__le64 *)out, *s;
307
308 while (digest_size >= bsize) {
309 for (int i = 0; i < bsize / 8; i++)
310 put_unaligned_le64(state->st[i], digest++);
311 digest_size -= bsize;
312 if (!digest_size)
313 goto done;
314 sha3_keccakf(ctx->state.st);
315 }
316

 > 317		s = state->st;
   318		for (; digest_size >= 8; digest_size -= 8)
 > 319			put_unaligned_le64(*s++, digest++);

320
321 u8 *sc = (u8 *)s;
322 u8 *dc = (u8 *)digest;
323
324 for (; digest_size >= 1; digest_size -= 1)
325 *dc++ = *sc++;
326 #endif
327 done:
328 digest_size = ctx->digest_size;
329 memzero_explicit(ctx, sizeof(*ctx));
330 return digest_size;
331 }
332 EXPORT_SYMBOL_GPL(sha3_final);
333

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki