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