bsaes-armv7.pl: closest shave. While 0.3 cpb improvement on S4 appears
[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 # When comparing to x86_64 results keep in mind that NEON unit is
30 # [mostly] single-issue and thus can't [fully] benefit from
31 # instruction-level parallelism. And when comparing to aes-armv4
32 # results keep in mind key schedule conversion overhead (see
33 # bsaes-x86_64.pl for further details)...
34 #
35 #                                               <appro@openssl.org>
36
37 while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
38 open STDOUT,">$output";
39
40 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
41 my @XMM=map("q$_",(0..15));
42
43 {
44 my ($key,$rounds,$const)=("r4","r5","r6");
45
46 sub Dlo()   { shift=~m|q([1]?[0-9])|?"d".($1*2):"";     }
47 sub Dhi()   { shift=~m|q([1]?[0-9])|?"d".($1*2+1):"";   }
48
49 sub Sbox {
50 # input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
51 # output in lsb > [b0, b1, b4, b6, b3, b7, b2, b5] < msb
52 my @b=@_[0..7];
53 my @t=@_[8..11];
54 my @s=@_[12..15];
55         &InBasisChange  (@b);
56         &Inv_GF256      (@b[6,5,0,3,7,1,4,2],@t,@s);
57         &OutBasisChange (@b[7,1,4,2,6,5,0,3]);
58 }
59
60 sub InBasisChange {
61 # input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
62 # output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb 
63 my @b=@_[0..7];
64 $code.=<<___;
65         veor    @b[2], @b[2], @b[1]
66         veor    @b[5], @b[5], @b[6]
67         veor    @b[3], @b[3], @b[0]
68         veor    @b[6], @b[6], @b[2]
69         veor    @b[5], @b[5], @b[0]
70
71         veor    @b[6], @b[6], @b[3]
72         veor    @b[3], @b[3], @b[7]
73         veor    @b[7], @b[7], @b[5]
74         veor    @b[3], @b[3], @b[4]
75         veor    @b[4], @b[4], @b[5]
76
77         veor    @b[2], @b[2], @b[7]
78         veor    @b[3], @b[3], @b[1]
79         veor    @b[1], @b[1], @b[5]
80 ___
81 }
82
83 sub OutBasisChange {
84 # input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
85 # output in lsb > [b6, b1, b2, b4, b7, b0, b3, b5] < msb
86 my @b=@_[0..7];
87 $code.=<<___;
88         veor    @b[0], @b[0], @b[6]
89         veor    @b[1], @b[1], @b[4]
90         veor    @b[4], @b[4], @b[6]
91         veor    @b[2], @b[2], @b[0]
92         veor    @b[6], @b[6], @b[1]
93
94         veor    @b[1], @b[1], @b[5]
95         veor    @b[5], @b[5], @b[3]
96         veor    @b[3], @b[3], @b[7]
97         veor    @b[7], @b[7], @b[5]
98         veor    @b[2], @b[2], @b[5]
99
100         veor    @b[4], @b[4], @b[7]
101 ___
102 }
103
104 sub InvSbox {
105 # input in lsb  > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
106 # output in lsb > [b0, b1, b6, b4, b2, b7, b3, b5] < msb
107 my @b=@_[0..7];
108 my @t=@_[8..11];
109 my @s=@_[12..15];
110         &InvInBasisChange       (@b);
111         &Inv_GF256              (@b[5,1,2,6,3,7,0,4],@t,@s);
112         &InvOutBasisChange      (@b[3,7,0,4,5,1,2,6]);
113 }
114
115 sub InvInBasisChange {          # OutBasisChange in reverse (with twist)
116 my @b=@_[5,1,2,6,3,7,0,4];
117 $code.=<<___
118          veor   @b[1], @b[1], @b[7]
119         veor    @b[4], @b[4], @b[7]
120
121         veor    @b[7], @b[7], @b[5]
122          veor   @b[1], @b[1], @b[3]
123         veor    @b[2], @b[2], @b[5]
124         veor    @b[3], @b[3], @b[7]
125
126         veor    @b[6], @b[6], @b[1]
127         veor    @b[2], @b[2], @b[0]
128          veor   @b[5], @b[5], @b[3]
129         veor    @b[4], @b[4], @b[6]
130         veor    @b[0], @b[0], @b[6]
131         veor    @b[1], @b[1], @b[4]
132 ___
133 }
134
135 sub InvOutBasisChange {         # InBasisChange in reverse
136 my @b=@_[2,5,7,3,6,1,0,4];
137 $code.=<<___;
138         veor    @b[1], @b[1], @b[5]
139         veor    @b[2], @b[2], @b[7]
140
141         veor    @b[3], @b[3], @b[1]
142         veor    @b[4], @b[4], @b[5]
143         veor    @b[7], @b[7], @b[5]
144         veor    @b[3], @b[3], @b[4]
145          veor   @b[5], @b[5], @b[0]
146         veor    @b[3], @b[3], @b[7]
147          veor   @b[6], @b[6], @b[2]
148          veor   @b[2], @b[2], @b[1]
149         veor    @b[6], @b[6], @b[3]
150
151         veor    @b[3], @b[3], @b[0]
152         veor    @b[5], @b[5], @b[6]
153 ___
154 }
155
156 sub Mul_GF4 {
157 #;*************************************************************
158 #;* Mul_GF4: Input x0-x1,y0-y1 Output x0-x1 Temp t0 (8) *
159 #;*************************************************************
160 my ($x0,$x1,$y0,$y1,$t0,$t1)=@_;
161 $code.=<<___;
162         veor    $t0, $y0, $y1
163         vand    $t0, $t0, $x0
164         veor    $x0, $x0, $x1
165         vand    $t1, $x1, $y0
166         vand    $x0, $x0, $y1
167         veor    $x1, $t1, $t0
168         veor    $x0, $x0, $t1
169 ___
170 }
171
172 sub Mul_GF4_N {                         # not used, see next subroutine
173 # multiply and scale by N
174 my ($x0,$x1,$y0,$y1,$t0)=@_;
175 $code.=<<___;
176         veor    $t0, $y0, $y1
177         vand    $t0, $t0, $x0
178         veor    $x0, $x0, $x1
179         vand    $x1, $x1, $y0
180         vand    $x0, $x0, $y1
181         veor    $x1, $x1, $x0
182         veor    $x0, $x0, $t0
183 ___
184 }
185
186 sub Mul_GF4_N_GF4 {
187 # interleaved Mul_GF4_N and Mul_GF4
188 my ($x0,$x1,$y0,$y1,$t0,
189     $x2,$x3,$y2,$y3,$t1)=@_;
190 $code.=<<___;
191         veor    $t0, $y0, $y1
192          veor   $t1, $y2, $y3
193         vand    $t0, $t0, $x0
194          vand   $t1, $t1, $x2
195         veor    $x0, $x0, $x1
196          veor   $x2, $x2, $x3
197         vand    $x1, $x1, $y0
198          vand   $x3, $x3, $y2
199         vand    $x0, $x0, $y1
200          vand   $x2, $x2, $y3
201         veor    $x1, $x1, $x0
202          veor   $x2, $x2, $x3
203         veor    $x0, $x0, $t0
204          veor   $x3, $x3, $t1
205 ___
206 }
207 sub Mul_GF16_2 {
208 my @x=@_[0..7];
209 my @y=@_[8..11];
210 my @t=@_[12..15];
211 $code.=<<___;
212         veor    @t[0], @x[0], @x[2]
213         veor    @t[1], @x[1], @x[3]
214 ___
215         &Mul_GF4        (@x[0], @x[1], @y[0], @y[1], @t[2..3]);
216 $code.=<<___;
217         veor    @y[0], @y[0], @y[2]
218         veor    @y[1], @y[1], @y[3]
219 ___
220         Mul_GF4_N_GF4   (@t[0], @t[1], @y[0], @y[1], @t[3],
221                          @x[2], @x[3], @y[2], @y[3], @t[2]);
222 $code.=<<___;
223         veor    @x[0], @x[0], @t[0]
224         veor    @x[2], @x[2], @t[0]
225         veor    @x[1], @x[1], @t[1]
226         veor    @x[3], @x[3], @t[1]
227
228         veor    @t[0], @x[4], @x[6]
229         veor    @t[1], @x[5], @x[7]
230 ___
231         &Mul_GF4_N_GF4  (@t[0], @t[1], @y[0], @y[1], @t[3],
232                          @x[6], @x[7], @y[2], @y[3], @t[2]);
233 $code.=<<___;
234         veor    @y[0], @y[0], @y[2]
235         veor    @y[1], @y[1], @y[3]
236 ___
237         &Mul_GF4        (@x[4], @x[5], @y[0], @y[1], @t[2..3]);
238 $code.=<<___;
239         veor    @x[4], @x[4], @t[0]
240         veor    @x[6], @x[6], @t[0]
241         veor    @x[5], @x[5], @t[1]
242         veor    @x[7], @x[7], @t[1]
243 ___
244 }
245 sub Inv_GF256 {
246 #;********************************************************************
247 #;* Inv_GF256: Input x0-x7 Output x0-x7 Temp t0-t3,s0-s3 (144)       *
248 #;********************************************************************
249 my @x=@_[0..7];
250 my @t=@_[8..11];
251 my @s=@_[12..15];
252 # direct optimizations from hardware
253 $code.=<<___;
254         veor    @t[3], @x[4], @x[6]
255         veor    @t[2], @x[5], @x[7]
256         veor    @t[1], @x[1], @x[3]
257         veor    @s[1], @x[7], @x[6]
258          vmov   @t[0], @t[2]
259         veor    @s[0], @x[0], @x[2]
260
261         vorr    @t[2], @t[2], @t[1]
262         veor    @s[3], @t[3], @t[0]
263         vand    @s[2], @t[3], @s[0]
264         vorr    @t[3], @t[3], @s[0]
265         veor    @s[0], @s[0], @t[1]
266         vand    @t[0], @t[0], @t[1]
267         veor    @t[1], @x[3], @x[2]
268         vand    @s[3], @s[3], @s[0]
269         vand    @s[1], @s[1], @t[1]
270         veor    @t[1], @x[4], @x[5]
271         veor    @s[0], @x[1], @x[0]
272         veor    @t[3], @t[3], @s[1]
273         veor    @t[2], @t[2], @s[1]
274         vand    @s[1], @t[1], @s[0]
275         vorr    @t[1], @t[1], @s[0]
276         veor    @t[3], @t[3], @s[3]
277         veor    @t[0], @t[0], @s[1]
278         veor    @t[2], @t[2], @s[2]
279         veor    @t[1], @t[1], @s[3]
280         veor    @t[0], @t[0], @s[2]
281         vand    @s[0], @x[7], @x[3]
282         veor    @t[1], @t[1], @s[2]
283         vand    @s[1], @x[6], @x[2]
284         vand    @s[2], @x[5], @x[1]
285         vorr    @s[3], @x[4], @x[0]
286         veor    @t[3], @t[3], @s[0]
287         veor    @t[1], @t[1], @s[2]
288         veor    @t[0], @t[0], @s[3]
289         veor    @t[2], @t[2], @s[1]
290
291         @ Inv_GF16 \t0, \t1, \t2, \t3, \s0, \s1, \s2, \s3
292
293         @ new smaller inversion
294
295         vand    @s[2], @t[3], @t[1]
296         vmov    @s[0], @t[0]
297
298         veor    @s[1], @t[2], @s[2]
299         veor    @s[3], @t[0], @s[2]
300         veor    @s[2], @t[0], @s[2]     @ @s[2]=@s[3]
301
302         vbsl    @s[1], @t[1], @t[0]
303         vbsl    @s[3], @t[3], @t[2]
304         veor    @t[3], @t[3], @t[2]
305
306         vbsl    @s[0], @s[1], @s[2]
307         vbsl    @t[0], @s[2], @s[1]
308
309         vand    @s[2], @s[0], @s[3]
310         veor    @t[1], @t[1], @t[0]
311
312         veor    @s[2], @s[2], @t[3]
313 ___
314 # output in s3, s2, s1, t1
315
316 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \t2, \t3, \t0, \t1, \s0, \s1, \s2, \s3
317
318 # Mul_GF16_2 \x0, \x1, \x2, \x3, \x4, \x5, \x6, \x7, \s3, \s2, \s1, \t1, \s0, \t0, \t2, \t3
319         &Mul_GF16_2(@x,@s[3,2,1],@t[1],@s[0],@t[0,2,3]);
320
321 ### output msb > [x3,x2,x1,x0,x7,x6,x5,x4] < lsb
322 }
323
324 # AES linear components
325
326 sub ShiftRows {
327 my @x=@_[0..7];
328 my @t=@_[8..11];
329 my $mask=pop;
330 $code.=<<___;
331         vldmia  $key!, {@t[0]-@t[3]}
332         veor    @t[0], @t[0], @x[0]
333         veor    @t[1], @t[1], @x[1]
334         vtbl.8  `&Dlo(@x[0])`, {@t[0]}, `&Dlo($mask)`
335         vtbl.8  `&Dhi(@x[0])`, {@t[0]}, `&Dhi($mask)`
336         vldmia  $key!, {@t[0]}
337         veor    @t[2], @t[2], @x[2]
338         vtbl.8  `&Dlo(@x[1])`, {@t[1]}, `&Dlo($mask)`
339         vtbl.8  `&Dhi(@x[1])`, {@t[1]}, `&Dhi($mask)`
340         vldmia  $key!, {@t[1]}
341         veor    @t[3], @t[3], @x[3]
342         vtbl.8  `&Dlo(@x[2])`, {@t[2]}, `&Dlo($mask)`
343         vtbl.8  `&Dhi(@x[2])`, {@t[2]}, `&Dhi($mask)`
344         vldmia  $key!, {@t[2]}
345         vtbl.8  `&Dlo(@x[3])`, {@t[3]}, `&Dlo($mask)`
346         vtbl.8  `&Dhi(@x[3])`, {@t[3]}, `&Dhi($mask)`
347         vldmia  $key!, {@t[3]}
348         veor    @t[0], @t[0], @x[4]
349         veor    @t[1], @t[1], @x[5]
350         vtbl.8  `&Dlo(@x[4])`, {@t[0]}, `&Dlo($mask)`
351         vtbl.8  `&Dhi(@x[4])`, {@t[0]}, `&Dhi($mask)`
352         veor    @t[2], @t[2], @x[6]
353         vtbl.8  `&Dlo(@x[5])`, {@t[1]}, `&Dlo($mask)`
354         vtbl.8  `&Dhi(@x[5])`, {@t[1]}, `&Dhi($mask)`
355         veor    @t[3], @t[3], @x[7]
356         vtbl.8  `&Dlo(@x[6])`, {@t[2]}, `&Dlo($mask)`
357         vtbl.8  `&Dhi(@x[6])`, {@t[2]}, `&Dhi($mask)`
358         vtbl.8  `&Dlo(@x[7])`, {@t[3]}, `&Dlo($mask)`
359         vtbl.8  `&Dhi(@x[7])`, {@t[3]}, `&Dhi($mask)`
360 ___
361 }
362
363 sub MixColumns {
364 # modified to emit output in order suitable for feeding back to aesenc[last]
365 my @x=@_[0..7];
366 my @t=@_[8..15];
367 $code.=<<___;
368         vext.8  @t[0], @x[0], @x[0], #12        @ x0 <<< 32
369         vext.8  @t[1], @x[1], @x[1], #12
370          veor   @x[0], @x[0], @t[0]             @ x0 ^ (x0 <<< 32)
371         vext.8  @t[2], @x[2], @x[2], #12
372          veor   @x[1], @x[1], @t[1]
373         vext.8  @t[3], @x[3], @x[3], #12
374          veor   @x[2], @x[2], @t[2]
375         vext.8  @t[4], @x[4], @x[4], #12
376          veor   @x[3], @x[3], @t[3]
377         vext.8  @t[5], @x[5], @x[5], #12
378          veor   @x[4], @x[4], @t[4]
379         vext.8  @t[6], @x[6], @x[6], #12
380          veor   @x[5], @x[5], @t[5]
381         vext.8  @t[7], @x[7], @x[7], #12
382          veor   @x[6], @x[6], @t[6]
383
384         veor    @t[1], @t[1], @x[0]
385          veor   @x[7], @x[7], @t[7]
386          vext.8 @x[0], @x[0], @x[0], #8         @ (x0 ^ (x0 <<< 32)) <<< 64)
387         veor    @t[2], @t[2], @x[1]
388         veor    @t[0], @t[0], @x[7]
389         veor    @t[1], @t[1], @x[7]
390          vext.8 @x[1], @x[1], @x[1], #8
391         veor    @t[5], @t[5], @x[4]
392          veor   @x[0], @x[0], @t[0]
393         veor    @t[6], @t[6], @x[5]
394          veor   @x[1], @x[1], @t[1]
395          vext.8 @t[0], @x[4], @x[4], #8
396         veor    @t[4], @t[4], @x[3]
397          vext.8 @t[1], @x[5], @x[5], #8
398         veor    @t[7], @t[7], @x[6]
399          vext.8 @x[4], @x[3], @x[3], #8
400         veor    @t[3], @t[3], @x[2]
401          vext.8 @x[5], @x[7], @x[7], #8
402         veor    @t[4], @t[4], @x[7]
403          vext.8 @x[3], @x[6], @x[6], #8
404         veor    @t[3], @t[3], @x[7]
405          vext.8 @x[6], @x[2], @x[2], #8
406         veor    @x[7], @t[1], @t[5]
407         veor    @x[2], @t[0], @t[4]
408
409         veor    @x[4], @x[4], @t[3]
410         veor    @x[5], @x[5], @t[7]
411         veor    @x[3], @x[3], @t[6]
412          @ vmov @x[2], @t[0]
413         veor    @x[6], @x[6], @t[2]
414          @ vmov @x[7], @t[1]
415 ___
416 }
417
418 sub InvMixColumns {
419 my @x=@_[0..7];
420 my @t=@_[8..15];
421
422 $code.=<<___;
423         @ multiplication by 0x0e
424         vext.8  @t[7], @x[7], @x[7], #12
425         vmov    @t[2], @x[2]
426         veor    @x[2], @x[2], @x[5]             @ 2 5
427         veor    @x[7], @x[7], @x[5]             @ 7 5
428         vext.8  @t[0], @x[0], @x[0], #12
429         vmov    @t[5], @x[5]
430         veor    @x[5], @x[5], @x[0]             @ 5 0           [1]
431         veor    @x[0], @x[0], @x[1]             @ 0 1
432         vext.8  @t[1], @x[1], @x[1], #12
433         veor    @x[1], @x[1], @x[2]             @ 1 25
434         veor    @x[0], @x[0], @x[6]             @ 01 6          [2]
435         vext.8  @t[3], @x[3], @x[3], #12
436         veor    @x[1], @x[1], @x[3]             @ 125 3         [4]
437         veor    @x[2], @x[2], @x[0]             @ 25 016        [3]
438         veor    @x[3], @x[3], @x[7]             @ 3 75
439         veor    @x[7], @x[7], @x[6]             @ 75 6          [0]
440         vext.8  @t[6], @x[6], @x[6], #12
441         vmov    @t[4], @x[4]
442         veor    @x[6], @x[6], @x[4]             @ 6 4
443         veor    @x[4], @x[4], @x[3]             @ 4 375         [6]
444         veor    @x[3], @x[3], @x[7]             @ 375 756=36
445         veor    @x[6], @x[6], @t[5]             @ 64 5          [7]
446         veor    @x[3], @x[3], @t[2]             @ 36 2
447         vext.8  @t[5], @t[5], @t[5], #12
448         veor    @x[3], @x[3], @t[4]             @ 362 4         [5]
449 ___
450                                         my @y = @x[7,5,0,2,1,3,4,6];
451 $code.=<<___;
452         @ multiplication by 0x0b
453         veor    @y[1], @y[1], @y[0]
454         veor    @y[0], @y[0], @t[0]
455         vext.8  @t[2], @t[2], @t[2], #12
456         veor    @y[1], @y[1], @t[1]
457         veor    @y[0], @y[0], @t[5]
458         vext.8  @t[4], @t[4], @t[4], #12
459         veor    @y[1], @y[1], @t[6]
460         veor    @y[0], @y[0], @t[7]
461         veor    @t[7], @t[7], @t[6]             @ clobber t[7]
462
463         veor    @y[3], @y[3], @t[0]
464          veor   @y[1], @y[1], @y[0]
465         vext.8  @t[0], @t[0], @t[0], #12
466         veor    @y[2], @y[2], @t[1]
467         veor    @y[4], @y[4], @t[1]
468         vext.8  @t[1], @t[1], @t[1], #12
469         veor    @y[2], @y[2], @t[2]
470         veor    @y[3], @y[3], @t[2]
471         veor    @y[5], @y[5], @t[2]
472         veor    @y[2], @y[2], @t[7]
473         vext.8  @t[2], @t[2], @t[2], #12
474         veor    @y[3], @y[3], @t[3]
475         veor    @y[6], @y[6], @t[3]
476         veor    @y[4], @y[4], @t[3]
477         veor    @y[7], @y[7], @t[4]
478         vext.8  @t[3], @t[3], @t[3], #12
479         veor    @y[5], @y[5], @t[4]
480         veor    @y[7], @y[7], @t[7]
481         veor    @t[7], @t[7], @t[5]             @ clobber t[7] even more
482         veor    @y[3], @y[3], @t[5]
483         veor    @y[4], @y[4], @t[4]
484
485         veor    @y[5], @y[5], @t[7]
486         vext.8  @t[4], @t[4], @t[4], #12
487         veor    @y[6], @y[6], @t[7]
488         veor    @y[4], @y[4], @t[7]
489
490         veor    @t[7], @t[7], @t[5]
491         vext.8  @t[5], @t[5], @t[5], #12
492
493         @ multiplication by 0x0d
494         veor    @y[4], @y[4], @y[7]
495          veor   @t[7], @t[7], @t[6]             @ restore t[7]
496         veor    @y[7], @y[7], @t[4]
497         vext.8  @t[6], @t[6], @t[6], #12
498         veor    @y[2], @y[2], @t[0]
499         veor    @y[7], @y[7], @t[5]
500         vext.8  @t[7], @t[7], @t[7], #12
501         veor    @y[2], @y[2], @t[2]
502
503         veor    @y[3], @y[3], @y[1]
504         veor    @y[1], @y[1], @t[1]
505         veor    @y[0], @y[0], @t[0]
506         veor    @y[3], @y[3], @t[0]
507         veor    @y[1], @y[1], @t[5]
508         veor    @y[0], @y[0], @t[5]
509         vext.8  @t[0], @t[0], @t[0], #12
510         veor    @y[1], @y[1], @t[7]
511         veor    @y[0], @y[0], @t[6]
512         veor    @y[3], @y[3], @y[1]
513         veor    @y[4], @y[4], @t[1]
514         vext.8  @t[1], @t[1], @t[1], #12
515
516         veor    @y[7], @y[7], @t[7]
517         veor    @y[4], @y[4], @t[2]
518         veor    @y[5], @y[5], @t[2]
519         veor    @y[2], @y[2], @t[6]
520         veor    @t[6], @t[6], @t[3]             @ clobber t[6]
521         vext.8  @t[2], @t[2], @t[2], #12
522         veor    @y[4], @y[4], @y[7]
523         veor    @y[3], @y[3], @t[6]
524
525         veor    @y[6], @y[6], @t[6]
526         veor    @y[5], @y[5], @t[5]
527         vext.8  @t[5], @t[5], @t[5], #12
528         veor    @y[6], @y[6], @t[4]
529         vext.8  @t[4], @t[4], @t[4], #12
530         veor    @y[5], @y[5], @t[6]
531         veor    @y[6], @y[6], @t[7]
532         vext.8  @t[7], @t[7], @t[7], #12
533         veor    @t[6], @t[6], @t[3]             @ restore t[6]
534         vext.8  @t[3], @t[3], @t[3], #12
535
536         @ multiplication by 0x09
537         veor    @y[4], @y[4], @y[1]
538         veor    @t[1], @t[1], @y[1]             @ t[1]=y[1]
539         veor    @t[0], @t[0], @t[5]             @ clobber t[0]
540         vext.8  @t[6], @t[6], @t[6], #12
541         veor    @t[1], @t[1], @t[5]
542         veor    @y[3], @y[3], @t[0]
543         veor    @t[0], @t[0], @y[0]             @ t[0]=y[0]
544         veor    @t[1], @t[1], @t[6]
545         veor    @t[6], @t[6], @t[7]             @ clobber t[6]
546         veor    @y[4], @y[4], @t[1]
547         veor    @y[7], @y[7], @t[4]
548         veor    @y[6], @y[6], @t[3]
549         veor    @y[5], @y[5], @t[2]
550         veor    @t[4], @t[4], @y[4]             @ t[4]=y[4]
551         veor    @t[3], @t[3], @y[3]             @ t[3]=y[3]
552         veor    @t[5], @t[5], @y[5]             @ t[5]=y[5]
553         veor    @t[2], @t[2], @y[2]             @ t[2]=y[2]
554         veor    @t[3], @t[3], @t[7]
555         veor    @XMM[5], @t[5], @t[6]
556         veor    @XMM[6], @t[6], @y[6]           @ t[6]=y[6]
557         veor    @XMM[2], @t[2], @t[6]
558         veor    @XMM[7], @t[7], @y[7]           @ t[7]=y[7]
559
560         vmov    @XMM[0], @t[0]
561         vmov    @XMM[1], @t[1]
562         @ vmov  @XMM[2], @t[2]
563         vmov    @XMM[3], @t[3]
564         vmov    @XMM[4], @t[4]
565         @ vmov  @XMM[5], @t[5]
566         @ vmov  @XMM[6], @t[6]
567         @ vmov  @XMM[7], @t[7]
568 ___
569 }
570
571 sub swapmove {
572 my ($a,$b,$n,$mask,$t)=@_;
573 $code.=<<___;
574         vshr.u64        $t, $b, #$n
575         veor            $t, $t, $a
576         vand            $t, $t, $mask
577         veor            $a, $a, $t
578         vshl.u64        $t, $t, #$n
579         veor            $b, $b, $t
580 ___
581 }
582 sub swapmove2x {
583 my ($a0,$b0,$a1,$b1,$n,$mask,$t0,$t1)=@_;
584 $code.=<<___;
585         vshr.u64        $t0, $b0, #$n
586          vshr.u64       $t1, $b1, #$n
587         veor            $t0, $t0, $a0
588          veor           $t1, $t1, $a1
589         vand            $t0, $t0, $mask
590          vand           $t1, $t1, $mask
591         veor            $a0, $a0, $t0
592         vshl.u64        $t0, $t0, #$n
593          veor           $a1, $a1, $t1
594          vshl.u64       $t1, $t1, #$n
595         veor            $b0, $b0, $t0
596          veor           $b1, $b1, $t1
597 ___
598 }
599
600 sub bitslice {
601 my @x=reverse(@_[0..7]);
602 my ($t0,$t1,$t2,$t3)=@_[8..11];
603 $code.=<<___;
604         vmov.i8 $t0,#0x55                       @ compose .LBS0
605         vmov.i8 $t1,#0x33                       @ compose .LBS1
606 ___
607         &swapmove2x(@x[0,1,2,3],1,$t0,$t2,$t3);
608         &swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
609 $code.=<<___;
610         vmov.i8 $t0,#0x0f                       @ compose .LBS2
611 ___
612         &swapmove2x(@x[0,2,1,3],2,$t1,$t2,$t3);
613         &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
614
615         &swapmove2x(@x[0,4,1,5],4,$t0,$t2,$t3);
616         &swapmove2x(@x[2,6,3,7],4,$t0,$t2,$t3);
617 }
618
619 $code.=<<___;
620 .text
621 .code   32
622 .fpu    neon
623
624 .type   _bsaes_decrypt8,%function
625 .align  4
626 _bsaes_decrypt8:
627         sub     $const,pc,#8                    @ _bsaes_decrypt8
628         vldmia  $key!, {@XMM[9]}                @ round 0 key
629         add     $const,$const,#.LM0ISR-_bsaes_decrypt8
630
631         vldmia  $const!, {@XMM[8]}              @ .LM0ISR
632         veor    @XMM[10], @XMM[0], @XMM[9]      @ xor with round0 key
633         veor    @XMM[11], @XMM[1], @XMM[9]
634          vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
635          vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
636         veor    @XMM[12], @XMM[2], @XMM[9]
637          vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
638          vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
639         veor    @XMM[13], @XMM[3], @XMM[9]
640          vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
641          vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
642         veor    @XMM[14], @XMM[4], @XMM[9]
643          vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
644          vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
645         veor    @XMM[15], @XMM[5], @XMM[9]
646          vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
647          vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
648         veor    @XMM[10], @XMM[6], @XMM[9]
649          vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
650          vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
651         veor    @XMM[11], @XMM[7], @XMM[9]
652          vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
653          vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
654          vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
655          vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
656 ___
657         &bitslice       (@XMM[0..7, 8..11]);
658 $code.=<<___;
659         sub     $rounds,$rounds,#1
660         b       .Ldec_sbox
661 .align  4
662 .Ldec_loop:
663 ___
664         &ShiftRows      (@XMM[0..7, 8..12]);
665 $code.=".Ldec_sbox:\n";
666         &InvSbox        (@XMM[0..7, 8..15]);
667 $code.=<<___;
668         subs    $rounds,$rounds,#1
669         bcc     .Ldec_done
670 ___
671         &InvMixColumns  (@XMM[0,1,6,4,2,7,3,5, 8..15]);
672 $code.=<<___;
673         vldmia  $const, {@XMM[12]}              @ .LISR
674         addeq   $const,$const,#0x10
675         bne     .Ldec_loop
676         vldmia  $const, {@XMM[12]}              @ .LISRM0
677         b       .Ldec_loop
678 .align  4
679 .Ldec_done:
680 ___
681         &bitslice       (@XMM[0,1,6,4,2,7,3,5, 8..11]);
682 $code.=<<___;
683         vldmia  $key, {@XMM[8]}                 @ last round key
684         veor    @XMM[6], @XMM[6], @XMM[8]
685         veor    @XMM[4], @XMM[4], @XMM[8]
686         veor    @XMM[2], @XMM[2], @XMM[8]
687         veor    @XMM[7], @XMM[7], @XMM[8]
688         veor    @XMM[3], @XMM[3], @XMM[8]
689         veor    @XMM[5], @XMM[5], @XMM[8]
690         veor    @XMM[0], @XMM[0], @XMM[8]
691         veor    @XMM[1], @XMM[1], @XMM[8]
692         bx      lr
693 .size   _bsaes_decrypt8,.-_bsaes_decrypt8
694
695 .type   _bsaes_const,%object
696 .align  6
697 _bsaes_const:
698 .LM0ISR:        @ InvShiftRows constants
699         .quad   0x0a0e0206070b0f03, 0x0004080c0d010509
700 .LISR:
701         .quad   0x0504070602010003, 0x0f0e0d0c080b0a09
702 .LISRM0:
703         .quad   0x01040b0e0205080f, 0x0306090c00070a0d
704 .LM0SR:         @ ShiftRows constants
705         .quad   0x0a0e02060f03070b, 0x0004080c05090d01
706 .LSR:
707         .quad   0x0504070600030201, 0x0f0e0d0c0a09080b
708 .LSRM0:
709         .quad   0x0304090e00050a0f, 0x01060b0c0207080d
710 .LM0:
711         .quad   0x02060a0e03070b0f, 0x0004080c0105090d
712 .asciz  "Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
713 .align  6
714 .size   _bsaes_const,.-_bsaes_const
715
716 .type   _bsaes_encrypt8,%function
717 .align  4
718 _bsaes_encrypt8:
719         sub     $const,pc,#8                    @ _bsaes_encrypt8
720         vldmia  $key!, {@XMM[9]}                @ round 0 key
721         sub     $const,$const,#_bsaes_encrypt8-.LM0SR
722
723         vldmia  $const!, {@XMM[8]}              @ .LM0SR
724         veor    @XMM[10], @XMM[0], @XMM[9]      @ xor with round0 key
725         veor    @XMM[11], @XMM[1], @XMM[9]
726          vtbl.8 `&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
727          vtbl.8 `&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
728         veor    @XMM[12], @XMM[2], @XMM[9]
729          vtbl.8 `&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
730          vtbl.8 `&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
731         veor    @XMM[13], @XMM[3], @XMM[9]
732          vtbl.8 `&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
733          vtbl.8 `&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
734         veor    @XMM[14], @XMM[4], @XMM[9]
735          vtbl.8 `&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
736          vtbl.8 `&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
737         veor    @XMM[15], @XMM[5], @XMM[9]
738          vtbl.8 `&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
739          vtbl.8 `&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
740         veor    @XMM[10], @XMM[6], @XMM[9]
741          vtbl.8 `&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
742          vtbl.8 `&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
743         veor    @XMM[11], @XMM[7], @XMM[9]
744          vtbl.8 `&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
745          vtbl.8 `&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
746          vtbl.8 `&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
747          vtbl.8 `&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
748 _bsaes_encrypt8_bitslice:
749 ___
750         &bitslice       (@XMM[0..7, 8..11]);
751 $code.=<<___;
752         sub     $rounds,$rounds,#1
753         b       .Lenc_sbox
754 .align  4
755 .Lenc_loop:
756 ___
757         &ShiftRows      (@XMM[0..7, 8..12]);
758 $code.=".Lenc_sbox:\n";
759         &Sbox           (@XMM[0..7, 8..15]);
760 $code.=<<___;
761         subs    $rounds,$rounds,#1
762         bcc     .Lenc_done
763 ___
764         &MixColumns     (@XMM[0,1,4,6,3,7,2,5, 8..15]);
765 $code.=<<___;
766         vldmia  $const, {@XMM[12]}              @ .LSR
767         addeq   $const,$const,#0x10
768         bne     .Lenc_loop
769         vldmia  $const, {@XMM[12]}              @ .LSRM0
770         b       .Lenc_loop
771 .align  4
772 .Lenc_done:
773 ___
774         # output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
775         &bitslice       (@XMM[0,1,4,6,3,7,2,5, 8..11]);
776 $code.=<<___;
777         vldmia  $key, {@XMM[8]}                 @ last round key
778         veor    @XMM[4], @XMM[4], @XMM[8]
779         veor    @XMM[6], @XMM[6], @XMM[8]
780         veor    @XMM[3], @XMM[3], @XMM[8]
781         veor    @XMM[7], @XMM[7], @XMM[8]
782         veor    @XMM[2], @XMM[2], @XMM[8]
783         veor    @XMM[5], @XMM[5], @XMM[8]
784         veor    @XMM[0], @XMM[0], @XMM[8]
785         veor    @XMM[1], @XMM[1], @XMM[8]
786         bx      lr
787 .size   _bsaes_encrypt8,.-_bsaes_encrypt8
788 ___
789 }
790 {
791 my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
792
793 sub bitslice_key {
794 my @x=reverse(@_[0..7]);
795 my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
796
797         &swapmove       (@x[0,1],1,$bs0,$t2,$t3);
798 $code.=<<___;
799         @ &swapmove(@x[2,3],1,$t0,$t2,$t3);
800         vmov    @x[2], @x[0]
801         vmov    @x[3], @x[1]
802 ___
803         #&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
804
805         &swapmove2x     (@x[0,2,1,3],2,$bs1,$t2,$t3);
806 $code.=<<___;
807         @ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
808         vmov    @x[4], @x[0]
809         vmov    @x[6], @x[2]
810         vmov    @x[5], @x[1]
811         vmov    @x[7], @x[3]
812 ___
813         &swapmove2x     (@x[0,4,1,5],4,$bs2,$t2,$t3);
814         &swapmove2x     (@x[2,6,3,7],4,$bs2,$t2,$t3);
815 }
816
817 $code.=<<___;
818 .type   _bsaes_key_convert,%function
819 .align  4
820 _bsaes_key_convert:
821         sub     $const,pc,#8                    @ _bsaes_key_convert
822         vld1.8  {@XMM[7]},  [$inp]!             @ load round 0 key
823         sub     $const,$const,#_bsaes_key_convert-.LM0
824         vld1.8  {@XMM[15]}, [$inp]!             @ load round 1 key
825
826         vmov.i8 @XMM[8],  #0x01                 @ bit masks
827         vmov.i8 @XMM[9],  #0x02
828         vmov.i8 @XMM[10], #0x04
829         vmov.i8 @XMM[11], #0x08
830         vmov.i8 @XMM[12], #0x10
831         vmov.i8 @XMM[13], #0x20
832         vldmia  $const, {@XMM[14]}              @ .LM0
833
834 #ifdef __ARMEL__
835         vrev32.8        @XMM[7],  @XMM[7]
836         vrev32.8        @XMM[15], @XMM[15]
837 #endif
838         sub     $rounds,$rounds,#1
839         vstmia  $out!, {@XMM[7]}                @ save round 0 key
840         b       .Lkey_loop
841
842 .align  4
843 .Lkey_loop:
844         vtbl.8  `&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
845         vtbl.8  `&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
846         vmov.i8 @XMM[6],  #0x40
847         vmov.i8 @XMM[15], #0x80
848
849         vtst.8  @XMM[0], @XMM[7], @XMM[8]
850         vtst.8  @XMM[1], @XMM[7], @XMM[9]
851         vtst.8  @XMM[2], @XMM[7], @XMM[10]
852         vtst.8  @XMM[3], @XMM[7], @XMM[11]
853         vtst.8  @XMM[4], @XMM[7], @XMM[12]
854         vtst.8  @XMM[5], @XMM[7], @XMM[13]
855         vtst.8  @XMM[6], @XMM[7], @XMM[6]
856         vtst.8  @XMM[7], @XMM[7], @XMM[15]
857         vld1.8  {@XMM[15]}, [$inp]!             @ load next round key
858         vmvn    @XMM[0], @XMM[0]                @ "pnot"
859         vmvn    @XMM[1], @XMM[1]
860         vmvn    @XMM[5], @XMM[5]
861         vmvn    @XMM[6], @XMM[6]
862 #ifdef __ARMEL__
863         vrev32.8        @XMM[15], @XMM[15]
864 #endif
865         subs    $rounds,$rounds,#1
866         vstmia  $out!,{@XMM[0]-@XMM[7]}         @ write bit-sliced round key
867         bne     .Lkey_loop
868
869         vmov.i8 @XMM[7],#0x63                   @ compose .L63
870         @ don't save last round key
871         bx      lr
872 .size   _bsaes_key_convert,.-_bsaes_key_convert
873 ___
874 }
875
876 if (1) {                # following four functions are unsupported interface
877                         # used for benchmarking...
878 $code.=<<___;
879 .globl  bsaes_enc_key_convert
880 .type   bsaes_enc_key_convert,%function
881 .align  4
882 bsaes_enc_key_convert:
883         stmdb   sp!,{r4-r6,lr}
884         vstmdb  sp!,{d8-d15}            @ ABI specification says so
885
886         ldr     r5,[$inp,#240]                  @ pass rounds
887         mov     r4,$inp                         @ pass key
888         mov     r12,$out                        @ pass key schedule
889         bl      _bsaes_key_convert
890         veor    @XMM[7],@XMM[7],@XMM[15]        @ fix up last round key
891         vstmia  r12, {@XMM[7]}                  @ save last round key
892
893         vldmia  sp!,{d8-d15}
894         ldmia   sp!,{r4-r6,pc}
895 .size   bsaes_enc_key_convert,.-bsaes_enc_key_convert
896
897 .globl  bsaes_encrypt_128
898 .type   bsaes_encrypt_128,%function
899 .align  4
900 bsaes_encrypt_128:
901         stmdb   sp!,{r4-r6,lr}
902         vstmdb  sp!,{d8-d15}            @ ABI specification says so
903 .Lenc128_loop:
904         vld1.8  {@XMM[0]-@XMM[1]}, [$inp]!      @ load input
905         vld1.8  {@XMM[2]-@XMM[3]}, [$inp]!
906         mov     r4,$key                         @ pass the key
907         vld1.8  {@XMM[4]-@XMM[5]}, [$inp]!
908         mov     r5,#10                          @ pass rounds
909         vld1.8  {@XMM[6]-@XMM[7]}, [$inp]!
910
911         bl      _bsaes_encrypt8
912
913         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
914         vst1.8  {@XMM[4]}, [$out]!
915         vst1.8  {@XMM[6]}, [$out]!
916         vst1.8  {@XMM[3]}, [$out]!
917         vst1.8  {@XMM[7]}, [$out]!
918         vst1.8  {@XMM[2]}, [$out]!
919         subs    $len,$len,#0x80
920         vst1.8  {@XMM[5]}, [$out]!
921         bhi     .Lenc128_loop
922
923         vldmia  sp!,{d8-d15}
924         ldmia   sp!,{r4-r6,pc}
925 .size   bsaes_encrypt_128,.-bsaes_encrypt_128
926
927 .globl  bsaes_dec_key_convert
928 .type   bsaes_dec_key_convert,%function
929 .align  4
930 bsaes_dec_key_convert:
931         stmdb   sp!,{r4-r6,lr}
932         vstmdb  sp!,{d8-d15}            @ ABI specification says so
933
934         ldr     r5,[$inp,#240]                  @ pass rounds
935         mov     r4,$inp                         @ pass key
936         mov     r12,$out                        @ pass key schedule
937         bl      _bsaes_key_convert
938         vldmia  $out, {@XMM[6]}
939         vstmia  r12,  {@XMM[15]}                @ save last round key
940         veor    @XMM[7], @XMM[7], @XMM[6]       @ fix up round 0 key
941         vstmia  $out, {@XMM[7]}
942
943         vldmia  sp!,{d8-d15}
944         ldmia   sp!,{r4-r6,pc}
945 .size   bsaes_dec_key_convert,.-bsaes_dec_key_convert
946
947 .globl  bsaes_decrypt_128
948 .type   bsaes_decrypt_128,%function
949 .align  4
950 bsaes_decrypt_128:
951         stmdb   sp!,{r4-r6,lr}
952         vstmdb  sp!,{d8-d15}            @ ABI specification says so
953 .Ldec128_loop:
954         vld1.8  {@XMM[0]-@XMM[1]}, [$inp]!      @ load input
955         vld1.8  {@XMM[2]-@XMM[3]}, [$inp]!
956         mov     r4,$key                         @ pass the key
957         vld1.8  {@XMM[4]-@XMM[5]}, [$inp]!
958         mov     r5,#10                          @ pass rounds
959         vld1.8  {@XMM[6]-@XMM[7]}, [$inp]!
960
961         bl      _bsaes_decrypt8
962
963         vst1.8  {@XMM[0]-@XMM[1]}, [$out]!      @ write output
964         vst1.8  {@XMM[6]}, [$out]!
965         vst1.8  {@XMM[4]}, [$out]!
966         vst1.8  {@XMM[2]}, [$out]!
967         vst1.8  {@XMM[7]}, [$out]!
968         vst1.8  {@XMM[3]}, [$out]!
969         subs    $len,$len,#0x80
970         vst1.8  {@XMM[5]}, [$out]!
971         bhi     .Ldec128_loop
972
973         vldmia  sp!,{d8-d15}
974         ldmia   sp!,{r4-r6,pc}
975 .size   bsaes_decrypt_128,.-bsaes_decrypt_128
976 ___
977 }
978
979 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
980
981 print $code;
982
983 close STDOUT;