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