aesni-sha1-x86_64.pl 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146
  1. #! /usr/bin/env perl
  2. # Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. #
  9. # ====================================================================
  10. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  11. # project. The module is, however, dual licensed under OpenSSL and
  12. # CRYPTOGAMS licenses depending on where you obtain it. For further
  13. # details see http://www.openssl.org/~appro/cryptogams/.
  14. # ====================================================================
  15. #
  16. # June 2011
  17. #
  18. # This is AESNI-CBC+SHA1 "stitch" implementation. The idea, as spelled
  19. # in http://download.intel.com/design/intarch/papers/323686.pdf, is
  20. # that since AESNI-CBC encrypt exhibit *very* low instruction-level
  21. # parallelism, interleaving it with another algorithm would allow to
  22. # utilize processor resources better and achieve better performance.
  23. # SHA1 instruction sequences(*) are taken from sha1-x86_64.pl and
  24. # AESNI code is weaved into it. Below are performance numbers in
  25. # cycles per processed byte, less is better, for standalone AESNI-CBC
  26. # encrypt, sum of the latter and standalone SHA1, and "stitched"
  27. # subroutine:
  28. #
  29. # AES-128-CBC +SHA1 stitch gain
  30. # Westmere 3.77[+5.3] 9.07 6.55 +38%
  31. # Sandy Bridge 5.05[+5.0(6.1)] 10.06(11.15) 5.98(7.05) +68%(+58%)
  32. # Ivy Bridge 5.05[+4.6] 9.65 5.54 +74%
  33. # Haswell 4.43[+3.6(4.2)] 8.00(8.58) 4.55(5.21) +75%(+65%)
  34. # Skylake 2.63[+3.5(4.1)] 6.17(6.69) 4.23(4.44) +46%(+51%)
  35. # Bulldozer 5.77[+6.0] 11.72 6.37 +84%
  36. # Ryzen(**) 2.71[+1.93] 4.64 2.74 +69%
  37. # Goldmont(**) 3.82[+1.70] 5.52 4.20 +31%
  38. #
  39. # AES-192-CBC
  40. # Westmere 4.51 9.81 6.80 +44%
  41. # Sandy Bridge 6.05 11.06(12.15) 6.11(7.19) +81%(+69%)
  42. # Ivy Bridge 6.05 10.65 6.07 +75%
  43. # Haswell 5.29 8.86(9.44) 5.32(5.32) +67%(+77%)
  44. # Bulldozer 6.89 12.84 6.96 +84%
  45. #
  46. # AES-256-CBC
  47. # Westmere 5.25 10.55 7.21 +46%
  48. # Sandy Bridge 7.05 12.06(13.15) 7.12(7.72) +69%(+70%)
  49. # Ivy Bridge 7.05 11.65 7.12 +64%
  50. # Haswell 6.19 9.76(10.34) 6.21(6.25) +57%(+65%)
  51. # Skylake 3.62 7.16(7.68) 4.56(4.76) +57%(+61%)
  52. # Bulldozer 8.00 13.95 8.25 +69%
  53. # Ryzen(**) 3.71 5.64 3.72 +52%
  54. # Goldmont(**) 5.35 7.05 5.76 +22%
  55. #
  56. # (*) There are two code paths: SSSE3 and AVX. See sha1-568.pl for
  57. # background information. Above numbers in parentheses are SSSE3
  58. # results collected on AVX-capable CPU, i.e. apply on OSes that
  59. # don't support AVX.
  60. # (**) SHAEXT results.
  61. #
  62. # Needless to mention that it makes no sense to implement "stitched"
  63. # *decrypt* subroutine. Because *both* AESNI-CBC decrypt and SHA1
  64. # fully utilize parallelism, so stitching would not give any gain
  65. # anyway. Well, there might be some, e.g. because of better cache
  66. # locality... For reference, here are performance results for
  67. # standalone AESNI-CBC decrypt:
  68. #
  69. # AES-128-CBC AES-192-CBC AES-256-CBC
  70. # Westmere 1.25 1.50 1.75
  71. # Sandy Bridge 0.74 0.91 1.09
  72. # Ivy Bridge 0.74 0.90 1.11
  73. # Haswell 0.63 0.76 0.88
  74. # Bulldozer 0.70 0.85 0.99
  75. # And indeed:
  76. #
  77. # AES-256-CBC +SHA1 stitch gain
  78. # Westmere 1.75 7.20 6.68 +7.8%
  79. # Sandy Bridge 1.09 6.09(7.22) 5.82(6.95) +4.6%(+3.9%)
  80. # Ivy Bridge 1.11 5.70 5.45 +4.6%
  81. # Haswell 0.88 4.45(5.00) 4.39(4.69) +1.4%(*)(+6.6%)
  82. # Bulldozer 0.99 6.95 5.95 +17%(**)
  83. #
  84. # (*) Tiny improvement coefficient on Haswell is because we compare
  85. # AVX1 stitch to sum with AVX2 SHA1.
  86. # (**) Execution is fully dominated by integer code sequence and
  87. # SIMD still hardly shows [in single-process benchmark;-]
  88. $flavour = shift;
  89. $output = shift;
  90. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  91. $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  92. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  93. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  94. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  95. die "can't locate x86_64-xlate.pl";
  96. $avx=1 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
  97. =~ /GNU assembler version ([2-9]\.[0-9]+)/ &&
  98. $1>=2.19);
  99. $avx=1 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
  100. `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/ &&
  101. $1>=2.09);
  102. $avx=1 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
  103. `ml64 2>&1` =~ /Version ([0-9]+)\./ &&
  104. $1>=10);
  105. $avx=1 if (!$avx && `$ENV{CC} -v 2>&1` =~ /((?:clang|LLVM) version|.*based on LLVM) ([0-9]+\.[0-9]+)/ && $2>=3.0);
  106. $shaext=1; ### set to zero if compiling for 1.0.1
  107. $stitched_decrypt=0;
  108. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
  109. *STDOUT=*OUT;
  110. # void aesni_cbc_sha1_enc(const void *inp,
  111. # void *out,
  112. # size_t length,
  113. # const AES_KEY *key,
  114. # unsigned char *iv,
  115. # SHA_CTX *ctx,
  116. # const void *in0);
  117. $code.=<<___;
  118. .text
  119. .extern OPENSSL_ia32cap_P
  120. .globl aesni_cbc_sha1_enc
  121. .type aesni_cbc_sha1_enc,\@abi-omnipotent
  122. .align 32
  123. aesni_cbc_sha1_enc:
  124. .cfi_startproc
  125. # caller should check for SSSE3 and AES-NI bits
  126. mov OPENSSL_ia32cap_P+0(%rip),%r10d
  127. mov OPENSSL_ia32cap_P+4(%rip),%r11
  128. ___
  129. $code.=<<___ if ($shaext);
  130. bt \$61,%r11 # check SHA bit
  131. jc aesni_cbc_sha1_enc_shaext
  132. ___
  133. $code.=<<___ if ($avx);
  134. and \$`1<<28`,%r11d # mask AVX bit
  135. and \$`1<<30`,%r10d # mask "Intel CPU" bit
  136. or %r11d,%r10d
  137. cmp \$`1<<28|1<<30`,%r10d
  138. je aesni_cbc_sha1_enc_avx
  139. ___
  140. $code.=<<___;
  141. jmp aesni_cbc_sha1_enc_ssse3
  142. ret
  143. .cfi_endproc
  144. .size aesni_cbc_sha1_enc,.-aesni_cbc_sha1_enc
  145. ___
  146. my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
  147. my $Xi=4;
  148. my @X=map("%xmm$_",(4..7,0..3));
  149. my @Tx=map("%xmm$_",(8..10));
  150. my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
  151. my @T=("%esi","%edi");
  152. my $j=0; my $jj=0; my $r=0; my $sn=0; my $rx=0;
  153. my $K_XX_XX="%r11";
  154. my ($rndkey0,$iv,$in)=map("%xmm$_",(11..13)); # for enc
  155. my @rndkey=("%xmm14","%xmm15"); # for enc
  156. my ($inout0,$inout1,$inout2,$inout3)=map("%xmm$_",(12..15)); # for dec
  157. if (1) { # reassign for Atom Silvermont
  158. # The goal is to minimize amount of instructions with more than
  159. # 3 prefix bytes. Or in more practical terms to keep AES-NI *and*
  160. # SSSE3 instructions to upper half of the register bank.
  161. @X=map("%xmm$_",(8..11,4..7));
  162. @Tx=map("%xmm$_",(12,13,3));
  163. ($iv,$in,$rndkey0)=map("%xmm$_",(2,14,15));
  164. @rndkey=("%xmm0","%xmm1");
  165. }
  166. sub AUTOLOAD() # thunk [simplified] 32-bit style perlasm
  167. { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
  168. my $arg = pop;
  169. $arg = "\$$arg" if ($arg*1 eq $arg);
  170. $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
  171. }
  172. my $_rol=sub { &rol(@_) };
  173. my $_ror=sub { &ror(@_) };
  174. $code.=<<___;
  175. .type aesni_cbc_sha1_enc_ssse3,\@function,6
  176. .align 32
  177. aesni_cbc_sha1_enc_ssse3:
  178. .cfi_startproc
  179. mov `($win64?56:8)`(%rsp),$inp # load 7th argument
  180. #shr \$6,$len # debugging artefact
  181. #jz .Lepilogue_ssse3 # debugging artefact
  182. push %rbx
  183. .cfi_push %rbx
  184. push %rbp
  185. .cfi_push %rbp
  186. push %r12
  187. .cfi_push %r12
  188. push %r13
  189. .cfi_push %r13
  190. push %r14
  191. .cfi_push %r14
  192. push %r15
  193. .cfi_push %r15
  194. lea `-104-($win64?10*16:0)`(%rsp),%rsp
  195. .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
  196. #mov $in0,$inp # debugging artefact
  197. #lea 64(%rsp),$ctx # debugging artefact
  198. ___
  199. $code.=<<___ if ($win64);
  200. movaps %xmm6,96+0(%rsp)
  201. movaps %xmm7,96+16(%rsp)
  202. movaps %xmm8,96+32(%rsp)
  203. movaps %xmm9,96+48(%rsp)
  204. movaps %xmm10,96+64(%rsp)
  205. movaps %xmm11,96+80(%rsp)
  206. movaps %xmm12,96+96(%rsp)
  207. movaps %xmm13,96+112(%rsp)
  208. movaps %xmm14,96+128(%rsp)
  209. movaps %xmm15,96+144(%rsp)
  210. .Lprologue_ssse3:
  211. ___
  212. $code.=<<___;
  213. mov $in0,%r12 # reassign arguments
  214. mov $out,%r13
  215. mov $len,%r14
  216. lea 112($key),%r15 # size optimization
  217. movdqu ($ivp),$iv # load IV
  218. mov $ivp,88(%rsp) # save $ivp
  219. ___
  220. ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
  221. my $rounds="${ivp}d";
  222. $code.=<<___;
  223. shl \$6,$len
  224. sub $in0,$out
  225. mov 240-112($key),$rounds
  226. add $inp,$len # end of input
  227. lea K_XX_XX(%rip),$K_XX_XX
  228. mov 0($ctx),$A # load context
  229. mov 4($ctx),$B
  230. mov 8($ctx),$C
  231. mov 12($ctx),$D
  232. mov $B,@T[0] # magic seed
  233. mov 16($ctx),$E
  234. mov $C,@T[1]
  235. xor $D,@T[1]
  236. and @T[1],@T[0]
  237. movdqa 64($K_XX_XX),@Tx[2] # pbswap mask
  238. movdqa 0($K_XX_XX),@Tx[1] # K_00_19
  239. movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
  240. movdqu 16($inp),@X[-3&7]
  241. movdqu 32($inp),@X[-2&7]
  242. movdqu 48($inp),@X[-1&7]
  243. pshufb @Tx[2],@X[-4&7] # byte swap
  244. pshufb @Tx[2],@X[-3&7]
  245. pshufb @Tx[2],@X[-2&7]
  246. add \$64,$inp
  247. paddd @Tx[1],@X[-4&7] # add K_00_19
  248. pshufb @Tx[2],@X[-1&7]
  249. paddd @Tx[1],@X[-3&7]
  250. paddd @Tx[1],@X[-2&7]
  251. movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
  252. psubd @Tx[1],@X[-4&7] # restore X[]
  253. movdqa @X[-3&7],16(%rsp)
  254. psubd @Tx[1],@X[-3&7]
  255. movdqa @X[-2&7],32(%rsp)
  256. psubd @Tx[1],@X[-2&7]
  257. movups -112($key),$rndkey0 # $key[0]
  258. movups 16-112($key),$rndkey[0] # forward reference
  259. jmp .Loop_ssse3
  260. ___
  261. my $aesenc=sub {
  262. use integer;
  263. my ($n,$k)=($r/10,$r%10);
  264. if ($k==0) {
  265. $code.=<<___;
  266. movups `16*$n`($in0),$in # load input
  267. xorps $rndkey0,$in
  268. ___
  269. $code.=<<___ if ($n);
  270. movups $iv,`16*($n-1)`($out,$in0) # write output
  271. ___
  272. $code.=<<___;
  273. xorps $in,$iv
  274. movups `32+16*$k-112`($key),$rndkey[1]
  275. aesenc $rndkey[0],$iv
  276. ___
  277. } elsif ($k==9) {
  278. $sn++;
  279. $code.=<<___;
  280. cmp \$11,$rounds
  281. jb .Laesenclast$sn
  282. movups `32+16*($k+0)-112`($key),$rndkey[1]
  283. aesenc $rndkey[0],$iv
  284. movups `32+16*($k+1)-112`($key),$rndkey[0]
  285. aesenc $rndkey[1],$iv
  286. je .Laesenclast$sn
  287. movups `32+16*($k+2)-112`($key),$rndkey[1]
  288. aesenc $rndkey[0],$iv
  289. movups `32+16*($k+3)-112`($key),$rndkey[0]
  290. aesenc $rndkey[1],$iv
  291. .Laesenclast$sn:
  292. aesenclast $rndkey[0],$iv
  293. movups 16-112($key),$rndkey[1] # forward reference
  294. ___
  295. } else {
  296. $code.=<<___;
  297. movups `32+16*$k-112`($key),$rndkey[1]
  298. aesenc $rndkey[0],$iv
  299. ___
  300. }
  301. $r++; unshift(@rndkey,pop(@rndkey));
  302. };
  303. sub Xupdate_ssse3_16_31() # recall that $Xi starts with 4
  304. { use integer;
  305. my $body = shift;
  306. my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
  307. my ($a,$b,$c,$d,$e);
  308. eval(shift(@insns)); # ror
  309. &pshufd (@X[0],@X[-4&7],0xee); # was &movdqa (@X[0],@X[-3&7]);
  310. eval(shift(@insns));
  311. &movdqa (@Tx[0],@X[-1&7]);
  312. &paddd (@Tx[1],@X[-1&7]);
  313. eval(shift(@insns));
  314. eval(shift(@insns));
  315. &punpcklqdq(@X[0],@X[-3&7]); # compose "X[-14]" in "X[0]", was &palignr(@X[0],@X[-4&7],8);
  316. eval(shift(@insns));
  317. eval(shift(@insns)); # rol
  318. eval(shift(@insns));
  319. &psrldq (@Tx[0],4); # "X[-3]", 3 dwords
  320. eval(shift(@insns));
  321. eval(shift(@insns));
  322. &pxor (@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
  323. eval(shift(@insns));
  324. eval(shift(@insns)); # ror
  325. &pxor (@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
  326. eval(shift(@insns));
  327. eval(shift(@insns));
  328. eval(shift(@insns));
  329. &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
  330. eval(shift(@insns));
  331. eval(shift(@insns)); # rol
  332. &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  333. eval(shift(@insns));
  334. eval(shift(@insns));
  335. &movdqa (@Tx[2],@X[0]);
  336. eval(shift(@insns));
  337. eval(shift(@insns));
  338. eval(shift(@insns)); # ror
  339. &movdqa (@Tx[0],@X[0]);
  340. eval(shift(@insns));
  341. &pslldq (@Tx[2],12); # "X[0]"<<96, extract one dword
  342. &paddd (@X[0],@X[0]);
  343. eval(shift(@insns));
  344. eval(shift(@insns));
  345. &psrld (@Tx[0],31);
  346. eval(shift(@insns));
  347. eval(shift(@insns)); # rol
  348. eval(shift(@insns));
  349. &movdqa (@Tx[1],@Tx[2]);
  350. eval(shift(@insns));
  351. eval(shift(@insns));
  352. &psrld (@Tx[2],30);
  353. eval(shift(@insns));
  354. eval(shift(@insns)); # ror
  355. &por (@X[0],@Tx[0]); # "X[0]"<<<=1
  356. eval(shift(@insns));
  357. eval(shift(@insns));
  358. eval(shift(@insns));
  359. &pslld (@Tx[1],2);
  360. &pxor (@X[0],@Tx[2]);
  361. eval(shift(@insns));
  362. &movdqa (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
  363. eval(shift(@insns)); # rol
  364. eval(shift(@insns));
  365. eval(shift(@insns));
  366. &pxor (@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
  367. &pshufd (@Tx[1],@X[-1&7],0xee) if ($Xi==7); # was &movdqa (@Tx[0],@X[-1&7]) in Xupdate_ssse3_32_79
  368. foreach (@insns) { eval; } # remaining instructions [if any]
  369. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  370. push(@Tx,shift(@Tx));
  371. }
  372. sub Xupdate_ssse3_32_79()
  373. { use integer;
  374. my $body = shift;
  375. my @insns = (&$body,&$body,&$body,&$body); # 32 to 44 instructions
  376. my ($a,$b,$c,$d,$e);
  377. eval(shift(@insns)) if ($Xi==8);
  378. &pxor (@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
  379. eval(shift(@insns)) if ($Xi==8);
  380. eval(shift(@insns)); # body_20_39
  381. eval(shift(@insns));
  382. eval(shift(@insns)) if (@insns[1] =~ /_ror/);
  383. eval(shift(@insns)) if (@insns[0] =~ /_ror/);
  384. &punpcklqdq(@Tx[0],@X[-1&7]); # compose "X[-6]", was &palignr(@Tx[0],@X[-2&7],8);
  385. eval(shift(@insns));
  386. eval(shift(@insns)); # rol
  387. &pxor (@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
  388. eval(shift(@insns));
  389. eval(shift(@insns));
  390. if ($Xi%5) {
  391. &movdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
  392. } else { # ... or load next one
  393. &movdqa (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
  394. }
  395. eval(shift(@insns)); # ror
  396. &paddd (@Tx[1],@X[-1&7]);
  397. eval(shift(@insns));
  398. &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-6]"
  399. eval(shift(@insns)); # body_20_39
  400. eval(shift(@insns));
  401. eval(shift(@insns));
  402. eval(shift(@insns)); # rol
  403. eval(shift(@insns)) if (@insns[0] =~ /_ror/);
  404. &movdqa (@Tx[0],@X[0]);
  405. eval(shift(@insns));
  406. eval(shift(@insns));
  407. &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  408. eval(shift(@insns)); # ror
  409. eval(shift(@insns));
  410. eval(shift(@insns)); # body_20_39
  411. &pslld (@X[0],2);
  412. eval(shift(@insns));
  413. eval(shift(@insns));
  414. &psrld (@Tx[0],30);
  415. eval(shift(@insns)) if (@insns[0] =~ /_rol/);# rol
  416. eval(shift(@insns));
  417. eval(shift(@insns));
  418. eval(shift(@insns)); # ror
  419. &por (@X[0],@Tx[0]); # "X[0]"<<<=2
  420. eval(shift(@insns));
  421. eval(shift(@insns)); # body_20_39
  422. eval(shift(@insns)) if (@insns[1] =~ /_rol/);
  423. eval(shift(@insns)) if (@insns[0] =~ /_rol/);
  424. &pshufd(@Tx[1],@X[-1&7],0xee) if ($Xi<19); # was &movdqa (@Tx[1],@X[0])
  425. eval(shift(@insns));
  426. eval(shift(@insns)); # rol
  427. eval(shift(@insns));
  428. eval(shift(@insns));
  429. eval(shift(@insns)); # rol
  430. eval(shift(@insns));
  431. foreach (@insns) { eval; } # remaining instructions
  432. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  433. push(@Tx,shift(@Tx));
  434. }
  435. sub Xuplast_ssse3_80()
  436. { use integer;
  437. my $body = shift;
  438. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  439. my ($a,$b,$c,$d,$e);
  440. eval(shift(@insns));
  441. eval(shift(@insns));
  442. eval(shift(@insns));
  443. eval(shift(@insns));
  444. &paddd (@Tx[1],@X[-1&7]);
  445. eval(shift(@insns));
  446. eval(shift(@insns));
  447. &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
  448. foreach (@insns) { eval; } # remaining instructions
  449. &cmp ($inp,$len);
  450. &je (shift);
  451. unshift(@Tx,pop(@Tx));
  452. &movdqa (@Tx[2],"64($K_XX_XX)"); # pbswap mask
  453. &movdqa (@Tx[1],"0($K_XX_XX)"); # K_00_19
  454. &movdqu (@X[-4&7],"0($inp)"); # load input
  455. &movdqu (@X[-3&7],"16($inp)");
  456. &movdqu (@X[-2&7],"32($inp)");
  457. &movdqu (@X[-1&7],"48($inp)");
  458. &pshufb (@X[-4&7],@Tx[2]); # byte swap
  459. &add ($inp,64);
  460. $Xi=0;
  461. }
  462. sub Xloop_ssse3()
  463. { use integer;
  464. my $body = shift;
  465. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  466. my ($a,$b,$c,$d,$e);
  467. eval(shift(@insns));
  468. eval(shift(@insns));
  469. eval(shift(@insns));
  470. &pshufb (@X[($Xi-3)&7],@Tx[2]);
  471. eval(shift(@insns));
  472. eval(shift(@insns));
  473. eval(shift(@insns));
  474. eval(shift(@insns));
  475. &paddd (@X[($Xi-4)&7],@Tx[1]);
  476. eval(shift(@insns));
  477. eval(shift(@insns));
  478. eval(shift(@insns));
  479. eval(shift(@insns));
  480. &movdqa (eval(16*$Xi)."(%rsp)",@X[($Xi-4)&7]); # X[]+K xfer to IALU
  481. eval(shift(@insns));
  482. eval(shift(@insns));
  483. eval(shift(@insns));
  484. eval(shift(@insns));
  485. &psubd (@X[($Xi-4)&7],@Tx[1]);
  486. foreach (@insns) { eval; }
  487. $Xi++;
  488. }
  489. sub Xtail_ssse3()
  490. { use integer;
  491. my $body = shift;
  492. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  493. my ($a,$b,$c,$d,$e);
  494. foreach (@insns) { eval; }
  495. }
  496. my @body_00_19 = (
  497. '($a,$b,$c,$d,$e)=@V;'.
  498. '&$_ror ($b,$j?7:2);', # $b>>>2
  499. '&xor (@T[0],$d);',
  500. '&mov (@T[1],$a);', # $b for next round
  501. '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
  502. '&xor ($b,$c);', # $c^$d for next round
  503. '&$_rol ($a,5);',
  504. '&add ($e,@T[0]);',
  505. '&and (@T[1],$b);', # ($b&($c^$d)) for next round
  506. '&xor ($b,$c);', # restore $b
  507. '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  508. );
  509. sub body_00_19 () { # ((c^d)&b)^d
  510. # on start @T[0]=(c^d)&b
  511. return &body_20_39() if ($rx==19); $rx++;
  512. use integer;
  513. my ($k,$n);
  514. my @r=@body_00_19;
  515. $n = scalar(@r);
  516. $k = (($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
  517. @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
  518. $jj++;
  519. return @r;
  520. }
  521. my @body_20_39 = (
  522. '($a,$b,$c,$d,$e)=@V;'.
  523. '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
  524. '&xor (@T[0],$d) if($j==19);'.
  525. '&xor (@T[0],$c) if($j> 19);', # ($b^$d^$c)
  526. '&mov (@T[1],$a);', # $b for next round
  527. '&$_rol ($a,5);',
  528. '&add ($e,@T[0]);',
  529. '&xor (@T[1],$c) if ($j< 79);', # $b^$d for next round
  530. '&$_ror ($b,7);', # $b>>>2
  531. '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  532. );
  533. sub body_20_39 () { # b^d^c
  534. # on entry @T[0]=b^d
  535. return &body_40_59() if ($rx==39); $rx++;
  536. use integer;
  537. my ($k,$n);
  538. my @r=@body_20_39;
  539. $n = scalar(@r);
  540. $k = (($jj+1)*8/20)*20*$n/8; # 8 aesencs per these 20 rounds
  541. @r[$k%$n].='&$aesenc();' if ($jj==$k/$n && $rx!=20);
  542. $jj++;
  543. return @r;
  544. }
  545. my @body_40_59 = (
  546. '($a,$b,$c,$d,$e)=@V;'.
  547. '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
  548. '&and (@T[0],$c) if ($j>=40);', # (b^c)&(c^d)
  549. '&xor ($c,$d) if ($j>=40);', # restore $c
  550. '&$_ror ($b,7);', # $b>>>2
  551. '&mov (@T[1],$a);', # $b for next round
  552. '&xor (@T[0],$c);',
  553. '&$_rol ($a,5);',
  554. '&add ($e,@T[0]);',
  555. '&xor (@T[1],$c) if ($j==59);'.
  556. '&xor (@T[1],$b) if ($j< 59);', # b^c for next round
  557. '&xor ($b,$c) if ($j< 59);', # c^d for next round
  558. '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
  559. );
  560. sub body_40_59 () { # ((b^c)&(c^d))^c
  561. # on entry @T[0]=(b^c), (c^=d)
  562. $rx++;
  563. use integer;
  564. my ($k,$n);
  565. my @r=@body_40_59;
  566. $n = scalar(@r);
  567. $k=(($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
  568. @r[$k%$n].='&$aesenc();' if ($jj==$k/$n && $rx!=40);
  569. $jj++;
  570. return @r;
  571. }
  572. $code.=<<___;
  573. .align 32
  574. .Loop_ssse3:
  575. ___
  576. &Xupdate_ssse3_16_31(\&body_00_19);
  577. &Xupdate_ssse3_16_31(\&body_00_19);
  578. &Xupdate_ssse3_16_31(\&body_00_19);
  579. &Xupdate_ssse3_16_31(\&body_00_19);
  580. &Xupdate_ssse3_32_79(\&body_00_19);
  581. &Xupdate_ssse3_32_79(\&body_20_39);
  582. &Xupdate_ssse3_32_79(\&body_20_39);
  583. &Xupdate_ssse3_32_79(\&body_20_39);
  584. &Xupdate_ssse3_32_79(\&body_20_39);
  585. &Xupdate_ssse3_32_79(\&body_20_39);
  586. &Xupdate_ssse3_32_79(\&body_40_59);
  587. &Xupdate_ssse3_32_79(\&body_40_59);
  588. &Xupdate_ssse3_32_79(\&body_40_59);
  589. &Xupdate_ssse3_32_79(\&body_40_59);
  590. &Xupdate_ssse3_32_79(\&body_40_59);
  591. &Xupdate_ssse3_32_79(\&body_20_39);
  592. &Xuplast_ssse3_80(\&body_20_39,".Ldone_ssse3"); # can jump to "done"
  593. $saved_j=$j; @saved_V=@V;
  594. $saved_r=$r; @saved_rndkey=@rndkey;
  595. &Xloop_ssse3(\&body_20_39);
  596. &Xloop_ssse3(\&body_20_39);
  597. &Xloop_ssse3(\&body_20_39);
  598. $code.=<<___;
  599. movups $iv,48($out,$in0) # write output
  600. lea 64($in0),$in0
  601. add 0($ctx),$A # update context
  602. add 4($ctx),@T[0]
  603. add 8($ctx),$C
  604. add 12($ctx),$D
  605. mov $A,0($ctx)
  606. add 16($ctx),$E
  607. mov @T[0],4($ctx)
  608. mov @T[0],$B # magic seed
  609. mov $C,8($ctx)
  610. mov $C,@T[1]
  611. mov $D,12($ctx)
  612. xor $D,@T[1]
  613. mov $E,16($ctx)
  614. and @T[1],@T[0]
  615. jmp .Loop_ssse3
  616. .Ldone_ssse3:
  617. ___
  618. $jj=$j=$saved_j; @V=@saved_V;
  619. $r=$saved_r; @rndkey=@saved_rndkey;
  620. &Xtail_ssse3(\&body_20_39);
  621. &Xtail_ssse3(\&body_20_39);
  622. &Xtail_ssse3(\&body_20_39);
  623. $code.=<<___;
  624. movups $iv,48($out,$in0) # write output
  625. mov 88(%rsp),$ivp # restore $ivp
  626. add 0($ctx),$A # update context
  627. add 4($ctx),@T[0]
  628. add 8($ctx),$C
  629. mov $A,0($ctx)
  630. add 12($ctx),$D
  631. mov @T[0],4($ctx)
  632. add 16($ctx),$E
  633. mov $C,8($ctx)
  634. mov $D,12($ctx)
  635. mov $E,16($ctx)
  636. movups $iv,($ivp) # write IV
  637. ___
  638. $code.=<<___ if ($win64);
  639. movaps 96+0(%rsp),%xmm6
  640. movaps 96+16(%rsp),%xmm7
  641. movaps 96+32(%rsp),%xmm8
  642. movaps 96+48(%rsp),%xmm9
  643. movaps 96+64(%rsp),%xmm10
  644. movaps 96+80(%rsp),%xmm11
  645. movaps 96+96(%rsp),%xmm12
  646. movaps 96+112(%rsp),%xmm13
  647. movaps 96+128(%rsp),%xmm14
  648. movaps 96+144(%rsp),%xmm15
  649. ___
  650. $code.=<<___;
  651. lea `104+($win64?10*16:0)`(%rsp),%rsi
  652. .cfi_def_cfa %rsi,56
  653. mov 0(%rsi),%r15
  654. .cfi_restore %r15
  655. mov 8(%rsi),%r14
  656. .cfi_restore %r14
  657. mov 16(%rsi),%r13
  658. .cfi_restore %r13
  659. mov 24(%rsi),%r12
  660. .cfi_restore %r12
  661. mov 32(%rsi),%rbp
  662. .cfi_restore %rbp
  663. mov 40(%rsi),%rbx
  664. .cfi_restore %rbx
  665. lea 48(%rsi),%rsp
  666. .cfi_def_cfa %rsp,8
  667. .Lepilogue_ssse3:
  668. ret
  669. .cfi_endproc
  670. .size aesni_cbc_sha1_enc_ssse3,.-aesni_cbc_sha1_enc_ssse3
  671. ___
  672. if ($stitched_decrypt) {{{
  673. # reset
  674. ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
  675. $j=$jj=$r=$rx=0;
  676. $Xi=4;
  677. # reassign for Atom Silvermont (see above)
  678. ($inout0,$inout1,$inout2,$inout3,$rndkey0)=map("%xmm$_",(0..4));
  679. @X=map("%xmm$_",(8..13,6,7));
  680. @Tx=map("%xmm$_",(14,15,5));
  681. my @aes256_dec = (
  682. '&movdqu($inout0,"0x00($in0)");',
  683. '&movdqu($inout1,"0x10($in0)"); &pxor ($inout0,$rndkey0);',
  684. '&movdqu($inout2,"0x20($in0)"); &pxor ($inout1,$rndkey0);',
  685. '&movdqu($inout3,"0x30($in0)"); &pxor ($inout2,$rndkey0);',
  686. '&pxor ($inout3,$rndkey0); &movups ($rndkey0,"16-112($key)");',
  687. '&movaps("64(%rsp)",@X[2]);', # save IV, originally @X[3]
  688. undef,undef
  689. );
  690. for ($i=0;$i<13;$i++) {
  691. push (@aes256_dec,(
  692. '&aesdec ($inout0,$rndkey0);',
  693. '&aesdec ($inout1,$rndkey0);',
  694. '&aesdec ($inout2,$rndkey0);',
  695. '&aesdec ($inout3,$rndkey0); &movups($rndkey0,"'.(16*($i+2)-112).'($key)");'
  696. ));
  697. push (@aes256_dec,(undef,undef)) if (($i>=3 && $i<=5) || $i>=11);
  698. push (@aes256_dec,(undef,undef)) if ($i==5);
  699. }
  700. push(@aes256_dec,(
  701. '&aesdeclast ($inout0,$rndkey0); &movups (@X[0],"0x00($in0)");',
  702. '&aesdeclast ($inout1,$rndkey0); &movups (@X[1],"0x10($in0)");',
  703. '&aesdeclast ($inout2,$rndkey0); &movups (@X[2],"0x20($in0)");',
  704. '&aesdeclast ($inout3,$rndkey0); &movups (@X[3],"0x30($in0)");',
  705. '&xorps ($inout0,"64(%rsp)"); &movdqu ($rndkey0,"-112($key)");',
  706. '&xorps ($inout1,@X[0]); &movups ("0x00($out,$in0)",$inout0);',
  707. '&xorps ($inout2,@X[1]); &movups ("0x10($out,$in0)",$inout1);',
  708. '&xorps ($inout3,@X[2]); &movups ("0x20($out,$in0)",$inout2);',
  709. '&movups ("0x30($out,$in0)",$inout3);'
  710. ));
  711. sub body_00_19_dec () { # ((c^d)&b)^d
  712. # on start @T[0]=(c^d)&b
  713. return &body_20_39_dec() if ($rx==19);
  714. my @r=@body_00_19;
  715. unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
  716. $rx++;
  717. return @r;
  718. }
  719. sub body_20_39_dec () { # b^d^c
  720. # on entry @T[0]=b^d
  721. return &body_40_59_dec() if ($rx==39);
  722. my @r=@body_20_39;
  723. unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
  724. $rx++;
  725. return @r;
  726. }
  727. sub body_40_59_dec () { # ((b^c)&(c^d))^c
  728. # on entry @T[0]=(b^c), (c^=d)
  729. my @r=@body_40_59;
  730. unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
  731. $rx++;
  732. return @r;
  733. }
  734. $code.=<<___;
  735. .globl aesni256_cbc_sha1_dec
  736. .type aesni256_cbc_sha1_dec,\@abi-omnipotent
  737. .align 32
  738. aesni256_cbc_sha1_dec:
  739. .cfi_startproc
  740. # caller should check for SSSE3 and AES-NI bits
  741. mov OPENSSL_ia32cap_P+0(%rip),%r10d
  742. mov OPENSSL_ia32cap_P+4(%rip),%r11d
  743. ___
  744. $code.=<<___ if ($avx);
  745. and \$`1<<28`,%r11d # mask AVX bit
  746. and \$`1<<30`,%r10d # mask "Intel CPU" bit
  747. or %r11d,%r10d
  748. cmp \$`1<<28|1<<30`,%r10d
  749. je aesni256_cbc_sha1_dec_avx
  750. ___
  751. $code.=<<___;
  752. jmp aesni256_cbc_sha1_dec_ssse3
  753. ret
  754. .cfi_endproc
  755. .size aesni256_cbc_sha1_dec,.-aesni256_cbc_sha1_dec
  756. .type aesni256_cbc_sha1_dec_ssse3,\@function,6
  757. .align 32
  758. aesni256_cbc_sha1_dec_ssse3:
  759. .cfi_startproc
  760. mov `($win64?56:8)`(%rsp),$inp # load 7th argument
  761. push %rbx
  762. .cfi_push %rbx
  763. push %rbp
  764. .cfi_push %rbp
  765. push %r12
  766. .cfi_push %r12
  767. push %r13
  768. .cfi_push %r13
  769. push %r14
  770. .cfi_push %r14
  771. push %r15
  772. .cfi_push %r15
  773. lea `-104-($win64?10*16:0)`(%rsp),%rsp
  774. .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
  775. ___
  776. $code.=<<___ if ($win64);
  777. movaps %xmm6,96+0(%rsp)
  778. movaps %xmm7,96+16(%rsp)
  779. movaps %xmm8,96+32(%rsp)
  780. movaps %xmm9,96+48(%rsp)
  781. movaps %xmm10,96+64(%rsp)
  782. movaps %xmm11,96+80(%rsp)
  783. movaps %xmm12,96+96(%rsp)
  784. movaps %xmm13,96+112(%rsp)
  785. movaps %xmm14,96+128(%rsp)
  786. movaps %xmm15,96+144(%rsp)
  787. .Lprologue_dec_ssse3:
  788. ___
  789. $code.=<<___;
  790. mov $in0,%r12 # reassign arguments
  791. mov $out,%r13
  792. mov $len,%r14
  793. lea 112($key),%r15 # size optimization
  794. movdqu ($ivp),@X[3] # load IV
  795. #mov $ivp,88(%rsp) # save $ivp
  796. ___
  797. ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
  798. $code.=<<___;
  799. shl \$6,$len
  800. sub $in0,$out
  801. add $inp,$len # end of input
  802. lea K_XX_XX(%rip),$K_XX_XX
  803. mov 0($ctx),$A # load context
  804. mov 4($ctx),$B
  805. mov 8($ctx),$C
  806. mov 12($ctx),$D
  807. mov $B,@T[0] # magic seed
  808. mov 16($ctx),$E
  809. mov $C,@T[1]
  810. xor $D,@T[1]
  811. and @T[1],@T[0]
  812. movdqa 64($K_XX_XX),@Tx[2] # pbswap mask
  813. movdqa 0($K_XX_XX),@Tx[1] # K_00_19
  814. movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
  815. movdqu 16($inp),@X[-3&7]
  816. movdqu 32($inp),@X[-2&7]
  817. movdqu 48($inp),@X[-1&7]
  818. pshufb @Tx[2],@X[-4&7] # byte swap
  819. add \$64,$inp
  820. pshufb @Tx[2],@X[-3&7]
  821. pshufb @Tx[2],@X[-2&7]
  822. pshufb @Tx[2],@X[-1&7]
  823. paddd @Tx[1],@X[-4&7] # add K_00_19
  824. paddd @Tx[1],@X[-3&7]
  825. paddd @Tx[1],@X[-2&7]
  826. movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
  827. psubd @Tx[1],@X[-4&7] # restore X[]
  828. movdqa @X[-3&7],16(%rsp)
  829. psubd @Tx[1],@X[-3&7]
  830. movdqa @X[-2&7],32(%rsp)
  831. psubd @Tx[1],@X[-2&7]
  832. movdqu -112($key),$rndkey0 # $key[0]
  833. jmp .Loop_dec_ssse3
  834. .align 32
  835. .Loop_dec_ssse3:
  836. ___
  837. &Xupdate_ssse3_16_31(\&body_00_19_dec);
  838. &Xupdate_ssse3_16_31(\&body_00_19_dec);
  839. &Xupdate_ssse3_16_31(\&body_00_19_dec);
  840. &Xupdate_ssse3_16_31(\&body_00_19_dec);
  841. &Xupdate_ssse3_32_79(\&body_00_19_dec);
  842. &Xupdate_ssse3_32_79(\&body_20_39_dec);
  843. &Xupdate_ssse3_32_79(\&body_20_39_dec);
  844. &Xupdate_ssse3_32_79(\&body_20_39_dec);
  845. &Xupdate_ssse3_32_79(\&body_20_39_dec);
  846. &Xupdate_ssse3_32_79(\&body_20_39_dec);
  847. &Xupdate_ssse3_32_79(\&body_40_59_dec);
  848. &Xupdate_ssse3_32_79(\&body_40_59_dec);
  849. &Xupdate_ssse3_32_79(\&body_40_59_dec);
  850. &Xupdate_ssse3_32_79(\&body_40_59_dec);
  851. &Xupdate_ssse3_32_79(\&body_40_59_dec);
  852. &Xupdate_ssse3_32_79(\&body_20_39_dec);
  853. &Xuplast_ssse3_80(\&body_20_39_dec,".Ldone_dec_ssse3"); # can jump to "done"
  854. $saved_j=$j; @saved_V=@V;
  855. $saved_rx=$rx;
  856. &Xloop_ssse3(\&body_20_39_dec);
  857. &Xloop_ssse3(\&body_20_39_dec);
  858. &Xloop_ssse3(\&body_20_39_dec);
  859. eval(@aes256_dec[-1]); # last store
  860. $code.=<<___;
  861. lea 64($in0),$in0
  862. add 0($ctx),$A # update context
  863. add 4($ctx),@T[0]
  864. add 8($ctx),$C
  865. add 12($ctx),$D
  866. mov $A,0($ctx)
  867. add 16($ctx),$E
  868. mov @T[0],4($ctx)
  869. mov @T[0],$B # magic seed
  870. mov $C,8($ctx)
  871. mov $C,@T[1]
  872. mov $D,12($ctx)
  873. xor $D,@T[1]
  874. mov $E,16($ctx)
  875. and @T[1],@T[0]
  876. jmp .Loop_dec_ssse3
  877. .Ldone_dec_ssse3:
  878. ___
  879. $jj=$j=$saved_j; @V=@saved_V;
  880. $rx=$saved_rx;
  881. &Xtail_ssse3(\&body_20_39_dec);
  882. &Xtail_ssse3(\&body_20_39_dec);
  883. &Xtail_ssse3(\&body_20_39_dec);
  884. eval(@aes256_dec[-1]); # last store
  885. $code.=<<___;
  886. add 0($ctx),$A # update context
  887. add 4($ctx),@T[0]
  888. add 8($ctx),$C
  889. mov $A,0($ctx)
  890. add 12($ctx),$D
  891. mov @T[0],4($ctx)
  892. add 16($ctx),$E
  893. mov $C,8($ctx)
  894. mov $D,12($ctx)
  895. mov $E,16($ctx)
  896. movups @X[3],($ivp) # write IV
  897. ___
  898. $code.=<<___ if ($win64);
  899. movaps 96+0(%rsp),%xmm6
  900. movaps 96+16(%rsp),%xmm7
  901. movaps 96+32(%rsp),%xmm8
  902. movaps 96+48(%rsp),%xmm9
  903. movaps 96+64(%rsp),%xmm10
  904. movaps 96+80(%rsp),%xmm11
  905. movaps 96+96(%rsp),%xmm12
  906. movaps 96+112(%rsp),%xmm13
  907. movaps 96+128(%rsp),%xmm14
  908. movaps 96+144(%rsp),%xmm15
  909. ___
  910. $code.=<<___;
  911. lea `104+($win64?10*16:0)`(%rsp),%rsi
  912. .cfi_cfa_def %rsi,56
  913. mov 0(%rsi),%r15
  914. .cfi_restore %r15
  915. mov 8(%rsi),%r14
  916. .cfi_restore %r14
  917. mov 16(%rsi),%r13
  918. .cfi_restore %r13
  919. mov 24(%rsi),%r12
  920. .cfi_restore %r12
  921. mov 32(%rsi),%rbp
  922. .cfi_restore %rbp
  923. mov 40(%rsi),%rbx
  924. .cfi_restore %rbx
  925. lea 48(%rsi),%rsp
  926. .cfi_cfa_def %rsp,8
  927. .Lepilogue_dec_ssse3:
  928. ret
  929. .cfi_endproc
  930. .size aesni256_cbc_sha1_dec_ssse3,.-aesni256_cbc_sha1_dec_ssse3
  931. ___
  932. }}}
  933. $j=$jj=$r=$rx=0;
  934. if ($avx) {
  935. my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
  936. my $Xi=4;
  937. my @X=map("%xmm$_",(4..7,0..3));
  938. my @Tx=map("%xmm$_",(8..10));
  939. my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
  940. my @T=("%esi","%edi");
  941. my ($rndkey0,$iv,$in)=map("%xmm$_",(11..13));
  942. my @rndkey=("%xmm14","%xmm15");
  943. my ($inout0,$inout1,$inout2,$inout3)=map("%xmm$_",(12..15)); # for dec
  944. my $Kx=@Tx[2];
  945. my $_rol=sub { &shld(@_[0],@_) };
  946. my $_ror=sub { &shrd(@_[0],@_) };
  947. $code.=<<___;
  948. .type aesni_cbc_sha1_enc_avx,\@function,6
  949. .align 32
  950. aesni_cbc_sha1_enc_avx:
  951. .cfi_startproc
  952. mov `($win64?56:8)`(%rsp),$inp # load 7th argument
  953. #shr \$6,$len # debugging artefact
  954. #jz .Lepilogue_avx # debugging artefact
  955. push %rbx
  956. .cfi_push %rbx
  957. push %rbp
  958. .cfi_push %rbp
  959. push %r12
  960. .cfi_push %r12
  961. push %r13
  962. .cfi_push %r13
  963. push %r14
  964. .cfi_push %r14
  965. push %r15
  966. .cfi_push %r15
  967. lea `-104-($win64?10*16:0)`(%rsp),%rsp
  968. .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
  969. #mov $in0,$inp # debugging artefact
  970. #lea 64(%rsp),$ctx # debugging artefact
  971. ___
  972. $code.=<<___ if ($win64);
  973. movaps %xmm6,96+0(%rsp)
  974. movaps %xmm7,96+16(%rsp)
  975. movaps %xmm8,96+32(%rsp)
  976. movaps %xmm9,96+48(%rsp)
  977. movaps %xmm10,96+64(%rsp)
  978. movaps %xmm11,96+80(%rsp)
  979. movaps %xmm12,96+96(%rsp)
  980. movaps %xmm13,96+112(%rsp)
  981. movaps %xmm14,96+128(%rsp)
  982. movaps %xmm15,96+144(%rsp)
  983. .Lprologue_avx:
  984. ___
  985. $code.=<<___;
  986. vzeroall
  987. mov $in0,%r12 # reassign arguments
  988. mov $out,%r13
  989. mov $len,%r14
  990. lea 112($key),%r15 # size optimization
  991. vmovdqu ($ivp),$iv # load IV
  992. mov $ivp,88(%rsp) # save $ivp
  993. ___
  994. ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
  995. my $rounds="${ivp}d";
  996. $code.=<<___;
  997. shl \$6,$len
  998. sub $in0,$out
  999. mov 240-112($key),$rounds
  1000. add $inp,$len # end of input
  1001. lea K_XX_XX(%rip),$K_XX_XX
  1002. mov 0($ctx),$A # load context
  1003. mov 4($ctx),$B
  1004. mov 8($ctx),$C
  1005. mov 12($ctx),$D
  1006. mov $B,@T[0] # magic seed
  1007. mov 16($ctx),$E
  1008. mov $C,@T[1]
  1009. xor $D,@T[1]
  1010. and @T[1],@T[0]
  1011. vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
  1012. vmovdqa 0($K_XX_XX),$Kx # K_00_19
  1013. vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
  1014. vmovdqu 16($inp),@X[-3&7]
  1015. vmovdqu 32($inp),@X[-2&7]
  1016. vmovdqu 48($inp),@X[-1&7]
  1017. vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
  1018. add \$64,$inp
  1019. vpshufb @X[2],@X[-3&7],@X[-3&7]
  1020. vpshufb @X[2],@X[-2&7],@X[-2&7]
  1021. vpshufb @X[2],@X[-1&7],@X[-1&7]
  1022. vpaddd $Kx,@X[-4&7],@X[0] # add K_00_19
  1023. vpaddd $Kx,@X[-3&7],@X[1]
  1024. vpaddd $Kx,@X[-2&7],@X[2]
  1025. vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
  1026. vmovdqa @X[1],16(%rsp)
  1027. vmovdqa @X[2],32(%rsp)
  1028. vmovups -112($key),$rndkey[1] # $key[0]
  1029. vmovups 16-112($key),$rndkey[0] # forward reference
  1030. jmp .Loop_avx
  1031. ___
  1032. my $aesenc=sub {
  1033. use integer;
  1034. my ($n,$k)=($r/10,$r%10);
  1035. if ($k==0) {
  1036. $code.=<<___;
  1037. vmovdqu `16*$n`($in0),$in # load input
  1038. vpxor $rndkey[1],$in,$in
  1039. ___
  1040. $code.=<<___ if ($n);
  1041. vmovups $iv,`16*($n-1)`($out,$in0) # write output
  1042. ___
  1043. $code.=<<___;
  1044. vpxor $in,$iv,$iv
  1045. vaesenc $rndkey[0],$iv,$iv
  1046. vmovups `32+16*$k-112`($key),$rndkey[1]
  1047. ___
  1048. } elsif ($k==9) {
  1049. $sn++;
  1050. $code.=<<___;
  1051. cmp \$11,$rounds
  1052. jb .Lvaesenclast$sn
  1053. vaesenc $rndkey[0],$iv,$iv
  1054. vmovups `32+16*($k+0)-112`($key),$rndkey[1]
  1055. vaesenc $rndkey[1],$iv,$iv
  1056. vmovups `32+16*($k+1)-112`($key),$rndkey[0]
  1057. je .Lvaesenclast$sn
  1058. vaesenc $rndkey[0],$iv,$iv
  1059. vmovups `32+16*($k+2)-112`($key),$rndkey[1]
  1060. vaesenc $rndkey[1],$iv,$iv
  1061. vmovups `32+16*($k+3)-112`($key),$rndkey[0]
  1062. .Lvaesenclast$sn:
  1063. vaesenclast $rndkey[0],$iv,$iv
  1064. vmovups -112($key),$rndkey[0]
  1065. vmovups 16-112($key),$rndkey[1] # forward reference
  1066. ___
  1067. } else {
  1068. $code.=<<___;
  1069. vaesenc $rndkey[0],$iv,$iv
  1070. vmovups `32+16*$k-112`($key),$rndkey[1]
  1071. ___
  1072. }
  1073. $r++; unshift(@rndkey,pop(@rndkey));
  1074. };
  1075. sub Xupdate_avx_16_31() # recall that $Xi starts with 4
  1076. { use integer;
  1077. my $body = shift;
  1078. my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
  1079. my ($a,$b,$c,$d,$e);
  1080. eval(shift(@insns));
  1081. eval(shift(@insns));
  1082. &vpalignr(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
  1083. eval(shift(@insns));
  1084. eval(shift(@insns));
  1085. &vpaddd (@Tx[1],$Kx,@X[-1&7]);
  1086. eval(shift(@insns));
  1087. eval(shift(@insns));
  1088. &vpsrldq(@Tx[0],@X[-1&7],4); # "X[-3]", 3 dwords
  1089. eval(shift(@insns));
  1090. eval(shift(@insns));
  1091. &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
  1092. eval(shift(@insns));
  1093. eval(shift(@insns));
  1094. &vpxor (@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
  1095. eval(shift(@insns));
  1096. eval(shift(@insns));
  1097. eval(shift(@insns));
  1098. eval(shift(@insns));
  1099. &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
  1100. eval(shift(@insns));
  1101. eval(shift(@insns));
  1102. &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  1103. eval(shift(@insns));
  1104. eval(shift(@insns));
  1105. &vpsrld (@Tx[0],@X[0],31);
  1106. eval(shift(@insns));
  1107. eval(shift(@insns));
  1108. eval(shift(@insns));
  1109. eval(shift(@insns));
  1110. &vpslldq(@Tx[1],@X[0],12); # "X[0]"<<96, extract one dword
  1111. &vpaddd (@X[0],@X[0],@X[0]);
  1112. eval(shift(@insns));
  1113. eval(shift(@insns));
  1114. eval(shift(@insns));
  1115. eval(shift(@insns));
  1116. &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=1
  1117. &vpsrld (@Tx[0],@Tx[1],30);
  1118. eval(shift(@insns));
  1119. eval(shift(@insns));
  1120. eval(shift(@insns));
  1121. eval(shift(@insns));
  1122. &vpslld (@Tx[1],@Tx[1],2);
  1123. &vpxor (@X[0],@X[0],@Tx[0]);
  1124. eval(shift(@insns));
  1125. eval(shift(@insns));
  1126. eval(shift(@insns));
  1127. eval(shift(@insns));
  1128. &vpxor (@X[0],@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
  1129. eval(shift(@insns));
  1130. eval(shift(@insns));
  1131. &vmovdqa ($Kx,eval(16*(($Xi)/5))."($K_XX_XX)") if ($Xi%5==0); # K_XX_XX
  1132. eval(shift(@insns));
  1133. eval(shift(@insns));
  1134. foreach (@insns) { eval; } # remaining instructions [if any]
  1135. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  1136. }
  1137. sub Xupdate_avx_32_79()
  1138. { use integer;
  1139. my $body = shift;
  1140. my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
  1141. my ($a,$b,$c,$d,$e);
  1142. &vpalignr(@Tx[0],@X[-1&7],@X[-2&7],8); # compose "X[-6]"
  1143. &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
  1144. eval(shift(@insns)); # body_20_39
  1145. eval(shift(@insns));
  1146. eval(shift(@insns));
  1147. eval(shift(@insns)); # rol
  1148. &vpxor (@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
  1149. eval(shift(@insns));
  1150. eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
  1151. &vpaddd (@Tx[1],$Kx,@X[-1&7]);
  1152. &vmovdqa ($Kx,eval(16*($Xi/5))."($K_XX_XX)") if ($Xi%5==0);
  1153. eval(shift(@insns)); # ror
  1154. eval(shift(@insns));
  1155. &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-6]"
  1156. eval(shift(@insns)); # body_20_39
  1157. eval(shift(@insns));
  1158. eval(shift(@insns));
  1159. eval(shift(@insns)); # rol
  1160. &vpsrld (@Tx[0],@X[0],30);
  1161. &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
  1162. eval(shift(@insns));
  1163. eval(shift(@insns));
  1164. eval(shift(@insns)); # ror
  1165. eval(shift(@insns));
  1166. &vpslld (@X[0],@X[0],2);
  1167. eval(shift(@insns)); # body_20_39
  1168. eval(shift(@insns));
  1169. eval(shift(@insns));
  1170. eval(shift(@insns)); # rol
  1171. eval(shift(@insns));
  1172. eval(shift(@insns));
  1173. eval(shift(@insns)); # ror
  1174. eval(shift(@insns));
  1175. &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=2
  1176. eval(shift(@insns)); # body_20_39
  1177. eval(shift(@insns));
  1178. eval(shift(@insns));
  1179. eval(shift(@insns)); # rol
  1180. eval(shift(@insns));
  1181. eval(shift(@insns));
  1182. eval(shift(@insns)); # rol
  1183. eval(shift(@insns));
  1184. foreach (@insns) { eval; } # remaining instructions
  1185. $Xi++; push(@X,shift(@X)); # "rotate" X[]
  1186. }
  1187. sub Xuplast_avx_80()
  1188. { use integer;
  1189. my $body = shift;
  1190. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  1191. my ($a,$b,$c,$d,$e);
  1192. eval(shift(@insns));
  1193. &vpaddd (@Tx[1],$Kx,@X[-1&7]);
  1194. eval(shift(@insns));
  1195. eval(shift(@insns));
  1196. eval(shift(@insns));
  1197. eval(shift(@insns));
  1198. &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
  1199. foreach (@insns) { eval; } # remaining instructions
  1200. &cmp ($inp,$len);
  1201. &je (shift);
  1202. &vmovdqa(@Tx[1],"64($K_XX_XX)"); # pbswap mask
  1203. &vmovdqa($Kx,"0($K_XX_XX)"); # K_00_19
  1204. &vmovdqu(@X[-4&7],"0($inp)"); # load input
  1205. &vmovdqu(@X[-3&7],"16($inp)");
  1206. &vmovdqu(@X[-2&7],"32($inp)");
  1207. &vmovdqu(@X[-1&7],"48($inp)");
  1208. &vpshufb(@X[-4&7],@X[-4&7],@Tx[1]); # byte swap
  1209. &add ($inp,64);
  1210. $Xi=0;
  1211. }
  1212. sub Xloop_avx()
  1213. { use integer;
  1214. my $body = shift;
  1215. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  1216. my ($a,$b,$c,$d,$e);
  1217. eval(shift(@insns));
  1218. eval(shift(@insns));
  1219. &vpshufb(@X[($Xi-3)&7],@X[($Xi-3)&7],@Tx[1]);
  1220. eval(shift(@insns));
  1221. eval(shift(@insns));
  1222. &vpaddd (@Tx[0],@X[($Xi-4)&7],$Kx);
  1223. eval(shift(@insns));
  1224. eval(shift(@insns));
  1225. eval(shift(@insns));
  1226. eval(shift(@insns));
  1227. &vmovdqa(eval(16*$Xi)."(%rsp)",@Tx[0]); # X[]+K xfer to IALU
  1228. eval(shift(@insns));
  1229. eval(shift(@insns));
  1230. foreach (@insns) { eval; }
  1231. $Xi++;
  1232. }
  1233. sub Xtail_avx()
  1234. { use integer;
  1235. my $body = shift;
  1236. my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
  1237. my ($a,$b,$c,$d,$e);
  1238. foreach (@insns) { eval; }
  1239. }
  1240. $code.=<<___;
  1241. .align 32
  1242. .Loop_avx:
  1243. ___
  1244. &Xupdate_avx_16_31(\&body_00_19);
  1245. &Xupdate_avx_16_31(\&body_00_19);
  1246. &Xupdate_avx_16_31(\&body_00_19);
  1247. &Xupdate_avx_16_31(\&body_00_19);
  1248. &Xupdate_avx_32_79(\&body_00_19);
  1249. &Xupdate_avx_32_79(\&body_20_39);
  1250. &Xupdate_avx_32_79(\&body_20_39);
  1251. &Xupdate_avx_32_79(\&body_20_39);
  1252. &Xupdate_avx_32_79(\&body_20_39);
  1253. &Xupdate_avx_32_79(\&body_20_39);
  1254. &Xupdate_avx_32_79(\&body_40_59);
  1255. &Xupdate_avx_32_79(\&body_40_59);
  1256. &Xupdate_avx_32_79(\&body_40_59);
  1257. &Xupdate_avx_32_79(\&body_40_59);
  1258. &Xupdate_avx_32_79(\&body_40_59);
  1259. &Xupdate_avx_32_79(\&body_20_39);
  1260. &Xuplast_avx_80(\&body_20_39,".Ldone_avx"); # can jump to "done"
  1261. $saved_j=$j; @saved_V=@V;
  1262. $saved_r=$r; @saved_rndkey=@rndkey;
  1263. &Xloop_avx(\&body_20_39);
  1264. &Xloop_avx(\&body_20_39);
  1265. &Xloop_avx(\&body_20_39);
  1266. $code.=<<___;
  1267. vmovups $iv,48($out,$in0) # write output
  1268. lea 64($in0),$in0
  1269. add 0($ctx),$A # update context
  1270. add 4($ctx),@T[0]
  1271. add 8($ctx),$C
  1272. add 12($ctx),$D
  1273. mov $A,0($ctx)
  1274. add 16($ctx),$E
  1275. mov @T[0],4($ctx)
  1276. mov @T[0],$B # magic seed
  1277. mov $C,8($ctx)
  1278. mov $C,@T[1]
  1279. mov $D,12($ctx)
  1280. xor $D,@T[1]
  1281. mov $E,16($ctx)
  1282. and @T[1],@T[0]
  1283. jmp .Loop_avx
  1284. .Ldone_avx:
  1285. ___
  1286. $jj=$j=$saved_j; @V=@saved_V;
  1287. $r=$saved_r; @rndkey=@saved_rndkey;
  1288. &Xtail_avx(\&body_20_39);
  1289. &Xtail_avx(\&body_20_39);
  1290. &Xtail_avx(\&body_20_39);
  1291. $code.=<<___;
  1292. vmovups $iv,48($out,$in0) # write output
  1293. mov 88(%rsp),$ivp # restore $ivp
  1294. add 0($ctx),$A # update context
  1295. add 4($ctx),@T[0]
  1296. add 8($ctx),$C
  1297. mov $A,0($ctx)
  1298. add 12($ctx),$D
  1299. mov @T[0],4($ctx)
  1300. add 16($ctx),$E
  1301. mov $C,8($ctx)
  1302. mov $D,12($ctx)
  1303. mov $E,16($ctx)
  1304. vmovups $iv,($ivp) # write IV
  1305. vzeroall
  1306. ___
  1307. $code.=<<___ if ($win64);
  1308. movaps 96+0(%rsp),%xmm6
  1309. movaps 96+16(%rsp),%xmm7
  1310. movaps 96+32(%rsp),%xmm8
  1311. movaps 96+48(%rsp),%xmm9
  1312. movaps 96+64(%rsp),%xmm10
  1313. movaps 96+80(%rsp),%xmm11
  1314. movaps 96+96(%rsp),%xmm12
  1315. movaps 96+112(%rsp),%xmm13
  1316. movaps 96+128(%rsp),%xmm14
  1317. movaps 96+144(%rsp),%xmm15
  1318. ___
  1319. $code.=<<___;
  1320. lea `104+($win64?10*16:0)`(%rsp),%rsi
  1321. .cfi_def_cfa %rsi,56
  1322. mov 0(%rsi),%r15
  1323. .cfi_restore %r15
  1324. mov 8(%rsi),%r14
  1325. .cfi_restore %r14
  1326. mov 16(%rsi),%r13
  1327. .cfi_restore %r13
  1328. mov 24(%rsi),%r12
  1329. .cfi_restore %r12
  1330. mov 32(%rsi),%rbp
  1331. .cfi_restore %rbp
  1332. mov 40(%rsi),%rbx
  1333. .cfi_restore %rbx
  1334. lea 48(%rsi),%rsp
  1335. .cfi_def_cfa %rsp,8
  1336. .Lepilogue_avx:
  1337. ret
  1338. .cfi_endproc
  1339. .size aesni_cbc_sha1_enc_avx,.-aesni_cbc_sha1_enc_avx
  1340. ___
  1341. if ($stitched_decrypt) {{{
  1342. # reset
  1343. ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
  1344. $j=$jj=$r=$rx=0;
  1345. $Xi=4;
  1346. @aes256_dec = (
  1347. '&vpxor ($inout0,$rndkey0,"0x00($in0)");',
  1348. '&vpxor ($inout1,$rndkey0,"0x10($in0)");',
  1349. '&vpxor ($inout2,$rndkey0,"0x20($in0)");',
  1350. '&vpxor ($inout3,$rndkey0,"0x30($in0)");',
  1351. '&vmovups($rndkey0,"16-112($key)");',
  1352. '&vmovups("64(%rsp)",@X[2]);', # save IV, originally @X[3]
  1353. undef,undef
  1354. );
  1355. for ($i=0;$i<13;$i++) {
  1356. push (@aes256_dec,(
  1357. '&vaesdec ($inout0,$inout0,$rndkey0);',
  1358. '&vaesdec ($inout1,$inout1,$rndkey0);',
  1359. '&vaesdec ($inout2,$inout2,$rndkey0);',
  1360. '&vaesdec ($inout3,$inout3,$rndkey0); &vmovups($rndkey0,"'.(16*($i+2)-112).'($key)");'
  1361. ));
  1362. push (@aes256_dec,(undef,undef)) if (($i>=3 && $i<=5) || $i>=11);
  1363. push (@aes256_dec,(undef,undef)) if ($i==5);
  1364. }
  1365. push(@aes256_dec,(
  1366. '&vaesdeclast ($inout0,$inout0,$rndkey0); &vmovups(@X[0],"0x00($in0)");',
  1367. '&vaesdeclast ($inout1,$inout1,$rndkey0); &vmovups(@X[1],"0x10($in0)");',
  1368. '&vaesdeclast ($inout2,$inout2,$rndkey0); &vmovups(@X[2],"0x20($in0)");',
  1369. '&vaesdeclast ($inout3,$inout3,$rndkey0); &vmovups(@X[3],"0x30($in0)");',
  1370. '&vxorps ($inout0,$inout0,"64(%rsp)"); &vmovdqu($rndkey0,"-112($key)");',
  1371. '&vxorps ($inout1,$inout1,@X[0]); &vmovups("0x00($out,$in0)",$inout0);',
  1372. '&vxorps ($inout2,$inout2,@X[1]); &vmovups("0x10($out,$in0)",$inout1);',
  1373. '&vxorps ($inout3,$inout3,@X[2]); &vmovups("0x20($out,$in0)",$inout2);',
  1374. '&vmovups ("0x30($out,$in0)",$inout3);'
  1375. ));
  1376. $code.=<<___;
  1377. .type aesni256_cbc_sha1_dec_avx,\@function,6
  1378. .align 32
  1379. aesni256_cbc_sha1_dec_avx:
  1380. .cfi_startproc
  1381. mov `($win64?56:8)`(%rsp),$inp # load 7th argument
  1382. push %rbx
  1383. .cfi_push %rbx
  1384. push %rbp
  1385. .cfi_push %rbp
  1386. push %r12
  1387. .cfi_push %r12
  1388. push %r13
  1389. .cfi_push %r13
  1390. push %r14
  1391. .cfi_push %r14
  1392. push %r15
  1393. .cfi_push %r15
  1394. lea `-104-($win64?10*16:0)`(%rsp),%rsp
  1395. .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
  1396. ___
  1397. $code.=<<___ if ($win64);
  1398. movaps %xmm6,96+0(%rsp)
  1399. movaps %xmm7,96+16(%rsp)
  1400. movaps %xmm8,96+32(%rsp)
  1401. movaps %xmm9,96+48(%rsp)
  1402. movaps %xmm10,96+64(%rsp)
  1403. movaps %xmm11,96+80(%rsp)
  1404. movaps %xmm12,96+96(%rsp)
  1405. movaps %xmm13,96+112(%rsp)
  1406. movaps %xmm14,96+128(%rsp)
  1407. movaps %xmm15,96+144(%rsp)
  1408. .Lprologue_dec_avx:
  1409. ___
  1410. $code.=<<___;
  1411. vzeroall
  1412. mov $in0,%r12 # reassign arguments
  1413. mov $out,%r13
  1414. mov $len,%r14
  1415. lea 112($key),%r15 # size optimization
  1416. vmovdqu ($ivp),@X[3] # load IV
  1417. ___
  1418. ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
  1419. $code.=<<___;
  1420. shl \$6,$len
  1421. sub $in0,$out
  1422. add $inp,$len # end of input
  1423. lea K_XX_XX(%rip),$K_XX_XX
  1424. mov 0($ctx),$A # load context
  1425. mov 4($ctx),$B
  1426. mov 8($ctx),$C
  1427. mov 12($ctx),$D
  1428. mov $B,@T[0] # magic seed
  1429. mov 16($ctx),$E
  1430. mov $C,@T[1]
  1431. xor $D,@T[1]
  1432. and @T[1],@T[0]
  1433. vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
  1434. vmovdqa 0($K_XX_XX),$Kx # K_00_19
  1435. vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
  1436. vmovdqu 16($inp),@X[-3&7]
  1437. vmovdqu 32($inp),@X[-2&7]
  1438. vmovdqu 48($inp),@X[-1&7]
  1439. vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
  1440. add \$64,$inp
  1441. vpshufb @X[2],@X[-3&7],@X[-3&7]
  1442. vpshufb @X[2],@X[-2&7],@X[-2&7]
  1443. vpshufb @X[2],@X[-1&7],@X[-1&7]
  1444. vpaddd $Kx,@X[-4&7],@X[0] # add K_00_19
  1445. vpaddd $Kx,@X[-3&7],@X[1]
  1446. vpaddd $Kx,@X[-2&7],@X[2]
  1447. vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
  1448. vmovdqa @X[1],16(%rsp)
  1449. vmovdqa @X[2],32(%rsp)
  1450. vmovups -112($key),$rndkey0 # $key[0]
  1451. jmp .Loop_dec_avx
  1452. .align 32
  1453. .Loop_dec_avx:
  1454. ___
  1455. &Xupdate_avx_16_31(\&body_00_19_dec);
  1456. &Xupdate_avx_16_31(\&body_00_19_dec);
  1457. &Xupdate_avx_16_31(\&body_00_19_dec);
  1458. &Xupdate_avx_16_31(\&body_00_19_dec);
  1459. &Xupdate_avx_32_79(\&body_00_19_dec);
  1460. &Xupdate_avx_32_79(\&body_20_39_dec);
  1461. &Xupdate_avx_32_79(\&body_20_39_dec);
  1462. &Xupdate_avx_32_79(\&body_20_39_dec);
  1463. &Xupdate_avx_32_79(\&body_20_39_dec);
  1464. &Xupdate_avx_32_79(\&body_20_39_dec);
  1465. &Xupdate_avx_32_79(\&body_40_59_dec);
  1466. &Xupdate_avx_32_79(\&body_40_59_dec);
  1467. &Xupdate_avx_32_79(\&body_40_59_dec);
  1468. &Xupdate_avx_32_79(\&body_40_59_dec);
  1469. &Xupdate_avx_32_79(\&body_40_59_dec);
  1470. &Xupdate_avx_32_79(\&body_20_39_dec);
  1471. &Xuplast_avx_80(\&body_20_39_dec,".Ldone_dec_avx"); # can jump to "done"
  1472. $saved_j=$j; @saved_V=@V;
  1473. $saved_rx=$rx;
  1474. &Xloop_avx(\&body_20_39_dec);
  1475. &Xloop_avx(\&body_20_39_dec);
  1476. &Xloop_avx(\&body_20_39_dec);
  1477. eval(@aes256_dec[-1]); # last store
  1478. $code.=<<___;
  1479. lea 64($in0),$in0
  1480. add 0($ctx),$A # update context
  1481. add 4($ctx),@T[0]
  1482. add 8($ctx),$C
  1483. add 12($ctx),$D
  1484. mov $A,0($ctx)
  1485. add 16($ctx),$E
  1486. mov @T[0],4($ctx)
  1487. mov @T[0],$B # magic seed
  1488. mov $C,8($ctx)
  1489. mov $C,@T[1]
  1490. mov $D,12($ctx)
  1491. xor $D,@T[1]
  1492. mov $E,16($ctx)
  1493. and @T[1],@T[0]
  1494. jmp .Loop_dec_avx
  1495. .Ldone_dec_avx:
  1496. ___
  1497. $jj=$j=$saved_j; @V=@saved_V;
  1498. $rx=$saved_rx;
  1499. &Xtail_avx(\&body_20_39_dec);
  1500. &Xtail_avx(\&body_20_39_dec);
  1501. &Xtail_avx(\&body_20_39_dec);
  1502. eval(@aes256_dec[-1]); # last store
  1503. $code.=<<___;
  1504. add 0($ctx),$A # update context
  1505. add 4($ctx),@T[0]
  1506. add 8($ctx),$C
  1507. mov $A,0($ctx)
  1508. add 12($ctx),$D
  1509. mov @T[0],4($ctx)
  1510. add 16($ctx),$E
  1511. mov $C,8($ctx)
  1512. mov $D,12($ctx)
  1513. mov $E,16($ctx)
  1514. vmovups @X[3],($ivp) # write IV
  1515. vzeroall
  1516. ___
  1517. $code.=<<___ if ($win64);
  1518. movaps 96+0(%rsp),%xmm6
  1519. movaps 96+16(%rsp),%xmm7
  1520. movaps 96+32(%rsp),%xmm8
  1521. movaps 96+48(%rsp),%xmm9
  1522. movaps 96+64(%rsp),%xmm10
  1523. movaps 96+80(%rsp),%xmm11
  1524. movaps 96+96(%rsp),%xmm12
  1525. movaps 96+112(%rsp),%xmm13
  1526. movaps 96+128(%rsp),%xmm14
  1527. movaps 96+144(%rsp),%xmm15
  1528. ___
  1529. $code.=<<___;
  1530. lea `104+($win64?10*16:0)`(%rsp),%rsi
  1531. .cfi_def_cfa %rsi,56
  1532. mov 0(%rsi),%r15
  1533. .cfi_restore %r15
  1534. mov 8(%rsi),%r14
  1535. .cfi_restore %r14
  1536. mov 16(%rsi),%r13
  1537. .cfi_restore %r13
  1538. mov 24(%rsi),%r12
  1539. .cfi_restore %r12
  1540. mov 32(%rsi),%rbp
  1541. .cfi_restore %rbp
  1542. mov 40(%rsi),%rbx
  1543. .cfi_restore %rbx
  1544. lea 48(%rsi),%rsp
  1545. .cfi_def_cfa %rsp,8
  1546. .Lepilogue_dec_avx:
  1547. ret
  1548. .cfi_endproc
  1549. .size aesni256_cbc_sha1_dec_avx,.-aesni256_cbc_sha1_dec_avx
  1550. ___
  1551. }}}
  1552. }
  1553. $code.=<<___;
  1554. .align 64
  1555. K_XX_XX:
  1556. .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
  1557. .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
  1558. .long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
  1559. .long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
  1560. .long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap mask
  1561. .byte 0xf,0xe,0xd,0xc,0xb,0xa,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0
  1562. .asciz "AESNI-CBC+SHA1 stitch for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
  1563. .align 64
  1564. ___
  1565. if ($shaext) {{{
  1566. ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
  1567. $rounds="%r11d";
  1568. ($iv,$in,$rndkey0)=map("%xmm$_",(2,14,15));
  1569. @rndkey=("%xmm0","%xmm1");
  1570. $r=0;
  1571. my ($BSWAP,$ABCD,$E,$E_,$ABCD_SAVE,$E_SAVE)=map("%xmm$_",(7..12));
  1572. my @MSG=map("%xmm$_",(3..6));
  1573. $code.=<<___;
  1574. .type aesni_cbc_sha1_enc_shaext,\@function,6
  1575. .align 32
  1576. aesni_cbc_sha1_enc_shaext:
  1577. .cfi_startproc
  1578. mov `($win64?56:8)`(%rsp),$inp # load 7th argument
  1579. ___
  1580. $code.=<<___ if ($win64);
  1581. lea `-8-10*16`(%rsp),%rsp
  1582. movaps %xmm6,-8-10*16(%rax)
  1583. movaps %xmm7,-8-9*16(%rax)
  1584. movaps %xmm8,-8-8*16(%rax)
  1585. movaps %xmm9,-8-7*16(%rax)
  1586. movaps %xmm10,-8-6*16(%rax)
  1587. movaps %xmm11,-8-5*16(%rax)
  1588. movaps %xmm12,-8-4*16(%rax)
  1589. movaps %xmm13,-8-3*16(%rax)
  1590. movaps %xmm14,-8-2*16(%rax)
  1591. movaps %xmm15,-8-1*16(%rax)
  1592. .Lprologue_shaext:
  1593. ___
  1594. $code.=<<___;
  1595. movdqu ($ctx),$ABCD
  1596. movd 16($ctx),$E
  1597. movdqa K_XX_XX+0x50(%rip),$BSWAP # byte-n-word swap
  1598. mov 240($key),$rounds
  1599. sub $in0,$out
  1600. movups ($key),$rndkey0 # $key[0]
  1601. movups ($ivp),$iv # load IV
  1602. movups 16($key),$rndkey[0] # forward reference
  1603. lea 112($key),$key # size optimization
  1604. pshufd \$0b00011011,$ABCD,$ABCD # flip word order
  1605. pshufd \$0b00011011,$E,$E # flip word order
  1606. jmp .Loop_shaext
  1607. .align 16
  1608. .Loop_shaext:
  1609. ___
  1610. &$aesenc();
  1611. $code.=<<___;
  1612. movdqu ($inp),@MSG[0]
  1613. movdqa $E,$E_SAVE # offload $E
  1614. pshufb $BSWAP,@MSG[0]
  1615. movdqu 0x10($inp),@MSG[1]
  1616. movdqa $ABCD,$ABCD_SAVE # offload $ABCD
  1617. ___
  1618. &$aesenc();
  1619. $code.=<<___;
  1620. pshufb $BSWAP,@MSG[1]
  1621. paddd @MSG[0],$E
  1622. movdqu 0x20($inp),@MSG[2]
  1623. lea 0x40($inp),$inp
  1624. pxor $E_SAVE,@MSG[0] # black magic
  1625. ___
  1626. &$aesenc();
  1627. $code.=<<___;
  1628. pxor $E_SAVE,@MSG[0] # black magic
  1629. movdqa $ABCD,$E_
  1630. pshufb $BSWAP,@MSG[2]
  1631. sha1rnds4 \$0,$E,$ABCD # 0-3
  1632. sha1nexte @MSG[1],$E_
  1633. ___
  1634. &$aesenc();
  1635. $code.=<<___;
  1636. sha1msg1 @MSG[1],@MSG[0]
  1637. movdqu -0x10($inp),@MSG[3]
  1638. movdqa $ABCD,$E
  1639. pshufb $BSWAP,@MSG[3]
  1640. ___
  1641. &$aesenc();
  1642. $code.=<<___;
  1643. sha1rnds4 \$0,$E_,$ABCD # 4-7
  1644. sha1nexte @MSG[2],$E
  1645. pxor @MSG[2],@MSG[0]
  1646. sha1msg1 @MSG[2],@MSG[1]
  1647. ___
  1648. &$aesenc();
  1649. for($i=2;$i<20-4;$i++) {
  1650. $code.=<<___;
  1651. movdqa $ABCD,$E_
  1652. sha1rnds4 \$`int($i/5)`,$E,$ABCD # 8-11
  1653. sha1nexte @MSG[3],$E_
  1654. ___
  1655. &$aesenc();
  1656. $code.=<<___;
  1657. sha1msg2 @MSG[3],@MSG[0]
  1658. pxor @MSG[3],@MSG[1]
  1659. sha1msg1 @MSG[3],@MSG[2]
  1660. ___
  1661. ($E,$E_)=($E_,$E);
  1662. push(@MSG,shift(@MSG));
  1663. &$aesenc();
  1664. }
  1665. $code.=<<___;
  1666. movdqa $ABCD,$E_
  1667. sha1rnds4 \$3,$E,$ABCD # 64-67
  1668. sha1nexte @MSG[3],$E_
  1669. sha1msg2 @MSG[3],@MSG[0]
  1670. pxor @MSG[3],@MSG[1]
  1671. ___
  1672. &$aesenc();
  1673. $code.=<<___;
  1674. movdqa $ABCD,$E
  1675. sha1rnds4 \$3,$E_,$ABCD # 68-71
  1676. sha1nexte @MSG[0],$E
  1677. sha1msg2 @MSG[0],@MSG[1]
  1678. ___
  1679. &$aesenc();
  1680. $code.=<<___;
  1681. movdqa $E_SAVE,@MSG[0]
  1682. movdqa $ABCD,$E_
  1683. sha1rnds4 \$3,$E,$ABCD # 72-75
  1684. sha1nexte @MSG[1],$E_
  1685. ___
  1686. &$aesenc();
  1687. $code.=<<___;
  1688. movdqa $ABCD,$E
  1689. sha1rnds4 \$3,$E_,$ABCD # 76-79
  1690. sha1nexte $MSG[0],$E
  1691. ___
  1692. while($r<40) { &$aesenc(); } # remaining aesenc's
  1693. $code.=<<___;
  1694. dec $len
  1695. paddd $ABCD_SAVE,$ABCD
  1696. movups $iv,48($out,$in0) # write output
  1697. lea 64($in0),$in0
  1698. jnz .Loop_shaext
  1699. pshufd \$0b00011011,$ABCD,$ABCD
  1700. pshufd \$0b00011011,$E,$E
  1701. movups $iv,($ivp) # write IV
  1702. movdqu $ABCD,($ctx)
  1703. movd $E,16($ctx)
  1704. ___
  1705. $code.=<<___ if ($win64);
  1706. movaps -8-10*16(%rax),%xmm6
  1707. movaps -8-9*16(%rax),%xmm7
  1708. movaps -8-8*16(%rax),%xmm8
  1709. movaps -8-7*16(%rax),%xmm9
  1710. movaps -8-6*16(%rax),%xmm10
  1711. movaps -8-5*16(%rax),%xmm11
  1712. movaps -8-4*16(%rax),%xmm12
  1713. movaps -8-3*16(%rax),%xmm13
  1714. movaps -8-2*16(%rax),%xmm14
  1715. movaps -8-1*16(%rax),%xmm15
  1716. mov %rax,%rsp
  1717. .Lepilogue_shaext:
  1718. ___
  1719. $code.=<<___;
  1720. ret
  1721. .cfi_endproc
  1722. .size aesni_cbc_sha1_enc_shaext,.-aesni_cbc_sha1_enc_shaext
  1723. ___
  1724. }}}
  1725. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  1726. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  1727. if ($win64) {
  1728. $rec="%rcx";
  1729. $frame="%rdx";
  1730. $context="%r8";
  1731. $disp="%r9";
  1732. $code.=<<___;
  1733. .extern __imp_RtlVirtualUnwind
  1734. .type ssse3_handler,\@abi-omnipotent
  1735. .align 16
  1736. ssse3_handler:
  1737. push %rsi
  1738. push %rdi
  1739. push %rbx
  1740. push %rbp
  1741. push %r12
  1742. push %r13
  1743. push %r14
  1744. push %r15
  1745. pushfq
  1746. sub \$64,%rsp
  1747. mov 120($context),%rax # pull context->Rax
  1748. mov 248($context),%rbx # pull context->Rip
  1749. mov 8($disp),%rsi # disp->ImageBase
  1750. mov 56($disp),%r11 # disp->HandlerData
  1751. mov 0(%r11),%r10d # HandlerData[0]
  1752. lea (%rsi,%r10),%r10 # prologue label
  1753. cmp %r10,%rbx # context->Rip<prologue label
  1754. jb .Lcommon_seh_tail
  1755. mov 152($context),%rax # pull context->Rsp
  1756. mov 4(%r11),%r10d # HandlerData[1]
  1757. lea (%rsi,%r10),%r10 # epilogue label
  1758. cmp %r10,%rbx # context->Rip>=epilogue label
  1759. jae .Lcommon_seh_tail
  1760. ___
  1761. $code.=<<___ if ($shaext);
  1762. lea aesni_cbc_sha1_enc_shaext(%rip),%r10
  1763. cmp %r10,%rbx
  1764. jb .Lseh_no_shaext
  1765. lea (%rax),%rsi
  1766. lea 512($context),%rdi # &context.Xmm6
  1767. mov \$20,%ecx
  1768. .long 0xa548f3fc # cld; rep movsq
  1769. lea 168(%rax),%rax # adjust stack pointer
  1770. jmp .Lcommon_seh_tail
  1771. .Lseh_no_shaext:
  1772. ___
  1773. $code.=<<___;
  1774. lea 96(%rax),%rsi
  1775. lea 512($context),%rdi # &context.Xmm6
  1776. mov \$20,%ecx
  1777. .long 0xa548f3fc # cld; rep movsq
  1778. lea `104+10*16`(%rax),%rax # adjust stack pointer
  1779. mov 0(%rax),%r15
  1780. mov 8(%rax),%r14
  1781. mov 16(%rax),%r13
  1782. mov 24(%rax),%r12
  1783. mov 32(%rax),%rbp
  1784. mov 40(%rax),%rbx
  1785. lea 48(%rax),%rax
  1786. mov %rbx,144($context) # restore context->Rbx
  1787. mov %rbp,160($context) # restore context->Rbp
  1788. mov %r12,216($context) # restore context->R12
  1789. mov %r13,224($context) # restore context->R13
  1790. mov %r14,232($context) # restore context->R14
  1791. mov %r15,240($context) # restore context->R15
  1792. .Lcommon_seh_tail:
  1793. mov 8(%rax),%rdi
  1794. mov 16(%rax),%rsi
  1795. mov %rax,152($context) # restore context->Rsp
  1796. mov %rsi,168($context) # restore context->Rsi
  1797. mov %rdi,176($context) # restore context->Rdi
  1798. mov 40($disp),%rdi # disp->ContextRecord
  1799. mov $context,%rsi # context
  1800. mov \$154,%ecx # sizeof(CONTEXT)
  1801. .long 0xa548f3fc # cld; rep movsq
  1802. mov $disp,%rsi
  1803. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  1804. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  1805. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  1806. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  1807. mov 40(%rsi),%r10 # disp->ContextRecord
  1808. lea 56(%rsi),%r11 # &disp->HandlerData
  1809. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  1810. mov %r10,32(%rsp) # arg5
  1811. mov %r11,40(%rsp) # arg6
  1812. mov %r12,48(%rsp) # arg7
  1813. mov %rcx,56(%rsp) # arg8, (NULL)
  1814. call *__imp_RtlVirtualUnwind(%rip)
  1815. mov \$1,%eax # ExceptionContinueSearch
  1816. add \$64,%rsp
  1817. popfq
  1818. pop %r15
  1819. pop %r14
  1820. pop %r13
  1821. pop %r12
  1822. pop %rbp
  1823. pop %rbx
  1824. pop %rdi
  1825. pop %rsi
  1826. ret
  1827. .size ssse3_handler,.-ssse3_handler
  1828. .section .pdata
  1829. .align 4
  1830. .rva .LSEH_begin_aesni_cbc_sha1_enc_ssse3
  1831. .rva .LSEH_end_aesni_cbc_sha1_enc_ssse3
  1832. .rva .LSEH_info_aesni_cbc_sha1_enc_ssse3
  1833. ___
  1834. $code.=<<___ if ($avx);
  1835. .rva .LSEH_begin_aesni_cbc_sha1_enc_avx
  1836. .rva .LSEH_end_aesni_cbc_sha1_enc_avx
  1837. .rva .LSEH_info_aesni_cbc_sha1_enc_avx
  1838. ___
  1839. $code.=<<___ if ($shaext);
  1840. .rva .LSEH_begin_aesni_cbc_sha1_enc_shaext
  1841. .rva .LSEH_end_aesni_cbc_sha1_enc_shaext
  1842. .rva .LSEH_info_aesni_cbc_sha1_enc_shaext
  1843. ___
  1844. $code.=<<___;
  1845. .section .xdata
  1846. .align 8
  1847. .LSEH_info_aesni_cbc_sha1_enc_ssse3:
  1848. .byte 9,0,0,0
  1849. .rva ssse3_handler
  1850. .rva .Lprologue_ssse3,.Lepilogue_ssse3 # HandlerData[]
  1851. ___
  1852. $code.=<<___ if ($avx);
  1853. .LSEH_info_aesni_cbc_sha1_enc_avx:
  1854. .byte 9,0,0,0
  1855. .rva ssse3_handler
  1856. .rva .Lprologue_avx,.Lepilogue_avx # HandlerData[]
  1857. ___
  1858. $code.=<<___ if ($shaext);
  1859. .LSEH_info_aesni_cbc_sha1_enc_shaext:
  1860. .byte 9,0,0,0
  1861. .rva ssse3_handler
  1862. .rva .Lprologue_shaext,.Lepilogue_shaext # HandlerData[]
  1863. ___
  1864. }
  1865. ####################################################################
  1866. sub rex {
  1867. local *opcode=shift;
  1868. my ($dst,$src)=@_;
  1869. my $rex=0;
  1870. $rex|=0x04 if($dst>=8);
  1871. $rex|=0x01 if($src>=8);
  1872. unshift @opcode,$rex|0x40 if($rex);
  1873. }
  1874. sub sha1rnds4 {
  1875. if (@_[0] =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  1876. my @opcode=(0x0f,0x3a,0xcc);
  1877. rex(\@opcode,$3,$2);
  1878. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  1879. my $c=$1;
  1880. push @opcode,$c=~/^0/?oct($c):$c;
  1881. return ".byte\t".join(',',@opcode);
  1882. } else {
  1883. return "sha1rnds4\t".@_[0];
  1884. }
  1885. }
  1886. sub sha1op38 {
  1887. my $instr = shift;
  1888. my %opcodelet = (
  1889. "sha1nexte" => 0xc8,
  1890. "sha1msg1" => 0xc9,
  1891. "sha1msg2" => 0xca );
  1892. if (defined($opcodelet{$instr}) && @_[0] =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  1893. my @opcode=(0x0f,0x38);
  1894. rex(\@opcode,$2,$1);
  1895. push @opcode,$opcodelet{$instr};
  1896. push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
  1897. return ".byte\t".join(',',@opcode);
  1898. } else {
  1899. return $instr."\t".@_[0];
  1900. }
  1901. }
  1902. sub aesni {
  1903. my $line=shift;
  1904. my @opcode=(0x0f,0x38);
  1905. if ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  1906. my %opcodelet = (
  1907. "aesenc" => 0xdc, "aesenclast" => 0xdd,
  1908. "aesdec" => 0xde, "aesdeclast" => 0xdf
  1909. );
  1910. return undef if (!defined($opcodelet{$1}));
  1911. rex(\@opcode,$3,$2);
  1912. push @opcode,$opcodelet{$1},0xc0|($2&7)|(($3&7)<<3); # ModR/M
  1913. unshift @opcode,0x66;
  1914. return ".byte\t".join(',',@opcode);
  1915. }
  1916. return $line;
  1917. }
  1918. foreach (split("\n",$code)) {
  1919. s/\`([^\`]*)\`/eval $1/geo;
  1920. s/\b(sha1rnds4)\s+(.*)/sha1rnds4($2)/geo or
  1921. s/\b(sha1[^\s]*)\s+(.*)/sha1op38($1,$2)/geo or
  1922. s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/geo;
  1923. print $_,"\n";
  1924. }
  1925. close STDOUT or die "error closing STDOUT: $!";