1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Twofish Cipher 3-way parallel algorithm (x86_64)
5 * Copyright (C) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
8 #include <linux/linkage.h>
10 .file "twofish-x86_64-asm-3way.S"
13 /* structure of crypto context */
21 /**********************************************************************
23 **********************************************************************/
45 #define CD2 0x10(%rsp)
47 # used only before/after all rounds
52 # used only during rounds
77 #define do16bit_ror(rot, op1, op2, T0, T1, tmp1, tmp2, ab, dst) \
78 movzbl ab ## bl, tmp2 ## d; \
79 movzbl ab ## bh, tmp1 ## d; \
81 op1##l T0(CTX, tmp2, 4), dst ## d; \
82 op2##l T1(CTX, tmp1, 4), dst ## d;
84 #define swap_ab_with_cd(ab, cd, tmp) \
90 * Combined G1 & G2 function. Reordered with help of rotates to have moves
93 #define g1g2_3(ab, cd, Tx0, Tx1, Tx2, Tx3, Ty0, Ty1, Ty2, Ty3, x, y) \
95 do16bit_ror(32, mov, xor, Tx0, Tx1, RT0, x ## 0, ab ## 0, x ## 0); \
96 do16bit_ror(48, mov, xor, Ty1, Ty2, RT0, y ## 0, ab ## 0, y ## 0); \
98 do16bit_ror(32, mov, xor, Tx0, Tx1, RT0, x ## 1, ab ## 1, x ## 1); \
99 do16bit_ror(48, mov, xor, Ty1, Ty2, RT0, y ## 1, ab ## 1, y ## 1); \
101 do16bit_ror(32, mov, xor, Tx0, Tx1, RT0, x ## 2, ab ## 2, x ## 2); \
102 do16bit_ror(48, mov, xor, Ty1, Ty2, RT0, y ## 2, ab ## 2, y ## 2); \
105 do16bit_ror(32, xor, xor, Tx2, Tx3, RT0, RT1, ab ## 0, x ## 0); \
106 do16bit_ror(16, xor, xor, Ty3, Ty0, RT0, RT1, ab ## 0, y ## 0); \
107 swap_ab_with_cd(ab ## 0, cd ## 0, RT0); \
109 do16bit_ror(32, xor, xor, Tx2, Tx3, RT0, RT1, ab ## 1, x ## 1); \
110 do16bit_ror(16, xor, xor, Ty3, Ty0, RT0, RT1, ab ## 1, y ## 1); \
111 swap_ab_with_cd(ab ## 1, cd ## 1, RT0); \
113 do16bit_ror(32, xor, xor, Tx2, Tx3, RT0, RT1, ab ## 2, x ## 2); \
114 do16bit_ror(16, xor, xor, Ty3, Ty0, RT0, RT1, ab ## 2, y ## 2); \
115 swap_ab_with_cd(ab ## 2, cd ## 2, RT0);
117 #define enc_round_end(ab, x, y, n) \
118 addl y ## d, x ## d; \
119 addl x ## d, y ## d; \
120 addl k+4*(2*(n))(CTX), x ## d; \
121 xorl ab ## d, x ## d; \
122 addl k+4*(2*(n)+1)(CTX), y ## d; \
125 xorl y ## d, ab ## d; \
130 #define dec_round_end(ba, x, y, n) \
131 addl y ## d, x ## d; \
132 addl x ## d, y ## d; \
133 addl k+4*(2*(n))(CTX), x ## d; \
134 addl k+4*(2*(n)+1)(CTX), y ## d; \
135 xorl ba ## d, y ## d; \
138 xorl x ## d, ba ## d; \
143 #define encrypt_round3(ab, cd, n) \
144 g1g2_3(ab, cd, s0, s1, s2, s3, s0, s1, s2, s3, RX, RY); \
146 enc_round_end(ab ## 0, RX0, RY0, n); \
147 enc_round_end(ab ## 1, RX1, RY1, n); \
148 enc_round_end(ab ## 2, RX2, RY2, n);
150 #define decrypt_round3(ba, dc, n) \
151 g1g2_3(ba, dc, s1, s2, s3, s0, s3, s0, s1, s2, RY, RX); \
153 dec_round_end(ba ## 0, RX0, RY0, n); \
154 dec_round_end(ba ## 1, RX1, RY1, n); \
155 dec_round_end(ba ## 2, RX2, RY2, n);
157 #define encrypt_cycle3(ab, cd, n) \
158 encrypt_round3(ab, cd, n*2); \
159 encrypt_round3(ab, cd, (n*2)+1);
161 #define decrypt_cycle3(ba, dc, n) \
162 decrypt_round3(ba, dc, (n*2)+1); \
163 decrypt_round3(ba, dc, (n*2));
175 #define inpack3(in, n, xy, m) \
176 movq 4*(n)(in), xy ## 0; \
177 xorq w+4*m(CTX), xy ## 0; \
179 movq 4*(4+(n))(in), xy ## 1; \
180 xorq w+4*m(CTX), xy ## 1; \
182 movq 4*(8+(n))(in), xy ## 2; \
183 xorq w+4*m(CTX), xy ## 2;
185 #define outunpack3(op, out, n, xy, m) \
186 xorq w+4*m(CTX), xy ## 0; \
187 op ## q xy ## 0, 4*(n)(out); \
189 xorq w+4*m(CTX), xy ## 1; \
190 op ## q xy ## 1, 4*(4+(n))(out); \
192 xorq w+4*m(CTX), xy ## 2; \
193 op ## q xy ## 2, 4*(8+(n))(out);
195 #define inpack_enc3() \
196 inpack3(RIO, 0, RAB, 0); \
197 inpack3(RIO, 2, RCD, 2);
199 #define outunpack_enc3(op) \
200 outunpack3(op, RIO, 2, RAB, 6); \
201 outunpack3(op, RIO, 0, RCD, 4);
203 #define inpack_dec3() \
204 inpack3(RIO, 0, RAB, 4); \
208 inpack3(RIO, 2, RCD, 6); \
213 #define outunpack_dec3() \
217 outunpack3(mov, RIO, 0, RCD, 0); \
221 outunpack3(mov, RIO, 2, RAB, 2);
223 SYM_FUNC_START(__twofish_enc_blk_3way)
228 * %rcx: bool, if true: xor output
234 pushq %rcx; /* bool xor */
235 pushq %rsi; /* dst */
240 encrypt_cycle3(RAB, CD, 0);
241 encrypt_cycle3(RAB, CD, 1);
242 encrypt_cycle3(RAB, CD, 2);
243 encrypt_cycle3(RAB, CD, 3);
244 encrypt_cycle3(RAB, CD, 4);
245 encrypt_cycle3(RAB, CD, 5);
246 encrypt_cycle3(RAB, CD, 6);
247 encrypt_cycle3(RAB, CD, 7);
251 popq RT1; /* bool xor */
270 SYM_FUNC_END(__twofish_enc_blk_3way)
272 SYM_FUNC_START(twofish_dec_blk_3way)
282 pushq %rsi; /* dst */
287 decrypt_cycle3(RAB, CD, 7);
288 decrypt_cycle3(RAB, CD, 6);
289 decrypt_cycle3(RAB, CD, 5);
290 decrypt_cycle3(RAB, CD, 4);
291 decrypt_cycle3(RAB, CD, 3);
292 decrypt_cycle3(RAB, CD, 2);
293 decrypt_cycle3(RAB, CD, 1);
294 decrypt_cycle3(RAB, CD, 0);
305 SYM_FUNC_END(twofish_dec_blk_3way)