sha512-avx2-asm.S 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. ########################################################################
  2. # Implement fast SHA-512 with AVX2 instructions. (x86_64)
  3. #
  4. # Copyright (C) 2013 Intel Corporation.
  5. #
  6. # Authors:
  7. # James Guilford <james.guilford@intel.com>
  8. # Kirk Yap <kirk.s.yap@intel.com>
  9. # David Cote <david.m.cote@intel.com>
  10. # Tim Chen <tim.c.chen@linux.intel.com>
  11. #
  12. # This software is available to you under a choice of one of two
  13. # licenses. You may choose to be licensed under the terms of the GNU
  14. # General Public License (GPL) Version 2, available from the file
  15. # COPYING in the main directory of this source tree, or the
  16. # OpenIB.org BSD license below:
  17. #
  18. # Redistribution and use in source and binary forms, with or
  19. # without modification, are permitted provided that the following
  20. # conditions are met:
  21. #
  22. # - Redistributions of source code must retain the above
  23. # copyright notice, this list of conditions and the following
  24. # disclaimer.
  25. #
  26. # - Redistributions in binary form must reproduce the above
  27. # copyright notice, this list of conditions and the following
  28. # disclaimer in the documentation and/or other materials
  29. # provided with the distribution.
  30. #
  31. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  32. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  33. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  34. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  35. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  36. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  37. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  38. # SOFTWARE.
  39. #
  40. ########################################################################
  41. #
  42. # This code is described in an Intel White-Paper:
  43. # "Fast SHA-512 Implementations on Intel Architecture Processors"
  44. #
  45. # To find it, surf to http://www.intel.com/p/en_US/embedded
  46. # and search for that title.
  47. #
  48. ########################################################################
  49. # This code schedules 1 blocks at a time, with 4 lanes per block
  50. ########################################################################
  51. #ifdef CONFIG_AS_AVX2
  52. #include <linux/linkage.h>
  53. .text
  54. # Virtual Registers
  55. Y_0 = %ymm4
  56. Y_1 = %ymm5
  57. Y_2 = %ymm6
  58. Y_3 = %ymm7
  59. YTMP0 = %ymm0
  60. YTMP1 = %ymm1
  61. YTMP2 = %ymm2
  62. YTMP3 = %ymm3
  63. YTMP4 = %ymm8
  64. XFER = YTMP0
  65. BYTE_FLIP_MASK = %ymm9
  66. # 1st arg
  67. CTX = %rdi
  68. # 2nd arg
  69. INP = %rsi
  70. # 3rd arg
  71. NUM_BLKS = %rdx
  72. c = %rcx
  73. d = %r8
  74. e = %rdx
  75. y3 = %rsi
  76. TBL = %rbp
  77. a = %rax
  78. b = %rbx
  79. f = %r9
  80. g = %r10
  81. h = %r11
  82. old_h = %r11
  83. T1 = %r12
  84. y0 = %r13
  85. y1 = %r14
  86. y2 = %r15
  87. y4 = %r12
  88. # Local variables (stack frame)
  89. XFER_SIZE = 4*8
  90. SRND_SIZE = 1*8
  91. INP_SIZE = 1*8
  92. INPEND_SIZE = 1*8
  93. RSPSAVE_SIZE = 1*8
  94. GPRSAVE_SIZE = 6*8
  95. frame_XFER = 0
  96. frame_SRND = frame_XFER + XFER_SIZE
  97. frame_INP = frame_SRND + SRND_SIZE
  98. frame_INPEND = frame_INP + INP_SIZE
  99. frame_RSPSAVE = frame_INPEND + INPEND_SIZE
  100. frame_GPRSAVE = frame_RSPSAVE + RSPSAVE_SIZE
  101. frame_size = frame_GPRSAVE + GPRSAVE_SIZE
  102. ## assume buffers not aligned
  103. #define VMOVDQ vmovdqu
  104. # addm [mem], reg
  105. # Add reg to mem using reg-mem add and store
  106. .macro addm p1 p2
  107. add \p1, \p2
  108. mov \p2, \p1
  109. .endm
  110. # COPY_YMM_AND_BSWAP ymm, [mem], byte_flip_mask
  111. # Load ymm with mem and byte swap each dword
  112. .macro COPY_YMM_AND_BSWAP p1 p2 p3
  113. VMOVDQ \p2, \p1
  114. vpshufb \p3, \p1, \p1
  115. .endm
  116. # rotate_Ys
  117. # Rotate values of symbols Y0...Y3
  118. .macro rotate_Ys
  119. Y_ = Y_0
  120. Y_0 = Y_1
  121. Y_1 = Y_2
  122. Y_2 = Y_3
  123. Y_3 = Y_
  124. .endm
  125. # RotateState
  126. .macro RotateState
  127. # Rotate symbols a..h right
  128. old_h = h
  129. TMP_ = h
  130. h = g
  131. g = f
  132. f = e
  133. e = d
  134. d = c
  135. c = b
  136. b = a
  137. a = TMP_
  138. .endm
  139. # macro MY_VPALIGNR YDST, YSRC1, YSRC2, RVAL
  140. # YDST = {YSRC1, YSRC2} >> RVAL*8
  141. .macro MY_VPALIGNR YDST YSRC1 YSRC2 RVAL
  142. vperm2f128 $0x3, \YSRC2, \YSRC1, \YDST # YDST = {YS1_LO, YS2_HI}
  143. vpalignr $\RVAL, \YSRC2, \YDST, \YDST # YDST = {YDS1, YS2} >> RVAL*8
  144. .endm
  145. .macro FOUR_ROUNDS_AND_SCHED
  146. ################################### RND N + 0 #########################################
  147. # Extract w[t-7]
  148. MY_VPALIGNR YTMP0, Y_3, Y_2, 8 # YTMP0 = W[-7]
  149. # Calculate w[t-16] + w[t-7]
  150. vpaddq Y_0, YTMP0, YTMP0 # YTMP0 = W[-7] + W[-16]
  151. # Extract w[t-15]
  152. MY_VPALIGNR YTMP1, Y_1, Y_0, 8 # YTMP1 = W[-15]
  153. # Calculate sigma0
  154. # Calculate w[t-15] ror 1
  155. vpsrlq $1, YTMP1, YTMP2
  156. vpsllq $(64-1), YTMP1, YTMP3
  157. vpor YTMP2, YTMP3, YTMP3 # YTMP3 = W[-15] ror 1
  158. # Calculate w[t-15] shr 7
  159. vpsrlq $7, YTMP1, YTMP4 # YTMP4 = W[-15] >> 7
  160. mov a, y3 # y3 = a # MAJA
  161. rorx $41, e, y0 # y0 = e >> 41 # S1A
  162. rorx $18, e, y1 # y1 = e >> 18 # S1B
  163. add frame_XFER(%rsp),h # h = k + w + h # --
  164. or c, y3 # y3 = a|c # MAJA
  165. mov f, y2 # y2 = f # CH
  166. rorx $34, a, T1 # T1 = a >> 34 # S0B
  167. xor y1, y0 # y0 = (e>>41) ^ (e>>18) # S1
  168. xor g, y2 # y2 = f^g # CH
  169. rorx $14, e, y1 # y1 = (e >> 14) # S1
  170. and e, y2 # y2 = (f^g)&e # CH
  171. xor y1, y0 # y0 = (e>>41) ^ (e>>18) ^ (e>>14) # S1
  172. rorx $39, a, y1 # y1 = a >> 39 # S0A
  173. add h, d # d = k + w + h + d # --
  174. and b, y3 # y3 = (a|c)&b # MAJA
  175. xor T1, y1 # y1 = (a>>39) ^ (a>>34) # S0
  176. rorx $28, a, T1 # T1 = (a >> 28) # S0
  177. xor g, y2 # y2 = CH = ((f^g)&e)^g # CH
  178. xor T1, y1 # y1 = (a>>39) ^ (a>>34) ^ (a>>28) # S0
  179. mov a, T1 # T1 = a # MAJB
  180. and c, T1 # T1 = a&c # MAJB
  181. add y0, y2 # y2 = S1 + CH # --
  182. or T1, y3 # y3 = MAJ = (a|c)&b)|(a&c) # MAJ
  183. add y1, h # h = k + w + h + S0 # --
  184. add y2, d # d = k + w + h + d + S1 + CH = d + t1 # --
  185. add y2, h # h = k + w + h + S0 + S1 + CH = t1 + S0# --
  186. add y3, h # h = t1 + S0 + MAJ # --
  187. RotateState
  188. ################################### RND N + 1 #########################################
  189. # Calculate w[t-15] ror 8
  190. vpsrlq $8, YTMP1, YTMP2
  191. vpsllq $(64-8), YTMP1, YTMP1
  192. vpor YTMP2, YTMP1, YTMP1 # YTMP1 = W[-15] ror 8
  193. # XOR the three components
  194. vpxor YTMP4, YTMP3, YTMP3 # YTMP3 = W[-15] ror 1 ^ W[-15] >> 7
  195. vpxor YTMP1, YTMP3, YTMP1 # YTMP1 = s0
  196. # Add three components, w[t-16], w[t-7] and sigma0
  197. vpaddq YTMP1, YTMP0, YTMP0 # YTMP0 = W[-16] + W[-7] + s0
  198. # Move to appropriate lanes for calculating w[16] and w[17]
  199. vperm2f128 $0x0, YTMP0, YTMP0, Y_0 # Y_0 = W[-16] + W[-7] + s0 {BABA}
  200. # Move to appropriate lanes for calculating w[18] and w[19]
  201. vpand MASK_YMM_LO(%rip), YTMP0, YTMP0 # YTMP0 = W[-16] + W[-7] + s0 {DC00}
  202. # Calculate w[16] and w[17] in both 128 bit lanes
  203. # Calculate sigma1 for w[16] and w[17] on both 128 bit lanes
  204. vperm2f128 $0x11, Y_3, Y_3, YTMP2 # YTMP2 = W[-2] {BABA}
  205. vpsrlq $6, YTMP2, YTMP4 # YTMP4 = W[-2] >> 6 {BABA}
  206. mov a, y3 # y3 = a # MAJA
  207. rorx $41, e, y0 # y0 = e >> 41 # S1A
  208. rorx $18, e, y1 # y1 = e >> 18 # S1B
  209. add 1*8+frame_XFER(%rsp), h # h = k + w + h # --
  210. or c, y3 # y3 = a|c # MAJA
  211. mov f, y2 # y2 = f # CH
  212. rorx $34, a, T1 # T1 = a >> 34 # S0B
  213. xor y1, y0 # y0 = (e>>41) ^ (e>>18) # S1
  214. xor g, y2 # y2 = f^g # CH
  215. rorx $14, e, y1 # y1 = (e >> 14) # S1
  216. xor y1, y0 # y0 = (e>>41) ^ (e>>18) ^ (e>>14) # S1
  217. rorx $39, a, y1 # y1 = a >> 39 # S0A
  218. and e, y2 # y2 = (f^g)&e # CH
  219. add h, d # d = k + w + h + d # --
  220. and b, y3 # y3 = (a|c)&b # MAJA
  221. xor T1, y1 # y1 = (a>>39) ^ (a>>34) # S0
  222. rorx $28, a, T1 # T1 = (a >> 28) # S0
  223. xor g, y2 # y2 = CH = ((f^g)&e)^g # CH
  224. xor T1, y1 # y1 = (a>>39) ^ (a>>34) ^ (a>>28) # S0
  225. mov a, T1 # T1 = a # MAJB
  226. and c, T1 # T1 = a&c # MAJB
  227. add y0, y2 # y2 = S1 + CH # --
  228. or T1, y3 # y3 = MAJ = (a|c)&b)|(a&c) # MAJ
  229. add y1, h # h = k + w + h + S0 # --
  230. add y2, d # d = k + w + h + d + S1 + CH = d + t1 # --
  231. add y2, h # h = k + w + h + S0 + S1 + CH = t1 + S0# --
  232. add y3, h # h = t1 + S0 + MAJ # --
  233. RotateState
  234. ################################### RND N + 2 #########################################
  235. vpsrlq $19, YTMP2, YTMP3 # YTMP3 = W[-2] >> 19 {BABA}
  236. vpsllq $(64-19), YTMP2, YTMP1 # YTMP1 = W[-2] << 19 {BABA}
  237. vpor YTMP1, YTMP3, YTMP3 # YTMP3 = W[-2] ror 19 {BABA}
  238. vpxor YTMP3, YTMP4, YTMP4 # YTMP4 = W[-2] ror 19 ^ W[-2] >> 6 {BABA}
  239. vpsrlq $61, YTMP2, YTMP3 # YTMP3 = W[-2] >> 61 {BABA}
  240. vpsllq $(64-61), YTMP2, YTMP1 # YTMP1 = W[-2] << 61 {BABA}
  241. vpor YTMP1, YTMP3, YTMP3 # YTMP3 = W[-2] ror 61 {BABA}
  242. vpxor YTMP3, YTMP4, YTMP4 # YTMP4 = s1 = (W[-2] ror 19) ^
  243. # (W[-2] ror 61) ^ (W[-2] >> 6) {BABA}
  244. # Add sigma1 to the other compunents to get w[16] and w[17]
  245. vpaddq YTMP4, Y_0, Y_0 # Y_0 = {W[1], W[0], W[1], W[0]}
  246. # Calculate sigma1 for w[18] and w[19] for upper 128 bit lane
  247. vpsrlq $6, Y_0, YTMP4 # YTMP4 = W[-2] >> 6 {DC--}
  248. mov a, y3 # y3 = a # MAJA
  249. rorx $41, e, y0 # y0 = e >> 41 # S1A
  250. add 2*8+frame_XFER(%rsp), h # h = k + w + h # --
  251. rorx $18, e, y1 # y1 = e >> 18 # S1B
  252. or c, y3 # y3 = a|c # MAJA
  253. mov f, y2 # y2 = f # CH
  254. xor g, y2 # y2 = f^g # CH
  255. rorx $34, a, T1 # T1 = a >> 34 # S0B
  256. xor y1, y0 # y0 = (e>>41) ^ (e>>18) # S1
  257. and e, y2 # y2 = (f^g)&e # CH
  258. rorx $14, e, y1 # y1 = (e >> 14) # S1
  259. add h, d # d = k + w + h + d # --
  260. and b, y3 # y3 = (a|c)&b # MAJA
  261. xor y1, y0 # y0 = (e>>41) ^ (e>>18) ^ (e>>14) # S1
  262. rorx $39, a, y1 # y1 = a >> 39 # S0A
  263. xor g, y2 # y2 = CH = ((f^g)&e)^g # CH
  264. xor T1, y1 # y1 = (a>>39) ^ (a>>34) # S0
  265. rorx $28, a, T1 # T1 = (a >> 28) # S0
  266. xor T1, y1 # y1 = (a>>39) ^ (a>>34) ^ (a>>28) # S0
  267. mov a, T1 # T1 = a # MAJB
  268. and c, T1 # T1 = a&c # MAJB
  269. add y0, y2 # y2 = S1 + CH # --
  270. or T1, y3 # y3 = MAJ = (a|c)&b)|(a&c) # MAJ
  271. add y1, h # h = k + w + h + S0 # --
  272. add y2, d # d = k + w + h + d + S1 + CH = d + t1 # --
  273. add y2, h # h = k + w + h + S0 + S1 + CH = t1 + S0# --
  274. add y3, h # h = t1 + S0 + MAJ # --
  275. RotateState
  276. ################################### RND N + 3 #########################################
  277. vpsrlq $19, Y_0, YTMP3 # YTMP3 = W[-2] >> 19 {DC--}
  278. vpsllq $(64-19), Y_0, YTMP1 # YTMP1 = W[-2] << 19 {DC--}
  279. vpor YTMP1, YTMP3, YTMP3 # YTMP3 = W[-2] ror 19 {DC--}
  280. vpxor YTMP3, YTMP4, YTMP4 # YTMP4 = W[-2] ror 19 ^ W[-2] >> 6 {DC--}
  281. vpsrlq $61, Y_0, YTMP3 # YTMP3 = W[-2] >> 61 {DC--}
  282. vpsllq $(64-61), Y_0, YTMP1 # YTMP1 = W[-2] << 61 {DC--}
  283. vpor YTMP1, YTMP3, YTMP3 # YTMP3 = W[-2] ror 61 {DC--}
  284. vpxor YTMP3, YTMP4, YTMP4 # YTMP4 = s1 = (W[-2] ror 19) ^
  285. # (W[-2] ror 61) ^ (W[-2] >> 6) {DC--}
  286. # Add the sigma0 + w[t-7] + w[t-16] for w[18] and w[19]
  287. # to newly calculated sigma1 to get w[18] and w[19]
  288. vpaddq YTMP4, YTMP0, YTMP2 # YTMP2 = {W[3], W[2], --, --}
  289. # Form w[19, w[18], w17], w[16]
  290. vpblendd $0xF0, YTMP2, Y_0, Y_0 # Y_0 = {W[3], W[2], W[1], W[0]}
  291. mov a, y3 # y3 = a # MAJA
  292. rorx $41, e, y0 # y0 = e >> 41 # S1A
  293. rorx $18, e, y1 # y1 = e >> 18 # S1B
  294. add 3*8+frame_XFER(%rsp), h # h = k + w + h # --
  295. or c, y3 # y3 = a|c # MAJA
  296. mov f, y2 # y2 = f # CH
  297. rorx $34, a, T1 # T1 = a >> 34 # S0B
  298. xor y1, y0 # y0 = (e>>41) ^ (e>>18) # S1
  299. xor g, y2 # y2 = f^g # CH
  300. rorx $14, e, y1 # y1 = (e >> 14) # S1
  301. and e, y2 # y2 = (f^g)&e # CH
  302. add h, d # d = k + w + h + d # --
  303. and b, y3 # y3 = (a|c)&b # MAJA
  304. xor y1, y0 # y0 = (e>>41) ^ (e>>18) ^ (e>>14) # S1
  305. xor g, y2 # y2 = CH = ((f^g)&e)^g # CH
  306. rorx $39, a, y1 # y1 = a >> 39 # S0A
  307. add y0, y2 # y2 = S1 + CH # --
  308. xor T1, y1 # y1 = (a>>39) ^ (a>>34) # S0
  309. add y2, d # d = k + w + h + d + S1 + CH = d + t1 # --
  310. rorx $28, a, T1 # T1 = (a >> 28) # S0
  311. xor T1, y1 # y1 = (a>>39) ^ (a>>34) ^ (a>>28) # S0
  312. mov a, T1 # T1 = a # MAJB
  313. and c, T1 # T1 = a&c # MAJB
  314. or T1, y3 # y3 = MAJ = (a|c)&b)|(a&c) # MAJ
  315. add y1, h # h = k + w + h + S0 # --
  316. add y2, h # h = k + w + h + S0 + S1 + CH = t1 + S0# --
  317. add y3, h # h = t1 + S0 + MAJ # --
  318. RotateState
  319. rotate_Ys
  320. .endm
  321. .macro DO_4ROUNDS
  322. ################################### RND N + 0 #########################################
  323. mov f, y2 # y2 = f # CH
  324. rorx $41, e, y0 # y0 = e >> 41 # S1A
  325. rorx $18, e, y1 # y1 = e >> 18 # S1B
  326. xor g, y2 # y2 = f^g # CH
  327. xor y1, y0 # y0 = (e>>41) ^ (e>>18) # S1
  328. rorx $14, e, y1 # y1 = (e >> 14) # S1
  329. and e, y2 # y2 = (f^g)&e # CH
  330. xor y1, y0 # y0 = (e>>41) ^ (e>>18) ^ (e>>14) # S1
  331. rorx $34, a, T1 # T1 = a >> 34 # S0B
  332. xor g, y2 # y2 = CH = ((f^g)&e)^g # CH
  333. rorx $39, a, y1 # y1 = a >> 39 # S0A
  334. mov a, y3 # y3 = a # MAJA
  335. xor T1, y1 # y1 = (a>>39) ^ (a>>34) # S0
  336. rorx $28, a, T1 # T1 = (a >> 28) # S0
  337. add frame_XFER(%rsp), h # h = k + w + h # --
  338. or c, y3 # y3 = a|c # MAJA
  339. xor T1, y1 # y1 = (a>>39) ^ (a>>34) ^ (a>>28) # S0
  340. mov a, T1 # T1 = a # MAJB
  341. and b, y3 # y3 = (a|c)&b # MAJA
  342. and c, T1 # T1 = a&c # MAJB
  343. add y0, y2 # y2 = S1 + CH # --
  344. add h, d # d = k + w + h + d # --
  345. or T1, y3 # y3 = MAJ = (a|c)&b)|(a&c) # MAJ
  346. add y1, h # h = k + w + h + S0 # --
  347. add y2, d # d = k + w + h + d + S1 + CH = d + t1 # --
  348. RotateState
  349. ################################### RND N + 1 #########################################
  350. add y2, old_h # h = k + w + h + S0 + S1 + CH = t1 + S0# --
  351. mov f, y2 # y2 = f # CH
  352. rorx $41, e, y0 # y0 = e >> 41 # S1A
  353. rorx $18, e, y1 # y1 = e >> 18 # S1B
  354. xor g, y2 # y2 = f^g # CH
  355. xor y1, y0 # y0 = (e>>41) ^ (e>>18) # S1
  356. rorx $14, e, y1 # y1 = (e >> 14) # S1
  357. and e, y2 # y2 = (f^g)&e # CH
  358. add y3, old_h # h = t1 + S0 + MAJ # --
  359. xor y1, y0 # y0 = (e>>41) ^ (e>>18) ^ (e>>14) # S1
  360. rorx $34, a, T1 # T1 = a >> 34 # S0B
  361. xor g, y2 # y2 = CH = ((f^g)&e)^g # CH
  362. rorx $39, a, y1 # y1 = a >> 39 # S0A
  363. mov a, y3 # y3 = a # MAJA
  364. xor T1, y1 # y1 = (a>>39) ^ (a>>34) # S0
  365. rorx $28, a, T1 # T1 = (a >> 28) # S0
  366. add 8*1+frame_XFER(%rsp), h # h = k + w + h # --
  367. or c, y3 # y3 = a|c # MAJA
  368. xor T1, y1 # y1 = (a>>39) ^ (a>>34) ^ (a>>28) # S0
  369. mov a, T1 # T1 = a # MAJB
  370. and b, y3 # y3 = (a|c)&b # MAJA
  371. and c, T1 # T1 = a&c # MAJB
  372. add y0, y2 # y2 = S1 + CH # --
  373. add h, d # d = k + w + h + d # --
  374. or T1, y3 # y3 = MAJ = (a|c)&b)|(a&c) # MAJ
  375. add y1, h # h = k + w + h + S0 # --
  376. add y2, d # d = k + w + h + d + S1 + CH = d + t1 # --
  377. RotateState
  378. ################################### RND N + 2 #########################################
  379. add y2, old_h # h = k + w + h + S0 + S1 + CH = t1 + S0# --
  380. mov f, y2 # y2 = f # CH
  381. rorx $41, e, y0 # y0 = e >> 41 # S1A
  382. rorx $18, e, y1 # y1 = e >> 18 # S1B
  383. xor g, y2 # y2 = f^g # CH
  384. xor y1, y0 # y0 = (e>>41) ^ (e>>18) # S1
  385. rorx $14, e, y1 # y1 = (e >> 14) # S1
  386. and e, y2 # y2 = (f^g)&e # CH
  387. add y3, old_h # h = t1 + S0 + MAJ # --
  388. xor y1, y0 # y0 = (e>>41) ^ (e>>18) ^ (e>>14) # S1
  389. rorx $34, a, T1 # T1 = a >> 34 # S0B
  390. xor g, y2 # y2 = CH = ((f^g)&e)^g # CH
  391. rorx $39, a, y1 # y1 = a >> 39 # S0A
  392. mov a, y3 # y3 = a # MAJA
  393. xor T1, y1 # y1 = (a>>39) ^ (a>>34) # S0
  394. rorx $28, a, T1 # T1 = (a >> 28) # S0
  395. add 8*2+frame_XFER(%rsp), h # h = k + w + h # --
  396. or c, y3 # y3 = a|c # MAJA
  397. xor T1, y1 # y1 = (a>>39) ^ (a>>34) ^ (a>>28) # S0
  398. mov a, T1 # T1 = a # MAJB
  399. and b, y3 # y3 = (a|c)&b # MAJA
  400. and c, T1 # T1 = a&c # MAJB
  401. add y0, y2 # y2 = S1 + CH # --
  402. add h, d # d = k + w + h + d # --
  403. or T1, y3 # y3 = MAJ = (a|c)&b)|(a&c) # MAJ
  404. add y1, h # h = k + w + h + S0 # --
  405. add y2, d # d = k + w + h + d + S1 + CH = d + t1 # --
  406. RotateState
  407. ################################### RND N + 3 #########################################
  408. add y2, old_h # h = k + w + h + S0 + S1 + CH = t1 + S0# --
  409. mov f, y2 # y2 = f # CH
  410. rorx $41, e, y0 # y0 = e >> 41 # S1A
  411. rorx $18, e, y1 # y1 = e >> 18 # S1B
  412. xor g, y2 # y2 = f^g # CH
  413. xor y1, y0 # y0 = (e>>41) ^ (e>>18) # S1
  414. rorx $14, e, y1 # y1 = (e >> 14) # S1
  415. and e, y2 # y2 = (f^g)&e # CH
  416. add y3, old_h # h = t1 + S0 + MAJ # --
  417. xor y1, y0 # y0 = (e>>41) ^ (e>>18) ^ (e>>14) # S1
  418. rorx $34, a, T1 # T1 = a >> 34 # S0B
  419. xor g, y2 # y2 = CH = ((f^g)&e)^g # CH
  420. rorx $39, a, y1 # y1 = a >> 39 # S0A
  421. mov a, y3 # y3 = a # MAJA
  422. xor T1, y1 # y1 = (a>>39) ^ (a>>34) # S0
  423. rorx $28, a, T1 # T1 = (a >> 28) # S0
  424. add 8*3+frame_XFER(%rsp), h # h = k + w + h # --
  425. or c, y3 # y3 = a|c # MAJA
  426. xor T1, y1 # y1 = (a>>39) ^ (a>>34) ^ (a>>28) # S0
  427. mov a, T1 # T1 = a # MAJB
  428. and b, y3 # y3 = (a|c)&b # MAJA
  429. and c, T1 # T1 = a&c # MAJB
  430. add y0, y2 # y2 = S1 + CH # --
  431. add h, d # d = k + w + h + d # --
  432. or T1, y3 # y3 = MAJ = (a|c)&b)|(a&c) # MAJ
  433. add y1, h # h = k + w + h + S0 # --
  434. add y2, d # d = k + w + h + d + S1 + CH = d + t1 # --
  435. add y2, h # h = k + w + h + S0 + S1 + CH = t1 + S0# --
  436. add y3, h # h = t1 + S0 + MAJ # --
  437. RotateState
  438. .endm
  439. ########################################################################
  440. # void sha512_transform_rorx(void* D, const void* M, uint64_t L)#
  441. # Purpose: Updates the SHA512 digest stored at D with the message stored in M.
  442. # The size of the message pointed to by M must be an integer multiple of SHA512
  443. # message blocks.
  444. # L is the message length in SHA512 blocks
  445. ########################################################################
  446. ENTRY(sha512_transform_rorx)
  447. # Allocate Stack Space
  448. mov %rsp, %rax
  449. sub $frame_size, %rsp
  450. and $~(0x20 - 1), %rsp
  451. mov %rax, frame_RSPSAVE(%rsp)
  452. # Save GPRs
  453. mov %rbp, frame_GPRSAVE(%rsp)
  454. mov %rbx, 8*1+frame_GPRSAVE(%rsp)
  455. mov %r12, 8*2+frame_GPRSAVE(%rsp)
  456. mov %r13, 8*3+frame_GPRSAVE(%rsp)
  457. mov %r14, 8*4+frame_GPRSAVE(%rsp)
  458. mov %r15, 8*5+frame_GPRSAVE(%rsp)
  459. shl $7, NUM_BLKS # convert to bytes
  460. jz done_hash
  461. add INP, NUM_BLKS # pointer to end of data
  462. mov NUM_BLKS, frame_INPEND(%rsp)
  463. ## load initial digest
  464. mov 8*0(CTX),a
  465. mov 8*1(CTX),b
  466. mov 8*2(CTX),c
  467. mov 8*3(CTX),d
  468. mov 8*4(CTX),e
  469. mov 8*5(CTX),f
  470. mov 8*6(CTX),g
  471. mov 8*7(CTX),h
  472. vmovdqa PSHUFFLE_BYTE_FLIP_MASK(%rip), BYTE_FLIP_MASK
  473. loop0:
  474. lea K512(%rip), TBL
  475. ## byte swap first 16 dwords
  476. COPY_YMM_AND_BSWAP Y_0, (INP), BYTE_FLIP_MASK
  477. COPY_YMM_AND_BSWAP Y_1, 1*32(INP), BYTE_FLIP_MASK
  478. COPY_YMM_AND_BSWAP Y_2, 2*32(INP), BYTE_FLIP_MASK
  479. COPY_YMM_AND_BSWAP Y_3, 3*32(INP), BYTE_FLIP_MASK
  480. mov INP, frame_INP(%rsp)
  481. ## schedule 64 input dwords, by doing 12 rounds of 4 each
  482. movq $4, frame_SRND(%rsp)
  483. .align 16
  484. loop1:
  485. vpaddq (TBL), Y_0, XFER
  486. vmovdqa XFER, frame_XFER(%rsp)
  487. FOUR_ROUNDS_AND_SCHED
  488. vpaddq 1*32(TBL), Y_0, XFER
  489. vmovdqa XFER, frame_XFER(%rsp)
  490. FOUR_ROUNDS_AND_SCHED
  491. vpaddq 2*32(TBL), Y_0, XFER
  492. vmovdqa XFER, frame_XFER(%rsp)
  493. FOUR_ROUNDS_AND_SCHED
  494. vpaddq 3*32(TBL), Y_0, XFER
  495. vmovdqa XFER, frame_XFER(%rsp)
  496. add $(4*32), TBL
  497. FOUR_ROUNDS_AND_SCHED
  498. subq $1, frame_SRND(%rsp)
  499. jne loop1
  500. movq $2, frame_SRND(%rsp)
  501. loop2:
  502. vpaddq (TBL), Y_0, XFER
  503. vmovdqa XFER, frame_XFER(%rsp)
  504. DO_4ROUNDS
  505. vpaddq 1*32(TBL), Y_1, XFER
  506. vmovdqa XFER, frame_XFER(%rsp)
  507. add $(2*32), TBL
  508. DO_4ROUNDS
  509. vmovdqa Y_2, Y_0
  510. vmovdqa Y_3, Y_1
  511. subq $1, frame_SRND(%rsp)
  512. jne loop2
  513. addm 8*0(CTX),a
  514. addm 8*1(CTX),b
  515. addm 8*2(CTX),c
  516. addm 8*3(CTX),d
  517. addm 8*4(CTX),e
  518. addm 8*5(CTX),f
  519. addm 8*6(CTX),g
  520. addm 8*7(CTX),h
  521. mov frame_INP(%rsp), INP
  522. add $128, INP
  523. cmp frame_INPEND(%rsp), INP
  524. jne loop0
  525. done_hash:
  526. # Restore GPRs
  527. mov frame_GPRSAVE(%rsp) ,%rbp
  528. mov 8*1+frame_GPRSAVE(%rsp) ,%rbx
  529. mov 8*2+frame_GPRSAVE(%rsp) ,%r12
  530. mov 8*3+frame_GPRSAVE(%rsp) ,%r13
  531. mov 8*4+frame_GPRSAVE(%rsp) ,%r14
  532. mov 8*5+frame_GPRSAVE(%rsp) ,%r15
  533. # Restore Stack Pointer
  534. mov frame_RSPSAVE(%rsp), %rsp
  535. ret
  536. ENDPROC(sha512_transform_rorx)
  537. ########################################################################
  538. ### Binary Data
  539. .data
  540. .align 64
  541. # K[t] used in SHA512 hashing
  542. K512:
  543. .quad 0x428a2f98d728ae22,0x7137449123ef65cd
  544. .quad 0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc
  545. .quad 0x3956c25bf348b538,0x59f111f1b605d019
  546. .quad 0x923f82a4af194f9b,0xab1c5ed5da6d8118
  547. .quad 0xd807aa98a3030242,0x12835b0145706fbe
  548. .quad 0x243185be4ee4b28c,0x550c7dc3d5ffb4e2
  549. .quad 0x72be5d74f27b896f,0x80deb1fe3b1696b1
  550. .quad 0x9bdc06a725c71235,0xc19bf174cf692694
  551. .quad 0xe49b69c19ef14ad2,0xefbe4786384f25e3
  552. .quad 0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65
  553. .quad 0x2de92c6f592b0275,0x4a7484aa6ea6e483
  554. .quad 0x5cb0a9dcbd41fbd4,0x76f988da831153b5
  555. .quad 0x983e5152ee66dfab,0xa831c66d2db43210
  556. .quad 0xb00327c898fb213f,0xbf597fc7beef0ee4
  557. .quad 0xc6e00bf33da88fc2,0xd5a79147930aa725
  558. .quad 0x06ca6351e003826f,0x142929670a0e6e70
  559. .quad 0x27b70a8546d22ffc,0x2e1b21385c26c926
  560. .quad 0x4d2c6dfc5ac42aed,0x53380d139d95b3df
  561. .quad 0x650a73548baf63de,0x766a0abb3c77b2a8
  562. .quad 0x81c2c92e47edaee6,0x92722c851482353b
  563. .quad 0xa2bfe8a14cf10364,0xa81a664bbc423001
  564. .quad 0xc24b8b70d0f89791,0xc76c51a30654be30
  565. .quad 0xd192e819d6ef5218,0xd69906245565a910
  566. .quad 0xf40e35855771202a,0x106aa07032bbd1b8
  567. .quad 0x19a4c116b8d2d0c8,0x1e376c085141ab53
  568. .quad 0x2748774cdf8eeb99,0x34b0bcb5e19b48a8
  569. .quad 0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb
  570. .quad 0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3
  571. .quad 0x748f82ee5defb2fc,0x78a5636f43172f60
  572. .quad 0x84c87814a1f0ab72,0x8cc702081a6439ec
  573. .quad 0x90befffa23631e28,0xa4506cebde82bde9
  574. .quad 0xbef9a3f7b2c67915,0xc67178f2e372532b
  575. .quad 0xca273eceea26619c,0xd186b8c721c0c207
  576. .quad 0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178
  577. .quad 0x06f067aa72176fba,0x0a637dc5a2c898a6
  578. .quad 0x113f9804bef90dae,0x1b710b35131c471b
  579. .quad 0x28db77f523047d84,0x32caab7b40c72493
  580. .quad 0x3c9ebe0a15c9bebc,0x431d67c49c100d4c
  581. .quad 0x4cc5d4becb3e42b6,0x597f299cfc657e2a
  582. .quad 0x5fcb6fab3ad6faec,0x6c44198c4a475817
  583. .align 32
  584. # Mask for byte-swapping a couple of qwords in an XMM register using (v)pshufb.
  585. PSHUFFLE_BYTE_FLIP_MASK:
  586. .octa 0x08090a0b0c0d0e0f0001020304050607
  587. .octa 0x18191a1b1c1d1e1f1011121314151617
  588. MASK_YMM_LO:
  589. .octa 0x00000000000000000000000000000000
  590. .octa 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  591. #endif