aes-ce.S 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <[email protected]>
  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, rounds, rk
  22. cmp \rounds, #12
  23. blo 2222f /* 128 bits */
  24. beq 1111f /* 192 bits */
  25. ld1 {v17.4s-v18.4s}, [\rk], #32
  26. 1111: ld1 {v19.4s-v20.4s}, [\rk], #32
  27. 2222: ld1 {v21.4s-v24.4s}, [\rk], #64
  28. ld1 {v25.4s-v28.4s}, [\rk], #64
  29. ld1 {v29.4s-v31.4s}, [\rk]
  30. .endm
  31. /* prepare for encryption with key in rk[] */
  32. .macro enc_prepare, rounds, rk, temp
  33. mov \temp, \rk
  34. load_round_keys \rounds, \temp
  35. .endm
  36. /* prepare for encryption (again) but with new key in rk[] */
  37. .macro enc_switch_key, rounds, rk, temp
  38. mov \temp, \rk
  39. load_round_keys \rounds, \temp
  40. .endm
  41. /* prepare for decryption with key in rk[] */
  42. .macro dec_prepare, rounds, rk, temp
  43. mov \temp, \rk
  44. load_round_keys \rounds, \temp
  45. .endm
  46. .macro do_enc_Nx, de, mc, k, i0, i1, i2, i3, i4
  47. aes\de \i0\().16b, \k\().16b
  48. aes\mc \i0\().16b, \i0\().16b
  49. .ifnb \i1
  50. aes\de \i1\().16b, \k\().16b
  51. aes\mc \i1\().16b, \i1\().16b
  52. .ifnb \i3
  53. aes\de \i2\().16b, \k\().16b
  54. aes\mc \i2\().16b, \i2\().16b
  55. aes\de \i3\().16b, \k\().16b
  56. aes\mc \i3\().16b, \i3\().16b
  57. .ifnb \i4
  58. aes\de \i4\().16b, \k\().16b
  59. aes\mc \i4\().16b, \i4\().16b
  60. .endif
  61. .endif
  62. .endif
  63. .endm
  64. /* up to 5 interleaved encryption rounds with the same round key */
  65. .macro round_Nx, enc, k, i0, i1, i2, i3, i4
  66. .ifc \enc, e
  67. do_enc_Nx e, mc, \k, \i0, \i1, \i2, \i3, \i4
  68. .else
  69. do_enc_Nx d, imc, \k, \i0, \i1, \i2, \i3, \i4
  70. .endif
  71. .endm
  72. /* up to 5 interleaved final rounds */
  73. .macro fin_round_Nx, de, k, k2, i0, i1, i2, i3, i4
  74. aes\de \i0\().16b, \k\().16b
  75. .ifnb \i1
  76. aes\de \i1\().16b, \k\().16b
  77. .ifnb \i3
  78. aes\de \i2\().16b, \k\().16b
  79. aes\de \i3\().16b, \k\().16b
  80. .ifnb \i4
  81. aes\de \i4\().16b, \k\().16b
  82. .endif
  83. .endif
  84. .endif
  85. eor \i0\().16b, \i0\().16b, \k2\().16b
  86. .ifnb \i1
  87. eor \i1\().16b, \i1\().16b, \k2\().16b
  88. .ifnb \i3
  89. eor \i2\().16b, \i2\().16b, \k2\().16b
  90. eor \i3\().16b, \i3\().16b, \k2\().16b
  91. .ifnb \i4
  92. eor \i4\().16b, \i4\().16b, \k2\().16b
  93. .endif
  94. .endif
  95. .endif
  96. .endm
  97. /* up to 5 interleaved blocks */
  98. .macro do_block_Nx, enc, rounds, i0, i1, i2, i3, i4
  99. cmp \rounds, #12
  100. blo 2222f /* 128 bits */
  101. beq 1111f /* 192 bits */
  102. round_Nx \enc, v17, \i0, \i1, \i2, \i3, \i4
  103. round_Nx \enc, v18, \i0, \i1, \i2, \i3, \i4
  104. 1111: round_Nx \enc, v19, \i0, \i1, \i2, \i3, \i4
  105. round_Nx \enc, v20, \i0, \i1, \i2, \i3, \i4
  106. 2222: .irp key, v21, v22, v23, v24, v25, v26, v27, v28, v29
  107. round_Nx \enc, \key, \i0, \i1, \i2, \i3, \i4
  108. .endr
  109. fin_round_Nx \enc, v30, v31, \i0, \i1, \i2, \i3, \i4
  110. .endm
  111. .macro encrypt_block, in, rounds, t0, t1, t2
  112. do_block_Nx e, \rounds, \in
  113. .endm
  114. .macro encrypt_block4x, i0, i1, i2, i3, rounds, t0, t1, t2
  115. do_block_Nx e, \rounds, \i0, \i1, \i2, \i3
  116. .endm
  117. .macro encrypt_block5x, i0, i1, i2, i3, i4, rounds, t0, t1, t2
  118. do_block_Nx e, \rounds, \i0, \i1, \i2, \i3, \i4
  119. .endm
  120. .macro decrypt_block, in, rounds, t0, t1, t2
  121. do_block_Nx d, \rounds, \in
  122. .endm
  123. .macro decrypt_block4x, i0, i1, i2, i3, rounds, t0, t1, t2
  124. do_block_Nx d, \rounds, \i0, \i1, \i2, \i3
  125. .endm
  126. .macro decrypt_block5x, i0, i1, i2, i3, i4, rounds, t0, t1, t2
  127. do_block_Nx d, \rounds, \i0, \i1, \i2, \i3, \i4
  128. .endm
  129. #define MAX_STRIDE 5
  130. #include "aes-modes.S"