aes-ce.S 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * linux/arch/arm64/crypto/aes-ce.S - AES cipher for ARMv8 with
  3. * Crypto Extensions
  4. *
  5. * Copyright (C) 2013 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. #define AES_ENTRY(func) ENTRY(ce_ ## func)
  13. #define AES_ENDPROC(func) ENDPROC(ce_ ## func)
  14. .arch armv8-a+crypto
  15. /* preload all round keys */
  16. .macro load_round_keys, rounds, rk
  17. cmp \rounds, #12
  18. blo 2222f /* 128 bits */
  19. beq 1111f /* 192 bits */
  20. ld1 {v17.16b-v18.16b}, [\rk], #32
  21. 1111: ld1 {v19.16b-v20.16b}, [\rk], #32
  22. 2222: ld1 {v21.16b-v24.16b}, [\rk], #64
  23. ld1 {v25.16b-v28.16b}, [\rk], #64
  24. ld1 {v29.16b-v31.16b}, [\rk]
  25. .endm
  26. /* prepare for encryption with key in rk[] */
  27. .macro enc_prepare, rounds, rk, ignore
  28. load_round_keys \rounds, \rk
  29. .endm
  30. /* prepare for encryption (again) but with new key in rk[] */
  31. .macro enc_switch_key, rounds, rk, ignore
  32. load_round_keys \rounds, \rk
  33. .endm
  34. /* prepare for decryption with key in rk[] */
  35. .macro dec_prepare, rounds, rk, ignore
  36. load_round_keys \rounds, \rk
  37. .endm
  38. .macro do_enc_Nx, de, mc, k, i0, i1, i2, i3
  39. aes\de \i0\().16b, \k\().16b
  40. .ifnb \i1
  41. aes\de \i1\().16b, \k\().16b
  42. .ifnb \i3
  43. aes\de \i2\().16b, \k\().16b
  44. aes\de \i3\().16b, \k\().16b
  45. .endif
  46. .endif
  47. aes\mc \i0\().16b, \i0\().16b
  48. .ifnb \i1
  49. aes\mc \i1\().16b, \i1\().16b
  50. .ifnb \i3
  51. aes\mc \i2\().16b, \i2\().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"