aesni-x86_64.pl: make it possibel to use in Linux kernel.
[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="%xmm6";
825 my $bswap_mask="%xmm7";
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         mov     240($key),$rounds               # key->rounds
843         movdqu  ($ivp),$iv
844         movdqa  .Lincrement64(%rip),$increment
845         movdqa  .Lbswap_mask(%rip),$bswap_mask
846
847         shr     \$1,$rounds
848         lea     0($key),$key_
849         movdqu  ($cmac),$inout1
850         movdqa  $iv,$inout0
851         mov     $rounds,$rnds_
852         pshufb  $bswap_mask,$iv
853         jmp     .Lccm64_enc_outer
854 .align  16
855 .Lccm64_enc_outer:
856         $movkey ($key_),$rndkey0
857         mov     $rnds_,$rounds
858         movups  ($inp),$in0                     # load inp
859
860         xorps   $rndkey0,$inout0                # counter
861         $movkey 16($key_),$rndkey1
862         xorps   $in0,$rndkey0
863         lea     32($key_),$key
864         xorps   $rndkey0,$inout1                # cmac^=inp
865         $movkey ($key),$rndkey0
866
867 .Lccm64_enc2_loop:
868         aesenc  $rndkey1,$inout0
869         dec     $rounds
870         aesenc  $rndkey1,$inout1
871         $movkey 16($key),$rndkey1
872         aesenc  $rndkey0,$inout0
873         lea     32($key),$key
874         aesenc  $rndkey0,$inout1
875         $movkey 0($key),$rndkey0
876         jnz     .Lccm64_enc2_loop
877         aesenc  $rndkey1,$inout0
878         aesenc  $rndkey1,$inout1
879         paddq   $increment,$iv
880         aesenclast      $rndkey0,$inout0
881         aesenclast      $rndkey0,$inout1
882
883         dec     $len
884         lea     16($inp),$inp
885         xorps   $inout0,$in0                    # inp ^= E(iv)
886         movdqa  $iv,$inout0
887         movups  $in0,($out)                     # save output
888         lea     16($out),$out
889         pshufb  $bswap_mask,$inout0
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         mov     240($key),$rounds               # key->rounds
923         movups  ($ivp),$iv
924         movdqu  ($cmac),$inout1
925         movdqa  .Lincrement64(%rip),$increment
926         movdqa  .Lbswap_mask(%rip),$bswap_mask
927
928         movaps  $iv,$inout0
929         mov     $rounds,$rnds_
930         mov     $key,$key_
931         pshufb  $bswap_mask,$iv
932 ___
933         &aesni_generate1("enc",$key,$rounds);
934 $code.=<<___;
935         movups  ($inp),$in0                     # load inp
936         paddq   $increment,$iv
937         lea     16($inp),$inp
938         jmp     .Lccm64_dec_outer
939 .align  16
940 .Lccm64_dec_outer:
941         xorps   $inout0,$in0                    # inp ^= E(iv)
942         movdqa  $iv,$inout0
943         mov     $rnds_,$rounds
944         movups  $in0,($out)                     # save output
945         lea     16($out),$out
946         pshufb  $bswap_mask,$inout0
947
948         sub     \$1,$len
949         jz      .Lccm64_dec_break
950
951         $movkey ($key_),$rndkey0
952         shr     \$1,$rounds
953         $movkey 16($key_),$rndkey1
954         xorps   $rndkey0,$in0
955         lea     32($key_),$key
956         xorps   $rndkey0,$inout0
957         xorps   $in0,$inout1                    # cmac^=out
958         $movkey ($key),$rndkey0
959
960 .Lccm64_dec2_loop:
961         aesenc  $rndkey1,$inout0
962         dec     $rounds
963         aesenc  $rndkey1,$inout1
964         $movkey 16($key),$rndkey1
965         aesenc  $rndkey0,$inout0
966         lea     32($key),$key
967         aesenc  $rndkey0,$inout1
968         $movkey 0($key),$rndkey0
969         jnz     .Lccm64_dec2_loop
970         movups  ($inp),$in0                     # load inp
971         paddq   $increment,$iv
972         aesenc  $rndkey1,$inout0
973         aesenc  $rndkey1,$inout1
974         lea     16($inp),$inp
975         aesenclast      $rndkey0,$inout0
976         aesenclast      $rndkey0,$inout1
977         jmp     .Lccm64_dec_outer
978
979 .align  16
980 .Lccm64_dec_break:
981         #xorps  $in0,$inout1                    # cmac^=out
982 ___
983         &aesni_generate1("enc",$key_,$rounds,$inout1,$in0);
984 $code.=<<___;
985         movups  $inout1,($cmac)
986 ___
987 $code.=<<___ if ($win64);
988         movaps  (%rsp),%xmm6
989         movaps  0x10(%rsp),%xmm7
990         movaps  0x20(%rsp),%xmm8
991         movaps  0x30(%rsp),%xmm9
992         lea     0x58(%rsp),%rsp
993 .Lccm64_dec_ret:
994 ___
995 $code.=<<___;
996         ret
997 .size   aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks
998 ___
999 }\f
1000 ######################################################################
1001 # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
1002 #                         size_t blocks, const AES_KEY *key,
1003 #                         const char *ivec);
1004 #
1005 # Handles only complete blocks, operates on 32-bit counter and
1006 # does not update *ivec! (see engine/eng_aesni.c for details)
1007 #
1008 {
1009 my $frame_size = 0x20+($win64?160:0);
1010 my ($in0,$in1,$in2,$in3)=map("%xmm$_",(8..11));
1011 my ($iv0,$iv1,$ivec)=("%xmm12","%xmm13","%xmm14");
1012 my $bswap_mask="%xmm15";
1013
1014 $code.=<<___;
1015 .globl  aesni_ctr32_encrypt_blocks
1016 .type   aesni_ctr32_encrypt_blocks,\@function,5
1017 .align  16
1018 aesni_ctr32_encrypt_blocks:
1019         lea     (%rsp),%rax
1020         push    %rbp
1021         sub     \$$frame_size,%rsp
1022         and     \$-16,%rsp      # Linux kernel stack can be incorrectly seeded
1023 ___
1024 $code.=<<___ if ($win64);
1025         movaps  %xmm6,0x20(%rsp)
1026         movaps  %xmm7,0x30(%rsp)
1027         movaps  %xmm8,0x40(%rsp)
1028         movaps  %xmm9,0x50(%rsp)
1029         movaps  %xmm10,0x60(%rsp)
1030         movaps  %xmm11,0x70(%rsp)
1031         movaps  %xmm12,0x80(%rsp)
1032         movaps  %xmm13,0x90(%rsp)
1033         movaps  %xmm14,0xa0(%rsp)
1034         movaps  %xmm15,0xb0(%rsp)
1035 .Lctr32_body:
1036 ___
1037 $code.=<<___;
1038         lea     -8(%rax),%rbp
1039         cmp     \$1,$len
1040         je      .Lctr32_one_shortcut
1041
1042         movdqu  ($ivp),$ivec
1043         movdqa  .Lbswap_mask(%rip),$bswap_mask
1044         xor     $rounds,$rounds
1045         pextrd  \$3,$ivec,$rnds_                # pull 32-bit counter
1046         pinsrd  \$3,$rounds,$ivec               # wipe 32-bit counter
1047
1048         mov     240($key),$rounds               # key->rounds
1049         bswap   $rnds_
1050         pxor    $iv0,$iv0                       # vector of 3 32-bit counters
1051         pxor    $iv1,$iv1                       # vector of 3 32-bit counters
1052         pinsrd  \$0,$rnds_,$iv0
1053         lea     3($rnds_),$key_
1054         pinsrd  \$0,$key_,$iv1
1055         inc     $rnds_
1056         pinsrd  \$1,$rnds_,$iv0
1057         inc     $key_
1058         pinsrd  \$1,$key_,$iv1
1059         inc     $rnds_
1060         pinsrd  \$2,$rnds_,$iv0
1061         inc     $key_
1062         pinsrd  \$2,$key_,$iv1
1063         movdqa  $iv0,0x00(%rsp)
1064         pshufb  $bswap_mask,$iv0
1065         movdqa  $iv1,0x10(%rsp)
1066         pshufb  $bswap_mask,$iv1
1067
1068         pshufd  \$`3<<6`,$iv0,$inout0           # place counter to upper dword
1069         pshufd  \$`2<<6`,$iv0,$inout1
1070         pshufd  \$`1<<6`,$iv0,$inout2
1071         cmp     \$6,$len
1072         jb      .Lctr32_tail
1073         shr     \$1,$rounds
1074         mov     $key,$key_                      # backup $key
1075         mov     $rounds,$rnds_                  # backup $rounds
1076         sub     \$6,$len
1077         jmp     .Lctr32_loop6
1078
1079 .align  16
1080 .Lctr32_loop6:
1081         pshufd  \$`3<<6`,$iv1,$inout3
1082         por     $ivec,$inout0                   # merge counter-less ivec
1083          $movkey        ($key_),$rndkey0
1084         pshufd  \$`2<<6`,$iv1,$inout4
1085         por     $ivec,$inout1
1086          $movkey        16($key_),$rndkey1
1087         pshufd  \$`1<<6`,$iv1,$inout5
1088         por     $ivec,$inout2
1089         por     $ivec,$inout3
1090          xorps          $rndkey0,$inout0
1091         por     $ivec,$inout4
1092         por     $ivec,$inout5
1093
1094         # inline _aesni_encrypt6 and interleave last rounds
1095         # with own code...
1096
1097         pxor            $rndkey0,$inout1
1098         aesenc          $rndkey1,$inout0
1099         lea             32($key_),$key
1100         pxor            $rndkey0,$inout2
1101         aesenc          $rndkey1,$inout1
1102          movdqa         .Lincrement32(%rip),$iv1
1103         pxor            $rndkey0,$inout3
1104         aesenc          $rndkey1,$inout2
1105          movdqa         (%rsp),$iv0
1106         pxor            $rndkey0,$inout4
1107         aesenc          $rndkey1,$inout3
1108         pxor            $rndkey0,$inout5
1109         $movkey         ($key),$rndkey0
1110         dec             $rounds
1111         aesenc          $rndkey1,$inout4
1112         aesenc          $rndkey1,$inout5
1113         jmp             .Lctr32_enc_loop6_enter
1114 .align  16
1115 .Lctr32_enc_loop6:
1116         aesenc          $rndkey1,$inout0
1117         aesenc          $rndkey1,$inout1
1118         dec             $rounds
1119         aesenc          $rndkey1,$inout2
1120         aesenc          $rndkey1,$inout3
1121         aesenc          $rndkey1,$inout4
1122         aesenc          $rndkey1,$inout5
1123 .Lctr32_enc_loop6_enter:
1124         $movkey         16($key),$rndkey1
1125         aesenc          $rndkey0,$inout0
1126         aesenc          $rndkey0,$inout1
1127         lea             32($key),$key
1128         aesenc          $rndkey0,$inout2
1129         aesenc          $rndkey0,$inout3
1130         aesenc          $rndkey0,$inout4
1131         aesenc          $rndkey0,$inout5
1132         $movkey         ($key),$rndkey0
1133         jnz             .Lctr32_enc_loop6
1134
1135         aesenc          $rndkey1,$inout0
1136          paddd          $iv1,$iv0               # increment counter vector
1137         aesenc          $rndkey1,$inout1
1138          paddd          0x10(%rsp),$iv1
1139         aesenc          $rndkey1,$inout2
1140          movdqa         $iv0,0x00(%rsp)         # save counter vector
1141         aesenc          $rndkey1,$inout3
1142          movdqa         $iv1,0x10(%rsp)
1143         aesenc          $rndkey1,$inout4
1144          pshufb         $bswap_mask,$iv0        # byte swap
1145         aesenc          $rndkey1,$inout5
1146          pshufb         $bswap_mask,$iv1
1147
1148         aesenclast      $rndkey0,$inout0
1149          movups         ($inp),$in0             # load input
1150         aesenclast      $rndkey0,$inout1
1151          movups         0x10($inp),$in1
1152         aesenclast      $rndkey0,$inout2
1153          movups         0x20($inp),$in2
1154         aesenclast      $rndkey0,$inout3
1155          movups         0x30($inp),$in3
1156         aesenclast      $rndkey0,$inout4
1157          movups         0x40($inp),$rndkey1
1158         aesenclast      $rndkey0,$inout5
1159          movups         0x50($inp),$rndkey0
1160          lea    0x60($inp),$inp
1161
1162         xorps   $inout0,$in0                    # xor
1163          pshufd \$`3<<6`,$iv0,$inout0
1164         xorps   $inout1,$in1
1165          pshufd \$`2<<6`,$iv0,$inout1
1166         movups  $in0,($out)                     # store output
1167         xorps   $inout2,$in2
1168          pshufd \$`1<<6`,$iv0,$inout2
1169         movups  $in1,0x10($out)
1170         xorps   $inout3,$in3
1171         movups  $in2,0x20($out)
1172         xorps   $inout4,$rndkey1
1173         movups  $in3,0x30($out)
1174         xorps   $inout5,$rndkey0
1175         movups  $rndkey1,0x40($out)
1176         movups  $rndkey0,0x50($out)
1177         lea     0x60($out),$out
1178         mov     $rnds_,$rounds
1179         sub     \$6,$len
1180         jnc     .Lctr32_loop6
1181
1182         add     \$6,$len
1183         jz      .Lctr32_done
1184         mov     $key_,$key                      # restore $key
1185         lea     1($rounds,$rounds),$rounds      # restore original value
1186
1187 .Lctr32_tail:
1188         por     $ivec,$inout0
1189         movups  ($inp),$in0
1190         cmp     \$2,$len
1191         jb      .Lctr32_one
1192
1193         por     $ivec,$inout1
1194         movups  0x10($inp),$in1
1195         je      .Lctr32_two
1196
1197         pshufd  \$`3<<6`,$iv1,$inout3
1198         por     $ivec,$inout2
1199         movups  0x20($inp),$in2
1200         cmp     \$4,$len
1201         jb      .Lctr32_three
1202
1203         pshufd  \$`2<<6`,$iv1,$inout4
1204         por     $ivec,$inout3
1205         movups  0x30($inp),$in3
1206         je      .Lctr32_four
1207
1208         por     $ivec,$inout4
1209         xorps   $inout5,$inout5
1210
1211         call    _aesni_encrypt6
1212
1213         movups  0x40($inp),$rndkey1
1214         xorps   $inout0,$in0
1215         xorps   $inout1,$in1
1216         movups  $in0,($out)
1217         xorps   $inout2,$in2
1218         movups  $in1,0x10($out)
1219         xorps   $inout3,$in3
1220         movups  $in2,0x20($out)
1221         xorps   $inout4,$rndkey1
1222         movups  $in3,0x30($out)
1223         movups  $rndkey1,0x40($out)
1224         jmp     .Lctr32_done
1225
1226 .align  16
1227 .Lctr32_one_shortcut:
1228         movups  ($ivp),$inout0
1229         movups  ($inp),$in0
1230         mov     240($key),$rounds               # key->rounds
1231 .Lctr32_one:
1232 ___
1233         &aesni_generate1("enc",$key,$rounds);
1234 $code.=<<___;
1235         xorps   $inout0,$in0
1236         movups  $in0,($out)
1237         jmp     .Lctr32_done
1238
1239 .align  16
1240 .Lctr32_two:
1241         xorps   $inout2,$inout2
1242         call    _aesni_encrypt3
1243         xorps   $inout0,$in0
1244         xorps   $inout1,$in1
1245         movups  $in0,($out)
1246         movups  $in1,0x10($out)
1247         jmp     .Lctr32_done
1248
1249 .align  16
1250 .Lctr32_three:
1251         call    _aesni_encrypt3
1252         xorps   $inout0,$in0
1253         xorps   $inout1,$in1
1254         movups  $in0,($out)
1255         xorps   $inout2,$in2
1256         movups  $in1,0x10($out)
1257         movups  $in2,0x20($out)
1258         jmp     .Lctr32_done
1259
1260 .align  16
1261 .Lctr32_four:
1262         call    _aesni_encrypt4
1263         xorps   $inout0,$in0
1264         xorps   $inout1,$in1
1265         movups  $in0,($out)
1266         xorps   $inout2,$in2
1267         movups  $in1,0x10($out)
1268         xorps   $inout3,$in3
1269         movups  $in2,0x20($out)
1270         movups  $in3,0x30($out)
1271
1272 .Lctr32_done:
1273 ___
1274 $code.=<<___ if ($win64);
1275         movaps  0x20(%rsp),%xmm6
1276         movaps  0x30(%rsp),%xmm7
1277         movaps  0x40(%rsp),%xmm8
1278         movaps  0x50(%rsp),%xmm9
1279         movaps  0x60(%rsp),%xmm10
1280         movaps  0x70(%rsp),%xmm11
1281         movaps  0x80(%rsp),%xmm12
1282         movaps  0x90(%rsp),%xmm13
1283         movaps  0xa0(%rsp),%xmm14
1284         movaps  0xb0(%rsp),%xmm15
1285 ___
1286 $code.=<<___;
1287         lea     (%rbp),%rsp
1288         pop     %rbp
1289 .Lctr32_ret:
1290         ret
1291 .size   aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks
1292 ___
1293 }
1294 \f
1295 ######################################################################
1296 # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1297 #       const AES_KEY *key1, const AES_KEY *key2
1298 #       const unsigned char iv[16]);
1299 #
1300 {
1301 my @tweak=map("%xmm$_",(10..15));
1302 my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
1303 my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
1304 my $frame_size = 0x60 + ($win64?160:0);
1305
1306 $code.=<<___;
1307 .globl  aesni_xts_encrypt
1308 .type   aesni_xts_encrypt,\@function,6
1309 .align  16
1310 aesni_xts_encrypt:
1311         lea     (%rsp),%rax
1312         push    %rbp
1313         sub     \$$frame_size,%rsp
1314         and     \$-16,%rsp      # Linux kernel stack can be incorrectly seeded
1315 ___
1316 $code.=<<___ if ($win64);
1317         movaps  %xmm6,0x60(%rsp)
1318         movaps  %xmm7,0x70(%rsp)
1319         movaps  %xmm8,0x80(%rsp)
1320         movaps  %xmm9,0x90(%rsp)
1321         movaps  %xmm10,0xa0(%rsp)
1322         movaps  %xmm11,0xb0(%rsp)
1323         movaps  %xmm12,0xc0(%rsp)
1324         movaps  %xmm13,0xd0(%rsp)
1325         movaps  %xmm14,0xe0(%rsp)
1326         movaps  %xmm15,0xf0(%rsp)
1327 .Lxts_enc_body:
1328 ___
1329 $code.=<<___;
1330         lea     -8(%rax),%rbp
1331         movups  ($ivp),@tweak[5]                # load clear-text tweak
1332         mov     240(%r8),$rounds                # key2->rounds
1333         mov     240($key),$rnds_                # key1->rounds
1334 ___
1335         # generate the tweak
1336         &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
1337 $code.=<<___;
1338         mov     $key,$key_                      # backup $key
1339         mov     $rnds_,$rounds                  # backup $rounds
1340         mov     $len,$len_                      # backup $len
1341         and     \$-16,$len
1342
1343         movdqa  .Lxts_magic(%rip),$twmask
1344         pxor    $twtmp,$twtmp
1345         pcmpgtd @tweak[5],$twtmp                # broadcast upper bits
1346 ___
1347     for ($i=0;$i<4;$i++) {
1348     $code.=<<___;
1349         pshufd  \$0x13,$twtmp,$twres
1350         pxor    $twtmp,$twtmp
1351         movdqa  @tweak[5],@tweak[$i]
1352         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1353         pand    $twmask,$twres                  # isolate carry and residue
1354         pcmpgtd @tweak[5],$twtmp                # broadcat upper bits
1355         pxor    $twres,@tweak[5]
1356 ___
1357     }
1358 $code.=<<___;
1359         sub     \$16*6,$len
1360         jc      .Lxts_enc_short
1361
1362         shr     \$1,$rounds
1363         sub     \$1,$rounds
1364         mov     $rounds,$rnds_
1365         jmp     .Lxts_enc_grandloop
1366
1367 .align  16
1368 .Lxts_enc_grandloop:
1369         pshufd  \$0x13,$twtmp,$twres
1370         movdqa  @tweak[5],@tweak[4]
1371         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1372         movdqu  `16*0`($inp),$inout0            # load input
1373         pand    $twmask,$twres                  # isolate carry and residue
1374         movdqu  `16*1`($inp),$inout1
1375         pxor    $twres,@tweak[5]
1376
1377         movdqu  `16*2`($inp),$inout2
1378         pxor    @tweak[0],$inout0               # input^=tweak
1379         movdqu  `16*3`($inp),$inout3
1380         pxor    @tweak[1],$inout1
1381         movdqu  `16*4`($inp),$inout4
1382         pxor    @tweak[2],$inout2
1383         movdqu  `16*5`($inp),$inout5
1384         lea     `16*6`($inp),$inp
1385         pxor    @tweak[3],$inout3
1386         $movkey         ($key_),$rndkey0
1387         pxor    @tweak[4],$inout4
1388         pxor    @tweak[5],$inout5
1389
1390         # inline _aesni_encrypt6 and interleave first and last rounds
1391         # with own code...
1392         $movkey         16($key_),$rndkey1
1393         pxor            $rndkey0,$inout0
1394         pxor            $rndkey0,$inout1
1395          movdqa @tweak[0],`16*0`(%rsp)          # put aside tweaks
1396         aesenc          $rndkey1,$inout0
1397         lea             32($key_),$key
1398         pxor            $rndkey0,$inout2
1399          movdqa @tweak[1],`16*1`(%rsp)
1400         aesenc          $rndkey1,$inout1
1401         pxor            $rndkey0,$inout3
1402          movdqa @tweak[2],`16*2`(%rsp)
1403         aesenc          $rndkey1,$inout2
1404         pxor            $rndkey0,$inout4
1405          movdqa @tweak[3],`16*3`(%rsp)
1406         aesenc          $rndkey1,$inout3
1407         pxor            $rndkey0,$inout5
1408         $movkey         ($key),$rndkey0
1409         dec             $rounds
1410          movdqa @tweak[4],`16*4`(%rsp)
1411         aesenc          $rndkey1,$inout4
1412          movdqa @tweak[5],`16*5`(%rsp)
1413         aesenc          $rndkey1,$inout5
1414         pxor    $twtmp,$twtmp
1415         pcmpgtd @tweak[5],$twtmp
1416         jmp             .Lxts_enc_loop6_enter
1417
1418 .align  16
1419 .Lxts_enc_loop6:
1420         aesenc          $rndkey1,$inout0
1421         aesenc          $rndkey1,$inout1
1422         dec             $rounds
1423         aesenc          $rndkey1,$inout2
1424         aesenc          $rndkey1,$inout3
1425         aesenc          $rndkey1,$inout4
1426         aesenc          $rndkey1,$inout5
1427 .Lxts_enc_loop6_enter:
1428         $movkey         16($key),$rndkey1
1429         aesenc          $rndkey0,$inout0
1430         aesenc          $rndkey0,$inout1
1431         lea             32($key),$key
1432         aesenc          $rndkey0,$inout2
1433         aesenc          $rndkey0,$inout3
1434         aesenc          $rndkey0,$inout4
1435         aesenc          $rndkey0,$inout5
1436         $movkey         ($key),$rndkey0
1437         jnz             .Lxts_enc_loop6
1438
1439         pshufd  \$0x13,$twtmp,$twres
1440         pxor    $twtmp,$twtmp
1441         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1442          aesenc         $rndkey1,$inout0
1443         pand    $twmask,$twres                  # isolate carry and residue
1444          aesenc         $rndkey1,$inout1
1445         pcmpgtd @tweak[5],$twtmp                # broadcast upper bits
1446          aesenc         $rndkey1,$inout2
1447         pxor    $twres,@tweak[5]
1448          aesenc         $rndkey1,$inout3
1449          aesenc         $rndkey1,$inout4
1450          aesenc         $rndkey1,$inout5
1451          $movkey        16($key),$rndkey1
1452
1453         pshufd  \$0x13,$twtmp,$twres
1454         pxor    $twtmp,$twtmp
1455         movdqa  @tweak[5],@tweak[0]
1456         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1457          aesenc         $rndkey0,$inout0
1458         pand    $twmask,$twres                  # isolate carry and residue
1459          aesenc         $rndkey0,$inout1
1460         pcmpgtd @tweak[5],$twtmp                # broadcat upper bits
1461          aesenc         $rndkey0,$inout2
1462         pxor    $twres,@tweak[5]
1463          aesenc         $rndkey0,$inout3
1464          aesenc         $rndkey0,$inout4
1465          aesenc         $rndkey0,$inout5
1466          $movkey        32($key),$rndkey0
1467
1468         pshufd  \$0x13,$twtmp,$twres
1469         pxor    $twtmp,$twtmp
1470         movdqa  @tweak[5],@tweak[1]
1471         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1472          aesenc         $rndkey1,$inout0
1473         pand    $twmask,$twres                  # isolate carry and residue
1474          aesenc         $rndkey1,$inout1
1475         pcmpgtd @tweak[5],$twtmp                # broadcat upper bits
1476          aesenc         $rndkey1,$inout2
1477         pxor    $twres,@tweak[5]
1478          aesenc         $rndkey1,$inout3
1479          aesenc         $rndkey1,$inout4
1480          aesenc         $rndkey1,$inout5
1481
1482         pshufd  \$0x13,$twtmp,$twres
1483         pxor    $twtmp,$twtmp
1484         movdqa  @tweak[5],@tweak[2]
1485         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1486          aesenclast     $rndkey0,$inout0
1487         pand    $twmask,$twres                  # isolate carry and residue
1488          aesenclast     $rndkey0,$inout1
1489         pcmpgtd @tweak[5],$twtmp                # broadcat upper bits
1490          aesenclast     $rndkey0,$inout2
1491         pxor    $twres,@tweak[5]
1492          aesenclast     $rndkey0,$inout3
1493          aesenclast     $rndkey0,$inout4
1494          aesenclast     $rndkey0,$inout5
1495
1496         pshufd  \$0x13,$twtmp,$twres
1497         pxor    $twtmp,$twtmp
1498         movdqa  @tweak[5],@tweak[3]
1499         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1500          xorps  `16*0`(%rsp),$inout0            # output^=tweak
1501         pand    $twmask,$twres                  # isolate carry and residue
1502          xorps  `16*1`(%rsp),$inout1
1503         pcmpgtd @tweak[5],$twtmp                # broadcat upper bits
1504         pxor    $twres,@tweak[5]
1505
1506         xorps   `16*2`(%rsp),$inout2
1507         movups  $inout0,`16*0`($out)            # write output
1508         xorps   `16*3`(%rsp),$inout3
1509         movups  $inout1,`16*1`($out)
1510         xorps   `16*4`(%rsp),$inout4
1511         movups  $inout2,`16*2`($out)
1512         xorps   `16*5`(%rsp),$inout5
1513         movups  $inout3,`16*3`($out)
1514         mov     $rnds_,$rounds                  # restore $rounds
1515         movups  $inout4,`16*4`($out)
1516         movups  $inout5,`16*5`($out)
1517         lea     `16*6`($out),$out
1518         sub     \$16*6,$len
1519         jnc     .Lxts_enc_grandloop
1520
1521         lea     3($rounds,$rounds),$rounds      # restore original value
1522         mov     $key_,$key                      # restore $key
1523         mov     $rounds,$rnds_                  # backup $rounds
1524
1525 .Lxts_enc_short:
1526         add     \$16*6,$len
1527         jz      .Lxts_enc_done
1528
1529         cmp     \$0x20,$len
1530         jb      .Lxts_enc_one
1531         je      .Lxts_enc_two
1532
1533         cmp     \$0x40,$len
1534         jb      .Lxts_enc_three
1535         je      .Lxts_enc_four
1536
1537         pshufd  \$0x13,$twtmp,$twres
1538         movdqa  @tweak[5],@tweak[4]
1539         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1540          movdqu ($inp),$inout0
1541         pand    $twmask,$twres                  # isolate carry and residue
1542          movdqu 16*1($inp),$inout1
1543         pxor    $twres,@tweak[5]
1544
1545         movdqu  16*2($inp),$inout2
1546         pxor    @tweak[0],$inout0
1547         movdqu  16*3($inp),$inout3
1548         pxor    @tweak[1],$inout1
1549         movdqu  16*4($inp),$inout4
1550         lea     16*5($inp),$inp
1551         pxor    @tweak[2],$inout2
1552         pxor    @tweak[3],$inout3
1553         pxor    @tweak[4],$inout4
1554
1555         call    _aesni_encrypt6
1556
1557         xorps   @tweak[0],$inout0
1558         movdqa  @tweak[5],@tweak[0]
1559         xorps   @tweak[1],$inout1
1560         xorps   @tweak[2],$inout2
1561         movdqu  $inout0,($out)
1562         xorps   @tweak[3],$inout3
1563         movdqu  $inout1,16*1($out)
1564         xorps   @tweak[4],$inout4
1565         movdqu  $inout2,16*2($out)
1566         movdqu  $inout3,16*3($out)
1567         movdqu  $inout4,16*4($out)
1568         lea     16*5($out),$out
1569         jmp     .Lxts_enc_done
1570
1571 .align  16
1572 .Lxts_enc_one:
1573         movups  ($inp),$inout0
1574         lea     16*1($inp),$inp
1575         xorps   @tweak[0],$inout0
1576 ___
1577         &aesni_generate1("enc",$key,$rounds);
1578 $code.=<<___;
1579         xorps   @tweak[0],$inout0
1580         movdqa  @tweak[1],@tweak[0]
1581         movups  $inout0,($out)
1582         lea     16*1($out),$out
1583         jmp     .Lxts_enc_done
1584
1585 .align  16
1586 .Lxts_enc_two:
1587         movups  ($inp),$inout0
1588         movups  16($inp),$inout1
1589         lea     32($inp),$inp
1590         xorps   @tweak[0],$inout0
1591         xorps   @tweak[1],$inout1
1592
1593         call    _aesni_encrypt3
1594
1595         xorps   @tweak[0],$inout0
1596         movdqa  @tweak[2],@tweak[0]
1597         xorps   @tweak[1],$inout1
1598         movups  $inout0,($out)
1599         movups  $inout1,16*1($out)
1600         lea     16*2($out),$out
1601         jmp     .Lxts_enc_done
1602
1603 .align  16
1604 .Lxts_enc_three:
1605         movups  ($inp),$inout0
1606         movups  16*1($inp),$inout1
1607         movups  16*2($inp),$inout2
1608         lea     16*3($inp),$inp
1609         xorps   @tweak[0],$inout0
1610         xorps   @tweak[1],$inout1
1611         xorps   @tweak[2],$inout2
1612
1613         call    _aesni_encrypt3
1614
1615         xorps   @tweak[0],$inout0
1616         movdqa  @tweak[3],@tweak[0]
1617         xorps   @tweak[1],$inout1
1618         xorps   @tweak[2],$inout2
1619         movups  $inout0,($out)
1620         movups  $inout1,16*1($out)
1621         movups  $inout2,16*2($out)
1622         lea     16*3($out),$out
1623         jmp     .Lxts_enc_done
1624
1625 .align  16
1626 .Lxts_enc_four:
1627         movups  ($inp),$inout0
1628         movups  16*1($inp),$inout1
1629         movups  16*2($inp),$inout2
1630         xorps   @tweak[0],$inout0
1631         movups  16*3($inp),$inout3
1632         lea     16*4($inp),$inp
1633         xorps   @tweak[1],$inout1
1634         xorps   @tweak[2],$inout2
1635         xorps   @tweak[3],$inout3
1636
1637         call    _aesni_encrypt4
1638
1639         xorps   @tweak[0],$inout0
1640         movdqa  @tweak[5],@tweak[0]
1641         xorps   @tweak[1],$inout1
1642         xorps   @tweak[2],$inout2
1643         movups  $inout0,($out)
1644         xorps   @tweak[3],$inout3
1645         movups  $inout1,16*1($out)
1646         movups  $inout2,16*2($out)
1647         movups  $inout3,16*3($out)
1648         lea     16*4($out),$out
1649         jmp     .Lxts_enc_done
1650
1651 .align  16
1652 .Lxts_enc_done:
1653         and     \$15,$len_
1654         jz      .Lxts_enc_ret
1655         mov     $len_,$len
1656
1657 .Lxts_enc_steal:
1658         movzb   ($inp),%eax                     # borrow $rounds ...
1659         movzb   -16($out),%ecx                  # ... and $key
1660         lea     1($inp),$inp
1661         mov     %al,-16($out)
1662         mov     %cl,0($out)
1663         lea     1($out),$out
1664         sub     \$1,$len
1665         jnz     .Lxts_enc_steal
1666
1667         sub     $len_,$out                      # rewind $out
1668         mov     $key_,$key                      # restore $key
1669         mov     $rnds_,$rounds                  # restore $rounds
1670
1671         movups  -16($out),$inout0
1672         xorps   @tweak[0],$inout0
1673 ___
1674         &aesni_generate1("enc",$key,$rounds);
1675 $code.=<<___;
1676         xorps   @tweak[0],$inout0
1677         movups  $inout0,-16($out)
1678
1679 .Lxts_enc_ret:
1680 ___
1681 $code.=<<___ if ($win64);
1682         movaps  0x60(%rsp),%xmm6
1683         movaps  0x70(%rsp),%xmm7
1684         movaps  0x80(%rsp),%xmm8
1685         movaps  0x90(%rsp),%xmm9
1686         movaps  0xa0(%rsp),%xmm10
1687         movaps  0xb0(%rsp),%xmm11
1688         movaps  0xc0(%rsp),%xmm12
1689         movaps  0xd0(%rsp),%xmm13
1690         movaps  0xe0(%rsp),%xmm14
1691         movaps  0xf0(%rsp),%xmm15
1692 ___
1693 $code.=<<___;
1694         lea     (%rbp),%rsp
1695         pop     %rbp
1696 .Lxts_enc_epilogue:
1697         ret
1698 .size   aesni_xts_encrypt,.-aesni_xts_encrypt
1699 ___
1700
1701 $code.=<<___;
1702 .globl  aesni_xts_decrypt
1703 .type   aesni_xts_decrypt,\@function,6
1704 .align  16
1705 aesni_xts_decrypt:
1706         lea     (%rsp),%rax
1707         push    %rbp
1708         sub     \$$frame_size,%rsp
1709         and     \$-16,%rsp      # Linux kernel stack can be incorrectly seeded
1710 ___
1711 $code.=<<___ if ($win64);
1712         movaps  %xmm6,0x60(%rsp)
1713         movaps  %xmm7,0x70(%rsp)
1714         movaps  %xmm8,0x80(%rsp)
1715         movaps  %xmm9,0x90(%rsp)
1716         movaps  %xmm10,0xa0(%rsp)
1717         movaps  %xmm11,0xb0(%rsp)
1718         movaps  %xmm12,0xc0(%rsp)
1719         movaps  %xmm13,0xd0(%rsp)
1720         movaps  %xmm14,0xe0(%rsp)
1721         movaps  %xmm15,0xf0(%rsp)
1722 .Lxts_dec_body:
1723 ___
1724 $code.=<<___;
1725         lea     -8(%rax),%rbp
1726         movups  ($ivp),@tweak[5]                # load clear-text tweak
1727         mov     240($key2),$rounds              # key2->rounds
1728         mov     240($key),$rnds_                # key1->rounds
1729 ___
1730         # generate the tweak
1731         &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
1732 $code.=<<___;
1733         xor     %eax,%eax                       # if ($len%16) len-=16;
1734         test    \$15,$len
1735         setnz   %al
1736         shl     \$4,%rax
1737         sub     %rax,$len
1738
1739         mov     $key,$key_                      # backup $key
1740         mov     $rnds_,$rounds                  # backup $rounds
1741         mov     $len,$len_                      # backup $len
1742         and     \$-16,$len
1743
1744         movdqa  .Lxts_magic(%rip),$twmask
1745         pxor    $twtmp,$twtmp
1746         pcmpgtd @tweak[5],$twtmp                # broadcast upper bits
1747 ___
1748     for ($i=0;$i<4;$i++) {
1749     $code.=<<___;
1750         pshufd  \$0x13,$twtmp,$twres
1751         pxor    $twtmp,$twtmp
1752         movdqa  @tweak[5],@tweak[$i]
1753         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1754         pand    $twmask,$twres                  # isolate carry and residue
1755         pcmpgtd @tweak[5],$twtmp                # broadcat upper bits
1756         pxor    $twres,@tweak[5]
1757 ___
1758     }
1759 $code.=<<___;
1760         sub     \$16*6,$len
1761         jc      .Lxts_dec_short
1762
1763         shr     \$1,$rounds
1764         sub     \$1,$rounds
1765         mov     $rounds,$rnds_
1766         jmp     .Lxts_dec_grandloop
1767
1768 .align  16
1769 .Lxts_dec_grandloop:
1770         pshufd  \$0x13,$twtmp,$twres
1771         movdqa  @tweak[5],@tweak[4]
1772         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1773         movdqu  `16*0`($inp),$inout0            # load input
1774         pand    $twmask,$twres                  # isolate carry and residue
1775         movdqu  `16*1`($inp),$inout1
1776         pxor    $twres,@tweak[5]
1777
1778         movdqu  `16*2`($inp),$inout2
1779         pxor    @tweak[0],$inout0               # input^=tweak
1780         movdqu  `16*3`($inp),$inout3
1781         pxor    @tweak[1],$inout1
1782         movdqu  `16*4`($inp),$inout4
1783         pxor    @tweak[2],$inout2
1784         movdqu  `16*5`($inp),$inout5
1785         lea     `16*6`($inp),$inp
1786         pxor    @tweak[3],$inout3
1787         $movkey         ($key_),$rndkey0
1788         pxor    @tweak[4],$inout4
1789         pxor    @tweak[5],$inout5
1790
1791         # inline _aesni_decrypt6 and interleave first and last rounds
1792         # with own code...
1793         $movkey         16($key_),$rndkey1
1794         pxor            $rndkey0,$inout0
1795         pxor            $rndkey0,$inout1
1796          movdqa @tweak[0],`16*0`(%rsp)          # put aside tweaks
1797         aesdec          $rndkey1,$inout0
1798         lea             32($key_),$key
1799         pxor            $rndkey0,$inout2
1800          movdqa @tweak[1],`16*1`(%rsp)
1801         aesdec          $rndkey1,$inout1
1802         pxor            $rndkey0,$inout3
1803          movdqa @tweak[2],`16*2`(%rsp)
1804         aesdec          $rndkey1,$inout2
1805         pxor            $rndkey0,$inout4
1806          movdqa @tweak[3],`16*3`(%rsp)
1807         aesdec          $rndkey1,$inout3
1808         pxor            $rndkey0,$inout5
1809         $movkey         ($key),$rndkey0
1810         dec             $rounds
1811          movdqa @tweak[4],`16*4`(%rsp)
1812         aesdec          $rndkey1,$inout4
1813          movdqa @tweak[5],`16*5`(%rsp)
1814         aesdec          $rndkey1,$inout5
1815         pxor    $twtmp,$twtmp
1816         pcmpgtd @tweak[5],$twtmp
1817         jmp             .Lxts_dec_loop6_enter
1818
1819 .align  16
1820 .Lxts_dec_loop6:
1821         aesdec          $rndkey1,$inout0
1822         aesdec          $rndkey1,$inout1
1823         dec             $rounds
1824         aesdec          $rndkey1,$inout2
1825         aesdec          $rndkey1,$inout3
1826         aesdec          $rndkey1,$inout4
1827         aesdec          $rndkey1,$inout5
1828 .Lxts_dec_loop6_enter:
1829         $movkey         16($key),$rndkey1
1830         aesdec          $rndkey0,$inout0
1831         aesdec          $rndkey0,$inout1
1832         lea             32($key),$key
1833         aesdec          $rndkey0,$inout2
1834         aesdec          $rndkey0,$inout3
1835         aesdec          $rndkey0,$inout4
1836         aesdec          $rndkey0,$inout5
1837         $movkey         ($key),$rndkey0
1838         jnz             .Lxts_dec_loop6
1839
1840         pshufd  \$0x13,$twtmp,$twres
1841         pxor    $twtmp,$twtmp
1842         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1843          aesdec         $rndkey1,$inout0
1844         pand    $twmask,$twres                  # isolate carry and residue
1845          aesdec         $rndkey1,$inout1
1846         pcmpgtd @tweak[5],$twtmp                # broadcast upper bits
1847          aesdec         $rndkey1,$inout2
1848         pxor    $twres,@tweak[5]
1849          aesdec         $rndkey1,$inout3
1850          aesdec         $rndkey1,$inout4
1851          aesdec         $rndkey1,$inout5
1852          $movkey        16($key),$rndkey1
1853
1854         pshufd  \$0x13,$twtmp,$twres
1855         pxor    $twtmp,$twtmp
1856         movdqa  @tweak[5],@tweak[0]
1857         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1858          aesdec         $rndkey0,$inout0
1859         pand    $twmask,$twres                  # isolate carry and residue
1860          aesdec         $rndkey0,$inout1
1861         pcmpgtd @tweak[5],$twtmp                # broadcat upper bits
1862          aesdec         $rndkey0,$inout2
1863         pxor    $twres,@tweak[5]
1864          aesdec         $rndkey0,$inout3
1865          aesdec         $rndkey0,$inout4
1866          aesdec         $rndkey0,$inout5
1867          $movkey        32($key),$rndkey0
1868
1869         pshufd  \$0x13,$twtmp,$twres
1870         pxor    $twtmp,$twtmp
1871         movdqa  @tweak[5],@tweak[1]
1872         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1873          aesdec         $rndkey1,$inout0
1874         pand    $twmask,$twres                  # isolate carry and residue
1875          aesdec         $rndkey1,$inout1
1876         pcmpgtd @tweak[5],$twtmp                # broadcat upper bits
1877          aesdec         $rndkey1,$inout2
1878         pxor    $twres,@tweak[5]
1879          aesdec         $rndkey1,$inout3
1880          aesdec         $rndkey1,$inout4
1881          aesdec         $rndkey1,$inout5
1882
1883         pshufd  \$0x13,$twtmp,$twres
1884         pxor    $twtmp,$twtmp
1885         movdqa  @tweak[5],@tweak[2]
1886         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1887          aesdeclast     $rndkey0,$inout0
1888         pand    $twmask,$twres                  # isolate carry and residue
1889          aesdeclast     $rndkey0,$inout1
1890         pcmpgtd @tweak[5],$twtmp                # broadcat upper bits
1891          aesdeclast     $rndkey0,$inout2
1892         pxor    $twres,@tweak[5]
1893          aesdeclast     $rndkey0,$inout3
1894          aesdeclast     $rndkey0,$inout4
1895          aesdeclast     $rndkey0,$inout5
1896
1897         pshufd  \$0x13,$twtmp,$twres
1898         pxor    $twtmp,$twtmp
1899         movdqa  @tweak[5],@tweak[3]
1900         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1901          xorps  `16*0`(%rsp),$inout0            # output^=tweak
1902         pand    $twmask,$twres                  # isolate carry and residue
1903          xorps  `16*1`(%rsp),$inout1
1904         pcmpgtd @tweak[5],$twtmp                # broadcat upper bits
1905         pxor    $twres,@tweak[5]
1906
1907         xorps   `16*2`(%rsp),$inout2
1908         movups  $inout0,`16*0`($out)            # write output
1909         xorps   `16*3`(%rsp),$inout3
1910         movups  $inout1,`16*1`($out)
1911         xorps   `16*4`(%rsp),$inout4
1912         movups  $inout2,`16*2`($out)
1913         xorps   `16*5`(%rsp),$inout5
1914         movups  $inout3,`16*3`($out)
1915         mov     $rnds_,$rounds                  # restore $rounds
1916         movups  $inout4,`16*4`($out)
1917         movups  $inout5,`16*5`($out)
1918         lea     `16*6`($out),$out
1919         sub     \$16*6,$len
1920         jnc     .Lxts_dec_grandloop
1921
1922         lea     3($rounds,$rounds),$rounds      # restore original value
1923         mov     $key_,$key                      # restore $key
1924         mov     $rounds,$rnds_                  # backup $rounds
1925
1926 .Lxts_dec_short:
1927         add     \$16*6,$len
1928         jz      .Lxts_dec_done
1929
1930         cmp     \$0x20,$len
1931         jb      .Lxts_dec_one
1932         je      .Lxts_dec_two
1933
1934         cmp     \$0x40,$len
1935         jb      .Lxts_dec_three
1936         je      .Lxts_dec_four
1937
1938         pshufd  \$0x13,$twtmp,$twres
1939         movdqa  @tweak[5],@tweak[4]
1940         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1941          movdqu ($inp),$inout0
1942         pand    $twmask,$twres                  # isolate carry and residue
1943          movdqu 16*1($inp),$inout1
1944         pxor    $twres,@tweak[5]
1945
1946         movdqu  16*2($inp),$inout2
1947         pxor    @tweak[0],$inout0
1948         movdqu  16*3($inp),$inout3
1949         pxor    @tweak[1],$inout1
1950         movdqu  16*4($inp),$inout4
1951         lea     16*5($inp),$inp
1952         pxor    @tweak[2],$inout2
1953         pxor    @tweak[3],$inout3
1954         pxor    @tweak[4],$inout4
1955
1956         call    _aesni_decrypt6
1957
1958         xorps   @tweak[0],$inout0
1959         xorps   @tweak[1],$inout1
1960         xorps   @tweak[2],$inout2
1961         movdqu  $inout0,($out)
1962         xorps   @tweak[3],$inout3
1963         movdqu  $inout1,16*1($out)
1964         xorps   @tweak[4],$inout4
1965         movdqu  $inout2,16*2($out)
1966          pxor           $twtmp,$twtmp
1967         movdqu  $inout3,16*3($out)
1968          pcmpgtd        @tweak[5],$twtmp
1969         movdqu  $inout4,16*4($out)
1970         lea     16*5($out),$out
1971          pshufd         \$0x13,$twtmp,@tweak[1] # $twres
1972         and     \$15,$len_
1973         jz      .Lxts_dec_ret
1974
1975         movdqa  @tweak[5],@tweak[0]
1976         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
1977         pand    $twmask,@tweak[1]               # isolate carry and residue
1978         pxor    @tweak[5],@tweak[1]
1979         jmp     .Lxts_dec_done2
1980
1981 .align  16
1982 .Lxts_dec_one:
1983         movups  ($inp),$inout0
1984         lea     16*1($inp),$inp
1985         xorps   @tweak[0],$inout0
1986 ___
1987         &aesni_generate1("dec",$key,$rounds);
1988 $code.=<<___;
1989         xorps   @tweak[0],$inout0
1990         movdqa  @tweak[1],@tweak[0]
1991         movups  $inout0,($out)
1992         movdqa  @tweak[2],@tweak[1]
1993         lea     16*1($out),$out
1994         jmp     .Lxts_dec_done
1995
1996 .align  16
1997 .Lxts_dec_two:
1998         movups  ($inp),$inout0
1999         movups  16($inp),$inout1
2000         lea     32($inp),$inp
2001         xorps   @tweak[0],$inout0
2002         xorps   @tweak[1],$inout1
2003
2004         call    _aesni_decrypt3
2005
2006         xorps   @tweak[0],$inout0
2007         movdqa  @tweak[2],@tweak[0]
2008         xorps   @tweak[1],$inout1
2009         movdqa  @tweak[3],@tweak[1]
2010         movups  $inout0,($out)
2011         movups  $inout1,16*1($out)
2012         lea     16*2($out),$out
2013         jmp     .Lxts_dec_done
2014
2015 .align  16
2016 .Lxts_dec_three:
2017         movups  ($inp),$inout0
2018         movups  16*1($inp),$inout1
2019         movups  16*2($inp),$inout2
2020         lea     16*3($inp),$inp
2021         xorps   @tweak[0],$inout0
2022         xorps   @tweak[1],$inout1
2023         xorps   @tweak[2],$inout2
2024
2025         call    _aesni_decrypt3
2026
2027         xorps   @tweak[0],$inout0
2028         movdqa  @tweak[3],@tweak[0]
2029         xorps   @tweak[1],$inout1
2030         movdqa  @tweak[5],@tweak[1]
2031         xorps   @tweak[2],$inout2
2032         movups  $inout0,($out)
2033         movups  $inout1,16*1($out)
2034         movups  $inout2,16*2($out)
2035         lea     16*3($out),$out
2036         jmp     .Lxts_dec_done
2037
2038 .align  16
2039 .Lxts_dec_four:
2040         pshufd  \$0x13,$twtmp,$twres
2041         movdqa  @tweak[5],@tweak[4]
2042         paddq   @tweak[5],@tweak[5]             # psllq 1,$tweak
2043          movups ($inp),$inout0
2044         pand    $twmask,$twres                  # isolate carry and residue
2045          movups 16*1($inp),$inout1
2046         pxor    $twres,@tweak[5]
2047
2048         movups  16*2($inp),$inout2
2049         xorps   @tweak[0],$inout0
2050         movups  16*3($inp),$inout3
2051         lea     16*4($inp),$inp
2052         xorps   @tweak[1],$inout1
2053         xorps   @tweak[2],$inout2
2054         xorps   @tweak[3],$inout3
2055
2056         call    _aesni_decrypt4
2057
2058         xorps   @tweak[0],$inout0
2059         movdqa  @tweak[4],@tweak[0]
2060         xorps   @tweak[1],$inout1
2061         movdqa  @tweak[5],@tweak[1]
2062         xorps   @tweak[2],$inout2
2063         movups  $inout0,($out)
2064         xorps   @tweak[3],$inout3
2065         movups  $inout1,16*1($out)
2066         movups  $inout2,16*2($out)
2067         movups  $inout3,16*3($out)
2068         lea     16*4($out),$out
2069         jmp     .Lxts_dec_done
2070
2071 .align  16
2072 .Lxts_dec_done:
2073         and     \$15,$len_
2074         jz      .Lxts_dec_ret
2075 .Lxts_dec_done2:
2076         mov     $len_,$len
2077         mov     $key_,$key                      # restore $key
2078         mov     $rnds_,$rounds                  # restore $rounds
2079
2080         movups  ($inp),$inout0
2081         xorps   @tweak[1],$inout0
2082 ___
2083         &aesni_generate1("dec",$key,$rounds);
2084 $code.=<<___;
2085         xorps   @tweak[1],$inout0
2086         movups  $inout0,($out)
2087
2088 .Lxts_dec_steal:
2089         movzb   16($inp),%eax                   # borrow $rounds ...
2090         movzb   ($out),%ecx                     # ... and $key
2091         lea     1($inp),$inp
2092         mov     %al,($out)
2093         mov     %cl,16($out)
2094         lea     1($out),$out
2095         sub     \$1,$len
2096         jnz     .Lxts_dec_steal
2097
2098         sub     $len_,$out                      # rewind $out
2099         mov     $key_,$key                      # restore $key
2100         mov     $rnds_,$rounds                  # restore $rounds
2101
2102         movups  ($out),$inout0
2103         xorps   @tweak[0],$inout0
2104 ___
2105         &aesni_generate1("dec",$key,$rounds);
2106 $code.=<<___;
2107         xorps   @tweak[0],$inout0
2108         movups  $inout0,($out)
2109
2110 .Lxts_dec_ret:
2111 ___
2112 $code.=<<___ if ($win64);
2113         movaps  0x60(%rsp),%xmm6
2114         movaps  0x70(%rsp),%xmm7
2115         movaps  0x80(%rsp),%xmm8
2116         movaps  0x90(%rsp),%xmm9
2117         movaps  0xa0(%rsp),%xmm10
2118         movaps  0xb0(%rsp),%xmm11
2119         movaps  0xc0(%rsp),%xmm12
2120         movaps  0xd0(%rsp),%xmm13
2121         movaps  0xe0(%rsp),%xmm14
2122         movaps  0xf0(%rsp),%xmm15
2123 ___
2124 $code.=<<___;
2125         lea     (%rbp),%rsp
2126         pop     %rbp
2127 .Lxts_dec_epilogue:
2128         ret
2129 .size   aesni_xts_decrypt,.-aesni_xts_decrypt
2130 ___
2131 } }}
2132 \f
2133 ########################################################################
2134 # void $PREFIX_cbc_encrypt (const void *inp, void *out,
2135 #                           size_t length, const AES_KEY *key,
2136 #                           unsigned char *ivp,const int enc);
2137 {
2138 my $frame_size = 0x10 + ($win64?0x40:0);        # used in decrypt
2139 $code.=<<___;
2140 .globl  ${PREFIX}_cbc_encrypt
2141 .type   ${PREFIX}_cbc_encrypt,\@function,6
2142 .align  16
2143 ${PREFIX}_cbc_encrypt:
2144         test    $len,$len               # check length
2145         jz      .Lcbc_ret
2146
2147         mov     240($key),$rnds_        # key->rounds
2148         mov     $key,$key_              # backup $key
2149         test    %r9d,%r9d               # 6th argument
2150         jz      .Lcbc_decrypt
2151 #--------------------------- CBC ENCRYPT ------------------------------#
2152         movups  ($ivp),$inout0          # load iv as initial state
2153         mov     $rnds_,$rounds
2154         cmp     \$16,$len
2155         jb      .Lcbc_enc_tail
2156         sub     \$16,$len
2157         jmp     .Lcbc_enc_loop
2158 .align  16
2159 .Lcbc_enc_loop:
2160         movups  ($inp),$inout1          # load input
2161         lea     16($inp),$inp
2162         #xorps  $inout1,$inout0
2163 ___
2164         &aesni_generate1("enc",$key,$rounds,$inout0,$inout1);
2165 $code.=<<___;
2166         mov     $rnds_,$rounds          # restore $rounds
2167         mov     $key_,$key              # restore $key
2168         movups  $inout0,0($out)         # store output
2169         lea     16($out),$out
2170         sub     \$16,$len
2171         jnc     .Lcbc_enc_loop
2172         add     \$16,$len
2173         jnz     .Lcbc_enc_tail
2174         movups  $inout0,($ivp)
2175         jmp     .Lcbc_ret
2176
2177 .Lcbc_enc_tail:
2178         mov     $len,%rcx       # zaps $key
2179         xchg    $inp,$out       # $inp is %rsi and $out is %rdi now
2180         .long   0x9066A4F3      # rep movsb
2181         mov     \$16,%ecx       # zero tail
2182         sub     $len,%rcx
2183         xor     %eax,%eax
2184         .long   0x9066AAF3      # rep stosb
2185         lea     -16(%rdi),%rdi  # rewind $out by 1 block
2186         mov     $rnds_,$rounds  # restore $rounds
2187         mov     %rdi,%rsi       # $inp and $out are the same
2188         mov     $key_,$key      # restore $key
2189         xor     $len,$len       # len=16
2190         jmp     .Lcbc_enc_loop  # one more spin
2191 \f#--------------------------- CBC DECRYPT ------------------------------#
2192 .align  16
2193 .Lcbc_decrypt:
2194         lea     (%rsp),%rax
2195         push    %rbp
2196         sub     \$$frame_size,%rsp
2197         and     \$-16,%rsp      # Linux kernel stack can be incorrectly seeded
2198 ___
2199 $code.=<<___ if ($win64);
2200         movaps  %xmm6,0x10(%rsp)
2201         movaps  %xmm7,0x20(%rsp)
2202         movaps  %xmm8,0x30(%rsp)
2203         movaps  %xmm9,0x40(%rsp)
2204 .Lcbc_decrypt_body:
2205 ___
2206 $code.=<<___;
2207         lea     -8(%rax),%rbp
2208         movups  ($ivp),$iv
2209         mov     $rnds_,$rounds
2210         cmp     \$0x70,$len
2211         jbe     .Lcbc_dec_tail
2212         shr     \$1,$rnds_
2213         sub     \$0x70,$len
2214         mov     $rnds_,$rounds
2215         movaps  $iv,(%rsp)
2216         jmp     .Lcbc_dec_loop8_enter
2217 .align  16
2218 .Lcbc_dec_loop8:
2219         movaps  $rndkey0,(%rsp)                 # save IV
2220         movups  $inout7,($out)
2221         lea     0x10($out),$out
2222 .Lcbc_dec_loop8_enter:
2223         $movkey         ($key),$rndkey0
2224         movups  ($inp),$inout0                  # load input
2225         movups  0x10($inp),$inout1
2226         $movkey         16($key),$rndkey1
2227
2228         lea             32($key),$key
2229         movdqu  0x20($inp),$inout2
2230         xorps           $rndkey0,$inout0
2231         movdqu  0x30($inp),$inout3
2232         xorps           $rndkey0,$inout1
2233         movdqu  0x40($inp),$inout4
2234         aesdec          $rndkey1,$inout0
2235         pxor            $rndkey0,$inout2
2236         movdqu  0x50($inp),$inout5
2237         aesdec          $rndkey1,$inout1
2238         pxor            $rndkey0,$inout3
2239         movdqu  0x60($inp),$inout6
2240         aesdec          $rndkey1,$inout2
2241         pxor            $rndkey0,$inout4
2242         movdqu  0x70($inp),$inout7
2243         aesdec          $rndkey1,$inout3
2244         pxor            $rndkey0,$inout5
2245         dec             $rounds
2246         aesdec          $rndkey1,$inout4
2247         pxor            $rndkey0,$inout6
2248         aesdec          $rndkey1,$inout5
2249         pxor            $rndkey0,$inout7
2250         $movkey         ($key),$rndkey0
2251         aesdec          $rndkey1,$inout6
2252         aesdec          $rndkey1,$inout7
2253         $movkey         16($key),$rndkey1
2254
2255         call            .Ldec_loop8_enter
2256
2257         movups  ($inp),$rndkey1         # re-load input
2258         movups  0x10($inp),$rndkey0
2259         xorps   (%rsp),$inout0          # ^= IV
2260         xorps   $rndkey1,$inout1
2261         movups  0x20($inp),$rndkey1
2262         xorps   $rndkey0,$inout2
2263         movups  0x30($inp),$rndkey0
2264         xorps   $rndkey1,$inout3
2265         movups  0x40($inp),$rndkey1
2266         xorps   $rndkey0,$inout4
2267         movups  0x50($inp),$rndkey0
2268         xorps   $rndkey1,$inout5
2269         movups  0x60($inp),$rndkey1
2270         xorps   $rndkey0,$inout6
2271         movups  0x70($inp),$rndkey0     # IV
2272         xorps   $rndkey1,$inout7
2273         movups  $inout0,($out)
2274         movups  $inout1,0x10($out)
2275         movups  $inout2,0x20($out)
2276         movups  $inout3,0x30($out)
2277         mov     $rnds_,$rounds          # restore $rounds
2278         movups  $inout4,0x40($out)
2279         mov     $key_,$key              # restore $key
2280         movups  $inout5,0x50($out)
2281         lea     0x80($inp),$inp
2282         movups  $inout6,0x60($out)
2283         lea     0x70($out),$out
2284         sub     \$0x80,$len
2285         ja      .Lcbc_dec_loop8
2286
2287         movaps  $inout7,$inout0
2288         movaps  $rndkey0,$iv
2289         add     \$0x70,$len
2290         jle     .Lcbc_dec_tail_collected
2291         movups  $inout0,($out)
2292         lea     1($rnds_,$rnds_),$rounds
2293         lea     0x10($out),$out
2294 .Lcbc_dec_tail:
2295         movups  ($inp),$inout0
2296         movaps  $inout0,$in0
2297         cmp     \$0x10,$len
2298         jbe     .Lcbc_dec_one
2299
2300         movups  0x10($inp),$inout1
2301         movaps  $inout1,$in1
2302         cmp     \$0x20,$len
2303         jbe     .Lcbc_dec_two
2304
2305         movups  0x20($inp),$inout2
2306         movaps  $inout2,$in2
2307         cmp     \$0x30,$len
2308         jbe     .Lcbc_dec_three
2309
2310         movups  0x30($inp),$inout3
2311         cmp     \$0x40,$len
2312         jbe     .Lcbc_dec_four
2313
2314         movups  0x40($inp),$inout4
2315         cmp     \$0x50,$len
2316         jbe     .Lcbc_dec_five
2317
2318         movups  0x50($inp),$inout5
2319         cmp     \$0x60,$len
2320         jbe     .Lcbc_dec_six
2321
2322         movups  0x60($inp),$inout6
2323         movaps  $iv,(%rsp)              # save IV
2324         call    _aesni_decrypt8
2325         movups  ($inp),$rndkey1
2326         movups  0x10($inp),$rndkey0
2327         xorps   (%rsp),$inout0          # ^= IV
2328         xorps   $rndkey1,$inout1
2329         movups  0x20($inp),$rndkey1
2330         xorps   $rndkey0,$inout2
2331         movups  0x30($inp),$rndkey0
2332         xorps   $rndkey1,$inout3
2333         movups  0x40($inp),$rndkey1
2334         xorps   $rndkey0,$inout4
2335         movups  0x50($inp),$rndkey0
2336         xorps   $rndkey1,$inout5
2337         movups  0x60($inp),$iv          # IV
2338         xorps   $rndkey0,$inout6
2339         movups  $inout0,($out)
2340         movups  $inout1,0x10($out)
2341         movups  $inout2,0x20($out)
2342         movups  $inout3,0x30($out)
2343         movups  $inout4,0x40($out)
2344         movups  $inout5,0x50($out)
2345         lea     0x60($out),$out
2346         movaps  $inout6,$inout0
2347         sub     \$0x70,$len
2348         jmp     .Lcbc_dec_tail_collected
2349 .align  16
2350 .Lcbc_dec_one:
2351 ___
2352         &aesni_generate1("dec",$key,$rounds);
2353 $code.=<<___;
2354         xorps   $iv,$inout0
2355         movaps  $in0,$iv
2356         sub     \$0x10,$len
2357         jmp     .Lcbc_dec_tail_collected
2358 .align  16
2359 .Lcbc_dec_two:
2360         xorps   $inout2,$inout2
2361         call    _aesni_decrypt3
2362         xorps   $iv,$inout0
2363         xorps   $in0,$inout1
2364         movups  $inout0,($out)
2365         movaps  $in1,$iv
2366         movaps  $inout1,$inout0
2367         lea     0x10($out),$out
2368         sub     \$0x20,$len
2369         jmp     .Lcbc_dec_tail_collected
2370 .align  16
2371 .Lcbc_dec_three:
2372         call    _aesni_decrypt3
2373         xorps   $iv,$inout0
2374         xorps   $in0,$inout1
2375         movups  $inout0,($out)
2376         xorps   $in1,$inout2
2377         movups  $inout1,0x10($out)
2378         movaps  $in2,$iv
2379         movaps  $inout2,$inout0
2380         lea     0x20($out),$out
2381         sub     \$0x30,$len
2382         jmp     .Lcbc_dec_tail_collected
2383 .align  16
2384 .Lcbc_dec_four:
2385         call    _aesni_decrypt4
2386         xorps   $iv,$inout0
2387         movups  0x30($inp),$iv
2388         xorps   $in0,$inout1
2389         movups  $inout0,($out)
2390         xorps   $in1,$inout2
2391         movups  $inout1,0x10($out)
2392         xorps   $in2,$inout3
2393         movups  $inout2,0x20($out)
2394         movaps  $inout3,$inout0
2395         lea     0x30($out),$out
2396         sub     \$0x40,$len
2397         jmp     .Lcbc_dec_tail_collected
2398 .align  16
2399 .Lcbc_dec_five:
2400         xorps   $inout5,$inout5
2401         call    _aesni_decrypt6
2402         movups  0x10($inp),$rndkey1
2403         movups  0x20($inp),$rndkey0
2404         xorps   $iv,$inout0
2405         xorps   $in0,$inout1
2406         xorps   $rndkey1,$inout2
2407         movups  0x30($inp),$rndkey1
2408         xorps   $rndkey0,$inout3
2409         movups  0x40($inp),$iv
2410         xorps   $rndkey1,$inout4
2411         movups  $inout0,($out)
2412         movups  $inout1,0x10($out)
2413         movups  $inout2,0x20($out)
2414         movups  $inout3,0x30($out)
2415         lea     0x40($out),$out
2416         movaps  $inout4,$inout0
2417         sub     \$0x50,$len
2418         jmp     .Lcbc_dec_tail_collected
2419 .align  16
2420 .Lcbc_dec_six:
2421         call    _aesni_decrypt6
2422         movups  0x10($inp),$rndkey1
2423         movups  0x20($inp),$rndkey0
2424         xorps   $iv,$inout0
2425         xorps   $in0,$inout1
2426         xorps   $rndkey1,$inout2
2427         movups  0x30($inp),$rndkey1
2428         xorps   $rndkey0,$inout3
2429         movups  0x40($inp),$rndkey0
2430         xorps   $rndkey1,$inout4
2431         movups  0x50($inp),$iv
2432         xorps   $rndkey0,$inout5
2433         movups  $inout0,($out)
2434         movups  $inout1,0x10($out)
2435         movups  $inout2,0x20($out)
2436         movups  $inout3,0x30($out)
2437         movups  $inout4,0x40($out)
2438         lea     0x50($out),$out
2439         movaps  $inout5,$inout0
2440         sub     \$0x60,$len
2441         jmp     .Lcbc_dec_tail_collected
2442 .align  16
2443 .Lcbc_dec_tail_collected:
2444         and     \$15,$len
2445         movups  $iv,($ivp)
2446         jnz     .Lcbc_dec_tail_partial
2447         movups  $inout0,($out)
2448         jmp     .Lcbc_dec_ret
2449 .align  16
2450 .Lcbc_dec_tail_partial:
2451         movaps  $inout0,(%rsp)
2452         mov     \$16,%rcx
2453         mov     $out,%rdi
2454         sub     $len,%rcx
2455         lea     (%rsp),%rsi
2456         .long   0x9066A4F3      # rep movsb
2457
2458 .Lcbc_dec_ret:
2459 ___
2460 $code.=<<___ if ($win64);
2461         movaps  0x10(%rsp),%xmm6
2462         movaps  0x20(%rsp),%xmm7
2463         movaps  0x30(%rsp),%xmm8
2464         movaps  0x40(%rsp),%xmm9
2465 ___
2466 $code.=<<___;
2467         lea     (%rbp),%rsp
2468         pop     %rbp
2469 .Lcbc_ret:
2470         ret
2471 .size   ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
2472 ___
2473\f
2474 # int $PREFIX_set_[en|de]crypt_key (const unsigned char *userKey,
2475 #                               int bits, AES_KEY *key)
2476 { my ($inp,$bits,$key) = @_4args;
2477   $bits =~ s/%r/%e/;
2478
2479 $code.=<<___;
2480 .globl  ${PREFIX}_set_decrypt_key
2481 .type   ${PREFIX}_set_decrypt_key,\@abi-omnipotent
2482 .align  16
2483 ${PREFIX}_set_decrypt_key:
2484         .byte   0x48,0x83,0xEC,0x08     # sub rsp,8
2485         call    __aesni_set_encrypt_key
2486         shl     \$4,$bits               # rounds-1 after _aesni_set_encrypt_key
2487         test    %eax,%eax
2488         jnz     .Ldec_key_ret
2489         lea     16($key,$bits),$inp     # points at the end of key schedule
2490
2491         $movkey ($key),%xmm0            # just swap
2492         $movkey ($inp),%xmm1
2493         $movkey %xmm0,($inp)
2494         $movkey %xmm1,($key)
2495         lea     16($key),$key
2496         lea     -16($inp),$inp
2497
2498 .Ldec_key_inverse:
2499         $movkey ($key),%xmm0            # swap and inverse
2500         $movkey ($inp),%xmm1
2501         aesimc  %xmm0,%xmm0
2502         aesimc  %xmm1,%xmm1
2503         lea     16($key),$key
2504         lea     -16($inp),$inp
2505         $movkey %xmm0,16($inp)
2506         $movkey %xmm1,-16($key)
2507         cmp     $key,$inp
2508         ja      .Ldec_key_inverse
2509
2510         $movkey ($key),%xmm0            # inverse middle
2511         aesimc  %xmm0,%xmm0
2512         $movkey %xmm0,($inp)
2513 .Ldec_key_ret:
2514         add     \$8,%rsp
2515         ret
2516 .LSEH_end_set_decrypt_key:
2517 .size   ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
2518 ___
2519 \f
2520 # This is based on submission by
2521 #
2522 #       Huang Ying <ying.huang@intel.com>
2523 #       Vinodh Gopal <vinodh.gopal@intel.com>
2524 #       Kahraman Akdemir
2525 #
2526 # Agressively optimized in respect to aeskeygenassist's critical path
2527 # and is contained in %xmm0-5 to meet Win64 ABI requirement.
2528 #
2529 $code.=<<___;
2530 .globl  ${PREFIX}_set_encrypt_key
2531 .type   ${PREFIX}_set_encrypt_key,\@abi-omnipotent
2532 .align  16
2533 ${PREFIX}_set_encrypt_key:
2534 __aesni_set_encrypt_key:
2535         .byte   0x48,0x83,0xEC,0x08     # sub rsp,8
2536         mov     \$-1,%rax
2537         test    $inp,$inp
2538         jz      .Lenc_key_ret
2539         test    $key,$key
2540         jz      .Lenc_key_ret
2541
2542         movups  ($inp),%xmm0            # pull first 128 bits of *userKey
2543         xorps   %xmm4,%xmm4             # low dword of xmm4 is assumed 0
2544         lea     16($key),%rax
2545         cmp     \$256,$bits
2546         je      .L14rounds
2547         cmp     \$192,$bits
2548         je      .L12rounds
2549         cmp     \$128,$bits
2550         jne     .Lbad_keybits
2551
2552 .L10rounds:
2553         mov     \$9,$bits                       # 10 rounds for 128-bit key
2554         $movkey %xmm0,($key)                    # round 0
2555         aeskeygenassist \$0x1,%xmm0,%xmm1       # round 1
2556         call            .Lkey_expansion_128_cold
2557         aeskeygenassist \$0x2,%xmm0,%xmm1       # round 2
2558         call            .Lkey_expansion_128
2559         aeskeygenassist \$0x4,%xmm0,%xmm1       # round 3
2560         call            .Lkey_expansion_128
2561         aeskeygenassist \$0x8,%xmm0,%xmm1       # round 4
2562         call            .Lkey_expansion_128
2563         aeskeygenassist \$0x10,%xmm0,%xmm1      # round 5
2564         call            .Lkey_expansion_128
2565         aeskeygenassist \$0x20,%xmm0,%xmm1      # round 6
2566         call            .Lkey_expansion_128
2567         aeskeygenassist \$0x40,%xmm0,%xmm1      # round 7
2568         call            .Lkey_expansion_128
2569         aeskeygenassist \$0x80,%xmm0,%xmm1      # round 8
2570         call            .Lkey_expansion_128
2571         aeskeygenassist \$0x1b,%xmm0,%xmm1      # round 9
2572         call            .Lkey_expansion_128
2573         aeskeygenassist \$0x36,%xmm0,%xmm1      # round 10
2574         call            .Lkey_expansion_128
2575         $movkey %xmm0,(%rax)
2576         mov     $bits,80(%rax)  # 240(%rdx)
2577         xor     %eax,%eax
2578         jmp     .Lenc_key_ret
2579
2580 .align  16
2581 .L12rounds:
2582         movq    16($inp),%xmm2                  # remaining 1/3 of *userKey
2583         mov     \$11,$bits                      # 12 rounds for 192
2584         $movkey %xmm0,($key)                    # round 0
2585         aeskeygenassist \$0x1,%xmm2,%xmm1       # round 1,2
2586         call            .Lkey_expansion_192a_cold
2587         aeskeygenassist \$0x2,%xmm2,%xmm1       # round 2,3
2588         call            .Lkey_expansion_192b
2589         aeskeygenassist \$0x4,%xmm2,%xmm1       # round 4,5
2590         call            .Lkey_expansion_192a
2591         aeskeygenassist \$0x8,%xmm2,%xmm1       # round 5,6
2592         call            .Lkey_expansion_192b
2593         aeskeygenassist \$0x10,%xmm2,%xmm1      # round 7,8
2594         call            .Lkey_expansion_192a
2595         aeskeygenassist \$0x20,%xmm2,%xmm1      # round 8,9
2596         call            .Lkey_expansion_192b
2597         aeskeygenassist \$0x40,%xmm2,%xmm1      # round 10,11
2598         call            .Lkey_expansion_192a
2599         aeskeygenassist \$0x80,%xmm2,%xmm1      # round 11,12
2600         call            .Lkey_expansion_192b
2601         $movkey %xmm0,(%rax)
2602         mov     $bits,48(%rax)  # 240(%rdx)
2603         xor     %rax, %rax
2604         jmp     .Lenc_key_ret
2605
2606 .align  16
2607 .L14rounds:
2608         movups  16($inp),%xmm2                  # remaning half of *userKey
2609         mov     \$13,$bits                      # 14 rounds for 256
2610         lea     16(%rax),%rax
2611         $movkey %xmm0,($key)                    # round 0
2612         $movkey %xmm2,16($key)                  # round 1
2613         aeskeygenassist \$0x1,%xmm2,%xmm1       # round 2
2614         call            .Lkey_expansion_256a_cold
2615         aeskeygenassist \$0x1,%xmm0,%xmm1       # round 3
2616         call            .Lkey_expansion_256b
2617         aeskeygenassist \$0x2,%xmm2,%xmm1       # round 4
2618         call            .Lkey_expansion_256a
2619         aeskeygenassist \$0x2,%xmm0,%xmm1       # round 5
2620         call            .Lkey_expansion_256b
2621         aeskeygenassist \$0x4,%xmm2,%xmm1       # round 6
2622         call            .Lkey_expansion_256a
2623         aeskeygenassist \$0x4,%xmm0,%xmm1       # round 7
2624         call            .Lkey_expansion_256b
2625         aeskeygenassist \$0x8,%xmm2,%xmm1       # round 8
2626         call            .Lkey_expansion_256a
2627         aeskeygenassist \$0x8,%xmm0,%xmm1       # round 9
2628         call            .Lkey_expansion_256b
2629         aeskeygenassist \$0x10,%xmm2,%xmm1      # round 10
2630         call            .Lkey_expansion_256a
2631         aeskeygenassist \$0x10,%xmm0,%xmm1      # round 11
2632         call            .Lkey_expansion_256b
2633         aeskeygenassist \$0x20,%xmm2,%xmm1      # round 12
2634         call            .Lkey_expansion_256a
2635         aeskeygenassist \$0x20,%xmm0,%xmm1      # round 13
2636         call            .Lkey_expansion_256b
2637         aeskeygenassist \$0x40,%xmm2,%xmm1      # round 14
2638         call            .Lkey_expansion_256a
2639         $movkey %xmm0,(%rax)
2640         mov     $bits,16(%rax)  # 240(%rdx)
2641         xor     %rax,%rax
2642         jmp     .Lenc_key_ret
2643
2644 .align  16
2645 .Lbad_keybits:
2646         mov     \$-2,%rax
2647 .Lenc_key_ret:
2648         add     \$8,%rsp
2649         ret
2650 .LSEH_end_set_encrypt_key:
2651 \f
2652 .align  16
2653 .Lkey_expansion_128:
2654         $movkey %xmm0,(%rax)
2655         lea     16(%rax),%rax
2656 .Lkey_expansion_128_cold:
2657         shufps  \$0b00010000,%xmm0,%xmm4
2658         xorps   %xmm4, %xmm0
2659         shufps  \$0b10001100,%xmm0,%xmm4
2660         xorps   %xmm4, %xmm0
2661         shufps  \$0b11111111,%xmm1,%xmm1        # critical path
2662         xorps   %xmm1,%xmm0
2663         ret
2664
2665 .align 16
2666 .Lkey_expansion_192a:
2667         $movkey %xmm0,(%rax)
2668         lea     16(%rax),%rax
2669 .Lkey_expansion_192a_cold:
2670         movaps  %xmm2, %xmm5
2671 .Lkey_expansion_192b_warm:
2672         shufps  \$0b00010000,%xmm0,%xmm4
2673         movdqa  %xmm2,%xmm3
2674         xorps   %xmm4,%xmm0
2675         shufps  \$0b10001100,%xmm0,%xmm4
2676         pslldq  \$4,%xmm3
2677         xorps   %xmm4,%xmm0
2678         pshufd  \$0b01010101,%xmm1,%xmm1        # critical path
2679         pxor    %xmm3,%xmm2
2680         pxor    %xmm1,%xmm0
2681         pshufd  \$0b11111111,%xmm0,%xmm3
2682         pxor    %xmm3,%xmm2
2683         ret
2684
2685 .align 16
2686 .Lkey_expansion_192b:
2687         movaps  %xmm0,%xmm3
2688         shufps  \$0b01000100,%xmm0,%xmm5
2689         $movkey %xmm5,(%rax)
2690         shufps  \$0b01001110,%xmm2,%xmm3
2691         $movkey %xmm3,16(%rax)
2692         lea     32(%rax),%rax
2693         jmp     .Lkey_expansion_192b_warm
2694
2695 .align  16
2696 .Lkey_expansion_256a:
2697         $movkey %xmm2,(%rax)
2698         lea     16(%rax),%rax
2699 .Lkey_expansion_256a_cold:
2700         shufps  \$0b00010000,%xmm0,%xmm4
2701         xorps   %xmm4,%xmm0
2702         shufps  \$0b10001100,%xmm0,%xmm4
2703         xorps   %xmm4,%xmm0
2704         shufps  \$0b11111111,%xmm1,%xmm1        # critical path
2705         xorps   %xmm1,%xmm0
2706         ret
2707
2708 .align 16
2709 .Lkey_expansion_256b:
2710         $movkey %xmm0,(%rax)
2711         lea     16(%rax),%rax
2712
2713         shufps  \$0b00010000,%xmm2,%xmm4
2714         xorps   %xmm4,%xmm2
2715         shufps  \$0b10001100,%xmm2,%xmm4
2716         xorps   %xmm4,%xmm2
2717         shufps  \$0b10101010,%xmm1,%xmm1        # critical path
2718         xorps   %xmm1,%xmm2
2719         ret
2720 .size   ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
2721 .size   __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
2722 ___
2723 }
2724 \f
2725 $code.=<<___;
2726 .align  64
2727 .Lbswap_mask:
2728         .byte   15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
2729 .Lincrement32:
2730         .long   6,6,6,0
2731 .Lincrement64:
2732         .long   1,0,0,0
2733 .Lxts_magic:
2734         .long   0x87,0,1,0
2735
2736 .asciz  "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
2737 .align  64
2738 ___
2739
2740 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
2741 #               CONTEXT *context,DISPATCHER_CONTEXT *disp)
2742 if ($win64) {
2743 $rec="%rcx";
2744 $frame="%rdx";
2745 $context="%r8";
2746 $disp="%r9";
2747
2748 $code.=<<___;
2749 .extern __imp_RtlVirtualUnwind
2750 ___
2751 $code.=<<___ if ($PREFIX eq "aesni");
2752 .type   ecb_se_handler,\@abi-omnipotent
2753 .align  16
2754 ecb_se_handler:
2755         push    %rsi
2756         push    %rdi
2757         push    %rbx
2758         push    %rbp
2759         push    %r12
2760         push    %r13
2761         push    %r14
2762         push    %r15
2763         pushfq
2764         sub     \$64,%rsp
2765
2766         mov     152($context),%rax      # pull context->Rsp
2767
2768         jmp     .Lcommon_seh_tail
2769 .size   ecb_se_handler,.-ecb_se_handler
2770
2771 .type   ccm64_se_handler,\@abi-omnipotent
2772 .align  16
2773 ccm64_se_handler:
2774         push    %rsi
2775         push    %rdi
2776         push    %rbx
2777         push    %rbp
2778         push    %r12
2779         push    %r13
2780         push    %r14
2781         push    %r15
2782         pushfq
2783         sub     \$64,%rsp
2784
2785         mov     120($context),%rax      # pull context->Rax
2786         mov     248($context),%rbx      # pull context->Rip
2787
2788         mov     8($disp),%rsi           # disp->ImageBase
2789         mov     56($disp),%r11          # disp->HandlerData
2790
2791         mov     0(%r11),%r10d           # HandlerData[0]
2792         lea     (%rsi,%r10),%r10        # prologue label
2793         cmp     %r10,%rbx               # context->Rip<prologue label
2794         jb      .Lcommon_seh_tail
2795
2796         mov     152($context),%rax      # pull context->Rsp
2797
2798         mov     4(%r11),%r10d           # HandlerData[1]
2799         lea     (%rsi,%r10),%r10        # epilogue label
2800         cmp     %r10,%rbx               # context->Rip>=epilogue label
2801         jae     .Lcommon_seh_tail
2802
2803         lea     0(%rax),%rsi            # %xmm save area
2804         lea     512($context),%rdi      # &context.Xmm6
2805         mov     \$8,%ecx                # 4*sizeof(%xmm0)/sizeof(%rax)
2806         .long   0xa548f3fc              # cld; rep movsq
2807         lea     0x58(%rax),%rax         # adjust stack pointer
2808
2809         jmp     .Lcommon_seh_tail
2810 .size   ccm64_se_handler,.-ccm64_se_handler
2811
2812 .type   ctr32_se_handler,\@abi-omnipotent
2813 .align  16
2814 ctr32_se_handler:
2815         push    %rsi
2816         push    %rdi
2817         push    %rbx
2818         push    %rbp
2819         push    %r12
2820         push    %r13
2821         push    %r14
2822         push    %r15
2823         pushfq
2824         sub     \$64,%rsp
2825
2826         mov     120($context),%rax      # pull context->Rax
2827         mov     248($context),%rbx      # pull context->Rip
2828
2829         lea     .Lctr32_body(%rip),%r10
2830         cmp     %r10,%rbx               # context->Rip<"prologue" label
2831         jb      .Lcommon_seh_tail
2832
2833         mov     152($context),%rax      # pull context->Rsp
2834
2835         lea     .Lctr32_ret(%rip),%r10
2836         cmp     %r10,%rbx
2837         jae     .Lcommon_seh_tail
2838
2839         lea     0x20(%rax),%rsi         # %xmm save area
2840         lea     512($context),%rdi      # &context.Xmm6
2841         mov     \$20,%ecx               # 10*sizeof(%xmm0)/sizeof(%rax)
2842         .long   0xa548f3fc              # cld; rep movsq
2843
2844         jmp     .Lcommon_rbp_tail
2845 .size   ctr32_se_handler,.-ctr32_se_handler
2846
2847 .type   xts_se_handler,\@abi-omnipotent
2848 .align  16
2849 xts_se_handler:
2850         push    %rsi
2851         push    %rdi
2852         push    %rbx
2853         push    %rbp
2854         push    %r12
2855         push    %r13
2856         push    %r14
2857         push    %r15
2858         pushfq
2859         sub     \$64,%rsp
2860
2861         mov     120($context),%rax      # pull context->Rax
2862         mov     248($context),%rbx      # pull context->Rip
2863
2864         mov     8($disp),%rsi           # disp->ImageBase
2865         mov     56($disp),%r11          # disp->HandlerData
2866
2867         mov     0(%r11),%r10d           # HandlerData[0]
2868         lea     (%rsi,%r10),%r10        # prologue lable
2869         cmp     %r10,%rbx               # context->Rip<prologue label
2870         jb      .Lcommon_seh_tail
2871
2872         mov     152($context),%rax      # pull context->Rsp
2873
2874         mov     4(%r11),%r10d           # HandlerData[1]
2875         lea     (%rsi,%r10),%r10        # epilogue label
2876         cmp     %r10,%rbx               # context->Rip>=epilogue label
2877         jae     .Lcommon_seh_tail
2878
2879         lea     0x60(%rax),%rsi         # %xmm save area
2880         lea     512($context),%rdi      # & context.Xmm6
2881         mov     \$20,%ecx               # 10*sizeof(%xmm0)/sizeof(%rax)
2882         .long   0xa548f3fc              # cld; rep movsq
2883
2884         jmp     .Lcommon_rbp_tail
2885 .size   xts_se_handler,.-xts_se_handler
2886 ___
2887 $code.=<<___;
2888 .type   cbc_se_handler,\@abi-omnipotent
2889 .align  16
2890 cbc_se_handler:
2891         push    %rsi
2892         push    %rdi
2893         push    %rbx
2894         push    %rbp
2895         push    %r12
2896         push    %r13
2897         push    %r14
2898         push    %r15
2899         pushfq
2900         sub     \$64,%rsp
2901
2902         mov     152($context),%rax      # pull context->Rsp
2903         mov     248($context),%rbx      # pull context->Rip
2904
2905         lea     .Lcbc_decrypt(%rip),%r10
2906         cmp     %r10,%rbx               # context->Rip<"prologue" label
2907         jb      .Lcommon_seh_tail
2908
2909         lea     .Lcbc_decrypt_body(%rip),%r10
2910         cmp     %r10,%rbx               # context->Rip<cbc_decrypt_body
2911         jb      .Lrestore_cbc_rax
2912
2913         lea     .Lcbc_ret(%rip),%r10
2914         cmp     %r10,%rbx               # context->Rip>="epilogue" label
2915         jae     .Lcommon_seh_tail
2916
2917         lea     16(%rax),%rsi           # %xmm save area
2918         lea     512($context),%rdi      # &context.Xmm6
2919         mov     \$8,%ecx                # 4*sizeof(%xmm0)/sizeof(%rax)
2920         .long   0xa548f3fc              # cld; rep movsq
2921
2922 .Lcommon_rbp_tail:
2923         mov     160($context),%rax      # pull context->Rbp
2924         mov     (%rax),%rbp             # restore saved %rbp
2925         lea     8(%rax),%rax            # adjust stack pointer
2926         mov     %rbp,160($context)      # restore context->Rbp
2927         jmp     .Lcommon_seh_tail
2928
2929 .Lrestore_cbc_rax:
2930         mov     120($context),%rax
2931
2932 .Lcommon_seh_tail:
2933         mov     8(%rax),%rdi
2934         mov     16(%rax),%rsi
2935         mov     %rax,152($context)      # restore context->Rsp
2936         mov     %rsi,168($context)      # restore context->Rsi
2937         mov     %rdi,176($context)      # restore context->Rdi
2938
2939         mov     40($disp),%rdi          # disp->ContextRecord
2940         mov     $context,%rsi           # context
2941         mov     \$154,%ecx              # sizeof(CONTEXT)
2942         .long   0xa548f3fc              # cld; rep movsq
2943
2944         mov     $disp,%rsi
2945         xor     %rcx,%rcx               # arg1, UNW_FLAG_NHANDLER
2946         mov     8(%rsi),%rdx            # arg2, disp->ImageBase
2947         mov     0(%rsi),%r8             # arg3, disp->ControlPc
2948         mov     16(%rsi),%r9            # arg4, disp->FunctionEntry
2949         mov     40(%rsi),%r10           # disp->ContextRecord
2950         lea     56(%rsi),%r11           # &disp->HandlerData
2951         lea     24(%rsi),%r12           # &disp->EstablisherFrame
2952         mov     %r10,32(%rsp)           # arg5
2953         mov     %r11,40(%rsp)           # arg6
2954         mov     %r12,48(%rsp)           # arg7
2955         mov     %rcx,56(%rsp)           # arg8, (NULL)
2956         call    *__imp_RtlVirtualUnwind(%rip)
2957
2958         mov     \$1,%eax                # ExceptionContinueSearch
2959         add     \$64,%rsp
2960         popfq
2961         pop     %r15
2962         pop     %r14
2963         pop     %r13
2964         pop     %r12
2965         pop     %rbp
2966         pop     %rbx
2967         pop     %rdi
2968         pop     %rsi
2969         ret
2970 .size   cbc_se_handler,.-cbc_se_handler
2971
2972 .section        .pdata
2973 .align  4
2974 ___
2975 $code.=<<___ if ($PREFIX eq "aesni");
2976         .rva    .LSEH_begin_aesni_ecb_encrypt
2977         .rva    .LSEH_end_aesni_ecb_encrypt
2978         .rva    .LSEH_info_ecb
2979
2980         .rva    .LSEH_begin_aesni_ccm64_encrypt_blocks
2981         .rva    .LSEH_end_aesni_ccm64_encrypt_blocks
2982         .rva    .LSEH_info_ccm64_enc
2983
2984         .rva    .LSEH_begin_aesni_ccm64_decrypt_blocks
2985         .rva    .LSEH_end_aesni_ccm64_decrypt_blocks
2986         .rva    .LSEH_info_ccm64_dec
2987
2988         .rva    .LSEH_begin_aesni_ctr32_encrypt_blocks
2989         .rva    .LSEH_end_aesni_ctr32_encrypt_blocks
2990         .rva    .LSEH_info_ctr32
2991
2992         .rva    .LSEH_begin_aesni_xts_encrypt
2993         .rva    .LSEH_end_aesni_xts_encrypt
2994         .rva    .LSEH_info_xts_enc
2995
2996         .rva    .LSEH_begin_aesni_xts_decrypt
2997         .rva    .LSEH_end_aesni_xts_decrypt
2998         .rva    .LSEH_info_xts_dec
2999 ___
3000 $code.=<<___;
3001         .rva    .LSEH_begin_${PREFIX}_cbc_encrypt
3002         .rva    .LSEH_end_${PREFIX}_cbc_encrypt
3003         .rva    .LSEH_info_cbc
3004
3005         .rva    ${PREFIX}_set_decrypt_key
3006         .rva    .LSEH_end_set_decrypt_key
3007         .rva    .LSEH_info_key
3008
3009         .rva    ${PREFIX}_set_encrypt_key
3010         .rva    .LSEH_end_set_encrypt_key
3011         .rva    .LSEH_info_key
3012 .section        .xdata
3013 .align  8
3014 ___
3015 $code.=<<___ if ($PREFIX eq "aesni");
3016 .LSEH_info_ecb:
3017         .byte   9,0,0,0
3018         .rva    ecb_se_handler
3019 .LSEH_info_ccm64_enc:
3020         .byte   9,0,0,0
3021         .rva    ccm64_se_handler
3022         .rva    .Lccm64_enc_body,.Lccm64_enc_ret        # HandlerData[]
3023 .LSEH_info_ccm64_dec:
3024         .byte   9,0,0,0
3025         .rva    ccm64_se_handler
3026         .rva    .Lccm64_dec_body,.Lccm64_dec_ret        # HandlerData[]
3027 .LSEH_info_ctr32:
3028         .byte   9,0,0,0
3029         .rva    ctr32_se_handler
3030 .LSEH_info_xts_enc:
3031         .byte   9,0,0,0
3032         .rva    xts_se_handler
3033         .rva    .Lxts_enc_body,.Lxts_enc_epilogue       # HandlerData[]
3034 .LSEH_info_xts_dec:
3035         .byte   9,0,0,0
3036         .rva    xts_se_handler
3037         .rva    .Lxts_dec_body,.Lxts_dec_epilogue       # HandlerData[]
3038 ___
3039 $code.=<<___;
3040 .LSEH_info_cbc:
3041         .byte   9,0,0,0
3042         .rva    cbc_se_handler
3043 .LSEH_info_key:
3044         .byte   0x01,0x04,0x01,0x00
3045         .byte   0x04,0x02,0x00,0x00     # sub rsp,8
3046 ___
3047 }
3048
3049 sub rex {
3050   local *opcode=shift;
3051   my ($dst,$src)=@_;
3052   my $rex=0;
3053
3054     $rex|=0x04                  if($dst>=8);
3055     $rex|=0x01                  if($src>=8);
3056     push @opcode,$rex|0x40      if($rex);
3057 }
3058
3059 sub aesni {
3060   my $line=shift;
3061   my @opcode=(0x66);
3062
3063     if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
3064         rex(\@opcode,$4,$3);
3065         push @opcode,0x0f,0x3a,0xdf;
3066         push @opcode,0xc0|($3&7)|(($4&7)<<3);   # ModR/M
3067         my $c=$2;
3068         push @opcode,$c=~/^0/?oct($c):$c;
3069         return ".byte\t".join(',',@opcode);
3070     }
3071     elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
3072         my %opcodelet = (
3073                 "aesimc" => 0xdb,
3074                 "aesenc" => 0xdc,       "aesenclast" => 0xdd,
3075                 "aesdec" => 0xde,       "aesdeclast" => 0xdf
3076         );
3077         return undef if (!defined($opcodelet{$1}));
3078         rex(\@opcode,$3,$2);
3079         push @opcode,0x0f,0x38,$opcodelet{$1};
3080         push @opcode,0xc0|($2&7)|(($3&7)<<3);   # ModR/M
3081         return ".byte\t".join(',',@opcode);
3082     }
3083     return $line;
3084 }
3085
3086 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
3087 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;
3088
3089 print $code;
3090
3091 close STDOUT;