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