Path: blob/master/lib/crypto/riscv/chacha-riscv64-glue.c
26285 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* ChaCha stream cipher (RISC-V optimized)3*4* Copyright (C) 2023 SiFive, Inc.5* Author: Jerry Shih <[email protected]>6*/78#include <asm/simd.h>9#include <asm/vector.h>10#include <crypto/chacha.h>11#include <crypto/internal/simd.h>12#include <linux/linkage.h>13#include <linux/module.h>1415static __ro_after_init DEFINE_STATIC_KEY_FALSE(use_zvkb);1617asmlinkage void chacha_zvkb(struct chacha_state *state, const u8 *in, u8 *out,18size_t nblocks, int nrounds);1920void hchacha_block_arch(const struct chacha_state *state,21u32 out[HCHACHA_OUT_WORDS], int nrounds)22{23hchacha_block_generic(state, out, nrounds);24}25EXPORT_SYMBOL(hchacha_block_arch);2627void chacha_crypt_arch(struct chacha_state *state, u8 *dst, const u8 *src,28unsigned int bytes, int nrounds)29{30u8 block_buffer[CHACHA_BLOCK_SIZE];31unsigned int full_blocks = bytes / CHACHA_BLOCK_SIZE;32unsigned int tail_bytes = bytes % CHACHA_BLOCK_SIZE;3334if (!static_branch_likely(&use_zvkb) || !crypto_simd_usable())35return chacha_crypt_generic(state, dst, src, bytes, nrounds);3637kernel_vector_begin();38if (full_blocks) {39chacha_zvkb(state, src, dst, full_blocks, nrounds);40src += full_blocks * CHACHA_BLOCK_SIZE;41dst += full_blocks * CHACHA_BLOCK_SIZE;42}43if (tail_bytes) {44memcpy(block_buffer, src, tail_bytes);45chacha_zvkb(state, block_buffer, block_buffer, 1, nrounds);46memcpy(dst, block_buffer, tail_bytes);47}48kernel_vector_end();49}50EXPORT_SYMBOL(chacha_crypt_arch);5152bool chacha_is_arch_optimized(void)53{54return static_key_enabled(&use_zvkb);55}56EXPORT_SYMBOL(chacha_is_arch_optimized);5758static int __init riscv64_chacha_mod_init(void)59{60if (riscv_isa_extension_available(NULL, ZVKB) &&61riscv_vector_vlen() >= 128)62static_branch_enable(&use_zvkb);63return 0;64}65subsys_initcall(riscv64_chacha_mod_init);6667static void __exit riscv64_chacha_mod_exit(void)68{69}70module_exit(riscv64_chacha_mod_exit);7172MODULE_DESCRIPTION("ChaCha stream cipher (RISC-V optimized)");73MODULE_AUTHOR("Jerry Shih <[email protected]>");74MODULE_LICENSE("GPL");757677