crypto: xxhash - Implement xxhash support
Subsystems (2)
PATCH v3 1/1 crypto: xxhash - Implement xxhash support
xxhash is currently implemented as a self-contained module in /lib. This patch enables that module to be used as part of the generic kernel crypto framework. It adds a simple wrapper to the 64bit version.
I've also added test vectors (with help from Nick Terrell). The upstream xxhash code is tested by running hashing operation on random 222 byte data with seed values of 0 and a prime number. The upstream test
suite can be found at https://github.com/Cyan4973/xxHash/blob/cf46e0c/xxhsum.c#L664
Essentially hashing is run on data of length 0,1,14,222 with the aforementioned seed values 0 and prime 2654435761. The particular random 222 byte string was provided to me by Nick Terrell by reading /dev/random and the checksums were calculated by the upstream xxsum utility with the following bash script:
dd if=/dev/random of=TEST_VECTOR bs=1 count=222
for a in 0 1; do
for l in 0 1 14 222; do for s in 0 2654435761; do echo algo $a length $l seed $s; head -c $l TEST_VECTOR | ~/projects/kernel/xxHash/xxhsum -H$a -s$s done done
done
This produces output as follows:
algo 0 length 0 seed 002cc5d05 stdin
algo 0 length 0 seed 265443576102cc5d05 stdin
algo 0 length 1 seed 0
25201171 stdin
algo 0 length 1 seed 2654435761
25201171 stdin
algo 0 length 14 seed 0c1d95975 stdin
algo 0 length 14 seed 2654435761c1d95975 stdin
algo 0 length 222 seed 0b38662a6 stdin
algo 0 length 222 seed 2654435761b38662a6 stdin
algo 1 length 0 seed 0ef46db3751d8e999 stdin
algo 1 length 0 seed 2654435761ac75fda2929b17ef stdin
algo 1 length 1 seed 027c3f04c2881203a stdin
algo 1 length 1 seed 26544357614a15ed26415dfe4d stdin
algo 1 length 14 seed 03d33dc700231dfad stdin
algo 1 length 14 seed 2654435761ea5f7ddef9a64f80 stdin
algo 1 length 222 seed 05f3d3c08ec2bef34 stdin
algo 1 length 222 seed 26544357616a9df59664c7ed62 stdin
algo 1 is xx64 variant, algo 0 is the 32 bit variant which is currently not hooked up.
---
Changes since v2:
* Use subsys_initcall instead of module_init (Eric Biggers)
* Move xxhash64_setkey before xxhash64_init to follow the logical sequence those function need to be called in (Eric Biggers)
Changes since v1:
* Added SPDX license (Stephan Mueller)
* Fixed module alias defines (Eric Biggers)
* Moved the definition of xxhash_generic object to be after LZ4HC (Stephan Mueller)
* Explained how the test vectors were derived in the commit message (Stephan Mueller)
* Changed XXHASH64_BLOCK_SIZE to 32 (Eric Biggers)
* Renamed xxhash64_crypto_ctx to xxhash64_tfm_ctx, also renamed variables of this type to have a 't' prefix (Eric Biggers)
* Removed redundant storage of 'seed' in xxhash64_desc_ctx
* Switched to using get/put unaligned when reading the seed value from user and when returning a digest. (Eric Biggers)
* Fixed forgotten initialization step in xxhash64_digest (Eric Biggers)
* Misc indentation fixes (Eric Biggers)
* Changed test vectors based on feedback from Nick Terrell
crypto/Kconfig | 8 +++ crypto/Makefile | 1 + crypto/testmgr.c | 7 +++ crypto/testmgr.h | 106 ++++++++++++++++++++++++++++++++++++ crypto/xxhash_generic.c | 115 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 237 insertions(+) create mode 100644 crypto/xxhash_generic.c
crypto/Kconfig +8 -0
@@ -665,6 +665,14 @@ config CRYPTO_CRC32_MIPS instructions, when available. +config CRYPTO_XXHASH + tristate "xxHash hash algorithm" + select CRYPTO_HASH + select XXHASH + help + xxHash non-cryptographic hash algorithm. Extremely fast, working at + speeds close to RAM limits. + config CRYPTO_CRCT10DIF tristate "CRCT10DIF algorithm" select CRYPTO_HASH
crypto/Makefile +1 -0
@@ -131,6 +131,7 @@ obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o authencesn.o obj-$(CONFIG_CRYPTO_LZO) += lzo.o lzo-rle.o obj-$(CONFIG_CRYPTO_LZ4) += lz4.o obj-$(CONFIG_CRYPTO_LZ4HC) += lz4hc.o +obj-$(CONFIG_CRYPTO_XXHASH) += xxhash_generic.o obj-$(CONFIG_CRYPTO_842) += 842.o obj-$(CONFIG_CRYPTO_RNG2) += rng.o obj-$(CONFIG_CRYPTO_ANSI_CPRNG) += ansi_cprng.o
crypto/testmgr.c +7 -0
@@ -3879,6 +3879,13 @@ static const struct alg_test_desc alg_test_descs[] = { .alg = "xts512(paes)", .test = alg_test_null, .fips_allowed = 1, + }, { + .alg = "xxhash64", + .test = alg_test_hash, + .fips_allowed = 1, + .suite = { + .hash = __VECS(xxhash64_tv_template) + } }, { .alg = "zlib-deflate", .test = alg_test_comp,
crypto/testmgr.h +106 -0
112 lines not on this page · raw message
crypto/xxhash_generic.c new file +115 -0
115 lines not on this page · raw message
Signature · 1 line
2.17.1
Hi Nikolay, some more comments from another read through. Sorry for not noticing these in v2.
crypto/xxhash_generic.c line 46
Quoted 4 lines · Nikolay Borisov
crypto/xxhash_generic.c new file +4
-0+static int xxhash64_update (struct shash_desc *desc,const u8 *data, +unsigned int length) +{ +struct xxhash64_desc_ctx *tctx =shash_desc_ctx (desc);
This variable should be named 'dctx' (for desc_ctx), not 'tctx' (for tfm_ctx).
crypto/xxhash_generic.c line 51
Quoted 5 lines
crypto/xxhash_generic.c new file +5
-0+ +xxh64_update (&tctx->xxhstate, data, length); + +return 0 ; +}
xxh64_update() has a return value (0 or -errno) which is not being checked, which at first glance seems to be a bug.
However, it only returns an error in this case:
if (input == NULL)
return -EINVAL;But data=NULL, length=0 are valid parameters to xxhash64_update(), so if you did check the return value, xxhash64_update() would break. So it's fine as-is.
However, if anyone changed xxh64_update() to an error in any other case, xxhash64_update() would break since it ignores the error.
I suggest avoiding this complexity around error codes by changing xxh64_update() to return void. It can be a separate patch.
crypto/xxhash_generic.c line 56
Quoted 5 lines
crypto/xxhash_generic.c new file +5
-0+ +static int xxhash64_final (struct shash_desc *desc,u8 *out) +{ +struct xxhash64_desc_ctx *ctx =shash_desc_ctx (desc); +
For consistency it should be 'dctx' here too.
Quoted 12 lines
crypto/xxhash_generic.c new file +11
-0+put_unaligned_le64 (xxh64_digest (&ctx->xxhstate), out); + +return 0 ; +} + +static int xxhash64_finup (struct shash_desc *desc,const u8 *data, +unsigned int len,u8 *out) +{ +xxhash64_update (desc, data, len); +xxhash64_final (desc, out); +
crypto/xxhash_generic.c line 77
Quoted 10 lines
crypto/xxhash_generic.c new file +10
-0+return 0 ; +} + +static int xxhash64_digest (struct shash_desc *desc,const u8 *data, +unsigned int length,u8 *out) +{ +xxhash64_init (desc); +return xxhash64_finup (desc, data, length, out); +} +
The purpose of the ->finup() and ->digest() methods is to allow the algorithm to work more efficiently than it could if ->init(), ->update(), and ->final() were called separately. So, implementing them on top of xxhash64_init(), xxhash64_update(), and xxhash64_final() is mostly pointless.
lib/xxhash.c already provides a function xxh64() which does a digest in one step, so that should be used to implement xxhash64_digest():
static int xxhash64_digest(struct shash_desc *desc, const u8 *data,
unsigned int length, u8 *out)
{
struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
put_unaligned_le64(xxh64(data, length, tctx->seed), out);
return 0;
}I suggest just deleting xxhash64_finup().
Quoted 9 lines
crypto/xxhash_generic.c new file +9
-0+static struct shash_alg alg = { + .digestsize =XXHASH64_DIGEST_SIZE , + .setkey = xxhash64_setkey, + .init = xxhash64_init, + .update = xxhash64_update, + .final = xxhash64_final, + .finup = xxhash64_finup, + .digest = xxhash64_digest, + .descsize =sizeof (struct xxhash64_desc_ctx ),
crypto/xxhash_generic.c line 96
Quoted 10 lines
crypto/xxhash_generic.c new file +10
-0+ .base = { + .cra_name = "xxhash64", + .cra_driver_name = "xxhash64-generic", + .cra_priority = 100, + .cra_flags = CRYPTO_ALG_OPTIONAL_KEY, + .cra_blocksize = XXHASH64_BLOCK_SIZE, + .cra_ctxsize = sizeof(struct xxhash64_tfm_ctx), + .cra_module = THIS_MODULE, + } +};
Note that because .export() and .import() aren't set, if someone calls crypto_shash_export() and crypto_shash_import() on an xxhash64 descriptor, the crypto API will export and import the state by memcpy().
That's perfectly fine, but it also means that the xxh64_copy_state() function won't be called. Since it exists, one might assume that all state copies go through that function. But it's actually pointless as it just does a memcpy, so I suggest removing it. (As a separate patch, which you don't necessarily have to do now. BTW, another thing that should be cleaned up is the random unnecessary local variable in xxh32_reset() and xxh64_reset()...)
Thanks,
- Eric
Quoted 108 lines · Eric Biggers
Hi Nikolay, some more comments from another read through. Sorry for not noticing these in v2.
crypto/xxhash_generic.c line 46
Quoted 4 lines · Nikolay Borisov
crypto/xxhash_generic.c new file +4
-0+static int xxhash64_update (struct shash_desc *desc,const u8 *data, +unsigned int length) +{ +struct xxhash64_desc_ctx *tctx =shash_desc_ctx (desc);This variable should be named 'dctx' (for
desc_ctx), not 'tctx' (fortfm_ctx).>crypto/xxhash_generic.c line 51
Quoted 5 lines
crypto/xxhash_generic.c new file +5
-0+ +xxh64_update (&tctx->xxhstate, data, length); + +return 0 ; +}
xxh64_update()has a return value (0 or -errno) which is not being checked, which at first glance seems to be a bug.However, it only returns an error in this case:
if (input == NULL) return -EINVAL;But data=NULL, length=0 are valid parameters to
xxhash64_update(), so if you did check the return value,xxhash64_update()would break. So it's fine as-is.However, if anyone changed
xxh64_update()to an error in any other case,xxhash64_update()would break since it ignores the error.I suggest avoiding this complexity around error codes by changing
xxh64_update()to return void. It can be a separate patch.crypto/xxhash_generic.c line 56
Quoted 5 lines
crypto/xxhash_generic.c new file +5
-0+ +static int xxhash64_final (struct shash_desc *desc,u8 *out) +{ +struct xxhash64_desc_ctx *ctx =shash_desc_ctx (desc); +For consistency it should be 'dctx' here too.
Quoted 12 lines
crypto/xxhash_generic.c new file +11
-0+put_unaligned_le64 (xxh64_digest (&ctx->xxhstate), out); + +return 0 ; +} + +static int xxhash64_finup (struct shash_desc *desc,const u8 *data, +unsigned int len,u8 *out) +{ +xxhash64_update (desc, data, len); +xxhash64_final (desc, out); +crypto/xxhash_generic.c line 77
Quoted 10 lines
crypto/xxhash_generic.c new file +10
-0+return 0 ; +} + +static int xxhash64_digest (struct shash_desc *desc,const u8 *data, +unsigned int length,u8 *out) +{ +xxhash64_init (desc); +return xxhash64_finup (desc, data, length, out); +} +The purpose of the ->finup() and ->digest() methods is to allow the algorithm to work more efficiently than it could if ->init(), ->update(), and ->final() were called separately. So, implementing them on top of
xxhash64_init(),xxhash64_update(), andxxhash64_final()is mostly pointless.
lib/xxhash.calready provides a functionxxh64()which does a digest in one step, so that should be used to implementxxhash64_digest():static int xxhash64_digest(struct
shash_desc*desc, const u8 *data,
unsigned int length, u8 *out){ struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm); put_unaligned_le64(xxh64(data, length, tctx->seed), out); return 0; }I suggest just deleting
xxhash64_finup().Quoted 9 lines
crypto/xxhash_generic.c new file +9
-0+static struct shash_alg alg = { + .digestsize =XXHASH64_DIGEST_SIZE , + .setkey = xxhash64_setkey, + .init = xxhash64_init, + .update = xxhash64_update, + .final = xxhash64_final, + .finup = xxhash64_finup, + .digest = xxhash64_digest, + .descsize =sizeof (struct xxhash64_desc_ctx ),crypto/xxhash_generic.c line 96
Quoted 10 lines
crypto/xxhash_generic.c new file +10
-0+ .base = { + .cra_name = "xxhash64", + .cra_driver_name = "xxhash64-generic", + .cra_priority = 100, + .cra_flags = CRYPTO_ALG_OPTIONAL_KEY, + .cra_blocksize = XXHASH64_BLOCK_SIZE, + .cra_ctxsize = sizeof(struct xxhash64_tfm_ctx), + .cra_module = THIS_MODULE, + } +};Note that because .export() and .import() aren't set, if someone calls
crypto_shash_export()andcrypto_shash_import()on an xxhash64 descriptor, the crypto API will export and import the state bymemcpy().
Quoted 6 lines
That's perfectly fine, but it also means that the
xxh64_copy_state()function won't be called. Since it exists, one might assume that all state copies go through that function. But it's actually pointless as it just does a memcpy, so I suggest removing it. (As a separate patch, which you don't necessarily have to do now. BTW, another thing that should be cleaned up is the random unnecessary local variable inxxh32_reset()andxxh64_reset()...)
Good suggestion, however they stretch beyond what I'm currently comfortable doing. I will do a v4 which deals with the minor inconsistencies in the crypto module (variable naming, removing finup, implementing digest via xxh64), however I will definitely won't be doing any changes in lib/xxhash.c now. I don't think any of the things you mentioned in that regard are a blockers towards merging crypto layer support for xxhash.
Quoted 5 lines
Thanks,
- Eric