2 * Twofish Cipher 3-way parallel algorithm (x86_64)
4 * Copyright (C) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 #include <linux/linkage.h>
25 .file "twofish-x86_64-asm-3way.S"
28 /* structure of crypto context */
36 /**********************************************************************
38 **********************************************************************/
60 #define CD2 0x10(%rsp)
62 # used only before/after all rounds
67 # used only during rounds
92 #define do16bit_ror(rot, op1, op2, T0, T1, tmp1, tmp2, ab, dst) \
93 movzbl ab ## bl, tmp2 ## d; \
94 movzbl ab ## bh, tmp1 ## d; \
96 op1##l T0(CTX, tmp2, 4), dst ## d; \
97 op2##l T1(CTX, tmp1, 4), dst ## d;
99 #define swap_ab_with_cd(ab, cd, tmp) \
105 * Combined G1 & G2 function. Reordered with help of rotates to have moves
108 #define g1g2_3(ab, cd, Tx0, Tx1, Tx2, Tx3, Ty0, Ty1, Ty2, Ty3, x, y) \
110 do16bit_ror(32, mov, xor, Tx0, Tx1, RT0, x ## 0, ab ## 0, x ## 0); \
111 do16bit_ror(48, mov, xor, Ty1, Ty2, RT0, y ## 0, ab ## 0, y ## 0); \
113 do16bit_ror(32, mov, xor, Tx0, Tx1, RT0, x ## 1, ab ## 1, x ## 1); \
114 do16bit_ror(48, mov, xor, Ty1, Ty2, RT0, y ## 1, ab ## 1, y ## 1); \
116 do16bit_ror(32, mov, xor, Tx0, Tx1, RT0, x ## 2, ab ## 2, x ## 2); \
117 do16bit_ror(48, mov, xor, Ty1, Ty2, RT0, y ## 2, ab ## 2, y ## 2); \
120 do16bit_ror(32, xor, xor, Tx2, Tx3, RT0, RT1, ab ## 0, x ## 0); \
121 do16bit_ror(16, xor, xor, Ty3, Ty0, RT0, RT1, ab ## 0, y ## 0); \
122 swap_ab_with_cd(ab ## 0, cd ## 0, RT0); \
124 do16bit_ror(32, xor, xor, Tx2, Tx3, RT0, RT1, ab ## 1, x ## 1); \
125 do16bit_ror(16, xor, xor, Ty3, Ty0, RT0, RT1, ab ## 1, y ## 1); \
126 swap_ab_with_cd(ab ## 1, cd ## 1, RT0); \
128 do16bit_ror(32, xor, xor, Tx2, Tx3, RT0, RT1, ab ## 2, x ## 2); \
129 do16bit_ror(16, xor, xor, Ty3, Ty0, RT0, RT1, ab ## 2, y ## 2); \
130 swap_ab_with_cd(ab ## 2, cd ## 2, RT0);
132 #define enc_round_end(ab, x, y, n) \
133 addl y ## d, x ## d; \
134 addl x ## d, y ## d; \
135 addl k+4*(2*(n))(CTX), x ## d; \
136 xorl ab ## d, x ## d; \
137 addl k+4*(2*(n)+1)(CTX), y ## d; \
140 xorl y ## d, ab ## d; \
145 #define dec_round_end(ba, x, y, n) \
146 addl y ## d, x ## d; \
147 addl x ## d, y ## d; \
148 addl k+4*(2*(n))(CTX), x ## d; \
149 addl k+4*(2*(n)+1)(CTX), y ## d; \
150 xorl ba ## d, y ## d; \
153 xorl x ## d, ba ## d; \
158 #define encrypt_round3(ab, cd, n) \
159 g1g2_3(ab, cd, s0, s1, s2, s3, s0, s1, s2, s3, RX, RY); \
161 enc_round_end(ab ## 0, RX0, RY0, n); \
162 enc_round_end(ab ## 1, RX1, RY1, n); \
163 enc_round_end(ab ## 2, RX2, RY2, n);
165 #define decrypt_round3(ba, dc, n) \
166 g1g2_3(ba, dc, s1, s2, s3, s0, s3, s0, s1, s2, RY, RX); \
168 dec_round_end(ba ## 0, RX0, RY0, n); \
169 dec_round_end(ba ## 1, RX1, RY1, n); \
170 dec_round_end(ba ## 2, RX2, RY2, n);
172 #define encrypt_cycle3(ab, cd, n) \
173 encrypt_round3(ab, cd, n*2); \
174 encrypt_round3(ab, cd, (n*2)+1);
176 #define decrypt_cycle3(ba, dc, n) \
177 decrypt_round3(ba, dc, (n*2)+1); \
178 decrypt_round3(ba, dc, (n*2));
190 #define inpack3(in, n, xy, m) \
191 movq 4*(n)(in), xy ## 0; \
192 xorq w+4*m(CTX), xy ## 0; \
194 movq 4*(4+(n))(in), xy ## 1; \
195 xorq w+4*m(CTX), xy ## 1; \
197 movq 4*(8+(n))(in), xy ## 2; \
198 xorq w+4*m(CTX), xy ## 2;
200 #define outunpack3(op, out, n, xy, m) \
201 xorq w+4*m(CTX), xy ## 0; \
202 op ## q xy ## 0, 4*(n)(out); \
204 xorq w+4*m(CTX), xy ## 1; \
205 op ## q xy ## 1, 4*(4+(n))(out); \
207 xorq w+4*m(CTX), xy ## 2; \
208 op ## q xy ## 2, 4*(8+(n))(out);
210 #define inpack_enc3() \
211 inpack3(RIO, 0, RAB, 0); \
212 inpack3(RIO, 2, RCD, 2);
214 #define outunpack_enc3(op) \
215 outunpack3(op, RIO, 2, RAB, 6); \
216 outunpack3(op, RIO, 0, RCD, 4);
218 #define inpack_dec3() \
219 inpack3(RIO, 0, RAB, 4); \
223 inpack3(RIO, 2, RCD, 6); \
228 #define outunpack_dec3() \
232 outunpack3(mov, RIO, 0, RCD, 0); \
236 outunpack3(mov, RIO, 2, RAB, 2);
238 ENTRY(__twofish_enc_blk_3way)
243 * %rcx: bool, if true: xor output
249 pushq %rcx; /* bool xor */
250 pushq %rsi; /* dst */
255 encrypt_cycle3(RAB, CD, 0);
256 encrypt_cycle3(RAB, CD, 1);
257 encrypt_cycle3(RAB, CD, 2);
258 encrypt_cycle3(RAB, CD, 3);
259 encrypt_cycle3(RAB, CD, 4);
260 encrypt_cycle3(RAB, CD, 5);
261 encrypt_cycle3(RAB, CD, 6);
262 encrypt_cycle3(RAB, CD, 7);
266 popq RT1; /* bool xor */
285 ENDPROC(__twofish_enc_blk_3way)
287 ENTRY(twofish_dec_blk_3way)
297 pushq %rsi; /* dst */
302 decrypt_cycle3(RAB, CD, 7);
303 decrypt_cycle3(RAB, CD, 6);
304 decrypt_cycle3(RAB, CD, 5);
305 decrypt_cycle3(RAB, CD, 4);
306 decrypt_cycle3(RAB, CD, 3);
307 decrypt_cycle3(RAB, CD, 2);
308 decrypt_cycle3(RAB, CD, 1);
309 decrypt_cycle3(RAB, CD, 0);
320 ENDPROC(twofish_dec_blk_3way)