sha3-ce-core.S 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * sha3-ce-core.S - core SHA-3 transform using v8.2 Crypto Extensions
  4. *
  5. * Copyright (C) 2018 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. .irp b,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
  14. .set .Lv\b\().2d, \b
  15. .set .Lv\b\().16b, \b
  16. .endr
  17. /*
  18. * ARMv8.2 Crypto Extensions instructions
  19. */
  20. .macro eor3, rd, rn, rm, ra
  21. .inst 0xce000000 | .L\rd | (.L\rn << 5) | (.L\ra << 10) | (.L\rm << 16)
  22. .endm
  23. .macro rax1, rd, rn, rm
  24. .inst 0xce608c00 | .L\rd | (.L\rn << 5) | (.L\rm << 16)
  25. .endm
  26. .macro bcax, rd, rn, rm, ra
  27. .inst 0xce200000 | .L\rd | (.L\rn << 5) | (.L\ra << 10) | (.L\rm << 16)
  28. .endm
  29. .macro xar, rd, rn, rm, imm6
  30. .inst 0xce800000 | .L\rd | (.L\rn << 5) | ((\imm6) << 10) | (.L\rm << 16)
  31. .endm
  32. /*
  33. * sha3_ce_transform(u64 *st, const u8 *data, int blocks, int dg_size)
  34. */
  35. .text
  36. ENTRY(sha3_ce_transform)
  37. frame_push 4
  38. mov x19, x0
  39. mov x20, x1
  40. mov x21, x2
  41. mov x22, x3
  42. 0: /* load state */
  43. add x8, x19, #32
  44. ld1 { v0.1d- v3.1d}, [x19]
  45. ld1 { v4.1d- v7.1d}, [x8], #32
  46. ld1 { v8.1d-v11.1d}, [x8], #32
  47. ld1 {v12.1d-v15.1d}, [x8], #32
  48. ld1 {v16.1d-v19.1d}, [x8], #32
  49. ld1 {v20.1d-v23.1d}, [x8], #32
  50. ld1 {v24.1d}, [x8]
  51. 1: sub w21, w21, #1
  52. mov w8, #24
  53. adr_l x9, .Lsha3_rcon
  54. /* load input */
  55. ld1 {v25.8b-v28.8b}, [x20], #32
  56. ld1 {v29.8b-v31.8b}, [x20], #24
  57. eor v0.8b, v0.8b, v25.8b
  58. eor v1.8b, v1.8b, v26.8b
  59. eor v2.8b, v2.8b, v27.8b
  60. eor v3.8b, v3.8b, v28.8b
  61. eor v4.8b, v4.8b, v29.8b
  62. eor v5.8b, v5.8b, v30.8b
  63. eor v6.8b, v6.8b, v31.8b
  64. tbnz x22, #6, 3f // SHA3-512
  65. ld1 {v25.8b-v28.8b}, [x20], #32
  66. ld1 {v29.8b-v30.8b}, [x20], #16
  67. eor v7.8b, v7.8b, v25.8b
  68. eor v8.8b, v8.8b, v26.8b
  69. eor v9.8b, v9.8b, v27.8b
  70. eor v10.8b, v10.8b, v28.8b
  71. eor v11.8b, v11.8b, v29.8b
  72. eor v12.8b, v12.8b, v30.8b
  73. tbnz x22, #4, 2f // SHA3-384 or SHA3-224
  74. // SHA3-256
  75. ld1 {v25.8b-v28.8b}, [x20], #32
  76. eor v13.8b, v13.8b, v25.8b
  77. eor v14.8b, v14.8b, v26.8b
  78. eor v15.8b, v15.8b, v27.8b
  79. eor v16.8b, v16.8b, v28.8b
  80. b 4f
  81. 2: tbz x22, #2, 4f // bit 2 cleared? SHA-384
  82. // SHA3-224
  83. ld1 {v25.8b-v28.8b}, [x20], #32
  84. ld1 {v29.8b}, [x20], #8
  85. eor v13.8b, v13.8b, v25.8b
  86. eor v14.8b, v14.8b, v26.8b
  87. eor v15.8b, v15.8b, v27.8b
  88. eor v16.8b, v16.8b, v28.8b
  89. eor v17.8b, v17.8b, v29.8b
  90. b 4f
  91. // SHA3-512
  92. 3: ld1 {v25.8b-v26.8b}, [x20], #16
  93. eor v7.8b, v7.8b, v25.8b
  94. eor v8.8b, v8.8b, v26.8b
  95. 4: sub w8, w8, #1
  96. eor3 v29.16b, v4.16b, v9.16b, v14.16b
  97. eor3 v26.16b, v1.16b, v6.16b, v11.16b
  98. eor3 v28.16b, v3.16b, v8.16b, v13.16b
  99. eor3 v25.16b, v0.16b, v5.16b, v10.16b
  100. eor3 v27.16b, v2.16b, v7.16b, v12.16b
  101. eor3 v29.16b, v29.16b, v19.16b, v24.16b
  102. eor3 v26.16b, v26.16b, v16.16b, v21.16b
  103. eor3 v28.16b, v28.16b, v18.16b, v23.16b
  104. eor3 v25.16b, v25.16b, v15.16b, v20.16b
  105. eor3 v27.16b, v27.16b, v17.16b, v22.16b
  106. rax1 v30.2d, v29.2d, v26.2d // bc[0]
  107. rax1 v26.2d, v26.2d, v28.2d // bc[2]
  108. rax1 v28.2d, v28.2d, v25.2d // bc[4]
  109. rax1 v25.2d, v25.2d, v27.2d // bc[1]
  110. rax1 v27.2d, v27.2d, v29.2d // bc[3]
  111. eor v0.16b, v0.16b, v30.16b
  112. xar v29.2d, v1.2d, v25.2d, (64 - 1)
  113. xar v1.2d, v6.2d, v25.2d, (64 - 44)
  114. xar v6.2d, v9.2d, v28.2d, (64 - 20)
  115. xar v9.2d, v22.2d, v26.2d, (64 - 61)
  116. xar v22.2d, v14.2d, v28.2d, (64 - 39)
  117. xar v14.2d, v20.2d, v30.2d, (64 - 18)
  118. xar v31.2d, v2.2d, v26.2d, (64 - 62)
  119. xar v2.2d, v12.2d, v26.2d, (64 - 43)
  120. xar v12.2d, v13.2d, v27.2d, (64 - 25)
  121. xar v13.2d, v19.2d, v28.2d, (64 - 8)
  122. xar v19.2d, v23.2d, v27.2d, (64 - 56)
  123. xar v23.2d, v15.2d, v30.2d, (64 - 41)
  124. xar v15.2d, v4.2d, v28.2d, (64 - 27)
  125. xar v28.2d, v24.2d, v28.2d, (64 - 14)
  126. xar v24.2d, v21.2d, v25.2d, (64 - 2)
  127. xar v8.2d, v8.2d, v27.2d, (64 - 55)
  128. xar v4.2d, v16.2d, v25.2d, (64 - 45)
  129. xar v16.2d, v5.2d, v30.2d, (64 - 36)
  130. xar v5.2d, v3.2d, v27.2d, (64 - 28)
  131. xar v27.2d, v18.2d, v27.2d, (64 - 21)
  132. xar v3.2d, v17.2d, v26.2d, (64 - 15)
  133. xar v25.2d, v11.2d, v25.2d, (64 - 10)
  134. xar v26.2d, v7.2d, v26.2d, (64 - 6)
  135. xar v30.2d, v10.2d, v30.2d, (64 - 3)
  136. bcax v20.16b, v31.16b, v22.16b, v8.16b
  137. bcax v21.16b, v8.16b, v23.16b, v22.16b
  138. bcax v22.16b, v22.16b, v24.16b, v23.16b
  139. bcax v23.16b, v23.16b, v31.16b, v24.16b
  140. bcax v24.16b, v24.16b, v8.16b, v31.16b
  141. ld1r {v31.2d}, [x9], #8
  142. bcax v17.16b, v25.16b, v19.16b, v3.16b
  143. bcax v18.16b, v3.16b, v15.16b, v19.16b
  144. bcax v19.16b, v19.16b, v16.16b, v15.16b
  145. bcax v15.16b, v15.16b, v25.16b, v16.16b
  146. bcax v16.16b, v16.16b, v3.16b, v25.16b
  147. bcax v10.16b, v29.16b, v12.16b, v26.16b
  148. bcax v11.16b, v26.16b, v13.16b, v12.16b
  149. bcax v12.16b, v12.16b, v14.16b, v13.16b
  150. bcax v13.16b, v13.16b, v29.16b, v14.16b
  151. bcax v14.16b, v14.16b, v26.16b, v29.16b
  152. bcax v7.16b, v30.16b, v9.16b, v4.16b
  153. bcax v8.16b, v4.16b, v5.16b, v9.16b
  154. bcax v9.16b, v9.16b, v6.16b, v5.16b
  155. bcax v5.16b, v5.16b, v30.16b, v6.16b
  156. bcax v6.16b, v6.16b, v4.16b, v30.16b
  157. bcax v3.16b, v27.16b, v0.16b, v28.16b
  158. bcax v4.16b, v28.16b, v1.16b, v0.16b
  159. bcax v0.16b, v0.16b, v2.16b, v1.16b
  160. bcax v1.16b, v1.16b, v27.16b, v2.16b
  161. bcax v2.16b, v2.16b, v28.16b, v27.16b
  162. eor v0.16b, v0.16b, v31.16b
  163. cbnz w8, 4b
  164. cbz w21, 5f
  165. if_will_cond_yield_neon
  166. add x8, x19, #32
  167. st1 { v0.1d- v3.1d}, [x19]
  168. st1 { v4.1d- v7.1d}, [x8], #32
  169. st1 { v8.1d-v11.1d}, [x8], #32
  170. st1 {v12.1d-v15.1d}, [x8], #32
  171. st1 {v16.1d-v19.1d}, [x8], #32
  172. st1 {v20.1d-v23.1d}, [x8], #32
  173. st1 {v24.1d}, [x8]
  174. do_cond_yield_neon
  175. b 0b
  176. endif_yield_neon
  177. b 1b
  178. /* save state */
  179. 5: st1 { v0.1d- v3.1d}, [x19], #32
  180. st1 { v4.1d- v7.1d}, [x19], #32
  181. st1 { v8.1d-v11.1d}, [x19], #32
  182. st1 {v12.1d-v15.1d}, [x19], #32
  183. st1 {v16.1d-v19.1d}, [x19], #32
  184. st1 {v20.1d-v23.1d}, [x19], #32
  185. st1 {v24.1d}, [x19]
  186. frame_pop
  187. ret
  188. ENDPROC(sha3_ce_transform)
  189. .section ".rodata", "a"
  190. .align 8
  191. .Lsha3_rcon:
  192. .quad 0x0000000000000001, 0x0000000000008082, 0x800000000000808a
  193. .quad 0x8000000080008000, 0x000000000000808b, 0x0000000080000001
  194. .quad 0x8000000080008081, 0x8000000000008009, 0x000000000000008a
  195. .quad 0x0000000000000088, 0x0000000080008009, 0x000000008000000a
  196. .quad 0x000000008000808b, 0x800000000000008b, 0x8000000000008089
  197. .quad 0x8000000000008003, 0x8000000000008002, 0x8000000000000080
  198. .quad 0x000000000000800a, 0x800000008000000a, 0x8000000080008081
  199. .quad 0x8000000000008080, 0x0000000080000001, 0x8000000080008008