8515c9d0abd99b3dc735579ab7c1ebd8a7297b27
[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 .fpu    neon
656
657 .type   _bsaes_decrypt8,%function
658 .align  4
659 _bsaes_decrypt8:
660         adr     $const,_bsaes_decrypt8
661         vldmia  $key!, {@XMM[9]}                @ round 0 key
662         add     $const,$const,#.LM0ISR-_bsaes_decrypt8
663
664         vldmia  $const!, {@XMM[8]}              @ .LM0ISR
665         veor    @XMM[10], @XMM[0], @XMM[9]      @ xor with round0 key
666         veor    @XMM[11], @XMM[1], @XMM[9]
667          vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
668          vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
669         veor    @XMM[12], @XMM[2], @XMM[9]
670          vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
671          vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
672         veor    @XMM[13], @XMM[3], @XMM[9]
673          vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
674          vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
675         veor    @XMM[14], @XMM[4], @XMM[9]
676          vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
677          vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
678         veor    @XMM[15], @XMM[5], @XMM[9]
679          vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
680          vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
681         veor    @XMM[10], @XMM[6], @XMM[9]
682          vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
683          vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
684         veor    @XMM[11], @XMM[7], @XMM[9]
685          vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
686          vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
687          vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
688          vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
689 ___
690         &bitslice       (@XMM[0..7, 8..11]);
691 $code.=<<___;
692         sub     $rounds,$rounds,#1
693         b       .Ldec_sbox
694 .align  4
695 .Ldec_loop:
696 ___
697         &ShiftRows      (@XMM[0..7, 8..12]);
698 $code.=".Ldec_sbox:\n";
699         &InvSbox        (@XMM[0..7, 8..15]);
700 $code.=<<___;
701         subs    $rounds,$rounds,#1
702         bcc     .Ldec_done
703 ___
704         &InvMixColumns  (@XMM[0,1,6,4,2,7,3,5, 8..15]);
705 $code.=<<___;
706         vldmia  $const, {@XMM[12]}              @ .LISR
707         ite     eq                              @ Thumb2 thing, sanity check in ARM
708         addeq   $const,$const,#0x10
709         bne     .Ldec_loop
710         vldmia  $const, {@XMM[12]}              @ .LISRM0
711         b       .Ldec_loop
712 .align  4
713 .Ldec_done:
714 ___
715         &bitslice       (@XMM[0,1,6,4,2,7,3,5, 8..11]);
716 $code.=<<___;
717         vldmia  $key, {@XMM[8]}                 @ last round key
718         veor    @XMM[6], @XMM[6], @XMM[8]
719         veor    @XMM[4], @XMM[4], @XMM[8]
720         veor    @XMM[2], @XMM[2], @XMM[8]
721         veor    @XMM[7], @XMM[7], @XMM[8]
722         veor    @XMM[3], @XMM[3], @XMM[8]
723         veor    @XMM[5], @XMM[5], @XMM[8]
724         veor    @XMM[0], @XMM[0], @XMM[8]
725         veor    @XMM[1], @XMM[1], @XMM[8]
726         bx      lr
727 .size   _bsaes_decrypt8,.-_bsaes_decrypt8
728
729 .type   _bsaes_const,%object
730 .align  6
731 _bsaes_const:
732 .LM0ISR:        @ InvShiftRows constants
733         .quad   0x0a0e0206070b0f03, 0x0004080c0d010509
734 .LISR:
735         .quad   0x0504070602010003, 0x0f0e0d0c080b0a09
736 .LISRM0:
737         .quad   0x01040b0e0205080f, 0x0306090c00070a0d
738 .LM0SR:         @ ShiftRows constants
739         .quad   0x0a0e02060f03070b, 0x0004080c05090d01
740 .LSR:
741         .quad   0x0504070600030201, 0x0f0e0d0c0a09080b
742 .LSRM0:
743         .quad   0x0304090e00050a0f, 0x01060b0c0207080d
744 .LM0:
745         .quad   0x02060a0e03070b0f, 0x0004080c0105090d
746 .LREVM0SR:
747         .quad   0x090d01050c000408, 0x03070b0f060a0e02
748 .asciz  "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
749 .align  6
750 .size   _bsaes_const,.-_bsaes_const
751
752 .type   _bsaes_encrypt8,%function
753 .align  4
754 _bsaes_encrypt8:
755         adr     $const,_bsaes_encrypt8
756         vldmia  $key!, {@XMM[9]}                @ round 0 key
757         sub     $const,$const,#_bsaes_encrypt8-.LM0SR
758
759         vldmia  $const!, {@XMM[8]}              @ .LM0SR
760 _bsaes_encrypt8_alt:
761         veor    @XMM[10], @XMM[0], @XMM[9]      @ xor with round0 key
762         veor    @XMM[11], @XMM[1], @XMM[9]
763          vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
764          vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
765         veor    @XMM[12], @XMM[2], @XMM[9]
766          vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
767          vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
768         veor    @XMM[13], @XMM[3], @XMM[9]
769          vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
770          vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
771         veor    @XMM[14], @XMM[4], @XMM[9]
772          vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
773          vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
774         veor    @XMM[15], @XMM[5], @XMM[9]
775          vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
776          vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
777         veor    @XMM[10], @XMM[6], @XMM[9]
778          vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
779          vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
780         veor    @XMM[11], @XMM[7], @XMM[9]
781          vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
782          vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
783          vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
784          vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
785 _bsaes_encrypt8_bitslice:
786 ___
787         &bitslice       (@XMM[0..7, 8..11]);
788 $code.=<<___;
789         sub     $rounds,$rounds,#1
790         b       .Lenc_sbox
791 .align  4
792 .Lenc_loop:
793 ___
794         &ShiftRows      (@XMM[0..7, 8..12]);
795 $code.=".Lenc_sbox:\n";
796         &Sbox           (@XMM[0..7, 8..15]);
797 $code.=<<___;
798         subs    $rounds,$rounds,#1
799         bcc     .Lenc_done
800 ___
801         &MixColumns     (@XMM[0,1,4,6,3,7,2,5, 8..15]);
802 $code.=<<___;
803         vldmia  $const, {@XMM[12]}              @ .LSR
804         ite     eq                              @ Thumb2 thing, samity check in ARM
805         addeq   $const,$const,#0x10
806         bne     .Lenc_loop
807         vldmia  $const, {@XMM[12]}              @ .LSRM0
808         b       .Lenc_loop
809 .align  4
810 .Lenc_done:
811 ___
812         # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
813         &bitslice       (@XMM[0,1,4,6,3,7,2,5, 8..11]);
814 $code.=<<___;
815         vldmia  $key, {@XMM[8]}                 @ last round key
816         veor    @XMM[4], @XMM[4], @XMM[8]
817         veor    @XMM[6], @XMM[6], @XMM[8]
818         veor    @XMM[3], @XMM[3], @XMM[8]
819         veor    @XMM[7], @XMM[7], @XMM[8]
820         veor    @XMM[2], @XMM[2], @XMM[8]
821         veor    @XMM[5], @XMM[5], @XMM[8]
822         veor    @XMM[0], @XMM[0], @XMM[8]
823         veor    @XMM[1], @XMM[1], @XMM[8]
824         bx      lr
825 .size   _bsaes_encrypt8,.-_bsaes_encrypt8
826 ___
827 }
828 {
829 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
830
831 sub bitslice_key {
832 my @x=reverse(@_[0..7]);
833 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
834
835         &swapmove       (@x[0,1],1,$bs0,$t2,$t3);
836 $code.=<<___;
837         @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
838         vmov    @x[2], @x[0]
839         vmov    @x[3], @x[1]
840 ___
841         #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
842
843         &swapmove2x     (@x[0,2,1,3],2,$bs1,$t2,$t3);
844 $code.=<<___;
845         @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
846         vmov    @x[4], @x[0]
847         vmov    @x[6], @x[2]
848         vmov    @x[5], @x[1]
849         vmov    @x[7], @x[3]
850 ___
851         &swapmove2x     (@x[0,4,1,5],4,$bs2,$t2,$t3);
852         &swapmove2x     (@x[2,6,3,7],4,$bs2,$t2,$t3);
853 }
854
855 $code.=<<___;
856 .type   _bsaes_key_convert,%function
857 .align  4
858 _bsaes_key_convert:
859         adr     $const,_bsaes_key_convert
860         vld1.8  {@XMM[7]},  [$inp]!             @ load round 0 key
861         sub     $const,$const,#_bsaes_key_convert-.LM0
862         vld1.8  {@XMM[15]}, [$inp]!             @ load round 1 key
863
864         vmov.i8 @XMM[8],  #0x01                 @ bit masks
865         vmov.i8 @XMM[9],  #0x02
866         vmov.i8 @XMM[10], #0x04
867         vmov.i8 @XMM[11], #0x08
868         vmov.i8 @XMM[12], #0x10
869         vmov.i8 @XMM[13], #0x20
870         vldmia  $const, {@XMM[14]}              @ .LM0
871
872 #ifdef __ARMEL__
873         vrev32.8        @XMM[7],  @XMM[7]
874         vrev32.8        @XMM[15], @XMM[15]
875 #endif
876         sub     $rounds,$rounds,#1
877         vstmia  $out!, {@XMM[7]}                @ save round 0 key
878         b       .Lkey_loop
879
880 .align  4
881 .Lkey_loop:
882         vtbl.8  `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
883         vtbl.8  `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
884         vmov.i8 @XMM[6],  #0x40
885         vmov.i8 @XMM[15], #0x80
886
887         vtst.8  @XMM[0], @XMM[7], @XMM[8]
888         vtst.8  @XMM[1], @XMM[7], @XMM[9]
889         vtst.8  @XMM[2], @XMM[7], @XMM[10]
890         vtst.8  @XMM[3], @XMM[7], @XMM[11]
891         vtst.8  @XMM[4], @XMM[7], @XMM[12]
892         vtst.8  @XMM[5], @XMM[7], @XMM[13]
893         vtst.8  @XMM[6], @XMM[7], @XMM[6]
894         vtst.8  @XMM[7], @XMM[7], @XMM[15]
895         vld1.8  {@XMM[15]}, [$inp]!             @ load next round key
896         vmvn    @XMM[0], @XMM[0]                @ "pnot"
897         vmvn    @XMM[1], @XMM[1]
898         vmvn    @XMM[5], @XMM[5]
899         vmvn    @XMM[6], @XMM[6]
900 #ifdef __ARMEL__
901         vrev32.8        @XMM[15], @XMM[15]
902 #endif
903         subs    $rounds,$rounds,#1
904         vstmia  $out!,{@XMM[0]-@XMM[7]}         @ write bit-sliced round key
905         bne     .Lkey_loop
906
907         vmov.i8 @XMM[7],#0x63                   @ compose .L63
908         @ don't save last round key
909         bx      lr
910 .size   _bsaes_key_convert,.-_bsaes_key_convert
911 ___
912 }
913
914 if (0) {                # following four functions are unsupported interface
915                         # used for benchmarking...
916 $code.=<<___;
917 .globl  bsaes_enc_key_convert
918 .type   bsaes_enc_key_convert,%function
919 .align  4
920 bsaes_enc_key_convert:
921         stmdb   sp!,{r4-r6,lr}
922         vstmdb  sp!,{d8-d15}            @ ABI specification says so
923
924         ldr     r5,[$inp,#240]                  @ pass rounds
925         mov     r4,$inp                         @ pass key
926         mov     r12,$out                        @ pass key schedule
927         bl      _bsaes_key_convert
928         veor    @XMM[7],@XMM[7],@XMM[15]        @ fix up last round key
929         vstmia  r12, {@XMM[7]}                  @ save last round key
930
931         vldmia  sp!,{d8-d15}
932         ldmia   sp!,{r4-r6,pc}
933 .size   bsaes_enc_key_convert,.-bsaes_enc_key_convert
934
935 .globl  bsaes_encrypt_128
936 .type   bsaes_encrypt_128,%function
937 .align  4
938 bsaes_encrypt_128:
939         stmdb   sp!,{r4-r6,lr}
940         vstmdb  sp!,{d8-d15}            @ ABI specification says so
941 .Lenc128_loop:
942         vld1.8  {@XMM[0]-@XMM[1]}, [$inp]!      @ load input
943         vld1.8  {@XMM[2]-@XMM[3]}, [$inp]!
944         mov     r4,$key                         @ pass the key
945         vld1.8  {@XMM[4]-@XMM[5]}, [$inp]!
946         mov     r5,#10                          @ pass rounds
947         vld1.8  {@XMM[6]-@XMM[7]}, [$inp]!
948
949         bl      _bsaes_encrypt8
950
951         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
952         vst1.8  {@XMM[4]}, [$out]!
953         vst1.8  {@XMM[6]}, [$out]!
954         vst1.8  {@XMM[3]}, [$out]!
955         vst1.8  {@XMM[7]}, [$out]!
956         vst1.8  {@XMM[2]}, [$out]!
957         subs    $len,$len,#0x80
958         vst1.8  {@XMM[5]}, [$out]!
959         bhi     .Lenc128_loop
960
961         vldmia  sp!,{d8-d15}
962         ldmia   sp!,{r4-r6,pc}
963 .size   bsaes_encrypt_128,.-bsaes_encrypt_128
964
965 .globl  bsaes_dec_key_convert
966 .type   bsaes_dec_key_convert,%function
967 .align  4
968 bsaes_dec_key_convert:
969         stmdb   sp!,{r4-r6,lr}
970         vstmdb  sp!,{d8-d15}            @ ABI specification says so
971
972         ldr     r5,[$inp,#240]                  @ pass rounds
973         mov     r4,$inp                         @ pass key
974         mov     r12,$out                        @ pass key schedule
975         bl      _bsaes_key_convert
976         vldmia  $out, {@XMM[6]}
977         vstmia  r12,  {@XMM[15]}                @ save last round key
978         veor    @XMM[7], @XMM[7], @XMM[6]       @ fix up round 0 key
979         vstmia  $out, {@XMM[7]}
980
981         vldmia  sp!,{d8-d15}
982         ldmia   sp!,{r4-r6,pc}
983 .size   bsaes_dec_key_convert,.-bsaes_dec_key_convert
984
985 .globl  bsaes_decrypt_128
986 .type   bsaes_decrypt_128,%function
987 .align  4
988 bsaes_decrypt_128:
989         stmdb   sp!,{r4-r6,lr}
990         vstmdb  sp!,{d8-d15}            @ ABI specification says so
991 .Ldec128_loop:
992         vld1.8  {@XMM[0]-@XMM[1]}, [$inp]!      @ load input
993         vld1.8  {@XMM[2]-@XMM[3]}, [$inp]!
994         mov     r4,$key                         @ pass the key
995         vld1.8  {@XMM[4]-@XMM[5]}, [$inp]!
996         mov     r5,#10                          @ pass rounds
997         vld1.8  {@XMM[6]-@XMM[7]}, [$inp]!
998
999         bl      _bsaes_decrypt8
1000
1001         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1002         vst1.8  {@XMM[6]}, [$out]!
1003         vst1.8  {@XMM[4]}, [$out]!
1004         vst1.8  {@XMM[2]}, [$out]!
1005         vst1.8  {@XMM[7]}, [$out]!
1006         vst1.8  {@XMM[3]}, [$out]!
1007         subs    $len,$len,#0x80
1008         vst1.8  {@XMM[5]}, [$out]!
1009         bhi     .Ldec128_loop
1010
1011         vldmia  sp!,{d8-d15}
1012         ldmia   sp!,{r4-r6,pc}
1013 .size   bsaes_decrypt_128,.-bsaes_decrypt_128
1014 ___
1015 }
1016 {
1017 my ($inp,$out,$len,$key, $ivp,$fp,$rounds)=map("r$_",(0..3,8..10));
1018 my ($keysched)=("sp");
1019
1020 $code.=<<___;
1021 .extern AES_cbc_encrypt
1022 .extern AES_decrypt
1023
1024 .global bsaes_cbc_encrypt
1025 .type   bsaes_cbc_encrypt,%function
1026 .align  5
1027 bsaes_cbc_encrypt:
1028 #ifndef __KERNEL__
1029         cmp     $len, #128
1030 #ifndef __thumb__
1031         blo     AES_cbc_encrypt
1032 #else
1033         bhs     1f
1034         b       AES_cbc_encrypt
1035 1:
1036 #endif
1037 #endif
1038
1039         @ it is up to the caller to make sure we are called with enc == 0
1040
1041         mov     ip, sp
1042         stmdb   sp!, {r4-r10, lr}
1043         VFP_ABI_PUSH
1044         ldr     $ivp, [ip]                      @ IV is 1st arg on the stack
1045         mov     $len, $len, lsr#4               @ len in 16 byte blocks
1046         sub     sp, #0x10                       @ scratch space to carry over the IV
1047         mov     $fp, sp                         @ save sp
1048
1049         ldr     $rounds, [$key, #240]           @ get # of rounds
1050 #ifndef BSAES_ASM_EXTENDED_KEY
1051         @ allocate the key schedule on the stack
1052         sub     r12, sp, $rounds, lsl#7         @ 128 bytes per inner round key
1053         add     r12, #`128-32`                  @ sifze of bit-slices key schedule
1054
1055         @ populate the key schedule
1056         mov     r4, $key                        @ pass key
1057         mov     r5, $rounds                     @ pass # of rounds
1058         mov     sp, r12                         @ sp is $keysched
1059         bl      _bsaes_key_convert
1060         vldmia  $keysched, {@XMM[6]}
1061         vstmia  r12,  {@XMM[15]}                @ save last round key
1062         veor    @XMM[7], @XMM[7], @XMM[6]       @ fix up round 0 key
1063         vstmia  $keysched, {@XMM[7]}
1064 #else
1065         ldr     r12, [$key, #244]
1066         eors    r12, #1
1067         beq     0f
1068
1069         @ populate the key schedule
1070         str     r12, [$key, #244]
1071         mov     r4, $key                        @ pass key
1072         mov     r5, $rounds                     @ pass # of rounds
1073         add     r12, $key, #248                 @ pass key schedule
1074         bl      _bsaes_key_convert
1075         add     r4, $key, #248
1076         vldmia  r4, {@XMM[6]}
1077         vstmia  r12, {@XMM[15]}                 @ save last round key
1078         veor    @XMM[7], @XMM[7], @XMM[6]       @ fix up round 0 key
1079         vstmia  r4, {@XMM[7]}
1080
1081 .align  2
1082 0:
1083 #endif
1084
1085         vld1.8  {@XMM[15]}, [$ivp]              @ load IV
1086         b       .Lcbc_dec_loop
1087
1088 .align  4
1089 .Lcbc_dec_loop:
1090         subs    $len, $len, #0x8
1091         bmi     .Lcbc_dec_loop_finish
1092
1093         vld1.8  {@XMM[0]-@XMM[1]}, [$inp]!      @ load input
1094         vld1.8  {@XMM[2]-@XMM[3]}, [$inp]!
1095 #ifndef BSAES_ASM_EXTENDED_KEY
1096         mov     r4, $keysched                   @ pass the key
1097 #else
1098         add     r4, $key, #248
1099 #endif
1100         vld1.8  {@XMM[4]-@XMM[5]}, [$inp]!
1101         mov     r5, $rounds
1102         vld1.8  {@XMM[6]-@XMM[7]}, [$inp]
1103         sub     $inp, $inp, #0x60
1104         vstmia  $fp, {@XMM[15]}                 @ put aside IV
1105
1106         bl      _bsaes_decrypt8
1107
1108         vldmia  $fp, {@XMM[14]}                 @ reload IV
1109         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1110         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1111         vld1.8  {@XMM[10]-@XMM[11]}, [$inp]!
1112         veor    @XMM[1], @XMM[1], @XMM[8]
1113         veor    @XMM[6], @XMM[6], @XMM[9]
1114         vld1.8  {@XMM[12]-@XMM[13]}, [$inp]!
1115         veor    @XMM[4], @XMM[4], @XMM[10]
1116         veor    @XMM[2], @XMM[2], @XMM[11]
1117         vld1.8  {@XMM[14]-@XMM[15]}, [$inp]!
1118         veor    @XMM[7], @XMM[7], @XMM[12]
1119         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1120         veor    @XMM[3], @XMM[3], @XMM[13]
1121         vst1.8  {@XMM[6]}, [$out]!
1122         veor    @XMM[5], @XMM[5], @XMM[14]
1123         vst1.8  {@XMM[4]}, [$out]!
1124         vst1.8  {@XMM[2]}, [$out]!
1125         vst1.8  {@XMM[7]}, [$out]!
1126         vst1.8  {@XMM[3]}, [$out]!
1127         vst1.8  {@XMM[5]}, [$out]!
1128
1129         b       .Lcbc_dec_loop
1130
1131 .Lcbc_dec_loop_finish:
1132         adds    $len, $len, #8
1133         beq     .Lcbc_dec_done
1134
1135         vld1.8  {@XMM[0]}, [$inp]!              @ load input
1136         cmp     $len, #2
1137         blo     .Lcbc_dec_one
1138         vld1.8  {@XMM[1]}, [$inp]!
1139 #ifndef BSAES_ASM_EXTENDED_KEY
1140         mov     r4, $keysched                   @ pass the key
1141 #else
1142         add     r4, $key, #248
1143 #endif
1144         mov     r5, $rounds
1145         vstmia  $fp, {@XMM[15]}                 @ put aside IV
1146         beq     .Lcbc_dec_two
1147         vld1.8  {@XMM[2]}, [$inp]!
1148         cmp     $len, #4
1149         blo     .Lcbc_dec_three
1150         vld1.8  {@XMM[3]}, [$inp]!
1151         beq     .Lcbc_dec_four
1152         vld1.8  {@XMM[4]}, [$inp]!
1153         cmp     $len, #6
1154         blo     .Lcbc_dec_five
1155         vld1.8  {@XMM[5]}, [$inp]!
1156         beq     .Lcbc_dec_six
1157         vld1.8  {@XMM[6]}, [$inp]!
1158         sub     $inp, $inp, #0x70
1159
1160         bl      _bsaes_decrypt8
1161
1162         vldmia  $fp, {@XMM[14]}                 @ reload IV
1163         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1164         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1165         vld1.8  {@XMM[10]-@XMM[11]}, [$inp]!
1166         veor    @XMM[1], @XMM[1], @XMM[8]
1167         veor    @XMM[6], @XMM[6], @XMM[9]
1168         vld1.8  {@XMM[12]-@XMM[13]}, [$inp]!
1169         veor    @XMM[4], @XMM[4], @XMM[10]
1170         veor    @XMM[2], @XMM[2], @XMM[11]
1171         vld1.8  {@XMM[15]}, [$inp]!
1172         veor    @XMM[7], @XMM[7], @XMM[12]
1173         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1174         veor    @XMM[3], @XMM[3], @XMM[13]
1175         vst1.8  {@XMM[6]}, [$out]!
1176         vst1.8  {@XMM[4]}, [$out]!
1177         vst1.8  {@XMM[2]}, [$out]!
1178         vst1.8  {@XMM[7]}, [$out]!
1179         vst1.8  {@XMM[3]}, [$out]!
1180         b       .Lcbc_dec_done
1181 .align  4
1182 .Lcbc_dec_six:
1183         sub     $inp, $inp, #0x60
1184         bl      _bsaes_decrypt8
1185         vldmia  $fp,{@XMM[14]}                  @ reload IV
1186         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1187         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1188         vld1.8  {@XMM[10]-@XMM[11]}, [$inp]!
1189         veor    @XMM[1], @XMM[1], @XMM[8]
1190         veor    @XMM[6], @XMM[6], @XMM[9]
1191         vld1.8  {@XMM[12]}, [$inp]!
1192         veor    @XMM[4], @XMM[4], @XMM[10]
1193         veor    @XMM[2], @XMM[2], @XMM[11]
1194         vld1.8  {@XMM[15]}, [$inp]!
1195         veor    @XMM[7], @XMM[7], @XMM[12]
1196         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1197         vst1.8  {@XMM[6]}, [$out]!
1198         vst1.8  {@XMM[4]}, [$out]!
1199         vst1.8  {@XMM[2]}, [$out]!
1200         vst1.8  {@XMM[7]}, [$out]!
1201         b       .Lcbc_dec_done
1202 .align  4
1203 .Lcbc_dec_five:
1204         sub     $inp, $inp, #0x50
1205         bl      _bsaes_decrypt8
1206         vldmia  $fp, {@XMM[14]}                 @ reload IV
1207         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1208         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1209         vld1.8  {@XMM[10]-@XMM[11]}, [$inp]!
1210         veor    @XMM[1], @XMM[1], @XMM[8]
1211         veor    @XMM[6], @XMM[6], @XMM[9]
1212         vld1.8  {@XMM[15]}, [$inp]!
1213         veor    @XMM[4], @XMM[4], @XMM[10]
1214         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1215         veor    @XMM[2], @XMM[2], @XMM[11]
1216         vst1.8  {@XMM[6]}, [$out]!
1217         vst1.8  {@XMM[4]}, [$out]!
1218         vst1.8  {@XMM[2]}, [$out]!
1219         b       .Lcbc_dec_done
1220 .align  4
1221 .Lcbc_dec_four:
1222         sub     $inp, $inp, #0x40
1223         bl      _bsaes_decrypt8
1224         vldmia  $fp, {@XMM[14]}                 @ reload IV
1225         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1226         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1227         vld1.8  {@XMM[10]}, [$inp]!
1228         veor    @XMM[1], @XMM[1], @XMM[8]
1229         veor    @XMM[6], @XMM[6], @XMM[9]
1230         vld1.8  {@XMM[15]}, [$inp]!
1231         veor    @XMM[4], @XMM[4], @XMM[10]
1232         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1233         vst1.8  {@XMM[6]}, [$out]!
1234         vst1.8  {@XMM[4]}, [$out]!
1235         b       .Lcbc_dec_done
1236 .align  4
1237 .Lcbc_dec_three:
1238         sub     $inp, $inp, #0x30
1239         bl      _bsaes_decrypt8
1240         vldmia  $fp, {@XMM[14]}                 @ reload IV
1241         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1242         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1243         vld1.8  {@XMM[15]}, [$inp]!
1244         veor    @XMM[1], @XMM[1], @XMM[8]
1245         veor    @XMM[6], @XMM[6], @XMM[9]
1246         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1247         vst1.8  {@XMM[6]}, [$out]!
1248         b       .Lcbc_dec_done
1249 .align  4
1250 .Lcbc_dec_two:
1251         sub     $inp, $inp, #0x20
1252         bl      _bsaes_decrypt8
1253         vldmia  $fp, {@XMM[14]}                 @ reload IV
1254         vld1.8  {@XMM[8]}, [$inp]!              @ reload input
1255         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1256         vld1.8  {@XMM[15]}, [$inp]!             @ reload input
1257         veor    @XMM[1], @XMM[1], @XMM[8]
1258         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1259         b       .Lcbc_dec_done
1260 .align  4
1261 .Lcbc_dec_one:
1262         sub     $inp, $inp, #0x10
1263         mov     $rounds, $out                   @ save original out pointer
1264         mov     $out, $fp                       @ use the iv scratch space as out buffer
1265         mov     r2, $key
1266         vmov    @XMM[4],@XMM[15]                @ just in case ensure that IV
1267         vmov    @XMM[5],@XMM[0]                 @ and input are preserved
1268         bl      AES_decrypt
1269         vld1.8  {@XMM[0]}, [$fp,:64]            @ load result
1270         veor    @XMM[0], @XMM[0], @XMM[4]       @ ^= IV
1271         vmov    @XMM[15], @XMM[5]               @ @XMM[5] holds input
1272         vst1.8  {@XMM[0]}, [$rounds]            @ write output
1273
1274 .Lcbc_dec_done:
1275 #ifndef BSAES_ASM_EXTENDED_KEY
1276         vmov.i32        q0, #0
1277         vmov.i32        q1, #0
1278 .Lcbc_dec_bzero:                                @ wipe key schedule [if any]
1279         vstmia          $keysched!, {q0-q1}
1280         cmp             $keysched, $fp
1281         bne             .Lcbc_dec_bzero
1282 #endif
1283
1284         mov     sp, $fp
1285         add     sp, #0x10                       @ add sp,$fp,#0x10 is no good for thumb
1286         vst1.8  {@XMM[15]}, [$ivp]              @ return IV
1287         VFP_ABI_POP
1288         ldmia   sp!, {r4-r10, pc}
1289 .size   bsaes_cbc_encrypt,.-bsaes_cbc_encrypt
1290 ___
1291 }
1292 {
1293 my ($inp,$out,$len,$key, $ctr,$fp,$rounds)=(map("r$_",(0..3,8..10)));
1294 my $const = "r6";       # shared with _bsaes_encrypt8_alt
1295 my $keysched = "sp";
1296
1297 $code.=<<___;
1298 .extern AES_encrypt
1299 .global bsaes_ctr32_encrypt_blocks
1300 .type   bsaes_ctr32_encrypt_blocks,%function
1301 .align  5
1302 bsaes_ctr32_encrypt_blocks:
1303         cmp     $len, #8                        @ use plain AES for
1304         blo     .Lctr_enc_short                 @ small sizes
1305
1306         mov     ip, sp
1307         stmdb   sp!, {r4-r10, lr}
1308         VFP_ABI_PUSH
1309         ldr     $ctr, [ip]                      @ ctr is 1st arg on the stack
1310         sub     sp, sp, #0x10                   @ scratch space to carry over the ctr
1311         mov     $fp, sp                         @ save sp
1312
1313         ldr     $rounds, [$key, #240]           @ get # of rounds
1314 #ifndef BSAES_ASM_EXTENDED_KEY
1315         @ allocate the key schedule on the stack
1316         sub     r12, sp, $rounds, lsl#7         @ 128 bytes per inner round key
1317         add     r12, #`128-32`                  @ size of bit-sliced key schedule
1318
1319         @ populate the key schedule
1320         mov     r4, $key                        @ pass key
1321         mov     r5, $rounds                     @ pass # of rounds
1322         mov     sp, r12                         @ sp is $keysched
1323         bl      _bsaes_key_convert
1324         veor    @XMM[7],@XMM[7],@XMM[15]        @ fix up last round key
1325         vstmia  r12, {@XMM[7]}                  @ save last round key
1326
1327         vld1.8  {@XMM[0]}, [$ctr]               @ load counter
1328         add     $ctr, $const, #.LREVM0SR-.LM0   @ borrow $ctr
1329         vldmia  $keysched, {@XMM[4]}            @ load round0 key
1330 #else
1331         ldr     r12, [$key, #244]
1332         eors    r12, #1
1333         beq     0f
1334
1335         @ populate the key schedule
1336         str     r12, [$key, #244]
1337         mov     r4, $key                        @ pass key
1338         mov     r5, $rounds                     @ pass # of rounds
1339         add     r12, $key, #248                 @ pass key schedule
1340         bl      _bsaes_key_convert
1341         veor    @XMM[7],@XMM[7],@XMM[15]        @ fix up last round key
1342         vstmia  r12, {@XMM[7]}                  @ save last round key
1343
1344 .align  2
1345 0:      add     r12, $key, #248
1346         vld1.8  {@XMM[0]}, [$ctr]               @ load counter
1347         adrl    $ctr, .LREVM0SR                 @ borrow $ctr
1348         vldmia  r12, {@XMM[4]}                  @ load round0 key
1349         sub     sp, #0x10                       @ place for adjusted round0 key
1350 #endif
1351
1352         vmov.i32        @XMM[8],#1              @ compose 1<<96
1353         veor            @XMM[9],@XMM[9],@XMM[9]
1354         vrev32.8        @XMM[0],@XMM[0]
1355         vext.8          @XMM[8],@XMM[9],@XMM[8],#4
1356         vrev32.8        @XMM[4],@XMM[4]
1357         vadd.u32        @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1358         vstmia  $keysched, {@XMM[4]}            @ save adjusted round0 key
1359         b       .Lctr_enc_loop
1360
1361 .align  4
1362 .Lctr_enc_loop:
1363         vadd.u32        @XMM[10], @XMM[8], @XMM[9]      @ compose 3<<96
1364         vadd.u32        @XMM[1], @XMM[0], @XMM[8]       @ +1
1365         vadd.u32        @XMM[2], @XMM[0], @XMM[9]       @ +2
1366         vadd.u32        @XMM[3], @XMM[0], @XMM[10]      @ +3
1367         vadd.u32        @XMM[4], @XMM[1], @XMM[10]
1368         vadd.u32        @XMM[5], @XMM[2], @XMM[10]
1369         vadd.u32        @XMM[6], @XMM[3], @XMM[10]
1370         vadd.u32        @XMM[7], @XMM[4], @XMM[10]
1371         vadd.u32        @XMM[10], @XMM[5], @XMM[10]     @ next counter
1372
1373         @ Borrow prologue from _bsaes_encrypt8 to use the opportunity
1374         @ to flip byte order in 32-bit counter
1375
1376         vldmia          $keysched, {@XMM[9]}            @ load round0 key
1377 #ifndef BSAES_ASM_EXTENDED_KEY
1378         add             r4, $keysched, #0x10            @ pass next round key
1379 #else
1380         add             r4, $key, #`248+16`
1381 #endif
1382         vldmia          $ctr, {@XMM[8]}                 @ .LREVM0SR
1383         mov             r5, $rounds                     @ pass rounds
1384         vstmia          $fp, {@XMM[10]}                 @ save next counter
1385         sub             $const, $ctr, #.LREVM0SR-.LSR   @ pass constants
1386
1387         bl              _bsaes_encrypt8_alt
1388
1389         subs            $len, $len, #8
1390         blo             .Lctr_enc_loop_done
1391
1392         vld1.8          {@XMM[8]-@XMM[9]}, [$inp]!      @ load input
1393         vld1.8          {@XMM[10]-@XMM[11]}, [$inp]!
1394         veor            @XMM[0], @XMM[8]
1395         veor            @XMM[1], @XMM[9]
1396         vld1.8          {@XMM[12]-@XMM[13]}, [$inp]!
1397         veor            @XMM[4], @XMM[10]
1398         veor            @XMM[6], @XMM[11]
1399         vld1.8          {@XMM[14]-@XMM[15]}, [$inp]!
1400         veor            @XMM[3], @XMM[12]
1401         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1402         veor            @XMM[7], @XMM[13]
1403         veor            @XMM[2], @XMM[14]
1404         vst1.8          {@XMM[4]}, [$out]!
1405         veor            @XMM[5], @XMM[15]
1406         vst1.8          {@XMM[6]}, [$out]!
1407         vmov.i32        @XMM[8], #1                     @ compose 1<<96
1408         vst1.8          {@XMM[3]}, [$out]!
1409         veor            @XMM[9], @XMM[9], @XMM[9]
1410         vst1.8          {@XMM[7]}, [$out]!
1411         vext.8          @XMM[8], @XMM[9], @XMM[8], #4
1412         vst1.8          {@XMM[2]}, [$out]!
1413         vadd.u32        @XMM[9],@XMM[8],@XMM[8]         @ compose 2<<96
1414         vst1.8          {@XMM[5]}, [$out]!
1415         vldmia          $fp, {@XMM[0]}                  @ load counter
1416
1417         bne             .Lctr_enc_loop
1418         b               .Lctr_enc_done
1419
1420 .align  4
1421 .Lctr_enc_loop_done:
1422         add             $len, $len, #8
1423         vld1.8          {@XMM[8]}, [$inp]!      @ load input
1424         veor            @XMM[0], @XMM[8]
1425         vst1.8          {@XMM[0]}, [$out]!      @ write output
1426         cmp             $len, #2
1427         blo             .Lctr_enc_done
1428         vld1.8          {@XMM[9]}, [$inp]!
1429         veor            @XMM[1], @XMM[9]
1430         vst1.8          {@XMM[1]}, [$out]!
1431         beq             .Lctr_enc_done
1432         vld1.8          {@XMM[10]}, [$inp]!
1433         veor            @XMM[4], @XMM[10]
1434         vst1.8          {@XMM[4]}, [$out]!
1435         cmp             $len, #4
1436         blo             .Lctr_enc_done
1437         vld1.8          {@XMM[11]}, [$inp]!
1438         veor            @XMM[6], @XMM[11]
1439         vst1.8          {@XMM[6]}, [$out]!
1440         beq             .Lctr_enc_done
1441         vld1.8          {@XMM[12]}, [$inp]!
1442         veor            @XMM[3], @XMM[12]
1443         vst1.8          {@XMM[3]}, [$out]!
1444         cmp             $len, #6
1445         blo             .Lctr_enc_done
1446         vld1.8          {@XMM[13]}, [$inp]!
1447         veor            @XMM[7], @XMM[13]
1448         vst1.8          {@XMM[7]}, [$out]!
1449         beq             .Lctr_enc_done
1450         vld1.8          {@XMM[14]}, [$inp]
1451         veor            @XMM[2], @XMM[14]
1452         vst1.8          {@XMM[2]}, [$out]!
1453
1454 .Lctr_enc_done:
1455         vmov.i32        q0, #0
1456         vmov.i32        q1, #0
1457 #ifndef BSAES_ASM_EXTENDED_KEY
1458 .Lctr_enc_bzero:                        @ wipe key schedule [if any]
1459         vstmia          $keysched!, {q0-q1}
1460         cmp             $keysched, $fp
1461         bne             .Lctr_enc_bzero
1462 #else
1463         vstmia          $keysched, {q0-q1}
1464 #endif
1465
1466         mov     sp, $fp
1467         add     sp, #0x10               @ add sp,$fp,#0x10 is no good for thumb
1468         VFP_ABI_POP
1469         ldmia   sp!, {r4-r10, pc}       @ return
1470
1471 .align  4
1472 .Lctr_enc_short:
1473         ldr     ip, [sp]                @ ctr pointer is passed on stack
1474         stmdb   sp!, {r4-r8, lr}
1475
1476         mov     r4, $inp                @ copy arguments
1477         mov     r5, $out
1478         mov     r6, $len
1479         mov     r7, $key
1480         ldr     r8, [ip, #12]           @ load counter LSW
1481         vld1.8  {@XMM[1]}, [ip]         @ load whole counter value
1482 #ifdef __ARMEL__
1483         rev     r8, r8
1484 #endif
1485         sub     sp, sp, #0x10
1486         vst1.8  {@XMM[1]}, [sp,:64]     @ copy counter value
1487         sub     sp, sp, #0x10
1488
1489 .Lctr_enc_short_loop:
1490         add     r0, sp, #0x10           @ input counter value
1491         mov     r1, sp                  @ output on the stack
1492         mov     r2, r7                  @ key
1493
1494         bl      AES_encrypt
1495
1496         vld1.8  {@XMM[0]}, [r4]!        @ load input
1497         vld1.8  {@XMM[1]}, [sp,:64]     @ load encrypted counter
1498         add     r8, r8, #1
1499 #ifdef __ARMEL__
1500         rev     r0, r8
1501         str     r0, [sp, #0x1c]         @ next counter value
1502 #else
1503         str     r8, [sp, #0x1c]         @ next counter value
1504 #endif
1505         veor    @XMM[0],@XMM[0],@XMM[1]
1506         vst1.8  {@XMM[0]}, [r5]!        @ store output
1507         subs    r6, r6, #1
1508         bne     .Lctr_enc_short_loop
1509
1510         vmov.i32        q0, #0
1511         vmov.i32        q1, #0
1512         vstmia          sp!, {q0-q1}
1513
1514         ldmia   sp!, {r4-r8, pc}
1515 .size   bsaes_ctr32_encrypt_blocks,.-bsaes_ctr32_encrypt_blocks
1516 ___
1517 }
1518 {
1519 ######################################################################
1520 # void bsaes_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1521 #       const AES_KEY *key1, const AES_KEY *key2,
1522 #       const unsigned char iv[16]);
1523 #
1524 my ($inp,$out,$len,$key,$rounds,$magic,$fp)=(map("r$_",(7..10,1..3)));
1525 my $const="r6";         # returned by _bsaes_key_convert
1526 my $twmask=@XMM[5];
1527 my @T=@XMM[6..7];
1528
1529 $code.=<<___;
1530 .globl  bsaes_xts_encrypt
1531 .type   bsaes_xts_encrypt,%function
1532 .align  4
1533 bsaes_xts_encrypt:
1534         mov     ip, sp
1535         stmdb   sp!, {r4-r10, lr}               @ 0x20
1536         VFP_ABI_PUSH
1537         mov     r6, sp                          @ future $fp
1538
1539         mov     $inp, r0
1540         mov     $out, r1
1541         mov     $len, r2
1542         mov     $key, r3
1543
1544         sub     r0, sp, #0x10                   @ 0x10
1545         bic     r0, #0xf                        @ align at 16 bytes
1546         mov     sp, r0
1547
1548 #ifdef  XTS_CHAIN_TWEAK
1549         ldr     r0, [ip]                        @ pointer to input tweak
1550 #else
1551         @ generate initial tweak
1552         ldr     r0, [ip, #4]                    @ iv[]
1553         mov     r1, sp
1554         ldr     r2, [ip, #0]                    @ key2
1555         bl      AES_encrypt
1556         mov     r0,sp                           @ pointer to initial tweak
1557 #endif
1558
1559         ldr     $rounds, [$key, #240]           @ get # of rounds
1560         mov     $fp, r6
1561 #ifndef BSAES_ASM_EXTENDED_KEY
1562         @ allocate the key schedule on the stack
1563         sub     r12, sp, $rounds, lsl#7         @ 128 bytes per inner round key
1564         @ add   r12, #`128-32`                  @ size of bit-sliced key schedule
1565         sub     r12, #`32+16`                   @ place for tweak[9]
1566
1567         @ populate the key schedule
1568         mov     r4, $key                        @ pass key
1569         mov     r5, $rounds                     @ pass # of rounds
1570         mov     sp, r12
1571         add     r12, #0x90                      @ pass key schedule
1572         bl      _bsaes_key_convert
1573         veor    @XMM[7], @XMM[7], @XMM[15]      @ fix up last round key
1574         vstmia  r12, {@XMM[7]}                  @ save last round key
1575 #else
1576         ldr     r12, [$key, #244]
1577         eors    r12, #1
1578         beq     0f
1579
1580         str     r12, [$key, #244]
1581         mov     r4, $key                        @ pass key
1582         mov     r5, $rounds                     @ pass # of rounds
1583         add     r12, $key, #248                 @ pass key schedule
1584         bl      _bsaes_key_convert
1585         veor    @XMM[7], @XMM[7], @XMM[15]      @ fix up last round key
1586         vstmia  r12, {@XMM[7]}
1587
1588 .align  2
1589 0:      sub     sp, #0x90                       @ place for tweak[9]
1590 #endif
1591
1592         vld1.8  {@XMM[8]}, [r0]                 @ initial tweak
1593         adr     $magic, .Lxts_magic
1594
1595         subs    $len, #0x80
1596         blo     .Lxts_enc_short
1597         b       .Lxts_enc_loop
1598
1599 .align  4
1600 .Lxts_enc_loop:
1601         vldmia          $magic, {$twmask}       @ load XTS magic
1602         vshr.s64        @T[0], @XMM[8], #63
1603         mov             r0, sp
1604         vand            @T[0], @T[0], $twmask
1605 ___
1606 for($i=9;$i<16;$i++) {
1607 $code.=<<___;
1608         vadd.u64        @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1609         vst1.64         {@XMM[$i-1]}, [r0,:128]!
1610         vswp            `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1611         vshr.s64        @T[1], @XMM[$i], #63
1612         veor            @XMM[$i], @XMM[$i], @T[0]
1613         vand            @T[1], @T[1], $twmask
1614 ___
1615         @T=reverse(@T);
1616
1617 $code.=<<___ if ($i>=10);
1618         vld1.8          {@XMM[$i-10]}, [$inp]!
1619 ___
1620 $code.=<<___ if ($i>=11);
1621         veor            @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1622 ___
1623 }
1624 $code.=<<___;
1625         vadd.u64        @XMM[8], @XMM[15], @XMM[15]
1626         vst1.64         {@XMM[15]}, [r0,:128]!
1627         vswp            `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1628         veor            @XMM[8], @XMM[8], @T[0]
1629         vst1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1630
1631         vld1.8          {@XMM[6]-@XMM[7]}, [$inp]!
1632         veor            @XMM[5], @XMM[5], @XMM[13]
1633 #ifndef BSAES_ASM_EXTENDED_KEY
1634         add             r4, sp, #0x90                   @ pass key schedule
1635 #else
1636         add             r4, $key, #248                  @ pass key schedule
1637 #endif
1638         veor            @XMM[6], @XMM[6], @XMM[14]
1639         mov             r5, $rounds                     @ pass rounds
1640         veor            @XMM[7], @XMM[7], @XMM[15]
1641         mov             r0, sp
1642
1643         bl              _bsaes_encrypt8
1644
1645         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1646         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
1647         veor            @XMM[0], @XMM[0], @XMM[ 8]
1648         vld1.64         {@XMM[12]-@XMM[13]}, [r0,:128]!
1649         veor            @XMM[1], @XMM[1], @XMM[ 9]
1650         veor            @XMM[8], @XMM[4], @XMM[10]
1651         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1652         veor            @XMM[9], @XMM[6], @XMM[11]
1653         vld1.64         {@XMM[14]-@XMM[15]}, [r0,:128]!
1654         veor            @XMM[10], @XMM[3], @XMM[12]
1655         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
1656         veor            @XMM[11], @XMM[7], @XMM[13]
1657         veor            @XMM[12], @XMM[2], @XMM[14]
1658         vst1.8          {@XMM[10]-@XMM[11]}, [$out]!
1659         veor            @XMM[13], @XMM[5], @XMM[15]
1660         vst1.8          {@XMM[12]-@XMM[13]}, [$out]!
1661
1662         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1663
1664         subs            $len, #0x80
1665         bpl             .Lxts_enc_loop
1666
1667 .Lxts_enc_short:
1668         adds            $len, #0x70
1669         bmi             .Lxts_enc_done
1670
1671         vldmia          $magic, {$twmask}       @ load XTS magic
1672         vshr.s64        @T[0], @XMM[8], #63
1673         mov             r0, sp
1674         vand            @T[0], @T[0], $twmask
1675 ___
1676 for($i=9;$i<16;$i++) {
1677 $code.=<<___;
1678         vadd.u64        @XMM[$i], @XMM[$i-1], @XMM[$i-1]
1679         vst1.64         {@XMM[$i-1]}, [r0,:128]!
1680         vswp            `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1681         vshr.s64        @T[1], @XMM[$i], #63
1682         veor            @XMM[$i], @XMM[$i], @T[0]
1683         vand            @T[1], @T[1], $twmask
1684 ___
1685         @T=reverse(@T);
1686
1687 $code.=<<___ if ($i>=10);
1688         vld1.8          {@XMM[$i-10]}, [$inp]!
1689         subs            $len, #0x10
1690         bmi             .Lxts_enc_`$i-9`
1691 ___
1692 $code.=<<___ if ($i>=11);
1693         veor            @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1694 ___
1695 }
1696 $code.=<<___;
1697         sub             $len, #0x10
1698         vst1.64         {@XMM[15]}, [r0,:128]           @ next round tweak
1699
1700         vld1.8          {@XMM[6]}, [$inp]!
1701         veor            @XMM[5], @XMM[5], @XMM[13]
1702 #ifndef BSAES_ASM_EXTENDED_KEY
1703         add             r4, sp, #0x90                   @ pass key schedule
1704 #else
1705         add             r4, $key, #248                  @ pass key schedule
1706 #endif
1707         veor            @XMM[6], @XMM[6], @XMM[14]
1708         mov             r5, $rounds                     @ pass rounds
1709         mov             r0, sp
1710
1711         bl              _bsaes_encrypt8
1712
1713         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1714         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
1715         veor            @XMM[0], @XMM[0], @XMM[ 8]
1716         vld1.64         {@XMM[12]-@XMM[13]}, [r0,:128]!
1717         veor            @XMM[1], @XMM[1], @XMM[ 9]
1718         veor            @XMM[8], @XMM[4], @XMM[10]
1719         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1720         veor            @XMM[9], @XMM[6], @XMM[11]
1721         vld1.64         {@XMM[14]}, [r0,:128]!
1722         veor            @XMM[10], @XMM[3], @XMM[12]
1723         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
1724         veor            @XMM[11], @XMM[7], @XMM[13]
1725         veor            @XMM[12], @XMM[2], @XMM[14]
1726         vst1.8          {@XMM[10]-@XMM[11]}, [$out]!
1727         vst1.8          {@XMM[12]}, [$out]!
1728
1729         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1730         b               .Lxts_enc_done
1731 .align  4
1732 .Lxts_enc_6:
1733         vst1.64         {@XMM[14]}, [r0,:128]           @ next round tweak
1734
1735         veor            @XMM[4], @XMM[4], @XMM[12]
1736 #ifndef BSAES_ASM_EXTENDED_KEY
1737         add             r4, sp, #0x90                   @ pass key schedule
1738 #else
1739         add             r4, $key, #248                  @ pass key schedule
1740 #endif
1741         veor            @XMM[5], @XMM[5], @XMM[13]
1742         mov             r5, $rounds                     @ pass rounds
1743         mov             r0, sp
1744
1745         bl              _bsaes_encrypt8
1746
1747         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1748         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
1749         veor            @XMM[0], @XMM[0], @XMM[ 8]
1750         vld1.64         {@XMM[12]-@XMM[13]}, [r0,:128]!
1751         veor            @XMM[1], @XMM[1], @XMM[ 9]
1752         veor            @XMM[8], @XMM[4], @XMM[10]
1753         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1754         veor            @XMM[9], @XMM[6], @XMM[11]
1755         veor            @XMM[10], @XMM[3], @XMM[12]
1756         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
1757         veor            @XMM[11], @XMM[7], @XMM[13]
1758         vst1.8          {@XMM[10]-@XMM[11]}, [$out]!
1759
1760         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1761         b               .Lxts_enc_done
1762
1763 @ put this in range for both ARM and Thumb mode adr instructions
1764 .align  5
1765 .Lxts_magic:
1766         .quad   1, 0x87
1767
1768 .align  5
1769 .Lxts_enc_5:
1770         vst1.64         {@XMM[13]}, [r0,:128]           @ next round tweak
1771
1772         veor            @XMM[3], @XMM[3], @XMM[11]
1773 #ifndef BSAES_ASM_EXTENDED_KEY
1774         add             r4, sp, #0x90                   @ pass key schedule
1775 #else
1776         add             r4, $key, #248                  @ pass key schedule
1777 #endif
1778         veor            @XMM[4], @XMM[4], @XMM[12]
1779         mov             r5, $rounds                     @ pass rounds
1780         mov             r0, sp
1781
1782         bl              _bsaes_encrypt8
1783
1784         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1785         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
1786         veor            @XMM[0], @XMM[0], @XMM[ 8]
1787         vld1.64         {@XMM[12]}, [r0,:128]!
1788         veor            @XMM[1], @XMM[1], @XMM[ 9]
1789         veor            @XMM[8], @XMM[4], @XMM[10]
1790         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1791         veor            @XMM[9], @XMM[6], @XMM[11]
1792         veor            @XMM[10], @XMM[3], @XMM[12]
1793         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
1794         vst1.8          {@XMM[10]}, [$out]!
1795
1796         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1797         b               .Lxts_enc_done
1798 .align  4
1799 .Lxts_enc_4:
1800         vst1.64         {@XMM[12]}, [r0,:128]           @ next round tweak
1801
1802         veor            @XMM[2], @XMM[2], @XMM[10]
1803 #ifndef BSAES_ASM_EXTENDED_KEY
1804         add             r4, sp, #0x90                   @ pass key schedule
1805 #else
1806         add             r4, $key, #248                  @ pass key schedule
1807 #endif
1808         veor            @XMM[3], @XMM[3], @XMM[11]
1809         mov             r5, $rounds                     @ pass rounds
1810         mov             r0, sp
1811
1812         bl              _bsaes_encrypt8
1813
1814         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1815         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
1816         veor            @XMM[0], @XMM[0], @XMM[ 8]
1817         veor            @XMM[1], @XMM[1], @XMM[ 9]
1818         veor            @XMM[8], @XMM[4], @XMM[10]
1819         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1820         veor            @XMM[9], @XMM[6], @XMM[11]
1821         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
1822
1823         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1824         b               .Lxts_enc_done
1825 .align  4
1826 .Lxts_enc_3:
1827         vst1.64         {@XMM[11]}, [r0,:128]           @ next round tweak
1828
1829         veor            @XMM[1], @XMM[1], @XMM[9]
1830 #ifndef BSAES_ASM_EXTENDED_KEY
1831         add             r4, sp, #0x90                   @ pass key schedule
1832 #else
1833         add             r4, $key, #248                  @ pass key schedule
1834 #endif
1835         veor            @XMM[2], @XMM[2], @XMM[10]
1836         mov             r5, $rounds                     @ pass rounds
1837         mov             r0, sp
1838
1839         bl              _bsaes_encrypt8
1840
1841         vld1.64         {@XMM[8]-@XMM[9]}, [r0,:128]!
1842         vld1.64         {@XMM[10]}, [r0,:128]!
1843         veor            @XMM[0], @XMM[0], @XMM[ 8]
1844         veor            @XMM[1], @XMM[1], @XMM[ 9]
1845         veor            @XMM[8], @XMM[4], @XMM[10]
1846         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1847         vst1.8          {@XMM[8]}, [$out]!
1848
1849         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1850         b               .Lxts_enc_done
1851 .align  4
1852 .Lxts_enc_2:
1853         vst1.64         {@XMM[10]}, [r0,:128]           @ next round tweak
1854
1855         veor            @XMM[0], @XMM[0], @XMM[8]
1856 #ifndef BSAES_ASM_EXTENDED_KEY
1857         add             r4, sp, #0x90                   @ pass key schedule
1858 #else
1859         add             r4, $key, #248                  @ pass key schedule
1860 #endif
1861         veor            @XMM[1], @XMM[1], @XMM[9]
1862         mov             r5, $rounds                     @ pass rounds
1863         mov             r0, sp
1864
1865         bl              _bsaes_encrypt8
1866
1867         vld1.64         {@XMM[8]-@XMM[9]}, [r0,:128]!
1868         veor            @XMM[0], @XMM[0], @XMM[ 8]
1869         veor            @XMM[1], @XMM[1], @XMM[ 9]
1870         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
1871
1872         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
1873         b               .Lxts_enc_done
1874 .align  4
1875 .Lxts_enc_1:
1876         mov             r0, sp
1877         veor            @XMM[0], @XMM[8]
1878         mov             r1, sp
1879         vst1.8          {@XMM[0]}, [sp,:128]
1880         mov             r2, $key
1881         mov             r4, $fp                         @ preserve fp
1882
1883         bl              AES_encrypt
1884
1885         vld1.8          {@XMM[0]}, [sp,:128]
1886         veor            @XMM[0], @XMM[0], @XMM[8]
1887         vst1.8          {@XMM[0]}, [$out]!
1888         mov             $fp, r4
1889
1890         vmov            @XMM[8], @XMM[9]                @ next round tweak
1891
1892 .Lxts_enc_done:
1893 #ifndef XTS_CHAIN_TWEAK
1894         adds            $len, #0x10
1895         beq             .Lxts_enc_ret
1896         sub             r6, $out, #0x10
1897
1898 .Lxts_enc_steal:
1899         ldrb            r0, [$inp], #1
1900         ldrb            r1, [$out, #-0x10]
1901         strb            r0, [$out, #-0x10]
1902         strb            r1, [$out], #1
1903
1904         subs            $len, #1
1905         bhi             .Lxts_enc_steal
1906
1907         vld1.8          {@XMM[0]}, [r6]
1908         mov             r0, sp
1909         veor            @XMM[0], @XMM[0], @XMM[8]
1910         mov             r1, sp
1911         vst1.8          {@XMM[0]}, [sp,:128]
1912         mov             r2, $key
1913         mov             r4, $fp                 @ preserve fp
1914
1915         bl              AES_encrypt
1916
1917         vld1.8          {@XMM[0]}, [sp,:128]
1918         veor            @XMM[0], @XMM[0], @XMM[8]
1919         vst1.8          {@XMM[0]}, [r6]
1920         mov             $fp, r4
1921 #endif
1922
1923 .Lxts_enc_ret:
1924         bic             r0, $fp, #0xf
1925         vmov.i32        q0, #0
1926         vmov.i32        q1, #0
1927 #ifdef  XTS_CHAIN_TWEAK
1928         ldr             r1, [$fp, #0x20+VFP_ABI_FRAME]  @ chain tweak
1929 #endif
1930 .Lxts_enc_bzero:                                @ wipe key schedule [if any]
1931         vstmia          sp!, {q0-q1}
1932         cmp             sp, r0
1933         bne             .Lxts_enc_bzero
1934
1935         mov             sp, $fp
1936 #ifdef  XTS_CHAIN_TWEAK
1937         vst1.8          {@XMM[8]}, [r1]
1938 #endif
1939         VFP_ABI_POP
1940         ldmia           sp!, {r4-r10, pc}       @ return
1941
1942 .size   bsaes_xts_encrypt,.-bsaes_xts_encrypt
1943
1944 .globl  bsaes_xts_decrypt
1945 .type   bsaes_xts_decrypt,%function
1946 .align  4
1947 bsaes_xts_decrypt:
1948         mov     ip, sp
1949         stmdb   sp!, {r4-r10, lr}               @ 0x20
1950         VFP_ABI_PUSH
1951         mov     r6, sp                          @ future $fp
1952
1953         mov     $inp, r0
1954         mov     $out, r1
1955         mov     $len, r2
1956         mov     $key, r3
1957
1958         sub     r0, sp, #0x10                   @ 0x10
1959         bic     r0, #0xf                        @ align at 16 bytes
1960         mov     sp, r0
1961
1962 #ifdef  XTS_CHAIN_TWEAK
1963         ldr     r0, [ip]                        @ pointer to input tweak
1964 #else
1965         @ generate initial tweak
1966         ldr     r0, [ip, #4]                    @ iv[]
1967         mov     r1, sp
1968         ldr     r2, [ip, #0]                    @ key2
1969         bl      AES_encrypt
1970         mov     r0, sp                          @ pointer to initial tweak
1971 #endif
1972
1973         ldr     $rounds, [$key, #240]           @ get # of rounds
1974         mov     $fp, r6
1975 #ifndef BSAES_ASM_EXTENDED_KEY
1976         @ allocate the key schedule on the stack
1977         sub     r12, sp, $rounds, lsl#7         @ 128 bytes per inner round key
1978         @ add   r12, #`128-32`                  @ size of bit-sliced key schedule
1979         sub     r12, #`32+16`                   @ place for tweak[9]
1980
1981         @ populate the key schedule
1982         mov     r4, $key                        @ pass key
1983         mov     r5, $rounds                     @ pass # of rounds
1984         mov     sp, r12
1985         add     r12, #0x90                      @ pass key schedule
1986         bl      _bsaes_key_convert
1987         add     r4, sp, #0x90
1988         vldmia  r4, {@XMM[6]}
1989         vstmia  r12,  {@XMM[15]}                @ save last round key
1990         veor    @XMM[7], @XMM[7], @XMM[6]       @ fix up round 0 key
1991         vstmia  r4, {@XMM[7]}
1992 #else
1993         ldr     r12, [$key, #244]
1994         eors    r12, #1
1995         beq     0f
1996
1997         str     r12, [$key, #244]
1998         mov     r4, $key                        @ pass key
1999         mov     r5, $rounds                     @ pass # of rounds
2000         add     r12, $key, #248                 @ pass key schedule
2001         bl      _bsaes_key_convert
2002         add     r4, $key, #248
2003         vldmia  r4, {@XMM[6]}
2004         vstmia  r12,  {@XMM[15]}                @ save last round key
2005         veor    @XMM[7], @XMM[7], @XMM[6]       @ fix up round 0 key
2006         vstmia  r4, {@XMM[7]}
2007
2008 .align  2
2009 0:      sub     sp, #0x90                       @ place for tweak[9]
2010 #endif
2011         vld1.8  {@XMM[8]}, [r0]                 @ initial tweak
2012         adr     $magic, .Lxts_magic
2013
2014         tst     $len, #0xf                      @ if not multiple of 16
2015         it      ne                              @ Thumb2 thing, sanity check in ARM
2016         subne   $len, #0x10                     @ subtract another 16 bytes
2017         subs    $len, #0x80
2018
2019         blo     .Lxts_dec_short
2020         b       .Lxts_dec_loop
2021
2022 .align  4
2023 .Lxts_dec_loop:
2024         vldmia          $magic, {$twmask}       @ load XTS magic
2025         vshr.s64        @T[0], @XMM[8], #63
2026         mov             r0, sp
2027         vand            @T[0], @T[0], $twmask
2028 ___
2029 for($i=9;$i<16;$i++) {
2030 $code.=<<___;
2031         vadd.u64        @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2032         vst1.64         {@XMM[$i-1]}, [r0,:128]!
2033         vswp            `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2034         vshr.s64        @T[1], @XMM[$i], #63
2035         veor            @XMM[$i], @XMM[$i], @T[0]
2036         vand            @T[1], @T[1], $twmask
2037 ___
2038         @T=reverse(@T);
2039
2040 $code.=<<___ if ($i>=10);
2041         vld1.8          {@XMM[$i-10]}, [$inp]!
2042 ___
2043 $code.=<<___ if ($i>=11);
2044         veor            @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2045 ___
2046 }
2047 $code.=<<___;
2048         vadd.u64        @XMM[8], @XMM[15], @XMM[15]
2049         vst1.64         {@XMM[15]}, [r0,:128]!
2050         vswp            `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2051         veor            @XMM[8], @XMM[8], @T[0]
2052         vst1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2053
2054         vld1.8          {@XMM[6]-@XMM[7]}, [$inp]!
2055         veor            @XMM[5], @XMM[5], @XMM[13]
2056 #ifndef BSAES_ASM_EXTENDED_KEY
2057         add             r4, sp, #0x90                   @ pass key schedule
2058 #else
2059         add             r4, $key, #248                  @ pass key schedule
2060 #endif
2061         veor            @XMM[6], @XMM[6], @XMM[14]
2062         mov             r5, $rounds                     @ pass rounds
2063         veor            @XMM[7], @XMM[7], @XMM[15]
2064         mov             r0, sp
2065
2066         bl              _bsaes_decrypt8
2067
2068         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2069         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
2070         veor            @XMM[0], @XMM[0], @XMM[ 8]
2071         vld1.64         {@XMM[12]-@XMM[13]}, [r0,:128]!
2072         veor            @XMM[1], @XMM[1], @XMM[ 9]
2073         veor            @XMM[8], @XMM[6], @XMM[10]
2074         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2075         veor            @XMM[9], @XMM[4], @XMM[11]
2076         vld1.64         {@XMM[14]-@XMM[15]}, [r0,:128]!
2077         veor            @XMM[10], @XMM[2], @XMM[12]
2078         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
2079         veor            @XMM[11], @XMM[7], @XMM[13]
2080         veor            @XMM[12], @XMM[3], @XMM[14]
2081         vst1.8          {@XMM[10]-@XMM[11]}, [$out]!
2082         veor            @XMM[13], @XMM[5], @XMM[15]
2083         vst1.8          {@XMM[12]-@XMM[13]}, [$out]!
2084
2085         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2086
2087         subs            $len, #0x80
2088         bpl             .Lxts_dec_loop
2089
2090 .Lxts_dec_short:
2091         adds            $len, #0x70
2092         bmi             .Lxts_dec_done
2093
2094         vldmia          $magic, {$twmask}       @ load XTS magic
2095         vshr.s64        @T[0], @XMM[8], #63
2096         mov             r0, sp
2097         vand            @T[0], @T[0], $twmask
2098 ___
2099 for($i=9;$i<16;$i++) {
2100 $code.=<<___;
2101         vadd.u64        @XMM[$i], @XMM[$i-1], @XMM[$i-1]
2102         vst1.64         {@XMM[$i-1]}, [r0,:128]!
2103         vswp            `&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2104         vshr.s64        @T[1], @XMM[$i], #63
2105         veor            @XMM[$i], @XMM[$i], @T[0]
2106         vand            @T[1], @T[1], $twmask
2107 ___
2108         @T=reverse(@T);
2109
2110 $code.=<<___ if ($i>=10);
2111         vld1.8          {@XMM[$i-10]}, [$inp]!
2112         subs            $len, #0x10
2113         bmi             .Lxts_dec_`$i-9`
2114 ___
2115 $code.=<<___ if ($i>=11);
2116         veor            @XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2117 ___
2118 }
2119 $code.=<<___;
2120         sub             $len, #0x10
2121         vst1.64         {@XMM[15]}, [r0,:128]           @ next round tweak
2122
2123         vld1.8          {@XMM[6]}, [$inp]!
2124         veor            @XMM[5], @XMM[5], @XMM[13]
2125 #ifndef BSAES_ASM_EXTENDED_KEY
2126         add             r4, sp, #0x90                   @ pass key schedule
2127 #else
2128         add             r4, $key, #248                  @ pass key schedule
2129 #endif
2130         veor            @XMM[6], @XMM[6], @XMM[14]
2131         mov             r5, $rounds                     @ pass rounds
2132         mov             r0, sp
2133
2134         bl              _bsaes_decrypt8
2135
2136         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2137         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
2138         veor            @XMM[0], @XMM[0], @XMM[ 8]
2139         vld1.64         {@XMM[12]-@XMM[13]}, [r0,:128]!
2140         veor            @XMM[1], @XMM[1], @XMM[ 9]
2141         veor            @XMM[8], @XMM[6], @XMM[10]
2142         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2143         veor            @XMM[9], @XMM[4], @XMM[11]
2144         vld1.64         {@XMM[14]}, [r0,:128]!
2145         veor            @XMM[10], @XMM[2], @XMM[12]
2146         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
2147         veor            @XMM[11], @XMM[7], @XMM[13]
2148         veor            @XMM[12], @XMM[3], @XMM[14]
2149         vst1.8          {@XMM[10]-@XMM[11]}, [$out]!
2150         vst1.8          {@XMM[12]}, [$out]!
2151
2152         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2153         b               .Lxts_dec_done
2154 .align  4
2155 .Lxts_dec_6:
2156         vst1.64         {@XMM[14]}, [r0,:128]           @ next round tweak
2157
2158         veor            @XMM[4], @XMM[4], @XMM[12]
2159 #ifndef BSAES_ASM_EXTENDED_KEY
2160         add             r4, sp, #0x90                   @ pass key schedule
2161 #else
2162         add             r4, $key, #248                  @ pass key schedule
2163 #endif
2164         veor            @XMM[5], @XMM[5], @XMM[13]
2165         mov             r5, $rounds                     @ pass rounds
2166         mov             r0, sp
2167
2168         bl              _bsaes_decrypt8
2169
2170         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2171         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
2172         veor            @XMM[0], @XMM[0], @XMM[ 8]
2173         vld1.64         {@XMM[12]-@XMM[13]}, [r0,:128]!
2174         veor            @XMM[1], @XMM[1], @XMM[ 9]
2175         veor            @XMM[8], @XMM[6], @XMM[10]
2176         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2177         veor            @XMM[9], @XMM[4], @XMM[11]
2178         veor            @XMM[10], @XMM[2], @XMM[12]
2179         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
2180         veor            @XMM[11], @XMM[7], @XMM[13]
2181         vst1.8          {@XMM[10]-@XMM[11]}, [$out]!
2182
2183         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2184         b               .Lxts_dec_done
2185 .align  4
2186 .Lxts_dec_5:
2187         vst1.64         {@XMM[13]}, [r0,:128]           @ next round tweak
2188
2189         veor            @XMM[3], @XMM[3], @XMM[11]
2190 #ifndef BSAES_ASM_EXTENDED_KEY
2191         add             r4, sp, #0x90                   @ pass key schedule
2192 #else
2193         add             r4, $key, #248                  @ pass key schedule
2194 #endif
2195         veor            @XMM[4], @XMM[4], @XMM[12]
2196         mov             r5, $rounds                     @ pass rounds
2197         mov             r0, sp
2198
2199         bl              _bsaes_decrypt8
2200
2201         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2202         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
2203         veor            @XMM[0], @XMM[0], @XMM[ 8]
2204         vld1.64         {@XMM[12]}, [r0,:128]!
2205         veor            @XMM[1], @XMM[1], @XMM[ 9]
2206         veor            @XMM[8], @XMM[6], @XMM[10]
2207         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2208         veor            @XMM[9], @XMM[4], @XMM[11]
2209         veor            @XMM[10], @XMM[2], @XMM[12]
2210         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
2211         vst1.8          {@XMM[10]}, [$out]!
2212
2213         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2214         b               .Lxts_dec_done
2215 .align  4
2216 .Lxts_dec_4:
2217         vst1.64         {@XMM[12]}, [r0,:128]           @ next round tweak
2218
2219         veor            @XMM[2], @XMM[2], @XMM[10]
2220 #ifndef BSAES_ASM_EXTENDED_KEY
2221         add             r4, sp, #0x90                   @ pass key schedule
2222 #else
2223         add             r4, $key, #248                  @ pass key schedule
2224 #endif
2225         veor            @XMM[3], @XMM[3], @XMM[11]
2226         mov             r5, $rounds                     @ pass rounds
2227         mov             r0, sp
2228
2229         bl              _bsaes_decrypt8
2230
2231         vld1.64         {@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2232         vld1.64         {@XMM[10]-@XMM[11]}, [r0,:128]!
2233         veor            @XMM[0], @XMM[0], @XMM[ 8]
2234         veor            @XMM[1], @XMM[1], @XMM[ 9]
2235         veor            @XMM[8], @XMM[6], @XMM[10]
2236         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2237         veor            @XMM[9], @XMM[4], @XMM[11]
2238         vst1.8          {@XMM[8]-@XMM[9]}, [$out]!
2239
2240         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2241         b               .Lxts_dec_done
2242 .align  4
2243 .Lxts_dec_3:
2244         vst1.64         {@XMM[11]}, [r0,:128]           @ next round tweak
2245
2246         veor            @XMM[1], @XMM[1], @XMM[9]
2247 #ifndef BSAES_ASM_EXTENDED_KEY
2248         add             r4, sp, #0x90                   @ pass key schedule
2249 #else
2250         add             r4, $key, #248                  @ pass key schedule
2251 #endif
2252         veor            @XMM[2], @XMM[2], @XMM[10]
2253         mov             r5, $rounds                     @ pass rounds
2254         mov             r0, sp
2255
2256         bl              _bsaes_decrypt8
2257
2258         vld1.64         {@XMM[8]-@XMM[9]}, [r0,:128]!
2259         vld1.64         {@XMM[10]}, [r0,:128]!
2260         veor            @XMM[0], @XMM[0], @XMM[ 8]
2261         veor            @XMM[1], @XMM[1], @XMM[ 9]
2262         veor            @XMM[8], @XMM[6], @XMM[10]
2263         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2264         vst1.8          {@XMM[8]}, [$out]!
2265
2266         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2267         b               .Lxts_dec_done
2268 .align  4
2269 .Lxts_dec_2:
2270         vst1.64         {@XMM[10]}, [r0,:128]           @ next round tweak
2271
2272         veor            @XMM[0], @XMM[0], @XMM[8]
2273 #ifndef BSAES_ASM_EXTENDED_KEY
2274         add             r4, sp, #0x90                   @ pass key schedule
2275 #else
2276         add             r4, $key, #248                  @ pass key schedule
2277 #endif
2278         veor            @XMM[1], @XMM[1], @XMM[9]
2279         mov             r5, $rounds                     @ pass rounds
2280         mov             r0, sp
2281
2282         bl              _bsaes_decrypt8
2283
2284         vld1.64         {@XMM[8]-@XMM[9]}, [r0,:128]!
2285         veor            @XMM[0], @XMM[0], @XMM[ 8]
2286         veor            @XMM[1], @XMM[1], @XMM[ 9]
2287         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!
2288
2289         vld1.64         {@XMM[8]}, [r0,:128]            @ next round tweak
2290         b               .Lxts_dec_done
2291 .align  4
2292 .Lxts_dec_1:
2293         mov             r0, sp
2294         veor            @XMM[0], @XMM[8]
2295         mov             r1, sp
2296         vst1.8          {@XMM[0]}, [sp,:128]
2297         mov             r2, $key
2298         mov             r4, $fp                         @ preserve fp
2299         mov             r5, $magic                      @ preserve magic
2300
2301         bl              AES_decrypt
2302
2303         vld1.8          {@XMM[0]}, [sp,:128]
2304         veor            @XMM[0], @XMM[0], @XMM[8]
2305         vst1.8          {@XMM[0]}, [$out]!
2306         mov             $fp, r4
2307         mov             $magic, r5
2308
2309         vmov            @XMM[8], @XMM[9]                @ next round tweak
2310
2311 .Lxts_dec_done:
2312 #ifndef XTS_CHAIN_TWEAK
2313         adds            $len, #0x10
2314         beq             .Lxts_dec_ret
2315
2316         @ calculate one round of extra tweak for the stolen ciphertext
2317         vldmia          $magic, {$twmask}
2318         vshr.s64        @XMM[6], @XMM[8], #63
2319         vand            @XMM[6], @XMM[6], $twmask
2320         vadd.u64        @XMM[9], @XMM[8], @XMM[8]
2321         vswp            `&Dhi("@XMM[6]")`,`&Dlo("@XMM[6]")`
2322         veor            @XMM[9], @XMM[9], @XMM[6]
2323
2324         @ perform the final decryption with the last tweak value
2325         vld1.8          {@XMM[0]}, [$inp]!
2326         mov             r0, sp
2327         veor            @XMM[0], @XMM[0], @XMM[9]
2328         mov             r1, sp
2329         vst1.8          {@XMM[0]}, [sp,:128]
2330         mov             r2, $key
2331         mov             r4, $fp                 @ preserve fp
2332
2333         bl              AES_decrypt
2334
2335         vld1.8          {@XMM[0]}, [sp,:128]
2336         veor            @XMM[0], @XMM[0], @XMM[9]
2337         vst1.8          {@XMM[0]}, [$out]
2338
2339         mov             r6, $out
2340 .Lxts_dec_steal:
2341         ldrb            r1, [$out]
2342         ldrb            r0, [$inp], #1
2343         strb            r1, [$out, #0x10]
2344         strb            r0, [$out], #1
2345
2346         subs            $len, #1
2347         bhi             .Lxts_dec_steal
2348
2349         vld1.8          {@XMM[0]}, [r6]
2350         mov             r0, sp
2351         veor            @XMM[0], @XMM[8]
2352         mov             r1, sp
2353         vst1.8          {@XMM[0]}, [sp,:128]
2354         mov             r2, $key
2355
2356         bl              AES_decrypt
2357
2358         vld1.8          {@XMM[0]}, [sp,:128]
2359         veor            @XMM[0], @XMM[0], @XMM[8]
2360         vst1.8          {@XMM[0]}, [r6]
2361         mov             $fp, r4
2362 #endif
2363
2364 .Lxts_dec_ret:
2365         bic             r0, $fp, #0xf
2366         vmov.i32        q0, #0
2367         vmov.i32        q1, #0
2368 #ifdef  XTS_CHAIN_TWEAK
2369         ldr             r1, [$fp, #0x20+VFP_ABI_FRAME]  @ chain tweak
2370 #endif
2371 .Lxts_dec_bzero:                                @ wipe key schedule [if any]
2372         vstmia          sp!, {q0-q1}
2373         cmp             sp, r0
2374         bne             .Lxts_dec_bzero
2375
2376         mov             sp, $fp
2377 #ifdef  XTS_CHAIN_TWEAK
2378         vst1.8          {@XMM[8]}, [r1]
2379 #endif
2380         VFP_ABI_POP
2381         ldmia           sp!, {r4-r10, pc}       @ return
2382
2383 .size   bsaes_xts_decrypt,.-bsaes_xts_decrypt
2384 ___
2385 }
2386 $code.=<<___;
2387 #endif
2388 ___
2389
2390 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
2391
2392 open SELF,$0;
2393 while(<SELF>) {
2394         next if (/^#!/);
2395         last if (!s/^#/@/ and !/^$/);
2396         print;
2397 }
2398 close SELF;
2399
2400 print $code;
2401
2402 close STDOUT;