| 1 |
#! /usr/bin/env perl |
| 2 |
# Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain a copy of the License at |
| 7 |
# |
| 8 |
# https://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
# |
| 17 |
# ==================================================================== |
| 18 |
# Written by Andy Polyakov <[email protected]> for the OpenSSL |
| 19 |
# project. Rights for redistribution and usage in source and binary |
| 20 |
# forms are granted according to the License. |
| 21 |
# ==================================================================== |
| 22 |
# |
| 23 |
# sha256/512_block procedure for x86_64. |
| 24 |
# |
| 25 |
# 40% improvement over compiler-generated code on Opteron. On EM64T |
| 26 |
# sha256 was observed to run >80% faster and sha512 - >40%. No magical |
| 27 |
# tricks, just straight implementation... I really wonder why gcc |
| 28 |
# [being armed with inline assembler] fails to generate as fast code. |
| 29 |
# The only thing which is cool about this module is that it's very |
| 30 |
# same instruction sequence used for both SHA-256 and SHA-512. In |
| 31 |
# former case the instructions operate on 32-bit operands, while in |
| 32 |
# latter - on 64-bit ones. All I had to do is to get one flavor right, |
| 33 |
# the other one passed the test right away:-) |
| 34 |
# |
| 35 |
# sha256_block runs in ~1005 cycles on Opteron, which gives you |
| 36 |
# asymptotic performance of 64*1000/1005=63.7MBps times CPU clock |
| 37 |
# frequency in GHz. sha512_block runs in ~1275 cycles, which results |
| 38 |
# in 128*1000/1275=100MBps per GHz. Is there room for improvement? |
| 39 |
# Well, if you compare it to IA-64 implementation, which maintains |
| 40 |
# X[16] in register bank[!], tends to 4 instructions per CPU clock |
| 41 |
# cycle and runs in 1003 cycles, 1275 is very good result for 3-way |
| 42 |
# issue Opteron pipeline and X[16] maintained in memory. So that *if* |
| 43 |
# there is a way to improve it, *then* the only way would be to try to |
| 44 |
# offload X[16] updates to SSE unit, but that would require "deeper" |
| 45 |
# loop unroll, which in turn would naturally cause size blow-up, not |
| 46 |
# to mention increased complexity! And once again, only *if* it's |
| 47 |
# actually possible to noticeably improve overall ILP, instruction |
| 48 |
# level parallelism, on a given CPU implementation in this case. |
| 49 |
# |
| 50 |
# Special note on Intel EM64T. While Opteron CPU exhibits perfect |
| 51 |
# performance ratio of 1.5 between 64- and 32-bit flavors [see above], |
| 52 |
# [currently available] EM64T CPUs apparently are far from it. On the |
| 53 |
# contrary, 64-bit version, sha512_block, is ~30% *slower* than 32-bit |
| 54 |
# sha256_block:-( This is presumably because 64-bit shifts/rotates |
| 55 |
# apparently are not atomic instructions, but implemented in microcode. |
| 56 |
# |
| 57 |
# May 2012. |
| 58 |
# |
| 59 |
# Optimization including one of Pavel Semjanov's ideas, alternative |
| 60 |
# Maj, resulted in >=5% improvement on most CPUs, +20% SHA256 and |
| 61 |
# unfortunately -2% SHA512 on P4 [which nobody should care about |
| 62 |
# that much]. |
| 63 |
# |
| 64 |
# June 2012. |
| 65 |
# |
| 66 |
# Add SIMD code paths, see below for improvement coefficients. SSSE3 |
| 67 |
# code path was not attempted for SHA512, because improvement is not |
| 68 |
# estimated to be high enough, noticeably less than 9%, to justify |
| 69 |
# the effort, not on pre-AVX processors. [Obviously with exclusion |
| 70 |
# for VIA Nano, but it has SHA512 instruction that is faster and |
| 71 |
# should be used instead.] For reference, corresponding estimated |
| 72 |
# upper limit for improvement for SSSE3 SHA256 is 28%. The fact that |
| 73 |
# higher coefficients are observed on VIA Nano and Bulldozer has more |
| 74 |
# to do with specifics of their architecture [which is topic for |
| 75 |
# separate discussion]. |
| 76 |
# |
| 77 |
# November 2012. |
| 78 |
# |
| 79 |
# Add AVX2 code path. Two consecutive input blocks are loaded to |
| 80 |
# 256-bit %ymm registers, with data from first block to least |
| 81 |
# significant 128-bit halves and data from second to most significant. |
| 82 |
# The data is then processed with same SIMD instruction sequence as |
| 83 |
# for AVX, but with %ymm as operands. Side effect is increased stack |
| 84 |
# frame, 448 additional bytes in SHA256 and 1152 in SHA512, and 1.2KB |
| 85 |
# code size increase. |
| 86 |
# |
| 87 |
# March 2014. |
| 88 |
# |
| 89 |
# Add support for Intel SHA Extensions. |
| 90 |
|
| 91 |
###################################################################### |
| 92 |
# Current performance in cycles per processed byte (less is better): |
| 93 |
# |
| 94 |
# SHA256 SSSE3 AVX/XOP(*) SHA512 AVX/XOP(*) |
| 95 |
# |
| 96 |
# AMD K8 14.9 - - 9.57 - |
| 97 |
# P4 17.3 - - 30.8 - |
| 98 |
# Core 2 15.6 13.8(+13%) - 9.97 - |
| 99 |
# Westmere 14.8 12.3(+19%) - 9.58 - |
| 100 |
# Sandy Bridge 17.4 14.2(+23%) 11.6(+50%(**)) 11.2 8.10(+38%(**)) |
| 101 |
# Ivy Bridge 12.6 10.5(+20%) 10.3(+22%) 8.17 7.22(+13%) |
| 102 |
# Haswell 12.2 9.28(+31%) 7.80(+56%) 7.66 5.40(+42%) |
| 103 |
# Skylake 11.4 9.03(+26%) 7.70(+48%) 7.25 5.20(+40%) |
| 104 |
# Bulldozer 21.1 13.6(+54%) 13.6(+54%(***)) 13.5 8.58(+57%) |
| 105 |
# Ryzen 11.0 9.02(+22%) 2.05(+440%) 7.05 5.67(+20%) |
| 106 |
# VIA Nano 23.0 16.5(+39%) - 14.7 - |
| 107 |
# Atom 23.0 18.9(+22%) - 14.7 - |
| 108 |
# Silvermont 27.4 20.6(+33%) - 17.5 - |
| 109 |
# Knights L 27.4 21.0(+30%) 19.6(+40%) 17.5 12.8(+37%) |
| 110 |
# Goldmont 18.9 14.3(+32%) 4.16(+350%) 12.0 - |
| 111 |
# |
| 112 |
# (*) whichever best applicable, including SHAEXT; |
| 113 |
# (**) switch from ror to shrd stands for fair share of improvement; |
| 114 |
# (***) execution time is fully determined by remaining integer-only |
| 115 |
# part, body_00_15; reducing the amount of SIMD instructions |
| 116 |
# below certain limit makes no difference/sense; to conserve |
| 117 |
# space SHA256 XOP code path is therefore omitted; |
| 118 |
# |
| 119 |
# Modified from upstream OpenSSL to remove the XOP code. |
| 120 |
|
| 121 |
my ($flavour, $output) = @ARGV; |
| 122 |
|
| 123 |
if ($output =~ /sha512-x86_64/) { |
| 124 |
$func="sha512_block_data_order"; |
| 125 |
$TABLE="K512"; |
| 126 |
$SZ=8; |
| 127 |
@ROT=($A,$B,$C,$D,$E,$F,$G,$H)=("%rax","%rbx","%rcx","%rdx", |
| 128 |
"%r8", "%r9", "%r10","%r11"); |
| 129 |
($T1,$a0,$a1,$a2,$a3)=("%r12","%r13","%r14","%r15","%rdi"); |
| 130 |
@Sigma0=(28,34,39); |
| 131 |
@Sigma1=(14,18,41); |
| 132 |
@sigma0=(1, 8, 7); |
| 133 |
@sigma1=(19,61, 6); |
| 134 |
$rounds=80; |
| 135 |
} else { |
| 136 |
$func="sha256_block_data_order"; |
| 137 |
$TABLE="K256"; |
| 138 |
$SZ=4; |
| 139 |
@ROT=($A,$B,$C,$D,$E,$F,$G,$H)=("%eax","%ebx","%ecx","%edx", |
| 140 |
"%r8d","%r9d","%r10d","%r11d"); |
| 141 |
($T1,$a0,$a1,$a2,$a3)=("%r12d","%r13d","%r14d","%r15d","%edi"); |
| 142 |
@Sigma0=( 2,13,22); |
| 143 |
@Sigma1=( 6,11,25); |
| 144 |
@sigma0=( 7,18, 3); |
| 145 |
@sigma1=(17,19,10); |
| 146 |
$rounds=64; |
| 147 |
} |
| 148 |
|
| 149 |
$win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/); |
| 150 |
|
| 151 |
$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1; |
| 152 |
( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or |
| 153 |
( $xlate="${dir}../../../perlasm/x86_64-xlate.pl" and -f $xlate) or |
| 154 |
die "can't locate x86_64-xlate.pl"; |
| 155 |
|
| 156 |
# In upstream, this is controlled by shelling out to the compiler to check |
| 157 |
# versions, but BoringSSL is intended to be used with pre-generated perlasm |
| 158 |
# output, so this isn't useful anyway. |
| 159 |
# |
| 160 |
# This file also has an AVX2 implementation, controlled by setting $avx to 2. |
| 161 |
# For now, we intentionally disable it. While it gives a 13-16% perf boost, the |
| 162 |
# CFI annotations are wrong. It allocates stack in a loop and should be |
| 163 |
# rewritten to avoid this. |
| 164 |
$avx = 1; |
| 165 |
$shaext = 1; |
| 166 |
|
| 167 |
open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""; |
| 168 |
*STDOUT=*OUT; |
| 169 |
|
| 170 |
$ctx="%rdi"; # 1st arg, zapped by $a3 |
| 171 |
$inp="%rsi"; # 2nd arg |
| 172 |
$Tbl="%rbp"; |
| 173 |
|
| 174 |
$_ctx="16*$SZ+0*8(%rsp)"; |
| 175 |
$_inp="16*$SZ+1*8(%rsp)"; |
| 176 |
$_end="16*$SZ+2*8(%rsp)"; |
| 177 |
$_rsp="`16*$SZ+3*8`(%rsp)"; |
| 178 |
$framesz="16*$SZ+4*8"; |
| 179 |
|
| 180 |
|
| 181 |
sub ROUND_00_15() |
| 182 |
{ my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_; |
| 183 |
my $STRIDE=$SZ; |
| 184 |
$STRIDE += 16 if ($i%(16/$SZ)==(16/$SZ-1)); |
| 185 |
|
| 186 |
$code.=<<___; |
| 187 |
ror \$`$Sigma1[2]-$Sigma1[1]`,$a0 |
| 188 |
mov $f,$a2 |
| 189 |
|
| 190 |
xor $e,$a0 |
| 191 |
ror \$`$Sigma0[2]-$Sigma0[1]`,$a1 |
| 192 |
xor $g,$a2 # f^g |
| 193 |
|
| 194 |
mov $T1,`$SZ*($i&0xf)`(%rsp) |
| 195 |
xor $a,$a1 |
| 196 |
and $e,$a2 # (f^g)&e |
| 197 |
|
| 198 |
ror \$`$Sigma1[1]-$Sigma1[0]`,$a0 |
| 199 |
add $h,$T1 # T1+=h |
| 200 |
xor $g,$a2 # Ch(e,f,g)=((f^g)&e)^g |
| 201 |
|
| 202 |
ror \$`$Sigma0[1]-$Sigma0[0]`,$a1 |
| 203 |
xor $e,$a0 |
| 204 |
add $a2,$T1 # T1+=Ch(e,f,g) |
| 205 |
|
| 206 |
mov $a,$a2 |
| 207 |
add ($Tbl),$T1 # T1+=K[round] |
| 208 |
xor $a,$a1 |
| 209 |
|
| 210 |
xor $b,$a2 # a^b, b^c in next round |
| 211 |
ror \$$Sigma1[0],$a0 # Sigma1(e) |
| 212 |
mov $b,$h |
| 213 |
|
| 214 |
and $a2,$a3 |
| 215 |
ror \$$Sigma0[0],$a1 # Sigma0(a) |
| 216 |
add $a0,$T1 # T1+=Sigma1(e) |
| 217 |
|
| 218 |
xor $a3,$h # h=Maj(a,b,c)=Ch(a^b,c,b) |
| 219 |
add $T1,$d # d+=T1 |
| 220 |
add $T1,$h # h+=T1 |
| 221 |
|
| 222 |
lea $STRIDE($Tbl),$Tbl # round++ |
| 223 |
___ |
| 224 |
$code.=<<___ if ($i<15); |
| 225 |
add $a1,$h # h+=Sigma0(a) |
| 226 |
___ |
| 227 |
($a2,$a3) = ($a3,$a2); |
| 228 |
} |
| 229 |
|
| 230 |
sub ROUND_16_XX() |
| 231 |
{ my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_; |
| 232 |
|
| 233 |
$code.=<<___; |
| 234 |
mov `$SZ*(($i+1)&0xf)`(%rsp),$a0 |
| 235 |
mov `$SZ*(($i+14)&0xf)`(%rsp),$a2 |
| 236 |
|
| 237 |
mov $a0,$T1 |
| 238 |
ror \$`$sigma0[1]-$sigma0[0]`,$a0 |
| 239 |
add $a1,$a # modulo-scheduled h+=Sigma0(a) |
| 240 |
mov $a2,$a1 |
| 241 |
ror \$`$sigma1[1]-$sigma1[0]`,$a2 |
| 242 |
|
| 243 |
xor $T1,$a0 |
| 244 |
shr \$$sigma0[2],$T1 |
| 245 |
ror \$$sigma0[0],$a0 |
| 246 |
xor $a1,$a2 |
| 247 |
shr \$$sigma1[2],$a1 |
| 248 |
|
| 249 |
ror \$$sigma1[0],$a2 |
| 250 |
xor $a0,$T1 # sigma0(X[(i+1)&0xf]) |
| 251 |
xor $a1,$a2 # sigma1(X[(i+14)&0xf]) |
| 252 |
add `$SZ*(($i+9)&0xf)`(%rsp),$T1 |
| 253 |
|
| 254 |
add `$SZ*($i&0xf)`(%rsp),$T1 |
| 255 |
mov $e,$a0 |
| 256 |
add $a2,$T1 |
| 257 |
mov $a,$a1 |
| 258 |
___ |
| 259 |
&ROUND_00_15(@_); |
| 260 |
} |
| 261 |
|
| 262 |
$code=<<___; |
| 263 |
.text |
| 264 |
|
| 265 |
.globl ${func}_nohw |
| 266 |
.type ${func}_nohw,\@function,3 |
| 267 |
.align 16 |
| 268 |
${func}_nohw: |
| 269 |
.cfi_startproc |
| 270 |
_CET_ENDBR |
| 271 |
mov %rsp,%rax # copy %rsp |
| 272 |
.cfi_def_cfa_register %rax |
| 273 |
push %rbx |
| 274 |
.cfi_push %rbx |
| 275 |
push %rbp |
| 276 |
.cfi_push %rbp |
| 277 |
push %r12 |
| 278 |
.cfi_push %r12 |
| 279 |
push %r13 |
| 280 |
.cfi_push %r13 |
| 281 |
push %r14 |
| 282 |
.cfi_push %r14 |
| 283 |
push %r15 |
| 284 |
.cfi_push %r15 |
| 285 |
shl \$4,%rdx # num*16 |
| 286 |
sub \$$framesz,%rsp |
| 287 |
lea ($inp,%rdx,$SZ),%rdx # inp+num*16*$SZ |
| 288 |
and \$-64,%rsp # align stack frame |
| 289 |
mov $ctx,$_ctx # save ctx, 1st arg |
| 290 |
mov $inp,$_inp # save inp, 2nd arh |
| 291 |
mov %rdx,$_end # save end pointer, "3rd" arg |
| 292 |
mov %rax,$_rsp # save copy of %rsp |
| 293 |
.cfi_cfa_expression $_rsp,deref,+8 |
| 294 |
.Lprologue: |
| 295 |
|
| 296 |
mov $SZ*0($ctx),$A |
| 297 |
mov $SZ*1($ctx),$B |
| 298 |
mov $SZ*2($ctx),$C |
| 299 |
mov $SZ*3($ctx),$D |
| 300 |
mov $SZ*4($ctx),$E |
| 301 |
mov $SZ*5($ctx),$F |
| 302 |
mov $SZ*6($ctx),$G |
| 303 |
mov $SZ*7($ctx),$H |
| 304 |
jmp .Lloop |
| 305 |
|
| 306 |
.align 16 |
| 307 |
.Lloop: |
| 308 |
mov $B,$a3 |
| 309 |
lea $TABLE(%rip),$Tbl |
| 310 |
xor $C,$a3 # magic |
| 311 |
___ |
| 312 |
for($i=0;$i<16;$i++) { |
| 313 |
$code.=" mov $SZ*$i($inp),$T1\n"; |
| 314 |
$code.=" mov @ROT[4],$a0\n"; |
| 315 |
$code.=" mov @ROT[0],$a1\n"; |
| 316 |
$code.=" bswap $T1\n"; |
| 317 |
&ROUND_00_15($i,@ROT); |
| 318 |
unshift(@ROT,pop(@ROT)); |
| 319 |
} |
| 320 |
$code.=<<___; |
| 321 |
jmp .Lrounds_16_xx |
| 322 |
.align 16 |
| 323 |
.Lrounds_16_xx: |
| 324 |
___ |
| 325 |
for(;$i<32;$i++) { |
| 326 |
&ROUND_16_XX($i,@ROT); |
| 327 |
unshift(@ROT,pop(@ROT)); |
| 328 |
} |
| 329 |
|
| 330 |
$code.=<<___; |
| 331 |
cmpb \$0,`$SZ-1`($Tbl) |
| 332 |
jnz .Lrounds_16_xx |
| 333 |
|
| 334 |
mov $_ctx,$ctx |
| 335 |
add $a1,$A # modulo-scheduled h+=Sigma0(a) |
| 336 |
lea 16*$SZ($inp),$inp |
| 337 |
|
| 338 |
add $SZ*0($ctx),$A |
| 339 |
add $SZ*1($ctx),$B |
| 340 |
add $SZ*2($ctx),$C |
| 341 |
add $SZ*3($ctx),$D |
| 342 |
add $SZ*4($ctx),$E |
| 343 |
add $SZ*5($ctx),$F |
| 344 |
add $SZ*6($ctx),$G |
| 345 |
add $SZ*7($ctx),$H |
| 346 |
|
| 347 |
cmp $_end,$inp |
| 348 |
|
| 349 |
mov $A,$SZ*0($ctx) |
| 350 |
mov $B,$SZ*1($ctx) |
| 351 |
mov $C,$SZ*2($ctx) |
| 352 |
mov $D,$SZ*3($ctx) |
| 353 |
mov $E,$SZ*4($ctx) |
| 354 |
mov $F,$SZ*5($ctx) |
| 355 |
mov $G,$SZ*6($ctx) |
| 356 |
mov $H,$SZ*7($ctx) |
| 357 |
jb .Lloop |
| 358 |
|
| 359 |
mov $_rsp,%rsi |
| 360 |
.cfi_def_cfa %rsi,8 |
| 361 |
mov -48(%rsi),%r15 |
| 362 |
.cfi_restore %r15 |
| 363 |
mov -40(%rsi),%r14 |
| 364 |
.cfi_restore %r14 |
| 365 |
mov -32(%rsi),%r13 |
| 366 |
.cfi_restore %r13 |
| 367 |
mov -24(%rsi),%r12 |
| 368 |
.cfi_restore %r12 |
| 369 |
mov -16(%rsi),%rbp |
| 370 |
.cfi_restore %rbp |
| 371 |
mov -8(%rsi),%rbx |
| 372 |
.cfi_restore %rbx |
| 373 |
lea (%rsi),%rsp |
| 374 |
.cfi_def_cfa_register %rsp |
| 375 |
.Lepilogue: |
| 376 |
ret |
| 377 |
.cfi_endproc |
| 378 |
.size ${func}_nohw,.-${func}_nohw |
| 379 |
___ |
| 380 |
|
| 381 |
if ($SZ==4) { |
| 382 |
$code.=<<___; |
| 383 |
.section .rodata |
| 384 |
.align 64 |
| 385 |
.type $TABLE,\@object |
| 386 |
$TABLE: |
| 387 |
.long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5 |
| 388 |
.long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5 |
| 389 |
.long 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5 |
| 390 |
.long 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5 |
| 391 |
.long 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3 |
| 392 |
.long 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3 |
| 393 |
.long 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174 |
| 394 |
.long 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174 |
| 395 |
.long 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc |
| 396 |
.long 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc |
| 397 |
.long 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da |
| 398 |
.long 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da |
| 399 |
.long 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7 |
| 400 |
.long 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7 |
| 401 |
.long 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967 |
| 402 |
.long 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967 |
| 403 |
.long 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13 |
| 404 |
.long 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13 |
| 405 |
.long 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85 |
| 406 |
.long 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85 |
| 407 |
.long 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3 |
| 408 |
.long 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3 |
| 409 |
.long 0xd192e819,0xd6990624,0xf40e3585,0x106aa070 |
| 410 |
.long 0xd192e819,0xd6990624,0xf40e3585,0x106aa070 |
| 411 |
.long 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5 |
| 412 |
.long 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5 |
| 413 |
.long 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3 |
| 414 |
.long 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3 |
| 415 |
.long 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208 |
| 416 |
.long 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208 |
| 417 |
.long 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 |
| 418 |
.long 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 |
| 419 |
|
| 420 |
.long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f |
| 421 |
.long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f |
| 422 |
.long 0x03020100,0x0b0a0908,0xffffffff,0xffffffff |
| 423 |
.long 0x03020100,0x0b0a0908,0xffffffff,0xffffffff |
| 424 |
.long 0xffffffff,0xffffffff,0x03020100,0x0b0a0908 |
| 425 |
.long 0xffffffff,0xffffffff,0x03020100,0x0b0a0908 |
| 426 |
.asciz "SHA256 block transform for x86_64, CRYPTOGAMS by <appro\@openssl.org>" |
| 427 |
.text |
| 428 |
___ |
| 429 |
} else { |
| 430 |
$code.=<<___; |
| 431 |
.section .rodata |
| 432 |
.align 64 |
| 433 |
.type $TABLE,\@object |
| 434 |
$TABLE: |
| 435 |
.quad 0x428a2f98d728ae22,0x7137449123ef65cd |
| 436 |
.quad 0x428a2f98d728ae22,0x7137449123ef65cd |
| 437 |
.quad 0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc |
| 438 |
.quad 0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc |
| 439 |
.quad 0x3956c25bf348b538,0x59f111f1b605d019 |
| 440 |
.quad 0x3956c25bf348b538,0x59f111f1b605d019 |
| 441 |
.quad 0x923f82a4af194f9b,0xab1c5ed5da6d8118 |
| 442 |
.quad 0x923f82a4af194f9b,0xab1c5ed5da6d8118 |
| 443 |
.quad 0xd807aa98a3030242,0x12835b0145706fbe |
| 444 |
.quad 0xd807aa98a3030242,0x12835b0145706fbe |
| 445 |
.quad 0x243185be4ee4b28c,0x550c7dc3d5ffb4e2 |
| 446 |
.quad 0x243185be4ee4b28c,0x550c7dc3d5ffb4e2 |
| 447 |
.quad 0x72be5d74f27b896f,0x80deb1fe3b1696b1 |
| 448 |
.quad 0x72be5d74f27b896f,0x80deb1fe3b1696b1 |
| 449 |
.quad 0x9bdc06a725c71235,0xc19bf174cf692694 |
| 450 |
.quad 0x9bdc06a725c71235,0xc19bf174cf692694 |
| 451 |
.quad 0xe49b69c19ef14ad2,0xefbe4786384f25e3 |
| 452 |
.quad 0xe49b69c19ef14ad2,0xefbe4786384f25e3 |
| 453 |
.quad 0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65 |
| 454 |
.quad 0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65 |
| 455 |
.quad 0x2de92c6f592b0275,0x4a7484aa6ea6e483 |
| 456 |
.quad 0x2de92c6f592b0275,0x4a7484aa6ea6e483 |
| 457 |
.quad 0x5cb0a9dcbd41fbd4,0x76f988da831153b5 |
| 458 |
.quad 0x5cb0a9dcbd41fbd4,0x76f988da831153b5 |
| 459 |
.quad 0x983e5152ee66dfab,0xa831c66d2db43210 |
| 460 |
.quad 0x983e5152ee66dfab,0xa831c66d2db43210 |
| 461 |
.quad 0xb00327c898fb213f,0xbf597fc7beef0ee4 |
| 462 |
.quad 0xb00327c898fb213f,0xbf597fc7beef0ee4 |
| 463 |
.quad 0xc6e00bf33da88fc2,0xd5a79147930aa725 |
| 464 |
.quad 0xc6e00bf33da88fc2,0xd5a79147930aa725 |
| 465 |
.quad 0x06ca6351e003826f,0x142929670a0e6e70 |
| 466 |
.quad 0x06ca6351e003826f,0x142929670a0e6e70 |
| 467 |
.quad 0x27b70a8546d22ffc,0x2e1b21385c26c926 |
| 468 |
.quad 0x27b70a8546d22ffc,0x2e1b21385c26c926 |
| 469 |
.quad 0x4d2c6dfc5ac42aed,0x53380d139d95b3df |
| 470 |
.quad 0x4d2c6dfc5ac42aed,0x53380d139d95b3df |
| 471 |
.quad 0x650a73548baf63de,0x766a0abb3c77b2a8 |
| 472 |
.quad 0x650a73548baf63de,0x766a0abb3c77b2a8 |
| 473 |
.quad 0x81c2c92e47edaee6,0x92722c851482353b |
| 474 |
.quad 0x81c2c92e47edaee6,0x92722c851482353b |
| 475 |
.quad 0xa2bfe8a14cf10364,0xa81a664bbc423001 |
| 476 |
.quad 0xa2bfe8a14cf10364,0xa81a664bbc423001 |
| 477 |
.quad 0xc24b8b70d0f89791,0xc76c51a30654be30 |
| 478 |
.quad 0xc24b8b70d0f89791,0xc76c51a30654be30 |
| 479 |
.quad 0xd192e819d6ef5218,0xd69906245565a910 |
| 480 |
.quad 0xd192e819d6ef5218,0xd69906245565a910 |
| 481 |
.quad 0xf40e35855771202a,0x106aa07032bbd1b8 |
| 482 |
.quad 0xf40e35855771202a,0x106aa07032bbd1b8 |
| 483 |
.quad 0x19a4c116b8d2d0c8,0x1e376c085141ab53 |
| 484 |
.quad 0x19a4c116b8d2d0c8,0x1e376c085141ab53 |
| 485 |
.quad 0x2748774cdf8eeb99,0x34b0bcb5e19b48a8 |
| 486 |
.quad 0x2748774cdf8eeb99,0x34b0bcb5e19b48a8 |
| 487 |
.quad 0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb |
| 488 |
.quad 0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb |
| 489 |
.quad 0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3 |
| 490 |
.quad 0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3 |
| 491 |
.quad 0x748f82ee5defb2fc,0x78a5636f43172f60 |
| 492 |
.quad 0x748f82ee5defb2fc,0x78a5636f43172f60 |
| 493 |
.quad 0x84c87814a1f0ab72,0x8cc702081a6439ec |
| 494 |
.quad 0x84c87814a1f0ab72,0x8cc702081a6439ec |
| 495 |
.quad 0x90befffa23631e28,0xa4506cebde82bde9 |
| 496 |
.quad 0x90befffa23631e28,0xa4506cebde82bde9 |
| 497 |
.quad 0xbef9a3f7b2c67915,0xc67178f2e372532b |
| 498 |
.quad 0xbef9a3f7b2c67915,0xc67178f2e372532b |
| 499 |
.quad 0xca273eceea26619c,0xd186b8c721c0c207 |
| 500 |
.quad 0xca273eceea26619c,0xd186b8c721c0c207 |
| 501 |
.quad 0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178 |
| 502 |
.quad 0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178 |
| 503 |
.quad 0x06f067aa72176fba,0x0a637dc5a2c898a6 |
| 504 |
.quad 0x06f067aa72176fba,0x0a637dc5a2c898a6 |
| 505 |
.quad 0x113f9804bef90dae,0x1b710b35131c471b |
| 506 |
.quad 0x113f9804bef90dae,0x1b710b35131c471b |
| 507 |
.quad 0x28db77f523047d84,0x32caab7b40c72493 |
| 508 |
.quad 0x28db77f523047d84,0x32caab7b40c72493 |
| 509 |
.quad 0x3c9ebe0a15c9bebc,0x431d67c49c100d4c |
| 510 |
.quad 0x3c9ebe0a15c9bebc,0x431d67c49c100d4c |
| 511 |
.quad 0x4cc5d4becb3e42b6,0x597f299cfc657e2a |
| 512 |
.quad 0x4cc5d4becb3e42b6,0x597f299cfc657e2a |
| 513 |
.quad 0x5fcb6fab3ad6faec,0x6c44198c4a475817 |
| 514 |
.quad 0x5fcb6fab3ad6faec,0x6c44198c4a475817 |
| 515 |
|
| 516 |
.quad 0x0001020304050607,0x08090a0b0c0d0e0f |
| 517 |
.quad 0x0001020304050607,0x08090a0b0c0d0e0f |
| 518 |
.asciz "SHA512 block transform for x86_64, CRYPTOGAMS by <appro\@openssl.org>" |
| 519 |
.text |
| 520 |
___ |
| 521 |
} |
| 522 |
|
| 523 |
###################################################################### |
| 524 |
# SIMD code paths |
| 525 |
# |
| 526 |
if ($SZ==4 && $shaext) {{{ |
| 527 |
###################################################################### |
| 528 |
# Intel SHA Extensions implementation of SHA256 update function. |
| 529 |
# |
| 530 |
my ($ctx,$inp,$num,$Tbl)=("%rdi","%rsi","%rdx","%rcx"); |
| 531 |
|
| 532 |
my ($Wi,$ABEF,$CDGH,$TMP,$BSWAP,$ABEF_SAVE,$CDGH_SAVE)=map("%xmm$_",(0..2,7..10)); |
| 533 |
my @MSG=map("%xmm$_",(3..6)); |
| 534 |
|
| 535 |
$code.=<<___; |
| 536 |
.globl sha256_block_data_order_hw |
| 537 |
.type sha256_block_data_order_hw,\@function,3 |
| 538 |
.align 64 |
| 539 |
sha256_block_data_order_hw: |
| 540 |
.cfi_startproc |
| 541 |
_CET_ENDBR |
| 542 |
___ |
| 543 |
$code.=<<___ if ($win64); |
| 544 |
lea `-8-5*16`(%rsp),%rsp |
| 545 |
movaps %xmm6,-8-5*16(%rax) |
| 546 |
movaps %xmm7,-8-4*16(%rax) |
| 547 |
movaps %xmm8,-8-3*16(%rax) |
| 548 |
movaps %xmm9,-8-2*16(%rax) |
| 549 |
movaps %xmm10,-8-1*16(%rax) |
| 550 |
.Lprologue_shaext: |
| 551 |
___ |
| 552 |
$code.=<<___; |
| 553 |
lea K256+0x80(%rip),$Tbl |
| 554 |
movdqu ($ctx),$ABEF # DCBA |
| 555 |
movdqu 16($ctx),$CDGH # HGFE |
| 556 |
movdqa 0x200-0x80($Tbl),$TMP # byte swap mask |
| 557 |
|
| 558 |
pshufd \$0x1b,$ABEF,$Wi # ABCD |
| 559 |
pshufd \$0xb1,$ABEF,$ABEF # CDAB |
| 560 |
pshufd \$0x1b,$CDGH,$CDGH # EFGH |
| 561 |
movdqa $TMP,$BSWAP # offload |
| 562 |
palignr \$8,$CDGH,$ABEF # ABEF |
| 563 |
punpcklqdq $Wi,$CDGH # CDGH |
| 564 |
jmp .Loop_shaext |
| 565 |
|
| 566 |
.align 16 |
| 567 |
.Loop_shaext: |
| 568 |
movdqu ($inp),@MSG[0] |
| 569 |
movdqu 0x10($inp),@MSG[1] |
| 570 |
movdqu 0x20($inp),@MSG[2] |
| 571 |
pshufb $TMP,@MSG[0] |
| 572 |
movdqu 0x30($inp),@MSG[3] |
| 573 |
|
| 574 |
movdqa 0*32-0x80($Tbl),$Wi |
| 575 |
paddd @MSG[0],$Wi |
| 576 |
pshufb $TMP,@MSG[1] |
| 577 |
movdqa $CDGH,$CDGH_SAVE # offload |
| 578 |
sha256rnds2 $ABEF,$CDGH # 0-3 |
| 579 |
pshufd \$0x0e,$Wi,$Wi |
| 580 |
nop |
| 581 |
movdqa $ABEF,$ABEF_SAVE # offload |
| 582 |
sha256rnds2 $CDGH,$ABEF |
| 583 |
|
| 584 |
movdqa 1*32-0x80($Tbl),$Wi |
| 585 |
paddd @MSG[1],$Wi |
| 586 |
pshufb $TMP,@MSG[2] |
| 587 |
sha256rnds2 $ABEF,$CDGH # 4-7 |
| 588 |
pshufd \$0x0e,$Wi,$Wi |
| 589 |
lea 0x40($inp),$inp |
| 590 |
sha256msg1 @MSG[1],@MSG[0] |
| 591 |
sha256rnds2 $CDGH,$ABEF |
| 592 |
|
| 593 |
movdqa 2*32-0x80($Tbl),$Wi |
| 594 |
paddd @MSG[2],$Wi |
| 595 |
pshufb $TMP,@MSG[3] |
| 596 |
sha256rnds2 $ABEF,$CDGH # 8-11 |
| 597 |
pshufd \$0x0e,$Wi,$Wi |
| 598 |
movdqa @MSG[3],$TMP |
| 599 |
palignr \$4,@MSG[2],$TMP |
| 600 |
nop |
| 601 |
paddd $TMP,@MSG[0] |
| 602 |
sha256msg1 @MSG[2],@MSG[1] |
| 603 |
sha256rnds2 $CDGH,$ABEF |
| 604 |
|
| 605 |
movdqa 3*32-0x80($Tbl),$Wi |
| 606 |
paddd @MSG[3],$Wi |
| 607 |
sha256msg2 @MSG[3],@MSG[0] |
| 608 |
sha256rnds2 $ABEF,$CDGH # 12-15 |
| 609 |
pshufd \$0x0e,$Wi,$Wi |
| 610 |
movdqa @MSG[0],$TMP |
| 611 |
palignr \$4,@MSG[3],$TMP |
| 612 |
nop |
| 613 |
paddd $TMP,@MSG[1] |
| 614 |
sha256msg1 @MSG[3],@MSG[2] |
| 615 |
sha256rnds2 $CDGH,$ABEF |
| 616 |
___ |
| 617 |
for($i=4;$i<16-3;$i++) { |
| 618 |
$code.=<<___; |
| 619 |
movdqa $i*32-0x80($Tbl),$Wi |
| 620 |
paddd @MSG[0],$Wi |
| 621 |
sha256msg2 @MSG[0],@MSG[1] |
| 622 |
sha256rnds2 $ABEF,$CDGH # 16-19... |
| 623 |
pshufd \$0x0e,$Wi,$Wi |
| 624 |
movdqa @MSG[1],$TMP |
| 625 |
palignr \$4,@MSG[0],$TMP |
| 626 |
nop |
| 627 |
paddd $TMP,@MSG[2] |
| 628 |
sha256msg1 @MSG[0],@MSG[3] |
| 629 |
sha256rnds2 $CDGH,$ABEF |
| 630 |
___ |
| 631 |
push(@MSG,shift(@MSG)); |
| 632 |
} |
| 633 |
$code.=<<___; |
| 634 |
movdqa 13*32-0x80($Tbl),$Wi |
| 635 |
paddd @MSG[0],$Wi |
| 636 |
sha256msg2 @MSG[0],@MSG[1] |
| 637 |
sha256rnds2 $ABEF,$CDGH # 52-55 |
| 638 |
pshufd \$0x0e,$Wi,$Wi |
| 639 |
movdqa @MSG[1],$TMP |
| 640 |
palignr \$4,@MSG[0],$TMP |
| 641 |
sha256rnds2 $CDGH,$ABEF |
| 642 |
paddd $TMP,@MSG[2] |
| 643 |
|
| 644 |
movdqa 14*32-0x80($Tbl),$Wi |
| 645 |
paddd @MSG[1],$Wi |
| 646 |
sha256rnds2 $ABEF,$CDGH # 56-59 |
| 647 |
pshufd \$0x0e,$Wi,$Wi |
| 648 |
sha256msg2 @MSG[1],@MSG[2] |
| 649 |
movdqa $BSWAP,$TMP |
| 650 |
sha256rnds2 $CDGH,$ABEF |
| 651 |
|
| 652 |
movdqa 15*32-0x80($Tbl),$Wi |
| 653 |
paddd @MSG[2],$Wi |
| 654 |
nop |
| 655 |
sha256rnds2 $ABEF,$CDGH # 60-63 |
| 656 |
pshufd \$0x0e,$Wi,$Wi |
| 657 |
dec $num |
| 658 |
nop |
| 659 |
sha256rnds2 $CDGH,$ABEF |
| 660 |
|
| 661 |
paddd $CDGH_SAVE,$CDGH |
| 662 |
paddd $ABEF_SAVE,$ABEF |
| 663 |
jnz .Loop_shaext |
| 664 |
|
| 665 |
pshufd \$0xb1,$CDGH,$CDGH # DCHG |
| 666 |
pshufd \$0x1b,$ABEF,$TMP # FEBA |
| 667 |
pshufd \$0xb1,$ABEF,$ABEF # BAFE |
| 668 |
punpckhqdq $CDGH,$ABEF # DCBA |
| 669 |
palignr \$8,$TMP,$CDGH # HGFE |
| 670 |
|
| 671 |
movdqu $ABEF,($ctx) |
| 672 |
movdqu $CDGH,16($ctx) |
| 673 |
___ |
| 674 |
$code.=<<___ if ($win64); |
| 675 |
movaps -8-5*16(%rax),%xmm6 |
| 676 |
movaps -8-4*16(%rax),%xmm7 |
| 677 |
movaps -8-3*16(%rax),%xmm8 |
| 678 |
movaps -8-2*16(%rax),%xmm9 |
| 679 |
movaps -8-1*16(%rax),%xmm10 |
| 680 |
mov %rax,%rsp |
| 681 |
.Lepilogue_shaext: |
| 682 |
___ |
| 683 |
$code.=<<___; |
| 684 |
ret |
| 685 |
.cfi_endproc |
| 686 |
.size sha256_block_data_order_hw,.-sha256_block_data_order_hw |
| 687 |
___ |
| 688 |
}}} |
| 689 |
{{{ |
| 690 |
|
| 691 |
my $a4=$T1; |
| 692 |
my ($a,$b,$c,$d,$e,$f,$g,$h); |
| 693 |
|
| 694 |
sub AUTOLOAD() # thunk [simplified] 32-bit style perlasm |
| 695 |
{ my $opcode = $AUTOLOAD; $opcode =~ s/.*:://; |
| 696 |
my $arg = pop; |
| 697 |
$arg = "\$$arg" if ($arg*1 eq $arg); |
| 698 |
$code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n"; |
| 699 |
} |
| 700 |
|
| 701 |
sub body_00_15 () { |
| 702 |
( |
| 703 |
'($a,$b,$c,$d,$e,$f,$g,$h)=@ROT;'. |
| 704 |
|
| 705 |
'&ror ($a0,$Sigma1[2]-$Sigma1[1])', |
| 706 |
'&mov ($a,$a1)', |
| 707 |
'&mov ($a4,$f)', |
| 708 |
|
| 709 |
'&ror ($a1,$Sigma0[2]-$Sigma0[1])', |
| 710 |
'&xor ($a0,$e)', |
| 711 |
'&xor ($a4,$g)', # f^g |
| 712 |
|
| 713 |
'&ror ($a0,$Sigma1[1]-$Sigma1[0])', |
| 714 |
'&xor ($a1,$a)', |
| 715 |
'&and ($a4,$e)', # (f^g)&e |
| 716 |
|
| 717 |
'&xor ($a0,$e)', |
| 718 |
'&add ($h,$SZ*($i&15)."(%rsp)")', # h+=X[i]+K[i] |
| 719 |
'&mov ($a2,$a)', |
| 720 |
|
| 721 |
'&xor ($a4,$g)', # Ch(e,f,g)=((f^g)&e)^g |
| 722 |
'&ror ($a1,$Sigma0[1]-$Sigma0[0])', |
| 723 |
'&xor ($a2,$b)', # a^b, b^c in next round |
| 724 |
|
| 725 |
'&add ($h,$a4)', # h+=Ch(e,f,g) |
| 726 |
'&ror ($a0,$Sigma1[0])', # Sigma1(e) |
| 727 |
'&and ($a3,$a2)', # (b^c)&(a^b) |
| 728 |
|
| 729 |
'&xor ($a1,$a)', |
| 730 |
'&add ($h,$a0)', # h+=Sigma1(e) |
| 731 |
'&xor ($a3,$b)', # Maj(a,b,c)=Ch(a^b,c,b) |
| 732 |
|
| 733 |
'&ror ($a1,$Sigma0[0])', # Sigma0(a) |
| 734 |
'&add ($d,$h)', # d+=h |
| 735 |
'&add ($h,$a3)', # h+=Maj(a,b,c) |
| 736 |
|
| 737 |
'&mov ($a0,$d)', |
| 738 |
'&add ($a1,$h);'. # h+=Sigma0(a) |
| 739 |
'($a2,$a3) = ($a3,$a2); unshift(@ROT,pop(@ROT)); $i++;' |
| 740 |
); |
| 741 |
} |
| 742 |
|
| 743 |
###################################################################### |
| 744 |
# SSSE3 code path |
| 745 |
# |
| 746 |
if ($SZ==4) { # SHA256 only |
| 747 |
my @X = map("%xmm$_",(0..3)); |
| 748 |
my ($t0,$t1,$t2,$t3, $t4,$t5) = map("%xmm$_",(4..9)); |
| 749 |
|
| 750 |
$code.=<<___; |
| 751 |
.globl ${func}_ssse3 |
| 752 |
.type ${func}_ssse3,\@function,3 |
| 753 |
.align 64 |
| 754 |
${func}_ssse3: |
| 755 |
.cfi_startproc |
| 756 |
_CET_ENDBR |
| 757 |
mov %rsp,%rax # copy %rsp |
| 758 |
.cfi_def_cfa_register %rax |
| 759 |
push %rbx |
| 760 |
.cfi_push %rbx |
| 761 |
push %rbp |
| 762 |
.cfi_push %rbp |
| 763 |
push %r12 |
| 764 |
.cfi_push %r12 |
| 765 |
push %r13 |
| 766 |
.cfi_push %r13 |
| 767 |
push %r14 |
| 768 |
.cfi_push %r14 |
| 769 |
push %r15 |
| 770 |
.cfi_push %r15 |
| 771 |
shl \$4,%rdx # num*16 |
| 772 |
sub \$`$framesz+$win64*16*4`,%rsp |
| 773 |
lea ($inp,%rdx,$SZ),%rdx # inp+num*16*$SZ |
| 774 |
and \$-64,%rsp # align stack frame |
| 775 |
mov $ctx,$_ctx # save ctx, 1st arg |
| 776 |
mov $inp,$_inp # save inp, 2nd arh |
| 777 |
mov %rdx,$_end # save end pointer, "3rd" arg |
| 778 |
mov %rax,$_rsp # save copy of %rsp |
| 779 |
.cfi_cfa_expression $_rsp,deref,+8 |
| 780 |
___ |
| 781 |
$code.=<<___ if ($win64); |
| 782 |
movaps %xmm6,16*$SZ+32(%rsp) |
| 783 |
movaps %xmm7,16*$SZ+48(%rsp) |
| 784 |
movaps %xmm8,16*$SZ+64(%rsp) |
| 785 |
movaps %xmm9,16*$SZ+80(%rsp) |
| 786 |
___ |
| 787 |
$code.=<<___; |
| 788 |
.Lprologue_ssse3: |
| 789 |
|
| 790 |
mov $SZ*0($ctx),$A |
| 791 |
mov $SZ*1($ctx),$B |
| 792 |
mov $SZ*2($ctx),$C |
| 793 |
mov $SZ*3($ctx),$D |
| 794 |
mov $SZ*4($ctx),$E |
| 795 |
mov $SZ*5($ctx),$F |
| 796 |
mov $SZ*6($ctx),$G |
| 797 |
mov $SZ*7($ctx),$H |
| 798 |
___ |
| 799 |
|
| 800 |
$code.=<<___; |
| 801 |
#movdqa $TABLE+`$SZ*2*$rounds`+32(%rip),$t4 |
| 802 |
#movdqa $TABLE+`$SZ*2*$rounds`+64(%rip),$t5 |
| 803 |
jmp .Lloop_ssse3 |
| 804 |
.align 16 |
| 805 |
.Lloop_ssse3: |
| 806 |
movdqa $TABLE+`$SZ*2*$rounds`(%rip),$t3 |
| 807 |
movdqu 0x00($inp),@X[0] |
| 808 |
movdqu 0x10($inp),@X[1] |
| 809 |
movdqu 0x20($inp),@X[2] |
| 810 |
pshufb $t3,@X[0] |
| 811 |
movdqu 0x30($inp),@X[3] |
| 812 |
lea $TABLE(%rip),$Tbl |
| 813 |
pshufb $t3,@X[1] |
| 814 |
movdqa 0x00($Tbl),$t0 |
| 815 |
movdqa 0x20($Tbl),$t1 |
| 816 |
pshufb $t3,@X[2] |
| 817 |
paddd @X[0],$t0 |
| 818 |
movdqa 0x40($Tbl),$t2 |
| 819 |
pshufb $t3,@X[3] |
| 820 |
movdqa 0x60($Tbl),$t3 |
| 821 |
paddd @X[1],$t1 |
| 822 |
paddd @X[2],$t2 |
| 823 |
paddd @X[3],$t3 |
| 824 |
movdqa $t0,0x00(%rsp) |
| 825 |
mov $A,$a1 |
| 826 |
movdqa $t1,0x10(%rsp) |
| 827 |
mov $B,$a3 |
| 828 |
movdqa $t2,0x20(%rsp) |
| 829 |
xor $C,$a3 # magic |
| 830 |
movdqa $t3,0x30(%rsp) |
| 831 |
mov $E,$a0 |
| 832 |
jmp .Lssse3_00_47 |
| 833 |
|
| 834 |
.align 16 |
| 835 |
.Lssse3_00_47: |
| 836 |
sub \$`-16*2*$SZ`,$Tbl # size optimization |
| 837 |
___ |
| 838 |
sub Xupdate_256_SSSE3 () { |
| 839 |
( |
| 840 |
'&movdqa ($t0,@X[1]);', |
| 841 |
'&movdqa ($t3,@X[3])', |
| 842 |
'&palignr ($t0,@X[0],$SZ)', # X[1..4] |
| 843 |
'&palignr ($t3,@X[2],$SZ);', # X[9..12] |
| 844 |
'&movdqa ($t1,$t0)', |
| 845 |
'&movdqa ($t2,$t0);', |
| 846 |
'&psrld ($t0,$sigma0[2])', |
| 847 |
'&paddd (@X[0],$t3);', # X[0..3] += X[9..12] |
| 848 |
'&psrld ($t2,$sigma0[0])', |
| 849 |
'&pshufd ($t3,@X[3],0b11111010)',# X[14..15] |
| 850 |
'&pslld ($t1,8*$SZ-$sigma0[1]);'. |
| 851 |
'&pxor ($t0,$t2)', |
| 852 |
'&psrld ($t2,$sigma0[1]-$sigma0[0]);'. |
| 853 |
'&pxor ($t0,$t1)', |
| 854 |
'&pslld ($t1,$sigma0[1]-$sigma0[0]);'. |
| 855 |
'&pxor ($t0,$t2);', |
| 856 |
'&movdqa ($t2,$t3)', |
| 857 |
'&pxor ($t0,$t1);', # sigma0(X[1..4]) |
| 858 |
'&psrld ($t3,$sigma1[2])', |
| 859 |
'&paddd (@X[0],$t0);', # X[0..3] += sigma0(X[1..4]) |
| 860 |
'&psrlq ($t2,$sigma1[0])', |
| 861 |
'&pxor ($t3,$t2);', |
| 862 |
'&psrlq ($t2,$sigma1[1]-$sigma1[0])', |
| 863 |
'&pxor ($t3,$t2)', |
| 864 |
'&pshufb ($t3,$t4)', # sigma1(X[14..15]) |
| 865 |
'&paddd (@X[0],$t3)', # X[0..1] += sigma1(X[14..15]) |
| 866 |
'&pshufd ($t3,@X[0],0b01010000)',# X[16..17] |
| 867 |
'&movdqa ($t2,$t3);', |
| 868 |
'&psrld ($t3,$sigma1[2])', |
| 869 |
'&psrlq ($t2,$sigma1[0])', |
| 870 |
'&pxor ($t3,$t2);', |
| 871 |
'&psrlq ($t2,$sigma1[1]-$sigma1[0])', |
| 872 |
'&pxor ($t3,$t2);', |
| 873 |
'&movdqa ($t2,16*2*$j."($Tbl)")', |
| 874 |
'&pshufb ($t3,$t5)', |
| 875 |
'&paddd (@X[0],$t3)' # X[2..3] += sigma1(X[16..17]) |
| 876 |
); |
| 877 |
} |
| 878 |
|
| 879 |
sub SSSE3_256_00_47 () { |
| 880 |
my $j = shift; |
| 881 |
my $body = shift; |
| 882 |
my @X = @_; |
| 883 |
my @insns = (&$body,&$body,&$body,&$body); # 104 instructions |
| 884 |
|
| 885 |
if (0) { |
| 886 |
foreach (Xupdate_256_SSSE3()) { # 36 instructions |
| 887 |
eval; |
| 888 |
eval(shift(@insns)); |
| 889 |
eval(shift(@insns)); |
| 890 |
eval(shift(@insns)); |
| 891 |
} |
| 892 |
} else { # squeeze extra 4% on Westmere and 19% on Atom |
| 893 |
eval(shift(@insns)); #@ |
| 894 |
&movdqa ($t0,@X[1]); |
| 895 |
eval(shift(@insns)); |
| 896 |
eval(shift(@insns)); |
| 897 |
&movdqa ($t3,@X[3]); |
| 898 |
eval(shift(@insns)); #@ |
| 899 |
eval(shift(@insns)); |
| 900 |
eval(shift(@insns)); |
| 901 |
eval(shift(@insns)); #@ |
| 902 |
eval(shift(@insns)); |
| 903 |
&palignr ($t0,@X[0],$SZ); # X[1..4] |
| 904 |
eval(shift(@insns)); |
| 905 |
eval(shift(@insns)); |
| 906 |
&palignr ($t3,@X[2],$SZ); # X[9..12] |
| 907 |
eval(shift(@insns)); |
| 908 |
eval(shift(@insns)); |
| 909 |
eval(shift(@insns)); |
| 910 |
eval(shift(@insns)); #@ |
| 911 |
&movdqa ($t1,$t0); |
| 912 |
eval(shift(@insns)); |
| 913 |
eval(shift(@insns)); |
| 914 |
&movdqa ($t2,$t0); |
| 915 |
eval(shift(@insns)); #@ |
| 916 |
eval(shift(@insns)); |
| 917 |
&psrld ($t0,$sigma0[2]); |
| 918 |
eval(shift(@insns)); |
| 919 |
eval(shift(@insns)); |
| 920 |
eval(shift(@insns)); |
| 921 |
&paddd (@X[0],$t3); # X[0..3] += X[9..12] |
| 922 |
eval(shift(@insns)); #@ |
| 923 |
eval(shift(@insns)); |
| 924 |
&psrld ($t2,$sigma0[0]); |
| 925 |
eval(shift(@insns)); |
| 926 |
eval(shift(@insns)); |
| 927 |
&pshufd ($t3,@X[3],0b11111010); # X[4..15] |
| 928 |
eval(shift(@insns)); |
| 929 |
eval(shift(@insns)); #@ |
| 930 |
&pslld ($t1,8*$SZ-$sigma0[1]); |
| 931 |
eval(shift(@insns)); |
| 932 |
eval(shift(@insns)); |
| 933 |
&pxor ($t0,$t2); |
| 934 |
eval(shift(@insns)); #@ |
| 935 |
eval(shift(@insns)); |
| 936 |
eval(shift(@insns)); |
| 937 |
eval(shift(@insns)); #@ |
| 938 |
&psrld ($t2,$sigma0[1]-$sigma0[0]); |
| 939 |
eval(shift(@insns)); |
| 940 |
&pxor ($t0,$t1); |
| 941 |
eval(shift(@insns)); |
| 942 |
eval(shift(@insns)); |
| 943 |
&pslld ($t1,$sigma0[1]-$sigma0[0]); |
| 944 |
eval(shift(@insns)); |
| 945 |
eval(shift(@insns)); |
| 946 |
&pxor ($t0,$t2); |
| 947 |
eval(shift(@insns)); |
| 948 |
eval(shift(@insns)); #@ |
| 949 |
&movdqa ($t2,$t3); |
| 950 |
eval(shift(@insns)); |
| 951 |
eval(shift(@insns)); |
| 952 |
&pxor ($t0,$t1); # sigma0(X[1..4]) |
| 953 |
eval(shift(@insns)); #@ |
| 954 |
eval(shift(@insns)); |
| 955 |
eval(shift(@insns)); |
| 956 |
&psrld ($t3,$sigma1[2]); |
| 957 |
eval(shift(@insns)); |
| 958 |
eval(shift(@insns)); |
| 959 |
&paddd (@X[0],$t0); # X[0..3] += sigma0(X[1..4]) |
| 960 |
eval(shift(@insns)); #@ |
| 961 |
eval(shift(@insns)); |
| 962 |
&psrlq ($t2,$sigma1[0]); |
| 963 |
eval(shift(@insns)); |
| 964 |
eval(shift(@insns)); |
| 965 |
eval(shift(@insns)); |
| 966 |
&pxor ($t3,$t2); |
| 967 |
eval(shift(@insns)); #@ |
| 968 |
eval(shift(@insns)); |
| 969 |
eval(shift(@insns)); |
| 970 |
eval(shift(@insns)); #@ |
| 971 |
&psrlq ($t2,$sigma1[1]-$sigma1[0]); |
| 972 |
eval(shift(@insns)); |
| 973 |
eval(shift(@insns)); |
| 974 |
&pxor ($t3,$t2); |
| 975 |
eval(shift(@insns)); #@ |
| 976 |
eval(shift(@insns)); |
| 977 |
eval(shift(@insns)); |
| 978 |
#&pshufb ($t3,$t4); # sigma1(X[14..15]) |
| 979 |
&pshufd ($t3,$t3,0b10000000); |
| 980 |
eval(shift(@insns)); |
| 981 |
eval(shift(@insns)); |
| 982 |
eval(shift(@insns)); |
| 983 |
&psrldq ($t3,8); |
| 984 |
eval(shift(@insns)); |
| 985 |
eval(shift(@insns)); #@ |
| 986 |
eval(shift(@insns)); |
| 987 |
eval(shift(@insns)); |
| 988 |
eval(shift(@insns)); #@ |
| 989 |
&paddd (@X[0],$t3); # X[0..1] += sigma1(X[14..15]) |
| 990 |
eval(shift(@insns)); |
| 991 |
eval(shift(@insns)); |
| 992 |
eval(shift(@insns)); |
| 993 |
&pshufd ($t3,@X[0],0b01010000); # X[16..17] |
| 994 |
eval(shift(@insns)); |
| 995 |
eval(shift(@insns)); #@ |
| 996 |
eval(shift(@insns)); |
| 997 |
&movdqa ($t2,$t3); |
| 998 |
eval(shift(@insns)); |
| 999 |
eval(shift(@insns)); |
| 1000 |
&psrld ($t3,$sigma1[2]); |
| 1001 |
eval(shift(@insns)); |
| 1002 |
eval(shift(@insns)); #@ |
| 1003 |
&psrlq ($t2,$sigma1[0]); |
| 1004 |
eval(shift(@insns)); |
| 1005 |
eval(shift(@insns)); |
| 1006 |
&pxor ($t3,$t2); |
| 1007 |
eval(shift(@insns)); #@ |
| 1008 |
eval(shift(@insns)); |
| 1009 |
eval(shift(@insns)); |
| 1010 |
eval(shift(@insns)); #@ |
| 1011 |
eval(shift(@insns)); |
| 1012 |
&psrlq ($t2,$sigma1[1]-$sigma1[0]); |
| 1013 |
eval(shift(@insns)); |
| 1014 |
eval(shift(@insns)); |
| 1015 |
eval(shift(@insns)); |
| 1016 |
&pxor ($t3,$t2); |
| 1017 |
eval(shift(@insns)); |
| 1018 |
eval(shift(@insns)); |
| 1019 |
eval(shift(@insns)); #@ |
| 1020 |
#&pshufb ($t3,$t5); |
| 1021 |
&pshufd ($t3,$t3,0b00001000); |
| 1022 |
eval(shift(@insns)); |
| 1023 |
eval(shift(@insns)); |
| 1024 |
&movdqa ($t2,16*2*$j."($Tbl)"); |
| 1025 |
eval(shift(@insns)); #@ |
| 1026 |
eval(shift(@insns)); |
| 1027 |
&pslldq ($t3,8); |
| 1028 |
eval(shift(@insns)); |
| 1029 |
eval(shift(@insns)); |
| 1030 |
eval(shift(@insns)); |
| 1031 |
&paddd (@X[0],$t3); # X[2..3] += sigma1(X[16..17]) |
| 1032 |
eval(shift(@insns)); #@ |
| 1033 |
eval(shift(@insns)); |
| 1034 |
eval(shift(@insns)); |
| 1035 |
} |
| 1036 |
&paddd ($t2,@X[0]); |
| 1037 |
foreach (@insns) { eval; } # remaining instructions |
| 1038 |
&movdqa (16*$j."(%rsp)",$t2); |
| 1039 |
} |
| 1040 |
|
| 1041 |
for ($i=0,$j=0; $j<4; $j++) { |
| 1042 |
&SSSE3_256_00_47($j,\&body_00_15,@X); |
| 1043 |
push(@X,shift(@X)); # rotate(@X) |
| 1044 |
} |
| 1045 |
&cmpb ($SZ-1+16*2*$SZ."($Tbl)",0); |
| 1046 |
&jne (".Lssse3_00_47"); |
| 1047 |
|
| 1048 |
for ($i=0; $i<16; ) { |
| 1049 |
foreach(body_00_15()) { eval; } |
| 1050 |
} |
| 1051 |
$code.=<<___; |
| 1052 |
mov $_ctx,$ctx |
| 1053 |
mov $a1,$A |
| 1054 |
|
| 1055 |
add $SZ*0($ctx),$A |
| 1056 |
lea 16*$SZ($inp),$inp |
| 1057 |
add $SZ*1($ctx),$B |
| 1058 |
add $SZ*2($ctx),$C |
| 1059 |
add $SZ*3($ctx),$D |
| 1060 |
add $SZ*4($ctx),$E |
| 1061 |
add $SZ*5($ctx),$F |
| 1062 |
add $SZ*6($ctx),$G |
| 1063 |
add $SZ*7($ctx),$H |
| 1064 |
|
| 1065 |
cmp $_end,$inp |
| 1066 |
|
| 1067 |
mov $A,$SZ*0($ctx) |
| 1068 |
mov $B,$SZ*1($ctx) |
| 1069 |
mov $C,$SZ*2($ctx) |
| 1070 |
mov $D,$SZ*3($ctx) |
| 1071 |
mov $E,$SZ*4($ctx) |
| 1072 |
mov $F,$SZ*5($ctx) |
| 1073 |
mov $G,$SZ*6($ctx) |
| 1074 |
mov $H,$SZ*7($ctx) |
| 1075 |
jb .Lloop_ssse3 |
| 1076 |
|
| 1077 |
mov $_rsp,%rsi |
| 1078 |
.cfi_def_cfa %rsi,8 |
| 1079 |
___ |
| 1080 |
$code.=<<___ if ($win64); |
| 1081 |
movaps 16*$SZ+32(%rsp),%xmm6 |
| 1082 |
movaps 16*$SZ+48(%rsp),%xmm7 |
| 1083 |
movaps 16*$SZ+64(%rsp),%xmm8 |
| 1084 |
movaps 16*$SZ+80(%rsp),%xmm9 |
| 1085 |
___ |
| 1086 |
$code.=<<___; |
| 1087 |
mov -48(%rsi),%r15 |
| 1088 |
.cfi_restore %r15 |
| 1089 |
mov -40(%rsi),%r14 |
| 1090 |
.cfi_restore %r14 |
| 1091 |
mov -32(%rsi),%r13 |
| 1092 |
.cfi_restore %r13 |
| 1093 |
mov -24(%rsi),%r12 |
| 1094 |
.cfi_restore %r12 |
| 1095 |
mov -16(%rsi),%rbp |
| 1096 |
.cfi_restore %rbp |
| 1097 |
mov -8(%rsi),%rbx |
| 1098 |
.cfi_restore %rbx |
| 1099 |
lea (%rsi),%rsp |
| 1100 |
.cfi_def_cfa_register %rsp |
| 1101 |
.Lepilogue_ssse3: |
| 1102 |
ret |
| 1103 |
.cfi_endproc |
| 1104 |
.size ${func}_ssse3,.-${func}_ssse3 |
| 1105 |
___ |
| 1106 |
} |
| 1107 |
|
| 1108 |
if ($avx) {{ |
| 1109 |
###################################################################### |
| 1110 |
# AVX+shrd code path |
| 1111 |
# |
| 1112 |
local *ror = sub { &shrd(@_[0],@_) }; |
| 1113 |
|
| 1114 |
$code.=<<___; |
| 1115 |
.globl ${func}_avx |
| 1116 |
.type ${func}_avx,\@function,3 |
| 1117 |
.align 64 |
| 1118 |
${func}_avx: |
| 1119 |
.cfi_startproc |
| 1120 |
_CET_ENDBR |
| 1121 |
mov %rsp,%rax # copy %rsp |
| 1122 |
.cfi_def_cfa_register %rax |
| 1123 |
push %rbx |
| 1124 |
.cfi_push %rbx |
| 1125 |
push %rbp |
| 1126 |
.cfi_push %rbp |
| 1127 |
push %r12 |
| 1128 |
.cfi_push %r12 |
| 1129 |
push %r13 |
| 1130 |
.cfi_push %r13 |
| 1131 |
push %r14 |
| 1132 |
.cfi_push %r14 |
| 1133 |
push %r15 |
| 1134 |
.cfi_push %r15 |
| 1135 |
shl \$4,%rdx # num*16 |
| 1136 |
sub \$`$framesz+$win64*16*($SZ==4?4:6)`,%rsp |
| 1137 |
lea ($inp,%rdx,$SZ),%rdx # inp+num*16*$SZ |
| 1138 |
and \$-64,%rsp # align stack frame |
| 1139 |
mov $ctx,$_ctx # save ctx, 1st arg |
| 1140 |
mov $inp,$_inp # save inp, 2nd arh |
| 1141 |
mov %rdx,$_end # save end pointer, "3rd" arg |
| 1142 |
mov %rax,$_rsp # save copy of %rsp |
| 1143 |
.cfi_cfa_expression $_rsp,deref,+8 |
| 1144 |
___ |
| 1145 |
$code.=<<___ if ($win64); |
| 1146 |
movaps %xmm6,16*$SZ+32(%rsp) |
| 1147 |
movaps %xmm7,16*$SZ+48(%rsp) |
| 1148 |
movaps %xmm8,16*$SZ+64(%rsp) |
| 1149 |
movaps %xmm9,16*$SZ+80(%rsp) |
| 1150 |
___ |
| 1151 |
$code.=<<___ if ($win64 && $SZ>4); |
| 1152 |
movaps %xmm10,16*$SZ+96(%rsp) |
| 1153 |
movaps %xmm11,16*$SZ+112(%rsp) |
| 1154 |
___ |
| 1155 |
$code.=<<___; |
| 1156 |
.Lprologue_avx: |
| 1157 |
|
| 1158 |
vzeroupper |
| 1159 |
mov $SZ*0($ctx),$A |
| 1160 |
mov $SZ*1($ctx),$B |
| 1161 |
mov $SZ*2($ctx),$C |
| 1162 |
mov $SZ*3($ctx),$D |
| 1163 |
mov $SZ*4($ctx),$E |
| 1164 |
mov $SZ*5($ctx),$F |
| 1165 |
mov $SZ*6($ctx),$G |
| 1166 |
mov $SZ*7($ctx),$H |
| 1167 |
___ |
| 1168 |
if ($SZ==4) { # SHA256 |
| 1169 |
my @X = map("%xmm$_",(0..3)); |
| 1170 |
my ($t0,$t1,$t2,$t3, $t4,$t5) = map("%xmm$_",(4..9)); |
| 1171 |
|
| 1172 |
$code.=<<___; |
| 1173 |
vmovdqa $TABLE+`$SZ*2*$rounds`+32(%rip),$t4 |
| 1174 |
vmovdqa $TABLE+`$SZ*2*$rounds`+64(%rip),$t5 |
| 1175 |
jmp .Lloop_avx |
| 1176 |
.align 16 |
| 1177 |
.Lloop_avx: |
| 1178 |
vmovdqa $TABLE+`$SZ*2*$rounds`(%rip),$t3 |
| 1179 |
vmovdqu 0x00($inp),@X[0] |
| 1180 |
vmovdqu 0x10($inp),@X[1] |
| 1181 |
vmovdqu 0x20($inp),@X[2] |
| 1182 |
vmovdqu 0x30($inp),@X[3] |
| 1183 |
vpshufb $t3,@X[0],@X[0] |
| 1184 |
lea $TABLE(%rip),$Tbl |
| 1185 |
vpshufb $t3,@X[1],@X[1] |
| 1186 |
vpshufb $t3,@X[2],@X[2] |
| 1187 |
vpaddd 0x00($Tbl),@X[0],$t0 |
| 1188 |
vpshufb $t3,@X[3],@X[3] |
| 1189 |
vpaddd 0x20($Tbl),@X[1],$t1 |
| 1190 |
vpaddd 0x40($Tbl),@X[2],$t2 |
| 1191 |
vpaddd 0x60($Tbl),@X[3],$t3 |
| 1192 |
vmovdqa $t0,0x00(%rsp) |
| 1193 |
mov $A,$a1 |
| 1194 |
vmovdqa $t1,0x10(%rsp) |
| 1195 |
mov $B,$a3 |
| 1196 |
vmovdqa $t2,0x20(%rsp) |
| 1197 |
xor $C,$a3 # magic |
| 1198 |
vmovdqa $t3,0x30(%rsp) |
| 1199 |
mov $E,$a0 |
| 1200 |
jmp .Lavx_00_47 |
| 1201 |
|
| 1202 |
.align 16 |
| 1203 |
.Lavx_00_47: |
| 1204 |
sub \$`-16*2*$SZ`,$Tbl # size optimization |
| 1205 |
___ |
| 1206 |
sub Xupdate_256_AVX () { |
| 1207 |
( |
| 1208 |
'&vpalignr ($t0,@X[1],@X[0],$SZ)', # X[1..4] |
| 1209 |
'&vpalignr ($t3,@X[3],@X[2],$SZ)', # X[9..12] |
| 1210 |
'&vpsrld ($t2,$t0,$sigma0[0]);', |
| 1211 |
'&vpaddd (@X[0],@X[0],$t3)', # X[0..3] += X[9..12] |
| 1212 |
'&vpsrld ($t3,$t0,$sigma0[2])', |
| 1213 |
'&vpslld ($t1,$t0,8*$SZ-$sigma0[1]);', |
| 1214 |
'&vpxor ($t0,$t3,$t2)', |
| 1215 |
'&vpshufd ($t3,@X[3],0b11111010)',# X[14..15] |
| 1216 |
'&vpsrld ($t2,$t2,$sigma0[1]-$sigma0[0]);', |
| 1217 |
'&vpxor ($t0,$t0,$t1)', |
| 1218 |
'&vpslld ($t1,$t1,$sigma0[1]-$sigma0[0]);', |
| 1219 |
'&vpxor ($t0,$t0,$t2)', |
| 1220 |
'&vpsrld ($t2,$t3,$sigma1[2]);', |
| 1221 |
'&vpxor ($t0,$t0,$t1)', # sigma0(X[1..4]) |
| 1222 |
'&vpsrlq ($t3,$t3,$sigma1[0]);', |
| 1223 |
'&vpaddd (@X[0],@X[0],$t0)', # X[0..3] += sigma0(X[1..4]) |
| 1224 |
'&vpxor ($t2,$t2,$t3);', |
| 1225 |
'&vpsrlq ($t3,$t3,$sigma1[1]-$sigma1[0])', |
| 1226 |
'&vpxor ($t2,$t2,$t3)', |
| 1227 |
'&vpshufb ($t2,$t2,$t4)', # sigma1(X[14..15]) |
| 1228 |
'&vpaddd (@X[0],@X[0],$t2)', # X[0..1] += sigma1(X[14..15]) |
| 1229 |
'&vpshufd ($t3,@X[0],0b01010000)',# X[16..17] |
| 1230 |
'&vpsrld ($t2,$t3,$sigma1[2])', |
| 1231 |
'&vpsrlq ($t3,$t3,$sigma1[0])', |
| 1232 |
'&vpxor ($t2,$t2,$t3);', |
| 1233 |
'&vpsrlq ($t3,$t3,$sigma1[1]-$sigma1[0])', |
| 1234 |
'&vpxor ($t2,$t2,$t3)', |
| 1235 |
'&vpshufb ($t2,$t2,$t5)', |
| 1236 |
'&vpaddd (@X[0],@X[0],$t2)' # X[2..3] += sigma1(X[16..17]) |
| 1237 |
); |
| 1238 |
} |
| 1239 |
|
| 1240 |
sub AVX_256_00_47 () { |
| 1241 |
my $j = shift; |
| 1242 |
my $body = shift; |
| 1243 |
my @X = @_; |
| 1244 |
my @insns = (&$body,&$body,&$body,&$body); # 104 instructions |
| 1245 |
|
| 1246 |
foreach (Xupdate_256_AVX()) { # 29 instructions |
| 1247 |
eval; |
| 1248 |
eval(shift(@insns)); |
| 1249 |
eval(shift(@insns)); |
| 1250 |
eval(shift(@insns)); |
| 1251 |
} |
| 1252 |
&vpaddd ($t2,@X[0],16*2*$j."($Tbl)"); |
| 1253 |
foreach (@insns) { eval; } # remaining instructions |
| 1254 |
&vmovdqa (16*$j."(%rsp)",$t2); |
| 1255 |
} |
| 1256 |
|
| 1257 |
for ($i=0,$j=0; $j<4; $j++) { |
| 1258 |
&AVX_256_00_47($j,\&body_00_15,@X); |
| 1259 |
push(@X,shift(@X)); # rotate(@X) |
| 1260 |
} |
| 1261 |
&cmpb ($SZ-1+16*2*$SZ."($Tbl)",0); |
| 1262 |
&jne (".Lavx_00_47"); |
| 1263 |
|
| 1264 |
for ($i=0; $i<16; ) { |
| 1265 |
foreach(body_00_15()) { eval; } |
| 1266 |
} |
| 1267 |
|
| 1268 |
} else { # SHA512 |
| 1269 |
my @X = map("%xmm$_",(0..7)); |
| 1270 |
my ($t0,$t1,$t2,$t3) = map("%xmm$_",(8..11)); |
| 1271 |
|
| 1272 |
$code.=<<___; |
| 1273 |
jmp .Lloop_avx |
| 1274 |
.align 16 |
| 1275 |
.Lloop_avx: |
| 1276 |
vmovdqa $TABLE+`$SZ*2*$rounds`(%rip),$t3 |
| 1277 |
vmovdqu 0x00($inp),@X[0] |
| 1278 |
lea $TABLE+0x80(%rip),$Tbl # size optimization |
| 1279 |
vmovdqu 0x10($inp),@X[1] |
| 1280 |
vmovdqu 0x20($inp),@X[2] |
| 1281 |
vpshufb $t3,@X[0],@X[0] |
| 1282 |
vmovdqu 0x30($inp),@X[3] |
| 1283 |
vpshufb $t3,@X[1],@X[1] |
| 1284 |
vmovdqu 0x40($inp),@X[4] |
| 1285 |
vpshufb $t3,@X[2],@X[2] |
| 1286 |
vmovdqu 0x50($inp),@X[5] |
| 1287 |
vpshufb $t3,@X[3],@X[3] |
| 1288 |
vmovdqu 0x60($inp),@X[6] |
| 1289 |
vpshufb $t3,@X[4],@X[4] |
| 1290 |
vmovdqu 0x70($inp),@X[7] |
| 1291 |
vpshufb $t3,@X[5],@X[5] |
| 1292 |
vpaddq -0x80($Tbl),@X[0],$t0 |
| 1293 |
vpshufb $t3,@X[6],@X[6] |
| 1294 |
vpaddq -0x60($Tbl),@X[1],$t1 |
| 1295 |
vpshufb $t3,@X[7],@X[7] |
| 1296 |
vpaddq -0x40($Tbl),@X[2],$t2 |
| 1297 |
vpaddq -0x20($Tbl),@X[3],$t3 |
| 1298 |
vmovdqa $t0,0x00(%rsp) |
| 1299 |
vpaddq 0x00($Tbl),@X[4],$t0 |
| 1300 |
vmovdqa $t1,0x10(%rsp) |
| 1301 |
vpaddq 0x20($Tbl),@X[5],$t1 |
| 1302 |
vmovdqa $t2,0x20(%rsp) |
| 1303 |
vpaddq 0x40($Tbl),@X[6],$t2 |
| 1304 |
vmovdqa $t3,0x30(%rsp) |
| 1305 |
vpaddq 0x60($Tbl),@X[7],$t3 |
| 1306 |
vmovdqa $t0,0x40(%rsp) |
| 1307 |
mov $A,$a1 |
| 1308 |
vmovdqa $t1,0x50(%rsp) |
| 1309 |
mov $B,$a3 |
| 1310 |
vmovdqa $t2,0x60(%rsp) |
| 1311 |
xor $C,$a3 # magic |
| 1312 |
vmovdqa $t3,0x70(%rsp) |
| 1313 |
mov $E,$a0 |
| 1314 |
jmp .Lavx_00_47 |
| 1315 |
|
| 1316 |
.align 16 |
| 1317 |
.Lavx_00_47: |
| 1318 |
add \$`16*2*$SZ`,$Tbl |
| 1319 |
___ |
| 1320 |
sub Xupdate_512_AVX () { |
| 1321 |
( |
| 1322 |
'&vpalignr ($t0,@X[1],@X[0],$SZ)', # X[1..2] |
| 1323 |
'&vpalignr ($t3,@X[5],@X[4],$SZ)', # X[9..10] |
| 1324 |
'&vpsrlq ($t2,$t0,$sigma0[0])', |
| 1325 |
'&vpaddq (@X[0],@X[0],$t3);', # X[0..1] += X[9..10] |
| 1326 |
'&vpsrlq ($t3,$t0,$sigma0[2])', |
| 1327 |
'&vpsllq ($t1,$t0,8*$SZ-$sigma0[1]);', |
| 1328 |
'&vpxor ($t0,$t3,$t2)', |
| 1329 |
'&vpsrlq ($t2,$t2,$sigma0[1]-$sigma0[0]);', |
| 1330 |
'&vpxor ($t0,$t0,$t1)', |
| 1331 |
'&vpsllq ($t1,$t1,$sigma0[1]-$sigma0[0]);', |
| 1332 |
'&vpxor ($t0,$t0,$t2)', |
| 1333 |
'&vpsrlq ($t3,@X[7],$sigma1[2]);', |
| 1334 |
'&vpxor ($t0,$t0,$t1)', # sigma0(X[1..2]) |
| 1335 |
'&vpsllq ($t2,@X[7],8*$SZ-$sigma1[1]);', |
| 1336 |
'&vpaddq (@X[0],@X[0],$t0)', # X[0..1] += sigma0(X[1..2]) |
| 1337 |
'&vpsrlq ($t1,@X[7],$sigma1[0]);', |
| 1338 |
'&vpxor ($t3,$t3,$t2)', |
| 1339 |
'&vpsllq ($t2,$t2,$sigma1[1]-$sigma1[0]);', |
| 1340 |
'&vpxor ($t3,$t3,$t1)', |
| 1341 |
'&vpsrlq ($t1,$t1,$sigma1[1]-$sigma1[0]);', |
| 1342 |
'&vpxor ($t3,$t3,$t2)', |
| 1343 |
'&vpxor ($t3,$t3,$t1)', # sigma1(X[14..15]) |
| 1344 |
'&vpaddq (@X[0],@X[0],$t3)', # X[0..1] += sigma1(X[14..15]) |
| 1345 |
); |
| 1346 |
} |
| 1347 |
|
| 1348 |
sub AVX_512_00_47 () { |
| 1349 |
my $j = shift; |
| 1350 |
my $body = shift; |
| 1351 |
my @X = @_; |
| 1352 |
my @insns = (&$body,&$body); # 52 instructions |
| 1353 |
|
| 1354 |
foreach (Xupdate_512_AVX()) { # 23 instructions |
| 1355 |
eval; |
| 1356 |
eval(shift(@insns)); |
| 1357 |
eval(shift(@insns)); |
| 1358 |
} |
| 1359 |
&vpaddq ($t2,@X[0],16*2*$j-0x80."($Tbl)"); |
| 1360 |
foreach (@insns) { eval; } # remaining instructions |
| 1361 |
&vmovdqa (16*$j."(%rsp)",$t2); |
| 1362 |
} |
| 1363 |
|
| 1364 |
for ($i=0,$j=0; $j<8; $j++) { |
| 1365 |
&AVX_512_00_47($j,\&body_00_15,@X); |
| 1366 |
push(@X,shift(@X)); # rotate(@X) |
| 1367 |
} |
| 1368 |
&cmpb ($SZ-1+16*2*$SZ-0x80."($Tbl)",0); |
| 1369 |
&jne (".Lavx_00_47"); |
| 1370 |
|
| 1371 |
for ($i=0; $i<16; ) { |
| 1372 |
foreach(body_00_15()) { eval; } |
| 1373 |
} |
| 1374 |
} |
| 1375 |
$code.=<<___; |
| 1376 |
mov $_ctx,$ctx |
| 1377 |
mov $a1,$A |
| 1378 |
|
| 1379 |
add $SZ*0($ctx),$A |
| 1380 |
lea 16*$SZ($inp),$inp |
| 1381 |
add $SZ*1($ctx),$B |
| 1382 |
add $SZ*2($ctx),$C |
| 1383 |
add $SZ*3($ctx),$D |
| 1384 |
add $SZ*4($ctx),$E |
| 1385 |
add $SZ*5($ctx),$F |
| 1386 |
add $SZ*6($ctx),$G |
| 1387 |
add $SZ*7($ctx),$H |
| 1388 |
|
| 1389 |
cmp $_end,$inp |
| 1390 |
|
| 1391 |
mov $A,$SZ*0($ctx) |
| 1392 |
mov $B,$SZ*1($ctx) |
| 1393 |
mov $C,$SZ*2($ctx) |
| 1394 |
mov $D,$SZ*3($ctx) |
| 1395 |
mov $E,$SZ*4($ctx) |
| 1396 |
mov $F,$SZ*5($ctx) |
| 1397 |
mov $G,$SZ*6($ctx) |
| 1398 |
mov $H,$SZ*7($ctx) |
| 1399 |
jb .Lloop_avx |
| 1400 |
|
| 1401 |
mov $_rsp,%rsi |
| 1402 |
.cfi_def_cfa %rsi,8 |
| 1403 |
vzeroupper |
| 1404 |
___ |
| 1405 |
$code.=<<___ if ($win64); |
| 1406 |
movaps 16*$SZ+32(%rsp),%xmm6 |
| 1407 |
movaps 16*$SZ+48(%rsp),%xmm7 |
| 1408 |
movaps 16*$SZ+64(%rsp),%xmm8 |
| 1409 |
movaps 16*$SZ+80(%rsp),%xmm9 |
| 1410 |
___ |
| 1411 |
$code.=<<___ if ($win64 && $SZ>4); |
| 1412 |
movaps 16*$SZ+96(%rsp),%xmm10 |
| 1413 |
movaps 16*$SZ+112(%rsp),%xmm11 |
| 1414 |
___ |
| 1415 |
$code.=<<___; |
| 1416 |
mov -48(%rsi),%r15 |
| 1417 |
.cfi_restore %r15 |
| 1418 |
mov -40(%rsi),%r14 |
| 1419 |
.cfi_restore %r14 |
| 1420 |
mov -32(%rsi),%r13 |
| 1421 |
.cfi_restore %r13 |
| 1422 |
mov -24(%rsi),%r12 |
| 1423 |
.cfi_restore %r12 |
| 1424 |
mov -16(%rsi),%rbp |
| 1425 |
.cfi_restore %rbp |
| 1426 |
mov -8(%rsi),%rbx |
| 1427 |
.cfi_restore %rbx |
| 1428 |
lea (%rsi),%rsp |
| 1429 |
.cfi_def_cfa_register %rsp |
| 1430 |
.Lepilogue_avx: |
| 1431 |
ret |
| 1432 |
.cfi_endproc |
| 1433 |
.size ${func}_avx,.-${func}_avx |
| 1434 |
___ |
| 1435 |
|
| 1436 |
}}}}} |
| 1437 |
|
| 1438 |
# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame, |
| 1439 |
# CONTEXT *context,DISPATCHER_CONTEXT *disp) |
| 1440 |
if ($win64) { |
| 1441 |
$rec="%rcx"; |
| 1442 |
$frame="%rdx"; |
| 1443 |
$context="%r8"; |
| 1444 |
$disp="%r9"; |
| 1445 |
|
| 1446 |
$code.=<<___; |
| 1447 |
.extern __imp_RtlVirtualUnwind |
| 1448 |
.type se_handler,\@abi-omnipotent |
| 1449 |
.align 16 |
| 1450 |
se_handler: |
| 1451 |
push %rsi |
| 1452 |
push %rdi |
| 1453 |
push %rbx |
| 1454 |
push %rbp |
| 1455 |
push %r12 |
| 1456 |
push %r13 |
| 1457 |
push %r14 |
| 1458 |
push %r15 |
| 1459 |
pushfq |
| 1460 |
sub \$64,%rsp |
| 1461 |
|
| 1462 |
mov 120($context),%rax # pull context->Rax |
| 1463 |
mov 248($context),%rbx # pull context->Rip |
| 1464 |
|
| 1465 |
mov 8($disp),%rsi # disp->ImageBase |
| 1466 |
mov 56($disp),%r11 # disp->HanderlData |
| 1467 |
|
| 1468 |
mov 0(%r11),%r10d # HandlerData[0] |
| 1469 |
lea (%rsi,%r10),%r10 # prologue label |
| 1470 |
cmp %r10,%rbx # context->Rip<prologue label |
| 1471 |
jb .Lin_prologue |
| 1472 |
|
| 1473 |
mov 152($context),%rax # pull context->Rsp |
| 1474 |
|
| 1475 |
mov 4(%r11),%r10d # HandlerData[1] |
| 1476 |
lea (%rsi,%r10),%r10 # epilogue label |
| 1477 |
cmp %r10,%rbx # context->Rip>=epilogue label |
| 1478 |
jae .Lin_prologue |
| 1479 |
___ |
| 1480 |
$code.=<<___; |
| 1481 |
mov %rax,%rsi # put aside Rsp |
| 1482 |
mov 16*$SZ+3*8(%rax),%rax # pull $_rsp |
| 1483 |
|
| 1484 |
mov -8(%rax),%rbx |
| 1485 |
mov -16(%rax),%rbp |
| 1486 |
mov -24(%rax),%r12 |
| 1487 |
mov -32(%rax),%r13 |
| 1488 |
mov -40(%rax),%r14 |
| 1489 |
mov -48(%rax),%r15 |
| 1490 |
mov %rbx,144($context) # restore context->Rbx |
| 1491 |
mov %rbp,160($context) # restore context->Rbp |
| 1492 |
mov %r12,216($context) # restore context->R12 |
| 1493 |
mov %r13,224($context) # restore context->R13 |
| 1494 |
mov %r14,232($context) # restore context->R14 |
| 1495 |
mov %r15,240($context) # restore context->R15 |
| 1496 |
|
| 1497 |
lea .Lepilogue(%rip),%r10 |
| 1498 |
cmp %r10,%rbx |
| 1499 |
jb .Lin_prologue # non-AVX code |
| 1500 |
|
| 1501 |
lea 16*$SZ+4*8(%rsi),%rsi # Xmm6- save area |
| 1502 |
lea 512($context),%rdi # &context.Xmm6 |
| 1503 |
mov \$`$SZ==4?8:12`,%ecx |
| 1504 |
.long 0xa548f3fc # cld; rep movsq |
| 1505 |
|
| 1506 |
.Lin_prologue: |
| 1507 |
mov 8(%rax),%rdi |
| 1508 |
mov 16(%rax),%rsi |
| 1509 |
mov %rax,152($context) # restore context->Rsp |
| 1510 |
mov %rsi,168($context) # restore context->Rsi |
| 1511 |
mov %rdi,176($context) # restore context->Rdi |
| 1512 |
|
| 1513 |
mov 40($disp),%rdi # disp->ContextRecord |
| 1514 |
mov $context,%rsi # context |
| 1515 |
mov \$154,%ecx # sizeof(CONTEXT) |
| 1516 |
.long 0xa548f3fc # cld; rep movsq |
| 1517 |
|
| 1518 |
mov $disp,%rsi |
| 1519 |
xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER |
| 1520 |
mov 8(%rsi),%rdx # arg2, disp->ImageBase |
| 1521 |
mov 0(%rsi),%r8 # arg3, disp->ControlPc |
| 1522 |
mov 16(%rsi),%r9 # arg4, disp->FunctionEntry |
| 1523 |
mov 40(%rsi),%r10 # disp->ContextRecord |
| 1524 |
lea 56(%rsi),%r11 # &disp->HandlerData |
| 1525 |
lea 24(%rsi),%r12 # &disp->EstablisherFrame |
| 1526 |
mov %r10,32(%rsp) # arg5 |
| 1527 |
mov %r11,40(%rsp) # arg6 |
| 1528 |
mov %r12,48(%rsp) # arg7 |
| 1529 |
mov %rcx,56(%rsp) # arg8, (NULL) |
| 1530 |
call *__imp_RtlVirtualUnwind(%rip) |
| 1531 |
|
| 1532 |
mov \$1,%eax # ExceptionContinueSearch |
| 1533 |
add \$64,%rsp |
| 1534 |
popfq |
| 1535 |
pop %r15 |
| 1536 |
pop %r14 |
| 1537 |
pop %r13 |
| 1538 |
pop %r12 |
| 1539 |
pop %rbp |
| 1540 |
pop %rbx |
| 1541 |
pop %rdi |
| 1542 |
pop %rsi |
| 1543 |
ret |
| 1544 |
.size se_handler,.-se_handler |
| 1545 |
___ |
| 1546 |
|
| 1547 |
$code.=<<___ if ($SZ==4 && $shaext); |
| 1548 |
.type shaext_handler,\@abi-omnipotent |
| 1549 |
.align 16 |
| 1550 |
shaext_handler: |
| 1551 |
push %rsi |
| 1552 |
push %rdi |
| 1553 |
push %rbx |
| 1554 |
push %rbp |
| 1555 |
push %r12 |
| 1556 |
push %r13 |
| 1557 |
push %r14 |
| 1558 |
push %r15 |
| 1559 |
pushfq |
| 1560 |
sub \$64,%rsp |
| 1561 |
|
| 1562 |
mov 120($context),%rax # pull context->Rax |
| 1563 |
mov 248($context),%rbx # pull context->Rip |
| 1564 |
|
| 1565 |
lea .Lprologue_shaext(%rip),%r10 |
| 1566 |
cmp %r10,%rbx # context->Rip<.Lprologue |
| 1567 |
jb .Lin_prologue |
| 1568 |
|
| 1569 |
lea .Lepilogue_shaext(%rip),%r10 |
| 1570 |
cmp %r10,%rbx # context->Rip>=.Lepilogue |
| 1571 |
jae .Lin_prologue |
| 1572 |
|
| 1573 |
lea -8-5*16(%rax),%rsi |
| 1574 |
lea 512($context),%rdi # &context.Xmm6 |
| 1575 |
mov \$10,%ecx |
| 1576 |
.long 0xa548f3fc # cld; rep movsq |
| 1577 |
|
| 1578 |
jmp .Lin_prologue |
| 1579 |
.size shaext_handler,.-shaext_handler |
| 1580 |
___ |
| 1581 |
|
| 1582 |
$code.=<<___; |
| 1583 |
.section .pdata |
| 1584 |
.align 4 |
| 1585 |
.rva .LSEH_begin_${func}_nohw |
| 1586 |
.rva .LSEH_end_${func}_nohw |
| 1587 |
.rva .LSEH_info_${func}_nohw |
| 1588 |
___ |
| 1589 |
$code.=<<___ if ($SZ==4 && $shaext); |
| 1590 |
.rva .LSEH_begin_${func}_hw |
| 1591 |
.rva .LSEH_end_${func}_hw |
| 1592 |
.rva .LSEH_info_${func}_hw |
| 1593 |
___ |
| 1594 |
$code.=<<___ if ($SZ==4); |
| 1595 |
.rva .LSEH_begin_${func}_ssse3 |
| 1596 |
.rva .LSEH_end_${func}_ssse3 |
| 1597 |
.rva .LSEH_info_${func}_ssse3 |
| 1598 |
___ |
| 1599 |
$code.=<<___ if ($avx); |
| 1600 |
.rva .LSEH_begin_${func}_avx |
| 1601 |
.rva .LSEH_end_${func}_avx |
| 1602 |
.rva .LSEH_info_${func}_avx |
| 1603 |
___ |
| 1604 |
$code.=<<___; |
| 1605 |
.section .xdata |
| 1606 |
.align 8 |
| 1607 |
.LSEH_info_${func}_nohw: |
| 1608 |
.byte 9,0,0,0 |
| 1609 |
.rva se_handler |
| 1610 |
.rva .Lprologue,.Lepilogue # HandlerData[] |
| 1611 |
___ |
| 1612 |
$code.=<<___ if ($SZ==4 && $shaext); |
| 1613 |
.LSEH_info_${func}_hw: |
| 1614 |
.byte 9,0,0,0 |
| 1615 |
.rva shaext_handler |
| 1616 |
___ |
| 1617 |
$code.=<<___ if ($SZ==4); |
| 1618 |
.LSEH_info_${func}_ssse3: |
| 1619 |
.byte 9,0,0,0 |
| 1620 |
.rva se_handler |
| 1621 |
.rva .Lprologue_ssse3,.Lepilogue_ssse3 # HandlerData[] |
| 1622 |
___ |
| 1623 |
$code.=<<___ if ($avx); |
| 1624 |
.LSEH_info_${func}_avx: |
| 1625 |
.byte 9,0,0,0 |
| 1626 |
.rva se_handler |
| 1627 |
.rva .Lprologue_avx,.Lepilogue_avx # HandlerData[] |
| 1628 |
___ |
| 1629 |
} |
| 1630 |
|
| 1631 |
sub sha256op38 { |
| 1632 |
my $instr = shift; |
| 1633 |
my %opcodelet = ( |
| 1634 |
"sha256rnds2" => 0xcb, |
| 1635 |
"sha256msg1" => 0xcc, |
| 1636 |
"sha256msg2" => 0xcd ); |
| 1637 |
|
| 1638 |
if (defined($opcodelet{$instr}) && @_[0] =~ /%xmm([0-7]),\s*%xmm([0-7])/) { |
| 1639 |
my @opcode=(0x0f,0x38); |
| 1640 |
push @opcode,$opcodelet{$instr}; |
| 1641 |
push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M |
| 1642 |
return ".byte\t".join(',',@opcode); |
| 1643 |
} else { |
| 1644 |
return $instr."\t".@_[0]; |
| 1645 |
} |
| 1646 |
} |
| 1647 |
|
| 1648 |
foreach (split("\n",$code)) { |
| 1649 |
s/\`([^\`]*)\`/eval $1/geo; |
| 1650 |
|
| 1651 |
s/\b(sha256[^\s]*)\s+(.*)/sha256op38($1,$2)/geo; |
| 1652 |
|
| 1653 |
print $_,"\n"; |
| 1654 |
} |
| 1655 |
close STDOUT or die "error closing STDOUT: $!"; |