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