chacha.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #include <asm/simd.h>
  9. #include <asm/vector.h>
  10. #include <crypto/internal/simd.h>
  11. #include <linux/linkage.h>
  12. static __ro_after_init DEFINE_STATIC_KEY_FALSE(use_zvkb);
  13. asmlinkage void chacha_zvkb(struct chacha_state *state, const u8 *in, u8 *out,
  14. size_t nblocks, int nrounds);
  15. #define hchacha_block_arch hchacha_block_generic /* not implemented yet */
  16. static void chacha_crypt_arch(struct chacha_state *state, u8 *dst,
  17. const u8 *src, unsigned int bytes, int nrounds)
  18. {
  19. u8 block_buffer[CHACHA_BLOCK_SIZE];
  20. unsigned int full_blocks = bytes / CHACHA_BLOCK_SIZE;
  21. unsigned int tail_bytes = bytes % CHACHA_BLOCK_SIZE;
  22. if (!static_branch_likely(&use_zvkb) || !crypto_simd_usable())
  23. return chacha_crypt_generic(state, dst, src, bytes, nrounds);
  24. kernel_vector_begin();
  25. if (full_blocks) {
  26. chacha_zvkb(state, src, dst, full_blocks, nrounds);
  27. src += full_blocks * CHACHA_BLOCK_SIZE;
  28. dst += full_blocks * CHACHA_BLOCK_SIZE;
  29. }
  30. if (tail_bytes) {
  31. memcpy(block_buffer, src, tail_bytes);
  32. chacha_zvkb(state, block_buffer, block_buffer, 1, nrounds);
  33. memcpy(dst, block_buffer, tail_bytes);
  34. }
  35. kernel_vector_end();
  36. }
  37. #define chacha_mod_init_arch chacha_mod_init_arch
  38. static void chacha_mod_init_arch(void)
  39. {
  40. if (riscv_isa_extension_available(NULL, ZVKB) &&
  41. riscv_vector_vlen() >= 128)
  42. static_branch_enable(&use_zvkb);
  43. }