2 * Cast5 Cipher 16-way parallel algorithm (AVX/x86_64)
4 * Copyright (C) 2012 Johannes Goetzfried
5 * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
7 * Copyright © 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <linux/linkage.h>
27 #include <asm/frame.h>
29 .file "cast5-avx-x86_64-asm_64.S"
36 /* structure of crypto context */
39 #define rr ((16*4)+16)
47 /**********************************************************************
49 **********************************************************************/
100 #define lookup_32bit(src, dst, op1, op2, op3, interleave_op, il_reg) \
101 movzbl src ## bh, RID1d; \
102 movzbl src ## bl, RID2d; \
104 movl s1(, RID1, 4), dst ## d; \
105 op1 s2(, RID2, 4), dst ## d; \
106 movzbl src ## bh, RID1d; \
107 movzbl src ## bl, RID2d; \
108 interleave_op(il_reg); \
109 op2 s3(, RID1, 4), dst ## d; \
110 op3 s4(, RID2, 4), dst ## d;
112 #define dummy(d) /* do nothing */
114 #define shr_next(reg) \
117 #define F_head(a, x, gi1, gi2, op0) \
119 vpslld RKRF, x, RTMP; \
126 #define F_tail(a, x, gi1, gi2, op1, op2, op3) \
127 lookup_32bit(##gi1, RFS1, op1, op2, op3, shr_next, ##gi1); \
128 lookup_32bit(##gi2, RFS3, op1, op2, op3, shr_next, ##gi2); \
130 lookup_32bit(##gi1, RFS2, op1, op2, op3, dummy, none); \
133 lookup_32bit(##gi2, RFS1, op1, op2, op3, dummy, none); \
138 vpinsrq $1, RFS3, x, x;
140 #define F_2(a1, b1, a2, b2, op0, op1, op2, op3) \
141 F_head(b1, RX, RGI1, RGI2, op0); \
142 F_head(b2, RX, RGI3, RGI4, op0); \
144 F_tail(b1, RX, RGI1, RGI2, op1, op2, op3); \
145 F_tail(b2, RTMP, RGI3, RGI4, op1, op2, op3); \
150 #define F1_2(a1, b1, a2, b2) \
151 F_2(a1, b1, a2, b2, vpaddd, xorl, subl, addl)
152 #define F2_2(a1, b1, a2, b2) \
153 F_2(a1, b1, a2, b2, vpxor, subl, addl, xorl)
154 #define F3_2(a1, b1, a2, b2) \
155 F_2(a1, b1, a2, b2, vpsubd, addl, xorl, subl)
157 #define subround(a1, b1, a2, b2, f) \
158 F ## f ## _2(a1, b1, a2, b2);
160 #define round(l, r, n, f) \
161 vbroadcastss (km+(4*n))(CTX), RKM; \
162 vpand R1ST, RKR, RKRF; \
163 vpsubq RKRF, R32, RKRR; \
164 vpsrldq $1, RKR, RKR; \
165 subround(l ## 1, r ## 1, l ## 2, r ## 2, f); \
166 subround(l ## 3, r ## 3, l ## 4, r ## 4, f);
168 #define enc_preload_rkr() \
169 vbroadcastss .L16_mask, RKR; \
170 /* add 16-bit rotation to key rotations (mod 32) */ \
171 vpxor kr(CTX), RKR, RKR;
173 #define dec_preload_rkr() \
174 vbroadcastss .L16_mask, RKR; \
175 /* add 16-bit rotation to key rotations (mod 32) */ \
176 vpxor kr(CTX), RKR, RKR; \
177 vpshufb .Lbswap128_mask, RKR, RKR;
179 #define transpose_2x4(x0, x1, t0, t1) \
180 vpunpckldq x1, x0, t0; \
181 vpunpckhdq x1, x0, t1; \
183 vpunpcklqdq t1, t0, x0; \
184 vpunpckhqdq t1, t0, x1;
186 #define inpack_blocks(x0, x1, t0, t1, rmask) \
187 vpshufb rmask, x0, x0; \
188 vpshufb rmask, x1, x1; \
190 transpose_2x4(x0, x1, t0, t1)
192 #define outunpack_blocks(x0, x1, t0, t1, rmask) \
193 transpose_2x4(x0, x1, t0, t1) \
195 vpshufb rmask, x0, x0; \
196 vpshufb rmask, x1, x1;
198 .section .rodata.cst16.bswap_mask, "aM", @progbits, 16
201 .byte 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12
202 .section .rodata.cst16.bswap128_mask, "aM", @progbits, 16
205 .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
206 .section .rodata.cst16.bswap_iv_mask, "aM", @progbits, 16
209 .byte 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0
211 .section .rodata.cst4.16_mask, "aM", @progbits, 4
215 .section .rodata.cst4.32_mask, "aM", @progbits, 4
219 .section .rodata.cst4.first_mask, "aM", @progbits, 4
230 * RL1: blocks 1 and 2
231 * RR1: blocks 3 and 4
232 * RL2: blocks 5 and 6
233 * RR2: blocks 7 and 8
234 * RL3: blocks 9 and 10
235 * RR3: blocks 11 and 12
236 * RL4: blocks 13 and 14
237 * RR4: blocks 15 and 16
239 * RL1: encrypted blocks 1 and 2
240 * RR1: encrypted blocks 3 and 4
241 * RL2: encrypted blocks 5 and 6
242 * RR2: encrypted blocks 7 and 8
243 * RL3: encrypted blocks 9 and 10
244 * RR3: encrypted blocks 11 and 12
245 * RL4: encrypted blocks 13 and 14
246 * RR4: encrypted blocks 15 and 16
254 vmovdqa .Lbswap_mask, RKM;
255 vmovd .Lfirst_mask, R1ST;
256 vmovd .L32_mask, R32;
259 inpack_blocks(RL1, RR1, RTMP, RX, RKM);
260 inpack_blocks(RL2, RR2, RTMP, RX, RKM);
261 inpack_blocks(RL3, RR3, RTMP, RX, RKM);
262 inpack_blocks(RL4, RR4, RTMP, RX, RKM);
274 round(RL, RR, 10, 2);
275 round(RR, RL, 11, 3);
277 movzbl rr(CTX), %eax;
281 round(RL, RR, 12, 1);
282 round(RR, RL, 13, 2);
283 round(RL, RR, 14, 3);
284 round(RR, RL, 15, 1);
290 vmovdqa .Lbswap_mask, RKM;
292 outunpack_blocks(RR1, RL1, RTMP, RX, RKM);
293 outunpack_blocks(RR2, RL2, RTMP, RX, RKM);
294 outunpack_blocks(RR3, RL3, RTMP, RX, RKM);
295 outunpack_blocks(RR4, RL4, RTMP, RX, RKM);
298 ENDPROC(__cast5_enc_blk16)
304 * RL1: encrypted blocks 1 and 2
305 * RR1: encrypted blocks 3 and 4
306 * RL2: encrypted blocks 5 and 6
307 * RR2: encrypted blocks 7 and 8
308 * RL3: encrypted blocks 9 and 10
309 * RR3: encrypted blocks 11 and 12
310 * RL4: encrypted blocks 13 and 14
311 * RR4: encrypted blocks 15 and 16
313 * RL1: decrypted blocks 1 and 2
314 * RR1: decrypted blocks 3 and 4
315 * RL2: decrypted blocks 5 and 6
316 * RR2: decrypted blocks 7 and 8
317 * RL3: decrypted blocks 9 and 10
318 * RR3: decrypted blocks 11 and 12
319 * RL4: decrypted blocks 13 and 14
320 * RR4: decrypted blocks 15 and 16
328 vmovdqa .Lbswap_mask, RKM;
329 vmovd .Lfirst_mask, R1ST;
330 vmovd .L32_mask, R32;
333 inpack_blocks(RL1, RR1, RTMP, RX, RKM);
334 inpack_blocks(RL2, RR2, RTMP, RX, RKM);
335 inpack_blocks(RL3, RR3, RTMP, RX, RKM);
336 inpack_blocks(RL4, RR4, RTMP, RX, RKM);
338 movzbl rr(CTX), %eax;
342 round(RL, RR, 15, 1);
343 round(RR, RL, 14, 3);
344 round(RL, RR, 13, 2);
345 round(RR, RL, 12, 1);
348 round(RL, RR, 11, 3);
349 round(RR, RL, 10, 2);
361 vmovdqa .Lbswap_mask, RKM;
365 outunpack_blocks(RR1, RL1, RTMP, RX, RKM);
366 outunpack_blocks(RR2, RL2, RTMP, RX, RKM);
367 outunpack_blocks(RR3, RL3, RTMP, RX, RKM);
368 outunpack_blocks(RR4, RL4, RTMP, RX, RKM);
373 vpsrldq $4, RKR, RKR;
375 ENDPROC(__cast5_dec_blk16)
377 ENTRY(cast5_ecb_enc_16way)
389 vmovdqu (0*4*4)(%rdx), RL1;
390 vmovdqu (1*4*4)(%rdx), RR1;
391 vmovdqu (2*4*4)(%rdx), RL2;
392 vmovdqu (3*4*4)(%rdx), RR2;
393 vmovdqu (4*4*4)(%rdx), RL3;
394 vmovdqu (5*4*4)(%rdx), RR3;
395 vmovdqu (6*4*4)(%rdx), RL4;
396 vmovdqu (7*4*4)(%rdx), RR4;
398 call __cast5_enc_blk16;
400 vmovdqu RR1, (0*4*4)(%r11);
401 vmovdqu RL1, (1*4*4)(%r11);
402 vmovdqu RR2, (2*4*4)(%r11);
403 vmovdqu RL2, (3*4*4)(%r11);
404 vmovdqu RR3, (4*4*4)(%r11);
405 vmovdqu RL3, (5*4*4)(%r11);
406 vmovdqu RR4, (6*4*4)(%r11);
407 vmovdqu RL4, (7*4*4)(%r11);
412 ENDPROC(cast5_ecb_enc_16way)
414 ENTRY(cast5_ecb_dec_16way)
427 vmovdqu (0*4*4)(%rdx), RL1;
428 vmovdqu (1*4*4)(%rdx), RR1;
429 vmovdqu (2*4*4)(%rdx), RL2;
430 vmovdqu (3*4*4)(%rdx), RR2;
431 vmovdqu (4*4*4)(%rdx), RL3;
432 vmovdqu (5*4*4)(%rdx), RR3;
433 vmovdqu (6*4*4)(%rdx), RL4;
434 vmovdqu (7*4*4)(%rdx), RR4;
436 call __cast5_dec_blk16;
438 vmovdqu RR1, (0*4*4)(%r11);
439 vmovdqu RL1, (1*4*4)(%r11);
440 vmovdqu RR2, (2*4*4)(%r11);
441 vmovdqu RL2, (3*4*4)(%r11);
442 vmovdqu RR3, (4*4*4)(%r11);
443 vmovdqu RL3, (5*4*4)(%r11);
444 vmovdqu RR4, (6*4*4)(%r11);
445 vmovdqu RL4, (7*4*4)(%r11);
450 ENDPROC(cast5_ecb_dec_16way)
452 ENTRY(cast5_cbc_dec_16way)
466 vmovdqu (0*16)(%rdx), RL1;
467 vmovdqu (1*16)(%rdx), RR1;
468 vmovdqu (2*16)(%rdx), RL2;
469 vmovdqu (3*16)(%rdx), RR2;
470 vmovdqu (4*16)(%rdx), RL3;
471 vmovdqu (5*16)(%rdx), RR3;
472 vmovdqu (6*16)(%rdx), RL4;
473 vmovdqu (7*16)(%rdx), RR4;
475 call __cast5_dec_blk16;
479 vpshufd $0x4f, RX, RX;
481 vpxor 0*16+8(%r12), RL1, RL1;
482 vpxor 1*16+8(%r12), RR2, RR2;
483 vpxor 2*16+8(%r12), RL2, RL2;
484 vpxor 3*16+8(%r12), RR3, RR3;
485 vpxor 4*16+8(%r12), RL3, RL3;
486 vpxor 5*16+8(%r12), RR4, RR4;
487 vpxor 6*16+8(%r12), RL4, RL4;
489 vmovdqu RR1, (0*16)(%r11);
490 vmovdqu RL1, (1*16)(%r11);
491 vmovdqu RR2, (2*16)(%r11);
492 vmovdqu RL2, (3*16)(%r11);
493 vmovdqu RR3, (4*16)(%r11);
494 vmovdqu RL3, (5*16)(%r11);
495 vmovdqu RR4, (6*16)(%r11);
496 vmovdqu RL4, (7*16)(%r11);
502 ENDPROC(cast5_cbc_dec_16way)
504 ENTRY(cast5_ctr_16way)
509 * %rcx: iv (big endian, 64bit)
519 vpcmpeqd RTMP, RTMP, RTMP;
520 vpsrldq $8, RTMP, RTMP; /* low: -1, high: 0 */
522 vpcmpeqd RKR, RKR, RKR;
523 vpaddq RKR, RKR, RKR; /* low: -2, high: -2 */
524 vmovdqa .Lbswap_iv_mask, R1ST;
525 vmovdqa .Lbswap128_mask, RKM;
527 /* load IV and byteswap */
529 vpshufb R1ST, RX, RX;
532 vpsubq RTMP, RX, RX; /* le: IV1, IV0 */
533 vpshufb RKM, RX, RL1; /* be: IV0, IV1 */
535 vpshufb RKM, RX, RR1; /* be: IV2, IV3 */
537 vpshufb RKM, RX, RL2; /* be: IV4, IV5 */
539 vpshufb RKM, RX, RR2; /* be: IV6, IV7 */
541 vpshufb RKM, RX, RL3; /* be: IV8, IV9 */
543 vpshufb RKM, RX, RR3; /* be: IV10, IV11 */
545 vpshufb RKM, RX, RL4; /* be: IV12, IV13 */
547 vpshufb RKM, RX, RR4; /* be: IV14, IV15 */
550 vpsubq RTMP, RX, RX; /* le: IV16, IV14 */
551 vpshufb R1ST, RX, RX; /* be: IV16, IV16 */
554 call __cast5_enc_blk16;
557 vpxor (0*16)(%r12), RR1, RR1;
558 vpxor (1*16)(%r12), RL1, RL1;
559 vpxor (2*16)(%r12), RR2, RR2;
560 vpxor (3*16)(%r12), RL2, RL2;
561 vpxor (4*16)(%r12), RR3, RR3;
562 vpxor (5*16)(%r12), RL3, RL3;
563 vpxor (6*16)(%r12), RR4, RR4;
564 vpxor (7*16)(%r12), RL4, RL4;
565 vmovdqu RR1, (0*16)(%r11);
566 vmovdqu RL1, (1*16)(%r11);
567 vmovdqu RR2, (2*16)(%r11);
568 vmovdqu RL2, (3*16)(%r11);
569 vmovdqu RR3, (4*16)(%r11);
570 vmovdqu RL3, (5*16)(%r11);
571 vmovdqu RR4, (6*16)(%r11);
572 vmovdqu RL4, (7*16)(%r11);
578 ENDPROC(cast5_ctr_16way)