sha3.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <asm/simd.h>
  10. #include <linux/cpufeature.h>
  11. static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_sha3);
  12. asmlinkage size_t sha3_ce_transform(struct sha3_state *state, const u8 *data,
  13. size_t nblocks, size_t block_size);
  14. static void sha3_absorb_blocks(struct sha3_state *state, const u8 *data,
  15. size_t nblocks, size_t block_size)
  16. {
  17. if (static_branch_likely(&have_sha3) && likely(may_use_simd())) {
  18. do {
  19. size_t rem;
  20. scoped_ksimd()
  21. rem = sha3_ce_transform(state, data, nblocks,
  22. block_size);
  23. data += (nblocks - rem) * block_size;
  24. nblocks = rem;
  25. } while (nblocks);
  26. } else {
  27. sha3_absorb_blocks_generic(state, data, nblocks, block_size);
  28. }
  29. }
  30. static void sha3_keccakf(struct sha3_state *state)
  31. {
  32. if (static_branch_likely(&have_sha3) && likely(may_use_simd())) {
  33. /*
  34. * Passing zeroes into sha3_ce_transform() gives the plain
  35. * Keccak-f permutation, which is what we want here. Any
  36. * supported block size may be used. Use SHA3_512_BLOCK_SIZE
  37. * since it's the shortest.
  38. */
  39. static const u8 zeroes[SHA3_512_BLOCK_SIZE];
  40. scoped_ksimd()
  41. sha3_ce_transform(state, zeroes, 1, sizeof(zeroes));
  42. } else {
  43. sha3_keccakf_generic(state);
  44. }
  45. }
  46. #define sha3_mod_init_arch sha3_mod_init_arch
  47. static void sha3_mod_init_arch(void)
  48. {
  49. if (cpu_have_named_feature(SHA3))
  50. static_branch_enable(&have_sha3);
  51. }