Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2
  3#include <crypto/internal/hash.h>
  4#include <linux/init.h>
  5#include <linux/module.h>
  6#include <linux/xxhash.h>
  7#include <asm/unaligned.h>
  8
  9#define XXHASH64_BLOCK_SIZE	32
 10#define XXHASH64_DIGEST_SIZE	8
 11
 12struct xxhash64_tfm_ctx {
 13	u64 seed;
 14};
 15
 16struct xxhash64_desc_ctx {
 17	struct xxh64_state xxhstate;
 18};
 19
 20static int xxhash64_setkey(struct crypto_shash *tfm, const u8 *key,
 21			 unsigned int keylen)
 22{
 23	struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(tfm);
 24
 25	if (keylen != sizeof(tctx->seed))
 
 26		return -EINVAL;
 
 27	tctx->seed = get_unaligned_le64(key);
 28	return 0;
 29}
 30
 31static int xxhash64_init(struct shash_desc *desc)
 32{
 33	struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
 34	struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
 35
 36	xxh64_reset(&dctx->xxhstate, tctx->seed);
 37
 38	return 0;
 39}
 40
 41static int xxhash64_update(struct shash_desc *desc, const u8 *data,
 42			 unsigned int length)
 43{
 44	struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
 45
 46	xxh64_update(&dctx->xxhstate, data, length);
 47
 48	return 0;
 49}
 50
 51static int xxhash64_final(struct shash_desc *desc, u8 *out)
 52{
 53	struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
 54
 55	put_unaligned_le64(xxh64_digest(&dctx->xxhstate), out);
 56
 57	return 0;
 58}
 59
 60static int xxhash64_digest(struct shash_desc *desc, const u8 *data,
 61			 unsigned int length, u8 *out)
 62{
 63	struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
 64
 65	put_unaligned_le64(xxh64(data, length, tctx->seed), out);
 66
 67	return 0;
 68}
 69
 70static struct shash_alg alg = {
 71	.digestsize	= XXHASH64_DIGEST_SIZE,
 72	.setkey		= xxhash64_setkey,
 73	.init		= xxhash64_init,
 74	.update		= xxhash64_update,
 75	.final		= xxhash64_final,
 76	.digest		= xxhash64_digest,
 77	.descsize	= sizeof(struct xxhash64_desc_ctx),
 78	.base		= {
 79		.cra_name	 = "xxhash64",
 80		.cra_driver_name = "xxhash64-generic",
 81		.cra_priority	 = 100,
 82		.cra_flags	 = CRYPTO_ALG_OPTIONAL_KEY,
 83		.cra_blocksize	 = XXHASH64_BLOCK_SIZE,
 84		.cra_ctxsize	 = sizeof(struct xxhash64_tfm_ctx),
 85		.cra_module	 = THIS_MODULE,
 86	}
 87};
 88
 89static int __init xxhash_mod_init(void)
 90{
 91	return crypto_register_shash(&alg);
 92}
 93
 94static void __exit xxhash_mod_fini(void)
 95{
 96	crypto_unregister_shash(&alg);
 97}
 98
 99subsys_initcall(xxhash_mod_init);
100module_exit(xxhash_mod_fini);
101
102MODULE_AUTHOR("Nikolay Borisov <nborisov@suse.com>");
103MODULE_DESCRIPTION("xxhash calculations wrapper for lib/xxhash.c");
104MODULE_LICENSE("GPL");
105MODULE_ALIAS_CRYPTO("xxhash64");
106MODULE_ALIAS_CRYPTO("xxhash64-generic");
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2
  3#include <crypto/internal/hash.h>
  4#include <linux/init.h>
  5#include <linux/module.h>
  6#include <linux/xxhash.h>
  7#include <asm/unaligned.h>
  8
  9#define XXHASH64_BLOCK_SIZE	32
 10#define XXHASH64_DIGEST_SIZE	8
 11
 12struct xxhash64_tfm_ctx {
 13	u64 seed;
 14};
 15
 16struct xxhash64_desc_ctx {
 17	struct xxh64_state xxhstate;
 18};
 19
 20static int xxhash64_setkey(struct crypto_shash *tfm, const u8 *key,
 21			 unsigned int keylen)
 22{
 23	struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(tfm);
 24
 25	if (keylen != sizeof(tctx->seed)) {
 26		crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
 27		return -EINVAL;
 28	}
 29	tctx->seed = get_unaligned_le64(key);
 30	return 0;
 31}
 32
 33static int xxhash64_init(struct shash_desc *desc)
 34{
 35	struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
 36	struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
 37
 38	xxh64_reset(&dctx->xxhstate, tctx->seed);
 39
 40	return 0;
 41}
 42
 43static int xxhash64_update(struct shash_desc *desc, const u8 *data,
 44			 unsigned int length)
 45{
 46	struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
 47
 48	xxh64_update(&dctx->xxhstate, data, length);
 49
 50	return 0;
 51}
 52
 53static int xxhash64_final(struct shash_desc *desc, u8 *out)
 54{
 55	struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
 56
 57	put_unaligned_le64(xxh64_digest(&dctx->xxhstate), out);
 58
 59	return 0;
 60}
 61
 62static int xxhash64_digest(struct shash_desc *desc, const u8 *data,
 63			 unsigned int length, u8 *out)
 64{
 65	struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
 66
 67	put_unaligned_le64(xxh64(data, length, tctx->seed), out);
 68
 69	return 0;
 70}
 71
 72static struct shash_alg alg = {
 73	.digestsize	= XXHASH64_DIGEST_SIZE,
 74	.setkey		= xxhash64_setkey,
 75	.init		= xxhash64_init,
 76	.update		= xxhash64_update,
 77	.final		= xxhash64_final,
 78	.digest		= xxhash64_digest,
 79	.descsize	= sizeof(struct xxhash64_desc_ctx),
 80	.base		= {
 81		.cra_name	 = "xxhash64",
 82		.cra_driver_name = "xxhash64-generic",
 83		.cra_priority	 = 100,
 84		.cra_flags	 = CRYPTO_ALG_OPTIONAL_KEY,
 85		.cra_blocksize	 = XXHASH64_BLOCK_SIZE,
 86		.cra_ctxsize	 = sizeof(struct xxhash64_tfm_ctx),
 87		.cra_module	 = THIS_MODULE,
 88	}
 89};
 90
 91static int __init xxhash_mod_init(void)
 92{
 93	return crypto_register_shash(&alg);
 94}
 95
 96static void __exit xxhash_mod_fini(void)
 97{
 98	crypto_unregister_shash(&alg);
 99}
100
101subsys_initcall(xxhash_mod_init);
102module_exit(xxhash_mod_fini);
103
104MODULE_AUTHOR("Nikolay Borisov <nborisov@suse.com>");
105MODULE_DESCRIPTION("xxhash calculations wrapper for lib/xxhash.c");
106MODULE_LICENSE("GPL");
107MODULE_ALIAS_CRYPTO("xxhash64");
108MODULE_ALIAS_CRYPTO("xxhash64-generic");