2 * Twofish Cipher 8-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 .file "twofish-avx-x86_64-asm_64.S"
29 /* structure of crypto context */
37 /**********************************************************************
39 **********************************************************************/
91 #define lookup_32bit(t0, t1, t2, t3, src, dst, interleave_op, il_reg) \
92 movzbl src ## bl, RID1d; \
93 movzbl src ## bh, RID2d; \
95 movl t0(CTX, RID1, 4), dst ## d; \
96 movl t1(CTX, RID2, 4), RID2d; \
97 movzbl src ## bl, RID1d; \
98 xorl RID2d, dst ## d; \
99 movzbl src ## bh, RID2d; \
100 interleave_op(il_reg); \
101 xorl t2(CTX, RID1, 4), dst ## d; \
102 xorl t3(CTX, RID2, 4), dst ## d;
104 #define dummy(d) /* do nothing */
106 #define shr_next(reg) \
109 #define G(gi1, gi2, x, t0, t1, t2, t3) \
110 lookup_32bit(t0, t1, t2, t3, ##gi1, RGS1, shr_next, ##gi1); \
111 lookup_32bit(t0, t1, t2, t3, ##gi2, RGS3, shr_next, ##gi2); \
113 lookup_32bit(t0, t1, t2, t3, ##gi1, RGS2, dummy, none); \
116 lookup_32bit(t0, t1, t2, t3, ##gi2, RGS1, dummy, none); \
120 #define round_head_2(a, b, x1, y1, x2, y2) \
121 vmovq b ## 1, RGI3; \
122 vpextrq $1, b ## 1, RGI4; \
124 G(RGI1, RGI2, x1, s0, s1, s2, s3); \
125 vmovq a ## 2, RGI1; \
126 vpextrq $1, a ## 2, RGI2; \
128 vpinsrq $1, RGS3, x1, x1; \
130 G(RGI3, RGI4, y1, s1, s2, s3, s0); \
131 vmovq b ## 2, RGI3; \
132 vpextrq $1, b ## 2, RGI4; \
134 vpinsrq $1, RGS3, y1, y1; \
136 G(RGI1, RGI2, x2, s0, s1, s2, s3); \
138 vpinsrq $1, RGS3, x2, x2; \
140 G(RGI3, RGI4, y2, s1, s2, s3, s0); \
142 vpinsrq $1, RGS3, y2, y2;
144 #define encround_tail(a, b, c, d, x, y, prerotate) \
152 vpslld $(32 - 1), c, c; \
156 #define decround_tail(a, b, c, d, x, y, prerotate) \
165 vpslld $(32 - 1), d, d; \
168 #define rotate_1l(x) \
170 vpsrld $(32 - 1), x, x; \
173 #define preload_rgi(c) \
177 #define encrypt_round(n, a, b, c, d, preload, prerotate) \
178 vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
179 vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
180 round_head_2(a, b, RX0, RY0, RX1, RY1); \
181 encround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
183 encround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
185 #define decrypt_round(n, a, b, c, d, preload, prerotate) \
186 vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
187 vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
188 round_head_2(a, b, RX0, RY0, RX1, RY1); \
189 decround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
191 decround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
193 #define encrypt_cycle(n) \
194 encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
195 encrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l);
197 #define encrypt_cycle_last(n) \
198 encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
199 encrypt_round(((2*n) + 1), RC, RD, RA, RB, dummy, dummy);
201 #define decrypt_cycle(n) \
202 decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
203 decrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l);
205 #define decrypt_cycle_last(n) \
206 decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
207 decrypt_round((2*n), RA, RB, RC, RD, dummy, dummy);
209 #define transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
210 vpunpckldq x1, x0, t0; \
211 vpunpckhdq x1, x0, t2; \
212 vpunpckldq x3, x2, t1; \
213 vpunpckhdq x3, x2, x3; \
215 vpunpcklqdq t1, t0, x0; \
216 vpunpckhqdq t1, t0, x1; \
217 vpunpcklqdq x3, t2, x2; \
218 vpunpckhqdq x3, t2, x3;
220 #define inpack_blocks(in, x0, x1, x2, x3, wkey, t0, t1, t2) \
221 vpxor (0*4*4)(in), wkey, x0; \
222 vpxor (1*4*4)(in), wkey, x1; \
223 vpxor (2*4*4)(in), wkey, x2; \
224 vpxor (3*4*4)(in), wkey, x3; \
226 transpose_4x4(x0, x1, x2, x3, t0, t1, t2)
228 #define outunpack_blocks(out, x0, x1, x2, x3, wkey, t0, t1, t2) \
229 transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
231 vpxor x0, wkey, x0; \
232 vmovdqu x0, (0*4*4)(out); \
233 vpxor x1, wkey, x1; \
234 vmovdqu x1, (1*4*4)(out); \
235 vpxor x2, wkey, x2; \
236 vmovdqu x2, (2*4*4)(out); \
237 vpxor x3, wkey, x3; \
238 vmovdqu x3, (3*4*4)(out);
240 #define outunpack_xor_blocks(out, x0, x1, x2, x3, wkey, t0, t1, t2) \
241 transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
243 vpxor x0, wkey, x0; \
244 vpxor (0*4*4)(out), x0, x0; \
245 vmovdqu x0, (0*4*4)(out); \
246 vpxor x1, wkey, x1; \
247 vpxor (1*4*4)(out), x1, x1; \
248 vmovdqu x1, (1*4*4)(out); \
249 vpxor x2, wkey, x2; \
250 vpxor (2*4*4)(out), x2, x2; \
251 vmovdqu x2, (2*4*4)(out); \
252 vpxor x3, wkey, x3; \
253 vpxor (3*4*4)(out), x3, x3; \
254 vmovdqu x3, (3*4*4)(out);
257 .global __twofish_enc_blk_8way
258 .type __twofish_enc_blk_8way,@function;
260 __twofish_enc_blk_8way:
265 * %rcx: bool, if true: xor output
274 leaq (4*4*4)(%rdx), %rax;
275 inpack_blocks(%rdx, RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
278 inpack_blocks(%rax, RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
290 encrypt_cycle_last(7);
292 vmovdqu (w+4*4)(CTX), RK1;
298 leaq (4*4*4)(%r11), %rax;
303 outunpack_blocks(%r11, RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
304 outunpack_blocks(%rax, RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
309 outunpack_xor_blocks(%r11, RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
310 outunpack_xor_blocks(%rax, RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
315 .global twofish_dec_blk_8way
316 .type twofish_dec_blk_8way,@function;
318 twofish_dec_blk_8way:
328 vmovdqu (w+4*4)(CTX), RK1;
330 leaq (4*4*4)(%rdx), %rax;
331 inpack_blocks(%rdx, RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
334 inpack_blocks(%rax, RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
346 decrypt_cycle_last(0);
348 vmovdqu (w)(CTX), RK1;
353 leaq (4*4*4)(%r11), %rax;
354 outunpack_blocks(%r11, RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
355 outunpack_blocks(%rax, RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);