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