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