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