2024-01-21 16:19:16 -08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
/*
|
2025-04-05 11:26:01 -07:00
|
|
|
* ChaCha stream cipher (RISC-V optimized)
|
2024-01-21 16:19:16 -08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2023 SiFive, Inc.
|
|
|
|
* Author: Jerry Shih <jerry.shih@sifive.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <asm/simd.h>
|
|
|
|
#include <asm/vector.h>
|
2025-04-05 11:26:01 -07:00
|
|
|
#include <crypto/chacha.h>
|
|
|
|
#include <crypto/internal/simd.h>
|
2024-01-21 16:19:16 -08:00
|
|
|
#include <linux/linkage.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
2025-04-05 11:26:01 -07:00
|
|
|
static __ro_after_init DEFINE_STATIC_KEY_FALSE(use_zvkb);
|
2024-01-21 16:19:16 -08:00
|
|
|
|
2025-05-05 11:18:21 -07:00
|
|
|
asmlinkage void chacha_zvkb(struct chacha_state *state, const u8 *in, u8 *out,
|
2025-04-05 11:26:01 -07:00
|
|
|
size_t nblocks, int nrounds);
|
|
|
|
|
2025-05-05 11:18:24 -07:00
|
|
|
void hchacha_block_arch(const struct chacha_state *state,
|
|
|
|
u32 out[HCHACHA_OUT_WORDS], int nrounds)
|
2024-01-21 16:19:16 -08:00
|
|
|
{
|
2025-04-05 11:26:01 -07:00
|
|
|
hchacha_block_generic(state, out, nrounds);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(hchacha_block_arch);
|
2024-01-21 16:19:16 -08:00
|
|
|
|
2025-05-05 11:18:21 -07:00
|
|
|
void chacha_crypt_arch(struct chacha_state *state, u8 *dst, const u8 *src,
|
|
|
|
unsigned int bytes, int nrounds)
|
2025-04-05 11:26:01 -07:00
|
|
|
{
|
|
|
|
u8 block_buffer[CHACHA_BLOCK_SIZE];
|
|
|
|
unsigned int full_blocks = bytes / CHACHA_BLOCK_SIZE;
|
|
|
|
unsigned int tail_bytes = bytes % CHACHA_BLOCK_SIZE;
|
2024-01-21 16:19:16 -08:00
|
|
|
|
2025-04-05 11:26:01 -07:00
|
|
|
if (!static_branch_likely(&use_zvkb) || !crypto_simd_usable())
|
|
|
|
return chacha_crypt_generic(state, dst, src, bytes, nrounds);
|
2024-01-21 16:19:16 -08:00
|
|
|
|
2025-04-05 11:26:01 -07:00
|
|
|
kernel_vector_begin();
|
|
|
|
if (full_blocks) {
|
|
|
|
chacha_zvkb(state, src, dst, full_blocks, nrounds);
|
|
|
|
src += full_blocks * CHACHA_BLOCK_SIZE;
|
|
|
|
dst += full_blocks * CHACHA_BLOCK_SIZE;
|
2024-01-21 16:19:16 -08:00
|
|
|
}
|
2025-04-05 11:26:01 -07:00
|
|
|
if (tail_bytes) {
|
|
|
|
memcpy(block_buffer, src, tail_bytes);
|
|
|
|
chacha_zvkb(state, block_buffer, block_buffer, 1, nrounds);
|
|
|
|
memcpy(dst, block_buffer, tail_bytes);
|
|
|
|
}
|
|
|
|
kernel_vector_end();
|
2024-01-21 16:19:16 -08:00
|
|
|
}
|
2025-04-05 11:26:01 -07:00
|
|
|
EXPORT_SYMBOL(chacha_crypt_arch);
|
2024-01-21 16:19:16 -08:00
|
|
|
|
2025-04-05 11:26:02 -07:00
|
|
|
bool chacha_is_arch_optimized(void)
|
|
|
|
{
|
|
|
|
return static_key_enabled(&use_zvkb);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(chacha_is_arch_optimized);
|
|
|
|
|
2024-01-21 16:19:16 -08:00
|
|
|
static int __init riscv64_chacha_mod_init(void)
|
|
|
|
{
|
|
|
|
if (riscv_isa_extension_available(NULL, ZVKB) &&
|
|
|
|
riscv_vector_vlen() >= 128)
|
2025-04-05 11:26:01 -07:00
|
|
|
static_branch_enable(&use_zvkb);
|
|
|
|
return 0;
|
2024-01-21 16:19:16 -08:00
|
|
|
}
|
2025-04-30 16:17:02 +08:00
|
|
|
subsys_initcall(riscv64_chacha_mod_init);
|
2024-01-21 16:19:16 -08:00
|
|
|
|
2025-04-17 20:59:09 -07:00
|
|
|
static void __exit riscv64_chacha_mod_exit(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
module_exit(riscv64_chacha_mod_exit);
|
|
|
|
|
2025-04-05 11:26:01 -07:00
|
|
|
MODULE_DESCRIPTION("ChaCha stream cipher (RISC-V optimized)");
|
2024-01-21 16:19:16 -08:00
|
|
|
MODULE_AUTHOR("Jerry Shih <jerry.shih@sifive.com>");
|
|
|
|
MODULE_LICENSE("GPL");
|