crypto: Add support for shake128/256 XOFs
Subsystems (2)
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
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 | 211 +++++++++++++ crypto/shash.c | 9 + crypto/testmgr.c | 72 +++++ crypto/testmgr.h | 522 +++++++++++++++++++++++++++++++++ include/crypto/algapi.h | 2 +- include/crypto/hash.h | 25 +- include/crypto/sha3.h | 19 ++ include/uapi/linux/hash_info.h | 2 + 9 files changed, 862 insertions(+), 4 deletions(-)
Signature · 1 line
2.49.0
RFC PATCH 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 | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+)
crypto/shash.c +9 -0
@@ -59,6 +59,15 @@ int crypto_shash_final(struct shash_desc *desc, u8 *out) }EXPORT_SYMBOL_GPL (crypto_shash_final); +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 +20 -0
@@ -183,6 +183,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 * @setkey: see struct ahash_alg
@@ -213,6 +214,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 (*setkey)(struct crypto_shash *tfm, const u8 *key,
@@ -980,6 +983,23 @@ int crypto_shash_final(struct shash_desc *desc, u8 *out);int crypto_shash_finup (struct shash_desc *desc,const u8 *data,unsigned int len,u8 *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
RFC PATCH 2/4 crypto: Add shake128/256 to generic sha3 module
In reply to Stefan BergerExtend 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 a buffer following a keccak permutation. On top of this functionality, implement the general squeeze function that returns a requested number of bytes to the user. 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 | 211 +++++++++++++++++++++++++++++++++ include/crypto/algapi.h | 2 +- include/crypto/hash.h | 5 +- include/crypto/sha3.h | 19 +++ include/uapi/linux/hash_info.h | 2 + 6 files changed, 239 insertions(+), 4 deletions(-)
crypto/hash_info.c +4 -0
@@ -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 +211 -0
232 lines not on this page · raw message
include/crypto/algapi.h +1 -1
@@ -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 +2 -3
@@ -166,10 +166,9 @@ struct shash_desc {#define HASH_MAX_DIGESTSIZE 64/* -* Worst case is hmac(sha3-224-generic). Its context is a nested 'shash_desc' -* containing a 'struct sha3_state'. +* Worst case is shake128 */ -#define HASH_MAX_DESCSIZE (sizeof(struct shash_desc) + 360) +#define HASH_MAX_DESCSIZE (sizeof(struct shash_desc) + 384)#define SHASH_DESC_ON_STACK (shash, ctx) \ char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \
include/crypto/sha3.h +19 -0
@@ -31,4 +31,23 @@ int crypto_sha3_update(struct shash_desc *desc, const u8 *data, unsigned int len);int crypto_sha3_final (struct shash_desc *desc,u8 *out); + +#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
@@ -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
RFC PATCH 3/4 crypto: Add tests cases for shake128 & shake256 to testmgr
In reply to Stefan BergerAdd test cases for shake128 & shake256 to the testmgr.
--- crypto/testmgr.c | 14 +++ crypto/testmgr.h | 310 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 324 insertions(+)
crypto/testmgr.c +14 -0
@@ -5558,6 +5558,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
RFC PATCH 4/4 crypto: Extend testmgr with tests for shake128/256 XOFs
In reply to Stefan BergerExtend 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.
Add test case data for shake128 and shake256 XOFs.
--- crypto/testmgr.c | 58 +++++++++++++ crypto/testmgr.h | 212 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 270 insertions(+)
crypto/testmgr.c +58 -0
@@ -1647,6 +1647,59 @@ 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, }, + { .first =0 , .other = alg->base.cra_blocksize +1 , }, + { .first =1 , .other = alg->base.cra_blocksize, }, + { .first =1 , .other =1 , }, + { .first =1 , .other =33 , }, + }; +unsigned char *output =NULL , *tmp; +unsigned int off, 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 (output)) +return PTR_ERR (output); + output = tmp; + +crypto_shash_init (desc); +crypto_shash_update (desc, vec->plaintext, vec->psize); +crypto_shash_squeeze (desc, output, steps[i].first, false); + off = steps[i].first; + +while (off < vec->xof_size) { + req = steps[i].other; +if (off + req > vec->xof_size) + req = vec->xof_size - off; +crypto_shash_squeeze (desc, &output[off], req, false); + off += 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,
@@ -1998,6 +2051,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