18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci#include <asm/neon.h>
48c2ecf20Sopenharmony_ci#include <asm/simd.h>
58c2ecf20Sopenharmony_ci#include <crypto/sm4.h>
68c2ecf20Sopenharmony_ci#include <crypto/internal/simd.h>
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/cpufeature.h>
98c2ecf20Sopenharmony_ci#include <linux/crypto.h>
108c2ecf20Sopenharmony_ci#include <linux/types.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("sm4");
138c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("sm4-ce");
148c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SM4 symmetric cipher using ARMv8 Crypto Extensions");
158c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
168c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciasmlinkage void sm4_ce_do_crypt(const u32 *rk, void *out, const void *in);
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic void sm4_ce_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	const struct crypto_sm4_ctx *ctx = crypto_tfm_ctx(tfm);
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	if (!crypto_simd_usable()) {
258c2ecf20Sopenharmony_ci		crypto_sm4_encrypt(tfm, out, in);
268c2ecf20Sopenharmony_ci	} else {
278c2ecf20Sopenharmony_ci		kernel_neon_begin();
288c2ecf20Sopenharmony_ci		sm4_ce_do_crypt(ctx->rkey_enc, out, in);
298c2ecf20Sopenharmony_ci		kernel_neon_end();
308c2ecf20Sopenharmony_ci	}
318c2ecf20Sopenharmony_ci}
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic void sm4_ce_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	const struct crypto_sm4_ctx *ctx = crypto_tfm_ctx(tfm);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	if (!crypto_simd_usable()) {
388c2ecf20Sopenharmony_ci		crypto_sm4_decrypt(tfm, out, in);
398c2ecf20Sopenharmony_ci	} else {
408c2ecf20Sopenharmony_ci		kernel_neon_begin();
418c2ecf20Sopenharmony_ci		sm4_ce_do_crypt(ctx->rkey_dec, out, in);
428c2ecf20Sopenharmony_ci		kernel_neon_end();
438c2ecf20Sopenharmony_ci	}
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic struct crypto_alg sm4_ce_alg = {
478c2ecf20Sopenharmony_ci	.cra_name			= "sm4",
488c2ecf20Sopenharmony_ci	.cra_driver_name		= "sm4-ce",
498c2ecf20Sopenharmony_ci	.cra_priority			= 200,
508c2ecf20Sopenharmony_ci	.cra_flags			= CRYPTO_ALG_TYPE_CIPHER,
518c2ecf20Sopenharmony_ci	.cra_blocksize			= SM4_BLOCK_SIZE,
528c2ecf20Sopenharmony_ci	.cra_ctxsize			= sizeof(struct crypto_sm4_ctx),
538c2ecf20Sopenharmony_ci	.cra_module			= THIS_MODULE,
548c2ecf20Sopenharmony_ci	.cra_u.cipher = {
558c2ecf20Sopenharmony_ci		.cia_min_keysize	= SM4_KEY_SIZE,
568c2ecf20Sopenharmony_ci		.cia_max_keysize	= SM4_KEY_SIZE,
578c2ecf20Sopenharmony_ci		.cia_setkey		= crypto_sm4_set_key,
588c2ecf20Sopenharmony_ci		.cia_encrypt		= sm4_ce_encrypt,
598c2ecf20Sopenharmony_ci		.cia_decrypt		= sm4_ce_decrypt
608c2ecf20Sopenharmony_ci	}
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic int __init sm4_ce_mod_init(void)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	return crypto_register_alg(&sm4_ce_alg);
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic void __exit sm4_ce_mod_fini(void)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	crypto_unregister_alg(&sm4_ce_alg);
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cimodule_cpu_feature_match(SM4, sm4_ce_mod_init);
748c2ecf20Sopenharmony_cimodule_exit(sm4_ce_mod_fini);
75