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