1 #!/usr/bin/env perl 2 # 3 # ==================================================================== 4 # Written by Andy Polyakov <appro (at] fy.chalmers.se> for the OpenSSL 5 # project. The module is, however, dual licensed under OpenSSL and 6 # CRYPTOGAMS licenses depending on where you obtain it. For further 7 # details see http://www.openssl.org/~appro/cryptogams/. 8 # ==================================================================== 9 # 10 # This module implements support for Intel AES-NI extension. In 11 # OpenSSL context it's used with Intel engine, but can also be used as 12 # drop-in replacement for crypto/aes/asm/aes-x86_64.pl [see below for 13 # details]. 14 # 15 # Performance. 16 # 17 # Given aes(enc|dec) instructions' latency asymptotic performance for 18 # non-parallelizable modes such as CBC encrypt is 3.75 cycles per byte 19 # processed with 128-bit key. And given their throughput asymptotic 20 # performance for parallelizable modes is 1.25 cycles per byte. Being 21 # asymptotic limit it's not something you commonly achieve in reality, 22 # but how close does one get? Below are results collected for 23 # different modes and block sized. Pairs of numbers are for en-/ 24 # decryption. 25 # 26 # 16-byte 64-byte 256-byte 1-KB 8-KB 27 # ECB 4.25/4.25 1.38/1.38 1.28/1.28 1.26/1.26 1.26/1.26 28 # CTR 5.42/5.42 1.92/1.92 1.44/1.44 1.28/1.28 1.26/1.26 29 # CBC 4.38/4.43 4.15/1.43 4.07/1.32 4.07/1.29 4.06/1.28 30 # CCM 5.66/9.42 4.42/5.41 4.16/4.40 4.09/4.15 4.06/4.07 31 # OFB 5.42/5.42 4.64/4.64 4.44/4.44 4.39/4.39 4.38/4.38 32 # CFB 5.73/5.85 5.56/5.62 5.48/5.56 5.47/5.55 5.47/5.55 33 # 34 # ECB, CTR, CBC and CCM results are free from EVP overhead. This means 35 # that otherwise used 'openssl speed -evp aes-128-??? -engine aesni 36 # [-decrypt]' will exhibit 10-15% worse results for smaller blocks. 37 # The results were collected with specially crafted speed.c benchmark 38 # in order to compare them with results reported in "Intel Advanced 39 # Encryption Standard (AES) New Instruction Set" White Paper Revision 40 # 3.0 dated May 2010. All above results are consistently better. This 41 # module also provides better performance for block sizes smaller than 42 # 128 bytes in points *not* represented in the above table. 43 # 44 # Looking at the results for 8-KB buffer. 45 # 46 # CFB and OFB results are far from the limit, because implementation 47 # uses "generic" CRYPTO_[c|o]fb128_encrypt interfaces relying on 48 # single-block aesni_encrypt, which is not the most optimal way to go. 49 # CBC encrypt result is unexpectedly high and there is no documented 50 # explanation for it. Seemingly there is a small penalty for feeding 51 # the result back to AES unit the way it's done in CBC mode. There is 52 # nothing one can do and the result appears optimal. CCM result is 53 # identical to CBC, because CBC-MAC is essentially CBC encrypt without 54 # saving output. CCM CTR "stays invisible," because it's neatly 55 # interleaved wih CBC-MAC. This provides ~30% improvement over 56 # "straghtforward" CCM implementation with CTR and CBC-MAC performed 57 # disjointly. Parallelizable modes practically achieve the theoretical 58 # limit. 59 # 60 # Looking at how results vary with buffer size. 61 # 62 # Curves are practically saturated at 1-KB buffer size. In most cases 63 # "256-byte" performance is >95%, and "64-byte" is ~90% of "8-KB" one. 64 # CTR curve doesn't follow this pattern and is "slowest" changing one 65 # with "256-byte" result being 87% of "8-KB." This is because overhead 66 # in CTR mode is most computationally intensive. Small-block CCM 67 # decrypt is slower than encrypt, because first CTR and last CBC-MAC 68 # iterations can't be interleaved. 69 # 70 # Results for 192- and 256-bit keys. 71 # 72 # EVP-free results were observed to scale perfectly with number of 73 # rounds for larger block sizes, i.e. 192-bit result being 10/12 times 74 # lower and 256-bit one - 10/14. Well, in CBC encrypt case differences 75 # are a tad smaller, because the above mentioned penalty biases all 76 # results by same constant value. In similar way function call 77 # overhead affects small-block performance, as well as OFB and CFB 78 # results. Differences are not large, most common coefficients are 79 # 10/11.7 and 10/13.4 (as opposite to 10/12.0 and 10/14.0), but one 80 # observe even 10/11.2 and 10/12.4 (CTR, OFB, CFB)... 81 82 # January 2011 83 # 84 # While Westmere processor features 6 cycles latency for aes[enc|dec] 85 # instructions, which can be scheduled every second cycle, Sandy 86 # Bridge spends 8 cycles per instruction, but it can schedule them 87 # every cycle. This means that code targeting Westmere would perform 88 # suboptimally on Sandy Bridge. Therefore this update. 89 # 90 # In addition, non-parallelizable CBC encrypt (as well as CCM) is 91 # optimized. Relative improvement might appear modest, 8% on Westmere, 92 # but in absolute terms it's 3.77 cycles per byte encrypted with 93 # 128-bit key on Westmere, and 5.07 - on Sandy Bridge. These numbers 94 # should be compared to asymptotic limits of 3.75 for Westmere and 95 # 5.00 for Sandy Bridge. Actually, the fact that they get this close 96 # to asymptotic limits is quite amazing. Indeed, the limit is 97 # calculated as latency times number of rounds, 10 for 128-bit key, 98 # and divided by 16, the number of bytes in block, or in other words 99 # it accounts *solely* for aesenc instructions. But there are extra 100 # instructions, and numbers so close to the asymptotic limits mean 101 # that it's as if it takes as little as *one* additional cycle to 102 # execute all of them. How is it possible? It is possible thanks to 103 # out-of-order execution logic, which manages to overlap post- 104 # processing of previous block, things like saving the output, with 105 # actual encryption of current block, as well as pre-processing of 106 # current block, things like fetching input and xor-ing it with 107 # 0-round element of the key schedule, with actual encryption of 108 # previous block. Keep this in mind... 109 # 110 # For parallelizable modes, such as ECB, CBC decrypt, CTR, higher 111 # performance is achieved by interleaving instructions working on 112 # independent blocks. In which case asymptotic limit for such modes 113 # can be obtained by dividing above mentioned numbers by AES 114 # instructions' interleave factor. Westmere can execute at most 3 115 # instructions at a time, meaning that optimal interleave factor is 3, 116 # and that's where the "magic" number of 1.25 come from. "Optimal 117 # interleave factor" means that increase of interleave factor does 118 # not improve performance. The formula has proven to reflect reality 119 # pretty well on Westmere... Sandy Bridge on the other hand can 120 # execute up to 8 AES instructions at a time, so how does varying 121 # interleave factor affect the performance? Here is table for ECB 122 # (numbers are cycles per byte processed with 128-bit key): 123 # 124 # instruction interleave factor 3x 6x 8x 125 # theoretical asymptotic limit 1.67 0.83 0.625 126 # measured performance for 8KB block 1.05 0.86 0.84 127 # 128 # "as if" interleave factor 4.7x 5.8x 6.0x 129 # 130 # Further data for other parallelizable modes: 131 # 132 # CBC decrypt 1.16 0.93 0.93 133 # CTR 1.14 0.91 n/a 134 # 135 # Well, given 3x column it's probably inappropriate to call the limit 136 # asymptotic, if it can be surpassed, isn't it? What happens there? 137 # Rewind to CBC paragraph for the answer. Yes, out-of-order execution 138 # magic is responsible for this. Processor overlaps not only the 139 # additional instructions with AES ones, but even AES instuctions 140 # processing adjacent triplets of independent blocks. In the 6x case 141 # additional instructions still claim disproportionally small amount 142 # of additional cycles, but in 8x case number of instructions must be 143 # a tad too high for out-of-order logic to cope with, and AES unit 144 # remains underutilized... As you can see 8x interleave is hardly 145 # justifiable, so there no need to feel bad that 32-bit aesni-x86.pl 146 # utilizies 6x interleave because of limited register bank capacity. 147 # 148 # Higher interleave factors do have negative impact on Westmere 149 # performance. While for ECB mode it's negligible ~1.5%, other 150 # parallelizables perform ~5% worse, which is outweighed by ~25% 151 # improvement on Sandy Bridge. To balance regression on Westmere 152 # CTR mode was implemented with 6x aesenc interleave factor. 153 154 # April 2011 155 # 156 # Add aesni_xts_[en|de]crypt. Westmere spends 1.33 cycles processing 157 # one byte out of 8KB with 128-bit key, Sandy Bridge - 0.97. Just like 158 # in CTR mode AES instruction interleave factor was chosen to be 6x. 159 160 $PREFIX="aesni"; # if $PREFIX is set to "AES", the script 161 # generates drop-in replacement for 162 # crypto/aes/asm/aes-x86_64.pl:-) 163 164 $flavour = shift; 165 $output = shift; 166 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; } 167 168 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/); 169 170 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1; 171 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or 172 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or 173 die "can't locate x86_64-xlate.pl"; 174 175 open OUT,"| \"$^X\" $xlate $flavour $output"; 176 *STDOUT=*OUT; 177 178 $movkey = $PREFIX eq "aesni" ? "movups" : "movups"; 179 @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") : # Win64 order 180 ("%rdi","%rsi","%rdx","%rcx"); # Unix order 181 182 $code=".text\n"; 183 184 $rounds="%eax"; # input to and changed by aesni_[en|de]cryptN !!! 185 # this is natural Unix argument order for public $PREFIX_[ecb|cbc]_encrypt ... 186 $inp="%rdi"; 187 $out="%rsi"; 188 $len="%rdx"; 189 $key="%rcx"; # input to and changed by aesni_[en|de]cryptN !!! 190 $ivp="%r8"; # cbc, ctr, ... 191 192 $rnds_="%r10d"; # backup copy for $rounds 193 $key_="%r11"; # backup copy for $key 194 195 # %xmm register layout 196 $rndkey0="%xmm0"; $rndkey1="%xmm1"; 197 $inout0="%xmm2"; $inout1="%xmm3"; 198 $inout2="%xmm4"; $inout3="%xmm5"; 199 $inout4="%xmm6"; $inout5="%xmm7"; 200 $inout6="%xmm8"; $inout7="%xmm9"; 201 202 $in2="%xmm6"; $in1="%xmm7"; # used in CBC decrypt, CTR, ... 203 $in0="%xmm8"; $iv="%xmm9"; 204 206 # Inline version of internal aesni_[en|de]crypt1. 207 # 208 # Why folded loop? Because aes[enc|dec] is slow enough to accommodate 209 # cycles which take care of loop variables... 210 { my $sn; 211 sub aesni_generate1 { 212 my ($p,$key,$rounds,$inout,$ivec)=@_; $inout=$inout0 if (!defined($inout)); 213 ++$sn; 214 $code.=<<___; 215 $movkey ($key),$rndkey0 216 $movkey 16($key),$rndkey1 217 ___ 218 $code.=<<___ if (defined($ivec)); 219 xorps $rndkey0,$ivec 220 lea 32($key),$key 221 xorps $ivec,$inout 222 ___ 223 $code.=<<___ if (!defined($ivec)); 224 lea 32($key),$key 225 xorps $rndkey0,$inout 226 ___ 227 $code.=<<___; 228 .Loop_${p}1_$sn: 229 aes${p} $rndkey1,$inout 230 dec $rounds 231 $movkey ($key),$rndkey1 232 lea 16($key),$key 233 jnz .Loop_${p}1_$sn # loop body is 16 bytes 234 aes${p}last $rndkey1,$inout 235 ___ 236 }} 237 # void $PREFIX_[en|de]crypt (const void *inp,void *out,const AES_KEY *key); 238 # 239 { my ($inp,$out,$key) = @_4args; 240 241 $code.=<<___; 242 .globl ${PREFIX}_encrypt 243 .type ${PREFIX}_encrypt,\@abi-omnipotent 244 .align 16 245 ${PREFIX}_encrypt: 246 movups ($inp),$inout0 # load input 247 mov 240($key),$rounds # key->rounds 248 ___ 249 &aesni_generate1("enc",$key,$rounds); 250 $code.=<<___; 251 movups $inout0,($out) # output 252 ret 253 .size ${PREFIX}_encrypt,.-${PREFIX}_encrypt 254 255 .globl ${PREFIX}_decrypt 256 .type ${PREFIX}_decrypt,\@abi-omnipotent 257 .align 16 258 ${PREFIX}_decrypt: 259 movups ($inp),$inout0 # load input 260 mov 240($key),$rounds # key->rounds 261 ___ 262 &aesni_generate1("dec",$key,$rounds); 263 $code.=<<___; 264 movups $inout0,($out) # output 265 ret 266 .size ${PREFIX}_decrypt, .-${PREFIX}_decrypt 267 ___ 268 } 269 271 # _aesni_[en|de]cryptN are private interfaces, N denotes interleave 272 # factor. Why 3x subroutine were originally used in loops? Even though 273 # aes[enc|dec] latency was originally 6, it could be scheduled only 274 # every *2nd* cycle. Thus 3x interleave was the one providing optimal 275 # utilization, i.e. when subroutine's throughput is virtually same as 276 # of non-interleaved subroutine [for number of input blocks up to 3]. 277 # This is why it makes no sense to implement 2x subroutine. 278 # aes[enc|dec] latency in next processor generation is 8, but the 279 # instructions can be scheduled every cycle. Optimal interleave for 280 # new processor is therefore 8x... 281 sub aesni_generate3 { 282 my $dir=shift; 283 # As already mentioned it takes in $key and $rounds, which are *not* 284 # preserved. $inout[0-2] is cipher/clear text... 285 $code.=<<___; 286 .type _aesni_${dir}rypt3,\@abi-omnipotent 287 .align 16 288 _aesni_${dir}rypt3: 289 $movkey ($key),$rndkey0 290 shr \$1,$rounds 291 $movkey 16($key),$rndkey1 292 lea 32($key),$key 293 xorps $rndkey0,$inout0 294 xorps $rndkey0,$inout1 295 xorps $rndkey0,$inout2 296 $movkey ($key),$rndkey0 297 298 .L${dir}_loop3: 299 aes${dir} $rndkey1,$inout0 300 aes${dir} $rndkey1,$inout1 301 dec $rounds 302 aes${dir} $rndkey1,$inout2 303 $movkey 16($key),$rndkey1 304 aes${dir} $rndkey0,$inout0 305 aes${dir} $rndkey0,$inout1 306 lea 32($key),$key 307 aes${dir} $rndkey0,$inout2 308 $movkey ($key),$rndkey0 309 jnz .L${dir}_loop3 310 311 aes${dir} $rndkey1,$inout0 312 aes${dir} $rndkey1,$inout1 313 aes${dir} $rndkey1,$inout2 314 aes${dir}last $rndkey0,$inout0 315 aes${dir}last $rndkey0,$inout1 316 aes${dir}last $rndkey0,$inout2 317 ret 318 .size _aesni_${dir}rypt3,.-_aesni_${dir}rypt3 319 ___ 320 } 321 # 4x interleave is implemented to improve small block performance, 322 # most notably [and naturally] 4 block by ~30%. One can argue that one 323 # should have implemented 5x as well, but improvement would be <20%, 324 # so it's not worth it... 325 sub aesni_generate4 { 326 my $dir=shift; 327 # As already mentioned it takes in $key and $rounds, which are *not* 328 # preserved. $inout[0-3] is cipher/clear text... 329 $code.=<<___; 330 .type _aesni_${dir}rypt4,\@abi-omnipotent 331 .align 16 332 _aesni_${dir}rypt4: 333 $movkey ($key),$rndkey0 334 shr \$1,$rounds 335 $movkey 16($key),$rndkey1 336 lea 32($key),$key 337 xorps $rndkey0,$inout0 338 xorps $rndkey0,$inout1 339 xorps $rndkey0,$inout2 340 xorps $rndkey0,$inout3 341 $movkey ($key),$rndkey0 342 343 .L${dir}_loop4: 344 aes${dir} $rndkey1,$inout0 345 aes${dir} $rndkey1,$inout1 346 dec $rounds 347 aes${dir} $rndkey1,$inout2 348 aes${dir} $rndkey1,$inout3 349 $movkey 16($key),$rndkey1 350 aes${dir} $rndkey0,$inout0 351 aes${dir} $rndkey0,$inout1 352 lea 32($key),$key 353 aes${dir} $rndkey0,$inout2 354 aes${dir} $rndkey0,$inout3 355 $movkey ($key),$rndkey0 356 jnz .L${dir}_loop4 357 358 aes${dir} $rndkey1,$inout0 359 aes${dir} $rndkey1,$inout1 360 aes${dir} $rndkey1,$inout2 361 aes${dir} $rndkey1,$inout3 362 aes${dir}last $rndkey0,$inout0 363 aes${dir}last $rndkey0,$inout1 364 aes${dir}last $rndkey0,$inout2 365 aes${dir}last $rndkey0,$inout3 366 ret 367 .size _aesni_${dir}rypt4,.-_aesni_${dir}rypt4 368 ___ 369 } 370 sub aesni_generate6 { 371 my $dir=shift; 372 # As already mentioned it takes in $key and $rounds, which are *not* 373 # preserved. $inout[0-5] is cipher/clear text... 374 $code.=<<___; 375 .type _aesni_${dir}rypt6,\@abi-omnipotent 376 .align 16 377 _aesni_${dir}rypt6: 378 $movkey ($key),$rndkey0 379 shr \$1,$rounds 380 $movkey 16($key),$rndkey1 381 lea 32($key),$key 382 xorps $rndkey0,$inout0 383 pxor $rndkey0,$inout1 384 aes${dir} $rndkey1,$inout0 385 pxor $rndkey0,$inout2 386 aes${dir} $rndkey1,$inout1 387 pxor $rndkey0,$inout3 388 aes${dir} $rndkey1,$inout2 389 pxor $rndkey0,$inout4 390 aes${dir} $rndkey1,$inout3 391 pxor $rndkey0,$inout5 392 dec $rounds 393 aes${dir} $rndkey1,$inout4 394 $movkey ($key),$rndkey0 395 aes${dir} $rndkey1,$inout5 396 jmp .L${dir}_loop6_enter 397 .align 16 398 .L${dir}_loop6: 399 aes${dir} $rndkey1,$inout0 400 aes${dir} $rndkey1,$inout1 401 dec $rounds 402 aes${dir} $rndkey1,$inout2 403 aes${dir} $rndkey1,$inout3 404 aes${dir} $rndkey1,$inout4 405 aes${dir} $rndkey1,$inout5 406 .L${dir}_loop6_enter: # happens to be 16-byte aligned 407 $movkey 16($key),$rndkey1 408 aes${dir} $rndkey0,$inout0 409 aes${dir} $rndkey0,$inout1 410 lea 32($key),$key 411 aes${dir} $rndkey0,$inout2 412 aes${dir} $rndkey0,$inout3 413 aes${dir} $rndkey0,$inout4 414 aes${dir} $rndkey0,$inout5 415 $movkey ($key),$rndkey0 416 jnz .L${dir}_loop6 417 418 aes${dir} $rndkey1,$inout0 419 aes${dir} $rndkey1,$inout1 420 aes${dir} $rndkey1,$inout2 421 aes${dir} $rndkey1,$inout3 422 aes${dir} $rndkey1,$inout4 423 aes${dir} $rndkey1,$inout5 424 aes${dir}last $rndkey0,$inout0 425 aes${dir}last $rndkey0,$inout1 426 aes${dir}last $rndkey0,$inout2 427 aes${dir}last $rndkey0,$inout3 428 aes${dir}last $rndkey0,$inout4 429 aes${dir}last $rndkey0,$inout5 430 ret 431 .size _aesni_${dir}rypt6,.-_aesni_${dir}rypt6 432 ___ 433 } 434 sub aesni_generate8 { 435 my $dir=shift; 436 # As already mentioned it takes in $key and $rounds, which are *not* 437 # preserved. $inout[0-7] is cipher/clear text... 438 $code.=<<___; 439 .type _aesni_${dir}rypt8,\@abi-omnipotent 440 .align 16 441 _aesni_${dir}rypt8: 442 $movkey ($key),$rndkey0 443 shr \$1,$rounds 444 $movkey 16($key),$rndkey1 445 lea 32($key),$key 446 xorps $rndkey0,$inout0 447 xorps $rndkey0,$inout1 448 aes${dir} $rndkey1,$inout0 449 pxor $rndkey0,$inout2 450 aes${dir} $rndkey1,$inout1 451 pxor $rndkey0,$inout3 452 aes${dir} $rndkey1,$inout2 453 pxor $rndkey0,$inout4 454 aes${dir} $rndkey1,$inout3 455 pxor $rndkey0,$inout5 456 dec $rounds 457 aes${dir} $rndkey1,$inout4 458 pxor $rndkey0,$inout6 459 aes${dir} $rndkey1,$inout5 460 pxor $rndkey0,$inout7 461 $movkey ($key),$rndkey0 462 aes${dir} $rndkey1,$inout6 463 aes${dir} $rndkey1,$inout7 464 $movkey 16($key),$rndkey1 465 jmp .L${dir}_loop8_enter 466 .align 16 467 .L${dir}_loop8: 468 aes${dir} $rndkey1,$inout0 469 aes${dir} $rndkey1,$inout1 470 dec $rounds 471 aes${dir} $rndkey1,$inout2 472 aes${dir} $rndkey1,$inout3 473 aes${dir} $rndkey1,$inout4 474 aes${dir} $rndkey1,$inout5 475 aes${dir} $rndkey1,$inout6 476 aes${dir} $rndkey1,$inout7 477 $movkey 16($key),$rndkey1 478 .L${dir}_loop8_enter: # happens to be 16-byte aligned 479 aes${dir} $rndkey0,$inout0 480 aes${dir} $rndkey0,$inout1 481 lea 32($key),$key 482 aes${dir} $rndkey0,$inout2 483 aes${dir} $rndkey0,$inout3 484 aes${dir} $rndkey0,$inout4 485 aes${dir} $rndkey0,$inout5 486 aes${dir} $rndkey0,$inout6 487 aes${dir} $rndkey0,$inout7 488 $movkey ($key),$rndkey0 489 jnz .L${dir}_loop8 490 491 aes${dir} $rndkey1,$inout0 492 aes${dir} $rndkey1,$inout1 493 aes${dir} $rndkey1,$inout2 494 aes${dir} $rndkey1,$inout3 495 aes${dir} $rndkey1,$inout4 496 aes${dir} $rndkey1,$inout5 497 aes${dir} $rndkey1,$inout6 498 aes${dir} $rndkey1,$inout7 499 aes${dir}last $rndkey0,$inout0 500 aes${dir}last $rndkey0,$inout1 501 aes${dir}last $rndkey0,$inout2 502 aes${dir}last $rndkey0,$inout3 503 aes${dir}last $rndkey0,$inout4 504 aes${dir}last $rndkey0,$inout5 505 aes${dir}last $rndkey0,$inout6 506 aes${dir}last $rndkey0,$inout7 507 ret 508 .size _aesni_${dir}rypt8,.-_aesni_${dir}rypt8 509 ___ 510 } 511 &aesni_generate3("enc") if ($PREFIX eq "aesni"); 512 &aesni_generate3("dec"); 513 &aesni_generate4("enc") if ($PREFIX eq "aesni"); 514 &aesni_generate4("dec"); 515 &aesni_generate6("enc") if ($PREFIX eq "aesni"); 516 &aesni_generate6("dec"); 517 &aesni_generate8("enc") if ($PREFIX eq "aesni"); 518 &aesni_generate8("dec"); 519 521 if ($PREFIX eq "aesni") { 522 ######################################################################## 523 # void aesni_ecb_encrypt (const void *in, void *out, 524 # size_t length, const AES_KEY *key, 525 # int enc); 526 $code.=<<___; 527 .globl aesni_ecb_encrypt 528 .type aesni_ecb_encrypt,\@function,5 529 .align 16 530 aesni_ecb_encrypt: 531 ___ 532 $code.=<<___ if ($win64); 533 lea -0x58(%rsp),%rsp 534 movaps %xmm6,(%rsp) 535 movaps %xmm7,0x10(%rsp) 536 movaps %xmm8,0x20(%rsp) 537 movaps %xmm9,0x30(%rsp) 538 .Lecb_enc_body: 539 ___ 540 $code.=<<___; 541 and \$-16,$len 542 jz .Lecb_ret 543 544 mov 240($key),$rounds # key->rounds 545 $movkey ($key),$rndkey0 546 mov $key,$key_ # backup $key 547 mov $rounds,$rnds_ # backup $rounds 548 test %r8d,%r8d # 5th argument 549 jz .Lecb_decrypt 550 #--------------------------- ECB ENCRYPT ------------------------------# 551 cmp \$0x80,$len 552 jb .Lecb_enc_tail 553 554 movdqu ($inp),$inout0 555 movdqu 0x10($inp),$inout1 556 movdqu 0x20($inp),$inout2 557 movdqu 0x30($inp),$inout3 558 movdqu 0x40($inp),$inout4 559 movdqu 0x50($inp),$inout5 560 movdqu 0x60($inp),$inout6 561 movdqu 0x70($inp),$inout7 562 lea 0x80($inp),$inp 563 sub \$0x80,$len 564 jmp .Lecb_enc_loop8_enter 565 .align 16 566 .Lecb_enc_loop8: 567 movups $inout0,($out) 568 mov $key_,$key # restore $key 569 movdqu ($inp),$inout0 570 mov $rnds_,$rounds # restore $rounds 571 movups $inout1,0x10($out) 572 movdqu 0x10($inp),$inout1 573 movups $inout2,0x20($out) 574 movdqu 0x20($inp),$inout2 575 movups $inout3,0x30($out) 576 movdqu 0x30($inp),$inout3 577 movups $inout4,0x40($out) 578 movdqu 0x40($inp),$inout4 579 movups $inout5,0x50($out) 580 movdqu 0x50($inp),$inout5 581 movups $inout6,0x60($out) 582 movdqu 0x60($inp),$inout6 583 movups $inout7,0x70($out) 584 lea 0x80($out),$out 585 movdqu 0x70($inp),$inout7 586 lea 0x80($inp),$inp 587 .Lecb_enc_loop8_enter: 588 589 call _aesni_encrypt8 590 591 sub \$0x80,$len 592 jnc .Lecb_enc_loop8 593 594 movups $inout0,($out) 595 mov $key_,$key # restore $key 596 movups $inout1,0x10($out) 597 mov $rnds_,$rounds # restore $rounds 598 movups $inout2,0x20($out) 599 movups $inout3,0x30($out) 600 movups $inout4,0x40($out) 601 movups $inout5,0x50($out) 602 movups $inout6,0x60($out) 603 movups $inout7,0x70($out) 604 lea 0x80($out),$out 605 add \$0x80,$len 606 jz .Lecb_ret 607 608 .Lecb_enc_tail: 609 movups ($inp),$inout0 610 cmp \$0x20,$len 611 jb .Lecb_enc_one 612 movups 0x10($inp),$inout1 613 je .Lecb_enc_two 614 movups 0x20($inp),$inout2 615 cmp \$0x40,$len 616 jb .Lecb_enc_three 617 movups 0x30($inp),$inout3 618 je .Lecb_enc_four 619 movups 0x40($inp),$inout4 620 cmp \$0x60,$len 621 jb .Lecb_enc_five 622 movups 0x50($inp),$inout5 623 je .Lecb_enc_six 624 movdqu 0x60($inp),$inout6 625 call _aesni_encrypt8 626 movups $inout0,($out) 627 movups $inout1,0x10($out) 628 movups $inout2,0x20($out) 629 movups $inout3,0x30($out) 630 movups $inout4,0x40($out) 631 movups $inout5,0x50($out) 632 movups $inout6,0x60($out) 633 jmp .Lecb_ret 634 .align 16 635 .Lecb_enc_one: 636 ___ 637 &aesni_generate1("enc",$key,$rounds); 638 $code.=<<___; 639 movups $inout0,($out) 640 jmp .Lecb_ret 641 .align 16 642 .Lecb_enc_two: 643 xorps $inout2,$inout2 644 call _aesni_encrypt3 645 movups $inout0,($out) 646 movups $inout1,0x10($out) 647 jmp .Lecb_ret 648 .align 16 649 .Lecb_enc_three: 650 call _aesni_encrypt3 651 movups $inout0,($out) 652 movups $inout1,0x10($out) 653 movups $inout2,0x20($out) 654 jmp .Lecb_ret 655 .align 16 656 .Lecb_enc_four: 657 call _aesni_encrypt4 658 movups $inout0,($out) 659 movups $inout1,0x10($out) 660 movups $inout2,0x20($out) 661 movups $inout3,0x30($out) 662 jmp .Lecb_ret 663 .align 16 664 .Lecb_enc_five: 665 xorps $inout5,$inout5 666 call _aesni_encrypt6 667 movups $inout0,($out) 668 movups $inout1,0x10($out) 669 movups $inout2,0x20($out) 670 movups $inout3,0x30($out) 671 movups $inout4,0x40($out) 672 jmp .Lecb_ret 673 .align 16 674 .Lecb_enc_six: 675 call _aesni_encrypt6 676 movups $inout0,($out) 677 movups $inout1,0x10($out) 678 movups $inout2,0x20($out) 679 movups $inout3,0x30($out) 680 movups $inout4,0x40($out) 681 movups $inout5,0x50($out) 682 jmp .Lecb_ret 683 #--------------------------- ECB DECRYPT ------------------------------# 685 .align 16 686 .Lecb_decrypt: 687 cmp \$0x80,$len 688 jb .Lecb_dec_tail 689 690 movdqu ($inp),$inout0 691 movdqu 0x10($inp),$inout1 692 movdqu 0x20($inp),$inout2 693 movdqu 0x30($inp),$inout3 694 movdqu 0x40($inp),$inout4 695 movdqu 0x50($inp),$inout5 696 movdqu 0x60($inp),$inout6 697 movdqu 0x70($inp),$inout7 698 lea 0x80($inp),$inp 699 sub \$0x80,$len 700 jmp .Lecb_dec_loop8_enter 701 .align 16 702 .Lecb_dec_loop8: 703 movups $inout0,($out) 704 mov $key_,$key # restore $key 705 movdqu ($inp),$inout0 706 mov $rnds_,$rounds # restore $rounds 707 movups $inout1,0x10($out) 708 movdqu 0x10($inp),$inout1 709 movups $inout2,0x20($out) 710 movdqu 0x20($inp),$inout2 711 movups $inout3,0x30($out) 712 movdqu 0x30($inp),$inout3 713 movups $inout4,0x40($out) 714 movdqu 0x40($inp),$inout4 715 movups $inout5,0x50($out) 716 movdqu 0x50($inp),$inout5 717 movups $inout6,0x60($out) 718 movdqu 0x60($inp),$inout6 719 movups $inout7,0x70($out) 720 lea 0x80($out),$out 721 movdqu 0x70($inp),$inout7 722 lea 0x80($inp),$inp 723 .Lecb_dec_loop8_enter: 724 725 call _aesni_decrypt8 726 727 $movkey ($key_),$rndkey0 728 sub \$0x80,$len 729 jnc .Lecb_dec_loop8 730 731 movups $inout0,($out) 732 mov $key_,$key # restore $key 733 movups $inout1,0x10($out) 734 mov $rnds_,$rounds # restore $rounds 735 movups $inout2,0x20($out) 736 movups $inout3,0x30($out) 737 movups $inout4,0x40($out) 738 movups $inout5,0x50($out) 739 movups $inout6,0x60($out) 740 movups $inout7,0x70($out) 741 lea 0x80($out),$out 742 add \$0x80,$len 743 jz .Lecb_ret 744 745 .Lecb_dec_tail: 746 movups ($inp),$inout0 747 cmp \$0x20,$len 748 jb .Lecb_dec_one 749 movups 0x10($inp),$inout1 750 je .Lecb_dec_two 751 movups 0x20($inp),$inout2 752 cmp \$0x40,$len 753 jb .Lecb_dec_three 754 movups 0x30($inp),$inout3 755 je .Lecb_dec_four 756 movups 0x40($inp),$inout4 757 cmp \$0x60,$len 758 jb .Lecb_dec_five 759 movups 0x50($inp),$inout5 760 je .Lecb_dec_six 761 movups 0x60($inp),$inout6 762 $movkey ($key),$rndkey0 763 call _aesni_decrypt8 764 movups $inout0,($out) 765 movups $inout1,0x10($out) 766 movups $inout2,0x20($out) 767 movups $inout3,0x30($out) 768 movups $inout4,0x40($out) 769 movups $inout5,0x50($out) 770 movups $inout6,0x60($out) 771 jmp .Lecb_ret 772 .align 16 773 .Lecb_dec_one: 774 ___ 775 &aesni_generate1("dec",$key,$rounds); 776 $code.=<<___; 777 movups $inout0,($out) 778 jmp .Lecb_ret 779 .align 16 780 .Lecb_dec_two: 781 xorps $inout2,$inout2 782 call _aesni_decrypt3 783 movups $inout0,($out) 784 movups $inout1,0x10($out) 785 jmp .Lecb_ret 786 .align 16 787 .Lecb_dec_three: 788 call _aesni_decrypt3 789 movups $inout0,($out) 790 movups $inout1,0x10($out) 791 movups $inout2,0x20($out) 792 jmp .Lecb_ret 793 .align 16 794 .Lecb_dec_four: 795 call _aesni_decrypt4 796 movups $inout0,($out) 797 movups $inout1,0x10($out) 798 movups $inout2,0x20($out) 799 movups $inout3,0x30($out) 800 jmp .Lecb_ret 801 .align 16 802 .Lecb_dec_five: 803 xorps $inout5,$inout5 804 call _aesni_decrypt6 805 movups $inout0,($out) 806 movups $inout1,0x10($out) 807 movups $inout2,0x20($out) 808 movups $inout3,0x30($out) 809 movups $inout4,0x40($out) 810 jmp .Lecb_ret 811 .align 16 812 .Lecb_dec_six: 813 call _aesni_decrypt6 814 movups $inout0,($out) 815 movups $inout1,0x10($out) 816 movups $inout2,0x20($out) 817 movups $inout3,0x30($out) 818 movups $inout4,0x40($out) 819 movups $inout5,0x50($out) 820 821 .Lecb_ret: 822 ___ 823 $code.=<<___ if ($win64); 824 movaps (%rsp),%xmm6 825 movaps 0x10(%rsp),%xmm7 826 movaps 0x20(%rsp),%xmm8 827 movaps 0x30(%rsp),%xmm9 828 lea 0x58(%rsp),%rsp 829 .Lecb_enc_ret: 830 ___ 831 $code.=<<___; 832 ret 833 .size aesni_ecb_encrypt,.-aesni_ecb_encrypt 834 ___ 835 837 { 838 ###################################################################### 839 # void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out, 840 # size_t blocks, const AES_KEY *key, 841 # const char *ivec,char *cmac); 842 # 843 # Handles only complete blocks, operates on 64-bit counter and 844 # does not update *ivec! Nor does it finalize CMAC value 845 # (see engine/eng_aesni.c for details) 846 # 847 { 848 my $cmac="%r9"; # 6th argument 849 850 my $increment="%xmm6"; 851 my $bswap_mask="%xmm7"; 852 853 $code.=<<___; 854 .globl aesni_ccm64_encrypt_blocks 855 .type aesni_ccm64_encrypt_blocks,\@function,6 856 .align 16 857 aesni_ccm64_encrypt_blocks: 858 ___ 859 $code.=<<___ if ($win64); 860 lea -0x58(%rsp),%rsp 861 movaps %xmm6,(%rsp) 862 movaps %xmm7,0x10(%rsp) 863 movaps %xmm8,0x20(%rsp) 864 movaps %xmm9,0x30(%rsp) 865 .Lccm64_enc_body: 866 ___ 867 $code.=<<___; 868 mov 240($key),$rounds # key->rounds 869 movdqu ($ivp),$iv 870 movdqa .Lincrement64(%rip),$increment 871 movdqa .Lbswap_mask(%rip),$bswap_mask 872 873 shr \$1,$rounds 874 lea 0($key),$key_ 875 movdqu ($cmac),$inout1 876 movdqa $iv,$inout0 877 mov $rounds,$rnds_ 878 pshufb $bswap_mask,$iv 879 jmp .Lccm64_enc_outer 880 .align 16 881 .Lccm64_enc_outer: 882 $movkey ($key_),$rndkey0 883 mov $rnds_,$rounds 884 movups ($inp),$in0 # load inp 885 886 xorps $rndkey0,$inout0 # counter 887 $movkey 16($key_),$rndkey1 888 xorps $in0,$rndkey0 889 lea 32($key_),$key 890 xorps $rndkey0,$inout1 # cmac^=inp 891 $movkey ($key),$rndkey0 892 893 .Lccm64_enc2_loop: 894 aesenc $rndkey1,$inout0 895 dec $rounds 896 aesenc $rndkey1,$inout1 897 $movkey 16($key),$rndkey1 898 aesenc $rndkey0,$inout0 899 lea 32($key),$key 900 aesenc $rndkey0,$inout1 901 $movkey 0($key),$rndkey0 902 jnz .Lccm64_enc2_loop 903 aesenc $rndkey1,$inout0 904 aesenc $rndkey1,$inout1 905 paddq $increment,$iv 906 aesenclast $rndkey0,$inout0 907 aesenclast $rndkey0,$inout1 908 909 dec $len 910 lea 16($inp),$inp 911 xorps $inout0,$in0 # inp ^= E(iv) 912 movdqa $iv,$inout0 913 movups $in0,($out) # save output 914 lea 16($out),$out 915 pshufb $bswap_mask,$inout0 916 jnz .Lccm64_enc_outer 917 918 movups $inout1,($cmac) 919 ___ 920 $code.=<<___ if ($win64); 921 movaps (%rsp),%xmm6 922 movaps 0x10(%rsp),%xmm7 923 movaps 0x20(%rsp),%xmm8 924 movaps 0x30(%rsp),%xmm9 925 lea 0x58(%rsp),%rsp 926 .Lccm64_enc_ret: 927 ___ 928 $code.=<<___; 929 ret 930 .size aesni_ccm64_encrypt_blocks,.-aesni_ccm64_encrypt_blocks 931 ___ 932 ###################################################################### 933 $code.=<<___; 934 .globl aesni_ccm64_decrypt_blocks 935 .type aesni_ccm64_decrypt_blocks,\@function,6 936 .align 16 937 aesni_ccm64_decrypt_blocks: 938 ___ 939 $code.=<<___ if ($win64); 940 lea -0x58(%rsp),%rsp 941 movaps %xmm6,(%rsp) 942 movaps %xmm7,0x10(%rsp) 943 movaps %xmm8,0x20(%rsp) 944 movaps %xmm9,0x30(%rsp) 945 .Lccm64_dec_body: 946 ___ 947 $code.=<<___; 948 mov 240($key),$rounds # key->rounds 949 movups ($ivp),$iv 950 movdqu ($cmac),$inout1 951 movdqa .Lincrement64(%rip),$increment 952 movdqa .Lbswap_mask(%rip),$bswap_mask 953 954 movaps $iv,$inout0 955 mov $rounds,$rnds_ 956 mov $key,$key_ 957 pshufb $bswap_mask,$iv 958 ___ 959 &aesni_generate1("enc",$key,$rounds); 960 $code.=<<___; 961 movups ($inp),$in0 # load inp 962 paddq $increment,$iv 963 lea 16($inp),$inp 964 jmp .Lccm64_dec_outer 965 .align 16 966 .Lccm64_dec_outer: 967 xorps $inout0,$in0 # inp ^= E(iv) 968 movdqa $iv,$inout0 969 mov $rnds_,$rounds 970 movups $in0,($out) # save output 971 lea 16($out),$out 972 pshufb $bswap_mask,$inout0 973 974 sub \$1,$len 975 jz .Lccm64_dec_break 976 977 $movkey ($key_),$rndkey0 978 shr \$1,$rounds 979 $movkey 16($key_),$rndkey1 980 xorps $rndkey0,$in0 981 lea 32($key_),$key 982 xorps $rndkey0,$inout0 983 xorps $in0,$inout1 # cmac^=out 984 $movkey ($key),$rndkey0 985 986 .Lccm64_dec2_loop: 987 aesenc $rndkey1,$inout0 988 dec $rounds 989 aesenc $rndkey1,$inout1 990 $movkey 16($key),$rndkey1 991 aesenc $rndkey0,$inout0 992 lea 32($key),$key 993 aesenc $rndkey0,$inout1 994 $movkey 0($key),$rndkey0 995 jnz .Lccm64_dec2_loop 996 movups ($inp),$in0 # load inp 997 paddq $increment,$iv 998 aesenc $rndkey1,$inout0 999 aesenc $rndkey1,$inout1 1000 lea 16($inp),$inp 1001 aesenclast $rndkey0,$inout0 1002 aesenclast $rndkey0,$inout1 1003 jmp .Lccm64_dec_outer 1004 1005 .align 16 1006 .Lccm64_dec_break: 1007 #xorps $in0,$inout1 # cmac^=out 1008 ___ 1009 &aesni_generate1("enc",$key_,$rounds,$inout1,$in0); 1010 $code.=<<___; 1011 movups $inout1,($cmac) 1012 ___ 1013 $code.=<<___ if ($win64); 1014 movaps (%rsp),%xmm6 1015 movaps 0x10(%rsp),%xmm7 1016 movaps 0x20(%rsp),%xmm8 1017 movaps 0x30(%rsp),%xmm9 1018 lea 0x58(%rsp),%rsp 1019 .Lccm64_dec_ret: 1020 ___ 1021 $code.=<<___; 1022 ret 1023 .size aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks 1024 ___ 1025 } 1027 ###################################################################### 1028 # void aesni_ctr32_encrypt_blocks (const void *in, void *out, 1029 # size_t blocks, const AES_KEY *key, 1030 # const char *ivec); 1031 # 1032 # Handles only complete blocks, operates on 32-bit counter and 1033 # does not update *ivec! (see engine/eng_aesni.c for details) 1034 # 1035 { 1036 my $reserved = $win64?0:-0x28; 1037 my ($in0,$in1,$in2,$in3)=map("%xmm$_",(8..11)); 1038 my ($iv0,$iv1,$ivec)=("%xmm12","%xmm13","%xmm14"); 1039 my $bswap_mask="%xmm15"; 1040 1041 $code.=<<___; 1042 .globl aesni_ctr32_encrypt_blocks 1043 .type aesni_ctr32_encrypt_blocks,\@function,5 1044 .align 16 1045 aesni_ctr32_encrypt_blocks: 1046 ___ 1047 $code.=<<___ if ($win64); 1048 lea -0xc8(%rsp),%rsp 1049 movaps %xmm6,0x20(%rsp) 1050 movaps %xmm7,0x30(%rsp) 1051 movaps %xmm8,0x40(%rsp) 1052 movaps %xmm9,0x50(%rsp) 1053 movaps %xmm10,0x60(%rsp) 1054 movaps %xmm11,0x70(%rsp) 1055 movaps %xmm12,0x80(%rsp) 1056 movaps %xmm13,0x90(%rsp) 1057 movaps %xmm14,0xa0(%rsp) 1058 movaps %xmm15,0xb0(%rsp) 1059 .Lctr32_body: 1060 ___ 1061 $code.=<<___; 1062 cmp \$1,$len 1063 je .Lctr32_one_shortcut 1064 1065 movdqu ($ivp),$ivec 1066 movdqa .Lbswap_mask(%rip),$bswap_mask 1067 xor $rounds,$rounds 1068 pextrd \$3,$ivec,$rnds_ # pull 32-bit counter 1069 pinsrd \$3,$rounds,$ivec # wipe 32-bit counter 1070 1071 mov 240($key),$rounds # key->rounds 1072 bswap $rnds_ 1073 pxor $iv0,$iv0 # vector of 3 32-bit counters 1074 pxor $iv1,$iv1 # vector of 3 32-bit counters 1075 pinsrd \$0,$rnds_,$iv0 1076 lea 3($rnds_),$key_ 1077 pinsrd \$0,$key_,$iv1 1078 inc $rnds_ 1079 pinsrd \$1,$rnds_,$iv0 1080 inc $key_ 1081 pinsrd \$1,$key_,$iv1 1082 inc $rnds_ 1083 pinsrd \$2,$rnds_,$iv0 1084 inc $key_ 1085 pinsrd \$2,$key_,$iv1 1086 movdqa $iv0,$reserved(%rsp) 1087 pshufb $bswap_mask,$iv0 1088 movdqa $iv1,`$reserved+0x10`(%rsp) 1089 pshufb $bswap_mask,$iv1 1090 1091 pshufd \$`3<<6`,$iv0,$inout0 # place counter to upper dword 1092 pshufd \$`2<<6`,$iv0,$inout1 1093 pshufd \$`1<<6`,$iv0,$inout2 1094 cmp \$6,$len 1095 jb .Lctr32_tail 1096 shr \$1,$rounds 1097 mov $key,$key_ # backup $key 1098 mov $rounds,$rnds_ # backup $rounds 1099 sub \$6,$len 1100 jmp .Lctr32_loop6 1101 1102 .align 16 1103 .Lctr32_loop6: 1104 pshufd \$`3<<6`,$iv1,$inout3 1105 por $ivec,$inout0 # merge counter-less ivec 1106 $movkey ($key_),$rndkey0 1107 pshufd \$`2<<6`,$iv1,$inout4 1108 por $ivec,$inout1 1109 $movkey 16($key_),$rndkey1 1110 pshufd \$`1<<6`,$iv1,$inout5 1111 por $ivec,$inout2 1112 por $ivec,$inout3 1113 xorps $rndkey0,$inout0 1114 por $ivec,$inout4 1115 por $ivec,$inout5 1116 1117 # inline _aesni_encrypt6 and interleave last rounds 1118 # with own code... 1119 1120 pxor $rndkey0,$inout1 1121 aesenc $rndkey1,$inout0 1122 lea 32($key_),$key 1123 pxor $rndkey0,$inout2 1124 aesenc $rndkey1,$inout1 1125 movdqa .Lincrement32(%rip),$iv1 1126 pxor $rndkey0,$inout3 1127 aesenc $rndkey1,$inout2 1128 movdqa $reserved(%rsp),$iv0 1129 pxor $rndkey0,$inout4 1130 aesenc $rndkey1,$inout3 1131 pxor $rndkey0,$inout5 1132 $movkey ($key),$rndkey0 1133 dec $rounds 1134 aesenc $rndkey1,$inout4 1135 aesenc $rndkey1,$inout5 1136 jmp .Lctr32_enc_loop6_enter 1137 .align 16 1138 .Lctr32_enc_loop6: 1139 aesenc $rndkey1,$inout0 1140 aesenc $rndkey1,$inout1 1141 dec $rounds 1142 aesenc $rndkey1,$inout2 1143 aesenc $rndkey1,$inout3 1144 aesenc $rndkey1,$inout4 1145 aesenc $rndkey1,$inout5 1146 .Lctr32_enc_loop6_enter: 1147 $movkey 16($key),$rndkey1 1148 aesenc $rndkey0,$inout0 1149 aesenc $rndkey0,$inout1 1150 lea 32($key),$key 1151 aesenc $rndkey0,$inout2 1152 aesenc $rndkey0,$inout3 1153 aesenc $rndkey0,$inout4 1154 aesenc $rndkey0,$inout5 1155 $movkey ($key),$rndkey0 1156 jnz .Lctr32_enc_loop6 1157 1158 aesenc $rndkey1,$inout0 1159 paddd $iv1,$iv0 # increment counter vector 1160 aesenc $rndkey1,$inout1 1161 paddd `$reserved+0x10`(%rsp),$iv1 1162 aesenc $rndkey1,$inout2 1163 movdqa $iv0,$reserved(%rsp) # save counter vector 1164 aesenc $rndkey1,$inout3 1165 movdqa $iv1,`$reserved+0x10`(%rsp) 1166 aesenc $rndkey1,$inout4 1167 pshufb $bswap_mask,$iv0 # byte swap 1168 aesenc $rndkey1,$inout5 1169 pshufb $bswap_mask,$iv1 1170 1171 aesenclast $rndkey0,$inout0 1172 movups ($inp),$in0 # load input 1173 aesenclast $rndkey0,$inout1 1174 movups 0x10($inp),$in1 1175 aesenclast $rndkey0,$inout2 1176 movups 0x20($inp),$in2 1177 aesenclast $rndkey0,$inout3 1178 movups 0x30($inp),$in3 1179 aesenclast $rndkey0,$inout4 1180 movups 0x40($inp),$rndkey1 1181 aesenclast $rndkey0,$inout5 1182 movups 0x50($inp),$rndkey0 1183 lea 0x60($inp),$inp 1184 1185 xorps $inout0,$in0 # xor 1186 pshufd \$`3<<6`,$iv0,$inout0 1187 xorps $inout1,$in1 1188 pshufd \$`2<<6`,$iv0,$inout1 1189 movups $in0,($out) # store output 1190 xorps $inout2,$in2 1191 pshufd \$`1<<6`,$iv0,$inout2 1192 movups $in1,0x10($out) 1193 xorps $inout3,$in3 1194 movups $in2,0x20($out) 1195 xorps $inout4,$rndkey1 1196 movups $in3,0x30($out) 1197 xorps $inout5,$rndkey0 1198 movups $rndkey1,0x40($out) 1199 movups $rndkey0,0x50($out) 1200 lea 0x60($out),$out 1201 mov $rnds_,$rounds 1202 sub \$6,$len 1203 jnc .Lctr32_loop6 1204 1205 add \$6,$len 1206 jz .Lctr32_done 1207 mov $key_,$key # restore $key 1208 lea 1($rounds,$rounds),$rounds # restore original value 1209 1210 .Lctr32_tail: 1211 por $ivec,$inout0 1212 movups ($inp),$in0 1213 cmp \$2,$len 1214 jb .Lctr32_one 1215 1216 por $ivec,$inout1 1217 movups 0x10($inp),$in1 1218 je .Lctr32_two 1219 1220 pshufd \$`3<<6`,$iv1,$inout3 1221 por $ivec,$inout2 1222 movups 0x20($inp),$in2 1223 cmp \$4,$len 1224 jb .Lctr32_three 1225 1226 pshufd \$`2<<6`,$iv1,$inout4 1227 por $ivec,$inout3 1228 movups 0x30($inp),$in3 1229 je .Lctr32_four 1230 1231 por $ivec,$inout4 1232 xorps $inout5,$inout5 1233 1234 call _aesni_encrypt6 1235 1236 movups 0x40($inp),$rndkey1 1237 xorps $inout0,$in0 1238 xorps $inout1,$in1 1239 movups $in0,($out) 1240 xorps $inout2,$in2 1241 movups $in1,0x10($out) 1242 xorps $inout3,$in3 1243 movups $in2,0x20($out) 1244 xorps $inout4,$rndkey1 1245 movups $in3,0x30($out) 1246 movups $rndkey1,0x40($out) 1247 jmp .Lctr32_done 1248 1249 .align 16 1250 .Lctr32_one_shortcut: 1251 movups ($ivp),$inout0 1252 movups ($inp),$in0 1253 mov 240($key),$rounds # key->rounds 1254 .Lctr32_one: 1255 ___ 1256 &aesni_generate1("enc",$key,$rounds); 1257 $code.=<<___; 1258 xorps $inout0,$in0 1259 movups $in0,($out) 1260 jmp .Lctr32_done 1261 1262 .align 16 1263 .Lctr32_two: 1264 xorps $inout2,$inout2 1265 call _aesni_encrypt3 1266 xorps $inout0,$in0 1267 xorps $inout1,$in1 1268 movups $in0,($out) 1269 movups $in1,0x10($out) 1270 jmp .Lctr32_done 1271 1272 .align 16 1273 .Lctr32_three: 1274 call _aesni_encrypt3 1275 xorps $inout0,$in0 1276 xorps $inout1,$in1 1277 movups $in0,($out) 1278 xorps $inout2,$in2 1279 movups $in1,0x10($out) 1280 movups $in2,0x20($out) 1281 jmp .Lctr32_done 1282 1283 .align 16 1284 .Lctr32_four: 1285 call _aesni_encrypt4 1286 xorps $inout0,$in0 1287 xorps $inout1,$in1 1288 movups $in0,($out) 1289 xorps $inout2,$in2 1290 movups $in1,0x10($out) 1291 xorps $inout3,$in3 1292 movups $in2,0x20($out) 1293 movups $in3,0x30($out) 1294 1295 .Lctr32_done: 1296 ___ 1297 $code.=<<___ if ($win64); 1298 movaps 0x20(%rsp),%xmm6 1299 movaps 0x30(%rsp),%xmm7 1300 movaps 0x40(%rsp),%xmm8 1301 movaps 0x50(%rsp),%xmm9 1302 movaps 0x60(%rsp),%xmm10 1303 movaps 0x70(%rsp),%xmm11 1304 movaps 0x80(%rsp),%xmm12 1305 movaps 0x90(%rsp),%xmm13 1306 movaps 0xa0(%rsp),%xmm14 1307 movaps 0xb0(%rsp),%xmm15 1308 lea 0xc8(%rsp),%rsp 1309 .Lctr32_ret: 1310 ___ 1311 $code.=<<___; 1312 ret 1313 .size aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks 1314 ___ 1315 } 1316 1318 ###################################################################### 1319 # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len, 1320 # const AES_KEY *key1, const AES_KEY *key2 1321 # const unsigned char iv[16]); 1322 # 1323 { 1324 my @tweak=map("%xmm$_",(10..15)); 1325 my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]); 1326 my ($key2,$ivp,$len_)=("%r8","%r9","%r9"); 1327 my $frame_size = 0x68 + ($win64?160:0); 1328 1329 $code.=<<___; 1330 .globl aesni_xts_encrypt 1331 .type aesni_xts_encrypt,\@function,6 1332 .align 16 1333 aesni_xts_encrypt: 1334 lea -$frame_size(%rsp),%rsp 1335 ___ 1336 $code.=<<___ if ($win64); 1337 movaps %xmm6,0x60(%rsp) 1338 movaps %xmm7,0x70(%rsp) 1339 movaps %xmm8,0x80(%rsp) 1340 movaps %xmm9,0x90(%rsp) 1341 movaps %xmm10,0xa0(%rsp) 1342 movaps %xmm11,0xb0(%rsp) 1343 movaps %xmm12,0xc0(%rsp) 1344 movaps %xmm13,0xd0(%rsp) 1345 movaps %xmm14,0xe0(%rsp) 1346 movaps %xmm15,0xf0(%rsp) 1347 .Lxts_enc_body: 1348 ___ 1349 $code.=<<___; 1350 movups ($ivp),@tweak[5] # load clear-text tweak 1351 mov 240(%r8),$rounds # key2->rounds 1352 mov 240($key),$rnds_ # key1->rounds 1353 ___ 1354 # generate the tweak 1355 &aesni_generate1("enc",$key2,$rounds,@tweak[5]); 1356 $code.=<<___; 1357 mov $key,$key_ # backup $key 1358 mov $rnds_,$rounds # backup $rounds 1359 mov $len,$len_ # backup $len 1360 and \$-16,$len 1361 1362 movdqa .Lxts_magic(%rip),$twmask 1363 pxor $twtmp,$twtmp 1364 pcmpgtd @tweak[5],$twtmp # broadcast upper bits 1365 ___ 1366 for ($i=0;$i<4;$i++) { 1367 $code.=<<___; 1368 pshufd \$0x13,$twtmp,$twres 1369 pxor $twtmp,$twtmp 1370 movdqa @tweak[5],@tweak[$i] 1371 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1372 pand $twmask,$twres # isolate carry and residue 1373 pcmpgtd @tweak[5],$twtmp # broadcat upper bits 1374 pxor $twres,@tweak[5] 1375 ___ 1376 } 1377 $code.=<<___; 1378 sub \$16*6,$len 1379 jc .Lxts_enc_short 1380 1381 shr \$1,$rounds 1382 sub \$1,$rounds 1383 mov $rounds,$rnds_ 1384 jmp .Lxts_enc_grandloop 1385 1386 .align 16 1387 .Lxts_enc_grandloop: 1388 pshufd \$0x13,$twtmp,$twres 1389 movdqa @tweak[5],@tweak[4] 1390 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1391 movdqu `16*0`($inp),$inout0 # load input 1392 pand $twmask,$twres # isolate carry and residue 1393 movdqu `16*1`($inp),$inout1 1394 pxor $twres,@tweak[5] 1395 1396 movdqu `16*2`($inp),$inout2 1397 pxor @tweak[0],$inout0 # input^=tweak 1398 movdqu `16*3`($inp),$inout3 1399 pxor @tweak[1],$inout1 1400 movdqu `16*4`($inp),$inout4 1401 pxor @tweak[2],$inout2 1402 movdqu `16*5`($inp),$inout5 1403 lea `16*6`($inp),$inp 1404 pxor @tweak[3],$inout3 1405 $movkey ($key_),$rndkey0 1406 pxor @tweak[4],$inout4 1407 pxor @tweak[5],$inout5 1408 1409 # inline _aesni_encrypt6 and interleave first and last rounds 1410 # with own code... 1411 $movkey 16($key_),$rndkey1 1412 pxor $rndkey0,$inout0 1413 pxor $rndkey0,$inout1 1414 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks 1415 aesenc $rndkey1,$inout0 1416 lea 32($key_),$key 1417 pxor $rndkey0,$inout2 1418 movdqa @tweak[1],`16*1`(%rsp) 1419 aesenc $rndkey1,$inout1 1420 pxor $rndkey0,$inout3 1421 movdqa @tweak[2],`16*2`(%rsp) 1422 aesenc $rndkey1,$inout2 1423 pxor $rndkey0,$inout4 1424 movdqa @tweak[3],`16*3`(%rsp) 1425 aesenc $rndkey1,$inout3 1426 pxor $rndkey0,$inout5 1427 $movkey ($key),$rndkey0 1428 dec $rounds 1429 movdqa @tweak[4],`16*4`(%rsp) 1430 aesenc $rndkey1,$inout4 1431 movdqa @tweak[5],`16*5`(%rsp) 1432 aesenc $rndkey1,$inout5 1433 pxor $twtmp,$twtmp 1434 pcmpgtd @tweak[5],$twtmp 1435 jmp .Lxts_enc_loop6_enter 1436 1437 .align 16 1438 .Lxts_enc_loop6: 1439 aesenc $rndkey1,$inout0 1440 aesenc $rndkey1,$inout1 1441 dec $rounds 1442 aesenc $rndkey1,$inout2 1443 aesenc $rndkey1,$inout3 1444 aesenc $rndkey1,$inout4 1445 aesenc $rndkey1,$inout5 1446 .Lxts_enc_loop6_enter: 1447 $movkey 16($key),$rndkey1 1448 aesenc $rndkey0,$inout0 1449 aesenc $rndkey0,$inout1 1450 lea 32($key),$key 1451 aesenc $rndkey0,$inout2 1452 aesenc $rndkey0,$inout3 1453 aesenc $rndkey0,$inout4 1454 aesenc $rndkey0,$inout5 1455 $movkey ($key),$rndkey0 1456 jnz .Lxts_enc_loop6 1457 1458 pshufd \$0x13,$twtmp,$twres 1459 pxor $twtmp,$twtmp 1460 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1461 aesenc $rndkey1,$inout0 1462 pand $twmask,$twres # isolate carry and residue 1463 aesenc $rndkey1,$inout1 1464 pcmpgtd @tweak[5],$twtmp # broadcast upper bits 1465 aesenc $rndkey1,$inout2 1466 pxor $twres,@tweak[5] 1467 aesenc $rndkey1,$inout3 1468 aesenc $rndkey1,$inout4 1469 aesenc $rndkey1,$inout5 1470 $movkey 16($key),$rndkey1 1471 1472 pshufd \$0x13,$twtmp,$twres 1473 pxor $twtmp,$twtmp 1474 movdqa @tweak[5],@tweak[0] 1475 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1476 aesenc $rndkey0,$inout0 1477 pand $twmask,$twres # isolate carry and residue 1478 aesenc $rndkey0,$inout1 1479 pcmpgtd @tweak[5],$twtmp # broadcat upper bits 1480 aesenc $rndkey0,$inout2 1481 pxor $twres,@tweak[5] 1482 aesenc $rndkey0,$inout3 1483 aesenc $rndkey0,$inout4 1484 aesenc $rndkey0,$inout5 1485 $movkey 32($key),$rndkey0 1486 1487 pshufd \$0x13,$twtmp,$twres 1488 pxor $twtmp,$twtmp 1489 movdqa @tweak[5],@tweak[1] 1490 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1491 aesenc $rndkey1,$inout0 1492 pand $twmask,$twres # isolate carry and residue 1493 aesenc $rndkey1,$inout1 1494 pcmpgtd @tweak[5],$twtmp # broadcat upper bits 1495 aesenc $rndkey1,$inout2 1496 pxor $twres,@tweak[5] 1497 aesenc $rndkey1,$inout3 1498 aesenc $rndkey1,$inout4 1499 aesenc $rndkey1,$inout5 1500 1501 pshufd \$0x13,$twtmp,$twres 1502 pxor $twtmp,$twtmp 1503 movdqa @tweak[5],@tweak[2] 1504 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1505 aesenclast $rndkey0,$inout0 1506 pand $twmask,$twres # isolate carry and residue 1507 aesenclast $rndkey0,$inout1 1508 pcmpgtd @tweak[5],$twtmp # broadcat upper bits 1509 aesenclast $rndkey0,$inout2 1510 pxor $twres,@tweak[5] 1511 aesenclast $rndkey0,$inout3 1512 aesenclast $rndkey0,$inout4 1513 aesenclast $rndkey0,$inout5 1514 1515 pshufd \$0x13,$twtmp,$twres 1516 pxor $twtmp,$twtmp 1517 movdqa @tweak[5],@tweak[3] 1518 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1519 xorps `16*0`(%rsp),$inout0 # output^=tweak 1520 pand $twmask,$twres # isolate carry and residue 1521 xorps `16*1`(%rsp),$inout1 1522 pcmpgtd @tweak[5],$twtmp # broadcat upper bits 1523 pxor $twres,@tweak[5] 1524 1525 xorps `16*2`(%rsp),$inout2 1526 movups $inout0,`16*0`($out) # write output 1527 xorps `16*3`(%rsp),$inout3 1528 movups $inout1,`16*1`($out) 1529 xorps `16*4`(%rsp),$inout4 1530 movups $inout2,`16*2`($out) 1531 xorps `16*5`(%rsp),$inout5 1532 movups $inout3,`16*3`($out) 1533 mov $rnds_,$rounds # restore $rounds 1534 movups $inout4,`16*4`($out) 1535 movups $inout5,`16*5`($out) 1536 lea `16*6`($out),$out 1537 sub \$16*6,$len 1538 jnc .Lxts_enc_grandloop 1539 1540 lea 3($rounds,$rounds),$rounds # restore original value 1541 mov $key_,$key # restore $key 1542 mov $rounds,$rnds_ # backup $rounds 1543 1544 .Lxts_enc_short: 1545 add \$16*6,$len 1546 jz .Lxts_enc_done 1547 1548 cmp \$0x20,$len 1549 jb .Lxts_enc_one 1550 je .Lxts_enc_two 1551 1552 cmp \$0x40,$len 1553 jb .Lxts_enc_three 1554 je .Lxts_enc_four 1555 1556 pshufd \$0x13,$twtmp,$twres 1557 movdqa @tweak[5],@tweak[4] 1558 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1559 movdqu ($inp),$inout0 1560 pand $twmask,$twres # isolate carry and residue 1561 movdqu 16*1($inp),$inout1 1562 pxor $twres,@tweak[5] 1563 1564 movdqu 16*2($inp),$inout2 1565 pxor @tweak[0],$inout0 1566 movdqu 16*3($inp),$inout3 1567 pxor @tweak[1],$inout1 1568 movdqu 16*4($inp),$inout4 1569 lea 16*5($inp),$inp 1570 pxor @tweak[2],$inout2 1571 pxor @tweak[3],$inout3 1572 pxor @tweak[4],$inout4 1573 1574 call _aesni_encrypt6 1575 1576 xorps @tweak[0],$inout0 1577 movdqa @tweak[5],@tweak[0] 1578 xorps @tweak[1],$inout1 1579 xorps @tweak[2],$inout2 1580 movdqu $inout0,($out) 1581 xorps @tweak[3],$inout3 1582 movdqu $inout1,16*1($out) 1583 xorps @tweak[4],$inout4 1584 movdqu $inout2,16*2($out) 1585 movdqu $inout3,16*3($out) 1586 movdqu $inout4,16*4($out) 1587 lea 16*5($out),$out 1588 jmp .Lxts_enc_done 1589 1590 .align 16 1591 .Lxts_enc_one: 1592 movups ($inp),$inout0 1593 lea 16*1($inp),$inp 1594 xorps @tweak[0],$inout0 1595 ___ 1596 &aesni_generate1("enc",$key,$rounds); 1597 $code.=<<___; 1598 xorps @tweak[0],$inout0 1599 movdqa @tweak[1],@tweak[0] 1600 movups $inout0,($out) 1601 lea 16*1($out),$out 1602 jmp .Lxts_enc_done 1603 1604 .align 16 1605 .Lxts_enc_two: 1606 movups ($inp),$inout0 1607 movups 16($inp),$inout1 1608 lea 32($inp),$inp 1609 xorps @tweak[0],$inout0 1610 xorps @tweak[1],$inout1 1611 1612 call _aesni_encrypt3 1613 1614 xorps @tweak[0],$inout0 1615 movdqa @tweak[2],@tweak[0] 1616 xorps @tweak[1],$inout1 1617 movups $inout0,($out) 1618 movups $inout1,16*1($out) 1619 lea 16*2($out),$out 1620 jmp .Lxts_enc_done 1621 1622 .align 16 1623 .Lxts_enc_three: 1624 movups ($inp),$inout0 1625 movups 16*1($inp),$inout1 1626 movups 16*2($inp),$inout2 1627 lea 16*3($inp),$inp 1628 xorps @tweak[0],$inout0 1629 xorps @tweak[1],$inout1 1630 xorps @tweak[2],$inout2 1631 1632 call _aesni_encrypt3 1633 1634 xorps @tweak[0],$inout0 1635 movdqa @tweak[3],@tweak[0] 1636 xorps @tweak[1],$inout1 1637 xorps @tweak[2],$inout2 1638 movups $inout0,($out) 1639 movups $inout1,16*1($out) 1640 movups $inout2,16*2($out) 1641 lea 16*3($out),$out 1642 jmp .Lxts_enc_done 1643 1644 .align 16 1645 .Lxts_enc_four: 1646 movups ($inp),$inout0 1647 movups 16*1($inp),$inout1 1648 movups 16*2($inp),$inout2 1649 xorps @tweak[0],$inout0 1650 movups 16*3($inp),$inout3 1651 lea 16*4($inp),$inp 1652 xorps @tweak[1],$inout1 1653 xorps @tweak[2],$inout2 1654 xorps @tweak[3],$inout3 1655 1656 call _aesni_encrypt4 1657 1658 xorps @tweak[0],$inout0 1659 movdqa @tweak[5],@tweak[0] 1660 xorps @tweak[1],$inout1 1661 xorps @tweak[2],$inout2 1662 movups $inout0,($out) 1663 xorps @tweak[3],$inout3 1664 movups $inout1,16*1($out) 1665 movups $inout2,16*2($out) 1666 movups $inout3,16*3($out) 1667 lea 16*4($out),$out 1668 jmp .Lxts_enc_done 1669 1670 .align 16 1671 .Lxts_enc_done: 1672 and \$15,$len_ 1673 jz .Lxts_enc_ret 1674 mov $len_,$len 1675 1676 .Lxts_enc_steal: 1677 movzb ($inp),%eax # borrow $rounds ... 1678 movzb -16($out),%ecx # ... and $key 1679 lea 1($inp),$inp 1680 mov %al,-16($out) 1681 mov %cl,0($out) 1682 lea 1($out),$out 1683 sub \$1,$len 1684 jnz .Lxts_enc_steal 1685 1686 sub $len_,$out # rewind $out 1687 mov $key_,$key # restore $key 1688 mov $rnds_,$rounds # restore $rounds 1689 1690 movups -16($out),$inout0 1691 xorps @tweak[0],$inout0 1692 ___ 1693 &aesni_generate1("enc",$key,$rounds); 1694 $code.=<<___; 1695 xorps @tweak[0],$inout0 1696 movups $inout0,-16($out) 1697 1698 .Lxts_enc_ret: 1699 ___ 1700 $code.=<<___ if ($win64); 1701 movaps 0x60(%rsp),%xmm6 1702 movaps 0x70(%rsp),%xmm7 1703 movaps 0x80(%rsp),%xmm8 1704 movaps 0x90(%rsp),%xmm9 1705 movaps 0xa0(%rsp),%xmm10 1706 movaps 0xb0(%rsp),%xmm11 1707 movaps 0xc0(%rsp),%xmm12 1708 movaps 0xd0(%rsp),%xmm13 1709 movaps 0xe0(%rsp),%xmm14 1710 movaps 0xf0(%rsp),%xmm15 1711 ___ 1712 $code.=<<___; 1713 lea $frame_size(%rsp),%rsp 1714 .Lxts_enc_epilogue: 1715 ret 1716 .size aesni_xts_encrypt,.-aesni_xts_encrypt 1717 ___ 1718 1719 $code.=<<___; 1720 .globl aesni_xts_decrypt 1721 .type aesni_xts_decrypt,\@function,6 1722 .align 16 1723 aesni_xts_decrypt: 1724 lea -$frame_size(%rsp),%rsp 1725 ___ 1726 $code.=<<___ if ($win64); 1727 movaps %xmm6,0x60(%rsp) 1728 movaps %xmm7,0x70(%rsp) 1729 movaps %xmm8,0x80(%rsp) 1730 movaps %xmm9,0x90(%rsp) 1731 movaps %xmm10,0xa0(%rsp) 1732 movaps %xmm11,0xb0(%rsp) 1733 movaps %xmm12,0xc0(%rsp) 1734 movaps %xmm13,0xd0(%rsp) 1735 movaps %xmm14,0xe0(%rsp) 1736 movaps %xmm15,0xf0(%rsp) 1737 .Lxts_dec_body: 1738 ___ 1739 $code.=<<___; 1740 movups ($ivp),@tweak[5] # load clear-text tweak 1741 mov 240($key2),$rounds # key2->rounds 1742 mov 240($key),$rnds_ # key1->rounds 1743 ___ 1744 # generate the tweak 1745 &aesni_generate1("enc",$key2,$rounds,@tweak[5]); 1746 $code.=<<___; 1747 xor %eax,%eax # if ($len%16) len-=16; 1748 test \$15,$len 1749 setnz %al 1750 shl \$4,%rax 1751 sub %rax,$len 1752 1753 mov $key,$key_ # backup $key 1754 mov $rnds_,$rounds # backup $rounds 1755 mov $len,$len_ # backup $len 1756 and \$-16,$len 1757 1758 movdqa .Lxts_magic(%rip),$twmask 1759 pxor $twtmp,$twtmp 1760 pcmpgtd @tweak[5],$twtmp # broadcast upper bits 1761 ___ 1762 for ($i=0;$i<4;$i++) { 1763 $code.=<<___; 1764 pshufd \$0x13,$twtmp,$twres 1765 pxor $twtmp,$twtmp 1766 movdqa @tweak[5],@tweak[$i] 1767 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1768 pand $twmask,$twres # isolate carry and residue 1769 pcmpgtd @tweak[5],$twtmp # broadcat upper bits 1770 pxor $twres,@tweak[5] 1771 ___ 1772 } 1773 $code.=<<___; 1774 sub \$16*6,$len 1775 jc .Lxts_dec_short 1776 1777 shr \$1,$rounds 1778 sub \$1,$rounds 1779 mov $rounds,$rnds_ 1780 jmp .Lxts_dec_grandloop 1781 1782 .align 16 1783 .Lxts_dec_grandloop: 1784 pshufd \$0x13,$twtmp,$twres 1785 movdqa @tweak[5],@tweak[4] 1786 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1787 movdqu `16*0`($inp),$inout0 # load input 1788 pand $twmask,$twres # isolate carry and residue 1789 movdqu `16*1`($inp),$inout1 1790 pxor $twres,@tweak[5] 1791 1792 movdqu `16*2`($inp),$inout2 1793 pxor @tweak[0],$inout0 # input^=tweak 1794 movdqu `16*3`($inp),$inout3 1795 pxor @tweak[1],$inout1 1796 movdqu `16*4`($inp),$inout4 1797 pxor @tweak[2],$inout2 1798 movdqu `16*5`($inp),$inout5 1799 lea `16*6`($inp),$inp 1800 pxor @tweak[3],$inout3 1801 $movkey ($key_),$rndkey0 1802 pxor @tweak[4],$inout4 1803 pxor @tweak[5],$inout5 1804 1805 # inline _aesni_decrypt6 and interleave first and last rounds 1806 # with own code... 1807 $movkey 16($key_),$rndkey1 1808 pxor $rndkey0,$inout0 1809 pxor $rndkey0,$inout1 1810 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks 1811 aesdec $rndkey1,$inout0 1812 lea 32($key_),$key 1813 pxor $rndkey0,$inout2 1814 movdqa @tweak[1],`16*1`(%rsp) 1815 aesdec $rndkey1,$inout1 1816 pxor $rndkey0,$inout3 1817 movdqa @tweak[2],`16*2`(%rsp) 1818 aesdec $rndkey1,$inout2 1819 pxor $rndkey0,$inout4 1820 movdqa @tweak[3],`16*3`(%rsp) 1821 aesdec $rndkey1,$inout3 1822 pxor $rndkey0,$inout5 1823 $movkey ($key),$rndkey0 1824 dec $rounds 1825 movdqa @tweak[4],`16*4`(%rsp) 1826 aesdec $rndkey1,$inout4 1827 movdqa @tweak[5],`16*5`(%rsp) 1828 aesdec $rndkey1,$inout5 1829 pxor $twtmp,$twtmp 1830 pcmpgtd @tweak[5],$twtmp 1831 jmp .Lxts_dec_loop6_enter 1832 1833 .align 16 1834 .Lxts_dec_loop6: 1835 aesdec $rndkey1,$inout0 1836 aesdec $rndkey1,$inout1 1837 dec $rounds 1838 aesdec $rndkey1,$inout2 1839 aesdec $rndkey1,$inout3 1840 aesdec $rndkey1,$inout4 1841 aesdec $rndkey1,$inout5 1842 .Lxts_dec_loop6_enter: 1843 $movkey 16($key),$rndkey1 1844 aesdec $rndkey0,$inout0 1845 aesdec $rndkey0,$inout1 1846 lea 32($key),$key 1847 aesdec $rndkey0,$inout2 1848 aesdec $rndkey0,$inout3 1849 aesdec $rndkey0,$inout4 1850 aesdec $rndkey0,$inout5 1851 $movkey ($key),$rndkey0 1852 jnz .Lxts_dec_loop6 1853 1854 pshufd \$0x13,$twtmp,$twres 1855 pxor $twtmp,$twtmp 1856 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1857 aesdec $rndkey1,$inout0 1858 pand $twmask,$twres # isolate carry and residue 1859 aesdec $rndkey1,$inout1 1860 pcmpgtd @tweak[5],$twtmp # broadcast upper bits 1861 aesdec $rndkey1,$inout2 1862 pxor $twres,@tweak[5] 1863 aesdec $rndkey1,$inout3 1864 aesdec $rndkey1,$inout4 1865 aesdec $rndkey1,$inout5 1866 $movkey 16($key),$rndkey1 1867 1868 pshufd \$0x13,$twtmp,$twres 1869 pxor $twtmp,$twtmp 1870 movdqa @tweak[5],@tweak[0] 1871 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1872 aesdec $rndkey0,$inout0 1873 pand $twmask,$twres # isolate carry and residue 1874 aesdec $rndkey0,$inout1 1875 pcmpgtd @tweak[5],$twtmp # broadcat upper bits 1876 aesdec $rndkey0,$inout2 1877 pxor $twres,@tweak[5] 1878 aesdec $rndkey0,$inout3 1879 aesdec $rndkey0,$inout4 1880 aesdec $rndkey0,$inout5 1881 $movkey 32($key),$rndkey0 1882 1883 pshufd \$0x13,$twtmp,$twres 1884 pxor $twtmp,$twtmp 1885 movdqa @tweak[5],@tweak[1] 1886 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1887 aesdec $rndkey1,$inout0 1888 pand $twmask,$twres # isolate carry and residue 1889 aesdec $rndkey1,$inout1 1890 pcmpgtd @tweak[5],$twtmp # broadcat upper bits 1891 aesdec $rndkey1,$inout2 1892 pxor $twres,@tweak[5] 1893 aesdec $rndkey1,$inout3 1894 aesdec $rndkey1,$inout4 1895 aesdec $rndkey1,$inout5 1896 1897 pshufd \$0x13,$twtmp,$twres 1898 pxor $twtmp,$twtmp 1899 movdqa @tweak[5],@tweak[2] 1900 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1901 aesdeclast $rndkey0,$inout0 1902 pand $twmask,$twres # isolate carry and residue 1903 aesdeclast $rndkey0,$inout1 1904 pcmpgtd @tweak[5],$twtmp # broadcat upper bits 1905 aesdeclast $rndkey0,$inout2 1906 pxor $twres,@tweak[5] 1907 aesdeclast $rndkey0,$inout3 1908 aesdeclast $rndkey0,$inout4 1909 aesdeclast $rndkey0,$inout5 1910 1911 pshufd \$0x13,$twtmp,$twres 1912 pxor $twtmp,$twtmp 1913 movdqa @tweak[5],@tweak[3] 1914 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1915 xorps `16*0`(%rsp),$inout0 # output^=tweak 1916 pand $twmask,$twres # isolate carry and residue 1917 xorps `16*1`(%rsp),$inout1 1918 pcmpgtd @tweak[5],$twtmp # broadcat upper bits 1919 pxor $twres,@tweak[5] 1920 1921 xorps `16*2`(%rsp),$inout2 1922 movups $inout0,`16*0`($out) # write output 1923 xorps `16*3`(%rsp),$inout3 1924 movups $inout1,`16*1`($out) 1925 xorps `16*4`(%rsp),$inout4 1926 movups $inout2,`16*2`($out) 1927 xorps `16*5`(%rsp),$inout5 1928 movups $inout3,`16*3`($out) 1929 mov $rnds_,$rounds # restore $rounds 1930 movups $inout4,`16*4`($out) 1931 movups $inout5,`16*5`($out) 1932 lea `16*6`($out),$out 1933 sub \$16*6,$len 1934 jnc .Lxts_dec_grandloop 1935 1936 lea 3($rounds,$rounds),$rounds # restore original value 1937 mov $key_,$key # restore $key 1938 mov $rounds,$rnds_ # backup $rounds 1939 1940 .Lxts_dec_short: 1941 add \$16*6,$len 1942 jz .Lxts_dec_done 1943 1944 cmp \$0x20,$len 1945 jb .Lxts_dec_one 1946 je .Lxts_dec_two 1947 1948 cmp \$0x40,$len 1949 jb .Lxts_dec_three 1950 je .Lxts_dec_four 1951 1952 pshufd \$0x13,$twtmp,$twres 1953 movdqa @tweak[5],@tweak[4] 1954 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1955 movdqu ($inp),$inout0 1956 pand $twmask,$twres # isolate carry and residue 1957 movdqu 16*1($inp),$inout1 1958 pxor $twres,@tweak[5] 1959 1960 movdqu 16*2($inp),$inout2 1961 pxor @tweak[0],$inout0 1962 movdqu 16*3($inp),$inout3 1963 pxor @tweak[1],$inout1 1964 movdqu 16*4($inp),$inout4 1965 lea 16*5($inp),$inp 1966 pxor @tweak[2],$inout2 1967 pxor @tweak[3],$inout3 1968 pxor @tweak[4],$inout4 1969 1970 call _aesni_decrypt6 1971 1972 xorps @tweak[0],$inout0 1973 xorps @tweak[1],$inout1 1974 xorps @tweak[2],$inout2 1975 movdqu $inout0,($out) 1976 xorps @tweak[3],$inout3 1977 movdqu $inout1,16*1($out) 1978 xorps @tweak[4],$inout4 1979 movdqu $inout2,16*2($out) 1980 pxor $twtmp,$twtmp 1981 movdqu $inout3,16*3($out) 1982 pcmpgtd @tweak[5],$twtmp 1983 movdqu $inout4,16*4($out) 1984 lea 16*5($out),$out 1985 pshufd \$0x13,$twtmp,@tweak[1] # $twres 1986 and \$15,$len_ 1987 jz .Lxts_dec_ret 1988 1989 movdqa @tweak[5],@tweak[0] 1990 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 1991 pand $twmask,@tweak[1] # isolate carry and residue 1992 pxor @tweak[5],@tweak[1] 1993 jmp .Lxts_dec_done2 1994 1995 .align 16 1996 .Lxts_dec_one: 1997 movups ($inp),$inout0 1998 lea 16*1($inp),$inp 1999 xorps @tweak[0],$inout0 2000 ___ 2001 &aesni_generate1("dec",$key,$rounds); 2002 $code.=<<___; 2003 xorps @tweak[0],$inout0 2004 movdqa @tweak[1],@tweak[0] 2005 movups $inout0,($out) 2006 movdqa @tweak[2],@tweak[1] 2007 lea 16*1($out),$out 2008 jmp .Lxts_dec_done 2009 2010 .align 16 2011 .Lxts_dec_two: 2012 movups ($inp),$inout0 2013 movups 16($inp),$inout1 2014 lea 32($inp),$inp 2015 xorps @tweak[0],$inout0 2016 xorps @tweak[1],$inout1 2017 2018 call _aesni_decrypt3 2019 2020 xorps @tweak[0],$inout0 2021 movdqa @tweak[2],@tweak[0] 2022 xorps @tweak[1],$inout1 2023 movdqa @tweak[3],@tweak[1] 2024 movups $inout0,($out) 2025 movups $inout1,16*1($out) 2026 lea 16*2($out),$out 2027 jmp .Lxts_dec_done 2028 2029 .align 16 2030 .Lxts_dec_three: 2031 movups ($inp),$inout0 2032 movups 16*1($inp),$inout1 2033 movups 16*2($inp),$inout2 2034 lea 16*3($inp),$inp 2035 xorps @tweak[0],$inout0 2036 xorps @tweak[1],$inout1 2037 xorps @tweak[2],$inout2 2038 2039 call _aesni_decrypt3 2040 2041 xorps @tweak[0],$inout0 2042 movdqa @tweak[3],@tweak[0] 2043 xorps @tweak[1],$inout1 2044 movdqa @tweak[5],@tweak[1] 2045 xorps @tweak[2],$inout2 2046 movups $inout0,($out) 2047 movups $inout1,16*1($out) 2048 movups $inout2,16*2($out) 2049 lea 16*3($out),$out 2050 jmp .Lxts_dec_done 2051 2052 .align 16 2053 .Lxts_dec_four: 2054 pshufd \$0x13,$twtmp,$twres 2055 movdqa @tweak[5],@tweak[4] 2056 paddq @tweak[5],@tweak[5] # psllq 1,$tweak 2057 movups ($inp),$inout0 2058 pand $twmask,$twres # isolate carry and residue 2059 movups 16*1($inp),$inout1 2060 pxor $twres,@tweak[5] 2061 2062 movups 16*2($inp),$inout2 2063 xorps @tweak[0],$inout0 2064 movups 16*3($inp),$inout3 2065 lea 16*4($inp),$inp 2066 xorps @tweak[1],$inout1 2067 xorps @tweak[2],$inout2 2068 xorps @tweak[3],$inout3 2069 2070 call _aesni_decrypt4 2071 2072 xorps @tweak[0],$inout0 2073 movdqa @tweak[4],@tweak[0] 2074 xorps @tweak[1],$inout1 2075 movdqa @tweak[5],@tweak[1] 2076 xorps @tweak[2],$inout2 2077 movups $inout0,($out) 2078 xorps @tweak[3],$inout3 2079 movups $inout1,16*1($out) 2080 movups $inout2,16*2($out) 2081 movups $inout3,16*3($out) 2082 lea 16*4($out),$out 2083 jmp .Lxts_dec_done 2084 2085 .align 16 2086 .Lxts_dec_done: 2087 and \$15,$len_ 2088 jz .Lxts_dec_ret 2089 .Lxts_dec_done2: 2090 mov $len_,$len 2091 mov $key_,$key # restore $key 2092 mov $rnds_,$rounds # restore $rounds 2093 2094 movups ($inp),$inout0 2095 xorps @tweak[1],$inout0 2096 ___ 2097 &aesni_generate1("dec",$key,$rounds); 2098 $code.=<<___; 2099 xorps @tweak[1],$inout0 2100 movups $inout0,($out) 2101 2102 .Lxts_dec_steal: 2103 movzb 16($inp),%eax # borrow $rounds ... 2104 movzb ($out),%ecx # ... and $key 2105 lea 1($inp),$inp 2106 mov %al,($out) 2107 mov %cl,16($out) 2108 lea 1($out),$out 2109 sub \$1,$len 2110 jnz .Lxts_dec_steal 2111 2112 sub $len_,$out # rewind $out 2113 mov $key_,$key # restore $key 2114 mov $rnds_,$rounds # restore $rounds 2115 2116 movups ($out),$inout0 2117 xorps @tweak[0],$inout0 2118 ___ 2119 &aesni_generate1("dec",$key,$rounds); 2120 $code.=<<___; 2121 xorps @tweak[0],$inout0 2122 movups $inout0,($out) 2123 2124 .Lxts_dec_ret: 2125 ___ 2126 $code.=<<___ if ($win64); 2127 movaps 0x60(%rsp),%xmm6 2128 movaps 0x70(%rsp),%xmm7 2129 movaps 0x80(%rsp),%xmm8 2130 movaps 0x90(%rsp),%xmm9 2131 movaps 0xa0(%rsp),%xmm10 2132 movaps 0xb0(%rsp),%xmm11 2133 movaps 0xc0(%rsp),%xmm12 2134 movaps 0xd0(%rsp),%xmm13 2135 movaps 0xe0(%rsp),%xmm14 2136 movaps 0xf0(%rsp),%xmm15 2137 ___ 2138 $code.=<<___; 2139 lea $frame_size(%rsp),%rsp 2140 .Lxts_dec_epilogue: 2141 ret 2142 .size aesni_xts_decrypt,.-aesni_xts_decrypt 2143 ___ 2144 } }} 2145 2147 ######################################################################## 2148 # void $PREFIX_cbc_encrypt (const void *inp, void *out, 2149 # size_t length, const AES_KEY *key, 2150 # unsigned char *ivp,const int enc); 2151 { 2152 my $reserved = $win64?0x40:-0x18; # used in decrypt 2153 $code.=<<___; 2154 .globl ${PREFIX}_cbc_encrypt 2155 .type ${PREFIX}_cbc_encrypt,\@function,6 2156 .align 16 2157 ${PREFIX}_cbc_encrypt: 2158 test $len,$len # check length 2159 jz .Lcbc_ret 2160 2161 mov 240($key),$rnds_ # key->rounds 2162 mov $key,$key_ # backup $key 2163 test %r9d,%r9d # 6th argument 2164 jz .Lcbc_decrypt 2165 #--------------------------- CBC ENCRYPT ------------------------------# 2166 movups ($ivp),$inout0 # load iv as initial state 2167 mov $rnds_,$rounds 2168 cmp \$16,$len 2169 jb .Lcbc_enc_tail 2170 sub \$16,$len 2171 jmp .Lcbc_enc_loop 2172 .align 16 2173 .Lcbc_enc_loop: 2174 movups ($inp),$inout1 # load input 2175 lea 16($inp),$inp 2176 #xorps $inout1,$inout0 2177 ___ 2178 &aesni_generate1("enc",$key,$rounds,$inout0,$inout1); 2179 $code.=<<___; 2180 mov $rnds_,$rounds # restore $rounds 2181 mov $key_,$key # restore $key 2182 movups $inout0,0($out) # store output 2183 lea 16($out),$out 2184 sub \$16,$len 2185 jnc .Lcbc_enc_loop 2186 add \$16,$len 2187 jnz .Lcbc_enc_tail 2188 movups $inout0,($ivp) 2189 jmp .Lcbc_ret 2190 2191 .Lcbc_enc_tail: 2192 mov $len,%rcx # zaps $key 2193 xchg $inp,$out # $inp is %rsi and $out is %rdi now 2194 .long 0x9066A4F3 # rep movsb 2195 mov \$16,%ecx # zero tail 2196 sub $len,%rcx 2197 xor %eax,%eax 2198 .long 0x9066AAF3 # rep stosb 2199 lea -16(%rdi),%rdi # rewind $out by 1 block 2200 mov $rnds_,$rounds # restore $rounds 2201 mov %rdi,%rsi # $inp and $out are the same 2202 mov $key_,$key # restore $key 2203 xor $len,$len # len=16 2204 jmp .Lcbc_enc_loop # one more spin 2205 #--------------------------- CBC DECRYPT ------------------------------# 2207 .align 16 2208 .Lcbc_decrypt: 2209 ___ 2210 $code.=<<___ if ($win64); 2211 lea -0x58(%rsp),%rsp 2212 movaps %xmm6,(%rsp) 2213 movaps %xmm7,0x10(%rsp) 2214 movaps %xmm8,0x20(%rsp) 2215 movaps %xmm9,0x30(%rsp) 2216 .Lcbc_decrypt_body: 2217 ___ 2218 $code.=<<___; 2219 movups ($ivp),$iv 2220 mov $rnds_,$rounds 2221 cmp \$0x70,$len 2222 jbe .Lcbc_dec_tail 2223 shr \$1,$rnds_ 2224 sub \$0x70,$len 2225 mov $rnds_,$rounds 2226 movaps $iv,$reserved(%rsp) 2227 jmp .Lcbc_dec_loop8_enter 2228 .align 16 2229 .Lcbc_dec_loop8: 2230 movaps $rndkey0,$reserved(%rsp) # save IV 2231 movups $inout7,($out) 2232 lea 0x10($out),$out 2233 .Lcbc_dec_loop8_enter: 2234 $movkey ($key),$rndkey0 2235 movups ($inp),$inout0 # load input 2236 movups 0x10($inp),$inout1 2237 $movkey 16($key),$rndkey1 2238 2239 lea 32($key),$key 2240 movdqu 0x20($inp),$inout2 2241 xorps $rndkey0,$inout0 2242 movdqu 0x30($inp),$inout3 2243 xorps $rndkey0,$inout1 2244 movdqu 0x40($inp),$inout4 2245 aesdec $rndkey1,$inout0 2246 pxor $rndkey0,$inout2 2247 movdqu 0x50($inp),$inout5 2248 aesdec $rndkey1,$inout1 2249 pxor $rndkey0,$inout3 2250 movdqu 0x60($inp),$inout6 2251 aesdec $rndkey1,$inout2 2252 pxor $rndkey0,$inout4 2253 movdqu 0x70($inp),$inout7 2254 aesdec $rndkey1,$inout3 2255 pxor $rndkey0,$inout5 2256 dec $rounds 2257 aesdec $rndkey1,$inout4 2258 pxor $rndkey0,$inout6 2259 aesdec $rndkey1,$inout5 2260 pxor $rndkey0,$inout7 2261 $movkey ($key),$rndkey0 2262 aesdec $rndkey1,$inout6 2263 aesdec $rndkey1,$inout7 2264 $movkey 16($key),$rndkey1 2265 2266 call .Ldec_loop8_enter 2267 2268 movups ($inp),$rndkey1 # re-load input 2269 movups 0x10($inp),$rndkey0 2270 xorps $reserved(%rsp),$inout0 # ^= IV 2271 xorps $rndkey1,$inout1 2272 movups 0x20($inp),$rndkey1 2273 xorps $rndkey0,$inout2 2274 movups 0x30($inp),$rndkey0 2275 xorps $rndkey1,$inout3 2276 movups 0x40($inp),$rndkey1 2277 xorps $rndkey0,$inout4 2278 movups 0x50($inp),$rndkey0 2279 xorps $rndkey1,$inout5 2280 movups 0x60($inp),$rndkey1 2281 xorps $rndkey0,$inout6 2282 movups 0x70($inp),$rndkey0 # IV 2283 xorps $rndkey1,$inout7 2284 movups $inout0,($out) 2285 movups $inout1,0x10($out) 2286 movups $inout2,0x20($out) 2287 movups $inout3,0x30($out) 2288 mov $rnds_,$rounds # restore $rounds 2289 movups $inout4,0x40($out) 2290 mov $key_,$key # restore $key 2291 movups $inout5,0x50($out) 2292 lea 0x80($inp),$inp 2293 movups $inout6,0x60($out) 2294 lea 0x70($out),$out 2295 sub \$0x80,$len 2296 ja .Lcbc_dec_loop8 2297 2298 movaps $inout7,$inout0 2299 movaps $rndkey0,$iv 2300 add \$0x70,$len 2301 jle .Lcbc_dec_tail_collected 2302 movups $inout0,($out) 2303 lea 1($rnds_,$rnds_),$rounds 2304 lea 0x10($out),$out 2305 .Lcbc_dec_tail: 2306 movups ($inp),$inout0 2307 movaps $inout0,$in0 2308 cmp \$0x10,$len 2309 jbe .Lcbc_dec_one 2310 2311 movups 0x10($inp),$inout1 2312 movaps $inout1,$in1 2313 cmp \$0x20,$len 2314 jbe .Lcbc_dec_two 2315 2316 movups 0x20($inp),$inout2 2317 movaps $inout2,$in2 2318 cmp \$0x30,$len 2319 jbe .Lcbc_dec_three 2320 2321 movups 0x30($inp),$inout3 2322 cmp \$0x40,$len 2323 jbe .Lcbc_dec_four 2324 2325 movups 0x40($inp),$inout4 2326 cmp \$0x50,$len 2327 jbe .Lcbc_dec_five 2328 2329 movups 0x50($inp),$inout5 2330 cmp \$0x60,$len 2331 jbe .Lcbc_dec_six 2332 2333 movups 0x60($inp),$inout6 2334 movaps $iv,$reserved(%rsp) # save IV 2335 call _aesni_decrypt8 2336 movups ($inp),$rndkey1 2337 movups 0x10($inp),$rndkey0 2338 xorps $reserved(%rsp),$inout0 # ^= IV 2339 xorps $rndkey1,$inout1 2340 movups 0x20($inp),$rndkey1 2341 xorps $rndkey0,$inout2 2342 movups 0x30($inp),$rndkey0 2343 xorps $rndkey1,$inout3 2344 movups 0x40($inp),$rndkey1 2345 xorps $rndkey0,$inout4 2346 movups 0x50($inp),$rndkey0 2347 xorps $rndkey1,$inout5 2348 movups 0x60($inp),$iv # IV 2349 xorps $rndkey0,$inout6 2350 movups $inout0,($out) 2351 movups $inout1,0x10($out) 2352 movups $inout2,0x20($out) 2353 movups $inout3,0x30($out) 2354 movups $inout4,0x40($out) 2355 movups $inout5,0x50($out) 2356 lea 0x60($out),$out 2357 movaps $inout6,$inout0 2358 sub \$0x70,$len 2359 jmp .Lcbc_dec_tail_collected 2360 .align 16 2361 .Lcbc_dec_one: 2362 ___ 2363 &aesni_generate1("dec",$key,$rounds); 2364 $code.=<<___; 2365 xorps $iv,$inout0 2366 movaps $in0,$iv 2367 sub \$0x10,$len 2368 jmp .Lcbc_dec_tail_collected 2369 .align 16 2370 .Lcbc_dec_two: 2371 xorps $inout2,$inout2 2372 call _aesni_decrypt3 2373 xorps $iv,$inout0 2374 xorps $in0,$inout1 2375 movups $inout0,($out) 2376 movaps $in1,$iv 2377 movaps $inout1,$inout0 2378 lea 0x10($out),$out 2379 sub \$0x20,$len 2380 jmp .Lcbc_dec_tail_collected 2381 .align 16 2382 .Lcbc_dec_three: 2383 call _aesni_decrypt3 2384 xorps $iv,$inout0 2385 xorps $in0,$inout1 2386 movups $inout0,($out) 2387 xorps $in1,$inout2 2388 movups $inout1,0x10($out) 2389 movaps $in2,$iv 2390 movaps $inout2,$inout0 2391 lea 0x20($out),$out 2392 sub \$0x30,$len 2393 jmp .Lcbc_dec_tail_collected 2394 .align 16 2395 .Lcbc_dec_four: 2396 call _aesni_decrypt4 2397 xorps $iv,$inout0 2398 movups 0x30($inp),$iv 2399 xorps $in0,$inout1 2400 movups $inout0,($out) 2401 xorps $in1,$inout2 2402 movups $inout1,0x10($out) 2403 xorps $in2,$inout3 2404 movups $inout2,0x20($out) 2405 movaps $inout3,$inout0 2406 lea 0x30($out),$out 2407 sub \$0x40,$len 2408 jmp .Lcbc_dec_tail_collected 2409 .align 16 2410 .Lcbc_dec_five: 2411 xorps $inout5,$inout5 2412 call _aesni_decrypt6 2413 movups 0x10($inp),$rndkey1 2414 movups 0x20($inp),$rndkey0 2415 xorps $iv,$inout0 2416 xorps $in0,$inout1 2417 xorps $rndkey1,$inout2 2418 movups 0x30($inp),$rndkey1 2419 xorps $rndkey0,$inout3 2420 movups 0x40($inp),$iv 2421 xorps $rndkey1,$inout4 2422 movups $inout0,($out) 2423 movups $inout1,0x10($out) 2424 movups $inout2,0x20($out) 2425 movups $inout3,0x30($out) 2426 lea 0x40($out),$out 2427 movaps $inout4,$inout0 2428 sub \$0x50,$len 2429 jmp .Lcbc_dec_tail_collected 2430 .align 16 2431 .Lcbc_dec_six: 2432 call _aesni_decrypt6 2433 movups 0x10($inp),$rndkey1 2434 movups 0x20($inp),$rndkey0 2435 xorps $iv,$inout0 2436 xorps $in0,$inout1 2437 xorps $rndkey1,$inout2 2438 movups 0x30($inp),$rndkey1 2439 xorps $rndkey0,$inout3 2440 movups 0x40($inp),$rndkey0 2441 xorps $rndkey1,$inout4 2442 movups 0x50($inp),$iv 2443 xorps $rndkey0,$inout5 2444 movups $inout0,($out) 2445 movups $inout1,0x10($out) 2446 movups $inout2,0x20($out) 2447 movups $inout3,0x30($out) 2448 movups $inout4,0x40($out) 2449 lea 0x50($out),$out 2450 movaps $inout5,$inout0 2451 sub \$0x60,$len 2452 jmp .Lcbc_dec_tail_collected 2453 .align 16 2454 .Lcbc_dec_tail_collected: 2455 and \$15,$len 2456 movups $iv,($ivp) 2457 jnz .Lcbc_dec_tail_partial 2458 movups $inout0,($out) 2459 jmp .Lcbc_dec_ret 2460 .align 16 2461 .Lcbc_dec_tail_partial: 2462 movaps $inout0,$reserved(%rsp) 2463 mov \$16,%rcx 2464 mov $out,%rdi 2465 sub $len,%rcx 2466 lea $reserved(%rsp),%rsi 2467 .long 0x9066A4F3 # rep movsb 2468 2469 .Lcbc_dec_ret: 2470 ___ 2471 $code.=<<___ if ($win64); 2472 movaps (%rsp),%xmm6 2473 movaps 0x10(%rsp),%xmm7 2474 movaps 0x20(%rsp),%xmm8 2475 movaps 0x30(%rsp),%xmm9 2476 lea 0x58(%rsp),%rsp 2477 ___ 2478 $code.=<<___; 2479 .Lcbc_ret: 2480 ret 2481 .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt 2482 ___ 2483 } 2485 # int $PREFIX_set_[en|de]crypt_key (const unsigned char *userKey, 2486 # int bits, AES_KEY *key) 2487 { my ($inp,$bits,$key) = @_4args; 2488 $bits =~ s/%r/%e/; 2489 2490 $code.=<<___; 2491 .globl ${PREFIX}_set_decrypt_key 2492 .type ${PREFIX}_set_decrypt_key,\@abi-omnipotent 2493 .align 16 2494 ${PREFIX}_set_decrypt_key: 2495 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8 2496 call __aesni_set_encrypt_key 2497 shl \$4,$bits # rounds-1 after _aesni_set_encrypt_key 2498 test %eax,%eax 2499 jnz .Ldec_key_ret 2500 lea 16($key,$bits),$inp # points at the end of key schedule 2501 2502 $movkey ($key),%xmm0 # just swap 2503 $movkey ($inp),%xmm1 2504 $movkey %xmm0,($inp) 2505 $movkey %xmm1,($key) 2506 lea 16($key),$key 2507 lea -16($inp),$inp 2508 2509 .Ldec_key_inverse: 2510 $movkey ($key),%xmm0 # swap and inverse 2511 $movkey ($inp),%xmm1 2512 aesimc %xmm0,%xmm0 2513 aesimc %xmm1,%xmm1 2514 lea 16($key),$key 2515 lea -16($inp),$inp 2516 $movkey %xmm0,16($inp) 2517 $movkey %xmm1,-16($key) 2518 cmp $key,$inp 2519 ja .Ldec_key_inverse 2520 2521 $movkey ($key),%xmm0 # inverse middle 2522 aesimc %xmm0,%xmm0 2523 $movkey %xmm0,($inp) 2524 .Ldec_key_ret: 2525 add \$8,%rsp 2526 ret 2527 .LSEH_end_set_decrypt_key: 2528 .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key 2529 ___ 2530 2532 # This is based on submission by 2533 # 2534 # Huang Ying <ying.huang (at] intel.com> 2535 # Vinodh Gopal <vinodh.gopal (at] intel.com> 2536 # Kahraman Akdemir 2537 # 2538 # Agressively optimized in respect to aeskeygenassist's critical path 2539 # and is contained in %xmm0-5 to meet Win64 ABI requirement. 2540 # 2541 $code.=<<___; 2542 .globl ${PREFIX}_set_encrypt_key 2543 .type ${PREFIX}_set_encrypt_key,\@abi-omnipotent 2544 .align 16 2545 ${PREFIX}_set_encrypt_key: 2546 __aesni_set_encrypt_key: 2547 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8 2548 mov \$-1,%rax 2549 test $inp,$inp 2550 jz .Lenc_key_ret 2551 test $key,$key 2552 jz .Lenc_key_ret 2553 2554 movups ($inp),%xmm0 # pull first 128 bits of *userKey 2555 xorps %xmm4,%xmm4 # low dword of xmm4 is assumed 0 2556 lea 16($key),%rax 2557 cmp \$256,$bits 2558 je .L14rounds 2559 cmp \$192,$bits 2560 je .L12rounds 2561 cmp \$128,$bits 2562 jne .Lbad_keybits 2563 2564 .L10rounds: 2565 mov \$9,$bits # 10 rounds for 128-bit key 2566 $movkey %xmm0,($key) # round 0 2567 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 1 2568 call .Lkey_expansion_128_cold 2569 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 2 2570 call .Lkey_expansion_128 2571 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 3 2572 call .Lkey_expansion_128 2573 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 4 2574 call .Lkey_expansion_128 2575 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 5 2576 call .Lkey_expansion_128 2577 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 6 2578 call .Lkey_expansion_128 2579 aeskeygenassist \$0x40,%xmm0,%xmm1 # round 7 2580 call .Lkey_expansion_128 2581 aeskeygenassist \$0x80,%xmm0,%xmm1 # round 8 2582 call .Lkey_expansion_128 2583 aeskeygenassist \$0x1b,%xmm0,%xmm1 # round 9 2584 call .Lkey_expansion_128 2585 aeskeygenassist \$0x36,%xmm0,%xmm1 # round 10 2586 call .Lkey_expansion_128 2587 $movkey %xmm0,(%rax) 2588 mov $bits,80(%rax) # 240(%rdx) 2589 xor %eax,%eax 2590 jmp .Lenc_key_ret 2591 2592 .align 16 2593 .L12rounds: 2594 movq 16($inp),%xmm2 # remaining 1/3 of *userKey 2595 mov \$11,$bits # 12 rounds for 192 2596 $movkey %xmm0,($key) # round 0 2597 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 1,2 2598 call .Lkey_expansion_192a_cold 2599 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 2,3 2600 call .Lkey_expansion_192b 2601 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 4,5 2602 call .Lkey_expansion_192a 2603 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 5,6 2604 call .Lkey_expansion_192b 2605 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 7,8 2606 call .Lkey_expansion_192a 2607 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 8,9 2608 call .Lkey_expansion_192b 2609 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 10,11 2610 call .Lkey_expansion_192a 2611 aeskeygenassist \$0x80,%xmm2,%xmm1 # round 11,12 2612 call .Lkey_expansion_192b 2613 $movkey %xmm0,(%rax) 2614 mov $bits,48(%rax) # 240(%rdx) 2615 xor %rax, %rax 2616 jmp .Lenc_key_ret 2617 2618 .align 16 2619 .L14rounds: 2620 movups 16($inp),%xmm2 # remaning half of *userKey 2621 mov \$13,$bits # 14 rounds for 256 2622 lea 16(%rax),%rax 2623 $movkey %xmm0,($key) # round 0 2624 $movkey %xmm2,16($key) # round 1 2625 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 2 2626 call .Lkey_expansion_256a_cold 2627 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 3 2628 call .Lkey_expansion_256b 2629 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 4 2630 call .Lkey_expansion_256a 2631 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 5 2632 call .Lkey_expansion_256b 2633 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 6 2634 call .Lkey_expansion_256a 2635 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 7 2636 call .Lkey_expansion_256b 2637 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 8 2638 call .Lkey_expansion_256a 2639 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 9 2640 call .Lkey_expansion_256b 2641 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 10 2642 call .Lkey_expansion_256a 2643 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 11 2644 call .Lkey_expansion_256b 2645 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 12 2646 call .Lkey_expansion_256a 2647 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 13 2648 call .Lkey_expansion_256b 2649 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 14 2650 call .Lkey_expansion_256a 2651 $movkey %xmm0,(%rax) 2652 mov $bits,16(%rax) # 240(%rdx) 2653 xor %rax,%rax 2654 jmp .Lenc_key_ret 2655 2656 .align 16 2657 .Lbad_keybits: 2658 mov \$-2,%rax 2659 .Lenc_key_ret: 2660 add \$8,%rsp 2661 ret 2662 .LSEH_end_set_encrypt_key: 2663 2665 .align 16 2666 .Lkey_expansion_128: 2667 $movkey %xmm0,(%rax) 2668 lea 16(%rax),%rax 2669 .Lkey_expansion_128_cold: 2670 shufps \$0b00010000,%xmm0,%xmm4 2671 xorps %xmm4, %xmm0 2672 shufps \$0b10001100,%xmm0,%xmm4 2673 xorps %xmm4, %xmm0 2674 shufps \$0b11111111,%xmm1,%xmm1 # critical path 2675 xorps %xmm1,%xmm0 2676 ret 2677 2678 .align 16 2679 .Lkey_expansion_192a: 2680 $movkey %xmm0,(%rax) 2681 lea 16(%rax),%rax 2682 .Lkey_expansion_192a_cold: 2683 movaps %xmm2, %xmm5 2684 .Lkey_expansion_192b_warm: 2685 shufps \$0b00010000,%xmm0,%xmm4 2686 movdqa %xmm2,%xmm3 2687 xorps %xmm4,%xmm0 2688 shufps \$0b10001100,%xmm0,%xmm4 2689 pslldq \$4,%xmm3 2690 xorps %xmm4,%xmm0 2691 pshufd \$0b01010101,%xmm1,%xmm1 # critical path 2692 pxor %xmm3,%xmm2 2693 pxor %xmm1,%xmm0 2694 pshufd \$0b11111111,%xmm0,%xmm3 2695 pxor %xmm3,%xmm2 2696 ret 2697 2698 .align 16 2699 .Lkey_expansion_192b: 2700 movaps %xmm0,%xmm3 2701 shufps \$0b01000100,%xmm0,%xmm5 2702 $movkey %xmm5,(%rax) 2703 shufps \$0b01001110,%xmm2,%xmm3 2704 $movkey %xmm3,16(%rax) 2705 lea 32(%rax),%rax 2706 jmp .Lkey_expansion_192b_warm 2707 2708 .align 16 2709 .Lkey_expansion_256a: 2710 $movkey %xmm2,(%rax) 2711 lea 16(%rax),%rax 2712 .Lkey_expansion_256a_cold: 2713 shufps \$0b00010000,%xmm0,%xmm4 2714 xorps %xmm4,%xmm0 2715 shufps \$0b10001100,%xmm0,%xmm4 2716 xorps %xmm4,%xmm0 2717 shufps \$0b11111111,%xmm1,%xmm1 # critical path 2718 xorps %xmm1,%xmm0 2719 ret 2720 2721 .align 16 2722 .Lkey_expansion_256b: 2723 $movkey %xmm0,(%rax) 2724 lea 16(%rax),%rax 2725 2726 shufps \$0b00010000,%xmm2,%xmm4 2727 xorps %xmm4,%xmm2 2728 shufps \$0b10001100,%xmm2,%xmm4 2729 xorps %xmm4,%xmm2 2730 shufps \$0b10101010,%xmm1,%xmm1 # critical path 2731 xorps %xmm1,%xmm2 2732 ret 2733 .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key 2734 .size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key 2735 ___ 2736 } 2737 2739 $code.=<<___; 2740 .align 64 2741 .Lbswap_mask: 2742 .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 2743 .Lincrement32: 2744 .long 6,6,6,0 2745 .Lincrement64: 2746 .long 1,0,0,0 2747 .Lxts_magic: 2748 .long 0x87,0,1,0 2749 2750 .asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>" 2751 .align 64 2752 ___ 2753 2754 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame, 2755 # CONTEXT *context,DISPATCHER_CONTEXT *disp) 2756 if ($win64) { 2757 $rec="%rcx"; 2758 $frame="%rdx"; 2759 $context="%r8"; 2760 $disp="%r9"; 2761 2762 $code.=<<___; 2763 .extern __imp_RtlVirtualUnwind 2764 ___ 2765 $code.=<<___ if ($PREFIX eq "aesni"); 2766 .type ecb_ccm64_se_handler,\@abi-omnipotent 2767 .align 16 2768 ecb_ccm64_se_handler: 2769 push %rsi 2770 push %rdi 2771 push %rbx 2772 push %rbp 2773 push %r12 2774 push %r13 2775 push %r14 2776 push %r15 2777 pushfq 2778 sub \$64,%rsp 2779 2780 mov 120($context),%rax # pull context->Rax 2781 mov 248($context),%rbx # pull context->Rip 2782 2783 mov 8($disp),%rsi # disp->ImageBase 2784 mov 56($disp),%r11 # disp->HandlerData 2785 2786 mov 0(%r11),%r10d # HandlerData[0] 2787 lea (%rsi,%r10),%r10 # prologue label 2788 cmp %r10,%rbx # context->Rip<prologue label 2789 jb .Lcommon_seh_tail 2790 2791 mov 152($context),%rax # pull context->Rsp 2792 2793 mov 4(%r11),%r10d # HandlerData[1] 2794 lea (%rsi,%r10),%r10 # epilogue label 2795 cmp %r10,%rbx # context->Rip>=epilogue label 2796 jae .Lcommon_seh_tail 2797 2798 lea 0(%rax),%rsi # %xmm save area 2799 lea 512($context),%rdi # &context.Xmm6 2800 mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax) 2801 .long 0xa548f3fc # cld; rep movsq 2802 lea 0x58(%rax),%rax # adjust stack pointer 2803 2804 jmp .Lcommon_seh_tail 2805 .size ecb_ccm64_se_handler,.-ecb_ccm64_se_handler 2806 2807 .type ctr32_se_handler,\@abi-omnipotent 2808 .align 16 2809 ctr32_se_handler: 2810 push %rsi 2811 push %rdi 2812 push %rbx 2813 push %rbp 2814 push %r12 2815 push %r13 2816 push %r14 2817 push %r15 2818 pushfq 2819 sub \$64,%rsp 2820 2821 mov 120($context),%rax # pull context->Rax 2822 mov 248($context),%rbx # pull context->Rip 2823 2824 lea .Lctr32_body(%rip),%r10 2825 cmp %r10,%rbx # context->Rip<"prologue" label 2826 jb .Lcommon_seh_tail 2827 2828 mov 152($context),%rax # pull context->Rsp 2829 2830 lea .Lctr32_ret(%rip),%r10 2831 cmp %r10,%rbx 2832 jae .Lcommon_seh_tail 2833 2834 lea 0x20(%rax),%rsi # %xmm save area 2835 lea 512($context),%rdi # &context.Xmm6 2836 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax) 2837 .long 0xa548f3fc # cld; rep movsq 2838 lea 0xc8(%rax),%rax # adjust stack pointer 2839 2840 jmp .Lcommon_seh_tail 2841 .size ctr32_se_handler,.-ctr32_se_handler 2842 2843 .type xts_se_handler,\@abi-omnipotent 2844 .align 16 2845 xts_se_handler: 2846 push %rsi 2847 push %rdi 2848 push %rbx 2849 push %rbp 2850 push %r12 2851 push %r13 2852 push %r14 2853 push %r15 2854 pushfq 2855 sub \$64,%rsp 2856 2857 mov 120($context),%rax # pull context->Rax 2858 mov 248($context),%rbx # pull context->Rip 2859 2860 mov 8($disp),%rsi # disp->ImageBase 2861 mov 56($disp),%r11 # disp->HandlerData 2862 2863 mov 0(%r11),%r10d # HandlerData[0] 2864 lea (%rsi,%r10),%r10 # prologue lable 2865 cmp %r10,%rbx # context->Rip<prologue label 2866 jb .Lcommon_seh_tail 2867 2868 mov 152($context),%rax # pull context->Rsp 2869 2870 mov 4(%r11),%r10d # HandlerData[1] 2871 lea (%rsi,%r10),%r10 # epilogue label 2872 cmp %r10,%rbx # context->Rip>=epilogue label 2873 jae .Lcommon_seh_tail 2874 2875 lea 0x60(%rax),%rsi # %xmm save area 2876 lea 512($context),%rdi # & context.Xmm6 2877 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax) 2878 .long 0xa548f3fc # cld; rep movsq 2879 lea 0x68+160(%rax),%rax # adjust stack pointer 2880 2881 jmp .Lcommon_seh_tail 2882 .size xts_se_handler,.-xts_se_handler 2883 ___ 2884 $code.=<<___; 2885 .type cbc_se_handler,\@abi-omnipotent 2886 .align 16 2887 cbc_se_handler: 2888 push %rsi 2889 push %rdi 2890 push %rbx 2891 push %rbp 2892 push %r12 2893 push %r13 2894 push %r14 2895 push %r15 2896 pushfq 2897 sub \$64,%rsp 2898 2899 mov 152($context),%rax # pull context->Rsp 2900 mov 248($context),%rbx # pull context->Rip 2901 2902 lea .Lcbc_decrypt(%rip),%r10 2903 cmp %r10,%rbx # context->Rip<"prologue" label 2904 jb .Lcommon_seh_tail 2905 2906 lea .Lcbc_decrypt_body(%rip),%r10 2907 cmp %r10,%rbx # context->Rip<cbc_decrypt_body 2908 jb .Lrestore_cbc_rax 2909 2910 lea .Lcbc_ret(%rip),%r10 2911 cmp %r10,%rbx # context->Rip>="epilogue" label 2912 jae .Lcommon_seh_tail 2913 2914 lea 0(%rax),%rsi # top of stack 2915 lea 512($context),%rdi # &context.Xmm6 2916 mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax) 2917 .long 0xa548f3fc # cld; rep movsq 2918 lea 0x58(%rax),%rax # adjust stack pointer 2919 jmp .Lcommon_seh_tail 2920 2921 .Lrestore_cbc_rax: 2922 mov 120($context),%rax 2923 2924 .Lcommon_seh_tail: 2925 mov 8(%rax),%rdi 2926 mov 16(%rax),%rsi 2927 mov %rax,152($context) # restore context->Rsp 2928 mov %rsi,168($context) # restore context->Rsi 2929 mov %rdi,176($context) # restore context->Rdi 2930 2931 mov 40($disp),%rdi # disp->ContextRecord 2932 mov $context,%rsi # context 2933 mov \$154,%ecx # sizeof(CONTEXT) 2934 .long 0xa548f3fc # cld; rep movsq 2935 2936 mov $disp,%rsi 2937 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER 2938 mov 8(%rsi),%rdx # arg2, disp->ImageBase 2939 mov 0(%rsi),%r8 # arg3, disp->ControlPc 2940 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry 2941 mov 40(%rsi),%r10 # disp->ContextRecord 2942 lea 56(%rsi),%r11 # &disp->HandlerData 2943 lea 24(%rsi),%r12 # &disp->EstablisherFrame 2944 mov %r10,32(%rsp) # arg5 2945 mov %r11,40(%rsp) # arg6 2946 mov %r12,48(%rsp) # arg7 2947 mov %rcx,56(%rsp) # arg8, (NULL) 2948 call *__imp_RtlVirtualUnwind(%rip) 2949 2950 mov \$1,%eax # ExceptionContinueSearch 2951 add \$64,%rsp 2952 popfq 2953 pop %r15 2954 pop %r14 2955 pop %r13 2956 pop %r12 2957 pop %rbp 2958 pop %rbx 2959 pop %rdi 2960 pop %rsi 2961 ret 2962 .size cbc_se_handler,.-cbc_se_handler 2963 2964 .section .pdata 2965 .align 4 2966 ___ 2967 $code.=<<___ if ($PREFIX eq "aesni"); 2968 .rva .LSEH_begin_aesni_ecb_encrypt 2969 .rva .LSEH_end_aesni_ecb_encrypt 2970 .rva .LSEH_info_ecb 2971 2972 .rva .LSEH_begin_aesni_ccm64_encrypt_blocks 2973 .rva .LSEH_end_aesni_ccm64_encrypt_blocks 2974 .rva .LSEH_info_ccm64_enc 2975 2976 .rva .LSEH_begin_aesni_ccm64_decrypt_blocks 2977 .rva .LSEH_end_aesni_ccm64_decrypt_blocks 2978 .rva .LSEH_info_ccm64_dec 2979 2980 .rva .LSEH_begin_aesni_ctr32_encrypt_blocks 2981 .rva .LSEH_end_aesni_ctr32_encrypt_blocks 2982 .rva .LSEH_info_ctr32 2983 2984 .rva .LSEH_begin_aesni_xts_encrypt 2985 .rva .LSEH_end_aesni_xts_encrypt 2986 .rva .LSEH_info_xts_enc 2987 2988 .rva .LSEH_begin_aesni_xts_decrypt 2989 .rva .LSEH_end_aesni_xts_decrypt 2990 .rva .LSEH_info_xts_dec 2991 ___ 2992 $code.=<<___; 2993 .rva .LSEH_begin_${PREFIX}_cbc_encrypt 2994 .rva .LSEH_end_${PREFIX}_cbc_encrypt 2995 .rva .LSEH_info_cbc 2996 2997 .rva ${PREFIX}_set_decrypt_key 2998 .rva .LSEH_end_set_decrypt_key 2999 .rva .LSEH_info_key 3000 3001 .rva ${PREFIX}_set_encrypt_key 3002 .rva .LSEH_end_set_encrypt_key 3003 .rva .LSEH_info_key 3004 .section .xdata 3005 .align 8 3006 ___ 3007 $code.=<<___ if ($PREFIX eq "aesni"); 3008 .LSEH_info_ecb: 3009 .byte 9,0,0,0 3010 .rva ecb_ccm64_se_handler 3011 .rva .Lecb_enc_body,.Lecb_enc_ret # HandlerData[] 3012 .LSEH_info_ccm64_enc: 3013 .byte 9,0,0,0 3014 .rva ecb_ccm64_se_handler 3015 .rva .Lccm64_enc_body,.Lccm64_enc_ret # HandlerData[] 3016 .LSEH_info_ccm64_dec: 3017 .byte 9,0,0,0 3018 .rva ecb_ccm64_se_handler 3019 .rva .Lccm64_dec_body,.Lccm64_dec_ret # HandlerData[] 3020 .LSEH_info_ctr32: 3021 .byte 9,0,0,0 3022 .rva ctr32_se_handler 3023 .LSEH_info_xts_enc: 3024 .byte 9,0,0,0 3025 .rva xts_se_handler 3026 .rva .Lxts_enc_body,.Lxts_enc_epilogue # HandlerData[] 3027 .LSEH_info_xts_dec: 3028 .byte 9,0,0,0 3029 .rva xts_se_handler 3030 .rva .Lxts_dec_body,.Lxts_dec_epilogue # HandlerData[] 3031 ___ 3032 $code.=<<___; 3033 .LSEH_info_cbc: 3034 .byte 9,0,0,0 3035 .rva cbc_se_handler 3036 .LSEH_info_key: 3037 .byte 0x01,0x04,0x01,0x00 3038 .byte 0x04,0x02,0x00,0x00 # sub rsp,8 3039 ___ 3040 } 3041 3042 sub rex { 3043 local *opcode=shift; 3044 my ($dst,$src)=@_; 3045 my $rex=0; 3046 3047 $rex|=0x04 if($dst>=8); 3048 $rex|=0x01 if($src>=8); 3049 push @opcode,$rex|0x40 if($rex); 3050 } 3051 3052 sub aesni { 3053 my $line=shift; 3054 my @opcode=(0x66); 3055 3056 if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) { 3057 rex(\@opcode,$4,$3); 3058 push @opcode,0x0f,0x3a,0xdf; 3059 push @opcode,0xc0|($3&7)|(($4&7)<<3); # ModR/M 3060 my $c=$2; 3061 push @opcode,$c=~/^0/?oct($c):$c; 3062 return ".byte\t".join(',',@opcode); 3063 } 3064 elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) { 3065 my %opcodelet = ( 3066 "aesimc" => 0xdb, 3067 "aesenc" => 0xdc, "aesenclast" => 0xdd, 3068 "aesdec" => 0xde, "aesdeclast" => 0xdf 3069 ); 3070 return undef if (!defined($opcodelet{$1})); 3071 rex(\@opcode,$3,$2); 3072 push @opcode,0x0f,0x38,$opcodelet{$1}; 3073 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M 3074 return ".byte\t".join(',',@opcode); 3075 } 3076 return $line; 3077 } 3078 3079 $code =~ s/\`([^\`]*)\`/eval($1)/gem; 3080 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem; 3081 3082 print $code; 3083 3084 close STDOUT; 3085