xref: /openbmc/linux/arch/arm64/crypto/sm4-ce-glue.c (revision dfd4f649)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <asm/neon.h>
4 #include <asm/simd.h>
5 #include <crypto/sm4.h>
6 #include <linux/module.h>
7 #include <linux/cpufeature.h>
8 #include <linux/crypto.h>
9 #include <linux/types.h>
10 
11 MODULE_ALIAS_CRYPTO("sm4");
12 MODULE_ALIAS_CRYPTO("sm4-ce");
13 MODULE_DESCRIPTION("SM4 symmetric cipher using ARMv8 Crypto Extensions");
14 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
15 MODULE_LICENSE("GPL v2");
16 
17 asmlinkage void sm4_ce_do_crypt(const u32 *rk, void *out, const void *in);
18 
19 static void sm4_ce_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
20 {
21 	const struct crypto_sm4_ctx *ctx = crypto_tfm_ctx(tfm);
22 
23 	if (!may_use_simd()) {
24 		crypto_sm4_encrypt(tfm, out, in);
25 	} else {
26 		kernel_neon_begin();
27 		sm4_ce_do_crypt(ctx->rkey_enc, out, in);
28 		kernel_neon_end();
29 	}
30 }
31 
32 static void sm4_ce_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
33 {
34 	const struct crypto_sm4_ctx *ctx = crypto_tfm_ctx(tfm);
35 
36 	if (!may_use_simd()) {
37 		crypto_sm4_decrypt(tfm, out, in);
38 	} else {
39 		kernel_neon_begin();
40 		sm4_ce_do_crypt(ctx->rkey_dec, out, in);
41 		kernel_neon_end();
42 	}
43 }
44 
45 static struct crypto_alg sm4_ce_alg = {
46 	.cra_name			= "sm4",
47 	.cra_driver_name		= "sm4-ce",
48 	.cra_priority			= 200,
49 	.cra_flags			= CRYPTO_ALG_TYPE_CIPHER,
50 	.cra_blocksize			= SM4_BLOCK_SIZE,
51 	.cra_ctxsize			= sizeof(struct crypto_sm4_ctx),
52 	.cra_module			= THIS_MODULE,
53 	.cra_u.cipher = {
54 		.cia_min_keysize	= SM4_KEY_SIZE,
55 		.cia_max_keysize	= SM4_KEY_SIZE,
56 		.cia_setkey		= crypto_sm4_set_key,
57 		.cia_encrypt		= sm4_ce_encrypt,
58 		.cia_decrypt		= sm4_ce_decrypt
59 	}
60 };
61 
62 static int __init sm4_ce_mod_init(void)
63 {
64 	return crypto_register_alg(&sm4_ce_alg);
65 }
66 
67 static void __exit sm4_ce_mod_fini(void)
68 {
69 	crypto_unregister_alg(&sm4_ce_alg);
70 }
71 
72 module_cpu_feature_match(SM4, sm4_ce_mod_init);
73 module_exit(sm4_ce_mod_fini);
74