1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23.file "twofish-x86_64-asm-3way.S"
24.text
25
26
27#define s0 0
28#define s1 1024
29#define s2 2048
30#define s3 3072
31#define w 4096
32#define k 4128
33
34
35
36
37#define CTX %rdi
38#define RIO %rdx
39
40#define RAB0 %rax
41#define RAB1 %rbx
42#define RAB2 %rcx
43
44#define RAB0d %eax
45#define RAB1d %ebx
46#define RAB2d %ecx
47
48#define RAB0bh %ah
49#define RAB1bh %bh
50#define RAB2bh %ch
51
52#define RAB0bl %al
53#define RAB1bl %bl
54#define RAB2bl %cl
55
56#define RCD0 %r8
57#define RCD1 %r9
58#define RCD2 %r10
59
60#define RCD0d %r8d
61#define RCD1d %r9d
62#define RCD2d %r10d
63
64#define RX0 %rbp
65#define RX1 %r11
66#define RX2 %r12
67
68#define RX0d %ebp
69#define RX1d %r11d
70#define RX2d %r12d
71
72#define RY0 %r13
73#define RY1 %r14
74#define RY2 %r15
75
76#define RY0d %r13d
77#define RY1d %r14d
78#define RY2d %r15d
79
80#define RT0 %rdx
81#define RT1 %rsi
82
83#define RT0d %edx
84#define RT1d %esi
85
86#define do16bit_ror(rot, op1, op2, T0, T1, tmp1, tmp2, ab, dst) \
87 movzbl ab
88 movzbl ab
89 rorq $(rot), ab; \
90 op1
91 op2
92
93
94
95
96
97#define g1g2_3(ab, cd, Tx0, Tx1, Tx2, Tx3, Ty0, Ty1, Ty2, Ty3, x, y) \
98 \
99 do16bit_ror(32, mov, xor, Tx0, Tx1, RT0, x
100 do16bit_ror(48, mov, xor, Ty1, Ty2, RT0, y
101 \
102 do16bit_ror(32, mov, xor, Tx0, Tx1, RT0, x
103 do16bit_ror(48, mov, xor, Ty1, Ty2, RT0, y
104 \
105 do16bit_ror(32, mov, xor, Tx0, Tx1, RT0, x
106 do16bit_ror(48, mov, xor, Ty1, Ty2, RT0, y
107 \
108 \
109 do16bit_ror(32, xor, xor, Tx2, Tx3, RT0, RT1, ab
110 do16bit_ror(16, xor, xor, Ty3, Ty0, RT0, RT1, ab
111 xchgq cd
112 \
113 do16bit_ror(32, xor, xor, Tx2, Tx3, RT0, RT1, ab
114 do16bit_ror(16, xor, xor, Ty3, Ty0, RT0, RT1, ab
115 xchgq cd
116 \
117 do16bit_ror(32, xor, xor, Tx2, Tx3, RT0, RT1, ab
118 do16bit_ror(16, xor, xor, Ty3, Ty0, RT0, RT1, ab
119 xchgq cd
120
121#define enc_round_end(ab, x, y, n) \
122 addl y
123 addl x
124 addl k+4*(2*(n))(CTX), x
125 xorl ab
126 addl k+4*(2*(n)+1)(CTX), y
127 shrq $32, ab; \
128 roll $1, ab
129 xorl y
130 shlq $32, ab; \
131 rorl $1, x
132 orq x, ab;
133
134#define dec_round_end(ba, x, y, n) \
135 addl y
136 addl x
137 addl k+4*(2*(n))(CTX), x
138 addl k+4*(2*(n)+1)(CTX), y
139 xorl ba
140 shrq $32, ba; \
141 roll $1, ba
142 xorl x
143 shlq $32, ba; \
144 rorl $1, y
145 orq y, ba;
146
147#define encrypt_round3(ab, cd, n) \
148 g1g2_3(ab, cd, s0, s1, s2, s3, s0, s1, s2, s3, RX, RY); \
149 \
150 enc_round_end(ab
151 enc_round_end(ab
152 enc_round_end(ab
153
154#define decrypt_round3(ba, dc, n) \
155 g1g2_3(ba, dc, s1, s2, s3, s0, s3, s0, s1, s2, RY, RX); \
156 \
157 dec_round_end(ba
158 dec_round_end(ba
159 dec_round_end(ba
160
161#define encrypt_cycle3(ab, cd, n) \
162 encrypt_round3(ab, cd, n*2); \
163 encrypt_round3(ab, cd, (n*2)+1);
164
165#define decrypt_cycle3(ba, dc, n) \
166 decrypt_round3(ba, dc, (n*2)+1); \
167 decrypt_round3(ba, dc, (n*2));
168
169#define inpack3(in, n, xy, m) \
170 movq 4*(n)(in), xy
171 xorq w+4*m(CTX), xy
172 \
173 movq 4*(4+(n))(in), xy
174 xorq w+4*m(CTX), xy
175 \
176 movq 4*(8+(n))(in), xy
177 xorq w+4*m(CTX), xy
178
179#define outunpack3(op, out, n, xy, m) \
180 xorq w+4*m(CTX), xy
181 op
182 \
183 xorq w+4*m(CTX), xy
184 op
185 \
186 xorq w+4*m(CTX), xy
187 op
188
189#define inpack_enc3() \
190 inpack3(RIO, 0, RAB, 0); \
191 inpack3(RIO, 2, RCD, 2);
192
193#define outunpack_enc3(op) \
194 outunpack3(op, RIO, 2, RAB, 6); \
195 outunpack3(op, RIO, 0, RCD, 4);
196
197#define inpack_dec3() \
198 inpack3(RIO, 0, RAB, 4); \
199 rorq $32, RAB0; \
200 rorq $32, RAB1; \
201 rorq $32, RAB2; \
202 inpack3(RIO, 2, RCD, 6); \
203 rorq $32, RCD0; \
204 rorq $32, RCD1; \
205 rorq $32, RCD2;
206
207#define outunpack_dec3() \
208 rorq $32, RCD0; \
209 rorq $32, RCD1; \
210 rorq $32, RCD2; \
211 outunpack3(mov, RIO, 0, RCD, 0); \
212 rorq $32, RAB0; \
213 rorq $32, RAB1; \
214 rorq $32, RAB2; \
215 outunpack3(mov, RIO, 2, RAB, 2);
216
217.align 8
218.global __twofish_enc_blk_3way
219.type __twofish_enc_blk_3way,@function;
220
221__twofish_enc_blk_3way:
222
223
224
225
226
227
228 pushq %r15;
229 pushq %r14;
230 pushq %r13;
231 pushq %r12;
232 pushq %rbp;
233 pushq %rbx;
234
235 pushq %rcx;
236 pushq %rsi;
237
238 inpack_enc3();
239
240 encrypt_cycle3(RAB, RCD, 0);
241 encrypt_cycle3(RAB, RCD, 1);
242 encrypt_cycle3(RAB, RCD, 2);
243 encrypt_cycle3(RAB, RCD, 3);
244 encrypt_cycle3(RAB, RCD, 4);
245 encrypt_cycle3(RAB, RCD, 5);
246 encrypt_cycle3(RAB, RCD, 6);
247 encrypt_cycle3(RAB, RCD, 7);
248
249 popq RIO;
250 popq %rbp;
251
252 testb %bpl, %bpl;
253 jnz __enc_xor3;
254
255 outunpack_enc3(mov);
256
257 popq %rbx;
258 popq %rbp;
259 popq %r12;
260 popq %r13;
261 popq %r14;
262 popq %r15;
263 ret;
264
265__enc_xor3:
266 outunpack_enc3(xor);
267
268 popq %rbx;
269 popq %rbp;
270 popq %r12;
271 popq %r13;
272 popq %r14;
273 popq %r15;
274 ret;
275
276.global twofish_dec_blk_3way
277.type twofish_dec_blk_3way,@function;
278
279twofish_dec_blk_3way:
280
281
282
283
284
285 pushq %r15;
286 pushq %r14;
287 pushq %r13;
288 pushq %r12;
289 pushq %rbp;
290 pushq %rbx;
291
292 pushq %rsi;
293
294 inpack_dec3();
295
296 decrypt_cycle3(RAB, RCD, 7);
297 decrypt_cycle3(RAB, RCD, 6);
298 decrypt_cycle3(RAB, RCD, 5);
299 decrypt_cycle3(RAB, RCD, 4);
300 decrypt_cycle3(RAB, RCD, 3);
301 decrypt_cycle3(RAB, RCD, 2);
302 decrypt_cycle3(RAB, RCD, 1);
303 decrypt_cycle3(RAB, RCD, 0);
304
305 popq RIO;
306
307 outunpack_dec3();
308
309 popq %rbx;
310 popq %rbp;
311 popq %r12;
312 popq %r13;
313 popq %r14;
314 popq %r15;
315 ret;
316
317