Invoke tear_down when exiting test_encode_tls_sct() prematurely
[openssl.git] / crypto / aes / asm / bsaes-armv7.pl
1 #! /usr/bin/env perl
2 # Copyright 2012-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 <appro@openssl.org> 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 # Specific modes and adaptation for Linux kernel by Ard Biesheuvel
17 # of Linaro. Permission to use under GPL terms is granted.
18 # ====================================================================
19
20 # Bit-sliced AES for ARM NEON
21 #
22 # February 2012.
23 #
24 # This implementation is direct adaptation of bsaes-x86_64 module for
25 # ARM NEON. Except that this module is endian-neutral [in sense that
26 # it can be compiled for either endianness] by courtesy of vld1.8's
27 # neutrality. Initial version doesn't implement interface to OpenSSL,
28 # only low-level primitives and unsupported entry points, just enough
29 # to collect performance results, which for Cortex-A8 core are:
30 #
31 # encrypt       19.5 cycles per byte processed with 128-bit key
32 # decrypt       22.1 cycles per byte processed with 128-bit key
33 # key conv.     440  cycles per 128-bit key/0.18 of 8x block
34 #
35 # Snapdragon S4 encrypts byte in 17.6 cycles and decrypts in 19.7,
36 # which is [much] worse than anticipated (for further details see
37 # http://www.openssl.org/~appro/Snapdragon-S4.html).
38 #
39 # Cortex-A15 manages in 14.2/16.1 cycles [when integer-only code
40 # manages in 20.0 cycles].
41 #
42 # When comparing to x86_64 results keep in mind that NEON unit is
43 # [mostly] single-issue and thus can't [fully] benefit from
44 # instruction-level parallelism. And when comparing to aes-armv4
45 # results keep in mind key schedule conversion overhead (see
46 # bsaes-x86_64.pl for further details)...
47 #
48 #                                               <appro@openssl.org>
49
50 # April-August 2013
51 # Add CBC, CTR and XTS subroutines and adapt for kernel use; courtesy of Ard.
52
53 # $output is the last argument if it looks like a file (it has an extension)
54 # $flavour is the first argument if it doesn't look like a file
55 $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
56 $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
57
58 if ($flavour && $flavour ne "void") {
59     $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
60     ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
61     ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
62     die "can't locate arm-xlate.pl";
63
64     open STDOUT,"| \"$^X\" $xlate $flavour \"$output\""
65         or die "can't call $xlate: $!";
66 } else {
67     $output and open STDOUT,">$output";
68 }
69
70 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
71 my @XMM=map("q$_",(0..15));
72
73 {
74 my ($key,$rounds,$const)=("r4","r5","r6");
75
76 sub Dlo()   { shift=~m|q([1]?[0-9])|?"d".($1*2):"";     }
77 sub Dhi()   { shift=~m|q([1]?[0-9])|?"d".($1*2+1):"";   }
78
79 sub Sbox {
80 # input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
81 # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
82 my @b=@_[0..7];
83 my @t=@_[8..11];
84 my @s=@_[12..15];
85         &InBasisChange  (@b);
86         &Inv_GF256      (@b[6,5,0,3,7,1,4,2],@t,@s);
87         &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
88 }
89
90 sub InBasisChange {
91 # input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
92 # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
93 my @b=@_[0..7];
94 $code.=<<___;
95         veor    @b[2], @b[2], @b[1]
96         veor    @b[5], @b[5], @b[6]
97         veor    @b[3], @b[3], @b[0]
98         veor    @b[6], @b[6], @b[2]
99         veor    @b[5], @b[5], @b[0]
100
101         veor    @b[6], @b[6], @b[3]
102         veor    @b[3], @b[3], @b[7]
103         veor    @b[7], @b[7], @b[5]
104         veor    @b[3], @b[3], @b[4]
105         veor    @b[4], @b[4], @b[5]
106
107         veor    @b[2], @b[2], @b[7]
108         veor    @b[3], @b[3], @b[1]
109         veor    @b[1], @b[1], @b[5]
110 ___
111 }
112
113 sub OutBasisChange {
114 # input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
115 # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
116 my @b=@_[0..7];
117 $code.=<<___;
118         veor    @b[0], @b[0], @b[6]
119         veor    @b[1], @b[1], @b[4]
120         veor    @b[4], @b[4], @b[6]
121         veor    @b[2], @b[2], @b[0]
122         veor    @b[6], @b[6], @b[1]
123
124         veor    @b[1], @b[1], @b[5]
125         veor    @b[5], @b[5], @b[3]
126         veor    @b[3], @b[3], @b[7]
127         veor    @b[7], @b[7], @b[5]
128         veor    @b[2], @b[2], @b[5]
129
130         veor    @b[4], @b[4], @b[7]
131 ___
132 }
133
134 sub InvSbox {
135 # input in lsb  > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
136 # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
137 my @b=@_[0..7];
138 my @t=@_[8..11];
139 my @s=@_[12..15];
140         &InvInBasisChange       (@b);
141         &Inv_GF256              (@b[5,1,2,6,3,7,0,4],@t,@s);
142         &InvOutBasisChange      (@b[3,7,0,4,5,1,2,6]);
143 }
144
145 sub InvInBasisChange {          # OutBasisChange in reverse (with twist)
146 my @b=@_[5,1,2,6,3,7,0,4];
147 $code.=<<___
148          veor   @b[1], @b[1], @b[7]
149         veor    @b[4], @b[4], @b[7]
150
151         veor    @b[7], @b[7], @b[5]
152          veor   @b[1], @b[1], @b[3]
153         veor    @b[2], @b[2], @b[5]
154         veor    @b[3], @b[3], @b[7]
155
156         veor    @b[6], @b[6], @b[1]
157         veor    @b[2], @b[2], @b[0]
158          veor   @b[5], @b[5], @b[3]
159         veor    @b[4], @b[4], @b[6]
160         veor    @b[0], @b[0], @b[6]
161         veor    @b[1], @b[1], @b[4]
162 ___
163 }
164
165 sub InvOutBasisChange {         # InBasisChange in reverse
166 my @b=@_[2,5,7,3,6,1,0,4];
167 $code.=<<___;
168         veor    @b[1], @b[1], @b[5]
169         veor    @b[2], @b[2], @b[7]
170
171         veor    @b[3], @b[3], @b[1]
172         veor    @b[4], @b[4], @b[5]
173         veor    @b[7], @b[7], @b[5]
174         veor    @b[3], @b[3], @b[4]
175          veor   @b[5], @b[5], @b[0]
176         veor    @b[3], @b[3], @b[7]
177          veor   @b[6], @b[6], @b[2]
178          veor   @b[2], @b[2], @b[1]
179         veor    @b[6], @b[6], @b[3]
180
181         veor    @b[3], @b[3], @b[0]
182         veor    @b[5], @b[5], @b[6]
183 ___
184 }
185
186 sub Mul_GF4 {
187 #;*************************************************************
188 #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
189 #;*************************************************************
190 my ($x0,$x1,$y0,$y1,$t0,$t1)=@_;
191 $code.=<<___;
192         veor    $t0, $y0, $y1
193         vand    $t0, $t0, $x0
194         veor    $x0, $x0, $x1
195         vand    $t1, $x1, $y0
196         vand    $x0, $x0, $y1
197         veor    $x1, $t1, $t0
198         veor    $x0, $x0, $t1
199 ___
200 }
201
202 sub Mul_GF4_N {                         # not used, see next subroutine
203 # multiply and scale by N
204 my ($x0,$x1,$y0,$y1,$t0)=@_;
205 $code.=<<___;
206         veor    $t0, $y0, $y1
207         vand    $t0, $t0, $x0
208         veor    $x0, $x0, $x1
209         vand    $x1, $x1, $y0
210         vand    $x0, $x0, $y1
211         veor    $x1, $x1, $x0
212         veor    $x0, $x0, $t0
213 ___
214 }
215
216 sub Mul_GF4_N_GF4 {
217 # interleaved Mul_GF4_N and Mul_GF4
218 my ($x0,$x1,$y0,$y1,$t0,
219     $x2,$x3,$y2,$y3,$t1)=@_;
220 $code.=<<___;
221         veor    $t0, $y0, $y1
222          veor   $t1, $y2, $y3
223         vand    $t0, $t0, $x0
224          vand   $t1, $t1, $x2
225         veor    $x0, $x0, $x1
226          veor   $x2, $x2, $x3
227         vand    $x1, $x1, $y0
228          vand   $x3, $x3, $y2
229         vand    $x0, $x0, $y1
230          vand   $x2, $x2, $y3
231         veor    $x1, $x1, $x0
232          veor   $x2, $x2, $x3
233         veor    $x0, $x0, $t0
234          veor   $x3, $x3, $t1
235 ___
236 }
237 sub Mul_GF16_2 {
238 my @x=@_[0..7];
239 my @y=@_[8..11];
240 my @t=@_[12..15];
241 $code.=<<___;
242         veor    @t[0], @x[0], @x[2]
243         veor    @t[1], @x[1], @x[3]
244 ___
245         &Mul_GF4        (@x[0], @x[1], @y[0], @y[1], @t[2..3]);
246 $code.=<<___;
247         veor    @y[0], @y[0], @y[2]
248         veor    @y[1], @y[1], @y[3]
249 ___
250         Mul_GF4_N_GF4   (@t[0], @t[1], @y[0], @y[1], @t[3],
251                          @x[2], @x[3], @y[2], @y[3], @t[2]);
252 $code.=<<___;
253         veor    @x[0], @x[0], @t[0]
254         veor    @x[2], @x[2], @t[0]
255         veor    @x[1], @x[1], @t[1]
256         veor    @x[3], @x[3], @t[1]
257
258         veor    @t[0], @x[4], @x[6]
259         veor    @t[1], @x[5], @x[7]
260 ___
261         &Mul_GF4_N_GF4  (@t[0], @t[1], @y[0], @y[1], @t[3],
262                          @x[6], @x[7], @y[2], @y[3], @t[2]);
263 $code.=<<___;
264         veor    @y[0], @y[0], @y[2]
265         veor    @y[1], @y[1], @y[3]
266 ___
267         &Mul_GF4        (@x[4], @x[5], @y[0], @y[1], @t[2..3]);
268 $code.=<<___;
269         veor    @x[4], @x[4], @t[0]
270         veor    @x[6], @x[6], @t[0]
271         veor    @x[5], @x[5], @t[1]
272         veor    @x[7], @x[7], @t[1]
273 ___
274 }
275 sub Inv_GF256 {
276 #;********************************************************************
277 #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144)       *
278 #;********************************************************************
279 my @x=@_[0..7];
280 my @t=@_[8..11];
281 my @s=@_[12..15];
282 # direct optimizations from hardware
283 $code.=<<___;
284         veor    @t[3], @x[4], @x[6]
285         veor    @t[2], @x[5], @x[7]
286         veor    @t[1], @x[1], @x[3]
287         veor    @s[1], @x[7], @x[6]
288          vmov   @t[0], @t[2]
289         veor    @s[0], @x[0], @x[2]
290
291         vorr    @t[2], @t[2], @t[1]
292         veor    @s[3], @t[3], @t[0]
293         vand    @s[2], @t[3], @s[0]
294         vorr    @t[3], @t[3], @s[0]
295         veor    @s[0], @s[0], @t[1]
296         vand    @t[0], @t[0], @t[1]
297         veor    @t[1], @x[3], @x[2]
298         vand    @s[3], @s[3], @s[0]
299         vand    @s[1], @s[1], @t[1]
300         veor    @t[1], @x[4], @x[5]
301         veor    @s[0], @x[1], @x[0]
302         veor    @t[3], @t[3], @s[1]
303         veor    @t[2], @t[2], @s[1]
304         vand    @s[1], @t[1], @s[0]
305         vorr    @t[1], @t[1], @s[0]
306         veor    @t[3], @t[3], @s[3]
307         veor    @t[0], @t[0], @s[1]
308         veor    @t[2], @t[2], @s[2]
309         veor    @t[1], @t[1], @s[3]
310         veor    @t[0], @t[0], @s[2]
311         vand    @s[0], @x[7], @x[3]
312         veor    @t[1], @t[1], @s[2]
313         vand    @s[1], @x[6], @x[2]
314         vand    @s[2], @x[5], @x[1]
315         vorr    @s[3], @x[4], @x[0]
316         veor    @t[3], @t[3], @s[0]
317         veor    @t[1], @t[1], @s[2]
318         veor    @t[0], @t[0], @s[3]
319         veor    @t[2], @t[2], @s[1]
320
321         @ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
322
323         @ new smaller inversion
324
325         vand    @s[2], @t[3], @t[1]
326         vmov    @s[0], @t[0]
327
328         veor    @s[1], @t[2], @s[2]
329         veor    @s[3], @t[0], @s[2]
330         veor    @s[2], @t[0], @s[2]     @ @s[2]=@s[3]
331
332         vbsl    @s[1], @t[1], @t[0]
333         vbsl    @s[3], @t[3], @t[2]
334         veor    @t[3], @t[3], @t[2]
335
336         vbsl    @s[0], @s[1], @s[2]
337         vbsl    @t[0], @s[2], @s[1]
338
339         vand    @s[2], @s[0], @s[3]
340         veor    @t[1], @t[1], @t[0]
341
342         veor    @s[2], @s[2], @t[3]
343 ___
344 # output in s3, s2, s1, t1
345
346 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
347
348 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
349         &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
350
351 ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
352 }
353
354 # AES linear components
355
356 sub ShiftRows {
357 my @x=@_[0..7];
358 my @t=@_[8..11];
359 my $mask=pop;
360 $code.=<<___;
361         vldmia  $key!, {@t[0]-@t[3]}
362         veor    @t[0], @t[0], @x[0]
363         veor    @t[1], @t[1], @x[1]
364         vtbl.8  `&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
365         vtbl.8  `&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
366         vldmia  $key!, {@t[0]}
367         veor    @t[2], @t[2], @x[2]
368         vtbl.8  `&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
369         vtbl.8  `&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
370         vldmia  $key!, {@t[1]}
371         veor    @t[3], @t[3], @x[3]
372         vtbl.8  `&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
373         vtbl.8  `&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
374         vldmia  $key!, {@t[2]}
375         vtbl.8  `&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
376         vtbl.8  `&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
377         vldmia  $key!, {@t[3]}
378         veor    @t[0], @t[0], @x[4]
379         veor    @t[1], @t[1], @x[5]
380         vtbl.8  `&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
381         vtbl.8  `&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
382         veor    @t[2], @t[2], @x[6]
383         vtbl.8  `&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
384         vtbl.8  `&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
385         veor    @t[3], @t[3], @x[7]
386         vtbl.8  `&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
387         vtbl.8  `&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
388         vtbl.8  `&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
389         vtbl.8  `&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
390 ___
391 }
392
393 sub MixColumns {
394 # modified to emit output in order suitable for feeding back to aesenc[last]
395 my @x=@_[0..7];
396 my @t=@_[8..15];
397 my $inv=@_[16]; # optional
398 $code.=<<___;
399         vext.8  @t[0], @x[0], @x[0], #12        @ x0 <<< 32
400         vext.8  @t[1], @x[1], @x[1], #12
401          veor   @x[0], @x[0], @t[0]             @ x0 ^ (x0 <<< 32)
402         vext.8  @t[2], @x[2], @x[2], #12
403          veor   @x[1], @x[1], @t[1]
404         vext.8  @t[3], @x[3], @x[3], #12
405          veor   @x[2], @x[2], @t[2]
406         vext.8  @t[4], @x[4], @x[4], #12
407          veor   @x[3], @x[3], @t[3]
408         vext.8  @t[5], @x[5], @x[5], #12
409          veor   @x[4], @x[4], @t[4]
410         vext.8  @t[6], @x[6], @x[6], #12
411          veor   @x[5], @x[5], @t[5]
412         vext.8  @t[7], @x[7], @x[7], #12
413          veor   @x[6], @x[6], @t[6]
414
415         veor    @t[1], @t[1], @x[0]
416          veor   @x[7], @x[7], @t[7]
417          vext.8 @x[0], @x[0], @x[0], #8         @ (x0 ^ (x0 <<< 32)) <<< 64)
418         veor    @t[2], @t[2], @x[1]
419         veor    @t[0], @t[0], @x[7]
420         veor    @t[1], @t[1], @x[7]
421          vext.8 @x[1], @x[1], @x[1], #8
422         veor    @t[5], @t[5], @x[4]
423          veor   @x[0], @x[0], @t[0]
424         veor    @t[6], @t[6], @x[5]
425          veor   @x[1], @x[1], @t[1]
426          vext.8 @t[0], @x[4], @x[4], #8
427         veor    @t[4], @t[4], @x[3]
428          vext.8 @t[1], @x[5], @x[5], #8
429         veor    @t[7], @t[7], @x[6]
430          vext.8 @x[4], @x[3], @x[3], #8
431         veor    @t[3], @t[3], @x[2]
432          vext.8 @x[5], @x[7], @x[7], #8
433         veor    @t[4], @t[4], @x[7]
434          vext.8 @x[3], @x[6], @x[6], #8
435         veor    @t[3], @t[3], @x[7]
436          vext.8 @x[6], @x[2], @x[2], #8
437         veor    @x[7], @t[1], @t[5]
438 ___
439 $code.=<<___ if (!$inv);
440         veor    @x[2], @t[0], @t[4]
441         veor    @x[4], @x[4], @t[3]
442         veor    @x[5], @x[5], @t[7]
443         veor    @x[3], @x[3], @t[6]
444          @ vmov @x[2], @t[0]
445         veor    @x[6], @x[6], @t[2]
446          @ vmov @x[7], @t[1]
447 ___
448 $code.=<<___ if ($inv);
449         veor    @t[3], @t[3], @x[4]
450         veor    @x[5], @x[5], @t[7]
451         veor    @x[2], @x[3], @t[6]
452         veor    @x[3], @t[0], @t[4]
453         veor    @x[4], @x[6], @t[2]
454         vmov    @x[6], @t[3]
455          @ vmov @x[7], @t[1]
456 ___
457 }
458
459 sub InvMixColumns_orig {
460 my @x=@_[0..7];
461 my @t=@_[8..15];
462
463 $code.=<<___;
464         @ multiplication by 0x0e
465         vext.8  @t[7], @x[7], @x[7], #12
466         vmov    @t[2], @x[2]
467         veor    @x[2], @x[2], @x[5]             @ 2 5
468         veor    @x[7], @x[7], @x[5]             @ 7 5
469         vext.8  @t[0], @x[0], @x[0], #12
470         vmov    @t[5], @x[5]
471         veor    @x[5], @x[5], @x[0]             @ 5 0           [1]
472         veor    @x[0], @x[0], @x[1]             @ 0 1
473         vext.8  @t[1], @x[1], @x[1], #12
474         veor    @x[1], @x[1], @x[2]             @ 1 25
475         veor    @x[0], @x[0], @x[6]             @ 01 6          [2]
476         vext.8  @t[3], @x[3], @x[3], #12
477         veor    @x[1], @x[1], @x[3]             @ 125 3         [4]
478         veor    @x[2], @x[2], @x[0]             @ 25 016        [3]
479         veor    @x[3], @x[3], @x[7]             @ 3 75
480         veor    @x[7], @x[7], @x[6]             @ 75 6          [0]
481         vext.8  @t[6], @x[6], @x[6], #12
482         vmov    @t[4], @x[4]
483         veor    @x[6], @x[6], @x[4]             @ 6 4
484         veor    @x[4], @x[4], @x[3]             @ 4 375         [6]
485         veor    @x[3], @x[3], @x[7]             @ 375 756=36
486         veor    @x[6], @x[6], @t[5]             @ 64 5          [7]
487         veor    @x[3], @x[3], @t[2]             @ 36 2
488         vext.8  @t[5], @t[5], @t[5], #12
489         veor    @x[3], @x[3], @t[4]             @ 362 4         [5]
490 ___
491                                         my @y = @x[7,5,0,2,1,3,4,6];
492 $code.=<<___;
493         @ multiplication by 0x0b
494         veor    @y[1], @y[1], @y[0]
495         veor    @y[0], @y[0], @t[0]
496         vext.8  @t[2], @t[2], @t[2], #12
497         veor    @y[1], @y[1], @t[1]
498         veor    @y[0], @y[0], @t[5]
499         vext.8  @t[4], @t[4], @t[4], #12
500         veor    @y[1], @y[1], @t[6]
501         veor    @y[0], @y[0], @t[7]
502         veor    @t[7], @t[7], @t[6]             @ clobber t[7]
503
504         veor    @y[3], @y[3], @t[0]
505          veor   @y[1], @y[1], @y[0]
506         vext.8  @t[0], @t[0], @t[0], #12
507         veor    @y[2], @y[2], @t[1]
508         veor    @y[4], @y[4], @t[1]
509         vext.8  @t[1], @t[1], @t[1], #12
510         veor    @y[2], @y[2], @t[2]
511         veor    @y[3], @y[3], @t[2]
512         veor    @y[5], @y[5], @t[2]
513         veor    @y[2], @y[2], @t[7]
514         vext.8  @t[2], @t[2], @t[2], #12
515         veor    @y[3], @y[3], @t[3]
516         veor    @y[6], @y[6], @t[3]
517         veor    @y[4], @y[4], @t[3]
518         veor    @y[7], @y[7], @t[4]
519         vext.8  @t[3], @t[3], @t[3], #12
520         veor    @y[5], @y[5], @t[4]
521         veor    @y[7], @y[7], @t[7]
522         veor    @t[7], @t[7], @t[5]             @ clobber t[7] even more
523         veor    @y[3], @y[3], @t[5]
524         veor    @y[4], @y[4], @t[4]
525
526         veor    @y[5], @y[5], @t[7]
527         vext.8  @t[4], @t[4], @t[4], #12
528         veor    @y[6], @y[6], @t[7]
529         veor    @y[4], @y[4], @t[7]
530
531         veor    @t[7], @t[7], @t[5]
532         vext.8  @t[5], @t[5], @t[5], #12
533
534         @ multiplication by 0x0d
535         veor    @y[4], @y[4], @y[7]
536          veor   @t[7], @t[7], @t[6]             @ restore t[7]
537         veor    @y[7], @y[7], @t[4]
538         vext.8  @t[6], @t[6], @t[6], #12
539         veor    @y[2], @y[2], @t[0]
540         veor    @y[7], @y[7], @t[5]
541         vext.8  @t[7], @t[7], @t[7], #12
542         veor    @y[2], @y[2], @t[2]
543
544         veor    @y[3], @y[3], @y[1]
545         veor    @y[1], @y[1], @t[1]
546         veor    @y[0], @y[0], @t[0]
547         veor    @y[3], @y[3], @t[0]
548         veor    @y[1], @y[1], @t[5]
549         veor    @y[0], @y[0], @t[5]
550         vext.8  @t[0], @t[0], @t[0], #12
551         veor    @y[1], @y[1], @t[7]
552         veor    @y[0], @y[0], @t[6]
553         veor    @y[3], @y[3], @y[1]
554         veor    @y[4], @y[4], @t[1]
555         vext.8  @t[1], @t[1], @t[1], #12
556
557         veor    @y[7], @y[7], @t[7]
558         veor    @y[4], @y[4], @t[2]
559         veor    @y[5], @y[5], @t[2]
560         veor    @y[2], @y[2], @t[6]
561         veor    @t[6], @t[6], @t[3]             @ clobber t[6]
562         vext.8  @t[2], @t[2], @t[2], #12
563         veor    @y[4], @y[4], @y[7]
564         veor    @y[3], @y[3], @t[6]
565
566         veor    @y[6], @y[6], @t[6]
567         veor    @y[5], @y[5], @t[5]
568         vext.8  @t[5], @t[5], @t[5], #12
569         veor    @y[6], @y[6], @t[4]
570         vext.8  @t[4], @t[4], @t[4], #12
571         veor    @y[5], @y[5], @t[6]
572         veor    @y[6], @y[6], @t[7]
573         vext.8  @t[7], @t[7], @t[7], #12
574         veor    @t[6], @t[6], @t[3]             @ restore t[6]
575         vext.8  @t[3], @t[3], @t[3], #12
576
577         @ multiplication by 0x09
578         veor    @y[4], @y[4], @y[1]
579         veor    @t[1], @t[1], @y[1]             @ t[1]=y[1]
580         veor    @t[0], @t[0], @t[5]             @ clobber t[0]
581         vext.8  @t[6], @t[6], @t[6], #12
582         veor    @t[1], @t[1], @t[5]
583         veor    @y[3], @y[3], @t[0]
584         veor    @t[0], @t[0], @y[0]             @ t[0]=y[0]
585         veor    @t[1], @t[1], @t[6]
586         veor    @t[6], @t[6], @t[7]             @ clobber t[6]
587         veor    @y[4], @y[4], @t[1]
588         veor    @y[7], @y[7], @t[4]
589         veor    @y[6], @y[6], @t[3]
590         veor    @y[5], @y[5], @t[2]
591         veor    @t[4], @t[4], @y[4]             @ t[4]=y[4]
592         veor    @t[3], @t[3], @y[3]             @ t[3]=y[3]
593         veor    @t[5], @t[5], @y[5]             @ t[5]=y[5]
594         veor    @t[2], @t[2], @y[2]             @ t[2]=y[2]
595         veor    @t[3], @t[3], @t[7]
596         veor    @XMM[5], @t[5], @t[6]
597         veor    @XMM[6], @t[6], @y[6]           @ t[6]=y[6]
598         veor    @XMM[2], @t[2], @t[6]
599         veor    @XMM[7], @t[7], @y[7]           @ t[7]=y[7]
600
601         vmov    @XMM[0], @t[0]
602         vmov    @XMM[1], @t[1]
603         @ vmov  @XMM[2], @t[2]
604         vmov    @XMM[3], @t[3]
605         vmov    @XMM[4], @t[4]
606         @ vmov  @XMM[5], @t[5]
607         @ vmov  @XMM[6], @t[6]
608         @ vmov  @XMM[7], @t[7]
609 ___
610 }
611
612 sub InvMixColumns {
613 my @x=@_[0..7];
614 my @t=@_[8..15];
615
616 # Thanks to Jussi Kivilinna for providing pointer to
617 #
618 # | 0e 0b 0d 09 |   | 02 03 01 01 |   | 05 00 04 00 |
619 # | 09 0e 0b 0d | = | 01 02 03 01 | x | 00 05 00 04 |
620 # | 0d 09 0e 0b |   | 01 01 02 03 |   | 04 00 05 00 |
621 # | 0b 0d 09 0e |   | 03 01 01 02 |   | 00 04 00 05 |
622
623 $code.=<<___;
624         @ multiplication by 0x05-0x00-0x04-0x00
625         vext.8  @t[0], @x[0], @x[0], #8
626         vext.8  @t[6], @x[6], @x[6], #8
627         vext.8  @t[7], @x[7], @x[7], #8
628         veor    @t[0], @t[0], @x[0]
629         vext.8  @t[1], @x[1], @x[1], #8
630         veor    @t[6], @t[6], @x[6]
631         vext.8  @t[2], @x[2], @x[2], #8
632         veor    @t[7], @t[7], @x[7]
633         vext.8  @t[3], @x[3], @x[3], #8
634         veor    @t[1], @t[1], @x[1]
635         vext.8  @t[4], @x[4], @x[4], #8
636         veor    @t[2], @t[2], @x[2]
637         vext.8  @t[5], @x[5], @x[5], #8
638         veor    @t[3], @t[3], @x[3]
639         veor    @t[4], @t[4], @x[4]
640         veor    @t[5], @t[5], @x[5]
641
642          veor   @x[0], @x[0], @t[6]
643          veor   @x[1], @x[1], @t[6]
644          veor   @x[2], @x[2], @t[0]
645          veor   @x[4], @x[4], @t[2]
646          veor   @x[3], @x[3], @t[1]
647          veor   @x[1], @x[1], @t[7]
648          veor   @x[2], @x[2], @t[7]
649          veor   @x[4], @x[4], @t[6]
650          veor   @x[5], @x[5], @t[3]
651          veor   @x[3], @x[3], @t[6]
652          veor   @x[6], @x[6], @t[4]
653          veor   @x[4], @x[4], @t[7]
654          veor   @x[5], @x[5], @t[7]
655          veor   @x[7], @x[7], @t[5]
656 ___
657         &MixColumns     (@x,@t,1);      # flipped 2<->3 and 4<->6
658 }
659
660 sub swapmove {
661 my ($a,$b,$n,$mask,$t)=@_;
662 $code.=<<___;
663         vshr.u64        $t, $b, #$n
664         veor            $t, $t, $a
665         vand            $t, $t, $mask
666         veor            $a, $a, $t
667         vshl.u64        $t, $t, #$n
668         veor            $b, $b, $t
669 ___
670 }
671 sub swapmove2x {
672 my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
673 $code.=<<___;
674         vshr.u64        $t0, $b0, #$n
675          vshr.u64       $t1, $b1, #$n
676         veor            $t0, $t0, $a0
677          veor           $t1, $t1, $a1
678         vand            $t0, $t0, $mask
679          vand           $t1, $t1, $mask
680         veor            $a0, $a0, $t0
681         vshl.u64        $t0, $t0, #$n
682          veor           $a1, $a1, $t1
683          vshl.u64       $t1, $t1, #$n
684         veor            $b0, $b0, $t0
685          veor           $b1, $b1, $t1
686 ___
687 }
688
689 sub bitslice {
690 my @x=reverse(@_[0..7]);
691 my ($t0,$t1,$t2,$t3)=@_[8..11];
692 $code.=<<___;
693         vmov.i8 $t0,#0x55                       @ compose .LBS0
694         vmov.i8 $t1,#0x33                       @ compose .LBS1
695 ___
696         &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
697         &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
698 $code.=<<___;
699         vmov.i8 $t0,#0x0f                       @ compose .LBS2
700 ___
701         &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
702         &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
703
704         &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
705         &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
706 }
707
708 $code.=<<___;
709 #ifndef __KERNEL__
710 # include "arm_arch.h"
711
712 # define VFP_ABI_PUSH   vstmdb  sp!,{d8-d15}
713 # define VFP_ABI_POP    vldmia  sp!,{d8-d15}
714 # define VFP_ABI_FRAME  0x40
715 #else
716 # define VFP_ABI_PUSH
717 # define VFP_ABI_POP
718 # define VFP_ABI_FRAME  0
719 # define BSAES_ASM_EXTENDED_KEY
720 # define XTS_CHAIN_TWEAK
721 # define __ARM_ARCH__ __LINUX_ARM_ARCH__
722 # define __ARM_MAX_ARCH__ 7
723 #endif
724
725 #ifdef __thumb__
726 # define adrl adr
727 #endif
728
729 #if __ARM_MAX_ARCH__>=7
730 .arch   armv7-a
731 .fpu    neon
732
733 .syntax unified         @ ARMv7-capable assembler is expected to handle this
734 #if defined(__thumb2__) && !defined(__APPLE__)
735 .thumb
736 #else
737 .code   32
738 # undef __thumb2__
739 #endif
740
741 .text
742
743 .type   _bsaes_decrypt8,%function
744 .align  4
745 _bsaes_decrypt8:
746         adr     $const,.
747         vldmia  $key!, {@XMM[9]}                @ round 0 key
748 #if defined(__thumb2__) || defined(__APPLE__)
749         adr     $const,.LM0ISR
750 #else
751         add     $const,$const,#.LM0ISR-_bsaes_decrypt8
752 #endif
753
754         vldmia  $const!, {@XMM[8]}              @ .LM0ISR
755         veor    @XMM[10], @XMM[0], @XMM[9]      @ xor with round0 key
756         veor    @XMM[11], @XMM[1], @XMM[9]
757          vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
758          vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
759         veor    @XMM[12], @XMM[2], @XMM[9]
760          vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
761          vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
762         veor    @XMM[13], @XMM[3], @XMM[9]
763          vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
764          vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
765         veor    @XMM[14], @XMM[4], @XMM[9]
766          vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
767          vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
768         veor    @XMM[15], @XMM[5], @XMM[9]
769          vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
770          vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
771         veor    @XMM[10], @XMM[6], @XMM[9]
772          vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
773          vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
774         veor    @XMM[11], @XMM[7], @XMM[9]
775          vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
776          vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
777          vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
778          vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
779 ___
780         &bitslice       (@XMM[0..7, 8..11]);
781 $code.=<<___;
782         sub     $rounds,$rounds,#1
783         b       .Ldec_sbox
784 .align  4
785 .Ldec_loop:
786 ___
787         &ShiftRows      (@XMM[0..7, 8..12]);
788 $code.=".Ldec_sbox:\n";
789         &InvSbox        (@XMM[0..7, 8..15]);
790 $code.=<<___;
791         subs    $rounds,$rounds,#1
792         bcc     .Ldec_done
793 ___
794         &InvMixColumns  (@XMM[0,1,6,4,2,7,3,5, 8..15]);
795 $code.=<<___;
796         vldmia  $const, {@XMM[12]}              @ .LISR
797         ite     eq                              @ Thumb2 thing, sanity check in ARM
798         addeq   $const,$const,#0x10
799         bne     .Ldec_loop
800         vldmia  $const, {@XMM[12]}              @ .LISRM0
801         b       .Ldec_loop
802 .align  4
803 .Ldec_done:
804 ___
805         &bitslice       (@XMM[0,1,6,4,2,7,3,5, 8..11]);
806 $code.=<<___;
807         vldmia  $key, {@XMM[8]}                 @ last round key
808         veor    @XMM[6], @XMM[6], @XMM[8]
809         veor    @XMM[4], @XMM[4], @XMM[8]
810         veor    @XMM[2], @XMM[2], @XMM[8]
811         veor    @XMM[7], @XMM[7], @XMM[8]
812         veor    @XMM[3], @XMM[3], @XMM[8]
813         veor    @XMM[5], @XMM[5], @XMM[8]
814         veor    @XMM[0], @XMM[0], @XMM[8]
815         veor    @XMM[1], @XMM[1], @XMM[8]
816         bx      lr
817 .size   _bsaes_decrypt8,.-_bsaes_decrypt8
818
819 .type   _bsaes_const,%object
820 .align  6
821 _bsaes_const:
822 .LM0ISR:        @ InvShiftRows constants
823         .quad   0x0a0e0206070b0f03, 0x0004080c0d010509
824 .LISR:
825         .quad   0x0504070602010003, 0x0f0e0d0c080b0a09
826 .LISRM0:
827         .quad   0x01040b0e0205080f, 0x0306090c00070a0d
828 .LM0SR:         @ ShiftRows constants
829         .quad   0x0a0e02060f03070b, 0x0004080c05090d01
830 .LSR:
831         .quad   0x0504070600030201, 0x0f0e0d0c0a09080b
832 .LSRM0:
833         .quad   0x0304090e00050a0f, 0x01060b0c0207080d
834 .LM0:
835         .quad   0x02060a0e03070b0f, 0x0004080c0105090d
836 .LREVM0SR:
837         .quad   0x090d01050c000408, 0x03070b0f060a0e02
838 .asciz  "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
839 .align  6
840 .size   _bsaes_const,.-_bsaes_const
841
842 .type   _bsaes_encrypt8,%function
843 .align  4
844 _bsaes_encrypt8:
845         adr     $const,.
846         vldmia  $key!, {@XMM[9]}                @ round 0 key
847 #if defined(__thumb2__) || defined(__APPLE__)
848         adr     $const,.LM0SR
849 #else
850         sub     $const,$const,#_bsaes_encrypt8-.LM0SR
851 #endif
852
853         vldmia  $const!, {@XMM[8]}              @ .LM0SR
854 _bsaes_encrypt8_alt:
855         veor    @XMM[10], @XMM[0], @XMM[9]      @ xor with round0 key
856         veor    @XMM[11], @XMM[1], @XMM[9]
857          vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
858          vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
859         veor    @XMM[12], @XMM[2], @XMM[9]
860          vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
861          vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
862         veor    @XMM[13], @XMM[3], @XMM[9]
863          vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
864          vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
865         veor    @XMM[14], @XMM[4], @XMM[9]
866          vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
867          vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
868         veor    @XMM[15], @XMM[5], @XMM[9]
869          vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
870          vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
871         veor    @XMM[10], @XMM[6], @XMM[9]
872          vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
873          vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
874         veor    @XMM[11], @XMM[7], @XMM[9]
875          vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
876          vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
877          vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
878          vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
879 _bsaes_encrypt8_bitslice:
880 ___
881         &bitslice       (@XMM[0..7, 8..11]);
882 $code.=<<___;
883         sub     $rounds,$rounds,#1
884         b       .Lenc_sbox
885 .align  4
886 .Lenc_loop:
887 ___
888         &ShiftRows      (@XMM[0..7, 8..12]);
889 $code.=".Lenc_sbox:\n";
890         &Sbox           (@XMM[0..7, 8..15]);
891 $code.=<<___;
892         subs    $rounds,$rounds,#1
893         bcc     .Lenc_done
894 ___
895         &MixColumns     (@XMM[0,1,4,6,3,7,2,5, 8..15]);
896 $code.=<<___;
897         vldmia  $const, {@XMM[12]}              @ .LSR
898         ite     eq                              @ Thumb2 thing, samity check in ARM
899         addeq   $const,$const,#0x10
900         bne     .Lenc_loop
901         vldmia  $const, {@XMM[12]}              @ .LSRM0
902         b       .Lenc_loop
903 .align  4
904 .Lenc_done:
905 ___
906         # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
907         &bitslice       (@XMM[0,1,4,6,3,7,2,5, 8..11]);
908 $code.=<<___;
909         vldmia  $key, {@XMM[8]}                 @ last round key
910         veor    @XMM[4], @XMM[4], @XMM[8]
911         veor    @XMM[6], @XMM[6], @XMM[8]
912         veor    @XMM[3], @XMM[3], @XMM[8]
913         veor    @XMM[7], @XMM[7], @XMM[8]
914         veor    @XMM[2], @XMM[2], @XMM[8]
915         veor    @XMM[5], @XMM[5], @XMM[8]
916         veor    @XMM[0], @XMM[0], @XMM[8]
917         veor    @XMM[1], @XMM[1], @XMM[8]
918         bx      lr
919 .size   _bsaes_encrypt8,.-_bsaes_encrypt8
920 ___
921 }
922 {
923 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
924
925 sub bitslice_key {
926 my @x=reverse(@_[0..7]);
927 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
928
929         &swapmove       (@x[0,1],1,$bs0,$t2,$t3);
930 $code.=<<___;
931         @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
932         vmov    @x[2], @x[0]
933         vmov    @x[3], @x[1]
934 ___
935         #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
936
937         &swapmove2x     (@x[0,2,1,3],2,$bs1,$t2,$t3);
938 $code.=<<___;
939         @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
940         vmov    @x[4], @x[0]
941         vmov    @x[6], @x[2]
942         vmov    @x[5], @x[1]
943         vmov    @x[7], @x[3]
944 ___
945         &swapmove2x     (@x[0,4,1,5],4,$bs2,$t2,$t3);
946         &swapmove2x     (@x[2,6,3,7],4,$bs2,$t2,$t3);
947 }
948
949 $code.=<<___;
950 .type   _bsaes_key_convert,%function
951 .align  4
952 _bsaes_key_convert:
953         adr     $const,.
954         vld1.8  {@XMM[7]},  [$inp]!             @ load round 0 key
955 #if defined(__thumb2__) || defined(__APPLE__)
956         adr     $const,.LM0
957 #else
958         sub     $const,$const,#_bsaes_key_convert-.LM0
959 #endif
960         vld1.8  {@XMM[15]}, [$inp]!             @ load round 1 key
961
962         vmov.i8 @XMM[8],  #0x01                 @ bit masks
963         vmov.i8 @XMM[9],  #0x02
964         vmov.i8 @XMM[10], #0x04
965         vmov.i8 @XMM[11], #0x08
966         vmov.i8 @XMM[12], #0x10
967         vmov.i8 @XMM[13], #0x20
968         vldmia  $const, {@XMM[14]}              @ .LM0
969
970 #ifdef __ARMEL__
971         vrev32.8        @XMM[7],  @XMM[7]
972         vrev32.8        @XMM[15], @XMM[15]
973 #endif
974         sub     $rounds,$rounds,#1
975         vstmia  $out!, {@XMM[7]}                @ save round 0 key
976         b       .Lkey_loop
977
978 .align  4
979 .Lkey_loop:
980         vtbl.8  `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
981         vtbl.8  `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
982         vmov.i8 @XMM[6],  #0x40
983         vmov.i8 @XMM[15], #0x80
984
985         vtst.8  @XMM[0], @XMM[7], @XMM[8]
986         vtst.8  @XMM[1], @XMM[7], @XMM[9]
987         vtst.8  @XMM[2], @XMM[7], @XMM[10]
988         vtst.8  @XMM[3], @XMM[7], @XMM[11]
989         vtst.8  @XMM[4], @XMM[7], @XMM[12]
990         vtst.8  @XMM[5], @XMM[7], @XMM[13]
991         vtst.8  @XMM[6], @XMM[7], @XMM[6]
992         vtst.8  @XMM[7], @XMM[7], @XMM[15]
993         vld1.8  {@XMM[15]}, [$inp]!             @ load next round key
994         vmvn    @XMM[0], @XMM[0]                @ "pnot"
995         vmvn    @XMM[1], @XMM[1]
996         vmvn    @XMM[5], @XMM[5]
997         vmvn    @XMM[6], @XMM[6]
998 #ifdef __ARMEL__
999         vrev32.8        @XMM[15], @XMM[15]
1000 #endif
1001         subs    $rounds,$rounds,#1
1002         vstmia  $out!,{@XMM[0]-@XMM[7]}         @ write bit-sliced round key
1003         bne     .Lkey_loop
1004
1005         vmov.i8 @XMM[7],#0x63                   @ compose .L63
1006         @ don't save last round key
1007         bx      lr
1008 .size   _bsaes_key_convert,.-_bsaes_key_convert
1009 ___
1010 }
1011
1012 if (0) {                # following four functions are unsupported interface
1013                         # used for benchmarking...
1014 $code.=<<___;
1015 .globl  bsaes_enc_key_convert
1016 .type   bsaes_enc_key_convert,%function
1017 .align  4
1018 bsaes_enc_key_convert:
1019         stmdb   sp!,{r4-r6,lr}
1020         vstmdb  sp!,{d8-d15}            @ ABI specification says so
1021
1022         ldr     r5,[$inp,#240]                  @ pass rounds
1023         mov     r4,$inp                         @ pass key
1024         mov     r12,$out                        @ pass key schedule
1025         bl      _bsaes_key_convert
1026         veor    @XMM[7],@XMM[7],@XMM[15]        @ fix up last round key
1027         vstmia  r12, {@XMM[7]}                  @ save last round key
1028
1029         vldmia  sp!,{d8-d15}
1030         ldmia   sp!,{r4-r6,pc}
1031 .size   bsaes_enc_key_convert,.-bsaes_enc_key_convert
1032
1033 .globl  bsaes_encrypt_128
1034 .type   bsaes_encrypt_128,%function
1035 .align  4
1036 bsaes_encrypt_128:
1037         stmdb   sp!,{r4-r6,lr}
1038         vstmdb  sp!,{d8-d15}            @ ABI specification says so
1039 .Lenc128_loop:
1040         vld1.8  {@XMM[0]-@XMM[1]}, [$inp]!      @ load input
1041         vld1.8  {@XMM[2]-@XMM[3]}, [$inp]!
1042         mov     r4,$key                         @ pass the key
1043         vld1.8  {@XMM[4]-@XMM[5]}, [$inp]!
1044         mov     r5,#10                          @ pass rounds
1045         vld1.8  {@XMM[6]-@XMM[7]}, [$inp]!
1046
1047         bl      _bsaes_encrypt8
1048
1049         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1050         vst1.8  {@XMM[4]}, [$out]!
1051         vst1.8  {@XMM[6]}, [$out]!
1052         vst1.8  {@XMM[3]}, [$out]!
1053         vst1.8  {@XMM[7]}, [$out]!
1054         vst1.8  {@XMM[2]}, [$out]!
1055         subs    $len,$len,#0x80
1056         vst1.8  {@XMM[5]}, [$out]!
1057         bhi     .Lenc128_loop
1058
1059         vldmia  sp!,{d8-d15}
1060         ldmia   sp!,{r4-r6,pc}
1061 .size   bsaes_encrypt_128,.-bsaes_encrypt_128
1062
1063 .globl  bsaes_dec_key_convert
1064 .type   bsaes_dec_key_convert,%function
1065 .align  4
1066 bsaes_dec_key_convert:
1067         stmdb   sp!,{r4-r6,lr}
1068         vstmdb  sp!,{d8-d15}            @ ABI specification says so
1069
1070         ldr     r5,[$inp,#240]                  @ pass rounds
1071         mov     r4,$inp                         @ pass key
1072         mov     r12,$out                        @ pass key schedule
1073         bl      _bsaes_key_convert
1074         vldmia  $out, {@XMM[6]}
1075         vstmia  r12,  {@XMM[15]}                @ save last round key
1076         veor    @XMM[7], @XMM[7], @XMM[6]       @ fix up round 0 key
1077         vstmia  $out, {@XMM[7]}
1078
1079         vldmia  sp!,{d8-d15}
1080         ldmia   sp!,{r4-r6,pc}
1081 .size   bsaes_dec_key_convert,.-bsaes_dec_key_convert
1082
1083 .globl  bsaes_decrypt_128
1084 .type   bsaes_decrypt_128,%function
1085 .align  4
1086 bsaes_decrypt_128:
1087         stmdb   sp!,{r4-r6,lr}
1088         vstmdb  sp!,{d8-d15}            @ ABI specification says so
1089 .Ldec128_loop:
1090         vld1.8  {@XMM[0]-@XMM[1]}, [$inp]!      @ load input
1091         vld1.8  {@XMM[2]-@XMM[3]}, [$inp]!
1092         mov     r4,$key                         @ pass the key
1093         vld1.8  {@XMM[4]-@XMM[5]}, [$inp]!
1094         mov     r5,#10                          @ pass rounds
1095         vld1.8  {@XMM[6]-@XMM[7]}, [$inp]!
1096
1097         bl      _bsaes_decrypt8
1098
1099         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1100         vst1.8  {@XMM[6]}, [$out]!
1101         vst1.8  {@XMM[4]}, [$out]!
1102         vst1.8  {@XMM[2]}, [$out]!
1103         vst1.8  {@XMM[7]}, [$out]!
1104         vst1.8  {@XMM[3]}, [$out]!
1105         subs    $len,$len,#0x80
1106         vst1.8  {@XMM[5]}, [$out]!
1107         bhi     .Ldec128_loop
1108
1109         vldmia  sp!,{d8-d15}
1110         ldmia   sp!,{r4-r6,pc}
1111 .size   bsaes_decrypt_128,.-bsaes_decrypt_128
1112 ___
1113 }
1114 {
1115 my ($inp,$out,$len,$key, $ivp,$fp,$rounds)=map("r$_",(0..3,8..10));
1116 my ($keysched)=("sp");
1117
1118 $code.=<<___;
1119 .extern AES_cbc_encrypt
1120 .extern AES_decrypt
1121
1122 .global bsaes_cbc_encrypt
1123 .type   bsaes_cbc_encrypt,%function
1124 .align  5
1125 bsaes_cbc_encrypt:
1126 #ifndef __KERNEL__
1127         cmp     $len, #128
1128 #ifndef __thumb__
1129         blo     AES_cbc_encrypt
1130 #else
1131         bhs     .Lcbc_do_bsaes
1132         b       AES_cbc_encrypt
1133 .Lcbc_do_bsaes:
1134 #endif
1135 #endif
1136
1137         @ it is up to the caller to make sure we are called with enc == 0
1138
1139         mov     ip, sp
1140         stmdb   sp!, {r4-r10, lr}
1141         VFP_ABI_PUSH
1142         ldr     $ivp, [ip]                      @ IV is 1st arg on the stack
1143         mov     $len, $len, lsr#4               @ len in 16 byte blocks
1144         sub     sp, #0x10                       @ scratch space to carry over the IV
1145         mov     $fp, sp                         @ save sp
1146
1147         ldr     $rounds, [$key, #240]           @ get # of rounds
1148 #ifndef BSAES_ASM_EXTENDED_KEY
1149         @ allocate the key schedule on the stack
1150         sub     r12, sp, $rounds, lsl#7         @ 128 bytes per inner round key
1151         add     r12, #`128-32`                  @ sifze of bit-slices key schedule
1152
1153         @ populate the key schedule
1154         mov     r4, $key                        @ pass key
1155         mov     r5, $rounds                     @ pass # of rounds
1156         mov     sp, r12                         @ sp is $keysched
1157         bl      _bsaes_key_convert
1158         vldmia  $keysched, {@XMM[6]}
1159         vstmia  r12,  {@XMM[15]}                @ save last round key
1160         veor    @XMM[7], @XMM[7], @XMM[6]       @ fix up round 0 key
1161         vstmia  $keysched, {@XMM[7]}
1162 #else
1163         ldr     r12, [$key, #244]
1164         eors    r12, #1
1165         beq     0f
1166
1167         @ populate the key schedule
1168         str     r12, [$key, #244]
1169         mov     r4, $key                        @ pass key
1170         mov     r5, $rounds                     @ pass # of rounds
1171         add     r12, $key, #248                 @ pass key schedule
1172         bl      _bsaes_key_convert
1173         add     r4, $key, #248
1174         vldmia  r4, {@XMM[6]}
1175         vstmia  r12, {@XMM[15]}                 @ save last round key
1176         veor    @XMM[7], @XMM[7], @XMM[6]       @ fix up round 0 key
1177         vstmia  r4, {@XMM[7]}
1178
1179 .align  2
1180 0:
1181 #endif
1182
1183         vld1.8  {@XMM[15]}, [$ivp]              @ load IV
1184         b       .Lcbc_dec_loop
1185
1186 .align  4
1187 .Lcbc_dec_loop:
1188         subs    $len, $len, #0x8
1189         bmi     .Lcbc_dec_loop_finish
1190
1191         vld1.8  {@XMM[0]-@XMM[1]}, [$inp]!      @ load input
1192         vld1.8  {@XMM[2]-@XMM[3]}, [$inp]!
1193 #ifndef BSAES_ASM_EXTENDED_KEY
1194         mov     r4, $keysched                   @ pass the key
1195 #else
1196         add     r4, $key, #248
1197 #endif
1198         vld1.8  {@XMM[4]-@XMM[5]}, [$inp]!
1199         mov     r5, $rounds
1200         vld1.8  {@XMM[6]-@XMM[7]}, [$inp]
1201         sub     $inp, $inp, #0x60
1202         vstmia  $fp, {@XMM[15]}                 @ put aside IV
1203
1204         bl      _bsaes_decrypt8
1205
1206         vldmia  $fp, {@XMM[14]}                 @ reload IV
1207         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1208         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1209         vld1.8  {@XMM[10]-@XMM[11]}, [$inp]!
1210         veor    @XMM[1], @XMM[1], @XMM[8]
1211         veor    @XMM[6], @XMM[6], @XMM[9]
1212         vld1.8  {@XMM[12]-@XMM[13]}, [$inp]!
1213         veor    @XMM[4], @XMM[4], @XMM[10]
1214         veor    @XMM[2], @XMM[2], @XMM[11]
1215         vld1.8  {@XMM[14]-@XMM[15]}, [$inp]!
1216         veor    @XMM[7], @XMM[7], @XMM[12]
1217         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1218         veor    @XMM[3], @XMM[3], @XMM[13]
1219         vst1.8  {@XMM[6]}, [$out]!
1220         veor    @XMM[5], @XMM[5], @XMM[14]
1221         vst1.8  {@XMM[4]}, [$out]!
1222         vst1.8  {@XMM[2]}, [$out]!
1223         vst1.8  {@XMM[7]}, [$out]!
1224         vst1.8  {@XMM[3]}, [$out]!
1225         vst1.8  {@XMM[5]}, [$out]!
1226
1227         b       .Lcbc_dec_loop
1228
1229 .Lcbc_dec_loop_finish:
1230         adds    $len, $len, #8
1231         beq     .Lcbc_dec_done
1232
1233         vld1.8  {@XMM[0]}, [$inp]!              @ load input
1234         cmp     $len, #2
1235         blo     .Lcbc_dec_one
1236         vld1.8  {@XMM[1]}, [$inp]!
1237 #ifndef BSAES_ASM_EXTENDED_KEY
1238         mov     r4, $keysched                   @ pass the key
1239 #else
1240         add     r4, $key, #248
1241 #endif
1242         mov     r5, $rounds
1243         vstmia  $fp, {@XMM[15]}                 @ put aside IV
1244         beq     .Lcbc_dec_two
1245         vld1.8  {@XMM[2]}, [$inp]!
1246         cmp     $len, #4
1247         blo     .Lcbc_dec_three
1248         vld1.8  {@XMM[3]}, [$inp]!
1249         beq     .Lcbc_dec_four
1250         vld1.8  {@XMM[4]}, [$inp]!
1251         cmp     $len, #6
1252         blo     .Lcbc_dec_five
1253         vld1.8  {@XMM[5]}, [$inp]!
1254         beq     .Lcbc_dec_six
1255         vld1.8  {@XMM[6]}, [$inp]!
1256         sub     $inp, $inp, #0x70
1257
1258         bl      _bsaes_decrypt8
1259
1260         vldmia  $fp, {@XMM[14]}                 @ reload IV
1261         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1262         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1263         vld1.8  {@XMM[10]-@XMM[11]}, [$inp]!
1264         veor    @XMM[1], @XMM[1], @XMM[8]
1265         veor    @XMM[6], @XMM[6], @XMM[9]
1266         vld1.8  {@XMM[12]-@XMM[13]}, [$inp]!
1267         veor    @XMM[4], @XMM[4], @XMM[10]
1268         veor    @XMM[2], @XMM[2], @XMM[11]
1269         vld1.8  {@XMM[15]}, [$inp]!
1270         veor    @XMM[7], @XMM[7], @XMM[12]
1271         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1272         veor    @XMM[3], @XMM[3], @XMM[13]
1273         vst1.8  {@XMM[6]}, [$out]!
1274         vst1.8  {@XMM[4]}, [$out]!
1275         vst1.8  {@XMM[2]}, [$out]!
1276         vst1.8  {@XMM[7]}, [$out]!
1277         vst1.8  {@XMM[3]}, [$out]!
1278         b       .Lcbc_dec_done
1279 .align  4
1280 .Lcbc_dec_six:
1281         sub     $inp, $inp, #0x60
1282         bl      _bsaes_decrypt8
1283         vldmia  $fp,{@XMM[14]}                  @ reload IV
1284         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1285         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1286         vld1.8  {@XMM[10]-@XMM[11]}, [$inp]!
1287         veor    @XMM[1], @XMM[1], @XMM[8]
1288         veor    @XMM[6], @XMM[6], @XMM[9]
1289         vld1.8  {@XMM[12]}, [$inp]!
1290         veor    @XMM[4], @XMM[4], @XMM[10]
1291         veor    @XMM[2], @XMM[2], @XMM[11]
1292         vld1.8  {@XMM[15]}, [$inp]!
1293         veor    @XMM[7], @XMM[7], @XMM[12]
1294         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1295         vst1.8  {@XMM[6]}, [$out]!
1296         vst1.8  {@XMM[4]}, [$out]!
1297         vst1.8  {@XMM[2]}, [$out]!
1298         vst1.8  {@XMM[7]}, [$out]!
1299         b       .Lcbc_dec_done
1300 .align  4
1301 .Lcbc_dec_five:
1302         sub     $inp, $inp, #0x50
1303         bl      _bsaes_decrypt8
1304         vldmia  $fp, {@XMM[14]}                 @ reload IV
1305         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1306         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1307         vld1.8  {@XMM[10]-@XMM[11]}, [$inp]!
1308         veor    @XMM[1], @XMM[1], @XMM[8]
1309         veor    @XMM[6], @XMM[6], @XMM[9]
1310         vld1.8  {@XMM[15]}, [$inp]!
1311         veor    @XMM[4], @XMM[4], @XMM[10]
1312         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1313         veor    @XMM[2], @XMM[2], @XMM[11]
1314         vst1.8  {@XMM[6]}, [$out]!
1315         vst1.8  {@XMM[4]}, [$out]!
1316         vst1.8  {@XMM[2]}, [$out]!
1317         b       .Lcbc_dec_done
1318 .align  4
1319 .Lcbc_dec_four:
1320         sub     $inp, $inp, #0x40
1321         bl      _bsaes_decrypt8
1322         vldmia  $fp, {@XMM[14]}                 @ reload IV
1323         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1324         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1325         vld1.8  {@XMM[10]}, [$inp]!
1326         veor    @XMM[1], @XMM[1], @XMM[8]
1327         veor    @XMM[6], @XMM[6], @XMM[9]
1328         vld1.8  {@XMM[15]}, [$inp]!
1329         veor    @XMM[4], @XMM[4], @XMM[10]
1330         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1331         vst1.8  {@XMM[6]}, [$out]!
1332         vst1.8  {@XMM[4]}, [$out]!
1333         b       .Lcbc_dec_done
1334 .align  4
1335 .Lcbc_dec_three:
1336         sub     $inp, $inp, #0x30
1337         bl      _bsaes_decrypt8
1338         vldmia  $fp, {@XMM[14]}                 @ reload IV
1339         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1340         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1341         vld1.8  {@XMM[15]}, [$inp]!
1342         veor    @XMM[1], @XMM[1], @XMM[8]
1343         veor    @XMM[6], @XMM[6], @XMM[9]
1344         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1345         vst1.8  {@XMM[6]}, [$out]!
1346         b       .Lcbc_dec_done
1347 .align  4
1348 .Lcbc_dec_two:
1349         sub     $inp, $inp, #0x20
1350         bl      _bsaes_decrypt8
1351         vldmia  $fp, {@XMM[14]}                 @ reload IV
1352         vld1.8  {@XMM[8]}, [$inp]!              @ reload input
1353         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1354         vld1.8  {@XMM[15]}, [$inp]!             @ reload input
1355         veor    @XMM[1], @XMM[1], @XMM[8]
1356         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1357         b       .Lcbc_dec_done
1358 .align  4
1359 .Lcbc_dec_one:
1360         sub     $inp, $inp, #0x10
1361         mov     $rounds, $out                   @ save original out pointer
1362         mov     $out, $fp                       @ use the iv scratch space as out buffer
1363         mov     r2, $key
1364         vmov    @XMM[4],@XMM[15]                @ just in case ensure that IV
1365         vmov    @XMM[5],@XMM[0]                 @ and input are preserved
1366         bl      AES_decrypt
1367         vld1.8  {@XMM[0]}, [$fp]                @ load result
1368         veor    @XMM[0], @XMM[0], @XMM[4]       @ ^= IV
1369         vmov    @XMM[15], @XMM[5]               @ @XMM[5] holds input
1370         vst1.8  {@XMM[0]}, [$rounds]            @ write output
1371
1372 .Lcbc_dec_done:
1373 #ifndef BSAES_ASM_EXTENDED_KEY
1374         vmov.i32        q0, #0
1375         vmov.i32        q1, #0
1376 .Lcbc_dec_bzero:                                @ wipe key schedule [if any]
1377         vstmia          $keysched!, {q0-q1}
1378         cmp             $keysched, $fp
1379         bne             .Lcbc_dec_bzero
1380 #endif
1381
1382         mov     sp, $fp
1383         add     sp, #0x10                       @ add sp,$fp,#0x10 is no good for thumb
1384         vst1.8  {@XMM[15]}, [$ivp]              @ return IV
1385         VFP_ABI_POP
1386         ldmia   sp!, {r4-r10, pc}
1387 .size   bsaes_cbc_encrypt,.-bsaes_cbc_encrypt
1388 ___
1389 }
1390 {
1391 my ($inp,$out,$len,$key, $ctr,$fp,$rounds)=(map("r$_",(0..3,8..10)));
1392 my $const = "r6";       # shared with _bsaes_encrypt8_alt
1393 my $keysched = "sp";
1394
1395 $code.=<<___;
1396 .extern AES_encrypt
1397 .global bsaes_ctr32_encrypt_blocks
1398 .type   bsaes_ctr32_encrypt_blocks,%function
1399 .align  5
1400 bsaes_ctr32_encrypt_blocks:
1401         cmp     $len, #8                        @ use plain AES for
1402         blo     .Lctr_enc_short                 @ small sizes
1403
1404         mov     ip, sp
1405         stmdb   sp!, {r4-r10, lr}
1406         VFP_ABI_PUSH
1407         ldr     $ctr, [ip]                      @ ctr is 1st arg on the stack
1408         sub     sp, sp, #0x10                   @ scratch space to carry over the ctr
1409         mov     $fp, sp                         @ save sp
1410
1411         ldr     $rounds, [$key, #240]           @ get # of rounds
1412 #ifndef BSAES_ASM_EXTENDED_KEY
1413         @ allocate the key schedule on the stack
1414         sub     r12, sp, $rounds, lsl#7         @ 128 bytes per inner round key
1415         add     r12, #`128-32`                  @ size of bit-sliced key schedule
1416
1417         @ populate the key schedule
1418         mov     r4, $key                        @ pass key
1419         mov     r5, $rounds                     @ pass # of rounds
1420         mov     sp, r12                         @ sp is $keysched
1421         bl      _bsaes_key_convert
1422         veor    @XMM[7],@XMM[7],@XMM[15]        @ fix up last round key
1423         vstmia  r12, {@XMM[7]}                  @ save last round key
1424
1425         vld1.8  {@XMM[0]}, [$ctr]               @ load counter
1426 #ifdef  __APPLE__
1427         mov     $ctr, #:lower16:(.LREVM0SR-.LM0)
1428         add     $ctr, $const, $ctr
1429 #else
1430         add     $ctr, $const, #.LREVM0SR-.LM0   @ borrow $ctr
1431 #endif
1432         vldmia  $keysched, {@XMM[4]}            @ load round0 key
1433 #else
1434         ldr     r12, [$key, #244]
1435         eors    r12, #1
1436         beq     0f
1437
1438         @ populate the key schedule
1439         str     r12, [$key, #244]
1440         mov     r4, $key                        @ pass key
1441         mov     r5, $rounds                     @ pass # of rounds
1442         add     r12, $key, #248                 @ pass key schedule
1443         bl      _bsaes_key_convert
1444         veor    @XMM[7],@XMM[7],@XMM[15]        @ fix up last round key
1445         vstmia  r12, {@XMM[7]}                  @ save last round key
1446
1447 .align  2
1448 0:      add     r12, $key, #248
1449         vld1.8  {@XMM[0]}, [$ctr]               @ load counter
1450         adrl    $ctr, .LREVM0SR                 @ borrow $ctr
1451         vldmia  r12, {@XMM[4]}                  @ load round0 key
1452         sub     sp, #0x10                       @ place for adjusted round0 key
1453 #endif
1454
1455         vmov.i32        @XMM[8],#1              @ compose 1<<96
1456         veor            @XMM[9],@XMM[9],@XMM[9]
1457         vrev32.8        @XMM[0],@XMM[0]
1458         vext.8          @XMM[8],@XMM[9],@XMM[8],#4
1459         vrev32.8        @XMM[4],@XMM[4]
1460         vadd.u32        @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1461         vstmia  $keysched, {@XMM[4]}            @ save adjusted round0 key
1462         b       .Lctr_enc_loop
1463
1464 .align  4
1465 .Lctr_enc_loop:
1466         vadd.u32        @XMM[10], @XMM[8], @XMM[9]      @ compose 3<<96
1467         vadd.u32        @XMM[1], @XMM[0], @XMM[8]       @ +1
1468         vadd.u32        @XMM[2], @XMM[0], @XMM[9]       @ +2
1469         vadd.u32        @XMM[3], @XMM[0], @XMM[10]      @ +3
1470         vadd.u32        @XMM[4], @XMM[1], @XMM[10]
1471         vadd.u32        @XMM[5], @XMM[2], @XMM[10]
1472         vadd.u32        @XMM[6], @XMM[3], @XMM[10]
1473         vadd.u32        @XMM[7], @XMM[4], @XMM[10]
1474         vadd.u32        @XMM[10], @XMM[5], @XMM[10]     @ next counter
1475
1476         @ Borrow prologue from _bsaes_encrypt8 to use the opportunity
1477         @ to flip byte order in 32-bit counter
1478
1479         vldmia          $keysched, {@XMM[9]}            @ load round0 key
1480 #ifndef BSAES_ASM_EXTENDED_KEY
1481         add             r4, $keysched, #0x10            @ pass next round key
1482 #else
1483         add             r4, $key, #`248+16`
1484 #endif
1485         vldmia          $ctr, {@XMM[8]}                 @ .LREVM0SR
1486         mov             r5, $rounds                     @ pass rounds
1487         vstmia          $fp, {@XMM[10]}                 @ save next counter
1488 #ifdef  __APPLE__
1489         mov             $const, #:lower16:(.LREVM0SR-.LSR)
1490         sub             $const, $ctr, $const
1491 #else
1492         sub             $const, $ctr, #.LREVM0SR-.LSR   @ pass constants
1493 #endif
1494
1495         bl              _bsaes_encrypt8_alt
1496
1497         subs            $len, $len, #8
1498         blo             .Lctr_enc_loop_done
1499
1500         vld1.8          {@XMM[8]-@XMM[9]}, [$inp]!      @ load input
1501         vld1.8          {@XMM[10]-@XMM[11]}, [$inp]!
1502         veor            @XMM[0], @XMM[8]
1503         veor            @XMM[1], @XMM[9]
1504         vld1.8          {@XMM[12]-@XMM[13]}, [$inp]!
1505         veor            @XMM[4], @XMM[10]
1506         veor            @XMM[6], @XMM[11]
1507         vld1.8          {@XMM[14]-@XMM[15]}, [$inp]!
1508         veor            @XMM[3], @XMM[12]
1509         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1510         veor            @XMM[7], @XMM[13]
1511         veor            @XMM[2], @XMM[14]
1512         vst1.8          {@XMM[4]}, [$out]!
1513         veor            @XMM[5], @XMM[15]
1514         vst1.8          {@XMM[6]}, [$out]!
1515         vmov.i32        @XMM[8], #1                     @ compose 1<<96
1516         vst1.8          {@XMM[3]}, [$out]!
1517         veor            @XMM[9], @XMM[9], @XMM[9]
1518         vst1.8          {@XMM[7]}, [$out]!
1519         vext.8          @XMM[8], @XMM[9], @XMM[8], #4
1520         vst1.8          {@XMM[2]}, [$out]!
1521         vadd.u32        @XMM[9],@XMM[8],@XMM[8]         @ compose 2<<96
1522         vst1.8          {@XMM[5]}, [$out]!
1523         vldmia          $fp, {@XMM[0]}                  @ load counter
1524
1525         bne             .Lctr_enc_loop
1526         b               .Lctr_enc_done
1527
1528 .align  4
1529 .Lctr_enc_loop_done:
1530         add             $len, $len, #8
1531         vld1.8          {@XMM[8]}, [$inp]!      @ load input
1532         veor            @XMM[0], @XMM[8]
1533         vst1.8          {@XMM[0]}, [$out]!      @ write output
1534         cmp             $len, #2
1535         blo             .Lctr_enc_done
1536         vld1.8          {@XMM[9]}, [$inp]!
1537         veor            @XMM[1], @XMM[9]
1538         vst1.8          {@XMM[1]}, [$out]!
1539         beq             .Lctr_enc_done
1540         vld1.8          {@XMM[10]}, [$inp]!
1541         veor            @XMM[4], @XMM[10]
1542         vst1.8          {@XMM[4]}, [$out]!
1543         cmp             $len, #4
1544         blo             .Lctr_enc_done
1545         vld1.8          {@XMM[11]}, [$inp]!
1546         veor            @XMM[6], @XMM[11]
1547         vst1.8          {@XMM[6]}, [$out]!
1548         beq             .Lctr_enc_done
1549         vld1.8          {@XMM[12]}, [$inp]!
1550         veor            @XMM[3], @XMM[12]
1551         vst1.8          {@XMM[3]}, [$out]!
1552         cmp             $len, #6
1553         blo             .Lctr_enc_done
1554         vld1.8          {@XMM[13]}, [$inp]!
1555         veor            @XMM[7], @XMM[13]
1556         vst1.8          {@XMM[7]}, [$out]!
1557         beq             .Lctr_enc_done
1558         vld1.8          {@XMM[14]}, [$inp]
1559         veor            @XMM[2], @XMM[14]
1560         vst1.8          {@XMM[2]}, [$out]!
1561
1562 .Lctr_enc_done:
1563         vmov.i32        q0, #0
1564         vmov.i32        q1, #0
1565 #ifndef BSAES_ASM_EXTENDED_KEY
1566 .Lctr_enc_bzero:                        @ wipe key schedule [if any]
1567         vstmia          $keysched!, {q0-q1}
1568         cmp             $keysched, $fp
1569         bne             .Lctr_enc_bzero
1570 #else
1571         vstmia          $keysched, {q0-q1}
1572 #endif
1573
1574         mov     sp, $fp
1575         add     sp, #0x10               @ add sp,$fp,#0x10 is no good for thumb
1576         VFP_ABI_POP
1577         ldmia   sp!, {r4-r10, pc}       @ return
1578
1579 .align  4
1580 .Lctr_enc_short:
1581         ldr     ip, [sp]                @ ctr pointer is passed on stack
1582         stmdb   sp!, {r4-r8, lr}
1583
1584         mov     r4, $inp                @ copy arguments
1585         mov     r5, $out
1586         mov     r6, $len
1587         mov     r7, $key
1588         ldr     r8, [ip, #12]           @ load counter LSW
1589         vld1.8  {@XMM[1]}, [ip]         @ load whole counter value
1590 #ifdef __ARMEL__
1591         rev     r8, r8
1592 #endif
1593         sub     sp, sp, #0x10
1594         vst1.8  {@XMM[1]}, [sp]         @ copy counter value
1595         sub     sp, sp, #0x10
1596
1597 .Lctr_enc_short_loop:
1598         add     r0, sp, #0x10           @ input counter value
1599         mov     r1, sp                  @ output on the stack
1600         mov     r2, r7                  @ key
1601
1602         bl      AES_encrypt
1603
1604         vld1.8  {@XMM[0]}, [r4]!        @ load input
1605         vld1.8  {@XMM[1]}, [sp]         @ load encrypted counter
1606         add     r8, r8, #1
1607 #ifdef __ARMEL__
1608         rev     r0, r8
1609         str     r0, [sp, #0x1c]         @ next counter value
1610 #else
1611         str     r8, [sp, #0x1c]         @ next counter value
1612 #endif
1613         veor    @XMM[0],@XMM[0],@XMM[1]
1614         vst1.8  {@XMM[0]}, [r5]!        @ store output
1615         subs    r6, r6, #1
1616         bne     .Lctr_enc_short_loop
1617
1618         vmov.i32        q0, #0
1619         vmov.i32        q1, #0
1620         vstmia          sp!, {q0-q1}
1621
1622         ldmia   sp!, {r4-r8, pc}
1623 .size   bsaes_ctr32_encrypt_blocks,.-bsaes_ctr32_encrypt_blocks
1624 ___
1625 }
1626 {
1627 ######################################################################
1628 # void bsaes_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1629 #       const AES_KEY *key1, const AES_KEY *key2,
1630 #       const unsigned char iv[16]);
1631 #
1632 my ($inp,$out,$len,$key,$rounds,$magic,$fp)=(map("r$_",(7..10,1..3)));
1633 my $const="r6";         # returned by _bsaes_key_convert
1634 my $twmask=@XMM[5];
1635 my @T=@XMM[6..7];
1636
1637 $code.=<<___;
1638 .globl  bsaes_xts_encrypt
1639 .type   bsaes_xts_encrypt,%function
1640 .align  4
1641 bsaes_xts_encrypt:
1642         mov     ip, sp
1643         stmdb   sp!, {r4-r10, lr}               @ 0x20
1644         VFP_ABI_PUSH
1645         mov     r6, sp                          @ future $fp
1646
1647         mov     $inp, r0
1648         mov     $out, r1
1649         mov     $len, r2
1650         mov     $key, r3
1651
1652         sub     r0, sp, #0x10                   @ 0x10
1653         bic     r0, #0xf                        @ align at 16 bytes
1654         mov     sp, r0
1655
1656 #ifdef  XTS_CHAIN_TWEAK
1657         ldr     r0, [ip]                        @ pointer to input tweak
1658 #else
1659         @ generate initial tweak
1660         ldr     r0, [ip, #4]                    @ iv[]
1661         mov     r1, sp
1662         ldr     r2, [ip, #0]                    @ key2
1663         bl      AES_encrypt
1664         mov     r0,sp                           @ pointer to initial tweak
1665 #endif
1666
1667         ldr     $rounds, [$key, #240]           @ get # of rounds
1668         mov     $fp, r6
1669 #ifndef BSAES_ASM_EXTENDED_KEY
1670         @ allocate the key schedule on the stack
1671         sub     r12, sp, $rounds, lsl#7         @ 128 bytes per inner round key
1672         @ add   r12, #`128-32`                  @ size of bit-sliced key schedule
1673         sub     r12, #`32+16`                   @ place for tweak[9]
1674
1675         @ populate the key schedule
1676         mov     r4, $key                        @ pass key
1677         mov     r5, $rounds                     @ pass # of rounds
1678         mov     sp, r12
1679         add     r12, #0x90                      @ pass key schedule
1680         bl      _bsaes_key_convert
1681         veor    @XMM[7], @XMM[7], @XMM[15]      @ fix up last round key
1682         vstmia  r12, {@XMM[7]}                  @ save last round key
1683 #else
1684         ldr     r12, [$key, #244]
1685         eors    r12, #1
1686         beq     0f
1687
1688         str     r12, [$key, #244]
1689         mov     r4, $key                        @ pass key
1690         mov     r5, $rounds                     @ pass # of rounds
1691         add     r12, $key, #248                 @ pass key schedule
1692         bl      _bsaes_key_convert
1693         veor    @XMM[7], @XMM[7], @XMM[15]      @ fix up last round key
1694         vstmia  r12, {@XMM[7]}
1695
1696 .align  2
1697 0:      sub     sp, #0x90                       @ place for tweak[9]
1698 #endif
1699
1700         vld1.8  {@XMM[8]}, [r0]                 @ initial tweak
1701         adr     $magic, .Lxts_magic
1702
1703         subs    $len, #0x80
1704         blo     .Lxts_enc_short
1705         b       .Lxts_enc_loop
1706
1707 .align  4
1708 .Lxts_enc_loop:
1709         vldmia          $magic, {$twmask}       @ load XTS magic
1710         vshr.s64        @T[0], @XMM[8], #63
1711         mov             r0, sp
1712         vand            @T[0], @T[0], $twmask
1713 ___
1714 for($i=9;$i<16;$i++) {
1715 $code.=<<___;
1716         vadd.u64        @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1717         vst1.64         {@XMM[$i-1]}, [r0,:128]!
1718         vswp            `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1719         vshr.s64        @T[1], @XMM[$i], #63
1720         veor            @XMM[$i], @XMM[$i], @T[0]
1721         vand            @T[1], @T[1], $twmask
1722 ___
1723         @T=reverse(@T);
1724
1725 $code.=<<___ if ($i>=10);
1726         vld1.8          {@XMM[$i-10]}, [$inp]!
1727 ___
1728 $code.=<<___ if ($i>=11);
1729         veor            @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1730 ___
1731 }
1732 $code.=<<___;
1733         vadd.u64        @XMM[8], @XMM[15], @XMM[15]
1734         vst1.64         {@XMM[15]}, [r0,:128]!
1735         vswp            `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1736         veor            @XMM[8], @XMM[8], @T[0]
1737         vst1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1738
1739         vld1.8          {@XMM[6]-@XMM[7]}, [$inp]!
1740         veor            @XMM[5], @XMM[5], @XMM[13]
1741 #ifndef BSAES_ASM_EXTENDED_KEY
1742         add             r4, sp, #0x90                   @ pass key schedule
1743 #else
1744         add             r4, $key, #248                  @ pass key schedule
1745 #endif
1746         veor            @XMM[6], @XMM[6], @XMM[14]
1747         mov             r5, $rounds                     @ pass rounds
1748         veor            @XMM[7], @XMM[7], @XMM[15]
1749         mov             r0, sp
1750
1751         bl              _bsaes_encrypt8
1752
1753         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1754         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
1755         veor            @XMM[0], @XMM[0], @XMM[ 8]
1756         vld1.64         {@XMM[12]-@XMM[13]}, [r0,:128]!
1757         veor            @XMM[1], @XMM[1], @XMM[ 9]
1758         veor            @XMM[8], @XMM[4], @XMM[10]
1759         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1760         veor            @XMM[9], @XMM[6], @XMM[11]
1761         vld1.64         {@XMM[14]-@XMM[15]}, [r0,:128]!
1762         veor            @XMM[10], @XMM[3], @XMM[12]
1763         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
1764         veor            @XMM[11], @XMM[7], @XMM[13]
1765         veor            @XMM[12], @XMM[2], @XMM[14]
1766         vst1.8          {@XMM[10]-@XMM[11]}, [$out]!
1767         veor            @XMM[13], @XMM[5], @XMM[15]
1768         vst1.8          {@XMM[12]-@XMM[13]}, [$out]!
1769
1770         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1771
1772         subs            $len, #0x80
1773         bpl             .Lxts_enc_loop
1774
1775 .Lxts_enc_short:
1776         adds            $len, #0x70
1777         bmi             .Lxts_enc_done
1778
1779         vldmia          $magic, {$twmask}       @ load XTS magic
1780         vshr.s64        @T[0], @XMM[8], #63
1781         mov             r0, sp
1782         vand            @T[0], @T[0], $twmask
1783 ___
1784 for($i=9;$i<16;$i++) {
1785 $code.=<<___;
1786         vadd.u64        @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1787         vst1.64         {@XMM[$i-1]}, [r0,:128]!
1788         vswp            `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1789         vshr.s64        @T[1], @XMM[$i], #63
1790         veor            @XMM[$i], @XMM[$i], @T[0]
1791         vand            @T[1], @T[1], $twmask
1792 ___
1793         @T=reverse(@T);
1794
1795 $code.=<<___ if ($i>=10);
1796         vld1.8          {@XMM[$i-10]}, [$inp]!
1797         subs            $len, #0x10
1798         bmi             .Lxts_enc_`$i-9`
1799 ___
1800 $code.=<<___ if ($i>=11);
1801         veor            @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1802 ___
1803 }
1804 $code.=<<___;
1805         sub             $len, #0x10
1806         vst1.64         {@XMM[15]}, [r0,:128]           @ next round tweak
1807
1808         vld1.8          {@XMM[6]}, [$inp]!
1809         veor            @XMM[5], @XMM[5], @XMM[13]
1810 #ifndef BSAES_ASM_EXTENDED_KEY
1811         add             r4, sp, #0x90                   @ pass key schedule
1812 #else
1813         add             r4, $key, #248                  @ pass key schedule
1814 #endif
1815         veor            @XMM[6], @XMM[6], @XMM[14]
1816         mov             r5, $rounds                     @ pass rounds
1817         mov             r0, sp
1818
1819         bl              _bsaes_encrypt8
1820
1821         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1822         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
1823         veor            @XMM[0], @XMM[0], @XMM[ 8]
1824         vld1.64         {@XMM[12]-@XMM[13]}, [r0,:128]!
1825         veor            @XMM[1], @XMM[1], @XMM[ 9]
1826         veor            @XMM[8], @XMM[4], @XMM[10]
1827         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1828         veor            @XMM[9], @XMM[6], @XMM[11]
1829         vld1.64         {@XMM[14]}, [r0,:128]!
1830         veor            @XMM[10], @XMM[3], @XMM[12]
1831         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
1832         veor            @XMM[11], @XMM[7], @XMM[13]
1833         veor            @XMM[12], @XMM[2], @XMM[14]
1834         vst1.8          {@XMM[10]-@XMM[11]}, [$out]!
1835         vst1.8          {@XMM[12]}, [$out]!
1836
1837         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1838         b               .Lxts_enc_done
1839 .align  4
1840 .Lxts_enc_6:
1841         veor            @XMM[4], @XMM[4], @XMM[12]
1842 #ifndef BSAES_ASM_EXTENDED_KEY
1843         add             r4, sp, #0x90                   @ pass key schedule
1844 #else
1845         add             r4, $key, #248                  @ pass key schedule
1846 #endif
1847         veor            @XMM[5], @XMM[5], @XMM[13]
1848         mov             r5, $rounds                     @ pass rounds
1849         mov             r0, sp
1850
1851         bl              _bsaes_encrypt8
1852
1853         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1854         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
1855         veor            @XMM[0], @XMM[0], @XMM[ 8]
1856         vld1.64         {@XMM[12]-@XMM[13]}, [r0,:128]!
1857         veor            @XMM[1], @XMM[1], @XMM[ 9]
1858         veor            @XMM[8], @XMM[4], @XMM[10]
1859         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1860         veor            @XMM[9], @XMM[6], @XMM[11]
1861         veor            @XMM[10], @XMM[3], @XMM[12]
1862         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
1863         veor            @XMM[11], @XMM[7], @XMM[13]
1864         vst1.8          {@XMM[10]-@XMM[11]}, [$out]!
1865
1866         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1867         b               .Lxts_enc_done
1868
1869 @ put this in range for both ARM and Thumb mode adr instructions
1870 .align  5
1871 .Lxts_magic:
1872         .quad   1, 0x87
1873
1874 .align  5
1875 .Lxts_enc_5:
1876         veor            @XMM[3], @XMM[3], @XMM[11]
1877 #ifndef BSAES_ASM_EXTENDED_KEY
1878         add             r4, sp, #0x90                   @ pass key schedule
1879 #else
1880         add             r4, $key, #248                  @ pass key schedule
1881 #endif
1882         veor            @XMM[4], @XMM[4], @XMM[12]
1883         mov             r5, $rounds                     @ pass rounds
1884         mov             r0, sp
1885
1886         bl              _bsaes_encrypt8
1887
1888         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1889         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
1890         veor            @XMM[0], @XMM[0], @XMM[ 8]
1891         vld1.64         {@XMM[12]}, [r0,:128]!
1892         veor            @XMM[1], @XMM[1], @XMM[ 9]
1893         veor            @XMM[8], @XMM[4], @XMM[10]
1894         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1895         veor            @XMM[9], @XMM[6], @XMM[11]
1896         veor            @XMM[10], @XMM[3], @XMM[12]
1897         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
1898         vst1.8          {@XMM[10]}, [$out]!
1899
1900         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1901         b               .Lxts_enc_done
1902 .align  4
1903 .Lxts_enc_4:
1904         veor            @XMM[2], @XMM[2], @XMM[10]
1905 #ifndef BSAES_ASM_EXTENDED_KEY
1906         add             r4, sp, #0x90                   @ pass key schedule
1907 #else
1908         add             r4, $key, #248                  @ pass key schedule
1909 #endif
1910         veor            @XMM[3], @XMM[3], @XMM[11]
1911         mov             r5, $rounds                     @ pass rounds
1912         mov             r0, sp
1913
1914         bl              _bsaes_encrypt8
1915
1916         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1917         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
1918         veor            @XMM[0], @XMM[0], @XMM[ 8]
1919         veor            @XMM[1], @XMM[1], @XMM[ 9]
1920         veor            @XMM[8], @XMM[4], @XMM[10]
1921         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1922         veor            @XMM[9], @XMM[6], @XMM[11]
1923         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
1924
1925         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1926         b               .Lxts_enc_done
1927 .align  4
1928 .Lxts_enc_3:
1929         veor            @XMM[1], @XMM[1], @XMM[9]
1930 #ifndef BSAES_ASM_EXTENDED_KEY
1931         add             r4, sp, #0x90                   @ pass key schedule
1932 #else
1933         add             r4, $key, #248                  @ pass key schedule
1934 #endif
1935         veor            @XMM[2], @XMM[2], @XMM[10]
1936         mov             r5, $rounds                     @ pass rounds
1937         mov             r0, sp
1938
1939         bl              _bsaes_encrypt8
1940
1941         vld1.64         {@XMM[8]-@XMM[9]}, [r0,:128]!
1942         vld1.64         {@XMM[10]}, [r0,:128]!
1943         veor            @XMM[0], @XMM[0], @XMM[ 8]
1944         veor            @XMM[1], @XMM[1], @XMM[ 9]
1945         veor            @XMM[8], @XMM[4], @XMM[10]
1946         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1947         vst1.8          {@XMM[8]}, [$out]!
1948
1949         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1950         b               .Lxts_enc_done
1951 .align  4
1952 .Lxts_enc_2:
1953         veor            @XMM[0], @XMM[0], @XMM[8]
1954 #ifndef BSAES_ASM_EXTENDED_KEY
1955         add             r4, sp, #0x90                   @ pass key schedule
1956 #else
1957         add             r4, $key, #248                  @ pass key schedule
1958 #endif
1959         veor            @XMM[1], @XMM[1], @XMM[9]
1960         mov             r5, $rounds                     @ pass rounds
1961         mov             r0, sp
1962
1963         bl              _bsaes_encrypt8
1964
1965         vld1.64         {@XMM[8]-@XMM[9]}, [r0,:128]!
1966         veor            @XMM[0], @XMM[0], @XMM[ 8]
1967         veor            @XMM[1], @XMM[1], @XMM[ 9]
1968         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1969
1970         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1971         b               .Lxts_enc_done
1972 .align  4
1973 .Lxts_enc_1:
1974         mov             r0, sp
1975         veor            @XMM[0], @XMM[0], @XMM[8]
1976         mov             r1, sp
1977         vst1.8          {@XMM[0]}, [sp,:128]
1978         mov             r2, $key
1979         mov             r4, $fp                         @ preserve fp
1980
1981         bl              AES_encrypt
1982
1983         vld1.8          {@XMM[0]}, [sp,:128]
1984         veor            @XMM[0], @XMM[0], @XMM[8]
1985         vst1.8          {@XMM[0]}, [$out]!
1986         mov             $fp, r4
1987
1988         vmov            @XMM[8], @XMM[9]                @ next round tweak
1989
1990 .Lxts_enc_done:
1991 #ifndef XTS_CHAIN_TWEAK
1992         adds            $len, #0x10
1993         beq             .Lxts_enc_ret
1994         sub             r6, $out, #0x10
1995
1996 .Lxts_enc_steal:
1997         ldrb            r0, [$inp], #1
1998         ldrb            r1, [$out, #-0x10]
1999         strb            r0, [$out, #-0x10]
2000         strb            r1, [$out], #1
2001
2002         subs            $len, #1
2003         bhi             .Lxts_enc_steal
2004
2005         vld1.8          {@XMM[0]}, [r6]
2006         mov             r0, sp
2007         veor            @XMM[0], @XMM[0], @XMM[8]
2008         mov             r1, sp
2009         vst1.8          {@XMM[0]}, [sp,:128]
2010         mov             r2, $key
2011         mov             r4, $fp                 @ preserve fp
2012
2013         bl              AES_encrypt
2014
2015         vld1.8          {@XMM[0]}, [sp,:128]
2016         veor            @XMM[0], @XMM[0], @XMM[8]
2017         vst1.8          {@XMM[0]}, [r6]
2018         mov             $fp, r4
2019 #endif
2020
2021 .Lxts_enc_ret:
2022         bic             r0, $fp, #0xf
2023         vmov.i32        q0, #0
2024         vmov.i32        q1, #0
2025 #ifdef  XTS_CHAIN_TWEAK
2026         ldr             r1, [$fp, #0x20+VFP_ABI_FRAME]  @ chain tweak
2027 #endif
2028 .Lxts_enc_bzero:                                @ wipe key schedule [if any]
2029         vstmia          sp!, {q0-q1}
2030         cmp             sp, r0
2031         bne             .Lxts_enc_bzero
2032
2033         mov             sp, $fp
2034 #ifdef  XTS_CHAIN_TWEAK
2035         vst1.8          {@XMM[8]}, [r1]
2036 #endif
2037         VFP_ABI_POP
2038         ldmia           sp!, {r4-r10, pc}       @ return
2039
2040 .size   bsaes_xts_encrypt,.-bsaes_xts_encrypt
2041
2042 .globl  bsaes_xts_decrypt
2043 .type   bsaes_xts_decrypt,%function
2044 .align  4
2045 bsaes_xts_decrypt:
2046         mov     ip, sp
2047         stmdb   sp!, {r4-r10, lr}               @ 0x20
2048         VFP_ABI_PUSH
2049         mov     r6, sp                          @ future $fp
2050
2051         mov     $inp, r0
2052         mov     $out, r1
2053         mov     $len, r2
2054         mov     $key, r3
2055
2056         sub     r0, sp, #0x10                   @ 0x10
2057         bic     r0, #0xf                        @ align at 16 bytes
2058         mov     sp, r0
2059
2060 #ifdef  XTS_CHAIN_TWEAK
2061         ldr     r0, [ip]                        @ pointer to input tweak
2062 #else
2063         @ generate initial tweak
2064         ldr     r0, [ip, #4]                    @ iv[]
2065         mov     r1, sp
2066         ldr     r2, [ip, #0]                    @ key2
2067         bl      AES_encrypt
2068         mov     r0, sp                          @ pointer to initial tweak
2069 #endif
2070
2071         ldr     $rounds, [$key, #240]           @ get # of rounds
2072         mov     $fp, r6
2073 #ifndef BSAES_ASM_EXTENDED_KEY
2074         @ allocate the key schedule on the stack
2075         sub     r12, sp, $rounds, lsl#7         @ 128 bytes per inner round key
2076         @ add   r12, #`128-32`                  @ size of bit-sliced key schedule
2077         sub     r12, #`32+16`                   @ place for tweak[9]
2078
2079         @ populate the key schedule
2080         mov     r4, $key                        @ pass key
2081         mov     r5, $rounds                     @ pass # of rounds
2082         mov     sp, r12
2083         add     r12, #0x90                      @ pass key schedule
2084         bl      _bsaes_key_convert
2085         add     r4, sp, #0x90
2086         vldmia  r4, {@XMM[6]}
2087         vstmia  r12,  {@XMM[15]}                @ save last round key
2088         veor    @XMM[7], @XMM[7], @XMM[6]       @ fix up round 0 key
2089         vstmia  r4, {@XMM[7]}
2090 #else
2091         ldr     r12, [$key, #244]
2092         eors    r12, #1
2093         beq     0f
2094
2095         str     r12, [$key, #244]
2096         mov     r4, $key                        @ pass key
2097         mov     r5, $rounds                     @ pass # of rounds
2098         add     r12, $key, #248                 @ pass key schedule
2099         bl      _bsaes_key_convert
2100         add     r4, $key, #248
2101         vldmia  r4, {@XMM[6]}
2102         vstmia  r12,  {@XMM[15]}                @ save last round key
2103         veor    @XMM[7], @XMM[7], @XMM[6]       @ fix up round 0 key
2104         vstmia  r4, {@XMM[7]}
2105
2106 .align  2
2107 0:      sub     sp, #0x90                       @ place for tweak[9]
2108 #endif
2109         vld1.8  {@XMM[8]}, [r0]                 @ initial tweak
2110         adr     $magic, .Lxts_magic
2111
2112 #ifndef XTS_CHAIN_TWEAK
2113         tst     $len, #0xf                      @ if not multiple of 16
2114         it      ne                              @ Thumb2 thing, sanity check in ARM
2115         subne   $len, #0x10                     @ subtract another 16 bytes
2116 #endif
2117         subs    $len, #0x80
2118
2119         blo     .Lxts_dec_short
2120         b       .Lxts_dec_loop
2121
2122 .align  4
2123 .Lxts_dec_loop:
2124         vldmia          $magic, {$twmask}       @ load XTS magic
2125         vshr.s64        @T[0], @XMM[8], #63
2126         mov             r0, sp
2127         vand            @T[0], @T[0], $twmask
2128 ___
2129 for($i=9;$i<16;$i++) {
2130 $code.=<<___;
2131         vadd.u64        @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2132         vst1.64         {@XMM[$i-1]}, [r0,:128]!
2133         vswp            `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2134         vshr.s64        @T[1], @XMM[$i], #63
2135         veor            @XMM[$i], @XMM[$i], @T[0]
2136         vand            @T[1], @T[1], $twmask
2137 ___
2138         @T=reverse(@T);
2139
2140 $code.=<<___ if ($i>=10);
2141         vld1.8          {@XMM[$i-10]}, [$inp]!
2142 ___
2143 $code.=<<___ if ($i>=11);
2144         veor            @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2145 ___
2146 }
2147 $code.=<<___;
2148         vadd.u64        @XMM[8], @XMM[15], @XMM[15]
2149         vst1.64         {@XMM[15]}, [r0,:128]!
2150         vswp            `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2151         veor            @XMM[8], @XMM[8], @T[0]
2152         vst1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2153
2154         vld1.8          {@XMM[6]-@XMM[7]}, [$inp]!
2155         veor            @XMM[5], @XMM[5], @XMM[13]
2156 #ifndef BSAES_ASM_EXTENDED_KEY
2157         add             r4, sp, #0x90                   @ pass key schedule
2158 #else
2159         add             r4, $key, #248                  @ pass key schedule
2160 #endif
2161         veor            @XMM[6], @XMM[6], @XMM[14]
2162         mov             r5, $rounds                     @ pass rounds
2163         veor            @XMM[7], @XMM[7], @XMM[15]
2164         mov             r0, sp
2165
2166         bl              _bsaes_decrypt8
2167
2168         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2169         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
2170         veor            @XMM[0], @XMM[0], @XMM[ 8]
2171         vld1.64         {@XMM[12]-@XMM[13]}, [r0,:128]!
2172         veor            @XMM[1], @XMM[1], @XMM[ 9]
2173         veor            @XMM[8], @XMM[6], @XMM[10]
2174         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2175         veor            @XMM[9], @XMM[4], @XMM[11]
2176         vld1.64         {@XMM[14]-@XMM[15]}, [r0,:128]!
2177         veor            @XMM[10], @XMM[2], @XMM[12]
2178         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
2179         veor            @XMM[11], @XMM[7], @XMM[13]
2180         veor            @XMM[12], @XMM[3], @XMM[14]
2181         vst1.8          {@XMM[10]-@XMM[11]}, [$out]!
2182         veor            @XMM[13], @XMM[5], @XMM[15]
2183         vst1.8          {@XMM[12]-@XMM[13]}, [$out]!
2184
2185         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2186
2187         subs            $len, #0x80
2188         bpl             .Lxts_dec_loop
2189
2190 .Lxts_dec_short:
2191         adds            $len, #0x70
2192         bmi             .Lxts_dec_done
2193
2194         vldmia          $magic, {$twmask}       @ load XTS magic
2195         vshr.s64        @T[0], @XMM[8], #63
2196         mov             r0, sp
2197         vand            @T[0], @T[0], $twmask
2198 ___
2199 for($i=9;$i<16;$i++) {
2200 $code.=<<___;
2201         vadd.u64        @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2202         vst1.64         {@XMM[$i-1]}, [r0,:128]!
2203         vswp            `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2204         vshr.s64        @T[1], @XMM[$i], #63
2205         veor            @XMM[$i], @XMM[$i], @T[0]
2206         vand            @T[1], @T[1], $twmask
2207 ___
2208         @T=reverse(@T);
2209
2210 $code.=<<___ if ($i>=10);
2211         vld1.8          {@XMM[$i-10]}, [$inp]!
2212         subs            $len, #0x10
2213         bmi             .Lxts_dec_`$i-9`
2214 ___
2215 $code.=<<___ if ($i>=11);
2216         veor            @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2217 ___
2218 }
2219 $code.=<<___;
2220         sub             $len, #0x10
2221         vst1.64         {@XMM[15]}, [r0,:128]           @ next round tweak
2222
2223         vld1.8          {@XMM[6]}, [$inp]!
2224         veor            @XMM[5], @XMM[5], @XMM[13]
2225 #ifndef BSAES_ASM_EXTENDED_KEY
2226         add             r4, sp, #0x90                   @ pass key schedule
2227 #else
2228         add             r4, $key, #248                  @ pass key schedule
2229 #endif
2230         veor            @XMM[6], @XMM[6], @XMM[14]
2231         mov             r5, $rounds                     @ pass rounds
2232         mov             r0, sp
2233
2234         bl              _bsaes_decrypt8
2235
2236         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2237         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
2238         veor            @XMM[0], @XMM[0], @XMM[ 8]
2239         vld1.64         {@XMM[12]-@XMM[13]}, [r0,:128]!
2240         veor            @XMM[1], @XMM[1], @XMM[ 9]
2241         veor            @XMM[8], @XMM[6], @XMM[10]
2242         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2243         veor            @XMM[9], @XMM[4], @XMM[11]
2244         vld1.64         {@XMM[14]}, [r0,:128]!
2245         veor            @XMM[10], @XMM[2], @XMM[12]
2246         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
2247         veor            @XMM[11], @XMM[7], @XMM[13]
2248         veor            @XMM[12], @XMM[3], @XMM[14]
2249         vst1.8          {@XMM[10]-@XMM[11]}, [$out]!
2250         vst1.8          {@XMM[12]}, [$out]!
2251
2252         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2253         b               .Lxts_dec_done
2254 .align  4
2255 .Lxts_dec_6:
2256         vst1.64         {@XMM[14]}, [r0,:128]           @ next round tweak
2257
2258         veor            @XMM[4], @XMM[4], @XMM[12]
2259 #ifndef BSAES_ASM_EXTENDED_KEY
2260         add             r4, sp, #0x90                   @ pass key schedule
2261 #else
2262         add             r4, $key, #248                  @ pass key schedule
2263 #endif
2264         veor            @XMM[5], @XMM[5], @XMM[13]
2265         mov             r5, $rounds                     @ pass rounds
2266         mov             r0, sp
2267
2268         bl              _bsaes_decrypt8
2269
2270         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2271         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
2272         veor            @XMM[0], @XMM[0], @XMM[ 8]
2273         vld1.64         {@XMM[12]-@XMM[13]}, [r0,:128]!
2274         veor            @XMM[1], @XMM[1], @XMM[ 9]
2275         veor            @XMM[8], @XMM[6], @XMM[10]
2276         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2277         veor            @XMM[9], @XMM[4], @XMM[11]
2278         veor            @XMM[10], @XMM[2], @XMM[12]
2279         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
2280         veor            @XMM[11], @XMM[7], @XMM[13]
2281         vst1.8          {@XMM[10]-@XMM[11]}, [$out]!
2282
2283         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2284         b               .Lxts_dec_done
2285 .align  4
2286 .Lxts_dec_5:
2287         veor            @XMM[3], @XMM[3], @XMM[11]
2288 #ifndef BSAES_ASM_EXTENDED_KEY
2289         add             r4, sp, #0x90                   @ pass key schedule
2290 #else
2291         add             r4, $key, #248                  @ pass key schedule
2292 #endif
2293         veor            @XMM[4], @XMM[4], @XMM[12]
2294         mov             r5, $rounds                     @ pass rounds
2295         mov             r0, sp
2296
2297         bl              _bsaes_decrypt8
2298
2299         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2300         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
2301         veor            @XMM[0], @XMM[0], @XMM[ 8]
2302         vld1.64         {@XMM[12]}, [r0,:128]!
2303         veor            @XMM[1], @XMM[1], @XMM[ 9]
2304         veor            @XMM[8], @XMM[6], @XMM[10]
2305         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2306         veor            @XMM[9], @XMM[4], @XMM[11]
2307         veor            @XMM[10], @XMM[2], @XMM[12]
2308         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
2309         vst1.8          {@XMM[10]}, [$out]!
2310
2311         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2312         b               .Lxts_dec_done
2313 .align  4
2314 .Lxts_dec_4:
2315         veor            @XMM[2], @XMM[2], @XMM[10]
2316 #ifndef BSAES_ASM_EXTENDED_KEY
2317         add             r4, sp, #0x90                   @ pass key schedule
2318 #else
2319         add             r4, $key, #248                  @ pass key schedule
2320 #endif
2321         veor            @XMM[3], @XMM[3], @XMM[11]
2322         mov             r5, $rounds                     @ pass rounds
2323         mov             r0, sp
2324
2325         bl              _bsaes_decrypt8
2326
2327         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2328         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
2329         veor            @XMM[0], @XMM[0], @XMM[ 8]
2330         veor            @XMM[1], @XMM[1], @XMM[ 9]
2331         veor            @XMM[8], @XMM[6], @XMM[10]
2332         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2333         veor            @XMM[9], @XMM[4], @XMM[11]
2334         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
2335
2336         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2337         b               .Lxts_dec_done
2338 .align  4
2339 .Lxts_dec_3:
2340         veor            @XMM[1], @XMM[1], @XMM[9]
2341 #ifndef BSAES_ASM_EXTENDED_KEY
2342         add             r4, sp, #0x90                   @ pass key schedule
2343 #else
2344         add             r4, $key, #248                  @ pass key schedule
2345 #endif
2346         veor            @XMM[2], @XMM[2], @XMM[10]
2347         mov             r5, $rounds                     @ pass rounds
2348         mov             r0, sp
2349
2350         bl              _bsaes_decrypt8
2351
2352         vld1.64         {@XMM[8]-@XMM[9]}, [r0,:128]!
2353         vld1.64         {@XMM[10]}, [r0,:128]!
2354         veor            @XMM[0], @XMM[0], @XMM[ 8]
2355         veor            @XMM[1], @XMM[1], @XMM[ 9]
2356         veor            @XMM[8], @XMM[6], @XMM[10]
2357         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2358         vst1.8          {@XMM[8]}, [$out]!
2359
2360         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2361         b               .Lxts_dec_done
2362 .align  4
2363 .Lxts_dec_2:
2364         veor            @XMM[0], @XMM[0], @XMM[8]
2365 #ifndef BSAES_ASM_EXTENDED_KEY
2366         add             r4, sp, #0x90                   @ pass key schedule
2367 #else
2368         add             r4, $key, #248                  @ pass key schedule
2369 #endif
2370         veor            @XMM[1], @XMM[1], @XMM[9]
2371         mov             r5, $rounds                     @ pass rounds
2372         mov             r0, sp
2373
2374         bl              _bsaes_decrypt8
2375
2376         vld1.64         {@XMM[8]-@XMM[9]}, [r0,:128]!
2377         veor            @XMM[0], @XMM[0], @XMM[ 8]
2378         veor            @XMM[1], @XMM[1], @XMM[ 9]
2379         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2380
2381         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2382         b               .Lxts_dec_done
2383 .align  4
2384 .Lxts_dec_1:
2385         mov             r0, sp
2386         veor            @XMM[0], @XMM[0], @XMM[8]
2387         mov             r1, sp
2388         vst1.8          {@XMM[0]}, [sp,:128]
2389         mov             r5, $magic                      @ preserve magic
2390         mov             r2, $key
2391         mov             r4, $fp                         @ preserve fp
2392
2393         bl              AES_decrypt
2394
2395         vld1.8          {@XMM[0]}, [sp,:128]
2396         veor            @XMM[0], @XMM[0], @XMM[8]
2397         vst1.8          {@XMM[0]}, [$out]!
2398         mov             $fp, r4
2399         mov             $magic, r5
2400
2401         vmov            @XMM[8], @XMM[9]                @ next round tweak
2402
2403 .Lxts_dec_done:
2404 #ifndef XTS_CHAIN_TWEAK
2405         adds            $len, #0x10
2406         beq             .Lxts_dec_ret
2407
2408         @ calculate one round of extra tweak for the stolen ciphertext
2409         vldmia          $magic, {$twmask}
2410         vshr.s64        @XMM[6], @XMM[8], #63
2411         vand            @XMM[6], @XMM[6], $twmask
2412         vadd.u64        @XMM[9], @XMM[8], @XMM[8]
2413         vswp            `&Dhi("@XMM[6]")`,`&Dlo("@XMM[6]")`
2414         veor            @XMM[9], @XMM[9], @XMM[6]
2415
2416         @ perform the final decryption with the last tweak value
2417         vld1.8          {@XMM[0]}, [$inp]!
2418         mov             r0, sp
2419         veor            @XMM[0], @XMM[0], @XMM[9]
2420         mov             r1, sp
2421         vst1.8          {@XMM[0]}, [sp,:128]
2422         mov             r2, $key
2423         mov             r4, $fp                 @ preserve fp
2424
2425         bl              AES_decrypt
2426
2427         vld1.8          {@XMM[0]}, [sp,:128]
2428         veor            @XMM[0], @XMM[0], @XMM[9]
2429         vst1.8          {@XMM[0]}, [$out]
2430
2431         mov             r6, $out
2432 .Lxts_dec_steal:
2433         ldrb            r1, [$out]
2434         ldrb            r0, [$inp], #1
2435         strb            r1, [$out, #0x10]
2436         strb            r0, [$out], #1
2437
2438         subs            $len, #1
2439         bhi             .Lxts_dec_steal
2440
2441         vld1.8          {@XMM[0]}, [r6]
2442         mov             r0, sp
2443         veor            @XMM[0], @XMM[8]
2444         mov             r1, sp
2445         vst1.8          {@XMM[0]}, [sp,:128]
2446         mov             r2, $key
2447
2448         bl              AES_decrypt
2449
2450         vld1.8          {@XMM[0]}, [sp,:128]
2451         veor            @XMM[0], @XMM[0], @XMM[8]
2452         vst1.8          {@XMM[0]}, [r6]
2453         mov             $fp, r4
2454 #endif
2455
2456 .Lxts_dec_ret:
2457         bic             r0, $fp, #0xf
2458         vmov.i32        q0, #0
2459         vmov.i32        q1, #0
2460 #ifdef  XTS_CHAIN_TWEAK
2461         ldr             r1, [$fp, #0x20+VFP_ABI_FRAME]  @ chain tweak
2462 #endif
2463 .Lxts_dec_bzero:                                @ wipe key schedule [if any]
2464         vstmia          sp!, {q0-q1}
2465         cmp             sp, r0
2466         bne             .Lxts_dec_bzero
2467
2468         mov             sp, $fp
2469 #ifdef  XTS_CHAIN_TWEAK
2470         vst1.8          {@XMM[8]}, [r1]
2471 #endif
2472         VFP_ABI_POP
2473         ldmia           sp!, {r4-r10, pc}       @ return
2474
2475 .size   bsaes_xts_decrypt,.-bsaes_xts_decrypt
2476 ___
2477 }
2478 $code.=<<___;
2479 #endif
2480 ___
2481
2482 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
2483
2484 open SELF,$0;
2485 while(<SELF>) {
2486         next if (/^#!/);
2487         last if (!s/^#/@/ and !/^$/);
2488         print;
2489 }
2490 close SELF;
2491
2492 print $code;
2493
2494 close STDOUT or die "error closing STDOUT: $!";