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 <asm/frame.h>
28 #include "glue_helper-asm-avx.S"
30 .file "twofish-avx-x86_64-asm_64.S"
32 .section .rodata.cst16.bswap128_mask, "aM", @progbits, 16
35 .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
37 .section .rodata.cst16.xts_gf128mul_and_shl1_mask, "aM", @progbits, 16
39 .Lxts_gf128mul_and_shl1_mask:
40 .byte 0x87, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
44 /* structure of crypto context */
52 /**********************************************************************
54 **********************************************************************/
106 #define lookup_32bit(t0, t1, t2, t3, src, dst, interleave_op, il_reg) \
107 movzbl src ## bl, RID1d; \
108 movzbl src ## bh, RID2d; \
110 movl t0(CTX, RID1, 4), dst ## d; \
111 movl t1(CTX, RID2, 4), RID2d; \
112 movzbl src ## bl, RID1d; \
113 xorl RID2d, dst ## d; \
114 movzbl src ## bh, RID2d; \
115 interleave_op(il_reg); \
116 xorl t2(CTX, RID1, 4), dst ## d; \
117 xorl t3(CTX, RID2, 4), dst ## d;
119 #define dummy(d) /* do nothing */
121 #define shr_next(reg) \
124 #define G(gi1, gi2, x, t0, t1, t2, t3) \
125 lookup_32bit(t0, t1, t2, t3, ##gi1, RGS1, shr_next, ##gi1); \
126 lookup_32bit(t0, t1, t2, t3, ##gi2, RGS3, shr_next, ##gi2); \
128 lookup_32bit(t0, t1, t2, t3, ##gi1, RGS2, dummy, none); \
131 lookup_32bit(t0, t1, t2, t3, ##gi2, RGS1, dummy, none); \
135 #define round_head_2(a, b, x1, y1, x2, y2) \
136 vmovq b ## 1, RGI3; \
137 vpextrq $1, b ## 1, RGI4; \
139 G(RGI1, RGI2, x1, s0, s1, s2, s3); \
140 vmovq a ## 2, RGI1; \
141 vpextrq $1, a ## 2, RGI2; \
143 vpinsrq $1, RGS3, x1, x1; \
145 G(RGI3, RGI4, y1, s1, s2, s3, s0); \
146 vmovq b ## 2, RGI3; \
147 vpextrq $1, b ## 2, RGI4; \
149 vpinsrq $1, RGS3, y1, y1; \
151 G(RGI1, RGI2, x2, s0, s1, s2, s3); \
153 vpinsrq $1, RGS3, x2, x2; \
155 G(RGI3, RGI4, y2, s1, s2, s3, s0); \
157 vpinsrq $1, RGS3, y2, y2;
159 #define encround_tail(a, b, c, d, x, y, prerotate) \
167 vpslld $(32 - 1), c, c; \
171 #define decround_tail(a, b, c, d, x, y, prerotate) \
180 vpslld $(32 - 1), d, d; \
183 #define rotate_1l(x) \
185 vpsrld $(32 - 1), x, x; \
188 #define preload_rgi(c) \
192 #define encrypt_round(n, a, b, c, d, preload, prerotate) \
193 vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
194 vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
195 round_head_2(a, b, RX0, RY0, RX1, RY1); \
196 encround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
198 encround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
200 #define decrypt_round(n, a, b, c, d, preload, prerotate) \
201 vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
202 vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
203 round_head_2(a, b, RX0, RY0, RX1, RY1); \
204 decround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
206 decround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
208 #define encrypt_cycle(n) \
209 encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
210 encrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l);
212 #define encrypt_cycle_last(n) \
213 encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
214 encrypt_round(((2*n) + 1), RC, RD, RA, RB, dummy, dummy);
216 #define decrypt_cycle(n) \
217 decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
218 decrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l);
220 #define decrypt_cycle_last(n) \
221 decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
222 decrypt_round((2*n), RA, RB, RC, RD, dummy, dummy);
224 #define transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
225 vpunpckldq x1, x0, t0; \
226 vpunpckhdq x1, x0, t2; \
227 vpunpckldq x3, x2, t1; \
228 vpunpckhdq x3, x2, x3; \
230 vpunpcklqdq t1, t0, x0; \
231 vpunpckhqdq t1, t0, x1; \
232 vpunpcklqdq x3, t2, x2; \
233 vpunpckhqdq x3, t2, x3;
235 #define inpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
236 vpxor x0, wkey, x0; \
237 vpxor x1, wkey, x1; \
238 vpxor x2, wkey, x2; \
239 vpxor x3, wkey, x3; \
241 transpose_4x4(x0, x1, x2, x3, t0, t1, t2)
243 #define outunpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
244 transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
246 vpxor x0, wkey, x0; \
247 vpxor x1, wkey, x1; \
248 vpxor x2, wkey, x2; \
255 * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: blocks
257 * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
266 inpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
269 inpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
279 encrypt_cycle_last(7);
281 vmovdqu (w+4*4)(CTX), RK1;
287 outunpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
288 outunpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
291 ENDPROC(__twofish_enc_blk8)
297 * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
299 * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: decrypted blocks
302 vmovdqu (w+4*4)(CTX), RK1;
307 inpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
310 inpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
320 decrypt_cycle_last(0);
322 vmovdqu (w)(CTX), RK1;
327 outunpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
328 outunpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
331 ENDPROC(__twofish_dec_blk8)
333 ENTRY(twofish_ecb_enc_8way)
343 load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
345 call __twofish_enc_blk8;
347 store_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
351 ENDPROC(twofish_ecb_enc_8way)
353 ENTRY(twofish_ecb_dec_8way)
363 load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
365 call __twofish_dec_blk8;
367 store_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
371 ENDPROC(twofish_ecb_dec_8way)
373 ENTRY(twofish_cbc_dec_8way)
386 load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
388 call __twofish_dec_blk8;
390 store_cbc_8way(%r12, %r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
396 ENDPROC(twofish_cbc_dec_8way)
398 ENTRY(twofish_ctr_8way)
403 * %rcx: iv (little endian, 128bit)
412 load_ctr_8way(%rcx, .Lbswap128_mask, RA1, RB1, RC1, RD1, RA2, RB2, RC2,
415 call __twofish_enc_blk8;
417 store_ctr_8way(%r12, %r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
423 ENDPROC(twofish_ctr_8way)
425 ENTRY(twofish_xts_enc_8way)
430 * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
436 /* regs <= src, dst <= IVs, regs <= regs xor IVs */
437 load_xts_8way(%rcx, %rdx, %rsi, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2,
438 RX0, RX1, RY0, .Lxts_gf128mul_and_shl1_mask);
440 call __twofish_enc_blk8;
442 /* dst <= regs xor IVs(in dst) */
443 store_xts_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
447 ENDPROC(twofish_xts_enc_8way)
449 ENTRY(twofish_xts_dec_8way)
454 * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
460 /* regs <= src, dst <= IVs, regs <= regs xor IVs */
461 load_xts_8way(%rcx, %rdx, %rsi, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2,
462 RX0, RX1, RY0, .Lxts_gf128mul_and_shl1_mask);
464 call __twofish_dec_blk8;
466 /* dst <= regs xor IVs(in dst) */
467 store_xts_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
471 ENDPROC(twofish_xts_dec_8way)