aes-ce.S 3.4 KB

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