aes-ce.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * linux/arch/arm64/crypto/aes-ce.S - AES cipher for ARMv8 with
  4. * Crypto Extensions
  5. *
  6. * Copyright (C) 2013 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
  7. */
  8. #include <linux/linkage.h>
  9. #include <asm/assembler.h>
  10. #define AES_FUNC_START(func) SYM_FUNC_START(ce_ ## func)
  11. #define AES_FUNC_END(func) SYM_FUNC_END(ce_ ## func)
  12. .arch armv8-a+crypto
  13. xtsmask .req v16
  14. cbciv .req v16
  15. vctr .req v16
  16. .macro xts_reload_mask, tmp
  17. .endm
  18. .macro xts_cts_skip_tw, reg, lbl
  19. .endm
  20. /* preload all round keys */
  21. .macro load_round_keys, rk, nr, tmp
  22. add \tmp, \rk, \nr, sxtw #4
  23. sub \tmp, \tmp, #160
  24. ld1 {v17.4s-v20.4s}, [\rk]
  25. ld1 {v21.4s-v24.4s}, [\tmp], #64
  26. ld1 {v25.4s-v28.4s}, [\tmp], #64
  27. ld1 {v29.4s-v31.4s}, [\tmp]
  28. .endm
  29. /* prepare for encryption with key in rk[] */
  30. .macro enc_prepare, rounds, rk, temp
  31. load_round_keys \rk, \rounds, \temp
  32. .endm
  33. /* prepare for encryption (again) but with new key in rk[] */
  34. .macro enc_switch_key, rounds, rk, temp
  35. load_round_keys \rk, \rounds, \temp
  36. .endm
  37. /* prepare for decryption with key in rk[] */
  38. .macro dec_prepare, rounds, rk, temp
  39. load_round_keys \rk, \rounds, \temp
  40. .endm
  41. .macro do_enc_Nx, de, mc, k, i0, i1, i2, i3, i4
  42. aes\de \i0\().16b, \k\().16b
  43. aes\mc \i0\().16b, \i0\().16b
  44. .ifnb \i1
  45. aes\de \i1\().16b, \k\().16b
  46. aes\mc \i1\().16b, \i1\().16b
  47. .ifnb \i3
  48. aes\de \i2\().16b, \k\().16b
  49. aes\mc \i2\().16b, \i2\().16b
  50. aes\de \i3\().16b, \k\().16b
  51. aes\mc \i3\().16b, \i3\().16b
  52. .ifnb \i4
  53. aes\de \i4\().16b, \k\().16b
  54. aes\mc \i4\().16b, \i4\().16b
  55. .endif
  56. .endif
  57. .endif
  58. .endm
  59. /* up to 5 interleaved encryption rounds with the same round key */
  60. .macro round_Nx, enc, k, i0, i1, i2, i3, i4
  61. .ifc \enc, e
  62. do_enc_Nx e, mc, \k, \i0, \i1, \i2, \i3, \i4
  63. .else
  64. do_enc_Nx d, imc, \k, \i0, \i1, \i2, \i3, \i4
  65. .endif
  66. .endm
  67. /* up to 5 interleaved final rounds */
  68. .macro fin_round_Nx, de, k, k2, i0, i1, i2, i3, i4
  69. aes\de \i0\().16b, \k\().16b
  70. .ifnb \i1
  71. aes\de \i1\().16b, \k\().16b
  72. .ifnb \i3
  73. aes\de \i2\().16b, \k\().16b
  74. aes\de \i3\().16b, \k\().16b
  75. .ifnb \i4
  76. aes\de \i4\().16b, \k\().16b
  77. .endif
  78. .endif
  79. .endif
  80. eor \i0\().16b, \i0\().16b, \k2\().16b
  81. .ifnb \i1
  82. eor \i1\().16b, \i1\().16b, \k2\().16b
  83. .ifnb \i3
  84. eor \i2\().16b, \i2\().16b, \k2\().16b
  85. eor \i3\().16b, \i3\().16b, \k2\().16b
  86. .ifnb \i4
  87. eor \i4\().16b, \i4\().16b, \k2\().16b
  88. .endif
  89. .endif
  90. .endif
  91. .endm
  92. /* up to 5 interleaved blocks */
  93. .macro do_block_Nx, enc, rounds, i0, i1, i2, i3, i4
  94. tbz \rounds, #2, .L\@ /* 128 bits */
  95. round_Nx \enc, v17, \i0, \i1, \i2, \i3, \i4
  96. round_Nx \enc, v18, \i0, \i1, \i2, \i3, \i4
  97. tbz \rounds, #1, .L\@ /* 192 bits */
  98. round_Nx \enc, v19, \i0, \i1, \i2, \i3, \i4
  99. round_Nx \enc, v20, \i0, \i1, \i2, \i3, \i4
  100. .L\@: .irp key, v21, v22, v23, v24, v25, v26, v27, v28, v29
  101. round_Nx \enc, \key, \i0, \i1, \i2, \i3, \i4
  102. .endr
  103. fin_round_Nx \enc, v30, v31, \i0, \i1, \i2, \i3, \i4
  104. .endm
  105. .macro encrypt_block, in, rounds, t0, t1, t2
  106. do_block_Nx e, \rounds, \in
  107. .endm
  108. .macro encrypt_block4x, i0, i1, i2, i3, rounds, t0, t1, t2
  109. do_block_Nx e, \rounds, \i0, \i1, \i2, \i3
  110. .endm
  111. .macro encrypt_block5x, i0, i1, i2, i3, i4, rounds, t0, t1, t2
  112. do_block_Nx e, \rounds, \i0, \i1, \i2, \i3, \i4
  113. .endm
  114. .macro decrypt_block, in, rounds, t0, t1, t2
  115. do_block_Nx d, \rounds, \in
  116. .endm
  117. .macro decrypt_block4x, i0, i1, i2, i3, rounds, t0, t1, t2
  118. do_block_Nx d, \rounds, \i0, \i1, \i2, \i3
  119. .endm
  120. .macro decrypt_block5x, i0, i1, i2, i3, i4, rounds, t0, t1, t2
  121. do_block_Nx d, \rounds, \i0, \i1, \i2, \i3, \i4
  122. .endm
  123. #define MAX_STRIDE 5
  124. #include "aes-modes.S"