aes.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * AES block cipher, optimized for ARM64
  4. *
  5. * Copyright (C) 2013 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
  6. * Copyright 2026 Google LLC
  7. */
  8. #include <asm/neon.h>
  9. #include <asm/simd.h>
  10. #include <linux/unaligned.h>
  11. #include <linux/cpufeature.h>
  12. static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_aes);
  13. struct aes_block {
  14. u8 b[AES_BLOCK_SIZE];
  15. };
  16. asmlinkage void __aes_arm64_encrypt(const u32 rk[], u8 out[AES_BLOCK_SIZE],
  17. const u8 in[AES_BLOCK_SIZE], int rounds);
  18. asmlinkage void __aes_arm64_decrypt(const u32 inv_rk[], u8 out[AES_BLOCK_SIZE],
  19. const u8 in[AES_BLOCK_SIZE], int rounds);
  20. asmlinkage void __aes_ce_encrypt(const u32 rk[], u8 out[AES_BLOCK_SIZE],
  21. const u8 in[AES_BLOCK_SIZE], int rounds);
  22. asmlinkage void __aes_ce_decrypt(const u32 inv_rk[], u8 out[AES_BLOCK_SIZE],
  23. const u8 in[AES_BLOCK_SIZE], int rounds);
  24. asmlinkage u32 __aes_ce_sub(u32 l);
  25. asmlinkage void __aes_ce_invert(struct aes_block *out,
  26. const struct aes_block *in);
  27. /*
  28. * Expand an AES key using the crypto extensions if supported and usable or
  29. * generic code otherwise. The expanded key format is compatible between the
  30. * two cases. The outputs are @rndkeys (required) and @inv_rndkeys (optional).
  31. */
  32. static void aes_expandkey_arm64(u32 rndkeys[], u32 *inv_rndkeys,
  33. const u8 *in_key, int key_len, int nrounds)
  34. {
  35. /*
  36. * The AES key schedule round constants
  37. */
  38. static u8 const rcon[] = {
  39. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
  40. };
  41. u32 kwords = key_len / sizeof(u32);
  42. struct aes_block *key_enc, *key_dec;
  43. int i, j;
  44. if (!IS_ENABLED(CONFIG_KERNEL_MODE_NEON) ||
  45. !static_branch_likely(&have_aes) || unlikely(!may_use_simd())) {
  46. aes_expandkey_generic(rndkeys, inv_rndkeys, in_key, key_len);
  47. return;
  48. }
  49. for (i = 0; i < kwords; i++)
  50. rndkeys[i] = get_unaligned_le32(&in_key[i * sizeof(u32)]);
  51. scoped_ksimd() {
  52. for (i = 0; i < sizeof(rcon); i++) {
  53. u32 *rki = &rndkeys[i * kwords];
  54. u32 *rko = rki + kwords;
  55. rko[0] = ror32(__aes_ce_sub(rki[kwords - 1]), 8) ^
  56. rcon[i] ^ rki[0];
  57. rko[1] = rko[0] ^ rki[1];
  58. rko[2] = rko[1] ^ rki[2];
  59. rko[3] = rko[2] ^ rki[3];
  60. if (key_len == AES_KEYSIZE_192) {
  61. if (i >= 7)
  62. break;
  63. rko[4] = rko[3] ^ rki[4];
  64. rko[5] = rko[4] ^ rki[5];
  65. } else if (key_len == AES_KEYSIZE_256) {
  66. if (i >= 6)
  67. break;
  68. rko[4] = __aes_ce_sub(rko[3]) ^ rki[4];
  69. rko[5] = rko[4] ^ rki[5];
  70. rko[6] = rko[5] ^ rki[6];
  71. rko[7] = rko[6] ^ rki[7];
  72. }
  73. }
  74. /*
  75. * Generate the decryption keys for the Equivalent Inverse
  76. * Cipher. This involves reversing the order of the round
  77. * keys, and applying the Inverse Mix Columns transformation on
  78. * all but the first and the last one.
  79. */
  80. if (inv_rndkeys) {
  81. key_enc = (struct aes_block *)rndkeys;
  82. key_dec = (struct aes_block *)inv_rndkeys;
  83. j = nrounds;
  84. key_dec[0] = key_enc[j];
  85. for (i = 1, j--; j > 0; i++, j--)
  86. __aes_ce_invert(key_dec + i, key_enc + j);
  87. key_dec[i] = key_enc[0];
  88. }
  89. }
  90. }
  91. static void aes_preparekey_arch(union aes_enckey_arch *k,
  92. union aes_invkey_arch *inv_k,
  93. const u8 *in_key, int key_len, int nrounds)
  94. {
  95. aes_expandkey_arm64(k->rndkeys, inv_k ? inv_k->inv_rndkeys : NULL,
  96. in_key, key_len, nrounds);
  97. }
  98. /*
  99. * This is here temporarily until the remaining AES mode implementations are
  100. * migrated from arch/arm64/crypto/ to lib/crypto/arm64/.
  101. */
  102. int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
  103. unsigned int key_len)
  104. {
  105. if (aes_check_keylen(key_len) != 0)
  106. return -EINVAL;
  107. ctx->key_length = key_len;
  108. aes_expandkey_arm64(ctx->key_enc, ctx->key_dec, in_key, key_len,
  109. 6 + key_len / 4);
  110. return 0;
  111. }
  112. EXPORT_SYMBOL(ce_aes_expandkey);
  113. static void aes_encrypt_arch(const struct aes_enckey *key,
  114. u8 out[AES_BLOCK_SIZE],
  115. const u8 in[AES_BLOCK_SIZE])
  116. {
  117. if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) &&
  118. static_branch_likely(&have_aes) && likely(may_use_simd())) {
  119. scoped_ksimd()
  120. __aes_ce_encrypt(key->k.rndkeys, out, in, key->nrounds);
  121. } else {
  122. __aes_arm64_encrypt(key->k.rndkeys, out, in, key->nrounds);
  123. }
  124. }
  125. static void aes_decrypt_arch(const struct aes_key *key,
  126. u8 out[AES_BLOCK_SIZE],
  127. const u8 in[AES_BLOCK_SIZE])
  128. {
  129. if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) &&
  130. static_branch_likely(&have_aes) && likely(may_use_simd())) {
  131. scoped_ksimd()
  132. __aes_ce_decrypt(key->inv_k.inv_rndkeys, out, in,
  133. key->nrounds);
  134. } else {
  135. __aes_arm64_decrypt(key->inv_k.inv_rndkeys, out, in,
  136. key->nrounds);
  137. }
  138. }
  139. #ifdef CONFIG_KERNEL_MODE_NEON
  140. #define aes_mod_init_arch aes_mod_init_arch
  141. static void aes_mod_init_arch(void)
  142. {
  143. if (cpu_have_named_feature(AES))
  144. static_branch_enable(&have_aes);
  145. }
  146. #endif /* CONFIG_KERNEL_MODE_NEON */