1 // SPDX-License-Identifier: GPL-2.0
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>
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");
18 asmlinkage
void sm4_ce_do_crypt(const u32
*rk
, void *out
, const void *in
);
20 static void sm4_ce_encrypt(struct crypto_tfm
*tfm
, u8
*out
, const u8
*in
)
22 const struct crypto_sm4_ctx
*ctx
= crypto_tfm_ctx(tfm
);
24 if (!crypto_simd_usable()) {
25 crypto_sm4_encrypt(tfm
, out
, in
);
28 sm4_ce_do_crypt(ctx
->rkey_enc
, out
, in
);
33 static void sm4_ce_decrypt(struct crypto_tfm
*tfm
, u8
*out
, const u8
*in
)
35 const struct crypto_sm4_ctx
*ctx
= crypto_tfm_ctx(tfm
);
37 if (!crypto_simd_usable()) {
38 crypto_sm4_decrypt(tfm
, out
, in
);
41 sm4_ce_do_crypt(ctx
->rkey_dec
, out
, in
);
46 static struct crypto_alg sm4_ce_alg
= {
48 .cra_driver_name
= "sm4-ce",
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
,
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
63 static int __init
sm4_ce_mod_init(void)
65 return crypto_register_alg(&sm4_ce_alg
);
68 static void __exit
sm4_ce_mod_fini(void)
70 crypto_unregister_alg(&sm4_ce_alg
);
73 module_cpu_feature_match(SM4
, sm4_ce_mod_init
);
74 module_exit(sm4_ce_mod_fini
);