Debian Codegraph @

cipher/cipher-gcm-intel-pclmul.c

raw
1 /* cipher-gcm-intel-pclmul.c - Intel PCLMUL accelerated Galois Counter Mode
2 * implementation
3 * Copyright (C) 2013-2014,2019,2022 Jussi Kivilinna <[email protected]>
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "g10lib.h"
28 #include "cipher.h"
29 #include "bufhelp.h"
30 #include "./cipher-internal.h"
31
32
33 #ifdef GCM_USE_INTEL_PCLMUL
34
35
36 #if _GCRY_GCC_VERSION >= 40400 /* 4.4 */
37 /* Prevent compiler from issuing SSE instructions between asm blocks. */
38 # pragma GCC target("no-sse")
39 #endif
40 #if __clang__
41 # pragma clang attribute push (__attribute__((target("no-sse"))), apply_to = function)
42 #endif
43
44
45 #define ALWAYS_INLINE inline __attribute__((always_inline))
46 #define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))
47
48 #define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION
49 #define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE
50
51
52 #define GCM_INTEL_USE_VPCLMUL_AVX2 (1 << 0)
53 #define GCM_INTEL_AGGR8_TABLE_INITIALIZED (1 << 1)
54 #define GCM_INTEL_AGGR16_TABLE_INITIALIZED (1 << 2)
55 #define GCM_INTEL_USE_VPCLMUL_AVX512 (1 << 3)
56 #define GCM_INTEL_AGGR32_TABLE_INITIALIZED (1 << 4)
57
58
59 /*
60 Intel PCLMUL ghash based on white paper:
61 "IntelĀ® Carry-Less Multiplication Instruction and its Usage for Computing the
62 GCM Mode - Rev 2.01"; Shay Gueron, Michael E. Kounavis.
63 */
64 static ASM_FUNC_ATTR_INLINE
65 void reduction(void)
66 {
67 /* input: <xmm1:xmm3> */
68
69 asm volatile (/* first phase of the reduction */
70 "movdqa %%xmm3, %%xmm6\n\t"
71 "movdqa %%xmm3, %%xmm5\n\t"
72 "psllq $1, %%xmm6\n\t" /* packed right shifting << 63 */
73 "pxor %%xmm3, %%xmm6\n\t"
74 "psllq $57, %%xmm5\n\t" /* packed right shifting << 57 */
75 "psllq $62, %%xmm6\n\t" /* packed right shifting << 62 */
76 "pxor %%xmm5, %%xmm6\n\t" /* xor the shifted versions */
77 "pshufd $0x6a, %%xmm6, %%xmm5\n\t"
78 "pshufd $0xae, %%xmm6, %%xmm6\n\t"
79 "pxor %%xmm5, %%xmm3\n\t" /* first phase of the reduction
80 complete */
81
82 /* second phase of the reduction */
83 "pxor %%xmm3, %%xmm1\n\t" /* xor the shifted versions */
84 "psrlq $1, %%xmm3\n\t" /* packed left shifting >> 1 */
85 "pxor %%xmm3, %%xmm6\n\t"
86 "psrlq $1, %%xmm3\n\t" /* packed left shifting >> 2 */
87 "pxor %%xmm3, %%xmm1\n\t"
88 "psrlq $5, %%xmm3\n\t" /* packed left shifting >> 7 */
89 "pxor %%xmm3, %%xmm6\n\t"
90 "pxor %%xmm6, %%xmm1\n\t" /* the result is in xmm1 */
91 ::: "memory" );
92 }
93
94 static ASM_FUNC_ATTR_INLINE
95 void gfmul_pclmul(void)
96 {
97 /* Input: XMM0 and XMM1, Output: XMM1. Input XMM0 stays unmodified.
98 Input must be converted to little-endian.
99 */
100 asm volatile (/* gfmul, xmm0 has operator a and xmm1 has operator b. */
101 "pshufd $78, %%xmm0, %%xmm2\n\t"
102 "pshufd $78, %%xmm1, %%xmm4\n\t"
103 "pxor %%xmm0, %%xmm2\n\t" /* xmm2 holds a0+a1 */
104 "pxor %%xmm1, %%xmm4\n\t" /* xmm4 holds b0+b1 */
105
106 "movdqa %%xmm0, %%xmm3\n\t"
107 "pclmulqdq $0, %%xmm1, %%xmm3\n\t" /* xmm3 holds a0*b0 */
108 "pclmulqdq $17, %%xmm0, %%xmm1\n\t" /* xmm6 holds a1*b1 */
109 "movdqa %%xmm3, %%xmm5\n\t"
110 "pclmulqdq $0, %%xmm2, %%xmm4\n\t" /* xmm4 holds (a0+a1)*(b0+b1) */
111
112 "pxor %%xmm1, %%xmm5\n\t" /* xmm5 holds a0*b0+a1*b1 */
113 "pxor %%xmm5, %%xmm4\n\t" /* xmm4 holds a0*b0+a1*b1+(a0+a1)*(b0+b1) */
114 "movdqa %%xmm4, %%xmm5\n\t"
115 "psrldq $8, %%xmm4\n\t"
116 "pslldq $8, %%xmm5\n\t"
117 "pxor %%xmm5, %%xmm3\n\t"
118 "pxor %%xmm4, %%xmm1\n\t" /* <xmm1:xmm3> holds the result of the
119 carry-less multiplication of xmm0
120 by xmm1 */
121 ::: "memory" );
122
123 reduction();
124 }
125
126 #define GFMUL_AGGR4_ASM_1(be_to_le) \
127 /* perform clmul and merge results... */ \
128 "movdqu 2*16(%[h_table]), %%xmm2\n\t" /* Load H4 */ \
129 "movdqu 0*16(%[buf]), %%xmm5\n\t" \
130 be_to_le("pshufb %[be_mask], %%xmm5\n\t") /* be => le */ \
131 "pxor %%xmm5, %%xmm1\n\t" \
132 \
133 "pshufd $78, %%xmm2, %%xmm5\n\t" \
134 "pshufd $78, %%xmm1, %%xmm4\n\t" \
135 "pxor %%xmm2, %%xmm5\n\t" /* xmm5 holds 4:a0+a1 */ \
136 "pxor %%xmm1, %%xmm4\n\t" /* xmm4 holds 4:b0+b1 */ \
137 "movdqa %%xmm2, %%xmm3\n\t" \
138 "pclmulqdq $0, %%xmm1, %%xmm3\n\t" /* xmm3 holds 4:a0*b0 */ \
139 "pclmulqdq $17, %%xmm2, %%xmm1\n\t" /* xmm1 holds 4:a1*b1 */ \
140 "pclmulqdq $0, %%xmm5, %%xmm4\n\t" /* xmm4 holds 4:(a0+a1)*(b0+b1) */ \
141 \
142 "movdqu 1*16(%[h_table]), %%xmm5\n\t" /* Load H3 */ \
143 "movdqu 1*16(%[buf]), %%xmm2\n\t" \
144 be_to_le("pshufb %[be_mask], %%xmm2\n\t") /* be => le */ \
145 \
146 "pshufd $78, %%xmm5, %%xmm0\n\t" \
147 "pshufd $78, %%xmm2, %%xmm7\n\t" \
148 "pxor %%xmm5, %%xmm0\n\t" /* xmm0 holds 3:a0+a1 */ \
149 "pxor %%xmm2, %%xmm7\n\t" /* xmm7 holds 3:b0+b1 */ \
150 "movdqa %%xmm5, %%xmm6\n\t" \
151 "pclmulqdq $0, %%xmm2, %%xmm6\n\t" /* xmm6 holds 3:a0*b0 */ \
152 "pclmulqdq $17, %%xmm5, %%xmm2\n\t" /* xmm2 holds 3:a1*b1 */ \
153 "pclmulqdq $0, %%xmm0, %%xmm7\n\t" /* xmm7 holds 3:(a0+a1)*(b0+b1) */ \
154 \
155 "movdqu 2*16(%[buf]), %%xmm5\n\t" \
156 be_to_le("pshufb %[be_mask], %%xmm5\n\t") /* be => le */ \
157 \
158 "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 3+4:a0*b0 */ \
159 "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 3+4:a1*b1 */ \
160 "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 3+4:(a0+a1)*(b0+b1) */ \
161 \
162 "movdqu 0*16(%[h_table]), %%xmm2\n\t" /* Load H2 */ \
163 \
164 "pshufd $78, %%xmm2, %%xmm0\n\t" \
165 "pshufd $78, %%xmm5, %%xmm7\n\t" \
166 "pxor %%xmm2, %%xmm0\n\t" /* xmm0 holds 2:a0+a1 */ \
167 "pxor %%xmm5, %%xmm7\n\t" /* xmm7 holds 2:b0+b1 */ \
168 "movdqa %%xmm2, %%xmm6\n\t" \
169 "pclmulqdq $0, %%xmm5, %%xmm6\n\t" /* xmm6 holds 2:a0*b0 */ \
170 "pclmulqdq $17, %%xmm2, %%xmm5\n\t" /* xmm5 holds 2:a1*b1 */ \
171 "pclmulqdq $0, %%xmm0, %%xmm7\n\t" /* xmm7 holds 2:(a0+a1)*(b0+b1) */ \
172 \
173 "movdqu 3*16(%[buf]), %%xmm2\n\t" \
174 be_to_le("pshufb %[be_mask], %%xmm2\n\t") /* be => le */ \
175 \
176 "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 2+3+4:a0*b0 */ \
177 "pxor %%xmm5, %%xmm1\n\t" /* xmm1 holds 2+3+4:a1*b1 */ \
178 "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 2+3+4:(a0+a1)*(b0+b1) */
179
180 #define GFMUL_AGGR4_ASM_2() \
181 "movdqu %[h_1], %%xmm5\n\t" /* Load H1 */ \
182 \
183 "pshufd $78, %%xmm5, %%xmm0\n\t" \
184 "pshufd $78, %%xmm2, %%xmm7\n\t" \
185 "pxor %%xmm5, %%xmm0\n\t" /* xmm0 holds 1:a0+a1 */ \
186 "pxor %%xmm2, %%xmm7\n\t" /* xmm7 holds 1:b0+b1 */ \
187 "movdqa %%xmm5, %%xmm6\n\t" \
188 "pclmulqdq $0, %%xmm2, %%xmm6\n\t" /* xmm6 holds 1:a0*b0 */ \
189 "pclmulqdq $17, %%xmm5, %%xmm2\n\t" /* xmm2 holds 1:a1*b1 */ \
190 "pclmulqdq $0, %%xmm0, %%xmm7\n\t" /* xmm7 holds 1:(a0+a1)*(b0+b1) */ \
191 \
192 "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 1+2+3+4:a0*b0 */ \
193 "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 1+2+3+4:a1*b1 */ \
194 "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 1+2+3+4:(a0+a1)*(b0+b1) */ \
195 \
196 /* aggregated reduction... */ \
197 "movdqa %%xmm3, %%xmm5\n\t" \
198 "pxor %%xmm1, %%xmm5\n\t" /* xmm5 holds a0*b0+a1*b1 */ \
199 "pxor %%xmm5, %%xmm4\n\t" /* xmm4 holds a0*b0+a1*b1+(a0+a1)*(b0+b1) */ \
200 "movdqa %%xmm4, %%xmm5\n\t" \
201 "psrldq $8, %%xmm4\n\t" \
202 "pslldq $8, %%xmm5\n\t" \
203 "pxor %%xmm5, %%xmm3\n\t" \
204 "pxor %%xmm4, %%xmm1\n\t" /* <xmm1:xmm3> holds the result of the \
205 carry-less multiplication of xmm0 \
206 by xmm1 */
207
208 #define be_to_le(...) __VA_ARGS__
209 #define le_to_le(...) /*_*/
210
211 static ASM_FUNC_ATTR_INLINE void
212 gfmul_pclmul_aggr4(const void *buf, const void *h_1, const void *h_table,
213 const unsigned char *be_mask)
214 {
215 /* Input:
216 Hash: XMM1
217 Output:
218 Hash: XMM1
219 */
220 asm volatile (GFMUL_AGGR4_ASM_1(be_to_le)
221 :
222 : [buf] "r" (buf),
223 [h_table] "r" (h_table),
224 [be_mask] "m" (*be_mask)
225 : "memory" );
226
227 asm volatile (GFMUL_AGGR4_ASM_2()
228 :
229 : [h_1] "m" (*(const unsigned char *)h_1)
230 : "memory" );
231
232 reduction();
233 }
234
235 static ASM_FUNC_ATTR_INLINE void
236 gfmul_pclmul_aggr4_le(const void *buf, const void *h_1, const void *h_table)
237 {
238 /* Input:
239 Hash: XMM1
240 Output:
241 Hash: XMM1
242 */
243 asm volatile (GFMUL_AGGR4_ASM_1(le_to_le)
244 :
245 : [buf] "r" (buf),
246 [h_table] "r" (h_table)
247 : "memory" );
248
249 asm volatile (GFMUL_AGGR4_ASM_2()
250 :
251 : [h_1] "m" (*(const unsigned char *)h_1)
252 : "memory" );
253
254 reduction();
255 }
256
257 #ifdef __x86_64__
258
259 #define GFMUL_AGGR8_ASM(be_to_le) \
260 /* Load H6, H7, H8. */ \
261 "movdqu 6*16(%[h_table]), %%xmm10\n\t" \
262 "movdqu 5*16(%[h_table]), %%xmm9\n\t" \
263 "movdqu 4*16(%[h_table]), %%xmm8\n\t" \
264 \
265 /* perform clmul and merge results... */ \
266 "movdqu 0*16(%[buf]), %%xmm5\n\t" \
267 "movdqu 1*16(%[buf]), %%xmm2\n\t" \
268 be_to_le("pshufb %%xmm15, %%xmm5\n\t") /* be => le */ \
269 be_to_le("pshufb %%xmm15, %%xmm2\n\t") /* be => le */ \
270 "pxor %%xmm5, %%xmm1\n\t" \
271 \
272 "pshufd $78, %%xmm10, %%xmm5\n\t" \
273 "pshufd $78, %%xmm1, %%xmm4\n\t" \
274 "pxor %%xmm10, %%xmm5\n\t" /* xmm5 holds 8:a0+a1 */ \
275 "pxor %%xmm1, %%xmm4\n\t" /* xmm4 holds 8:b0+b1 */ \
276 "movdqa %%xmm10, %%xmm3\n\t" \
277 "pclmulqdq $0, %%xmm1, %%xmm3\n\t" /* xmm3 holds 8:a0*b0 */ \
278 "pclmulqdq $17, %%xmm10, %%xmm1\n\t" /* xmm1 holds 8:a1*b1 */ \
279 "pclmulqdq $0, %%xmm5, %%xmm4\n\t" /* xmm4 holds 8:(a0+a1)*(b0+b1) */ \
280 \
281 "pshufd $78, %%xmm9, %%xmm11\n\t" \
282 "pshufd $78, %%xmm2, %%xmm7\n\t" \
283 "pxor %%xmm9, %%xmm11\n\t" /* xmm11 holds 7:a0+a1 */ \
284 "pxor %%xmm2, %%xmm7\n\t" /* xmm7 holds 7:b0+b1 */ \
285 "movdqa %%xmm9, %%xmm6\n\t" \
286 "pclmulqdq $0, %%xmm2, %%xmm6\n\t" /* xmm6 holds 7:a0*b0 */ \
287 "pclmulqdq $17, %%xmm9, %%xmm2\n\t" /* xmm2 holds 7:a1*b1 */ \
288 "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 7:(a0+a1)*(b0+b1) */ \
289 \
290 "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 7+8:a0*b0 */ \
291 "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 7+8:a1*b1 */ \
292 "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 7+8:(a0+a1)*(b0+b1) */ \
293 \
294 "movdqu 2*16(%[buf]), %%xmm5\n\t" \
295 "movdqu 3*16(%[buf]), %%xmm2\n\t" \
296 be_to_le("pshufb %%xmm15, %%xmm5\n\t") /* be => le */ \
297 be_to_le("pshufb %%xmm15, %%xmm2\n\t") /* be => le */ \
298 \
299 "pshufd $78, %%xmm8, %%xmm11\n\t" \
300 "pshufd $78, %%xmm5, %%xmm7\n\t" \
301 "pxor %%xmm8, %%xmm11\n\t" /* xmm11 holds 6:a0+a1 */ \
302 "pxor %%xmm5, %%xmm7\n\t" /* xmm7 holds 6:b0+b1 */ \
303 "movdqa %%xmm8, %%xmm6\n\t" \
304 "pclmulqdq $0, %%xmm5, %%xmm6\n\t" /* xmm6 holds 6:a0*b0 */ \
305 "pclmulqdq $17, %%xmm8, %%xmm5\n\t" /* xmm5 holds 6:a1*b1 */ \
306 "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 6:(a0+a1)*(b0+b1) */ \
307 \
308 /* Load H3, H4, H5. */ \
309 "movdqu 3*16(%[h_table]), %%xmm10\n\t" \
310 "movdqu 2*16(%[h_table]), %%xmm9\n\t" \
311 "movdqu 1*16(%[h_table]), %%xmm8\n\t" \
312 \
313 "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 6+7+8:a0*b0 */ \
314 "pxor %%xmm5, %%xmm1\n\t" /* xmm1 holds 6+7+8:a1*b1 */ \
315 "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 6+7+8:(a0+a1)*(b0+b1) */ \
316 \
317 "pshufd $78, %%xmm10, %%xmm11\n\t" \
318 "pshufd $78, %%xmm2, %%xmm7\n\t" \
319 "pxor %%xmm10, %%xmm11\n\t" /* xmm11 holds 5:a0+a1 */ \
320 "pxor %%xmm2, %%xmm7\n\t" /* xmm7 holds 5:b0+b1 */ \
321 "movdqa %%xmm10, %%xmm6\n\t" \
322 "pclmulqdq $0, %%xmm2, %%xmm6\n\t" /* xmm6 holds 5:a0*b0 */ \
323 "pclmulqdq $17, %%xmm10, %%xmm2\n\t" /* xmm2 holds 5:a1*b1 */ \
324 "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 5:(a0+a1)*(b0+b1) */ \
325 \
326 "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 5+6+7+8:a0*b0 */ \
327 "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 5+6+7+8:a1*b1 */ \
328 "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 5+6+7+8:(a0+a1)*(b0+b1) */ \
329 \
330 "movdqu 4*16(%[buf]), %%xmm5\n\t" \
331 "movdqu 5*16(%[buf]), %%xmm2\n\t" \
332 be_to_le("pshufb %%xmm15, %%xmm5\n\t") /* be => le */ \
333 be_to_le("pshufb %%xmm15, %%xmm2\n\t") /* be => le */ \
334 \
335 "pshufd $78, %%xmm9, %%xmm11\n\t" \
336 "pshufd $78, %%xmm5, %%xmm7\n\t" \
337 "pxor %%xmm9, %%xmm11\n\t" /* xmm11 holds 4:a0+a1 */ \
338 "pxor %%xmm5, %%xmm7\n\t" /* xmm7 holds 4:b0+b1 */ \
339 "movdqa %%xmm9, %%xmm6\n\t" \
340 "pclmulqdq $0, %%xmm5, %%xmm6\n\t" /* xmm6 holds 4:a0*b0 */ \
341 "pclmulqdq $17, %%xmm9, %%xmm5\n\t" /* xmm5 holds 4:a1*b1 */ \
342 "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 4:(a0+a1)*(b0+b1) */ \
343 \
344 "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 4+5+6+7+8:a0*b0 */ \
345 "pxor %%xmm5, %%xmm1\n\t" /* xmm1 holds 4+5+6+7+8:a1*b1 */ \
346 "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 4+5+6+7+8:(a0+a1)*(b0+b1) */ \
347 \
348 "pshufd $78, %%xmm8, %%xmm11\n\t" \
349 "pshufd $78, %%xmm2, %%xmm7\n\t" \
350 "pxor %%xmm8, %%xmm11\n\t" /* xmm11 holds 3:a0+a1 */ \
351 "pxor %%xmm2, %%xmm7\n\t" /* xmm7 holds 3:b0+b1 */ \
352 "movdqa %%xmm8, %%xmm6\n\t" \
353 "pclmulqdq $0, %%xmm2, %%xmm6\n\t" /* xmm6 holds 3:a0*b0 */ \
354 "pclmulqdq $17, %%xmm8, %%xmm2\n\t" /* xmm2 holds 3:a1*b1 */ \
355 "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 3:(a0+a1)*(b0+b1) */ \
356 \
357 "movdqu 0*16(%[h_table]), %%xmm8\n\t" /* Load H2 */ \
358 \
359 "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 3+4+5+6+7+8:a0*b0 */ \
360 "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 3+4+5+6+7+8:a1*b1 */ \
361 "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 3+4+5+6+7+8:(a0+a1)*(b0+b1) */ \
362 \
363 "movdqu 6*16(%[buf]), %%xmm5\n\t" \
364 "movdqu 7*16(%[buf]), %%xmm2\n\t" \
365 be_to_le("pshufb %%xmm15, %%xmm5\n\t") /* be => le */ \
366 be_to_le("pshufb %%xmm15, %%xmm2\n\t") /* be => le */ \
367 \
368 "pshufd $78, %%xmm8, %%xmm11\n\t" \
369 "pshufd $78, %%xmm5, %%xmm7\n\t" \
370 "pxor %%xmm8, %%xmm11\n\t" /* xmm11 holds 2:a0+a1 */ \
371 "pxor %%xmm5, %%xmm7\n\t" /* xmm7 holds 2:b0+b1 */ \
372 "movdqa %%xmm8, %%xmm6\n\t" \
373 "pclmulqdq $0, %%xmm5, %%xmm6\n\t" /* xmm6 holds 2:a0*b0 */ \
374 "pclmulqdq $17, %%xmm8, %%xmm5\n\t" /* xmm5 holds 2:a1*b1 */ \
375 "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 2:(a0+a1)*(b0+b1) */ \
376 \
377 "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 2+3+4+5+6+7+8:a0*b0 */ \
378 "pxor %%xmm5, %%xmm1\n\t" /* xmm1 holds 2+3+4+5+6+7+8:a1*b1 */ \
379 "pxor %%xmm7, %%xmm4\n\t" /* xmm4 holds 2+3+4+5+6+7+8:(a0+a1)*(b0+b1) */ \
380 \
381 "pshufd $78, %%xmm0, %%xmm11\n\t" \
382 "pshufd $78, %%xmm2, %%xmm7\n\t" \
383 "pxor %%xmm0, %%xmm11\n\t" /* xmm11 holds 1:a0+a1 */ \
384 "pxor %%xmm2, %%xmm7\n\t" /* xmm7 holds 1:b0+b1 */ \
385 "movdqa %%xmm0, %%xmm6\n\t" \
386 "pclmulqdq $0, %%xmm2, %%xmm6\n\t" /* xmm6 holds 1:a0*b0 */ \
387 "pclmulqdq $17, %%xmm0, %%xmm2\n\t" /* xmm2 holds 1:a1*b1 */ \
388 "pclmulqdq $0, %%xmm11, %%xmm7\n\t" /* xmm7 holds 1:(a0+a1)*(b0+b1) */ \
389 \
390 "pxor %%xmm6, %%xmm3\n\t" /* xmm3 holds 1+2+3+4+5+6+7+8:a0*b0 */ \
391 "pxor %%xmm2, %%xmm1\n\t" /* xmm1 holds 1+2+3+4+5+6+7+8:a1*b1 */ \
392 "pxor %%xmm7, %%xmm4\n\t"/* xmm4 holds 1+2+3+4+5+6+7+8:(a0+a1)*(b0+b1) */ \
393 \
394 /* aggregated reduction... */ \
395 "movdqa %%xmm3, %%xmm5\n\t" \
396 "pxor %%xmm1, %%xmm5\n\t" /* xmm5 holds a0*b0+a1*b1 */ \
397 "pxor %%xmm5, %%xmm4\n\t" /* xmm4 holds a0*b0+a1*b1+(a0+a1)*(b0+b1) */ \
398 "movdqa %%xmm4, %%xmm5\n\t" \
399 "psrldq $8, %%xmm4\n\t" \
400 "pslldq $8, %%xmm5\n\t" \
401 "pxor %%xmm5, %%xmm3\n\t" \
402 "pxor %%xmm4, %%xmm1\n\t" /* <xmm1:xmm3> holds the result of the \
403 carry-less multiplication of xmm0 \
404 by xmm1 */
405
406 static ASM_FUNC_ATTR_INLINE void
407 gfmul_pclmul_aggr8(const void *buf, const void *h_table)
408 {
409 /* Input:
410 H¹: XMM0
411 bemask: XMM15
412 Hash: XMM1
413 Output:
414 Hash: XMM1
415 Inputs XMM0 and XMM15 stays unmodified.
416 */
417 asm volatile (GFMUL_AGGR8_ASM(be_to_le)
418 :
419 : [buf] "r" (buf),
420 [h_table] "r" (h_table)
421 : "memory" );
422
423 reduction();
424 }
425
426 static ASM_FUNC_ATTR_INLINE void
427 gfmul_pclmul_aggr8_le(const void *buf, const void *h_table)
428 {
429 /* Input:
430 H¹: XMM0
431 Hash: XMM1
432 Output:
433 Hash: XMM1
434 Inputs XMM0 and XMM15 stays unmodified.
435 */
436 asm volatile (GFMUL_AGGR8_ASM(le_to_le)
437 :
438 : [buf] "r" (buf),
439 [h_table] "r" (h_table)
440 : "memory" );
441
442 reduction();
443 }
444
445 #ifdef GCM_USE_INTEL_VPCLMUL_AVX2
446
447 #define GFMUL_AGGR16_ASM_VPCMUL_AVX2(be_to_le) \
448 /* perform clmul and merge results... */ \
449 "vmovdqu 0*16(%[buf]), %%ymm5\n\t" \
450 "vmovdqu 2*16(%[buf]), %%ymm2\n\t" \
451 be_to_le("vpshufb %%ymm15, %%ymm5, %%ymm5\n\t") /* be => le */ \
452 be_to_le("vpshufb %%ymm15, %%ymm2, %%ymm2\n\t") /* be => le */ \
453 "vpxor %%ymm5, %%ymm1, %%ymm1\n\t" \
454 \
455 "vpshufd $78, %%ymm0, %%ymm5\n\t" \
456 "vpshufd $78, %%ymm1, %%ymm4\n\t" \
457 "vpxor %%ymm0, %%ymm5, %%ymm5\n\t" /* ymm5 holds 15|16:a0+a1 */ \
458 "vpxor %%ymm1, %%ymm4, %%ymm4\n\t" /* ymm4 holds 15|16:b0+b1 */ \
459 "vpclmulqdq $0, %%ymm1, %%ymm0, %%ymm3\n\t" /* ymm3 holds 15|16:a0*b0 */ \
460 "vpclmulqdq $17, %%ymm0, %%ymm1, %%ymm1\n\t" /* ymm1 holds 15|16:a1*b1 */ \
461 "vpclmulqdq $0, %%ymm5, %%ymm4, %%ymm4\n\t" /* ymm4 holds 15|16:(a0+a1)*(b0+b1) */ \
462 \
463 "vmovdqu %[h1_h2], %%ymm0\n\t" \
464 \
465 "vpshufd $78, %%ymm13, %%ymm14\n\t" \
466 "vpshufd $78, %%ymm2, %%ymm7\n\t" \
467 "vpxor %%ymm13, %%ymm14, %%ymm14\n\t" /* ymm14 holds 13|14:a0+a1 */ \
468 "vpxor %%ymm2, %%ymm7, %%ymm7\n\t" /* ymm7 holds 13|14:b0+b1 */ \
469 "vpclmulqdq $0, %%ymm2, %%ymm13, %%ymm6\n\t" /* ymm6 holds 13|14:a0*b0 */ \
470 "vpclmulqdq $17, %%ymm13, %%ymm2, %%ymm2\n\t" /* ymm2 holds 13|14:a1*b1 */ \
471 "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t" /* ymm7 holds 13|14:(a0+a1)*(b0+b1) */\
472 \
473 "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 13+15|14+16:a0*b0 */ \
474 "vpxor %%ymm2, %%ymm1, %%ymm1\n\t" /* ymm1 holds 13+15|14+16:a1*b1 */ \
475 "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 13+15|14+16:(a0+a1)*(b0+b1) */ \
476 \
477 "vmovdqu 4*16(%[buf]), %%ymm5\n\t" \
478 "vmovdqu 6*16(%[buf]), %%ymm2\n\t" \
479 be_to_le("vpshufb %%ymm15, %%ymm5, %%ymm5\n\t") /* be => le */ \
480 be_to_le("vpshufb %%ymm15, %%ymm2, %%ymm2\n\t") /* be => le */ \
481 \
482 "vpshufd $78, %%ymm12, %%ymm14\n\t" \
483 "vpshufd $78, %%ymm5, %%ymm7\n\t" \
484 "vpxor %%ymm12, %%ymm14, %%ymm14\n\t" /* ymm14 holds 11|12:a0+a1 */ \
485 "vpxor %%ymm5, %%ymm7, %%ymm7\n\t" /* ymm7 holds 11|12:b0+b1 */ \
486 "vpclmulqdq $0, %%ymm5, %%ymm12, %%ymm6\n\t" /* ymm6 holds 11|12:a0*b0 */ \
487 "vpclmulqdq $17, %%ymm12, %%ymm5, %%ymm5\n\t" /* ymm5 holds 11|12:a1*b1 */ \
488 "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t" /* ymm7 holds 11|12:(a0+a1)*(b0+b1) */\
489 \
490 "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 11+13+15|12+14+16:a0*b0 */ \
491 "vpxor %%ymm5, %%ymm1, %%ymm1\n\t" /* ymm1 holds 11+13+15|12+14+16:a1*b1 */ \
492 "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 11+13+15|12+14+16:(a0+a1)*(b0+b1) */\
493 \
494 "vpshufd $78, %%ymm11, %%ymm14\n\t" \
495 "vpshufd $78, %%ymm2, %%ymm7\n\t" \
496 "vpxor %%ymm11, %%ymm14, %%ymm14\n\t" /* ymm14 holds 9|10:a0+a1 */ \
497 "vpxor %%ymm2, %%ymm7, %%ymm7\n\t" /* ymm7 holds 9|10:b0+b1 */ \
498 "vpclmulqdq $0, %%ymm2, %%ymm11, %%ymm6\n\t" /* ymm6 holds 9|10:a0*b0 */ \
499 "vpclmulqdq $17, %%ymm11, %%ymm2, %%ymm2\n\t" /* ymm2 holds 9|10:a1*b1 */ \
500 "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t" /* ymm7 holds 9|10:(a0+a1)*(b0+b1) */ \
501 \
502 "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 9+11+…+15|10+12+…+16:a0*b0 */ \
503 "vpxor %%ymm2, %%ymm1, %%ymm1\n\t" /* ymm1 holds 9+11+…+15|10+12+…+16:a1*b1 */ \
504 "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 9+11+…+15|10+12+…+16:(a0+a1)*(b0+b1) */\
505 \
506 "vmovdqu 8*16(%[buf]), %%ymm5\n\t" \
507 "vmovdqu 10*16(%[buf]), %%ymm2\n\t" \
508 be_to_le("vpshufb %%ymm15, %%ymm5, %%ymm5\n\t") /* be => le */ \
509 be_to_le("vpshufb %%ymm15, %%ymm2, %%ymm2\n\t") /* be => le */ \
510 \
511 "vpshufd $78, %%ymm10, %%ymm14\n\t" \
512 "vpshufd $78, %%ymm5, %%ymm7\n\t" \
513 "vpxor %%ymm10, %%ymm14, %%ymm14\n\t" /* ymm14 holds 7|8:a0+a1 */ \
514 "vpxor %%ymm5, %%ymm7, %%ymm7\n\t" /* ymm7 holds 7|8:b0+b1 */ \
515 "vpclmulqdq $0, %%ymm5, %%ymm10, %%ymm6\n\t" /* ymm6 holds 7|8:a0*b0 */ \
516 "vpclmulqdq $17, %%ymm10, %%ymm5, %%ymm5\n\t" /* ymm5 holds 7|8:a1*b1 */ \
517 "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t" /* ymm7 holds 7|8:(a0+a1)*(b0+b1) */ \
518 \
519 "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 7+9+…+15|8+10+…+16:a0*b0 */ \
520 "vpxor %%ymm5, %%ymm1, %%ymm1\n\t" /* ymm1 holds 7+9+…+15|8+10+…+16:a1*b1 */ \
521 "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 7+9+…+15|8+10+…+16:(a0+a1)*(b0+b1) */\
522 \
523 "vpshufd $78, %%ymm9, %%ymm14\n\t" \
524 "vpshufd $78, %%ymm2, %%ymm7\n\t" \
525 "vpxor %%ymm9, %%ymm14, %%ymm14\n\t" /* ymm14 holds 5|6:a0+a1 */ \
526 "vpxor %%ymm2, %%ymm7, %%ymm7\n\t" /* ymm7 holds 5|6:b0+b1 */ \
527 "vpclmulqdq $0, %%ymm2, %%ymm9, %%ymm6\n\t" /* ymm6 holds 5|6:a0*b0 */ \
528 "vpclmulqdq $17, %%ymm9, %%ymm2, %%ymm2\n\t" /* ymm2 holds 5|6:a1*b1 */ \
529 "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t" /* ymm7 holds 5|6:(a0+a1)*(b0+b1) */ \
530 \
531 "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 5+7+…+15|6+8+…+16:a0*b0 */ \
532 "vpxor %%ymm2, %%ymm1, %%ymm1\n\t" /* ymm1 holds 5+7+…+15|6+8+…+16:a1*b1 */ \
533 "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 5+7+…+15|6+8+…+16:(a0+a1)*(b0+b1) */\
534 \
535 "vmovdqu 12*16(%[buf]), %%ymm5\n\t" \
536 "vmovdqu 14*16(%[buf]), %%ymm2\n\t" \
537 be_to_le("vpshufb %%ymm15, %%ymm5, %%ymm5\n\t") /* be => le */ \
538 be_to_le("vpshufb %%ymm15, %%ymm2, %%ymm2\n\t") /* be => le */ \
539 \
540 "vpshufd $78, %%ymm8, %%ymm14\n\t" \
541 "vpshufd $78, %%ymm5, %%ymm7\n\t" \
542 "vpxor %%ymm8, %%ymm14, %%ymm14\n\t" /* ymm14 holds 3|4:a0+a1 */ \
543 "vpxor %%ymm5, %%ymm7, %%ymm7\n\t" /* ymm7 holds 3|4:b0+b1 */ \
544 "vpclmulqdq $0, %%ymm5, %%ymm8, %%ymm6\n\t" /* ymm6 holds 3|4:a0*b0 */ \
545 "vpclmulqdq $17, %%ymm8, %%ymm5, %%ymm5\n\t" /* ymm5 holds 3|4:a1*b1 */ \
546 "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t" /* ymm7 holds 3|4:(a0+a1)*(b0+b1) */ \
547 \
548 "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 3+5+…+15|4+6+…+16:a0*b0 */ \
549 "vpxor %%ymm5, %%ymm1, %%ymm1\n\t" /* ymm1 holds 3+5+…+15|4+6+…+16:a1*b1 */ \
550 "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 3+5+…+15|4+6+…+16:(a0+a1)*(b0+b1) */\
551 \
552 "vpshufd $78, %%ymm0, %%ymm14\n\t" \
553 "vpshufd $78, %%ymm2, %%ymm7\n\t" \
554 "vpxor %%ymm0, %%ymm14, %%ymm14\n\t" /* ymm14 holds 1|2:a0+a1 */ \
555 "vpxor %%ymm2, %%ymm7, %%ymm7\n\t" /* ymm7 holds 1|2:b0+b1 */ \
556 "vpclmulqdq $0, %%ymm2, %%ymm0, %%ymm6\n\t" /* ymm6 holds 1|2:a0*b0 */ \
557 "vpclmulqdq $17, %%ymm0, %%ymm2, %%ymm2\n\t" /* ymm2 holds 1|2:a1*b1 */ \
558 "vpclmulqdq $0, %%ymm14, %%ymm7, %%ymm7\n\t" /* ymm7 holds 1|2:(a0+a1)*(b0+b1) */ \
559 \
560 "vmovdqu %[h15_h16], %%ymm0\n\t" \
561 \
562 "vpxor %%ymm6, %%ymm3, %%ymm3\n\t" /* ymm3 holds 1+3+…+15|2+4+…+16:a0*b0 */ \
563 "vpxor %%ymm2, %%ymm1, %%ymm1\n\t" /* ymm1 holds 1+3+…+15|2+4+…+16:a1*b1 */ \
564 "vpxor %%ymm7, %%ymm4, %%ymm4\n\t" /* ymm4 holds 1+3+…+15|2+4+…+16:(a0+a1)*(b0+b1) */\
565 \
566 /* aggregated reduction... */ \
567 "vpxor %%ymm1, %%ymm3, %%ymm5\n\t" /* ymm5 holds a0*b0+a1*b1 */ \
568 "vpxor %%ymm5, %%ymm4, %%ymm4\n\t" /* ymm4 holds a0*b0+a1*b1+(a0+a1)*(b0+b1) */ \
569 "vpslldq $8, %%ymm4, %%ymm5\n\t" \
570 "vpsrldq $8, %%ymm4, %%ymm4\n\t" \
571 "vpxor %%ymm5, %%ymm3, %%ymm3\n\t" \
572 "vpxor %%ymm4, %%ymm1, %%ymm1\n\t" /* <ymm1:xmm3> holds the result of the \
573 carry-less multiplication of ymm0 \
574 by ymm1 */ \
575 \
576 /* first phase of the reduction */ \
577 "vpsllq $1, %%ymm3, %%ymm6\n\t" /* packed right shifting << 63 */ \
578 "vpxor %%ymm3, %%ymm6, %%ymm6\n\t" \
579 "vpsllq $57, %%ymm3, %%ymm5\n\t" /* packed right shifting << 57 */ \
580 "vpsllq $62, %%ymm6, %%ymm6\n\t" /* packed right shifting << 62 */ \
581 "vpxor %%ymm5, %%ymm6, %%ymm6\n\t" /* xor the shifted versions */ \
582 "vpshufd $0x6a, %%ymm6, %%ymm5\n\t" \
583 "vpshufd $0xae, %%ymm6, %%ymm6\n\t" \
584 "vpxor %%ymm5, %%ymm3, %%ymm3\n\t" /* first phase of the reduction complete */ \
585 \
586 /* second phase of the reduction */ \
587 "vpxor %%ymm3, %%ymm1, %%ymm1\n\t" /* xor the shifted versions */ \
588 "vpsrlq $1, %%ymm3, %%ymm3\n\t" /* packed left shifting >> 1 */ \
589 "vpxor %%ymm3, %%ymm6, %%ymm6\n\t" \
590 "vpsrlq $1, %%ymm3, %%ymm3\n\t" /* packed left shifting >> 2 */ \
591 "vpxor %%ymm3, %%ymm1, %%ymm1\n\t" \
592 "vpsrlq $5, %%ymm3, %%ymm3\n\t" /* packed left shifting >> 7 */ \
593 "vpxor %%ymm3, %%ymm6, %%ymm6\n\t" \
594 "vpxor %%ymm6, %%ymm1, %%ymm1\n\t" /* the result is in ymm1 */ \
595 \
596 /* merge 128-bit halves */ \
597 "vextracti128 $1, %%ymm1, %%xmm2\n\t" \
598 "vpxor %%xmm2, %%xmm1, %%xmm1\n\t"
599
600 static ASM_FUNC_ATTR_INLINE void
601 gfmul_vpclmul_avx2_aggr16(const void *buf, const void *h_table,
602 const u64 *h1_h2_h15_h16)
603 {
604 /* Input:
605 Hx: YMM0, YMM8, YMM9, YMM10, YMM11, YMM12, YMM13
606 bemask: YMM15
607 Hash: XMM1
608 Output:
609 Hash: XMM1
610 Inputs YMM0, YMM8, YMM9, YMM10, YMM11, YMM12, YMM13 and YMM15 stay
611 unmodified.
612 */
613 asm volatile (GFMUL_AGGR16_ASM_VPCMUL_AVX2(be_to_le)
614 :
615 : [buf] "r" (buf),
616 [h_table] "r" (h_table),
617 [h1_h2] "m" (h1_h2_h15_h16[0]),
618 [h15_h16] "m" (h1_h2_h15_h16[4])
619 : "memory" );
620 }
621
622 static ASM_FUNC_ATTR_INLINE void
623 gfmul_vpclmul_avx2_aggr16_le(const void *buf, const void *h_table,
624 const u64 *h1_h2_h15_h16)
625 {
626 /* Input:
627 Hx: YMM0, YMM8, YMM9, YMM10, YMM11, YMM12, YMM13
628 bemask: YMM15
629 Hash: XMM1
630 Output:
631 Hash: XMM1
632 Inputs YMM0, YMM8, YMM9, YMM10, YMM11, YMM12, YMM13 and YMM15 stay
633 unmodified.
634 */
635 asm volatile (GFMUL_AGGR16_ASM_VPCMUL_AVX2(le_to_le)
636 :
637 : [buf] "r" (buf),
638 [h_table] "r" (h_table),
639 [h1_h2] "m" (h1_h2_h15_h16[0]),
640 [h15_h16] "m" (h1_h2_h15_h16[4])
641 : "memory" );
642 }
643
644 static ASM_FUNC_ATTR_INLINE
645 void gfmul_pclmul_avx2(void)
646 {
647 /* Input: YMM0 and YMM1, Output: YMM1. Input YMM0 stays unmodified.
648 Input must be converted to little-endian.
649 */
650 asm volatile (/* gfmul, ymm0 has operator a and ymm1 has operator b. */
651 "vpshufd $78, %%ymm0, %%ymm2\n\t"
652 "vpshufd $78, %%ymm1, %%ymm4\n\t"
653 "vpxor %%ymm0, %%ymm2, %%ymm2\n\t" /* ymm2 holds a0+a1 */
654 "vpxor %%ymm1, %%ymm4, %%ymm4\n\t" /* ymm4 holds b0+b1 */
655
656 "vpclmulqdq $0, %%ymm1, %%ymm0, %%ymm3\n\t" /* ymm3 holds a0*b0 */
657 "vpclmulqdq $17, %%ymm0, %%ymm1, %%ymm1\n\t" /* ymm6 holds a1*b1 */
658 "vpclmulqdq $0, %%ymm2, %%ymm4, %%ymm4\n\t" /* ymm4 holds (a0+a1)*(b0+b1) */
659
660 "vpxor %%ymm1, %%ymm3, %%ymm5\n\t" /* ymm5 holds a0*b0+a1*b1 */
661 "vpxor %%ymm5, %%ymm4, %%ymm4\n\t" /* ymm4 holds a0*b0+a1*b1+(a0+a1)*(b0+b1) */
662 "vpslldq $8, %%ymm4, %%ymm5\n\t"
663 "vpsrldq $8, %%ymm4, %%ymm4\n\t"
664 "vpxor %%ymm5, %%ymm3, %%ymm3\n\t"
665 "vpxor %%ymm4, %%ymm1, %%ymm1\n\t" /* <ymm1:ymm3> holds the result of the
666 carry-less multiplication of ymm0
667 by ymm1 */
668
669 /* first phase of the reduction */
670 "vpsllq $1, %%ymm3, %%ymm6\n\t" /* packed right shifting << 63 */
671 "vpxor %%ymm3, %%ymm6, %%ymm6\n\t"
672 "vpsllq $57, %%ymm3, %%ymm5\n\t" /* packed right shifting << 57 */
673 "vpsllq $62, %%ymm6, %%ymm6\n\t" /* packed right shifting << 62 */
674 "vpxor %%ymm5, %%ymm6, %%ymm6\n\t" /* xor the shifted versions */
675 "vpshufd $0x6a, %%ymm6, %%ymm5\n\t"
676 "vpshufd $0xae, %%ymm6, %%ymm6\n\t"
677 "vpxor %%ymm5, %%ymm3, %%ymm3\n\t" /* first phase of the reduction complete */
678
679 /* second phase of the reduction */
680 "vpxor %%ymm3, %%ymm1, %%ymm1\n\t" /* xor the shifted versions */
681 "vpsrlq $1, %%ymm3, %%ymm3\n\t" /* packed left shifting >> 1 */
682 "vpxor %%ymm3, %%ymm6, %%ymm6\n\t"
683 "vpsrlq $1, %%ymm3, %%ymm3\n\t" /* packed left shifting >> 2 */
684 "vpxor %%ymm3, %%ymm1, %%ymm1\n\t"
685 "vpsrlq $5, %%ymm3, %%ymm3\n\t" /* packed left shifting >> 7 */
686 "vpxor %%ymm3, %%ymm6, %%ymm6\n\t"
687 "vpxor %%ymm6, %%ymm1, %%ymm1\n\t" /* the result is in ymm1 */
688 ::: "memory" );
689 }
690
691 static ASM_FUNC_ATTR_INLINE void
692 gcm_lsh_avx2(void *h, unsigned int hoffs)
693 {
694 static const u64 pconst[4] __attribute__ ((aligned (32))) =
695 {
696 U64_C(0x0000000000000001), U64_C(0xc200000000000000),
697 U64_C(0x0000000000000001), U64_C(0xc200000000000000)
698 };
699
700 asm volatile ("vmovdqu %[h], %%ymm2\n\t"
701 "vpshufd $0xff, %%ymm2, %%ymm3\n\t"
702 "vpsrad $31, %%ymm3, %%ymm3\n\t"
703 "vpslldq $8, %%ymm2, %%ymm4\n\t"
704 "vpand %[pconst], %%ymm3, %%ymm3\n\t"
705 "vpaddq %%ymm2, %%ymm2, %%ymm2\n\t"
706 "vpsrlq $63, %%ymm4, %%ymm4\n\t"
707 "vpxor %%ymm3, %%ymm2, %%ymm2\n\t"
708 "vpxor %%ymm4, %%ymm2, %%ymm2\n\t"
709 "vmovdqu %%ymm2, %[h]\n\t"
710 : [h] "+m" (*((byte *)h + hoffs))
711 : [pconst] "m" (*pconst)
712 : "memory" );
713 }
714
715 static ASM_FUNC_ATTR_INLINE void
716 load_h1h2_to_ymm1(gcry_cipher_hd_t c)
717 {
718 unsigned int key_pos =
719 offsetof(struct gcry_cipher_handle, u_mode.gcm.u_ghash_key.key);
720 unsigned int table_pos =
721 offsetof(struct gcry_cipher_handle, u_mode.gcm.gcm_table);
722
723 if (key_pos + 16 == table_pos)
724 {
725 /* Optimization: Table follows immediately after key. */
726 asm volatile ("vmovdqu %[key], %%ymm1\n\t"
727 :
728 : [key] "m" (*c->u_mode.gcm.u_ghash_key.key)
729 : "memory");
730 }
731 else
732 {
733 asm volatile ("vmovdqa %[key], %%xmm1\n\t"
734 "vinserti128 $1, 0*16(%[h_table]), %%ymm1, %%ymm1\n\t"
735 :
736 : [h_table] "r" (c->u_mode.gcm.gcm_table),
737 [key] "m" (*c->u_mode.gcm.u_ghash_key.key)
738 : "memory");
739 }
740 }
741
742 static ASM_FUNC_ATTR void
743 ghash_setup_aggr8_avx2(gcry_cipher_hd_t c)
744 {
745 c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_AGGR8_TABLE_INITIALIZED;
746
747 asm volatile (/* load H⁓ */
748 "vbroadcasti128 3*16(%[h_table]), %%ymm0\n\t"
749 :
750 : [h_table] "r" (c->u_mode.gcm.gcm_table)
751 : "memory");
752 /* load H <<< 1, H² <<< 1 */
753 load_h1h2_to_ymm1 (c);
754
755 gfmul_pclmul_avx2 (); /* H<<<1•H⁓ => H⁵, H²<<<1•H⁓ => H⁶ */
756
757 asm volatile ("vmovdqu %%ymm1, 3*16(%[h_table])\n\t"
758 /* load H³ <<< 1, H⁓ <<< 1 */
759 "vmovdqu 1*16(%[h_table]), %%ymm1\n\t"
760 :
761 : [h_table] "r" (c->u_mode.gcm.gcm_table)
762 : "memory");
763
764 gfmul_pclmul_avx2 (); /* H³<<<1•H⁓ => H⁷, H⁓<<<1•H⁓ => H⁸ */
765
766 asm volatile ("vmovdqu %%ymm1, 6*16(%[h_table])\n\t" /* store H⁸ for aggr16 setup */
767 "vmovdqu %%ymm1, 5*16(%[h_table])\n\t"
768 :
769 : [h_table] "r" (c->u_mode.gcm.gcm_table)
770 : "memory");
771
772 gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 3 * 16); /* H⁵ <<< 1, H⁶ <<< 1 */
773 gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 5 * 16); /* H⁷ <<< 1, H⁸ <<< 1 */
774 }
775
776 static ASM_FUNC_ATTR void
777 ghash_setup_aggr16_avx2(gcry_cipher_hd_t c)
778 {
779 c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_AGGR16_TABLE_INITIALIZED;
780
781 asm volatile (/* load H⁸ */
782 "vbroadcasti128 7*16(%[h_table]), %%ymm0\n\t"
783 :
784 : [h_table] "r" (c->u_mode.gcm.gcm_table)
785 : "memory");
786 /* load H <<< 1, H² <<< 1 */
787 load_h1h2_to_ymm1 (c);
788
789 gfmul_pclmul_avx2 (); /* H<<<1•H⁸ => H⁹, H²<<<1•H⁸ => H¹⁰ */
790
791 asm volatile ("vmovdqu %%ymm1, 7*16(%[h_table])\n\t"
792 /* load H³ <<< 1, H⁓ <<< 1 */
793 "vmovdqu 1*16(%[h_table]), %%ymm1\n\t"
794 :
795 : [h_table] "r" (c->u_mode.gcm.gcm_table)
796 : "memory");
797
798 gfmul_pclmul_avx2 (); /* H³<<<1•H⁸ => H¹¹, H⁓<<<1•H⁸ => H¹² */
799
800 asm volatile ("vmovdqu %%ymm1, 9*16(%[h_table])\n\t"
801 /* load H⁵ <<< 1, H⁶ <<< 1 */
802 "vmovdqu 3*16(%[h_table]), %%ymm1\n\t"
803 :
804 : [h_table] "r" (c->u_mode.gcm.gcm_table)
805 : "memory");
806
807 gfmul_pclmul_avx2 (); /* H⁵<<<1•H⁸ => H¹³, H⁶<<<1•H⁸ => H¹⁓ */
808
809 asm volatile ("vmovdqu %%ymm1, 11*16(%[h_table])\n\t"
810 /* load H⁷ <<< 1, H⁸ <<< 1 */
811 "vmovdqu 5*16(%[h_table]), %%ymm1\n\t"
812 :
813 : [h_table] "r" (c->u_mode.gcm.gcm_table)
814 : "memory");
815
816 gfmul_pclmul_avx2 (); /* H⁷<<<1•H⁸ => H¹⁵, H⁸<<<1•H⁸ => H¹⁶ */
817
818 asm volatile ("vmovdqu %%ymm1, 14*16(%[h_table])\n\t" /* store H¹⁶ for aggr32 setup */
819 "vmovdqu %%ymm1, 13*16(%[h_table])\n\t"
820 :
821 : [h_table] "r" (c->u_mode.gcm.gcm_table)
822 : "memory");
823
824 gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 7 * 16); /* H⁹ <<< 1, H¹⁰ <<< 1 */
825 gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 9 * 16); /* H¹¹ <<< 1, H¹² <<< 1 */
826 gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 11 * 16); /* H¹³ <<< 1, H¹⁓ <<< 1 */
827 gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 13 * 16); /* H¹⁵ <<< 1, H¹⁶ <<< 1 */
828 }
829
830 #endif /* GCM_USE_INTEL_VPCLMUL_AVX2 */
831
832 #ifdef GCM_USE_INTEL_VPCLMUL_AVX512
833
834 #define GFMUL_AGGR32_ASM_VPCMUL_AVX512(be_to_le) \
835 /* perform clmul and merge results... */ \
836 "vmovdqu64 0*16(%[buf]), %%zmm5\n\t" \
837 "vmovdqu64 4*16(%[buf]), %%zmm2\n\t" \
838 be_to_le("vpshufb %%zmm15, %%zmm5, %%zmm5\n\t") /* be => le */ \
839 be_to_le("vpshufb %%zmm15, %%zmm2, %%zmm2\n\t") /* be => le */ \
840 "vpxorq %%zmm5, %%zmm1, %%zmm1\n\t" \
841 \
842 "vpshufd $78, %%zmm0, %%zmm5\n\t" \
843 "vpshufd $78, %%zmm1, %%zmm4\n\t" \
844 "vpxorq %%zmm0, %%zmm5, %%zmm5\n\t" /* zmm5 holds 29|…|32:a0+a1 */ \
845 "vpxorq %%zmm1, %%zmm4, %%zmm4\n\t" /* zmm4 holds 29|…|32:b0+b1 */ \
846 "vpclmulqdq $0, %%zmm1, %%zmm0, %%zmm3\n\t" /* zmm3 holds 29|…|32:a0*b0 */ \
847 "vpclmulqdq $17, %%zmm0, %%zmm1, %%zmm1\n\t" /* zmm1 holds 29|…|32:a1*b1 */ \
848 "vpclmulqdq $0, %%zmm5, %%zmm4, %%zmm4\n\t" /* zmm4 holds 29|…|32:(a0+a1)*(b0+b1) */ \
849 \
850 "vpshufd $78, %%zmm13, %%zmm14\n\t" \
851 "vpshufd $78, %%zmm2, %%zmm7\n\t" \
852 "vpxorq %%zmm13, %%zmm14, %%zmm14\n\t" /* zmm14 holds 25|…|28:a0+a1 */ \
853 "vpxorq %%zmm2, %%zmm7, %%zmm7\n\t" /* zmm7 holds 25|…|28:b0+b1 */ \
854 "vpclmulqdq $0, %%zmm2, %%zmm13, %%zmm17\n\t" /* zmm17 holds 25|…|28:a0*b0 */ \
855 "vpclmulqdq $17, %%zmm13, %%zmm2, %%zmm18\n\t" /* zmm18 holds 25|…|28:a1*b1 */ \
856 "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm19\n\t" /* zmm19 holds 25|…|28:(a0+a1)*(b0+b1) */\
857 \
858 "vmovdqu64 8*16(%[buf]), %%zmm5\n\t" \
859 "vmovdqu64 12*16(%[buf]), %%zmm2\n\t" \
860 be_to_le("vpshufb %%zmm15, %%zmm5, %%zmm5\n\t") /* be => le */ \
861 be_to_le("vpshufb %%zmm15, %%zmm2, %%zmm2\n\t") /* be => le */ \
862 \
863 "vpshufd $78, %%zmm12, %%zmm14\n\t" \
864 "vpshufd $78, %%zmm5, %%zmm7\n\t" \
865 "vpxorq %%zmm12, %%zmm14, %%zmm14\n\t" /* zmm14 holds 21|…|24:a0+a1 */ \
866 "vpxorq %%zmm5, %%zmm7, %%zmm7\n\t" /* zmm7 holds 21|…|24:b0+b1 */ \
867 "vpclmulqdq $0, %%zmm5, %%zmm12, %%zmm6\n\t" /* zmm6 holds 21|…|24:a0*b0 */ \
868 "vpclmulqdq $17, %%zmm12, %%zmm5, %%zmm5\n\t" /* zmm5 holds 21|…|24:a1*b1 */ \
869 "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm7\n\t" /* zmm7 holds 21|…|24:(a0+a1)*(b0+b1) */\
870 \
871 "vpternlogq $0x96, %%zmm6, %%zmm17, %%zmm3\n\t" /* zmm3 holds 21+…|…|…+32:a0*b0 */ \
872 "vpternlogq $0x96, %%zmm5, %%zmm18, %%zmm1\n\t" /* zmm1 holds 21+…|…|…+32:a1*b1 */ \
873 "vpternlogq $0x96, %%zmm7, %%zmm19, %%zmm4\n\t" /* zmm4 holds 21+…|…|…+32:(a0+a1)*(b0+b1) */\
874 \
875 "vpshufd $78, %%zmm11, %%zmm14\n\t" \
876 "vpshufd $78, %%zmm2, %%zmm7\n\t" \
877 "vpxorq %%zmm11, %%zmm14, %%zmm14\n\t" /* zmm14 holds 17|…|20:a0+a1 */ \
878 "vpxorq %%zmm2, %%zmm7, %%zmm7\n\t" /* zmm7 holds 17|…|20:b0+b1 */ \
879 "vpclmulqdq $0, %%zmm2, %%zmm11, %%zmm17\n\t" /* zmm17 holds 17|…|20:a0*b0 */ \
880 "vpclmulqdq $17, %%zmm11, %%zmm2, %%zmm18\n\t" /* zmm18 holds 17|…|20:a1*b1 */ \
881 "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm19\n\t" /* zmm19 holds 17|…|20:(a0+a1)*(b0+b1) */\
882 \
883 "vmovdqu64 16*16(%[buf]), %%zmm5\n\t" \
884 "vmovdqu64 20*16(%[buf]), %%zmm2\n\t" \
885 be_to_le("vpshufb %%zmm15, %%zmm5, %%zmm5\n\t") /* be => le */ \
886 be_to_le("vpshufb %%zmm15, %%zmm2, %%zmm2\n\t") /* be => le */ \
887 \
888 "vpshufd $78, %%zmm10, %%zmm14\n\t" \
889 "vpshufd $78, %%zmm5, %%zmm7\n\t" \
890 "vpxorq %%zmm10, %%zmm14, %%zmm14\n\t" /* zmm14 holds 13|…|16:a0+a1 */ \
891 "vpxorq %%zmm5, %%zmm7, %%zmm7\n\t" /* zmm7 holds 13|…|16:b0+b1 */ \
892 "vpclmulqdq $0, %%zmm5, %%zmm10, %%zmm6\n\t" /* zmm6 holds 13|…|16:a0*b0 */ \
893 "vpclmulqdq $17, %%zmm10, %%zmm5, %%zmm5\n\t" /* zmm5 holds 13|…|16:a1*b1 */ \
894 "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm7\n\t" /* zmm7 holds 13|…|16:(a0+a1)*(b0+b1) */ \
895 \
896 "vpternlogq $0x96, %%zmm6, %%zmm17, %%zmm3\n\t" /* zmm3 holds 13+…|…|…+32:a0*b0 */ \
897 "vpternlogq $0x96, %%zmm5, %%zmm18, %%zmm1\n\t" /* zmm1 holds 13+…|…|…+32:a1*b1 */ \
898 "vpternlogq $0x96, %%zmm7, %%zmm19, %%zmm4\n\t" /* zmm4 holds 13+…|…|…+32:(a0+a1)*(b0+b1) */\
899 \
900 "vpshufd $78, %%zmm9, %%zmm14\n\t" \
901 "vpshufd $78, %%zmm2, %%zmm7\n\t" \
902 "vpxorq %%zmm9, %%zmm14, %%zmm14\n\t" /* zmm14 holds 9|…|12:a0+a1 */ \
903 "vpxorq %%zmm2, %%zmm7, %%zmm7\n\t" /* zmm7 holds 9|…|12:b0+b1 */ \
904 "vpclmulqdq $0, %%zmm2, %%zmm9, %%zmm17\n\t" /* zmm17 holds 9|…|12:a0*b0 */ \
905 "vpclmulqdq $17, %%zmm9, %%zmm2, %%zmm18\n\t" /* zmm18 holds 9|…|12:a1*b1 */ \
906 "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm19\n\t" /* zmm19 holds 9|…|12:(a0+a1)*(b0+b1) */\
907 \
908 "vmovdqu64 24*16(%[buf]), %%zmm5\n\t" \
909 "vmovdqu64 28*16(%[buf]), %%zmm2\n\t" \
910 be_to_le("vpshufb %%zmm15, %%zmm5, %%zmm5\n\t") /* be => le */ \
911 be_to_le("vpshufb %%zmm15, %%zmm2, %%zmm2\n\t") /* be => le */ \
912 \
913 "vpshufd $78, %%zmm8, %%zmm14\n\t" \
914 "vpshufd $78, %%zmm5, %%zmm7\n\t" \
915 "vpxorq %%zmm8, %%zmm14, %%zmm14\n\t" /* zmm14 holds 5|…|8:a0+a1 */ \
916 "vpxorq %%zmm5, %%zmm7, %%zmm7\n\t" /* zmm7 holds 5|…|8:b0+b1 */ \
917 "vpclmulqdq $0, %%zmm5, %%zmm8, %%zmm6\n\t" /* zmm6 holds 5|…|8:a0*b0 */ \
918 "vpclmulqdq $17, %%zmm8, %%zmm5, %%zmm5\n\t" /* zmm5 holds 5|…|8:a1*b1 */ \
919 "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm7\n\t" /* zmm7 holds 5|…|8:(a0+a1)*(b0+b1) */ \
920 \
921 "vpternlogq $0x96, %%zmm6, %%zmm17, %%zmm3\n\t" /* zmm3 holds 5+…|…|…+32:a0*b0 */ \
922 "vpternlogq $0x96, %%zmm5, %%zmm18, %%zmm1\n\t" /* zmm1 holds 5+…|…|…+32:a1*b1 */ \
923 "vpternlogq $0x96, %%zmm7, %%zmm19, %%zmm4\n\t" /* zmm4 holds 5+…|…|…+32:(a0+a1)*(b0+b1) */\
924 \
925 "vpshufd $78, %%zmm16, %%zmm14\n\t" \
926 "vpshufd $78, %%zmm2, %%zmm7\n\t" \
927 "vpxorq %%zmm16, %%zmm14, %%zmm14\n\t" /* zmm14 holds 1|…|4:a0+a1 */ \
928 "vpxorq %%zmm2, %%zmm7, %%zmm7\n\t" /* zmm7 holds 1|2:b0+b1 */ \
929 "vpclmulqdq $0, %%zmm2, %%zmm16, %%zmm6\n\t" /* zmm6 holds 1|2:a0*b0 */ \
930 "vpclmulqdq $17, %%zmm16, %%zmm2, %%zmm2\n\t" /* zmm2 holds 1|2:a1*b1 */ \
931 "vpclmulqdq $0, %%zmm14, %%zmm7, %%zmm7\n\t" /* zmm7 holds 1|2:(a0+a1)*(b0+b1) */ \
932 \
933 "vpxorq %%zmm6, %%zmm3, %%zmm3\n\t" /* zmm3 holds 1+3+…+15|2+4+…+16:a0*b0 */ \
934 "vpxorq %%zmm2, %%zmm1, %%zmm1\n\t" /* zmm1 holds 1+3+…+15|2+4+…+16:a1*b1 */ \
935 "vpxorq %%zmm7, %%zmm4, %%zmm4\n\t" /* zmm4 holds 1+3+…+15|2+4+…+16:(a0+a1)*(b0+b1) */\
936 \
937 /* aggregated reduction... */ \
938 "vpternlogq $0x96, %%zmm1, %%zmm3, %%zmm4\n\t" /* zmm4 holds \
939 * a0*b0+a1*b1+(a0+a1)*(b0+b1) */ \
940 "vpslldq $8, %%zmm4, %%zmm5\n\t" \
941 "vpsrldq $8, %%zmm4, %%zmm4\n\t" \
942 "vpxorq %%zmm5, %%zmm3, %%zmm3\n\t" \
943 "vpxorq %%zmm4, %%zmm1, %%zmm1\n\t" /* <zmm1:zmm3> holds the result of the \
944 carry-less multiplication of zmm0 \
945 by zmm1 */ \
946 \
947 /* first phase of the reduction */ \
948 "vpsllq $1, %%zmm3, %%zmm6\n\t" /* packed right shifting << 63 */ \
949 "vpxorq %%zmm3, %%zmm6, %%zmm6\n\t" \
950 "vpsllq $57, %%zmm3, %%zmm5\n\t" /* packed right shifting << 57 */ \
951 "vpsllq $62, %%zmm6, %%zmm6\n\t" /* packed right shifting << 62 */ \
952 "vpxorq %%zmm5, %%zmm6, %%zmm6\n\t" /* xor the shifted versions */ \
953 "vpshufd $0x6a, %%zmm6, %%zmm5\n\t" \
954 "vpshufd $0xae, %%zmm6, %%zmm6\n\t" \
955 "vpxorq %%zmm5, %%zmm3, %%zmm3\n\t" /* first phase of the reduction complete */ \
956 \
957 /* second phase of the reduction */ \
958 "vpsrlq $1, %%zmm3, %%zmm2\n\t" /* packed left shifting >> 1 */ \
959 "vpsrlq $2, %%zmm3, %%zmm4\n\t" /* packed left shifting >> 2 */ \
960 "vpsrlq $7, %%zmm3, %%zmm5\n\t" /* packed left shifting >> 7 */ \
961 "vpternlogq $0x96, %%zmm3, %%zmm2, %%zmm1\n\t" /* xor the shifted versions */ \
962 "vpternlogq $0x96, %%zmm4, %%zmm5, %%zmm6\n\t" \
963 "vpxorq %%zmm6, %%zmm1, %%zmm1\n\t" /* the result is in zmm1 */ \
964 \
965 /* merge 256-bit halves */ \
966 "vextracti64x4 $1, %%zmm1, %%ymm2\n\t" \
967 "vpxor %%ymm2, %%ymm1, %%ymm1\n\t" \
968 /* merge 128-bit halves */ \
969 "vextracti128 $1, %%ymm1, %%xmm2\n\t" \
970 "vpxor %%xmm2, %%xmm1, %%xmm1\n\t"
971
972 static ASM_FUNC_ATTR_INLINE void
973 gfmul_vpclmul_avx512_aggr32(const void *buf, const void *h_table)
974 {
975 /* Input:
976 Hx: ZMM0, ZMM8, ZMM9, ZMM10, ZMM11, ZMM12, ZMM13, ZMM16
977 bemask: ZMM15
978 Hash: XMM1
979 Output:
980 Hash: XMM1
981 Inputs ZMM0, ZMM8, ZMM9, ZMM10, ZMM11, ZMM12, ZMM13, ZMM16 and YMM15 stay
982 unmodified.
983 */
984 asm volatile (GFMUL_AGGR32_ASM_VPCMUL_AVX512(be_to_le)
985 :
986 : [buf] "r" (buf),
987 [h_table] "r" (h_table)
988 : "memory" );
989 }
990
991 static ASM_FUNC_ATTR_INLINE void
992 gfmul_vpclmul_avx512_aggr32_le(const void *buf, const void *h_table)
993 {
994 /* Input:
995 Hx: ZMM0, ZMM8, ZMM9, ZMM10, ZMM11, ZMM12, ZMM13, ZMM16
996 bemask: ZMM15
997 Hash: XMM1
998 Output:
999 Hash: XMM1
1000 Inputs ZMM0, ZMM8, ZMM9, ZMM10, ZMM11, ZMM12, ZMM13, ZMM16 and YMM15 stay
1001 unmodified.
1002 */
1003 asm volatile (GFMUL_AGGR32_ASM_VPCMUL_AVX512(le_to_le)
1004 :
1005 : [buf] "r" (buf),
1006 [h_table] "r" (h_table)
1007 : "memory" );
1008 }
1009
1010 static ASM_FUNC_ATTR_INLINE
1011 void gfmul_pclmul_avx512(void)
1012 {
1013 /* Input: ZMM0 and ZMM1, Output: ZMM1. Input ZMM0 stays unmodified.
1014 Input must be converted to little-endian.
1015 */
1016 asm volatile (/* gfmul, zmm0 has operator a and zmm1 has operator b. */
1017 "vpshufd $78, %%zmm0, %%zmm2\n\t"
1018 "vpshufd $78, %%zmm1, %%zmm4\n\t"
1019 "vpxorq %%zmm0, %%zmm2, %%zmm2\n\t" /* zmm2 holds a0+a1 */
1020 "vpxorq %%zmm1, %%zmm4, %%zmm4\n\t" /* zmm4 holds b0+b1 */
1021
1022 "vpclmulqdq $0, %%zmm1, %%zmm0, %%zmm3\n\t" /* zmm3 holds a0*b0 */
1023 "vpclmulqdq $17, %%zmm0, %%zmm1, %%zmm1\n\t" /* zmm6 holds a1*b1 */
1024 "vpclmulqdq $0, %%zmm2, %%zmm4, %%zmm4\n\t" /* zmm4 holds (a0+a1)*(b0+b1) */
1025
1026 "vpternlogq $0x96, %%zmm1, %%zmm3, %%zmm4\n\t" /* zmm4 holds
1027 * a0*b0+a1*b1+(a0+a1)*(b0+b1) */
1028 "vpslldq $8, %%zmm4, %%zmm5\n\t"
1029 "vpsrldq $8, %%zmm4, %%zmm4\n\t"
1030 "vpxorq %%zmm5, %%zmm3, %%zmm3\n\t"
1031 "vpxorq %%zmm4, %%zmm1, %%zmm1\n\t" /* <zmm1:zmm3> holds the result of the
1032 carry-less multiplication of zmm0
1033 by zmm1 */
1034
1035 /* first phase of the reduction */
1036 "vpsllq $1, %%zmm3, %%zmm6\n\t" /* packed right shifting << 63 */
1037 "vpxorq %%zmm3, %%zmm6, %%zmm6\n\t"
1038 "vpsllq $57, %%zmm3, %%zmm5\n\t" /* packed right shifting << 57 */
1039 "vpsllq $62, %%zmm6, %%zmm6\n\t" /* packed right shifting << 62 */
1040 "vpxorq %%zmm5, %%zmm6, %%zmm6\n\t" /* xor the shifted versions */
1041 "vpshufd $0x6a, %%zmm6, %%zmm5\n\t"
1042 "vpshufd $0xae, %%zmm6, %%zmm6\n\t"
1043 "vpxorq %%zmm5, %%zmm3, %%zmm3\n\t" /* first phase of the reduction complete */
1044
1045 /* second phase of the reduction */
1046 "vpsrlq $1, %%zmm3, %%zmm2\n\t" /* packed left shifting >> 1 */
1047 "vpsrlq $2, %%zmm3, %%zmm4\n\t" /* packed left shifting >> 2 */
1048 "vpsrlq $7, %%zmm3, %%zmm5\n\t" /* packed left shifting >> 7 */
1049 "vpternlogq $0x96, %%zmm3, %%zmm2, %%zmm1\n\t" /* xor the shifted versions */
1050 "vpternlogq $0x96, %%zmm4, %%zmm5, %%zmm6\n\t"
1051 "vpxorq %%zmm6, %%zmm1, %%zmm1\n\t" /* the result is in zmm1 */
1052 ::: "memory" );
1053 }
1054
1055 static ASM_FUNC_ATTR_INLINE void
1056 gcm_lsh_avx512(void *h, unsigned int hoffs)
1057 {
1058 static const u64 pconst[8] __attribute__ ((aligned (64))) =
1059 {
1060 U64_C(0x0000000000000001), U64_C(0xc200000000000000),
1061 U64_C(0x0000000000000001), U64_C(0xc200000000000000),
1062 U64_C(0x0000000000000001), U64_C(0xc200000000000000),
1063 U64_C(0x0000000000000001), U64_C(0xc200000000000000)
1064 };
1065
1066 asm volatile ("vmovdqu64 %[h], %%zmm2\n\t"
1067 "vpshufd $0xff, %%zmm2, %%zmm3\n\t"
1068 "vpsrad $31, %%zmm3, %%zmm3\n\t"
1069 "vpslldq $8, %%zmm2, %%zmm4\n\t"
1070 "vpandq %[pconst], %%zmm3, %%zmm3\n\t"
1071 "vpaddq %%zmm2, %%zmm2, %%zmm2\n\t"
1072 "vpsrlq $63, %%zmm4, %%zmm4\n\t"
1073 "vpternlogq $0x96, %%zmm4, %%zmm3, %%zmm2\n\t"
1074 "vmovdqu64 %%zmm2, %[h]\n\t"
1075 : [h] "+m" (*((byte *)h + hoffs))
1076 : [pconst] "m" (*pconst)
1077 : "memory" );
1078 }
1079
1080 static ASM_FUNC_ATTR_INLINE void
1081 load_h1h4_to_zmm1(gcry_cipher_hd_t c)
1082 {
1083 unsigned int key_pos =
1084 offsetof(struct gcry_cipher_handle, u_mode.gcm.u_ghash_key.key);
1085 unsigned int table_pos =
1086 offsetof(struct gcry_cipher_handle, u_mode.gcm.gcm_table);
1087
1088 if (key_pos + 16 == table_pos)
1089 {
1090 /* Optimization: Table follows immediately after key. */
1091 asm volatile ("vmovdqu64 %[key], %%zmm1\n\t"
1092 :
1093 : [key] "m" (*c->u_mode.gcm.u_ghash_key.key)
1094 : "memory");
1095 }
1096 else
1097 {
1098 asm volatile ("vmovdqu64 -1*16(%[h_table]), %%zmm1\n\t"
1099 "vinserti64x2 $0, %[key], %%zmm1, %%zmm1\n\t"
1100 :
1101 : [h_table] "r" (c->u_mode.gcm.gcm_table),
1102 [key] "m" (*c->u_mode.gcm.u_ghash_key.key)
1103 : "memory");
1104 }
1105 }
1106
1107 static ASM_FUNC_ATTR void
1108 ghash_setup_aggr8_avx512(gcry_cipher_hd_t c)
1109 {
1110 c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_AGGR8_TABLE_INITIALIZED;
1111
1112 asm volatile (/* load H⁓ */
1113 "vbroadcasti64x2 3*16(%[h_table]), %%zmm0\n\t"
1114 :
1115 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1116 : "memory");
1117 /* load H <<< 1, H² <<< 1, H³ <<< 1, H⁓ <<< 1 */
1118 load_h1h4_to_zmm1 (c);
1119
1120 gfmul_pclmul_avx512 (); /* H<<<1•H⁓ => H⁵, …, H⁓<<<1•H⁓ => H⁸ */
1121
1122 asm volatile ("vmovdqu64 %%zmm1, 4*16(%[h_table])\n\t" /* store H⁸ for aggr16 setup */
1123 "vmovdqu64 %%zmm1, 3*16(%[h_table])\n\t"
1124 :
1125 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1126 : "memory");
1127
1128 gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 3 * 16); /* H⁵ <<< 1, …, H⁸ <<< 1 */
1129 }
1130
1131 static ASM_FUNC_ATTR void
1132 ghash_setup_aggr16_avx512(gcry_cipher_hd_t c)
1133 {
1134 c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_AGGR16_TABLE_INITIALIZED;
1135
1136 asm volatile (/* load H⁸ */
1137 "vbroadcasti64x2 7*16(%[h_table]), %%zmm0\n\t"
1138 :
1139 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1140 : "memory");
1141 /* load H <<< 1, H² <<< 1, H³ <<< 1, H⁓ <<< 1 */
1142 load_h1h4_to_zmm1 (c);
1143
1144 gfmul_pclmul_avx512 (); /* H<<<1•H⁸ => H⁹, … , H⁓<<<1•H⁸ => H¹² */
1145
1146 asm volatile ("vmovdqu64 %%zmm1, 7*16(%[h_table])\n\t"
1147 /* load H⁵ <<< 1, …, H⁸ <<< 1 */
1148 "vmovdqu64 3*16(%[h_table]), %%zmm1\n\t"
1149 :
1150 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1151 : "memory");
1152
1153 gfmul_pclmul_avx512 (); /* H⁵<<<1•H⁸ => H¹¹, … , H⁸<<<1•H⁸ => H¹⁶ */
1154
1155 asm volatile ("vmovdqu64 %%zmm1, 12*16(%[h_table])\n\t" /* store H¹⁶ for aggr32 setup */
1156 "vmovdqu64 %%zmm1, 11*16(%[h_table])\n\t"
1157 :
1158 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1159 : "memory");
1160
1161 gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 7 * 16); /* H⁹ <<< 1, …, H¹² <<< 1 */
1162 gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 11 * 16); /* H¹³ <<< 1, …, H¹⁶ <<< 1 */
1163 }
1164
1165 static ASM_FUNC_ATTR void
1166 ghash_setup_aggr32_avx512(gcry_cipher_hd_t c)
1167 {
1168 c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_AGGR32_TABLE_INITIALIZED;
1169
1170 asm volatile (/* load H¹⁶ */
1171 "vbroadcasti64x2 15*16(%[h_table]), %%zmm0\n\t"
1172 :
1173 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1174 : "memory");
1175 /* load H <<< 1, H² <<< 1, H³ <<< 1, H⁓ <<< 1 */
1176 load_h1h4_to_zmm1 (c);
1177
1178 gfmul_pclmul_avx512 (); /* H<<<1•H¹⁶ => H¹⁷, …, H⁓<<<1•H¹⁶ => H²⁰ */
1179
1180 asm volatile ("vmovdqu64 %%zmm1, 15*16(%[h_table])\n\t"
1181 /* load H⁵ <<< 1, …, H⁸ <<< 1 */
1182 "vmovdqu64 3*16(%[h_table]), %%zmm1\n\t"
1183 :
1184 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1185 : "memory");
1186
1187 gfmul_pclmul_avx512 (); /* H⁵<<<1•H¹⁶ => H²¹, …, H⁹<<<1•H¹⁶ => H²⁓ */
1188
1189 asm volatile ("vmovdqu64 %%zmm1, 19*16(%[h_table])\n\t"
1190 /* load H⁹ <<< 1, …, H¹² <<< 1 */
1191 "vmovdqu64 7*16(%[h_table]), %%zmm1\n\t"
1192 :
1193 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1194 : "memory");
1195
1196 gfmul_pclmul_avx512 (); /* H⁹<<<1•H¹⁶ => H²⁵, …, H¹²<<<1•H¹⁶ => H²⁸ */
1197
1198 asm volatile ("vmovdqu64 %%zmm1, 23*16(%[h_table])\n\t"
1199 /* load H¹³ <<< 1, …, H¹⁶ <<< 1 */
1200 "vmovdqu64 11*16(%[h_table]), %%zmm1\n\t"
1201 :
1202 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1203 : "memory");
1204
1205 gfmul_pclmul_avx512 (); /* H¹³<<<1•H¹⁶ => H²⁹, …, H¹⁶<<<1•H¹⁶ => H³² */
1206
1207 asm volatile ("vmovdqu64 %%zmm1, 27*16(%[h_table])\n\t"
1208 :
1209 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1210 : "memory");
1211
1212 gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 15 * 16);
1213 gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 19 * 16);
1214 gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 23 * 16);
1215 gcm_lsh_avx512 (c->u_mode.gcm.gcm_table, 27 * 16);
1216 }
1217
1218 static const u64 swap128b_perm[8] __attribute__ ((aligned (64))) =
1219 {
1220 /* For swapping order of 128bit lanes in 512bit register using vpermq. */
1221 6, 7, 4, 5, 2, 3, 0, 1
1222 };
1223
1224 #endif /* GCM_USE_INTEL_VPCLMUL_AVX512 */
1225 #endif /* __x86_64__ */
1226
1227 static unsigned int ASM_FUNC_ATTR
1228 _gcry_ghash_intel_pclmul (gcry_cipher_hd_t c, byte *result, const byte *buf,
1229 size_t nblocks);
1230
1231 static unsigned int ASM_FUNC_ATTR
1232 _gcry_polyval_intel_pclmul (gcry_cipher_hd_t c, byte *result, const byte *buf,
1233 size_t nblocks);
1234
1235 static ASM_FUNC_ATTR_INLINE void
1236 gcm_lsh(void *h, unsigned int hoffs)
1237 {
1238 static const u64 pconst[2] __attribute__ ((aligned (16))) =
1239 { U64_C(0x0000000000000001), U64_C(0xc200000000000000) };
1240
1241 asm volatile ("movdqu %[h], %%xmm2\n\t"
1242 "pshufd $0xff, %%xmm2, %%xmm3\n\t"
1243 "movdqa %%xmm2, %%xmm4\n\t"
1244 "psrad $31, %%xmm3\n\t"
1245 "pslldq $8, %%xmm4\n\t"
1246 "pand %[pconst], %%xmm3\n\t"
1247 "paddq %%xmm2, %%xmm2\n\t"
1248 "psrlq $63, %%xmm4\n\t"
1249 "pxor %%xmm3, %%xmm2\n\t"
1250 "pxor %%xmm4, %%xmm2\n\t"
1251 "movdqu %%xmm2, %[h]\n\t"
1252 : [h] "+m" (*((byte *)h + hoffs))
1253 : [pconst] "m" (*pconst)
1254 : "memory" );
1255 }
1256
1257 void ASM_FUNC_ATTR
1258 _gcry_ghash_setup_intel_pclmul (gcry_cipher_hd_t c, unsigned int hw_features)
1259 {
1260 static const unsigned char be_mask[16] __attribute__ ((aligned (16))) =
1261 { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
1262 #if defined(__x86_64__) && defined(__WIN64__)
1263 char win64tmp[10 * 16];
1264
1265 /* XMM6-XMM15 need to be restored after use. */
1266 asm volatile ("movdqu %%xmm6, 0*16(%0)\n\t"
1267 "movdqu %%xmm7, 1*16(%0)\n\t"
1268 "movdqu %%xmm8, 2*16(%0)\n\t"
1269 "movdqu %%xmm9, 3*16(%0)\n\t"
1270 "movdqu %%xmm10, 4*16(%0)\n\t"
1271 "movdqu %%xmm11, 5*16(%0)\n\t"
1272 "movdqu %%xmm12, 6*16(%0)\n\t"
1273 "movdqu %%xmm13, 7*16(%0)\n\t"
1274 "movdqu %%xmm14, 8*16(%0)\n\t"
1275 "movdqu %%xmm15, 9*16(%0)\n\t"
1276 :
1277 : "r" (win64tmp)
1278 : "memory" );
1279 #endif
1280
1281 (void)hw_features;
1282
1283 c->u_mode.gcm.hw_impl_flags = 0;
1284 c->u_mode.gcm.ghash_fn = _gcry_ghash_intel_pclmul;
1285 c->u_mode.gcm.polyval_fn = _gcry_polyval_intel_pclmul;
1286
1287 /* Swap endianness of hsub. */
1288 asm volatile ("movdqu (%[key]), %%xmm0\n\t"
1289 "pshufb %[be_mask], %%xmm0\n\t"
1290 "movdqu %%xmm0, (%[key])\n\t"
1291 :
1292 : [key] "r" (c->u_mode.gcm.u_ghash_key.key),
1293 [be_mask] "m" (*be_mask)
1294 : "memory");
1295
1296 gcm_lsh (c->u_mode.gcm.u_ghash_key.key, 0); /* H <<< 1 */
1297
1298 asm volatile ("movdqa %%xmm0, %%xmm1\n\t"
1299 "movdqu (%[key]), %%xmm0\n\t" /* load H <<< 1 */
1300 :
1301 : [key] "r" (c->u_mode.gcm.u_ghash_key.key)
1302 : "memory");
1303
1304 gfmul_pclmul (); /* H<<<1•H => H² */
1305
1306 asm volatile ("movdqu %%xmm1, 0*16(%[h_table])\n\t"
1307 :
1308 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1309 : "memory");
1310
1311 gcm_lsh (c->u_mode.gcm.gcm_table, 0 * 16); /* H² <<< 1 */
1312
1313 if (0)
1314 { }
1315 #ifdef GCM_USE_INTEL_VPCLMUL_AVX2
1316 else if ((hw_features & HWF_INTEL_VAES_VPCLMUL)
1317 && (hw_features & HWF_INTEL_AVX2))
1318 {
1319 c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_USE_VPCLMUL_AVX2;
1320
1321 #ifdef GCM_USE_INTEL_VPCLMUL_AVX512
1322 if (hw_features & HWF_INTEL_AVX512)
1323 c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_USE_VPCLMUL_AVX512;
1324 #endif
1325
1326 asm volatile (/* H² */
1327 "vinserti128 $1, %%xmm1, %%ymm1, %%ymm1\n\t"
1328 /* load H <<< 1, H² <<< 1 */
1329 "vinserti128 $1, 0*16(%[h_table]), %%ymm0, %%ymm0\n\t"
1330 :
1331 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1332 : "memory");
1333
1334 gfmul_pclmul_avx2 (); /* H<<<1•H² => H³, H²<<<1•H² => H⁓ */
1335
1336 asm volatile ("vmovdqu %%ymm1, 2*16(%[h_table])\n\t" /* store H⁓ for aggr8 setup */
1337 "vmovdqu %%ymm1, 1*16(%[h_table])\n\t"
1338 :
1339 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1340 : "memory");
1341
1342 gcm_lsh_avx2 (c->u_mode.gcm.gcm_table, 1 * 16); /* H³ <<< 1, H⁓ <<< 1 */
1343
1344 asm volatile ("vzeroupper\n\t"
1345 ::: "memory" );
1346 }
1347 #endif /* GCM_USE_INTEL_VPCLMUL_AVX2 */
1348 else
1349 {
1350 asm volatile ("movdqa %%xmm1, %%xmm7\n\t"
1351 ::: "memory");
1352
1353 gfmul_pclmul (); /* H<<<1•H² => H³ */
1354
1355 asm volatile ("movdqa %%xmm7, %%xmm0\n\t"
1356 "movdqu %%xmm1, 1*16(%[h_table])\n\t"
1357 "movdqu 0*16(%[h_table]), %%xmm1\n\t" /* load H² <<< 1 */
1358 :
1359 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1360 : "memory");
1361
1362 gfmul_pclmul (); /* H²<<<1•H² => H⁓ */
1363
1364 asm volatile ("movdqu %%xmm1, 3*16(%[h_table])\n\t" /* store H⁓ for aggr8 setup */
1365 "movdqu %%xmm1, 2*16(%[h_table])\n\t"
1366 :
1367 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1368 : "memory");
1369
1370 gcm_lsh (c->u_mode.gcm.gcm_table, 1 * 16); /* H³ <<< 1 */
1371 gcm_lsh (c->u_mode.gcm.gcm_table, 2 * 16); /* H⁓ <<< 1 */
1372 }
1373
1374 /* Clear/restore used registers. */
1375 asm volatile ("pxor %%xmm0, %%xmm0\n\t"
1376 "pxor %%xmm1, %%xmm1\n\t"
1377 "pxor %%xmm2, %%xmm2\n\t"
1378 "pxor %%xmm3, %%xmm3\n\t"
1379 "pxor %%xmm4, %%xmm4\n\t"
1380 "pxor %%xmm5, %%xmm5\n\t"
1381 "pxor %%xmm6, %%xmm6\n\t"
1382 "pxor %%xmm7, %%xmm7\n\t"
1383 ::: "memory" );
1384 #ifdef __x86_64__
1385 #ifdef __WIN64__
1386 asm volatile ("movdqu 0*16(%0), %%xmm6\n\t"
1387 "movdqu 1*16(%0), %%xmm7\n\t"
1388 "movdqu 2*16(%0), %%xmm8\n\t"
1389 "movdqu 3*16(%0), %%xmm9\n\t"
1390 "movdqu 4*16(%0), %%xmm10\n\t"
1391 "movdqu 5*16(%0), %%xmm11\n\t"
1392 "movdqu 6*16(%0), %%xmm12\n\t"
1393 "movdqu 7*16(%0), %%xmm13\n\t"
1394 "movdqu 8*16(%0), %%xmm14\n\t"
1395 "movdqu 9*16(%0), %%xmm15\n\t"
1396 :
1397 : "r" (win64tmp)
1398 : "memory" );
1399 #else
1400 asm volatile ("pxor %%xmm8, %%xmm8\n\t"
1401 "pxor %%xmm9, %%xmm9\n\t"
1402 "pxor %%xmm10, %%xmm10\n\t"
1403 "pxor %%xmm11, %%xmm11\n\t"
1404 "pxor %%xmm12, %%xmm12\n\t"
1405 "pxor %%xmm13, %%xmm13\n\t"
1406 "pxor %%xmm14, %%xmm14\n\t"
1407 "pxor %%xmm15, %%xmm15\n\t"
1408 ::: "memory" );
1409 #endif /* __WIN64__ */
1410 #endif /* __x86_64__ */
1411 }
1412
1413
1414 #ifdef __x86_64__
1415 static ASM_FUNC_ATTR void
1416 ghash_setup_aggr8(gcry_cipher_hd_t c)
1417 {
1418 c->u_mode.gcm.hw_impl_flags |= GCM_INTEL_AGGR8_TABLE_INITIALIZED;
1419
1420 asm volatile ("movdqa 3*16(%[h_table]), %%xmm0\n\t" /* load H⁓ */
1421 "movdqu %[key], %%xmm1\n\t" /* load H <<< 1 */
1422 :
1423 : [h_table] "r" (c->u_mode.gcm.gcm_table),
1424 [key] "m" (*c->u_mode.gcm.u_ghash_key.key)
1425 : "memory");
1426
1427 gfmul_pclmul (); /* H<<<1•H⁓ => H⁵ */
1428
1429 asm volatile ("movdqu %%xmm1, 3*16(%[h_table])\n\t"
1430 "movdqu 0*16(%[h_table]), %%xmm1\n\t" /* load H² <<< 1 */
1431 :
1432 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1433 : "memory");
1434
1435 gfmul_pclmul (); /* H²<<<1•H⁓ => H⁶ */
1436
1437 asm volatile ("movdqu %%xmm1, 4*16(%[h_table])\n\t"
1438 "movdqu 1*16(%[h_table]), %%xmm1\n\t" /* load H³ <<< 1 */
1439 :
1440 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1441 : "memory");
1442
1443 gfmul_pclmul (); /* H³<<<1•H⁓ => H⁷ */
1444
1445 asm volatile ("movdqu %%xmm1, 5*16(%[h_table])\n\t"
1446 "movdqu 2*16(%[h_table]), %%xmm1\n\t" /* load H⁓ <<< 1 */
1447 :
1448 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1449 : "memory");
1450
1451 gfmul_pclmul (); /* H⁓<<<1•H⁓ => H⁸ */
1452
1453 asm volatile ("movdqu %%xmm1, 6*16(%[h_table])\n\t"
1454 "movdqu %%xmm1, 7*16(%[h_table])\n\t" /* store H⁸ for aggr16 setup */
1455 :
1456 : [h_table] "r" (c->u_mode.gcm.gcm_table)
1457 : "memory");
1458
1459 gcm_lsh (c->u_mode.gcm.gcm_table, 3 * 16); /* H⁵ <<< 1 */
1460 gcm_lsh (c->u_mode.gcm.gcm_table, 4 * 16); /* H⁶ <<< 1 */
1461 gcm_lsh (c->u_mode.gcm.gcm_table, 5 * 16); /* H⁷ <<< 1 */
1462 gcm_lsh (c->u_mode.gcm.gcm_table, 6 * 16); /* H⁸ <<< 1 */
1463 }
1464 #endif /* __x86_64__ */
1465
1466
1467 unsigned int ASM_FUNC_ATTR
1468 _gcry_ghash_intel_pclmul (gcry_cipher_hd_t c, byte *result, const byte *buf,
1469 size_t nblocks)
1470 {
1471 static const unsigned char be_mask[16] __attribute__ ((aligned (16))) =
1472 { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
1473 const unsigned int blocksize = GCRY_GCM_BLOCK_LEN;
1474 #if defined(__x86_64__) && defined(__WIN64__)
1475 char win64tmp[10 * 16];
1476 #endif
1477
1478 if (nblocks == 0)
1479 return 0;
1480
1481 #if defined(__x86_64__) && defined(__WIN64__)
1482 /* XMM6-XMM15 need to be restored after use. */
1483 asm volatile ("movdqu %%xmm6, 0*16(%0)\n\t"
1484 "movdqu %%xmm7, 1*16(%0)\n\t"
1485 "movdqu %%xmm8, 2*16(%0)\n\t"
1486 "movdqu %%xmm9, 3*16(%0)\n\t"
1487 "movdqu %%xmm10, 4*16(%0)\n\t"
1488 "movdqu %%xmm11, 5*16(%0)\n\t"
1489 "movdqu %%xmm12, 6*16(%0)\n\t"
1490 "movdqu %%xmm13, 7*16(%0)\n\t"
1491 "movdqu %%xmm14, 8*16(%0)\n\t"
1492 "movdqu %%xmm15, 9*16(%0)\n\t"
1493 :
1494 : "r" (win64tmp)
1495 : "memory" );
1496 #endif
1497
1498 /* Preload hash. */
1499 asm volatile ("movdqa %[be_mask], %%xmm7\n\t"
1500 "movdqu %[hash], %%xmm1\n\t"
1501 "pshufb %%xmm7, %%xmm1\n\t" /* be => le */
1502 :
1503 : [hash] "m" (*result),
1504 [be_mask] "m" (*be_mask)
1505 : "memory" );
1506
1507 #if defined(GCM_USE_INTEL_VPCLMUL_AVX2)
1508 if (nblocks >= 16
1509 && ((c->u_mode.gcm.hw_impl_flags & GCM_INTEL_USE_VPCLMUL_AVX2)
1510 || (c->u_mode.gcm.hw_impl_flags & GCM_INTEL_USE_VPCLMUL_AVX512)))
1511 {
1512 #if defined(GCM_USE_INTEL_VPCLMUL_AVX512)
1513 if (nblocks >= 32
1514 && (c->u_mode.gcm.hw_impl_flags & GCM_INTEL_USE_VPCLMUL_AVX512))
1515 {
1516 asm volatile ("vpopcntb %%xmm7, %%xmm16\n\t" /* spec stop for old AVX512 CPUs */
1517 "vshufi64x2 $0, %%zmm7, %%zmm7, %%zmm15\n\t"
1518 "vmovdqa %%xmm1, %%xmm8\n\t"
1519 "vmovdqu64 %[swapperm], %%zmm14\n\t"
1520 :
1521 : [swapperm] "m" (swap128b_perm),
1522 [h_table] "r" (c->u_mode.gcm.gcm_table)
1523 : "memory" );
1524
1525 if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR32_TABLE_INITIALIZED))
1526 {
1527 if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR16_TABLE_INITIALIZED))
1528 {
1529 if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR8_TABLE_INITIALIZED))
1530 ghash_setup_aggr8_avx512 (c); /* Clobbers registers XMM0-XMM7. */
1531
1532 ghash_setup_aggr16_avx512 (c); /* Clobbers registers XMM0-XMM7. */
1533 }
1534
1535 ghash_setup_aggr32_avx512 (c); /* Clobbers registers XMM0-XMM7. */
1536 }
1537
1538 /* Preload H1-H32. */
1539 load_h1h4_to_zmm1 (c);
1540 asm volatile ("vpermq %%zmm1, %%zmm14, %%zmm16\n\t" /* H1|H2|H3|H4 */
1541 "vmovdqa %%xmm8, %%xmm1\n\t"
1542 "vpermq 27*16(%[h_table]), %%zmm14, %%zmm0\n\t" /* H28|H29|H31|H32 */
1543 "vpermq 23*16(%[h_table]), %%zmm14, %%zmm13\n\t" /* H25|H26|H27|H28 */
1544 "vpermq 19*16(%[h_table]), %%zmm14, %%zmm12\n\t" /* H21|H22|H23|H24 */
1545 "vpermq 15*16(%[h_table]), %%zmm14, %%zmm11\n\t" /* H17|H18|H19|H20 */
1546 "vpermq 11*16(%[h_table]), %%zmm14, %%zmm10\n\t" /* H13|H14|H15|H16 */
1547 "vpermq 7*16(%[h_table]), %%zmm14, %%zmm9\n\t" /* H9|H10|H11|H12 */
1548 "vpermq 3*16(%[h_table]), %%zmm14, %%zmm8\n\t" /* H4|H6|H7|H8 */
1549 :
1550 : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key),
1551 [h_table] "r" (c->u_mode.gcm.gcm_table)
1552 : "memory" );
1553
1554 while (nblocks >= 32)
1555 {
1556 gfmul_vpclmul_avx512_aggr32 (buf, c->u_mode.gcm.gcm_table);
1557
1558 buf += 32 * blocksize;
1559 nblocks -= 32;
1560 }
1561
1562 asm volatile ("vmovdqa %%xmm15, %%xmm7\n\t"
1563 "vpxorq %%ymm16, %%ymm16, %%ymm16\n\t"
1564 "vpxorq %%ymm17, %%ymm17, %%ymm17\n\t"
1565 "vpxorq %%ymm18, %%ymm18, %%ymm18\n\t"
1566 "vpxorq %%ymm19, %%ymm19, %%ymm19\n\t"
1567 :
1568 :
1569 : "memory" );
1570 }
1571 #endif /* GCM_USE_INTEL_VPCLMUL_AVX512 */
1572
1573 if (nblocks >= 16)
1574 {
1575 u64 h1_h2_h15_h16[4*2];
1576
1577 asm volatile ("vinserti128 $1, %%xmm7, %%ymm7, %%ymm15\n\t"
1578 "vmovdqa %%xmm1, %%xmm8\n\t"
1579 ::: "memory" );
1580
1581 if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR16_TABLE_INITIALIZED))
1582 {
1583 if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR8_TABLE_INITIALIZED))
1584 ghash_setup_aggr8_avx2 (c); /* Clobbers registers XMM0-XMM7. */
1585
1586 ghash_setup_aggr16_avx2 (c); /* Clobbers registers XMM0-XMM7. */
1587 }
1588
1589 /* Preload H1-H16. */
1590 load_h1h2_to_ymm1 (c);
1591 asm volatile ("vperm2i128 $0x23, %%ymm1, %%ymm1, %%ymm7\n\t" /* H1|H2 */
1592 "vmovdqa %%xmm8, %%xmm1\n\t"
1593 "vpxor %%xmm8, %%xmm8, %%xmm8\n\t"
1594 "vperm2i128 $0x23, 13*16(%[h_table]), %%ymm8, %%ymm0\n\t" /* H15|H16 */
1595 "vperm2i128 $0x23, 11*16(%[h_table]), %%ymm8, %%ymm13\n\t" /* H13|H14 */
1596 "vperm2i128 $0x23, 9*16(%[h_table]), %%ymm8, %%ymm12\n\t" /* H11|H12 */
1597 "vperm2i128 $0x23, 7*16(%[h_table]), %%ymm8, %%ymm11\n\t" /* H9|H10 */
1598 "vperm2i128 $0x23, 5*16(%[h_table]), %%ymm8, %%ymm10\n\t" /* H7|H8 */
1599 "vperm2i128 $0x23, 3*16(%[h_table]), %%ymm8, %%ymm9\n\t" /* H5|H6 */
1600 "vperm2i128 $0x23, 1*16(%[h_table]), %%ymm8, %%ymm8\n\t" /* H3|H4 */
1601 "vmovdqu %%ymm0, %[h15_h16]\n\t"
1602 "vmovdqu %%ymm7, %[h1_h2]\n\t"
1603 : [h1_h2] "=m" (h1_h2_h15_h16[0]),
1604 [h15_h16] "=m" (h1_h2_h15_h16[4])
1605 : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key),
1606 [h_table] "r" (c->u_mode.gcm.gcm_table)
1607 : "memory" );
1608
1609 while (nblocks >= 16)
1610 {
1611 gfmul_vpclmul_avx2_aggr16 (buf, c->u_mode.gcm.gcm_table,
1612 h1_h2_h15_h16);
1613
1614 buf += 16 * blocksize;
1615 nblocks -= 16;
1616 }
1617
1618 asm volatile ("vmovdqu %%ymm15, %[h15_h16]\n\t"
1619 "vmovdqu %%ymm15, %[h1_h2]\n\t"
1620 "vmovdqa %%xmm15, %%xmm7\n\t"
1621 :
1622 [h1_h2] "=m" (h1_h2_h15_h16[0]),
1623 [h15_h16] "=m" (h1_h2_h15_h16[4])
1624 :
1625 : "memory" );
1626 }
1627
1628 asm volatile ("vzeroupper\n\t" ::: "memory" );
1629 }
1630 #endif /* GCM_USE_INTEL_VPCLMUL_AVX2 */
1631
1632 #ifdef __x86_64__
1633 if (nblocks >= 8)
1634 {
1635 asm volatile ("movdqa %%xmm7, %%xmm15\n\t"
1636 "movdqa %%xmm1, %%xmm8\n\t"
1637 ::: "memory" );
1638
1639 if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR8_TABLE_INITIALIZED))
1640 ghash_setup_aggr8 (c); /* Clobbers registers XMM0-XMM7. */
1641
1642 /* Preload H1. */
1643 asm volatile ("movdqa %%xmm8, %%xmm1\n\t"
1644 "movdqa %[h_1], %%xmm0\n\t"
1645 :
1646 : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key)
1647 : "memory" );
1648
1649 while (nblocks >= 8)
1650 {
1651 gfmul_pclmul_aggr8 (buf, c->u_mode.gcm.gcm_table);
1652
1653 buf += 8 * blocksize;
1654 nblocks -= 8;
1655 }
1656 }
1657 #endif /* __x86_64__ */
1658
1659 while (nblocks >= 4)
1660 {
1661 gfmul_pclmul_aggr4 (buf, c->u_mode.gcm.u_ghash_key.key,
1662 c->u_mode.gcm.gcm_table, be_mask);
1663
1664 buf += 4 * blocksize;
1665 nblocks -= 4;
1666 }
1667
1668 if (nblocks)
1669 {
1670 /* Preload H1. */
1671 asm volatile ("movdqa %[h_1], %%xmm0\n\t"
1672 :
1673 : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key)
1674 : "memory" );
1675
1676 while (nblocks)
1677 {
1678 asm volatile ("movdqu %[buf], %%xmm2\n\t"
1679 "pshufb %[be_mask], %%xmm2\n\t" /* be => le */
1680 "pxor %%xmm2, %%xmm1\n\t"
1681 :
1682 : [buf] "m" (*buf), [be_mask] "m" (*be_mask)
1683 : "memory" );
1684
1685 gfmul_pclmul ();
1686
1687 buf += blocksize;
1688 nblocks--;
1689 }
1690 }
1691
1692 /* Store hash. */
1693 asm volatile ("pshufb %[be_mask], %%xmm1\n\t" /* be => le */
1694 "movdqu %%xmm1, %[hash]\n\t"
1695 : [hash] "=m" (*result)
1696 : [be_mask] "m" (*be_mask)
1697 : "memory" );
1698
1699 /* Clear/restore used registers. */
1700 asm volatile ("pxor %%xmm0, %%xmm0\n\t"
1701 "pxor %%xmm1, %%xmm1\n\t"
1702 "pxor %%xmm2, %%xmm2\n\t"
1703 "pxor %%xmm3, %%xmm3\n\t"
1704 "pxor %%xmm4, %%xmm4\n\t"
1705 "pxor %%xmm5, %%xmm5\n\t"
1706 "pxor %%xmm6, %%xmm6\n\t"
1707 "pxor %%xmm7, %%xmm7\n\t"
1708 :
1709 :
1710 : "memory" );
1711 #ifdef __x86_64__
1712 #ifdef __WIN64__
1713 asm volatile ("movdqu 0*16(%0), %%xmm6\n\t"
1714 "movdqu 1*16(%0), %%xmm7\n\t"
1715 "movdqu 2*16(%0), %%xmm8\n\t"
1716 "movdqu 3*16(%0), %%xmm9\n\t"
1717 "movdqu 4*16(%0), %%xmm10\n\t"
1718 "movdqu 5*16(%0), %%xmm11\n\t"
1719 "movdqu 6*16(%0), %%xmm12\n\t"
1720 "movdqu 7*16(%0), %%xmm13\n\t"
1721 "movdqu 8*16(%0), %%xmm14\n\t"
1722 "movdqu 9*16(%0), %%xmm15\n\t"
1723 :
1724 : "r" (win64tmp)
1725 : "memory" );
1726 #else
1727 /* Clear used registers. */
1728 asm volatile (
1729 "pxor %%xmm8, %%xmm8\n\t"
1730 "pxor %%xmm9, %%xmm9\n\t"
1731 "pxor %%xmm10, %%xmm10\n\t"
1732 "pxor %%xmm11, %%xmm11\n\t"
1733 "pxor %%xmm12, %%xmm12\n\t"
1734 "pxor %%xmm13, %%xmm13\n\t"
1735 "pxor %%xmm14, %%xmm14\n\t"
1736 "pxor %%xmm15, %%xmm15\n\t"
1737 :
1738 :
1739 : "memory" );
1740 #endif /* __WIN64__ */
1741 #endif /* __x86_64__ */
1742
1743 return 0;
1744 }
1745
1746 unsigned int ASM_FUNC_ATTR
1747 _gcry_polyval_intel_pclmul (gcry_cipher_hd_t c, byte *result, const byte *buf,
1748 size_t nblocks)
1749 {
1750 static const unsigned char be_mask[16] __attribute__ ((aligned (16))) =
1751 { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
1752 const unsigned int blocksize = GCRY_GCM_BLOCK_LEN;
1753 #if defined(__x86_64__) && defined(__WIN64__)
1754 char win64tmp[10 * 16];
1755 #endif
1756
1757 if (nblocks == 0)
1758 return 0;
1759
1760 #if defined(__x86_64__) && defined(__WIN64__)
1761 /* XMM6-XMM15 need to be restored after use. */
1762 asm volatile ("movdqu %%xmm6, 0*16(%0)\n\t"
1763 "movdqu %%xmm7, 1*16(%0)\n\t"
1764 "movdqu %%xmm8, 2*16(%0)\n\t"
1765 "movdqu %%xmm9, 3*16(%0)\n\t"
1766 "movdqu %%xmm10, 4*16(%0)\n\t"
1767 "movdqu %%xmm11, 5*16(%0)\n\t"
1768 "movdqu %%xmm12, 6*16(%0)\n\t"
1769 "movdqu %%xmm13, 7*16(%0)\n\t"
1770 "movdqu %%xmm14, 8*16(%0)\n\t"
1771 "movdqu %%xmm15, 9*16(%0)\n\t"
1772 :
1773 : "r" (win64tmp)
1774 : "memory" );
1775 #endif
1776
1777 /* Preload hash. */
1778 asm volatile ("pxor %%xmm7, %%xmm7\n\t"
1779 "movdqu %[hash], %%xmm1\n\t"
1780 "pshufb %[be_mask], %%xmm1\n\t" /* be => le */
1781 :
1782 : [hash] "m" (*result),
1783 [be_mask] "m" (*be_mask)
1784 : "memory" );
1785
1786 #if defined(GCM_USE_INTEL_VPCLMUL_AVX2)
1787 if (nblocks >= 16
1788 && ((c->u_mode.gcm.hw_impl_flags & GCM_INTEL_USE_VPCLMUL_AVX2)
1789 || (c->u_mode.gcm.hw_impl_flags & GCM_INTEL_USE_VPCLMUL_AVX512)))
1790 {
1791 #if defined(GCM_USE_INTEL_VPCLMUL_AVX512)
1792 if (nblocks >= 32
1793 && (c->u_mode.gcm.hw_impl_flags & GCM_INTEL_USE_VPCLMUL_AVX512))
1794 {
1795 asm volatile ("vpopcntb %%xmm1, %%xmm16\n\t" /* spec stop for old AVX512 CPUs */
1796 "vmovdqa %%xmm1, %%xmm8\n\t"
1797 "vmovdqu64 %[swapperm], %%zmm14\n\t"
1798 :
1799 : [swapperm] "m" (swap128b_perm),
1800 [h_table] "r" (c->u_mode.gcm.gcm_table)
1801 : "memory" );
1802
1803 if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR32_TABLE_INITIALIZED))
1804 {
1805 if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR16_TABLE_INITIALIZED))
1806 {
1807 if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR8_TABLE_INITIALIZED))
1808 ghash_setup_aggr8_avx512 (c); /* Clobbers registers XMM0-XMM7. */
1809
1810 ghash_setup_aggr16_avx512 (c); /* Clobbers registers XMM0-XMM7. */
1811 }
1812
1813 ghash_setup_aggr32_avx512 (c); /* Clobbers registers XMM0-XMM7. */
1814 }
1815
1816 /* Preload H1-H32. */
1817 load_h1h4_to_zmm1 (c);
1818 asm volatile ("vpermq %%zmm1, %%zmm14, %%zmm16\n\t" /* H1|H2|H3|H4 */
1819 "vmovdqa %%xmm8, %%xmm1\n\t"
1820 "vpermq 27*16(%[h_table]), %%zmm14, %%zmm0\n\t" /* H28|H29|H31|H32 */
1821 "vpermq 23*16(%[h_table]), %%zmm14, %%zmm13\n\t" /* H25|H26|H27|H28 */
1822 "vpermq 19*16(%[h_table]), %%zmm14, %%zmm12\n\t" /* H21|H22|H23|H24 */
1823 "vpermq 15*16(%[h_table]), %%zmm14, %%zmm11\n\t" /* H17|H18|H19|H20 */
1824 "vpermq 11*16(%[h_table]), %%zmm14, %%zmm10\n\t" /* H13|H14|H15|H16 */
1825 "vpermq 7*16(%[h_table]), %%zmm14, %%zmm9\n\t" /* H9|H10|H11|H12 */
1826 "vpermq 3*16(%[h_table]), %%zmm14, %%zmm8\n\t" /* H4|H6|H7|H8 */
1827 :
1828 : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key),
1829 [h_table] "r" (c->u_mode.gcm.gcm_table)
1830 : "memory" );
1831
1832 while (nblocks >= 32)
1833 {
1834 gfmul_vpclmul_avx512_aggr32_le (buf, c->u_mode.gcm.gcm_table);
1835
1836 buf += 32 * blocksize;
1837 nblocks -= 32;
1838 }
1839
1840 asm volatile ("vpxor %%xmm7, %%xmm7, %%xmm7\n\t"
1841 "vpxorq %%ymm16, %%ymm16, %%ymm16\n\t"
1842 "vpxorq %%ymm17, %%ymm17, %%ymm17\n\t"
1843 "vpxorq %%ymm18, %%ymm18, %%ymm18\n\t"
1844 "vpxorq %%ymm19, %%ymm19, %%ymm19\n\t"
1845 :
1846 :
1847 : "memory" );
1848 }
1849 #endif /* GCM_USE_INTEL_VPCLMUL_AVX512 */
1850
1851 if (nblocks >= 16)
1852 {
1853 u64 h1_h2_h15_h16[4*2];
1854
1855 asm volatile ("vmovdqa %%xmm1, %%xmm8\n\t"
1856 ::: "memory" );
1857
1858 if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR16_TABLE_INITIALIZED))
1859 {
1860 if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR8_TABLE_INITIALIZED))
1861 ghash_setup_aggr8_avx2 (c); /* Clobbers registers XMM0-XMM7. */
1862
1863 ghash_setup_aggr16_avx2 (c); /* Clobbers registers XMM0-XMM7. */
1864 }
1865
1866 /* Preload H1-H16. */
1867 load_h1h2_to_ymm1 (c);
1868 asm volatile ("vperm2i128 $0x23, %%ymm1, %%ymm1, %%ymm7\n\t" /* H1|H2 */
1869 "vmovdqa %%xmm8, %%xmm1\n\t"
1870 "vpxor %%xmm8, %%xmm8, %%xmm8\n\t"
1871 "vperm2i128 $0x23, 13*16(%[h_table]), %%ymm8, %%ymm0\n\t" /* H15|H16 */
1872 "vperm2i128 $0x23, 11*16(%[h_table]), %%ymm8, %%ymm13\n\t" /* H13|H14 */
1873 "vperm2i128 $0x23, 9*16(%[h_table]), %%ymm8, %%ymm12\n\t" /* H11|H12 */
1874 "vperm2i128 $0x23, 7*16(%[h_table]), %%ymm8, %%ymm11\n\t" /* H9|H10 */
1875 "vperm2i128 $0x23, 5*16(%[h_table]), %%ymm8, %%ymm10\n\t" /* H7|H8 */
1876 "vperm2i128 $0x23, 3*16(%[h_table]), %%ymm8, %%ymm9\n\t" /* H5|H6 */
1877 "vperm2i128 $0x23, 1*16(%[h_table]), %%ymm8, %%ymm8\n\t" /* H3|H4 */
1878 "vmovdqu %%ymm0, %[h15_h16]\n\t"
1879 "vmovdqu %%ymm7, %[h1_h2]\n\t"
1880 : [h1_h2] "=m" (h1_h2_h15_h16[0]),
1881 [h15_h16] "=m" (h1_h2_h15_h16[4])
1882 : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key),
1883 [h_table] "r" (c->u_mode.gcm.gcm_table)
1884 : "memory" );
1885
1886 while (nblocks >= 16)
1887 {
1888 gfmul_vpclmul_avx2_aggr16_le (buf, c->u_mode.gcm.gcm_table,
1889 h1_h2_h15_h16);
1890
1891 buf += 16 * blocksize;
1892 nblocks -= 16;
1893 }
1894
1895 asm volatile ("vpxor %%xmm7, %%xmm7, %%xmm7\n\t"
1896 "vmovdqu %%ymm7, %[h15_h16]\n\t"
1897 "vmovdqu %%ymm7, %[h1_h2]\n\t"
1898 : [h1_h2] "=m" (h1_h2_h15_h16[0]),
1899 [h15_h16] "=m" (h1_h2_h15_h16[4])
1900 :
1901 : "memory" );
1902 }
1903
1904 asm volatile ("vzeroupper\n\t" ::: "memory" );
1905 }
1906 #endif /* GCM_USE_INTEL_VPCLMUL_AVX2 */
1907
1908 #ifdef __x86_64__
1909 if (nblocks >= 8)
1910 {
1911 asm volatile ("movdqa %%xmm1, %%xmm8\n\t"
1912 ::: "memory" );
1913
1914 if (!(c->u_mode.gcm.hw_impl_flags & GCM_INTEL_AGGR8_TABLE_INITIALIZED))
1915 ghash_setup_aggr8 (c); /* Clobbers registers XMM0-XMM7. */
1916
1917 /* Preload H1. */
1918 asm volatile ("movdqa %%xmm8, %%xmm1\n\t"
1919 "pxor %%xmm15, %%xmm15\n\t"
1920 "movdqa %[h_1], %%xmm0\n\t"
1921 :
1922 : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key)
1923 : "memory" );
1924
1925 while (nblocks >= 8)
1926 {
1927 gfmul_pclmul_aggr8_le (buf, c->u_mode.gcm.gcm_table);
1928
1929 buf += 8 * blocksize;
1930 nblocks -= 8;
1931 }
1932 }
1933 #endif
1934
1935 while (nblocks >= 4)
1936 {
1937 gfmul_pclmul_aggr4_le (buf, c->u_mode.gcm.u_ghash_key.key,
1938 c->u_mode.gcm.gcm_table);
1939
1940 buf += 4 * blocksize;
1941 nblocks -= 4;
1942 }
1943
1944 if (nblocks)
1945 {
1946 /* Preload H1. */
1947 asm volatile ("movdqa %[h_1], %%xmm0\n\t"
1948 :
1949 : [h_1] "m" (*c->u_mode.gcm.u_ghash_key.key)
1950 : "memory" );
1951
1952 while (nblocks)
1953 {
1954 asm volatile ("movdqu %[buf], %%xmm2\n\t"
1955 "pxor %%xmm2, %%xmm1\n\t"
1956 :
1957 : [buf] "m" (*buf)
1958 : "memory" );
1959
1960 gfmul_pclmul ();
1961
1962 buf += blocksize;
1963 nblocks--;
1964 }
1965 }
1966
1967 /* Store hash. */
1968 asm volatile ("pshufb %[be_mask], %%xmm1\n\t" /* be => le */
1969 "movdqu %%xmm1, %[hash]\n\t"
1970 : [hash] "=m" (*result)
1971 : [be_mask] "m" (*be_mask)
1972 : "memory" );
1973
1974 /* Clear/restore used registers. */
1975 asm volatile ("pxor %%xmm0, %%xmm0\n\t"
1976 "pxor %%xmm1, %%xmm1\n\t"
1977 "pxor %%xmm2, %%xmm2\n\t"
1978 "pxor %%xmm3, %%xmm3\n\t"
1979 "pxor %%xmm4, %%xmm4\n\t"
1980 "pxor %%xmm5, %%xmm5\n\t"
1981 "pxor %%xmm6, %%xmm6\n\t"
1982 "pxor %%xmm7, %%xmm7\n\t"
1983 :
1984 :
1985 : "memory" );
1986 #ifdef __x86_64__
1987 #ifdef __WIN64__
1988 asm volatile ("movdqu 0*16(%0), %%xmm6\n\t"
1989 "movdqu 1*16(%0), %%xmm7\n\t"
1990 "movdqu 2*16(%0), %%xmm8\n\t"
1991 "movdqu 3*16(%0), %%xmm9\n\t"
1992 "movdqu 4*16(%0), %%xmm10\n\t"
1993 "movdqu 5*16(%0), %%xmm11\n\t"
1994 "movdqu 6*16(%0), %%xmm12\n\t"
1995 "movdqu 7*16(%0), %%xmm13\n\t"
1996 "movdqu 8*16(%0), %%xmm14\n\t"
1997 "movdqu 9*16(%0), %%xmm15\n\t"
1998 :
1999 : "r" (win64tmp)
2000 : "memory" );
2001 #else
2002 /* Clear used registers. */
2003 asm volatile (
2004 "pxor %%xmm8, %%xmm8\n\t"
2005 "pxor %%xmm9, %%xmm9\n\t"
2006 "pxor %%xmm10, %%xmm10\n\t"
2007 "pxor %%xmm11, %%xmm11\n\t"
2008 "pxor %%xmm12, %%xmm12\n\t"
2009 "pxor %%xmm13, %%xmm13\n\t"
2010 "pxor %%xmm14, %%xmm14\n\t"
2011 "pxor %%xmm15, %%xmm15\n\t"
2012 :
2013 :
2014 : "memory" );
2015 #endif /* __WIN64__ */
2016 #endif /* __x86_64__ */
2017
2018 return 0;
2019 }
2020
2021 #if __clang__
2022 # pragma clang attribute pop
2023 #endif
2024
2025 #endif /* GCM_USE_INTEL_PCLMUL */