Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 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 | * |
Jussi Kivilinna | 18be452 | 2013-04-08 21:51:00 +0300 | [diff] [blame] | 7 | * Copyright © 2012-2013 Jussi Kivilinna <jussi.kivilinna@iki.fi> |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 8 | * |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 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 | |
Jussi Kivilinna | d3f5188 | 2013-01-19 13:39:46 +0200 | [diff] [blame] | 26 | #include <linux/linkage.h> |
Josh Poimboeuf | 8691ccd | 2016-01-21 16:49:19 -0600 | [diff] [blame] | 27 | #include <asm/frame.h> |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 28 | #include "glue_helper-asm-avx.S" |
| 29 | |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 30 | .file "twofish-avx-x86_64-asm_64.S" |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 31 | |
Denys Vlasenko | e183914 | 2017-01-19 22:33:04 +0100 | [diff] [blame^] | 32 | .section .rodata.cst16.bswap128_mask, "aM", @progbits, 16 |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 33 | .align 16 |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 34 | .Lbswap128_mask: |
| 35 | .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 |
Denys Vlasenko | e183914 | 2017-01-19 22:33:04 +0100 | [diff] [blame^] | 36 | |
| 37 | .section .rodata.cst16.xts_gf128mul_and_shl1_mask, "aM", @progbits, 16 |
| 38 | .align 16 |
Jussi Kivilinna | 18be452 | 2013-04-08 21:51:00 +0300 | [diff] [blame] | 39 | .Lxts_gf128mul_and_shl1_mask: |
| 40 | .byte 0x87, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 41 | |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 42 | .text |
| 43 | |
| 44 | /* structure of crypto context */ |
| 45 | #define s0 0 |
| 46 | #define s1 1024 |
| 47 | #define s2 2048 |
| 48 | #define s3 3072 |
| 49 | #define w 4096 |
| 50 | #define k 4128 |
| 51 | |
| 52 | /********************************************************************** |
| 53 | 8-way AVX twofish |
| 54 | **********************************************************************/ |
| 55 | #define CTX %rdi |
| 56 | |
| 57 | #define RA1 %xmm0 |
| 58 | #define RB1 %xmm1 |
| 59 | #define RC1 %xmm2 |
| 60 | #define RD1 %xmm3 |
| 61 | |
| 62 | #define RA2 %xmm4 |
| 63 | #define RB2 %xmm5 |
| 64 | #define RC2 %xmm6 |
| 65 | #define RD2 %xmm7 |
| 66 | |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 67 | #define RX0 %xmm8 |
| 68 | #define RY0 %xmm9 |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 69 | |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 70 | #define RX1 %xmm10 |
| 71 | #define RY1 %xmm11 |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 72 | |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 73 | #define RK1 %xmm12 |
| 74 | #define RK2 %xmm13 |
| 75 | |
| 76 | #define RT %xmm14 |
| 77 | #define RR %xmm15 |
| 78 | |
| 79 | #define RID1 %rbp |
| 80 | #define RID1d %ebp |
| 81 | #define RID2 %rsi |
| 82 | #define RID2d %esi |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 83 | |
| 84 | #define RGI1 %rdx |
| 85 | #define RGI1bl %dl |
| 86 | #define RGI1bh %dh |
| 87 | #define RGI2 %rcx |
| 88 | #define RGI2bl %cl |
| 89 | #define RGI2bh %ch |
| 90 | |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 91 | #define RGI3 %rax |
| 92 | #define RGI3bl %al |
| 93 | #define RGI3bh %ah |
| 94 | #define RGI4 %rbx |
| 95 | #define RGI4bl %bl |
| 96 | #define RGI4bh %bh |
| 97 | |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 98 | #define RGS1 %r8 |
| 99 | #define RGS1d %r8d |
| 100 | #define RGS2 %r9 |
| 101 | #define RGS2d %r9d |
| 102 | #define RGS3 %r10 |
| 103 | #define RGS3d %r10d |
| 104 | |
| 105 | |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 106 | #define lookup_32bit(t0, t1, t2, t3, src, dst, interleave_op, il_reg) \ |
| 107 | movzbl src ## bl, RID1d; \ |
| 108 | movzbl src ## bh, RID2d; \ |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 109 | shrq $16, src; \ |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 110 | movl t0(CTX, RID1, 4), dst ## d; \ |
| 111 | movl t1(CTX, RID2, 4), RID2d; \ |
| 112 | movzbl src ## bl, RID1d; \ |
| 113 | xorl RID2d, dst ## d; \ |
| 114 | movzbl src ## bh, RID2d; \ |
| 115 | interleave_op(il_reg); \ |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 116 | xorl t2(CTX, RID1, 4), dst ## d; \ |
| 117 | xorl t3(CTX, RID2, 4), dst ## d; |
| 118 | |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 119 | #define dummy(d) /* do nothing */ |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 120 | |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 121 | #define shr_next(reg) \ |
| 122 | shrq $16, reg; |
| 123 | |
| 124 | #define G(gi1, gi2, x, t0, t1, t2, t3) \ |
| 125 | lookup_32bit(t0, t1, t2, t3, ##gi1, RGS1, shr_next, ##gi1); \ |
| 126 | lookup_32bit(t0, t1, t2, t3, ##gi2, RGS3, shr_next, ##gi2); \ |
| 127 | \ |
| 128 | lookup_32bit(t0, t1, t2, t3, ##gi1, RGS2, dummy, none); \ |
| 129 | shlq $32, RGS2; \ |
| 130 | orq RGS1, RGS2; \ |
| 131 | lookup_32bit(t0, t1, t2, t3, ##gi2, RGS1, dummy, none); \ |
| 132 | shlq $32, RGS1; \ |
| 133 | orq RGS1, RGS3; |
| 134 | |
| 135 | #define round_head_2(a, b, x1, y1, x2, y2) \ |
| 136 | vmovq b ## 1, RGI3; \ |
| 137 | vpextrq $1, b ## 1, RGI4; \ |
| 138 | \ |
| 139 | G(RGI1, RGI2, x1, s0, s1, s2, s3); \ |
| 140 | vmovq a ## 2, RGI1; \ |
| 141 | vpextrq $1, a ## 2, RGI2; \ |
| 142 | vmovq RGS2, x1; \ |
| 143 | vpinsrq $1, RGS3, x1, x1; \ |
| 144 | \ |
| 145 | G(RGI3, RGI4, y1, s1, s2, s3, s0); \ |
| 146 | vmovq b ## 2, RGI3; \ |
| 147 | vpextrq $1, b ## 2, RGI4; \ |
| 148 | vmovq RGS2, y1; \ |
| 149 | vpinsrq $1, RGS3, y1, y1; \ |
| 150 | \ |
| 151 | G(RGI1, RGI2, x2, s0, s1, s2, s3); \ |
| 152 | vmovq RGS2, x2; \ |
| 153 | vpinsrq $1, RGS3, x2, x2; \ |
| 154 | \ |
| 155 | G(RGI3, RGI4, y2, s1, s2, s3, s0); \ |
| 156 | vmovq RGS2, y2; \ |
| 157 | vpinsrq $1, RGS3, y2, y2; |
| 158 | |
| 159 | #define encround_tail(a, b, c, d, x, y, prerotate) \ |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 160 | vpaddd x, y, x; \ |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 161 | vpaddd x, RK1, RT;\ |
| 162 | prerotate(b); \ |
| 163 | vpxor RT, c, c; \ |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 164 | vpaddd y, x, y; \ |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 165 | vpaddd y, RK2, y; \ |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 166 | vpsrld $1, c, RT; \ |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 167 | vpslld $(32 - 1), c, c; \ |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 168 | vpor c, RT, c; \ |
| 169 | vpxor d, y, d; \ |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 170 | |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 171 | #define decround_tail(a, b, c, d, x, y, prerotate) \ |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 172 | vpaddd x, y, x; \ |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 173 | vpaddd x, RK1, RT;\ |
| 174 | prerotate(a); \ |
| 175 | vpxor RT, c, c; \ |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 176 | vpaddd y, x, y; \ |
| 177 | vpaddd y, RK2, y; \ |
| 178 | vpxor d, y, d; \ |
| 179 | vpsrld $1, d, y; \ |
| 180 | vpslld $(32 - 1), d, d; \ |
| 181 | vpor d, y, d; \ |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 182 | |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 183 | #define rotate_1l(x) \ |
| 184 | vpslld $1, x, RR; \ |
| 185 | vpsrld $(32 - 1), x, x; \ |
| 186 | vpor x, RR, x; |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 187 | |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 188 | #define preload_rgi(c) \ |
| 189 | vmovq c, RGI1; \ |
| 190 | vpextrq $1, c, RGI2; |
| 191 | |
| 192 | #define encrypt_round(n, a, b, c, d, preload, prerotate) \ |
| 193 | vbroadcastss (k+4*(2*(n)))(CTX), RK1; \ |
| 194 | vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \ |
| 195 | round_head_2(a, b, RX0, RY0, RX1, RY1); \ |
| 196 | encround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \ |
| 197 | preload(c ## 1); \ |
| 198 | encround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate); |
| 199 | |
| 200 | #define decrypt_round(n, a, b, c, d, preload, prerotate) \ |
| 201 | vbroadcastss (k+4*(2*(n)))(CTX), RK1; \ |
| 202 | vbroadcastss (k+4*(2*(n)+1))(CTX), RK2; \ |
| 203 | round_head_2(a, b, RX0, RY0, RX1, RY1); \ |
| 204 | decround_tail(a ## 1, b ## 1, c ## 1, d ## 1, RX0, RY0, prerotate); \ |
| 205 | preload(c ## 1); \ |
| 206 | decround_tail(a ## 2, b ## 2, c ## 2, d ## 2, RX1, RY1, prerotate); |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 207 | |
| 208 | #define encrypt_cycle(n) \ |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 209 | encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \ |
| 210 | encrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); |
| 211 | |
| 212 | #define encrypt_cycle_last(n) \ |
| 213 | encrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); \ |
| 214 | encrypt_round(((2*n) + 1), RC, RD, RA, RB, dummy, dummy); |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 215 | |
| 216 | #define decrypt_cycle(n) \ |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 217 | decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \ |
| 218 | decrypt_round((2*n), RA, RB, RC, RD, preload_rgi, rotate_1l); |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 219 | |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 220 | #define decrypt_cycle_last(n) \ |
| 221 | decrypt_round(((2*n) + 1), RC, RD, RA, RB, preload_rgi, rotate_1l); \ |
| 222 | decrypt_round((2*n), RA, RB, RC, RD, dummy, dummy); |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 223 | |
| 224 | #define transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \ |
| 225 | vpunpckldq x1, x0, t0; \ |
| 226 | vpunpckhdq x1, x0, t2; \ |
| 227 | vpunpckldq x3, x2, t1; \ |
| 228 | vpunpckhdq x3, x2, x3; \ |
| 229 | \ |
| 230 | vpunpcklqdq t1, t0, x0; \ |
| 231 | vpunpckhqdq t1, t0, x1; \ |
| 232 | vpunpcklqdq x3, t2, x2; \ |
| 233 | vpunpckhqdq x3, t2, x3; |
| 234 | |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 235 | #define inpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \ |
| 236 | vpxor x0, wkey, x0; \ |
| 237 | vpxor x1, wkey, x1; \ |
| 238 | vpxor x2, wkey, x2; \ |
| 239 | vpxor x3, wkey, x3; \ |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 240 | \ |
| 241 | transpose_4x4(x0, x1, x2, x3, t0, t1, t2) |
| 242 | |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 243 | #define outunpack_blocks(x0, x1, x2, x3, wkey, t0, t1, t2) \ |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 244 | transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \ |
| 245 | \ |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 246 | vpxor x0, wkey, x0; \ |
| 247 | vpxor x1, wkey, x1; \ |
| 248 | vpxor x2, wkey, x2; \ |
| 249 | vpxor x3, wkey, x3; |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 250 | |
| 251 | .align 8 |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 252 | __twofish_enc_blk8: |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 253 | /* input: |
| 254 | * %rdi: ctx, CTX |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 255 | * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: blocks |
| 256 | * output: |
| 257 | * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 258 | */ |
| 259 | |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 260 | vmovdqu w(CTX), RK1; |
| 261 | |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 262 | pushq %rbp; |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 263 | pushq %rbx; |
| 264 | pushq %rcx; |
| 265 | |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 266 | inpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2); |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 267 | preload_rgi(RA1); |
| 268 | rotate_1l(RD1); |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 269 | inpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2); |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 270 | rotate_1l(RD2); |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 271 | |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 272 | encrypt_cycle(0); |
| 273 | encrypt_cycle(1); |
| 274 | encrypt_cycle(2); |
| 275 | encrypt_cycle(3); |
| 276 | encrypt_cycle(4); |
| 277 | encrypt_cycle(5); |
| 278 | encrypt_cycle(6); |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 279 | encrypt_cycle_last(7); |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 280 | |
| 281 | vmovdqu (w+4*4)(CTX), RK1; |
| 282 | |
| 283 | popq %rcx; |
| 284 | popq %rbx; |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 285 | popq %rbp; |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 286 | |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 287 | outunpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2); |
| 288 | outunpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2); |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 289 | |
| 290 | ret; |
Jussi Kivilinna | d3f5188 | 2013-01-19 13:39:46 +0200 | [diff] [blame] | 291 | ENDPROC(__twofish_enc_blk8) |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 292 | |
| 293 | .align 8 |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 294 | __twofish_dec_blk8: |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 295 | /* input: |
| 296 | * %rdi: ctx, CTX |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 297 | * RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2: encrypted blocks |
| 298 | * output: |
| 299 | * RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2: decrypted blocks |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 300 | */ |
| 301 | |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 302 | vmovdqu (w+4*4)(CTX), RK1; |
| 303 | |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 304 | pushq %rbp; |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 305 | pushq %rbx; |
| 306 | |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 307 | inpack_blocks(RC1, RD1, RA1, RB1, RK1, RX0, RY0, RK2); |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 308 | preload_rgi(RC1); |
| 309 | rotate_1l(RA1); |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 310 | inpack_blocks(RC2, RD2, RA2, RB2, RK1, RX0, RY0, RK2); |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 311 | rotate_1l(RA2); |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 312 | |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 313 | decrypt_cycle(7); |
| 314 | decrypt_cycle(6); |
| 315 | decrypt_cycle(5); |
| 316 | decrypt_cycle(4); |
| 317 | decrypt_cycle(3); |
| 318 | decrypt_cycle(2); |
| 319 | decrypt_cycle(1); |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 320 | decrypt_cycle_last(0); |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 321 | |
| 322 | vmovdqu (w)(CTX), RK1; |
| 323 | |
| 324 | popq %rbx; |
Jussi Kivilinna | f94a73f | 2012-08-28 14:24:43 +0300 | [diff] [blame] | 325 | popq %rbp; |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 326 | |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 327 | outunpack_blocks(RA1, RB1, RC1, RD1, RK1, RX0, RY0, RK2); |
| 328 | outunpack_blocks(RA2, RB2, RC2, RD2, RK1, RX0, RY0, RK2); |
| 329 | |
| 330 | ret; |
Jussi Kivilinna | d3f5188 | 2013-01-19 13:39:46 +0200 | [diff] [blame] | 331 | ENDPROC(__twofish_dec_blk8) |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 332 | |
Jussi Kivilinna | d3f5188 | 2013-01-19 13:39:46 +0200 | [diff] [blame] | 333 | ENTRY(twofish_ecb_enc_8way) |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 334 | /* input: |
| 335 | * %rdi: ctx, CTX |
| 336 | * %rsi: dst |
| 337 | * %rdx: src |
| 338 | */ |
Josh Poimboeuf | 8691ccd | 2016-01-21 16:49:19 -0600 | [diff] [blame] | 339 | FRAME_BEGIN |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 340 | |
| 341 | movq %rsi, %r11; |
| 342 | |
| 343 | load_8way(%rdx, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2); |
| 344 | |
| 345 | call __twofish_enc_blk8; |
| 346 | |
| 347 | store_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2); |
| 348 | |
Josh Poimboeuf | 8691ccd | 2016-01-21 16:49:19 -0600 | [diff] [blame] | 349 | FRAME_END |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 350 | ret; |
Jussi Kivilinna | d3f5188 | 2013-01-19 13:39:46 +0200 | [diff] [blame] | 351 | ENDPROC(twofish_ecb_enc_8way) |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 352 | |
Jussi Kivilinna | d3f5188 | 2013-01-19 13:39:46 +0200 | [diff] [blame] | 353 | ENTRY(twofish_ecb_dec_8way) |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 354 | /* input: |
| 355 | * %rdi: ctx, CTX |
| 356 | * %rsi: dst |
| 357 | * %rdx: src |
| 358 | */ |
Josh Poimboeuf | 8691ccd | 2016-01-21 16:49:19 -0600 | [diff] [blame] | 359 | FRAME_BEGIN |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 360 | |
| 361 | movq %rsi, %r11; |
| 362 | |
| 363 | load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2); |
| 364 | |
| 365 | call __twofish_dec_blk8; |
| 366 | |
| 367 | store_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2); |
| 368 | |
Josh Poimboeuf | 8691ccd | 2016-01-21 16:49:19 -0600 | [diff] [blame] | 369 | FRAME_END |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 370 | ret; |
Jussi Kivilinna | d3f5188 | 2013-01-19 13:39:46 +0200 | [diff] [blame] | 371 | ENDPROC(twofish_ecb_dec_8way) |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 372 | |
Jussi Kivilinna | d3f5188 | 2013-01-19 13:39:46 +0200 | [diff] [blame] | 373 | ENTRY(twofish_cbc_dec_8way) |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 374 | /* input: |
| 375 | * %rdi: ctx, CTX |
| 376 | * %rsi: dst |
| 377 | * %rdx: src |
| 378 | */ |
Josh Poimboeuf | 8691ccd | 2016-01-21 16:49:19 -0600 | [diff] [blame] | 379 | FRAME_BEGIN |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 380 | |
| 381 | pushq %r12; |
| 382 | |
| 383 | movq %rsi, %r11; |
| 384 | movq %rdx, %r12; |
| 385 | |
| 386 | load_8way(%rdx, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2); |
| 387 | |
| 388 | call __twofish_dec_blk8; |
| 389 | |
| 390 | store_cbc_8way(%r12, %r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2); |
| 391 | |
| 392 | popq %r12; |
| 393 | |
Josh Poimboeuf | 8691ccd | 2016-01-21 16:49:19 -0600 | [diff] [blame] | 394 | FRAME_END |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 395 | ret; |
Jussi Kivilinna | d3f5188 | 2013-01-19 13:39:46 +0200 | [diff] [blame] | 396 | ENDPROC(twofish_cbc_dec_8way) |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 397 | |
Jussi Kivilinna | d3f5188 | 2013-01-19 13:39:46 +0200 | [diff] [blame] | 398 | ENTRY(twofish_ctr_8way) |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 399 | /* input: |
| 400 | * %rdi: ctx, CTX |
| 401 | * %rsi: dst |
| 402 | * %rdx: src |
| 403 | * %rcx: iv (little endian, 128bit) |
| 404 | */ |
Josh Poimboeuf | 8691ccd | 2016-01-21 16:49:19 -0600 | [diff] [blame] | 405 | FRAME_BEGIN |
Jussi Kivilinna | 8435a3c | 2012-10-20 15:06:46 +0300 | [diff] [blame] | 406 | |
| 407 | pushq %r12; |
| 408 | |
| 409 | movq %rsi, %r11; |
| 410 | movq %rdx, %r12; |
| 411 | |
| 412 | load_ctr_8way(%rcx, .Lbswap128_mask, RA1, RB1, RC1, RD1, RA2, RB2, RC2, |
| 413 | RD2, RX0, RX1, RY0); |
| 414 | |
| 415 | call __twofish_enc_blk8; |
| 416 | |
| 417 | store_ctr_8way(%r12, %r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2); |
| 418 | |
| 419 | popq %r12; |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 420 | |
Josh Poimboeuf | 8691ccd | 2016-01-21 16:49:19 -0600 | [diff] [blame] | 421 | FRAME_END |
Johannes Goetzfried | 107778b | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 422 | ret; |
Jussi Kivilinna | d3f5188 | 2013-01-19 13:39:46 +0200 | [diff] [blame] | 423 | ENDPROC(twofish_ctr_8way) |
Jussi Kivilinna | 18be452 | 2013-04-08 21:51:00 +0300 | [diff] [blame] | 424 | |
| 425 | ENTRY(twofish_xts_enc_8way) |
| 426 | /* input: |
| 427 | * %rdi: ctx, CTX |
| 428 | * %rsi: dst |
| 429 | * %rdx: src |
| 430 | * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸)) |
| 431 | */ |
Josh Poimboeuf | 8691ccd | 2016-01-21 16:49:19 -0600 | [diff] [blame] | 432 | FRAME_BEGIN |
Jussi Kivilinna | 18be452 | 2013-04-08 21:51:00 +0300 | [diff] [blame] | 433 | |
| 434 | movq %rsi, %r11; |
| 435 | |
| 436 | /* regs <= src, dst <= IVs, regs <= regs xor IVs */ |
| 437 | load_xts_8way(%rcx, %rdx, %rsi, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2, |
| 438 | RX0, RX1, RY0, .Lxts_gf128mul_and_shl1_mask); |
| 439 | |
| 440 | call __twofish_enc_blk8; |
| 441 | |
| 442 | /* dst <= regs xor IVs(in dst) */ |
| 443 | store_xts_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2); |
| 444 | |
Josh Poimboeuf | 8691ccd | 2016-01-21 16:49:19 -0600 | [diff] [blame] | 445 | FRAME_END |
Jussi Kivilinna | 18be452 | 2013-04-08 21:51:00 +0300 | [diff] [blame] | 446 | ret; |
| 447 | ENDPROC(twofish_xts_enc_8way) |
| 448 | |
| 449 | ENTRY(twofish_xts_dec_8way) |
| 450 | /* input: |
| 451 | * %rdi: ctx, CTX |
| 452 | * %rsi: dst |
| 453 | * %rdx: src |
| 454 | * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸)) |
| 455 | */ |
Josh Poimboeuf | 8691ccd | 2016-01-21 16:49:19 -0600 | [diff] [blame] | 456 | FRAME_BEGIN |
Jussi Kivilinna | 18be452 | 2013-04-08 21:51:00 +0300 | [diff] [blame] | 457 | |
| 458 | movq %rsi, %r11; |
| 459 | |
| 460 | /* regs <= src, dst <= IVs, regs <= regs xor IVs */ |
| 461 | load_xts_8way(%rcx, %rdx, %rsi, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2, |
| 462 | RX0, RX1, RY0, .Lxts_gf128mul_and_shl1_mask); |
| 463 | |
| 464 | call __twofish_dec_blk8; |
| 465 | |
| 466 | /* dst <= regs xor IVs(in dst) */ |
| 467 | store_xts_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2); |
| 468 | |
Josh Poimboeuf | 8691ccd | 2016-01-21 16:49:19 -0600 | [diff] [blame] | 469 | FRAME_END |
Jussi Kivilinna | 18be452 | 2013-04-08 21:51:00 +0300 | [diff] [blame] | 470 | ret; |
| 471 | ENDPROC(twofish_xts_dec_8way) |