blob: 2f03e1ce4f99951ce58e9ff9996245b78665b019 [file] [log] [blame]
Denys Vlasenko711e20e2022-01-07 00:43:59 +01001#if ENABLE_SHA1_HWACCEL && defined(__GNUC__) && defined(__x86_64__)
2/* The code is adapted from Linux kernel's source */
3
4// We use shorter insns, even though they are for "wrong"
5// data type (fp, not int).
6// For Intel, there is no penalty for doing it at all
Denys Vlasenko6f56fa12022-02-10 15:38:10 +01007// (CPUs which do have such penalty do not support SHA insns).
Denys Vlasenko711e20e2022-01-07 00:43:59 +01008// For AMD, the penalty is one extra cycle
9// (allegedly: I failed to find measurable difference).
10
11//#define mova128 movdqa
12#define mova128 movaps
13//#define movu128 movdqu
14#define movu128 movups
15//#define xor128 pxor
16#define xor128 xorps
17//#define shuf128_32 pshufd
18#define shuf128_32 shufps
19
20#define extr128_32 pextrd
Denys Vlasenko805ecec2022-01-08 00:41:09 +010021//#define extr128_32 extractps # not shorter
Denys Vlasenko711e20e2022-01-07 00:43:59 +010022
Denys Vlasenkodda77e82022-02-11 14:53:26 +010023// pshufb is a SSSE3 insn.
24// pinsrd, pextrd, extractps are SSE4.1 insns.
25// We do not check SSSE3/SSE4.1 in cpuid,
26// all SHA-capable CPUs support them as well.
27
Ludwig Nussel526625b2022-08-25 12:48:07 +020028#ifdef __linux__
29 .section .note.GNU-stack, "", @progbits
30#endif
Denys Vlasenko6472ac92022-02-03 14:15:20 +010031 .section .text.sha1_process_block64_shaNI, "ax", @progbits
Denys Vlasenko805ecec2022-01-08 00:41:09 +010032 .globl sha1_process_block64_shaNI
33 .hidden sha1_process_block64_shaNI
Denys Vlasenko711e20e2022-01-07 00:43:59 +010034 .type sha1_process_block64_shaNI, @function
35
36#define ABCD %xmm0
37#define E0 %xmm1 /* Need two E's b/c they ping pong */
38#define E1 %xmm2
39#define MSG0 %xmm3
40#define MSG1 %xmm4
41#define MSG2 %xmm5
42#define MSG3 %xmm6
Denys Vlasenko711e20e2022-01-07 00:43:59 +010043
44 .balign 8 # allow decoders to fetch at least 2 first insns
45sha1_process_block64_shaNI:
46 /* load initial hash values */
Denys Vlasenko711e20e2022-01-07 00:43:59 +010047 movu128 80(%rdi), ABCD
Denys Vlasenkoeb8d5f32022-02-08 15:34:02 +010048 xor128 E0, E0
Denys Vlasenko0bab5da2022-01-07 14:55:31 +010049 pinsrd $3, 80+4*4(%rdi), E0 # load to uppermost 32-bit word
50 shuf128_32 $0x1B, ABCD, ABCD # DCBA -> ABCD
Denys Vlasenko711e20e2022-01-07 00:43:59 +010051
Denys Vlasenko71a1ccc2022-02-08 08:20:27 +010052 mova128 PSHUFFLE_BYTE_FLIP_MASK(%rip), %xmm7
53
54 movu128 0*16(%rdi), MSG0
55 pshufb %xmm7, MSG0
56 movu128 1*16(%rdi), MSG1
57 pshufb %xmm7, MSG1
58 movu128 2*16(%rdi), MSG2
59 pshufb %xmm7, MSG2
60 movu128 3*16(%rdi), MSG3
61 pshufb %xmm7, MSG3
Denys Vlasenko711e20e2022-01-07 00:43:59 +010062
63 /* Save hash values for addition after rounds */
Denys Vlasenko71a1ccc2022-02-08 08:20:27 +010064 mova128 E0, %xmm7
Denys Vlasenko711e20e2022-01-07 00:43:59 +010065 mova128 ABCD, %xmm8
66
67 /* Rounds 0-3 */
Denys Vlasenko711e20e2022-01-07 00:43:59 +010068 paddd MSG0, E0
69 mova128 ABCD, E1
70 sha1rnds4 $0, E0, ABCD
71
72 /* Rounds 4-7 */
Denys Vlasenko711e20e2022-01-07 00:43:59 +010073 sha1nexte MSG1, E1
74 mova128 ABCD, E0
75 sha1rnds4 $0, E1, ABCD
76 sha1msg1 MSG1, MSG0
77
78 /* Rounds 8-11 */
Denys Vlasenko711e20e2022-01-07 00:43:59 +010079 sha1nexte MSG2, E0
80 mova128 ABCD, E1
81 sha1rnds4 $0, E0, ABCD
82 sha1msg1 MSG2, MSG1
83 xor128 MSG2, MSG0
84
85 /* Rounds 12-15 */
Denys Vlasenko711e20e2022-01-07 00:43:59 +010086 sha1nexte MSG3, E1
87 mova128 ABCD, E0
88 sha1msg2 MSG3, MSG0
89 sha1rnds4 $0, E1, ABCD
90 sha1msg1 MSG3, MSG2
91 xor128 MSG3, MSG1
92
93 /* Rounds 16-19 */
94 sha1nexte MSG0, E0
95 mova128 ABCD, E1
96 sha1msg2 MSG0, MSG1
97 sha1rnds4 $0, E0, ABCD
98 sha1msg1 MSG0, MSG3
99 xor128 MSG0, MSG2
100
101 /* Rounds 20-23 */
102 sha1nexte MSG1, E1
103 mova128 ABCD, E0
104 sha1msg2 MSG1, MSG2
105 sha1rnds4 $1, E1, ABCD
106 sha1msg1 MSG1, MSG0
107 xor128 MSG1, MSG3
108
109 /* Rounds 24-27 */
110 sha1nexte MSG2, E0
111 mova128 ABCD, E1
112 sha1msg2 MSG2, MSG3
113 sha1rnds4 $1, E0, ABCD
114 sha1msg1 MSG2, MSG1
115 xor128 MSG2, MSG0
116
117 /* Rounds 28-31 */
118 sha1nexte MSG3, E1
119 mova128 ABCD, E0
120 sha1msg2 MSG3, MSG0
121 sha1rnds4 $1, E1, ABCD
122 sha1msg1 MSG3, MSG2
123 xor128 MSG3, MSG1
124
125 /* Rounds 32-35 */
126 sha1nexte MSG0, E0
127 mova128 ABCD, E1
128 sha1msg2 MSG0, MSG1
129 sha1rnds4 $1, E0, ABCD
130 sha1msg1 MSG0, MSG3
131 xor128 MSG0, MSG2
132
133 /* Rounds 36-39 */
134 sha1nexte MSG1, E1
135 mova128 ABCD, E0
136 sha1msg2 MSG1, MSG2
137 sha1rnds4 $1, E1, ABCD
138 sha1msg1 MSG1, MSG0
139 xor128 MSG1, MSG3
140
141 /* Rounds 40-43 */
142 sha1nexte MSG2, E0
143 mova128 ABCD, E1
144 sha1msg2 MSG2, MSG3
145 sha1rnds4 $2, E0, ABCD
146 sha1msg1 MSG2, MSG1
147 xor128 MSG2, MSG0
148
149 /* Rounds 44-47 */
150 sha1nexte MSG3, E1
151 mova128 ABCD, E0
152 sha1msg2 MSG3, MSG0
153 sha1rnds4 $2, E1, ABCD
154 sha1msg1 MSG3, MSG2
155 xor128 MSG3, MSG1
156
157 /* Rounds 48-51 */
158 sha1nexte MSG0, E0
159 mova128 ABCD, E1
160 sha1msg2 MSG0, MSG1
161 sha1rnds4 $2, E0, ABCD
162 sha1msg1 MSG0, MSG3
163 xor128 MSG0, MSG2
164
165 /* Rounds 52-55 */
166 sha1nexte MSG1, E1
167 mova128 ABCD, E0
168 sha1msg2 MSG1, MSG2
169 sha1rnds4 $2, E1, ABCD
170 sha1msg1 MSG1, MSG0
171 xor128 MSG1, MSG3
172
173 /* Rounds 56-59 */
174 sha1nexte MSG2, E0
175 mova128 ABCD, E1
176 sha1msg2 MSG2, MSG3
177 sha1rnds4 $2, E0, ABCD
178 sha1msg1 MSG2, MSG1
179 xor128 MSG2, MSG0
180
181 /* Rounds 60-63 */
182 sha1nexte MSG3, E1
183 mova128 ABCD, E0
184 sha1msg2 MSG3, MSG0
185 sha1rnds4 $3, E1, ABCD
186 sha1msg1 MSG3, MSG2
187 xor128 MSG3, MSG1
188
189 /* Rounds 64-67 */
190 sha1nexte MSG0, E0
191 mova128 ABCD, E1
192 sha1msg2 MSG0, MSG1
193 sha1rnds4 $3, E0, ABCD
194 sha1msg1 MSG0, MSG3
195 xor128 MSG0, MSG2
196
197 /* Rounds 68-71 */
198 sha1nexte MSG1, E1
199 mova128 ABCD, E0
200 sha1msg2 MSG1, MSG2
201 sha1rnds4 $3, E1, ABCD
202 xor128 MSG1, MSG3
203
204 /* Rounds 72-75 */
205 sha1nexte MSG2, E0
206 mova128 ABCD, E1
207 sha1msg2 MSG2, MSG3
208 sha1rnds4 $3, E0, ABCD
209
210 /* Rounds 76-79 */
211 sha1nexte MSG3, E1
212 mova128 ABCD, E0
213 sha1rnds4 $3, E1, ABCD
214
215 /* Add current hash values with previously saved */
Denys Vlasenko71a1ccc2022-02-08 08:20:27 +0100216 sha1nexte %xmm7, E0
Denys Vlasenko711e20e2022-01-07 00:43:59 +0100217 paddd %xmm8, ABCD
218
219 /* Write hash values back in the correct order */
220 shuf128_32 $0x1B, ABCD, ABCD
221 movu128 ABCD, 80(%rdi)
222 extr128_32 $3, E0, 80+4*4(%rdi)
223
224 ret
Denys Vlasenko805ecec2022-01-08 00:41:09 +0100225 .size sha1_process_block64_shaNI, .-sha1_process_block64_shaNI
Denys Vlasenko711e20e2022-01-07 00:43:59 +0100226
Denys Vlasenkoc193cbd2022-02-07 02:06:18 +0100227 .section .rodata.cst16.PSHUFFLE_BYTE_FLIP_MASK, "aM", @progbits, 16
Denys Vlasenko6a6c1c02022-02-09 11:29:23 +0100228 .balign 16
Denys Vlasenko711e20e2022-01-07 00:43:59 +0100229PSHUFFLE_BYTE_FLIP_MASK:
Denys Vlasenko6a6c1c02022-02-09 11:29:23 +0100230 .octa 0x000102030405060708090a0b0c0d0e0f
Denys Vlasenko711e20e2022-01-07 00:43:59 +0100231
232#endif