1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ChaCha stream cipher (RISC-V optimized)
4 *
5 * Copyright (C) 2023 SiFive, Inc.
6 * Author: Jerry Shih <jerry.shih@sifive.com>
7 */
8
9 #include <asm/simd.h>
10 #include <asm/vector.h>
11 #include <crypto/chacha.h>
12 #include <crypto/internal/simd.h>
13 #include <linux/linkage.h>
14 #include <linux/module.h>
15
16 static __ro_after_init DEFINE_STATIC_KEY_FALSE(use_zvkb);
17
18 asmlinkage void chacha_zvkb(struct chacha_state *state, const u8 *in, u8 *out,
19 size_t nblocks, int nrounds);
20
hchacha_block_arch(const struct chacha_state * state,u32 out[HCHACHA_OUT_WORDS],int nrounds)21 void hchacha_block_arch(const struct chacha_state *state,
22 u32 out[HCHACHA_OUT_WORDS], int nrounds)
23 {
24 hchacha_block_generic(state, out, nrounds);
25 }
26 EXPORT_SYMBOL(hchacha_block_arch);
27
chacha_crypt_arch(struct chacha_state * state,u8 * dst,const u8 * src,unsigned int bytes,int nrounds)28 void chacha_crypt_arch(struct chacha_state *state, u8 *dst, const u8 *src,
29 unsigned int bytes, int nrounds)
30 {
31 u8 block_buffer[CHACHA_BLOCK_SIZE];
32 unsigned int full_blocks = bytes / CHACHA_BLOCK_SIZE;
33 unsigned int tail_bytes = bytes % CHACHA_BLOCK_SIZE;
34
35 if (!static_branch_likely(&use_zvkb) || !crypto_simd_usable())
36 return chacha_crypt_generic(state, dst, src, bytes, nrounds);
37
38 kernel_vector_begin();
39 if (full_blocks) {
40 chacha_zvkb(state, src, dst, full_blocks, nrounds);
41 src += full_blocks * CHACHA_BLOCK_SIZE;
42 dst += full_blocks * CHACHA_BLOCK_SIZE;
43 }
44 if (tail_bytes) {
45 memcpy(block_buffer, src, tail_bytes);
46 chacha_zvkb(state, block_buffer, block_buffer, 1, nrounds);
47 memcpy(dst, block_buffer, tail_bytes);
48 }
49 kernel_vector_end();
50 }
51 EXPORT_SYMBOL(chacha_crypt_arch);
52
chacha_is_arch_optimized(void)53 bool chacha_is_arch_optimized(void)
54 {
55 return static_key_enabled(&use_zvkb);
56 }
57 EXPORT_SYMBOL(chacha_is_arch_optimized);
58
riscv64_chacha_mod_init(void)59 static int __init riscv64_chacha_mod_init(void)
60 {
61 if (riscv_isa_extension_available(NULL, ZVKB) &&
62 riscv_vector_vlen() >= 128)
63 static_branch_enable(&use_zvkb);
64 return 0;
65 }
66 subsys_initcall(riscv64_chacha_mod_init);
67
riscv64_chacha_mod_exit(void)68 static void __exit riscv64_chacha_mod_exit(void)
69 {
70 }
71 module_exit(riscv64_chacha_mod_exit);
72
73 MODULE_DESCRIPTION("ChaCha stream cipher (RISC-V optimized)");
74 MODULE_AUTHOR("Jerry Shih <jerry.shih@sifive.com>");
75 MODULE_LICENSE("GPL");
76