mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-11-01 17:08:10 +00:00
67882e7649
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 0 02cc5d05 stdin algo 0 length 0 seed 2654435761 02cc5d05 stdin algo 0 length 1 seed 0 25201171 stdin algo 0 length 1 seed 2654435761 25201171 stdin algo 0 length 14 seed 0 c1d95975 stdin algo 0 length 14 seed 2654435761 c1d95975 stdin algo 0 length 222 seed 0 b38662a6 stdin algo 0 length 222 seed 2654435761 b38662a6 stdin algo 1 length 0 seed 0 ef46db3751d8e999 stdin algo 1 length 0 seed 2654435761 ac75fda2929b17ef stdin algo 1 length 1 seed 0 27c3f04c2881203a stdin algo 1 length 1 seed 2654435761 4a15ed26415dfe4d stdin algo 1 length 14 seed 0 3d33dc700231dfad stdin algo 1 length 14 seed 2654435761 ea5f7ddef9a64f80 stdin algo 1 length 222 seed 0 5f3d3c08ec2bef34 stdin algo 1 length 222 seed 2654435761 6a9df59664c7ed62 stdin algo 1 is xx64 variant, algo 0 is the 32 bit variant which is currently not hooked up. Signed-off-by: Nikolay Borisov <nborisov@suse.com> Reviewed-by: Eric Biggers <ebiggers@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
108 lines
2.5 KiB
C
108 lines
2.5 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <crypto/internal/hash.h>
|
|
#include <linux/init.h>
|
|
#include <linux/module.h>
|
|
#include <linux/xxhash.h>
|
|
#include <asm/unaligned.h>
|
|
|
|
#define XXHASH64_BLOCK_SIZE 32
|
|
#define XXHASH64_DIGEST_SIZE 8
|
|
|
|
struct xxhash64_tfm_ctx {
|
|
u64 seed;
|
|
};
|
|
|
|
struct xxhash64_desc_ctx {
|
|
struct xxh64_state xxhstate;
|
|
};
|
|
|
|
static int xxhash64_setkey(struct crypto_shash *tfm, const u8 *key,
|
|
unsigned int keylen)
|
|
{
|
|
struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(tfm);
|
|
|
|
if (keylen != sizeof(tctx->seed)) {
|
|
crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
|
|
return -EINVAL;
|
|
}
|
|
tctx->seed = get_unaligned_le64(key);
|
|
return 0;
|
|
}
|
|
|
|
static int xxhash64_init(struct shash_desc *desc)
|
|
{
|
|
struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
|
|
struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
|
|
|
|
xxh64_reset(&dctx->xxhstate, tctx->seed);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int xxhash64_update(struct shash_desc *desc, const u8 *data,
|
|
unsigned int length)
|
|
{
|
|
struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
|
|
|
|
xxh64_update(&dctx->xxhstate, data, length);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int xxhash64_final(struct shash_desc *desc, u8 *out)
|
|
{
|
|
struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
|
|
|
|
put_unaligned_le64(xxh64_digest(&dctx->xxhstate), out);
|
|
|
|
return 0;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
static struct shash_alg alg = {
|
|
.digestsize = XXHASH64_DIGEST_SIZE,
|
|
.setkey = xxhash64_setkey,
|
|
.init = xxhash64_init,
|
|
.update = xxhash64_update,
|
|
.final = xxhash64_final,
|
|
.digest = xxhash64_digest,
|
|
.descsize = sizeof(struct xxhash64_desc_ctx),
|
|
.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,
|
|
}
|
|
};
|
|
|
|
static int __init xxhash_mod_init(void)
|
|
{
|
|
return crypto_register_shash(&alg);
|
|
}
|
|
|
|
static void __exit xxhash_mod_fini(void)
|
|
{
|
|
crypto_unregister_shash(&alg);
|
|
}
|
|
|
|
subsys_initcall(xxhash_mod_init);
|
|
module_exit(xxhash_mod_fini);
|
|
|
|
MODULE_AUTHOR("Nikolay Borisov <nborisov@suse.com>");
|
|
MODULE_DESCRIPTION("xxhash calculations wrapper for lib/xxhash.c");
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_ALIAS_CRYPTO("xxhash64");
|
|
MODULE_ALIAS_CRYPTO("xxhash64-generic");
|