Skip to contentSkip to conversation

crypto: Add support for shake128/256 XOFs

PATCH In review Stefan Berger 2 participants 6 messages Jun 30, 2025 – Jul 18, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover crypto: Add support for shake128/256 XOFs

This series adds support for shake128/256 extended output functions (XOFs) along with test cases to verify the produced digest and XOF output. A new squeeze method is added to the shash_alg structure to get an arbitrary number of bytes from these XOFs.

Regards,
Stefan

v2:
- Refactored crypto_shake_squeeze_bytes to be called for arbitrary number of bytes to return
- Adjusted XOF test case parameters to better test modfied squeeze function

Stefan Berger (4):
crypto: Add squeeze function to shash_alg for support of XOFs
crypto: Add shake128/256 to generic sha3 module
crypto: Add tests cases for shake128 & shake256 to testmgr
crypto: Extend testmgr with tests for shake128/256 XOFs

 crypto/hash_info.c             |   4 +
 crypto/sha3_generic.c          | 238 +++++++++++++++
 crypto/shash.c                 |   9 +
 crypto/testmgr.c               |  70 +++++
 crypto/testmgr.h               | 522 +++++++++++++++++++++++++++++++++
 include/crypto/algapi.h        |   2 +-
 include/crypto/hash.h          |  28 +-
 include/crypto/sha3.h          |  19 ++
 include/uapi/linux/hash_info.h |   2 +
 9 files changed, 888 insertions(+), 6 deletions(-)

base-commit: a71d3e1beb7a9637eb75929b995f01d20981f013

Signature · 1 line
2.49.0
#1 lore raw

PATCH v2 1/4 crypto: Add squeeze function to shash_alg for support of XOFs

Add a squeeze function for support of shake128/256 XOFs. This function accepts a variable-length output buffer for the XOFs to return their data in. The 'final' parameter clears the state of an XOF and should only be set to 'true' when the last output is requested.

---
 crypto/shash.c        |  9 +++++++++
 include/crypto/hash.h | 19 +++++++++++++++++++
 2 files changed, 28 insertions(+)
crypto/shash.c +9 -0
index 4721f5f134f4..12b3882e9a26 100644
@@ -86,6 +86,15 @@ int crypto_shash_init(struct shash_desc *desc)
 }
 EXPORT_SYMBOL_GPL(crypto_shash_init);
 
