~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/riscv/crypto/sm4-riscv64-glue.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * SM4 using the RISC-V vector crypto extensions
  4  *
  5  * Copyright (C) 2023 VRULL GmbH
  6  * Author: Heiko Stuebner <heiko.stuebner@vrull.eu>
  7  *
  8  * Copyright (C) 2023 SiFive, Inc.
  9  * Author: Jerry Shih <jerry.shih@sifive.com>
 10  */
 11 
 12 #include <asm/simd.h>
 13 #include <asm/vector.h>
 14 #include <crypto/internal/cipher.h>
 15 #include <crypto/internal/simd.h>
 16 #include <crypto/sm4.h>
 17 #include <linux/linkage.h>
 18 #include <linux/module.h>
 19 
 20 asmlinkage void sm4_expandkey_zvksed_zvkb(const u8 user_key[SM4_KEY_SIZE],
 21                                           u32 rkey_enc[SM4_RKEY_WORDS],
 22                                           u32 rkey_dec[SM4_RKEY_WORDS]);
 23 asmlinkage void sm4_crypt_zvksed_zvkb(const u32 rkey[SM4_RKEY_WORDS],
 24                                       const u8 in[SM4_BLOCK_SIZE],
 25                                       u8 out[SM4_BLOCK_SIZE]);
 26 
 27 static int riscv64_sm4_setkey(struct crypto_tfm *tfm, const u8 *key,
 28                               unsigned int keylen)
 29 {
 30         struct sm4_ctx *ctx = crypto_tfm_ctx(tfm);
 31 
 32         if (crypto_simd_usable()) {
 33                 if (keylen != SM4_KEY_SIZE)
 34                         return -EINVAL;
 35                 kernel_vector_begin();
 36                 sm4_expandkey_zvksed_zvkb(key, ctx->rkey_enc, ctx->rkey_dec);
 37                 kernel_vector_end();
 38                 return 0;
 39         }
 40         return sm4_expandkey(ctx, key, keylen);
 41 }
 42 
 43 static void riscv64_sm4_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
 44 {
 45         const struct sm4_ctx *ctx = crypto_tfm_ctx(tfm);
 46 
 47         if (crypto_simd_usable()) {
 48                 kernel_vector_begin();
 49                 sm4_crypt_zvksed_zvkb(ctx->rkey_enc, src, dst);
 50                 kernel_vector_end();
 51         } else {
 52                 sm4_crypt_block(ctx->rkey_enc, dst, src);
 53         }
 54 }
 55 
 56 static void riscv64_sm4_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
 57 {
 58         const struct sm4_ctx *ctx = crypto_tfm_ctx(tfm);
 59 
 60         if (crypto_simd_usable()) {
 61                 kernel_vector_begin();
 62                 sm4_crypt_zvksed_zvkb(ctx->rkey_dec, src, dst);
 63                 kernel_vector_end();
 64         } else {
 65                 sm4_crypt_block(ctx->rkey_dec, dst, src);
 66         }
 67 }
 68 
 69 static struct crypto_alg riscv64_sm4_alg = {
 70         .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
 71         .cra_blocksize = SM4_BLOCK_SIZE,
 72         .cra_ctxsize = sizeof(struct sm4_ctx),
 73         .cra_priority = 300,
 74         .cra_name = "sm4",
 75         .cra_driver_name = "sm4-riscv64-zvksed-zvkb",
 76         .cra_cipher = {
 77                 .cia_min_keysize = SM4_KEY_SIZE,
 78                 .cia_max_keysize = SM4_KEY_SIZE,
 79                 .cia_setkey = riscv64_sm4_setkey,
 80                 .cia_encrypt = riscv64_sm4_encrypt,
 81                 .cia_decrypt = riscv64_sm4_decrypt,
 82         },
 83         .cra_module = THIS_MODULE,
 84 };
 85 
 86 static int __init riscv64_sm4_mod_init(void)
 87 {
 88         if (riscv_isa_extension_available(NULL, ZVKSED) &&
 89             riscv_isa_extension_available(NULL, ZVKB) &&
 90             riscv_vector_vlen() >= 128)
 91                 return crypto_register_alg(&riscv64_sm4_alg);
 92 
 93         return -ENODEV;
 94 }
 95 
 96 static void __exit riscv64_sm4_mod_exit(void)
 97 {
 98         crypto_unregister_alg(&riscv64_sm4_alg);
 99 }
100 
101 module_init(riscv64_sm4_mod_init);
102 module_exit(riscv64_sm4_mod_exit);
103 
104 MODULE_DESCRIPTION("SM4 (RISC-V accelerated)");
105 MODULE_AUTHOR("Heiko Stuebner <heiko.stuebner@vrull.eu>");
106 MODULE_LICENSE("GPL");
107 MODULE_ALIAS_CRYPTO("sm4");
108 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php