Merge tag 'phy-for-5.6_v2' of git://git.kernel.org/pub/scm/linux/kernel/git/kishon...
[linux/fpc-iii.git] / crypto / xxhash_generic.c
blob4aad2c0f40a9f6a07e2f1382f3d529c0eb1758b5
1 // SPDX-License-Identifier: GPL-2.0
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>
9 #define XXHASH64_BLOCK_SIZE 32
10 #define XXHASH64_DIGEST_SIZE 8
12 struct xxhash64_tfm_ctx {
13 u64 seed;
16 struct xxhash64_desc_ctx {
17 struct xxh64_state xxhstate;
20 static int xxhash64_setkey(struct crypto_shash *tfm, const u8 *key,
21 unsigned int keylen)
23 struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(tfm);
25 if (keylen != sizeof(tctx->seed)) {
26 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
27 return -EINVAL;
29 tctx->seed = get_unaligned_le64(key);
30 return 0;
33 static int xxhash64_init(struct shash_desc *desc)
35 struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
36 struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
38 xxh64_reset(&dctx->xxhstate, tctx->seed);
40 return 0;
43 static int xxhash64_update(struct shash_desc *desc, const u8 *data,
44 unsigned int length)
46 struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
48 xxh64_update(&dctx->xxhstate, data, length);
50 return 0;
53 static int xxhash64_final(struct shash_desc *desc, u8 *out)
55 struct xxhash64_desc_ctx *dctx = shash_desc_ctx(desc);
57 put_unaligned_le64(xxh64_digest(&dctx->xxhstate), out);
59 return 0;
62 static int xxhash64_digest(struct shash_desc *desc, const u8 *data,
63 unsigned int length, u8 *out)
65 struct xxhash64_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
67 put_unaligned_le64(xxh64(data, length, tctx->seed), out);
69 return 0;
72 static 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,
91 static int __init xxhash_mod_init(void)
93 return crypto_register_shash(&alg);
96 static void __exit xxhash_mod_fini(void)
98 crypto_unregister_shash(&alg);
101 subsys_initcall(xxhash_mod_init);
102 module_exit(xxhash_mod_fini);
104 MODULE_AUTHOR("Nikolay Borisov <nborisov@suse.com>");
105 MODULE_DESCRIPTION("xxhash calculations wrapper for lib/xxhash.c");
106 MODULE_LICENSE("GPL");
107 MODULE_ALIAS_CRYPTO("xxhash64");
108 MODULE_ALIAS_CRYPTO("xxhash64-generic");