1 | #! /usr/bin/env perl
|
---|
2 | # Copyright 2009-2020 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | #
|
---|
4 | # Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
5 | # this file except in compliance with the License. You can obtain a copy
|
---|
6 | # in the file LICENSE in the source distribution or at
|
---|
7 | # https://www.openssl.org/source/license.html
|
---|
8 |
|
---|
9 | #
|
---|
10 | # ====================================================================
|
---|
11 | # Written by Andy Polyakov <[email protected]> for the OpenSSL
|
---|
12 | # project. The module is, however, dual licensed under OpenSSL and
|
---|
13 | # CRYPTOGAMS licenses depending on where you obtain it. For further
|
---|
14 | # details see http://www.openssl.org/~appro/cryptogams/.
|
---|
15 | # ====================================================================
|
---|
16 | #
|
---|
17 | # This module implements support for Intel AES-NI extension. In
|
---|
18 | # OpenSSL context it's used with Intel engine, but can also be used as
|
---|
19 | # drop-in replacement for crypto/aes/asm/aes-x86_64.pl [see below for
|
---|
20 | # details].
|
---|
21 | #
|
---|
22 | # Performance.
|
---|
23 | #
|
---|
24 | # Given aes(enc|dec) instructions' latency asymptotic performance for
|
---|
25 | # non-parallelizable modes such as CBC encrypt is 3.75 cycles per byte
|
---|
26 | # processed with 128-bit key. And given their throughput asymptotic
|
---|
27 | # performance for parallelizable modes is 1.25 cycles per byte. Being
|
---|
28 | # asymptotic limit it's not something you commonly achieve in reality,
|
---|
29 | # but how close does one get? Below are results collected for
|
---|
30 | # different modes and block sized. Pairs of numbers are for en-/
|
---|
31 | # decryption.
|
---|
32 | #
|
---|
33 | # 16-byte 64-byte 256-byte 1-KB 8-KB
|
---|
34 | # ECB 4.25/4.25 1.38/1.38 1.28/1.28 1.26/1.26 1.26/1.26
|
---|
35 | # CTR 5.42/5.42 1.92/1.92 1.44/1.44 1.28/1.28 1.26/1.26
|
---|
36 | # CBC 4.38/4.43 4.15/1.43 4.07/1.32 4.07/1.29 4.06/1.28
|
---|
37 | # CCM 5.66/9.42 4.42/5.41 4.16/4.40 4.09/4.15 4.06/4.07
|
---|
38 | # OFB 5.42/5.42 4.64/4.64 4.44/4.44 4.39/4.39 4.38/4.38
|
---|
39 | # CFB 5.73/5.85 5.56/5.62 5.48/5.56 5.47/5.55 5.47/5.55
|
---|
40 | #
|
---|
41 | # ECB, CTR, CBC and CCM results are free from EVP overhead. This means
|
---|
42 | # that otherwise used 'openssl speed -evp aes-128-??? -engine aesni
|
---|
43 | # [-decrypt]' will exhibit 10-15% worse results for smaller blocks.
|
---|
44 | # The results were collected with specially crafted speed.c benchmark
|
---|
45 | # in order to compare them with results reported in "Intel Advanced
|
---|
46 | # Encryption Standard (AES) New Instruction Set" White Paper Revision
|
---|
47 | # 3.0 dated May 2010. All above results are consistently better. This
|
---|
48 | # module also provides better performance for block sizes smaller than
|
---|
49 | # 128 bytes in points *not* represented in the above table.
|
---|
50 | #
|
---|
51 | # Looking at the results for 8-KB buffer.
|
---|
52 | #
|
---|
53 | # CFB and OFB results are far from the limit, because implementation
|
---|
54 | # uses "generic" CRYPTO_[c|o]fb128_encrypt interfaces relying on
|
---|
55 | # single-block aesni_encrypt, which is not the most optimal way to go.
|
---|
56 | # CBC encrypt result is unexpectedly high and there is no documented
|
---|
57 | # explanation for it. Seemingly there is a small penalty for feeding
|
---|
58 | # the result back to AES unit the way it's done in CBC mode. There is
|
---|
59 | # nothing one can do and the result appears optimal. CCM result is
|
---|
60 | # identical to CBC, because CBC-MAC is essentially CBC encrypt without
|
---|
61 | # saving output. CCM CTR "stays invisible," because it's neatly
|
---|
62 | # interleaved with CBC-MAC. This provides ~30% improvement over
|
---|
63 | # "straightforward" CCM implementation with CTR and CBC-MAC performed
|
---|
64 | # disjointly. Parallelizable modes practically achieve the theoretical
|
---|
65 | # limit.
|
---|
66 | #
|
---|
67 | # Looking at how results vary with buffer size.
|
---|
68 | #
|
---|
69 | # Curves are practically saturated at 1-KB buffer size. In most cases
|
---|
70 | # "256-byte" performance is >95%, and "64-byte" is ~90% of "8-KB" one.
|
---|
71 | # CTR curve doesn't follow this pattern and is "slowest" changing one
|
---|
72 | # with "256-byte" result being 87% of "8-KB." This is because overhead
|
---|
73 | # in CTR mode is most computationally intensive. Small-block CCM
|
---|
74 | # decrypt is slower than encrypt, because first CTR and last CBC-MAC
|
---|
75 | # iterations can't be interleaved.
|
---|
76 | #
|
---|
77 | # Results for 192- and 256-bit keys.
|
---|
78 | #
|
---|
79 | # EVP-free results were observed to scale perfectly with number of
|
---|
80 | # rounds for larger block sizes, i.e. 192-bit result being 10/12 times
|
---|
81 | # lower and 256-bit one - 10/14. Well, in CBC encrypt case differences
|
---|
82 | # are a tad smaller, because the above mentioned penalty biases all
|
---|
83 | # results by same constant value. In similar way function call
|
---|
84 | # overhead affects small-block performance, as well as OFB and CFB
|
---|
85 | # results. Differences are not large, most common coefficients are
|
---|
86 | # 10/11.7 and 10/13.4 (as opposite to 10/12.0 and 10/14.0), but one
|
---|
87 | # observe even 10/11.2 and 10/12.4 (CTR, OFB, CFB)...
|
---|
88 |
|
---|
89 | # January 2011
|
---|
90 | #
|
---|
91 | # While Westmere processor features 6 cycles latency for aes[enc|dec]
|
---|
92 | # instructions, which can be scheduled every second cycle, Sandy
|
---|
93 | # Bridge spends 8 cycles per instruction, but it can schedule them
|
---|
94 | # every cycle. This means that code targeting Westmere would perform
|
---|
95 | # suboptimally on Sandy Bridge. Therefore this update.
|
---|
96 | #
|
---|
97 | # In addition, non-parallelizable CBC encrypt (as well as CCM) is
|
---|
98 | # optimized. Relative improvement might appear modest, 8% on Westmere,
|
---|
99 | # but in absolute terms it's 3.77 cycles per byte encrypted with
|
---|
100 | # 128-bit key on Westmere, and 5.07 - on Sandy Bridge. These numbers
|
---|
101 | # should be compared to asymptotic limits of 3.75 for Westmere and
|
---|
102 | # 5.00 for Sandy Bridge. Actually, the fact that they get this close
|
---|
103 | # to asymptotic limits is quite amazing. Indeed, the limit is
|
---|
104 | # calculated as latency times number of rounds, 10 for 128-bit key,
|
---|
105 | # and divided by 16, the number of bytes in block, or in other words
|
---|
106 | # it accounts *solely* for aesenc instructions. But there are extra
|
---|
107 | # instructions, and numbers so close to the asymptotic limits mean
|
---|
108 | # that it's as if it takes as little as *one* additional cycle to
|
---|
109 | # execute all of them. How is it possible? It is possible thanks to
|
---|
110 | # out-of-order execution logic, which manages to overlap post-
|
---|
111 | # processing of previous block, things like saving the output, with
|
---|
112 | # actual encryption of current block, as well as pre-processing of
|
---|
113 | # current block, things like fetching input and xor-ing it with
|
---|
114 | # 0-round element of the key schedule, with actual encryption of
|
---|
115 | # previous block. Keep this in mind...
|
---|
116 | #
|
---|
117 | # For parallelizable modes, such as ECB, CBC decrypt, CTR, higher
|
---|
118 | # performance is achieved by interleaving instructions working on
|
---|
119 | # independent blocks. In which case asymptotic limit for such modes
|
---|
120 | # can be obtained by dividing above mentioned numbers by AES
|
---|
121 | # instructions' interleave factor. Westmere can execute at most 3
|
---|
122 | # instructions at a time, meaning that optimal interleave factor is 3,
|
---|
123 | # and that's where the "magic" number of 1.25 come from. "Optimal
|
---|
124 | # interleave factor" means that increase of interleave factor does
|
---|
125 | # not improve performance. The formula has proven to reflect reality
|
---|
126 | # pretty well on Westmere... Sandy Bridge on the other hand can
|
---|
127 | # execute up to 8 AES instructions at a time, so how does varying
|
---|
128 | # interleave factor affect the performance? Here is table for ECB
|
---|
129 | # (numbers are cycles per byte processed with 128-bit key):
|
---|
130 | #
|
---|
131 | # instruction interleave factor 3x 6x 8x
|
---|
132 | # theoretical asymptotic limit 1.67 0.83 0.625
|
---|
133 | # measured performance for 8KB block 1.05 0.86 0.84
|
---|
134 | #
|
---|
135 | # "as if" interleave factor 4.7x 5.8x 6.0x
|
---|
136 | #
|
---|
137 | # Further data for other parallelizable modes:
|
---|
138 | #
|
---|
139 | # CBC decrypt 1.16 0.93 0.74
|
---|
140 | # CTR 1.14 0.91 0.74
|
---|
141 | #
|
---|
142 | # Well, given 3x column it's probably inappropriate to call the limit
|
---|
143 | # asymptotic, if it can be surpassed, isn't it? What happens there?
|
---|
144 | # Rewind to CBC paragraph for the answer. Yes, out-of-order execution
|
---|
145 | # magic is responsible for this. Processor overlaps not only the
|
---|
146 | # additional instructions with AES ones, but even AES instructions
|
---|
147 | # processing adjacent triplets of independent blocks. In the 6x case
|
---|
148 | # additional instructions still claim disproportionally small amount
|
---|
149 | # of additional cycles, but in 8x case number of instructions must be
|
---|
150 | # a tad too high for out-of-order logic to cope with, and AES unit
|
---|
151 | # remains underutilized... As you can see 8x interleave is hardly
|
---|
152 | # justifiable, so there no need to feel bad that 32-bit aesni-x86.pl
|
---|
153 | # utilizes 6x interleave because of limited register bank capacity.
|
---|
154 | #
|
---|
155 | # Higher interleave factors do have negative impact on Westmere
|
---|
156 | # performance. While for ECB mode it's negligible ~1.5%, other
|
---|
157 | # parallelizables perform ~5% worse, which is outweighed by ~25%
|
---|
158 | # improvement on Sandy Bridge. To balance regression on Westmere
|
---|
159 | # CTR mode was implemented with 6x aesenc interleave factor.
|
---|
160 |
|
---|
161 | # April 2011
|
---|
162 | #
|
---|
163 | # Add aesni_xts_[en|de]crypt. Westmere spends 1.25 cycles processing
|
---|
164 | # one byte out of 8KB with 128-bit key, Sandy Bridge - 0.90. Just like
|
---|
165 | # in CTR mode AES instruction interleave factor was chosen to be 6x.
|
---|
166 |
|
---|
167 | # November 2015
|
---|
168 | #
|
---|
169 | # Add aesni_ocb_[en|de]crypt. AES instruction interleave factor was
|
---|
170 | # chosen to be 6x.
|
---|
171 |
|
---|
172 | ######################################################################
|
---|
173 | # Current large-block performance in cycles per byte processed with
|
---|
174 | # 128-bit key (less is better).
|
---|
175 | #
|
---|
176 | # CBC en-/decrypt CTR XTS ECB OCB
|
---|
177 | # Westmere 3.77/1.25 1.25 1.25 1.26
|
---|
178 | # * Bridge 5.07/0.74 0.75 0.90 0.85 0.98
|
---|
179 | # Haswell 4.44/0.63 0.63 0.73 0.63 0.70
|
---|
180 | # Skylake 2.62/0.63 0.63 0.63 0.63
|
---|
181 | # Silvermont 5.75/3.54 3.56 4.12 3.87(*) 4.11
|
---|
182 | # Knights L 2.54/0.77 0.78 0.85 - 1.50
|
---|
183 | # Goldmont 3.82/1.26 1.26 1.29 1.29 1.50
|
---|
184 | # Bulldozer 5.77/0.70 0.72 0.90 0.70 0.95
|
---|
185 | # Ryzen 2.71/0.35 0.35 0.44 0.38 0.49
|
---|
186 | #
|
---|
187 | # (*) Atom Silvermont ECB result is suboptimal because of penalties
|
---|
188 | # incurred by operations on %xmm8-15. As ECB is not considered
|
---|
189 | # critical, nothing was done to mitigate the problem.
|
---|
190 |
|
---|
191 | $PREFIX="aesni"; # if $PREFIX is set to "AES", the script
|
---|
192 | # generates drop-in replacement for
|
---|
193 | # crypto/aes/asm/aes-x86_64.pl:-)
|
---|
194 |
|
---|
195 | # $output is the last argument if it looks like a file (it has an extension)
|
---|
196 | # $flavour is the first argument if it doesn't look like a file
|
---|
197 | $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
|
---|
198 | $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
|
---|
199 |
|
---|
200 | $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
|
---|
201 |
|
---|
202 | $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
|
---|
203 | ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
|
---|
204 | ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
|
---|
205 | die "can't locate x86_64-xlate.pl";
|
---|
206 |
|
---|
207 | open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
|
---|
208 | or die "can't call $xlate: $!";
|
---|
209 | *STDOUT=*OUT;
|
---|
210 |
|
---|
211 | $movkey = $PREFIX eq "aesni" ? "movups" : "movups";
|
---|
212 | @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
|
---|
213 | ("%rdi","%rsi","%rdx","%rcx"); # Unix order
|
---|
214 |
|
---|
215 | $code=".text\n";
|
---|
216 | $code.=".extern OPENSSL_ia32cap_P\n";
|
---|
217 |
|
---|
218 | $rounds="%eax"; # input to and changed by aesni_[en|de]cryptN !!!
|
---|
219 | # this is natural Unix argument order for public $PREFIX_[ecb|cbc]_encrypt ...
|
---|
220 | $inp="%rdi";
|
---|
221 | $out="%rsi";
|
---|
222 | $len="%rdx";
|
---|
223 | $key="%rcx"; # input to and changed by aesni_[en|de]cryptN !!!
|
---|
224 | $ivp="%r8"; # cbc, ctr, ...
|
---|
225 |
|
---|
226 | $rnds_="%r10d"; # backup copy for $rounds
|
---|
227 | $key_="%r11"; # backup copy for $key
|
---|
228 |
|
---|
229 | # %xmm register layout
|
---|
230 | $rndkey0="%xmm0"; $rndkey1="%xmm1";
|
---|
231 | $inout0="%xmm2"; $inout1="%xmm3";
|
---|
232 | $inout2="%xmm4"; $inout3="%xmm5";
|
---|
233 | $inout4="%xmm6"; $inout5="%xmm7";
|
---|
234 | $inout6="%xmm8"; $inout7="%xmm9";
|
---|
235 |
|
---|
236 | $in2="%xmm6"; $in1="%xmm7"; # used in CBC decrypt, CTR, ...
|
---|
237 | $in0="%xmm8"; $iv="%xmm9";
|
---|
238 | |
---|
239 |
|
---|
240 | # Inline version of internal aesni_[en|de]crypt1.
|
---|
241 | #
|
---|
242 | # Why folded loop? Because aes[enc|dec] is slow enough to accommodate
|
---|
243 | # cycles which take care of loop variables...
|
---|
244 | { my $sn;
|
---|
245 | sub aesni_generate1 {
|
---|
246 | my ($p,$key,$rounds,$inout,$ivec)=@_; $inout=$inout0 if (!defined($inout));
|
---|
247 | ++$sn;
|
---|
248 | $code.=<<___;
|
---|
249 | $movkey ($key),$rndkey0
|
---|
250 | $movkey 16($key),$rndkey1
|
---|
251 | ___
|
---|
252 | $code.=<<___ if (defined($ivec));
|
---|
253 | xorps $rndkey0,$ivec
|
---|
254 | lea 32($key),$key
|
---|
255 | xorps $ivec,$inout
|
---|
256 | ___
|
---|
257 | $code.=<<___ if (!defined($ivec));
|
---|
258 | lea 32($key),$key
|
---|
259 | xorps $rndkey0,$inout
|
---|
260 | ___
|
---|
261 | $code.=<<___;
|
---|
262 | .Loop_${p}1_$sn:
|
---|
263 | aes${p} $rndkey1,$inout
|
---|
264 | dec $rounds
|
---|
265 | $movkey ($key),$rndkey1
|
---|
266 | lea 16($key),$key
|
---|
267 | jnz .Loop_${p}1_$sn # loop body is 16 bytes
|
---|
268 | aes${p}last $rndkey1,$inout
|
---|
269 | ___
|
---|
270 | }}
|
---|
271 | # void $PREFIX_[en|de]crypt (const void *inp,void *out,const AES_KEY *key);
|
---|
272 | #
|
---|
273 | { my ($inp,$out,$key) = @_4args;
|
---|
274 |
|
---|
275 | $code.=<<___;
|
---|
276 | .globl ${PREFIX}_encrypt
|
---|
277 | .type ${PREFIX}_encrypt,\@abi-omnipotent
|
---|
278 | .align 16
|
---|
279 | ${PREFIX}_encrypt:
|
---|
280 | .cfi_startproc
|
---|
281 | endbranch
|
---|
282 | movups ($inp),$inout0 # load input
|
---|
283 | mov 240($key),$rounds # key->rounds
|
---|
284 | ___
|
---|
285 | &aesni_generate1("enc",$key,$rounds);
|
---|
286 | $code.=<<___;
|
---|
287 | pxor $rndkey0,$rndkey0 # clear register bank
|
---|
288 | pxor $rndkey1,$rndkey1
|
---|
289 | movups $inout0,($out) # output
|
---|
290 | pxor $inout0,$inout0
|
---|
291 | ret
|
---|
292 | .cfi_endproc
|
---|
293 | .size ${PREFIX}_encrypt,.-${PREFIX}_encrypt
|
---|
294 |
|
---|
295 | .globl ${PREFIX}_decrypt
|
---|
296 | .type ${PREFIX}_decrypt,\@abi-omnipotent
|
---|
297 | .align 16
|
---|
298 | ${PREFIX}_decrypt:
|
---|
299 | .cfi_startproc
|
---|
300 | endbranch
|
---|
301 | movups ($inp),$inout0 # load input
|
---|
302 | mov 240($key),$rounds # key->rounds
|
---|
303 | ___
|
---|
304 | &aesni_generate1("dec",$key,$rounds);
|
---|
305 | $code.=<<___;
|
---|
306 | pxor $rndkey0,$rndkey0 # clear register bank
|
---|
307 | pxor $rndkey1,$rndkey1
|
---|
308 | movups $inout0,($out) # output
|
---|
309 | pxor $inout0,$inout0
|
---|
310 | ret
|
---|
311 | .cfi_endproc
|
---|
312 | .size ${PREFIX}_decrypt, .-${PREFIX}_decrypt
|
---|
313 | ___
|
---|
314 | }
|
---|
315 | |
---|
316 |
|
---|
317 | # _aesni_[en|de]cryptN are private interfaces, N denotes interleave
|
---|
318 | # factor. Why 3x subroutine were originally used in loops? Even though
|
---|
319 | # aes[enc|dec] latency was originally 6, it could be scheduled only
|
---|
320 | # every *2nd* cycle. Thus 3x interleave was the one providing optimal
|
---|
321 | # utilization, i.e. when subroutine's throughput is virtually same as
|
---|
322 | # of non-interleaved subroutine [for number of input blocks up to 3].
|
---|
323 | # This is why it originally made no sense to implement 2x subroutine.
|
---|
324 | # But times change and it became appropriate to spend extra 192 bytes
|
---|
325 | # on 2x subroutine on Atom Silvermont account. For processors that
|
---|
326 | # can schedule aes[enc|dec] every cycle optimal interleave factor
|
---|
327 | # equals to corresponding instructions latency. 8x is optimal for
|
---|
328 | # * Bridge and "super-optimal" for other Intel CPUs...
|
---|
329 |
|
---|
330 | sub aesni_generate2 {
|
---|
331 | my $dir=shift;
|
---|
332 | # As already mentioned it takes in $key and $rounds, which are *not*
|
---|
333 | # preserved. $inout[0-1] is cipher/clear text...
|
---|
334 | $code.=<<___;
|
---|
335 | .type _aesni_${dir}rypt2,\@abi-omnipotent
|
---|
336 | .align 16
|
---|
337 | _aesni_${dir}rypt2:
|
---|
338 | .cfi_startproc
|
---|
339 | $movkey ($key),$rndkey0
|
---|
340 | shl \$4,$rounds
|
---|
341 | $movkey 16($key),$rndkey1
|
---|
342 | xorps $rndkey0,$inout0
|
---|
343 | xorps $rndkey0,$inout1
|
---|
344 | $movkey 32($key),$rndkey0
|
---|
345 | lea 32($key,$rounds),$key
|
---|
346 | neg %rax # $rounds
|
---|
347 | add \$16,%rax
|
---|
348 |
|
---|
349 | .L${dir}_loop2:
|
---|
350 | aes${dir} $rndkey1,$inout0
|
---|
351 | aes${dir} $rndkey1,$inout1
|
---|
352 | $movkey ($key,%rax),$rndkey1
|
---|
353 | add \$32,%rax
|
---|
354 | aes${dir} $rndkey0,$inout0
|
---|
355 | aes${dir} $rndkey0,$inout1
|
---|
356 | $movkey -16($key,%rax),$rndkey0
|
---|
357 | jnz .L${dir}_loop2
|
---|
358 |
|
---|
359 | aes${dir} $rndkey1,$inout0
|
---|
360 | aes${dir} $rndkey1,$inout1
|
---|
361 | aes${dir}last $rndkey0,$inout0
|
---|
362 | aes${dir}last $rndkey0,$inout1
|
---|
363 | ret
|
---|
364 | .cfi_endproc
|
---|
365 | .size _aesni_${dir}rypt2,.-_aesni_${dir}rypt2
|
---|
366 | ___
|
---|
367 | }
|
---|
368 | sub aesni_generate3 {
|
---|
369 | my $dir=shift;
|
---|
370 | # As already mentioned it takes in $key and $rounds, which are *not*
|
---|
371 | # preserved. $inout[0-2] is cipher/clear text...
|
---|
372 | $code.=<<___;
|
---|
373 | .type _aesni_${dir}rypt3,\@abi-omnipotent
|
---|
374 | .align 16
|
---|
375 | _aesni_${dir}rypt3:
|
---|
376 | .cfi_startproc
|
---|
377 | $movkey ($key),$rndkey0
|
---|
378 | shl \$4,$rounds
|
---|
379 | $movkey 16($key),$rndkey1
|
---|
380 | xorps $rndkey0,$inout0
|
---|
381 | xorps $rndkey0,$inout1
|
---|
382 | xorps $rndkey0,$inout2
|
---|
383 | $movkey 32($key),$rndkey0
|
---|
384 | lea 32($key,$rounds),$key
|
---|
385 | neg %rax # $rounds
|
---|
386 | add \$16,%rax
|
---|
387 |
|
---|
388 | .L${dir}_loop3:
|
---|
389 | aes${dir} $rndkey1,$inout0
|
---|
390 | aes${dir} $rndkey1,$inout1
|
---|
391 | aes${dir} $rndkey1,$inout2
|
---|
392 | $movkey ($key,%rax),$rndkey1
|
---|
393 | add \$32,%rax
|
---|
394 | aes${dir} $rndkey0,$inout0
|
---|
395 | aes${dir} $rndkey0,$inout1
|
---|
396 | aes${dir} $rndkey0,$inout2
|
---|
397 | $movkey -16($key,%rax),$rndkey0
|
---|
398 | jnz .L${dir}_loop3
|
---|
399 |
|
---|
400 | aes${dir} $rndkey1,$inout0
|
---|
401 | aes${dir} $rndkey1,$inout1
|
---|
402 | aes${dir} $rndkey1,$inout2
|
---|
403 | aes${dir}last $rndkey0,$inout0
|
---|
404 | aes${dir}last $rndkey0,$inout1
|
---|
405 | aes${dir}last $rndkey0,$inout2
|
---|
406 | ret
|
---|
407 | .cfi_endproc
|
---|
408 | .size _aesni_${dir}rypt3,.-_aesni_${dir}rypt3
|
---|
409 | ___
|
---|
410 | }
|
---|
411 | # 4x interleave is implemented to improve small block performance,
|
---|
412 | # most notably [and naturally] 4 block by ~30%. One can argue that one
|
---|
413 | # should have implemented 5x as well, but improvement would be <20%,
|
---|
414 | # so it's not worth it...
|
---|
415 | sub aesni_generate4 {
|
---|
416 | my $dir=shift;
|
---|
417 | # As already mentioned it takes in $key and $rounds, which are *not*
|
---|
418 | # preserved. $inout[0-3] is cipher/clear text...
|
---|
419 | $code.=<<___;
|
---|
420 | .type _aesni_${dir}rypt4,\@abi-omnipotent
|
---|
421 | .align 16
|
---|
422 | _aesni_${dir}rypt4:
|
---|
423 | .cfi_startproc
|
---|
424 | $movkey ($key),$rndkey0
|
---|
425 | shl \$4,$rounds
|
---|
426 | $movkey 16($key),$rndkey1
|
---|
427 | xorps $rndkey0,$inout0
|
---|
428 | xorps $rndkey0,$inout1
|
---|
429 | xorps $rndkey0,$inout2
|
---|
430 | xorps $rndkey0,$inout3
|
---|
431 | $movkey 32($key),$rndkey0
|
---|
432 | lea 32($key,$rounds),$key
|
---|
433 | neg %rax # $rounds
|
---|
434 | .byte 0x0f,0x1f,0x00
|
---|
435 | add \$16,%rax
|
---|
436 |
|
---|
437 | .L${dir}_loop4:
|
---|
438 | aes${dir} $rndkey1,$inout0
|
---|
439 | aes${dir} $rndkey1,$inout1
|
---|
440 | aes${dir} $rndkey1,$inout2
|
---|
441 | aes${dir} $rndkey1,$inout3
|
---|
442 | $movkey ($key,%rax),$rndkey1
|
---|
443 | add \$32,%rax
|
---|
444 | aes${dir} $rndkey0,$inout0
|
---|
445 | aes${dir} $rndkey0,$inout1
|
---|
446 | aes${dir} $rndkey0,$inout2
|
---|
447 | aes${dir} $rndkey0,$inout3
|
---|
448 | $movkey -16($key,%rax),$rndkey0
|
---|
449 | jnz .L${dir}_loop4
|
---|
450 |
|
---|
451 | aes${dir} $rndkey1,$inout0
|
---|
452 | aes${dir} $rndkey1,$inout1
|
---|
453 | aes${dir} $rndkey1,$inout2
|
---|
454 | aes${dir} $rndkey1,$inout3
|
---|
455 | aes${dir}last $rndkey0,$inout0
|
---|
456 | aes${dir}last $rndkey0,$inout1
|
---|
457 | aes${dir}last $rndkey0,$inout2
|
---|
458 | aes${dir}last $rndkey0,$inout3
|
---|
459 | ret
|
---|
460 | .cfi_endproc
|
---|
461 | .size _aesni_${dir}rypt4,.-_aesni_${dir}rypt4
|
---|
462 | ___
|
---|
463 | }
|
---|
464 | sub aesni_generate6 {
|
---|
465 | my $dir=shift;
|
---|
466 | # As already mentioned it takes in $key and $rounds, which are *not*
|
---|
467 | # preserved. $inout[0-5] is cipher/clear text...
|
---|
468 | $code.=<<___;
|
---|
469 | .type _aesni_${dir}rypt6,\@abi-omnipotent
|
---|
470 | .align 16
|
---|
471 | _aesni_${dir}rypt6:
|
---|
472 | .cfi_startproc
|
---|
473 | $movkey ($key),$rndkey0
|
---|
474 | shl \$4,$rounds
|
---|
475 | $movkey 16($key),$rndkey1
|
---|
476 | xorps $rndkey0,$inout0
|
---|
477 | pxor $rndkey0,$inout1
|
---|
478 | pxor $rndkey0,$inout2
|
---|
479 | aes${dir} $rndkey1,$inout0
|
---|
480 | lea 32($key,$rounds),$key
|
---|
481 | neg %rax # $rounds
|
---|
482 | aes${dir} $rndkey1,$inout1
|
---|
483 | pxor $rndkey0,$inout3
|
---|
484 | pxor $rndkey0,$inout4
|
---|
485 | aes${dir} $rndkey1,$inout2
|
---|
486 | pxor $rndkey0,$inout5
|
---|
487 | $movkey ($key,%rax),$rndkey0
|
---|
488 | add \$16,%rax
|
---|
489 | jmp .L${dir}_loop6_enter
|
---|
490 | .align 16
|
---|
491 | .L${dir}_loop6:
|
---|
492 | aes${dir} $rndkey1,$inout0
|
---|
493 | aes${dir} $rndkey1,$inout1
|
---|
494 | aes${dir} $rndkey1,$inout2
|
---|
495 | .L${dir}_loop6_enter:
|
---|
496 | aes${dir} $rndkey1,$inout3
|
---|
497 | aes${dir} $rndkey1,$inout4
|
---|
498 | aes${dir} $rndkey1,$inout5
|
---|
499 | $movkey ($key,%rax),$rndkey1
|
---|
500 | add \$32,%rax
|
---|
501 | aes${dir} $rndkey0,$inout0
|
---|
502 | aes${dir} $rndkey0,$inout1
|
---|
503 | aes${dir} $rndkey0,$inout2
|
---|
504 | aes${dir} $rndkey0,$inout3
|
---|
505 | aes${dir} $rndkey0,$inout4
|
---|
506 | aes${dir} $rndkey0,$inout5
|
---|
507 | $movkey -16($key,%rax),$rndkey0
|
---|
508 | jnz .L${dir}_loop6
|
---|
509 |
|
---|
510 | aes${dir} $rndkey1,$inout0
|
---|
511 | aes${dir} $rndkey1,$inout1
|
---|
512 | aes${dir} $rndkey1,$inout2
|
---|
513 | aes${dir} $rndkey1,$inout3
|
---|
514 | aes${dir} $rndkey1,$inout4
|
---|
515 | aes${dir} $rndkey1,$inout5
|
---|
516 | aes${dir}last $rndkey0,$inout0
|
---|
517 | aes${dir}last $rndkey0,$inout1
|
---|
518 | aes${dir}last $rndkey0,$inout2
|
---|
519 | aes${dir}last $rndkey0,$inout3
|
---|
520 | aes${dir}last $rndkey0,$inout4
|
---|
521 | aes${dir}last $rndkey0,$inout5
|
---|
522 | ret
|
---|
523 | .cfi_endproc
|
---|
524 | .size _aesni_${dir}rypt6,.-_aesni_${dir}rypt6
|
---|
525 | ___
|
---|
526 | }
|
---|
527 | sub aesni_generate8 {
|
---|
528 | my $dir=shift;
|
---|
529 | # As already mentioned it takes in $key and $rounds, which are *not*
|
---|
530 | # preserved. $inout[0-7] is cipher/clear text...
|
---|
531 | $code.=<<___;
|
---|
532 | .type _aesni_${dir}rypt8,\@abi-omnipotent
|
---|
533 | .align 16
|
---|
534 | _aesni_${dir}rypt8:
|
---|
535 | .cfi_startproc
|
---|
536 | $movkey ($key),$rndkey0
|
---|
537 | shl \$4,$rounds
|
---|
538 | $movkey 16($key),$rndkey1
|
---|
539 | xorps $rndkey0,$inout0
|
---|
540 | xorps $rndkey0,$inout1
|
---|
541 | pxor $rndkey0,$inout2
|
---|
542 | pxor $rndkey0,$inout3
|
---|
543 | pxor $rndkey0,$inout4
|
---|
544 | lea 32($key,$rounds),$key
|
---|
545 | neg %rax # $rounds
|
---|
546 | aes${dir} $rndkey1,$inout0
|
---|
547 | pxor $rndkey0,$inout5
|
---|
548 | pxor $rndkey0,$inout6
|
---|
549 | aes${dir} $rndkey1,$inout1
|
---|
550 | pxor $rndkey0,$inout7
|
---|
551 | $movkey ($key,%rax),$rndkey0
|
---|
552 | add \$16,%rax
|
---|
553 | jmp .L${dir}_loop8_inner
|
---|
554 | .align 16
|
---|
555 | .L${dir}_loop8:
|
---|
556 | aes${dir} $rndkey1,$inout0
|
---|
557 | aes${dir} $rndkey1,$inout1
|
---|
558 | .L${dir}_loop8_inner:
|
---|
559 | aes${dir} $rndkey1,$inout2
|
---|
560 | aes${dir} $rndkey1,$inout3
|
---|
561 | aes${dir} $rndkey1,$inout4
|
---|
562 | aes${dir} $rndkey1,$inout5
|
---|
563 | aes${dir} $rndkey1,$inout6
|
---|
564 | aes${dir} $rndkey1,$inout7
|
---|
565 | .L${dir}_loop8_enter:
|
---|
566 | $movkey ($key,%rax),$rndkey1
|
---|
567 | add \$32,%rax
|
---|
568 | aes${dir} $rndkey0,$inout0
|
---|
569 | aes${dir} $rndkey0,$inout1
|
---|
570 | aes${dir} $rndkey0,$inout2
|
---|
571 | aes${dir} $rndkey0,$inout3
|
---|
572 | aes${dir} $rndkey0,$inout4
|
---|
573 | aes${dir} $rndkey0,$inout5
|
---|
574 | aes${dir} $rndkey0,$inout6
|
---|
575 | aes${dir} $rndkey0,$inout7
|
---|
576 | $movkey -16($key,%rax),$rndkey0
|
---|
577 | jnz .L${dir}_loop8
|
---|
578 |
|
---|
579 | aes${dir} $rndkey1,$inout0
|
---|
580 | aes${dir} $rndkey1,$inout1
|
---|
581 | aes${dir} $rndkey1,$inout2
|
---|
582 | aes${dir} $rndkey1,$inout3
|
---|
583 | aes${dir} $rndkey1,$inout4
|
---|
584 | aes${dir} $rndkey1,$inout5
|
---|
585 | aes${dir} $rndkey1,$inout6
|
---|
586 | aes${dir} $rndkey1,$inout7
|
---|
587 | aes${dir}last $rndkey0,$inout0
|
---|
588 | aes${dir}last $rndkey0,$inout1
|
---|
589 | aes${dir}last $rndkey0,$inout2
|
---|
590 | aes${dir}last $rndkey0,$inout3
|
---|
591 | aes${dir}last $rndkey0,$inout4
|
---|
592 | aes${dir}last $rndkey0,$inout5
|
---|
593 | aes${dir}last $rndkey0,$inout6
|
---|
594 | aes${dir}last $rndkey0,$inout7
|
---|
595 | ret
|
---|
596 | .cfi_endproc
|
---|
597 | .size _aesni_${dir}rypt8,.-_aesni_${dir}rypt8
|
---|
598 | ___
|
---|
599 | }
|
---|
600 | &aesni_generate2("enc") if ($PREFIX eq "aesni");
|
---|
601 | &aesni_generate2("dec");
|
---|
602 | &aesni_generate3("enc") if ($PREFIX eq "aesni");
|
---|
603 | &aesni_generate3("dec");
|
---|
604 | &aesni_generate4("enc") if ($PREFIX eq "aesni");
|
---|
605 | &aesni_generate4("dec");
|
---|
606 | &aesni_generate6("enc") if ($PREFIX eq "aesni");
|
---|
607 | &aesni_generate6("dec");
|
---|
608 | &aesni_generate8("enc") if ($PREFIX eq "aesni");
|
---|
609 | &aesni_generate8("dec");
|
---|
610 | |
---|
611 |
|
---|
612 | if ($PREFIX eq "aesni") {
|
---|
613 | ########################################################################
|
---|
614 | # void aesni_ecb_encrypt (const void *in, void *out,
|
---|
615 | # size_t length, const AES_KEY *key,
|
---|
616 | # int enc);
|
---|
617 | $code.=<<___;
|
---|
618 | .globl aesni_ecb_encrypt
|
---|
619 | .type aesni_ecb_encrypt,\@function,5
|
---|
620 | .align 16
|
---|
621 | aesni_ecb_encrypt:
|
---|
622 | .cfi_startproc
|
---|
623 | endbranch
|
---|
624 | ___
|
---|
625 | $code.=<<___ if ($win64);
|
---|
626 | lea -0x58(%rsp),%rsp
|
---|
627 | movaps %xmm6,(%rsp) # offload $inout4..7
|
---|
628 | movaps %xmm7,0x10(%rsp)
|
---|
629 | movaps %xmm8,0x20(%rsp)
|
---|
630 | movaps %xmm9,0x30(%rsp)
|
---|
631 | .Lecb_enc_body:
|
---|
632 | ___
|
---|
633 | $code.=<<___;
|
---|
634 | and \$-16,$len # if ($len<16)
|
---|
635 | jz .Lecb_ret # return
|
---|
636 |
|
---|
637 | mov 240($key),$rounds # key->rounds
|
---|
638 | $movkey ($key),$rndkey0
|
---|
639 | mov $key,$key_ # backup $key
|
---|
640 | mov $rounds,$rnds_ # backup $rounds
|
---|
641 | test %r8d,%r8d # 5th argument
|
---|
642 | jz .Lecb_decrypt
|
---|
643 | #--------------------------- ECB ENCRYPT ------------------------------#
|
---|
644 | cmp \$0x80,$len # if ($len<8*16)
|
---|
645 | jb .Lecb_enc_tail # short input
|
---|
646 |
|
---|
647 | movdqu ($inp),$inout0 # load 8 input blocks
|
---|
648 | movdqu 0x10($inp),$inout1
|
---|
649 | movdqu 0x20($inp),$inout2
|
---|
650 | movdqu 0x30($inp),$inout3
|
---|
651 | movdqu 0x40($inp),$inout4
|
---|
652 | movdqu 0x50($inp),$inout5
|
---|
653 | movdqu 0x60($inp),$inout6
|
---|
654 | movdqu 0x70($inp),$inout7
|
---|
655 | lea 0x80($inp),$inp # $inp+=8*16
|
---|
656 | sub \$0x80,$len # $len-=8*16 (can be zero)
|
---|
657 | jmp .Lecb_enc_loop8_enter
|
---|
658 | .align 16
|
---|
659 | .Lecb_enc_loop8:
|
---|
660 | movups $inout0,($out) # store 8 output blocks
|
---|
661 | mov $key_,$key # restore $key
|
---|
662 | movdqu ($inp),$inout0 # load 8 input blocks
|
---|
663 | mov $rnds_,$rounds # restore $rounds
|
---|
664 | movups $inout1,0x10($out)
|
---|
665 | movdqu 0x10($inp),$inout1
|
---|
666 | movups $inout2,0x20($out)
|
---|
667 | movdqu 0x20($inp),$inout2
|
---|
668 | movups $inout3,0x30($out)
|
---|
669 | movdqu 0x30($inp),$inout3
|
---|
670 | movups $inout4,0x40($out)
|
---|
671 | movdqu 0x40($inp),$inout4
|
---|
672 | movups $inout5,0x50($out)
|
---|
673 | movdqu 0x50($inp),$inout5
|
---|
674 | movups $inout6,0x60($out)
|
---|
675 | movdqu 0x60($inp),$inout6
|
---|
676 | movups $inout7,0x70($out)
|
---|
677 | lea 0x80($out),$out # $out+=8*16
|
---|
678 | movdqu 0x70($inp),$inout7
|
---|
679 | lea 0x80($inp),$inp # $inp+=8*16
|
---|
680 | .Lecb_enc_loop8_enter:
|
---|
681 |
|
---|
682 | call _aesni_encrypt8
|
---|
683 |
|
---|
684 | sub \$0x80,$len
|
---|
685 | jnc .Lecb_enc_loop8 # loop if $len-=8*16 didn't borrow
|
---|
686 |
|
---|
687 | movups $inout0,($out) # store 8 output blocks
|
---|
688 | mov $key_,$key # restore $key
|
---|
689 | movups $inout1,0x10($out)
|
---|
690 | mov $rnds_,$rounds # restore $rounds
|
---|
691 | movups $inout2,0x20($out)
|
---|
692 | movups $inout3,0x30($out)
|
---|
693 | movups $inout4,0x40($out)
|
---|
694 | movups $inout5,0x50($out)
|
---|
695 | movups $inout6,0x60($out)
|
---|
696 | movups $inout7,0x70($out)
|
---|
697 | lea 0x80($out),$out # $out+=8*16
|
---|
698 | add \$0x80,$len # restore real remaining $len
|
---|
699 | jz .Lecb_ret # done if ($len==0)
|
---|
700 |
|
---|
701 | .Lecb_enc_tail: # $len is less than 8*16
|
---|
702 | movups ($inp),$inout0
|
---|
703 | cmp \$0x20,$len
|
---|
704 | jb .Lecb_enc_one
|
---|
705 | movups 0x10($inp),$inout1
|
---|
706 | je .Lecb_enc_two
|
---|
707 | movups 0x20($inp),$inout2
|
---|
708 | cmp \$0x40,$len
|
---|
709 | jb .Lecb_enc_three
|
---|
710 | movups 0x30($inp),$inout3
|
---|
711 | je .Lecb_enc_four
|
---|
712 | movups 0x40($inp),$inout4
|
---|
713 | cmp \$0x60,$len
|
---|
714 | jb .Lecb_enc_five
|
---|
715 | movups 0x50($inp),$inout5
|
---|
716 | je .Lecb_enc_six
|
---|
717 | movdqu 0x60($inp),$inout6
|
---|
718 | xorps $inout7,$inout7
|
---|
719 | call _aesni_encrypt8
|
---|
720 | movups $inout0,($out) # store 7 output blocks
|
---|
721 | movups $inout1,0x10($out)
|
---|
722 | movups $inout2,0x20($out)
|
---|
723 | movups $inout3,0x30($out)
|
---|
724 | movups $inout4,0x40($out)
|
---|
725 | movups $inout5,0x50($out)
|
---|
726 | movups $inout6,0x60($out)
|
---|
727 | jmp .Lecb_ret
|
---|
728 | .align 16
|
---|
729 | .Lecb_enc_one:
|
---|
730 | ___
|
---|
731 | &aesni_generate1("enc",$key,$rounds);
|
---|
732 | $code.=<<___;
|
---|
733 | movups $inout0,($out) # store one output block
|
---|
734 | jmp .Lecb_ret
|
---|
735 | .align 16
|
---|
736 | .Lecb_enc_two:
|
---|
737 | call _aesni_encrypt2
|
---|
738 | movups $inout0,($out) # store 2 output blocks
|
---|
739 | movups $inout1,0x10($out)
|
---|
740 | jmp .Lecb_ret
|
---|
741 | .align 16
|
---|
742 | .Lecb_enc_three:
|
---|
743 | call _aesni_encrypt3
|
---|
744 | movups $inout0,($out) # store 3 output blocks
|
---|
745 | movups $inout1,0x10($out)
|
---|
746 | movups $inout2,0x20($out)
|
---|
747 | jmp .Lecb_ret
|
---|
748 | .align 16
|
---|
749 | .Lecb_enc_four:
|
---|
750 | call _aesni_encrypt4
|
---|
751 | movups $inout0,($out) # store 4 output blocks
|
---|
752 | movups $inout1,0x10($out)
|
---|
753 | movups $inout2,0x20($out)
|
---|
754 | movups $inout3,0x30($out)
|
---|
755 | jmp .Lecb_ret
|
---|
756 | .align 16
|
---|
757 | .Lecb_enc_five:
|
---|
758 | xorps $inout5,$inout5
|
---|
759 | call _aesni_encrypt6
|
---|
760 | movups $inout0,($out) # store 5 output blocks
|
---|
761 | movups $inout1,0x10($out)
|
---|
762 | movups $inout2,0x20($out)
|
---|
763 | movups $inout3,0x30($out)
|
---|
764 | movups $inout4,0x40($out)
|
---|
765 | jmp .Lecb_ret
|
---|
766 | .align 16
|
---|
767 | .Lecb_enc_six:
|
---|
768 | call _aesni_encrypt6
|
---|
769 | movups $inout0,($out) # store 6 output blocks
|
---|
770 | movups $inout1,0x10($out)
|
---|
771 | movups $inout2,0x20($out)
|
---|
772 | movups $inout3,0x30($out)
|
---|
773 | movups $inout4,0x40($out)
|
---|
774 | movups $inout5,0x50($out)
|
---|
775 | jmp .Lecb_ret
|
---|
776 | |
---|
777 | #--------------------------- ECB DECRYPT ------------------------------#
|
---|
778 | .align 16
|
---|
779 | .Lecb_decrypt:
|
---|
780 | cmp \$0x80,$len # if ($len<8*16)
|
---|
781 | jb .Lecb_dec_tail # short input
|
---|
782 |
|
---|
783 | movdqu ($inp),$inout0 # load 8 input blocks
|
---|
784 | movdqu 0x10($inp),$inout1
|
---|
785 | movdqu 0x20($inp),$inout2
|
---|
786 | movdqu 0x30($inp),$inout3
|
---|
787 | movdqu 0x40($inp),$inout4
|
---|
788 | movdqu 0x50($inp),$inout5
|
---|
789 | movdqu 0x60($inp),$inout6
|
---|
790 | movdqu 0x70($inp),$inout7
|
---|
791 | lea 0x80($inp),$inp # $inp+=8*16
|
---|
792 | sub \$0x80,$len # $len-=8*16 (can be zero)
|
---|
793 | jmp .Lecb_dec_loop8_enter
|
---|
794 | .align 16
|
---|
795 | .Lecb_dec_loop8:
|
---|
796 | movups $inout0,($out) # store 8 output blocks
|
---|
797 | mov $key_,$key # restore $key
|
---|
798 | movdqu ($inp),$inout0 # load 8 input blocks
|
---|
799 | mov $rnds_,$rounds # restore $rounds
|
---|
800 | movups $inout1,0x10($out)
|
---|
801 | movdqu 0x10($inp),$inout1
|
---|
802 | movups $inout2,0x20($out)
|
---|
803 | movdqu 0x20($inp),$inout2
|
---|
804 | movups $inout3,0x30($out)
|
---|
805 | movdqu 0x30($inp),$inout3
|
---|
806 | movups $inout4,0x40($out)
|
---|
807 | movdqu 0x40($inp),$inout4
|
---|
808 | movups $inout5,0x50($out)
|
---|
809 | movdqu 0x50($inp),$inout5
|
---|
810 | movups $inout6,0x60($out)
|
---|
811 | movdqu 0x60($inp),$inout6
|
---|
812 | movups $inout7,0x70($out)
|
---|
813 | lea 0x80($out),$out # $out+=8*16
|
---|
814 | movdqu 0x70($inp),$inout7
|
---|
815 | lea 0x80($inp),$inp # $inp+=8*16
|
---|
816 | .Lecb_dec_loop8_enter:
|
---|
817 |
|
---|
818 | call _aesni_decrypt8
|
---|
819 |
|
---|
820 | $movkey ($key_),$rndkey0
|
---|
821 | sub \$0x80,$len
|
---|
822 | jnc .Lecb_dec_loop8 # loop if $len-=8*16 didn't borrow
|
---|
823 |
|
---|
824 | movups $inout0,($out) # store 8 output blocks
|
---|
825 | pxor $inout0,$inout0 # clear register bank
|
---|
826 | mov $key_,$key # restore $key
|
---|
827 | movups $inout1,0x10($out)
|
---|
828 | pxor $inout1,$inout1
|
---|
829 | mov $rnds_,$rounds # restore $rounds
|
---|
830 | movups $inout2,0x20($out)
|
---|
831 | pxor $inout2,$inout2
|
---|
832 | movups $inout3,0x30($out)
|
---|
833 | pxor $inout3,$inout3
|
---|
834 | movups $inout4,0x40($out)
|
---|
835 | pxor $inout4,$inout4
|
---|
836 | movups $inout5,0x50($out)
|
---|
837 | pxor $inout5,$inout5
|
---|
838 | movups $inout6,0x60($out)
|
---|
839 | pxor $inout6,$inout6
|
---|
840 | movups $inout7,0x70($out)
|
---|
841 | pxor $inout7,$inout7
|
---|
842 | lea 0x80($out),$out # $out+=8*16
|
---|
843 | add \$0x80,$len # restore real remaining $len
|
---|
844 | jz .Lecb_ret # done if ($len==0)
|
---|
845 |
|
---|
846 | .Lecb_dec_tail:
|
---|
847 | movups ($inp),$inout0
|
---|
848 | cmp \$0x20,$len
|
---|
849 | jb .Lecb_dec_one
|
---|
850 | movups 0x10($inp),$inout1
|
---|
851 | je .Lecb_dec_two
|
---|
852 | movups 0x20($inp),$inout2
|
---|
853 | cmp \$0x40,$len
|
---|
854 | jb .Lecb_dec_three
|
---|
855 | movups 0x30($inp),$inout3
|
---|
856 | je .Lecb_dec_four
|
---|
857 | movups 0x40($inp),$inout4
|
---|
858 | cmp \$0x60,$len
|
---|
859 | jb .Lecb_dec_five
|
---|
860 | movups 0x50($inp),$inout5
|
---|
861 | je .Lecb_dec_six
|
---|
862 | movups 0x60($inp),$inout6
|
---|
863 | $movkey ($key),$rndkey0
|
---|
864 | xorps $inout7,$inout7
|
---|
865 | call _aesni_decrypt8
|
---|
866 | movups $inout0,($out) # store 7 output blocks
|
---|
867 | pxor $inout0,$inout0 # clear register bank
|
---|
868 | movups $inout1,0x10($out)
|
---|
869 | pxor $inout1,$inout1
|
---|
870 | movups $inout2,0x20($out)
|
---|
871 | pxor $inout2,$inout2
|
---|
872 | movups $inout3,0x30($out)
|
---|
873 | pxor $inout3,$inout3
|
---|
874 | movups $inout4,0x40($out)
|
---|
875 | pxor $inout4,$inout4
|
---|
876 | movups $inout5,0x50($out)
|
---|
877 | pxor $inout5,$inout5
|
---|
878 | movups $inout6,0x60($out)
|
---|
879 | pxor $inout6,$inout6
|
---|
880 | pxor $inout7,$inout7
|
---|
881 | jmp .Lecb_ret
|
---|
882 | .align 16
|
---|
883 | .Lecb_dec_one:
|
---|
884 | ___
|
---|
885 | &aesni_generate1("dec",$key,$rounds);
|
---|
886 | $code.=<<___;
|
---|
887 | movups $inout0,($out) # store one output block
|
---|
888 | pxor $inout0,$inout0 # clear register bank
|
---|
889 | jmp .Lecb_ret
|
---|
890 | .align 16
|
---|
891 | .Lecb_dec_two:
|
---|
892 | call _aesni_decrypt2
|
---|
893 | movups $inout0,($out) # store 2 output blocks
|
---|
894 | pxor $inout0,$inout0 # clear register bank
|
---|
895 | movups $inout1,0x10($out)
|
---|
896 | pxor $inout1,$inout1
|
---|
897 | jmp .Lecb_ret
|
---|
898 | .align 16
|
---|
899 | .Lecb_dec_three:
|
---|
900 | call _aesni_decrypt3
|
---|
901 | movups $inout0,($out) # store 3 output blocks
|
---|
902 | pxor $inout0,$inout0 # clear register bank
|
---|
903 | movups $inout1,0x10($out)
|
---|
904 | pxor $inout1,$inout1
|
---|
905 | movups $inout2,0x20($out)
|
---|
906 | pxor $inout2,$inout2
|
---|
907 | jmp .Lecb_ret
|
---|
908 | .align 16
|
---|
909 | .Lecb_dec_four:
|
---|
910 | call _aesni_decrypt4
|
---|
911 | movups $inout0,($out) # store 4 output blocks
|
---|
912 | pxor $inout0,$inout0 # clear register bank
|
---|
913 | movups $inout1,0x10($out)
|
---|
914 | pxor $inout1,$inout1
|
---|
915 | movups $inout2,0x20($out)
|
---|
916 | pxor $inout2,$inout2
|
---|
917 | movups $inout3,0x30($out)
|
---|
918 | pxor $inout3,$inout3
|
---|
919 | jmp .Lecb_ret
|
---|
920 | .align 16
|
---|
921 | .Lecb_dec_five:
|
---|
922 | xorps $inout5,$inout5
|
---|
923 | call _aesni_decrypt6
|
---|
924 | movups $inout0,($out) # store 5 output blocks
|
---|
925 | pxor $inout0,$inout0 # clear register bank
|
---|
926 | movups $inout1,0x10($out)
|
---|
927 | pxor $inout1,$inout1
|
---|
928 | movups $inout2,0x20($out)
|
---|
929 | pxor $inout2,$inout2
|
---|
930 | movups $inout3,0x30($out)
|
---|
931 | pxor $inout3,$inout3
|
---|
932 | movups $inout4,0x40($out)
|
---|
933 | pxor $inout4,$inout4
|
---|
934 | pxor $inout5,$inout5
|
---|
935 | jmp .Lecb_ret
|
---|
936 | .align 16
|
---|
937 | .Lecb_dec_six:
|
---|
938 | call _aesni_decrypt6
|
---|
939 | movups $inout0,($out) # store 6 output blocks
|
---|
940 | pxor $inout0,$inout0 # clear register bank
|
---|
941 | movups $inout1,0x10($out)
|
---|
942 | pxor $inout1,$inout1
|
---|
943 | movups $inout2,0x20($out)
|
---|
944 | pxor $inout2,$inout2
|
---|
945 | movups $inout3,0x30($out)
|
---|
946 | pxor $inout3,$inout3
|
---|
947 | movups $inout4,0x40($out)
|
---|
948 | pxor $inout4,$inout4
|
---|
949 | movups $inout5,0x50($out)
|
---|
950 | pxor $inout5,$inout5
|
---|
951 |
|
---|
952 | .Lecb_ret:
|
---|
953 | xorps $rndkey0,$rndkey0 # %xmm0
|
---|
954 | pxor $rndkey1,$rndkey1
|
---|
955 | ___
|
---|
956 | $code.=<<___ if ($win64);
|
---|
957 | movaps (%rsp),%xmm6
|
---|
958 | movaps %xmm0,(%rsp) # clear stack
|
---|
959 | movaps 0x10(%rsp),%xmm7
|
---|
960 | movaps %xmm0,0x10(%rsp)
|
---|
961 | movaps 0x20(%rsp),%xmm8
|
---|
962 | movaps %xmm0,0x20(%rsp)
|
---|
963 | movaps 0x30(%rsp),%xmm9
|
---|
964 | movaps %xmm0,0x30(%rsp)
|
---|
965 | lea 0x58(%rsp),%rsp
|
---|
966 | .Lecb_enc_ret:
|
---|
967 | ___
|
---|
968 | $code.=<<___;
|
---|
969 | ret
|
---|
970 | .cfi_endproc
|
---|
971 | .size aesni_ecb_encrypt,.-aesni_ecb_encrypt
|
---|
972 | ___
|
---|
973 | |
---|
974 |
|
---|
975 | {
|
---|
976 | ######################################################################
|
---|
977 | # void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out,
|
---|
978 | # size_t blocks, const AES_KEY *key,
|
---|
979 | # const char *ivec,char *cmac);
|
---|
980 | #
|
---|
981 | # Handles only complete blocks, operates on 64-bit counter and
|
---|
982 | # does not update *ivec! Nor does it finalize CMAC value
|
---|
983 | # (see engine/eng_aesni.c for details)
|
---|
984 | #
|
---|
985 | {
|
---|
986 | my $cmac="%r9"; # 6th argument
|
---|
987 |
|
---|
988 | my $increment="%xmm9";
|
---|
989 | my $iv="%xmm6";
|
---|
990 | my $bswap_mask="%xmm7";
|
---|
991 |
|
---|
992 | $code.=<<___;
|
---|
993 | .globl aesni_ccm64_encrypt_blocks
|
---|
994 | .type aesni_ccm64_encrypt_blocks,\@function,6
|
---|
995 | .align 16
|
---|
996 | aesni_ccm64_encrypt_blocks:
|
---|
997 | .cfi_startproc
|
---|
998 | endbranch
|
---|
999 | ___
|
---|
1000 | $code.=<<___ if ($win64);
|
---|
1001 | lea -0x58(%rsp),%rsp
|
---|
1002 | movaps %xmm6,(%rsp) # $iv
|
---|
1003 | movaps %xmm7,0x10(%rsp) # $bswap_mask
|
---|
1004 | movaps %xmm8,0x20(%rsp) # $in0
|
---|
1005 | movaps %xmm9,0x30(%rsp) # $increment
|
---|
1006 | .Lccm64_enc_body:
|
---|
1007 | ___
|
---|
1008 | $code.=<<___;
|
---|
1009 | mov 240($key),$rounds # key->rounds
|
---|
1010 | movdqu ($ivp),$iv
|
---|
1011 | movdqa .Lincrement64(%rip),$increment
|
---|
1012 | movdqa .Lbswap_mask(%rip),$bswap_mask
|
---|
1013 |
|
---|
1014 | shl \$4,$rounds
|
---|
1015 | mov \$16,$rnds_
|
---|
1016 | lea 0($key),$key_
|
---|
1017 | movdqu ($cmac),$inout1
|
---|
1018 | movdqa $iv,$inout0
|
---|
1019 | lea 32($key,$rounds),$key # end of key schedule
|
---|
1020 | pshufb $bswap_mask,$iv
|
---|
1021 | sub %rax,%r10 # twisted $rounds
|
---|
1022 | jmp .Lccm64_enc_outer
|
---|
1023 | .align 16
|
---|
1024 | .Lccm64_enc_outer:
|
---|
1025 | $movkey ($key_),$rndkey0
|
---|
1026 | mov %r10,%rax
|
---|
1027 | movups ($inp),$in0 # load inp
|
---|
1028 |
|
---|
1029 | xorps $rndkey0,$inout0 # counter
|
---|
1030 | $movkey 16($key_),$rndkey1
|
---|
1031 | xorps $in0,$rndkey0
|
---|
1032 | xorps $rndkey0,$inout1 # cmac^=inp
|
---|
1033 | $movkey 32($key_),$rndkey0
|
---|
1034 |
|
---|
1035 | .Lccm64_enc2_loop:
|
---|
1036 | aesenc $rndkey1,$inout0
|
---|
1037 | aesenc $rndkey1,$inout1
|
---|
1038 | $movkey ($key,%rax),$rndkey1
|
---|
1039 | add \$32,%rax
|
---|
1040 | aesenc $rndkey0,$inout0
|
---|
1041 | aesenc $rndkey0,$inout1
|
---|
1042 | $movkey -16($key,%rax),$rndkey0
|
---|
1043 | jnz .Lccm64_enc2_loop
|
---|
1044 | aesenc $rndkey1,$inout0
|
---|
1045 | aesenc $rndkey1,$inout1
|
---|
1046 | paddq $increment,$iv
|
---|
1047 | dec $len # $len-- ($len is in blocks)
|
---|
1048 | aesenclast $rndkey0,$inout0
|
---|
1049 | aesenclast $rndkey0,$inout1
|
---|
1050 |
|
---|
1051 | lea 16($inp),$inp
|
---|
1052 | xorps $inout0,$in0 # inp ^= E(iv)
|
---|
1053 | movdqa $iv,$inout0
|
---|
1054 | movups $in0,($out) # save output
|
---|
1055 | pshufb $bswap_mask,$inout0
|
---|
1056 | lea 16($out),$out # $out+=16
|
---|
1057 | jnz .Lccm64_enc_outer # loop if ($len!=0)
|
---|
1058 |
|
---|
1059 | pxor $rndkey0,$rndkey0 # clear register bank
|
---|
1060 | pxor $rndkey1,$rndkey1
|
---|
1061 | pxor $inout0,$inout0
|
---|
1062 | movups $inout1,($cmac) # store resulting mac
|
---|
1063 | pxor $inout1,$inout1
|
---|
1064 | pxor $in0,$in0
|
---|
1065 | pxor $iv,$iv
|
---|
1066 | ___
|
---|
1067 | $code.=<<___ if ($win64);
|
---|
1068 | movaps (%rsp),%xmm6
|
---|
1069 | movaps %xmm0,(%rsp) # clear stack
|
---|
1070 | movaps 0x10(%rsp),%xmm7
|
---|
1071 | movaps %xmm0,0x10(%rsp)
|
---|
1072 | movaps 0x20(%rsp),%xmm8
|
---|
1073 | movaps %xmm0,0x20(%rsp)
|
---|
1074 | movaps 0x30(%rsp),%xmm9
|
---|
1075 | movaps %xmm0,0x30(%rsp)
|
---|
1076 | lea 0x58(%rsp),%rsp
|
---|
1077 | .Lccm64_enc_ret:
|
---|
1078 | ___
|
---|
1079 | $code.=<<___;
|
---|
1080 | ret
|
---|
1081 | .cfi_endproc
|
---|
1082 | .size aesni_ccm64_encrypt_blocks,.-aesni_ccm64_encrypt_blocks
|
---|
1083 | ___
|
---|
1084 | ######################################################################
|
---|
1085 | $code.=<<___;
|
---|
1086 | .globl aesni_ccm64_decrypt_blocks
|
---|
1087 | .type aesni_ccm64_decrypt_blocks,\@function,6
|
---|
1088 | .align 16
|
---|
1089 | aesni_ccm64_decrypt_blocks:
|
---|
1090 | .cfi_startproc
|
---|
1091 | endbranch
|
---|
1092 | ___
|
---|
1093 | $code.=<<___ if ($win64);
|
---|
1094 | lea -0x58(%rsp),%rsp
|
---|
1095 | movaps %xmm6,(%rsp) # $iv
|
---|
1096 | movaps %xmm7,0x10(%rsp) # $bswap_mask
|
---|
1097 | movaps %xmm8,0x20(%rsp) # $in8
|
---|
1098 | movaps %xmm9,0x30(%rsp) # $increment
|
---|
1099 | .Lccm64_dec_body:
|
---|
1100 | ___
|
---|
1101 | $code.=<<___;
|
---|
1102 | mov 240($key),$rounds # key->rounds
|
---|
1103 | movups ($ivp),$iv
|
---|
1104 | movdqu ($cmac),$inout1
|
---|
1105 | movdqa .Lincrement64(%rip),$increment
|
---|
1106 | movdqa .Lbswap_mask(%rip),$bswap_mask
|
---|
1107 |
|
---|
1108 | movaps $iv,$inout0
|
---|
1109 | mov $rounds,$rnds_
|
---|
1110 | mov $key,$key_
|
---|
1111 | pshufb $bswap_mask,$iv
|
---|
1112 | ___
|
---|
1113 | &aesni_generate1("enc",$key,$rounds);
|
---|
1114 | $code.=<<___;
|
---|
1115 | shl \$4,$rnds_
|
---|
1116 | mov \$16,$rounds
|
---|
1117 | movups ($inp),$in0 # load inp
|
---|
1118 | paddq $increment,$iv
|
---|
1119 | lea 16($inp),$inp # $inp+=16
|
---|
1120 | sub %r10,%rax # twisted $rounds
|
---|
1121 | lea 32($key_,$rnds_),$key # end of key schedule
|
---|
1122 | mov %rax,%r10
|
---|
1123 | jmp .Lccm64_dec_outer
|
---|
1124 | .align 16
|
---|
1125 | .Lccm64_dec_outer:
|
---|
1126 | xorps $inout0,$in0 # inp ^= E(iv)
|
---|
1127 | movdqa $iv,$inout0
|
---|
1128 | movups $in0,($out) # save output
|
---|
1129 | lea 16($out),$out # $out+=16
|
---|
1130 | pshufb $bswap_mask,$inout0
|
---|
1131 |
|
---|
1132 | sub \$1,$len # $len-- ($len is in blocks)
|
---|
1133 | jz .Lccm64_dec_break # if ($len==0) break
|
---|
1134 |
|
---|
1135 | $movkey ($key_),$rndkey0
|
---|
1136 | mov %r10,%rax
|
---|
1137 | $movkey 16($key_),$rndkey1
|
---|
1138 | xorps $rndkey0,$in0
|
---|
1139 | xorps $rndkey0,$inout0
|
---|
1140 | xorps $in0,$inout1 # cmac^=out
|
---|
1141 | $movkey 32($key_),$rndkey0
|
---|
1142 | jmp .Lccm64_dec2_loop
|
---|
1143 | .align 16
|
---|
1144 | .Lccm64_dec2_loop:
|
---|
1145 | aesenc $rndkey1,$inout0
|
---|
1146 | aesenc $rndkey1,$inout1
|
---|
1147 | $movkey ($key,%rax),$rndkey1
|
---|
1148 | add \$32,%rax
|
---|
1149 | aesenc $rndkey0,$inout0
|
---|
1150 | aesenc $rndkey0,$inout1
|
---|
1151 | $movkey -16($key,%rax),$rndkey0
|
---|
1152 | jnz .Lccm64_dec2_loop
|
---|
1153 | movups ($inp),$in0 # load input
|
---|
1154 | paddq $increment,$iv
|
---|
1155 | aesenc $rndkey1,$inout0
|
---|
1156 | aesenc $rndkey1,$inout1
|
---|
1157 | aesenclast $rndkey0,$inout0
|
---|
1158 | aesenclast $rndkey0,$inout1
|
---|
1159 | lea 16($inp),$inp # $inp+=16
|
---|
1160 | jmp .Lccm64_dec_outer
|
---|
1161 |
|
---|
1162 | .align 16
|
---|
1163 | .Lccm64_dec_break:
|
---|
1164 | #xorps $in0,$inout1 # cmac^=out
|
---|
1165 | mov 240($key_),$rounds
|
---|
1166 | ___
|
---|
1167 | &aesni_generate1("enc",$key_,$rounds,$inout1,$in0);
|
---|
1168 | $code.=<<___;
|
---|
1169 | pxor $rndkey0,$rndkey0 # clear register bank
|
---|
1170 | pxor $rndkey1,$rndkey1
|
---|
1171 | pxor $inout0,$inout0
|
---|
1172 | movups $inout1,($cmac) # store resulting mac
|
---|
1173 | pxor $inout1,$inout1
|
---|
1174 | pxor $in0,$in0
|
---|
1175 | pxor $iv,$iv
|
---|
1176 | ___
|
---|
1177 | $code.=<<___ if ($win64);
|
---|
1178 | movaps (%rsp),%xmm6
|
---|
1179 | movaps %xmm0,(%rsp) # clear stack
|
---|
1180 | movaps 0x10(%rsp),%xmm7
|
---|
1181 | movaps %xmm0,0x10(%rsp)
|
---|
1182 | movaps 0x20(%rsp),%xmm8
|
---|
1183 | movaps %xmm0,0x20(%rsp)
|
---|
1184 | movaps 0x30(%rsp),%xmm9
|
---|
1185 | movaps %xmm0,0x30(%rsp)
|
---|
1186 | lea 0x58(%rsp),%rsp
|
---|
1187 | .Lccm64_dec_ret:
|
---|
1188 | ___
|
---|
1189 | $code.=<<___;
|
---|
1190 | ret
|
---|
1191 | .cfi_endproc
|
---|
1192 | .size aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks
|
---|
1193 | ___
|
---|
1194 | } |
---|
1195 |
|
---|
1196 | ######################################################################
|
---|
1197 | # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
|
---|
1198 | # size_t blocks, const AES_KEY *key,
|
---|
1199 | # const char *ivec);
|
---|
1200 | #
|
---|
1201 | # Handles only complete blocks, operates on 32-bit counter and
|
---|
1202 | # does not update *ivec! (see crypto/modes/ctr128.c for details)
|
---|
1203 | #
|
---|
1204 | # Overhaul based on suggestions from Shay Gueron and Vlad Krasnov,
|
---|
1205 | # http://rt.openssl.org/Ticket/Display.html?id=3021&user=guest&pass=guest.
|
---|
1206 | # Keywords are full unroll and modulo-schedule counter calculations
|
---|
1207 | # with zero-round key xor.
|
---|
1208 | {
|
---|
1209 | my ($in0,$in1,$in2,$in3,$in4,$in5)=map("%xmm$_",(10..15));
|
---|
1210 | my ($key0,$ctr)=("%ebp","${ivp}d");
|
---|
1211 | my $frame_size = 0x80 + ($win64?160:0);
|
---|
1212 |
|
---|
1213 | $code.=<<___;
|
---|
1214 | .globl aesni_ctr32_encrypt_blocks
|
---|
1215 | .type aesni_ctr32_encrypt_blocks,\@function,5
|
---|
1216 | .align 16
|
---|
1217 | aesni_ctr32_encrypt_blocks:
|
---|
1218 | .cfi_startproc
|
---|
1219 | endbranch
|
---|
1220 | cmp \$1,$len
|
---|
1221 | jne .Lctr32_bulk
|
---|
1222 |
|
---|
1223 | # handle single block without allocating stack frame,
|
---|
1224 | # useful when handling edges
|
---|
1225 | movups ($ivp),$inout0
|
---|
1226 | movups ($inp),$inout1
|
---|
1227 | mov 240($key),%edx # key->rounds
|
---|
1228 | ___
|
---|
1229 | &aesni_generate1("enc",$key,"%edx");
|
---|
1230 | $code.=<<___;
|
---|
1231 | pxor $rndkey0,$rndkey0 # clear register bank
|
---|
1232 | pxor $rndkey1,$rndkey1
|
---|
1233 | xorps $inout1,$inout0
|
---|
1234 | pxor $inout1,$inout1
|
---|
1235 | movups $inout0,($out)
|
---|
1236 | xorps $inout0,$inout0
|
---|
1237 | jmp .Lctr32_epilogue
|
---|
1238 |
|
---|
1239 | .align 16
|
---|
1240 | .Lctr32_bulk:
|
---|
1241 | lea (%rsp),$key_ # use $key_ as frame pointer
|
---|
1242 | .cfi_def_cfa_register $key_
|
---|
1243 | push %rbp
|
---|
1244 | .cfi_push %rbp
|
---|
1245 | sub \$$frame_size,%rsp
|
---|
1246 | and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
|
---|
1247 | ___
|
---|
1248 | $code.=<<___ if ($win64);
|
---|
1249 | movaps %xmm6,-0xa8($key_) # offload everything
|
---|
1250 | movaps %xmm7,-0x98($key_)
|
---|
1251 | movaps %xmm8,-0x88($key_)
|
---|
1252 | movaps %xmm9,-0x78($key_)
|
---|
1253 | movaps %xmm10,-0x68($key_)
|
---|
1254 | movaps %xmm11,-0x58($key_)
|
---|
1255 | movaps %xmm12,-0x48($key_)
|
---|
1256 | movaps %xmm13,-0x38($key_)
|
---|
1257 | movaps %xmm14,-0x28($key_)
|
---|
1258 | movaps %xmm15,-0x18($key_)
|
---|
1259 | .Lctr32_body:
|
---|
1260 | ___
|
---|
1261 | $code.=<<___;
|
---|
1262 |
|
---|
1263 | # 8 16-byte words on top of stack are counter values
|
---|
1264 | # xor-ed with zero-round key
|
---|
1265 |
|
---|
1266 | movdqu ($ivp),$inout0
|
---|
1267 | movdqu ($key),$rndkey0
|
---|
1268 | mov 12($ivp),$ctr # counter LSB
|
---|
1269 | pxor $rndkey0,$inout0
|
---|
1270 | mov 12($key),$key0 # 0-round key LSB
|
---|
1271 | movdqa $inout0,0x00(%rsp) # populate counter block
|
---|
1272 | bswap $ctr
|
---|
1273 | movdqa $inout0,$inout1
|
---|
1274 | movdqa $inout0,$inout2
|
---|
1275 | movdqa $inout0,$inout3
|
---|
1276 | movdqa $inout0,0x40(%rsp)
|
---|
1277 | movdqa $inout0,0x50(%rsp)
|
---|
1278 | movdqa $inout0,0x60(%rsp)
|
---|
1279 | mov %rdx,%r10 # about to borrow %rdx
|
---|
1280 | movdqa $inout0,0x70(%rsp)
|
---|
1281 |
|
---|
1282 | lea 1($ctr),%rax
|
---|
1283 | lea 2($ctr),%rdx
|
---|
1284 | bswap %eax
|
---|
1285 | bswap %edx
|
---|
1286 | xor $key0,%eax
|
---|
1287 | xor $key0,%edx
|
---|
1288 | pinsrd \$3,%eax,$inout1
|
---|
1289 | lea 3($ctr),%rax
|
---|
1290 | movdqa $inout1,0x10(%rsp)
|
---|
1291 | pinsrd \$3,%edx,$inout2
|
---|
1292 | bswap %eax
|
---|
1293 | mov %r10,%rdx # restore %rdx
|
---|
1294 | lea 4($ctr),%r10
|
---|
1295 | movdqa $inout2,0x20(%rsp)
|
---|
1296 | xor $key0,%eax
|
---|
1297 | bswap %r10d
|
---|
1298 | pinsrd \$3,%eax,$inout3
|
---|
1299 | xor $key0,%r10d
|
---|
1300 | movdqa $inout3,0x30(%rsp)
|
---|
1301 | lea 5($ctr),%r9
|
---|
1302 | mov %r10d,0x40+12(%rsp)
|
---|
1303 | bswap %r9d
|
---|
1304 | lea 6($ctr),%r10
|
---|
1305 | mov 240($key),$rounds # key->rounds
|
---|
1306 | xor $key0,%r9d
|
---|
1307 | bswap %r10d
|
---|
1308 | mov %r9d,0x50+12(%rsp)
|
---|
1309 | xor $key0,%r10d
|
---|
1310 | lea 7($ctr),%r9
|
---|
1311 | mov %r10d,0x60+12(%rsp)
|
---|
1312 | bswap %r9d
|
---|
1313 | mov OPENSSL_ia32cap_P+4(%rip),%r10d
|
---|
1314 | xor $key0,%r9d
|
---|
1315 | and \$`1<<26|1<<22`,%r10d # isolate XSAVE+MOVBE
|
---|
1316 | mov %r9d,0x70+12(%rsp)
|
---|
1317 |
|
---|
1318 | $movkey 0x10($key),$rndkey1
|
---|
1319 |
|
---|
1320 | movdqa 0x40(%rsp),$inout4
|
---|
1321 | movdqa 0x50(%rsp),$inout5
|
---|
1322 |
|
---|
1323 | cmp \$8,$len # $len is in blocks
|
---|
1324 | jb .Lctr32_tail # short input if ($len<8)
|
---|
1325 |
|
---|
1326 | sub \$6,$len # $len is biased by -6
|
---|
1327 | cmp \$`1<<22`,%r10d # check for MOVBE without XSAVE
|
---|
1328 | je .Lctr32_6x # [which denotes Atom Silvermont]
|
---|
1329 |
|
---|
1330 | lea 0x80($key),$key # size optimization
|
---|
1331 | sub \$2,$len # $len is biased by -8
|
---|
1332 | jmp .Lctr32_loop8
|
---|
1333 |
|
---|
1334 | .align 16
|
---|
1335 | .Lctr32_6x:
|
---|
1336 | shl \$4,$rounds
|
---|
1337 | mov \$48,$rnds_
|
---|
1338 | bswap $key0
|
---|
1339 | lea 32($key,$rounds),$key # end of key schedule
|
---|
1340 | sub %rax,%r10 # twisted $rounds
|
---|
1341 | jmp .Lctr32_loop6
|
---|
1342 |
|
---|
1343 | .align 16
|
---|
1344 | .Lctr32_loop6:
|
---|
1345 | add \$6,$ctr # next counter value
|
---|
1346 | $movkey -48($key,$rnds_),$rndkey0
|
---|
1347 | aesenc $rndkey1,$inout0
|
---|
1348 | mov $ctr,%eax
|
---|
1349 | xor $key0,%eax
|
---|
1350 | aesenc $rndkey1,$inout1
|
---|
1351 | movbe %eax,`0x00+12`(%rsp) # store next counter value
|
---|
1352 | lea 1($ctr),%eax
|
---|
1353 | aesenc $rndkey1,$inout2
|
---|
1354 | xor $key0,%eax
|
---|
1355 | movbe %eax,`0x10+12`(%rsp)
|
---|
1356 | aesenc $rndkey1,$inout3
|
---|
1357 | lea 2($ctr),%eax
|
---|
1358 | xor $key0,%eax
|
---|
1359 | aesenc $rndkey1,$inout4
|
---|
1360 | movbe %eax,`0x20+12`(%rsp)
|
---|
1361 | lea 3($ctr),%eax
|
---|
1362 | aesenc $rndkey1,$inout5
|
---|
1363 | $movkey -32($key,$rnds_),$rndkey1
|
---|
1364 | xor $key0,%eax
|
---|
1365 |
|
---|
1366 | aesenc $rndkey0,$inout0
|
---|
1367 | movbe %eax,`0x30+12`(%rsp)
|
---|
1368 | lea 4($ctr),%eax
|
---|
1369 | aesenc $rndkey0,$inout1
|
---|
1370 | xor $key0,%eax
|
---|
1371 | movbe %eax,`0x40+12`(%rsp)
|
---|
1372 | aesenc $rndkey0,$inout2
|
---|
1373 | lea 5($ctr),%eax
|
---|
1374 | xor $key0,%eax
|
---|
1375 | aesenc $rndkey0,$inout3
|
---|
1376 | movbe %eax,`0x50+12`(%rsp)
|
---|
1377 | mov %r10,%rax # mov $rnds_,$rounds
|
---|
1378 | aesenc $rndkey0,$inout4
|
---|
1379 | aesenc $rndkey0,$inout5
|
---|
1380 | $movkey -16($key,$rnds_),$rndkey0
|
---|
1381 |
|
---|
1382 | call .Lenc_loop6
|
---|
1383 |
|
---|
1384 | movdqu ($inp),$inout6 # load 6 input blocks
|
---|
1385 | movdqu 0x10($inp),$inout7
|
---|
1386 | movdqu 0x20($inp),$in0
|
---|
1387 | movdqu 0x30($inp),$in1
|
---|
1388 | movdqu 0x40($inp),$in2
|
---|
1389 | movdqu 0x50($inp),$in3
|
---|
1390 | lea 0x60($inp),$inp # $inp+=6*16
|
---|
1391 | $movkey -64($key,$rnds_),$rndkey1
|
---|
1392 | pxor $inout0,$inout6 # inp^=E(ctr)
|
---|
1393 | movaps 0x00(%rsp),$inout0 # load next counter [xor-ed with 0 round]
|
---|
1394 | pxor $inout1,$inout7
|
---|
1395 | movaps 0x10(%rsp),$inout1
|
---|
1396 | pxor $inout2,$in0
|
---|
1397 | movaps 0x20(%rsp),$inout2
|
---|
1398 | pxor $inout3,$in1
|
---|
1399 | movaps 0x30(%rsp),$inout3
|
---|
1400 | pxor $inout4,$in2
|
---|
1401 | movaps 0x40(%rsp),$inout4
|
---|
1402 | pxor $inout5,$in3
|
---|
1403 | movaps 0x50(%rsp),$inout5
|
---|
1404 | movdqu $inout6,($out) # store 6 output blocks
|
---|
1405 | movdqu $inout7,0x10($out)
|
---|
1406 | movdqu $in0,0x20($out)
|
---|
1407 | movdqu $in1,0x30($out)
|
---|
1408 | movdqu $in2,0x40($out)
|
---|
1409 | movdqu $in3,0x50($out)
|
---|
1410 | lea 0x60($out),$out # $out+=6*16
|
---|
1411 |
|
---|
1412 | sub \$6,$len
|
---|
1413 | jnc .Lctr32_loop6 # loop if $len-=6 didn't borrow
|
---|
1414 |
|
---|
1415 | add \$6,$len # restore real remaining $len
|
---|
1416 | jz .Lctr32_done # done if ($len==0)
|
---|
1417 |
|
---|
1418 | lea -48($rnds_),$rounds
|
---|
1419 | lea -80($key,$rnds_),$key # restore $key
|
---|
1420 | neg $rounds
|
---|
1421 | shr \$4,$rounds # restore $rounds
|
---|
1422 | jmp .Lctr32_tail
|
---|
1423 |
|
---|
1424 | .align 32
|
---|
1425 | .Lctr32_loop8:
|
---|
1426 | add \$8,$ctr # next counter value
|
---|
1427 | movdqa 0x60(%rsp),$inout6
|
---|
1428 | aesenc $rndkey1,$inout0
|
---|
1429 | mov $ctr,%r9d
|
---|
1430 | movdqa 0x70(%rsp),$inout7
|
---|
1431 | aesenc $rndkey1,$inout1
|
---|
1432 | bswap %r9d
|
---|
1433 | $movkey 0x20-0x80($key),$rndkey0
|
---|
1434 | aesenc $rndkey1,$inout2
|
---|
1435 | xor $key0,%r9d
|
---|
1436 | nop
|
---|
1437 | aesenc $rndkey1,$inout3
|
---|
1438 | mov %r9d,0x00+12(%rsp) # store next counter value
|
---|
1439 | lea 1($ctr),%r9
|
---|
1440 | aesenc $rndkey1,$inout4
|
---|
1441 | aesenc $rndkey1,$inout5
|
---|
1442 | aesenc $rndkey1,$inout6
|
---|
1443 | aesenc $rndkey1,$inout7
|
---|
1444 | $movkey 0x30-0x80($key),$rndkey1
|
---|
1445 | ___
|
---|
1446 | for($i=2;$i<8;$i++) {
|
---|
1447 | my $rndkeyx = ($i&1)?$rndkey1:$rndkey0;
|
---|
1448 | $code.=<<___;
|
---|
1449 | bswap %r9d
|
---|
1450 | aesenc $rndkeyx,$inout0
|
---|
1451 | aesenc $rndkeyx,$inout1
|
---|
1452 | xor $key0,%r9d
|
---|
1453 | .byte 0x66,0x90
|
---|
1454 | aesenc $rndkeyx,$inout2
|
---|
1455 | aesenc $rndkeyx,$inout3
|
---|
1456 | mov %r9d,`0x10*($i-1)`+12(%rsp)
|
---|
1457 | lea $i($ctr),%r9
|
---|
1458 | aesenc $rndkeyx,$inout4
|
---|
1459 | aesenc $rndkeyx,$inout5
|
---|
1460 | aesenc $rndkeyx,$inout6
|
---|
1461 | aesenc $rndkeyx,$inout7
|
---|
1462 | $movkey `0x20+0x10*$i`-0x80($key),$rndkeyx
|
---|
1463 | ___
|
---|
1464 | }
|
---|
1465 | $code.=<<___;
|
---|
1466 | bswap %r9d
|
---|
1467 | aesenc $rndkey0,$inout0
|
---|
1468 | aesenc $rndkey0,$inout1
|
---|
1469 | aesenc $rndkey0,$inout2
|
---|
1470 | xor $key0,%r9d
|
---|
1471 | movdqu 0x00($inp),$in0 # start loading input
|
---|
1472 | aesenc $rndkey0,$inout3
|
---|
1473 | mov %r9d,0x70+12(%rsp)
|
---|
1474 | cmp \$11,$rounds
|
---|
1475 | aesenc $rndkey0,$inout4
|
---|
1476 | aesenc $rndkey0,$inout5
|
---|
1477 | aesenc $rndkey0,$inout6
|
---|
1478 | aesenc $rndkey0,$inout7
|
---|
1479 | $movkey 0xa0-0x80($key),$rndkey0
|
---|
1480 |
|
---|
1481 | jb .Lctr32_enc_done
|
---|
1482 |
|
---|
1483 | aesenc $rndkey1,$inout0
|
---|
1484 | aesenc $rndkey1,$inout1
|
---|
1485 | aesenc $rndkey1,$inout2
|
---|
1486 | aesenc $rndkey1,$inout3
|
---|
1487 | aesenc $rndkey1,$inout4
|
---|
1488 | aesenc $rndkey1,$inout5
|
---|
1489 | aesenc $rndkey1,$inout6
|
---|
1490 | aesenc $rndkey1,$inout7
|
---|
1491 | $movkey 0xb0-0x80($key),$rndkey1
|
---|
1492 |
|
---|
1493 | aesenc $rndkey0,$inout0
|
---|
1494 | aesenc $rndkey0,$inout1
|
---|
1495 | aesenc $rndkey0,$inout2
|
---|
1496 | aesenc $rndkey0,$inout3
|
---|
1497 | aesenc $rndkey0,$inout4
|
---|
1498 | aesenc $rndkey0,$inout5
|
---|
1499 | aesenc $rndkey0,$inout6
|
---|
1500 | aesenc $rndkey0,$inout7
|
---|
1501 | $movkey 0xc0-0x80($key),$rndkey0
|
---|
1502 | je .Lctr32_enc_done
|
---|
1503 |
|
---|
1504 | aesenc $rndkey1,$inout0
|
---|
1505 | aesenc $rndkey1,$inout1
|
---|
1506 | aesenc $rndkey1,$inout2
|
---|
1507 | aesenc $rndkey1,$inout3
|
---|
1508 | aesenc $rndkey1,$inout4
|
---|
1509 | aesenc $rndkey1,$inout5
|
---|
1510 | aesenc $rndkey1,$inout6
|
---|
1511 | aesenc $rndkey1,$inout7
|
---|
1512 | $movkey 0xd0-0x80($key),$rndkey1
|
---|
1513 |
|
---|
1514 | aesenc $rndkey0,$inout0
|
---|
1515 | aesenc $rndkey0,$inout1
|
---|
1516 | aesenc $rndkey0,$inout2
|
---|
1517 | aesenc $rndkey0,$inout3
|
---|
1518 | aesenc $rndkey0,$inout4
|
---|
1519 | aesenc $rndkey0,$inout5
|
---|
1520 | aesenc $rndkey0,$inout6
|
---|
1521 | aesenc $rndkey0,$inout7
|
---|
1522 | $movkey 0xe0-0x80($key),$rndkey0
|
---|
1523 | jmp .Lctr32_enc_done
|
---|
1524 |
|
---|
1525 | .align 16
|
---|
1526 | .Lctr32_enc_done:
|
---|
1527 | movdqu 0x10($inp),$in1
|
---|
1528 | pxor $rndkey0,$in0 # input^=round[last]
|
---|
1529 | movdqu 0x20($inp),$in2
|
---|
1530 | pxor $rndkey0,$in1
|
---|
1531 | movdqu 0x30($inp),$in3
|
---|
1532 | pxor $rndkey0,$in2
|
---|
1533 | movdqu 0x40($inp),$in4
|
---|
1534 | pxor $rndkey0,$in3
|
---|
1535 | movdqu 0x50($inp),$in5
|
---|
1536 | pxor $rndkey0,$in4
|
---|
1537 | pxor $rndkey0,$in5
|
---|
1538 | aesenc $rndkey1,$inout0
|
---|
1539 | aesenc $rndkey1,$inout1
|
---|
1540 | aesenc $rndkey1,$inout2
|
---|
1541 | aesenc $rndkey1,$inout3
|
---|
1542 | aesenc $rndkey1,$inout4
|
---|
1543 | aesenc $rndkey1,$inout5
|
---|
1544 | aesenc $rndkey1,$inout6
|
---|
1545 | aesenc $rndkey1,$inout7
|
---|
1546 | movdqu 0x60($inp),$rndkey1 # borrow $rndkey1 for inp[6]
|
---|
1547 | lea 0x80($inp),$inp # $inp+=8*16
|
---|
1548 |
|
---|
1549 | aesenclast $in0,$inout0 # $inN is inp[N]^round[last]
|
---|
1550 | pxor $rndkey0,$rndkey1 # borrowed $rndkey
|
---|
1551 | movdqu 0x70-0x80($inp),$in0
|
---|
1552 | aesenclast $in1,$inout1
|
---|
1553 | pxor $rndkey0,$in0
|
---|
1554 | movdqa 0x00(%rsp),$in1 # load next counter block
|
---|
1555 | aesenclast $in2,$inout2
|
---|
1556 | aesenclast $in3,$inout3
|
---|
1557 | movdqa 0x10(%rsp),$in2
|
---|
1558 | movdqa 0x20(%rsp),$in3
|
---|
1559 | aesenclast $in4,$inout4
|
---|
1560 | aesenclast $in5,$inout5
|
---|
1561 | movdqa 0x30(%rsp),$in4
|
---|
1562 | movdqa 0x40(%rsp),$in5
|
---|
1563 | aesenclast $rndkey1,$inout6
|
---|
1564 | movdqa 0x50(%rsp),$rndkey0
|
---|
1565 | $movkey 0x10-0x80($key),$rndkey1#real 1st-round key
|
---|
1566 | aesenclast $in0,$inout7
|
---|
1567 |
|
---|
1568 | movups $inout0,($out) # store 8 output blocks
|
---|
1569 | movdqa $in1,$inout0
|
---|
1570 | movups $inout1,0x10($out)
|
---|
1571 | movdqa $in2,$inout1
|
---|
1572 | movups $inout2,0x20($out)
|
---|
1573 | movdqa $in3,$inout2
|
---|
1574 | movups $inout3,0x30($out)
|
---|
1575 | movdqa $in4,$inout3
|
---|
1576 | movups $inout4,0x40($out)
|
---|
1577 | movdqa $in5,$inout4
|
---|
1578 | movups $inout5,0x50($out)
|
---|
1579 | movdqa $rndkey0,$inout5
|
---|
1580 | movups $inout6,0x60($out)
|
---|
1581 | movups $inout7,0x70($out)
|
---|
1582 | lea 0x80($out),$out # $out+=8*16
|
---|
1583 |
|
---|
1584 | sub \$8,$len
|
---|
1585 | jnc .Lctr32_loop8 # loop if $len-=8 didn't borrow
|
---|
1586 |
|
---|
1587 | add \$8,$len # restore real remaining $len
|
---|
1588 | jz .Lctr32_done # done if ($len==0)
|
---|
1589 | lea -0x80($key),$key
|
---|
1590 |
|
---|
1591 | .Lctr32_tail:
|
---|
1592 | # note that at this point $inout0..5 are populated with
|
---|
1593 | # counter values xor-ed with 0-round key
|
---|
1594 | lea 16($key),$key
|
---|
1595 | cmp \$4,$len
|
---|
1596 | jb .Lctr32_loop3
|
---|
1597 | je .Lctr32_loop4
|
---|
1598 |
|
---|
1599 | # if ($len>4) compute 7 E(counter)
|
---|
1600 | shl \$4,$rounds
|
---|
1601 | movdqa 0x60(%rsp),$inout6
|
---|
1602 | pxor $inout7,$inout7
|
---|
1603 |
|
---|
1604 | $movkey 16($key),$rndkey0
|
---|
1605 | aesenc $rndkey1,$inout0
|
---|
1606 | aesenc $rndkey1,$inout1
|
---|
1607 | lea 32-16($key,$rounds),$key# prepare for .Lenc_loop8_enter
|
---|
1608 | neg %rax
|
---|
1609 | aesenc $rndkey1,$inout2
|
---|
1610 | add \$16,%rax # prepare for .Lenc_loop8_enter
|
---|
1611 | movups ($inp),$in0
|
---|
1612 | aesenc $rndkey1,$inout3
|
---|
1613 | aesenc $rndkey1,$inout4
|
---|
1614 | movups 0x10($inp),$in1 # pre-load input
|
---|
1615 | movups 0x20($inp),$in2
|
---|
1616 | aesenc $rndkey1,$inout5
|
---|
1617 | aesenc $rndkey1,$inout6
|
---|
1618 |
|
---|
1619 | call .Lenc_loop8_enter
|
---|
1620 |
|
---|
1621 | movdqu 0x30($inp),$in3
|
---|
1622 | pxor $in0,$inout0
|
---|
1623 | movdqu 0x40($inp),$in0
|
---|
1624 | pxor $in1,$inout1
|
---|
1625 | movdqu $inout0,($out) # store output
|
---|
1626 | pxor $in2,$inout2
|
---|
1627 | movdqu $inout1,0x10($out)
|
---|
1628 | pxor $in3,$inout3
|
---|
1629 | movdqu $inout2,0x20($out)
|
---|
1630 | pxor $in0,$inout4
|
---|
1631 | movdqu $inout3,0x30($out)
|
---|
1632 | movdqu $inout4,0x40($out)
|
---|
1633 | cmp \$6,$len
|
---|
1634 | jb .Lctr32_done # $len was 5, stop store
|
---|
1635 |
|
---|
1636 | movups 0x50($inp),$in1
|
---|
1637 | xorps $in1,$inout5
|
---|
1638 | movups $inout5,0x50($out)
|
---|
1639 | je .Lctr32_done # $len was 6, stop store
|
---|
1640 |
|
---|
1641 | movups 0x60($inp),$in2
|
---|
1642 | xorps $in2,$inout6
|
---|
1643 | movups $inout6,0x60($out)
|
---|
1644 | jmp .Lctr32_done # $len was 7, stop store
|
---|
1645 |
|
---|
1646 | .align 32
|
---|
1647 | .Lctr32_loop4:
|
---|
1648 | aesenc $rndkey1,$inout0
|
---|
1649 | lea 16($key),$key
|
---|
1650 | dec $rounds
|
---|
1651 | aesenc $rndkey1,$inout1
|
---|
1652 | aesenc $rndkey1,$inout2
|
---|
1653 | aesenc $rndkey1,$inout3
|
---|
1654 | $movkey ($key),$rndkey1
|
---|
1655 | jnz .Lctr32_loop4
|
---|
1656 | aesenclast $rndkey1,$inout0
|
---|
1657 | aesenclast $rndkey1,$inout1
|
---|
1658 | movups ($inp),$in0 # load input
|
---|
1659 | movups 0x10($inp),$in1
|
---|
1660 | aesenclast $rndkey1,$inout2
|
---|
1661 | aesenclast $rndkey1,$inout3
|
---|
1662 | movups 0x20($inp),$in2
|
---|
1663 | movups 0x30($inp),$in3
|
---|
1664 |
|
---|
1665 | xorps $in0,$inout0
|
---|
1666 | movups $inout0,($out) # store output
|
---|
1667 | xorps $in1,$inout1
|
---|
1668 | movups $inout1,0x10($out)
|
---|
1669 | pxor $in2,$inout2
|
---|
1670 | movdqu $inout2,0x20($out)
|
---|
1671 | pxor $in3,$inout3
|
---|
1672 | movdqu $inout3,0x30($out)
|
---|
1673 | jmp .Lctr32_done # $len was 4, stop store
|
---|
1674 |
|
---|
1675 | .align 32
|
---|
1676 | .Lctr32_loop3:
|
---|
1677 | aesenc $rndkey1,$inout0
|
---|
1678 | lea 16($key),$key
|
---|
1679 | dec $rounds
|
---|
1680 | aesenc $rndkey1,$inout1
|
---|
1681 | aesenc $rndkey1,$inout2
|
---|
1682 | $movkey ($key),$rndkey1
|
---|
1683 | jnz .Lctr32_loop3
|
---|
1684 | aesenclast $rndkey1,$inout0
|
---|
1685 | aesenclast $rndkey1,$inout1
|
---|
1686 | aesenclast $rndkey1,$inout2
|
---|
1687 |
|
---|
1688 | movups ($inp),$in0 # load input
|
---|
1689 | xorps $in0,$inout0
|
---|
1690 | movups $inout0,($out) # store output
|
---|
1691 | cmp \$2,$len
|
---|
1692 | jb .Lctr32_done # $len was 1, stop store
|
---|
1693 |
|
---|
1694 | movups 0x10($inp),$in1
|
---|
1695 | xorps $in1,$inout1
|
---|
1696 | movups $inout1,0x10($out)
|
---|
1697 | je .Lctr32_done # $len was 2, stop store
|
---|
1698 |
|
---|
1699 | movups 0x20($inp),$in2
|
---|
1700 | xorps $in2,$inout2
|
---|
1701 | movups $inout2,0x20($out) # $len was 3, stop store
|
---|
1702 |
|
---|
1703 | .Lctr32_done:
|
---|
1704 | xorps %xmm0,%xmm0 # clear register bank
|
---|
1705 | xor $key0,$key0
|
---|
1706 | pxor %xmm1,%xmm1
|
---|
1707 | pxor %xmm2,%xmm2
|
---|
1708 | pxor %xmm3,%xmm3
|
---|
1709 | pxor %xmm4,%xmm4
|
---|
1710 | pxor %xmm5,%xmm5
|
---|
1711 | ___
|
---|
1712 | $code.=<<___ if (!$win64);
|
---|
1713 | pxor %xmm6,%xmm6
|
---|
1714 | pxor %xmm7,%xmm7
|
---|
1715 | movaps %xmm0,0x00(%rsp) # clear stack
|
---|
1716 | pxor %xmm8,%xmm8
|
---|
1717 | movaps %xmm0,0x10(%rsp)
|
---|
1718 | pxor %xmm9,%xmm9
|
---|
1719 | movaps %xmm0,0x20(%rsp)
|
---|
1720 | pxor %xmm10,%xmm10
|
---|
1721 | movaps %xmm0,0x30(%rsp)
|
---|
1722 | pxor %xmm11,%xmm11
|
---|
1723 | movaps %xmm0,0x40(%rsp)
|
---|
1724 | pxor %xmm12,%xmm12
|
---|
1725 | movaps %xmm0,0x50(%rsp)
|
---|
1726 | pxor %xmm13,%xmm13
|
---|
1727 | movaps %xmm0,0x60(%rsp)
|
---|
1728 | pxor %xmm14,%xmm14
|
---|
1729 | movaps %xmm0,0x70(%rsp)
|
---|
1730 | pxor %xmm15,%xmm15
|
---|
1731 | ___
|
---|
1732 | $code.=<<___ if ($win64);
|
---|
1733 | movaps -0xa8($key_),%xmm6
|
---|
1734 | movaps %xmm0,-0xa8($key_) # clear stack
|
---|
1735 | movaps -0x98($key_),%xmm7
|
---|
1736 | movaps %xmm0,-0x98($key_)
|
---|
1737 | movaps -0x88($key_),%xmm8
|
---|
1738 | movaps %xmm0,-0x88($key_)
|
---|
1739 | movaps -0x78($key_),%xmm9
|
---|
1740 | movaps %xmm0,-0x78($key_)
|
---|
1741 | movaps -0x68($key_),%xmm10
|
---|
1742 | movaps %xmm0,-0x68($key_)
|
---|
1743 | movaps -0x58($key_),%xmm11
|
---|
1744 | movaps %xmm0,-0x58($key_)
|
---|
1745 | movaps -0x48($key_),%xmm12
|
---|
1746 | movaps %xmm0,-0x48($key_)
|
---|
1747 | movaps -0x38($key_),%xmm13
|
---|
1748 | movaps %xmm0,-0x38($key_)
|
---|
1749 | movaps -0x28($key_),%xmm14
|
---|
1750 | movaps %xmm0,-0x28($key_)
|
---|
1751 | movaps -0x18($key_),%xmm15
|
---|
1752 | movaps %xmm0,-0x18($key_)
|
---|
1753 | movaps %xmm0,0x00(%rsp)
|
---|
1754 | movaps %xmm0,0x10(%rsp)
|
---|
1755 | movaps %xmm0,0x20(%rsp)
|
---|
1756 | movaps %xmm0,0x30(%rsp)
|
---|
1757 | movaps %xmm0,0x40(%rsp)
|
---|
1758 | movaps %xmm0,0x50(%rsp)
|
---|
1759 | movaps %xmm0,0x60(%rsp)
|
---|
1760 | movaps %xmm0,0x70(%rsp)
|
---|
1761 | ___
|
---|
1762 | $code.=<<___;
|
---|
1763 | mov -8($key_),%rbp
|
---|
1764 | .cfi_restore %rbp
|
---|
1765 | lea ($key_),%rsp
|
---|
1766 | .cfi_def_cfa_register %rsp
|
---|
1767 | .Lctr32_epilogue:
|
---|
1768 | ret
|
---|
1769 | .cfi_endproc
|
---|
1770 | .size aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks
|
---|
1771 | ___
|
---|
1772 | }
|
---|
1773 | |
---|
1774 |
|
---|
1775 | ######################################################################
|
---|
1776 | # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
|
---|
1777 | # const AES_KEY *key1, const AES_KEY *key2
|
---|
1778 | # const unsigned char iv[16]);
|
---|
1779 | #
|
---|
1780 | {
|
---|
1781 | my @tweak=map("%xmm$_",(10..15));
|
---|
1782 | my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
|
---|
1783 | my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
|
---|
1784 | my $frame_size = 0x70 + ($win64?160:0);
|
---|
1785 | my $key_ = "%rbp"; # override so that we can use %r11 as FP
|
---|
1786 |
|
---|
1787 | $code.=<<___;
|
---|
1788 | .globl aesni_xts_encrypt
|
---|
1789 | .type aesni_xts_encrypt,\@function,6
|
---|
1790 | .align 16
|
---|
1791 | aesni_xts_encrypt:
|
---|
1792 | .cfi_startproc
|
---|
1793 | endbranch
|
---|
1794 | lea (%rsp),%r11 # frame pointer
|
---|
1795 | .cfi_def_cfa_register %r11
|
---|
1796 | push %rbp
|
---|
1797 | .cfi_push %rbp
|
---|
1798 | sub \$$frame_size,%rsp
|
---|
1799 | and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
|
---|
1800 | ___
|
---|
1801 | $code.=<<___ if ($win64);
|
---|
1802 | movaps %xmm6,-0xa8(%r11) # offload everything
|
---|
1803 | movaps %xmm7,-0x98(%r11)
|
---|
1804 | movaps %xmm8,-0x88(%r11)
|
---|
1805 | movaps %xmm9,-0x78(%r11)
|
---|
1806 | movaps %xmm10,-0x68(%r11)
|
---|
1807 | movaps %xmm11,-0x58(%r11)
|
---|
1808 | movaps %xmm12,-0x48(%r11)
|
---|
1809 | movaps %xmm13,-0x38(%r11)
|
---|
1810 | movaps %xmm14,-0x28(%r11)
|
---|
1811 | movaps %xmm15,-0x18(%r11)
|
---|
1812 | .Lxts_enc_body:
|
---|
1813 | ___
|
---|
1814 | $code.=<<___;
|
---|
1815 | movups ($ivp),$inout0 # load clear-text tweak
|
---|
1816 | mov 240(%r8),$rounds # key2->rounds
|
---|
1817 | mov 240($key),$rnds_ # key1->rounds
|
---|
1818 | ___
|
---|
1819 | # generate the tweak
|
---|
1820 | &aesni_generate1("enc",$key2,$rounds,$inout0);
|
---|
1821 | $code.=<<___;
|
---|
1822 | $movkey ($key),$rndkey0 # zero round key
|
---|
1823 | mov $key,$key_ # backup $key
|
---|
1824 | mov $rnds_,$rounds # backup $rounds
|
---|
1825 | shl \$4,$rnds_
|
---|
1826 | mov $len,$len_ # backup $len
|
---|
1827 | and \$-16,$len
|
---|
1828 |
|
---|
1829 | $movkey 16($key,$rnds_),$rndkey1 # last round key
|
---|
1830 |
|
---|
1831 | movdqa .Lxts_magic(%rip),$twmask
|
---|
1832 | movdqa $inout0,@tweak[5]
|
---|
1833 | pshufd \$0x5f,$inout0,$twres
|
---|
1834 | pxor $rndkey0,$rndkey1
|
---|
1835 | ___
|
---|
1836 | # alternative tweak calculation algorithm is based on suggestions
|
---|
1837 | # by Shay Gueron. psrad doesn't conflict with AES-NI instructions
|
---|
1838 | # and should help in the future...
|
---|
1839 | for ($i=0;$i<4;$i++) {
|
---|
1840 | $code.=<<___;
|
---|
1841 | movdqa $twres,$twtmp
|
---|
1842 | paddd $twres,$twres
|
---|
1843 | movdqa @tweak[5],@tweak[$i]
|
---|
1844 | psrad \$31,$twtmp # broadcast upper bits
|
---|
1845 | paddq @tweak[5],@tweak[5]
|
---|
1846 | pand $twmask,$twtmp
|
---|
1847 | pxor $rndkey0,@tweak[$i]
|
---|
1848 | pxor $twtmp,@tweak[5]
|
---|
1849 | ___
|
---|
1850 | }
|
---|
1851 | $code.=<<___;
|
---|
1852 | movdqa @tweak[5],@tweak[4]
|
---|
1853 | psrad \$31,$twres
|
---|
1854 | paddq @tweak[5],@tweak[5]
|
---|
1855 | pand $twmask,$twres
|
---|
1856 | pxor $rndkey0,@tweak[4]
|
---|
1857 | pxor $twres,@tweak[5]
|
---|
1858 | movaps $rndkey1,0x60(%rsp) # save round[0]^round[last]
|
---|
1859 |
|
---|
1860 | sub \$16*6,$len
|
---|
1861 | jc .Lxts_enc_short # if $len-=6*16 borrowed
|
---|
1862 |
|
---|
1863 | mov \$16+96,$rounds
|
---|
1864 | lea 32($key_,$rnds_),$key # end of key schedule
|
---|
1865 | sub %r10,%rax # twisted $rounds
|
---|
1866 | $movkey 16($key_),$rndkey1
|
---|
1867 | mov %rax,%r10 # backup twisted $rounds
|
---|
1868 | lea .Lxts_magic(%rip),%r8
|
---|
1869 | jmp .Lxts_enc_grandloop
|
---|
1870 |
|
---|
1871 | .align 32
|
---|
1872 | .Lxts_enc_grandloop:
|
---|
1873 | movdqu `16*0`($inp),$inout0 # load input
|
---|
1874 | movdqa $rndkey0,$twmask
|
---|
1875 | movdqu `16*1`($inp),$inout1
|
---|
1876 | pxor @tweak[0],$inout0 # input^=tweak^round[0]
|
---|
1877 | movdqu `16*2`($inp),$inout2
|
---|
1878 | pxor @tweak[1],$inout1
|
---|
1879 | aesenc $rndkey1,$inout0
|
---|
1880 | movdqu `16*3`($inp),$inout3
|
---|
1881 | pxor @tweak[2],$inout2
|
---|
1882 | aesenc $rndkey1,$inout1
|
---|
1883 | movdqu `16*4`($inp),$inout4
|
---|
1884 | pxor @tweak[3],$inout3
|
---|
1885 | aesenc $rndkey1,$inout2
|
---|
1886 | movdqu `16*5`($inp),$inout5
|
---|
1887 | pxor @tweak[5],$twmask # round[0]^=tweak[5]
|
---|
1888 | movdqa 0x60(%rsp),$twres # load round[0]^round[last]
|
---|
1889 | pxor @tweak[4],$inout4
|
---|
1890 | aesenc $rndkey1,$inout3
|
---|
1891 | $movkey 32($key_),$rndkey0
|
---|
1892 | lea `16*6`($inp),$inp
|
---|
1893 | pxor $twmask,$inout5
|
---|
1894 |
|
---|
1895 | pxor $twres,@tweak[0] # calculate tweaks^round[last]
|
---|
1896 | aesenc $rndkey1,$inout4
|
---|
1897 | pxor $twres,@tweak[1]
|
---|
1898 | movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks^round[last]
|
---|
1899 | aesenc $rndkey1,$inout5
|
---|
1900 | $movkey 48($key_),$rndkey1
|
---|
1901 | pxor $twres,@tweak[2]
|
---|
1902 |
|
---|
1903 | aesenc $rndkey0,$inout0
|
---|
1904 | pxor $twres,@tweak[3]
|
---|
1905 | movdqa @tweak[1],`16*1`(%rsp)
|
---|
1906 | aesenc $rndkey0,$inout1
|
---|
1907 | pxor $twres,@tweak[4]
|
---|
1908 | movdqa @tweak[2],`16*2`(%rsp)
|
---|
1909 | aesenc $rndkey0,$inout2
|
---|
1910 | aesenc $rndkey0,$inout3
|
---|
1911 | pxor $twres,$twmask
|
---|
1912 | movdqa @tweak[4],`16*4`(%rsp)
|
---|
1913 | aesenc $rndkey0,$inout4
|
---|
1914 | aesenc $rndkey0,$inout5
|
---|
1915 | $movkey 64($key_),$rndkey0
|
---|
1916 | movdqa $twmask,`16*5`(%rsp)
|
---|
1917 | pshufd \$0x5f,@tweak[5],$twres
|
---|
1918 | jmp .Lxts_enc_loop6
|
---|
1919 | .align 32
|
---|
1920 | .Lxts_enc_loop6:
|
---|
1921 | aesenc $rndkey1,$inout0
|
---|
1922 | aesenc $rndkey1,$inout1
|
---|
1923 | aesenc $rndkey1,$inout2
|
---|
1924 | aesenc $rndkey1,$inout3
|
---|
1925 | aesenc $rndkey1,$inout4
|
---|
1926 | aesenc $rndkey1,$inout5
|
---|
1927 | $movkey -64($key,%rax),$rndkey1
|
---|
1928 | add \$32,%rax
|
---|
1929 |
|
---|
1930 | aesenc $rndkey0,$inout0
|
---|
1931 | aesenc $rndkey0,$inout1
|
---|
1932 | aesenc $rndkey0,$inout2
|
---|
1933 | aesenc $rndkey0,$inout3
|
---|
1934 | aesenc $rndkey0,$inout4
|
---|
1935 | aesenc $rndkey0,$inout5
|
---|
1936 | $movkey -80($key,%rax),$rndkey0
|
---|
1937 | jnz .Lxts_enc_loop6
|
---|
1938 |
|
---|
1939 | movdqa (%r8),$twmask # start calculating next tweak
|
---|
1940 | movdqa $twres,$twtmp
|
---|
1941 | paddd $twres,$twres
|
---|
1942 | aesenc $rndkey1,$inout0
|
---|
1943 | paddq @tweak[5],@tweak[5]
|
---|
1944 | psrad \$31,$twtmp
|
---|
1945 | aesenc $rndkey1,$inout1
|
---|
1946 | pand $twmask,$twtmp
|
---|
1947 | $movkey ($key_),@tweak[0] # load round[0]
|
---|
1948 | aesenc $rndkey1,$inout2
|
---|
1949 | aesenc $rndkey1,$inout3
|
---|
1950 | aesenc $rndkey1,$inout4
|
---|
1951 | pxor $twtmp,@tweak[5]
|
---|
1952 | movaps @tweak[0],@tweak[1] # copy round[0]
|
---|
1953 | aesenc $rndkey1,$inout5
|
---|
1954 | $movkey -64($key),$rndkey1
|
---|
1955 |
|
---|
1956 | movdqa $twres,$twtmp
|
---|
1957 | aesenc $rndkey0,$inout0
|
---|
1958 | paddd $twres,$twres
|
---|
1959 | pxor @tweak[5],@tweak[0]
|
---|
1960 | aesenc $rndkey0,$inout1
|
---|
1961 | psrad \$31,$twtmp
|
---|
1962 | paddq @tweak[5],@tweak[5]
|
---|
1963 | aesenc $rndkey0,$inout2
|
---|
1964 | aesenc $rndkey0,$inout3
|
---|
1965 | pand $twmask,$twtmp
|
---|
1966 | movaps @tweak[1],@tweak[2]
|
---|
1967 | aesenc $rndkey0,$inout4
|
---|
1968 | pxor $twtmp,@tweak[5]
|
---|
1969 | movdqa $twres,$twtmp
|
---|
1970 | aesenc $rndkey0,$inout5
|
---|
1971 | $movkey -48($key),$rndkey0
|
---|
1972 |
|
---|
1973 | paddd $twres,$twres
|
---|
1974 | aesenc $rndkey1,$inout0
|
---|
1975 | pxor @tweak[5],@tweak[1]
|
---|
1976 | psrad \$31,$twtmp
|
---|
1977 | aesenc $rndkey1,$inout1
|
---|
1978 | paddq @tweak[5],@tweak[5]
|
---|
1979 | pand $twmask,$twtmp
|
---|
1980 | aesenc $rndkey1,$inout2
|
---|
1981 | aesenc $rndkey1,$inout3
|
---|
1982 | movdqa @tweak[3],`16*3`(%rsp)
|
---|
1983 | pxor $twtmp,@tweak[5]
|
---|
1984 | aesenc $rndkey1,$inout4
|
---|
1985 | movaps @tweak[2],@tweak[3]
|
---|
1986 | movdqa $twres,$twtmp
|
---|
1987 | aesenc $rndkey1,$inout5
|
---|
1988 | $movkey -32($key),$rndkey1
|
---|
1989 |
|
---|
1990 | paddd $twres,$twres
|
---|
1991 | aesenc $rndkey0,$inout0
|
---|
1992 | pxor @tweak[5],@tweak[2]
|
---|
1993 | psrad \$31,$twtmp
|
---|
1994 | aesenc $rndkey0,$inout1
|
---|
1995 | paddq @tweak[5],@tweak[5]
|
---|
1996 | pand $twmask,$twtmp
|
---|
1997 | aesenc $rndkey0,$inout2
|
---|
1998 | aesenc $rndkey0,$inout3
|
---|
1999 | aesenc $rndkey0,$inout4
|
---|
2000 | pxor $twtmp,@tweak[5]
|
---|
2001 | movaps @tweak[3],@tweak[4]
|
---|
2002 | aesenc $rndkey0,$inout5
|
---|
2003 |
|
---|
2004 | movdqa $twres,$rndkey0
|
---|
2005 | paddd $twres,$twres
|
---|
2006 | aesenc $rndkey1,$inout0
|
---|
2007 | pxor @tweak[5],@tweak[3]
|
---|
2008 | psrad \$31,$rndkey0
|
---|
2009 | aesenc $rndkey1,$inout1
|
---|
2010 | paddq @tweak[5],@tweak[5]
|
---|
2011 | pand $twmask,$rndkey0
|
---|
2012 | aesenc $rndkey1,$inout2
|
---|
2013 | aesenc $rndkey1,$inout3
|
---|
2014 | pxor $rndkey0,@tweak[5]
|
---|
2015 | $movkey ($key_),$rndkey0
|
---|
2016 | aesenc $rndkey1,$inout4
|
---|
2017 | aesenc $rndkey1,$inout5
|
---|
2018 | $movkey 16($key_),$rndkey1
|
---|
2019 |
|
---|
2020 | pxor @tweak[5],@tweak[4]
|
---|
2021 | aesenclast `16*0`(%rsp),$inout0
|
---|
2022 | psrad \$31,$twres
|
---|
2023 | paddq @tweak[5],@tweak[5]
|
---|
2024 | aesenclast `16*1`(%rsp),$inout1
|
---|
2025 | aesenclast `16*2`(%rsp),$inout2
|
---|
2026 | pand $twmask,$twres
|
---|
2027 | mov %r10,%rax # restore $rounds
|
---|
2028 | aesenclast `16*3`(%rsp),$inout3
|
---|
2029 | aesenclast `16*4`(%rsp),$inout4
|
---|
2030 | aesenclast `16*5`(%rsp),$inout5
|
---|
2031 | pxor $twres,@tweak[5]
|
---|
2032 |
|
---|
2033 | lea `16*6`($out),$out # $out+=6*16
|
---|
2034 | movups $inout0,`-16*6`($out) # store 6 output blocks
|
---|
2035 | movups $inout1,`-16*5`($out)
|
---|
2036 | movups $inout2,`-16*4`($out)
|
---|
2037 | movups $inout3,`-16*3`($out)
|
---|
2038 | movups $inout4,`-16*2`($out)
|
---|
2039 | movups $inout5,`-16*1`($out)
|
---|
2040 | sub \$16*6,$len
|
---|
2041 | jnc .Lxts_enc_grandloop # loop if $len-=6*16 didn't borrow
|
---|
2042 |
|
---|
2043 | mov \$16+96,$rounds
|
---|
2044 | sub $rnds_,$rounds
|
---|
2045 | mov $key_,$key # restore $key
|
---|
2046 | shr \$4,$rounds # restore original value
|
---|
2047 |
|
---|
2048 | .Lxts_enc_short:
|
---|
2049 | # at the point @tweak[0..5] are populated with tweak values
|
---|
2050 | mov $rounds,$rnds_ # backup $rounds
|
---|
2051 | pxor $rndkey0,@tweak[0]
|
---|
2052 | add \$16*6,$len # restore real remaining $len
|
---|
2053 | jz .Lxts_enc_done # done if ($len==0)
|
---|
2054 |
|
---|
2055 | pxor $rndkey0,@tweak[1]
|
---|
2056 | cmp \$0x20,$len
|
---|
2057 | jb .Lxts_enc_one # $len is 1*16
|
---|
2058 | pxor $rndkey0,@tweak[2]
|
---|
2059 | je .Lxts_enc_two # $len is 2*16
|
---|
2060 |
|
---|
2061 | pxor $rndkey0,@tweak[3]
|
---|
2062 | cmp \$0x40,$len
|
---|
2063 | jb .Lxts_enc_three # $len is 3*16
|
---|
2064 | pxor $rndkey0,@tweak[4]
|
---|
2065 | je .Lxts_enc_four # $len is 4*16
|
---|
2066 |
|
---|
2067 | movdqu ($inp),$inout0 # $len is 5*16
|
---|
2068 | movdqu 16*1($inp),$inout1
|
---|
2069 | movdqu 16*2($inp),$inout2
|
---|
2070 | pxor @tweak[0],$inout0
|
---|
2071 | movdqu 16*3($inp),$inout3
|
---|
2072 | pxor @tweak[1],$inout1
|
---|
2073 | movdqu 16*4($inp),$inout4
|
---|
2074 | lea 16*5($inp),$inp # $inp+=5*16
|
---|
2075 | pxor @tweak[2],$inout2
|
---|
2076 | pxor @tweak[3],$inout3
|
---|
2077 | pxor @tweak[4],$inout4
|
---|
2078 | pxor $inout5,$inout5
|
---|
2079 |
|
---|
2080 | call _aesni_encrypt6
|
---|
2081 |
|
---|
2082 | xorps @tweak[0],$inout0
|
---|
2083 | movdqa @tweak[5],@tweak[0]
|
---|
2084 | xorps @tweak[1],$inout1
|
---|
2085 | xorps @tweak[2],$inout2
|
---|
2086 | movdqu $inout0,($out) # store 5 output blocks
|
---|
2087 | xorps @tweak[3],$inout3
|
---|
2088 | movdqu $inout1,16*1($out)
|
---|
2089 | xorps @tweak[4],$inout4
|
---|
2090 | movdqu $inout2,16*2($out)
|
---|
2091 | movdqu $inout3,16*3($out)
|
---|
2092 | movdqu $inout4,16*4($out)
|
---|
2093 | lea 16*5($out),$out # $out+=5*16
|
---|
2094 | jmp .Lxts_enc_done
|
---|
2095 |
|
---|
2096 | .align 16
|
---|
2097 | .Lxts_enc_one:
|
---|
2098 | movups ($inp),$inout0
|
---|
2099 | lea 16*1($inp),$inp # inp+=1*16
|
---|
2100 | xorps @tweak[0],$inout0
|
---|
2101 | ___
|
---|
2102 | &aesni_generate1("enc",$key,$rounds);
|
---|
2103 | $code.=<<___;
|
---|
2104 | xorps @tweak[0],$inout0
|
---|
2105 | movdqa @tweak[1],@tweak[0]
|
---|
2106 | movups $inout0,($out) # store one output block
|
---|
2107 | lea 16*1($out),$out # $out+=1*16
|
---|
2108 | jmp .Lxts_enc_done
|
---|
2109 |
|
---|
2110 | .align 16
|
---|
2111 | .Lxts_enc_two:
|
---|
2112 | movups ($inp),$inout0
|
---|
2113 | movups 16($inp),$inout1
|
---|
2114 | lea 32($inp),$inp # $inp+=2*16
|
---|
2115 | xorps @tweak[0],$inout0
|
---|
2116 | xorps @tweak[1],$inout1
|
---|
2117 |
|
---|
2118 | call _aesni_encrypt2
|
---|
2119 |
|
---|
2120 | xorps @tweak[0],$inout0
|
---|
2121 | movdqa @tweak[2],@tweak[0]
|
---|
2122 | xorps @tweak[1],$inout1
|
---|
2123 | movups $inout0,($out) # store 2 output blocks
|
---|
2124 | movups $inout1,16*1($out)
|
---|
2125 | lea 16*2($out),$out # $out+=2*16
|
---|
2126 | jmp .Lxts_enc_done
|
---|
2127 |
|
---|
2128 | .align 16
|
---|
2129 | .Lxts_enc_three:
|
---|
2130 | movups ($inp),$inout0
|
---|
2131 | movups 16*1($inp),$inout1
|
---|
2132 | movups 16*2($inp),$inout2
|
---|
2133 | lea 16*3($inp),$inp # $inp+=3*16
|
---|
2134 | xorps @tweak[0],$inout0
|
---|
2135 | xorps @tweak[1],$inout1
|
---|
2136 | xorps @tweak[2],$inout2
|
---|
2137 |
|
---|
2138 | call _aesni_encrypt3
|
---|
2139 |
|
---|
2140 | xorps @tweak[0],$inout0
|
---|
2141 | movdqa @tweak[3],@tweak[0]
|
---|
2142 | xorps @tweak[1],$inout1
|
---|
2143 | xorps @tweak[2],$inout2
|
---|
2144 | movups $inout0,($out) # store 3 output blocks
|
---|
2145 | movups $inout1,16*1($out)
|
---|
2146 | movups $inout2,16*2($out)
|
---|
2147 | lea 16*3($out),$out # $out+=3*16
|
---|
2148 | jmp .Lxts_enc_done
|
---|
2149 |
|
---|
2150 | .align 16
|
---|
2151 | .Lxts_enc_four:
|
---|
2152 | movups ($inp),$inout0
|
---|
2153 | movups 16*1($inp),$inout1
|
---|
2154 | movups 16*2($inp),$inout2
|
---|
2155 | xorps @tweak[0],$inout0
|
---|
2156 | movups 16*3($inp),$inout3
|
---|
2157 | lea 16*4($inp),$inp # $inp+=4*16
|
---|
2158 | xorps @tweak[1],$inout1
|
---|
2159 | xorps @tweak[2],$inout2
|
---|
2160 | xorps @tweak[3],$inout3
|
---|
2161 |
|
---|
2162 | call _aesni_encrypt4
|
---|
2163 |
|
---|
2164 | pxor @tweak[0],$inout0
|
---|
2165 | movdqa @tweak[4],@tweak[0]
|
---|
2166 | pxor @tweak[1],$inout1
|
---|
2167 | pxor @tweak[2],$inout2
|
---|
2168 | movdqu $inout0,($out) # store 4 output blocks
|
---|
2169 | pxor @tweak[3],$inout3
|
---|
2170 | movdqu $inout1,16*1($out)
|
---|
2171 | movdqu $inout2,16*2($out)
|
---|
2172 | movdqu $inout3,16*3($out)
|
---|
2173 | lea 16*4($out),$out # $out+=4*16
|
---|
2174 | jmp .Lxts_enc_done
|
---|
2175 |
|
---|
2176 | .align 16
|
---|
2177 | .Lxts_enc_done:
|
---|
2178 | and \$15,$len_ # see if $len%16 is 0
|
---|
2179 | jz .Lxts_enc_ret
|
---|
2180 | mov $len_,$len
|
---|
2181 |
|
---|
2182 | .Lxts_enc_steal:
|
---|
2183 | movzb ($inp),%eax # borrow $rounds ...
|
---|
2184 | movzb -16($out),%ecx # ... and $key
|
---|
2185 | lea 1($inp),$inp
|
---|
2186 | mov %al,-16($out)
|
---|
2187 | mov %cl,0($out)
|
---|
2188 | lea 1($out),$out
|
---|
2189 | sub \$1,$len
|
---|
2190 | jnz .Lxts_enc_steal
|
---|
2191 |
|
---|
2192 | sub $len_,$out # rewind $out
|
---|
2193 | mov $key_,$key # restore $key
|
---|
2194 | mov $rnds_,$rounds # restore $rounds
|
---|
2195 |
|
---|
2196 | movups -16($out),$inout0
|
---|
2197 | xorps @tweak[0],$inout0
|
---|
2198 | ___
|
---|
2199 | &aesni_generate1("enc",$key,$rounds);
|
---|
2200 | $code.=<<___;
|
---|
2201 | xorps @tweak[0],$inout0
|
---|
2202 | movups $inout0,-16($out)
|
---|
2203 |
|
---|
2204 | .Lxts_enc_ret:
|
---|
2205 | xorps %xmm0,%xmm0 # clear register bank
|
---|
2206 | pxor %xmm1,%xmm1
|
---|
2207 | pxor %xmm2,%xmm2
|
---|
2208 | pxor %xmm3,%xmm3
|
---|
2209 | pxor %xmm4,%xmm4
|
---|
2210 | pxor %xmm5,%xmm5
|
---|
2211 | ___
|
---|
2212 | $code.=<<___ if (!$win64);
|
---|
2213 | pxor %xmm6,%xmm6
|
---|
2214 | pxor %xmm7,%xmm7
|
---|
2215 | movaps %xmm0,0x00(%rsp) # clear stack
|
---|
2216 | pxor %xmm8,%xmm8
|
---|
2217 | movaps %xmm0,0x10(%rsp)
|
---|
2218 | pxor %xmm9,%xmm9
|
---|
2219 | movaps %xmm0,0x20(%rsp)
|
---|
2220 | pxor %xmm10,%xmm10
|
---|
2221 | movaps %xmm0,0x30(%rsp)
|
---|
2222 | pxor %xmm11,%xmm11
|
---|
2223 | movaps %xmm0,0x40(%rsp)
|
---|
2224 | pxor %xmm12,%xmm12
|
---|
2225 | movaps %xmm0,0x50(%rsp)
|
---|
2226 | pxor %xmm13,%xmm13
|
---|
2227 | movaps %xmm0,0x60(%rsp)
|
---|
2228 | pxor %xmm14,%xmm14
|
---|
2229 | pxor %xmm15,%xmm15
|
---|
2230 | ___
|
---|
2231 | $code.=<<___ if ($win64);
|
---|
2232 | movaps -0xa8(%r11),%xmm6
|
---|
2233 | movaps %xmm0,-0xa8(%r11) # clear stack
|
---|
2234 | movaps -0x98(%r11),%xmm7
|
---|
2235 | movaps %xmm0,-0x98(%r11)
|
---|
2236 | movaps -0x88(%r11),%xmm8
|
---|
2237 | movaps %xmm0,-0x88(%r11)
|
---|
2238 | movaps -0x78(%r11),%xmm9
|
---|
2239 | movaps %xmm0,-0x78(%r11)
|
---|
2240 | movaps -0x68(%r11),%xmm10
|
---|
2241 | movaps %xmm0,-0x68(%r11)
|
---|
2242 | movaps -0x58(%r11),%xmm11
|
---|
2243 | movaps %xmm0,-0x58(%r11)
|
---|
2244 | movaps -0x48(%r11),%xmm12
|
---|
2245 | movaps %xmm0,-0x48(%r11)
|
---|
2246 | movaps -0x38(%r11),%xmm13
|
---|
2247 | movaps %xmm0,-0x38(%r11)
|
---|
2248 | movaps -0x28(%r11),%xmm14
|
---|
2249 | movaps %xmm0,-0x28(%r11)
|
---|
2250 | movaps -0x18(%r11),%xmm15
|
---|
2251 | movaps %xmm0,-0x18(%r11)
|
---|
2252 | movaps %xmm0,0x00(%rsp)
|
---|
2253 | movaps %xmm0,0x10(%rsp)
|
---|
2254 | movaps %xmm0,0x20(%rsp)
|
---|
2255 | movaps %xmm0,0x30(%rsp)
|
---|
2256 | movaps %xmm0,0x40(%rsp)
|
---|
2257 | movaps %xmm0,0x50(%rsp)
|
---|
2258 | movaps %xmm0,0x60(%rsp)
|
---|
2259 | ___
|
---|
2260 | $code.=<<___;
|
---|
2261 | mov -8(%r11),%rbp
|
---|
2262 | .cfi_restore %rbp
|
---|
2263 | lea (%r11),%rsp
|
---|
2264 | .cfi_def_cfa_register %rsp
|
---|
2265 | .Lxts_enc_epilogue:
|
---|
2266 | ret
|
---|
2267 | .cfi_endproc
|
---|
2268 | .size aesni_xts_encrypt,.-aesni_xts_encrypt
|
---|
2269 | ___
|
---|
2270 |
|
---|
2271 | $code.=<<___;
|
---|
2272 | .globl aesni_xts_decrypt
|
---|
2273 | .type aesni_xts_decrypt,\@function,6
|
---|
2274 | .align 16
|
---|
2275 | aesni_xts_decrypt:
|
---|
2276 | .cfi_startproc
|
---|
2277 | endbranch
|
---|
2278 | lea (%rsp),%r11 # frame pointer
|
---|
2279 | .cfi_def_cfa_register %r11
|
---|
2280 | push %rbp
|
---|
2281 | .cfi_push %rbp
|
---|
2282 | sub \$$frame_size,%rsp
|
---|
2283 | and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
|
---|
2284 | ___
|
---|
2285 | $code.=<<___ if ($win64);
|
---|
2286 | movaps %xmm6,-0xa8(%r11) # offload everything
|
---|
2287 | movaps %xmm7,-0x98(%r11)
|
---|
2288 | movaps %xmm8,-0x88(%r11)
|
---|
2289 | movaps %xmm9,-0x78(%r11)
|
---|
2290 | movaps %xmm10,-0x68(%r11)
|
---|
2291 | movaps %xmm11,-0x58(%r11)
|
---|
2292 | movaps %xmm12,-0x48(%r11)
|
---|
2293 | movaps %xmm13,-0x38(%r11)
|
---|
2294 | movaps %xmm14,-0x28(%r11)
|
---|
2295 | movaps %xmm15,-0x18(%r11)
|
---|
2296 | .Lxts_dec_body:
|
---|
2297 | ___
|
---|
2298 | $code.=<<___;
|
---|
2299 | movups ($ivp),$inout0 # load clear-text tweak
|
---|
2300 | mov 240($key2),$rounds # key2->rounds
|
---|
2301 | mov 240($key),$rnds_ # key1->rounds
|
---|
2302 | ___
|
---|
2303 | # generate the tweak
|
---|
2304 | &aesni_generate1("enc",$key2,$rounds,$inout0);
|
---|
2305 | $code.=<<___;
|
---|
2306 | xor %eax,%eax # if ($len%16) len-=16;
|
---|
2307 | test \$15,$len
|
---|
2308 | setnz %al
|
---|
2309 | shl \$4,%rax
|
---|
2310 | sub %rax,$len
|
---|
2311 |
|
---|
2312 | $movkey ($key),$rndkey0 # zero round key
|
---|
2313 | mov $key,$key_ # backup $key
|
---|
2314 | mov $rnds_,$rounds # backup $rounds
|
---|
2315 | shl \$4,$rnds_
|
---|
2316 | mov $len,$len_ # backup $len
|
---|
2317 | and \$-16,$len
|
---|
2318 |
|
---|
2319 | $movkey 16($key,$rnds_),$rndkey1 # last round key
|
---|
2320 |
|
---|
2321 | movdqa .Lxts_magic(%rip),$twmask
|
---|
2322 | movdqa $inout0,@tweak[5]
|
---|
2323 | pshufd \$0x5f,$inout0,$twres
|
---|
2324 | pxor $rndkey0,$rndkey1
|
---|
2325 | ___
|
---|
2326 | for ($i=0;$i<4;$i++) {
|
---|
2327 | $code.=<<___;
|
---|
2328 | movdqa $twres,$twtmp
|
---|
2329 | paddd $twres,$twres
|
---|
2330 | movdqa @tweak[5],@tweak[$i]
|
---|
2331 | psrad \$31,$twtmp # broadcast upper bits
|
---|
2332 | paddq @tweak[5],@tweak[5]
|
---|
2333 | pand $twmask,$twtmp
|
---|
2334 | pxor $rndkey0,@tweak[$i]
|
---|
2335 | pxor $twtmp,@tweak[5]
|
---|
2336 | ___
|
---|
2337 | }
|
---|
2338 | $code.=<<___;
|
---|
2339 | movdqa @tweak[5],@tweak[4]
|
---|
2340 | psrad \$31,$twres
|
---|
2341 | paddq @tweak[5],@tweak[5]
|
---|
2342 | pand $twmask,$twres
|
---|
2343 | pxor $rndkey0,@tweak[4]
|
---|
2344 | pxor $twres,@tweak[5]
|
---|
2345 | movaps $rndkey1,0x60(%rsp) # save round[0]^round[last]
|
---|
2346 |
|
---|
2347 | sub \$16*6,$len
|
---|
2348 | jc .Lxts_dec_short # if $len-=6*16 borrowed
|
---|
2349 |
|
---|
2350 | mov \$16+96,$rounds
|
---|
2351 | lea 32($key_,$rnds_),$key # end of key schedule
|
---|
2352 | sub %r10,%rax # twisted $rounds
|
---|
2353 | $movkey 16($key_),$rndkey1
|
---|
2354 | mov %rax,%r10 # backup twisted $rounds
|
---|
2355 | lea .Lxts_magic(%rip),%r8
|
---|
2356 | jmp .Lxts_dec_grandloop
|
---|
2357 |
|
---|
2358 | .align 32
|
---|
2359 | .Lxts_dec_grandloop:
|
---|
2360 | movdqu `16*0`($inp),$inout0 # load input
|
---|
2361 | movdqa $rndkey0,$twmask
|
---|
2362 | movdqu `16*1`($inp),$inout1
|
---|
2363 | pxor @tweak[0],$inout0 # input^=tweak^round[0]
|
---|
2364 | movdqu `16*2`($inp),$inout2
|
---|
2365 | pxor @tweak[1],$inout1
|
---|
2366 | aesdec $rndkey1,$inout0
|
---|
2367 | movdqu `16*3`($inp),$inout3
|
---|
2368 | pxor @tweak[2],$inout2
|
---|
2369 | aesdec $rndkey1,$inout1
|
---|
2370 | movdqu `16*4`($inp),$inout4
|
---|
2371 | pxor @tweak[3],$inout3
|
---|
2372 | aesdec $rndkey1,$inout2
|
---|
2373 | movdqu `16*5`($inp),$inout5
|
---|
2374 | pxor @tweak[5],$twmask # round[0]^=tweak[5]
|
---|
2375 | movdqa 0x60(%rsp),$twres # load round[0]^round[last]
|
---|
2376 | pxor @tweak[4],$inout4
|
---|
2377 | aesdec $rndkey1,$inout3
|
---|
2378 | $movkey 32($key_),$rndkey0
|
---|
2379 | lea `16*6`($inp),$inp
|
---|
2380 | pxor $twmask,$inout5
|
---|
2381 |
|
---|
2382 | pxor $twres,@tweak[0] # calculate tweaks^round[last]
|
---|
2383 | aesdec $rndkey1,$inout4
|
---|
2384 | pxor $twres,@tweak[1]
|
---|
2385 | movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks^last round key
|
---|
2386 | aesdec $rndkey1,$inout5
|
---|
2387 | $movkey 48($key_),$rndkey1
|
---|
2388 | pxor $twres,@tweak[2]
|
---|
2389 |
|
---|
2390 | aesdec $rndkey0,$inout0
|
---|
2391 | pxor $twres,@tweak[3]
|
---|
2392 | movdqa @tweak[1],`16*1`(%rsp)
|
---|
2393 | aesdec $rndkey0,$inout1
|
---|
2394 | pxor $twres,@tweak[4]
|
---|
2395 | movdqa @tweak[2],`16*2`(%rsp)
|
---|
2396 | aesdec $rndkey0,$inout2
|
---|
2397 | aesdec $rndkey0,$inout3
|
---|
2398 | pxor $twres,$twmask
|
---|
2399 | movdqa @tweak[4],`16*4`(%rsp)
|
---|
2400 | aesdec $rndkey0,$inout4
|
---|
2401 | aesdec $rndkey0,$inout5
|
---|
2402 | $movkey 64($key_),$rndkey0
|
---|
2403 | movdqa $twmask,`16*5`(%rsp)
|
---|
2404 | pshufd \$0x5f,@tweak[5],$twres
|
---|
2405 | jmp .Lxts_dec_loop6
|
---|
2406 | .align 32
|
---|
2407 | .Lxts_dec_loop6:
|
---|
2408 | aesdec $rndkey1,$inout0
|
---|
2409 | aesdec $rndkey1,$inout1
|
---|
2410 | aesdec $rndkey1,$inout2
|
---|
2411 | aesdec $rndkey1,$inout3
|
---|
2412 | aesdec $rndkey1,$inout4
|
---|
2413 | aesdec $rndkey1,$inout5
|
---|
2414 | $movkey -64($key,%rax),$rndkey1
|
---|
2415 | add \$32,%rax
|
---|
2416 |
|
---|
2417 | aesdec $rndkey0,$inout0
|
---|
2418 | aesdec $rndkey0,$inout1
|
---|
2419 | aesdec $rndkey0,$inout2
|
---|
2420 | aesdec $rndkey0,$inout3
|
---|
2421 | aesdec $rndkey0,$inout4
|
---|
2422 | aesdec $rndkey0,$inout5
|
---|
2423 | $movkey -80($key,%rax),$rndkey0
|
---|
2424 | jnz .Lxts_dec_loop6
|
---|
2425 |
|
---|
2426 | movdqa (%r8),$twmask # start calculating next tweak
|
---|
2427 | movdqa $twres,$twtmp
|
---|
2428 | paddd $twres,$twres
|
---|
2429 | aesdec $rndkey1,$inout0
|
---|
2430 | paddq @tweak[5],@tweak[5]
|
---|
2431 | psrad \$31,$twtmp
|
---|
2432 | aesdec $rndkey1,$inout1
|
---|
2433 | pand $twmask,$twtmp
|
---|
2434 | $movkey ($key_),@tweak[0] # load round[0]
|
---|
2435 | aesdec $rndkey1,$inout2
|
---|
2436 | aesdec $rndkey1,$inout3
|
---|
2437 | aesdec $rndkey1,$inout4
|
---|
2438 | pxor $twtmp,@tweak[5]
|
---|
2439 | movaps @tweak[0],@tweak[1] # copy round[0]
|
---|
2440 | aesdec $rndkey1,$inout5
|
---|
2441 | $movkey -64($key),$rndkey1
|
---|
2442 |
|
---|
2443 | movdqa $twres,$twtmp
|
---|
2444 | aesdec $rndkey0,$inout0
|
---|
2445 | paddd $twres,$twres
|
---|
2446 | pxor @tweak[5],@tweak[0]
|
---|
2447 | aesdec $rndkey0,$inout1
|
---|
2448 | psrad \$31,$twtmp
|
---|
2449 | paddq @tweak[5],@tweak[5]
|
---|
2450 | aesdec $rndkey0,$inout2
|
---|
2451 | aesdec $rndkey0,$inout3
|
---|
2452 | pand $twmask,$twtmp
|
---|
2453 | movaps @tweak[1],@tweak[2]
|
---|
2454 | aesdec $rndkey0,$inout4
|
---|
2455 | pxor $twtmp,@tweak[5]
|
---|
2456 | movdqa $twres,$twtmp
|
---|
2457 | aesdec $rndkey0,$inout5
|
---|
2458 | $movkey -48($key),$rndkey0
|
---|
2459 |
|
---|
2460 | paddd $twres,$twres
|
---|
2461 | aesdec $rndkey1,$inout0
|
---|
2462 | pxor @tweak[5],@tweak[1]
|
---|
2463 | psrad \$31,$twtmp
|
---|
2464 | aesdec $rndkey1,$inout1
|
---|
2465 | paddq @tweak[5],@tweak[5]
|
---|
2466 | pand $twmask,$twtmp
|
---|
2467 | aesdec $rndkey1,$inout2
|
---|
2468 | aesdec $rndkey1,$inout3
|
---|
2469 | movdqa @tweak[3],`16*3`(%rsp)
|
---|
2470 | pxor $twtmp,@tweak[5]
|
---|
2471 | aesdec $rndkey1,$inout4
|
---|
2472 | movaps @tweak[2],@tweak[3]
|
---|
2473 | movdqa $twres,$twtmp
|
---|
2474 | aesdec $rndkey1,$inout5
|
---|
2475 | $movkey -32($key),$rndkey1
|
---|
2476 |
|
---|
2477 | paddd $twres,$twres
|
---|
2478 | aesdec $rndkey0,$inout0
|
---|
2479 | pxor @tweak[5],@tweak[2]
|
---|
2480 | psrad \$31,$twtmp
|
---|
2481 | aesdec $rndkey0,$inout1
|
---|
2482 | paddq @tweak[5],@tweak[5]
|
---|
2483 | pand $twmask,$twtmp
|
---|
2484 | aesdec $rndkey0,$inout2
|
---|
2485 | aesdec $rndkey0,$inout3
|
---|
2486 | aesdec $rndkey0,$inout4
|
---|
2487 | pxor $twtmp,@tweak[5]
|
---|
2488 | movaps @tweak[3],@tweak[4]
|
---|
2489 | aesdec $rndkey0,$inout5
|
---|
2490 |
|
---|
2491 | movdqa $twres,$rndkey0
|
---|
2492 | paddd $twres,$twres
|
---|
2493 | aesdec $rndkey1,$inout0
|
---|
2494 | pxor @tweak[5],@tweak[3]
|
---|
2495 | psrad \$31,$rndkey0
|
---|
2496 | aesdec $rndkey1,$inout1
|
---|
2497 | paddq @tweak[5],@tweak[5]
|
---|
2498 | pand $twmask,$rndkey0
|
---|
2499 | aesdec $rndkey1,$inout2
|
---|
2500 | aesdec $rndkey1,$inout3
|
---|
2501 | pxor $rndkey0,@tweak[5]
|
---|
2502 | $movkey ($key_),$rndkey0
|
---|
2503 | aesdec $rndkey1,$inout4
|
---|
2504 | aesdec $rndkey1,$inout5
|
---|
2505 | $movkey 16($key_),$rndkey1
|
---|
2506 |
|
---|
2507 | pxor @tweak[5],@tweak[4]
|
---|
2508 | aesdeclast `16*0`(%rsp),$inout0
|
---|
2509 | psrad \$31,$twres
|
---|
2510 | paddq @tweak[5],@tweak[5]
|
---|
2511 | aesdeclast `16*1`(%rsp),$inout1
|
---|
2512 | aesdeclast `16*2`(%rsp),$inout2
|
---|
2513 | pand $twmask,$twres
|
---|
2514 | mov %r10,%rax # restore $rounds
|
---|
2515 | aesdeclast `16*3`(%rsp),$inout3
|
---|
2516 | aesdeclast `16*4`(%rsp),$inout4
|
---|
2517 | aesdeclast `16*5`(%rsp),$inout5
|
---|
2518 | pxor $twres,@tweak[5]
|
---|
2519 |
|
---|
2520 | lea `16*6`($out),$out # $out+=6*16
|
---|
2521 | movups $inout0,`-16*6`($out) # store 6 output blocks
|
---|
2522 | movups $inout1,`-16*5`($out)
|
---|
2523 | movups $inout2,`-16*4`($out)
|
---|
2524 | movups $inout3,`-16*3`($out)
|
---|
2525 | movups $inout4,`-16*2`($out)
|
---|
2526 | movups $inout5,`-16*1`($out)
|
---|
2527 | sub \$16*6,$len
|
---|
2528 | jnc .Lxts_dec_grandloop # loop if $len-=6*16 didn't borrow
|
---|
2529 |
|
---|
2530 | mov \$16+96,$rounds
|
---|
2531 | sub $rnds_,$rounds
|
---|
2532 | mov $key_,$key # restore $key
|
---|
2533 | shr \$4,$rounds # restore original value
|
---|
2534 |
|
---|
2535 | .Lxts_dec_short:
|
---|
2536 | # at the point @tweak[0..5] are populated with tweak values
|
---|
2537 | mov $rounds,$rnds_ # backup $rounds
|
---|
2538 | pxor $rndkey0,@tweak[0]
|
---|
2539 | pxor $rndkey0,@tweak[1]
|
---|
2540 | add \$16*6,$len # restore real remaining $len
|
---|
2541 | jz .Lxts_dec_done # done if ($len==0)
|
---|
2542 |
|
---|
2543 | pxor $rndkey0,@tweak[2]
|
---|
2544 | cmp \$0x20,$len
|
---|
2545 | jb .Lxts_dec_one # $len is 1*16
|
---|
2546 | pxor $rndkey0,@tweak[3]
|
---|
2547 | je .Lxts_dec_two # $len is 2*16
|
---|
2548 |
|
---|
2549 | pxor $rndkey0,@tweak[4]
|
---|
2550 | cmp \$0x40,$len
|
---|
2551 | jb .Lxts_dec_three # $len is 3*16
|
---|
2552 | je .Lxts_dec_four # $len is 4*16
|
---|
2553 |
|
---|
2554 | movdqu ($inp),$inout0 # $len is 5*16
|
---|
2555 | movdqu 16*1($inp),$inout1
|
---|
2556 | movdqu 16*2($inp),$inout2
|
---|
2557 | pxor @tweak[0],$inout0
|
---|
2558 | movdqu 16*3($inp),$inout3
|
---|
2559 | pxor @tweak[1],$inout1
|
---|
2560 | movdqu 16*4($inp),$inout4
|
---|
2561 | lea 16*5($inp),$inp # $inp+=5*16
|
---|
2562 | pxor @tweak[2],$inout2
|
---|
2563 | pxor @tweak[3],$inout3
|
---|
2564 | pxor @tweak[4],$inout4
|
---|
2565 |
|
---|
2566 | call _aesni_decrypt6
|
---|
2567 |
|
---|
2568 | xorps @tweak[0],$inout0
|
---|
2569 | xorps @tweak[1],$inout1
|
---|
2570 | xorps @tweak[2],$inout2
|
---|
2571 | movdqu $inout0,($out) # store 5 output blocks
|
---|
2572 | xorps @tweak[3],$inout3
|
---|
2573 | movdqu $inout1,16*1($out)
|
---|
2574 | xorps @tweak[4],$inout4
|
---|
2575 | movdqu $inout2,16*2($out)
|
---|
2576 | pxor $twtmp,$twtmp
|
---|
2577 | movdqu $inout3,16*3($out)
|
---|
2578 | pcmpgtd @tweak[5],$twtmp
|
---|
2579 | movdqu $inout4,16*4($out)
|
---|
2580 | lea 16*5($out),$out # $out+=5*16
|
---|
2581 | pshufd \$0x13,$twtmp,@tweak[1] # $twres
|
---|
2582 | and \$15,$len_
|
---|
2583 | jz .Lxts_dec_ret
|
---|
2584 |
|
---|
2585 | movdqa @tweak[5],@tweak[0]
|
---|
2586 | paddq @tweak[5],@tweak[5] # psllq 1,$tweak
|
---|
2587 | pand $twmask,@tweak[1] # isolate carry and residue
|
---|
2588 | pxor @tweak[5],@tweak[1]
|
---|
2589 | jmp .Lxts_dec_done2
|
---|
2590 |
|
---|
2591 | .align 16
|
---|
2592 | .Lxts_dec_one:
|
---|
2593 | movups ($inp),$inout0
|
---|
2594 | lea 16*1($inp),$inp # $inp+=1*16
|
---|
2595 | xorps @tweak[0],$inout0
|
---|
2596 | ___
|
---|
2597 | &aesni_generate1("dec",$key,$rounds);
|
---|
2598 | $code.=<<___;
|
---|
2599 | xorps @tweak[0],$inout0
|
---|
2600 | movdqa @tweak[1],@tweak[0]
|
---|
2601 | movups $inout0,($out) # store one output block
|
---|
2602 | movdqa @tweak[2],@tweak[1]
|
---|
2603 | lea 16*1($out),$out # $out+=1*16
|
---|
2604 | jmp .Lxts_dec_done
|
---|
2605 |
|
---|
2606 | .align 16
|
---|
2607 | .Lxts_dec_two:
|
---|
2608 | movups ($inp),$inout0
|
---|
2609 | movups 16($inp),$inout1
|
---|
2610 | lea 32($inp),$inp # $inp+=2*16
|
---|
2611 | xorps @tweak[0],$inout0
|
---|
2612 | xorps @tweak[1],$inout1
|
---|
2613 |
|
---|
2614 | call _aesni_decrypt2
|
---|
2615 |
|
---|
2616 | xorps @tweak[0],$inout0
|
---|
2617 | movdqa @tweak[2],@tweak[0]
|
---|
2618 | xorps @tweak[1],$inout1
|
---|
2619 | movdqa @tweak[3],@tweak[1]
|
---|
2620 | movups $inout0,($out) # store 2 output blocks
|
---|
2621 | movups $inout1,16*1($out)
|
---|
2622 | lea 16*2($out),$out # $out+=2*16
|
---|
2623 | jmp .Lxts_dec_done
|
---|
2624 |
|
---|
2625 | .align 16
|
---|
2626 | .Lxts_dec_three:
|
---|
2627 | movups ($inp),$inout0
|
---|
2628 | movups 16*1($inp),$inout1
|
---|
2629 | movups 16*2($inp),$inout2
|
---|
2630 | lea 16*3($inp),$inp # $inp+=3*16
|
---|
2631 | xorps @tweak[0],$inout0
|
---|
2632 | xorps @tweak[1],$inout1
|
---|
2633 | xorps @tweak[2],$inout2
|
---|
2634 |
|
---|
2635 | call _aesni_decrypt3
|
---|
2636 |
|
---|
2637 | xorps @tweak[0],$inout0
|
---|
2638 | movdqa @tweak[3],@tweak[0]
|
---|
2639 | xorps @tweak[1],$inout1
|
---|
2640 | movdqa @tweak[4],@tweak[1]
|
---|
2641 | xorps @tweak[2],$inout2
|
---|
2642 | movups $inout0,($out) # store 3 output blocks
|
---|
2643 | movups $inout1,16*1($out)
|
---|
2644 | movups $inout2,16*2($out)
|
---|
2645 | lea 16*3($out),$out # $out+=3*16
|
---|
2646 | jmp .Lxts_dec_done
|
---|
2647 |
|
---|
2648 | .align 16
|
---|
2649 | .Lxts_dec_four:
|
---|
2650 | movups ($inp),$inout0
|
---|
2651 | movups 16*1($inp),$inout1
|
---|
2652 | movups 16*2($inp),$inout2
|
---|
2653 | xorps @tweak[0],$inout0
|
---|
2654 | movups 16*3($inp),$inout3
|
---|
2655 | lea 16*4($inp),$inp # $inp+=4*16
|
---|
2656 | xorps @tweak[1],$inout1
|
---|
2657 | xorps @tweak[2],$inout2
|
---|
2658 | xorps @tweak[3],$inout3
|
---|
2659 |
|
---|
2660 | call _aesni_decrypt4
|
---|
2661 |
|
---|
2662 | pxor @tweak[0],$inout0
|
---|
2663 | movdqa @tweak[4],@tweak[0]
|
---|
2664 | pxor @tweak[1],$inout1
|
---|
2665 | movdqa @tweak[5],@tweak[1]
|
---|
2666 | pxor @tweak[2],$inout2
|
---|
2667 | movdqu $inout0,($out) # store 4 output blocks
|
---|
2668 | pxor @tweak[3],$inout3
|
---|
2669 | movdqu $inout1,16*1($out)
|
---|
2670 | movdqu $inout2,16*2($out)
|
---|
2671 | movdqu $inout3,16*3($out)
|
---|
2672 | lea 16*4($out),$out # $out+=4*16
|
---|
2673 | jmp .Lxts_dec_done
|
---|
2674 |
|
---|
2675 | .align 16
|
---|
2676 | .Lxts_dec_done:
|
---|
2677 | and \$15,$len_ # see if $len%16 is 0
|
---|
2678 | jz .Lxts_dec_ret
|
---|
2679 | .Lxts_dec_done2:
|
---|
2680 | mov $len_,$len
|
---|
2681 | mov $key_,$key # restore $key
|
---|
2682 | mov $rnds_,$rounds # restore $rounds
|
---|
2683 |
|
---|
2684 | movups ($inp),$inout0
|
---|
2685 | xorps @tweak[1],$inout0
|
---|
2686 | ___
|
---|
2687 | &aesni_generate1("dec",$key,$rounds);
|
---|
2688 | $code.=<<___;
|
---|
2689 | xorps @tweak[1],$inout0
|
---|
2690 | movups $inout0,($out)
|
---|
2691 |
|
---|
2692 | .Lxts_dec_steal:
|
---|
2693 | movzb 16($inp),%eax # borrow $rounds ...
|
---|
2694 | movzb ($out),%ecx # ... and $key
|
---|
2695 | lea 1($inp),$inp
|
---|
2696 | mov %al,($out)
|
---|
2697 | mov %cl,16($out)
|
---|
2698 | lea 1($out),$out
|
---|
2699 | sub \$1,$len
|
---|
2700 | jnz .Lxts_dec_steal
|
---|
2701 |
|
---|
2702 | sub $len_,$out # rewind $out
|
---|
2703 | mov $key_,$key # restore $key
|
---|
2704 | mov $rnds_,$rounds # restore $rounds
|
---|
2705 |
|
---|
2706 | movups ($out),$inout0
|
---|
2707 | xorps @tweak[0],$inout0
|
---|
2708 | ___
|
---|
2709 | &aesni_generate1("dec",$key,$rounds);
|
---|
2710 | $code.=<<___;
|
---|
2711 | xorps @tweak[0],$inout0
|
---|
2712 | movups $inout0,($out)
|
---|
2713 |
|
---|
2714 | .Lxts_dec_ret:
|
---|
2715 | xorps %xmm0,%xmm0 # clear register bank
|
---|
2716 | pxor %xmm1,%xmm1
|
---|
2717 | pxor %xmm2,%xmm2
|
---|
2718 | pxor %xmm3,%xmm3
|
---|
2719 | pxor %xmm4,%xmm4
|
---|
2720 | pxor %xmm5,%xmm5
|
---|
2721 | ___
|
---|
2722 | $code.=<<___ if (!$win64);
|
---|
2723 | pxor %xmm6,%xmm6
|
---|
2724 | pxor %xmm7,%xmm7
|
---|
2725 | movaps %xmm0,0x00(%rsp) # clear stack
|
---|
2726 | pxor %xmm8,%xmm8
|
---|
2727 | movaps %xmm0,0x10(%rsp)
|
---|
2728 | pxor %xmm9,%xmm9
|
---|
2729 | movaps %xmm0,0x20(%rsp)
|
---|
2730 | pxor %xmm10,%xmm10
|
---|
2731 | movaps %xmm0,0x30(%rsp)
|
---|
2732 | pxor %xmm11,%xmm11
|
---|
2733 | movaps %xmm0,0x40(%rsp)
|
---|
2734 | pxor %xmm12,%xmm12
|
---|
2735 | movaps %xmm0,0x50(%rsp)
|
---|
2736 | pxor %xmm13,%xmm13
|
---|
2737 | movaps %xmm0,0x60(%rsp)
|
---|
2738 | pxor %xmm14,%xmm14
|
---|
2739 | pxor %xmm15,%xmm15
|
---|
2740 | ___
|
---|
2741 | $code.=<<___ if ($win64);
|
---|
2742 | movaps -0xa8(%r11),%xmm6
|
---|
2743 | movaps %xmm0,-0xa8(%r11) # clear stack
|
---|
2744 | movaps -0x98(%r11),%xmm7
|
---|
2745 | movaps %xmm0,-0x98(%r11)
|
---|
2746 | movaps -0x88(%r11),%xmm8
|
---|
2747 | movaps %xmm0,-0x88(%r11)
|
---|
2748 | movaps -0x78(%r11),%xmm9
|
---|
2749 | movaps %xmm0,-0x78(%r11)
|
---|
2750 | movaps -0x68(%r11),%xmm10
|
---|
2751 | movaps %xmm0,-0x68(%r11)
|
---|
2752 | movaps -0x58(%r11),%xmm11
|
---|
2753 | movaps %xmm0,-0x58(%r11)
|
---|
2754 | movaps -0x48(%r11),%xmm12
|
---|
2755 | movaps %xmm0,-0x48(%r11)
|
---|
2756 | movaps -0x38(%r11),%xmm13
|
---|
2757 | movaps %xmm0,-0x38(%r11)
|
---|
2758 | movaps -0x28(%r11),%xmm14
|
---|
2759 | movaps %xmm0,-0x28(%r11)
|
---|
2760 | movaps -0x18(%r11),%xmm15
|
---|
2761 | movaps %xmm0,-0x18(%r11)
|
---|
2762 | movaps %xmm0,0x00(%rsp)
|
---|
2763 | movaps %xmm0,0x10(%rsp)
|
---|
2764 | movaps %xmm0,0x20(%rsp)
|
---|
2765 | movaps %xmm0,0x30(%rsp)
|
---|
2766 | movaps %xmm0,0x40(%rsp)
|
---|
2767 | movaps %xmm0,0x50(%rsp)
|
---|
2768 | movaps %xmm0,0x60(%rsp)
|
---|
2769 | ___
|
---|
2770 | $code.=<<___;
|
---|
2771 | mov -8(%r11),%rbp
|
---|
2772 | .cfi_restore %rbp
|
---|
2773 | lea (%r11),%rsp
|
---|
2774 | .cfi_def_cfa_register %rsp
|
---|
2775 | .Lxts_dec_epilogue:
|
---|
2776 | ret
|
---|
2777 | .cfi_endproc
|
---|
2778 | .size aesni_xts_decrypt,.-aesni_xts_decrypt
|
---|
2779 | ___
|
---|
2780 | }
|
---|
2781 | |
---|
2782 |
|
---|
2783 | ######################################################################
|
---|
2784 | # void aesni_ocb_[en|de]crypt(const char *inp, char *out, size_t blocks,
|
---|
2785 | # const AES_KEY *key, unsigned int start_block_num,
|
---|
2786 | # unsigned char offset_i[16], const unsigned char L_[][16],
|
---|
2787 | # unsigned char checksum[16]);
|
---|
2788 | #
|
---|
2789 | {
|
---|
2790 | my @offset=map("%xmm$_",(10..15));
|
---|
2791 | my ($checksum,$rndkey0l)=("%xmm8","%xmm9");
|
---|
2792 | my ($block_num,$offset_p)=("%r8","%r9"); # 5th and 6th arguments
|
---|
2793 | my ($L_p,$checksum_p) = ("%rbx","%rbp");
|
---|
2794 | my ($i1,$i3,$i5) = ("%r12","%r13","%r14");
|
---|
2795 | my $seventh_arg = $win64 ? 56 : 8;
|
---|
2796 | my $blocks = $len;
|
---|
2797 |
|
---|
2798 | $code.=<<___;
|
---|
2799 | .globl aesni_ocb_encrypt
|
---|
2800 | .type aesni_ocb_encrypt,\@function,6
|
---|
2801 | .align 32
|
---|
2802 | aesni_ocb_encrypt:
|
---|
2803 | .cfi_startproc
|
---|
2804 | endbranch
|
---|
2805 | lea (%rsp),%rax
|
---|
2806 | push %rbx
|
---|
2807 | .cfi_push %rbx
|
---|
2808 | push %rbp
|
---|
2809 | .cfi_push %rbp
|
---|
2810 | push %r12
|
---|
2811 | .cfi_push %r12
|
---|
2812 | push %r13
|
---|
2813 | .cfi_push %r13
|
---|
2814 | push %r14
|
---|
2815 | .cfi_push %r14
|
---|
2816 | ___
|
---|
2817 | $code.=<<___ if ($win64);
|
---|
2818 | lea -0xa0(%rsp),%rsp
|
---|
2819 | movaps %xmm6,0x00(%rsp) # offload everything
|
---|
2820 | movaps %xmm7,0x10(%rsp)
|
---|
2821 | movaps %xmm8,0x20(%rsp)
|
---|
2822 | movaps %xmm9,0x30(%rsp)
|
---|
2823 | movaps %xmm10,0x40(%rsp)
|
---|
2824 | movaps %xmm11,0x50(%rsp)
|
---|
2825 | movaps %xmm12,0x60(%rsp)
|
---|
2826 | movaps %xmm13,0x70(%rsp)
|
---|
2827 | movaps %xmm14,0x80(%rsp)
|
---|
2828 | movaps %xmm15,0x90(%rsp)
|
---|
2829 | .Locb_enc_body:
|
---|
2830 | ___
|
---|
2831 | $code.=<<___;
|
---|
2832 | mov $seventh_arg(%rax),$L_p # 7th argument
|
---|
2833 | mov $seventh_arg+8(%rax),$checksum_p# 8th argument
|
---|
2834 |
|
---|
2835 | mov 240($key),$rnds_
|
---|
2836 | mov $key,$key_
|
---|
2837 | shl \$4,$rnds_
|
---|
2838 | $movkey ($key),$rndkey0l # round[0]
|
---|
2839 | $movkey 16($key,$rnds_),$rndkey1 # round[last]
|
---|
2840 |
|
---|
2841 | movdqu ($offset_p),@offset[5] # load last offset_i
|
---|
2842 | pxor $rndkey1,$rndkey0l # round[0] ^ round[last]
|
---|
2843 | pxor $rndkey1,@offset[5] # offset_i ^ round[last]
|
---|
2844 |
|
---|
2845 | mov \$16+32,$rounds
|
---|
2846 | lea 32($key_,$rnds_),$key
|
---|
2847 | $movkey 16($key_),$rndkey1 # round[1]
|
---|
2848 | sub %r10,%rax # twisted $rounds
|
---|
2849 | mov %rax,%r10 # backup twisted $rounds
|
---|
2850 |
|
---|
2851 | movdqu ($L_p),@offset[0] # L_0 for all odd-numbered blocks
|
---|
2852 | movdqu ($checksum_p),$checksum # load checksum
|
---|
2853 |
|
---|
2854 | test \$1,$block_num # is first block number odd?
|
---|
2855 | jnz .Locb_enc_odd
|
---|
2856 |
|
---|
2857 | bsf $block_num,$i1
|
---|
2858 | add \$1,$block_num
|
---|
2859 | shl \$4,$i1
|
---|
2860 | movdqu ($L_p,$i1),$inout5 # borrow
|
---|
2861 | movdqu ($inp),$inout0
|
---|
2862 | lea 16($inp),$inp
|
---|
2863 |
|
---|
2864 | call __ocb_encrypt1
|
---|
2865 |
|
---|
2866 | movdqa $inout5,@offset[5]
|
---|
2867 | movups $inout0,($out)
|
---|
2868 | lea 16($out),$out
|
---|
2869 | sub \$1,$blocks
|
---|
2870 | jz .Locb_enc_done
|
---|
2871 |
|
---|
2872 | .Locb_enc_odd:
|
---|
2873 | lea 1($block_num),$i1 # even-numbered blocks
|
---|
2874 | lea 3($block_num),$i3
|
---|
2875 | lea 5($block_num),$i5
|
---|
2876 | lea 6($block_num),$block_num
|
---|
2877 | bsf $i1,$i1 # ntz(block)
|
---|
2878 | bsf $i3,$i3
|
---|
2879 | bsf $i5,$i5
|
---|
2880 | shl \$4,$i1 # ntz(block) -> table offset
|
---|
2881 | shl \$4,$i3
|
---|
2882 | shl \$4,$i5
|
---|
2883 |
|
---|
2884 | sub \$6,$blocks
|
---|
2885 | jc .Locb_enc_short
|
---|
2886 | jmp .Locb_enc_grandloop
|
---|
2887 |
|
---|
2888 | .align 32
|
---|
2889 | .Locb_enc_grandloop:
|
---|
2890 | movdqu `16*0`($inp),$inout0 # load input
|
---|
2891 | movdqu `16*1`($inp),$inout1
|
---|
2892 | movdqu `16*2`($inp),$inout2
|
---|
2893 | movdqu `16*3`($inp),$inout3
|
---|
2894 | movdqu `16*4`($inp),$inout4
|
---|
2895 | movdqu `16*5`($inp),$inout5
|
---|
2896 | lea `16*6`($inp),$inp
|
---|
2897 |
|
---|
2898 | call __ocb_encrypt6
|
---|
2899 |
|
---|
2900 | movups $inout0,`16*0`($out) # store output
|
---|
2901 | movups $inout1,`16*1`($out)
|
---|
2902 | movups $inout2,`16*2`($out)
|
---|
2903 | movups $inout3,`16*3`($out)
|
---|
2904 | movups $inout4,`16*4`($out)
|
---|
2905 | movups $inout5,`16*5`($out)
|
---|
2906 | lea `16*6`($out),$out
|
---|
2907 | sub \$6,$blocks
|
---|
2908 | jnc .Locb_enc_grandloop
|
---|
2909 |
|
---|
2910 | .Locb_enc_short:
|
---|
2911 | add \$6,$blocks
|
---|
2912 | jz .Locb_enc_done
|
---|
2913 |
|
---|
2914 | movdqu `16*0`($inp),$inout0
|
---|
2915 | cmp \$2,$blocks
|
---|
2916 | jb .Locb_enc_one
|
---|
2917 | movdqu `16*1`($inp),$inout1
|
---|
2918 | je .Locb_enc_two
|
---|
2919 |
|
---|
2920 | movdqu `16*2`($inp),$inout2
|
---|
2921 | cmp \$4,$blocks
|
---|
2922 | jb .Locb_enc_three
|
---|
2923 | movdqu `16*3`($inp),$inout3
|
---|
2924 | je .Locb_enc_four
|
---|
2925 |
|
---|
2926 | movdqu `16*4`($inp),$inout4
|
---|
2927 | pxor $inout5,$inout5
|
---|
2928 |
|
---|
2929 | call __ocb_encrypt6
|
---|
2930 |
|
---|
2931 | movdqa @offset[4],@offset[5]
|
---|
2932 | movups $inout0,`16*0`($out)
|
---|
2933 | movups $inout1,`16*1`($out)
|
---|
2934 | movups $inout2,`16*2`($out)
|
---|
2935 | movups $inout3,`16*3`($out)
|
---|
2936 | movups $inout4,`16*4`($out)
|
---|
2937 |
|
---|
2938 | jmp .Locb_enc_done
|
---|
2939 |
|
---|
2940 | .align 16
|
---|
2941 | .Locb_enc_one:
|
---|
2942 | movdqa @offset[0],$inout5 # borrow
|
---|
2943 |
|
---|
2944 | call __ocb_encrypt1
|
---|
2945 |
|
---|
2946 | movdqa $inout5,@offset[5]
|
---|
2947 | movups $inout0,`16*0`($out)
|
---|
2948 | jmp .Locb_enc_done
|
---|
2949 |
|
---|
2950 | .align 16
|
---|
2951 | .Locb_enc_two:
|
---|
2952 | pxor $inout2,$inout2
|
---|
2953 | pxor $inout3,$inout3
|
---|
2954 |
|
---|
2955 | call __ocb_encrypt4
|
---|
2956 |
|
---|
2957 | movdqa @offset[1],@offset[5]
|
---|
2958 | movups $inout0,`16*0`($out)
|
---|
2959 | movups $inout1,`16*1`($out)
|
---|
2960 |
|
---|
2961 | jmp .Locb_enc_done
|
---|
2962 |
|
---|
2963 | .align 16
|
---|
2964 | .Locb_enc_three:
|
---|
2965 | pxor $inout3,$inout3
|
---|
2966 |
|
---|
2967 | call __ocb_encrypt4
|
---|
2968 |
|
---|
2969 | movdqa @offset[2],@offset[5]
|
---|
2970 | movups $inout0,`16*0`($out)
|
---|
2971 | movups $inout1,`16*1`($out)
|
---|
2972 | movups $inout2,`16*2`($out)
|
---|
2973 |
|
---|
2974 | jmp .Locb_enc_done
|
---|
2975 |
|
---|
2976 | .align 16
|
---|
2977 | .Locb_enc_four:
|
---|
2978 | call __ocb_encrypt4
|
---|
2979 |
|
---|
2980 | movdqa @offset[3],@offset[5]
|
---|
2981 | movups $inout0,`16*0`($out)
|
---|
2982 | movups $inout1,`16*1`($out)
|
---|
2983 | movups $inout2,`16*2`($out)
|
---|
2984 | movups $inout3,`16*3`($out)
|
---|
2985 |
|
---|
2986 | .Locb_enc_done:
|
---|
2987 | pxor $rndkey0,@offset[5] # "remove" round[last]
|
---|
2988 | movdqu $checksum,($checksum_p) # store checksum
|
---|
2989 | movdqu @offset[5],($offset_p) # store last offset_i
|
---|
2990 |
|
---|
2991 | xorps %xmm0,%xmm0 # clear register bank
|
---|
2992 | pxor %xmm1,%xmm1
|
---|
2993 | pxor %xmm2,%xmm2
|
---|
2994 | pxor %xmm3,%xmm3
|
---|
2995 | pxor %xmm4,%xmm4
|
---|
2996 | pxor %xmm5,%xmm5
|
---|
2997 | ___
|
---|
2998 | $code.=<<___ if (!$win64);
|
---|
2999 | pxor %xmm6,%xmm6
|
---|
3000 | pxor %xmm7,%xmm7
|
---|
3001 | pxor %xmm8,%xmm8
|
---|
3002 | pxor %xmm9,%xmm9
|
---|
3003 | pxor %xmm10,%xmm10
|
---|
3004 | pxor %xmm11,%xmm11
|
---|
3005 | pxor %xmm12,%xmm12
|
---|
3006 | pxor %xmm13,%xmm13
|
---|
3007 | pxor %xmm14,%xmm14
|
---|
3008 | pxor %xmm15,%xmm15
|
---|
3009 | lea 0x28(%rsp),%rax
|
---|
3010 | .cfi_def_cfa %rax,8
|
---|
3011 | ___
|
---|
3012 | $code.=<<___ if ($win64);
|
---|
3013 | movaps 0x00(%rsp),%xmm6
|
---|
3014 | movaps %xmm0,0x00(%rsp) # clear stack
|
---|
3015 | movaps 0x10(%rsp),%xmm7
|
---|
3016 | movaps %xmm0,0x10(%rsp)
|
---|
3017 | movaps 0x20(%rsp),%xmm8
|
---|
3018 | movaps %xmm0,0x20(%rsp)
|
---|
3019 | movaps 0x30(%rsp),%xmm9
|
---|
3020 | movaps %xmm0,0x30(%rsp)
|
---|
3021 | movaps 0x40(%rsp),%xmm10
|
---|
3022 | movaps %xmm0,0x40(%rsp)
|
---|
3023 | movaps 0x50(%rsp),%xmm11
|
---|
3024 | movaps %xmm0,0x50(%rsp)
|
---|
3025 | movaps 0x60(%rsp),%xmm12
|
---|
3026 | movaps %xmm0,0x60(%rsp)
|
---|
3027 | movaps 0x70(%rsp),%xmm13
|
---|
3028 | movaps %xmm0,0x70(%rsp)
|
---|
3029 | movaps 0x80(%rsp),%xmm14
|
---|
3030 | movaps %xmm0,0x80(%rsp)
|
---|
3031 | movaps 0x90(%rsp),%xmm15
|
---|
3032 | movaps %xmm0,0x90(%rsp)
|
---|
3033 | lea 0xa0+0x28(%rsp),%rax
|
---|
3034 | .Locb_enc_pop:
|
---|
3035 | ___
|
---|
3036 | $code.=<<___;
|
---|
3037 | mov -40(%rax),%r14
|
---|
3038 | .cfi_restore %r14
|
---|
3039 | mov -32(%rax),%r13
|
---|
3040 | .cfi_restore %r13
|
---|
3041 | mov -24(%rax),%r12
|
---|
3042 | .cfi_restore %r12
|
---|
3043 | mov -16(%rax),%rbp
|
---|
3044 | .cfi_restore %rbp
|
---|
3045 | mov -8(%rax),%rbx
|
---|
3046 | .cfi_restore %rbx
|
---|
3047 | lea (%rax),%rsp
|
---|
3048 | .cfi_def_cfa_register %rsp
|
---|
3049 | .Locb_enc_epilogue:
|
---|
3050 | ret
|
---|
3051 | .cfi_endproc
|
---|
3052 | .size aesni_ocb_encrypt,.-aesni_ocb_encrypt
|
---|
3053 |
|
---|
3054 | .type __ocb_encrypt6,\@abi-omnipotent
|
---|
3055 | .align 32
|
---|
3056 | __ocb_encrypt6:
|
---|
3057 | .cfi_startproc
|
---|
3058 | pxor $rndkey0l,@offset[5] # offset_i ^ round[0]
|
---|
3059 | movdqu ($L_p,$i1),@offset[1]
|
---|
3060 | movdqa @offset[0],@offset[2]
|
---|
3061 | movdqu ($L_p,$i3),@offset[3]
|
---|
3062 | movdqa @offset[0],@offset[4]
|
---|
3063 | pxor @offset[5],@offset[0]
|
---|
3064 | movdqu ($L_p,$i5),@offset[5]
|
---|
3065 | pxor @offset[0],@offset[1]
|
---|
3066 | pxor $inout0,$checksum # accumulate checksum
|
---|
3067 | pxor @offset[0],$inout0 # input ^ round[0] ^ offset_i
|
---|
3068 | pxor @offset[1],@offset[2]
|
---|
3069 | pxor $inout1,$checksum
|
---|
3070 | pxor @offset[1],$inout1
|
---|
3071 | pxor @offset[2],@offset[3]
|
---|
3072 | pxor $inout2,$checksum
|
---|
3073 | pxor @offset[2],$inout2
|
---|
3074 | pxor @offset[3],@offset[4]
|
---|
3075 | pxor $inout3,$checksum
|
---|
3076 | pxor @offset[3],$inout3
|
---|
3077 | pxor @offset[4],@offset[5]
|
---|
3078 | pxor $inout4,$checksum
|
---|
3079 | pxor @offset[4],$inout4
|
---|
3080 | pxor $inout5,$checksum
|
---|
3081 | pxor @offset[5],$inout5
|
---|
3082 | $movkey 32($key_),$rndkey0
|
---|
3083 |
|
---|
3084 | lea 1($block_num),$i1 # even-numbered blocks
|
---|
3085 | lea 3($block_num),$i3
|
---|
3086 | lea 5($block_num),$i5
|
---|
3087 | add \$6,$block_num
|
---|
3088 | pxor $rndkey0l,@offset[0] # offset_i ^ round[last]
|
---|
3089 | bsf $i1,$i1 # ntz(block)
|
---|
3090 | bsf $i3,$i3
|
---|
3091 | bsf $i5,$i5
|
---|
3092 |
|
---|
3093 | aesenc $rndkey1,$inout0
|
---|
3094 | aesenc $rndkey1,$inout1
|
---|
3095 | aesenc $rndkey1,$inout2
|
---|
3096 | aesenc $rndkey1,$inout3
|
---|
3097 | pxor $rndkey0l,@offset[1]
|
---|
3098 | pxor $rndkey0l,@offset[2]
|
---|
3099 | aesenc $rndkey1,$inout4
|
---|
3100 | pxor $rndkey0l,@offset[3]
|
---|
3101 | pxor $rndkey0l,@offset[4]
|
---|
3102 | aesenc $rndkey1,$inout5
|
---|
3103 | $movkey 48($key_),$rndkey1
|
---|
3104 | pxor $rndkey0l,@offset[5]
|
---|
3105 |
|
---|
3106 | aesenc $rndkey0,$inout0
|
---|
3107 | aesenc $rndkey0,$inout1
|
---|
3108 | aesenc $rndkey0,$inout2
|
---|
3109 | aesenc $rndkey0,$inout3
|
---|
3110 | aesenc $rndkey0,$inout4
|
---|
3111 | aesenc $rndkey0,$inout5
|
---|
3112 | $movkey 64($key_),$rndkey0
|
---|
3113 | shl \$4,$i1 # ntz(block) -> table offset
|
---|
3114 | shl \$4,$i3
|
---|
3115 | jmp .Locb_enc_loop6
|
---|
3116 |
|
---|
3117 | .align 32
|
---|
3118 | .Locb_enc_loop6:
|
---|
3119 | aesenc $rndkey1,$inout0
|
---|
3120 | aesenc $rndkey1,$inout1
|
---|
3121 | aesenc $rndkey1,$inout2
|
---|
3122 | aesenc $rndkey1,$inout3
|
---|
3123 | aesenc $rndkey1,$inout4
|
---|
3124 | aesenc $rndkey1,$inout5
|
---|
3125 | $movkey ($key,%rax),$rndkey1
|
---|
3126 | add \$32,%rax
|
---|
3127 |
|
---|
3128 | aesenc $rndkey0,$inout0
|
---|
3129 | aesenc $rndkey0,$inout1
|
---|
3130 | aesenc $rndkey0,$inout2
|
---|
3131 | aesenc $rndkey0,$inout3
|
---|
3132 | aesenc $rndkey0,$inout4
|
---|
3133 | aesenc $rndkey0,$inout5
|
---|
3134 | $movkey -16($key,%rax),$rndkey0
|
---|
3135 | jnz .Locb_enc_loop6
|
---|
3136 |
|
---|
3137 | aesenc $rndkey1,$inout0
|
---|
3138 | aesenc $rndkey1,$inout1
|
---|
3139 | aesenc $rndkey1,$inout2
|
---|
3140 | aesenc $rndkey1,$inout3
|
---|
3141 | aesenc $rndkey1,$inout4
|
---|
3142 | aesenc $rndkey1,$inout5
|
---|
3143 | $movkey 16($key_),$rndkey1
|
---|
3144 | shl \$4,$i5
|
---|
3145 |
|
---|
3146 | aesenclast @offset[0],$inout0
|
---|
3147 | movdqu ($L_p),@offset[0] # L_0 for all odd-numbered blocks
|
---|
3148 | mov %r10,%rax # restore twisted rounds
|
---|
3149 | aesenclast @offset[1],$inout1
|
---|
3150 | aesenclast @offset[2],$inout2
|
---|
3151 | aesenclast @offset[3],$inout3
|
---|
3152 | aesenclast @offset[4],$inout4
|
---|
3153 | aesenclast @offset[5],$inout5
|
---|
3154 | ret
|
---|
3155 | .cfi_endproc
|
---|
3156 | .size __ocb_encrypt6,.-__ocb_encrypt6
|
---|
3157 |
|
---|
3158 | .type __ocb_encrypt4,\@abi-omnipotent
|
---|
3159 | .align 32
|
---|
3160 | __ocb_encrypt4:
|
---|
3161 | .cfi_startproc
|
---|
3162 | pxor $rndkey0l,@offset[5] # offset_i ^ round[0]
|
---|
3163 | movdqu ($L_p,$i1),@offset[1]
|
---|
3164 | movdqa @offset[0],@offset[2]
|
---|
3165 | movdqu ($L_p,$i3),@offset[3]
|
---|
3166 | pxor @offset[5],@offset[0]
|
---|
3167 | pxor @offset[0],@offset[1]
|
---|
3168 | pxor $inout0,$checksum # accumulate checksum
|
---|
3169 | pxor @offset[0],$inout0 # input ^ round[0] ^ offset_i
|
---|
3170 | pxor @offset[1],@offset[2]
|
---|
3171 | pxor $inout1,$checksum
|
---|
3172 | pxor @offset[1],$inout1
|
---|
3173 | pxor @offset[2],@offset[3]
|
---|
3174 | pxor $inout2,$checksum
|
---|
3175 | pxor @offset[2],$inout2
|
---|
3176 | pxor $inout3,$checksum
|
---|
3177 | pxor @offset[3],$inout3
|
---|
3178 | $movkey 32($key_),$rndkey0
|
---|
3179 |
|
---|
3180 | pxor $rndkey0l,@offset[0] # offset_i ^ round[last]
|
---|
3181 | pxor $rndkey0l,@offset[1]
|
---|
3182 | pxor $rndkey0l,@offset[2]
|
---|
3183 | pxor $rndkey0l,@offset[3]
|
---|
3184 |
|
---|
3185 | aesenc $rndkey1,$inout0
|
---|
3186 | aesenc $rndkey1,$inout1
|
---|
3187 | aesenc $rndkey1,$inout2
|
---|
3188 | aesenc $rndkey1,$inout3
|
---|
3189 | $movkey 48($key_),$rndkey1
|
---|
3190 |
|
---|
3191 | aesenc $rndkey0,$inout0
|
---|
3192 | aesenc $rndkey0,$inout1
|
---|
3193 | aesenc $rndkey0,$inout2
|
---|
3194 | aesenc $rndkey0,$inout3
|
---|
3195 | $movkey 64($key_),$rndkey0
|
---|
3196 | jmp .Locb_enc_loop4
|
---|
3197 |
|
---|
3198 | .align 32
|
---|
3199 | .Locb_enc_loop4:
|
---|
3200 | aesenc $rndkey1,$inout0
|
---|
3201 | aesenc $rndkey1,$inout1
|
---|
3202 | aesenc $rndkey1,$inout2
|
---|
3203 | aesenc $rndkey1,$inout3
|
---|
3204 | $movkey ($key,%rax),$rndkey1
|
---|
3205 | add \$32,%rax
|
---|
3206 |
|
---|
3207 | aesenc $rndkey0,$inout0
|
---|
3208 | aesenc $rndkey0,$inout1
|
---|
3209 | aesenc $rndkey0,$inout2
|
---|
3210 | aesenc $rndkey0,$inout3
|
---|
3211 | $movkey -16($key,%rax),$rndkey0
|
---|
3212 | jnz .Locb_enc_loop4
|
---|
3213 |
|
---|
3214 | aesenc $rndkey1,$inout0
|
---|
3215 | aesenc $rndkey1,$inout1
|
---|
3216 | aesenc $rndkey1,$inout2
|
---|
3217 | aesenc $rndkey1,$inout3
|
---|
3218 | $movkey 16($key_),$rndkey1
|
---|
3219 | mov %r10,%rax # restore twisted rounds
|
---|
3220 |
|
---|
3221 | aesenclast @offset[0],$inout0
|
---|
3222 | aesenclast @offset[1],$inout1
|
---|
3223 | aesenclast @offset[2],$inout2
|
---|
3224 | aesenclast @offset[3],$inout3
|
---|
3225 | ret
|
---|
3226 | .cfi_endproc
|
---|
3227 | .size __ocb_encrypt4,.-__ocb_encrypt4
|
---|
3228 |
|
---|
3229 | .type __ocb_encrypt1,\@abi-omnipotent
|
---|
3230 | .align 32
|
---|
3231 | __ocb_encrypt1:
|
---|
3232 | .cfi_startproc
|
---|
3233 | pxor @offset[5],$inout5 # offset_i
|
---|
3234 | pxor $rndkey0l,$inout5 # offset_i ^ round[0]
|
---|
3235 | pxor $inout0,$checksum # accumulate checksum
|
---|
3236 | pxor $inout5,$inout0 # input ^ round[0] ^ offset_i
|
---|
3237 | $movkey 32($key_),$rndkey0
|
---|
3238 |
|
---|
3239 | aesenc $rndkey1,$inout0
|
---|
3240 | $movkey 48($key_),$rndkey1
|
---|
3241 | pxor $rndkey0l,$inout5 # offset_i ^ round[last]
|
---|
3242 |
|
---|
3243 | aesenc $rndkey0,$inout0
|
---|
3244 | $movkey 64($key_),$rndkey0
|
---|
3245 | jmp .Locb_enc_loop1
|
---|
3246 |
|
---|
3247 | .align 32
|
---|
3248 | .Locb_enc_loop1:
|
---|
3249 | aesenc $rndkey1,$inout0
|
---|
3250 | $movkey ($key,%rax),$rndkey1
|
---|
3251 | add \$32,%rax
|
---|
3252 |
|
---|
3253 | aesenc $rndkey0,$inout0
|
---|
3254 | $movkey -16($key,%rax),$rndkey0
|
---|
3255 | jnz .Locb_enc_loop1
|
---|
3256 |
|
---|
3257 | aesenc $rndkey1,$inout0
|
---|
3258 | $movkey 16($key_),$rndkey1 # redundant in tail
|
---|
3259 | mov %r10,%rax # restore twisted rounds
|
---|
3260 |
|
---|
3261 | aesenclast $inout5,$inout0
|
---|
3262 | ret
|
---|
3263 | .cfi_endproc
|
---|
3264 | .size __ocb_encrypt1,.-__ocb_encrypt1
|
---|
3265 |
|
---|
3266 | .globl aesni_ocb_decrypt
|
---|
3267 | .type aesni_ocb_decrypt,\@function,6
|
---|
3268 | .align 32
|
---|
3269 | aesni_ocb_decrypt:
|
---|
3270 | .cfi_startproc
|
---|
3271 | endbranch
|
---|
3272 | lea (%rsp),%rax
|
---|
3273 | push %rbx
|
---|
3274 | .cfi_push %rbx
|
---|
3275 | push %rbp
|
---|
3276 | .cfi_push %rbp
|
---|
3277 | push %r12
|
---|
3278 | .cfi_push %r12
|
---|
3279 | push %r13
|
---|
3280 | .cfi_push %r13
|
---|
3281 | push %r14
|
---|
3282 | .cfi_push %r14
|
---|
3283 | ___
|
---|
3284 | $code.=<<___ if ($win64);
|
---|
3285 | lea -0xa0(%rsp),%rsp
|
---|
3286 | movaps %xmm6,0x00(%rsp) # offload everything
|
---|
3287 | movaps %xmm7,0x10(%rsp)
|
---|
3288 | movaps %xmm8,0x20(%rsp)
|
---|
3289 | movaps %xmm9,0x30(%rsp)
|
---|
3290 | movaps %xmm10,0x40(%rsp)
|
---|
3291 | movaps %xmm11,0x50(%rsp)
|
---|
3292 | movaps %xmm12,0x60(%rsp)
|
---|
3293 | movaps %xmm13,0x70(%rsp)
|
---|
3294 | movaps %xmm14,0x80(%rsp)
|
---|
3295 | movaps %xmm15,0x90(%rsp)
|
---|
3296 | .Locb_dec_body:
|
---|
3297 | ___
|
---|
3298 | $code.=<<___;
|
---|
3299 | mov $seventh_arg(%rax),$L_p # 7th argument
|
---|
3300 | mov $seventh_arg+8(%rax),$checksum_p# 8th argument
|
---|
3301 |
|
---|
3302 | mov 240($key),$rnds_
|
---|
3303 | mov $key,$key_
|
---|
3304 | shl \$4,$rnds_
|
---|
3305 | $movkey ($key),$rndkey0l # round[0]
|
---|
3306 | $movkey 16($key,$rnds_),$rndkey1 # round[last]
|
---|
3307 |
|
---|
3308 | movdqu ($offset_p),@offset[5] # load last offset_i
|
---|
3309 | pxor $rndkey1,$rndkey0l # round[0] ^ round[last]
|
---|
3310 | pxor $rndkey1,@offset[5] # offset_i ^ round[last]
|
---|
3311 |
|
---|
3312 | mov \$16+32,$rounds
|
---|
3313 | lea 32($key_,$rnds_),$key
|
---|
3314 | $movkey 16($key_),$rndkey1 # round[1]
|
---|
3315 | sub %r10,%rax # twisted $rounds
|
---|
3316 | mov %rax,%r10 # backup twisted $rounds
|
---|
3317 |
|
---|
3318 | movdqu ($L_p),@offset[0] # L_0 for all odd-numbered blocks
|
---|
3319 | movdqu ($checksum_p),$checksum # load checksum
|
---|
3320 |
|
---|
3321 | test \$1,$block_num # is first block number odd?
|
---|
3322 | jnz .Locb_dec_odd
|
---|
3323 |
|
---|
3324 | bsf $block_num,$i1
|
---|
3325 | add \$1,$block_num
|
---|
3326 | shl \$4,$i1
|
---|
3327 | movdqu ($L_p,$i1),$inout5 # borrow
|
---|
3328 | movdqu ($inp),$inout0
|
---|
3329 | lea 16($inp),$inp
|
---|
3330 |
|
---|
3331 | call __ocb_decrypt1
|
---|
3332 |
|
---|
3333 | movdqa $inout5,@offset[5]
|
---|
3334 | movups $inout0,($out)
|
---|
3335 | xorps $inout0,$checksum # accumulate checksum
|
---|
3336 | lea 16($out),$out
|
---|
3337 | sub \$1,$blocks
|
---|
3338 | jz .Locb_dec_done
|
---|
3339 |
|
---|
3340 | .Locb_dec_odd:
|
---|
3341 | lea 1($block_num),$i1 # even-numbered blocks
|
---|
3342 | lea 3($block_num),$i3
|
---|
3343 | lea 5($block_num),$i5
|
---|
3344 | lea 6($block_num),$block_num
|
---|
3345 | bsf $i1,$i1 # ntz(block)
|
---|
3346 | bsf $i3,$i3
|
---|
3347 | bsf $i5,$i5
|
---|
3348 | shl \$4,$i1 # ntz(block) -> table offset
|
---|
3349 | shl \$4,$i3
|
---|
3350 | shl \$4,$i5
|
---|
3351 |
|
---|
3352 | sub \$6,$blocks
|
---|
3353 | jc .Locb_dec_short
|
---|
3354 | jmp .Locb_dec_grandloop
|
---|
3355 |
|
---|
3356 | .align 32
|
---|
3357 | .Locb_dec_grandloop:
|
---|
3358 | movdqu `16*0`($inp),$inout0 # load input
|
---|
3359 | movdqu `16*1`($inp),$inout1
|
---|
3360 | movdqu `16*2`($inp),$inout2
|
---|
3361 | movdqu `16*3`($inp),$inout3
|
---|
3362 | movdqu `16*4`($inp),$inout4
|
---|
3363 | movdqu `16*5`($inp),$inout5
|
---|
3364 | lea `16*6`($inp),$inp
|
---|
3365 |
|
---|
3366 | call __ocb_decrypt6
|
---|
3367 |
|
---|
3368 | movups $inout0,`16*0`($out) # store output
|
---|
3369 | pxor $inout0,$checksum # accumulate checksum
|
---|
3370 | movups $inout1,`16*1`($out)
|
---|
3371 | pxor $inout1,$checksum
|
---|
3372 | movups $inout2,`16*2`($out)
|
---|
3373 | pxor $inout2,$checksum
|
---|
3374 | movups $inout3,`16*3`($out)
|
---|
3375 | pxor $inout3,$checksum
|
---|
3376 | movups $inout4,`16*4`($out)
|
---|
3377 | pxor $inout4,$checksum
|
---|
3378 | movups $inout5,`16*5`($out)
|
---|
3379 | pxor $inout5,$checksum
|
---|
3380 | lea `16*6`($out),$out
|
---|
3381 | sub \$6,$blocks
|
---|
3382 | jnc .Locb_dec_grandloop
|
---|
3383 |
|
---|
3384 | .Locb_dec_short:
|
---|
3385 | add \$6,$blocks
|
---|
3386 | jz .Locb_dec_done
|
---|
3387 |
|
---|
3388 | movdqu `16*0`($inp),$inout0
|
---|
3389 | cmp \$2,$blocks
|
---|
3390 | jb .Locb_dec_one
|
---|
3391 | movdqu `16*1`($inp),$inout1
|
---|
3392 | je .Locb_dec_two
|
---|
3393 |
|
---|
3394 | movdqu `16*2`($inp),$inout2
|
---|
3395 | cmp \$4,$blocks
|
---|
3396 | jb .Locb_dec_three
|
---|
3397 | movdqu `16*3`($inp),$inout3
|
---|
3398 | je .Locb_dec_four
|
---|
3399 |
|
---|
3400 | movdqu `16*4`($inp),$inout4
|
---|
3401 | pxor $inout5,$inout5
|
---|
3402 |
|
---|
3403 | call __ocb_decrypt6
|
---|
3404 |
|
---|
3405 | movdqa @offset[4],@offset[5]
|
---|
3406 | movups $inout0,`16*0`($out) # store output
|
---|
3407 | pxor $inout0,$checksum # accumulate checksum
|
---|
3408 | movups $inout1,`16*1`($out)
|
---|
3409 | pxor $inout1,$checksum
|
---|
3410 | movups $inout2,`16*2`($out)
|
---|
3411 | pxor $inout2,$checksum
|
---|
3412 | movups $inout3,`16*3`($out)
|
---|
3413 | pxor $inout3,$checksum
|
---|
3414 | movups $inout4,`16*4`($out)
|
---|
3415 | pxor $inout4,$checksum
|
---|
3416 |
|
---|
3417 | jmp .Locb_dec_done
|
---|
3418 |
|
---|
3419 | .align 16
|
---|
3420 | .Locb_dec_one:
|
---|
3421 | movdqa @offset[0],$inout5 # borrow
|
---|
3422 |
|
---|
3423 | call __ocb_decrypt1
|
---|
3424 |
|
---|
3425 | movdqa $inout5,@offset[5]
|
---|
3426 | movups $inout0,`16*0`($out) # store output
|
---|
3427 | xorps $inout0,$checksum # accumulate checksum
|
---|
3428 | jmp .Locb_dec_done
|
---|
3429 |
|
---|
3430 | .align 16
|
---|
3431 | .Locb_dec_two:
|
---|
3432 | pxor $inout2,$inout2
|
---|
3433 | pxor $inout3,$inout3
|
---|
3434 |
|
---|
3435 | call __ocb_decrypt4
|
---|
3436 |
|
---|
3437 | movdqa @offset[1],@offset[5]
|
---|
3438 | movups $inout0,`16*0`($out) # store output
|
---|
3439 | xorps $inout0,$checksum # accumulate checksum
|
---|
3440 | movups $inout1,`16*1`($out)
|
---|
3441 | xorps $inout1,$checksum
|
---|
3442 |
|
---|
3443 | jmp .Locb_dec_done
|
---|
3444 |
|
---|
3445 | .align 16
|
---|
3446 | .Locb_dec_three:
|
---|
3447 | pxor $inout3,$inout3
|
---|
3448 |
|
---|
3449 | call __ocb_decrypt4
|
---|
3450 |
|
---|
3451 | movdqa @offset[2],@offset[5]
|
---|
3452 | movups $inout0,`16*0`($out) # store output
|
---|
3453 | xorps $inout0,$checksum # accumulate checksum
|
---|
3454 | movups $inout1,`16*1`($out)
|
---|
3455 | xorps $inout1,$checksum
|
---|
3456 | movups $inout2,`16*2`($out)
|
---|
3457 | xorps $inout2,$checksum
|
---|
3458 |
|
---|
3459 | jmp .Locb_dec_done
|
---|
3460 |
|
---|
3461 | .align 16
|
---|
3462 | .Locb_dec_four:
|
---|
3463 | call __ocb_decrypt4
|
---|
3464 |
|
---|
3465 | movdqa @offset[3],@offset[5]
|
---|
3466 | movups $inout0,`16*0`($out) # store output
|
---|
3467 | pxor $inout0,$checksum # accumulate checksum
|
---|
3468 | movups $inout1,`16*1`($out)
|
---|
3469 | pxor $inout1,$checksum
|
---|
3470 | movups $inout2,`16*2`($out)
|
---|
3471 | pxor $inout2,$checksum
|
---|
3472 | movups $inout3,`16*3`($out)
|
---|
3473 | pxor $inout3,$checksum
|
---|
3474 |
|
---|
3475 | .Locb_dec_done:
|
---|
3476 | pxor $rndkey0,@offset[5] # "remove" round[last]
|
---|
3477 | movdqu $checksum,($checksum_p) # store checksum
|
---|
3478 | movdqu @offset[5],($offset_p) # store last offset_i
|
---|
3479 |
|
---|
3480 | xorps %xmm0,%xmm0 # clear register bank
|
---|
3481 | pxor %xmm1,%xmm1
|
---|
3482 | pxor %xmm2,%xmm2
|
---|
3483 | pxor %xmm3,%xmm3
|
---|
3484 | pxor %xmm4,%xmm4
|
---|
3485 | pxor %xmm5,%xmm5
|
---|
3486 | ___
|
---|
3487 | $code.=<<___ if (!$win64);
|
---|
3488 | pxor %xmm6,%xmm6
|
---|
3489 | pxor %xmm7,%xmm7
|
---|
3490 | pxor %xmm8,%xmm8
|
---|
3491 | pxor %xmm9,%xmm9
|
---|
3492 | pxor %xmm10,%xmm10
|
---|
3493 | pxor %xmm11,%xmm11
|
---|
3494 | pxor %xmm12,%xmm12
|
---|
3495 | pxor %xmm13,%xmm13
|
---|
3496 | pxor %xmm14,%xmm14
|
---|
3497 | pxor %xmm15,%xmm15
|
---|
3498 | lea 0x28(%rsp),%rax
|
---|
3499 | .cfi_def_cfa %rax,8
|
---|
3500 | ___
|
---|
3501 | $code.=<<___ if ($win64);
|
---|
3502 | movaps 0x00(%rsp),%xmm6
|
---|
3503 | movaps %xmm0,0x00(%rsp) # clear stack
|
---|
3504 | movaps 0x10(%rsp),%xmm7
|
---|
3505 | movaps %xmm0,0x10(%rsp)
|
---|
3506 | movaps 0x20(%rsp),%xmm8
|
---|
3507 | movaps %xmm0,0x20(%rsp)
|
---|
3508 | movaps 0x30(%rsp),%xmm9
|
---|
3509 | movaps %xmm0,0x30(%rsp)
|
---|
3510 | movaps 0x40(%rsp),%xmm10
|
---|
3511 | movaps %xmm0,0x40(%rsp)
|
---|
3512 | movaps 0x50(%rsp),%xmm11
|
---|
3513 | movaps %xmm0,0x50(%rsp)
|
---|
3514 | movaps 0x60(%rsp),%xmm12
|
---|
3515 | movaps %xmm0,0x60(%rsp)
|
---|
3516 | movaps 0x70(%rsp),%xmm13
|
---|
3517 | movaps %xmm0,0x70(%rsp)
|
---|
3518 | movaps 0x80(%rsp),%xmm14
|
---|
3519 | movaps %xmm0,0x80(%rsp)
|
---|
3520 | movaps 0x90(%rsp),%xmm15
|
---|
3521 | movaps %xmm0,0x90(%rsp)
|
---|
3522 | lea 0xa0+0x28(%rsp),%rax
|
---|
3523 | .Locb_dec_pop:
|
---|
3524 | ___
|
---|
3525 | $code.=<<___;
|
---|
3526 | mov -40(%rax),%r14
|
---|
3527 | .cfi_restore %r14
|
---|
3528 | mov -32(%rax),%r13
|
---|
3529 | .cfi_restore %r13
|
---|
3530 | mov -24(%rax),%r12
|
---|
3531 | .cfi_restore %r12
|
---|
3532 | mov -16(%rax),%rbp
|
---|
3533 | .cfi_restore %rbp
|
---|
3534 | mov -8(%rax),%rbx
|
---|
3535 | .cfi_restore %rbx
|
---|
3536 | lea (%rax),%rsp
|
---|
3537 | .cfi_def_cfa_register %rsp
|
---|
3538 | .Locb_dec_epilogue:
|
---|
3539 | ret
|
---|
3540 | .cfi_endproc
|
---|
3541 | .size aesni_ocb_decrypt,.-aesni_ocb_decrypt
|
---|
3542 |
|
---|
3543 | .type __ocb_decrypt6,\@abi-omnipotent
|
---|
3544 | .align 32
|
---|
3545 | __ocb_decrypt6:
|
---|
3546 | .cfi_startproc
|
---|
3547 | pxor $rndkey0l,@offset[5] # offset_i ^ round[0]
|
---|
3548 | movdqu ($L_p,$i1),@offset[1]
|
---|
3549 | movdqa @offset[0],@offset[2]
|
---|
3550 | movdqu ($L_p,$i3),@offset[3]
|
---|
3551 | movdqa @offset[0],@offset[4]
|
---|
3552 | pxor @offset[5],@offset[0]
|
---|
3553 | movdqu ($L_p,$i5),@offset[5]
|
---|
3554 | pxor @offset[0],@offset[1]
|
---|
3555 | pxor @offset[0],$inout0 # input ^ round[0] ^ offset_i
|
---|
3556 | pxor @offset[1],@offset[2]
|
---|
3557 | pxor @offset[1],$inout1
|
---|
3558 | pxor @offset[2],@offset[3]
|
---|
3559 | pxor @offset[2],$inout2
|
---|
3560 | pxor @offset[3],@offset[4]
|
---|
3561 | pxor @offset[3],$inout3
|
---|
3562 | pxor @offset[4],@offset[5]
|
---|
3563 | pxor @offset[4],$inout4
|
---|
3564 | pxor @offset[5],$inout5
|
---|
3565 | $movkey 32($key_),$rndkey0
|
---|
3566 |
|
---|
3567 | lea 1($block_num),$i1 # even-numbered blocks
|
---|
3568 | lea 3($block_num),$i3
|
---|
3569 | lea 5($block_num),$i5
|
---|
3570 | add \$6,$block_num
|
---|
3571 | pxor $rndkey0l,@offset[0] # offset_i ^ round[last]
|
---|
3572 | bsf $i1,$i1 # ntz(block)
|
---|
3573 | bsf $i3,$i3
|
---|
3574 | bsf $i5,$i5
|
---|
3575 |
|
---|
3576 | aesdec $rndkey1,$inout0
|
---|
3577 | aesdec $rndkey1,$inout1
|
---|
3578 | aesdec $rndkey1,$inout2
|
---|
3579 | aesdec $rndkey1,$inout3
|
---|
3580 | pxor $rndkey0l,@offset[1]
|
---|
3581 | pxor $rndkey0l,@offset[2]
|
---|
3582 | aesdec $rndkey1,$inout4
|
---|
3583 | pxor $rndkey0l,@offset[3]
|
---|
3584 | pxor $rndkey0l,@offset[4]
|
---|
3585 | aesdec $rndkey1,$inout5
|
---|
3586 | $movkey 48($key_),$rndkey1
|
---|
3587 | pxor $rndkey0l,@offset[5]
|
---|
3588 |
|
---|
3589 | aesdec $rndkey0,$inout0
|
---|
3590 | aesdec $rndkey0,$inout1
|
---|
3591 | aesdec $rndkey0,$inout2
|
---|
3592 | aesdec $rndkey0,$inout3
|
---|
3593 | aesdec $rndkey0,$inout4
|
---|
3594 | aesdec $rndkey0,$inout5
|
---|
3595 | $movkey 64($key_),$rndkey0
|
---|
3596 | shl \$4,$i1 # ntz(block) -> table offset
|
---|
3597 | shl \$4,$i3
|
---|
3598 | jmp .Locb_dec_loop6
|
---|
3599 |
|
---|
3600 | .align 32
|
---|
3601 | .Locb_dec_loop6:
|
---|
3602 | aesdec $rndkey1,$inout0
|
---|
3603 | aesdec $rndkey1,$inout1
|
---|
3604 | aesdec $rndkey1,$inout2
|
---|
3605 | aesdec $rndkey1,$inout3
|
---|
3606 | aesdec $rndkey1,$inout4
|
---|
3607 | aesdec $rndkey1,$inout5
|
---|
3608 | $movkey ($key,%rax),$rndkey1
|
---|
3609 | add \$32,%rax
|
---|
3610 |
|
---|
3611 | aesdec $rndkey0,$inout0
|
---|
3612 | aesdec $rndkey0,$inout1
|
---|
3613 | aesdec $rndkey0,$inout2
|
---|
3614 | aesdec $rndkey0,$inout3
|
---|
3615 | aesdec $rndkey0,$inout4
|
---|
3616 | aesdec $rndkey0,$inout5
|
---|
3617 | $movkey -16($key,%rax),$rndkey0
|
---|
3618 | jnz .Locb_dec_loop6
|
---|
3619 |
|
---|
3620 | aesdec $rndkey1,$inout0
|
---|
3621 | aesdec $rndkey1,$inout1
|
---|
3622 | aesdec $rndkey1,$inout2
|
---|
3623 | aesdec $rndkey1,$inout3
|
---|
3624 | aesdec $rndkey1,$inout4
|
---|
3625 | aesdec $rndkey1,$inout5
|
---|
3626 | $movkey 16($key_),$rndkey1
|
---|
3627 | shl \$4,$i5
|
---|
3628 |
|
---|
3629 | aesdeclast @offset[0],$inout0
|
---|
3630 | movdqu ($L_p),@offset[0] # L_0 for all odd-numbered blocks
|
---|
3631 | mov %r10,%rax # restore twisted rounds
|
---|
3632 | aesdeclast @offset[1],$inout1
|
---|
3633 | aesdeclast @offset[2],$inout2
|
---|
3634 | aesdeclast @offset[3],$inout3
|
---|
3635 | aesdeclast @offset[4],$inout4
|
---|
3636 | aesdeclast @offset[5],$inout5
|
---|
3637 | ret
|
---|
3638 | .cfi_endproc
|
---|
3639 | .size __ocb_decrypt6,.-__ocb_decrypt6
|
---|
3640 |
|
---|
3641 | .type __ocb_decrypt4,\@abi-omnipotent
|
---|
3642 | .align 32
|
---|
3643 | __ocb_decrypt4:
|
---|
3644 | .cfi_startproc
|
---|
3645 | pxor $rndkey0l,@offset[5] # offset_i ^ round[0]
|
---|
3646 | movdqu ($L_p,$i1),@offset[1]
|
---|
3647 | movdqa @offset[0],@offset[2]
|
---|
3648 | movdqu ($L_p,$i3),@offset[3]
|
---|
3649 | pxor @offset[5],@offset[0]
|
---|
3650 | pxor @offset[0],@offset[1]
|
---|
3651 | pxor @offset[0],$inout0 # input ^ round[0] ^ offset_i
|
---|
3652 | pxor @offset[1],@offset[2]
|
---|
3653 | pxor @offset[1],$inout1
|
---|
3654 | pxor @offset[2],@offset[3]
|
---|
3655 | pxor @offset[2],$inout2
|
---|
3656 | pxor @offset[3],$inout3
|
---|
3657 | $movkey 32($key_),$rndkey0
|
---|
3658 |
|
---|
3659 | pxor $rndkey0l,@offset[0] # offset_i ^ round[last]
|
---|
3660 | pxor $rndkey0l,@offset[1]
|
---|
3661 | pxor $rndkey0l,@offset[2]
|
---|
3662 | pxor $rndkey0l,@offset[3]
|
---|
3663 |
|
---|
3664 | aesdec $rndkey1,$inout0
|
---|
3665 | aesdec $rndkey1,$inout1
|
---|
3666 | aesdec $rndkey1,$inout2
|
---|
3667 | aesdec $rndkey1,$inout3
|
---|
3668 | $movkey 48($key_),$rndkey1
|
---|
3669 |
|
---|
3670 | aesdec $rndkey0,$inout0
|
---|
3671 | aesdec $rndkey0,$inout1
|
---|
3672 | aesdec $rndkey0,$inout2
|
---|
3673 | aesdec $rndkey0,$inout3
|
---|
3674 | $movkey 64($key_),$rndkey0
|
---|
3675 | jmp .Locb_dec_loop4
|
---|
3676 |
|
---|
3677 | .align 32
|
---|
3678 | .Locb_dec_loop4:
|
---|
3679 | aesdec $rndkey1,$inout0
|
---|
3680 | aesdec $rndkey1,$inout1
|
---|
3681 | aesdec $rndkey1,$inout2
|
---|
3682 | aesdec $rndkey1,$inout3
|
---|
3683 | $movkey ($key,%rax),$rndkey1
|
---|
3684 | add \$32,%rax
|
---|
3685 |
|
---|
3686 | aesdec $rndkey0,$inout0
|
---|
3687 | aesdec $rndkey0,$inout1
|
---|
3688 | aesdec $rndkey0,$inout2
|
---|
3689 | aesdec $rndkey0,$inout3
|
---|
3690 | $movkey -16($key,%rax),$rndkey0
|
---|
3691 | jnz .Locb_dec_loop4
|
---|
3692 |
|
---|
3693 | aesdec $rndkey1,$inout0
|
---|
3694 | aesdec $rndkey1,$inout1
|
---|
3695 | aesdec $rndkey1,$inout2
|
---|
3696 | aesdec $rndkey1,$inout3
|
---|
3697 | $movkey 16($key_),$rndkey1
|
---|
3698 | mov %r10,%rax # restore twisted rounds
|
---|
3699 |
|
---|
3700 | aesdeclast @offset[0],$inout0
|
---|
3701 | aesdeclast @offset[1],$inout1
|
---|
3702 | aesdeclast @offset[2],$inout2
|
---|
3703 | aesdeclast @offset[3],$inout3
|
---|
3704 | ret
|
---|
3705 | .cfi_endproc
|
---|
3706 | .size __ocb_decrypt4,.-__ocb_decrypt4
|
---|
3707 |
|
---|
3708 | .type __ocb_decrypt1,\@abi-omnipotent
|
---|
3709 | .align 32
|
---|
3710 | __ocb_decrypt1:
|
---|
3711 | .cfi_startproc
|
---|
3712 | pxor @offset[5],$inout5 # offset_i
|
---|
3713 | pxor $rndkey0l,$inout5 # offset_i ^ round[0]
|
---|
3714 | pxor $inout5,$inout0 # input ^ round[0] ^ offset_i
|
---|
3715 | $movkey 32($key_),$rndkey0
|
---|
3716 |
|
---|
3717 | aesdec $rndkey1,$inout0
|
---|
3718 | $movkey 48($key_),$rndkey1
|
---|
3719 | pxor $rndkey0l,$inout5 # offset_i ^ round[last]
|
---|
3720 |
|
---|
3721 | aesdec $rndkey0,$inout0
|
---|
3722 | $movkey 64($key_),$rndkey0
|
---|
3723 | jmp .Locb_dec_loop1
|
---|
3724 |
|
---|
3725 | .align 32
|
---|
3726 | .Locb_dec_loop1:
|
---|
3727 | aesdec $rndkey1,$inout0
|
---|
3728 | $movkey ($key,%rax),$rndkey1
|
---|
3729 | add \$32,%rax
|
---|
3730 |
|
---|
3731 | aesdec $rndkey0,$inout0
|
---|
3732 | $movkey -16($key,%rax),$rndkey0
|
---|
3733 | jnz .Locb_dec_loop1
|
---|
3734 |
|
---|
3735 | aesdec $rndkey1,$inout0
|
---|
3736 | $movkey 16($key_),$rndkey1 # redundant in tail
|
---|
3737 | mov %r10,%rax # restore twisted rounds
|
---|
3738 |
|
---|
3739 | aesdeclast $inout5,$inout0
|
---|
3740 | ret
|
---|
3741 | .cfi_endproc
|
---|
3742 | .size __ocb_decrypt1,.-__ocb_decrypt1
|
---|
3743 | ___
|
---|
3744 | } }}
|
---|
3745 | |
---|
3746 |
|
---|
3747 | ########################################################################
|
---|
3748 | # void $PREFIX_cbc_encrypt (const void *inp, void *out,
|
---|
3749 | # size_t length, const AES_KEY *key,
|
---|
3750 | # unsigned char *ivp,const int enc);
|
---|
3751 | {
|
---|
3752 | my $frame_size = 0x10 + ($win64?0xa0:0); # used in decrypt
|
---|
3753 | my ($iv,$in0,$in1,$in2,$in3,$in4)=map("%xmm$_",(10..15));
|
---|
3754 |
|
---|
3755 | $code.=<<___;
|
---|
3756 | .globl ${PREFIX}_cbc_encrypt
|
---|
3757 | .type ${PREFIX}_cbc_encrypt,\@function,6
|
---|
3758 | .align 16
|
---|
3759 | ${PREFIX}_cbc_encrypt:
|
---|
3760 | .cfi_startproc
|
---|
3761 | endbranch
|
---|
3762 | test $len,$len # check length
|
---|
3763 | jz .Lcbc_ret
|
---|
3764 |
|
---|
3765 | mov 240($key),$rnds_ # key->rounds
|
---|
3766 | mov $key,$key_ # backup $key
|
---|
3767 | test %r9d,%r9d # 6th argument
|
---|
3768 | jz .Lcbc_decrypt
|
---|
3769 | #--------------------------- CBC ENCRYPT ------------------------------#
|
---|
3770 | movups ($ivp),$inout0 # load iv as initial state
|
---|
3771 | mov $rnds_,$rounds
|
---|
3772 | cmp \$16,$len
|
---|
3773 | jb .Lcbc_enc_tail
|
---|
3774 | sub \$16,$len
|
---|
3775 | jmp .Lcbc_enc_loop
|
---|
3776 | .align 16
|
---|
3777 | .Lcbc_enc_loop:
|
---|
3778 | movups ($inp),$inout1 # load input
|
---|
3779 | lea 16($inp),$inp
|
---|
3780 | #xorps $inout1,$inout0
|
---|
3781 | ___
|
---|
3782 | &aesni_generate1("enc",$key,$rounds,$inout0,$inout1);
|
---|
3783 | $code.=<<___;
|
---|
3784 | mov $rnds_,$rounds # restore $rounds
|
---|
3785 | mov $key_,$key # restore $key
|
---|
3786 | movups $inout0,0($out) # store output
|
---|
3787 | lea 16($out),$out
|
---|
3788 | sub \$16,$len
|
---|
3789 | jnc .Lcbc_enc_loop
|
---|
3790 | add \$16,$len
|
---|
3791 | jnz .Lcbc_enc_tail
|
---|
3792 | pxor $rndkey0,$rndkey0 # clear register bank
|
---|
3793 | pxor $rndkey1,$rndkey1
|
---|
3794 | movups $inout0,($ivp)
|
---|
3795 | pxor $inout0,$inout0
|
---|
3796 | pxor $inout1,$inout1
|
---|
3797 | jmp .Lcbc_ret
|
---|
3798 |
|
---|
3799 | .Lcbc_enc_tail:
|
---|
3800 | mov $len,%rcx # zaps $key
|
---|
3801 | xchg $inp,$out # $inp is %rsi and $out is %rdi now
|
---|
3802 | .long 0x9066A4F3 # rep movsb
|
---|
3803 | mov \$16,%ecx # zero tail
|
---|
3804 | sub $len,%rcx
|
---|
3805 | xor %eax,%eax
|
---|
3806 | .long 0x9066AAF3 # rep stosb
|
---|
3807 | lea -16(%rdi),%rdi # rewind $out by 1 block
|
---|
3808 | mov $rnds_,$rounds # restore $rounds
|
---|
3809 | mov %rdi,%rsi # $inp and $out are the same
|
---|
3810 | mov $key_,$key # restore $key
|
---|
3811 | xor $len,$len # len=16
|
---|
3812 | jmp .Lcbc_enc_loop # one more spin
|
---|
3813 | |
---|
3814 | #--------------------------- CBC DECRYPT ------------------------------#
|
---|
3815 | .align 16
|
---|
3816 | .Lcbc_decrypt:
|
---|
3817 | cmp \$16,$len
|
---|
3818 | jne .Lcbc_decrypt_bulk
|
---|
3819 |
|
---|
3820 | # handle single block without allocating stack frame,
|
---|
3821 | # useful in ciphertext stealing mode
|
---|
3822 | movdqu ($inp),$inout0 # load input
|
---|
3823 | movdqu ($ivp),$inout1 # load iv
|
---|
3824 | movdqa $inout0,$inout2 # future iv
|
---|
3825 | ___
|
---|
3826 | &aesni_generate1("dec",$key,$rnds_);
|
---|
3827 | $code.=<<___;
|
---|
3828 | pxor $rndkey0,$rndkey0 # clear register bank
|
---|
3829 | pxor $rndkey1,$rndkey1
|
---|
3830 | movdqu $inout2,($ivp) # store iv
|
---|
3831 | xorps $inout1,$inout0 # ^=iv
|
---|
3832 | pxor $inout1,$inout1
|
---|
3833 | movups $inout0,($out) # store output
|
---|
3834 | pxor $inout0,$inout0
|
---|
3835 | jmp .Lcbc_ret
|
---|
3836 | .align 16
|
---|
3837 | .Lcbc_decrypt_bulk:
|
---|
3838 | lea (%rsp),%r11 # frame pointer
|
---|
3839 | .cfi_def_cfa_register %r11
|
---|
3840 | push %rbp
|
---|
3841 | .cfi_push %rbp
|
---|
3842 | sub \$$frame_size,%rsp
|
---|
3843 | and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
|
---|
3844 | ___
|
---|
3845 | $code.=<<___ if ($win64);
|
---|
3846 | movaps %xmm6,0x10(%rsp)
|
---|
3847 | movaps %xmm7,0x20(%rsp)
|
---|
3848 | movaps %xmm8,0x30(%rsp)
|
---|
3849 | movaps %xmm9,0x40(%rsp)
|
---|
3850 | movaps %xmm10,0x50(%rsp)
|
---|
3851 | movaps %xmm11,0x60(%rsp)
|
---|
3852 | movaps %xmm12,0x70(%rsp)
|
---|
3853 | movaps %xmm13,0x80(%rsp)
|
---|
3854 | movaps %xmm14,0x90(%rsp)
|
---|
3855 | movaps %xmm15,0xa0(%rsp)
|
---|
3856 | .Lcbc_decrypt_body:
|
---|
3857 | ___
|
---|
3858 |
|
---|
3859 | my $inp_=$key_="%rbp"; # reassign $key_
|
---|
3860 |
|
---|
3861 | $code.=<<___;
|
---|
3862 | mov $key,$key_ # [re-]backup $key [after reassignment]
|
---|
3863 | movups ($ivp),$iv
|
---|
3864 | mov $rnds_,$rounds
|
---|
3865 | cmp \$0x50,$len
|
---|
3866 | jbe .Lcbc_dec_tail
|
---|
3867 |
|
---|
3868 | $movkey ($key),$rndkey0
|
---|
3869 | movdqu 0x00($inp),$inout0 # load input
|
---|
3870 | movdqu 0x10($inp),$inout1
|
---|
3871 | movdqa $inout0,$in0
|
---|
3872 | movdqu 0x20($inp),$inout2
|
---|
3873 | movdqa $inout1,$in1
|
---|
3874 | movdqu 0x30($inp),$inout3
|
---|
3875 | movdqa $inout2,$in2
|
---|
3876 | movdqu 0x40($inp),$inout4
|
---|
3877 | movdqa $inout3,$in3
|
---|
3878 | movdqu 0x50($inp),$inout5
|
---|
3879 | movdqa $inout4,$in4
|
---|
3880 | mov OPENSSL_ia32cap_P+4(%rip),%r9d
|
---|
3881 | cmp \$0x70,$len
|
---|
3882 | jbe .Lcbc_dec_six_or_seven
|
---|
3883 |
|
---|
3884 | and \$`1<<26|1<<22`,%r9d # isolate XSAVE+MOVBE
|
---|
3885 | sub \$0x50,$len # $len is biased by -5*16
|
---|
3886 | cmp \$`1<<22`,%r9d # check for MOVBE without XSAVE
|
---|
3887 | je .Lcbc_dec_loop6_enter # [which denotes Atom Silvermont]
|
---|
3888 | sub \$0x20,$len # $len is biased by -7*16
|
---|
3889 | lea 0x70($key),$key # size optimization
|
---|
3890 | jmp .Lcbc_dec_loop8_enter
|
---|
3891 | .align 16
|
---|
3892 | .Lcbc_dec_loop8:
|
---|
3893 | movups $inout7,($out)
|
---|
3894 | lea 0x10($out),$out
|
---|
3895 | .Lcbc_dec_loop8_enter:
|
---|
3896 | movdqu 0x60($inp),$inout6
|
---|
3897 | pxor $rndkey0,$inout0
|
---|
3898 | movdqu 0x70($inp),$inout7
|
---|
3899 | pxor $rndkey0,$inout1
|
---|
3900 | $movkey 0x10-0x70($key),$rndkey1
|
---|
3901 | pxor $rndkey0,$inout2
|
---|
3902 | mov \$-1,$inp_
|
---|
3903 | cmp \$0x70,$len # is there at least 0x60 bytes ahead?
|
---|
3904 | pxor $rndkey0,$inout3
|
---|
3905 | pxor $rndkey0,$inout4
|
---|
3906 | pxor $rndkey0,$inout5
|
---|
3907 | pxor $rndkey0,$inout6
|
---|
3908 |
|
---|
3909 | aesdec $rndkey1,$inout0
|
---|
3910 | pxor $rndkey0,$inout7
|
---|
3911 | $movkey 0x20-0x70($key),$rndkey0
|
---|
3912 | aesdec $rndkey1,$inout1
|
---|
3913 | aesdec $rndkey1,$inout2
|
---|
3914 | aesdec $rndkey1,$inout3
|
---|
3915 | aesdec $rndkey1,$inout4
|
---|
3916 | aesdec $rndkey1,$inout5
|
---|
3917 | aesdec $rndkey1,$inout6
|
---|
3918 | adc \$0,$inp_
|
---|
3919 | and \$128,$inp_
|
---|
3920 | aesdec $rndkey1,$inout7
|
---|
3921 | add $inp,$inp_
|
---|
3922 | $movkey 0x30-0x70($key),$rndkey1
|
---|
3923 | ___
|
---|
3924 | for($i=1;$i<12;$i++) {
|
---|
3925 | my $rndkeyx = ($i&1)?$rndkey0:$rndkey1;
|
---|
3926 | $code.=<<___ if ($i==7);
|
---|
3927 | cmp \$11,$rounds
|
---|
3928 | ___
|
---|
3929 | $code.=<<___;
|
---|
3930 | aesdec $rndkeyx,$inout0
|
---|
3931 | aesdec $rndkeyx,$inout1
|
---|
3932 | aesdec $rndkeyx,$inout2
|
---|
3933 | aesdec $rndkeyx,$inout3
|
---|
3934 | aesdec $rndkeyx,$inout4
|
---|
3935 | aesdec $rndkeyx,$inout5
|
---|
3936 | aesdec $rndkeyx,$inout6
|
---|
3937 | aesdec $rndkeyx,$inout7
|
---|
3938 | $movkey `0x30+0x10*$i`-0x70($key),$rndkeyx
|
---|
3939 | ___
|
---|
3940 | $code.=<<___ if ($i<6 || (!($i&1) && $i>7));
|
---|
3941 | nop
|
---|
3942 | ___
|
---|
3943 | $code.=<<___ if ($i==7);
|
---|
3944 | jb .Lcbc_dec_done
|
---|
3945 | ___
|
---|
3946 | $code.=<<___ if ($i==9);
|
---|
3947 | je .Lcbc_dec_done
|
---|
3948 | ___
|
---|
3949 | $code.=<<___ if ($i==11);
|
---|
3950 | jmp .Lcbc_dec_done
|
---|
3951 | ___
|
---|
3952 | }
|
---|
3953 | $code.=<<___;
|
---|
3954 | .align 16
|
---|
3955 | .Lcbc_dec_done:
|
---|
3956 | aesdec $rndkey1,$inout0
|
---|
3957 | aesdec $rndkey1,$inout1
|
---|
3958 | pxor $rndkey0,$iv
|
---|
3959 | pxor $rndkey0,$in0
|
---|
3960 | aesdec $rndkey1,$inout2
|
---|
3961 | aesdec $rndkey1,$inout3
|
---|
3962 | pxor $rndkey0,$in1
|
---|
3963 | pxor $rndkey0,$in2
|
---|
3964 | aesdec $rndkey1,$inout4
|
---|
3965 | aesdec $rndkey1,$inout5
|
---|
3966 | pxor $rndkey0,$in3
|
---|
3967 | pxor $rndkey0,$in4
|
---|
3968 | aesdec $rndkey1,$inout6
|
---|
3969 | aesdec $rndkey1,$inout7
|
---|
3970 | movdqu 0x50($inp),$rndkey1
|
---|
3971 |
|
---|
3972 | aesdeclast $iv,$inout0
|
---|
3973 | movdqu 0x60($inp),$iv # borrow $iv
|
---|
3974 | pxor $rndkey0,$rndkey1
|
---|
3975 | aesdeclast $in0,$inout1
|
---|
3976 | pxor $rndkey0,$iv
|
---|
3977 | movdqu 0x70($inp),$rndkey0 # next IV
|
---|
3978 | aesdeclast $in1,$inout2
|
---|
3979 | lea 0x80($inp),$inp
|
---|
3980 | movdqu 0x00($inp_),$in0
|
---|
3981 | aesdeclast $in2,$inout3
|
---|
3982 | aesdeclast $in3,$inout4
|
---|
3983 | movdqu 0x10($inp_),$in1
|
---|
3984 | movdqu 0x20($inp_),$in2
|
---|
3985 | aesdeclast $in4,$inout5
|
---|
3986 | aesdeclast $rndkey1,$inout6
|
---|
3987 | movdqu 0x30($inp_),$in3
|
---|
3988 | movdqu 0x40($inp_),$in4
|
---|
3989 | aesdeclast $iv,$inout7
|
---|
3990 | movdqa $rndkey0,$iv # return $iv
|
---|
3991 | movdqu 0x50($inp_),$rndkey1
|
---|
3992 | $movkey -0x70($key),$rndkey0
|
---|
3993 |
|
---|
3994 | movups $inout0,($out) # store output
|
---|
3995 | movdqa $in0,$inout0
|
---|
3996 | movups $inout1,0x10($out)
|
---|
3997 | movdqa $in1,$inout1
|
---|
3998 | movups $inout2,0x20($out)
|
---|
3999 | movdqa $in2,$inout2
|
---|
4000 | movups $inout3,0x30($out)
|
---|
4001 | movdqa $in3,$inout3
|
---|
4002 | movups $inout4,0x40($out)
|
---|
4003 | movdqa $in4,$inout4
|
---|
4004 | movups $inout5,0x50($out)
|
---|
4005 | movdqa $rndkey1,$inout5
|
---|
4006 | movups $inout6,0x60($out)
|
---|
4007 | lea 0x70($out),$out
|
---|
4008 |
|
---|
4009 | sub \$0x80,$len
|
---|
4010 | ja .Lcbc_dec_loop8
|
---|
4011 |
|
---|
4012 | movaps $inout7,$inout0
|
---|
4013 | lea -0x70($key),$key
|
---|
4014 | add \$0x70,$len
|
---|
4015 | jle .Lcbc_dec_clear_tail_collected
|
---|
4016 | movups $inout7,($out)
|
---|
4017 | lea 0x10($out),$out
|
---|
4018 | cmp \$0x50,$len
|
---|
4019 | jbe .Lcbc_dec_tail
|
---|
4020 |
|
---|
4021 | movaps $in0,$inout0
|
---|
4022 | .Lcbc_dec_six_or_seven:
|
---|
4023 | cmp \$0x60,$len
|
---|
4024 | ja .Lcbc_dec_seven
|
---|
4025 |
|
---|
4026 | movaps $inout5,$inout6
|
---|
4027 | call _aesni_decrypt6
|
---|
4028 | pxor $iv,$inout0 # ^= IV
|
---|
4029 | movaps $inout6,$iv
|
---|
4030 | pxor $in0,$inout1
|
---|
4031 | movdqu $inout0,($out)
|
---|
4032 | pxor $in1,$inout2
|
---|
4033 | movdqu $inout1,0x10($out)
|
---|
4034 | pxor $inout1,$inout1 # clear register bank
|
---|
4035 | pxor $in2,$inout3
|
---|
4036 | movdqu $inout2,0x20($out)
|
---|
4037 | pxor $inout2,$inout2
|
---|
4038 | pxor $in3,$inout4
|
---|
4039 | movdqu $inout3,0x30($out)
|
---|
4040 | pxor $inout3,$inout3
|
---|
4041 | pxor $in4,$inout5
|
---|
4042 | movdqu $inout4,0x40($out)
|
---|
4043 | pxor $inout4,$inout4
|
---|
4044 | lea 0x50($out),$out
|
---|
4045 | movdqa $inout5,$inout0
|
---|
4046 | pxor $inout5,$inout5
|
---|
4047 | jmp .Lcbc_dec_tail_collected
|
---|
4048 |
|
---|
4049 | .align 16
|
---|
4050 | .Lcbc_dec_seven:
|
---|
4051 | movups 0x60($inp),$inout6
|
---|
4052 | xorps $inout7,$inout7
|
---|
4053 | call _aesni_decrypt8
|
---|
4054 | movups 0x50($inp),$inout7
|
---|
4055 | pxor $iv,$inout0 # ^= IV
|
---|
4056 | movups 0x60($inp),$iv
|
---|
4057 | pxor $in0,$inout1
|
---|
4058 | movdqu $inout0,($out)
|
---|
4059 | pxor $in1,$inout2
|
---|
4060 | movdqu $inout1,0x10($out)
|
---|
4061 | pxor $inout1,$inout1 # clear register bank
|
---|
4062 | pxor $in2,$inout3
|
---|
4063 | movdqu $inout2,0x20($out)
|
---|
4064 | pxor $inout2,$inout2
|
---|
4065 | pxor $in3,$inout4
|
---|
4066 | movdqu $inout3,0x30($out)
|
---|
4067 | pxor $inout3,$inout3
|
---|
4068 | pxor $in4,$inout5
|
---|
4069 | movdqu $inout4,0x40($out)
|
---|
4070 | pxor $inout4,$inout4
|
---|
4071 | pxor $inout7,$inout6
|
---|
4072 | movdqu $inout5,0x50($out)
|
---|
4073 | pxor $inout5,$inout5
|
---|
4074 | lea 0x60($out),$out
|
---|
4075 | movdqa $inout6,$inout0
|
---|
4076 | pxor $inout6,$inout6
|
---|
4077 | pxor $inout7,$inout7
|
---|
4078 | jmp .Lcbc_dec_tail_collected
|
---|
4079 |
|
---|
4080 | .align 16
|
---|
4081 | .Lcbc_dec_loop6:
|
---|
4082 | movups $inout5,($out)
|
---|
4083 | lea 0x10($out),$out
|
---|
4084 | movdqu 0x00($inp),$inout0 # load input
|
---|
4085 | movdqu 0x10($inp),$inout1
|
---|
4086 | movdqa $inout0,$in0
|
---|
4087 | movdqu 0x20($inp),$inout2
|
---|
4088 | movdqa $inout1,$in1
|
---|
4089 | movdqu 0x30($inp),$inout3
|
---|
4090 | movdqa $inout2,$in2
|
---|
4091 | movdqu 0x40($inp),$inout4
|
---|
4092 | movdqa $inout3,$in3
|
---|
4093 | movdqu 0x50($inp),$inout5
|
---|
4094 | movdqa $inout4,$in4
|
---|
4095 | .Lcbc_dec_loop6_enter:
|
---|
4096 | lea 0x60($inp),$inp
|
---|
4097 | movdqa $inout5,$inout6
|
---|
4098 |
|
---|
4099 | call _aesni_decrypt6
|
---|
4100 |
|
---|
4101 | pxor $iv,$inout0 # ^= IV
|
---|
4102 | movdqa $inout6,$iv
|
---|
4103 | pxor $in0,$inout1
|
---|
4104 | movdqu $inout0,($out)
|
---|
4105 | pxor $in1,$inout2
|
---|
4106 | movdqu $inout1,0x10($out)
|
---|
4107 | pxor $in2,$inout3
|
---|
4108 | movdqu $inout2,0x20($out)
|
---|
4109 | pxor $in3,$inout4
|
---|
4110 | mov $key_,$key
|
---|
4111 | movdqu $inout3,0x30($out)
|
---|
4112 | pxor $in4,$inout5
|
---|
4113 | mov $rnds_,$rounds
|
---|
4114 | movdqu $inout4,0x40($out)
|
---|
4115 | lea 0x50($out),$out
|
---|
4116 | sub \$0x60,$len
|
---|
4117 | ja .Lcbc_dec_loop6
|
---|
4118 |
|
---|
4119 | movdqa $inout5,$inout0
|
---|
4120 | add \$0x50,$len
|
---|
4121 | jle .Lcbc_dec_clear_tail_collected
|
---|
4122 | movups $inout5,($out)
|
---|
4123 | lea 0x10($out),$out
|
---|
4124 |
|
---|
4125 | .Lcbc_dec_tail:
|
---|
4126 | movups ($inp),$inout0
|
---|
4127 | sub \$0x10,$len
|
---|
4128 | jbe .Lcbc_dec_one # $len is 1*16 or less
|
---|
4129 |
|
---|
4130 | movups 0x10($inp),$inout1
|
---|
4131 | movaps $inout0,$in0
|
---|
4132 | sub \$0x10,$len
|
---|
4133 | jbe .Lcbc_dec_two # $len is 2*16 or less
|
---|
4134 |
|
---|
4135 | movups 0x20($inp),$inout2
|
---|
4136 | movaps $inout1,$in1
|
---|
4137 | sub \$0x10,$len
|
---|
4138 | jbe .Lcbc_dec_three # $len is 3*16 or less
|
---|
4139 |
|
---|
4140 | movups 0x30($inp),$inout3
|
---|
4141 | movaps $inout2,$in2
|
---|
4142 | sub \$0x10,$len
|
---|
4143 | jbe .Lcbc_dec_four # $len is 4*16 or less
|
---|
4144 |
|
---|
4145 | movups 0x40($inp),$inout4 # $len is 5*16 or less
|
---|
4146 | movaps $inout3,$in3
|
---|
4147 | movaps $inout4,$in4
|
---|
4148 | xorps $inout5,$inout5
|
---|
4149 | call _aesni_decrypt6
|
---|
4150 | pxor $iv,$inout0
|
---|
4151 | movaps $in4,$iv
|
---|
4152 | pxor $in0,$inout1
|
---|
4153 | movdqu $inout0,($out)
|
---|
4154 | pxor $in1,$inout2
|
---|
4155 | movdqu $inout1,0x10($out)
|
---|
4156 | pxor $inout1,$inout1 # clear register bank
|
---|
4157 | pxor $in2,$inout3
|
---|
4158 | movdqu $inout2,0x20($out)
|
---|
4159 | pxor $inout2,$inout2
|
---|
4160 | pxor $in3,$inout4
|
---|
4161 | movdqu $inout3,0x30($out)
|
---|
4162 | pxor $inout3,$inout3
|
---|
4163 | lea 0x40($out),$out
|
---|
4164 | movdqa $inout4,$inout0
|
---|
4165 | pxor $inout4,$inout4
|
---|
4166 | pxor $inout5,$inout5
|
---|
4167 | sub \$0x10,$len
|
---|
4168 | jmp .Lcbc_dec_tail_collected
|
---|
4169 |
|
---|
4170 | .align 16
|
---|
4171 | .Lcbc_dec_one:
|
---|
4172 | movaps $inout0,$in0
|
---|
4173 | ___
|
---|
4174 | &aesni_generate1("dec",$key,$rounds);
|
---|
4175 | $code.=<<___;
|
---|
4176 | xorps $iv,$inout0
|
---|
4177 | movaps $in0,$iv
|
---|
4178 | jmp .Lcbc_dec_tail_collected
|
---|
4179 | .align 16
|
---|
4180 | .Lcbc_dec_two:
|
---|
4181 | movaps $inout1,$in1
|
---|
4182 | call _aesni_decrypt2
|
---|
4183 | pxor $iv,$inout0
|
---|
4184 | movaps $in1,$iv
|
---|
4185 | pxor $in0,$inout1
|
---|
4186 | movdqu $inout0,($out)
|
---|
4187 | movdqa $inout1,$inout0
|
---|
4188 | pxor $inout1,$inout1 # clear register bank
|
---|
4189 | lea 0x10($out),$out
|
---|
4190 | jmp .Lcbc_dec_tail_collected
|
---|
4191 | .align 16
|
---|
4192 | .Lcbc_dec_three:
|
---|
4193 | movaps $inout2,$in2
|
---|
4194 | call _aesni_decrypt3
|
---|
4195 | pxor $iv,$inout0
|
---|
4196 | movaps $in2,$iv
|
---|
4197 | pxor $in0,$inout1
|
---|
4198 | movdqu $inout0,($out)
|
---|
4199 | pxor $in1,$inout2
|
---|
4200 | movdqu $inout1,0x10($out)
|
---|
4201 | pxor $inout1,$inout1 # clear register bank
|
---|
4202 | movdqa $inout2,$inout0
|
---|
4203 | pxor $inout2,$inout2
|
---|
4204 | lea 0x20($out),$out
|
---|
4205 | jmp .Lcbc_dec_tail_collected
|
---|
4206 | .align 16
|
---|
4207 | .Lcbc_dec_four:
|
---|
4208 | movaps $inout3,$in3
|
---|
4209 | call _aesni_decrypt4
|
---|
4210 | pxor $iv,$inout0
|
---|
4211 | movaps $in3,$iv
|
---|
4212 | pxor $in0,$inout1
|
---|
4213 | movdqu $inout0,($out)
|
---|
4214 | pxor $in1,$inout2
|
---|
4215 | movdqu $inout1,0x10($out)
|
---|
4216 | pxor $inout1,$inout1 # clear register bank
|
---|
4217 | pxor $in2,$inout3
|
---|
4218 | movdqu $inout2,0x20($out)
|
---|
4219 | pxor $inout2,$inout2
|
---|
4220 | movdqa $inout3,$inout0
|
---|
4221 | pxor $inout3,$inout3
|
---|
4222 | lea 0x30($out),$out
|
---|
4223 | jmp .Lcbc_dec_tail_collected
|
---|
4224 |
|
---|
4225 | .align 16
|
---|
4226 | .Lcbc_dec_clear_tail_collected:
|
---|
4227 | pxor $inout1,$inout1 # clear register bank
|
---|
4228 | pxor $inout2,$inout2
|
---|
4229 | pxor $inout3,$inout3
|
---|
4230 | ___
|
---|
4231 | $code.=<<___ if (!$win64);
|
---|
4232 | pxor $inout4,$inout4 # %xmm6..9
|
---|
4233 | pxor $inout5,$inout5
|
---|
4234 | pxor $inout6,$inout6
|
---|
4235 | pxor $inout7,$inout7
|
---|
4236 | ___
|
---|
4237 | $code.=<<___;
|
---|
4238 | .Lcbc_dec_tail_collected:
|
---|
4239 | movups $iv,($ivp)
|
---|
4240 | and \$15,$len
|
---|
4241 | jnz .Lcbc_dec_tail_partial
|
---|
4242 | movups $inout0,($out)
|
---|
4243 | pxor $inout0,$inout0
|
---|
4244 | jmp .Lcbc_dec_ret
|
---|
4245 | .align 16
|
---|
4246 | .Lcbc_dec_tail_partial:
|
---|
4247 | movaps $inout0,(%rsp)
|
---|
4248 | pxor $inout0,$inout0
|
---|
4249 | mov \$16,%rcx
|
---|
4250 | mov $out,%rdi
|
---|
4251 | sub $len,%rcx
|
---|
4252 | lea (%rsp),%rsi
|
---|
4253 | .long 0x9066A4F3 # rep movsb
|
---|
4254 | movdqa $inout0,(%rsp)
|
---|
4255 |
|
---|
4256 | .Lcbc_dec_ret:
|
---|
4257 | xorps $rndkey0,$rndkey0 # %xmm0
|
---|
4258 | pxor $rndkey1,$rndkey1
|
---|
4259 | ___
|
---|
4260 | $code.=<<___ if ($win64);
|
---|
4261 | movaps 0x10(%rsp),%xmm6
|
---|
4262 | movaps %xmm0,0x10(%rsp) # clear stack
|
---|
4263 | movaps 0x20(%rsp),%xmm7
|
---|
4264 | movaps %xmm0,0x20(%rsp)
|
---|
4265 | movaps 0x30(%rsp),%xmm8
|
---|
4266 | movaps %xmm0,0x30(%rsp)
|
---|
4267 | movaps 0x40(%rsp),%xmm9
|
---|
4268 | movaps %xmm0,0x40(%rsp)
|
---|
4269 | movaps 0x50(%rsp),%xmm10
|
---|
4270 | movaps %xmm0,0x50(%rsp)
|
---|
4271 | movaps 0x60(%rsp),%xmm11
|
---|
4272 | movaps %xmm0,0x60(%rsp)
|
---|
4273 | movaps 0x70(%rsp),%xmm12
|
---|
4274 | movaps %xmm0,0x70(%rsp)
|
---|
4275 | movaps 0x80(%rsp),%xmm13
|
---|
4276 | movaps %xmm0,0x80(%rsp)
|
---|
4277 | movaps 0x90(%rsp),%xmm14
|
---|
4278 | movaps %xmm0,0x90(%rsp)
|
---|
4279 | movaps 0xa0(%rsp),%xmm15
|
---|
4280 | movaps %xmm0,0xa0(%rsp)
|
---|
4281 | ___
|
---|
4282 | $code.=<<___;
|
---|
4283 | mov -8(%r11),%rbp
|
---|
4284 | .cfi_restore %rbp
|
---|
4285 | lea (%r11),%rsp
|
---|
4286 | .cfi_def_cfa_register %rsp
|
---|
4287 | .Lcbc_ret:
|
---|
4288 | ret
|
---|
4289 | .cfi_endproc
|
---|
4290 | .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
|
---|
4291 | ___
|
---|
4292 | } |
---|
4293 |
|
---|
4294 | # int ${PREFIX}_set_decrypt_key(const unsigned char *inp,
|
---|
4295 | # int bits, AES_KEY *key)
|
---|
4296 | #
|
---|
4297 | # input: $inp user-supplied key
|
---|
4298 | # $bits $inp length in bits
|
---|
4299 | # $key pointer to key schedule
|
---|
4300 | # output: %eax 0 denoting success, -1 or -2 - failure (see C)
|
---|
4301 | # *$key key schedule
|
---|
4302 | #
|
---|
4303 | { my ($inp,$bits,$key) = @_4args;
|
---|
4304 | $bits =~ s/%r/%e/;
|
---|
4305 |
|
---|
4306 | $code.=<<___;
|
---|
4307 | .globl ${PREFIX}_set_decrypt_key
|
---|
4308 | .type ${PREFIX}_set_decrypt_key,\@abi-omnipotent
|
---|
4309 | .align 16
|
---|
4310 | ${PREFIX}_set_decrypt_key:
|
---|
4311 | .cfi_startproc
|
---|
4312 | .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
|
---|
4313 | .cfi_adjust_cfa_offset 8
|
---|
4314 | call __aesni_set_encrypt_key
|
---|
4315 | shl \$4,$bits # rounds-1 after _aesni_set_encrypt_key
|
---|
4316 | test %eax,%eax
|
---|
4317 | jnz .Ldec_key_ret
|
---|
4318 | lea 16($key,$bits),$inp # points at the end of key schedule
|
---|
4319 |
|
---|
4320 | $movkey ($key),%xmm0 # just swap
|
---|
4321 | $movkey ($inp),%xmm1
|
---|
4322 | $movkey %xmm0,($inp)
|
---|
4323 | $movkey %xmm1,($key)
|
---|
4324 | lea 16($key),$key
|
---|
4325 | lea -16($inp),$inp
|
---|
4326 |
|
---|
4327 | .Ldec_key_inverse:
|
---|
4328 | $movkey ($key),%xmm0 # swap and inverse
|
---|
4329 | $movkey ($inp),%xmm1
|
---|
4330 | aesimc %xmm0,%xmm0
|
---|
4331 | aesimc %xmm1,%xmm1
|
---|
4332 | lea 16($key),$key
|
---|
4333 | lea -16($inp),$inp
|
---|
4334 | $movkey %xmm0,16($inp)
|
---|
4335 | $movkey %xmm1,-16($key)
|
---|
4336 | cmp $key,$inp
|
---|
4337 | ja .Ldec_key_inverse
|
---|
4338 |
|
---|
4339 | $movkey ($key),%xmm0 # inverse middle
|
---|
4340 | aesimc %xmm0,%xmm0
|
---|
4341 | pxor %xmm1,%xmm1
|
---|
4342 | $movkey %xmm0,($inp)
|
---|
4343 | pxor %xmm0,%xmm0
|
---|
4344 | .Ldec_key_ret:
|
---|
4345 | add \$8,%rsp
|
---|
4346 | .cfi_adjust_cfa_offset -8
|
---|
4347 | ret
|
---|
4348 | .cfi_endproc
|
---|
4349 | .LSEH_end_set_decrypt_key:
|
---|
4350 | .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
|
---|
4351 | ___
|
---|
4352 | |
---|
4353 |
|
---|
4354 | # This is based on submission from Intel by
|
---|
4355 | # Huang Ying
|
---|
4356 | # Vinodh Gopal
|
---|
4357 | # Kahraman Akdemir
|
---|
4358 | #
|
---|
4359 | # Aggressively optimized in respect to aeskeygenassist's critical path
|
---|
4360 | # and is contained in %xmm0-5 to meet Win64 ABI requirement.
|
---|
4361 | #
|
---|
4362 | # int ${PREFIX}_set_encrypt_key(const unsigned char *inp,
|
---|
4363 | # int bits, AES_KEY * const key);
|
---|
4364 | #
|
---|
4365 | # input: $inp user-supplied key
|
---|
4366 | # $bits $inp length in bits
|
---|
4367 | # $key pointer to key schedule
|
---|
4368 | # output: %eax 0 denoting success, -1 or -2 - failure (see C)
|
---|
4369 | # $bits rounds-1 (used in aesni_set_decrypt_key)
|
---|
4370 | # *$key key schedule
|
---|
4371 | # $key pointer to key schedule (used in
|
---|
4372 | # aesni_set_decrypt_key)
|
---|
4373 | #
|
---|
4374 | # Subroutine is frame-less, which means that only volatile registers
|
---|
4375 | # are used. Note that it's declared "abi-omnipotent", which means that
|
---|
4376 | # amount of volatile registers is smaller on Windows.
|
---|
4377 | #
|
---|
4378 | $code.=<<___;
|
---|
4379 | .globl ${PREFIX}_set_encrypt_key
|
---|
4380 | .type ${PREFIX}_set_encrypt_key,\@abi-omnipotent
|
---|
4381 | .align 16
|
---|
4382 | ${PREFIX}_set_encrypt_key:
|
---|
4383 | __aesni_set_encrypt_key:
|
---|
4384 | .cfi_startproc
|
---|
4385 | .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
|
---|
4386 | .cfi_adjust_cfa_offset 8
|
---|
4387 | mov \$-1,%rax
|
---|
4388 | test $inp,$inp
|
---|
4389 | jz .Lenc_key_ret
|
---|
4390 | test $key,$key
|
---|
4391 | jz .Lenc_key_ret
|
---|
4392 |
|
---|
4393 | mov \$`1<<28|1<<11`,%r10d # AVX and XOP bits
|
---|
4394 | movups ($inp),%xmm0 # pull first 128 bits of *userKey
|
---|
4395 | xorps %xmm4,%xmm4 # low dword of xmm4 is assumed 0
|
---|
4396 | and OPENSSL_ia32cap_P+4(%rip),%r10d
|
---|
4397 | lea 16($key),%rax # %rax is used as modifiable copy of $key
|
---|
4398 | cmp \$256,$bits
|
---|
4399 | je .L14rounds
|
---|
4400 | cmp \$192,$bits
|
---|
4401 | je .L12rounds
|
---|
4402 | cmp \$128,$bits
|
---|
4403 | jne .Lbad_keybits
|
---|
4404 |
|
---|
4405 | .L10rounds:
|
---|
4406 | mov \$9,$bits # 10 rounds for 128-bit key
|
---|
4407 | cmp \$`1<<28`,%r10d # AVX, bit no XOP
|
---|
4408 | je .L10rounds_alt
|
---|
4409 |
|
---|
4410 | $movkey %xmm0,($key) # round 0
|
---|
4411 | aeskeygenassist \$0x1,%xmm0,%xmm1 # round 1
|
---|
4412 | call .Lkey_expansion_128_cold
|
---|
4413 | aeskeygenassist \$0x2,%xmm0,%xmm1 # round 2
|
---|
4414 | call .Lkey_expansion_128
|
---|
4415 | aeskeygenassist \$0x4,%xmm0,%xmm1 # round 3
|
---|
4416 | call .Lkey_expansion_128
|
---|
4417 | aeskeygenassist \$0x8,%xmm0,%xmm1 # round 4
|
---|
4418 | call .Lkey_expansion_128
|
---|
4419 | aeskeygenassist \$0x10,%xmm0,%xmm1 # round 5
|
---|
4420 | call .Lkey_expansion_128
|
---|
4421 | aeskeygenassist \$0x20,%xmm0,%xmm1 # round 6
|
---|
4422 | call .Lkey_expansion_128
|
---|
4423 | aeskeygenassist \$0x40,%xmm0,%xmm1 # round 7
|
---|
4424 | call .Lkey_expansion_128
|
---|
4425 | aeskeygenassist \$0x80,%xmm0,%xmm1 # round 8
|
---|
4426 | call .Lkey_expansion_128
|
---|
4427 | aeskeygenassist \$0x1b,%xmm0,%xmm1 # round 9
|
---|
4428 | call .Lkey_expansion_128
|
---|
4429 | aeskeygenassist \$0x36,%xmm0,%xmm1 # round 10
|
---|
4430 | call .Lkey_expansion_128
|
---|
4431 | $movkey %xmm0,(%rax)
|
---|
4432 | mov $bits,80(%rax) # 240(%rdx)
|
---|
4433 | xor %eax,%eax
|
---|
4434 | jmp .Lenc_key_ret
|
---|
4435 |
|
---|
4436 | .align 16
|
---|
4437 | .L10rounds_alt:
|
---|
4438 | movdqa .Lkey_rotate(%rip),%xmm5
|
---|
4439 | mov \$8,%r10d
|
---|
4440 | movdqa .Lkey_rcon1(%rip),%xmm4
|
---|
4441 | movdqa %xmm0,%xmm2
|
---|
4442 | movdqu %xmm0,($key)
|
---|
4443 | jmp .Loop_key128
|
---|
4444 |
|
---|
4445 | .align 16
|
---|
4446 | .Loop_key128:
|
---|
4447 | pshufb %xmm5,%xmm0
|
---|
4448 | aesenclast %xmm4,%xmm0
|
---|
4449 | pslld \$1,%xmm4
|
---|
4450 | lea 16(%rax),%rax
|
---|
4451 |
|
---|
4452 | movdqa %xmm2,%xmm3
|
---|
4453 | pslldq \$4,%xmm2
|
---|
4454 | pxor %xmm2,%xmm3
|
---|
4455 | pslldq \$4,%xmm2
|
---|
4456 | pxor %xmm2,%xmm3
|
---|
4457 | pslldq \$4,%xmm2
|
---|
4458 | pxor %xmm3,%xmm2
|
---|
4459 |
|
---|
4460 | pxor %xmm2,%xmm0
|
---|
4461 | movdqu %xmm0,-16(%rax)
|
---|
4462 | movdqa %xmm0,%xmm2
|
---|
4463 |
|
---|
4464 | dec %r10d
|
---|
4465 | jnz .Loop_key128
|
---|
4466 |
|
---|
4467 | movdqa .Lkey_rcon1b(%rip),%xmm4
|
---|
4468 |
|
---|
4469 | pshufb %xmm5,%xmm0
|
---|
4470 | aesenclast %xmm4,%xmm0
|
---|
4471 | pslld \$1,%xmm4
|
---|
4472 |
|
---|
4473 | movdqa %xmm2,%xmm3
|
---|
4474 | pslldq \$4,%xmm2
|
---|
4475 | pxor %xmm2,%xmm3
|
---|
4476 | pslldq \$4,%xmm2
|
---|
4477 | pxor %xmm2,%xmm3
|
---|
4478 | pslldq \$4,%xmm2
|
---|
4479 | pxor %xmm3,%xmm2
|
---|
4480 |
|
---|
4481 | pxor %xmm2,%xmm0
|
---|
4482 | movdqu %xmm0,(%rax)
|
---|
4483 |
|
---|
4484 | movdqa %xmm0,%xmm2
|
---|
4485 | pshufb %xmm5,%xmm0
|
---|
4486 | aesenclast %xmm4,%xmm0
|
---|
4487 |
|
---|
4488 | movdqa %xmm2,%xmm3
|
---|
4489 | pslldq \$4,%xmm2
|
---|
4490 | pxor %xmm2,%xmm3
|
---|
4491 | pslldq \$4,%xmm2
|
---|
4492 | pxor %xmm2,%xmm3
|
---|
4493 | pslldq \$4,%xmm2
|
---|
4494 | pxor %xmm3,%xmm2
|
---|
4495 |
|
---|
4496 | pxor %xmm2,%xmm0
|
---|
4497 | movdqu %xmm0,16(%rax)
|
---|
4498 |
|
---|
4499 | mov $bits,96(%rax) # 240($key)
|
---|
4500 | xor %eax,%eax
|
---|
4501 | jmp .Lenc_key_ret
|
---|
4502 |
|
---|
4503 | .align 16
|
---|
4504 | .L12rounds:
|
---|
4505 | movq 16($inp),%xmm2 # remaining 1/3 of *userKey
|
---|
4506 | mov \$11,$bits # 12 rounds for 192
|
---|
4507 | cmp \$`1<<28`,%r10d # AVX, but no XOP
|
---|
4508 | je .L12rounds_alt
|
---|
4509 |
|
---|
4510 | $movkey %xmm0,($key) # round 0
|
---|
4511 | aeskeygenassist \$0x1,%xmm2,%xmm1 # round 1,2
|
---|
4512 | call .Lkey_expansion_192a_cold
|
---|
4513 | aeskeygenassist \$0x2,%xmm2,%xmm1 # round 2,3
|
---|
4514 | call .Lkey_expansion_192b
|
---|
4515 | aeskeygenassist \$0x4,%xmm2,%xmm1 # round 4,5
|
---|
4516 | call .Lkey_expansion_192a
|
---|
4517 | aeskeygenassist \$0x8,%xmm2,%xmm1 # round 5,6
|
---|
4518 | call .Lkey_expansion_192b
|
---|
4519 | aeskeygenassist \$0x10,%xmm2,%xmm1 # round 7,8
|
---|
4520 | call .Lkey_expansion_192a
|
---|
4521 | aeskeygenassist \$0x20,%xmm2,%xmm1 # round 8,9
|
---|
4522 | call .Lkey_expansion_192b
|
---|
4523 | aeskeygenassist \$0x40,%xmm2,%xmm1 # round 10,11
|
---|
4524 | call .Lkey_expansion_192a
|
---|
4525 | aeskeygenassist \$0x80,%xmm2,%xmm1 # round 11,12
|
---|
4526 | call .Lkey_expansion_192b
|
---|
4527 | $movkey %xmm0,(%rax)
|
---|
4528 | mov $bits,48(%rax) # 240(%rdx)
|
---|
4529 | xor %rax, %rax
|
---|
4530 | jmp .Lenc_key_ret
|
---|
4531 |
|
---|
4532 | .align 16
|
---|
4533 | .L12rounds_alt:
|
---|
4534 | movdqa .Lkey_rotate192(%rip),%xmm5
|
---|
4535 | movdqa .Lkey_rcon1(%rip),%xmm4
|
---|
4536 | mov \$8,%r10d
|
---|
4537 | movdqu %xmm0,($key)
|
---|
4538 | jmp .Loop_key192
|
---|
4539 |
|
---|
4540 | .align 16
|
---|
4541 | .Loop_key192:
|
---|
4542 | movq %xmm2,0(%rax)
|
---|
4543 | movdqa %xmm2,%xmm1
|
---|
4544 | pshufb %xmm5,%xmm2
|
---|
4545 | aesenclast %xmm4,%xmm2
|
---|
4546 | pslld \$1, %xmm4
|
---|
4547 | lea 24(%rax),%rax
|
---|
4548 |
|
---|
4549 | movdqa %xmm0,%xmm3
|
---|
4550 | pslldq \$4,%xmm0
|
---|
4551 | pxor %xmm0,%xmm3
|
---|
4552 | pslldq \$4,%xmm0
|
---|
4553 | pxor %xmm0,%xmm3
|
---|
4554 | pslldq \$4,%xmm0
|
---|
4555 | pxor %xmm3,%xmm0
|
---|
4556 |
|
---|
4557 | pshufd \$0xff,%xmm0,%xmm3
|
---|
4558 | pxor %xmm1,%xmm3
|
---|
4559 | pslldq \$4,%xmm1
|
---|
4560 | pxor %xmm1,%xmm3
|
---|
4561 |
|
---|
4562 | pxor %xmm2,%xmm0
|
---|
4563 | pxor %xmm3,%xmm2
|
---|
4564 | movdqu %xmm0,-16(%rax)
|
---|
4565 |
|
---|
4566 | dec %r10d
|
---|
4567 | jnz .Loop_key192
|
---|
4568 |
|
---|
4569 | mov $bits,32(%rax) # 240($key)
|
---|
4570 | xor %eax,%eax
|
---|
4571 | jmp .Lenc_key_ret
|
---|
4572 |
|
---|
4573 | .align 16
|
---|
4574 | .L14rounds:
|
---|
4575 | movups 16($inp),%xmm2 # remaining half of *userKey
|
---|
4576 | mov \$13,$bits # 14 rounds for 256
|
---|
4577 | lea 16(%rax),%rax
|
---|
4578 | cmp \$`1<<28`,%r10d # AVX, but no XOP
|
---|
4579 | je .L14rounds_alt
|
---|
4580 |
|
---|
4581 | $movkey %xmm0,($key) # round 0
|
---|
4582 | $movkey %xmm2,16($key) # round 1
|
---|
4583 | aeskeygenassist \$0x1,%xmm2,%xmm1 # round 2
|
---|
4584 | call .Lkey_expansion_256a_cold
|
---|
4585 | aeskeygenassist \$0x1,%xmm0,%xmm1 # round 3
|
---|
4586 | call .Lkey_expansion_256b
|
---|
4587 | aeskeygenassist \$0x2,%xmm2,%xmm1 # round 4
|
---|
4588 | call .Lkey_expansion_256a
|
---|
4589 | aeskeygenassist \$0x2,%xmm0,%xmm1 # round 5
|
---|
4590 | call .Lkey_expansion_256b
|
---|
4591 | aeskeygenassist \$0x4,%xmm2,%xmm1 # round 6
|
---|
4592 | call .Lkey_expansion_256a
|
---|
4593 | aeskeygenassist \$0x4,%xmm0,%xmm1 # round 7
|
---|
4594 | call .Lkey_expansion_256b
|
---|
4595 | aeskeygenassist \$0x8,%xmm2,%xmm1 # round 8
|
---|
4596 | call .Lkey_expansion_256a
|
---|
4597 | aeskeygenassist \$0x8,%xmm0,%xmm1 # round 9
|
---|
4598 | call .Lkey_expansion_256b
|
---|
4599 | aeskeygenassist \$0x10,%xmm2,%xmm1 # round 10
|
---|
4600 | call .Lkey_expansion_256a
|
---|
4601 | aeskeygenassist \$0x10,%xmm0,%xmm1 # round 11
|
---|
4602 | call .Lkey_expansion_256b
|
---|
4603 | aeskeygenassist \$0x20,%xmm2,%xmm1 # round 12
|
---|
4604 | call .Lkey_expansion_256a
|
---|
4605 | aeskeygenassist \$0x20,%xmm0,%xmm1 # round 13
|
---|
4606 | call .Lkey_expansion_256b
|
---|
4607 | aeskeygenassist \$0x40,%xmm2,%xmm1 # round 14
|
---|
4608 | call .Lkey_expansion_256a
|
---|
4609 | $movkey %xmm0,(%rax)
|
---|
4610 | mov $bits,16(%rax) # 240(%rdx)
|
---|
4611 | xor %rax,%rax
|
---|
4612 | jmp .Lenc_key_ret
|
---|
4613 |
|
---|
4614 | .align 16
|
---|
4615 | .L14rounds_alt:
|
---|
4616 | movdqa .Lkey_rotate(%rip),%xmm5
|
---|
4617 | movdqa .Lkey_rcon1(%rip),%xmm4
|
---|
4618 | mov \$7,%r10d
|
---|
4619 | movdqu %xmm0,0($key)
|
---|
4620 | movdqa %xmm2,%xmm1
|
---|
4621 | movdqu %xmm2,16($key)
|
---|
4622 | jmp .Loop_key256
|
---|
4623 |
|
---|
4624 | .align 16
|
---|
4625 | .Loop_key256:
|
---|
4626 | pshufb %xmm5,%xmm2
|
---|
4627 | aesenclast %xmm4,%xmm2
|
---|
4628 |
|
---|
4629 | movdqa %xmm0,%xmm3
|
---|
4630 | pslldq \$4,%xmm0
|
---|
4631 | pxor %xmm0,%xmm3
|
---|
4632 | pslldq \$4,%xmm0
|
---|
4633 | pxor %xmm0,%xmm3
|
---|
4634 | pslldq \$4,%xmm0
|
---|
4635 | pxor %xmm3,%xmm0
|
---|
4636 | pslld \$1,%xmm4
|
---|
4637 |
|
---|
4638 | pxor %xmm2,%xmm0
|
---|
4639 | movdqu %xmm0,(%rax)
|
---|
4640 |
|
---|
4641 | dec %r10d
|
---|
4642 | jz .Ldone_key256
|
---|
4643 |
|
---|
4644 | pshufd \$0xff,%xmm0,%xmm2
|
---|
4645 | pxor %xmm3,%xmm3
|
---|
4646 | aesenclast %xmm3,%xmm2
|
---|
4647 |
|
---|
4648 | movdqa %xmm1,%xmm3
|
---|
4649 | pslldq \$4,%xmm1
|
---|
4650 | pxor %xmm1,%xmm3
|
---|
4651 | pslldq \$4,%xmm1
|
---|
4652 | pxor %xmm1,%xmm3
|
---|
4653 | pslldq \$4,%xmm1
|
---|
4654 | pxor %xmm3,%xmm1
|
---|
4655 |
|
---|
4656 | pxor %xmm1,%xmm2
|
---|
4657 | movdqu %xmm2,16(%rax)
|
---|
4658 | lea 32(%rax),%rax
|
---|
4659 | movdqa %xmm2,%xmm1
|
---|
4660 |
|
---|
4661 | jmp .Loop_key256
|
---|
4662 |
|
---|
4663 | .Ldone_key256:
|
---|
4664 | mov $bits,16(%rax) # 240($key)
|
---|
4665 | xor %eax,%eax
|
---|
4666 | jmp .Lenc_key_ret
|
---|
4667 |
|
---|
4668 | .align 16
|
---|
4669 | .Lbad_keybits:
|
---|
4670 | mov \$-2,%rax
|
---|
4671 | .Lenc_key_ret:
|
---|
4672 | pxor %xmm0,%xmm0
|
---|
4673 | pxor %xmm1,%xmm1
|
---|
4674 | pxor %xmm2,%xmm2
|
---|
4675 | pxor %xmm3,%xmm3
|
---|
4676 | pxor %xmm4,%xmm4
|
---|
4677 | pxor %xmm5,%xmm5
|
---|
4678 | add \$8,%rsp
|
---|
4679 | .cfi_adjust_cfa_offset -8
|
---|
4680 | ret
|
---|
4681 | .LSEH_end_set_encrypt_key:
|
---|
4682 | |
---|
4683 |
|
---|
4684 | .align 16
|
---|
4685 | .Lkey_expansion_128:
|
---|
4686 | $movkey %xmm0,(%rax)
|
---|
4687 | lea 16(%rax),%rax
|
---|
4688 | .Lkey_expansion_128_cold:
|
---|
4689 | shufps \$0b00010000,%xmm0,%xmm4
|
---|
4690 | xorps %xmm4, %xmm0
|
---|
4691 | shufps \$0b10001100,%xmm0,%xmm4
|
---|
4692 | xorps %xmm4, %xmm0
|
---|
4693 | shufps \$0b11111111,%xmm1,%xmm1 # critical path
|
---|
4694 | xorps %xmm1,%xmm0
|
---|
4695 | ret
|
---|
4696 |
|
---|
4697 | .align 16
|
---|
4698 | .Lkey_expansion_192a:
|
---|
4699 | $movkey %xmm0,(%rax)
|
---|
4700 | lea 16(%rax),%rax
|
---|
4701 | .Lkey_expansion_192a_cold:
|
---|
4702 | movaps %xmm2, %xmm5
|
---|
4703 | .Lkey_expansion_192b_warm:
|
---|
4704 | shufps \$0b00010000,%xmm0,%xmm4
|
---|
4705 | movdqa %xmm2,%xmm3
|
---|
4706 | xorps %xmm4,%xmm0
|
---|
4707 | shufps \$0b10001100,%xmm0,%xmm4
|
---|
4708 | pslldq \$4,%xmm3
|
---|
4709 | xorps %xmm4,%xmm0
|
---|
4710 | pshufd \$0b01010101,%xmm1,%xmm1 # critical path
|
---|
4711 | pxor %xmm3,%xmm2
|
---|
4712 | pxor %xmm1,%xmm0
|
---|
4713 | pshufd \$0b11111111,%xmm0,%xmm3
|
---|
4714 | pxor %xmm3,%xmm2
|
---|
4715 | ret
|
---|
4716 |
|
---|
4717 | .align 16
|
---|
4718 | .Lkey_expansion_192b:
|
---|
4719 | movaps %xmm0,%xmm3
|
---|
4720 | shufps \$0b01000100,%xmm0,%xmm5
|
---|
4721 | $movkey %xmm5,(%rax)
|
---|
4722 | shufps \$0b01001110,%xmm2,%xmm3
|
---|
4723 | $movkey %xmm3,16(%rax)
|
---|
4724 | lea 32(%rax),%rax
|
---|
4725 | jmp .Lkey_expansion_192b_warm
|
---|
4726 |
|
---|
4727 | .align 16
|
---|
4728 | .Lkey_expansion_256a:
|
---|
4729 | $movkey %xmm2,(%rax)
|
---|
4730 | lea 16(%rax),%rax
|
---|
4731 | .Lkey_expansion_256a_cold:
|
---|
4732 | shufps \$0b00010000,%xmm0,%xmm4
|
---|
4733 | xorps %xmm4,%xmm0
|
---|
4734 | shufps \$0b10001100,%xmm0,%xmm4
|
---|
4735 | xorps %xmm4,%xmm0
|
---|
4736 | shufps \$0b11111111,%xmm1,%xmm1 # critical path
|
---|
4737 | xorps %xmm1,%xmm0
|
---|
4738 | ret
|
---|
4739 |
|
---|
4740 | .align 16
|
---|
4741 | .Lkey_expansion_256b:
|
---|
4742 | $movkey %xmm0,(%rax)
|
---|
4743 | lea 16(%rax),%rax
|
---|
4744 |
|
---|
4745 | shufps \$0b00010000,%xmm2,%xmm4
|
---|
4746 | xorps %xmm4,%xmm2
|
---|
4747 | shufps \$0b10001100,%xmm2,%xmm4
|
---|
4748 | xorps %xmm4,%xmm2
|
---|
4749 | shufps \$0b10101010,%xmm1,%xmm1 # critical path
|
---|
4750 | xorps %xmm1,%xmm2
|
---|
4751 | ret
|
---|
4752 | .cfi_endproc
|
---|
4753 | .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
|
---|
4754 | .size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
|
---|
4755 | ___
|
---|
4756 | }
|
---|
4757 | |
---|
4758 |
|
---|
4759 | $code.=<<___;
|
---|
4760 | .align 64
|
---|
4761 | .Lbswap_mask:
|
---|
4762 | .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
|
---|
4763 | .Lincrement32:
|
---|
4764 | .long 6,6,6,0
|
---|
4765 | .Lincrement64:
|
---|
4766 | .long 1,0,0,0
|
---|
4767 | .Lxts_magic:
|
---|
4768 | .long 0x87,0,1,0
|
---|
4769 | .Lincrement1:
|
---|
4770 | .byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
|
---|
4771 | .Lkey_rotate:
|
---|
4772 | .long 0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d
|
---|
4773 | .Lkey_rotate192:
|
---|
4774 | .long 0x04070605,0x04070605,0x04070605,0x04070605
|
---|
4775 | .Lkey_rcon1:
|
---|
4776 | .long 1,1,1,1
|
---|
4777 | .Lkey_rcon1b:
|
---|
4778 | .long 0x1b,0x1b,0x1b,0x1b
|
---|
4779 |
|
---|
4780 | .asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
|
---|
4781 | .align 64
|
---|
4782 | ___
|
---|
4783 |
|
---|
4784 | # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
|
---|
4785 | # CONTEXT *context,DISPATCHER_CONTEXT *disp)
|
---|
4786 | if ($win64) {
|
---|
4787 | $rec="%rcx";
|
---|
4788 | $frame="%rdx";
|
---|
4789 | $context="%r8";
|
---|
4790 | $disp="%r9";
|
---|
4791 |
|
---|
4792 | $code.=<<___;
|
---|
4793 | .extern __imp_RtlVirtualUnwind
|
---|
4794 | ___
|
---|
4795 | $code.=<<___ if ($PREFIX eq "aesni");
|
---|
4796 | .type ecb_ccm64_se_handler,\@abi-omnipotent
|
---|
4797 | .align 16
|
---|
4798 | ecb_ccm64_se_handler:
|
---|
4799 | push %rsi
|
---|
4800 | push %rdi
|
---|
4801 | push %rbx
|
---|
4802 | push %rbp
|
---|
4803 | push %r12
|
---|
4804 | push %r13
|
---|
4805 | push %r14
|
---|
4806 | push %r15
|
---|
4807 | pushfq
|
---|
4808 | sub \$64,%rsp
|
---|
4809 |
|
---|
4810 | mov 120($context),%rax # pull context->Rax
|
---|
4811 | mov 248($context),%rbx # pull context->Rip
|
---|
4812 |
|
---|
4813 | mov 8($disp),%rsi # disp->ImageBase
|
---|
4814 | mov 56($disp),%r11 # disp->HandlerData
|
---|
4815 |
|
---|
4816 | mov 0(%r11),%r10d # HandlerData[0]
|
---|
4817 | lea (%rsi,%r10),%r10 # prologue label
|
---|
4818 | cmp %r10,%rbx # context->Rip<prologue label
|
---|
4819 | jb .Lcommon_seh_tail
|
---|
4820 |
|
---|
4821 | mov 152($context),%rax # pull context->Rsp
|
---|
4822 |
|
---|
4823 | mov 4(%r11),%r10d # HandlerData[1]
|
---|
4824 | lea (%rsi,%r10),%r10 # epilogue label
|
---|
4825 | cmp %r10,%rbx # context->Rip>=epilogue label
|
---|
4826 | jae .Lcommon_seh_tail
|
---|
4827 |
|
---|
4828 | lea 0(%rax),%rsi # %xmm save area
|
---|
4829 | lea 512($context),%rdi # &context.Xmm6
|
---|
4830 | mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
|
---|
4831 | .long 0xa548f3fc # cld; rep movsq
|
---|
4832 | lea 0x58(%rax),%rax # adjust stack pointer
|
---|
4833 |
|
---|
4834 | jmp .Lcommon_seh_tail
|
---|
4835 | .size ecb_ccm64_se_handler,.-ecb_ccm64_se_handler
|
---|
4836 |
|
---|
4837 | .type ctr_xts_se_handler,\@abi-omnipotent
|
---|
4838 | .align 16
|
---|
4839 | ctr_xts_se_handler:
|
---|
4840 | push %rsi
|
---|
4841 | push %rdi
|
---|
4842 | push %rbx
|
---|
4843 | push %rbp
|
---|
4844 | push %r12
|
---|
4845 | push %r13
|
---|
4846 | push %r14
|
---|
4847 | push %r15
|
---|
4848 | pushfq
|
---|
4849 | sub \$64,%rsp
|
---|
4850 |
|
---|
4851 | mov 120($context),%rax # pull context->Rax
|
---|
4852 | mov 248($context),%rbx # pull context->Rip
|
---|
4853 |
|
---|
4854 | mov 8($disp),%rsi # disp->ImageBase
|
---|
4855 | mov 56($disp),%r11 # disp->HandlerData
|
---|
4856 |
|
---|
4857 | mov 0(%r11),%r10d # HandlerData[0]
|
---|
4858 | lea (%rsi,%r10),%r10 # prologue label
|
---|
4859 | cmp %r10,%rbx # context->Rip<prologue label
|
---|
4860 | jb .Lcommon_seh_tail
|
---|
4861 |
|
---|
4862 | mov 152($context),%rax # pull context->Rsp
|
---|
4863 |
|
---|
4864 | mov 4(%r11),%r10d # HandlerData[1]
|
---|
4865 | lea (%rsi,%r10),%r10 # epilogue label
|
---|
4866 | cmp %r10,%rbx # context->Rip>=epilogue label
|
---|
4867 | jae .Lcommon_seh_tail
|
---|
4868 |
|
---|
4869 | mov 208($context),%rax # pull context->R11
|
---|
4870 |
|
---|
4871 | lea -0xa8(%rax),%rsi # %xmm save area
|
---|
4872 | lea 512($context),%rdi # & context.Xmm6
|
---|
4873 | mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
|
---|
4874 | .long 0xa548f3fc # cld; rep movsq
|
---|
4875 |
|
---|
4876 | mov -8(%rax),%rbp # restore saved %rbp
|
---|
4877 | mov %rbp,160($context) # restore context->Rbp
|
---|
4878 | jmp .Lcommon_seh_tail
|
---|
4879 | .size ctr_xts_se_handler,.-ctr_xts_se_handler
|
---|
4880 |
|
---|
4881 | .type ocb_se_handler,\@abi-omnipotent
|
---|
4882 | .align 16
|
---|
4883 | ocb_se_handler:
|
---|
4884 | push %rsi
|
---|
4885 | push %rdi
|
---|
4886 | push %rbx
|
---|
4887 | push %rbp
|
---|
4888 | push %r12
|
---|
4889 | push %r13
|
---|
4890 | push %r14
|
---|
4891 | push %r15
|
---|
4892 | pushfq
|
---|
4893 | sub \$64,%rsp
|
---|
4894 |
|
---|
4895 | mov 120($context),%rax # pull context->Rax
|
---|
4896 | mov 248($context),%rbx # pull context->Rip
|
---|
4897 |
|
---|
4898 | mov 8($disp),%rsi # disp->ImageBase
|
---|
4899 | mov 56($disp),%r11 # disp->HandlerData
|
---|
4900 |
|
---|
4901 | mov 0(%r11),%r10d # HandlerData[0]
|
---|
4902 | lea (%rsi,%r10),%r10 # prologue label
|
---|
4903 | cmp %r10,%rbx # context->Rip<prologue label
|
---|
4904 | jb .Lcommon_seh_tail
|
---|
4905 |
|
---|
4906 | mov 4(%r11),%r10d # HandlerData[1]
|
---|
4907 | lea (%rsi,%r10),%r10 # epilogue label
|
---|
4908 | cmp %r10,%rbx # context->Rip>=epilogue label
|
---|
4909 | jae .Lcommon_seh_tail
|
---|
4910 |
|
---|
4911 | mov 8(%r11),%r10d # HandlerData[2]
|
---|
4912 | lea (%rsi,%r10),%r10
|
---|
4913 | cmp %r10,%rbx # context->Rip>=pop label
|
---|
4914 | jae .Locb_no_xmm
|
---|
4915 |
|
---|
4916 | mov 152($context),%rax # pull context->Rsp
|
---|
4917 |
|
---|
4918 | lea (%rax),%rsi # %xmm save area
|
---|
4919 | lea 512($context),%rdi # & context.Xmm6
|
---|
4920 | mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
|
---|
4921 | .long 0xa548f3fc # cld; rep movsq
|
---|
4922 | lea 0xa0+0x28(%rax),%rax
|
---|
4923 |
|
---|
4924 | .Locb_no_xmm:
|
---|
4925 | mov -8(%rax),%rbx
|
---|
4926 | mov -16(%rax),%rbp
|
---|
4927 | mov -24(%rax),%r12
|
---|
4928 | mov -32(%rax),%r13
|
---|
4929 | mov -40(%rax),%r14
|
---|
4930 |
|
---|
4931 | mov %rbx,144($context) # restore context->Rbx
|
---|
4932 | mov %rbp,160($context) # restore context->Rbp
|
---|
4933 | mov %r12,216($context) # restore context->R12
|
---|
4934 | mov %r13,224($context) # restore context->R13
|
---|
4935 | mov %r14,232($context) # restore context->R14
|
---|
4936 |
|
---|
4937 | jmp .Lcommon_seh_tail
|
---|
4938 | .size ocb_se_handler,.-ocb_se_handler
|
---|
4939 | ___
|
---|
4940 | $code.=<<___;
|
---|
4941 | .type cbc_se_handler,\@abi-omnipotent
|
---|
4942 | .align 16
|
---|
4943 | cbc_se_handler:
|
---|
4944 | push %rsi
|
---|
4945 | push %rdi
|
---|
4946 | push %rbx
|
---|
4947 | push %rbp
|
---|
4948 | push %r12
|
---|
4949 | push %r13
|
---|
4950 | push %r14
|
---|
4951 | push %r15
|
---|
4952 | pushfq
|
---|
4953 | sub \$64,%rsp
|
---|
4954 |
|
---|
4955 | mov 152($context),%rax # pull context->Rsp
|
---|
4956 | mov 248($context),%rbx # pull context->Rip
|
---|
4957 |
|
---|
4958 | lea .Lcbc_decrypt_bulk(%rip),%r10
|
---|
4959 | cmp %r10,%rbx # context->Rip<"prologue" label
|
---|
4960 | jb .Lcommon_seh_tail
|
---|
4961 |
|
---|
4962 | mov 120($context),%rax # pull context->Rax
|
---|
4963 |
|
---|
4964 | lea .Lcbc_decrypt_body(%rip),%r10
|
---|
4965 | cmp %r10,%rbx # context->Rip<cbc_decrypt_body
|
---|
4966 | jb .Lcommon_seh_tail
|
---|
4967 |
|
---|
4968 | mov 152($context),%rax # pull context->Rsp
|
---|
4969 |
|
---|
4970 | lea .Lcbc_ret(%rip),%r10
|
---|
4971 | cmp %r10,%rbx # context->Rip>="epilogue" label
|
---|
4972 | jae .Lcommon_seh_tail
|
---|
4973 |
|
---|
4974 | lea 16(%rax),%rsi # %xmm save area
|
---|
4975 | lea 512($context),%rdi # &context.Xmm6
|
---|
4976 | mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
|
---|
4977 | .long 0xa548f3fc # cld; rep movsq
|
---|
4978 |
|
---|
4979 | mov 208($context),%rax # pull context->R11
|
---|
4980 |
|
---|
4981 | mov -8(%rax),%rbp # restore saved %rbp
|
---|
4982 | mov %rbp,160($context) # restore context->Rbp
|
---|
4983 |
|
---|
4984 | .Lcommon_seh_tail:
|
---|
4985 | mov 8(%rax),%rdi
|
---|
4986 | mov 16(%rax),%rsi
|
---|
4987 | mov %rax,152($context) # restore context->Rsp
|
---|
4988 | mov %rsi,168($context) # restore context->Rsi
|
---|
4989 | mov %rdi,176($context) # restore context->Rdi
|
---|
4990 |
|
---|
4991 | mov 40($disp),%rdi # disp->ContextRecord
|
---|
4992 | mov $context,%rsi # context
|
---|
4993 | mov \$154,%ecx # sizeof(CONTEXT)
|
---|
4994 | .long 0xa548f3fc # cld; rep movsq
|
---|
4995 |
|
---|
4996 | mov $disp,%rsi
|
---|
4997 | xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
|
---|
4998 | mov 8(%rsi),%rdx # arg2, disp->ImageBase
|
---|
4999 | mov 0(%rsi),%r8 # arg3, disp->ControlPc
|
---|
5000 | mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
|
---|
5001 | mov 40(%rsi),%r10 # disp->ContextRecord
|
---|
5002 | lea 56(%rsi),%r11 # &disp->HandlerData
|
---|
5003 | lea 24(%rsi),%r12 # &disp->EstablisherFrame
|
---|
5004 | mov %r10,32(%rsp) # arg5
|
---|
5005 | mov %r11,40(%rsp) # arg6
|
---|
5006 | mov %r12,48(%rsp) # arg7
|
---|
5007 | mov %rcx,56(%rsp) # arg8, (NULL)
|
---|
5008 | call *__imp_RtlVirtualUnwind(%rip)
|
---|
5009 |
|
---|
5010 | mov \$1,%eax # ExceptionContinueSearch
|
---|
5011 | add \$64,%rsp
|
---|
5012 | popfq
|
---|
5013 | pop %r15
|
---|
5014 | pop %r14
|
---|
5015 | pop %r13
|
---|
5016 | pop %r12
|
---|
5017 | pop %rbp
|
---|
5018 | pop %rbx
|
---|
5019 | pop %rdi
|
---|
5020 | pop %rsi
|
---|
5021 | ret
|
---|
5022 | .size cbc_se_handler,.-cbc_se_handler
|
---|
5023 |
|
---|
5024 | .section .pdata
|
---|
5025 | .align 4
|
---|
5026 | ___
|
---|
5027 | $code.=<<___ if ($PREFIX eq "aesni");
|
---|
5028 | .rva .LSEH_begin_aesni_ecb_encrypt
|
---|
5029 | .rva .LSEH_end_aesni_ecb_encrypt
|
---|
5030 | .rva .LSEH_info_ecb
|
---|
5031 |
|
---|
5032 | .rva .LSEH_begin_aesni_ccm64_encrypt_blocks
|
---|
5033 | .rva .LSEH_end_aesni_ccm64_encrypt_blocks
|
---|
5034 | .rva .LSEH_info_ccm64_enc
|
---|
5035 |
|
---|
5036 | .rva .LSEH_begin_aesni_ccm64_decrypt_blocks
|
---|
5037 | .rva .LSEH_end_aesni_ccm64_decrypt_blocks
|
---|
5038 | .rva .LSEH_info_ccm64_dec
|
---|
5039 |
|
---|
5040 | .rva .LSEH_begin_aesni_ctr32_encrypt_blocks
|
---|
5041 | .rva .LSEH_end_aesni_ctr32_encrypt_blocks
|
---|
5042 | .rva .LSEH_info_ctr32
|
---|
5043 |
|
---|
5044 | .rva .LSEH_begin_aesni_xts_encrypt
|
---|
5045 | .rva .LSEH_end_aesni_xts_encrypt
|
---|
5046 | .rva .LSEH_info_xts_enc
|
---|
5047 |
|
---|
5048 | .rva .LSEH_begin_aesni_xts_decrypt
|
---|
5049 | .rva .LSEH_end_aesni_xts_decrypt
|
---|
5050 | .rva .LSEH_info_xts_dec
|
---|
5051 |
|
---|
5052 | .rva .LSEH_begin_aesni_ocb_encrypt
|
---|
5053 | .rva .LSEH_end_aesni_ocb_encrypt
|
---|
5054 | .rva .LSEH_info_ocb_enc
|
---|
5055 |
|
---|
5056 | .rva .LSEH_begin_aesni_ocb_decrypt
|
---|
5057 | .rva .LSEH_end_aesni_ocb_decrypt
|
---|
5058 | .rva .LSEH_info_ocb_dec
|
---|
5059 | ___
|
---|
5060 | $code.=<<___;
|
---|
5061 | .rva .LSEH_begin_${PREFIX}_cbc_encrypt
|
---|
5062 | .rva .LSEH_end_${PREFIX}_cbc_encrypt
|
---|
5063 | .rva .LSEH_info_cbc
|
---|
5064 |
|
---|
5065 | .rva ${PREFIX}_set_decrypt_key
|
---|
5066 | .rva .LSEH_end_set_decrypt_key
|
---|
5067 | .rva .LSEH_info_key
|
---|
5068 |
|
---|
5069 | .rva ${PREFIX}_set_encrypt_key
|
---|
5070 | .rva .LSEH_end_set_encrypt_key
|
---|
5071 | .rva .LSEH_info_key
|
---|
5072 | .section .xdata
|
---|
5073 | .align 8
|
---|
5074 | ___
|
---|
5075 | $code.=<<___ if ($PREFIX eq "aesni");
|
---|
5076 | .LSEH_info_ecb:
|
---|
5077 | .byte 9,0,0,0
|
---|
5078 | .rva ecb_ccm64_se_handler
|
---|
5079 | .rva .Lecb_enc_body,.Lecb_enc_ret # HandlerData[]
|
---|
5080 | .LSEH_info_ccm64_enc:
|
---|
5081 | .byte 9,0,0,0
|
---|
5082 | .rva ecb_ccm64_se_handler
|
---|
5083 | .rva .Lccm64_enc_body,.Lccm64_enc_ret # HandlerData[]
|
---|
5084 | .LSEH_info_ccm64_dec:
|
---|
5085 | .byte 9,0,0,0
|
---|
5086 | .rva ecb_ccm64_se_handler
|
---|
5087 | .rva .Lccm64_dec_body,.Lccm64_dec_ret # HandlerData[]
|
---|
5088 | .LSEH_info_ctr32:
|
---|
5089 | .byte 9,0,0,0
|
---|
5090 | .rva ctr_xts_se_handler
|
---|
5091 | .rva .Lctr32_body,.Lctr32_epilogue # HandlerData[]
|
---|
5092 | .LSEH_info_xts_enc:
|
---|
5093 | .byte 9,0,0,0
|
---|
5094 | .rva ctr_xts_se_handler
|
---|
5095 | .rva .Lxts_enc_body,.Lxts_enc_epilogue # HandlerData[]
|
---|
5096 | .LSEH_info_xts_dec:
|
---|
5097 | .byte 9,0,0,0
|
---|
5098 | .rva ctr_xts_se_handler
|
---|
5099 | .rva .Lxts_dec_body,.Lxts_dec_epilogue # HandlerData[]
|
---|
5100 | .LSEH_info_ocb_enc:
|
---|
5101 | .byte 9,0,0,0
|
---|
5102 | .rva ocb_se_handler
|
---|
5103 | .rva .Locb_enc_body,.Locb_enc_epilogue # HandlerData[]
|
---|
5104 | .rva .Locb_enc_pop
|
---|
5105 | .long 0
|
---|
5106 | .LSEH_info_ocb_dec:
|
---|
5107 | .byte 9,0,0,0
|
---|
5108 | .rva ocb_se_handler
|
---|
5109 | .rva .Locb_dec_body,.Locb_dec_epilogue # HandlerData[]
|
---|
5110 | .rva .Locb_dec_pop
|
---|
5111 | .long 0
|
---|
5112 | ___
|
---|
5113 | $code.=<<___;
|
---|
5114 | .LSEH_info_cbc:
|
---|
5115 | .byte 9,0,0,0
|
---|
5116 | .rva cbc_se_handler
|
---|
5117 | .LSEH_info_key:
|
---|
5118 | .byte 0x01,0x04,0x01,0x00
|
---|
5119 | .byte 0x04,0x02,0x00,0x00 # sub rsp,8
|
---|
5120 | ___
|
---|
5121 | }
|
---|
5122 |
|
---|
5123 | sub rex {
|
---|
5124 | local *opcode=shift;
|
---|
5125 | my ($dst,$src)=@_;
|
---|
5126 | my $rex=0;
|
---|
5127 |
|
---|
5128 | $rex|=0x04 if($dst>=8);
|
---|
5129 | $rex|=0x01 if($src>=8);
|
---|
5130 | push @opcode,$rex|0x40 if($rex);
|
---|
5131 | }
|
---|
5132 |
|
---|
5133 | sub aesni {
|
---|
5134 | my $line=shift;
|
---|
5135 | my @opcode=(0x66);
|
---|
5136 |
|
---|
5137 | if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
|
---|
5138 | rex(\@opcode,$4,$3);
|
---|
5139 | push @opcode,0x0f,0x3a,0xdf;
|
---|
5140 | push @opcode,0xc0|($3&7)|(($4&7)<<3); # ModR/M
|
---|
5141 | my $c=$2;
|
---|
5142 | push @opcode,$c=~/^0/?oct($c):$c;
|
---|
5143 | return ".byte\t".join(',',@opcode);
|
---|
5144 | }
|
---|
5145 | elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
|
---|
5146 | my %opcodelet = (
|
---|
5147 | "aesimc" => 0xdb,
|
---|
5148 | "aesenc" => 0xdc, "aesenclast" => 0xdd,
|
---|
5149 | "aesdec" => 0xde, "aesdeclast" => 0xdf
|
---|
5150 | );
|
---|
5151 | return undef if (!defined($opcodelet{$1}));
|
---|
5152 | rex(\@opcode,$3,$2);
|
---|
5153 | push @opcode,0x0f,0x38,$opcodelet{$1};
|
---|
5154 | push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
|
---|
5155 | return ".byte\t".join(',',@opcode);
|
---|
5156 | }
|
---|
5157 | elsif ($line=~/(aes[a-z]+)\s+([0x1-9a-fA-F]*)\(%rsp\),\s*%xmm([0-9]+)/) {
|
---|
5158 | my %opcodelet = (
|
---|
5159 | "aesenc" => 0xdc, "aesenclast" => 0xdd,
|
---|
5160 | "aesdec" => 0xde, "aesdeclast" => 0xdf
|
---|
5161 | );
|
---|
5162 | return undef if (!defined($opcodelet{$1}));
|
---|
5163 | my $off = $2;
|
---|
5164 | push @opcode,0x44 if ($3>=8);
|
---|
5165 | push @opcode,0x0f,0x38,$opcodelet{$1};
|
---|
5166 | push @opcode,0x44|(($3&7)<<3),0x24; # ModR/M
|
---|
5167 | push @opcode,($off=~/^0/?oct($off):$off)&0xff;
|
---|
5168 | return ".byte\t".join(',',@opcode);
|
---|
5169 | }
|
---|
5170 | return $line;
|
---|
5171 | }
|
---|
5172 |
|
---|
5173 | sub movbe {
|
---|
5174 | ".byte 0x0f,0x38,0xf1,0x44,0x24,".shift;
|
---|
5175 | }
|
---|
5176 |
|
---|
5177 | $code =~ s/\`([^\`]*)\`/eval($1)/gem;
|
---|
5178 | $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;
|
---|
5179 | #$code =~ s/\bmovbe\s+%eax/bswap %eax; mov %eax/gm; # debugging artefact
|
---|
5180 | $code =~ s/\bmovbe\s+%eax,\s*([0-9]+)\(%rsp\)/movbe($1)/gem;
|
---|
5181 |
|
---|
5182 | print $code;
|
---|
5183 |
|
---|
5184 | close STDOUT or die "error closing STDOUT: $!";
|
---|