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