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