3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> 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 # ====================================================================
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
17 # Given aes(enc|dec) instructions' latency asymptotic performance for
18 # non-parallelizable modes such as CBC encrypt is 3.75 cycles per byte
19 # processed with 128-bit key. And given their throughput asymptotic
20 # performance for parallelizable modes is 1.25 cycles per byte. Being
21 # asymptotic limit it's not something you commonly achieve in reality,
22 # but how close does one get? Below are results collected for
23 # different modes and block sized. Pairs of numbers are for en-/
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
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.
44 # Looking at the results for 8-KB buffer.
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
60 # Looking at how results vary with buffer size.
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.
70 # Results for 192- and 256-bit keys.
72 # EVP-free results were observed to scale perfectly with number of
73 # rounds for larger block sizes, i.e. 192-bit result being 10/12 times
74 # lower and 256-bit one - 10/14. Well, in CBC encrypt case differences
75 # are a tad smaller, because the above mentioned penalty biases all
76 # results by same constant value. In similar way function call
77 # overhead affects small-block performance, as well as OFB and CFB
78 # results. Differences are not large, most common coefficients are
79 # 10/11.7 and 10/13.4 (as opposite to 10/12.0 and 10/14.0), but one
80 # observe even 10/11.2 and 10/12.4 (CTR, OFB, CFB)...
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.
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...
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):
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
128 # "as if" interleave factor 4.7x 5.8x 6.0x
130 # Further data for other parallelizable modes:
132 # CBC decrypt 1.16 0.93 0.74
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.
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.
156 # Add aesni_xts_[en|de]crypt. Westmere spends 1.25 cycles processing
157 # one byte out of 8KB with 128-bit key, Sandy Bridge - 0.90. Just like
158 # in CTR mode AES instruction interleave factor was chosen to be 6x.
160 ######################################################################
161 # Current large-block performance in cycles per byte processed with
162 # 128-bit key (less is better).
164 # CBC en-/decrypt CTR XTS ECB
165 # Westmere 3.77/1.25 1.25 1.25 1.26
166 # * Bridge 5.07/0.74 0.75 0.90 0.85
167 # Haswell 4.44/0.63 0.63 0.73 0.63
168 # Silvermont 5.75/3.54 3.56 4.12 3.87(*)
169 # Bulldozer 5.77/0.70 0.72 0.90 0.70
171 # (*) Atom Silvermont ECB result is suboptimal because of penalties
172 # incurred by operations on %xmm8-15. As ECB is not considered
173 # critical, nothing was done to mitigate the problem.
175 $PREFIX="aesni"; # if $PREFIX is set to "AES", the script
176 # generates drop-in replacement for
177 # crypto/aes/asm/aes-x86_64.pl:-)
181 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
183 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
185 $0 =~ m/(.*[\/\\])[^\
/\\]+$/; $dir=$1;
186 ( $xlate="${dir}x86_64-xlate.pl" and -f
$xlate ) or
187 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f
$xlate) or
188 die "can't locate x86_64-xlate.pl";
190 open OUT
,"| \"$^X\" $xlate $flavour $output";
193 $movkey = $PREFIX eq "aesni" ?
"movups" : "movups";
194 @_4args=$win64?
("%rcx","%rdx","%r8", "%r9") : # Win64 order
195 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
198 $code.=".extern OPENSSL_ia32cap_P\n";
200 $rounds="%eax"; # input to and changed by aesni_[en|de]cryptN !!!
201 # this is natural Unix argument order for public $PREFIX_[ecb|cbc]_encrypt ...
205 $key="%rcx"; # input to and changed by aesni_[en|de]cryptN !!!
206 $ivp="%r8"; # cbc, ctr, ...
208 $rnds_="%r10d"; # backup copy for $rounds
209 $key_="%r11"; # backup copy for $key
211 # %xmm register layout
212 $rndkey0="%xmm0"; $rndkey1="%xmm1";
213 $inout0="%xmm2"; $inout1="%xmm3";
214 $inout2="%xmm4"; $inout3="%xmm5";
215 $inout4="%xmm6"; $inout5="%xmm7";
216 $inout6="%xmm8"; $inout7="%xmm9";
218 $in2="%xmm6"; $in1="%xmm7"; # used in CBC decrypt, CTR, ...
219 $in0="%xmm8"; $iv="%xmm9";
221 # Inline version of internal aesni_[en|de]crypt1.
223 # Why folded loop? Because aes[enc|dec] is slow enough to accommodate
224 # cycles which take care of loop variables...
226 sub aesni_generate1
{
227 my ($p,$key,$rounds,$inout,$ivec)=@_; $inout=$inout0 if (!defined($inout));
230 $movkey ($key),$rndkey0
231 $movkey 16($key),$rndkey1
233 $code.=<<___
if (defined($ivec));
238 $code.=<<___
if (!defined($ivec));
240 xorps
$rndkey0,$inout
244 aes
${p
} $rndkey1,$inout
246 $movkey ($key),$rndkey1
248 jnz
.Loop_
${p
}1_
$sn # loop body is 16 bytes
249 aes
${p
}last $rndkey1,$inout
252 # void $PREFIX_[en|de]crypt (const void *inp,void *out,const AES_KEY *key);
254 { my ($inp,$out,$key) = @_4args;
257 .globl
${PREFIX
}_encrypt
258 .type
${PREFIX
}_encrypt
,\
@abi-omnipotent
261 movups
($inp),$inout0 # load input
262 mov
240($key),$rounds # key->rounds
264 &aesni_generate1
("enc",$key,$rounds);
266 pxor
$rndkey0,$rndkey0 # clear register bank
267 pxor
$rndkey1,$rndkey1
268 movups
$inout0,($out) # output
271 .size
${PREFIX
}_encrypt
,.-${PREFIX
}_encrypt
273 .globl
${PREFIX
}_decrypt
274 .type
${PREFIX
}_decrypt
,\
@abi-omnipotent
277 movups
($inp),$inout0 # load input
278 mov
240($key),$rounds # key->rounds
280 &aesni_generate1
("dec",$key,$rounds);
282 pxor
$rndkey0,$rndkey0 # clear register bank
283 pxor
$rndkey1,$rndkey1
284 movups
$inout0,($out) # output
287 .size
${PREFIX
}_decrypt
, .-${PREFIX
}_decrypt
291 # _aesni_[en|de]cryptN are private interfaces, N denotes interleave
292 # factor. Why 3x subroutine were originally used in loops? Even though
293 # aes[enc|dec] latency was originally 6, it could be scheduled only
294 # every *2nd* cycle. Thus 3x interleave was the one providing optimal
295 # utilization, i.e. when subroutine's throughput is virtually same as
296 # of non-interleaved subroutine [for number of input blocks up to 3].
297 # This is why it originally made no sense to implement 2x subroutine.
298 # But times change and it became appropriate to spend extra 192 bytes
299 # on 2x subroutine on Atom Silvermont account. For processors that
300 # can schedule aes[enc|dec] every cycle optimal interleave factor
301 # equals to corresponding instructions latency. 8x is optimal for
302 # * Bridge and "super-optimal" for other Intel CPUs...
304 sub aesni_generate2
{
306 # As already mentioned it takes in $key and $rounds, which are *not*
307 # preserved. $inout[0-1] is cipher/clear text...
309 .type _aesni_
${dir
}rypt2
,\
@abi-omnipotent
312 $movkey ($key),$rndkey0
314 $movkey 16($key),$rndkey1
315 xorps
$rndkey0,$inout0
316 xorps
$rndkey0,$inout1
317 $movkey 32($key),$rndkey0
318 lea
32($key,$rounds),$key
323 aes
${dir
} $rndkey1,$inout0
324 aes
${dir
} $rndkey1,$inout1
325 $movkey ($key,%rax),$rndkey1
327 aes
${dir
} $rndkey0,$inout0
328 aes
${dir
} $rndkey0,$inout1
329 $movkey -16($key,%rax),$rndkey0
332 aes
${dir
} $rndkey1,$inout0
333 aes
${dir
} $rndkey1,$inout1
334 aes
${dir
}last $rndkey0,$inout0
335 aes
${dir
}last $rndkey0,$inout1
337 .size _aesni_
${dir
}rypt2
,.-_aesni_
${dir
}rypt2
340 sub aesni_generate3
{
342 # As already mentioned it takes in $key and $rounds, which are *not*
343 # preserved. $inout[0-2] is cipher/clear text...
345 .type _aesni_
${dir
}rypt3
,\
@abi-omnipotent
348 $movkey ($key),$rndkey0
350 $movkey 16($key),$rndkey1
351 xorps
$rndkey0,$inout0
352 xorps
$rndkey0,$inout1
353 xorps
$rndkey0,$inout2
354 $movkey 32($key),$rndkey0
355 lea
32($key,$rounds),$key
360 aes
${dir
} $rndkey1,$inout0
361 aes
${dir
} $rndkey1,$inout1
362 aes
${dir
} $rndkey1,$inout2
363 $movkey ($key,%rax),$rndkey1
365 aes
${dir
} $rndkey0,$inout0
366 aes
${dir
} $rndkey0,$inout1
367 aes
${dir
} $rndkey0,$inout2
368 $movkey -16($key,%rax),$rndkey0
371 aes
${dir
} $rndkey1,$inout0
372 aes
${dir
} $rndkey1,$inout1
373 aes
${dir
} $rndkey1,$inout2
374 aes
${dir
}last $rndkey0,$inout0
375 aes
${dir
}last $rndkey0,$inout1
376 aes
${dir
}last $rndkey0,$inout2
378 .size _aesni_
${dir
}rypt3
,.-_aesni_
${dir
}rypt3
381 # 4x interleave is implemented to improve small block performance,
382 # most notably [and naturally] 4 block by ~30%. One can argue that one
383 # should have implemented 5x as well, but improvement would be <20%,
384 # so it's not worth it...
385 sub aesni_generate4
{
387 # As already mentioned it takes in $key and $rounds, which are *not*
388 # preserved. $inout[0-3] is cipher/clear text...
390 .type _aesni_
${dir
}rypt4
,\
@abi-omnipotent
393 $movkey ($key),$rndkey0
395 $movkey 16($key),$rndkey1
396 xorps
$rndkey0,$inout0
397 xorps
$rndkey0,$inout1
398 xorps
$rndkey0,$inout2
399 xorps
$rndkey0,$inout3
400 $movkey 32($key),$rndkey0
401 lea
32($key,$rounds),$key
407 aes
${dir
} $rndkey1,$inout0
408 aes
${dir
} $rndkey1,$inout1
409 aes
${dir
} $rndkey1,$inout2
410 aes
${dir
} $rndkey1,$inout3
411 $movkey ($key,%rax),$rndkey1
413 aes
${dir
} $rndkey0,$inout0
414 aes
${dir
} $rndkey0,$inout1
415 aes
${dir
} $rndkey0,$inout2
416 aes
${dir
} $rndkey0,$inout3
417 $movkey -16($key,%rax),$rndkey0
420 aes
${dir
} $rndkey1,$inout0
421 aes
${dir
} $rndkey1,$inout1
422 aes
${dir
} $rndkey1,$inout2
423 aes
${dir
} $rndkey1,$inout3
424 aes
${dir
}last $rndkey0,$inout0
425 aes
${dir
}last $rndkey0,$inout1
426 aes
${dir
}last $rndkey0,$inout2
427 aes
${dir
}last $rndkey0,$inout3
429 .size _aesni_
${dir
}rypt4
,.-_aesni_
${dir
}rypt4
432 sub aesni_generate6
{
434 # As already mentioned it takes in $key and $rounds, which are *not*
435 # preserved. $inout[0-5] is cipher/clear text...
437 .type _aesni_
${dir
}rypt6
,\
@abi-omnipotent
440 $movkey ($key),$rndkey0
442 $movkey 16($key),$rndkey1
443 xorps
$rndkey0,$inout0
444 pxor
$rndkey0,$inout1
445 pxor
$rndkey0,$inout2
446 aes
${dir
} $rndkey1,$inout0
447 lea
32($key,$rounds),$key
449 aes
${dir
} $rndkey1,$inout1
450 pxor
$rndkey0,$inout3
451 pxor
$rndkey0,$inout4
452 aes
${dir
} $rndkey1,$inout2
453 pxor
$rndkey0,$inout5
454 $movkey ($key,%rax),$rndkey0
456 jmp
.L
${dir
}_loop6_enter
459 aes
${dir
} $rndkey1,$inout0
460 aes
${dir
} $rndkey1,$inout1
461 aes
${dir
} $rndkey1,$inout2
462 .L
${dir
}_loop6_enter
:
463 aes
${dir
} $rndkey1,$inout3
464 aes
${dir
} $rndkey1,$inout4
465 aes
${dir
} $rndkey1,$inout5
466 $movkey ($key,%rax),$rndkey1
468 aes
${dir
} $rndkey0,$inout0
469 aes
${dir
} $rndkey0,$inout1
470 aes
${dir
} $rndkey0,$inout2
471 aes
${dir
} $rndkey0,$inout3
472 aes
${dir
} $rndkey0,$inout4
473 aes
${dir
} $rndkey0,$inout5
474 $movkey -16($key,%rax),$rndkey0
477 aes
${dir
} $rndkey1,$inout0
478 aes
${dir
} $rndkey1,$inout1
479 aes
${dir
} $rndkey1,$inout2
480 aes
${dir
} $rndkey1,$inout3
481 aes
${dir
} $rndkey1,$inout4
482 aes
${dir
} $rndkey1,$inout5
483 aes
${dir
}last $rndkey0,$inout0
484 aes
${dir
}last $rndkey0,$inout1
485 aes
${dir
}last $rndkey0,$inout2
486 aes
${dir
}last $rndkey0,$inout3
487 aes
${dir
}last $rndkey0,$inout4
488 aes
${dir
}last $rndkey0,$inout5
490 .size _aesni_
${dir
}rypt6
,.-_aesni_
${dir
}rypt6
493 sub aesni_generate8
{
495 # As already mentioned it takes in $key and $rounds, which are *not*
496 # preserved. $inout[0-7] is cipher/clear text...
498 .type _aesni_
${dir
}rypt8
,\
@abi-omnipotent
501 $movkey ($key),$rndkey0
503 $movkey 16($key),$rndkey1
504 xorps
$rndkey0,$inout0
505 xorps
$rndkey0,$inout1
506 pxor
$rndkey0,$inout2
507 pxor
$rndkey0,$inout3
508 pxor
$rndkey0,$inout4
509 lea
32($key,$rounds),$key
511 aes
${dir
} $rndkey1,$inout0
512 pxor
$rndkey0,$inout5
513 pxor
$rndkey0,$inout6
514 aes
${dir
} $rndkey1,$inout1
515 pxor
$rndkey0,$inout7
516 $movkey ($key,%rax),$rndkey0
518 jmp
.L
${dir
}_loop8_inner
521 aes
${dir
} $rndkey1,$inout0
522 aes
${dir
} $rndkey1,$inout1
523 .L
${dir
}_loop8_inner
:
524 aes
${dir
} $rndkey1,$inout2
525 aes
${dir
} $rndkey1,$inout3
526 aes
${dir
} $rndkey1,$inout4
527 aes
${dir
} $rndkey1,$inout5
528 aes
${dir
} $rndkey1,$inout6
529 aes
${dir
} $rndkey1,$inout7
530 .L
${dir
}_loop8_enter
:
531 $movkey ($key,%rax),$rndkey1
533 aes
${dir
} $rndkey0,$inout0
534 aes
${dir
} $rndkey0,$inout1
535 aes
${dir
} $rndkey0,$inout2
536 aes
${dir
} $rndkey0,$inout3
537 aes
${dir
} $rndkey0,$inout4
538 aes
${dir
} $rndkey0,$inout5
539 aes
${dir
} $rndkey0,$inout6
540 aes
${dir
} $rndkey0,$inout7
541 $movkey -16($key,%rax),$rndkey0
544 aes
${dir
} $rndkey1,$inout0
545 aes
${dir
} $rndkey1,$inout1
546 aes
${dir
} $rndkey1,$inout2
547 aes
${dir
} $rndkey1,$inout3
548 aes
${dir
} $rndkey1,$inout4
549 aes
${dir
} $rndkey1,$inout5
550 aes
${dir
} $rndkey1,$inout6
551 aes
${dir
} $rndkey1,$inout7
552 aes
${dir
}last $rndkey0,$inout0
553 aes
${dir
}last $rndkey0,$inout1
554 aes
${dir
}last $rndkey0,$inout2
555 aes
${dir
}last $rndkey0,$inout3
556 aes
${dir
}last $rndkey0,$inout4
557 aes
${dir
}last $rndkey0,$inout5
558 aes
${dir
}last $rndkey0,$inout6
559 aes
${dir
}last $rndkey0,$inout7
561 .size _aesni_
${dir
}rypt8
,.-_aesni_
${dir
}rypt8
564 &aesni_generate2
("enc") if ($PREFIX eq "aesni");
565 &aesni_generate2
("dec");
566 &aesni_generate3
("enc") if ($PREFIX eq "aesni");
567 &aesni_generate3
("dec");
568 &aesni_generate4
("enc") if ($PREFIX eq "aesni");
569 &aesni_generate4
("dec");
570 &aesni_generate6
("enc") if ($PREFIX eq "aesni");
571 &aesni_generate6
("dec");
572 &aesni_generate8
("enc") if ($PREFIX eq "aesni");
573 &aesni_generate8
("dec");
575 if ($PREFIX eq "aesni") {
576 ########################################################################
577 # void aesni_ecb_encrypt (const void *in, void *out,
578 # size_t length, const AES_KEY *key,
581 .globl aesni_ecb_encrypt
582 .type aesni_ecb_encrypt
,\
@function,5
586 $code.=<<___
if ($win64);
588 movaps
%xmm6,(%rsp) # offload $inout4..7
589 movaps
%xmm7,0x10(%rsp)
590 movaps
%xmm8,0x20(%rsp)
591 movaps
%xmm9,0x30(%rsp)
595 and \
$-16,$len # if ($len<16)
596 jz
.Lecb_ret
# return
598 mov
240($key),$rounds # key->rounds
599 $movkey ($key),$rndkey0
600 mov
$key,$key_ # backup $key
601 mov
$rounds,$rnds_ # backup $rounds
602 test
%r8d,%r8d # 5th argument
604 #--------------------------- ECB ENCRYPT ------------------------------#
605 cmp \
$0x80,$len # if ($len<8*16)
606 jb
.Lecb_enc_tail
# short input
608 movdqu
($inp),$inout0 # load 8 input blocks
609 movdqu
0x10($inp),$inout1
610 movdqu
0x20($inp),$inout2
611 movdqu
0x30($inp),$inout3
612 movdqu
0x40($inp),$inout4
613 movdqu
0x50($inp),$inout5
614 movdqu
0x60($inp),$inout6
615 movdqu
0x70($inp),$inout7
616 lea
0x80($inp),$inp # $inp+=8*16
617 sub \
$0x80,$len # $len-=8*16 (can be zero)
618 jmp
.Lecb_enc_loop8_enter
621 movups
$inout0,($out) # store 8 output blocks
622 mov
$key_,$key # restore $key
623 movdqu
($inp),$inout0 # load 8 input blocks
624 mov
$rnds_,$rounds # restore $rounds
625 movups
$inout1,0x10($out)
626 movdqu
0x10($inp),$inout1
627 movups
$inout2,0x20($out)
628 movdqu
0x20($inp),$inout2
629 movups
$inout3,0x30($out)
630 movdqu
0x30($inp),$inout3
631 movups
$inout4,0x40($out)
632 movdqu
0x40($inp),$inout4
633 movups
$inout5,0x50($out)
634 movdqu
0x50($inp),$inout5
635 movups
$inout6,0x60($out)
636 movdqu
0x60($inp),$inout6
637 movups
$inout7,0x70($out)
638 lea
0x80($out),$out # $out+=8*16
639 movdqu
0x70($inp),$inout7
640 lea
0x80($inp),$inp # $inp+=8*16
641 .Lecb_enc_loop8_enter
:
646 jnc
.Lecb_enc_loop8
# loop if $len-=8*16 didn't borrow
648 movups
$inout0,($out) # store 8 output blocks
649 mov
$key_,$key # restore $key
650 movups
$inout1,0x10($out)
651 mov
$rnds_,$rounds # restore $rounds
652 movups
$inout2,0x20($out)
653 movups
$inout3,0x30($out)
654 movups
$inout4,0x40($out)
655 movups
$inout5,0x50($out)
656 movups
$inout6,0x60($out)
657 movups
$inout7,0x70($out)
658 lea
0x80($out),$out # $out+=8*16
659 add \
$0x80,$len # restore real remaining $len
660 jz
.Lecb_ret
# done if ($len==0)
662 .Lecb_enc_tail
: # $len is less than 8*16
663 movups
($inp),$inout0
666 movups
0x10($inp),$inout1
668 movups
0x20($inp),$inout2
671 movups
0x30($inp),$inout3
673 movups
0x40($inp),$inout4
676 movups
0x50($inp),$inout5
678 movdqu
0x60($inp),$inout6
679 xorps
$inout7,$inout7
681 movups
$inout0,($out) # store 7 output blocks
682 movups
$inout1,0x10($out)
683 movups
$inout2,0x20($out)
684 movups
$inout3,0x30($out)
685 movups
$inout4,0x40($out)
686 movups
$inout5,0x50($out)
687 movups
$inout6,0x60($out)
692 &aesni_generate1
("enc",$key,$rounds);
694 movups
$inout0,($out) # store one output block
699 movups
$inout0,($out) # store 2 output blocks
700 movups
$inout1,0x10($out)
705 movups
$inout0,($out) # store 3 output blocks
706 movups
$inout1,0x10($out)
707 movups
$inout2,0x20($out)
712 movups
$inout0,($out) # store 4 output blocks
713 movups
$inout1,0x10($out)
714 movups
$inout2,0x20($out)
715 movups
$inout3,0x30($out)
719 xorps
$inout5,$inout5
721 movups
$inout0,($out) # store 5 output blocks
722 movups
$inout1,0x10($out)
723 movups
$inout2,0x20($out)
724 movups
$inout3,0x30($out)
725 movups
$inout4,0x40($out)
730 movups
$inout0,($out) # store 6 output blocks
731 movups
$inout1,0x10($out)
732 movups
$inout2,0x20($out)
733 movups
$inout3,0x30($out)
734 movups
$inout4,0x40($out)
735 movups
$inout5,0x50($out)
737 \f#--------------------------- ECB DECRYPT ------------------------------#
740 cmp \
$0x80,$len # if ($len<8*16)
741 jb
.Lecb_dec_tail
# short input
743 movdqu
($inp),$inout0 # load 8 input blocks
744 movdqu
0x10($inp),$inout1
745 movdqu
0x20($inp),$inout2
746 movdqu
0x30($inp),$inout3
747 movdqu
0x40($inp),$inout4
748 movdqu
0x50($inp),$inout5
749 movdqu
0x60($inp),$inout6
750 movdqu
0x70($inp),$inout7
751 lea
0x80($inp),$inp # $inp+=8*16
752 sub \
$0x80,$len # $len-=8*16 (can be zero)
753 jmp
.Lecb_dec_loop8_enter
756 movups
$inout0,($out) # store 8 output blocks
757 mov
$key_,$key # restore $key
758 movdqu
($inp),$inout0 # load 8 input blocks
759 mov
$rnds_,$rounds # restore $rounds
760 movups
$inout1,0x10($out)
761 movdqu
0x10($inp),$inout1
762 movups
$inout2,0x20($out)
763 movdqu
0x20($inp),$inout2
764 movups
$inout3,0x30($out)
765 movdqu
0x30($inp),$inout3
766 movups
$inout4,0x40($out)
767 movdqu
0x40($inp),$inout4
768 movups
$inout5,0x50($out)
769 movdqu
0x50($inp),$inout5
770 movups
$inout6,0x60($out)
771 movdqu
0x60($inp),$inout6
772 movups
$inout7,0x70($out)
773 lea
0x80($out),$out # $out+=8*16
774 movdqu
0x70($inp),$inout7
775 lea
0x80($inp),$inp # $inp+=8*16
776 .Lecb_dec_loop8_enter
:
780 $movkey ($key_),$rndkey0
782 jnc
.Lecb_dec_loop8
# loop if $len-=8*16 didn't borrow
784 movups
$inout0,($out) # store 8 output blocks
785 pxor
$inout0,$inout0 # clear register bank
786 mov
$key_,$key # restore $key
787 movups
$inout1,0x10($out)
789 mov
$rnds_,$rounds # restore $rounds
790 movups
$inout2,0x20($out)
792 movups
$inout3,0x30($out)
794 movups
$inout4,0x40($out)
796 movups
$inout5,0x50($out)
798 movups
$inout6,0x60($out)
800 movups
$inout7,0x70($out)
802 lea
0x80($out),$out # $out+=8*16
803 add \
$0x80,$len # restore real remaining $len
804 jz
.Lecb_ret
# done if ($len==0)
807 movups
($inp),$inout0
810 movups
0x10($inp),$inout1
812 movups
0x20($inp),$inout2
815 movups
0x30($inp),$inout3
817 movups
0x40($inp),$inout4
820 movups
0x50($inp),$inout5
822 movups
0x60($inp),$inout6
823 $movkey ($key),$rndkey0
824 xorps
$inout7,$inout7
826 movups
$inout0,($out) # store 7 output blocks
827 pxor
$inout0,$inout0 # clear register bank
828 movups
$inout1,0x10($out)
830 movups
$inout2,0x20($out)
832 movups
$inout3,0x30($out)
834 movups
$inout4,0x40($out)
836 movups
$inout5,0x50($out)
838 movups
$inout6,0x60($out)
845 &aesni_generate1
("dec",$key,$rounds);
847 movups
$inout0,($out) # store one output block
848 pxor
$inout0,$inout0 # clear register bank
853 movups
$inout0,($out) # store 2 output blocks
854 pxor
$inout0,$inout0 # clear register bank
855 movups
$inout1,0x10($out)
861 movups
$inout0,($out) # store 3 output blocks
862 pxor
$inout0,$inout0 # clear register bank
863 movups
$inout1,0x10($out)
865 movups
$inout2,0x20($out)
871 movups
$inout0,($out) # store 4 output blocks
872 pxor
$inout0,$inout0 # clear register bank
873 movups
$inout1,0x10($out)
875 movups
$inout2,0x20($out)
877 movups
$inout3,0x30($out)
882 xorps
$inout5,$inout5
884 movups
$inout0,($out) # store 5 output blocks
885 pxor
$inout0,$inout0 # clear register bank
886 movups
$inout1,0x10($out)
888 movups
$inout2,0x20($out)
890 movups
$inout3,0x30($out)
892 movups
$inout4,0x40($out)
899 movups
$inout0,($out) # store 6 output blocks
900 pxor
$inout0,$inout0 # clear register bank
901 movups
$inout1,0x10($out)
903 movups
$inout2,0x20($out)
905 movups
$inout3,0x30($out)
907 movups
$inout4,0x40($out)
909 movups
$inout5,0x50($out)
913 xorps
$rndkey0,$rndkey0 # %xmm0
914 pxor
$rndkey1,$rndkey1
916 $code.=<<___
if ($win64);
918 movaps
%xmm0,(%rsp) # clear stack
919 movaps
0x10(%rsp),%xmm7
920 movaps
%xmm0,0x10(%rsp)
921 movaps
0x20(%rsp),%xmm8
922 movaps
%xmm0,0x20(%rsp)
923 movaps
0x30(%rsp),%xmm9
924 movaps
%xmm0,0x30(%rsp)
930 .size aesni_ecb_encrypt
,.-aesni_ecb_encrypt
934 ######################################################################
935 # void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out,
936 # size_t blocks, const AES_KEY *key,
937 # const char *ivec,char *cmac);
939 # Handles only complete blocks, operates on 64-bit counter and
940 # does not update *ivec! Nor does it finalize CMAC value
941 # (see engine/eng_aesni.c for details)
944 my $cmac="%r9"; # 6th argument
946 my $increment="%xmm9";
948 my $bswap_mask="%xmm7";
951 .globl aesni_ccm64_encrypt_blocks
952 .type aesni_ccm64_encrypt_blocks
,\
@function,6
954 aesni_ccm64_encrypt_blocks
:
956 $code.=<<___
if ($win64);
958 movaps
%xmm6,(%rsp) # $iv
959 movaps
%xmm7,0x10(%rsp) # $bswap_mask
960 movaps
%xmm8,0x20(%rsp) # $in0
961 movaps
%xmm9,0x30(%rsp) # $increment
965 mov
240($key),$rounds # key->rounds
967 movdqa
.Lincrement64
(%rip),$increment
968 movdqa
.Lbswap_mask
(%rip),$bswap_mask
973 movdqu
($cmac),$inout1
975 lea
32($key,$rounds),$key # end of key schedule
976 pshufb
$bswap_mask,$iv
977 sub %rax,%r10 # twisted $rounds
978 jmp
.Lccm64_enc_outer
981 $movkey ($key_),$rndkey0
983 movups
($inp),$in0 # load inp
985 xorps
$rndkey0,$inout0 # counter
986 $movkey 16($key_),$rndkey1
988 xorps
$rndkey0,$inout1 # cmac^=inp
989 $movkey 32($key_),$rndkey0
992 aesenc
$rndkey1,$inout0
993 aesenc
$rndkey1,$inout1
994 $movkey ($key,%rax),$rndkey1
996 aesenc
$rndkey0,$inout0
997 aesenc
$rndkey0,$inout1
998 $movkey -16($key,%rax),$rndkey0
999 jnz
.Lccm64_enc2_loop
1000 aesenc
$rndkey1,$inout0
1001 aesenc
$rndkey1,$inout1
1002 paddq
$increment,$iv
1003 dec
$len # $len-- ($len is in blocks)
1004 aesenclast
$rndkey0,$inout0
1005 aesenclast
$rndkey0,$inout1
1008 xorps
$inout0,$in0 # inp ^= E(iv)
1010 movups
$in0,($out) # save output
1011 pshufb
$bswap_mask,$inout0
1012 lea
16($out),$out # $out+=16
1013 jnz
.Lccm64_enc_outer
# loop if ($len!=0)
1015 pxor
$rndkey0,$rndkey0 # clear register bank
1016 pxor
$rndkey1,$rndkey1
1017 pxor
$inout0,$inout0
1018 movups
$inout1,($cmac) # store resulting mac
1019 pxor
$inout1,$inout1
1023 $code.=<<___
if ($win64);
1025 movaps
%xmm0,(%rsp) # clear stack
1026 movaps
0x10(%rsp),%xmm7
1027 movaps
%xmm0,0x10(%rsp)
1028 movaps
0x20(%rsp),%xmm8
1029 movaps
%xmm0,0x20(%rsp)
1030 movaps
0x30(%rsp),%xmm9
1031 movaps
%xmm0,0x30(%rsp)
1037 .size aesni_ccm64_encrypt_blocks
,.-aesni_ccm64_encrypt_blocks
1039 ######################################################################
1041 .globl aesni_ccm64_decrypt_blocks
1042 .type aesni_ccm64_decrypt_blocks
,\
@function,6
1044 aesni_ccm64_decrypt_blocks
:
1046 $code.=<<___
if ($win64);
1047 lea
-0x58(%rsp),%rsp
1048 movaps
%xmm6,(%rsp) # $iv
1049 movaps
%xmm7,0x10(%rsp) # $bswap_mask
1050 movaps
%xmm8,0x20(%rsp) # $in8
1051 movaps
%xmm9,0x30(%rsp) # $increment
1055 mov
240($key),$rounds # key->rounds
1057 movdqu
($cmac),$inout1
1058 movdqa
.Lincrement64
(%rip),$increment
1059 movdqa
.Lbswap_mask
(%rip),$bswap_mask
1064 pshufb
$bswap_mask,$iv
1066 &aesni_generate1
("enc",$key,$rounds);
1070 movups
($inp),$in0 # load inp
1071 paddq
$increment,$iv
1072 lea
16($inp),$inp # $inp+=16
1073 sub %r10,%rax # twisted $rounds
1074 lea
32($key_,$rnds_),$key # end of key schedule
1076 jmp
.Lccm64_dec_outer
1079 xorps
$inout0,$in0 # inp ^= E(iv)
1081 movups
$in0,($out) # save output
1082 lea
16($out),$out # $out+=16
1083 pshufb
$bswap_mask,$inout0
1085 sub \
$1,$len # $len-- ($len is in blocks)
1086 jz
.Lccm64_dec_break
# if ($len==0) break
1088 $movkey ($key_),$rndkey0
1090 $movkey 16($key_),$rndkey1
1092 xorps
$rndkey0,$inout0
1093 xorps
$in0,$inout1 # cmac^=out
1094 $movkey 32($key_),$rndkey0
1095 jmp
.Lccm64_dec2_loop
1098 aesenc
$rndkey1,$inout0
1099 aesenc
$rndkey1,$inout1
1100 $movkey ($key,%rax),$rndkey1
1102 aesenc
$rndkey0,$inout0
1103 aesenc
$rndkey0,$inout1
1104 $movkey -16($key,%rax),$rndkey0
1105 jnz
.Lccm64_dec2_loop
1106 movups
($inp),$in0 # load input
1107 paddq
$increment,$iv
1108 aesenc
$rndkey1,$inout0
1109 aesenc
$rndkey1,$inout1
1110 aesenclast
$rndkey0,$inout0
1111 aesenclast
$rndkey0,$inout1
1112 lea
16($inp),$inp # $inp+=16
1113 jmp
.Lccm64_dec_outer
1117 #xorps $in0,$inout1 # cmac^=out
1118 mov
240($key_),$rounds
1120 &aesni_generate1
("enc",$key_,$rounds,$inout1,$in0);
1122 pxor
$rndkey0,$rndkey0 # clear register bank
1123 pxor
$rndkey1,$rndkey1
1124 pxor
$inout0,$inout0
1125 movups
$inout1,($cmac) # store resulting mac
1126 pxor
$inout1,$inout1
1130 $code.=<<___
if ($win64);
1132 movaps
%xmm0,(%rsp) # clear stack
1133 movaps
0x10(%rsp),%xmm7
1134 movaps
%xmm0,0x10(%rsp)
1135 movaps
0x20(%rsp),%xmm8
1136 movaps
%xmm0,0x20(%rsp)
1137 movaps
0x30(%rsp),%xmm9
1138 movaps
%xmm0,0x30(%rsp)
1144 .size aesni_ccm64_decrypt_blocks
,.-aesni_ccm64_decrypt_blocks
1147 ######################################################################
1148 # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
1149 # size_t blocks, const AES_KEY *key,
1150 # const char *ivec);
1152 # Handles only complete blocks, operates on 32-bit counter and
1153 # does not update *ivec! (see crypto/modes/ctr128.c for details)
1155 # Overhaul based on suggestions from Shay Gueron and Vlad Krasnov,
1156 # http://rt.openssl.org/Ticket/Display.html?id=3021&user=guest&pass=guest.
1157 # Keywords are full unroll and modulo-schedule counter calculations
1158 # with zero-round key xor.
1160 my ($in0,$in1,$in2,$in3,$in4,$in5)=map("%xmm$_",(10..15));
1161 my ($key0,$ctr)=("${key_}d","${ivp}d");
1162 my $frame_size = 0x80 + ($win64?
160:0);
1165 .globl aesni_ctr32_encrypt_blocks
1166 .type aesni_ctr32_encrypt_blocks
,\
@function,5
1168 aesni_ctr32_encrypt_blocks
:
1172 # handle single block without allocating stack frame,
1173 # useful when handling edges
1174 movups
($ivp),$inout0
1175 movups
($inp),$inout1
1176 mov
240($key),%edx # key->rounds
1178 &aesni_generate1
("enc",$key,"%edx");
1180 pxor
$rndkey0,$rndkey0 # clear register bank
1181 pxor
$rndkey1,$rndkey1
1182 xorps
$inout1,$inout0
1183 pxor
$inout1,$inout1
1184 movups
$inout0,($out)
1185 xorps
$inout0,$inout0
1186 jmp
.Lctr32_epilogue
1192 sub \
$$frame_size,%rsp
1193 and \
$-16,%rsp # Linux kernel stack can be incorrectly seeded
1195 $code.=<<___
if ($win64);
1196 movaps
%xmm6,-0xa8(%rax) # offload everything
1197 movaps
%xmm7,-0x98(%rax)
1198 movaps
%xmm8,-0x88(%rax)
1199 movaps
%xmm9,-0x78(%rax)
1200 movaps
%xmm10,-0x68(%rax)
1201 movaps
%xmm11,-0x58(%rax)
1202 movaps
%xmm12,-0x48(%rax)
1203 movaps
%xmm13,-0x38(%rax)
1204 movaps
%xmm14,-0x28(%rax)
1205 movaps
%xmm15,-0x18(%rax)
1211 # 8 16-byte words on top of stack are counter values
1212 # xor-ed with zero-round key
1214 movdqu
($ivp),$inout0
1215 movdqu
($key),$rndkey0
1216 mov
12($ivp),$ctr # counter LSB
1217 pxor
$rndkey0,$inout0
1218 mov
12($key),$key0 # 0-round key LSB
1219 movdqa
$inout0,0x00(%rsp) # populate counter block
1221 movdqa
$inout0,$inout1
1222 movdqa
$inout0,$inout2
1223 movdqa
$inout0,$inout3
1224 movdqa
$inout0,0x40(%rsp)
1225 movdqa
$inout0,0x50(%rsp)
1226 movdqa
$inout0,0x60(%rsp)
1227 mov
%rdx,%r10 # about to borrow %rdx
1228 movdqa
$inout0,0x70(%rsp)
1236 pinsrd \
$3,%eax,$inout1
1238 movdqa
$inout1,0x10(%rsp)
1239 pinsrd \
$3,%edx,$inout2
1241 mov
%r10,%rdx # restore %rdx
1243 movdqa
$inout2,0x20(%rsp)
1246 pinsrd \
$3,%eax,$inout3
1248 movdqa
$inout3,0x30(%rsp)
1250 mov
%r10d,0x40+12(%rsp)
1253 mov
240($key),$rounds # key->rounds
1256 mov
%r9d,0x50+12(%rsp)
1259 mov
%r10d,0x60+12(%rsp)
1261 mov OPENSSL_ia32cap_P
+4(%rip),%r10d
1263 and \
$`1<<26|1<<22`,%r10d # isolate XSAVE+MOVBE
1264 mov
%r9d,0x70+12(%rsp)
1266 $movkey 0x10($key),$rndkey1
1268 movdqa
0x40(%rsp),$inout4
1269 movdqa
0x50(%rsp),$inout5
1271 cmp \
$8,$len # $len is in blocks
1272 jb
.Lctr32_tail
# short input if ($len<8)
1274 sub \
$6,$len # $len is biased by -6
1275 cmp \
$`1<<22`,%r10d # check for MOVBE without XSAVE
1276 je
.Lctr32_6x
# [which denotes Atom Silvermont]
1278 lea
0x80($key),$key # size optimization
1279 sub \
$2,$len # $len is biased by -8
1287 lea
32($key,$rounds),$key # end of key schedule
1288 sub %rax,%r10 # twisted $rounds
1293 add \
$6,$ctr # next counter value
1294 $movkey -48($key,$rnds_),$rndkey0
1295 aesenc
$rndkey1,$inout0
1298 aesenc
$rndkey1,$inout1
1299 movbe
%eax,`0x00+12`(%rsp) # store next counter value
1301 aesenc
$rndkey1,$inout2
1303 movbe
%eax,`0x10+12`(%rsp)
1304 aesenc
$rndkey1,$inout3
1307 aesenc
$rndkey1,$inout4
1308 movbe
%eax,`0x20+12`(%rsp)
1310 aesenc
$rndkey1,$inout5
1311 $movkey -32($key,$rnds_),$rndkey1
1314 aesenc
$rndkey0,$inout0
1315 movbe
%eax,`0x30+12`(%rsp)
1317 aesenc
$rndkey0,$inout1
1319 movbe
%eax,`0x40+12`(%rsp)
1320 aesenc
$rndkey0,$inout2
1323 aesenc
$rndkey0,$inout3
1324 movbe
%eax,`0x50+12`(%rsp)
1325 mov
%r10,%rax # mov $rnds_,$rounds
1326 aesenc
$rndkey0,$inout4
1327 aesenc
$rndkey0,$inout5
1328 $movkey -16($key,$rnds_),$rndkey0
1332 movdqu
($inp),$inout6 # load 6 input blocks
1333 movdqu
0x10($inp),$inout7
1334 movdqu
0x20($inp),$in0
1335 movdqu
0x30($inp),$in1
1336 movdqu
0x40($inp),$in2
1337 movdqu
0x50($inp),$in3
1338 lea
0x60($inp),$inp # $inp+=6*16
1339 $movkey -64($key,$rnds_),$rndkey1
1340 pxor
$inout0,$inout6 # inp^=E(ctr)
1341 movaps
0x00(%rsp),$inout0 # load next counter [xor-ed with 0 round]
1342 pxor
$inout1,$inout7
1343 movaps
0x10(%rsp),$inout1
1345 movaps
0x20(%rsp),$inout2
1347 movaps
0x30(%rsp),$inout3
1349 movaps
0x40(%rsp),$inout4
1351 movaps
0x50(%rsp),$inout5
1352 movdqu
$inout6,($out) # store 6 output blocks
1353 movdqu
$inout7,0x10($out)
1354 movdqu
$in0,0x20($out)
1355 movdqu
$in1,0x30($out)
1356 movdqu
$in2,0x40($out)
1357 movdqu
$in3,0x50($out)
1358 lea
0x60($out),$out # $out+=6*16
1361 jnc
.Lctr32_loop6
# loop if $len-=6 didn't borrow
1363 add \
$6,$len # restore real remaining $len
1364 jz
.Lctr32_done
# done if ($len==0)
1366 lea
-48($rnds_),$rounds
1367 lea
-80($key,$rnds_),$key # restore $key
1369 shr \
$4,$rounds # restore $rounds
1374 add \
$8,$ctr # next counter value
1375 movdqa
0x60(%rsp),$inout6
1376 aesenc
$rndkey1,$inout0
1378 movdqa
0x70(%rsp),$inout7
1379 aesenc
$rndkey1,$inout1
1381 $movkey 0x20-0x80($key),$rndkey0
1382 aesenc
$rndkey1,$inout2
1385 aesenc
$rndkey1,$inout3
1386 mov
%r9d,0x00+12(%rsp) # store next counter value
1388 aesenc
$rndkey1,$inout4
1389 aesenc
$rndkey1,$inout5
1390 aesenc
$rndkey1,$inout6
1391 aesenc
$rndkey1,$inout7
1392 $movkey 0x30-0x80($key),$rndkey1
1394 for($i=2;$i<8;$i++) {
1395 my $rndkeyx = ($i&1)?
$rndkey1:$rndkey0;
1398 aesenc
$rndkeyx,$inout0
1399 aesenc
$rndkeyx,$inout1
1402 aesenc
$rndkeyx,$inout2
1403 aesenc
$rndkeyx,$inout3
1404 mov
%r9d,`0x10*($i-1)`+12(%rsp)
1406 aesenc
$rndkeyx,$inout4
1407 aesenc
$rndkeyx,$inout5
1408 aesenc
$rndkeyx,$inout6
1409 aesenc
$rndkeyx,$inout7
1410 $movkey `0x20+0x10*$i`-0x80($key),$rndkeyx
1415 aesenc
$rndkey0,$inout0
1416 aesenc
$rndkey0,$inout1
1417 aesenc
$rndkey0,$inout2
1419 movdqu
0x00($inp),$in0 # start loading input
1420 aesenc
$rndkey0,$inout3
1421 mov
%r9d,0x70+12(%rsp)
1423 aesenc
$rndkey0,$inout4
1424 aesenc
$rndkey0,$inout5
1425 aesenc
$rndkey0,$inout6
1426 aesenc
$rndkey0,$inout7
1427 $movkey 0xa0-0x80($key),$rndkey0
1431 aesenc
$rndkey1,$inout0
1432 aesenc
$rndkey1,$inout1
1433 aesenc
$rndkey1,$inout2
1434 aesenc
$rndkey1,$inout3
1435 aesenc
$rndkey1,$inout4
1436 aesenc
$rndkey1,$inout5
1437 aesenc
$rndkey1,$inout6
1438 aesenc
$rndkey1,$inout7
1439 $movkey 0xb0-0x80($key),$rndkey1
1441 aesenc
$rndkey0,$inout0
1442 aesenc
$rndkey0,$inout1
1443 aesenc
$rndkey0,$inout2
1444 aesenc
$rndkey0,$inout3
1445 aesenc
$rndkey0,$inout4
1446 aesenc
$rndkey0,$inout5
1447 aesenc
$rndkey0,$inout6
1448 aesenc
$rndkey0,$inout7
1449 $movkey 0xc0-0x80($key),$rndkey0
1452 aesenc
$rndkey1,$inout0
1453 aesenc
$rndkey1,$inout1
1454 aesenc
$rndkey1,$inout2
1455 aesenc
$rndkey1,$inout3
1456 aesenc
$rndkey1,$inout4
1457 aesenc
$rndkey1,$inout5
1458 aesenc
$rndkey1,$inout6
1459 aesenc
$rndkey1,$inout7
1460 $movkey 0xd0-0x80($key),$rndkey1
1462 aesenc
$rndkey0,$inout0
1463 aesenc
$rndkey0,$inout1
1464 aesenc
$rndkey0,$inout2
1465 aesenc
$rndkey0,$inout3
1466 aesenc
$rndkey0,$inout4
1467 aesenc
$rndkey0,$inout5
1468 aesenc
$rndkey0,$inout6
1469 aesenc
$rndkey0,$inout7
1470 $movkey 0xe0-0x80($key),$rndkey0
1471 jmp
.Lctr32_enc_done
1475 movdqu
0x10($inp),$in1
1476 pxor
$rndkey0,$in0 # input^=round[last]
1477 movdqu
0x20($inp),$in2
1479 movdqu
0x30($inp),$in3
1481 movdqu
0x40($inp),$in4
1483 movdqu
0x50($inp),$in5
1486 aesenc
$rndkey1,$inout0
1487 aesenc
$rndkey1,$inout1
1488 aesenc
$rndkey1,$inout2
1489 aesenc
$rndkey1,$inout3
1490 aesenc
$rndkey1,$inout4
1491 aesenc
$rndkey1,$inout5
1492 aesenc
$rndkey1,$inout6
1493 aesenc
$rndkey1,$inout7
1494 movdqu
0x60($inp),$rndkey1 # borrow $rndkey1 for inp[6]
1495 lea
0x80($inp),$inp # $inp+=8*16
1497 aesenclast
$in0,$inout0 # $inN is inp[N]^round[last]
1498 pxor
$rndkey0,$rndkey1 # borrowed $rndkey
1499 movdqu
0x70-0x80($inp),$in0
1500 aesenclast
$in1,$inout1
1502 movdqa
0x00(%rsp),$in1 # load next counter block
1503 aesenclast
$in2,$inout2
1504 aesenclast
$in3,$inout3
1505 movdqa
0x10(%rsp),$in2
1506 movdqa
0x20(%rsp),$in3
1507 aesenclast
$in4,$inout4
1508 aesenclast
$in5,$inout5
1509 movdqa
0x30(%rsp),$in4
1510 movdqa
0x40(%rsp),$in5
1511 aesenclast
$rndkey1,$inout6
1512 movdqa
0x50(%rsp),$rndkey0
1513 $movkey 0x10-0x80($key),$rndkey1#real 1st-round key
1514 aesenclast
$in0,$inout7
1516 movups
$inout0,($out) # store 8 output blocks
1518 movups
$inout1,0x10($out)
1520 movups
$inout2,0x20($out)
1522 movups
$inout3,0x30($out)
1524 movups
$inout4,0x40($out)
1526 movups
$inout5,0x50($out)
1527 movdqa
$rndkey0,$inout5
1528 movups
$inout6,0x60($out)
1529 movups
$inout7,0x70($out)
1530 lea
0x80($out),$out # $out+=8*16
1533 jnc
.Lctr32_loop8
# loop if $len-=8 didn't borrow
1535 add \
$8,$len # restore real remainig $len
1536 jz
.Lctr32_done
# done if ($len==0)
1537 lea
-0x80($key),$key
1540 # note that at this point $inout0..5 are populated with
1541 # counter values xor-ed with 0-round key
1547 # if ($len>4) compute 7 E(counter)
1549 movdqa
0x60(%rsp),$inout6
1550 pxor
$inout7,$inout7
1552 $movkey 16($key),$rndkey0
1553 aesenc
$rndkey1,$inout0
1554 aesenc
$rndkey1,$inout1
1555 lea
32-16($key,$rounds),$key# prepare for .Lenc_loop8_enter
1557 aesenc
$rndkey1,$inout2
1558 add \
$16,%rax # prepare for .Lenc_loop8_enter
1560 aesenc
$rndkey1,$inout3
1561 aesenc
$rndkey1,$inout4
1562 movups
0x10($inp),$in1 # pre-load input
1563 movups
0x20($inp),$in2
1564 aesenc
$rndkey1,$inout5
1565 aesenc
$rndkey1,$inout6
1567 call
.Lenc_loop8_enter
1569 movdqu
0x30($inp),$in3
1571 movdqu
0x40($inp),$in0
1573 movdqu
$inout0,($out) # store output
1575 movdqu
$inout1,0x10($out)
1577 movdqu
$inout2,0x20($out)
1579 movdqu
$inout3,0x30($out)
1580 movdqu
$inout4,0x40($out)
1582 jb
.Lctr32_done
# $len was 5, stop store
1584 movups
0x50($inp),$in1
1586 movups
$inout5,0x50($out)
1587 je
.Lctr32_done
# $len was 6, stop store
1589 movups
0x60($inp),$in2
1591 movups
$inout6,0x60($out)
1592 jmp
.Lctr32_done
# $len was 7, stop store
1596 aesenc
$rndkey1,$inout0
1599 aesenc
$rndkey1,$inout1
1600 aesenc
$rndkey1,$inout2
1601 aesenc
$rndkey1,$inout3
1602 $movkey ($key),$rndkey1
1604 aesenclast
$rndkey1,$inout0
1605 aesenclast
$rndkey1,$inout1
1606 movups
($inp),$in0 # load input
1607 movups
0x10($inp),$in1
1608 aesenclast
$rndkey1,$inout2
1609 aesenclast
$rndkey1,$inout3
1610 movups
0x20($inp),$in2
1611 movups
0x30($inp),$in3
1614 movups
$inout0,($out) # store output
1616 movups
$inout1,0x10($out)
1618 movdqu
$inout2,0x20($out)
1620 movdqu
$inout3,0x30($out)
1621 jmp
.Lctr32_done
# $len was 4, stop store
1625 aesenc
$rndkey1,$inout0
1628 aesenc
$rndkey1,$inout1
1629 aesenc
$rndkey1,$inout2
1630 $movkey ($key),$rndkey1
1632 aesenclast
$rndkey1,$inout0
1633 aesenclast
$rndkey1,$inout1
1634 aesenclast
$rndkey1,$inout2
1636 movups
($inp),$in0 # load input
1638 movups
$inout0,($out) # store output
1640 jb
.Lctr32_done
# $len was 1, stop store
1642 movups
0x10($inp),$in1
1644 movups
$inout1,0x10($out)
1645 je
.Lctr32_done
# $len was 2, stop store
1647 movups
0x20($inp),$in2
1649 movups
$inout2,0x20($out) # $len was 3, stop store
1652 xorps
%xmm0,%xmm0 # clear regiser bank
1660 $code.=<<___
if (!$win64);
1663 movaps
%xmm0,0x00(%rsp) # clear stack
1665 movaps
%xmm0,0x10(%rsp)
1667 movaps
%xmm0,0x20(%rsp)
1669 movaps
%xmm0,0x30(%rsp)
1671 movaps
%xmm0,0x40(%rsp)
1673 movaps
%xmm0,0x50(%rsp)
1675 movaps
%xmm0,0x60(%rsp)
1677 movaps
%xmm0,0x70(%rsp)
1680 $code.=<<___
if ($win64);
1681 movaps
-0xa0(%rbp),%xmm6
1682 movaps
%xmm0,-0xa0(%rbp) # clear stack
1683 movaps
-0x90(%rbp),%xmm7
1684 movaps
%xmm0,-0x90(%rbp)
1685 movaps
-0x80(%rbp),%xmm8
1686 movaps
%xmm0,-0x80(%rbp)
1687 movaps
-0x70(%rbp),%xmm9
1688 movaps
%xmm0,-0x70(%rbp)
1689 movaps
-0x60(%rbp),%xmm10
1690 movaps
%xmm0,-0x60(%rbp)
1691 movaps
-0x50(%rbp),%xmm11
1692 movaps
%xmm0,-0x50(%rbp)
1693 movaps
-0x40(%rbp),%xmm12
1694 movaps
%xmm0,-0x40(%rbp)
1695 movaps
-0x30(%rbp),%xmm13
1696 movaps
%xmm0,-0x30(%rbp)
1697 movaps
-0x20(%rbp),%xmm14
1698 movaps
%xmm0,-0x20(%rbp)
1699 movaps
-0x10(%rbp),%xmm15
1700 movaps
%xmm0,-0x10(%rbp)
1701 movaps
%xmm0,0x00(%rsp)
1702 movaps
%xmm0,0x10(%rsp)
1703 movaps
%xmm0,0x20(%rsp)
1704 movaps
%xmm0,0x30(%rsp)
1705 movaps
%xmm0,0x40(%rsp)
1706 movaps
%xmm0,0x50(%rsp)
1707 movaps
%xmm0,0x60(%rsp)
1708 movaps
%xmm0,0x70(%rsp)
1715 .size aesni_ctr32_encrypt_blocks
,.-aesni_ctr32_encrypt_blocks
1719 ######################################################################
1720 # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1721 # const AES_KEY *key1, const AES_KEY *key2
1722 # const unsigned char iv[16]);
1725 my @tweak=map("%xmm$_",(10..15));
1726 my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
1727 my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
1728 my $frame_size = 0x70 + ($win64?
160:0);
1731 .globl aesni_xts_encrypt
1732 .type aesni_xts_encrypt
,\
@function,6
1737 sub \
$$frame_size,%rsp
1738 and \
$-16,%rsp # Linux kernel stack can be incorrectly seeded
1740 $code.=<<___
if ($win64);
1741 movaps
%xmm6,-0xa8(%rax) # offload everything
1742 movaps
%xmm7,-0x98(%rax)
1743 movaps
%xmm8,-0x88(%rax)
1744 movaps
%xmm9,-0x78(%rax)
1745 movaps
%xmm10,-0x68(%rax)
1746 movaps
%xmm11,-0x58(%rax)
1747 movaps
%xmm12,-0x48(%rax)
1748 movaps
%xmm13,-0x38(%rax)
1749 movaps
%xmm14,-0x28(%rax)
1750 movaps
%xmm15,-0x18(%rax)
1755 movups
($ivp),$inout0 # load clear-text tweak
1756 mov
240(%r8),$rounds # key2->rounds
1757 mov
240($key),$rnds_ # key1->rounds
1759 # generate the tweak
1760 &aesni_generate1
("enc",$key2,$rounds,$inout0);
1762 $movkey ($key),$rndkey0 # zero round key
1763 mov
$key,$key_ # backup $key
1764 mov
$rnds_,$rounds # backup $rounds
1766 mov
$len,$len_ # backup $len
1769 $movkey 16($key,$rnds_),$rndkey1 # last round key
1771 movdqa
.Lxts_magic
(%rip),$twmask
1772 movdqa
$inout0,@tweak[5]
1773 pshufd \
$0x5f,$inout0,$twres
1774 pxor
$rndkey0,$rndkey1
1776 # alternative tweak calculation algorithm is based on suggestions
1777 # by Shay Gueron. psrad doesn't conflict with AES-NI instructions
1778 # and should help in the future...
1779 for ($i=0;$i<4;$i++) {
1781 movdqa
$twres,$twtmp
1783 movdqa
@tweak[5],@tweak[$i]
1784 psrad \
$31,$twtmp # broadcast upper bits
1785 paddq
@tweak[5],@tweak[5]
1787 pxor
$rndkey0,@tweak[$i]
1788 pxor
$twtmp,@tweak[5]
1792 movdqa
@tweak[5],@tweak[4]
1794 paddq
@tweak[5],@tweak[5]
1796 pxor
$rndkey0,@tweak[4]
1797 pxor
$twres,@tweak[5]
1798 movaps
$rndkey1,0x60(%rsp) # save round[0]^round[last]
1801 jc
.Lxts_enc_short
# if $len-=6*16 borrowed
1804 lea
32($key_,$rnds_),$key # end of key schedule
1805 sub %r10,%rax # twisted $rounds
1806 $movkey 16($key_),$rndkey1
1807 mov
%rax,%r10 # backup twisted $rounds
1808 lea
.Lxts_magic
(%rip),%r8
1809 jmp
.Lxts_enc_grandloop
1812 .Lxts_enc_grandloop
:
1813 movdqu
`16*0`($inp),$inout0 # load input
1814 movdqa
$rndkey0,$twmask
1815 movdqu
`16*1`($inp),$inout1
1816 pxor
@tweak[0],$inout0 # input^=tweak^round[0]
1817 movdqu
`16*2`($inp),$inout2
1818 pxor
@tweak[1],$inout1
1819 aesenc
$rndkey1,$inout0
1820 movdqu
`16*3`($inp),$inout3
1821 pxor
@tweak[2],$inout2
1822 aesenc
$rndkey1,$inout1
1823 movdqu
`16*4`($inp),$inout4
1824 pxor
@tweak[3],$inout3
1825 aesenc
$rndkey1,$inout2
1826 movdqu
`16*5`($inp),$inout5
1827 pxor
@tweak[5],$twmask # round[0]^=tweak[5]
1828 movdqa
0x60(%rsp),$twres # load round[0]^round[last]
1829 pxor
@tweak[4],$inout4
1830 aesenc
$rndkey1,$inout3
1831 $movkey 32($key_),$rndkey0
1832 lea
`16*6`($inp),$inp
1833 pxor
$twmask,$inout5
1835 pxor
$twres,@tweak[0] # calclulate tweaks^round[last]
1836 aesenc
$rndkey1,$inout4
1837 pxor
$twres,@tweak[1]
1838 movdqa
@tweak[0],`16*0`(%rsp) # put aside tweaks^round[last]
1839 aesenc
$rndkey1,$inout5
1840 $movkey 48($key_),$rndkey1
1841 pxor
$twres,@tweak[2]
1843 aesenc
$rndkey0,$inout0
1844 pxor
$twres,@tweak[3]
1845 movdqa
@tweak[1],`16*1`(%rsp)
1846 aesenc
$rndkey0,$inout1
1847 pxor
$twres,@tweak[4]
1848 movdqa
@tweak[2],`16*2`(%rsp)
1849 aesenc
$rndkey0,$inout2
1850 aesenc
$rndkey0,$inout3
1852 movdqa
@tweak[4],`16*4`(%rsp)
1853 aesenc
$rndkey0,$inout4
1854 aesenc
$rndkey0,$inout5
1855 $movkey 64($key_),$rndkey0
1856 movdqa
$twmask,`16*5`(%rsp)
1857 pshufd \
$0x5f,@tweak[5],$twres
1861 aesenc
$rndkey1,$inout0
1862 aesenc
$rndkey1,$inout1
1863 aesenc
$rndkey1,$inout2
1864 aesenc
$rndkey1,$inout3
1865 aesenc
$rndkey1,$inout4
1866 aesenc
$rndkey1,$inout5
1867 $movkey -64($key,%rax),$rndkey1
1870 aesenc
$rndkey0,$inout0
1871 aesenc
$rndkey0,$inout1
1872 aesenc
$rndkey0,$inout2
1873 aesenc
$rndkey0,$inout3
1874 aesenc
$rndkey0,$inout4
1875 aesenc
$rndkey0,$inout5
1876 $movkey -80($key,%rax),$rndkey0
1879 movdqa
(%r8),$twmask # start calculating next tweak
1880 movdqa
$twres,$twtmp
1882 aesenc
$rndkey1,$inout0
1883 paddq
@tweak[5],@tweak[5]
1885 aesenc
$rndkey1,$inout1
1887 $movkey ($key_),@tweak[0] # load round[0]
1888 aesenc
$rndkey1,$inout2
1889 aesenc
$rndkey1,$inout3
1890 aesenc
$rndkey1,$inout4
1891 pxor
$twtmp,@tweak[5]
1892 movaps
@tweak[0],@tweak[1] # copy round[0]
1893 aesenc
$rndkey1,$inout5
1894 $movkey -64($key),$rndkey1
1896 movdqa
$twres,$twtmp
1897 aesenc
$rndkey0,$inout0
1899 pxor
@tweak[5],@tweak[0]
1900 aesenc
$rndkey0,$inout1
1902 paddq
@tweak[5],@tweak[5]
1903 aesenc
$rndkey0,$inout2
1904 aesenc
$rndkey0,$inout3
1906 movaps
@tweak[1],@tweak[2]
1907 aesenc
$rndkey0,$inout4
1908 pxor
$twtmp,@tweak[5]
1909 movdqa
$twres,$twtmp
1910 aesenc
$rndkey0,$inout5
1911 $movkey -48($key),$rndkey0
1914 aesenc
$rndkey1,$inout0
1915 pxor
@tweak[5],@tweak[1]
1917 aesenc
$rndkey1,$inout1
1918 paddq
@tweak[5],@tweak[5]
1920 aesenc
$rndkey1,$inout2
1921 aesenc
$rndkey1,$inout3
1922 movdqa
@tweak[3],`16*3`(%rsp)
1923 pxor
$twtmp,@tweak[5]
1924 aesenc
$rndkey1,$inout4
1925 movaps
@tweak[2],@tweak[3]
1926 movdqa
$twres,$twtmp
1927 aesenc
$rndkey1,$inout5
1928 $movkey -32($key),$rndkey1
1931 aesenc
$rndkey0,$inout0
1932 pxor
@tweak[5],@tweak[2]
1934 aesenc
$rndkey0,$inout1
1935 paddq
@tweak[5],@tweak[5]
1937 aesenc
$rndkey0,$inout2
1938 aesenc
$rndkey0,$inout3
1939 aesenc
$rndkey0,$inout4
1940 pxor
$twtmp,@tweak[5]
1941 movaps
@tweak[3],@tweak[4]
1942 aesenc
$rndkey0,$inout5
1944 movdqa
$twres,$rndkey0
1946 aesenc
$rndkey1,$inout0
1947 pxor
@tweak[5],@tweak[3]
1949 aesenc
$rndkey1,$inout1
1950 paddq
@tweak[5],@tweak[5]
1951 pand
$twmask,$rndkey0
1952 aesenc
$rndkey1,$inout2
1953 aesenc
$rndkey1,$inout3
1954 pxor
$rndkey0,@tweak[5]
1955 $movkey ($key_),$rndkey0
1956 aesenc
$rndkey1,$inout4
1957 aesenc
$rndkey1,$inout5
1958 $movkey 16($key_),$rndkey1
1960 pxor
@tweak[5],@tweak[4]
1961 aesenclast
`16*0`(%rsp),$inout0
1963 paddq
@tweak[5],@tweak[5]
1964 aesenclast
`16*1`(%rsp),$inout1
1965 aesenclast
`16*2`(%rsp),$inout2
1967 mov
%r10,%rax # restore $rounds
1968 aesenclast
`16*3`(%rsp),$inout3
1969 aesenclast
`16*4`(%rsp),$inout4
1970 aesenclast
`16*5`(%rsp),$inout5
1971 pxor
$twres,@tweak[5]
1973 lea
`16*6`($out),$out # $out+=6*16
1974 movups
$inout0,`-16*6`($out) # store 6 output blocks
1975 movups
$inout1,`-16*5`($out)
1976 movups
$inout2,`-16*4`($out)
1977 movups
$inout3,`-16*3`($out)
1978 movups
$inout4,`-16*2`($out)
1979 movups
$inout5,`-16*1`($out)
1981 jnc
.Lxts_enc_grandloop
# loop if $len-=6*16 didn't borrow
1985 mov
$key_,$key # restore $key
1986 shr \
$4,$rounds # restore original value
1989 # at the point @tweak[0..5] are populated with tweak values
1990 mov
$rounds,$rnds_ # backup $rounds
1991 pxor
$rndkey0,@tweak[0]
1992 add \
$16*6,$len # restore real remaining $len
1993 jz
.Lxts_enc_done
# done if ($len==0)
1995 pxor
$rndkey0,@tweak[1]
1997 jb
.Lxts_enc_one
# $len is 1*16
1998 pxor
$rndkey0,@tweak[2]
1999 je
.Lxts_enc_two
# $len is 2*16
2001 pxor
$rndkey0,@tweak[3]
2003 jb
.Lxts_enc_three
# $len is 3*16
2004 pxor
$rndkey0,@tweak[4]
2005 je
.Lxts_enc_four
# $len is 4*16
2007 movdqu
($inp),$inout0 # $len is 5*16
2008 movdqu
16*1($inp),$inout1
2009 movdqu
16*2($inp),$inout2
2010 pxor
@tweak[0],$inout0
2011 movdqu
16*3($inp),$inout3
2012 pxor
@tweak[1],$inout1
2013 movdqu
16*4($inp),$inout4
2014 lea
16*5($inp),$inp # $inp+=5*16
2015 pxor
@tweak[2],$inout2
2016 pxor
@tweak[3],$inout3
2017 pxor
@tweak[4],$inout4
2018 pxor
$inout5,$inout5
2020 call _aesni_encrypt6
2022 xorps
@tweak[0],$inout0
2023 movdqa
@tweak[5],@tweak[0]
2024 xorps
@tweak[1],$inout1
2025 xorps
@tweak[2],$inout2
2026 movdqu
$inout0,($out) # store 5 output blocks
2027 xorps
@tweak[3],$inout3
2028 movdqu
$inout1,16*1($out)
2029 xorps
@tweak[4],$inout4
2030 movdqu
$inout2,16*2($out)
2031 movdqu
$inout3,16*3($out)
2032 movdqu
$inout4,16*4($out)
2033 lea
16*5($out),$out # $out+=5*16
2038 movups
($inp),$inout0
2039 lea
16*1($inp),$inp # inp+=1*16
2040 xorps
@tweak[0],$inout0
2042 &aesni_generate1
("enc",$key,$rounds);
2044 xorps
@tweak[0],$inout0
2045 movdqa
@tweak[1],@tweak[0]
2046 movups
$inout0,($out) # store one output block
2047 lea
16*1($out),$out # $out+=1*16
2052 movups
($inp),$inout0
2053 movups
16($inp),$inout1
2054 lea
32($inp),$inp # $inp+=2*16
2055 xorps
@tweak[0],$inout0
2056 xorps
@tweak[1],$inout1
2058 call _aesni_encrypt2
2060 xorps
@tweak[0],$inout0
2061 movdqa
@tweak[2],@tweak[0]
2062 xorps
@tweak[1],$inout1
2063 movups
$inout0,($out) # store 2 output blocks
2064 movups
$inout1,16*1($out)
2065 lea
16*2($out),$out # $out+=2*16
2070 movups
($inp),$inout0
2071 movups
16*1($inp),$inout1
2072 movups
16*2($inp),$inout2
2073 lea
16*3($inp),$inp # $inp+=3*16
2074 xorps
@tweak[0],$inout0
2075 xorps
@tweak[1],$inout1
2076 xorps
@tweak[2],$inout2
2078 call _aesni_encrypt3
2080 xorps
@tweak[0],$inout0
2081 movdqa
@tweak[3],@tweak[0]
2082 xorps
@tweak[1],$inout1
2083 xorps
@tweak[2],$inout2
2084 movups
$inout0,($out) # store 3 output blocks
2085 movups
$inout1,16*1($out)
2086 movups
$inout2,16*2($out)
2087 lea
16*3($out),$out # $out+=3*16
2092 movups
($inp),$inout0
2093 movups
16*1($inp),$inout1
2094 movups
16*2($inp),$inout2
2095 xorps
@tweak[0],$inout0
2096 movups
16*3($inp),$inout3
2097 lea
16*4($inp),$inp # $inp+=4*16
2098 xorps
@tweak[1],$inout1
2099 xorps
@tweak[2],$inout2
2100 xorps
@tweak[3],$inout3
2102 call _aesni_encrypt4
2104 pxor
@tweak[0],$inout0
2105 movdqa
@tweak[4],@tweak[0]
2106 pxor
@tweak[1],$inout1
2107 pxor
@tweak[2],$inout2
2108 movdqu
$inout0,($out) # store 4 output blocks
2109 pxor
@tweak[3],$inout3
2110 movdqu
$inout1,16*1($out)
2111 movdqu
$inout2,16*2($out)
2112 movdqu
$inout3,16*3($out)
2113 lea
16*4($out),$out # $out+=4*16
2118 and \
$15,$len_ # see if $len%16 is 0
2123 movzb
($inp),%eax # borrow $rounds ...
2124 movzb
-16($out),%ecx # ... and $key
2132 sub $len_,$out # rewind $out
2133 mov
$key_,$key # restore $key
2134 mov
$rnds_,$rounds # restore $rounds
2136 movups
-16($out),$inout0
2137 xorps
@tweak[0],$inout0
2139 &aesni_generate1
("enc",$key,$rounds);
2141 xorps
@tweak[0],$inout0
2142 movups
$inout0,-16($out)
2145 xorps
%xmm0,%xmm0 # clear register bank
2152 $code.=<<___
if (!$win64);
2155 movaps
%xmm0,0x00(%rsp) # clear stack
2157 movaps
%xmm0,0x10(%rsp)
2159 movaps
%xmm0,0x20(%rsp)
2161 movaps
%xmm0,0x30(%rsp)
2163 movaps
%xmm0,0x40(%rsp)
2165 movaps
%xmm0,0x50(%rsp)
2167 movaps
%xmm0,0x60(%rsp)
2171 $code.=<<___
if ($win64);
2172 movaps
-0xa0(%rbp),%xmm6
2173 movaps
%xmm0,-0xa0(%rbp) # clear stack
2174 movaps
-0x90(%rbp),%xmm7
2175 movaps
%xmm0,-0x90(%rbp)
2176 movaps
-0x80(%rbp),%xmm8
2177 movaps
%xmm0,-0x80(%rbp)
2178 movaps
-0x70(%rbp),%xmm9
2179 movaps
%xmm0,-0x70(%rbp)
2180 movaps
-0x60(%rbp),%xmm10
2181 movaps
%xmm0,-0x60(%rbp)
2182 movaps
-0x50(%rbp),%xmm11
2183 movaps
%xmm0,-0x50(%rbp)
2184 movaps
-0x40(%rbp),%xmm12
2185 movaps
%xmm0,-0x40(%rbp)
2186 movaps
-0x30(%rbp),%xmm13
2187 movaps
%xmm0,-0x30(%rbp)
2188 movaps
-0x20(%rbp),%xmm14
2189 movaps
%xmm0,-0x20(%rbp)
2190 movaps
-0x10(%rbp),%xmm15
2191 movaps
%xmm0,-0x10(%rbp)
2192 movaps
%xmm0,0x00(%rsp)
2193 movaps
%xmm0,0x10(%rsp)
2194 movaps
%xmm0,0x20(%rsp)
2195 movaps
%xmm0,0x30(%rsp)
2196 movaps
%xmm0,0x40(%rsp)
2197 movaps
%xmm0,0x50(%rsp)
2198 movaps
%xmm0,0x60(%rsp)
2205 .size aesni_xts_encrypt
,.-aesni_xts_encrypt
2209 .globl aesni_xts_decrypt
2210 .type aesni_xts_decrypt
,\
@function,6
2215 sub \
$$frame_size,%rsp
2216 and \
$-16,%rsp # Linux kernel stack can be incorrectly seeded
2218 $code.=<<___
if ($win64);
2219 movaps
%xmm6,-0xa8(%rax) # offload everything
2220 movaps
%xmm7,-0x98(%rax)
2221 movaps
%xmm8,-0x88(%rax)
2222 movaps
%xmm9,-0x78(%rax)
2223 movaps
%xmm10,-0x68(%rax)
2224 movaps
%xmm11,-0x58(%rax)
2225 movaps
%xmm12,-0x48(%rax)
2226 movaps
%xmm13,-0x38(%rax)
2227 movaps
%xmm14,-0x28(%rax)
2228 movaps
%xmm15,-0x18(%rax)
2233 movups
($ivp),$inout0 # load clear-text tweak
2234 mov
240($key2),$rounds # key2->rounds
2235 mov
240($key),$rnds_ # key1->rounds
2237 # generate the tweak
2238 &aesni_generate1
("enc",$key2,$rounds,$inout0);
2240 xor %eax,%eax # if ($len%16) len-=16;
2246 $movkey ($key),$rndkey0 # zero round key
2247 mov
$key,$key_ # backup $key
2248 mov
$rnds_,$rounds # backup $rounds
2250 mov
$len,$len_ # backup $len
2253 $movkey 16($key,$rnds_),$rndkey1 # last round key
2255 movdqa
.Lxts_magic
(%rip),$twmask
2256 movdqa
$inout0,@tweak[5]
2257 pshufd \
$0x5f,$inout0,$twres
2258 pxor
$rndkey0,$rndkey1
2260 for ($i=0;$i<4;$i++) {
2262 movdqa
$twres,$twtmp
2264 movdqa
@tweak[5],@tweak[$i]
2265 psrad \
$31,$twtmp # broadcast upper bits
2266 paddq
@tweak[5],@tweak[5]
2268 pxor
$rndkey0,@tweak[$i]
2269 pxor
$twtmp,@tweak[5]
2273 movdqa
@tweak[5],@tweak[4]
2275 paddq
@tweak[5],@tweak[5]
2277 pxor
$rndkey0,@tweak[4]
2278 pxor
$twres,@tweak[5]
2279 movaps
$rndkey1,0x60(%rsp) # save round[0]^round[last]
2282 jc
.Lxts_dec_short
# if $len-=6*16 borrowed
2285 lea
32($key_,$rnds_),$key # end of key schedule
2286 sub %r10,%rax # twisted $rounds
2287 $movkey 16($key_),$rndkey1
2288 mov
%rax,%r10 # backup twisted $rounds
2289 lea
.Lxts_magic
(%rip),%r8
2290 jmp
.Lxts_dec_grandloop
2293 .Lxts_dec_grandloop
:
2294 movdqu
`16*0`($inp),$inout0 # load input
2295 movdqa
$rndkey0,$twmask
2296 movdqu
`16*1`($inp),$inout1
2297 pxor
@tweak[0],$inout0 # intput^=tweak^round[0]
2298 movdqu
`16*2`($inp),$inout2
2299 pxor
@tweak[1],$inout1
2300 aesdec
$rndkey1,$inout0
2301 movdqu
`16*3`($inp),$inout3
2302 pxor
@tweak[2],$inout2
2303 aesdec
$rndkey1,$inout1
2304 movdqu
`16*4`($inp),$inout4
2305 pxor
@tweak[3],$inout3
2306 aesdec
$rndkey1,$inout2
2307 movdqu
`16*5`($inp),$inout5
2308 pxor
@tweak[5],$twmask # round[0]^=tweak[5]
2309 movdqa
0x60(%rsp),$twres # load round[0]^round[last]
2310 pxor
@tweak[4],$inout4
2311 aesdec
$rndkey1,$inout3
2312 $movkey 32($key_),$rndkey0
2313 lea
`16*6`($inp),$inp
2314 pxor
$twmask,$inout5
2316 pxor
$twres,@tweak[0] # calclulate tweaks^round[last]
2317 aesdec
$rndkey1,$inout4
2318 pxor
$twres,@tweak[1]
2319 movdqa
@tweak[0],`16*0`(%rsp) # put aside tweaks^last round key
2320 aesdec
$rndkey1,$inout5
2321 $movkey 48($key_),$rndkey1
2322 pxor
$twres,@tweak[2]
2324 aesdec
$rndkey0,$inout0
2325 pxor
$twres,@tweak[3]
2326 movdqa
@tweak[1],`16*1`(%rsp)
2327 aesdec
$rndkey0,$inout1
2328 pxor
$twres,@tweak[4]
2329 movdqa
@tweak[2],`16*2`(%rsp)
2330 aesdec
$rndkey0,$inout2
2331 aesdec
$rndkey0,$inout3
2333 movdqa
@tweak[4],`16*4`(%rsp)
2334 aesdec
$rndkey0,$inout4
2335 aesdec
$rndkey0,$inout5
2336 $movkey 64($key_),$rndkey0
2337 movdqa
$twmask,`16*5`(%rsp)
2338 pshufd \
$0x5f,@tweak[5],$twres
2342 aesdec
$rndkey1,$inout0
2343 aesdec
$rndkey1,$inout1
2344 aesdec
$rndkey1,$inout2
2345 aesdec
$rndkey1,$inout3
2346 aesdec
$rndkey1,$inout4
2347 aesdec
$rndkey1,$inout5
2348 $movkey -64($key,%rax),$rndkey1
2351 aesdec
$rndkey0,$inout0
2352 aesdec
$rndkey0,$inout1
2353 aesdec
$rndkey0,$inout2
2354 aesdec
$rndkey0,$inout3
2355 aesdec
$rndkey0,$inout4
2356 aesdec
$rndkey0,$inout5
2357 $movkey -80($key,%rax),$rndkey0
2360 movdqa
(%r8),$twmask # start calculating next tweak
2361 movdqa
$twres,$twtmp
2363 aesdec
$rndkey1,$inout0
2364 paddq
@tweak[5],@tweak[5]
2366 aesdec
$rndkey1,$inout1
2368 $movkey ($key_),@tweak[0] # load round[0]
2369 aesdec
$rndkey1,$inout2
2370 aesdec
$rndkey1,$inout3
2371 aesdec
$rndkey1,$inout4
2372 pxor
$twtmp,@tweak[5]
2373 movaps
@tweak[0],@tweak[1] # copy round[0]
2374 aesdec
$rndkey1,$inout5
2375 $movkey -64($key),$rndkey1
2377 movdqa
$twres,$twtmp
2378 aesdec
$rndkey0,$inout0
2380 pxor
@tweak[5],@tweak[0]
2381 aesdec
$rndkey0,$inout1
2383 paddq
@tweak[5],@tweak[5]
2384 aesdec
$rndkey0,$inout2
2385 aesdec
$rndkey0,$inout3
2387 movaps
@tweak[1],@tweak[2]
2388 aesdec
$rndkey0,$inout4
2389 pxor
$twtmp,@tweak[5]
2390 movdqa
$twres,$twtmp
2391 aesdec
$rndkey0,$inout5
2392 $movkey -48($key),$rndkey0
2395 aesdec
$rndkey1,$inout0
2396 pxor
@tweak[5],@tweak[1]
2398 aesdec
$rndkey1,$inout1
2399 paddq
@tweak[5],@tweak[5]
2401 aesdec
$rndkey1,$inout2
2402 aesdec
$rndkey1,$inout3
2403 movdqa
@tweak[3],`16*3`(%rsp)
2404 pxor
$twtmp,@tweak[5]
2405 aesdec
$rndkey1,$inout4
2406 movaps
@tweak[2],@tweak[3]
2407 movdqa
$twres,$twtmp
2408 aesdec
$rndkey1,$inout5
2409 $movkey -32($key),$rndkey1
2412 aesdec
$rndkey0,$inout0
2413 pxor
@tweak[5],@tweak[2]
2415 aesdec
$rndkey0,$inout1
2416 paddq
@tweak[5],@tweak[5]
2418 aesdec
$rndkey0,$inout2
2419 aesdec
$rndkey0,$inout3
2420 aesdec
$rndkey0,$inout4
2421 pxor
$twtmp,@tweak[5]
2422 movaps
@tweak[3],@tweak[4]
2423 aesdec
$rndkey0,$inout5
2425 movdqa
$twres,$rndkey0
2427 aesdec
$rndkey1,$inout0
2428 pxor
@tweak[5],@tweak[3]
2430 aesdec
$rndkey1,$inout1
2431 paddq
@tweak[5],@tweak[5]
2432 pand
$twmask,$rndkey0
2433 aesdec
$rndkey1,$inout2
2434 aesdec
$rndkey1,$inout3
2435 pxor
$rndkey0,@tweak[5]
2436 $movkey ($key_),$rndkey0
2437 aesdec
$rndkey1,$inout4
2438 aesdec
$rndkey1,$inout5
2439 $movkey 16($key_),$rndkey1
2441 pxor
@tweak[5],@tweak[4]
2442 aesdeclast
`16*0`(%rsp),$inout0
2444 paddq
@tweak[5],@tweak[5]
2445 aesdeclast
`16*1`(%rsp),$inout1
2446 aesdeclast
`16*2`(%rsp),$inout2
2448 mov
%r10,%rax # restore $rounds
2449 aesdeclast
`16*3`(%rsp),$inout3
2450 aesdeclast
`16*4`(%rsp),$inout4
2451 aesdeclast
`16*5`(%rsp),$inout5
2452 pxor
$twres,@tweak[5]
2454 lea
`16*6`($out),$out # $out+=6*16
2455 movups
$inout0,`-16*6`($out) # store 6 output blocks
2456 movups
$inout1,`-16*5`($out)
2457 movups
$inout2,`-16*4`($out)
2458 movups
$inout3,`-16*3`($out)
2459 movups
$inout4,`-16*2`($out)
2460 movups
$inout5,`-16*1`($out)
2462 jnc
.Lxts_dec_grandloop
# loop if $len-=6*16 didn't borrow
2466 mov
$key_,$key # restore $key
2467 shr \
$4,$rounds # restore original value
2470 # at the point @tweak[0..5] are populated with tweak values
2471 mov
$rounds,$rnds_ # backup $rounds
2472 pxor
$rndkey0,@tweak[0]
2473 pxor
$rndkey0,@tweak[1]
2474 add \
$16*6,$len # restore real remaining $len
2475 jz
.Lxts_dec_done
# done if ($len==0)
2477 pxor
$rndkey0,@tweak[2]
2479 jb
.Lxts_dec_one
# $len is 1*16
2480 pxor
$rndkey0,@tweak[3]
2481 je
.Lxts_dec_two
# $len is 2*16
2483 pxor
$rndkey0,@tweak[4]
2485 jb
.Lxts_dec_three
# $len is 3*16
2486 je
.Lxts_dec_four
# $len is 4*16
2488 movdqu
($inp),$inout0 # $len is 5*16
2489 movdqu
16*1($inp),$inout1
2490 movdqu
16*2($inp),$inout2
2491 pxor
@tweak[0],$inout0
2492 movdqu
16*3($inp),$inout3
2493 pxor
@tweak[1],$inout1
2494 movdqu
16*4($inp),$inout4
2495 lea
16*5($inp),$inp # $inp+=5*16
2496 pxor
@tweak[2],$inout2
2497 pxor
@tweak[3],$inout3
2498 pxor
@tweak[4],$inout4
2500 call _aesni_decrypt6
2502 xorps
@tweak[0],$inout0
2503 xorps
@tweak[1],$inout1
2504 xorps
@tweak[2],$inout2
2505 movdqu
$inout0,($out) # store 5 output blocks
2506 xorps
@tweak[3],$inout3
2507 movdqu
$inout1,16*1($out)
2508 xorps
@tweak[4],$inout4
2509 movdqu
$inout2,16*2($out)
2511 movdqu
$inout3,16*3($out)
2512 pcmpgtd
@tweak[5],$twtmp
2513 movdqu
$inout4,16*4($out)
2514 lea
16*5($out),$out # $out+=5*16
2515 pshufd \
$0x13,$twtmp,@tweak[1] # $twres
2519 movdqa
@tweak[5],@tweak[0]
2520 paddq
@tweak[5],@tweak[5] # psllq 1,$tweak
2521 pand
$twmask,@tweak[1] # isolate carry and residue
2522 pxor
@tweak[5],@tweak[1]
2527 movups
($inp),$inout0
2528 lea
16*1($inp),$inp # $inp+=1*16
2529 xorps
@tweak[0],$inout0
2531 &aesni_generate1
("dec",$key,$rounds);
2533 xorps
@tweak[0],$inout0
2534 movdqa
@tweak[1],@tweak[0]
2535 movups
$inout0,($out) # store one output block
2536 movdqa
@tweak[2],@tweak[1]
2537 lea
16*1($out),$out # $out+=1*16
2542 movups
($inp),$inout0
2543 movups
16($inp),$inout1
2544 lea
32($inp),$inp # $inp+=2*16
2545 xorps
@tweak[0],$inout0
2546 xorps
@tweak[1],$inout1
2548 call _aesni_decrypt2
2550 xorps
@tweak[0],$inout0
2551 movdqa
@tweak[2],@tweak[0]
2552 xorps
@tweak[1],$inout1
2553 movdqa
@tweak[3],@tweak[1]
2554 movups
$inout0,($out) # store 2 output blocks
2555 movups
$inout1,16*1($out)
2556 lea
16*2($out),$out # $out+=2*16
2561 movups
($inp),$inout0
2562 movups
16*1($inp),$inout1
2563 movups
16*2($inp),$inout2
2564 lea
16*3($inp),$inp # $inp+=3*16
2565 xorps
@tweak[0],$inout0
2566 xorps
@tweak[1],$inout1
2567 xorps
@tweak[2],$inout2
2569 call _aesni_decrypt3
2571 xorps
@tweak[0],$inout0
2572 movdqa
@tweak[3],@tweak[0]
2573 xorps
@tweak[1],$inout1
2574 movdqa
@tweak[4],@tweak[1]
2575 xorps
@tweak[2],$inout2
2576 movups
$inout0,($out) # store 3 output blocks
2577 movups
$inout1,16*1($out)
2578 movups
$inout2,16*2($out)
2579 lea
16*3($out),$out # $out+=3*16
2584 movups
($inp),$inout0
2585 movups
16*1($inp),$inout1
2586 movups
16*2($inp),$inout2
2587 xorps
@tweak[0],$inout0
2588 movups
16*3($inp),$inout3
2589 lea
16*4($inp),$inp # $inp+=4*16
2590 xorps
@tweak[1],$inout1
2591 xorps
@tweak[2],$inout2
2592 xorps
@tweak[3],$inout3
2594 call _aesni_decrypt4
2596 pxor
@tweak[0],$inout0
2597 movdqa
@tweak[4],@tweak[0]
2598 pxor
@tweak[1],$inout1
2599 movdqa
@tweak[5],@tweak[1]
2600 pxor
@tweak[2],$inout2
2601 movdqu
$inout0,($out) # store 4 output blocks
2602 pxor
@tweak[3],$inout3
2603 movdqu
$inout1,16*1($out)
2604 movdqu
$inout2,16*2($out)
2605 movdqu
$inout3,16*3($out)
2606 lea
16*4($out),$out # $out+=4*16
2611 and \
$15,$len_ # see if $len%16 is 0
2615 mov
$key_,$key # restore $key
2616 mov
$rnds_,$rounds # restore $rounds
2618 movups
($inp),$inout0
2619 xorps
@tweak[1],$inout0
2621 &aesni_generate1
("dec",$key,$rounds);
2623 xorps
@tweak[1],$inout0
2624 movups
$inout0,($out)
2627 movzb
16($inp),%eax # borrow $rounds ...
2628 movzb
($out),%ecx # ... and $key
2636 sub $len_,$out # rewind $out
2637 mov
$key_,$key # restore $key
2638 mov
$rnds_,$rounds # restore $rounds
2640 movups
($out),$inout0
2641 xorps
@tweak[0],$inout0
2643 &aesni_generate1
("dec",$key,$rounds);
2645 xorps
@tweak[0],$inout0
2646 movups
$inout0,($out)
2649 xorps
%xmm0,%xmm0 # clear register bank
2656 $code.=<<___
if (!$win64);
2659 movaps
%xmm0,0x00(%rsp) # clear stack
2661 movaps
%xmm0,0x10(%rsp)
2663 movaps
%xmm0,0x20(%rsp)
2665 movaps
%xmm0,0x30(%rsp)
2667 movaps
%xmm0,0x40(%rsp)
2669 movaps
%xmm0,0x50(%rsp)
2671 movaps
%xmm0,0x60(%rsp)
2675 $code.=<<___
if ($win64);
2676 movaps
-0xa0(%rbp),%xmm6
2677 movaps
%xmm0,-0xa0(%rbp) # clear stack
2678 movaps
-0x90(%rbp),%xmm7
2679 movaps
%xmm0,-0x90(%rbp)
2680 movaps
-0x80(%rbp),%xmm8
2681 movaps
%xmm0,-0x80(%rbp)
2682 movaps
-0x70(%rbp),%xmm9
2683 movaps
%xmm0,-0x70(%rbp)
2684 movaps
-0x60(%rbp),%xmm10
2685 movaps
%xmm0,-0x60(%rbp)
2686 movaps
-0x50(%rbp),%xmm11
2687 movaps
%xmm0,-0x50(%rbp)
2688 movaps
-0x40(%rbp),%xmm12
2689 movaps
%xmm0,-0x40(%rbp)
2690 movaps
-0x30(%rbp),%xmm13
2691 movaps
%xmm0,-0x30(%rbp)
2692 movaps
-0x20(%rbp),%xmm14
2693 movaps
%xmm0,-0x20(%rbp)
2694 movaps
-0x10(%rbp),%xmm15
2695 movaps
%xmm0,-0x10(%rbp)
2696 movaps
%xmm0,0x00(%rsp)
2697 movaps
%xmm0,0x10(%rsp)
2698 movaps
%xmm0,0x20(%rsp)
2699 movaps
%xmm0,0x30(%rsp)
2700 movaps
%xmm0,0x40(%rsp)
2701 movaps
%xmm0,0x50(%rsp)
2702 movaps
%xmm0,0x60(%rsp)
2709 .size aesni_xts_decrypt
,.-aesni_xts_decrypt
2713 ########################################################################
2714 # void $PREFIX_cbc_encrypt (const void *inp, void *out,
2715 # size_t length, const AES_KEY *key,
2716 # unsigned char *ivp,const int enc);
2718 my $frame_size = 0x10 + ($win64?
0xa0:0); # used in decrypt
2719 my ($iv,$in0,$in1,$in2,$in3,$in4)=map("%xmm$_",(10..15));
2723 .globl
${PREFIX
}_cbc_encrypt
2724 .type
${PREFIX
}_cbc_encrypt
,\
@function,6
2726 ${PREFIX
}_cbc_encrypt
:
2727 test
$len,$len # check length
2730 mov
240($key),$rnds_ # key->rounds
2731 mov
$key,$key_ # backup $key
2732 test
%r9d,%r9d # 6th argument
2734 #--------------------------- CBC ENCRYPT ------------------------------#
2735 movups
($ivp),$inout0 # load iv as initial state
2743 movups
($inp),$inout1 # load input
2745 #xorps $inout1,$inout0
2747 &aesni_generate1
("enc",$key,$rounds,$inout0,$inout1);
2749 mov
$rnds_,$rounds # restore $rounds
2750 mov
$key_,$key # restore $key
2751 movups
$inout0,0($out) # store output
2757 pxor
$rndkey0,$rndkey0 # clear register bank
2758 pxor
$rndkey1,$rndkey1
2759 movups
$inout0,($ivp)
2760 pxor
$inout0,$inout0
2761 pxor
$inout1,$inout1
2765 mov
$len,%rcx # zaps $key
2766 xchg
$inp,$out # $inp is %rsi and $out is %rdi now
2767 .long
0x9066A4F3 # rep movsb
2768 mov \
$16,%ecx # zero tail
2771 .long
0x9066AAF3 # rep stosb
2772 lea
-16(%rdi),%rdi # rewind $out by 1 block
2773 mov
$rnds_,$rounds # restore $rounds
2774 mov
%rdi,%rsi # $inp and $out are the same
2775 mov
$key_,$key # restore $key
2776 xor $len,$len # len=16
2777 jmp
.Lcbc_enc_loop
# one more spin
2778 \f#--------------------------- CBC DECRYPT ------------------------------#
2782 jne
.Lcbc_decrypt_bulk
2784 # handle single block without allocating stack frame,
2785 # useful in ciphertext stealing mode
2786 movdqu
($inp),$inout0 # load input
2787 movdqu
($ivp),$inout1 # load iv
2788 movdqa
$inout0,$inout2 # future iv
2790 &aesni_generate1
("dec",$key,$rnds_);
2792 pxor
$rndkey0,$rndkey0 # clear register bank
2793 pxor
$rndkey1,$rndkey1
2794 movdqu
$inout2,($ivp) # store iv
2795 xorps
$inout1,$inout0 # ^=iv
2796 pxor
$inout1,$inout1
2797 movups
$inout0,($out) # store output
2798 pxor
$inout0,$inout0
2804 sub \
$$frame_size,%rsp
2805 and \
$-16,%rsp # Linux kernel stack can be incorrectly seeded
2807 $code.=<<___
if ($win64);
2808 movaps
%xmm6,0x10(%rsp)
2809 movaps
%xmm7,0x20(%rsp)
2810 movaps
%xmm8,0x30(%rsp)
2811 movaps
%xmm9,0x40(%rsp)
2812 movaps
%xmm10,0x50(%rsp)
2813 movaps
%xmm11,0x60(%rsp)
2814 movaps
%xmm12,0x70(%rsp)
2815 movaps
%xmm13,0x80(%rsp)
2816 movaps
%xmm14,0x90(%rsp)
2817 movaps
%xmm15,0xa0(%rsp)
2827 $movkey ($key),$rndkey0
2828 movdqu
0x00($inp),$inout0 # load input
2829 movdqu
0x10($inp),$inout1
2831 movdqu
0x20($inp),$inout2
2833 movdqu
0x30($inp),$inout3
2835 movdqu
0x40($inp),$inout4
2837 movdqu
0x50($inp),$inout5
2839 mov OPENSSL_ia32cap_P
+4(%rip),%r9d
2841 jbe
.Lcbc_dec_six_or_seven
2843 and \
$`1<<26|1<<22`,%r9d # isolate XSAVE+MOVBE
2844 sub \
$0x50,$len # $len is biased by -5*16
2845 cmp \
$`1<<22`,%r9d # check for MOVBE without XSAVE
2846 je
.Lcbc_dec_loop6_enter
# [which denotes Atom Silvermont]
2847 sub \
$0x20,$len # $len is biased by -7*16
2848 lea
0x70($key),$key # size optimization
2849 jmp
.Lcbc_dec_loop8_enter
2852 movups
$inout7,($out)
2854 .Lcbc_dec_loop8_enter
:
2855 movdqu
0x60($inp),$inout6
2856 pxor
$rndkey0,$inout0
2857 movdqu
0x70($inp),$inout7
2858 pxor
$rndkey0,$inout1
2859 $movkey 0x10-0x70($key),$rndkey1
2860 pxor
$rndkey0,$inout2
2862 cmp \
$0x70,$len # is there at least 0x60 bytes ahead?
2863 pxor
$rndkey0,$inout3
2864 pxor
$rndkey0,$inout4
2865 pxor
$rndkey0,$inout5
2866 pxor
$rndkey0,$inout6
2868 aesdec
$rndkey1,$inout0
2869 pxor
$rndkey0,$inout7
2870 $movkey 0x20-0x70($key),$rndkey0
2871 aesdec
$rndkey1,$inout1
2872 aesdec
$rndkey1,$inout2
2873 aesdec
$rndkey1,$inout3
2874 aesdec
$rndkey1,$inout4
2875 aesdec
$rndkey1,$inout5
2876 aesdec
$rndkey1,$inout6
2879 aesdec
$rndkey1,$inout7
2881 $movkey 0x30-0x70($key),$rndkey1
2883 for($i=1;$i<12;$i++) {
2884 my $rndkeyx = ($i&1)?
$rndkey0:$rndkey1;
2885 $code.=<<___
if ($i==7);
2889 aesdec
$rndkeyx,$inout0
2890 aesdec
$rndkeyx,$inout1
2891 aesdec
$rndkeyx,$inout2
2892 aesdec
$rndkeyx,$inout3
2893 aesdec
$rndkeyx,$inout4
2894 aesdec
$rndkeyx,$inout5
2895 aesdec
$rndkeyx,$inout6
2896 aesdec
$rndkeyx,$inout7
2897 $movkey `0x30+0x10*$i`-0x70($key),$rndkeyx
2899 $code.=<<___
if ($i<6 || (!($i&1) && $i>7));
2902 $code.=<<___
if ($i==7);
2905 $code.=<<___
if ($i==9);
2908 $code.=<<___
if ($i==11);
2915 aesdec
$rndkey1,$inout0
2916 aesdec
$rndkey1,$inout1
2919 aesdec
$rndkey1,$inout2
2920 aesdec
$rndkey1,$inout3
2923 aesdec
$rndkey1,$inout4
2924 aesdec
$rndkey1,$inout5
2927 aesdec
$rndkey1,$inout6
2928 aesdec
$rndkey1,$inout7
2929 movdqu
0x50($inp),$rndkey1
2931 aesdeclast
$iv,$inout0
2932 movdqu
0x60($inp),$iv # borrow $iv
2933 pxor
$rndkey0,$rndkey1
2934 aesdeclast
$in0,$inout1
2936 movdqu
0x70($inp),$rndkey0 # next IV
2937 aesdeclast
$in1,$inout2
2939 movdqu
0x00($inp_),$in0
2940 aesdeclast
$in2,$inout3
2941 aesdeclast
$in3,$inout4
2942 movdqu
0x10($inp_),$in1
2943 movdqu
0x20($inp_),$in2
2944 aesdeclast
$in4,$inout5
2945 aesdeclast
$rndkey1,$inout6
2946 movdqu
0x30($inp_),$in3
2947 movdqu
0x40($inp_),$in4
2948 aesdeclast
$iv,$inout7
2949 movdqa
$rndkey0,$iv # return $iv
2950 movdqu
0x50($inp_),$rndkey1
2951 $movkey -0x70($key),$rndkey0
2953 movups
$inout0,($out) # store output
2955 movups
$inout1,0x10($out)
2957 movups
$inout2,0x20($out)
2959 movups
$inout3,0x30($out)
2961 movups
$inout4,0x40($out)
2963 movups
$inout5,0x50($out)
2964 movdqa
$rndkey1,$inout5
2965 movups
$inout6,0x60($out)
2971 movaps
$inout7,$inout0
2972 lea
-0x70($key),$key
2974 jle
.Lcbc_dec_clear_tail_collected
2975 movups
$inout7,($out)
2981 .Lcbc_dec_six_or_seven
:
2985 movaps
$inout5,$inout6
2986 call _aesni_decrypt6
2987 pxor
$iv,$inout0 # ^= IV
2990 movdqu
$inout0,($out)
2992 movdqu
$inout1,0x10($out)
2993 pxor
$inout1,$inout1 # clear register bank
2995 movdqu
$inout2,0x20($out)
2996 pxor
$inout2,$inout2
2998 movdqu
$inout3,0x30($out)
2999 pxor
$inout3,$inout3
3001 movdqu
$inout4,0x40($out)
3002 pxor
$inout4,$inout4
3004 movdqa
$inout5,$inout0
3005 pxor
$inout5,$inout5
3006 jmp
.Lcbc_dec_tail_collected
3010 movups
0x60($inp),$inout6
3011 xorps
$inout7,$inout7
3012 call _aesni_decrypt8
3013 movups
0x50($inp),$inout7
3014 pxor
$iv,$inout0 # ^= IV
3015 movups
0x60($inp),$iv
3017 movdqu
$inout0,($out)
3019 movdqu
$inout1,0x10($out)
3020 pxor
$inout1,$inout1 # clear register bank
3022 movdqu
$inout2,0x20($out)
3023 pxor
$inout2,$inout2
3025 movdqu
$inout3,0x30($out)
3026 pxor
$inout3,$inout3
3028 movdqu
$inout4,0x40($out)
3029 pxor
$inout4,$inout4
3030 pxor
$inout7,$inout6
3031 movdqu
$inout5,0x50($out)
3032 pxor
$inout5,$inout5
3034 movdqa
$inout6,$inout0
3035 pxor
$inout6,$inout6
3036 pxor
$inout7,$inout7
3037 jmp
.Lcbc_dec_tail_collected
3041 movups
$inout5,($out)
3043 movdqu
0x00($inp),$inout0 # load input
3044 movdqu
0x10($inp),$inout1
3046 movdqu
0x20($inp),$inout2
3048 movdqu
0x30($inp),$inout3
3050 movdqu
0x40($inp),$inout4
3052 movdqu
0x50($inp),$inout5
3054 .Lcbc_dec_loop6_enter
:
3056 movdqa
$inout5,$inout6
3058 call _aesni_decrypt6
3060 pxor
$iv,$inout0 # ^= IV
3063 movdqu
$inout0,($out)
3065 movdqu
$inout1,0x10($out)
3067 movdqu
$inout2,0x20($out)
3070 movdqu
$inout3,0x30($out)
3073 movdqu
$inout4,0x40($out)
3078 movdqa
$inout5,$inout0
3080 jle
.Lcbc_dec_clear_tail_collected
3081 movups
$inout5,($out)
3085 movups
($inp),$inout0
3087 jbe
.Lcbc_dec_one
# $len is 1*16 or less
3089 movups
0x10($inp),$inout1
3092 jbe
.Lcbc_dec_two
# $len is 2*16 or less
3094 movups
0x20($inp),$inout2
3097 jbe
.Lcbc_dec_three
# $len is 3*16 or less
3099 movups
0x30($inp),$inout3
3102 jbe
.Lcbc_dec_four
# $len is 4*16 or less
3104 movups
0x40($inp),$inout4 # $len is 5*16 or less
3107 xorps
$inout5,$inout5
3108 call _aesni_decrypt6
3112 movdqu
$inout0,($out)
3114 movdqu
$inout1,0x10($out)
3115 pxor
$inout1,$inout1 # clear register bank
3117 movdqu
$inout2,0x20($out)
3118 pxor
$inout2,$inout2
3120 movdqu
$inout3,0x30($out)
3121 pxor
$inout3,$inout3
3123 movdqa
$inout4,$inout0
3124 pxor
$inout4,$inout4
3125 pxor
$inout5,$inout5
3127 jmp
.Lcbc_dec_tail_collected
3133 &aesni_generate1
("dec",$key,$rounds);
3137 jmp
.Lcbc_dec_tail_collected
3141 call _aesni_decrypt2
3145 movdqu
$inout0,($out)
3146 movdqa
$inout1,$inout0
3147 pxor
$inout1,$inout1 # clear register bank
3149 jmp
.Lcbc_dec_tail_collected
3153 call _aesni_decrypt3
3157 movdqu
$inout0,($out)
3159 movdqu
$inout1,0x10($out)
3160 pxor
$inout1,$inout1 # clear register bank
3161 movdqa
$inout2,$inout0
3162 pxor
$inout2,$inout2
3164 jmp
.Lcbc_dec_tail_collected
3168 call _aesni_decrypt4
3172 movdqu
$inout0,($out)
3174 movdqu
$inout1,0x10($out)
3175 pxor
$inout1,$inout1 # clear register bank
3177 movdqu
$inout2,0x20($out)
3178 pxor
$inout2,$inout2
3179 movdqa
$inout3,$inout0
3180 pxor
$inout3,$inout3
3182 jmp
.Lcbc_dec_tail_collected
3185 .Lcbc_dec_clear_tail_collected
:
3186 pxor
$inout1,$inout1 # clear register bank
3187 pxor
$inout2,$inout2
3188 pxor
$inout3,$inout3
3190 $code.=<<___
if (!$win64);
3191 pxor
$inout4,$inout4 # %xmm6..9
3192 pxor
$inout5,$inout5
3193 pxor
$inout6,$inout6
3194 pxor
$inout7,$inout7
3197 .Lcbc_dec_tail_collected
:
3200 jnz
.Lcbc_dec_tail_partial
3201 movups
$inout0,($out)
3202 pxor
$inout0,$inout0
3205 .Lcbc_dec_tail_partial
:
3206 movaps
$inout0,(%rsp)
3207 pxor
$inout0,$inout0
3212 .long
0x9066A4F3 # rep movsb
3213 movdqa
$inout0,(%rsp)
3216 xorps
$rndkey0,$rndkey0 # %xmm0
3217 pxor
$rndkey1,$rndkey1
3219 $code.=<<___
if ($win64);
3220 movaps
0x10(%rsp),%xmm6
3221 movaps
%xmm0,0x10(%rsp) # clear stack
3222 movaps
0x20(%rsp),%xmm7
3223 movaps
%xmm0,0x20(%rsp)
3224 movaps
0x30(%rsp),%xmm8
3225 movaps
%xmm0,0x30(%rsp)
3226 movaps
0x40(%rsp),%xmm9
3227 movaps
%xmm0,0x40(%rsp)
3228 movaps
0x50(%rsp),%xmm10
3229 movaps
%xmm0,0x50(%rsp)
3230 movaps
0x60(%rsp),%xmm11
3231 movaps
%xmm0,0x60(%rsp)
3232 movaps
0x70(%rsp),%xmm12
3233 movaps
%xmm0,0x70(%rsp)
3234 movaps
0x80(%rsp),%xmm13
3235 movaps
%xmm0,0x80(%rsp)
3236 movaps
0x90(%rsp),%xmm14
3237 movaps
%xmm0,0x90(%rsp)
3238 movaps
0xa0(%rsp),%xmm15
3239 movaps
%xmm0,0xa0(%rsp)
3246 .size
${PREFIX
}_cbc_encrypt
,.-${PREFIX
}_cbc_encrypt
3249 # int ${PREFIX}_set_decrypt_key(const unsigned char *inp,
3250 # int bits, AES_KEY *key)
3252 # input: $inp user-supplied key
3253 # $bits $inp length in bits
3254 # $key pointer to key schedule
3255 # output: %eax 0 denoting success, -1 or -2 - failure (see C)
3256 # *$key key schedule
3258 { my ($inp,$bits,$key) = @_4args;
3262 .globl
${PREFIX
}_set_decrypt_key
3263 .type
${PREFIX
}_set_decrypt_key
,\
@abi-omnipotent
3265 ${PREFIX
}_set_decrypt_key
:
3266 .byte
0x48,0x83,0xEC,0x08 # sub rsp,8
3267 call __aesni_set_encrypt_key
3268 shl \
$4,$bits # rounds-1 after _aesni_set_encrypt_key
3271 lea
16($key,$bits),$inp # points at the end of key schedule
3273 $movkey ($key),%xmm0 # just swap
3274 $movkey ($inp),%xmm1
3275 $movkey %xmm0,($inp)
3276 $movkey %xmm1,($key)
3281 $movkey ($key),%xmm0 # swap and inverse
3282 $movkey ($inp),%xmm1
3287 $movkey %xmm0,16($inp)
3288 $movkey %xmm1,-16($key)
3290 ja
.Ldec_key_inverse
3292 $movkey ($key),%xmm0 # inverse middle
3295 $movkey %xmm0,($inp)
3300 .LSEH_end_set_decrypt_key
:
3301 .size
${PREFIX
}_set_decrypt_key
,.-${PREFIX
}_set_decrypt_key
3304 # This is based on submission by
3306 # Huang Ying <ying.huang@intel.com>
3307 # Vinodh Gopal <vinodh.gopal@intel.com>
3310 # Agressively optimized in respect to aeskeygenassist's critical path
3311 # and is contained in %xmm0-5 to meet Win64 ABI requirement.
3313 # int ${PREFIX}_set_encrypt_key(const unsigned char *inp,
3314 # int bits, AES_KEY * const key);
3316 # input: $inp user-supplied key
3317 # $bits $inp length in bits
3318 # $key pointer to key schedule
3319 # output: %eax 0 denoting success, -1 or -2 - failure (see C)
3320 # $bits rounds-1 (used in aesni_set_decrypt_key)
3321 # *$key key schedule
3322 # $key pointer to key schedule (used in
3323 # aesni_set_decrypt_key)
3325 # Subroutine is frame-less, which means that only volatile registers
3326 # are used. Note that it's declared "abi-omnipotent", which means that
3327 # amount of volatile registers is smaller on Windows.
3330 .globl
${PREFIX
}_set_encrypt_key
3331 .type
${PREFIX
}_set_encrypt_key
,\
@abi-omnipotent
3333 ${PREFIX
}_set_encrypt_key
:
3334 __aesni_set_encrypt_key
:
3335 .byte
0x48,0x83,0xEC,0x08 # sub rsp,8
3342 mov \
$`1<<28|1<<11`,%r10d # AVX and XOP bits
3343 movups
($inp),%xmm0 # pull first 128 bits of *userKey
3344 xorps
%xmm4,%xmm4 # low dword of xmm4 is assumed 0
3345 and OPENSSL_ia32cap_P
+4(%rip),%r10d
3346 lea
16($key),%rax # %rax is used as modifiable copy of $key
3355 mov \
$9,$bits # 10 rounds for 128-bit key
3356 cmp \
$`1<<28`,%r10d # AVX, bit no XOP
3359 $movkey %xmm0,($key) # round 0
3360 aeskeygenassist \
$0x1,%xmm0,%xmm1 # round 1
3361 call
.Lkey_expansion_128_cold
3362 aeskeygenassist \
$0x2,%xmm0,%xmm1 # round 2
3363 call
.Lkey_expansion_128
3364 aeskeygenassist \
$0x4,%xmm0,%xmm1 # round 3
3365 call
.Lkey_expansion_128
3366 aeskeygenassist \
$0x8,%xmm0,%xmm1 # round 4
3367 call
.Lkey_expansion_128
3368 aeskeygenassist \
$0x10,%xmm0,%xmm1 # round 5
3369 call
.Lkey_expansion_128
3370 aeskeygenassist \
$0x20,%xmm0,%xmm1 # round 6
3371 call
.Lkey_expansion_128
3372 aeskeygenassist \
$0x40,%xmm0,%xmm1 # round 7
3373 call
.Lkey_expansion_128
3374 aeskeygenassist \
$0x80,%xmm0,%xmm1 # round 8
3375 call
.Lkey_expansion_128
3376 aeskeygenassist \
$0x1b,%xmm0,%xmm1 # round 9
3377 call
.Lkey_expansion_128
3378 aeskeygenassist \
$0x36,%xmm0,%xmm1 # round 10
3379 call
.Lkey_expansion_128
3380 $movkey %xmm0,(%rax)
3381 mov
$bits,80(%rax) # 240(%rdx)
3387 movdqa
.Lkey_rotate
(%rip),%xmm5
3389 movdqa
.Lkey_rcon1
(%rip),%xmm4
3397 aesenclast
%xmm4,%xmm0
3410 movdqu
%xmm0,-16(%rax)
3416 movdqa
.Lkey_rcon1b
(%rip),%xmm4
3419 aesenclast
%xmm4,%xmm0
3435 aesenclast
%xmm4,%xmm0
3446 movdqu
%xmm0,16(%rax)
3448 mov
$bits,96(%rax) # 240($key)
3454 movq
16($inp),%xmm2 # remaining 1/3 of *userKey
3455 mov \
$11,$bits # 12 rounds for 192
3456 cmp \
$`1<<28`,%r10d # AVX, but no XOP
3459 $movkey %xmm0,($key) # round 0
3460 aeskeygenassist \
$0x1,%xmm2,%xmm1 # round 1,2
3461 call
.Lkey_expansion_192a_cold
3462 aeskeygenassist \
$0x2,%xmm2,%xmm1 # round 2,3
3463 call
.Lkey_expansion_192b
3464 aeskeygenassist \
$0x4,%xmm2,%xmm1 # round 4,5
3465 call
.Lkey_expansion_192a
3466 aeskeygenassist \
$0x8,%xmm2,%xmm1 # round 5,6
3467 call
.Lkey_expansion_192b
3468 aeskeygenassist \
$0x10,%xmm2,%xmm1 # round 7,8
3469 call
.Lkey_expansion_192a
3470 aeskeygenassist \
$0x20,%xmm2,%xmm1 # round 8,9
3471 call
.Lkey_expansion_192b
3472 aeskeygenassist \
$0x40,%xmm2,%xmm1 # round 10,11
3473 call
.Lkey_expansion_192a
3474 aeskeygenassist \
$0x80,%xmm2,%xmm1 # round 11,12
3475 call
.Lkey_expansion_192b
3476 $movkey %xmm0,(%rax)
3477 mov
$bits,48(%rax) # 240(%rdx)
3483 movdqa
.Lkey_rotate192
(%rip),%xmm5
3484 movdqa
.Lkey_rcon1
(%rip),%xmm4
3494 aesenclast
%xmm4,%xmm2
3506 pshufd \
$0xff,%xmm0,%xmm3
3513 movdqu
%xmm0,-16(%rax)
3518 mov
$bits,32(%rax) # 240($key)
3524 movups
16($inp),%xmm2 # remaning half of *userKey
3525 mov \
$13,$bits # 14 rounds for 256
3527 cmp \
$`1<<28`,%r10d # AVX, but no XOP
3530 $movkey %xmm0,($key) # round 0
3531 $movkey %xmm2,16($key) # round 1
3532 aeskeygenassist \
$0x1,%xmm2,%xmm1 # round 2
3533 call
.Lkey_expansion_256a_cold
3534 aeskeygenassist \
$0x1,%xmm0,%xmm1 # round 3
3535 call
.Lkey_expansion_256b
3536 aeskeygenassist \
$0x2,%xmm2,%xmm1 # round 4
3537 call
.Lkey_expansion_256a
3538 aeskeygenassist \
$0x2,%xmm0,%xmm1 # round 5
3539 call
.Lkey_expansion_256b
3540 aeskeygenassist \
$0x4,%xmm2,%xmm1 # round 6
3541 call
.Lkey_expansion_256a
3542 aeskeygenassist \
$0x4,%xmm0,%xmm1 # round 7
3543 call
.Lkey_expansion_256b
3544 aeskeygenassist \
$0x8,%xmm2,%xmm1 # round 8
3545 call
.Lkey_expansion_256a
3546 aeskeygenassist \
$0x8,%xmm0,%xmm1 # round 9
3547 call
.Lkey_expansion_256b
3548 aeskeygenassist \
$0x10,%xmm2,%xmm1 # round 10
3549 call
.Lkey_expansion_256a
3550 aeskeygenassist \
$0x10,%xmm0,%xmm1 # round 11
3551 call
.Lkey_expansion_256b
3552 aeskeygenassist \
$0x20,%xmm2,%xmm1 # round 12
3553 call
.Lkey_expansion_256a
3554 aeskeygenassist \
$0x20,%xmm0,%xmm1 # round 13
3555 call
.Lkey_expansion_256b
3556 aeskeygenassist \
$0x40,%xmm2,%xmm1 # round 14
3557 call
.Lkey_expansion_256a
3558 $movkey %xmm0,(%rax)
3559 mov
$bits,16(%rax) # 240(%rdx)
3565 movdqa
.Lkey_rotate
(%rip),%xmm5
3566 movdqa
.Lkey_rcon1
(%rip),%xmm4
3568 movdqu
%xmm0,0($key)
3570 movdqu
%xmm2,16($key)
3576 aesenclast
%xmm4,%xmm2
3593 pshufd \
$0xff,%xmm0,%xmm2
3595 aesenclast
%xmm3,%xmm2
3606 movdqu
%xmm2,16(%rax)
3613 mov
$bits,16(%rax) # 240($key)
3629 .LSEH_end_set_encrypt_key
:
3632 .Lkey_expansion_128
:
3633 $movkey %xmm0,(%rax)
3635 .Lkey_expansion_128_cold
:
3636 shufps \
$0b00010000,%xmm0,%xmm4
3638 shufps \
$0b10001100,%xmm0,%xmm4
3640 shufps \
$0b11111111,%xmm1,%xmm1 # critical path
3645 .Lkey_expansion_192a
:
3646 $movkey %xmm0,(%rax)
3648 .Lkey_expansion_192a_cold
:
3650 .Lkey_expansion_192b_warm
:
3651 shufps \
$0b00010000,%xmm0,%xmm4
3654 shufps \
$0b10001100,%xmm0,%xmm4
3657 pshufd \
$0b01010101,%xmm1,%xmm1 # critical path
3660 pshufd \
$0b11111111,%xmm0,%xmm3
3665 .Lkey_expansion_192b
:
3667 shufps \
$0b01000100,%xmm0,%xmm5
3668 $movkey %xmm5,(%rax)
3669 shufps \
$0b01001110,%xmm2,%xmm3
3670 $movkey %xmm3,16(%rax)
3672 jmp
.Lkey_expansion_192b_warm
3675 .Lkey_expansion_256a
:
3676 $movkey %xmm2,(%rax)
3678 .Lkey_expansion_256a_cold
:
3679 shufps \
$0b00010000,%xmm0,%xmm4
3681 shufps \
$0b10001100,%xmm0,%xmm4
3683 shufps \
$0b11111111,%xmm1,%xmm1 # critical path
3688 .Lkey_expansion_256b
:
3689 $movkey %xmm0,(%rax)
3692 shufps \
$0b00010000,%xmm2,%xmm4
3694 shufps \
$0b10001100,%xmm2,%xmm4
3696 shufps \
$0b10101010,%xmm1,%xmm1 # critical path
3699 .size
${PREFIX
}_set_encrypt_key
,.-${PREFIX
}_set_encrypt_key
3700 .size __aesni_set_encrypt_key
,.-__aesni_set_encrypt_key
3707 .byte
15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
3715 .byte
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
3717 .long
0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d
3719 .long
0x04070605,0x04070605,0x04070605,0x04070605
3723 .long
0x1b,0x1b,0x1b,0x1b
3725 .asciz
"AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
3729 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
3730 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
3738 .extern __imp_RtlVirtualUnwind
3740 $code.=<<___
if ($PREFIX eq "aesni");
3741 .type ecb_ccm64_se_handler
,\
@abi-omnipotent
3743 ecb_ccm64_se_handler
:
3755 mov
120($context),%rax # pull context->Rax
3756 mov
248($context),%rbx # pull context->Rip
3758 mov
8($disp),%rsi # disp->ImageBase
3759 mov
56($disp),%r11 # disp->HandlerData
3761 mov
0(%r11),%r10d # HandlerData[0]
3762 lea
(%rsi,%r10),%r10 # prologue label
3763 cmp %r10,%rbx # context->Rip<prologue label
3764 jb
.Lcommon_seh_tail
3766 mov
152($context),%rax # pull context->Rsp
3768 mov
4(%r11),%r10d # HandlerData[1]
3769 lea
(%rsi,%r10),%r10 # epilogue label
3770 cmp %r10,%rbx # context->Rip>=epilogue label
3771 jae
.Lcommon_seh_tail
3773 lea
0(%rax),%rsi # %xmm save area
3774 lea
512($context),%rdi # &context.Xmm6
3775 mov \
$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
3776 .long
0xa548f3fc # cld; rep movsq
3777 lea
0x58(%rax),%rax # adjust stack pointer
3779 jmp
.Lcommon_seh_tail
3780 .size ecb_ccm64_se_handler
,.-ecb_ccm64_se_handler
3782 .type ctr_xts_se_handler
,\
@abi-omnipotent
3796 mov
120($context),%rax # pull context->Rax
3797 mov
248($context),%rbx # pull context->Rip
3799 mov
8($disp),%rsi # disp->ImageBase
3800 mov
56($disp),%r11 # disp->HandlerData
3802 mov
0(%r11),%r10d # HandlerData[0]
3803 lea
(%rsi,%r10),%r10 # prologue lable
3804 cmp %r10,%rbx # context->Rip<prologue label
3805 jb
.Lcommon_seh_tail
3807 mov
152($context),%rax # pull context->Rsp
3809 mov
4(%r11),%r10d # HandlerData[1]
3810 lea
(%rsi,%r10),%r10 # epilogue label
3811 cmp %r10,%rbx # context->Rip>=epilogue label
3812 jae
.Lcommon_seh_tail
3814 mov
160($context),%rax # pull context->Rbp
3815 lea
-0xa0(%rax),%rsi # %xmm save area
3816 lea
512($context),%rdi # & context.Xmm6
3817 mov \
$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
3818 .long
0xa548f3fc # cld; rep movsq
3820 jmp
.Lcommon_rbp_tail
3821 .size ctr_xts_se_handler
,.-ctr_xts_se_handler
3824 .type cbc_se_handler
,\
@abi-omnipotent
3838 mov
152($context),%rax # pull context->Rsp
3839 mov
248($context),%rbx # pull context->Rip
3841 lea
.Lcbc_decrypt_bulk
(%rip),%r10
3842 cmp %r10,%rbx # context->Rip<"prologue" label
3843 jb
.Lcommon_seh_tail
3845 lea
.Lcbc_decrypt_body
(%rip),%r10
3846 cmp %r10,%rbx # context->Rip<cbc_decrypt_body
3847 jb
.Lrestore_cbc_rax
3849 lea
.Lcbc_ret
(%rip),%r10
3850 cmp %r10,%rbx # context->Rip>="epilogue" label
3851 jae
.Lcommon_seh_tail
3853 lea
16(%rax),%rsi # %xmm save area
3854 lea
512($context),%rdi # &context.Xmm6
3855 mov \
$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
3856 .long
0xa548f3fc # cld; rep movsq
3859 mov
160($context),%rax # pull context->Rbp
3860 mov
(%rax),%rbp # restore saved %rbp
3861 lea
8(%rax),%rax # adjust stack pointer
3862 mov
%rbp,160($context) # restore context->Rbp
3863 jmp
.Lcommon_seh_tail
3866 mov
120($context),%rax
3871 mov
%rax,152($context) # restore context->Rsp
3872 mov
%rsi,168($context) # restore context->Rsi
3873 mov
%rdi,176($context) # restore context->Rdi
3875 mov
40($disp),%rdi # disp->ContextRecord
3876 mov
$context,%rsi # context
3877 mov \
$154,%ecx # sizeof(CONTEXT)
3878 .long
0xa548f3fc # cld; rep movsq
3881 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
3882 mov
8(%rsi),%rdx # arg2, disp->ImageBase
3883 mov
0(%rsi),%r8 # arg3, disp->ControlPc
3884 mov
16(%rsi),%r9 # arg4, disp->FunctionEntry
3885 mov
40(%rsi),%r10 # disp->ContextRecord
3886 lea
56(%rsi),%r11 # &disp->HandlerData
3887 lea
24(%rsi),%r12 # &disp->EstablisherFrame
3888 mov
%r10,32(%rsp) # arg5
3889 mov
%r11,40(%rsp) # arg6
3890 mov
%r12,48(%rsp) # arg7
3891 mov
%rcx,56(%rsp) # arg8, (NULL)
3892 call
*__imp_RtlVirtualUnwind
(%rip)
3894 mov \
$1,%eax # ExceptionContinueSearch
3906 .size cbc_se_handler
,.-cbc_se_handler
3911 $code.=<<___
if ($PREFIX eq "aesni");
3912 .rva
.LSEH_begin_aesni_ecb_encrypt
3913 .rva
.LSEH_end_aesni_ecb_encrypt
3916 .rva
.LSEH_begin_aesni_ccm64_encrypt_blocks
3917 .rva
.LSEH_end_aesni_ccm64_encrypt_blocks
3918 .rva
.LSEH_info_ccm64_enc
3920 .rva
.LSEH_begin_aesni_ccm64_decrypt_blocks
3921 .rva
.LSEH_end_aesni_ccm64_decrypt_blocks
3922 .rva
.LSEH_info_ccm64_dec
3924 .rva
.LSEH_begin_aesni_ctr32_encrypt_blocks
3925 .rva
.LSEH_end_aesni_ctr32_encrypt_blocks
3926 .rva
.LSEH_info_ctr32
3928 .rva
.LSEH_begin_aesni_xts_encrypt
3929 .rva
.LSEH_end_aesni_xts_encrypt
3930 .rva
.LSEH_info_xts_enc
3932 .rva
.LSEH_begin_aesni_xts_decrypt
3933 .rva
.LSEH_end_aesni_xts_decrypt
3934 .rva
.LSEH_info_xts_dec
3937 .rva
.LSEH_begin_
${PREFIX
}_cbc_encrypt
3938 .rva
.LSEH_end_
${PREFIX
}_cbc_encrypt
3941 .rva
${PREFIX
}_set_decrypt_key
3942 .rva
.LSEH_end_set_decrypt_key
3945 .rva
${PREFIX
}_set_encrypt_key
3946 .rva
.LSEH_end_set_encrypt_key
3951 $code.=<<___
if ($PREFIX eq "aesni");
3954 .rva ecb_ccm64_se_handler
3955 .rva
.Lecb_enc_body
,.Lecb_enc_ret
# HandlerData[]
3956 .LSEH_info_ccm64_enc
:
3958 .rva ecb_ccm64_se_handler
3959 .rva
.Lccm64_enc_body
,.Lccm64_enc_ret
# HandlerData[]
3960 .LSEH_info_ccm64_dec
:
3962 .rva ecb_ccm64_se_handler
3963 .rva
.Lccm64_dec_body
,.Lccm64_dec_ret
# HandlerData[]
3966 .rva ctr_xts_se_handler
3967 .rva
.Lctr32_body
,.Lctr32_epilogue
# HandlerData[]
3970 .rva ctr_xts_se_handler
3971 .rva
.Lxts_enc_body
,.Lxts_enc_epilogue
# HandlerData[]
3974 .rva ctr_xts_se_handler
3975 .rva
.Lxts_dec_body
,.Lxts_dec_epilogue
# HandlerData[]
3982 .byte
0x01,0x04,0x01,0x00
3983 .byte
0x04,0x02,0x00,0x00 # sub rsp,8
3988 local *opcode
=shift;
3992 $rex|=0x04 if($dst>=8);
3993 $rex|=0x01 if($src>=8);
3994 push @opcode,$rex|0x40 if($rex);
4001 if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
4002 rex
(\
@opcode,$4,$3);
4003 push @opcode,0x0f,0x3a,0xdf;
4004 push @opcode,0xc0|($3&7)|(($4&7)<<3); # ModR/M
4006 push @opcode,$c=~/^0/?
oct($c):$c;
4007 return ".byte\t".join(',',@opcode);
4009 elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
4012 "aesenc" => 0xdc, "aesenclast" => 0xdd,
4013 "aesdec" => 0xde, "aesdeclast" => 0xdf
4015 return undef if (!defined($opcodelet{$1}));
4016 rex
(\
@opcode,$3,$2);
4017 push @opcode,0x0f,0x38,$opcodelet{$1};
4018 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
4019 return ".byte\t".join(',',@opcode);
4021 elsif ($line=~/(aes[a-z]+)\s+([0x1-9a-fA-F]*)\(%rsp\),\s*%xmm([0-9]+)/) {
4023 "aesenc" => 0xdc, "aesenclast" => 0xdd,
4024 "aesdec" => 0xde, "aesdeclast" => 0xdf
4026 return undef if (!defined($opcodelet{$1}));
4028 push @opcode,0x44 if ($3>=8);
4029 push @opcode,0x0f,0x38,$opcodelet{$1};
4030 push @opcode,0x44|(($3&7)<<3),0x24; # ModR/M
4031 push @opcode,($off=~/^0/?
oct($off):$off)&0xff;
4032 return ".byte\t".join(',',@opcode);
4038 ".byte 0x0f,0x38,0xf1,0x44,0x24,".shift;
4041 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
4042 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;
4043 #$code =~ s/\bmovbe\s+%eax/bswap %eax; mov %eax/gm; # debugging artefact
4044 $code =~ s/\bmovbe\s+%eax,\s*([0-9]+)\(%rsp\)/movbe($1)/gem;