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
24 /* structure of crypto context */
32 /**********************************************************************
34 **********************************************************************/
86 #define lookup_32bit(t0, t1, t2, t3, src, dst, interleave_op, il_reg) \
87 movzbl src ## bl, RID1d; \
88 movzbl src ## bh, RID2d; \
90 movl t0(CTX, RID1, 4), dst ## d; \
91 movl t1(CTX, RID2, 4), RID2d; \
92 movzbl src ## bl, RID1d; \
93 xorl RID2d, dst ## d; \
94 movzbl src ## bh, RID2d; \
95 interleave_op(il_reg); \
96 xorl t2(CTX, RID1, 4), dst ## d; \
97 xorl t3(CTX, RID2, 4), dst ## d;
99 #define dummy(d) /* do nothing */
101 #define shr_next(reg) \
104 #define G(gi1, gi2, x, t0, t1, t2, t3) \
105 lookup_32bit(t0, t1, t2, t3, ##gi1, RGS1, shr_next, ##gi1); \
106 lookup_32bit(t0, t1, t2, t3, ##gi2, RGS3, shr_next, ##gi2); \
108 lookup_32bit(t0, t1, t2, t3, ##gi1, RGS2, dummy, none); \
111 lookup_32bit(t0, t1, t2, t3, ##gi2, RGS1, dummy, none); \
115 #define round_head_2(a, b, x1, y1, x2, y2) \
116 vmovq b ## 1, RGI3; \
117 vpextrq $1, b ## 1, RGI4; \
119 G(RGI1, RGI2, x1, s0, s1, s2, s3); \
120 vmovq a ## 2, RGI1; \
121 vpextrq $1, a ## 2, RGI2; \
123 vpinsrq $1, RGS3, x1, x1; \
125 G(RGI3, RGI4, y1, s1, s2, s3, s0); \
126 vmovq b ## 2, RGI3; \
127 vpextrq $1, b ## 2, RGI4; \
129 vpinsrq $1, RGS3, y1, y1; \
131 G(RGI1, RGI2, x2, s0, s1, s2, s3); \
133 vpinsrq $1, RGS3, x2, x2; \
135 G(RGI3, RGI4, y2, s1, s2, s3, s0); \
137 vpinsrq $1, RGS3, y2, y2;
139 #define encround_tail(a, b, c, d, x, y, prerotate) \
147 vpslld $(32 - 1), c, c; \
151 #define decround_tail(a, b, c, d, x, y, prerotate) \
160 vpslld $(32 - 1), d, d; \
163 #define rotate_1l(x) \
165 vpsrld $(32 - 1), x, x; \
168 #define preload_rgi(c) \
172 #define encrypt_round(n, a, b, c, d, preload, prerotate) \
173 vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
174 vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
175 round_head_2(a, b, RX0, RY0, RX1, RY1); \
176 encround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
178 encround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
180 #define decrypt_round(n, a, b, c, d, preload, prerotate) \
181 vbroadcastss (k+4*(2*(n)))(CTX), RK1; \
182 vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \
183 round_head_2(a, b, RX0, RY0, RX1, RY1); \
184 decround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
186 decround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
188 #define encrypt_cycle(n) \
189 encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
190 encrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l);
192 #define encrypt_cycle_last(n) \
193 encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
194 encrypt_round(((2*n) + 1), RC, RD, RA, RB, dummy, dummy);
196 #define decrypt_cycle(n) \
197 decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
198 decrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l);
200 #define decrypt_cycle_last(n) \
201 decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
202 decrypt_round((2*n), RA, RB, RC, RD, dummy, dummy);
204 #define transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
205 vpunpckldq x1, x0, t0; \
206 vpunpckhdq x1, x0, t2; \
207 vpunpckldq x3, x2, t1; \
208 vpunpckhdq x3, x2, x3; \
210 vpunpcklqdq t1, t0, x0; \
211 vpunpckhqdq t1, t0, x1; \
212 vpunpcklqdq x3, t2, x2; \
213 vpunpckhqdq x3, t2, x3;
215 #define inpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
216 vpxor x0, wkey, x0; \
217 vpxor x1, wkey, x1; \
218 vpxor x2, wkey, x2; \
219 vpxor x3, wkey, x3; \
221 transpose_4x4(x0, x1, x2, x3, t0, t1, t2)
223 #define outunpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
224 transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
226 vpxor x0, wkey, x0; \
227 vpxor x1, wkey, x1; \
228 vpxor x2, wkey, x2; \
231 SYM_FUNC_START_LOCAL(__twofish_enc_blk8)
234 * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: blocks
236 * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
245 inpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
248 inpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
258 encrypt_cycle_last(7);
260 vmovdqu (w+4*4)(CTX), RK1;
266 outunpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
267 outunpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
270 SYM_FUNC_END(__twofish_enc_blk8)
272 SYM_FUNC_START_LOCAL(__twofish_dec_blk8)
275 * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
277 * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: decrypted blocks
280 vmovdqu (w+4*4)(CTX), RK1;
285 inpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
288 inpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
298 decrypt_cycle_last(0);
300 vmovdqu (w)(CTX), RK1;
305 outunpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
306 outunpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
309 SYM_FUNC_END(__twofish_dec_blk8)
311 SYM_FUNC_START(twofish_ecb_enc_8way)
321 load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
323 call __twofish_enc_blk8;
325 store_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
329 SYM_FUNC_END(twofish_ecb_enc_8way)
331 SYM_FUNC_START(twofish_ecb_dec_8way)
341 load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
343 call __twofish_dec_blk8;
345 store_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
349 SYM_FUNC_END(twofish_ecb_dec_8way)
351 SYM_FUNC_START(twofish_cbc_dec_8way)
364 load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
366 call __twofish_dec_blk8;
368 store_cbc_8way(%r12, %r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
374 SYM_FUNC_END(twofish_cbc_dec_8way)