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