aesni-sha1-x86_64.pl 53 KB

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