]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/x86/crypto/twofish-avx-x86_64-asm_64.S
Merge tag 'for-linville-20130318' of git://github.com/kvalo/ath6kl
[karo-tx-linux.git] / arch / x86 / crypto / twofish-avx-x86_64-asm_64.S
1 /*
2  * Twofish Cipher 8-way parallel algorithm (AVX/x86_64)
3  *
4  * Copyright (C) 2012 Johannes Goetzfried
5  *     <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
6  *
7  * Copyright © 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
22  * USA
23  *
24  */
25
26 #include <linux/linkage.h>
27 #include "glue_helper-asm-avx.S"
28
29 .file "twofish-avx-x86_64-asm_64.S"
30
31 .data
32 .align 16
33
34 .Lbswap128_mask:
35         .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
36
37 .text
38
39 /* structure of crypto context */
40 #define s0      0
41 #define s1      1024
42 #define s2      2048
43 #define s3      3072
44 #define w       4096
45 #define k       4128
46
47 /**********************************************************************
48   8-way AVX twofish
49  **********************************************************************/
50 #define CTX %rdi
51
52 #define RA1 %xmm0
53 #define RB1 %xmm1
54 #define RC1 %xmm2
55 #define RD1 %xmm3
56
57 #define RA2 %xmm4
58 #define RB2 %xmm5
59 #define RC2 %xmm6
60 #define RD2 %xmm7
61
62 #define RX0 %xmm8
63 #define RY0 %xmm9
64
65 #define RX1 %xmm10
66 #define RY1 %xmm11
67
68 #define RK1 %xmm12
69 #define RK2 %xmm13
70
71 #define RT %xmm14
72 #define RR %xmm15
73
74 #define RID1  %rbp
75 #define RID1d %ebp
76 #define RID2  %rsi
77 #define RID2d %esi
78
79 #define RGI1   %rdx
80 #define RGI1bl %dl
81 #define RGI1bh %dh
82 #define RGI2   %rcx
83 #define RGI2bl %cl
84 #define RGI2bh %ch
85
86 #define RGI3   %rax
87 #define RGI3bl %al
88 #define RGI3bh %ah
89 #define RGI4   %rbx
90 #define RGI4bl %bl
91 #define RGI4bh %bh
92
93 #define RGS1  %r8
94 #define RGS1d %r8d
95 #define RGS2  %r9
96 #define RGS2d %r9d
97 #define RGS3  %r10
98 #define RGS3d %r10d
99
100
101 #define lookup_32bit(t0, t1, t2, t3, src, dst, interleave_op, il_reg) \
102         movzbl          src ## bl,        RID1d;     \
103         movzbl          src ## bh,        RID2d;     \
104         shrq $16,       src;                         \
105         movl            t0(CTX, RID1, 4), dst ## d;  \
106         movl            t1(CTX, RID2, 4), RID2d;     \
107         movzbl          src ## bl,        RID1d;     \
108         xorl            RID2d,            dst ## d;  \
109         movzbl          src ## bh,        RID2d;     \
110         interleave_op(il_reg);                       \
111         xorl            t2(CTX, RID1, 4), dst ## d;  \
112         xorl            t3(CTX, RID2, 4), dst ## d;
113
114 #define dummy(d) /* do nothing */
115
116 #define shr_next(reg) \
117         shrq $16,       reg;
118
119 #define G(gi1, gi2, x, t0, t1, t2, t3) \
120         lookup_32bit(t0, t1, t2, t3, ##gi1, RGS1, shr_next, ##gi1);  \
121         lookup_32bit(t0, t1, t2, t3, ##gi2, RGS3, shr_next, ##gi2);  \
122         \
123         lookup_32bit(t0, t1, t2, t3, ##gi1, RGS2, dummy, none);      \
124         shlq $32,       RGS2;                                        \
125         orq             RGS1, RGS2;                                  \
126         lookup_32bit(t0, t1, t2, t3, ##gi2, RGS1, dummy, none);      \
127         shlq $32,       RGS1;                                        \
128         orq             RGS1, RGS3;
129
130 #define round_head_2(a, b, x1, y1, x2, y2) \
131         vmovq           b ## 1, RGI3;           \
132         vpextrq $1,     b ## 1, RGI4;           \
133         \
134         G(RGI1, RGI2, x1, s0, s1, s2, s3);      \
135         vmovq           a ## 2, RGI1;           \
136         vpextrq $1,     a ## 2, RGI2;           \
137         vmovq           RGS2, x1;               \
138         vpinsrq $1,     RGS3, x1, x1;           \
139         \
140         G(RGI3, RGI4, y1, s1, s2, s3, s0);      \
141         vmovq           b ## 2, RGI3;           \
142         vpextrq $1,     b ## 2, RGI4;           \
143         vmovq           RGS2, y1;               \
144         vpinsrq $1,     RGS3, y1, y1;           \
145         \
146         G(RGI1, RGI2, x2, s0, s1, s2, s3);      \
147         vmovq           RGS2, x2;               \
148         vpinsrq $1,     RGS3, x2, x2;           \
149         \
150         G(RGI3, RGI4, y2, s1, s2, s3, s0);      \
151         vmovq           RGS2, y2;               \
152         vpinsrq $1,     RGS3, y2, y2;
153
154 #define encround_tail(a, b, c, d, x, y, prerotate) \
155         vpaddd                  x, y,   x; \
156         vpaddd                  x, RK1, RT;\
157         prerotate(b);                      \
158         vpxor                   RT, c,  c; \
159         vpaddd                  y, x,   y; \
160         vpaddd                  y, RK2, y; \
161         vpsrld $1,              c, RT;     \
162         vpslld $(32 - 1),       c, c;      \
163         vpor                    c, RT,  c; \
164         vpxor                   d, y,   d; \
165
166 #define decround_tail(a, b, c, d, x, y, prerotate) \
167         vpaddd                  x, y,   x; \
168         vpaddd                  x, RK1, RT;\
169         prerotate(a);                      \
170         vpxor                   RT, c,  c; \
171         vpaddd                  y, x,   y; \
172         vpaddd                  y, RK2, y; \
173         vpxor                   d, y,   d; \
174         vpsrld $1,              d, y;      \
175         vpslld $(32 - 1),       d, d;      \
176         vpor                    d, y,   d; \
177
178 #define rotate_1l(x) \
179         vpslld $1,              x, RR;     \
180         vpsrld $(32 - 1),       x, x;      \
181         vpor                    x, RR,  x;
182
183 #define preload_rgi(c) \
184         vmovq                   c, RGI1; \
185         vpextrq $1,             c, RGI2;
186
187 #define encrypt_round(n, a, b, c, d, preload, prerotate) \
188         vbroadcastss (k+4*(2*(n)))(CTX),   RK1;                  \
189         vbroadcastss (k+4*(2*(n)+1))(CTX), RK2;                  \
190         round_head_2(a, b, RX0, RY0, RX1, RY1);                  \
191         encround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
192         preload(c ## 1);                                         \
193         encround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
194
195 #define decrypt_round(n, a, b, c, d, preload, prerotate) \
196         vbroadcastss (k+4*(2*(n)))(CTX),   RK1;                  \
197         vbroadcastss (k+4*(2*(n)+1))(CTX), RK2;                  \
198         round_head_2(a, b, RX0, RY0, RX1, RY1);                  \
199         decround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \
200         preload(c ## 1);                                         \
201         decround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate);
202
203 #define encrypt_cycle(n) \
204         encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
205         encrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l);
206
207 #define encrypt_cycle_last(n) \
208         encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \
209         encrypt_round(((2*n) + 1), RC, RD, RA, RB, dummy, dummy);
210
211 #define decrypt_cycle(n) \
212         decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
213         decrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l);
214
215 #define decrypt_cycle_last(n) \
216         decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \
217         decrypt_round((2*n), RA, RB, RC, RD, dummy, dummy);
218
219 #define transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
220         vpunpckldq              x1, x0, t0; \
221         vpunpckhdq              x1, x0, t2; \
222         vpunpckldq              x3, x2, t1; \
223         vpunpckhdq              x3, x2, x3; \
224         \
225         vpunpcklqdq             t1, t0, x0; \
226         vpunpckhqdq             t1, t0, x1; \
227         vpunpcklqdq             x3, t2, x2; \
228         vpunpckhqdq             x3, t2, x3;
229
230 #define inpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
231         vpxor           x0, wkey, x0; \
232         vpxor           x1, wkey, x1; \
233         vpxor           x2, wkey, x2; \
234         vpxor           x3, wkey, x3; \
235         \
236         transpose_4x4(x0, x1, x2, x3, t0, t1, t2)
237
238 #define outunpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \
239         transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
240         \
241         vpxor           x0, wkey, x0; \
242         vpxor           x1, wkey, x1; \
243         vpxor           x2, wkey, x2; \
244         vpxor           x3, wkey, x3;
245
246 .align 8
247 __twofish_enc_blk8:
248         /* input:
249          *      %rdi: ctx, CTX
250          *      RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: blocks
251          * output:
252          *      RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
253          */
254
255         vmovdqu w(CTX), RK1;
256
257         pushq %rbp;
258         pushq %rbx;
259         pushq %rcx;
260
261         inpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
262         preload_rgi(RA1);
263         rotate_1l(RD1);
264         inpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
265         rotate_1l(RD2);
266
267         encrypt_cycle(0);
268         encrypt_cycle(1);
269         encrypt_cycle(2);
270         encrypt_cycle(3);
271         encrypt_cycle(4);
272         encrypt_cycle(5);
273         encrypt_cycle(6);
274         encrypt_cycle_last(7);
275
276         vmovdqu (w+4*4)(CTX), RK1;
277
278         popq %rcx;
279         popq %rbx;
280         popq %rbp;
281
282         outunpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
283         outunpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
284
285         ret;
286 ENDPROC(__twofish_enc_blk8)
287
288 .align 8
289 __twofish_dec_blk8:
290         /* input:
291          *      %rdi: ctx, CTX
292          *      RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks
293          * output:
294          *      RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: decrypted blocks
295          */
296
297         vmovdqu (w+4*4)(CTX), RK1;
298
299         pushq %rbp;
300         pushq %rbx;
301
302         inpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2);
303         preload_rgi(RC1);
304         rotate_1l(RA1);
305         inpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2);
306         rotate_1l(RA2);
307
308         decrypt_cycle(7);
309         decrypt_cycle(6);
310         decrypt_cycle(5);
311         decrypt_cycle(4);
312         decrypt_cycle(3);
313         decrypt_cycle(2);
314         decrypt_cycle(1);
315         decrypt_cycle_last(0);
316
317         vmovdqu (w)(CTX), RK1;
318
319         popq %rbx;
320         popq %rbp;
321
322         outunpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2);
323         outunpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2);
324
325         ret;
326 ENDPROC(__twofish_dec_blk8)
327
328 ENTRY(twofish_ecb_enc_8way)
329         /* input:
330          *      %rdi: ctx, CTX
331          *      %rsi: dst
332          *      %rdx: src
333          */
334
335         movq %rsi, %r11;
336
337         load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
338
339         call __twofish_enc_blk8;
340
341         store_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
342
343         ret;
344 ENDPROC(twofish_ecb_enc_8way)
345
346 ENTRY(twofish_ecb_dec_8way)
347         /* input:
348          *      %rdi: ctx, CTX
349          *      %rsi: dst
350          *      %rdx: src
351          */
352
353         movq %rsi, %r11;
354
355         load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
356
357         call __twofish_dec_blk8;
358
359         store_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
360
361         ret;
362 ENDPROC(twofish_ecb_dec_8way)
363
364 ENTRY(twofish_cbc_dec_8way)
365         /* input:
366          *      %rdi: ctx, CTX
367          *      %rsi: dst
368          *      %rdx: src
369          */
370
371         pushq %r12;
372
373         movq %rsi, %r11;
374         movq %rdx, %r12;
375
376         load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
377
378         call __twofish_dec_blk8;
379
380         store_cbc_8way(%r12, %r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
381
382         popq %r12;
383
384         ret;
385 ENDPROC(twofish_cbc_dec_8way)
386
387 ENTRY(twofish_ctr_8way)
388         /* input:
389          *      %rdi: ctx, CTX
390          *      %rsi: dst
391          *      %rdx: src
392          *      %rcx: iv (little endian, 128bit)
393          */
394
395         pushq %r12;
396
397         movq %rsi, %r11;
398         movq %rdx, %r12;
399
400         load_ctr_8way(%rcx, .Lbswap128_mask, RA1, RB1, RC1, RD1, RA2, RB2, RC2,
401                       RD2, RX0, RX1, RY0);
402
403         call __twofish_enc_blk8;
404
405         store_ctr_8way(%r12, %r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
406
407         popq %r12;
408
409         ret;
410 ENDPROC(twofish_ctr_8way)