1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Twofish Cipher 8-way parallel algorithm (AVX/x86_64)
5 * Copyright (C) 2012 Johannes Goetzfried
6 * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
8 * Copyright © 2012-2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
11 #include <linux/linkage.h>
12 #include <asm/frame.h>
13 #include "glue_helper-asm-avx.S"
15 .file "twofish-avx-x86_64-asm_64.S"
17 .section .rodata.cst16.bswap128_mask, "aM", @progbits, 16
20 .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
22 .section .rodata.cst16.xts_gf128mul_and_shl1_mask, "aM", @progbits, 16
24 .Lxts_gf128mul_and_shl1_mask:
25 .byte 0x87, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
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(x0, x1, x2, x3, wkey, t0, t1, t2) \
221 vpxor x0, wkey, x0; \
222 vpxor x1, wkey, x1; \
223 vpxor x2, wkey, x2; \
224 vpxor x3, wkey, x3; \
226 transpose_4x4(x0, x1, x2, x3, t0, t1, t2)
228 #define outunpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
229 transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
231 vpxor x0, wkey, x0; \
232 vpxor x1, wkey, x1; \
233 vpxor x2, wkey, x2; \
237 SYM_FUNC_START_LOCAL(__twofish_enc_blk8)
240 * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: blocks
242 * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
251 inpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
254 inpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
264 encrypt_cycle_last(7);
266 vmovdqu (w+4*4)(CTX), RK1;
272 outunpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
273 outunpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
276 SYM_FUNC_END(__twofish_enc_blk8)
279 SYM_FUNC_START_LOCAL(__twofish_dec_blk8)
282 * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
284 * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: decrypted blocks
287 vmovdqu (w+4*4)(CTX), RK1;
292 inpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
295 inpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
305 decrypt_cycle_last(0);
307 vmovdqu (w)(CTX), RK1;
312 outunpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
313 outunpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
316 SYM_FUNC_END(__twofish_dec_blk8)
318 SYM_FUNC_START(twofish_ecb_enc_8way)
328 load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
330 call __twofish_enc_blk8;
332 store_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
336 SYM_FUNC_END(twofish_ecb_enc_8way)
338 SYM_FUNC_START(twofish_ecb_dec_8way)
348 load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
350 call __twofish_dec_blk8;
352 store_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
356 SYM_FUNC_END(twofish_ecb_dec_8way)
358 SYM_FUNC_START(twofish_cbc_dec_8way)
371 load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
373 call __twofish_dec_blk8;
375 store_cbc_8way(%r12, %r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
381 SYM_FUNC_END(twofish_cbc_dec_8way)
383 SYM_FUNC_START(twofish_ctr_8way)
388 * %rcx: iv (little endian, 128bit)
397 load_ctr_8way(%rcx, .Lbswap128_mask, RA1, RB1, RC1, RD1, RA2, RB2, RC2,
400 call __twofish_enc_blk8;
402 store_ctr_8way(%r12, %r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
408 SYM_FUNC_END(twofish_ctr_8way)
410 SYM_FUNC_START(twofish_xts_enc_8way)
415 * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
421 /* regs <= src, dst <= IVs, regs <= regs xor IVs */
422 load_xts_8way(%rcx, %rdx, %rsi, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2,
423 RX0, RX1, RY0, .Lxts_gf128mul_and_shl1_mask);
425 call __twofish_enc_blk8;
427 /* dst <= regs xor IVs(in dst) */
428 store_xts_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
432 SYM_FUNC_END(twofish_xts_enc_8way)
434 SYM_FUNC_START(twofish_xts_dec_8way)
439 * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
445 /* regs <= src, dst <= IVs, regs <= regs xor IVs */
446 load_xts_8way(%rcx, %rdx, %rsi, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2,
447 RX0, RX1, RY0, .Lxts_gf128mul_and_shl1_mask);
449 call __twofish_dec_blk8;
451 /* dst <= regs xor IVs(in dst) */
452 store_xts_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
456 SYM_FUNC_END(twofish_xts_dec_8way)