+int crypto_shash_squeeze(struct shash_desc *desc, u8 *out, size_t outlen,
+			 bool final)
+{
+	if (!crypto_shash_alg(desc->tfm)->squeeze)
+		return -EINVAL;
+	return crypto_shash_alg(desc->tfm)->squeeze(desc, out, outlen, final);
+}
+EXPORT_SYMBOL_GPL(crypto_shash_squeeze);
+
 static int shash_default_finup(struct shash_desc *desc, const u8 *data,
 			       unsigned int len, u8 *out)
 {
include/crypto/hash.h +19 -0
index 6f6b9de12cd3..36b88d34c0dd 100644
@@ -209,6 +209,7 @@ struct shash_desc {
  * @final: see struct ahash_alg
  * @finup: see struct ahash_alg
  * @digest: see struct ahash_alg
+ * @squeeze: Get data from an XOF type of hash
  * @export: see struct ahash_alg
  * @import: see struct ahash_alg
  * @export_core: see struct ahash_alg
@@ -241,6 +242,8 @@ struct shash_alg {
 		     unsigned int len, u8 *out);
 	int (*digest)(struct shash_desc *desc, const u8 *data,
 		      unsigned int len, u8 *out);
+	int (*squeeze)(struct shash_desc *desc, u8 *out, size_t outlen,
+		       bool final);
 	int (*export)(struct shash_desc *desc, void *out);
 	int (*import)(struct shash_desc *desc, const void *in);
 	int (*export_core)(struct shash_desc *desc, void *out);
@@ -1011,6 +1014,22 @@ static inline int crypto_shash_final(struct shash_desc *desc, u8 *out)
 	return crypto_shash_finup(desc, NULL, 0, out);
 }
 
+/**
+ * crypto_shash_squeeze() - get xof message digest data
+ * @desc: operational state handle that is already filled with data
+ * @out: output buffer filled with the XOF message digest
+ * @outlen: number of bytes to get from the XOF
+ * @final: whether this is the final squeeze call
+ *
+ * Get message digest data from an extend output function (XOF)
+ *
+ * Context: Any context.
+ * Return: 0 if the data could be created successfully; < 0 if an error
+ *         occurred
+ */
+int crypto_shash_squeeze(struct shash_desc *desc, u8 *out, size_t outlen,
+			 bool final);
+
 static inline void shash_desc_zero(struct shash_desc *desc)
 {
 	memzero_explicit(desc,
Signature · 1 line
2.49.0
#2 lore raw

PATCH v2 2/4 crypto: Add shake128/256 to generic sha3 module

In reply to Stefan Berger

Extend the sha3 module with shake128 & shake256. For this, implement functions to get (squeeze) a number of bytes or blocks from the keccak sponge. A block here corresponds to the number of bytes available in the state buffer following a keccak permutation. On top of this functionality implement the general squeeze function that returns a requested number of bytes to the caller. Implement the 'final' function on top of the squeeze function. The 'final' function will always request a fixed number of bytes from the squeeze function and set the 'final' parameter to true, clearing the state of the hash as usual.

Adjust the maximum hash description and block sizes due to shake128.

Extend the arrays for supported hashes with entries for shake128 and shake256.

---
 crypto/hash_info.c             |   4 +
 crypto/sha3_generic.c          | 238 +++++++++++++++++++++++++++++++++
 include/crypto/algapi.h        |   2 +-
 include/crypto/hash.h          |   9 +-
 include/crypto/sha3.h          |  19 +++
 include/uapi/linux/hash_info.h |   2 +
 6 files changed, 268 insertions(+), 6 deletions(-)
crypto/hash_info.c +4 -0
index 9a467638c971..2e426be89463 100644
@@ -32,6 +32,8 @@ const char *const hash_algo_name[HASH_ALGO__LAST] = {
 	[HASH_ALGO_SHA3_256]    = "sha3-256",
 	[HASH_ALGO_SHA3_384]    = "sha3-384",
 	[HASH_ALGO_SHA3_512]    = "sha3-512",
+	[HASH_ALGO_SHAKE128]	= "shake128",
+	[HASH_ALGO_SHAKE256]	= "shake256",
 };
 EXPORT_SYMBOL_GPL(hash_algo_name);
 
@@ -59,5 +61,7 @@ const int hash_digest_size[HASH_ALGO__LAST] = {
 	[HASH_ALGO_SHA3_256]    = SHA3_256_DIGEST_SIZE,
 	[HASH_ALGO_SHA3_384]    = SHA3_384_DIGEST_SIZE,
 	[HASH_ALGO_SHA3_512]    = SHA3_512_DIGEST_SIZE,
+	[HASH_ALGO_SHAKE128]	= SHAKE128_DIGEST_SIZE,
+	[HASH_ALGO_SHAKE256]	= SHAKE256_DIGEST_SIZE,
 };
 EXPORT_SYMBOL_GPL(hash_digest_size);
crypto/sha3_generic.c +238 -0

259 lines not on this page · raw message

include/crypto/algapi.h +1 -1
index 188eface0a11..72c29dd1de9a 100644
@@ -20,7 +20,7 @@
  * static buffers that are big enough for any combination of
  * algs and architectures. Ciphers have a lower maximum size.
  */
-#define MAX_ALGAPI_BLOCKSIZE		160
+#define MAX_ALGAPI_BLOCKSIZE		168 /* shake128 */
 #define MAX_ALGAPI_ALIGNMASK		127
 #define MAX_CIPHER_BLOCKSIZE		16
 #define MAX_CIPHER_ALIGNMASK		15
include/crypto/hash.h +4 -5
index 36b88d34c0dd..a94763913acf 100644
@@ -177,14 +177,13 @@ struct shash_desc {
 
 #define HASH_MAX_DIGESTSIZE	 64
 
-/* Worst case is sha3-224. */
-#define HASH_MAX_STATESIZE	 200 + 144 + 1
+/* Worst case is shake128 */
+#define HASH_MAX_STATESIZE	 200 + 168 + 5 * 4 + 4
 
 /*
- * Worst case is hmac(sha3-224-s390).  Its context is a nested 'shash_desc'
- * containing a 'struct s390_sha_ctx'.
+ * Worst case is shake128
  */
-#define HASH_MAX_DESCSIZE	(sizeof(struct shash_desc) + 360)
+#define HASH_MAX_DESCSIZE	(sizeof(struct shash_desc) + 384)
 #define MAX_SYNC_HASH_REQSIZE	(sizeof(struct ahash_request) + \
 				 HASH_MAX_DESCSIZE)
 
include/crypto/sha3.h +19 -0
index 41e1b83a6d91..cc393d06a8da 100644
@@ -33,4 +33,23 @@ struct sha3_state {
 
 int crypto_sha3_init(struct shash_desc *desc);
 
+
+#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)
+
+struct shake_state {
+	u64		st[25];
+	unsigned int	rsiz;
+	unsigned int	rsizw;
+
+	unsigned int	partial;
+	u8		buf[SHAKE128_BLOCK_SIZE];
+	bool		finalized;
+	bool		permute;
+	unsigned int	ridx;
+};
+
 #endif
include/uapi/linux/hash_info.h +2 -0
index 0af23ec196d8..97af74326d31 100644
@@ -38,6 +38,8 @@ enum hash_algo {
 	HASH_ALGO_SHA3_256,
 	HASH_ALGO_SHA3_384,
 	HASH_ALGO_SHA3_512,
+	HASH_ALGO_SHAKE128,
+	HASH_ALGO_SHAKE256,
 	HASH_ALGO__LAST
 };
 
Signature · 1 line
2.49.0
#3 lore raw

PATCH v2 3/4 crypto: Add tests cases for shake128 & shake256 to testmgr

In reply to Stefan Berger

Add test cases for shake128 & shake256 to the testmgr to test their hash creation.

---
 crypto/testmgr.c |  14 +++
 crypto/testmgr.h | 310 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 324 insertions(+)
crypto/testmgr.c +14 -0
index 97190d9dcc0e..6bd67ca23bb1 100644
@@ -5525,6 +5525,20 @@ static const struct alg_test_desc alg_test_descs[] = {
 		.suite = {
 			.hash = __VECS(sha512_tv_template)
 		}
+	}, {
+		.alg = "shake128",
+		.test = alg_test_hash,
+		.fips_allowed = 1,
+		.suite = {
+			.hash = __VECS(shake128_tv_template)
+		}
+	}, {
+		.alg = "shake256",
+		.test = alg_test_hash,
+		.fips_allowed = 1,
+		.suite = {
+			.hash = __VECS(shake256_tv_template)
+		}
 	}, {
 		.alg = "sm3",
 		.test = alg_test_hash,
crypto/testmgr.h +310 -0

316 lines not on this page · raw message

Signature · 1 line
2.49.0
#4 lore raw

PATCH v2 4/4 crypto: Extend testmgr with tests for shake128/256 XOFs

In reply to Stefan Berger

Extend the testmgr to run tests for XOFs where it squeezes a certain number of bytes in a first step and then a different number in subsequent steps to test for issues related to alignments and proper copying of bytes and blocks. The test case parameters have been chosen so that all loops and break statements within loops of the shake squeeze function are exercised.

Add test case data for shake128 and shake256 XOFs.

---
 crypto/testmgr.c |  56 +++++++++++++
 crypto/testmgr.h | 212 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 268 insertions(+)
crypto/testmgr.c +56 -0
index 6bd67ca23bb1..0123be9d8396 100644
@@ -1630,6 +1630,57 @@ static int test_ahash_vec_cfg(const struct hash_testvec *vec,
 				 driver, cfg);
 }
 
+static int test_shash_xof(const struct hash_testvec *vec,
+			  struct shash_desc *desc)
+{
+	struct shash_alg *alg = crypto_shash_alg(desc->tfm);
+	struct steps {
+		unsigned int first;
+		unsigned int other;
+	} steps[] = {
+		{ .first = 0, .other = alg->base.cra_blocksize + 1, },
+		{ .first = alg->base.cra_blocksize - 1, .other = 10, },
+		{ .first = alg->base.cra_blocksize - 15, .other = 33, },
+	};
+	unsigned char *output = NULL, *tmp;
+	unsigned int offset, req;
+	int ret = 0;
+	size_t i;
+
+	for (i = 0; i < ARRAY_SIZE(steps); i++) {
+		if (!vec->xof)
+			continue;
+
+		tmp = krealloc(output, vec->xof_size, GFP_KERNEL);
+		if (IS_ERR(tmp))
+			return PTR_ERR(tmp);
+		output = tmp;
+
+		crypto_shash_init(desc);
+		crypto_shash_update(desc, vec->plaintext, vec->psize);
+		crypto_shash_squeeze(desc, output, steps[i].first, false);
+
+		offset = steps[i].first;
+		while (offset < vec->xof_size) {
+			req = steps[i].other;
+			if (offset + req > vec->xof_size)
+				req = vec->xof_size - offset;
+			crypto_shash_squeeze(desc, &output[offset], req, false);
+			offset += req;
+		}
+
+		if (memcmp(output, vec->xof, vec->xof_size) != 0) {
+			pr_err("XOF output of %s is wrong! (steps: %d, %d)\n",
+			       alg->base.cra_name, steps[i].first,
+			       steps[i].other);
+			ret = -EINVAL;
+		}
+	}
+	kfree(output);
+
+	return ret;
+}
+
 static int test_hash_vec_cfg(const struct hash_testvec *vec,
 			     const char *vec_name,
 			     const struct testvec_config *cfg,
@@ -1966,6 +2017,11 @@ static int __alg_test_hash(const struct hash_testvec *vecs,
 		err = test_hash_vec(&vecs[i], i, req, desc, tsgl, hashstate);
 		if (err)
 			goto out;
+		if (desc && crypto_shash_alg(desc->tfm)->squeeze) {
+			err = test_shash_xof(&vecs[i], desc);
+			if (err)
+				return err;
+		}
 		cond_resched();
 	}
 	err = test_hash_vs_generic_impl(generic_driver, maxkeysize, req,
crypto/testmgr.h +212 -0

275 lines not on this page · raw message

Signature · 1 line
2.49.0
maintainer
#5 lore raw
In reply to Stefan Berger
Quoted 9 lines · Stefan Berger

This series adds support for shake128/256 extended output functions (XOFs) along with test cases to verify the produced digest and XOF output. A new squeeze method is added to the shash_alg structure to get an arbitrary number of bytes from these XOFs.

Regards,
Stefan

Quoted 5 lines

v2:
- Refactored crypto_shake_squeeze_bytes to be called for arbitrary number of bytes to return
- Adjusted XOF test case parameters to better test modfied squeeze function

Please post this along with the actual user so that it can be evaluated in the proper context.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt