1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  */
5 
6 #include <crypto/internal/blake2s.h>
7 #include <crypto/internal/simd.h>
8 #include <crypto/internal/hash.h>
9 
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/sizes.h>
14 
15 #include <asm/cpufeature.h>
16 #include <asm/processor.h>
17 
crypto_blake2s_update_x86(struct shash_desc *desc, const u8 *in, unsigned int inlen)18 static int crypto_blake2s_update_x86(struct shash_desc *desc,
19 				     const u8 *in, unsigned int inlen)
20 {
21 	return crypto_blake2s_update(desc, in, inlen, false);
22 }
23 
crypto_blake2s_final_x86(struct shash_desc *desc, u8 *out)24 static int crypto_blake2s_final_x86(struct shash_desc *desc, u8 *out)
25 {
26 	return crypto_blake2s_final(desc, out, false);
27 }
28 
29 #define BLAKE2S_ALG(name, driver_name, digest_size)			\
30 	{								\
31 		.base.cra_name		= name,				\
32 		.base.cra_driver_name	= driver_name,			\
33 		.base.cra_priority	= 200,				\
34 		.base.cra_flags		= CRYPTO_ALG_OPTIONAL_KEY,	\
35 		.base.cra_blocksize	= BLAKE2S_BLOCK_SIZE,		\
36 		.base.cra_ctxsize	= sizeof(struct blake2s_tfm_ctx), \
37 		.base.cra_module	= THIS_MODULE,			\
38 		.digestsize		= digest_size,			\
39 		.setkey			= crypto_blake2s_setkey,	\
40 		.init			= crypto_blake2s_init,		\
41 		.update			= crypto_blake2s_update_x86,	\
42 		.final			= crypto_blake2s_final_x86,	\
43 		.descsize		= sizeof(struct blake2s_state),	\
44 	}
45 
46 static struct shash_alg blake2s_algs[] = {
47 	BLAKE2S_ALG("blake2s-128", "blake2s-128-x86", BLAKE2S_128_HASH_SIZE),
48 	BLAKE2S_ALG("blake2s-160", "blake2s-160-x86", BLAKE2S_160_HASH_SIZE),
49 	BLAKE2S_ALG("blake2s-224", "blake2s-224-x86", BLAKE2S_224_HASH_SIZE),
50 	BLAKE2S_ALG("blake2s-256", "blake2s-256-x86", BLAKE2S_256_HASH_SIZE),
51 };
52 
blake2s_mod_init(void)53 static int __init blake2s_mod_init(void)
54 {
55 	if (IS_REACHABLE(CONFIG_CRYPTO_HASH) && boot_cpu_has(X86_FEATURE_SSSE3))
56 		return crypto_register_shashes(blake2s_algs, ARRAY_SIZE(blake2s_algs));
57 	return 0;
58 }
59 
blake2s_mod_exit(void)60 static void __exit blake2s_mod_exit(void)
61 {
62 	if (IS_REACHABLE(CONFIG_CRYPTO_HASH) && boot_cpu_has(X86_FEATURE_SSSE3))
63 		crypto_unregister_shashes(blake2s_algs, ARRAY_SIZE(blake2s_algs));
64 }
65 
66 module_init(blake2s_mod_init);
67 module_exit(blake2s_mod_exit);
68 
69 MODULE_ALIAS_CRYPTO("blake2s-128");
70 MODULE_ALIAS_CRYPTO("blake2s-128-x86");
71 MODULE_ALIAS_CRYPTO("blake2s-160");
72 MODULE_ALIAS_CRYPTO("blake2s-160-x86");
73 MODULE_ALIAS_CRYPTO("blake2s-224");
74 MODULE_ALIAS_CRYPTO("blake2s-224-x86");
75 MODULE_ALIAS_CRYPTO("blake2s-256");
76 MODULE_ALIAS_CRYPTO("blake2s-256-x86");
77 MODULE_LICENSE("GPL v2");
78