xref: /linux/arch/arm64/crypto/sm4-ce-cipher-glue.c (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <asm/neon.h>
4 #include <asm/simd.h>
5 #include <crypto/algapi.h>
6 #include <crypto/sm4.h>
7 #include <crypto/internal/simd.h>
8 #include <linux/module.h>
9 #include <linux/cpufeature.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 int sm4_ce_setkey(struct crypto_tfm *tfm, const u8 *key,
21 		       unsigned int key_len)
22 {
23 	struct sm4_ctx *ctx = crypto_tfm_ctx(tfm);
24 
25 	return sm4_expandkey(ctx, key, key_len);
26 }
27 
28 static void sm4_ce_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
29 {
30 	const struct sm4_ctx *ctx = crypto_tfm_ctx(tfm);
31 
32 	if (!crypto_simd_usable()) {
33 		sm4_crypt_block(ctx->rkey_enc, out, in);
34 	} else {
35 		scoped_ksimd()
36 			sm4_ce_do_crypt(ctx->rkey_enc, out, in);
37 	}
38 }
39 
40 static void sm4_ce_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
41 {
42 	const struct sm4_ctx *ctx = crypto_tfm_ctx(tfm);
43 
44 	if (!crypto_simd_usable()) {
45 		sm4_crypt_block(ctx->rkey_dec, out, in);
46 	} else {
47 		scoped_ksimd()
48 			sm4_ce_do_crypt(ctx->rkey_dec, out, in);
49 	}
50 }
51 
52 static struct crypto_alg sm4_ce_alg = {
53 	.cra_name			= "sm4",
54 	.cra_driver_name		= "sm4-ce",
55 	.cra_priority			= 300,
56 	.cra_flags			= CRYPTO_ALG_TYPE_CIPHER,
57 	.cra_blocksize			= SM4_BLOCK_SIZE,
58 	.cra_ctxsize			= sizeof(struct sm4_ctx),
59 	.cra_module			= THIS_MODULE,
60 	.cra_u.cipher = {
61 		.cia_min_keysize	= SM4_KEY_SIZE,
62 		.cia_max_keysize	= SM4_KEY_SIZE,
63 		.cia_setkey		= sm4_ce_setkey,
64 		.cia_encrypt		= sm4_ce_encrypt,
65 		.cia_decrypt		= sm4_ce_decrypt
66 	}
67 };
68 
69 static int __init sm4_ce_mod_init(void)
70 {
71 	return crypto_register_alg(&sm4_ce_alg);
72 }
73 
74 static void __exit sm4_ce_mod_fini(void)
75 {
76 	crypto_unregister_alg(&sm4_ce_alg);
77 }
78 
79 module_cpu_feature_match(SM4, sm4_ce_mod_init);
80 module_exit(sm4_ce_mod_fini);
81