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