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