aes-ce.S 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * linux/arch/arm64/crypto/aes-ce.S - AES cipher for ARMv8 with
  3. * Crypto Extensions
  4. *
  5. * Copyright (C) 2013 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/linkage.h>
  12. #include <asm/assembler.h>
  13. #define AES_ENTRY(func) ENTRY(ce_ ## func)
  14. #define AES_ENDPROC(func) ENDPROC(ce_ ## func)
  15. .arch armv8-a+crypto
  16. /* preload all round keys */
  17. .macro load_round_keys, rounds, rk
  18. cmp \rounds, #12
  19. blo 2222f /* 128 bits */
  20. beq 1111f /* 192 bits */
  21. ld1 {v17.4s-v18.4s}, [\rk], #32
  22. 1111: ld1 {v19.4s-v20.4s}, [\rk], #32
  23. 2222: ld1 {v21.4s-v24.4s}, [\rk], #64
  24. ld1 {v25.4s-v28.4s}, [\rk], #64
  25. ld1 {v29.4s-v31.4s}, [\rk]
  26. .endm
  27. /* prepare for encryption with key in rk[] */
  28. .macro enc_prepare, rounds, rk, temp
  29. mov \temp, \rk
  30. load_round_keys \rounds, \temp
  31. .endm
  32. /* prepare for encryption (again) but with new key in rk[] */
  33. .macro enc_switch_key, rounds, rk, temp
  34. mov \temp, \rk
  35. load_round_keys \rounds, \temp
  36. .endm
  37. /* prepare for decryption with key in rk[] */
  38. .macro dec_prepare, rounds, rk, temp
  39. mov \temp, \rk
  40. load_round_keys \rounds, \temp
  41. .endm
  42. .macro do_enc_Nx, de, mc, k, i0, i1, i2, i3
  43. aes\de \i0\().16b, \k\().16b
  44. aes\mc \i0\().16b, \i0\().16b
  45. .ifnb \i1
  46. aes\de \i1\().16b, \k\().16b
  47. aes\mc \i1\().16b, \i1\().16b
  48. .ifnb \i3
  49. aes\de \i2\().16b, \k\().16b
  50. aes\mc \i2\().16b, \i2\().16b
  51. aes\de \i3\().16b, \k\().16b
  52. aes\mc \i3\().16b, \i3\().16b
  53. .endif
  54. .endif
  55. .endm
  56. /* up to 4 interleaved encryption rounds with the same round key */
  57. .macro round_Nx, enc, k, i0, i1, i2, i3
  58. .ifc \enc, e
  59. do_enc_Nx e, mc, \k, \i0, \i1, \i2, \i3
  60. .else
  61. do_enc_Nx d, imc, \k, \i0, \i1, \i2, \i3
  62. .endif
  63. .endm
  64. /* up to 4 interleaved final rounds */
  65. .macro fin_round_Nx, de, k, k2, i0, i1, i2, i3
  66. aes\de \i0\().16b, \k\().16b
  67. .ifnb \i1
  68. aes\de \i1\().16b, \k\().16b
  69. .ifnb \i3
  70. aes\de \i2\().16b, \k\().16b
  71. aes\de \i3\().16b, \k\().16b
  72. .endif
  73. .endif
  74. eor \i0\().16b, \i0\().16b, \k2\().16b
  75. .ifnb \i1
  76. eor \i1\().16b, \i1\().16b, \k2\().16b
  77. .ifnb \i3
  78. eor \i2\().16b, \i2\().16b, \k2\().16b
  79. eor \i3\().16b, \i3\().16b, \k2\().16b
  80. .endif
  81. .endif
  82. .endm
  83. /* up to 4 interleaved blocks */
  84. .macro do_block_Nx, enc, rounds, i0, i1, i2, i3
  85. cmp \rounds, #12
  86. blo 2222f /* 128 bits */
  87. beq 1111f /* 192 bits */
  88. round_Nx \enc, v17, \i0, \i1, \i2, \i3
  89. round_Nx \enc, v18, \i0, \i1, \i2, \i3
  90. 1111: round_Nx \enc, v19, \i0, \i1, \i2, \i3
  91. round_Nx \enc, v20, \i0, \i1, \i2, \i3
  92. 2222: .irp key, v21, v22, v23, v24, v25, v26, v27, v28, v29
  93. round_Nx \enc, \key, \i0, \i1, \i2, \i3
  94. .endr
  95. fin_round_Nx \enc, v30, v31, \i0, \i1, \i2, \i3
  96. .endm
  97. .macro encrypt_block, in, rounds, t0, t1, t2
  98. do_block_Nx e, \rounds, \in
  99. .endm
  100. .macro encrypt_block2x, i0, i1, rounds, t0, t1, t2
  101. do_block_Nx e, \rounds, \i0, \i1
  102. .endm
  103. .macro encrypt_block4x, i0, i1, i2, i3, rounds, t0, t1, t2
  104. do_block_Nx e, \rounds, \i0, \i1, \i2, \i3
  105. .endm
  106. .macro decrypt_block, in, rounds, t0, t1, t2
  107. do_block_Nx d, \rounds, \in
  108. .endm
  109. .macro decrypt_block2x, i0, i1, rounds, t0, t1, t2
  110. do_block_Nx d, \rounds, \i0, \i1
  111. .endm
  112. .macro decrypt_block4x, i0, i1, i2, i3, rounds, t0, t1, t2
  113. do_block_Nx d, \rounds, \i0, \i1, \i2, \i3
  114. .endm
  115. #include "aes-modes.S"