bsaes-armv7.pl: add bsaes_cbc_encrypt and bsaes_ctr32_encrypt_blocks.
[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
10 # Bit-sliced AES for ARM NEON
11 #
12 # February 2012.
13 #
14 # This implementation is direct adaptation of bsaes-x86_64 module for
15 # ARM NEON. Except that this module is endian-neutral [in sense that
16 # it can be compiled for either endianness] by courtesy of vld1.8's
17 # neutrality. Initial version doesn't implement interface to OpenSSL,
18 # only low-level primitives and unsupported entry points, just enough
19 # to collect performance results, which for Cortex-A8 core are:
20 #
21 # encrypt       19.5 cycles per byte processed with 128-bit key
22 # decrypt       24.0 cycles per byte processed with 128-bit key
23 # key conv.     440  cycles per 128-bit key/0.18 of 8x block
24 #
25 # Snapdragon S4 encrypts byte in 17.6 cycles and decrypts in 22.6,
26 # which is [much] worse than anticipated (for further details see
27 # http://www.openssl.org/~appro/Snapdragon-S4.html).
28 #
29 # Cortex-A15 manages in 14.2/19.6 cycles [when integer-only code
30 # manages in 20.0 cycles].
31 #
32 # When comparing to x86_64 results keep in mind that NEON unit is
33 # [mostly] single-issue and thus can't [fully] benefit from
34 # instruction-level parallelism. And when comparing to aes-armv4
35 # results keep in mind key schedule conversion overhead (see
36 # bsaes-x86_64.pl for further details)...
37 #
38 #                                               <appro@openssl.org>
39
40 while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
41 open STDOUT,">$output";
42
43 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
44 my @XMM=map("q$_",(0..15));
45
46 {
47 my ($key,$rounds,$const)=("r4","r5","r6");
48
49 sub Dlo()   { shift=~m|q([1]?[0-9])|?"d".($1*2):"";     }
50 sub Dhi()   { shift=~m|q([1]?[0-9])|?"d".($1*2+1):"";   }
51
52 sub Sbox {
53 # input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
54 # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
55 my @b=@_[0..7];
56 my @t=@_[8..11];
57 my @s=@_[12..15];
58         &InBasisChange  (@b);
59         &Inv_GF256      (@b[6,5,0,3,7,1,4,2],@t,@s);
60         &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
61 }
62
63 sub InBasisChange {
64 # input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
65 # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb 
66 my @b=@_[0..7];
67 $code.=<<___;
68         veor    @b[2], @b[2], @b[1]
69         veor    @b[5], @b[5], @b[6]
70         veor    @b[3], @b[3], @b[0]
71         veor    @b[6], @b[6], @b[2]
72         veor    @b[5], @b[5], @b[0]
73
74         veor    @b[6], @b[6], @b[3]
75         veor    @b[3], @b[3], @b[7]
76         veor    @b[7], @b[7], @b[5]
77         veor    @b[3], @b[3], @b[4]
78         veor    @b[4], @b[4], @b[5]
79
80         veor    @b[2], @b[2], @b[7]
81         veor    @b[3], @b[3], @b[1]
82         veor    @b[1], @b[1], @b[5]
83 ___
84 }
85
86 sub OutBasisChange {
87 # input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
88 # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
89 my @b=@_[0..7];
90 $code.=<<___;
91         veor    @b[0], @b[0], @b[6]
92         veor    @b[1], @b[1], @b[4]
93         veor    @b[4], @b[4], @b[6]
94         veor    @b[2], @b[2], @b[0]
95         veor    @b[6], @b[6], @b[1]
96
97         veor    @b[1], @b[1], @b[5]
98         veor    @b[5], @b[5], @b[3]
99         veor    @b[3], @b[3], @b[7]
100         veor    @b[7], @b[7], @b[5]
101         veor    @b[2], @b[2], @b[5]
102
103         veor    @b[4], @b[4], @b[7]
104 ___
105 }
106
107 sub InvSbox {
108 # input in lsb  > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
109 # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
110 my @b=@_[0..7];
111 my @t=@_[8..11];
112 my @s=@_[12..15];
113         &InvInBasisChange       (@b);
114         &Inv_GF256              (@b[5,1,2,6,3,7,0,4],@t,@s);
115         &InvOutBasisChange      (@b[3,7,0,4,5,1,2,6]);
116 }
117
118 sub InvInBasisChange {          # OutBasisChange in reverse (with twist)
119 my @b=@_[5,1,2,6,3,7,0,4];
120 $code.=<<___
121          veor   @b[1], @b[1], @b[7]
122         veor    @b[4], @b[4], @b[7]
123
124         veor    @b[7], @b[7], @b[5]
125          veor   @b[1], @b[1], @b[3]
126         veor    @b[2], @b[2], @b[5]
127         veor    @b[3], @b[3], @b[7]
128
129         veor    @b[6], @b[6], @b[1]
130         veor    @b[2], @b[2], @b[0]
131          veor   @b[5], @b[5], @b[3]
132         veor    @b[4], @b[4], @b[6]
133         veor    @b[0], @b[0], @b[6]
134         veor    @b[1], @b[1], @b[4]
135 ___
136 }
137
138 sub InvOutBasisChange {         # InBasisChange in reverse
139 my @b=@_[2,5,7,3,6,1,0,4];
140 $code.=<<___;
141         veor    @b[1], @b[1], @b[5]
142         veor    @b[2], @b[2], @b[7]
143
144         veor    @b[3], @b[3], @b[1]
145         veor    @b[4], @b[4], @b[5]
146         veor    @b[7], @b[7], @b[5]
147         veor    @b[3], @b[3], @b[4]
148          veor   @b[5], @b[5], @b[0]
149         veor    @b[3], @b[3], @b[7]
150          veor   @b[6], @b[6], @b[2]
151          veor   @b[2], @b[2], @b[1]
152         veor    @b[6], @b[6], @b[3]
153
154         veor    @b[3], @b[3], @b[0]
155         veor    @b[5], @b[5], @b[6]
156 ___
157 }
158
159 sub Mul_GF4 {
160 #;*************************************************************
161 #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
162 #;*************************************************************
163 my ($x0,$x1,$y0,$y1,$t0,$t1)=@_;
164 $code.=<<___;
165         veor    $t0, $y0, $y1
166         vand    $t0, $t0, $x0
167         veor    $x0, $x0, $x1
168         vand    $t1, $x1, $y0
169         vand    $x0, $x0, $y1
170         veor    $x1, $t1, $t0
171         veor    $x0, $x0, $t1
172 ___
173 }
174
175 sub Mul_GF4_N {                         # not used, see next subroutine
176 # multiply and scale by N
177 my ($x0,$x1,$y0,$y1,$t0)=@_;
178 $code.=<<___;
179         veor    $t0, $y0, $y1
180         vand    $t0, $t0, $x0
181         veor    $x0, $x0, $x1
182         vand    $x1, $x1, $y0
183         vand    $x0, $x0, $y1
184         veor    $x1, $x1, $x0
185         veor    $x0, $x0, $t0
186 ___
187 }
188
189 sub Mul_GF4_N_GF4 {
190 # interleaved Mul_GF4_N and Mul_GF4
191 my ($x0,$x1,$y0,$y1,$t0,
192     $x2,$x3,$y2,$y3,$t1)=@_;
193 $code.=<<___;
194         veor    $t0, $y0, $y1
195          veor   $t1, $y2, $y3
196         vand    $t0, $t0, $x0
197          vand   $t1, $t1, $x2
198         veor    $x0, $x0, $x1
199          veor   $x2, $x2, $x3
200         vand    $x1, $x1, $y0
201          vand   $x3, $x3, $y2
202         vand    $x0, $x0, $y1
203          vand   $x2, $x2, $y3
204         veor    $x1, $x1, $x0
205          veor   $x2, $x2, $x3
206         veor    $x0, $x0, $t0
207          veor   $x3, $x3, $t1
208 ___
209 }
210 sub Mul_GF16_2 {
211 my @x=@_[0..7];
212 my @y=@_[8..11];
213 my @t=@_[12..15];
214 $code.=<<___;
215         veor    @t[0], @x[0], @x[2]
216         veor    @t[1], @x[1], @x[3]
217 ___
218         &Mul_GF4        (@x[0], @x[1], @y[0], @y[1], @t[2..3]);
219 $code.=<<___;
220         veor    @y[0], @y[0], @y[2]
221         veor    @y[1], @y[1], @y[3]
222 ___
223         Mul_GF4_N_GF4   (@t[0], @t[1], @y[0], @y[1], @t[3],
224                          @x[2], @x[3], @y[2], @y[3], @t[2]);
225 $code.=<<___;
226         veor    @x[0], @x[0], @t[0]
227         veor    @x[2], @x[2], @t[0]
228         veor    @x[1], @x[1], @t[1]
229         veor    @x[3], @x[3], @t[1]
230
231         veor    @t[0], @x[4], @x[6]
232         veor    @t[1], @x[5], @x[7]
233 ___
234         &Mul_GF4_N_GF4  (@t[0], @t[1], @y[0], @y[1], @t[3],
235                          @x[6], @x[7], @y[2], @y[3], @t[2]);
236 $code.=<<___;
237         veor    @y[0], @y[0], @y[2]
238         veor    @y[1], @y[1], @y[3]
239 ___
240         &Mul_GF4        (@x[4], @x[5], @y[0], @y[1], @t[2..3]);
241 $code.=<<___;
242         veor    @x[4], @x[4], @t[0]
243         veor    @x[6], @x[6], @t[0]
244         veor    @x[5], @x[5], @t[1]
245         veor    @x[7], @x[7], @t[1]
246 ___
247 }
248 sub Inv_GF256 {
249 #;********************************************************************
250 #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144)       *
251 #;********************************************************************
252 my @x=@_[0..7];
253 my @t=@_[8..11];
254 my @s=@_[12..15];
255 # direct optimizations from hardware
256 $code.=<<___;
257         veor    @t[3], @x[4], @x[6]
258         veor    @t[2], @x[5], @x[7]
259         veor    @t[1], @x[1], @x[3]
260         veor    @s[1], @x[7], @x[6]
261          vmov   @t[0], @t[2]
262         veor    @s[0], @x[0], @x[2]
263
264         vorr    @t[2], @t[2], @t[1]
265         veor    @s[3], @t[3], @t[0]
266         vand    @s[2], @t[3], @s[0]
267         vorr    @t[3], @t[3], @s[0]
268         veor    @s[0], @s[0], @t[1]
269         vand    @t[0], @t[0], @t[1]
270         veor    @t[1], @x[3], @x[2]
271         vand    @s[3], @s[3], @s[0]
272         vand    @s[1], @s[1], @t[1]
273         veor    @t[1], @x[4], @x[5]
274         veor    @s[0], @x[1], @x[0]
275         veor    @t[3], @t[3], @s[1]
276         veor    @t[2], @t[2], @s[1]
277         vand    @s[1], @t[1], @s[0]
278         vorr    @t[1], @t[1], @s[0]
279         veor    @t[3], @t[3], @s[3]
280         veor    @t[0], @t[0], @s[1]
281         veor    @t[2], @t[2], @s[2]
282         veor    @t[1], @t[1], @s[3]
283         veor    @t[0], @t[0], @s[2]
284         vand    @s[0], @x[7], @x[3]
285         veor    @t[1], @t[1], @s[2]
286         vand    @s[1], @x[6], @x[2]
287         vand    @s[2], @x[5], @x[1]
288         vorr    @s[3], @x[4], @x[0]
289         veor    @t[3], @t[3], @s[0]
290         veor    @t[1], @t[1], @s[2]
291         veor    @t[0], @t[0], @s[3]
292         veor    @t[2], @t[2], @s[1]
293
294         @ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
295
296         @ new smaller inversion
297
298         vand    @s[2], @t[3], @t[1]
299         vmov    @s[0], @t[0]
300
301         veor    @s[1], @t[2], @s[2]
302         veor    @s[3], @t[0], @s[2]
303         veor    @s[2], @t[0], @s[2]     @ @s[2]=@s[3]
304
305         vbsl    @s[1], @t[1], @t[0]
306         vbsl    @s[3], @t[3], @t[2]
307         veor    @t[3], @t[3], @t[2]
308
309         vbsl    @s[0], @s[1], @s[2]
310         vbsl    @t[0], @s[2], @s[1]
311
312         vand    @s[2], @s[0], @s[3]
313         veor    @t[1], @t[1], @t[0]
314
315         veor    @s[2], @s[2], @t[3]
316 ___
317 # output in s3, s2, s1, t1
318
319 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
320
321 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
322         &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
323
324 ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
325 }
326
327 # AES linear components
328
329 sub ShiftRows {
330 my @x=@_[0..7];
331 my @t=@_[8..11];
332 my $mask=pop;
333 $code.=<<___;
334         vldmia  $key!, {@t[0]-@t[3]}
335         veor    @t[0], @t[0], @x[0]
336         veor    @t[1], @t[1], @x[1]
337         vtbl.8  `&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
338         vtbl.8  `&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
339         vldmia  $key!, {@t[0]}
340         veor    @t[2], @t[2], @x[2]
341         vtbl.8  `&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
342         vtbl.8  `&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
343         vldmia  $key!, {@t[1]}
344         veor    @t[3], @t[3], @x[3]
345         vtbl.8  `&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
346         vtbl.8  `&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
347         vldmia  $key!, {@t[2]}
348         vtbl.8  `&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
349         vtbl.8  `&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
350         vldmia  $key!, {@t[3]}
351         veor    @t[0], @t[0], @x[4]
352         veor    @t[1], @t[1], @x[5]
353         vtbl.8  `&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
354         vtbl.8  `&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
355         veor    @t[2], @t[2], @x[6]
356         vtbl.8  `&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
357         vtbl.8  `&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
358         veor    @t[3], @t[3], @x[7]
359         vtbl.8  `&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
360         vtbl.8  `&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
361         vtbl.8  `&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
362         vtbl.8  `&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
363 ___
364 }
365
366 sub MixColumns {
367 # modified to emit output in order suitable for feeding back to aesenc[last]
368 my @x=@_[0..7];
369 my @t=@_[8..15];
370 $code.=<<___;
371         vext.8  @t[0], @x[0], @x[0], #12        @ x0 <<< 32
372         vext.8  @t[1], @x[1], @x[1], #12
373          veor   @x[0], @x[0], @t[0]             @ x0 ^ (x0 <<< 32)
374         vext.8  @t[2], @x[2], @x[2], #12
375          veor   @x[1], @x[1], @t[1]
376         vext.8  @t[3], @x[3], @x[3], #12
377          veor   @x[2], @x[2], @t[2]
378         vext.8  @t[4], @x[4], @x[4], #12
379          veor   @x[3], @x[3], @t[3]
380         vext.8  @t[5], @x[5], @x[5], #12
381          veor   @x[4], @x[4], @t[4]
382         vext.8  @t[6], @x[6], @x[6], #12
383          veor   @x[5], @x[5], @t[5]
384         vext.8  @t[7], @x[7], @x[7], #12
385          veor   @x[6], @x[6], @t[6]
386
387         veor    @t[1], @t[1], @x[0]
388          veor   @x[7], @x[7], @t[7]
389          vext.8 @x[0], @x[0], @x[0], #8         @ (x0 ^ (x0 <<< 32)) <<< 64)
390         veor    @t[2], @t[2], @x[1]
391         veor    @t[0], @t[0], @x[7]
392         veor    @t[1], @t[1], @x[7]
393          vext.8 @x[1], @x[1], @x[1], #8
394         veor    @t[5], @t[5], @x[4]
395          veor   @x[0], @x[0], @t[0]
396         veor    @t[6], @t[6], @x[5]
397          veor   @x[1], @x[1], @t[1]
398          vext.8 @t[0], @x[4], @x[4], #8
399         veor    @t[4], @t[4], @x[3]
400          vext.8 @t[1], @x[5], @x[5], #8
401         veor    @t[7], @t[7], @x[6]
402          vext.8 @x[4], @x[3], @x[3], #8
403         veor    @t[3], @t[3], @x[2]
404          vext.8 @x[5], @x[7], @x[7], #8
405         veor    @t[4], @t[4], @x[7]
406          vext.8 @x[3], @x[6], @x[6], #8
407         veor    @t[3], @t[3], @x[7]
408          vext.8 @x[6], @x[2], @x[2], #8
409         veor    @x[7], @t[1], @t[5]
410         veor    @x[2], @t[0], @t[4]
411
412         veor    @x[4], @x[4], @t[3]
413         veor    @x[5], @x[5], @t[7]
414         veor    @x[3], @x[3], @t[6]
415          @ vmov @x[2], @t[0]
416         veor    @x[6], @x[6], @t[2]
417          @ vmov @x[7], @t[1]
418 ___
419 }
420
421 sub InvMixColumns {
422 my @x=@_[0..7];
423 my @t=@_[8..15];
424
425 $code.=<<___;
426         @ multiplication by 0x0e
427         vext.8  @t[7], @x[7], @x[7], #12
428         vmov    @t[2], @x[2]
429         veor    @x[2], @x[2], @x[5]             @ 2 5
430         veor    @x[7], @x[7], @x[5]             @ 7 5
431         vext.8  @t[0], @x[0], @x[0], #12
432         vmov    @t[5], @x[5]
433         veor    @x[5], @x[5], @x[0]             @ 5 0           [1]
434         veor    @x[0], @x[0], @x[1]             @ 0 1
435         vext.8  @t[1], @x[1], @x[1], #12
436         veor    @x[1], @x[1], @x[2]             @ 1 25
437         veor    @x[0], @x[0], @x[6]             @ 01 6          [2]
438         vext.8  @t[3], @x[3], @x[3], #12
439         veor    @x[1], @x[1], @x[3]             @ 125 3         [4]
440         veor    @x[2], @x[2], @x[0]             @ 25 016        [3]
441         veor    @x[3], @x[3], @x[7]             @ 3 75
442         veor    @x[7], @x[7], @x[6]             @ 75 6          [0]
443         vext.8  @t[6], @x[6], @x[6], #12
444         vmov    @t[4], @x[4]
445         veor    @x[6], @x[6], @x[4]             @ 6 4
446         veor    @x[4], @x[4], @x[3]             @ 4 375         [6]
447         veor    @x[3], @x[3], @x[7]             @ 375 756=36
448         veor    @x[6], @x[6], @t[5]             @ 64 5          [7]
449         veor    @x[3], @x[3], @t[2]             @ 36 2
450         vext.8  @t[5], @t[5], @t[5], #12
451         veor    @x[3], @x[3], @t[4]             @ 362 4         [5]
452 ___
453                                         my @y = @x[7,5,0,2,1,3,4,6];
454 $code.=<<___;
455         @ multiplication by 0x0b
456         veor    @y[1], @y[1], @y[0]
457         veor    @y[0], @y[0], @t[0]
458         vext.8  @t[2], @t[2], @t[2], #12
459         veor    @y[1], @y[1], @t[1]
460         veor    @y[0], @y[0], @t[5]
461         vext.8  @t[4], @t[4], @t[4], #12
462         veor    @y[1], @y[1], @t[6]
463         veor    @y[0], @y[0], @t[7]
464         veor    @t[7], @t[7], @t[6]             @ clobber t[7]
465
466         veor    @y[3], @y[3], @t[0]
467          veor   @y[1], @y[1], @y[0]
468         vext.8  @t[0], @t[0], @t[0], #12
469         veor    @y[2], @y[2], @t[1]
470         veor    @y[4], @y[4], @t[1]
471         vext.8  @t[1], @t[1], @t[1], #12
472         veor    @y[2], @y[2], @t[2]
473         veor    @y[3], @y[3], @t[2]
474         veor    @y[5], @y[5], @t[2]
475         veor    @y[2], @y[2], @t[7]
476         vext.8  @t[2], @t[2], @t[2], #12
477         veor    @y[3], @y[3], @t[3]
478         veor    @y[6], @y[6], @t[3]
479         veor    @y[4], @y[4], @t[3]
480         veor    @y[7], @y[7], @t[4]
481         vext.8  @t[3], @t[3], @t[3], #12
482         veor    @y[5], @y[5], @t[4]
483         veor    @y[7], @y[7], @t[7]
484         veor    @t[7], @t[7], @t[5]             @ clobber t[7] even more
485         veor    @y[3], @y[3], @t[5]
486         veor    @y[4], @y[4], @t[4]
487
488         veor    @y[5], @y[5], @t[7]
489         vext.8  @t[4], @t[4], @t[4], #12
490         veor    @y[6], @y[6], @t[7]
491         veor    @y[4], @y[4], @t[7]
492
493         veor    @t[7], @t[7], @t[5]
494         vext.8  @t[5], @t[5], @t[5], #12
495
496         @ multiplication by 0x0d
497         veor    @y[4], @y[4], @y[7]
498          veor   @t[7], @t[7], @t[6]             @ restore t[7]
499         veor    @y[7], @y[7], @t[4]
500         vext.8  @t[6], @t[6], @t[6], #12
501         veor    @y[2], @y[2], @t[0]
502         veor    @y[7], @y[7], @t[5]
503         vext.8  @t[7], @t[7], @t[7], #12
504         veor    @y[2], @y[2], @t[2]
505
506         veor    @y[3], @y[3], @y[1]
507         veor    @y[1], @y[1], @t[1]
508         veor    @y[0], @y[0], @t[0]
509         veor    @y[3], @y[3], @t[0]
510         veor    @y[1], @y[1], @t[5]
511         veor    @y[0], @y[0], @t[5]
512         vext.8  @t[0], @t[0], @t[0], #12
513         veor    @y[1], @y[1], @t[7]
514         veor    @y[0], @y[0], @t[6]
515         veor    @y[3], @y[3], @y[1]
516         veor    @y[4], @y[4], @t[1]
517         vext.8  @t[1], @t[1], @t[1], #12
518
519         veor    @y[7], @y[7], @t[7]
520         veor    @y[4], @y[4], @t[2]
521         veor    @y[5], @y[5], @t[2]
522         veor    @y[2], @y[2], @t[6]
523         veor    @t[6], @t[6], @t[3]             @ clobber t[6]
524         vext.8  @t[2], @t[2], @t[2], #12
525         veor    @y[4], @y[4], @y[7]
526         veor    @y[3], @y[3], @t[6]
527
528         veor    @y[6], @y[6], @t[6]
529         veor    @y[5], @y[5], @t[5]
530         vext.8  @t[5], @t[5], @t[5], #12
531         veor    @y[6], @y[6], @t[4]
532         vext.8  @t[4], @t[4], @t[4], #12
533         veor    @y[5], @y[5], @t[6]
534         veor    @y[6], @y[6], @t[7]
535         vext.8  @t[7], @t[7], @t[7], #12
536         veor    @t[6], @t[6], @t[3]             @ restore t[6]
537         vext.8  @t[3], @t[3], @t[3], #12
538
539         @ multiplication by 0x09
540         veor    @y[4], @y[4], @y[1]
541         veor    @t[1], @t[1], @y[1]             @ t[1]=y[1]
542         veor    @t[0], @t[0], @t[5]             @ clobber t[0]
543         vext.8  @t[6], @t[6], @t[6], #12
544         veor    @t[1], @t[1], @t[5]
545         veor    @y[3], @y[3], @t[0]
546         veor    @t[0], @t[0], @y[0]             @ t[0]=y[0]
547         veor    @t[1], @t[1], @t[6]
548         veor    @t[6], @t[6], @t[7]             @ clobber t[6]
549         veor    @y[4], @y[4], @t[1]
550         veor    @y[7], @y[7], @t[4]
551         veor    @y[6], @y[6], @t[3]
552         veor    @y[5], @y[5], @t[2]
553         veor    @t[4], @t[4], @y[4]             @ t[4]=y[4]
554         veor    @t[3], @t[3], @y[3]             @ t[3]=y[3]
555         veor    @t[5], @t[5], @y[5]             @ t[5]=y[5]
556         veor    @t[2], @t[2], @y[2]             @ t[2]=y[2]
557         veor    @t[3], @t[3], @t[7]
558         veor    @XMM[5], @t[5], @t[6]
559         veor    @XMM[6], @t[6], @y[6]           @ t[6]=y[6]
560         veor    @XMM[2], @t[2], @t[6]
561         veor    @XMM[7], @t[7], @y[7]           @ t[7]=y[7]
562
563         vmov    @XMM[0], @t[0]
564         vmov    @XMM[1], @t[1]
565         @ vmov  @XMM[2], @t[2]
566         vmov    @XMM[3], @t[3]
567         vmov    @XMM[4], @t[4]
568         @ vmov  @XMM[5], @t[5]
569         @ vmov  @XMM[6], @t[6]
570         @ vmov  @XMM[7], @t[7]
571 ___
572 }
573
574 sub swapmove {
575 my ($a,$b,$n,$mask,$t)=@_;
576 $code.=<<___;
577         vshr.u64        $t, $b, #$n
578         veor            $t, $t, $a
579         vand            $t, $t, $mask
580         veor            $a, $a, $t
581         vshl.u64        $t, $t, #$n
582         veor            $b, $b, $t
583 ___
584 }
585 sub swapmove2x {
586 my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
587 $code.=<<___;
588         vshr.u64        $t0, $b0, #$n
589          vshr.u64       $t1, $b1, #$n
590         veor            $t0, $t0, $a0
591          veor           $t1, $t1, $a1
592         vand            $t0, $t0, $mask
593          vand           $t1, $t1, $mask
594         veor            $a0, $a0, $t0
595         vshl.u64        $t0, $t0, #$n
596          veor           $a1, $a1, $t1
597          vshl.u64       $t1, $t1, #$n
598         veor            $b0, $b0, $t0
599          veor           $b1, $b1, $t1
600 ___
601 }
602
603 sub bitslice {
604 my @x=reverse(@_[0..7]);
605 my ($t0,$t1,$t2,$t3)=@_[8..11];
606 $code.=<<___;
607         vmov.i8 $t0,#0x55                       @ compose .LBS0
608         vmov.i8 $t1,#0x33                       @ compose .LBS1
609 ___
610         &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
611         &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
612 $code.=<<___;
613         vmov.i8 $t0,#0x0f                       @ compose .LBS2
614 ___
615         &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
616         &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
617
618         &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
619         &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
620 }
621
622 $code.=<<___;
623 #include "arm_arch.h"
624
625 #if __ARM_ARCH__>=7
626 .text
627 .code   32
628 .fpu    neon
629
630 .type   _bsaes_decrypt8,%function
631 .align  4
632 _bsaes_decrypt8:
633         sub     $const,pc,#8                    @ _bsaes_decrypt8
634         vldmia  $key!, {@XMM[9]}                @ round 0 key
635         add     $const,$const,#.LM0ISR-_bsaes_decrypt8
636
637         vldmia  $const!, {@XMM[8]}              @ .LM0ISR
638         veor    @XMM[10], @XMM[0], @XMM[9]      @ xor with round0 key
639         veor    @XMM[11], @XMM[1], @XMM[9]
640          vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
641          vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
642         veor    @XMM[12], @XMM[2], @XMM[9]
643          vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
644          vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
645         veor    @XMM[13], @XMM[3], @XMM[9]
646          vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
647          vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
648         veor    @XMM[14], @XMM[4], @XMM[9]
649          vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
650          vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
651         veor    @XMM[15], @XMM[5], @XMM[9]
652          vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
653          vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
654         veor    @XMM[10], @XMM[6], @XMM[9]
655          vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
656          vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
657         veor    @XMM[11], @XMM[7], @XMM[9]
658          vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
659          vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
660          vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
661          vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
662 ___
663         &bitslice       (@XMM[0..7, 8..11]);
664 $code.=<<___;
665         sub     $rounds,$rounds,#1
666         b       .Ldec_sbox
667 .align  4
668 .Ldec_loop:
669 ___
670         &ShiftRows      (@XMM[0..7, 8..12]);
671 $code.=".Ldec_sbox:\n";
672         &InvSbox        (@XMM[0..7, 8..15]);
673 $code.=<<___;
674         subs    $rounds,$rounds,#1
675         bcc     .Ldec_done
676 ___
677         &InvMixColumns  (@XMM[0,1,6,4,2,7,3,5, 8..15]);
678 $code.=<<___;
679         vldmia  $const, {@XMM[12]}              @ .LISR
680         addeq   $const,$const,#0x10
681         bne     .Ldec_loop
682         vldmia  $const, {@XMM[12]}              @ .LISRM0
683         b       .Ldec_loop
684 .align  4
685 .Ldec_done:
686 ___
687         &bitslice       (@XMM[0,1,6,4,2,7,3,5, 8..11]);
688 $code.=<<___;
689         vldmia  $key, {@XMM[8]}                 @ last round key
690         veor    @XMM[6], @XMM[6], @XMM[8]
691         veor    @XMM[4], @XMM[4], @XMM[8]
692         veor    @XMM[2], @XMM[2], @XMM[8]
693         veor    @XMM[7], @XMM[7], @XMM[8]
694         veor    @XMM[3], @XMM[3], @XMM[8]
695         veor    @XMM[5], @XMM[5], @XMM[8]
696         veor    @XMM[0], @XMM[0], @XMM[8]
697         veor    @XMM[1], @XMM[1], @XMM[8]
698         bx      lr
699 .size   _bsaes_decrypt8,.-_bsaes_decrypt8
700
701 .type   _bsaes_const,%object
702 .align  6
703 _bsaes_const:
704 .LM0ISR:        @ InvShiftRows constants
705         .quad   0x0a0e0206070b0f03, 0x0004080c0d010509
706 .LISR:
707         .quad   0x0504070602010003, 0x0f0e0d0c080b0a09
708 .LISRM0:
709         .quad   0x01040b0e0205080f, 0x0306090c00070a0d
710 .LM0SR:         @ ShiftRows constants
711         .quad   0x0a0e02060f03070b, 0x0004080c05090d01
712 .LSR:
713         .quad   0x0504070600030201, 0x0f0e0d0c0a09080b
714 .LSRM0:
715         .quad   0x0304090e00050a0f, 0x01060b0c0207080d
716 .LM0:
717         .quad   0x02060a0e03070b0f, 0x0004080c0105090d
718 .LREVM0SR:
719         .quad   0x090d02060c030708, 0x00040b0f050a0e01
720 .asciz  "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
721 .align  6
722 .size   _bsaes_const,.-_bsaes_const
723
724 .type   _bsaes_encrypt8,%function
725 .align  4
726 _bsaes_encrypt8:
727         sub     $const,pc,#8                    @ _bsaes_encrypt8
728         vldmia  $key!, {@XMM[9]}                @ round 0 key
729         sub     $const,$const,#_bsaes_encrypt8-.LM0SR
730
731         vldmia  $const!, {@XMM[8]}              @ .LM0SR
732 _bsaes_encrypt8_alt:
733         veor    @XMM[10], @XMM[0], @XMM[9]      @ xor with round0 key
734         veor    @XMM[11], @XMM[1], @XMM[9]
735          vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
736          vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
737         veor    @XMM[12], @XMM[2], @XMM[9]
738          vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
739          vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
740         veor    @XMM[13], @XMM[3], @XMM[9]
741          vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
742          vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
743         veor    @XMM[14], @XMM[4], @XMM[9]
744          vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
745          vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
746         veor    @XMM[15], @XMM[5], @XMM[9]
747          vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
748          vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
749         veor    @XMM[10], @XMM[6], @XMM[9]
750          vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
751          vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
752         veor    @XMM[11], @XMM[7], @XMM[9]
753          vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
754          vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
755          vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
756          vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
757 _bsaes_encrypt8_bitslice:
758 ___
759         &bitslice       (@XMM[0..7, 8..11]);
760 $code.=<<___;
761         sub     $rounds,$rounds,#1
762         b       .Lenc_sbox
763 .align  4
764 .Lenc_loop:
765 ___
766         &ShiftRows      (@XMM[0..7, 8..12]);
767 $code.=".Lenc_sbox:\n";
768         &Sbox           (@XMM[0..7, 8..15]);
769 $code.=<<___;
770         subs    $rounds,$rounds,#1
771         bcc     .Lenc_done
772 ___
773         &MixColumns     (@XMM[0,1,4,6,3,7,2,5, 8..15]);
774 $code.=<<___;
775         vldmia  $const, {@XMM[12]}              @ .LSR
776         addeq   $const,$const,#0x10
777         bne     .Lenc_loop
778         vldmia  $const, {@XMM[12]}              @ .LSRM0
779         b       .Lenc_loop
780 .align  4
781 .Lenc_done:
782 ___
783         # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
784         &bitslice       (@XMM[0,1,4,6,3,7,2,5, 8..11]);
785 $code.=<<___;
786         vldmia  $key, {@XMM[8]}                 @ last round key
787         veor    @XMM[4], @XMM[4], @XMM[8]
788         veor    @XMM[6], @XMM[6], @XMM[8]
789         veor    @XMM[3], @XMM[3], @XMM[8]
790         veor    @XMM[7], @XMM[7], @XMM[8]
791         veor    @XMM[2], @XMM[2], @XMM[8]
792         veor    @XMM[5], @XMM[5], @XMM[8]
793         veor    @XMM[0], @XMM[0], @XMM[8]
794         veor    @XMM[1], @XMM[1], @XMM[8]
795         bx      lr
796 .size   _bsaes_encrypt8,.-_bsaes_encrypt8
797 ___
798 }
799 {
800 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
801
802 sub bitslice_key {
803 my @x=reverse(@_[0..7]);
804 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
805
806         &swapmove       (@x[0,1],1,$bs0,$t2,$t3);
807 $code.=<<___;
808         @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
809         vmov    @x[2], @x[0]
810         vmov    @x[3], @x[1]
811 ___
812         #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
813
814         &swapmove2x     (@x[0,2,1,3],2,$bs1,$t2,$t3);
815 $code.=<<___;
816         @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
817         vmov    @x[4], @x[0]
818         vmov    @x[6], @x[2]
819         vmov    @x[5], @x[1]
820         vmov    @x[7], @x[3]
821 ___
822         &swapmove2x     (@x[0,4,1,5],4,$bs2,$t2,$t3);
823         &swapmove2x     (@x[2,6,3,7],4,$bs2,$t2,$t3);
824 }
825
826 $code.=<<___;
827 .type   _bsaes_key_convert,%function
828 .align  4
829 _bsaes_key_convert:
830         sub     $const,pc,#8                    @ _bsaes_key_convert
831         vld1.8  {@XMM[7]},  [$inp]!             @ load round 0 key
832         sub     $const,$const,#_bsaes_key_convert-.LM0
833         vld1.8  {@XMM[15]}, [$inp]!             @ load round 1 key
834
835         vmov.i8 @XMM[8],  #0x01                 @ bit masks
836         vmov.i8 @XMM[9],  #0x02
837         vmov.i8 @XMM[10], #0x04
838         vmov.i8 @XMM[11], #0x08
839         vmov.i8 @XMM[12], #0x10
840         vmov.i8 @XMM[13], #0x20
841         vldmia  $const, {@XMM[14]}              @ .LM0
842
843 #ifdef __ARMEL__
844         vrev32.8        @XMM[7],  @XMM[7]
845         vrev32.8        @XMM[15], @XMM[15]
846 #endif
847         sub     $rounds,$rounds,#1
848         vstmia  $out!, {@XMM[7]}                @ save round 0 key
849         b       .Lkey_loop
850
851 .align  4
852 .Lkey_loop:
853         vtbl.8  `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
854         vtbl.8  `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
855         vmov.i8 @XMM[6],  #0x40
856         vmov.i8 @XMM[15], #0x80
857
858         vtst.8  @XMM[0], @XMM[7], @XMM[8]
859         vtst.8  @XMM[1], @XMM[7], @XMM[9]
860         vtst.8  @XMM[2], @XMM[7], @XMM[10]
861         vtst.8  @XMM[3], @XMM[7], @XMM[11]
862         vtst.8  @XMM[4], @XMM[7], @XMM[12]
863         vtst.8  @XMM[5], @XMM[7], @XMM[13]
864         vtst.8  @XMM[6], @XMM[7], @XMM[6]
865         vtst.8  @XMM[7], @XMM[7], @XMM[15]
866         vld1.8  {@XMM[15]}, [$inp]!             @ load next round key
867         vmvn    @XMM[0], @XMM[0]                @ "pnot"
868         vmvn    @XMM[1], @XMM[1]
869         vmvn    @XMM[5], @XMM[5]
870         vmvn    @XMM[6], @XMM[6]
871 #ifdef __ARMEL__
872         vrev32.8        @XMM[15], @XMM[15]
873 #endif
874         subs    $rounds,$rounds,#1
875         vstmia  $out!,{@XMM[0]-@XMM[7]}         @ write bit-sliced round key
876         bne     .Lkey_loop
877
878         vmov.i8 @XMM[7],#0x63                   @ compose .L63
879         @ don't save last round key
880         bx      lr
881 .size   _bsaes_key_convert,.-_bsaes_key_convert
882 ___
883 }
884
885 if (0) {                # following four functions are unsupported interface
886                         # used for benchmarking...
887 $code.=<<___;
888 .globl  bsaes_enc_key_convert
889 .type   bsaes_enc_key_convert,%function
890 .align  4
891 bsaes_enc_key_convert:
892         stmdb   sp!,{r4-r6,lr}
893         vstmdb  sp!,{d8-d15}            @ ABI specification says so
894
895         ldr     r5,[$inp,#240]                  @ pass rounds
896         mov     r4,$inp                         @ pass key
897         mov     r12,$out                        @ pass key schedule
898         bl      _bsaes_key_convert
899         veor    @XMM[7],@XMM[7],@XMM[15]        @ fix up last round key
900         vstmia  r12, {@XMM[7]}                  @ save last round key
901
902         vldmia  sp!,{d8-d15}
903         ldmia   sp!,{r4-r6,pc}
904 .size   bsaes_enc_key_convert,.-bsaes_enc_key_convert
905
906 .globl  bsaes_encrypt_128
907 .type   bsaes_encrypt_128,%function
908 .align  4
909 bsaes_encrypt_128:
910         stmdb   sp!,{r4-r6,lr}
911         vstmdb  sp!,{d8-d15}            @ ABI specification says so
912 .Lenc128_loop:
913         vld1.8  {@XMM[0]-@XMM[1]}, [$inp]!      @ load input
914         vld1.8  {@XMM[2]-@XMM[3]}, [$inp]!
915         mov     r4,$key                         @ pass the key
916         vld1.8  {@XMM[4]-@XMM[5]}, [$inp]!
917         mov     r5,#10                          @ pass rounds
918         vld1.8  {@XMM[6]-@XMM[7]}, [$inp]!
919
920         bl      _bsaes_encrypt8
921
922         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
923         vst1.8  {@XMM[4]}, [$out]!
924         vst1.8  {@XMM[6]}, [$out]!
925         vst1.8  {@XMM[3]}, [$out]!
926         vst1.8  {@XMM[7]}, [$out]!
927         vst1.8  {@XMM[2]}, [$out]!
928         subs    $len,$len,#0x80
929         vst1.8  {@XMM[5]}, [$out]!
930         bhi     .Lenc128_loop
931
932         vldmia  sp!,{d8-d15}
933         ldmia   sp!,{r4-r6,pc}
934 .size   bsaes_encrypt_128,.-bsaes_encrypt_128
935
936 .globl  bsaes_dec_key_convert
937 .type   bsaes_dec_key_convert,%function
938 .align  4
939 bsaes_dec_key_convert:
940         stmdb   sp!,{r4-r6,lr}
941         vstmdb  sp!,{d8-d15}            @ ABI specification says so
942
943         ldr     r5,[$inp,#240]                  @ pass rounds
944         mov     r4,$inp                         @ pass key
945         mov     r12,$out                        @ pass key schedule
946         bl      _bsaes_key_convert
947         vldmia  $out, {@XMM[6]}
948         vstmia  r12,  {@XMM[15]}                @ save last round key
949         veor    @XMM[7], @XMM[7], @XMM[6]       @ fix up round 0 key
950         vstmia  $out, {@XMM[7]}
951
952         vldmia  sp!,{d8-d15}
953         ldmia   sp!,{r4-r6,pc}
954 .size   bsaes_dec_key_convert,.-bsaes_dec_key_convert
955
956 .globl  bsaes_decrypt_128
957 .type   bsaes_decrypt_128,%function
958 .align  4
959 bsaes_decrypt_128:
960         stmdb   sp!,{r4-r6,lr}
961         vstmdb  sp!,{d8-d15}            @ ABI specification says so
962 .Ldec128_loop:
963         vld1.8  {@XMM[0]-@XMM[1]}, [$inp]!      @ load input
964         vld1.8  {@XMM[2]-@XMM[3]}, [$inp]!
965         mov     r4,$key                         @ pass the key
966         vld1.8  {@XMM[4]-@XMM[5]}, [$inp]!
967         mov     r5,#10                          @ pass rounds
968         vld1.8  {@XMM[6]-@XMM[7]}, [$inp]!
969
970         bl      _bsaes_decrypt8
971
972         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
973         vst1.8  {@XMM[6]}, [$out]!
974         vst1.8  {@XMM[4]}, [$out]!
975         vst1.8  {@XMM[2]}, [$out]!
976         vst1.8  {@XMM[7]}, [$out]!
977         vst1.8  {@XMM[3]}, [$out]!
978         subs    $len,$len,#0x80
979         vst1.8  {@XMM[5]}, [$out]!
980         bhi     .Ldec128_loop
981
982         vldmia  sp!,{d8-d15}
983         ldmia   sp!,{r4-r6,pc}
984 .size   bsaes_decrypt_128,.-bsaes_decrypt_128
985 ___
986 }
987 {
988 my ($inp,$out,$len,$key, $ivp,$fp,$rounds)=map("r$_",(0..3,8..10));
989 my ($keysched)=("sp");
990
991 $code.=<<___;
992 .extern AES_cbc_encrypt
993 .extern AES_decrypt
994
995 .global bsaes_cbc_encrypt
996 .type   bsaes_cbc_encrypt,%function
997 .align  5
998 bsaes_cbc_encrypt:
999         cmp     $len, #128
1000         blo     AES_cbc_encrypt
1001
1002         @ it is up to the caller to make sure we are called with enc == 0
1003
1004         stmdb   sp!, {r4-r10, lr}
1005         vstmdb  sp!, {d8-d15}                   @ ABI specification says so
1006         ldr     $ivp, [sp, #0x60]               @ IV is 1st arg on the stack
1007         mov     $len, $len, lsr#4               @ len in 16 byte blocks
1008         sub     sp, #0x10                       @ scratch space to carry over the IV
1009         mov     $fp, sp                         @ save sp
1010
1011         @ allocate the key schedule on the stack
1012         ldr     $rounds, [$key, #240]           @ get # of rounds
1013         sub     sp, sp, $rounds, lsl#7          @ 128 bytes per inner round key
1014         add     sp, sp, #`128-32`               @ size of bit-sliced key schedule
1015
1016         @ populate the key schedule
1017         mov     r4, $key                        @ pass key
1018         mov     r5, $rounds                     @ pass # of rounds
1019         mov     r12, $keysched                  @ pass key schedule
1020         bl      _bsaes_key_convert
1021         vldmia  $keysched, {@XMM[6]}
1022         vstmia  r12,  {@XMM[15]}                @ save last round key
1023         veor    @XMM[7], @XMM[7], @XMM[6]       @ fix up round 0 key
1024         vstmia  $keysched, {@XMM[7]}
1025
1026         vld1.8  {@XMM[15]}, [$ivp]              @ load IV
1027         b       .Lcbc_dec_loop
1028
1029 .align  4
1030 .Lcbc_dec_loop:
1031         subs    $len, $len, #0x8
1032         bmi     .Lcbc_dec_loop_finish
1033
1034         vld1.8  {@XMM[0]-@XMM[1]}, [$inp]!      @ load input
1035         vld1.8  {@XMM[2]-@XMM[3]}, [$inp]!
1036         mov     r4, $keysched                   @ pass the key
1037         vld1.8  {@XMM[4]-@XMM[5]}, [$inp]!
1038         mov     r5, $rounds
1039         vld1.8  {@XMM[6]-@XMM[7]}, [$inp]
1040         sub     $inp, $inp, #0x60
1041         vstmia  $fp, {@XMM[15]}                 @ put aside IV
1042
1043         bl      _bsaes_decrypt8
1044
1045         vldmia  $fp, {@XMM[14]}                 @ reload IV
1046         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1047         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1048         vld1.8  {@XMM[10]-@XMM[11]}, [$inp]!
1049         veor    @XMM[1], @XMM[1], @XMM[8]
1050         veor    @XMM[6], @XMM[6], @XMM[9]
1051         vld1.8  {@XMM[12]-@XMM[13]}, [$inp]!
1052         veor    @XMM[4], @XMM[4], @XMM[10]
1053         veor    @XMM[2], @XMM[2], @XMM[11]
1054         vld1.8  {@XMM[14]-@XMM[15]}, [$inp]!
1055         veor    @XMM[7], @XMM[7], @XMM[12]
1056         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1057         veor    @XMM[3], @XMM[3], @XMM[13]
1058         vst1.8  {@XMM[6]}, [$out]!
1059         veor    @XMM[5], @XMM[5], @XMM[14]
1060         vst1.8  {@XMM[4]}, [$out]!
1061         vst1.8  {@XMM[2]}, [$out]!
1062         vst1.8  {@XMM[7]}, [$out]!
1063         vst1.8  {@XMM[3]}, [$out]!
1064         vst1.8  {@XMM[5]}, [$out]!
1065
1066         b       .Lcbc_dec_loop
1067
1068 .Lcbc_dec_loop_finish:
1069         adds    $len, $len, #8
1070         beq     .Lcbc_dec_done
1071
1072         vld1.8  {@XMM[0]}, [$inp]!              @ load input
1073         cmp     $len, #2
1074         blo     .Lcbc_dec_one
1075         vld1.8  {@XMM[1]}, [$inp]!
1076         mov     r4, $keysched                   @ pass the key
1077         mov     r5, $rounds
1078         vstmia  $fp, {@XMM[15]}                 @ put aside IV
1079         beq     .Lcbc_dec_two
1080         vld1.8  {@XMM[2]}, [$inp]!
1081         cmp     $len, #4
1082         blo     .Lcbc_dec_three
1083         vld1.8  {@XMM[3]}, [$inp]!
1084         beq     .Lcbc_dec_four
1085         vld1.8  {@XMM[4]}, [$inp]!
1086         cmp     $len, #6
1087         blo     .Lcbc_dec_five
1088         vld1.8  {@XMM[5]}, [$inp]!
1089         beq     .Lcbc_dec_six
1090         vld1.8  {@XMM[6]}, [$inp]!
1091         sub     $inp, $inp, #0x70
1092
1093         bl      _bsaes_decrypt8
1094
1095         vldmia  $fp, {@XMM[14]}                 @ reload IV
1096         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1097         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1098         vld1.8  {@XMM[10]-@XMM[11]}, [$inp]!
1099         veor    @XMM[1], @XMM[1], @XMM[8]
1100         veor    @XMM[6], @XMM[6], @XMM[9]
1101         vld1.8  {@XMM[12]-@XMM[13]}, [$inp]!
1102         veor    @XMM[4], @XMM[4], @XMM[10]
1103         veor    @XMM[2], @XMM[2], @XMM[11]
1104         vld1.8  {@XMM[15]}, [$inp]!
1105         veor    @XMM[7], @XMM[7], @XMM[12]
1106         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1107         veor    @XMM[3], @XMM[3], @XMM[13]
1108         vst1.8  {@XMM[6]}, [$out]!
1109         vst1.8  {@XMM[4]}, [$out]!
1110         vst1.8  {@XMM[2]}, [$out]!
1111         vst1.8  {@XMM[7]}, [$out]!
1112         vst1.8  {@XMM[3]}, [$out]!
1113         b       .Lcbc_dec_done
1114 .align  4
1115 .Lcbc_dec_six:
1116         sub     $inp, $inp, #0x60
1117         bl      _bsaes_decrypt8
1118         vldmia  $fp,{@XMM[14]}                  @ reload IV
1119         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1120         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1121         vld1.8  {@XMM[10]-@XMM[11]}, [$inp]!
1122         veor    @XMM[1], @XMM[1], @XMM[8]
1123         veor    @XMM[6], @XMM[6], @XMM[9]
1124         vld1.8  {@XMM[12]}, [$inp]!
1125         veor    @XMM[4], @XMM[4], @XMM[10]
1126         veor    @XMM[2], @XMM[2], @XMM[11]
1127         vld1.8  {@XMM[15]}, [$inp]!
1128         veor    @XMM[7], @XMM[7], @XMM[12]
1129         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1130         vst1.8  {@XMM[6]}, [$out]!
1131         vst1.8  {@XMM[4]}, [$out]!
1132         vst1.8  {@XMM[2]}, [$out]!
1133         vst1.8  {@XMM[7]}, [$out]!
1134         b       .Lcbc_dec_done
1135 .align  4
1136 .Lcbc_dec_five:
1137         sub     $inp, $inp, #0x50
1138         bl      _bsaes_decrypt8
1139         vldmia  $fp, {@XMM[14]}                 @ reload IV
1140         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1141         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1142         vld1.8  {@XMM[10]-@XMM[11]}, [$inp]!
1143         veor    @XMM[1], @XMM[1], @XMM[8]
1144         veor    @XMM[6], @XMM[6], @XMM[9]
1145         vld1.8  {@XMM[15]}, [$inp]!
1146         veor    @XMM[4], @XMM[4], @XMM[10]
1147         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1148         veor    @XMM[2], @XMM[2], @XMM[11]
1149         vst1.8  {@XMM[6]}, [$out]!
1150         vst1.8  {@XMM[4]}, [$out]!
1151         vst1.8  {@XMM[2]}, [$out]!
1152         b       .Lcbc_dec_done
1153 .align  4
1154 .Lcbc_dec_four:
1155         sub     $inp, $inp, #0x40
1156         bl      _bsaes_decrypt8
1157         vldmia  $fp, {@XMM[14]}                 @ reload IV
1158         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1159         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1160         vld1.8  {@XMM[10]}, [$inp]!
1161         veor    @XMM[1], @XMM[1], @XMM[8]
1162         veor    @XMM[6], @XMM[6], @XMM[9]
1163         vld1.8  {@XMM[15]}, [$inp]!
1164         veor    @XMM[4], @XMM[4], @XMM[10]
1165         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1166         vst1.8  {@XMM[6]}, [$out]!
1167         vst1.8  {@XMM[4]}, [$out]!
1168         b       .Lcbc_dec_done
1169 .align  4
1170 .Lcbc_dec_three:
1171         sub     $inp, $inp, #0x30
1172         bl      _bsaes_decrypt8
1173         vldmia  $fp, {@XMM[14]}                 @ reload IV
1174         vld1.8  {@XMM[8]-@XMM[9]}, [$inp]!      @ reload input
1175         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1176         vld1.8  {@XMM[15]}, [$inp]!
1177         veor    @XMM[1], @XMM[1], @XMM[8]
1178         veor    @XMM[6], @XMM[6], @XMM[9]
1179         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1180         vst1.8  {@XMM[6]}, [$out]!
1181         b       .Lcbc_dec_done
1182 .align  4
1183 .Lcbc_dec_two:
1184         sub     $inp, $inp, #0x20
1185         bl      _bsaes_decrypt8
1186         vldmia  $fp, {@XMM[14]}                 @ reload IV
1187         vld1.8  {@XMM[8]}, [$inp]!              @ reload input
1188         veor    @XMM[0], @XMM[0], @XMM[14]      @ ^= IV
1189         vld1.8  {@XMM[15]}, [$inp]!             @ reload input
1190         veor    @XMM[1], @XMM[1], @XMM[8]
1191         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1192         b       .Lcbc_dec_done
1193 .align  4
1194 .Lcbc_dec_one:
1195         sub     $inp, $inp, #0x10
1196         mov     $rounds, $out                   @ save original out pointer
1197         mov     $out, $fp                       @ use the iv scratch space as out buffer
1198         mov     r2, $key
1199         vmov    @XMM[4],@XMM[15]                @ just in case ensure that IV
1200         vmov    @XMM[5],@XMM[0]                 @ and input are preserved
1201         bl      AES_decrypt
1202         vld1.8  {@XMM[0]}, [$fp,:64]            @ load result
1203         veor    @XMM[0], @XMM[0], @XMM[4]       @ ^= IV
1204         vmov    @XMM[15], @XMM[5]               @ @XMM[5] holds input
1205         vst1.8  {@XMM[0]}, [$rounds]            @ write output
1206
1207 .Lcbc_dec_done:
1208         vmov.i32        q0, #0
1209         vmov.i32        q1, #0
1210 .Lcbc_dec_bzero:                                @ wipe key schedule [if any]
1211         vstmia          $keysched!, {q0-q1}
1212         teq             $keysched, $fp
1213         bne             .Lcbc_dec_bzero
1214
1215         add     sp, $fp, #0x10
1216         vst1.8  {@XMM[15]}, [$ivp]              @ return IV
1217         vldmia  sp!, {d8-d15}
1218         ldmia   sp!, {r4-r10, pc}
1219 .size   bsaes_cbc_encrypt,.-bsaes_cbc_encrypt
1220 ___
1221 }
1222 {
1223 my ($inp,$out,$len,$key, $ctr,$fp,$rounds)=(map("r$_",(0..3,8..10)));
1224 my $const = "r6";       # shared with _bsaes_encrypt8_alt
1225 my $keysched = "sp";
1226
1227 $code.=<<___;
1228 .extern AES_encrypt
1229 .global bsaes_ctr32_encrypt_blocks
1230 .type   bsaes_ctr32_encrypt_blocks,%function
1231 .align  5
1232 bsaes_ctr32_encrypt_blocks:
1233         cmp     $len, #8                        @ use plain AES for
1234         blo     .Lctr_enc_short                 @ small sizes
1235
1236         stmdb   sp!, {r4-r10, lr}
1237         vstmdb  sp!, {d8-d15}                   @ ABI specification says so
1238         ldr     $ctr, [sp, #0x60]               @ ctr is 1st arg on the stack
1239         sub     sp, sp, #0x10                   @ scratch space to carry over the ctr
1240         mov     $fp, sp                         @ save sp
1241
1242         @ allocate the key schedule on the stack
1243         ldr     $rounds, [$key, #240]           @ get # of rounds
1244         sub     sp, sp, $rounds, lsl#7          @ 128 bytes per inner round key
1245         add     sp, sp, #`128-32`               @ size of bit-sliced key schedule
1246
1247         @ populate the key schedule
1248         mov     r4, $key                        @ pass key
1249         mov     r5, $rounds                     @ pass # of rounds
1250         mov     r12, $keysched                  @ pass key schedule
1251         bl      _bsaes_key_convert
1252         veor    @XMM[7],@XMM[7],@XMM[15]        @ fix up last round key
1253         vstmia  r12, {@XMM[7]}                  @ save last round key
1254
1255         vld1.8  {@XMM[0]}, [$ctr]               @ load counter
1256         add     $ctr, $const, #.LREVM0SR-.LM0   @ borrow $ctr
1257         vldmia  $keysched, {@XMM[4]}            @ load round0 key
1258
1259         vmov.i32        `&Dhi("@XMM[8]")`,#1    @ compose 1<<96
1260         vmov.i32        `&Dlo("@XMM[8]")`,#0
1261         vrev32.8        `&Dhi("@XMM[0]")`,`&Dhi("@XMM[0]")`
1262         vshl.u64        `&Dhi("@XMM[8]")`,#32
1263         vrev32.8        `&Dhi("@XMM[4]")`,`&Dhi("@XMM[4]")`
1264         vadd.u32        @XMM[9],@XMM[8],@XMM[8] @ compose 2<<96
1265         vstmia  $keysched, {@XMM[4]}            @ save adjusted round0 key
1266         b       .Lctr_enc_loop
1267
1268 .align  4
1269 .Lctr_enc_loop:
1270         vadd.u32        @XMM[10], @XMM[8], @XMM[9]      @ compose 3<<96
1271         vadd.u32        @XMM[1], @XMM[0], @XMM[8]       @ +1
1272         vadd.u32        @XMM[2], @XMM[0], @XMM[9]       @ +2
1273         vadd.u32        @XMM[3], @XMM[0], @XMM[10]      @ +3
1274         vadd.u32        @XMM[4], @XMM[1], @XMM[10]
1275         vadd.u32        @XMM[5], @XMM[2], @XMM[10]
1276         vadd.u32        @XMM[6], @XMM[3], @XMM[10]
1277         vadd.u32        @XMM[7], @XMM[4], @XMM[10]
1278         vadd.u32        @XMM[10], @XMM[5], @XMM[10]     @ next counter
1279
1280         @ Borrow prologue from _bsaes_encrypt8 to use the opportunity
1281         @ to flip byte order in 32-bit counter
1282
1283         vldmia          $keysched, {@XMM[9]}            @ load round0 key
1284         add             r4, $keysched, #0x10            @ pass next round key
1285         vldmia          $ctr, {@XMM[8]}                 @ .LREVM0SR
1286         mov             r5, $rounds                     @ pass rounds
1287         vstmia          $fp, {@XMM[10]}                 @ save next counter
1288         sub             $const, $ctr, #.LREVM0SR-.LSR   @ pass constants
1289
1290         bl              _bsaes_encrypt8_alt
1291
1292         subs            $len, $len, #8
1293         blo             .Lctr_enc_loop_done
1294
1295         vld1.8          {@XMM[8]-@XMM[9]}, [$inp]!      @ load input
1296         vld1.8          {@XMM[10]-@XMM[11]}, [$inp]!
1297         veor            @XMM[0], @XMM[8]
1298         veor            @XMM[1], @XMM[9]
1299         vld1.8          {@XMM[12]-@XMM[13]}, [$inp]!
1300         veor            @XMM[4], @XMM[10]
1301         veor            @XMM[6], @XMM[11]
1302         vld1.8          {@XMM[14]-@XMM[15]}, [$inp]!
1303         veor            @XMM[3], @XMM[12]
1304         vst1.8          {@XMM[0]-@XMM[1]}, [$out]!      @ write output
1305         veor            @XMM[7], @XMM[13]
1306         veor            @XMM[2], @XMM[14]
1307         vst1.8          {@XMM[4]}, [$out]!
1308         veor            @XMM[5], @XMM[15]
1309         vst1.8          {@XMM[6]}, [$out]!
1310         vmov.i32        `&Dhi("@XMM[8]")`,#1            @ compose 1<<96
1311         vst1.8          {@XMM[3]}, [$out]!
1312         vmov.i32        `&Dlo("@XMM[8]")`,#0
1313         vst1.8          {@XMM[7]}, [$out]!
1314         vshl.u64        `&Dhi("@XMM[8]")`,#32
1315         vst1.8          {@XMM[2]}, [$out]!
1316         vadd.u32        @XMM[9],@XMM[8],@XMM[8]         @ compose 2<<96
1317         vst1.8          {@XMM[5]}, [$out]!
1318         vldmia          $fp, {@XMM[0]}                  @ load counter
1319
1320         bne             .Lctr_enc_loop
1321         b               .Lctr_enc_done
1322
1323 .align  4
1324 .Lctr_enc_loop_done:
1325         add             $len, $len, #8
1326         vld1.8          {@XMM[8]}, [$inp]!      @ load input
1327         veor            @XMM[0], @XMM[8]
1328         vst1.8          {@XMM[0]}, [$out]!      @ write output
1329         cmp             $len, #2
1330         blo             .Lctr_enc_done
1331         vld1.8          {@XMM[9]}, [$inp]!
1332         veor            @XMM[1], @XMM[9]
1333         vst1.8          {@XMM[1]}, [$out]!
1334         beq             .Lctr_enc_done
1335         vld1.8          {@XMM[10]}, [$inp]!
1336         veor            @XMM[4], @XMM[10]
1337         vst1.8          {@XMM[4]}, [$out]!
1338         cmp             $len, #4
1339         blo             .Lctr_enc_done
1340         vld1.8          {@XMM[11]}, [$inp]!
1341         veor            @XMM[6], @XMM[11]
1342         vst1.8          {@XMM[6]}, [$out]!
1343         beq             .Lctr_enc_done
1344         vld1.8          {@XMM[12]}, [$inp]!
1345         veor            @XMM[3], @XMM[12]
1346         vst1.8          {@XMM[3]}, [$out]!
1347         cmp             $len, #6
1348         blo             .Lctr_enc_done
1349         vld1.8          {@XMM[13]}, [$inp]!
1350         veor            @XMM[7], @XMM[13]
1351         vst1.8          {@XMM[7]}, [$out]!
1352         beq             .Lctr_enc_done
1353         vld1.8          {@XMM[14]}, [$inp]
1354         veor            @XMM[2], @XMM[14]
1355         vst1.8          {@XMM[2]}, [$out]!
1356
1357 .Lctr_enc_done:
1358         vmov.i32        q0, #0
1359         vmov.i32        q1, #0
1360 .Lctr_enc_bzero:                        @ wipe key schedule [if any]
1361         vstmia          $keysched!, {q0-q1}
1362         teq             $keysched, $fp
1363         bne             .Lctr_enc_bzero
1364
1365         add     sp, $fp, #0x10
1366         vldmia  sp!, {d8-d15}
1367         ldmia   sp!, {r4-r10, pc}       @ return
1368
1369 .align  4
1370 .Lctr_enc_short:
1371         ldr     ip, [sp]                @ ctr pointer is passed on stack
1372         stmdb   sp!, {r4-r8, lr}
1373
1374         mov     r4, $inp                @ copy arguments
1375         mov     r5, $out
1376         mov     r6, $len
1377         mov     r7, $key
1378         ldr     r8, [ip, #12]           @ load counter LSW
1379         vld1.8  {@XMM[1]}, [ip]         @ load whole counter value
1380 #ifdef __ARMEL__
1381         rev     r8, r8
1382 #endif
1383         sub     sp, sp, #0x10
1384         vst1.8  {@XMM[1]}, [sp,:64]     @ copy counter value
1385         sub     sp, sp, #0x10
1386
1387 .Lctr_enc_short_loop:
1388         add     r0, sp, #0x10           @ input counter value
1389         mov     r1, sp                  @ output on the stack
1390         mov     r2, r7                  @ key
1391
1392         bl      AES_encrypt
1393
1394         vld1.8  {@XMM[0]}, [r4]!        @ load input
1395         vld1.8  {@XMM[1]}, [sp,:64]     @ load encrypted counter
1396         add     r8, r8, #1
1397 #ifdef __ARMEL__
1398         rev     r0, r8
1399         str     r0, [sp, #0x1c]         @ next counter value
1400 #else
1401         str     r8, [sp, #0x1c]         @ next counter value
1402 #endif
1403         veor    @XMM[0],@XMM[0],@XMM[1]
1404         vst1.8  {@XMM[0]}, [r5]!        @ store output
1405         subs    r6, r6, #1
1406         bne     .Lctr_enc_short_loop
1407
1408         add     sp, sp, #0x20
1409         ldmia   sp!, {r4-r8, pc}
1410 .size   bsaes_ctr32_encrypt_blocks,.-bsaes_ctr32_encrypt_blocks
1411 ___
1412 }
1413 $code.=<<___;
1414 #endif
1415 ___
1416
1417 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
1418
1419 print $code;
1420
1421 close STDOUT;