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
{
16 struct xxhash64_desc_ctx
{
17 struct xxh64_state xxhstate
;
20 static int xxhash64_setkey(struct crypto_shash
*tfm
, const u8
*key
,
23 struct xxhash64_tfm_ctx
*tctx
= crypto_shash_ctx(tfm
);
25 if (keylen
!= sizeof(tctx
->seed
))
27 tctx
->seed
= get_unaligned_le64(key
);
31 static int xxhash64_init(struct shash_desc
*desc
)
33 struct xxhash64_tfm_ctx
*tctx
= crypto_shash_ctx(desc
->tfm
);
34 struct xxhash64_desc_ctx
*dctx
= shash_desc_ctx(desc
);
36 xxh64_reset(&dctx
->xxhstate
, tctx
->seed
);
41 static int xxhash64_update(struct shash_desc
*desc
, const u8
*data
,
44 struct xxhash64_desc_ctx
*dctx
= shash_desc_ctx(desc
);
46 xxh64_update(&dctx
->xxhstate
, data
, length
);
51 static int xxhash64_final(struct shash_desc
*desc
, u8
*out
)
53 struct xxhash64_desc_ctx
*dctx
= shash_desc_ctx(desc
);
55 put_unaligned_le64(xxh64_digest(&dctx
->xxhstate
), out
);
60 static int xxhash64_digest(struct shash_desc
*desc
, const u8
*data
,
61 unsigned int length
, u8
*out
)
63 struct xxhash64_tfm_ctx
*tctx
= crypto_shash_ctx(desc
->tfm
);
65 put_unaligned_le64(xxh64(data
, length
, tctx
->seed
), out
);
70 static 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
),
79 .cra_name
= "xxhash64",
80 .cra_driver_name
= "xxhash64-generic",
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
,
89 static int __init
xxhash_mod_init(void)
91 return crypto_register_shash(&alg
);
94 static void __exit
xxhash_mod_fini(void)
96 crypto_unregister_shash(&alg
);
99 subsys_initcall(xxhash_mod_init
);
100 module_exit(xxhash_mod_fini
);
102 MODULE_AUTHOR("Nikolay Borisov <nborisov@suse.com>");
103 MODULE_DESCRIPTION("xxhash calculations wrapper for lib/xxhash.c");
104 MODULE_LICENSE("GPL");
105 MODULE_ALIAS_CRYPTO("xxhash64");
106 MODULE_ALIAS_CRYPTO("xxhash64-generic");