aes-ce.S 3.3 KB

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