Debian Codegraph @

cipher/serpent-sse2-amd64.S

raw
1 /* serpent-sse2-amd64.S - SSE2 implementation of Serpent cipher
2 *
3 * Copyright (C) 2013-2015 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 #ifdef __x86_64
22 #include <config.h>
23 #if (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
24 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)) && defined(USE_SERPENT)
25
26 #include "asm-common-amd64.h"
27
28 /* struct serpent_context: */
29 #define ctx_keys 0
30
31 /* register macros */
32 #define CTX %rdi
33
34 /* vector registers */
35 #define RA0 %xmm0
36 #define RA1 %xmm1
37 #define RA2 %xmm2
38 #define RA3 %xmm3
39 #define RA4 %xmm4
40
41 #define RB0 %xmm5
42 #define RB1 %xmm6
43 #define RB2 %xmm7
44 #define RB3 %xmm8
45 #define RB4 %xmm9
46
47 #define RNOT %xmm10
48 #define RTMP0 %xmm11
49 #define RTMP1 %xmm12
50 #define RTMP2 %xmm13
51
52 /**********************************************************************
53 helper macros
54 **********************************************************************/
55
56 /* vector 32-bit rotation to left */
57 #define vec_rol(reg, nleft, tmp) \
58 movdqa reg, tmp; \
59 pslld $(nleft), tmp; \
60 psrld $(32 - (nleft)), reg; \
61 por tmp, reg;
62
63 /* vector 32-bit rotation to right */
64 #define vec_ror(reg, nright, tmp) \
65 vec_rol(reg, 32 - nright, tmp)
66
67 /* 4x4 32-bit integer matrix transpose */
68 #define transpose_4x4(x0, x1, x2, x3, t1, t2, t3) \
69 movdqa x0, t2; \
70 punpckhdq x1, t2; \
71 punpckldq x1, x0; \
72 \
73 movdqa x2, t1; \
74 punpckldq x3, t1; \
75 punpckhdq x3, x2; \
76 \
77 movdqa x0, x1; \
78 punpckhqdq t1, x1; \
79 punpcklqdq t1, x0; \
80 \
81 movdqa t2, x3; \
82 punpckhqdq x2, x3; \
83 punpcklqdq x2, t2; \
84 movdqa t2, x2;
85
86 /* fill xmm register with 32-bit value from memory */
87 #define pbroadcastd(mem32, xreg) \
88 movd mem32, xreg; \
89 pshufd $0, xreg, xreg;
90
91 /* xor with unaligned memory operand */
92 #define pxor_u(umem128, xreg, t) \
93 movdqu umem128, t; \
94 pxor t, xreg;
95
96 /* 128-bit wide byte swap */
97 #define pbswap(xreg, t0) \
98 /* reorder 32-bit words, [a,b,c,d] => [d,c,b,a] */ \
99 pshufd $0x1b, xreg, xreg; \
100 /* reorder high&low 16-bit words, [d0,d1,c0,c1] => [d1,d0,c1,c0] */ \
101 pshuflw $0xb1, xreg, xreg; \
102 pshufhw $0xb1, xreg, xreg; \
103 /* reorder bytes in 16-bit words */ \
104 movdqa xreg, t0; \
105 psrlw $8, t0; \
106 psllw $8, xreg; \
107 por t0, xreg;
108
109 /**********************************************************************
110 8-way serpent
111 **********************************************************************/
112
113 /*
114 * These are the S-Boxes of Serpent from following research paper.
115 *
116 * D. A. Osvik, “Speeding up Serpent,” in Third AES Candidate Conference,
117 * (New York, New York, USA), p. 317–329, National Institute of Standards and
118 * Technology, 2000.
119 *
120 * Paper is also available at: http://www.ii.uib.no/~osvik/pub/aes3.pdf
121 *
122 */
123 #define SBOX0(r0, r1, r2, r3, r4) \
124 pxor r0, r3; movdqa r1, r4; \
125 pand r3, r1; pxor r2, r4; \
126 pxor r0, r1; por r3, r0; \
127 pxor r4, r0; pxor r3, r4; \
128 pxor r2, r3; por r1, r2; \
129 pxor r4, r2; pxor RNOT, r4; \
130 por r1, r4; pxor r3, r1; \
131 pxor r4, r1; por r0, r3; \
132 pxor r3, r1; pxor r3, r4;
133
134 #define SBOX0_INVERSE(r0, r1, r2, r3, r4) \
135 pxor RNOT, r2; movdqa r1, r4; \
136 por r0, r1; pxor RNOT, r4; \
137 pxor r2, r1; por r4, r2; \
138 pxor r3, r1; pxor r4, r0; \
139 pxor r0, r2; pand r3, r0; \
140 pxor r0, r4; por r1, r0; \
141 pxor r2, r0; pxor r4, r3; \
142 pxor r1, r2; pxor r0, r3; \
143 pxor r1, r3; \
144 pand r3, r2; \
145 pxor r2, r4;
146
147 #define SBOX1(r0, r1, r2, r3, r4) \
148 pxor RNOT, r0; pxor RNOT, r2; \
149 movdqa r0, r4; pand r1, r0; \
150 pxor r0, r2; por r3, r0; \
151 pxor r2, r3; pxor r0, r1; \
152 pxor r4, r0; por r1, r4; \
153 pxor r3, r1; por r0, r2; \
154 pand r4, r2; pxor r1, r0; \
155 pand r2, r1; \
156 pxor r0, r1; pand r2, r0; \
157 pxor r4, r0;
158
159 #define SBOX1_INVERSE(r0, r1, r2, r3, r4) \
160 movdqa r1, r4; pxor r3, r1; \
161 pand r1, r3; pxor r2, r4; \
162 pxor r0, r3; por r1, r0; \
163 pxor r3, r2; pxor r4, r0; \
164 por r2, r0; pxor r3, r1; \
165 pxor r1, r0; por r3, r1; \
166 pxor r0, r1; pxor RNOT, r4; \
167 pxor r1, r4; por r0, r1; \
168 pxor r0, r1; \
169 por r4, r1; \
170 pxor r1, r3;
171
172 #define SBOX2(r0, r1, r2, r3, r4) \
173 movdqa r0, r4; pand r2, r0; \
174 pxor r3, r0; pxor r1, r2; \
175 pxor r0, r2; por r4, r3; \
176 pxor r1, r3; pxor r2, r4; \
177 movdqa r3, r1; por r4, r3; \
178 pxor r0, r3; pand r1, r0; \
179 pxor r0, r4; pxor r3, r1; \
180 pxor r4, r1; pxor RNOT, r4;
181
182 #define SBOX2_INVERSE(r0, r1, r2, r3, r4) \
183 pxor r3, r2; pxor r0, r3; \
184 movdqa r3, r4; pand r2, r3; \
185 pxor r1, r3; por r2, r1; \
186 pxor r4, r1; pand r3, r4; \
187 pxor r3, r2; pand r0, r4; \
188 pxor r2, r4; pand r1, r2; \
189 por r0, r2; pxor RNOT, r3; \
190 pxor r3, r2; pxor r3, r0; \
191 pand r1, r0; pxor r4, r3; \
192 pxor r0, r3;
193
194 #define SBOX3(r0, r1, r2, r3, r4) \
195 movdqa r0, r4; por r3, r0; \
196 pxor r1, r3; pand r4, r1; \
197 pxor r2, r4; pxor r3, r2; \
198 pand r0, r3; por r1, r4; \
199 pxor r4, r3; pxor r1, r0; \
200 pand r0, r4; pxor r3, r1; \
201 pxor r2, r4; por r0, r1; \
202 pxor r2, r1; pxor r3, r0; \
203 movdqa r1, r2; por r3, r1; \
204 pxor r0, r1;
205
206 #define SBOX3_INVERSE(r0, r1, r2, r3, r4) \
207 movdqa r2, r4; pxor r1, r2; \
208 pxor r2, r0; pand r2, r4; \
209 pxor r0, r4; pand r1, r0; \
210 pxor r3, r1; por r4, r3; \
211 pxor r3, r2; pxor r3, r0; \
212 pxor r4, r1; pand r2, r3; \
213 pxor r1, r3; pxor r0, r1; \
214 por r2, r1; pxor r3, r0; \
215 pxor r4, r1; \
216 pxor r1, r0;
217
218 #define SBOX4(r0, r1, r2, r3, r4) \
219 pxor r3, r1; pxor RNOT, r3; \
220 pxor r3, r2; pxor r0, r3; \
221 movdqa r1, r4; pand r3, r1; \
222 pxor r2, r1; pxor r3, r4; \
223 pxor r4, r0; pand r4, r2; \
224 pxor r0, r2; pand r1, r0; \
225 pxor r0, r3; por r1, r4; \
226 pxor r0, r4; por r3, r0; \
227 pxor r2, r0; pand r3, r2; \
228 pxor RNOT, r0; pxor r2, r4;
229
230 #define SBOX4_INVERSE(r0, r1, r2, r3, r4) \
231 movdqa r2, r4; pand r3, r2; \
232 pxor r1, r2; por r3, r1; \
233 pand r0, r1; pxor r2, r4; \
234 pxor r1, r4; pand r2, r1; \
235 pxor RNOT, r0; pxor r4, r3; \
236 pxor r3, r1; pand r0, r3; \
237 pxor r2, r3; pxor r1, r0; \
238 pand r0, r2; pxor r0, r3; \
239 pxor r4, r2; \
240 por r3, r2; pxor r0, r3; \
241 pxor r1, r2;
242
243 #define SBOX5(r0, r1, r2, r3, r4) \
244 pxor r1, r0; pxor r3, r1; \
245 pxor RNOT, r3; movdqa r1, r4; \
246 pand r0, r1; pxor r3, r2; \
247 pxor r2, r1; por r4, r2; \
248 pxor r3, r4; pand r1, r3; \
249 pxor r0, r3; pxor r1, r4; \
250 pxor r2, r4; pxor r0, r2; \
251 pand r3, r0; pxor RNOT, r2; \
252 pxor r4, r0; por r3, r4; \
253 pxor r4, r2;
254
255 #define SBOX5_INVERSE(r0, r1, r2, r3, r4) \
256 pxor RNOT, r1; movdqa r3, r4; \
257 pxor r1, r2; por r0, r3; \
258 pxor r2, r3; por r1, r2; \
259 pand r0, r2; pxor r3, r4; \
260 pxor r4, r2; por r0, r4; \
261 pxor r1, r4; pand r2, r1; \
262 pxor r3, r1; pxor r2, r4; \
263 pand r4, r3; pxor r1, r4; \
264 pxor r4, r3; pxor RNOT, r4; \
265 pxor r0, r3;
266
267 #define SBOX6(r0, r1, r2, r3, r4) \
268 pxor RNOT, r2; movdqa r3, r4; \
269 pand r0, r3; pxor r4, r0; \
270 pxor r2, r3; por r4, r2; \
271 pxor r3, r1; pxor r0, r2; \
272 por r1, r0; pxor r1, r2; \
273 pxor r0, r4; por r3, r0; \
274 pxor r2, r0; pxor r3, r4; \
275 pxor r0, r4; pxor RNOT, r3; \
276 pand r4, r2; \
277 pxor r3, r2;
278
279 #define SBOX6_INVERSE(r0, r1, r2, r3, r4) \
280 pxor r2, r0; movdqa r2, r4; \
281 pand r0, r2; pxor r3, r4; \
282 pxor RNOT, r2; pxor r1, r3; \
283 pxor r3, r2; por r0, r4; \
284 pxor r2, r0; pxor r4, r3; \
285 pxor r1, r4; pand r3, r1; \
286 pxor r0, r1; pxor r3, r0; \
287 por r2, r0; pxor r1, r3; \
288 pxor r0, r4;
289
290 #define SBOX7(r0, r1, r2, r3, r4) \
291 movdqa r1, r4; por r2, r1; \
292 pxor r3, r1; pxor r2, r4; \
293 pxor r1, r2; por r4, r3; \
294 pand r0, r3; pxor r2, r4; \
295 pxor r1, r3; por r4, r1; \
296 pxor r0, r1; por r4, r0; \
297 pxor r2, r0; pxor r4, r1; \
298 pxor r1, r2; pand r0, r1; \
299 pxor r4, r1; pxor RNOT, r2; \
300 por r0, r2; \
301 pxor r2, r4;
302
303 #define SBOX7_INVERSE(r0, r1, r2, r3, r4) \
304 movdqa r2, r4; pxor r0, r2; \
305 pand r3, r0; por r3, r4; \
306 pxor RNOT, r2; pxor r1, r3; \
307 por r0, r1; pxor r2, r0; \
308 pand r4, r2; pand r4, r3; \
309 pxor r2, r1; pxor r0, r2; \
310 por r2, r0; pxor r1, r4; \
311 pxor r3, r0; pxor r4, r3; \
312 por r0, r4; pxor r2, r3; \
313 pxor r2, r4;
314
315 /* Apply SBOX number WHICH to to the block. */
316 #define SBOX(which, r0, r1, r2, r3, r4) \
317 SBOX##which (r0, r1, r2, r3, r4)
318
319 /* Apply inverse SBOX number WHICH to to the block. */
320 #define SBOX_INVERSE(which, r0, r1, r2, r3, r4) \
321 SBOX##which##_INVERSE (r0, r1, r2, r3, r4)
322
323 /* XOR round key into block state in r0,r1,r2,r3. r4 used as temporary. */
324 #define BLOCK_XOR_KEY(r0, r1, r2, r3, r4, round) \
325 pbroadcastd ((ctx_keys + (round) * 16 + 0 * 4)(CTX), r4); \
326 pxor r4, r0; \
327 pbroadcastd ((ctx_keys + (round) * 16 + 1 * 4)(CTX), r4); \
328 pxor r4, r1; \
329 pbroadcastd ((ctx_keys + (round) * 16 + 2 * 4)(CTX), r4); \
330 pxor r4, r2; \
331 pbroadcastd ((ctx_keys + (round) * 16 + 3 * 4)(CTX), r4); \
332 pxor r4, r3;
333
334 /* Apply the linear transformation to BLOCK. */
335 #define LINEAR_TRANSFORMATION(r0, r1, r2, r3, r4) \
336 vec_rol(r0, 13, r4); \
337 vec_rol(r2, 3, r4); \
338 pxor r0, r1; \
339 pxor r2, r1; \
340 movdqa r0, r4; \
341 pslld $3, r4; \
342 pxor r2, r3; \
343 pxor r4, r3; \
344 vec_rol(r1, 1, r4); \
345 vec_rol(r3, 7, r4); \
346 pxor r1, r0; \
347 pxor r3, r0; \
348 movdqa r1, r4; \
349 pslld $7, r4; \
350 pxor r3, r2; \
351 pxor r4, r2; \
352 vec_rol(r0, 5, r4); \
353 vec_rol(r2, 22, r4);
354
355 /* Apply the inverse linear transformation to BLOCK. */
356 #define LINEAR_TRANSFORMATION_INVERSE(r0, r1, r2, r3, r4) \
357 vec_ror(r2, 22, r4); \
358 vec_ror(r0, 5, r4); \
359 movdqa r1, r4; \
360 pslld $7, r4; \
361 pxor r3, r2; \
362 pxor r4, r2; \
363 pxor r1, r0; \
364 pxor r3, r0; \
365 vec_ror(r3, 7, r4); \
366 vec_ror(r1, 1, r4); \
367 movdqa r0, r4; \
368 pslld $3, r4; \
369 pxor r2, r3; \
370 pxor r4, r3; \
371 pxor r0, r1; \
372 pxor r2, r1; \
373 vec_ror(r2, 3, r4); \
374 vec_ror(r0, 13, r4);
375
376 /* Apply a Serpent round to eight parallel blocks. This macro increments
377 `round'. */
378 #define ROUND(round, which, a0, a1, a2, a3, a4, na0, na1, na2, na3, na4, \
379 b0, b1, b2, b3, b4, nb0, nb1, nb2, nb3, nb4) \
380 BLOCK_XOR_KEY (a0, a1, a2, a3, a4, round); \
381 SBOX (which, a0, a1, a2, a3, a4); \
382 BLOCK_XOR_KEY (b0, b1, b2, b3, b4, round); \
383 SBOX (which, b0, b1, b2, b3, b4); \
384 LINEAR_TRANSFORMATION (na0, na1, na2, na3, na4); \
385 LINEAR_TRANSFORMATION (nb0, nb1, nb2, nb3, nb4);
386
387 /* Apply the last Serpent round to eight parallel blocks. This macro increments
388 `round'. */
389 #define ROUND_LAST(round, which, a0, a1, a2, a3, a4, na0, na1, na2, na3, na4, \
390 b0, b1, b2, b3, b4, nb0, nb1, nb2, nb3, nb4) \
391 BLOCK_XOR_KEY (a0, a1, a2, a3, a4, round); \
392 SBOX (which, a0, a1, a2, a3, a4); \
393 BLOCK_XOR_KEY (b0, b1, b2, b3, b4, round); \
394 SBOX (which, b0, b1, b2, b3, b4); \
395 BLOCK_XOR_KEY (na0, na1, na2, na3, na4, ((round) + 1)); \
396 BLOCK_XOR_KEY (nb0, nb1, nb2, nb3, nb4, ((round) + 1));
397
398 /* Apply an inverse Serpent round to eight parallel blocks. This macro
399 increments `round'. */
400 #define ROUND_INVERSE(round, which, a0, a1, a2, a3, a4, \
401 na0, na1, na2, na3, na4, \
402 b0, b1, b2, b3, b4, \
403 nb0, nb1, nb2, nb3, nb4) \
404 LINEAR_TRANSFORMATION_INVERSE (a0, a1, a2, a3, a4); \
405 LINEAR_TRANSFORMATION_INVERSE (b0, b1, b2, b3, b4); \
406 SBOX_INVERSE (which, a0, a1, a2, a3, a4); \
407 BLOCK_XOR_KEY (na0, na1, na2, na3, na4, round); \
408 SBOX_INVERSE (which, b0, b1, b2, b3, b4); \
409 BLOCK_XOR_KEY (nb0, nb1, nb2, nb3, nb4, round);
410
411 /* Apply the first inverse Serpent round to eight parallel blocks. This macro
412 increments `round'. */
413 #define ROUND_FIRST_INVERSE(round, which, a0, a1, a2, a3, a4, \
414 na0, na1, na2, na3, na4, \
415 b0, b1, b2, b3, b4, \
416 nb0, nb1, nb2, nb3, nb4) \
417 BLOCK_XOR_KEY (a0, a1, a2, a3, a4, ((round) + 1)); \
418 BLOCK_XOR_KEY (b0, b1, b2, b3, b4, ((round) + 1)); \
419 SBOX_INVERSE (which, a0, a1, a2, a3, a4); \
420 BLOCK_XOR_KEY (na0, na1, na2, na3, na4, round); \
421 SBOX_INVERSE (which, b0, b1, b2, b3, b4); \
422 BLOCK_XOR_KEY (nb0, nb1, nb2, nb3, nb4, round);
423
424 .text
425
426 .align 16
427 ELF(.type __serpent_enc_blk8,@function;)
428 __serpent_enc_blk8:
429 /* input:
430 * %rdi: ctx, CTX
431 * RA0, RA1, RA2, RA3, RB0, RB1, RB2, RB3: eight parallel plaintext
432 * blocks
433 * output:
434 * RA4, RA1, RA2, RA0, RB4, RB1, RB2, RB0: eight parallel
435 * ciphertext blocks
436 */
437 CFI_STARTPROC();
438
439 pcmpeqd RNOT, RNOT;
440
441 transpose_4x4(RA0, RA1, RA2, RA3, RA4, RTMP0, RTMP1);
442 transpose_4x4(RB0, RB1, RB2, RB3, RB4, RTMP0, RTMP1);
443
444 ROUND (0, 0, RA0, RA1, RA2, RA3, RA4, RA1, RA4, RA2, RA0, RA3,
445 RB0, RB1, RB2, RB3, RB4, RB1, RB4, RB2, RB0, RB3);
446 ROUND (1, 1, RA1, RA4, RA2, RA0, RA3, RA2, RA1, RA0, RA4, RA3,
447 RB1, RB4, RB2, RB0, RB3, RB2, RB1, RB0, RB4, RB3);
448 ROUND (2, 2, RA2, RA1, RA0, RA4, RA3, RA0, RA4, RA1, RA3, RA2,
449 RB2, RB1, RB0, RB4, RB3, RB0, RB4, RB1, RB3, RB2);
450 ROUND (3, 3, RA0, RA4, RA1, RA3, RA2, RA4, RA1, RA3, RA2, RA0,
451 RB0, RB4, RB1, RB3, RB2, RB4, RB1, RB3, RB2, RB0);
452 ROUND (4, 4, RA4, RA1, RA3, RA2, RA0, RA1, RA0, RA4, RA2, RA3,
453 RB4, RB1, RB3, RB2, RB0, RB1, RB0, RB4, RB2, RB3);
454 ROUND (5, 5, RA1, RA0, RA4, RA2, RA3, RA0, RA2, RA1, RA4, RA3,
455 RB1, RB0, RB4, RB2, RB3, RB0, RB2, RB1, RB4, RB3);
456 ROUND (6, 6, RA0, RA2, RA1, RA4, RA3, RA0, RA2, RA3, RA1, RA4,
457 RB0, RB2, RB1, RB4, RB3, RB0, RB2, RB3, RB1, RB4);
458 ROUND (7, 7, RA0, RA2, RA3, RA1, RA4, RA4, RA1, RA2, RA0, RA3,
459 RB0, RB2, RB3, RB1, RB4, RB4, RB1, RB2, RB0, RB3);
460 ROUND (8, 0, RA4, RA1, RA2, RA0, RA3, RA1, RA3, RA2, RA4, RA0,
461 RB4, RB1, RB2, RB0, RB3, RB1, RB3, RB2, RB4, RB0);
462 ROUND (9, 1, RA1, RA3, RA2, RA4, RA0, RA2, RA1, RA4, RA3, RA0,
463 RB1, RB3, RB2, RB4, RB0, RB2, RB1, RB4, RB3, RB0);
464 ROUND (10, 2, RA2, RA1, RA4, RA3, RA0, RA4, RA3, RA1, RA0, RA2,
465 RB2, RB1, RB4, RB3, RB0, RB4, RB3, RB1, RB0, RB2);
466 ROUND (11, 3, RA4, RA3, RA1, RA0, RA2, RA3, RA1, RA0, RA2, RA4,
467 RB4, RB3, RB1, RB0, RB2, RB3, RB1, RB0, RB2, RB4);
468 ROUND (12, 4, RA3, RA1, RA0, RA2, RA4, RA1, RA4, RA3, RA2, RA0,
469 RB3, RB1, RB0, RB2, RB4, RB1, RB4, RB3, RB2, RB0);
470 ROUND (13, 5, RA1, RA4, RA3, RA2, RA0, RA4, RA2, RA1, RA3, RA0,
471 RB1, RB4, RB3, RB2, RB0, RB4, RB2, RB1, RB3, RB0);
472 ROUND (14, 6, RA4, RA2, RA1, RA3, RA0, RA4, RA2, RA0, RA1, RA3,
473 RB4, RB2, RB1, RB3, RB0, RB4, RB2, RB0, RB1, RB3);
474 ROUND (15, 7, RA4, RA2, RA0, RA1, RA3, RA3, RA1, RA2, RA4, RA0,
475 RB4, RB2, RB0, RB1, RB3, RB3, RB1, RB2, RB4, RB0);
476 ROUND (16, 0, RA3, RA1, RA2, RA4, RA0, RA1, RA0, RA2, RA3, RA4,
477 RB3, RB1, RB2, RB4, RB0, RB1, RB0, RB2, RB3, RB4);
478 ROUND (17, 1, RA1, RA0, RA2, RA3, RA4, RA2, RA1, RA3, RA0, RA4,
479 RB1, RB0, RB2, RB3, RB4, RB2, RB1, RB3, RB0, RB4);
480 ROUND (18, 2, RA2, RA1, RA3, RA0, RA4, RA3, RA0, RA1, RA4, RA2,
481 RB2, RB1, RB3, RB0, RB4, RB3, RB0, RB1, RB4, RB2);
482 ROUND (19, 3, RA3, RA0, RA1, RA4, RA2, RA0, RA1, RA4, RA2, RA3,
483 RB3, RB0, RB1, RB4, RB2, RB0, RB1, RB4, RB2, RB3);
484 ROUND (20, 4, RA0, RA1, RA4, RA2, RA3, RA1, RA3, RA0, RA2, RA4,
485 RB0, RB1, RB4, RB2, RB3, RB1, RB3, RB0, RB2, RB4);
486 ROUND (21, 5, RA1, RA3, RA0, RA2, RA4, RA3, RA2, RA1, RA0, RA4,
487 RB1, RB3, RB0, RB2, RB4, RB3, RB2, RB1, RB0, RB4);
488 ROUND (22, 6, RA3, RA2, RA1, RA0, RA4, RA3, RA2, RA4, RA1, RA0,
489 RB3, RB2, RB1, RB0, RB4, RB3, RB2, RB4, RB1, RB0);
490 ROUND (23, 7, RA3, RA2, RA4, RA1, RA0, RA0, RA1, RA2, RA3, RA4,
491 RB3, RB2, RB4, RB1, RB0, RB0, RB1, RB2, RB3, RB4);
492 ROUND (24, 0, RA0, RA1, RA2, RA3, RA4, RA1, RA4, RA2, RA0, RA3,
493 RB0, RB1, RB2, RB3, RB4, RB1, RB4, RB2, RB0, RB3);
494 ROUND (25, 1, RA1, RA4, RA2, RA0, RA3, RA2, RA1, RA0, RA4, RA3,
495 RB1, RB4, RB2, RB0, RB3, RB2, RB1, RB0, RB4, RB3);
496 ROUND (26, 2, RA2, RA1, RA0, RA4, RA3, RA0, RA4, RA1, RA3, RA2,
497 RB2, RB1, RB0, RB4, RB3, RB0, RB4, RB1, RB3, RB2);
498 ROUND (27, 3, RA0, RA4, RA1, RA3, RA2, RA4, RA1, RA3, RA2, RA0,
499 RB0, RB4, RB1, RB3, RB2, RB4, RB1, RB3, RB2, RB0);
500 ROUND (28, 4, RA4, RA1, RA3, RA2, RA0, RA1, RA0, RA4, RA2, RA3,
501 RB4, RB1, RB3, RB2, RB0, RB1, RB0, RB4, RB2, RB3);
502 ROUND (29, 5, RA1, RA0, RA4, RA2, RA3, RA0, RA2, RA1, RA4, RA3,
503 RB1, RB0, RB4, RB2, RB3, RB0, RB2, RB1, RB4, RB3);
504 ROUND (30, 6, RA0, RA2, RA1, RA4, RA3, RA0, RA2, RA3, RA1, RA4,
505 RB0, RB2, RB1, RB4, RB3, RB0, RB2, RB3, RB1, RB4);
506 ROUND_LAST (31, 7, RA0, RA2, RA3, RA1, RA4, RA4, RA1, RA2, RA0, RA3,
507 RB0, RB2, RB3, RB1, RB4, RB4, RB1, RB2, RB0, RB3);
508
509 transpose_4x4(RA4, RA1, RA2, RA0, RA3, RTMP0, RTMP1);
510 transpose_4x4(RB4, RB1, RB2, RB0, RB3, RTMP0, RTMP1);
511
512 ret_spec_stop;
513 CFI_ENDPROC();
514 ELF(.size __serpent_enc_blk8,.-__serpent_enc_blk8;)
515
516 .align 16
517 ELF(.type __serpent_dec_blk8,@function;)
518 __serpent_dec_blk8:
519 /* input:
520 * %rdi: ctx, CTX
521 * RA0, RA1, RA2, RA3, RB0, RB1, RB2, RB3: eight parallel
522 * ciphertext blocks
523 * output:
524 * RA0, RA1, RA2, RA3, RB0, RB1, RB2, RB3: eight parallel plaintext
525 * blocks
526 */
527 CFI_STARTPROC();
528
529 pcmpeqd RNOT, RNOT;
530
531 transpose_4x4(RA0, RA1, RA2, RA3, RA4, RTMP0, RTMP1);
532 transpose_4x4(RB0, RB1, RB2, RB3, RB4, RTMP0, RTMP1);
533
534 ROUND_FIRST_INVERSE (31, 7, RA0, RA1, RA2, RA3, RA4,
535 RA3, RA0, RA1, RA4, RA2,
536 RB0, RB1, RB2, RB3, RB4,
537 RB3, RB0, RB1, RB4, RB2);
538 ROUND_INVERSE (30, 6, RA3, RA0, RA1, RA4, RA2, RA0, RA1, RA2, RA4, RA3,
539 RB3, RB0, RB1, RB4, RB2, RB0, RB1, RB2, RB4, RB3);
540 ROUND_INVERSE (29, 5, RA0, RA1, RA2, RA4, RA3, RA1, RA3, RA4, RA2, RA0,
541 RB0, RB1, RB2, RB4, RB3, RB1, RB3, RB4, RB2, RB0);
542 ROUND_INVERSE (28, 4, RA1, RA3, RA4, RA2, RA0, RA1, RA2, RA4, RA0, RA3,
543 RB1, RB3, RB4, RB2, RB0, RB1, RB2, RB4, RB0, RB3);
544 ROUND_INVERSE (27, 3, RA1, RA2, RA4, RA0, RA3, RA4, RA2, RA0, RA1, RA3,
545 RB1, RB2, RB4, RB0, RB3, RB4, RB2, RB0, RB1, RB3);
546 ROUND_INVERSE (26, 2, RA4, RA2, RA0, RA1, RA3, RA2, RA3, RA0, RA1, RA4,
547 RB4, RB2, RB0, RB1, RB3, RB2, RB3, RB0, RB1, RB4);
548 ROUND_INVERSE (25, 1, RA2, RA3, RA0, RA1, RA4, RA4, RA2, RA1, RA0, RA3,
549 RB2, RB3, RB0, RB1, RB4, RB4, RB2, RB1, RB0, RB3);
550 ROUND_INVERSE (24, 0, RA4, RA2, RA1, RA0, RA3, RA4, RA3, RA2, RA0, RA1,
551 RB4, RB2, RB1, RB0, RB3, RB4, RB3, RB2, RB0, RB1);
552 ROUND_INVERSE (23, 7, RA4, RA3, RA2, RA0, RA1, RA0, RA4, RA3, RA1, RA2,
553 RB4, RB3, RB2, RB0, RB1, RB0, RB4, RB3, RB1, RB2);
554 ROUND_INVERSE (22, 6, RA0, RA4, RA3, RA1, RA2, RA4, RA3, RA2, RA1, RA0,
555 RB0, RB4, RB3, RB1, RB2, RB4, RB3, RB2, RB1, RB0);
556 ROUND_INVERSE (21, 5, RA4, RA3, RA2, RA1, RA0, RA3, RA0, RA1, RA2, RA4,
557 RB4, RB3, RB2, RB1, RB0, RB3, RB0, RB1, RB2, RB4);
558 ROUND_INVERSE (20, 4, RA3, RA0, RA1, RA2, RA4, RA3, RA2, RA1, RA4, RA0,
559 RB3, RB0, RB1, RB2, RB4, RB3, RB2, RB1, RB4, RB0);
560 ROUND_INVERSE (19, 3, RA3, RA2, RA1, RA4, RA0, RA1, RA2, RA4, RA3, RA0,
561 RB3, RB2, RB1, RB4, RB0, RB1, RB2, RB4, RB3, RB0);
562 ROUND_INVERSE (18, 2, RA1, RA2, RA4, RA3, RA0, RA2, RA0, RA4, RA3, RA1,
563 RB1, RB2, RB4, RB3, RB0, RB2, RB0, RB4, RB3, RB1);
564 ROUND_INVERSE (17, 1, RA2, RA0, RA4, RA3, RA1, RA1, RA2, RA3, RA4, RA0,
565 RB2, RB0, RB4, RB3, RB1, RB1, RB2, RB3, RB4, RB0);
566 ROUND_INVERSE (16, 0, RA1, RA2, RA3, RA4, RA0, RA1, RA0, RA2, RA4, RA3,
567 RB1, RB2, RB3, RB4, RB0, RB1, RB0, RB2, RB4, RB3);
568 ROUND_INVERSE (15, 7, RA1, RA0, RA2, RA4, RA3, RA4, RA1, RA0, RA3, RA2,
569 RB1, RB0, RB2, RB4, RB3, RB4, RB1, RB0, RB3, RB2);
570 ROUND_INVERSE (14, 6, RA4, RA1, RA0, RA3, RA2, RA1, RA0, RA2, RA3, RA4,
571 RB4, RB1, RB0, RB3, RB2, RB1, RB0, RB2, RB3, RB4);
572 ROUND_INVERSE (13, 5, RA1, RA0, RA2, RA3, RA4, RA0, RA4, RA3, RA2, RA1,
573 RB1, RB0, RB2, RB3, RB4, RB0, RB4, RB3, RB2, RB1);
574 ROUND_INVERSE (12, 4, RA0, RA4, RA3, RA2, RA1, RA0, RA2, RA3, RA1, RA4,
575 RB0, RB4, RB3, RB2, RB1, RB0, RB2, RB3, RB1, RB4);
576 ROUND_INVERSE (11, 3, RA0, RA2, RA3, RA1, RA4, RA3, RA2, RA1, RA0, RA4,
577 RB0, RB2, RB3, RB1, RB4, RB3, RB2, RB1, RB0, RB4);
578 ROUND_INVERSE (10, 2, RA3, RA2, RA1, RA0, RA4, RA2, RA4, RA1, RA0, RA3,
579 RB3, RB2, RB1, RB0, RB4, RB2, RB4, RB1, RB0, RB3);
580 ROUND_INVERSE (9, 1, RA2, RA4, RA1, RA0, RA3, RA3, RA2, RA0, RA1, RA4,
581 RB2, RB4, RB1, RB0, RB3, RB3, RB2, RB0, RB1, RB4);
582 ROUND_INVERSE (8, 0, RA3, RA2, RA0, RA1, RA4, RA3, RA4, RA2, RA1, RA0,
583 RB3, RB2, RB0, RB1, RB4, RB3, RB4, RB2, RB1, RB0);
584 ROUND_INVERSE (7, 7, RA3, RA4, RA2, RA1, RA0, RA1, RA3, RA4, RA0, RA2,
585 RB3, RB4, RB2, RB1, RB0, RB1, RB3, RB4, RB0, RB2);
586 ROUND_INVERSE (6, 6, RA1, RA3, RA4, RA0, RA2, RA3, RA4, RA2, RA0, RA1,
587 RB1, RB3, RB4, RB0, RB2, RB3, RB4, RB2, RB0, RB1);
588 ROUND_INVERSE (5, 5, RA3, RA4, RA2, RA0, RA1, RA4, RA1, RA0, RA2, RA3,
589 RB3, RB4, RB2, RB0, RB1, RB4, RB1, RB0, RB2, RB3);
590 ROUND_INVERSE (4, 4, RA4, RA1, RA0, RA2, RA3, RA4, RA2, RA0, RA3, RA1,
591 RB4, RB1, RB0, RB2, RB3, RB4, RB2, RB0, RB3, RB1);
592 ROUND_INVERSE (3, 3, RA4, RA2, RA0, RA3, RA1, RA0, RA2, RA3, RA4, RA1,
593 RB4, RB2, RB0, RB3, RB1, RB0, RB2, RB3, RB4, RB1);
594 ROUND_INVERSE (2, 2, RA0, RA2, RA3, RA4, RA1, RA2, RA1, RA3, RA4, RA0,
595 RB0, RB2, RB3, RB4, RB1, RB2, RB1, RB3, RB4, RB0);
596 ROUND_INVERSE (1, 1, RA2, RA1, RA3, RA4, RA0, RA0, RA2, RA4, RA3, RA1,
597 RB2, RB1, RB3, RB4, RB0, RB0, RB2, RB4, RB3, RB1);
598 ROUND_INVERSE (0, 0, RA0, RA2, RA4, RA3, RA1, RA0, RA1, RA2, RA3, RA4,
599 RB0, RB2, RB4, RB3, RB1, RB0, RB1, RB2, RB3, RB4);
600
601 transpose_4x4(RA0, RA1, RA2, RA3, RA4, RTMP0, RTMP1);
602 transpose_4x4(RB0, RB1, RB2, RB3, RB4, RTMP0, RTMP1);
603
604 ret_spec_stop;
605 CFI_ENDPROC();
606 ELF(.size __serpent_dec_blk8,.-__serpent_dec_blk8;)
607
608 .align 16
609 .globl _gcry_serpent_sse2_blk8
610 ELF(.type _gcry_serpent_sse2_blk8,@function;)
611 _gcry_serpent_sse2_blk8:
612 /* input:
613 * %rdi: ctx, CTX
614 * %rsi: dst (8 blocks)
615 * %rdx: src (8 blocks)
616 * %ecx: encrypt
617 */
618 CFI_STARTPROC();
619
620 movdqu (0 * 16)(%rdx), RA0;
621 movdqu (1 * 16)(%rdx), RA1;
622 movdqu (2 * 16)(%rdx), RA2;
623 movdqu (3 * 16)(%rdx), RA3;
624 movdqu (4 * 16)(%rdx), RB0;
625 movdqu (5 * 16)(%rdx), RB1;
626 movdqu (6 * 16)(%rdx), RB2;
627 movdqu (7 * 16)(%rdx), RB3;
628
629 testl %ecx, %ecx;
630 jz .Lblk8_dec;
631 call __serpent_enc_blk8;
632 movdqu RA4, (0 * 16)(%rsi);
633 movdqu RA1, (1 * 16)(%rsi);
634 movdqu RA2, (2 * 16)(%rsi);
635 movdqu RA0, (3 * 16)(%rsi);
636 movdqu RB4, (4 * 16)(%rsi);
637 movdqu RB1, (5 * 16)(%rsi);
638 movdqu RB2, (6 * 16)(%rsi);
639 movdqu RB0, (7 * 16)(%rsi);
640 jmp .Lblk8_end;
641 .Lblk8_dec:
642 call __serpent_dec_blk8;
643 movdqu RA0, (0 * 16)(%rsi);
644 movdqu RA1, (1 * 16)(%rsi);
645 movdqu RA2, (2 * 16)(%rsi);
646 movdqu RA3, (3 * 16)(%rsi);
647 movdqu RB0, (4 * 16)(%rsi);
648 movdqu RB1, (5 * 16)(%rsi);
649 movdqu RB2, (6 * 16)(%rsi);
650 movdqu RB3, (7 * 16)(%rsi);
651
652 .Lblk8_end:
653 /* clear the used registers */
654 pxor RA0, RA0;
655 pxor RA1, RA1;
656 pxor RA2, RA2;
657 pxor RA3, RA3;
658 pxor RA4, RA4;
659 pxor RB0, RB0;
660 pxor RB1, RB1;
661 pxor RB2, RB2;
662 pxor RB3, RB3;
663 pxor RB4, RB4;
664 pxor RTMP0, RTMP0;
665 pxor RTMP1, RTMP1;
666 pxor RTMP2, RTMP2;
667 pxor RNOT, RNOT;
668
669 ret_spec_stop;
670 CFI_ENDPROC();
671 ELF(.size _gcry_serpent_sse2_blk8,.-_gcry_serpent_sse2_blk8;)
672
673 .align 16
674 .globl _gcry_serpent_sse2_ctr_enc
675 ELF(.type _gcry_serpent_sse2_ctr_enc,@function;)
676 _gcry_serpent_sse2_ctr_enc:
677 /* input:
678 * %rdi: ctx, CTX
679 * %rsi: dst (8 blocks)
680 * %rdx: src (8 blocks)
681 * %rcx: iv (big endian, 128bit)
682 */
683 CFI_STARTPROC();
684
685 /* load IV and byteswap */
686 movdqu (%rcx), RA0;
687 movdqa RA0, RTMP0;
688 pbswap(RTMP0, RTMP1); /* be => le */
689
690 pcmpeqd RNOT, RNOT;
691 psrldq $8, RNOT; /* low: -1, high: 0 */
692 movdqa RNOT, RTMP2;
693 paddq RTMP2, RTMP2; /* low: -2, high: 0 */
694
695 /* construct IVs */
696 movdqa RTMP0, RTMP1;
697 psubq RNOT, RTMP0; /* +1 */
698 movdqa RTMP0, RA1;
699 psubq RTMP2, RTMP1; /* +2 */
700 movdqa RTMP1, RA2;
701 psubq RTMP2, RTMP0; /* +3 */
702 movdqa RTMP0, RA3;
703 psubq RTMP2, RTMP1; /* +4 */
704 movdqa RTMP1, RB0;
705 psubq RTMP2, RTMP0; /* +5 */
706 movdqa RTMP0, RB1;
707 psubq RTMP2, RTMP1; /* +6 */
708 movdqa RTMP1, RB2;
709 psubq RTMP2, RTMP0; /* +7 */
710 movdqa RTMP0, RB3;
711 psubq RTMP2, RTMP1; /* +8 */
712
713 /* check need for handling 64-bit overflow and carry */
714 cmpl $0xffffffff, 8(%rcx);
715 jne .Lno_ctr_carry;
716
717 movl 12(%rcx), %eax;
718 bswapl %eax;
719 cmpl $-8, %eax;
720 jb .Lno_ctr_carry;
721 pslldq $8, RNOT; /* low: 0, high: -1 */
722 je .Lcarry_RTMP0;
723
724 cmpl $-6, %eax;
725 jb .Lcarry_RB3;
726 je .Lcarry_RB2;
727
728 cmpl $-4, %eax;
729 jb .Lcarry_RB1;
730 je .Lcarry_RB0;
731
732 cmpl $-2, %eax;
733 jb .Lcarry_RA3;
734 je .Lcarry_RA2;
735
736 psubq RNOT, RA1;
737 .Lcarry_RA2:
738 psubq RNOT, RA2;
739 .Lcarry_RA3:
740 psubq RNOT, RA3;
741 .Lcarry_RB0:
742 psubq RNOT, RB0;
743 .Lcarry_RB1:
744 psubq RNOT, RB1;
745 .Lcarry_RB2:
746 psubq RNOT, RB2;
747 .Lcarry_RB3:
748 psubq RNOT, RB3;
749 .Lcarry_RTMP0:
750 psubq RNOT, RTMP1;
751
752 .Lno_ctr_carry:
753 /* le => be */
754 pbswap(RA1, RTMP0);
755 pbswap(RA2, RTMP0);
756 pbswap(RA3, RTMP0);
757 pbswap(RB0, RTMP0);
758 pbswap(RB1, RTMP0);
759 pbswap(RB2, RTMP0);
760 pbswap(RB3, RTMP0);
761 pbswap(RTMP1, RTMP0);
762 /* store new IV */
763 movdqu RTMP1, (%rcx);
764
765 call __serpent_enc_blk8;
766
767 pxor_u((0 * 16)(%rdx), RA4, RTMP0);
768 pxor_u((1 * 16)(%rdx), RA1, RTMP0);
769 pxor_u((2 * 16)(%rdx), RA2, RTMP0);
770 pxor_u((3 * 16)(%rdx), RA0, RTMP0);
771 pxor_u((4 * 16)(%rdx), RB4, RTMP0);
772 pxor_u((5 * 16)(%rdx), RB1, RTMP0);
773 pxor_u((6 * 16)(%rdx), RB2, RTMP0);
774 pxor_u((7 * 16)(%rdx), RB0, RTMP0);
775
776 movdqu RA4, (0 * 16)(%rsi);
777 movdqu RA1, (1 * 16)(%rsi);
778 movdqu RA2, (2 * 16)(%rsi);
779 movdqu RA0, (3 * 16)(%rsi);
780 movdqu RB4, (4 * 16)(%rsi);
781 movdqu RB1, (5 * 16)(%rsi);
782 movdqu RB2, (6 * 16)(%rsi);
783 movdqu RB0, (7 * 16)(%rsi);
784
785 /* clear the used registers */
786 pxor RA0, RA0;
787 pxor RA1, RA1;
788 pxor RA2, RA2;
789 pxor RA3, RA3;
790 pxor RA4, RA4;
791 pxor RB0, RB0;
792 pxor RB1, RB1;
793 pxor RB2, RB2;
794 pxor RB3, RB3;
795 pxor RB4, RB4;
796 pxor RTMP0, RTMP0;
797 pxor RTMP1, RTMP1;
798 pxor RTMP2, RTMP2;
799 pxor RNOT, RNOT;
800
801 ret_spec_stop;
802 CFI_ENDPROC();
803 ELF(.size _gcry_serpent_sse2_ctr_enc,.-_gcry_serpent_sse2_ctr_enc;)
804
805 .align 16
806 .globl _gcry_serpent_sse2_cbc_dec
807 ELF(.type _gcry_serpent_sse2_cbc_dec,@function;)
808 _gcry_serpent_sse2_cbc_dec:
809 /* input:
810 * %rdi: ctx, CTX
811 * %rsi: dst (8 blocks)
812 * %rdx: src (8 blocks)
813 * %rcx: iv
814 */
815 CFI_STARTPROC();
816
817 movdqu (0 * 16)(%rdx), RA0;
818 movdqu (1 * 16)(%rdx), RA1;
819 movdqu (2 * 16)(%rdx), RA2;
820 movdqu (3 * 16)(%rdx), RA3;
821 movdqu (4 * 16)(%rdx), RB0;
822 movdqu (5 * 16)(%rdx), RB1;
823 movdqu (6 * 16)(%rdx), RB2;
824 movdqu (7 * 16)(%rdx), RB3;
825
826 call __serpent_dec_blk8;
827
828 movdqu (7 * 16)(%rdx), RNOT;
829 pxor_u((%rcx), RA0, RTMP0);
830 pxor_u((0 * 16)(%rdx), RA1, RTMP0);
831 pxor_u((1 * 16)(%rdx), RA2, RTMP0);
832 pxor_u((2 * 16)(%rdx), RA3, RTMP0);
833 pxor_u((3 * 16)(%rdx), RB0, RTMP0);
834 pxor_u((4 * 16)(%rdx), RB1, RTMP0);
835 pxor_u((5 * 16)(%rdx), RB2, RTMP0);
836 pxor_u((6 * 16)(%rdx), RB3, RTMP0);
837 movdqu RNOT, (%rcx); /* store new IV */
838
839 movdqu RA0, (0 * 16)(%rsi);
840 movdqu RA1, (1 * 16)(%rsi);
841 movdqu RA2, (2 * 16)(%rsi);
842 movdqu RA3, (3 * 16)(%rsi);
843 movdqu RB0, (4 * 16)(%rsi);
844 movdqu RB1, (5 * 16)(%rsi);
845 movdqu RB2, (6 * 16)(%rsi);
846 movdqu RB3, (7 * 16)(%rsi);
847
848 /* clear the used registers */
849 pxor RA0, RA0;
850 pxor RA1, RA1;
851 pxor RA2, RA2;
852 pxor RA3, RA3;
853 pxor RA4, RA4;
854 pxor RB0, RB0;
855 pxor RB1, RB1;
856 pxor RB2, RB2;
857 pxor RB3, RB3;
858 pxor RB4, RB4;
859 pxor RTMP0, RTMP0;
860 pxor RTMP1, RTMP1;
861 pxor RTMP2, RTMP2;
862 pxor RNOT, RNOT;
863
864 ret_spec_stop;
865 CFI_ENDPROC();
866 ELF(.size _gcry_serpent_sse2_cbc_dec,.-_gcry_serpent_sse2_cbc_dec;)
867
868 .align 16
869 .globl _gcry_serpent_sse2_cfb_dec
870 ELF(.type _gcry_serpent_sse2_cfb_dec,@function;)
871 _gcry_serpent_sse2_cfb_dec:
872 /* input:
873 * %rdi: ctx, CTX
874 * %rsi: dst (8 blocks)
875 * %rdx: src (8 blocks)
876 * %rcx: iv
877 */
878 CFI_STARTPROC();
879
880 /* Load input */
881 movdqu (%rcx), RA0;
882 movdqu 0 * 16(%rdx), RA1;
883 movdqu 1 * 16(%rdx), RA2;
884 movdqu 2 * 16(%rdx), RA3;
885 movdqu 3 * 16(%rdx), RB0;
886 movdqu 4 * 16(%rdx), RB1;
887 movdqu 5 * 16(%rdx), RB2;
888 movdqu 6 * 16(%rdx), RB3;
889
890 /* Update IV */
891 movdqu 7 * 16(%rdx), RNOT;
892 movdqu RNOT, (%rcx);
893
894 call __serpent_enc_blk8;
895
896 pxor_u((0 * 16)(%rdx), RA4, RTMP0);
897 pxor_u((1 * 16)(%rdx), RA1, RTMP0);
898 pxor_u((2 * 16)(%rdx), RA2, RTMP0);
899 pxor_u((3 * 16)(%rdx), RA0, RTMP0);
900 pxor_u((4 * 16)(%rdx), RB4, RTMP0);
901 pxor_u((5 * 16)(%rdx), RB1, RTMP0);
902 pxor_u((6 * 16)(%rdx), RB2, RTMP0);
903 pxor_u((7 * 16)(%rdx), RB0, RTMP0);
904
905 movdqu RA4, (0 * 16)(%rsi);
906 movdqu RA1, (1 * 16)(%rsi);
907 movdqu RA2, (2 * 16)(%rsi);
908 movdqu RA0, (3 * 16)(%rsi);
909 movdqu RB4, (4 * 16)(%rsi);
910 movdqu RB1, (5 * 16)(%rsi);
911 movdqu RB2, (6 * 16)(%rsi);
912 movdqu RB0, (7 * 16)(%rsi);
913
914 /* clear the used registers */
915 pxor RA0, RA0;
916 pxor RA1, RA1;
917 pxor RA2, RA2;
918 pxor RA3, RA3;
919 pxor RA4, RA4;
920 pxor RB0, RB0;
921 pxor RB1, RB1;
922 pxor RB2, RB2;
923 pxor RB3, RB3;
924 pxor RB4, RB4;
925 pxor RTMP0, RTMP0;
926 pxor RTMP1, RTMP1;
927 pxor RTMP2, RTMP2;
928 pxor RNOT, RNOT;
929
930 ret_spec_stop;
931 CFI_ENDPROC();
932 ELF(.size _gcry_serpent_sse2_cfb_dec,.-_gcry_serpent_sse2_cfb_dec;)
933
934 .align 16
935 .globl _gcry_serpent_sse2_ocb_enc
936 ELF(.type _gcry_serpent_sse2_ocb_enc,@function;)
937
938 _gcry_serpent_sse2_ocb_enc:
939 /* input:
940 * %rdi: ctx, CTX
941 * %rsi: dst (8 blocks)
942 * %rdx: src (8 blocks)
943 * %rcx: offset
944 * %r8 : checksum
945 * %r9 : L pointers (void *L[8])
946 */
947 CFI_STARTPROC();
948
949 subq $(4 * 8), %rsp;
950 CFI_ADJUST_CFA_OFFSET(4 * 8);
951
952 movq %r10, (0 * 8)(%rsp);
953 movq %r11, (1 * 8)(%rsp);
954 movq %r12, (2 * 8)(%rsp);
955 movq %r13, (3 * 8)(%rsp);
956 CFI_REL_OFFSET(%r10, 0 * 8);
957 CFI_REL_OFFSET(%r11, 1 * 8);
958 CFI_REL_OFFSET(%r12, 2 * 8);
959 CFI_REL_OFFSET(%r13, 3 * 8);
960
961 movdqu (%rcx), RTMP0;
962 movdqu (%r8), RTMP1;
963
964 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
965 /* Checksum_i = Checksum_{i-1} xor P_i */
966 /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i) */
967
968 #define OCB_INPUT(n, lreg, xreg) \
969 movdqu (n * 16)(%rdx), xreg; \
970 movdqu (lreg), RNOT; \
971 pxor RNOT, RTMP0; \
972 pxor xreg, RTMP1; \
973 pxor RTMP0, xreg; \
974 movdqu RTMP0, (n * 16)(%rsi);
975 movq (0 * 8)(%r9), %r10;
976 movq (1 * 8)(%r9), %r11;
977 movq (2 * 8)(%r9), %r12;
978 movq (3 * 8)(%r9), %r13;
979 OCB_INPUT(0, %r10, RA0);
980 OCB_INPUT(1, %r11, RA1);
981 OCB_INPUT(2, %r12, RA2);
982 OCB_INPUT(3, %r13, RA3);
983 movq (4 * 8)(%r9), %r10;
984 movq (5 * 8)(%r9), %r11;
985 movq (6 * 8)(%r9), %r12;
986 movq (7 * 8)(%r9), %r13;
987 OCB_INPUT(4, %r10, RB0);
988 OCB_INPUT(5, %r11, RB1);
989 OCB_INPUT(6, %r12, RB2);
990 OCB_INPUT(7, %r13, RB3);
991 #undef OCB_INPUT
992
993 movdqu RTMP0, (%rcx);
994 movdqu RTMP1, (%r8);
995
996 movq (0 * 8)(%rsp), %r10;
997 movq (1 * 8)(%rsp), %r11;
998 movq (2 * 8)(%rsp), %r12;
999 movq (3 * 8)(%rsp), %r13;
1000 CFI_RESTORE(%r10);
1001 CFI_RESTORE(%r11);
1002 CFI_RESTORE(%r12);
1003 CFI_RESTORE(%r13);
1004
1005 call __serpent_enc_blk8;
1006
1007 addq $(4 * 8), %rsp;
1008 CFI_ADJUST_CFA_OFFSET(-4 * 8);
1009
1010 pxor_u((0 * 16)(%rsi), RA4, RTMP0);
1011 pxor_u((1 * 16)(%rsi), RA1, RTMP0);
1012 pxor_u((2 * 16)(%rsi), RA2, RTMP0);
1013 pxor_u((3 * 16)(%rsi), RA0, RTMP0);
1014 pxor_u((4 * 16)(%rsi), RB4, RTMP0);
1015 pxor_u((5 * 16)(%rsi), RB1, RTMP0);
1016 pxor_u((6 * 16)(%rsi), RB2, RTMP0);
1017 pxor_u((7 * 16)(%rsi), RB0, RTMP0);
1018
1019 movdqu RA4, (0 * 16)(%rsi);
1020 movdqu RA1, (1 * 16)(%rsi);
1021 movdqu RA2, (2 * 16)(%rsi);
1022 movdqu RA0, (3 * 16)(%rsi);
1023 movdqu RB4, (4 * 16)(%rsi);
1024 movdqu RB1, (5 * 16)(%rsi);
1025 movdqu RB2, (6 * 16)(%rsi);
1026 movdqu RB0, (7 * 16)(%rsi);
1027
1028 /* clear the used registers */
1029 pxor RA0, RA0;
1030 pxor RA1, RA1;
1031 pxor RA2, RA2;
1032 pxor RA3, RA3;
1033 pxor RA4, RA4;
1034 pxor RB0, RB0;
1035 pxor RB1, RB1;
1036 pxor RB2, RB2;
1037 pxor RB3, RB3;
1038 pxor RB4, RB4;
1039 pxor RTMP0, RTMP0;
1040 pxor RTMP1, RTMP1;
1041 pxor RTMP2, RTMP2;
1042 pxor RNOT, RNOT;
1043
1044 ret_spec_stop;
1045 CFI_ENDPROC();
1046 ELF(.size _gcry_serpent_sse2_ocb_enc,.-_gcry_serpent_sse2_ocb_enc;)
1047
1048 .align 16
1049 .globl _gcry_serpent_sse2_ocb_dec
1050 ELF(.type _gcry_serpent_sse2_ocb_dec,@function;)
1051
1052 _gcry_serpent_sse2_ocb_dec:
1053 /* input:
1054 * %rdi: ctx, CTX
1055 * %rsi: dst (8 blocks)
1056 * %rdx: src (8 blocks)
1057 * %rcx: offset
1058 * %r8 : checksum
1059 * %r9 : L pointers (void *L[8])
1060 */
1061 CFI_STARTPROC();
1062
1063 subq $(4 * 8), %rsp;
1064 CFI_ADJUST_CFA_OFFSET(4 * 8);
1065
1066 movq %r10, (0 * 8)(%rsp);
1067 movq %r11, (1 * 8)(%rsp);
1068 movq %r12, (2 * 8)(%rsp);
1069 movq %r13, (3 * 8)(%rsp);
1070 CFI_REL_OFFSET(%r10, 0 * 8);
1071 CFI_REL_OFFSET(%r11, 1 * 8);
1072 CFI_REL_OFFSET(%r12, 2 * 8);
1073 CFI_REL_OFFSET(%r13, 3 * 8);
1074
1075 movdqu (%rcx), RTMP0;
1076
1077 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1078 /* P_i = Offset_i xor DECIPHER(K, C_i xor Offset_i) */
1079
1080 #define OCB_INPUT(n, lreg, xreg) \
1081 movdqu (n * 16)(%rdx), xreg; \
1082 movdqu (lreg), RNOT; \
1083 pxor RNOT, RTMP0; \
1084 pxor RTMP0, xreg; \
1085 movdqu RTMP0, (n * 16)(%rsi);
1086 movq (0 * 8)(%r9), %r10;
1087 movq (1 * 8)(%r9), %r11;
1088 movq (2 * 8)(%r9), %r12;
1089 movq (3 * 8)(%r9), %r13;
1090 OCB_INPUT(0, %r10, RA0);
1091 OCB_INPUT(1, %r11, RA1);
1092 OCB_INPUT(2, %r12, RA2);
1093 OCB_INPUT(3, %r13, RA3);
1094 movq (4 * 8)(%r9), %r10;
1095 movq (5 * 8)(%r9), %r11;
1096 movq (6 * 8)(%r9), %r12;
1097 movq (7 * 8)(%r9), %r13;
1098 OCB_INPUT(4, %r10, RB0);
1099 OCB_INPUT(5, %r11, RB1);
1100 OCB_INPUT(6, %r12, RB2);
1101 OCB_INPUT(7, %r13, RB3);
1102 #undef OCB_INPUT
1103
1104 movdqu RTMP0, (%rcx);
1105
1106 movq (0 * 8)(%rsp), %r10;
1107 movq (1 * 8)(%rsp), %r11;
1108 movq (2 * 8)(%rsp), %r12;
1109 movq (3 * 8)(%rsp), %r13;
1110 CFI_RESTORE(%r10);
1111 CFI_RESTORE(%r11);
1112 CFI_RESTORE(%r12);
1113 CFI_RESTORE(%r13);
1114
1115 call __serpent_dec_blk8;
1116
1117 addq $(4 * 8), %rsp;
1118 CFI_ADJUST_CFA_OFFSET(-4 * 8);
1119
1120 movdqu (%r8), RTMP0;
1121
1122 pxor_u((0 * 16)(%rsi), RA0, RTMP1);
1123 pxor_u((1 * 16)(%rsi), RA1, RTMP1);
1124 pxor_u((2 * 16)(%rsi), RA2, RTMP1);
1125 pxor_u((3 * 16)(%rsi), RA3, RTMP1);
1126 pxor_u((4 * 16)(%rsi), RB0, RTMP1);
1127 pxor_u((5 * 16)(%rsi), RB1, RTMP1);
1128 pxor_u((6 * 16)(%rsi), RB2, RTMP1);
1129 pxor_u((7 * 16)(%rsi), RB3, RTMP1);
1130
1131 /* Checksum_i = Checksum_{i-1} xor P_i */
1132
1133 movdqu RA0, (0 * 16)(%rsi);
1134 pxor RA0, RTMP0;
1135 movdqu RA1, (1 * 16)(%rsi);
1136 pxor RA1, RTMP0;
1137 movdqu RA2, (2 * 16)(%rsi);
1138 pxor RA2, RTMP0;
1139 movdqu RA3, (3 * 16)(%rsi);
1140 pxor RA3, RTMP0;
1141 movdqu RB0, (4 * 16)(%rsi);
1142 pxor RB0, RTMP0;
1143 movdqu RB1, (5 * 16)(%rsi);
1144 pxor RB1, RTMP0;
1145 movdqu RB2, (6 * 16)(%rsi);
1146 pxor RB2, RTMP0;
1147 movdqu RB3, (7 * 16)(%rsi);
1148 pxor RB3, RTMP0;
1149
1150 movdqu RTMP0, (%r8);
1151
1152 /* clear the used registers */
1153 pxor RA0, RA0;
1154 pxor RA1, RA1;
1155 pxor RA2, RA2;
1156 pxor RA3, RA3;
1157 pxor RA4, RA4;
1158 pxor RB0, RB0;
1159 pxor RB1, RB1;
1160 pxor RB2, RB2;
1161 pxor RB3, RB3;
1162 pxor RB4, RB4;
1163 pxor RTMP0, RTMP0;
1164 pxor RTMP1, RTMP1;
1165 pxor RTMP2, RTMP2;
1166 pxor RNOT, RNOT;
1167
1168 ret_spec_stop;
1169 CFI_ENDPROC();
1170 ELF(.size _gcry_serpent_sse2_ocb_dec,.-_gcry_serpent_sse2_ocb_dec;)
1171
1172 .align 16
1173 .globl _gcry_serpent_sse2_ocb_auth
1174 ELF(.type _gcry_serpent_sse2_ocb_auth,@function;)
1175
1176 _gcry_serpent_sse2_ocb_auth:
1177 /* input:
1178 * %rdi: ctx, CTX
1179 * %rsi: abuf (8 blocks)
1180 * %rdx: offset
1181 * %rcx: checksum
1182 * %r8 : L pointers (void *L[8])
1183 */
1184 CFI_STARTPROC();
1185
1186 subq $(4 * 8), %rsp;
1187 CFI_ADJUST_CFA_OFFSET(4 * 8);
1188
1189 movq %r10, (0 * 8)(%rsp);
1190 movq %r11, (1 * 8)(%rsp);
1191 movq %r12, (2 * 8)(%rsp);
1192 movq %r13, (3 * 8)(%rsp);
1193 CFI_REL_OFFSET(%r10, 0 * 8);
1194 CFI_REL_OFFSET(%r11, 1 * 8);
1195 CFI_REL_OFFSET(%r12, 2 * 8);
1196 CFI_REL_OFFSET(%r13, 3 * 8);
1197
1198 movdqu (%rdx), RTMP0;
1199
1200 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1201 /* Sum_i = Sum_{i-1} xor ENCIPHER(K, A_i xor Offset_i) */
1202
1203 #define OCB_INPUT(n, lreg, xreg) \
1204 movdqu (n * 16)(%rsi), xreg; \
1205 movdqu (lreg), RNOT; \
1206 pxor RNOT, RTMP0; \
1207 pxor RTMP0, xreg;
1208 movq (0 * 8)(%r8), %r10;
1209 movq (1 * 8)(%r8), %r11;
1210 movq (2 * 8)(%r8), %r12;
1211 movq (3 * 8)(%r8), %r13;
1212 OCB_INPUT(0, %r10, RA0);
1213 OCB_INPUT(1, %r11, RA1);
1214 OCB_INPUT(2, %r12, RA2);
1215 OCB_INPUT(3, %r13, RA3);
1216 movq (4 * 8)(%r8), %r10;
1217 movq (5 * 8)(%r8), %r11;
1218 movq (6 * 8)(%r8), %r12;
1219 movq (7 * 8)(%r8), %r13;
1220 OCB_INPUT(4, %r10, RB0);
1221 OCB_INPUT(5, %r11, RB1);
1222 OCB_INPUT(6, %r12, RB2);
1223 OCB_INPUT(7, %r13, RB3);
1224 #undef OCB_INPUT
1225
1226 movdqu RTMP0, (%rdx);
1227
1228 movq (0 * 8)(%rsp), %r10;
1229 movq (1 * 8)(%rsp), %r11;
1230 movq (2 * 8)(%rsp), %r12;
1231 movq (3 * 8)(%rsp), %r13;
1232 CFI_RESTORE(%r10);
1233 CFI_RESTORE(%r11);
1234 CFI_RESTORE(%r12);
1235 CFI_RESTORE(%r13);
1236
1237 call __serpent_enc_blk8;
1238
1239 addq $(4 * 8), %rsp;
1240 CFI_ADJUST_CFA_OFFSET(-4 * 8);
1241
1242 movdqu (%rcx), RTMP0;
1243 pxor RB4, RA4;
1244 pxor RB1, RA1;
1245 pxor RB2, RA2;
1246 pxor RB0, RA0;
1247
1248 pxor RTMP0, RA2;
1249 pxor RA4, RA1;
1250 pxor RA2, RA0;
1251
1252 pxor RA1, RA0;
1253 movdqu RA0, (%rcx);
1254
1255 /* clear the used registers */
1256 pxor RA0, RA0;
1257 pxor RA1, RA1;
1258 pxor RA2, RA2;
1259 pxor RA3, RA3;
1260 pxor RA4, RA4;
1261 pxor RB0, RB0;
1262 pxor RB1, RB1;
1263 pxor RB2, RB2;
1264 pxor RB3, RB3;
1265 pxor RB4, RB4;
1266 pxor RTMP0, RTMP0;
1267 pxor RTMP1, RTMP1;
1268 pxor RTMP2, RTMP2;
1269 pxor RNOT, RNOT;
1270
1271 ret_spec_stop;
1272 CFI_ENDPROC();
1273 ELF(.size _gcry_serpent_sse2_ocb_auth,.-_gcry_serpent_sse2_ocb_auth;)
1274
1275 #endif /*defined(USE_SERPENT)*/
1276 #endif /*__x86_64*/