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