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 int sm4_ce_setkey(struct crypto_tfm *tfm, const u8 *key,
23 struct sm4_ctx *ctx = crypto_tfm_ctx(tfm);
25 return sm4_expandkey(ctx, key, key_len);
28 static void sm4_ce_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
30 const struct sm4_ctx *ctx = crypto_tfm_ctx(tfm);
32 if (!crypto_simd_usable()) {
33 sm4_crypt_block(ctx->rkey_enc, out, in);
36 sm4_ce_do_crypt(ctx->rkey_enc, out, in);
41 static void sm4_ce_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
43 const struct sm4_ctx *ctx = crypto_tfm_ctx(tfm);
45 if (!crypto_simd_usable()) {
46 sm4_crypt_block(ctx->rkey_dec, out, in);
49 sm4_ce_do_crypt(ctx->rkey_dec, out, in);
54 static struct crypto_alg sm4_ce_alg = {
56 .cra_driver_name = "sm4-ce",
58 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
59 .cra_blocksize = SM4_BLOCK_SIZE,
60 .cra_ctxsize = sizeof(struct sm4_ctx),
61 .cra_module = THIS_MODULE,
63 .cia_min_keysize = SM4_KEY_SIZE,
64 .cia_max_keysize = SM4_KEY_SIZE,
65 .cia_setkey = sm4_ce_setkey,
66 .cia_encrypt = sm4_ce_encrypt,
67 .cia_decrypt = sm4_ce_decrypt
71 static int __init sm4_ce_mod_init(void)
73 return crypto_register_alg(&sm4_ce_alg);
76 static void __exit sm4_ce_mod_fini(void)
78 crypto_unregister_alg(&sm4_ce_alg);
81 module_cpu_feature_match(SM4, sm4_ce_mod_init);
82 module_exit(sm4_ce_mod_fini);