18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h>
48c2ecf20Sopenharmony_ci#include <linux/init.h>
58c2ecf20Sopenharmony_ci#include <linux/module.h>
68c2ecf20Sopenharmony_ci#include <linux/xxhash.h>
78c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#define XXHASH64_BLOCK_SIZE	32
108c2ecf20Sopenharmony_ci#define XXHASH64_DIGEST_SIZE	8
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistruct xxhash64_tfm_ctx {
138c2ecf20Sopenharmony_ci	u64 seed;
148c2ecf20Sopenharmony_ci};
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistruct xxhash64_desc_ctx {
178c2ecf20Sopenharmony_ci	struct xxh64_state xxhstate;
188c2ecf20Sopenharmony_ci};
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic int xxhash64_setkey(struct crypto_shash *tfm, const u8 *key,
218c2ecf20Sopenharmony_ci			 unsigned int keylen)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(tfm);
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	if (keylen != sizeof(tctx->seed))
268c2ecf20Sopenharmony_ci		return -EINVAL;
278c2ecf20Sopenharmony_ci	tctx->seed = get_unaligned_le64(key);
288c2ecf20Sopenharmony_ci	return 0;
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic int xxhash64_init(struct shash_desc *desc)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
348c2ecf20Sopenharmony_ci	struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	xxh64_reset(&dctx->xxhstate, tctx->seed);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	return 0;
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic int xxhash64_update(struct shash_desc *desc, const u8 *data,
428c2ecf20Sopenharmony_ci			 unsigned int length)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	xxh64_update(&dctx->xxhstate, data, length);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	return 0;
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int xxhash64_final(struct shash_desc *desc, u8 *out)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	put_unaligned_le64(xxh64_digest(&dctx->xxhstate), out);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return 0;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic int xxhash64_digest(struct shash_desc *desc, const u8 *data,
618c2ecf20Sopenharmony_ci			 unsigned int length, u8 *out)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	put_unaligned_le64(xxh64(data, length, tctx->seed), out);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	return 0;
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic struct shash_alg alg = {
718c2ecf20Sopenharmony_ci	.digestsize	= XXHASH64_DIGEST_SIZE,
728c2ecf20Sopenharmony_ci	.setkey		= xxhash64_setkey,
738c2ecf20Sopenharmony_ci	.init		= xxhash64_init,
748c2ecf20Sopenharmony_ci	.update		= xxhash64_update,
758c2ecf20Sopenharmony_ci	.final		= xxhash64_final,
768c2ecf20Sopenharmony_ci	.digest		= xxhash64_digest,
778c2ecf20Sopenharmony_ci	.descsize	= sizeof(struct xxhash64_desc_ctx),
788c2ecf20Sopenharmony_ci	.base		= {
798c2ecf20Sopenharmony_ci		.cra_name	 = "xxhash64",
808c2ecf20Sopenharmony_ci		.cra_driver_name = "xxhash64-generic",
818c2ecf20Sopenharmony_ci		.cra_priority	 = 100,
828c2ecf20Sopenharmony_ci		.cra_flags	 = CRYPTO_ALG_OPTIONAL_KEY,
838c2ecf20Sopenharmony_ci		.cra_blocksize	 = XXHASH64_BLOCK_SIZE,
848c2ecf20Sopenharmony_ci		.cra_ctxsize	 = sizeof(struct xxhash64_tfm_ctx),
858c2ecf20Sopenharmony_ci		.cra_module	 = THIS_MODULE,
868c2ecf20Sopenharmony_ci	}
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic int __init xxhash_mod_init(void)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	return crypto_register_shash(&alg);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic void __exit xxhash_mod_fini(void)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	crypto_unregister_shash(&alg);
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cisubsys_initcall(xxhash_mod_init);
1008c2ecf20Sopenharmony_cimodule_exit(xxhash_mod_fini);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ciMODULE_AUTHOR("Nikolay Borisov <nborisov@suse.com>");
1038c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("xxhash calculations wrapper for lib/xxhash.c");
1048c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1058c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("xxhash64");
1068c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("xxhash64-generic");
107