aes.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * AES block cipher, optimized for ARM
  4. *
  5. * Copyright (C) 2017 Linaro Ltd.
  6. * Copyright 2026 Google LLC
  7. */
  8. asmlinkage void __aes_arm_encrypt(const u32 rk[], int rounds,
  9. const u8 in[AES_BLOCK_SIZE],
  10. u8 out[AES_BLOCK_SIZE]);
  11. asmlinkage void __aes_arm_decrypt(const u32 inv_rk[], int rounds,
  12. const u8 in[AES_BLOCK_SIZE],
  13. u8 out[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 (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
  26. !IS_ALIGNED((uintptr_t)out | (uintptr_t)in, 4)) {
  27. u8 bounce_buf[AES_BLOCK_SIZE] __aligned(4);
  28. memcpy(bounce_buf, in, AES_BLOCK_SIZE);
  29. __aes_arm_encrypt(key->k.rndkeys, key->nrounds, bounce_buf,
  30. bounce_buf);
  31. memcpy(out, bounce_buf, AES_BLOCK_SIZE);
  32. return;
  33. }
  34. __aes_arm_encrypt(key->k.rndkeys, key->nrounds, in, out);
  35. }
  36. static void aes_decrypt_arch(const struct aes_key *key,
  37. u8 out[AES_BLOCK_SIZE],
  38. const u8 in[AES_BLOCK_SIZE])
  39. {
  40. if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
  41. !IS_ALIGNED((uintptr_t)out | (uintptr_t)in, 4)) {
  42. u8 bounce_buf[AES_BLOCK_SIZE] __aligned(4);
  43. memcpy(bounce_buf, in, AES_BLOCK_SIZE);
  44. __aes_arm_decrypt(key->inv_k.inv_rndkeys, key->nrounds,
  45. bounce_buf, bounce_buf);
  46. memcpy(out, bounce_buf, AES_BLOCK_SIZE);
  47. return;
  48. }
  49. __aes_arm_decrypt(key->inv_k.inv_rndkeys, key->nrounds, in, out);
  50. }