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