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

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

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ 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-or-later
  2 /*
  3  * SHA-512 and SHA-384 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/hash.h>
 15 #include <crypto/internal/simd.h>
 16 #include <crypto/sha512_base.h>
 17 #include <linux/linkage.h>
 18 #include <linux/module.h>
 19 
 20 /*
 21  * Note: the asm function only uses the 'state' field of struct sha512_state.
 22  * It is assumed to be the first field.
 23  */
 24 asmlinkage void sha512_transform_zvknhb_zvkb(
 25         struct sha512_state *state, const u8 *data, int num_blocks);
 26 
 27 static int riscv64_sha512_update(struct shash_desc *desc, const u8 *data,
 28                                  unsigned int len)
 29 {
 30         /*
 31          * Ensure struct sha512_state begins directly with the SHA-512
 32          * 512-bit internal state, as this is what the asm function expects.
 33          */
 34         BUILD_BUG_ON(offsetof(struct sha512_state, state) != 0);
 35 
 36         if (crypto_simd_usable()) {
 37                 kernel_vector_begin();
 38                 sha512_base_do_update(desc, data, len,
 39                                       sha512_transform_zvknhb_zvkb);
 40                 kernel_vector_end();
 41         } else {
 42                 crypto_sha512_update(desc, data, len);
 43         }
 44         return 0;
 45 }
 46 
 47 static int riscv64_sha512_finup(struct shash_desc *desc, const u8 *data,
 48                                 unsigned int len, u8 *out)
 49 {
 50         if (crypto_simd_usable()) {
 51                 kernel_vector_begin();
 52                 if (len)
 53                         sha512_base_do_update(desc, data, len,
 54                                               sha512_transform_zvknhb_zvkb);
 55                 sha512_base_do_finalize(desc, sha512_transform_zvknhb_zvkb);
 56                 kernel_vector_end();
 57 
 58                 return sha512_base_finish(desc, out);
 59         }
 60 
 61         return crypto_sha512_finup(desc, data, len, out);
 62 }
 63 
 64 static int riscv64_sha512_final(struct shash_desc *desc, u8 *out)
 65 {
 66         return riscv64_sha512_finup(desc, NULL, 0, out);
 67 }
 68 
 69 static int riscv64_sha512_digest(struct shash_desc *desc, const u8 *data,
 70                                  unsigned int len, u8 *out)
 71 {
 72         return sha512_base_init(desc) ?:
 73                riscv64_sha512_finup(desc, data, len, out);
 74 }
 75 
 76 static struct shash_alg riscv64_sha512_algs[] = {
 77         {
 78                 .init = sha512_base_init,
 79                 .update = riscv64_sha512_update,
 80                 .final = riscv64_sha512_final,
 81                 .finup = riscv64_sha512_finup,
 82                 .digest = riscv64_sha512_digest,
 83                 .descsize = sizeof(struct sha512_state),
 84                 .digestsize = SHA512_DIGEST_SIZE,
 85                 .base = {
 86                         .cra_blocksize = SHA512_BLOCK_SIZE,
 87                         .cra_priority = 300,
 88                         .cra_name = "sha512",
 89                         .cra_driver_name = "sha512-riscv64-zvknhb-zvkb",
 90                         .cra_module = THIS_MODULE,
 91                 },
 92         }, {
 93                 .init = sha384_base_init,
 94                 .update = riscv64_sha512_update,
 95                 .final = riscv64_sha512_final,
 96                 .finup = riscv64_sha512_finup,
 97                 .descsize = sizeof(struct sha512_state),
 98                 .digestsize = SHA384_DIGEST_SIZE,
 99                 .base = {
100                         .cra_blocksize = SHA384_BLOCK_SIZE,
101                         .cra_priority = 300,
102                         .cra_name = "sha384",
103                         .cra_driver_name = "sha384-riscv64-zvknhb-zvkb",
104                         .cra_module = THIS_MODULE,
105                 },
106         },
107 };
108 
109 static int __init riscv64_sha512_mod_init(void)
110 {
111         if (riscv_isa_extension_available(NULL, ZVKNHB) &&
112             riscv_isa_extension_available(NULL, ZVKB) &&
113             riscv_vector_vlen() >= 128)
114                 return crypto_register_shashes(riscv64_sha512_algs,
115                                                ARRAY_SIZE(riscv64_sha512_algs));
116 
117         return -ENODEV;
118 }
119 
120 static void __exit riscv64_sha512_mod_exit(void)
121 {
122         crypto_unregister_shashes(riscv64_sha512_algs,
123                                   ARRAY_SIZE(riscv64_sha512_algs));
124 }
125 
126 module_init(riscv64_sha512_mod_init);
127 module_exit(riscv64_sha512_mod_exit);
128 
129 MODULE_DESCRIPTION("SHA-512 (RISC-V accelerated)");
130 MODULE_AUTHOR("Heiko Stuebner <heiko.stuebner@vrull.eu>");
131 MODULE_LICENSE("GPL");
132 MODULE_ALIAS_CRYPTO("sha512");
133 MODULE_ALIAS_CRYPTO("sha384");
134 

~ [ 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