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
52#include <linux/linkage.h>
53
54.text
55
56# Virtual Registers
57Y_0 = %ymm4
58Y_1 = %ymm5
59Y_2 = %ymm6
60Y_3 = %ymm7
61
62YTMP0 = %ymm0
63YTMP1 = %ymm1
64YTMP2 = %ymm2
65YTMP3 = %ymm3
66YTMP4 = %ymm8
67XFER  = YTMP0
68
69BYTE_FLIP_MASK  = %ymm9
70
71# 1st arg is %rdi, which is saved to the stack and accessed later via %r12
72CTX1        = %rdi
73CTX2        = %r12
74# 2nd arg
75INP         = %rsi
76# 3rd arg
77NUM_BLKS    = %rdx
78
79c           = %rcx
80d           = %r8
81e           = %rdx
82y3          = %rsi
83
84TBL   = %rdi # clobbers CTX1
85
86a     = %rax
87b     = %rbx
88
89f     = %r9
90g     = %r10
91h     = %r11
92old_h = %r11
93
94T1    = %r12 # clobbers CTX2
95y0    = %r13
96y1    = %r14
97y2    = %r15
98
99# Local variables (stack frame)
100XFER_SIZE = 4*8
101SRND_SIZE = 1*8
102INP_SIZE = 1*8
103INPEND_SIZE = 1*8
104CTX_SIZE = 1*8
105
106frame_XFER = 0
107frame_SRND = frame_XFER + XFER_SIZE
108frame_INP = frame_SRND + SRND_SIZE
109frame_INPEND = frame_INP + INP_SIZE
110frame_CTX = frame_INPEND + INPEND_SIZE
111frame_size = frame_CTX + CTX_SIZE
112
113## assume buffers not aligned
114#define	VMOVDQ vmovdqu
115
116# addm [mem], reg
117# Add reg to mem using reg-mem add and store
118.macro addm p1 p2
119	add	\p1, \p2
120	mov	\p2, \p1
121.endm
122
123
124# COPY_YMM_AND_BSWAP ymm, [mem], byte_flip_mask
125# Load ymm with mem and byte swap each dword
126.macro COPY_YMM_AND_BSWAP p1 p2 p3
127	VMOVDQ \p2, \p1
128	vpshufb \p3, \p1, \p1
129.endm
130# rotate_Ys
131# Rotate values of symbols Y0...Y3
132.macro rotate_Ys
133	Y_ = Y_0
134	Y_0 = Y_1
135	Y_1 = Y_2
136	Y_2 = Y_3
137	Y_3 = Y_
138.endm
139
140# RotateState
141.macro RotateState
142	# Rotate symbols a..h right
143	old_h  = h
144	TMP_   = h
145	h      = g
146	g      = f
147	f      = e
148	e      = d
149	d      = c
150	c      = b
151	b      = a
152	a      = TMP_
153.endm
154
155# macro MY_VPALIGNR	YDST, YSRC1, YSRC2, RVAL
156# YDST = {YSRC1, YSRC2} >> RVAL*8
157.macro MY_VPALIGNR YDST YSRC1 YSRC2 RVAL
158	vperm2f128      $0x3, \YSRC2, \YSRC1, \YDST     # YDST = {YS1_LO, YS2_HI}
159	vpalignr        $\RVAL, \YSRC2, \YDST, \YDST    # YDST = {YDS1, YS2} >> RVAL*8
160.endm
161
162.macro FOUR_ROUNDS_AND_SCHED
163################################### RND N + 0 #########################################
164
165	# Extract w[t-7]
166	MY_VPALIGNR	YTMP0, Y_3, Y_2, 8		# YTMP0 = W[-7]
167	# Calculate w[t-16] + w[t-7]
168	vpaddq		Y_0, YTMP0, YTMP0		# YTMP0 = W[-7] + W[-16]
169	# Extract w[t-15]
170	MY_VPALIGNR	YTMP1, Y_1, Y_0, 8		# YTMP1 = W[-15]
171
172	# Calculate sigma0
173
174	# Calculate w[t-15] ror 1
175	vpsrlq		$1, YTMP1, YTMP2
176	vpsllq		$(64-1), YTMP1, YTMP3
177	vpor		YTMP2, YTMP3, YTMP3		# YTMP3 = W[-15] ror 1
178	# Calculate w[t-15] shr 7
179	vpsrlq		$7, YTMP1, YTMP4		# YTMP4 = W[-15] >> 7
180
181	mov	a, y3		# y3 = a                                # MAJA
182	rorx	$41, e, y0	# y0 = e >> 41				# S1A
183	rorx	$18, e, y1	# y1 = e >> 18				# S1B
184	add	frame_XFER(%rsp),h		# h = k + w + h         # --
185	or	c, y3		# y3 = a|c                              # MAJA
186	mov	f, y2		# y2 = f                                # CH
187	rorx	$34, a, T1	# T1 = a >> 34				# S0B
188
189	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
190	xor	g, y2		# y2 = f^g                              # CH
191	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
192
193	and	e, y2		# y2 = (f^g)&e                          # CH
194	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
195	rorx	$39, a, y1	# y1 = a >> 39				# S0A
196	add	h, d		# d = k + w + h + d                     # --
197
198	and	b, y3		# y3 = (a|c)&b                          # MAJA
199	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
200	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
201
202	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
203	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
204	mov	a, T1		# T1 = a                                # MAJB
205	and	c, T1		# T1 = a&c                              # MAJB
206
207	add	y0, y2		# y2 = S1 + CH                          # --
208	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
209	add	y1, h		# h = k + w + h + S0                    # --
210
211	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
212
213	add	y2, h		# h = k + w + h + S0 + S1 + CH = t1 + S0# --
214	add	y3, h		# h = t1 + S0 + MAJ                     # --
215
216	RotateState
217
218################################### RND N + 1 #########################################
219
220	# Calculate w[t-15] ror 8
221	vpsrlq		$8, YTMP1, YTMP2
222	vpsllq		$(64-8), YTMP1, YTMP1
223	vpor		YTMP2, YTMP1, YTMP1		# YTMP1 = W[-15] ror 8
224	# XOR the three components
225	vpxor		YTMP4, YTMP3, YTMP3		# YTMP3 = W[-15] ror 1 ^ W[-15] >> 7
226	vpxor		YTMP1, YTMP3, YTMP1		# YTMP1 = s0
227
228
229	# Add three components, w[t-16], w[t-7] and sigma0
230	vpaddq		YTMP1, YTMP0, YTMP0		# YTMP0 = W[-16] + W[-7] + s0
231	# Move to appropriate lanes for calculating w[16] and w[17]
232	vperm2f128	$0x0, YTMP0, YTMP0, Y_0		# Y_0 = W[-16] + W[-7] + s0 {BABA}
233	# Move to appropriate lanes for calculating w[18] and w[19]
234	vpand		MASK_YMM_LO(%rip), YTMP0, YTMP0	# YTMP0 = W[-16] + W[-7] + s0 {DC00}
235
236	# Calculate w[16] and w[17] in both 128 bit lanes
237
238	# Calculate sigma1 for w[16] and w[17] on both 128 bit lanes
239	vperm2f128	$0x11, Y_3, Y_3, YTMP2		# YTMP2 = W[-2] {BABA}
240	vpsrlq		$6, YTMP2, YTMP4		# YTMP4 = W[-2] >> 6 {BABA}
241
242
243	mov	a, y3		# y3 = a                                # MAJA
244	rorx	$41, e, y0	# y0 = e >> 41				# S1A
245	rorx	$18, e, y1	# y1 = e >> 18				# S1B
246	add	1*8+frame_XFER(%rsp), h		# h = k + w + h         # --
247	or	c, y3		# y3 = a|c                              # MAJA
248
249
250	mov	f, y2		# y2 = f                                # CH
251	rorx	$34, a, T1	# T1 = a >> 34				# S0B
252	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
253	xor	g, y2		# y2 = f^g                              # CH
254
255
256	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
257	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
258	rorx	$39, a, y1	# y1 = a >> 39				# S0A
259	and	e, y2		# y2 = (f^g)&e                          # CH
260	add	h, d		# d = k + w + h + d                     # --
261
262	and	b, y3		# y3 = (a|c)&b                          # MAJA
263	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
264
265	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
266	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
267
268	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
269	mov	a, T1		# T1 = a                                # MAJB
270	and	c, T1		# T1 = a&c                              # MAJB
271	add	y0, y2		# y2 = S1 + CH                          # --
272
273	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
274	add	y1, h		# h = k + w + h + S0                    # --
275
276	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
277	add	y2, h		# h = k + w + h + S0 + S1 + CH = t1 + S0# --
278	add	y3, h		# h = t1 + S0 + MAJ                     # --
279
280	RotateState
281
282
283################################### RND N + 2 #########################################
284
285	vpsrlq		$19, YTMP2, YTMP3		# YTMP3 = W[-2] >> 19 {BABA}
286	vpsllq		$(64-19), YTMP2, YTMP1		# YTMP1 = W[-2] << 19 {BABA}
287	vpor		YTMP1, YTMP3, YTMP3		# YTMP3 = W[-2] ror 19 {BABA}
288	vpxor		YTMP3, YTMP4, YTMP4		# YTMP4 = W[-2] ror 19 ^ W[-2] >> 6 {BABA}
289	vpsrlq		$61, YTMP2, YTMP3		# YTMP3 = W[-2] >> 61 {BABA}
290	vpsllq		$(64-61), YTMP2, YTMP1		# YTMP1 = W[-2] << 61 {BABA}
291	vpor		YTMP1, YTMP3, YTMP3		# YTMP3 = W[-2] ror 61 {BABA}
292	vpxor		YTMP3, YTMP4, YTMP4		# YTMP4 = s1 = (W[-2] ror 19) ^
293							#  (W[-2] ror 61) ^ (W[-2] >> 6) {BABA}
294
295	# Add sigma1 to the other compunents to get w[16] and w[17]
296	vpaddq		YTMP4, Y_0, Y_0			# Y_0 = {W[1], W[0], W[1], W[0]}
297
298	# Calculate sigma1 for w[18] and w[19] for upper 128 bit lane
299	vpsrlq		$6, Y_0, YTMP4			# YTMP4 = W[-2] >> 6 {DC--}
300
301	mov	a, y3		# y3 = a                                # MAJA
302	rorx	$41, e, y0	# y0 = e >> 41				# S1A
303	add	2*8+frame_XFER(%rsp), h		# h = k + w + h         # --
304
305	rorx	$18, e, y1	# y1 = e >> 18				# S1B
306	or	c, y3		# y3 = a|c                              # MAJA
307	mov	f, y2		# y2 = f                                # CH
308	xor	g, y2		# y2 = f^g                              # CH
309
310	rorx	$34, a, T1	# T1 = a >> 34				# S0B
311	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
312	and	e, y2		# y2 = (f^g)&e                          # CH
313
314	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
315	add	h, d		# d = k + w + h + d                     # --
316	and	b, y3		# y3 = (a|c)&b                          # MAJA
317
318	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
319	rorx	$39, a, y1	# y1 = a >> 39				# S0A
320	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
321
322	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
323	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
324
325	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
326	mov	a, T1		# T1 = a                                # MAJB
327	and	c, T1		# T1 = a&c                              # MAJB
328	add	y0, y2		# y2 = S1 + CH                          # --
329
330	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
331	add	y1, h		# h = k + w + h + S0                    # --
332	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
333	add	y2, h		# h = k + w + h + S0 + S1 + CH = t1 + S0# --
334
335	add	y3, h		# h = t1 + S0 + MAJ                     # --
336
337	RotateState
338
339################################### RND N + 3 #########################################
340
341	vpsrlq		$19, Y_0, YTMP3			# YTMP3 = W[-2] >> 19 {DC--}
342	vpsllq		$(64-19), Y_0, YTMP1		# YTMP1 = W[-2] << 19 {DC--}
343	vpor		YTMP1, YTMP3, YTMP3		# YTMP3 = W[-2] ror 19 {DC--}
344	vpxor		YTMP3, YTMP4, YTMP4		# YTMP4 = W[-2] ror 19 ^ W[-2] >> 6 {DC--}
345	vpsrlq		$61, Y_0, YTMP3			# YTMP3 = W[-2] >> 61 {DC--}
346	vpsllq		$(64-61), Y_0, YTMP1		# YTMP1 = W[-2] << 61 {DC--}
347	vpor		YTMP1, YTMP3, YTMP3		# YTMP3 = W[-2] ror 61 {DC--}
348	vpxor		YTMP3, YTMP4, YTMP4		# YTMP4 = s1 = (W[-2] ror 19) ^
349							#  (W[-2] ror 61) ^ (W[-2] >> 6) {DC--}
350
351	# Add the sigma0 + w[t-7] + w[t-16] for w[18] and w[19]
352	# to newly calculated sigma1 to get w[18] and w[19]
353	vpaddq		YTMP4, YTMP0, YTMP2		# YTMP2 = {W[3], W[2], --, --}
354
355	# Form w[19, w[18], w17], w[16]
356	vpblendd		$0xF0, YTMP2, Y_0, Y_0		# Y_0 = {W[3], W[2], W[1], W[0]}
357
358	mov	a, y3		# y3 = a                                # MAJA
359	rorx	$41, e, y0	# y0 = e >> 41				# S1A
360	rorx	$18, e, y1	# y1 = e >> 18				# S1B
361	add	3*8+frame_XFER(%rsp), h		# h = k + w + h         # --
362	or	c, y3		# y3 = a|c                              # MAJA
363
364
365	mov	f, y2		# y2 = f                                # CH
366	rorx	$34, a, T1	# T1 = a >> 34				# S0B
367	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
368	xor	g, y2		# y2 = f^g                              # CH
369
370
371	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
372	and	e, y2		# y2 = (f^g)&e                          # CH
373	add	h, d		# d = k + w + h + d                     # --
374	and	b, y3		# y3 = (a|c)&b                          # MAJA
375
376	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
377	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
378
379	rorx	$39, a, y1	# y1 = a >> 39				# S0A
380	add	y0, y2		# y2 = S1 + CH                          # --
381
382	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
383	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
384
385	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
386
387	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
388	mov	a, T1		# T1 = a                                # MAJB
389	and	c, T1		# T1 = a&c                              # MAJB
390	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
391
392	add	y1, h		# h = k + w + h + S0                    # --
393	add	y2, h		# h = k + w + h + S0 + S1 + CH = t1 + S0# --
394	add	y3, h		# h = t1 + S0 + MAJ                     # --
395
396	RotateState
397
398	rotate_Ys
399.endm
400
401.macro DO_4ROUNDS
402
403################################### RND N + 0 #########################################
404
405	mov	f, y2		# y2 = f                                # CH
406	rorx	$41, e, y0	# y0 = e >> 41				# S1A
407	rorx	$18, e, y1	# y1 = e >> 18				# S1B
408	xor	g, y2		# y2 = f^g                              # CH
409
410	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
411	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
412	and	e, y2		# y2 = (f^g)&e                          # CH
413
414	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
415	rorx	$34, a, T1	# T1 = a >> 34				# S0B
416	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
417	rorx	$39, a, y1	# y1 = a >> 39				# S0A
418	mov	a, y3		# y3 = a                                # MAJA
419
420	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
421	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
422	add	frame_XFER(%rsp), h		# h = k + w + h         # --
423	or	c, y3		# y3 = a|c                              # MAJA
424
425	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
426	mov	a, T1		# T1 = a                                # MAJB
427	and	b, y3		# y3 = (a|c)&b                          # MAJA
428	and	c, T1		# T1 = a&c                              # MAJB
429	add	y0, y2		# y2 = S1 + CH                          # --
430
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
435	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
436
437	RotateState
438
439################################### RND N + 1 #########################################
440
441	add	y2, old_h	# h = k + w + h + S0 + S1 + CH = t1 + S0# --
442	mov	f, y2		# y2 = f                                # CH
443	rorx	$41, e, y0	# y0 = e >> 41				# S1A
444	rorx	$18, e, y1	# y1 = e >> 18				# S1B
445	xor	g, y2		# y2 = f^g                              # CH
446
447	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
448	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
449	and	e, y2		# y2 = (f^g)&e                          # CH
450	add	y3, old_h	# h = t1 + S0 + MAJ                     # --
451
452	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
453	rorx	$34, a, T1	# T1 = a >> 34				# S0B
454	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
455	rorx	$39, a, y1	# y1 = a >> 39				# S0A
456	mov	a, y3		# y3 = a                                # MAJA
457
458	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
459	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
460	add	8*1+frame_XFER(%rsp), h		# h = k + w + h         # --
461	or	c, y3		# y3 = a|c                              # MAJA
462
463	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
464	mov	a, T1		# T1 = a                                # MAJB
465	and	b, y3		# y3 = (a|c)&b                          # MAJA
466	and	c, T1		# T1 = a&c                              # MAJB
467	add	y0, y2		# y2 = S1 + CH                          # --
468
469	add	h, d		# d = k + w + h + d                     # --
470	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
471	add	y1, h		# h = k + w + h + S0                    # --
472
473	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
474
475	RotateState
476
477################################### RND N + 2 #########################################
478
479	add	y2, old_h	# h = k + w + h + S0 + S1 + CH = t1 + S0# --
480	mov	f, y2		# y2 = f                                # CH
481	rorx	$41, e, y0	# y0 = e >> 41				# S1A
482	rorx	$18, e, y1	# y1 = e >> 18				# S1B
483	xor	g, y2		# y2 = f^g                              # CH
484
485	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
486	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
487	and	e, y2		# y2 = (f^g)&e                          # CH
488	add	y3, old_h	# h = t1 + S0 + MAJ                     # --
489
490	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
491	rorx	$34, a, T1	# T1 = a >> 34				# S0B
492	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
493	rorx	$39, a, y1	# y1 = a >> 39				# S0A
494	mov	a, y3		# y3 = a                                # MAJA
495
496	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
497	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
498	add	8*2+frame_XFER(%rsp), h		# h = k + w + h         # --
499	or	c, y3		# y3 = a|c                              # MAJA
500
501	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
502	mov	a, T1		# T1 = a                                # MAJB
503	and	b, y3		# y3 = (a|c)&b                          # MAJA
504	and	c, T1		# T1 = a&c                              # MAJB
505	add	y0, y2		# y2 = S1 + CH                          # --
506
507	add	h, d		# d = k + w + h + d                     # --
508	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
509	add	y1, h		# h = k + w + h + S0                    # --
510
511	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
512
513	RotateState
514
515################################### RND N + 3 #########################################
516
517	add	y2, old_h	# h = k + w + h + S0 + S1 + CH = t1 + S0# --
518	mov	f, y2		# y2 = f                                # CH
519	rorx	$41, e, y0	# y0 = e >> 41				# S1A
520	rorx	$18, e, y1	# y1 = e >> 18				# S1B
521	xor	g, y2		# y2 = f^g                              # CH
522
523	xor	y1, y0		# y0 = (e>>41) ^ (e>>18)		# S1
524	rorx	$14, e, y1	# y1 = (e >> 14)			# S1
525	and	e, y2		# y2 = (f^g)&e                          # CH
526	add	y3, old_h	# h = t1 + S0 + MAJ                     # --
527
528	xor	y1, y0		# y0 = (e>>41) ^ (e>>18) ^ (e>>14)	# S1
529	rorx	$34, a, T1	# T1 = a >> 34				# S0B
530	xor	g, y2		# y2 = CH = ((f^g)&e)^g                 # CH
531	rorx	$39, a, y1	# y1 = a >> 39				# S0A
532	mov	a, y3		# y3 = a                                # MAJA
533
534	xor	T1, y1		# y1 = (a>>39) ^ (a>>34)		# S0
535	rorx	$28, a, T1	# T1 = (a >> 28)			# S0
536	add	8*3+frame_XFER(%rsp), h		# h = k + w + h         # --
537	or	c, y3		# y3 = a|c                              # MAJA
538
539	xor	T1, y1		# y1 = (a>>39) ^ (a>>34) ^ (a>>28)	# S0
540	mov	a, T1		# T1 = a                                # MAJB
541	and	b, y3		# y3 = (a|c)&b                          # MAJA
542	and	c, T1		# T1 = a&c                              # MAJB
543	add	y0, y2		# y2 = S1 + CH                          # --
544
545
546	add	h, d		# d = k + w + h + d                     # --
547	or	T1, y3		# y3 = MAJ = (a|c)&b)|(a&c)             # MAJ
548	add	y1, h		# h = k + w + h + S0                    # --
549
550	add	y2, d		# d = k + w + h + d + S1 + CH = d + t1  # --
551
552	add	y2, h		# h = k + w + h + S0 + S1 + CH = t1 + S0# --
553
554	add	y3, h		# h = t1 + S0 + MAJ                     # --
555
556	RotateState
557
558.endm
559
560########################################################################
561# void sha512_transform_rorx(sha512_state *state, const u8 *data, int blocks)
562# Purpose: Updates the SHA512 digest stored at "state" with the message
563# stored in "data".
564# The size of the message pointed to by "data" must be an integer multiple
565# of SHA512 message blocks.
566# "blocks" is the message length in SHA512 blocks
567########################################################################
568SYM_FUNC_START(sha512_transform_rorx)
569	# Save GPRs
570	push	%rbx
571	push	%r12
572	push	%r13
573	push	%r14
574	push	%r15
575
576	# Allocate Stack Space
577	push	%rbp
578	mov	%rsp, %rbp
579	sub	$frame_size, %rsp
580	and	$~(0x20 - 1), %rsp
581
582	shl	$7, NUM_BLKS	# convert to bytes
583	jz	done_hash
584	add	INP, NUM_BLKS	# pointer to end of data
585	mov	NUM_BLKS, frame_INPEND(%rsp)
586
587	## load initial digest
588	mov	8*0(CTX1), a
589	mov	8*1(CTX1), b
590	mov	8*2(CTX1), c
591	mov	8*3(CTX1), d
592	mov	8*4(CTX1), e
593	mov	8*5(CTX1), f
594	mov	8*6(CTX1), g
595	mov	8*7(CTX1), h
596
597	# save %rdi (CTX) before it gets clobbered
598	mov	%rdi, frame_CTX(%rsp)
599
600	vmovdqa	PSHUFFLE_BYTE_FLIP_MASK(%rip), BYTE_FLIP_MASK
601
602loop0:
603	lea	K512(%rip), TBL
604
605	## byte swap first 16 dwords
606	COPY_YMM_AND_BSWAP	Y_0, (INP), BYTE_FLIP_MASK
607	COPY_YMM_AND_BSWAP	Y_1, 1*32(INP), BYTE_FLIP_MASK
608	COPY_YMM_AND_BSWAP	Y_2, 2*32(INP), BYTE_FLIP_MASK
609	COPY_YMM_AND_BSWAP	Y_3, 3*32(INP), BYTE_FLIP_MASK
610
611	mov	INP, frame_INP(%rsp)
612
613	## schedule 64 input dwords, by doing 12 rounds of 4 each
614	movq	$4, frame_SRND(%rsp)
615
616.align 16
617loop1:
618	vpaddq	(TBL), Y_0, XFER
619	vmovdqa XFER, frame_XFER(%rsp)
620	FOUR_ROUNDS_AND_SCHED
621
622	vpaddq	1*32(TBL), Y_0, XFER
623	vmovdqa XFER, frame_XFER(%rsp)
624	FOUR_ROUNDS_AND_SCHED
625
626	vpaddq	2*32(TBL), Y_0, XFER
627	vmovdqa XFER, frame_XFER(%rsp)
628	FOUR_ROUNDS_AND_SCHED
629
630	vpaddq	3*32(TBL), Y_0, XFER
631	vmovdqa XFER, frame_XFER(%rsp)
632	add	$(4*32), TBL
633	FOUR_ROUNDS_AND_SCHED
634
635	subq	$1, frame_SRND(%rsp)
636	jne	loop1
637
638	movq	$2, frame_SRND(%rsp)
639loop2:
640	vpaddq	(TBL), Y_0, XFER
641	vmovdqa XFER, frame_XFER(%rsp)
642	DO_4ROUNDS
643	vpaddq	1*32(TBL), Y_1, XFER
644	vmovdqa XFER, frame_XFER(%rsp)
645	add	$(2*32), TBL
646	DO_4ROUNDS
647
648	vmovdqa	Y_2, Y_0
649	vmovdqa	Y_3, Y_1
650
651	subq	$1, frame_SRND(%rsp)
652	jne	loop2
653
654	mov	frame_CTX(%rsp), CTX2
655	addm	8*0(CTX2), a
656	addm	8*1(CTX2), b
657	addm	8*2(CTX2), c
658	addm	8*3(CTX2), d
659	addm	8*4(CTX2), e
660	addm	8*5(CTX2), f
661	addm	8*6(CTX2), g
662	addm	8*7(CTX2), h
663
664	mov	frame_INP(%rsp), INP
665	add	$128, INP
666	cmp	frame_INPEND(%rsp), INP
667	jne	loop0
668
669done_hash:
670
671	# Restore Stack Pointer
672	mov	%rbp, %rsp
673	pop	%rbp
674
675	# Restore GPRs
676	pop	%r15
677	pop	%r14
678	pop	%r13
679	pop	%r12
680	pop	%rbx
681
682	ret
683SYM_FUNC_END(sha512_transform_rorx)
684
685########################################################################
686### Binary Data
687
688
689# Mergeable 640-byte rodata section. This allows linker to merge the table
690# with other, exactly the same 640-byte fragment of another rodata section
691# (if such section exists).
692.section	.rodata.cst640.K512, "aM", @progbits, 640
693.align 64
694# K[t] used in SHA512 hashing
695K512:
696	.quad	0x428a2f98d728ae22,0x7137449123ef65cd
697	.quad	0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc
698	.quad	0x3956c25bf348b538,0x59f111f1b605d019
699	.quad	0x923f82a4af194f9b,0xab1c5ed5da6d8118
700	.quad	0xd807aa98a3030242,0x12835b0145706fbe
701	.quad	0x243185be4ee4b28c,0x550c7dc3d5ffb4e2
702	.quad	0x72be5d74f27b896f,0x80deb1fe3b1696b1
703	.quad	0x9bdc06a725c71235,0xc19bf174cf692694
704	.quad	0xe49b69c19ef14ad2,0xefbe4786384f25e3
705	.quad	0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65
706	.quad	0x2de92c6f592b0275,0x4a7484aa6ea6e483
707	.quad	0x5cb0a9dcbd41fbd4,0x76f988da831153b5
708	.quad	0x983e5152ee66dfab,0xa831c66d2db43210
709	.quad	0xb00327c898fb213f,0xbf597fc7beef0ee4
710	.quad	0xc6e00bf33da88fc2,0xd5a79147930aa725
711	.quad	0x06ca6351e003826f,0x142929670a0e6e70
712	.quad	0x27b70a8546d22ffc,0x2e1b21385c26c926
713	.quad	0x4d2c6dfc5ac42aed,0x53380d139d95b3df
714	.quad	0x650a73548baf63de,0x766a0abb3c77b2a8
715	.quad	0x81c2c92e47edaee6,0x92722c851482353b
716	.quad	0xa2bfe8a14cf10364,0xa81a664bbc423001
717	.quad	0xc24b8b70d0f89791,0xc76c51a30654be30
718	.quad	0xd192e819d6ef5218,0xd69906245565a910
719	.quad	0xf40e35855771202a,0x106aa07032bbd1b8
720	.quad	0x19a4c116b8d2d0c8,0x1e376c085141ab53
721	.quad	0x2748774cdf8eeb99,0x34b0bcb5e19b48a8
722	.quad	0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb
723	.quad	0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3
724	.quad	0x748f82ee5defb2fc,0x78a5636f43172f60
725	.quad	0x84c87814a1f0ab72,0x8cc702081a6439ec
726	.quad	0x90befffa23631e28,0xa4506cebde82bde9
727	.quad	0xbef9a3f7b2c67915,0xc67178f2e372532b
728	.quad	0xca273eceea26619c,0xd186b8c721c0c207
729	.quad	0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178
730	.quad	0x06f067aa72176fba,0x0a637dc5a2c898a6
731	.quad	0x113f9804bef90dae,0x1b710b35131c471b
732	.quad	0x28db77f523047d84,0x32caab7b40c72493
733	.quad	0x3c9ebe0a15c9bebc,0x431d67c49c100d4c
734	.quad	0x4cc5d4becb3e42b6,0x597f299cfc657e2a
735	.quad	0x5fcb6fab3ad6faec,0x6c44198c4a475817
736
737.section	.rodata.cst32.PSHUFFLE_BYTE_FLIP_MASK, "aM", @progbits, 32
738.align 32
739# Mask for byte-swapping a couple of qwords in an XMM register using (v)pshufb.
740PSHUFFLE_BYTE_FLIP_MASK:
741	.octa 0x08090a0b0c0d0e0f0001020304050607
742	.octa 0x18191a1b1c1d1e1f1011121314151617
743
744.section	.rodata.cst32.MASK_YMM_LO, "aM", @progbits, 32
745.align 32
746MASK_YMM_LO:
747	.octa 0x00000000000000000000000000000000
748	.octa 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
749