sha256_sse4_amd64.asm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. ;/*
  2. ; * Copyright 2011 Neil Kettle
  3. ; * Copyright 2011 Ufasoft
  4. ; * Copyright 2013 James Z.M. Gao
  5. ; * Copyright 2012-2013 Luke Dashjr
  6. ; *
  7. ; * This program is free software; you can redistribute it and/or modify it
  8. ; * under the terms of the GNU General Public License as published by the Free
  9. ; * Software Foundation; either version 3 of the License, or (at your option)
  10. ; * any later version. See COPYING for more details.
  11. ; */
  12. ALIGN 32
  13. BITS 64
  14. %ifidn __OUTPUT_FORMAT__,win64
  15. %define hash rcx
  16. %define data rdx
  17. %define init r8
  18. %define temp r9
  19. %else
  20. %define hash rdi
  21. %define data rsi
  22. %define init rdx
  23. %define temp rcx
  24. %endif
  25. ; 0 = (1024 - 256) (mod (LAB_CALC_UNROLL*LAB_CALC_PARA*16))
  26. %define LAB_CALC_PARA 2
  27. %define LAB_CALC_UNROLL 8
  28. %define LAB_LOOP_UNROLL 8
  29. extern g_4sha256_k
  30. global CalcSha256_x64_sse4
  31. ; CalcSha256 hash(rdi), data(rsi), init(rdx)
  32. ; CalcSha256 hash(rcx), data(rdx), init(r8)
  33. CalcSha256_x64_sse4:
  34. push rbx
  35. %ifidn __OUTPUT_FORMAT__,win64
  36. sub rsp, 16 * 6
  37. movdqa [rsp + 16*0], xmm6
  38. movdqa [rsp + 16*1], xmm7
  39. movdqa [rsp + 16*2], xmm8
  40. movdqa [rsp + 16*3], xmm9
  41. movdqa [rsp + 16*4], xmm10
  42. movdqa [rsp + 16*5], xmm11
  43. %endif
  44. LAB_NEXT_NONCE:
  45. mov temp, 64*4 ; 256 - temp is # of SHA-2 rounds
  46. mov rax, 16*4 ; 64 - rax is where we expand to
  47. LAB_SHA:
  48. push temp
  49. lea temp, qword [data+temp*4] ; + 1024
  50. lea r11, qword [data+rax*4] ; + 256
  51. LAB_CALC:
  52. %macro lab_calc_blk 1
  53. movntdqa xmm0, [r11-(15-%1)*16] ; xmm0 = W[I-15]
  54. movdqa xmm2, xmm0 ; xmm2 = W[I-15]
  55. movntdqa xmm4, [r11-(15-(%1+1))*16] ; xmm4 = W[I-15+1]
  56. movdqa xmm6, xmm4 ; xmm6 = W[I-15+1]
  57. psrld xmm0, 3 ; xmm0 = W[I-15] >> 3
  58. movdqa xmm1, xmm0 ; xmm1 = W[I-15] >> 3
  59. pslld xmm2, 14 ; xmm2 = W[I-15] << 14
  60. psrld xmm4, 3 ; xmm4 = W[I-15+1] >> 3
  61. movdqa xmm5, xmm4 ; xmm5 = W[I-15+1] >> 3
  62. psrld xmm5, 4 ; xmm5 = W[I-15+1] >> 7
  63. pxor xmm4, xmm5 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7)
  64. pslld xmm6, 14 ; xmm6 = W[I-15+1] << 14
  65. psrld xmm1, 4 ; xmm1 = W[I-15] >> 7
  66. pxor xmm0, xmm1 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7)
  67. pxor xmm0, xmm2 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14)
  68. psrld xmm1, 11 ; xmm1 = W[I-15] >> 18
  69. psrld xmm5, 11 ; xmm5 = W[I-15+1] >> 18
  70. pxor xmm4, xmm6 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14)
  71. pxor xmm4, xmm5 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14) ^ (W[I-15+1] >> 18)
  72. pslld xmm2, 11 ; xmm2 = W[I-15] << 25
  73. pslld xmm6, 11 ; xmm6 = W[I-15+1] << 25
  74. pxor xmm4, xmm6 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14) ^ (W[I-15+1] >> 18) ^ (W[I-15+1] << 25)
  75. pxor xmm0, xmm1 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14) ^ (W[I-15] >> 18)
  76. pxor xmm0, xmm2 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14) ^ (W[I-15] >> 18) ^ (W[I-15] << 25)
  77. paddd xmm0, [r11-(16-%1)*16] ; xmm0 = s0(W[I-15]) + W[I-16]
  78. paddd xmm4, [r11-(16-(%1+1))*16] ; xmm4 = s0(W[I-15+1]) + W[I-16+1]
  79. movntdqa xmm3, [r11-(2-%1)*16] ; xmm3 = W[I-2]
  80. movntdqa xmm7, [r11-(2-(%1+1))*16] ; xmm7 = W[I-2+1]
  81. ;;;;;;;;;;;;;;;;;;
  82. movdqa xmm2, xmm3 ; xmm2 = W[I-2]
  83. psrld xmm3, 10 ; xmm3 = W[I-2] >> 10
  84. movdqa xmm1, xmm3 ; xmm1 = W[I-2] >> 10
  85. movdqa xmm6, xmm7 ; xmm6 = W[I-2+1]
  86. psrld xmm7, 10 ; xmm7 = W[I-2+1] >> 10
  87. movdqa xmm5, xmm7 ; xmm5 = W[I-2+1] >> 10
  88. paddd xmm0, [r11-(7-%1)*16] ; xmm0 = s0(W[I-15]) + W[I-16] + W[I-7]
  89. paddd xmm4, [r11-(7-(%1+1))*16] ; xmm4 = s0(W[I-15+1]) + W[I-16+1] + W[I-7+1]
  90. pslld xmm2, 13 ; xmm2 = W[I-2] << 13
  91. pslld xmm6, 13 ; xmm6 = W[I-2+1] << 13
  92. psrld xmm1, 7 ; xmm1 = W[I-2] >> 17
  93. psrld xmm5, 7 ; xmm5 = W[I-2+1] >> 17
  94. pxor xmm3, xmm1 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17)
  95. psrld xmm1, 2 ; xmm1 = W[I-2] >> 19
  96. pxor xmm3, xmm2 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13)
  97. pslld xmm2, 2 ; xmm2 = W[I-2] << 15
  98. pxor xmm7, xmm5 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17)
  99. psrld xmm5, 2 ; xmm5 = W[I-2+1] >> 19
  100. pxor xmm7, xmm6 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13)
  101. pslld xmm6, 2 ; xmm6 = W[I-2+1] << 15
  102. pxor xmm3, xmm1 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13) ^ (W[I-2] >> 19)
  103. pxor xmm3, xmm2 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13) ^ (W[I-2] >> 19) ^ (W[I-2] << 15)
  104. paddd xmm0, xmm3 ; xmm0 = s0(W[I-15]) + W[I-16] + s1(W[I-2]) + W[I-7]
  105. pxor xmm7, xmm5 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13) ^ (W[I-2+1] >> 19)
  106. pxor xmm7, xmm6 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13) ^ (W[I-2+1] >> 19) ^ (W[I-2+1] << 15)
  107. paddd xmm4, xmm7 ; xmm4 = s0(W[I-15+1]) + W[I-16+1] + s1(W[I-2+1]) + W[I-7+1]
  108. movdqa [r11+(%1*16)], xmm0
  109. movdqa [r11+((%1+1)*16)], xmm4
  110. %endmacro
  111. %assign i 0
  112. %rep LAB_CALC_UNROLL
  113. lab_calc_blk i
  114. %assign i i+LAB_CALC_PARA
  115. %endrep
  116. add r11, LAB_CALC_UNROLL*LAB_CALC_PARA*16
  117. cmp r11, temp
  118. jb LAB_CALC
  119. pop temp
  120. mov rax, 0
  121. ; Load the init values of the message into the hash.
  122. movntdqa xmm7, [init]
  123. pshufd xmm5, xmm7, 0x55 ; xmm5 == b
  124. pshufd xmm4, xmm7, 0xAA ; xmm4 == c
  125. pshufd xmm3, xmm7, 0xFF ; xmm3 == d
  126. pshufd xmm7, xmm7, 0 ; xmm7 == a
  127. movntdqa xmm0, [init+4*4]
  128. pshufd xmm8, xmm0, 0x55 ; xmm8 == f
  129. pshufd xmm9, xmm0, 0xAA ; xmm9 == g
  130. pshufd xmm10, xmm0, 0xFF ; xmm10 == h
  131. pshufd xmm0, xmm0, 0 ; xmm0 == e
  132. LAB_LOOP:
  133. ;; T t1 = h + (Rotr32(e, 6) ^ Rotr32(e, 11) ^ Rotr32(e, 25)) + ((e & f) ^ AndNot(e, g)) + Expand32<T>(g_sha256_k[j]) + w[j]
  134. %macro lab_loop_blk 0
  135. movntdqa xmm6, [data+rax*4]
  136. paddd xmm6, g_4sha256_k[rax*4]
  137. add rax, 4
  138. paddd xmm6, xmm10 ; +h
  139. movdqa xmm1, xmm0
  140. movdqa xmm2, xmm9
  141. pandn xmm1, xmm2 ; ~e & g
  142. movdqa xmm10, xmm2 ; h = g
  143. movdqa xmm2, xmm8 ; f
  144. movdqa xmm9, xmm2 ; g = f
  145. pand xmm2, xmm0 ; e & f
  146. pxor xmm1, xmm2 ; (e & f) ^ (~e & g)
  147. movdqa xmm8, xmm0 ; f = e
  148. paddd xmm6, xmm1 ; Ch + h + w[i] + k[i]
  149. movdqa xmm1, xmm0
  150. psrld xmm0, 6
  151. movdqa xmm2, xmm0
  152. pslld xmm1, 7
  153. psrld xmm2, 5
  154. pxor xmm0, xmm1
  155. pxor xmm0, xmm2
  156. pslld xmm1, 14
  157. psrld xmm2, 14
  158. pxor xmm0, xmm1
  159. pxor xmm0, xmm2
  160. pslld xmm1, 5
  161. pxor xmm0, xmm1 ; Rotr32(e, 6) ^ Rotr32(e, 11) ^ Rotr32(e, 25)
  162. paddd xmm6, xmm0 ; xmm6 = t1
  163. movdqa xmm0, xmm3 ; d
  164. paddd xmm0, xmm6 ; e = d+t1
  165. movdqa xmm1, xmm5 ; =b
  166. movdqa xmm3, xmm4 ; d = c
  167. movdqa xmm2, xmm4 ; c
  168. pand xmm2, xmm5 ; b & c
  169. pand xmm4, xmm7 ; a & c
  170. pand xmm1, xmm7 ; a & b
  171. pxor xmm1, xmm4
  172. movdqa xmm4, xmm5 ; c = b
  173. movdqa xmm5, xmm7 ; b = a
  174. pxor xmm1, xmm2 ; (a & c) ^ (a & d) ^ (c & d)
  175. paddd xmm6, xmm1 ; t1 + ((a & c) ^ (a & d) ^ (c & d))
  176. movdqa xmm2, xmm7
  177. psrld xmm7, 2
  178. movdqa xmm1, xmm7
  179. pslld xmm2, 10
  180. psrld xmm1, 11
  181. pxor xmm7, xmm2
  182. pxor xmm7, xmm1
  183. pslld xmm2, 9
  184. psrld xmm1, 9
  185. pxor xmm7, xmm2
  186. pxor xmm7, xmm1
  187. pslld xmm2, 11
  188. pxor xmm7, xmm2
  189. paddd xmm7, xmm6 ; a = t1 + (Rotr32(a, 2) ^ Rotr32(a, 13) ^ Rotr32(a, 22)) + ((a & c) ^ (a & d) ^ (c & d));
  190. %endmacro
  191. %assign i 0
  192. %rep LAB_LOOP_UNROLL
  193. lab_loop_blk
  194. %assign i i+1
  195. %endrep
  196. cmp rax, temp
  197. jb LAB_LOOP
  198. ; Finished the 64 rounds, calculate hash and save
  199. movntdqa xmm1, [init]
  200. pshufd xmm2, xmm1, 0x55
  201. paddd xmm5, xmm2
  202. pshufd xmm6, xmm1, 0xAA
  203. paddd xmm4, xmm6
  204. pshufd xmm11, xmm1, 0xFF
  205. paddd xmm3, xmm11
  206. pshufd xmm1, xmm1, 0
  207. paddd xmm7, xmm1
  208. movntdqa xmm1, [init+4*4]
  209. pshufd xmm2, xmm1, 0x55
  210. paddd xmm8, xmm2
  211. pshufd xmm6, xmm1, 0xAA
  212. paddd xmm9, xmm6
  213. pshufd xmm11, xmm1, 0xFF
  214. paddd xmm10, xmm11
  215. pshufd xmm1, xmm1, 0
  216. paddd xmm0, xmm1
  217. movdqa [hash+0*16], xmm7
  218. movdqa [hash+1*16], xmm5
  219. movdqa [hash+2*16], xmm4
  220. movdqa [hash+3*16], xmm3
  221. movdqa [hash+4*16], xmm0
  222. movdqa [hash+5*16], xmm8
  223. movdqa [hash+6*16], xmm9
  224. movdqa [hash+7*16], xmm10
  225. LAB_RET:
  226. %ifidn __OUTPUT_FORMAT__,win64
  227. movdqa xmm6, [rsp + 16*0]
  228. movdqa xmm7, [rsp + 16*1]
  229. movdqa xmm8, [rsp + 16*2]
  230. movdqa xmm9, [rsp + 16*3]
  231. movdqa xmm10, [rsp + 16*4]
  232. movdqa xmm11, [rsp + 16*5]
  233. add rsp, 16 * 6
  234. %endif
  235. pop rbx
  236. ret
  237. %ifidn __OUTPUT_FORMAT__,elf
  238. section .note.GNU-stack noalloc noexec nowrite progbits
  239. %endif
  240. %ifidn __OUTPUT_FORMAT__,elf64
  241. section .note.GNU-stack noalloc noexec nowrite progbits
  242. %endif