Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <asm/neon.h>
0004 #include <asm/simd.h>
0005 #include <crypto/sm4.h>
0006 #include <crypto/internal/simd.h>
0007 #include <linux/module.h>
0008 #include <linux/cpufeature.h>
0009 #include <linux/crypto.h>
0010 #include <linux/types.h>
0011 
0012 MODULE_ALIAS_CRYPTO("sm4");
0013 MODULE_ALIAS_CRYPTO("sm4-ce");
0014 MODULE_DESCRIPTION("SM4 symmetric cipher using ARMv8 Crypto Extensions");
0015 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
0016 MODULE_LICENSE("GPL v2");
0017 
0018 asmlinkage void sm4_ce_do_crypt(const u32 *rk, void *out, const void *in);
0019 
0020 static int sm4_ce_setkey(struct crypto_tfm *tfm, const u8 *key,
0021                unsigned int key_len)
0022 {
0023     struct sm4_ctx *ctx = crypto_tfm_ctx(tfm);
0024 
0025     return sm4_expandkey(ctx, key, key_len);
0026 }
0027 
0028 static void sm4_ce_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
0029 {
0030     const struct sm4_ctx *ctx = crypto_tfm_ctx(tfm);
0031 
0032     if (!crypto_simd_usable()) {
0033         sm4_crypt_block(ctx->rkey_enc, out, in);
0034     } else {
0035         kernel_neon_begin();
0036         sm4_ce_do_crypt(ctx->rkey_enc, out, in);
0037         kernel_neon_end();
0038     }
0039 }
0040 
0041 static void sm4_ce_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
0042 {
0043     const struct sm4_ctx *ctx = crypto_tfm_ctx(tfm);
0044 
0045     if (!crypto_simd_usable()) {
0046         sm4_crypt_block(ctx->rkey_dec, out, in);
0047     } else {
0048         kernel_neon_begin();
0049         sm4_ce_do_crypt(ctx->rkey_dec, out, in);
0050         kernel_neon_end();
0051     }
0052 }
0053 
0054 static struct crypto_alg sm4_ce_alg = {
0055     .cra_name           = "sm4",
0056     .cra_driver_name        = "sm4-ce",
0057     .cra_priority           = 300,
0058     .cra_flags          = CRYPTO_ALG_TYPE_CIPHER,
0059     .cra_blocksize          = SM4_BLOCK_SIZE,
0060     .cra_ctxsize            = sizeof(struct sm4_ctx),
0061     .cra_module         = THIS_MODULE,
0062     .cra_u.cipher = {
0063         .cia_min_keysize    = SM4_KEY_SIZE,
0064         .cia_max_keysize    = SM4_KEY_SIZE,
0065         .cia_setkey     = sm4_ce_setkey,
0066         .cia_encrypt        = sm4_ce_encrypt,
0067         .cia_decrypt        = sm4_ce_decrypt
0068     }
0069 };
0070 
0071 static int __init sm4_ce_mod_init(void)
0072 {
0073     return crypto_register_alg(&sm4_ce_alg);
0074 }
0075 
0076 static void __exit sm4_ce_mod_fini(void)
0077 {
0078     crypto_unregister_alg(&sm4_ce_alg);
0079 }
0080 
0081 module_cpu_feature_match(SM4, sm4_ce_mod_init);
0082 module_exit(sm4_ce_mod_fini);