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