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-2013 Jussi Kivilinna <jussi.kivilinna@iki.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 "glue_helper-asm-avx.S"
29 .file "twofish-avx-x86_64-asm_64.S"
35 .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
36 .Lxts_gf128mul_and_shl1_mask:
37 .byte 0x87, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
41 /* structure of crypto context */
49 /**********************************************************************
51 **********************************************************************/
103 #define lookup_32bit(t0, t1, t2, t3, src, dst, interleave_op, il_reg) \
104 movzbl src ## bl, RID1d; \
105 movzbl src ## bh, RID2d; \
107 movl t0(CTX, RID1, 4), dst ## d; \
108 movl t1(CTX, RID2, 4), RID2d; \
109 movzbl src ## bl, RID1d; \
110 xorl RID2d, dst ## d; \
111 movzbl src ## bh, RID2d; \
112 interleave_op(il_reg); \
113 xorl t2(CTX, RID1, 4), dst ## d; \
114 xorl t3(CTX, RID2, 4), dst ## d;
116 #define dummy(d) /* do nothing */
118 #define shr_next(reg) \
121 #define G(gi1, gi2, x, t0, t1, t2, t3) \
122 lookup_32bit(t0, t1, t2, t3, ##gi1, RGS1, shr_next, ##gi1); \
123 lookup_32bit(t0, t1, t2, t3, ##gi2, RGS3, shr_next, ##gi2); \
125 lookup_32bit(t0, t1, t2, t3, ##gi1, RGS2, dummy, none); \
128 lookup_32bit(t0, t1, t2, t3, ##gi2, RGS1, dummy, none); \
132 #define round_head_2(a, b, x1, y1, x2, y2) \
133 vmovq b ## 1, RGI3; \
134 vpextrq $1, b ## 1, RGI4; \
136 G(RGI1, RGI2, x1, s0, s1, s2, s3); \
137 vmovq a ## 2, RGI1; \
138 vpextrq $1, a ## 2, RGI2; \
140 vpinsrq $1, RGS3, x1, x1; \
142 G(RGI3, RGI4, y1, s1, s2, s3, s0); \
143 vmovq b ## 2, RGI3; \
144 vpextrq $1, b ## 2, RGI4; \
146 vpinsrq $1, RGS3, y1, y1; \
148 G(RGI1, RGI2, x2, s0, s1, s2, s3); \
150 vpinsrq $1, RGS3, x2, x2; \
152 G(RGI3, RGI4, y2, s1, s2, s3, s0); \
154 vpinsrq $1, RGS3, y2, y2;
156 #define encround_tail(a, b, c, d, x, y, prerotate) \
164 vpslld $(32 - 1), c, c; \
168 #define decround_tail(a, b, c, d, x, y, prerotate) \
177 vpslld $(32 - 1), d, d; \
180 #define rotate_1l(x) \
182 vpsrld $(32 - 1), x, x; \
185 #define preload_rgi(c) \
189 #define encrypt_round(n, a, b, c, d, preload, prerotate) \
190 vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
191 vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
192 round_head_2(a, b, RX0, RY0, RX1, RY1); \
193 encround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
195 encround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
197 #define decrypt_round(n, a, b, c, d, preload, prerotate) \
198 vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
199 vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
200 round_head_2(a, b, RX0, RY0, RX1, RY1); \
201 decround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
203 decround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
205 #define encrypt_cycle(n) \
206 encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
207 encrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l);
209 #define encrypt_cycle_last(n) \
210 encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
211 encrypt_round(((2*n) + 1), RC, RD, RA, RB, dummy, dummy);
213 #define decrypt_cycle(n) \
214 decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
215 decrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l);
217 #define decrypt_cycle_last(n) \
218 decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
219 decrypt_round((2*n), RA, RB, RC, RD, dummy, dummy);
221 #define transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
222 vpunpckldq x1, x0, t0; \
223 vpunpckhdq x1, x0, t2; \
224 vpunpckldq x3, x2, t1; \
225 vpunpckhdq x3, x2, x3; \
227 vpunpcklqdq t1, t0, x0; \
228 vpunpckhqdq t1, t0, x1; \
229 vpunpcklqdq x3, t2, x2; \
230 vpunpckhqdq x3, t2, x3;
232 #define inpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
233 vpxor x0, wkey, x0; \
234 vpxor x1, wkey, x1; \
235 vpxor x2, wkey, x2; \
236 vpxor x3, wkey, x3; \
238 transpose_4x4(x0, x1, x2, x3, t0, t1, t2)
240 #define outunpack_blocks(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 x1, wkey, x1; \
245 vpxor x2, wkey, x2; \
252 * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: blocks
254 * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
263 inpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
266 inpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
276 encrypt_cycle_last(7);
278 vmovdqu (w+4*4)(CTX), RK1;
284 outunpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
285 outunpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
288 ENDPROC(__twofish_enc_blk8)
294 * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
296 * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: decrypted blocks
299 vmovdqu (w+4*4)(CTX), RK1;
304 inpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
307 inpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
317 decrypt_cycle_last(0);
319 vmovdqu (w)(CTX), RK1;
324 outunpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
325 outunpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
328 ENDPROC(__twofish_dec_blk8)
330 ENTRY(twofish_ecb_enc_8way)
339 load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
341 call __twofish_enc_blk8;
343 store_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
346 ENDPROC(twofish_ecb_enc_8way)
348 ENTRY(twofish_ecb_dec_8way)
357 load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
359 call __twofish_dec_blk8;
361 store_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
364 ENDPROC(twofish_ecb_dec_8way)
366 ENTRY(twofish_cbc_dec_8way)
378 load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
380 call __twofish_dec_blk8;
382 store_cbc_8way(%r12, %r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
387 ENDPROC(twofish_cbc_dec_8way)
389 ENTRY(twofish_ctr_8way)
394 * %rcx: iv (little endian, 128bit)
402 load_ctr_8way(%rcx, .Lbswap128_mask, RA1, RB1, RC1, RD1, RA2, RB2, RC2,
405 call __twofish_enc_blk8;
407 store_ctr_8way(%r12, %r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
412 ENDPROC(twofish_ctr_8way)
414 ENTRY(twofish_xts_enc_8way)
419 * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
424 /* regs <= src, dst <= IVs, regs <= regs xor IVs */
425 load_xts_8way(%rcx, %rdx, %rsi, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2,
426 RX0, RX1, RY0, .Lxts_gf128mul_and_shl1_mask);
428 call __twofish_enc_blk8;
430 /* dst <= regs xor IVs(in dst) */
431 store_xts_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
434 ENDPROC(twofish_xts_enc_8way)
436 ENTRY(twofish_xts_dec_8way)
441 * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
446 /* regs <= src, dst <= IVs, regs <= regs xor IVs */
447 load_xts_8way(%rcx, %rdx, %rsi, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2,
448 RX0, RX1, RY0, .Lxts_gf128mul_and_shl1_mask);
450 call __twofish_dec_blk8;
452 /* dst <= regs xor IVs(in dst) */
453 store_xts_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
456 ENDPROC(twofish_xts_dec_8way)