aes.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2023 VRULL GmbH
  4. * Copyright (C) 2023 SiFive, Inc.
  5. * Copyright 2024 Google LLC
  6. */
  7. #include <asm/simd.h>
  8. #include <asm/vector.h>
  9. static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_zvkned);
  10. void aes_encrypt_zvkned(const u32 rndkeys[], int key_len,
  11. u8 out[AES_BLOCK_SIZE], const u8 in[AES_BLOCK_SIZE]);
  12. void aes_decrypt_zvkned(const u32 rndkeys[], int key_len,
  13. u8 out[AES_BLOCK_SIZE], const u8 in[AES_BLOCK_SIZE]);
  14. static void aes_preparekey_arch(union aes_enckey_arch *k,
  15. union aes_invkey_arch *inv_k,
  16. const u8 *in_key, int key_len, int nrounds)
  17. {
  18. aes_expandkey_generic(k->rndkeys, inv_k ? inv_k->inv_rndkeys : NULL,
  19. in_key, key_len);
  20. }
  21. static void aes_encrypt_arch(const struct aes_enckey *key,
  22. u8 out[AES_BLOCK_SIZE],
  23. const u8 in[AES_BLOCK_SIZE])
  24. {
  25. if (static_branch_likely(&have_zvkned) && likely(may_use_simd())) {
  26. kernel_vector_begin();
  27. aes_encrypt_zvkned(key->k.rndkeys, key->len, out, in);
  28. kernel_vector_end();
  29. } else {
  30. aes_encrypt_generic(key->k.rndkeys, key->nrounds, out, in);
  31. }
  32. }
  33. static void aes_decrypt_arch(const struct aes_key *key,
  34. u8 out[AES_BLOCK_SIZE],
  35. const u8 in[AES_BLOCK_SIZE])
  36. {
  37. /*
  38. * Note that the Zvkned code uses the standard round keys, while the
  39. * fallback uses the inverse round keys. Thus both must be present.
  40. */
  41. if (static_branch_likely(&have_zvkned) && likely(may_use_simd())) {
  42. kernel_vector_begin();
  43. aes_decrypt_zvkned(key->k.rndkeys, key->len, out, in);
  44. kernel_vector_end();
  45. } else {
  46. aes_decrypt_generic(key->inv_k.inv_rndkeys, key->nrounds,
  47. out, in);
  48. }
  49. }
  50. #define aes_mod_init_arch aes_mod_init_arch
  51. static void aes_mod_init_arch(void)
  52. {
  53. if (riscv_isa_extension_available(NULL, ZVKNED) &&
  54. riscv_vector_vlen() >= 128)
  55. static_branch_enable(&have_zvkned);
  56. }