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 <appro@openssl.org> 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#						<appro@openssl.org>
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_ARCH__ __LINUX_ARM_ARCH__
722# define __ARM_MAX_ARCH__ 7
723#endif
724
725#ifdef __thumb__
726# define adrl adr
727#endif
728
729#if __ARM_MAX_ARCH__>=7
730.arch	armv7-a
731.fpu	neon
732
733.text
734.syntax	unified 	@ ARMv7-capable assembler is expected to handle this
735#if defined(__thumb2__) && !defined(__APPLE__)
736.thumb
737#else
738.code   32
739# undef __thumb2__
740#endif
741
742.type	_bsaes_decrypt8,%function
743.align	4
744_bsaes_decrypt8:
745	adr	$const,.
746	vldmia	$key!, {@XMM[9]}		@ round 0 key
747#if defined(__thumb2__) || defined(__APPLE__)
748	adr	$const,.LM0ISR
749#else
750	add	$const,$const,#.LM0ISR-_bsaes_decrypt8
751#endif
752
753	vldmia	$const!, {@XMM[8]}		@ .LM0ISR
754	veor	@XMM[10], @XMM[0], @XMM[9]	@ xor with round0 key
755	veor	@XMM[11], @XMM[1], @XMM[9]
756	 vtbl.8	`&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
757	 vtbl.8	`&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
758	veor	@XMM[12], @XMM[2], @XMM[9]
759	 vtbl.8	`&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
760	 vtbl.8	`&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
761	veor	@XMM[13], @XMM[3], @XMM[9]
762	 vtbl.8	`&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
763	 vtbl.8	`&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
764	veor	@XMM[14], @XMM[4], @XMM[9]
765	 vtbl.8	`&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
766	 vtbl.8	`&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
767	veor	@XMM[15], @XMM[5], @XMM[9]
768	 vtbl.8	`&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
769	 vtbl.8	`&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
770	veor	@XMM[10], @XMM[6], @XMM[9]
771	 vtbl.8	`&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
772	 vtbl.8	`&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
773	veor	@XMM[11], @XMM[7], @XMM[9]
774	 vtbl.8	`&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
775	 vtbl.8	`&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
776	 vtbl.8	`&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
777	 vtbl.8	`&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
778___
779	&bitslice	(@XMM[0..7, 8..11]);
780$code.=<<___;
781	sub	$rounds,$rounds,#1
782	b	.Ldec_sbox
783.align	4
784.Ldec_loop:
785___
786	&ShiftRows	(@XMM[0..7, 8..12]);
787$code.=".Ldec_sbox:\n";
788	&InvSbox	(@XMM[0..7, 8..15]);
789$code.=<<___;
790	subs	$rounds,$rounds,#1
791	bcc	.Ldec_done
792___
793	&InvMixColumns	(@XMM[0,1,6,4,2,7,3,5, 8..15]);
794$code.=<<___;
795	vldmia	$const, {@XMM[12]}		@ .LISR
796	ite	eq				@ Thumb2 thing, sanity check in ARM
797	addeq	$const,$const,#0x10
798	bne	.Ldec_loop
799	vldmia	$const, {@XMM[12]}		@ .LISRM0
800	b	.Ldec_loop
801.align	4
802.Ldec_done:
803___
804	&bitslice	(@XMM[0,1,6,4,2,7,3,5, 8..11]);
805$code.=<<___;
806	vldmia	$key, {@XMM[8]}			@ last round key
807	veor	@XMM[6], @XMM[6], @XMM[8]
808	veor	@XMM[4], @XMM[4], @XMM[8]
809	veor	@XMM[2], @XMM[2], @XMM[8]
810	veor	@XMM[7], @XMM[7], @XMM[8]
811	veor	@XMM[3], @XMM[3], @XMM[8]
812	veor	@XMM[5], @XMM[5], @XMM[8]
813	veor	@XMM[0], @XMM[0], @XMM[8]
814	veor	@XMM[1], @XMM[1], @XMM[8]
815	bx	lr
816.size	_bsaes_decrypt8,.-_bsaes_decrypt8
817
818.type	_bsaes_const,%object
819.align	6
820_bsaes_const:
821.LM0ISR:	@ InvShiftRows constants
822	.quad	0x0a0e0206070b0f03, 0x0004080c0d010509
823.LISR:
824	.quad	0x0504070602010003, 0x0f0e0d0c080b0a09
825.LISRM0:
826	.quad	0x01040b0e0205080f, 0x0306090c00070a0d
827.LM0SR:		@ ShiftRows constants
828	.quad	0x0a0e02060f03070b, 0x0004080c05090d01
829.LSR:
830	.quad	0x0504070600030201, 0x0f0e0d0c0a09080b
831.LSRM0:
832	.quad	0x0304090e00050a0f, 0x01060b0c0207080d
833.LM0:
834	.quad	0x02060a0e03070b0f, 0x0004080c0105090d
835.LREVM0SR:
836	.quad	0x090d01050c000408, 0x03070b0f060a0e02
837.asciz	"Bit-sliced AES for NEON, CRYPTOGAMS by <appro\@openssl.org>"
838.align	6
839.size	_bsaes_const,.-_bsaes_const
840
841.type	_bsaes_encrypt8,%function
842.align	4
843_bsaes_encrypt8:
844	adr	$const,.
845	vldmia	$key!, {@XMM[9]}		@ round 0 key
846#if defined(__thumb2__) || defined(__APPLE__)
847	adr	$const,.LM0SR
848#else
849	sub	$const,$const,#_bsaes_encrypt8-.LM0SR
850#endif
851
852	vldmia	$const!, {@XMM[8]}		@ .LM0SR
853_bsaes_encrypt8_alt:
854	veor	@XMM[10], @XMM[0], @XMM[9]	@ xor with round0 key
855	veor	@XMM[11], @XMM[1], @XMM[9]
856	 vtbl.8	`&Dlo(@XMM[0])`, {@XMM[10]}, `&Dlo(@XMM[8])`
857	 vtbl.8	`&Dhi(@XMM[0])`, {@XMM[10]}, `&Dhi(@XMM[8])`
858	veor	@XMM[12], @XMM[2], @XMM[9]
859	 vtbl.8	`&Dlo(@XMM[1])`, {@XMM[11]}, `&Dlo(@XMM[8])`
860	 vtbl.8	`&Dhi(@XMM[1])`, {@XMM[11]}, `&Dhi(@XMM[8])`
861	veor	@XMM[13], @XMM[3], @XMM[9]
862	 vtbl.8	`&Dlo(@XMM[2])`, {@XMM[12]}, `&Dlo(@XMM[8])`
863	 vtbl.8	`&Dhi(@XMM[2])`, {@XMM[12]}, `&Dhi(@XMM[8])`
864	veor	@XMM[14], @XMM[4], @XMM[9]
865	 vtbl.8	`&Dlo(@XMM[3])`, {@XMM[13]}, `&Dlo(@XMM[8])`
866	 vtbl.8	`&Dhi(@XMM[3])`, {@XMM[13]}, `&Dhi(@XMM[8])`
867	veor	@XMM[15], @XMM[5], @XMM[9]
868	 vtbl.8	`&Dlo(@XMM[4])`, {@XMM[14]}, `&Dlo(@XMM[8])`
869	 vtbl.8	`&Dhi(@XMM[4])`, {@XMM[14]}, `&Dhi(@XMM[8])`
870	veor	@XMM[10], @XMM[6], @XMM[9]
871	 vtbl.8	`&Dlo(@XMM[5])`, {@XMM[15]}, `&Dlo(@XMM[8])`
872	 vtbl.8	`&Dhi(@XMM[5])`, {@XMM[15]}, `&Dhi(@XMM[8])`
873	veor	@XMM[11], @XMM[7], @XMM[9]
874	 vtbl.8	`&Dlo(@XMM[6])`, {@XMM[10]}, `&Dlo(@XMM[8])`
875	 vtbl.8	`&Dhi(@XMM[6])`, {@XMM[10]}, `&Dhi(@XMM[8])`
876	 vtbl.8	`&Dlo(@XMM[7])`, {@XMM[11]}, `&Dlo(@XMM[8])`
877	 vtbl.8	`&Dhi(@XMM[7])`, {@XMM[11]}, `&Dhi(@XMM[8])`
878_bsaes_encrypt8_bitslice:
879___
880	&bitslice	(@XMM[0..7, 8..11]);
881$code.=<<___;
882	sub	$rounds,$rounds,#1
883	b	.Lenc_sbox
884.align	4
885.Lenc_loop:
886___
887	&ShiftRows	(@XMM[0..7, 8..12]);
888$code.=".Lenc_sbox:\n";
889	&Sbox		(@XMM[0..7, 8..15]);
890$code.=<<___;
891	subs	$rounds,$rounds,#1
892	bcc	.Lenc_done
893___
894	&MixColumns	(@XMM[0,1,4,6,3,7,2,5, 8..15]);
895$code.=<<___;
896	vldmia	$const, {@XMM[12]}		@ .LSR
897	ite	eq				@ Thumb2 thing, samity check in ARM
898	addeq	$const,$const,#0x10
899	bne	.Lenc_loop
900	vldmia	$const, {@XMM[12]}		@ .LSRM0
901	b	.Lenc_loop
902.align	4
903.Lenc_done:
904___
905	# output in lsb > [t0, t1, t4, t6, t3, t7, t2, t5] < msb
906	&bitslice	(@XMM[0,1,4,6,3,7,2,5, 8..11]);
907$code.=<<___;
908	vldmia	$key, {@XMM[8]}			@ last round key
909	veor	@XMM[4], @XMM[4], @XMM[8]
910	veor	@XMM[6], @XMM[6], @XMM[8]
911	veor	@XMM[3], @XMM[3], @XMM[8]
912	veor	@XMM[7], @XMM[7], @XMM[8]
913	veor	@XMM[2], @XMM[2], @XMM[8]
914	veor	@XMM[5], @XMM[5], @XMM[8]
915	veor	@XMM[0], @XMM[0], @XMM[8]
916	veor	@XMM[1], @XMM[1], @XMM[8]
917	bx	lr
918.size	_bsaes_encrypt8,.-_bsaes_encrypt8
919___
920}
921{
922my ($out,$inp,$rounds,$const)=("r12","r4","r5","r6");
923
924sub bitslice_key {
925my @x=reverse(@_[0..7]);
926my ($bs0,$bs1,$bs2,$t2,$t3)=@_[8..12];
927
928	&swapmove	(@x[0,1],1,$bs0,$t2,$t3);
929$code.=<<___;
930	@ &swapmove(@x[2,3],1,$t0,$t2,$t3);
931	vmov	@x[2], @x[0]
932	vmov	@x[3], @x[1]
933___
934	#&swapmove2x(@x[4,5,6,7],1,$t0,$t2,$t3);
935
936	&swapmove2x	(@x[0,2,1,3],2,$bs1,$t2,$t3);
937$code.=<<___;
938	@ &swapmove2x(@x[4,6,5,7],2,$t1,$t2,$t3);
939	vmov	@x[4], @x[0]
940	vmov	@x[6], @x[2]
941	vmov	@x[5], @x[1]
942	vmov	@x[7], @x[3]
943___
944	&swapmove2x	(@x[0,4,1,5],4,$bs2,$t2,$t3);
945	&swapmove2x	(@x[2,6,3,7],4,$bs2,$t2,$t3);
946}
947
948$code.=<<___;
949.type	_bsaes_key_convert,%function
950.align	4
951_bsaes_key_convert:
952	adr	$const,.
953	vld1.8	{@XMM[7]},  [$inp]!		@ load round 0 key
954#if defined(__thumb2__) || defined(__APPLE__)
955	adr	$const,.LM0
956#else
957	sub	$const,$const,#_bsaes_key_convert-.LM0
958#endif
959	vld1.8	{@XMM[15]}, [$inp]!		@ load round 1 key
960
961	vmov.i8	@XMM[8],  #0x01			@ bit masks
962	vmov.i8	@XMM[9],  #0x02
963	vmov.i8	@XMM[10], #0x04
964	vmov.i8	@XMM[11], #0x08
965	vmov.i8	@XMM[12], #0x10
966	vmov.i8	@XMM[13], #0x20
967	vldmia	$const, {@XMM[14]}		@ .LM0
968
969#ifdef __ARMEL__
970	vrev32.8	@XMM[7],  @XMM[7]
971	vrev32.8	@XMM[15], @XMM[15]
972#endif
973	sub	$rounds,$rounds,#1
974	vstmia	$out!, {@XMM[7]}		@ save round 0 key
975	b	.Lkey_loop
976
977.align	4
978.Lkey_loop:
979	vtbl.8	`&Dlo(@XMM[7])`,{@XMM[15]},`&Dlo(@XMM[14])`
980	vtbl.8	`&Dhi(@XMM[7])`,{@XMM[15]},`&Dhi(@XMM[14])`
981	vmov.i8	@XMM[6],  #0x40
982	vmov.i8	@XMM[15], #0x80
983
984	vtst.8	@XMM[0], @XMM[7], @XMM[8]
985	vtst.8	@XMM[1], @XMM[7], @XMM[9]
986	vtst.8	@XMM[2], @XMM[7], @XMM[10]
987	vtst.8	@XMM[3], @XMM[7], @XMM[11]
988	vtst.8	@XMM[4], @XMM[7], @XMM[12]
989	vtst.8	@XMM[5], @XMM[7], @XMM[13]
990	vtst.8	@XMM[6], @XMM[7], @XMM[6]
991	vtst.8	@XMM[7], @XMM[7], @XMM[15]
992	vld1.8	{@XMM[15]}, [$inp]!		@ load next round key
993	vmvn	@XMM[0], @XMM[0]		@ "pnot"
994	vmvn	@XMM[1], @XMM[1]
995	vmvn	@XMM[5], @XMM[5]
996	vmvn	@XMM[6], @XMM[6]
997#ifdef __ARMEL__
998	vrev32.8	@XMM[15], @XMM[15]
999#endif
1000	subs	$rounds,$rounds,#1
1001	vstmia	$out!,{@XMM[0]-@XMM[7]}		@ write bit-sliced round key
1002	bne	.Lkey_loop
1003
1004	vmov.i8	@XMM[7],#0x63			@ compose .L63
1005	@ don't save last round key
1006	bx	lr
1007.size	_bsaes_key_convert,.-_bsaes_key_convert
1008___
1009}
1010
1011if (0) {		# following four functions are unsupported interface
1012			# used for benchmarking...
1013$code.=<<___;
1014.globl	bsaes_enc_key_convert
1015.type	bsaes_enc_key_convert,%function
1016.align	4
1017bsaes_enc_key_convert:
1018	stmdb	sp!,{r4-r6,lr}
1019	vstmdb	sp!,{d8-d15}		@ ABI specification says so
1020
1021	ldr	r5,[$inp,#240]			@ pass rounds
1022	mov	r4,$inp				@ pass key
1023	mov	r12,$out			@ pass key schedule
1024	bl	_bsaes_key_convert
1025	veor	@XMM[7],@XMM[7],@XMM[15]	@ fix up last round key
1026	vstmia	r12, {@XMM[7]}			@ save last round key
1027
1028	vldmia	sp!,{d8-d15}
1029	ldmia	sp!,{r4-r6,pc}
1030.size	bsaes_enc_key_convert,.-bsaes_enc_key_convert
1031
1032.globl	bsaes_encrypt_128
1033.type	bsaes_encrypt_128,%function
1034.align	4
1035bsaes_encrypt_128:
1036	stmdb	sp!,{r4-r6,lr}
1037	vstmdb	sp!,{d8-d15}		@ ABI specification says so
1038.Lenc128_loop:
1039	vld1.8	{@XMM[0]-@XMM[1]}, [$inp]!	@ load input
1040	vld1.8	{@XMM[2]-@XMM[3]}, [$inp]!
1041	mov	r4,$key				@ pass the key
1042	vld1.8	{@XMM[4]-@XMM[5]}, [$inp]!
1043	mov	r5,#10				@ pass rounds
1044	vld1.8	{@XMM[6]-@XMM[7]}, [$inp]!
1045
1046	bl	_bsaes_encrypt8
1047
1048	vst1.8	{@XMM[0]-@XMM[1]}, [$out]!	@ write output
1049	vst1.8	{@XMM[4]}, [$out]!
1050	vst1.8	{@XMM[6]}, [$out]!
1051	vst1.8	{@XMM[3]}, [$out]!
1052	vst1.8	{@XMM[7]}, [$out]!
1053	vst1.8	{@XMM[2]}, [$out]!
1054	subs	$len,$len,#0x80
1055	vst1.8	{@XMM[5]}, [$out]!
1056	bhi	.Lenc128_loop
1057
1058	vldmia	sp!,{d8-d15}
1059	ldmia	sp!,{r4-r6,pc}
1060.size	bsaes_encrypt_128,.-bsaes_encrypt_128
1061
1062.globl	bsaes_dec_key_convert
1063.type	bsaes_dec_key_convert,%function
1064.align	4
1065bsaes_dec_key_convert:
1066	stmdb	sp!,{r4-r6,lr}
1067	vstmdb	sp!,{d8-d15}		@ ABI specification says so
1068
1069	ldr	r5,[$inp,#240]			@ pass rounds
1070	mov	r4,$inp				@ pass key
1071	mov	r12,$out			@ pass key schedule
1072	bl	_bsaes_key_convert
1073	vldmia	$out, {@XMM[6]}
1074	vstmia	r12,  {@XMM[15]}		@ save last round key
1075	veor	@XMM[7], @XMM[7], @XMM[6]	@ fix up round 0 key
1076	vstmia	$out, {@XMM[7]}
1077
1078	vldmia	sp!,{d8-d15}
1079	ldmia	sp!,{r4-r6,pc}
1080.size	bsaes_dec_key_convert,.-bsaes_dec_key_convert
1081
1082.globl	bsaes_decrypt_128
1083.type	bsaes_decrypt_128,%function
1084.align	4
1085bsaes_decrypt_128:
1086	stmdb	sp!,{r4-r6,lr}
1087	vstmdb	sp!,{d8-d15}		@ ABI specification says so
1088.Ldec128_loop:
1089	vld1.8	{@XMM[0]-@XMM[1]}, [$inp]!	@ load input
1090	vld1.8	{@XMM[2]-@XMM[3]}, [$inp]!
1091	mov	r4,$key				@ pass the key
1092	vld1.8	{@XMM[4]-@XMM[5]}, [$inp]!
1093	mov	r5,#10				@ pass rounds
1094	vld1.8	{@XMM[6]-@XMM[7]}, [$inp]!
1095
1096	bl	_bsaes_decrypt8
1097
1098	vst1.8	{@XMM[0]-@XMM[1]}, [$out]!	@ write output
1099	vst1.8	{@XMM[6]}, [$out]!
1100	vst1.8	{@XMM[4]}, [$out]!
1101	vst1.8	{@XMM[2]}, [$out]!
1102	vst1.8	{@XMM[7]}, [$out]!
1103	vst1.8	{@XMM[3]}, [$out]!
1104	subs	$len,$len,#0x80
1105	vst1.8	{@XMM[5]}, [$out]!
1106	bhi	.Ldec128_loop
1107
1108	vldmia	sp!,{d8-d15}
1109	ldmia	sp!,{r4-r6,pc}
1110.size	bsaes_decrypt_128,.-bsaes_decrypt_128
1111___
1112}
1113{
1114my ($inp,$out,$len,$key, $ivp,$fp,$rounds)=map("r$_",(0..3,8..10));
1115my ($keysched)=("sp");
1116
1117$code.=<<___;
1118.global	bsaes_cbc_encrypt
1119.type	bsaes_cbc_encrypt,%function
1120.align	5
1121bsaes_cbc_encrypt:
1122	@ In OpenSSL, this function had a fallback to aes_nohw_cbc_encrypt for
1123	@ short inputs. We patch this out, using bsaes for all input sizes.
1124
1125	@ it is up to the caller to make sure we are called with enc == 0
1126
1127	mov	ip, sp
1128	stmdb	sp!, {r4-r10, lr}
1129	VFP_ABI_PUSH
1130	ldr	$ivp, [ip]			@ IV is 1st arg on the stack
1131	mov	$len, $len, lsr#4		@ len in 16 byte blocks
1132	sub	sp, #0x10			@ scratch space to carry over the IV
1133	mov	$fp, sp				@ save sp
1134
1135	ldr	$rounds, [$key, #240]		@ get # of rounds
1136#ifndef	BSAES_ASM_EXTENDED_KEY
1137	@ allocate the key schedule on the stack
1138	sub	r12, sp, $rounds, lsl#7		@ 128 bytes per inner round key
1139	add	r12, #`128-32`			@ sifze of bit-slices key schedule
1140
1141	@ populate the key schedule
1142	mov	r4, $key			@ pass key
1143	mov	r5, $rounds			@ pass # of rounds
1144	mov	sp, r12				@ sp is $keysched
1145	bl	_bsaes_key_convert
1146	vldmia	$keysched, {@XMM[6]}
1147	vstmia	r12,  {@XMM[15]}		@ save last round key
1148	veor	@XMM[7], @XMM[7], @XMM[6]	@ fix up round 0 key
1149	vstmia	$keysched, {@XMM[7]}
1150#else
1151	ldr	r12, [$key, #244]
1152	eors	r12, #1
1153	beq	0f
1154
1155	@ populate the key schedule
1156	str	r12, [$key, #244]
1157	mov	r4, $key			@ pass key
1158	mov	r5, $rounds			@ pass # of rounds
1159	add	r12, $key, #248			@ pass key schedule
1160	bl	_bsaes_key_convert
1161	add	r4, $key, #248
1162	vldmia	r4, {@XMM[6]}
1163	vstmia	r12, {@XMM[15]}			@ save last round key
1164	veor	@XMM[7], @XMM[7], @XMM[6]	@ fix up round 0 key
1165	vstmia	r4, {@XMM[7]}
1166
1167.align	2
11680:
1169#endif
1170
1171	vld1.8	{@XMM[15]}, [$ivp]		@ load IV
1172	b	.Lcbc_dec_loop
1173
1174.align	4
1175.Lcbc_dec_loop:
1176	subs	$len, $len, #0x8
1177	bmi	.Lcbc_dec_loop_finish
1178
1179	vld1.8	{@XMM[0]-@XMM[1]}, [$inp]!	@ load input
1180	vld1.8	{@XMM[2]-@XMM[3]}, [$inp]!
1181#ifndef	BSAES_ASM_EXTENDED_KEY
1182	mov	r4, $keysched			@ pass the key
1183#else
1184	add	r4, $key, #248
1185#endif
1186	vld1.8	{@XMM[4]-@XMM[5]}, [$inp]!
1187	mov	r5, $rounds
1188	vld1.8	{@XMM[6]-@XMM[7]}, [$inp]
1189	sub	$inp, $inp, #0x60
1190	vstmia	$fp, {@XMM[15]}			@ put aside IV
1191
1192	bl	_bsaes_decrypt8
1193
1194	vldmia	$fp, {@XMM[14]}			@ reload IV
1195	vld1.8	{@XMM[8]-@XMM[9]}, [$inp]!	@ reload input
1196	veor	@XMM[0], @XMM[0], @XMM[14]	@ ^= IV
1197	vld1.8	{@XMM[10]-@XMM[11]}, [$inp]!
1198	veor	@XMM[1], @XMM[1], @XMM[8]
1199	veor	@XMM[6], @XMM[6], @XMM[9]
1200	vld1.8	{@XMM[12]-@XMM[13]}, [$inp]!
1201	veor	@XMM[4], @XMM[4], @XMM[10]
1202	veor	@XMM[2], @XMM[2], @XMM[11]
1203	vld1.8	{@XMM[14]-@XMM[15]}, [$inp]!
1204	veor	@XMM[7], @XMM[7], @XMM[12]
1205	vst1.8	{@XMM[0]-@XMM[1]}, [$out]!	@ write output
1206	veor	@XMM[3], @XMM[3], @XMM[13]
1207	vst1.8	{@XMM[6]}, [$out]!
1208	veor	@XMM[5], @XMM[5], @XMM[14]
1209	vst1.8	{@XMM[4]}, [$out]!
1210	vst1.8	{@XMM[2]}, [$out]!
1211	vst1.8	{@XMM[7]}, [$out]!
1212	vst1.8	{@XMM[3]}, [$out]!
1213	vst1.8	{@XMM[5]}, [$out]!
1214
1215	b	.Lcbc_dec_loop
1216
1217.Lcbc_dec_loop_finish:
1218	adds	$len, $len, #8
1219	beq	.Lcbc_dec_done
1220
1221	@ Set up most parameters for the _bsaes_decrypt8 call.
1222#ifndef	BSAES_ASM_EXTENDED_KEY
1223	mov	r4, $keysched			@ pass the key
1224#else
1225	add	r4, $key, #248
1226#endif
1227	mov	r5, $rounds
1228	vstmia	$fp, {@XMM[15]}			@ put aside IV
1229
1230	vld1.8	{@XMM[0]}, [$inp]!		@ load input
1231	cmp	$len, #2
1232	blo	.Lcbc_dec_one
1233	vld1.8	{@XMM[1]}, [$inp]!
1234	beq	.Lcbc_dec_two
1235	vld1.8	{@XMM[2]}, [$inp]!
1236	cmp	$len, #4
1237	blo	.Lcbc_dec_three
1238	vld1.8	{@XMM[3]}, [$inp]!
1239	beq	.Lcbc_dec_four
1240	vld1.8	{@XMM[4]}, [$inp]!
1241	cmp	$len, #6
1242	blo	.Lcbc_dec_five
1243	vld1.8	{@XMM[5]}, [$inp]!
1244	beq	.Lcbc_dec_six
1245	vld1.8	{@XMM[6]}, [$inp]!
1246	sub	$inp, $inp, #0x70
1247
1248	bl	_bsaes_decrypt8
1249
1250	vldmia	$fp, {@XMM[14]}			@ reload IV
1251	vld1.8	{@XMM[8]-@XMM[9]}, [$inp]!	@ reload input
1252	veor	@XMM[0], @XMM[0], @XMM[14]	@ ^= IV
1253	vld1.8	{@XMM[10]-@XMM[11]}, [$inp]!
1254	veor	@XMM[1], @XMM[1], @XMM[8]
1255	veor	@XMM[6], @XMM[6], @XMM[9]
1256	vld1.8	{@XMM[12]-@XMM[13]}, [$inp]!
1257	veor	@XMM[4], @XMM[4], @XMM[10]
1258	veor	@XMM[2], @XMM[2], @XMM[11]
1259	vld1.8	{@XMM[15]}, [$inp]!
1260	veor	@XMM[7], @XMM[7], @XMM[12]
1261	vst1.8	{@XMM[0]-@XMM[1]}, [$out]!	@ write output
1262	veor	@XMM[3], @XMM[3], @XMM[13]
1263	vst1.8	{@XMM[6]}, [$out]!
1264	vst1.8	{@XMM[4]}, [$out]!
1265	vst1.8	{@XMM[2]}, [$out]!
1266	vst1.8	{@XMM[7]}, [$out]!
1267	vst1.8	{@XMM[3]}, [$out]!
1268	b	.Lcbc_dec_done
1269.align	4
1270.Lcbc_dec_six:
1271	sub	$inp, $inp, #0x60
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[12]}, [$inp]!
1280	veor	@XMM[4], @XMM[4], @XMM[10]
1281	veor	@XMM[2], @XMM[2], @XMM[11]
1282	vld1.8	{@XMM[15]}, [$inp]!
1283	veor	@XMM[7], @XMM[7], @XMM[12]
1284	vst1.8	{@XMM[0]-@XMM[1]}, [$out]!	@ write output
1285	vst1.8	{@XMM[6]}, [$out]!
1286	vst1.8	{@XMM[4]}, [$out]!
1287	vst1.8	{@XMM[2]}, [$out]!
1288	vst1.8	{@XMM[7]}, [$out]!
1289	b	.Lcbc_dec_done
1290.align	4
1291.Lcbc_dec_five:
1292	sub	$inp, $inp, #0x50
1293	bl	_bsaes_decrypt8
1294	vldmia	$fp, {@XMM[14]}			@ reload IV
1295	vld1.8	{@XMM[8]-@XMM[9]}, [$inp]!	@ reload input
1296	veor	@XMM[0], @XMM[0], @XMM[14]	@ ^= IV
1297	vld1.8	{@XMM[10]-@XMM[11]}, [$inp]!
1298	veor	@XMM[1], @XMM[1], @XMM[8]
1299	veor	@XMM[6], @XMM[6], @XMM[9]
1300	vld1.8	{@XMM[15]}, [$inp]!
1301	veor	@XMM[4], @XMM[4], @XMM[10]
1302	vst1.8	{@XMM[0]-@XMM[1]}, [$out]!	@ write output
1303	veor	@XMM[2], @XMM[2], @XMM[11]
1304	vst1.8	{@XMM[6]}, [$out]!
1305	vst1.8	{@XMM[4]}, [$out]!
1306	vst1.8	{@XMM[2]}, [$out]!
1307	b	.Lcbc_dec_done
1308.align	4
1309.Lcbc_dec_four:
1310	sub	$inp, $inp, #0x40
1311	bl	_bsaes_decrypt8
1312	vldmia	$fp, {@XMM[14]}			@ reload IV
1313	vld1.8	{@XMM[8]-@XMM[9]}, [$inp]!	@ reload input
1314	veor	@XMM[0], @XMM[0], @XMM[14]	@ ^= IV
1315	vld1.8	{@XMM[10]}, [$inp]!
1316	veor	@XMM[1], @XMM[1], @XMM[8]
1317	veor	@XMM[6], @XMM[6], @XMM[9]
1318	vld1.8	{@XMM[15]}, [$inp]!
1319	veor	@XMM[4], @XMM[4], @XMM[10]
1320	vst1.8	{@XMM[0]-@XMM[1]}, [$out]!	@ write output
1321	vst1.8	{@XMM[6]}, [$out]!
1322	vst1.8	{@XMM[4]}, [$out]!
1323	b	.Lcbc_dec_done
1324.align	4
1325.Lcbc_dec_three:
1326	sub	$inp, $inp, #0x30
1327	bl	_bsaes_decrypt8
1328	vldmia	$fp, {@XMM[14]}			@ reload IV
1329	vld1.8	{@XMM[8]-@XMM[9]}, [$inp]!	@ reload input
1330	veor	@XMM[0], @XMM[0], @XMM[14]	@ ^= IV
1331	vld1.8	{@XMM[15]}, [$inp]!
1332	veor	@XMM[1], @XMM[1], @XMM[8]
1333	veor	@XMM[6], @XMM[6], @XMM[9]
1334	vst1.8	{@XMM[0]-@XMM[1]}, [$out]!	@ write output
1335	vst1.8	{@XMM[6]}, [$out]!
1336	b	.Lcbc_dec_done
1337.align	4
1338.Lcbc_dec_two:
1339	sub	$inp, $inp, #0x20
1340	bl	_bsaes_decrypt8
1341	vldmia	$fp, {@XMM[14]}			@ reload IV
1342	vld1.8	{@XMM[8]}, [$inp]!		@ reload input
1343	veor	@XMM[0], @XMM[0], @XMM[14]	@ ^= IV
1344	vld1.8	{@XMM[15]}, [$inp]!		@ reload input
1345	veor	@XMM[1], @XMM[1], @XMM[8]
1346	vst1.8	{@XMM[0]-@XMM[1]}, [$out]!	@ write output
1347	b	.Lcbc_dec_done
1348.align	4
1349.Lcbc_dec_one:
1350	sub	$inp, $inp, #0x10
1351	bl	_bsaes_decrypt8
1352	vldmia	$fp, {@XMM[14]}			@ reload IV
1353	vld1.8	{@XMM[15]}, [$inp]!		@ reload input
1354	veor	@XMM[0], @XMM[0], @XMM[14]	@ ^= IV
1355	vst1.8	{@XMM[0]}, [$out]!		@ write output
1356
1357.Lcbc_dec_done:
1358#ifndef	BSAES_ASM_EXTENDED_KEY
1359	vmov.i32	q0, #0
1360	vmov.i32	q1, #0
1361.Lcbc_dec_bzero:				@ wipe key schedule [if any]
1362	vstmia		$keysched!, {q0-q1}
1363	cmp		$keysched, $fp
1364	bne		.Lcbc_dec_bzero
1365#endif
1366
1367	mov	sp, $fp
1368	add	sp, #0x10			@ add sp,$fp,#0x10 is no good for thumb
1369	vst1.8	{@XMM[15]}, [$ivp]		@ return IV
1370	VFP_ABI_POP
1371	ldmia	sp!, {r4-r10, pc}
1372.size	bsaes_cbc_encrypt,.-bsaes_cbc_encrypt
1373___
1374}
1375{
1376my ($inp,$out,$len,$key, $ctr,$fp,$rounds)=(map("r$_",(0..3,8..10)));
1377my $const = "r6";	# shared with _bsaes_encrypt8_alt
1378my $keysched = "sp";
1379
1380$code.=<<___;
1381.global	bsaes_ctr32_encrypt_blocks
1382.type	bsaes_ctr32_encrypt_blocks,%function
1383.align	5
1384bsaes_ctr32_encrypt_blocks:
1385	@ In OpenSSL, short inputs fall back to aes_nohw_* here. We patch this
1386	@ out to retain a constant-time implementation.
1387	mov	ip, sp
1388	stmdb	sp!, {r4-r10, lr}
1389	VFP_ABI_PUSH
1390	ldr	$ctr, [ip]			@ ctr is 1st arg on the stack
1391	sub	sp, sp, #0x10			@ scratch space to carry over the ctr
1392	mov	$fp, sp				@ save sp
1393
1394	ldr	$rounds, [$key, #240]		@ get # of rounds
1395#ifndef	BSAES_ASM_EXTENDED_KEY
1396	@ allocate the key schedule on the stack
1397	sub	r12, sp, $rounds, lsl#7		@ 128 bytes per inner round key
1398	add	r12, #`128-32`			@ size of bit-sliced key schedule
1399
1400	@ populate the key schedule
1401	mov	r4, $key			@ pass key
1402	mov	r5, $rounds			@ pass # of rounds
1403	mov	sp, r12				@ sp is $keysched
1404	bl	_bsaes_key_convert
1405	veor	@XMM[7],@XMM[7],@XMM[15]	@ fix up last round key
1406	vstmia	r12, {@XMM[7]}			@ save last round key
1407
1408	vld1.8	{@XMM[0]}, [$ctr]		@ load counter
1409#ifdef	__APPLE__
1410	mov	$ctr, #:lower16:(.LREVM0SR-.LM0)
1411	add	$ctr, $const, $ctr
1412#else
1413	add	$ctr, $const, #.LREVM0SR-.LM0	@ borrow $ctr
1414#endif
1415	vldmia	$keysched, {@XMM[4]}		@ load round0 key
1416#else
1417	ldr	r12, [$key, #244]
1418	eors	r12, #1
1419	beq	0f
1420
1421	@ populate the key schedule
1422	str	r12, [$key, #244]
1423	mov	r4, $key			@ pass key
1424	mov	r5, $rounds			@ pass # of rounds
1425	add	r12, $key, #248			@ pass key schedule
1426	bl	_bsaes_key_convert
1427	veor	@XMM[7],@XMM[7],@XMM[15]	@ fix up last round key
1428	vstmia	r12, {@XMM[7]}			@ save last round key
1429
1430.align	2
14310:	add	r12, $key, #248
1432	vld1.8	{@XMM[0]}, [$ctr]		@ load counter
1433	adrl	$ctr, .LREVM0SR			@ borrow $ctr
1434	vldmia	r12, {@XMM[4]}			@ load round0 key
1435	sub	sp, #0x10			@ place for adjusted round0 key
1436#endif
1437
1438	vmov.i32	@XMM[8],#1		@ compose 1<<96
1439	veor		@XMM[9],@XMM[9],@XMM[9]
1440	vrev32.8	@XMM[0],@XMM[0]
1441	vext.8		@XMM[8],@XMM[9],@XMM[8],#4
1442	vrev32.8	@XMM[4],@XMM[4]
1443	vadd.u32	@XMM[9],@XMM[8],@XMM[8]	@ compose 2<<96
1444	vstmia	$keysched, {@XMM[4]}		@ save adjusted round0 key
1445	b	.Lctr_enc_loop
1446
1447.align	4
1448.Lctr_enc_loop:
1449	vadd.u32	@XMM[10], @XMM[8], @XMM[9]	@ compose 3<<96
1450	vadd.u32	@XMM[1], @XMM[0], @XMM[8]	@ +1
1451	vadd.u32	@XMM[2], @XMM[0], @XMM[9]	@ +2
1452	vadd.u32	@XMM[3], @XMM[0], @XMM[10]	@ +3
1453	vadd.u32	@XMM[4], @XMM[1], @XMM[10]
1454	vadd.u32	@XMM[5], @XMM[2], @XMM[10]
1455	vadd.u32	@XMM[6], @XMM[3], @XMM[10]
1456	vadd.u32	@XMM[7], @XMM[4], @XMM[10]
1457	vadd.u32	@XMM[10], @XMM[5], @XMM[10]	@ next counter
1458
1459	@ Borrow prologue from _bsaes_encrypt8 to use the opportunity
1460	@ to flip byte order in 32-bit counter
1461
1462	vldmia		$keysched, {@XMM[9]}		@ load round0 key
1463#ifndef	BSAES_ASM_EXTENDED_KEY
1464	add		r4, $keysched, #0x10		@ pass next round key
1465#else
1466	add		r4, $key, #`248+16`
1467#endif
1468	vldmia		$ctr, {@XMM[8]}			@ .LREVM0SR
1469	mov		r5, $rounds			@ pass rounds
1470	vstmia		$fp, {@XMM[10]}			@ save next counter
1471#ifdef	__APPLE__
1472	mov		$const, #:lower16:(.LREVM0SR-.LSR)
1473	sub		$const, $ctr, $const
1474#else
1475	sub		$const, $ctr, #.LREVM0SR-.LSR	@ pass constants
1476#endif
1477
1478	bl		_bsaes_encrypt8_alt
1479
1480	subs		$len, $len, #8
1481	blo		.Lctr_enc_loop_done
1482
1483	vld1.8		{@XMM[8]-@XMM[9]}, [$inp]!	@ load input
1484	vld1.8		{@XMM[10]-@XMM[11]}, [$inp]!
1485	veor		@XMM[0], @XMM[8]
1486	veor		@XMM[1], @XMM[9]
1487	vld1.8		{@XMM[12]-@XMM[13]}, [$inp]!
1488	veor		@XMM[4], @XMM[10]
1489	veor		@XMM[6], @XMM[11]
1490	vld1.8		{@XMM[14]-@XMM[15]}, [$inp]!
1491	veor		@XMM[3], @XMM[12]
1492	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!	@ write output
1493	veor		@XMM[7], @XMM[13]
1494	veor		@XMM[2], @XMM[14]
1495	vst1.8		{@XMM[4]}, [$out]!
1496	veor		@XMM[5], @XMM[15]
1497	vst1.8		{@XMM[6]}, [$out]!
1498	vmov.i32	@XMM[8], #1			@ compose 1<<96
1499	vst1.8		{@XMM[3]}, [$out]!
1500	veor		@XMM[9], @XMM[9], @XMM[9]
1501	vst1.8		{@XMM[7]}, [$out]!
1502	vext.8		@XMM[8], @XMM[9], @XMM[8], #4
1503	vst1.8		{@XMM[2]}, [$out]!
1504	vadd.u32	@XMM[9],@XMM[8],@XMM[8]		@ compose 2<<96
1505	vst1.8		{@XMM[5]}, [$out]!
1506	vldmia		$fp, {@XMM[0]}			@ load counter
1507
1508	bne		.Lctr_enc_loop
1509	b		.Lctr_enc_done
1510
1511.align	4
1512.Lctr_enc_loop_done:
1513	add		$len, $len, #8
1514	vld1.8		{@XMM[8]}, [$inp]!	@ load input
1515	veor		@XMM[0], @XMM[8]
1516	vst1.8		{@XMM[0]}, [$out]!	@ write output
1517	cmp		$len, #2
1518	blo		.Lctr_enc_done
1519	vld1.8		{@XMM[9]}, [$inp]!
1520	veor		@XMM[1], @XMM[9]
1521	vst1.8		{@XMM[1]}, [$out]!
1522	beq		.Lctr_enc_done
1523	vld1.8		{@XMM[10]}, [$inp]!
1524	veor		@XMM[4], @XMM[10]
1525	vst1.8		{@XMM[4]}, [$out]!
1526	cmp		$len, #4
1527	blo		.Lctr_enc_done
1528	vld1.8		{@XMM[11]}, [$inp]!
1529	veor		@XMM[6], @XMM[11]
1530	vst1.8		{@XMM[6]}, [$out]!
1531	beq		.Lctr_enc_done
1532	vld1.8		{@XMM[12]}, [$inp]!
1533	veor		@XMM[3], @XMM[12]
1534	vst1.8		{@XMM[3]}, [$out]!
1535	cmp		$len, #6
1536	blo		.Lctr_enc_done
1537	vld1.8		{@XMM[13]}, [$inp]!
1538	veor		@XMM[7], @XMM[13]
1539	vst1.8		{@XMM[7]}, [$out]!
1540	beq		.Lctr_enc_done
1541	vld1.8		{@XMM[14]}, [$inp]
1542	veor		@XMM[2], @XMM[14]
1543	vst1.8		{@XMM[2]}, [$out]!
1544
1545.Lctr_enc_done:
1546	vmov.i32	q0, #0
1547	vmov.i32	q1, #0
1548#ifndef	BSAES_ASM_EXTENDED_KEY
1549.Lctr_enc_bzero:			@ wipe key schedule [if any]
1550	vstmia		$keysched!, {q0-q1}
1551	cmp		$keysched, $fp
1552	bne		.Lctr_enc_bzero
1553#else
1554	vstmia		$keysched, {q0-q1}
1555#endif
1556
1557	mov	sp, $fp
1558	add	sp, #0x10		@ add sp,$fp,#0x10 is no good for thumb
1559	VFP_ABI_POP
1560	ldmia	sp!, {r4-r10, pc}	@ return
1561
1562	@ OpenSSL contains aes_nohw_* fallback code here. We patch this
1563	@ out to retain a constant-time implementation.
1564.size	bsaes_ctr32_encrypt_blocks,.-bsaes_ctr32_encrypt_blocks
1565___
1566}
1567# In BorinSSL, we patch XTS support out.
1568if (0) {
1569######################################################################
1570# void bsaes_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1571#	const AES_KEY *key1, const AES_KEY *key2,
1572#	const unsigned char iv[16]);
1573#
1574my ($inp,$out,$len,$key,$rounds,$magic,$fp)=(map("r$_",(7..10,1..3)));
1575my $const="r6";		# returned by _bsaes_key_convert
1576my $twmask=@XMM[5];
1577my @T=@XMM[6..7];
1578
1579$code.=<<___;
1580.globl	bsaes_xts_encrypt
1581.type	bsaes_xts_encrypt,%function
1582.align	4
1583bsaes_xts_encrypt:
1584	mov	ip, sp
1585	stmdb	sp!, {r4-r10, lr}		@ 0x20
1586	VFP_ABI_PUSH
1587	mov	r6, sp				@ future $fp
1588
1589	mov	$inp, r0
1590	mov	$out, r1
1591	mov	$len, r2
1592	mov	$key, r3
1593
1594	sub	r0, sp, #0x10			@ 0x10
1595	bic	r0, #0xf			@ align at 16 bytes
1596	mov	sp, r0
1597
1598#ifdef	XTS_CHAIN_TWEAK
1599	ldr	r0, [ip]			@ pointer to input tweak
1600#else
1601	@ generate initial tweak
1602	ldr	r0, [ip, #4]			@ iv[]
1603	mov	r1, sp
1604	ldr	r2, [ip, #0]			@ key2
1605	bl	aes_nohw_encrypt
1606	mov	r0,sp				@ pointer to initial tweak
1607#endif
1608
1609	ldr	$rounds, [$key, #240]		@ get # of rounds
1610	mov	$fp, r6
1611#ifndef	BSAES_ASM_EXTENDED_KEY
1612	@ allocate the key schedule on the stack
1613	sub	r12, sp, $rounds, lsl#7		@ 128 bytes per inner round key
1614	@ add	r12, #`128-32`			@ size of bit-sliced key schedule
1615	sub	r12, #`32+16`			@ place for tweak[9]
1616
1617	@ populate the key schedule
1618	mov	r4, $key			@ pass key
1619	mov	r5, $rounds			@ pass # of rounds
1620	mov	sp, r12
1621	add	r12, #0x90			@ pass key schedule
1622	bl	_bsaes_key_convert
1623	veor	@XMM[7], @XMM[7], @XMM[15]	@ fix up last round key
1624	vstmia	r12, {@XMM[7]}			@ save last round key
1625#else
1626	ldr	r12, [$key, #244]
1627	eors	r12, #1
1628	beq	0f
1629
1630	str	r12, [$key, #244]
1631	mov	r4, $key			@ pass key
1632	mov	r5, $rounds			@ pass # of rounds
1633	add	r12, $key, #248			@ pass key schedule
1634	bl	_bsaes_key_convert
1635	veor	@XMM[7], @XMM[7], @XMM[15]	@ fix up last round key
1636	vstmia	r12, {@XMM[7]}
1637
1638.align	2
16390:	sub	sp, #0x90			@ place for tweak[9]
1640#endif
1641
1642	vld1.8	{@XMM[8]}, [r0]			@ initial tweak
1643	adr	$magic, .Lxts_magic
1644
1645	subs	$len, #0x80
1646	blo	.Lxts_enc_short
1647	b	.Lxts_enc_loop
1648
1649.align	4
1650.Lxts_enc_loop:
1651	vldmia		$magic, {$twmask}	@ load XTS magic
1652	vshr.s64	@T[0], @XMM[8], #63
1653	mov		r0, sp
1654	vand		@T[0], @T[0], $twmask
1655___
1656for($i=9;$i<16;$i++) {
1657$code.=<<___;
1658	vadd.u64	@XMM[$i], @XMM[$i-1], @XMM[$i-1]
1659	vst1.64		{@XMM[$i-1]}, [r0,:128]!
1660	vswp		`&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1661	vshr.s64	@T[1], @XMM[$i], #63
1662	veor		@XMM[$i], @XMM[$i], @T[0]
1663	vand		@T[1], @T[1], $twmask
1664___
1665	@T=reverse(@T);
1666
1667$code.=<<___ if ($i>=10);
1668	vld1.8		{@XMM[$i-10]}, [$inp]!
1669___
1670$code.=<<___ if ($i>=11);
1671	veor		@XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1672___
1673}
1674$code.=<<___;
1675	vadd.u64	@XMM[8], @XMM[15], @XMM[15]
1676	vst1.64		{@XMM[15]}, [r0,:128]!
1677	vswp		`&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1678	veor		@XMM[8], @XMM[8], @T[0]
1679	vst1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
1680
1681	vld1.8		{@XMM[6]-@XMM[7]}, [$inp]!
1682	veor		@XMM[5], @XMM[5], @XMM[13]
1683#ifndef	BSAES_ASM_EXTENDED_KEY
1684	add		r4, sp, #0x90			@ pass key schedule
1685#else
1686	add		r4, $key, #248			@ pass key schedule
1687#endif
1688	veor		@XMM[6], @XMM[6], @XMM[14]
1689	mov		r5, $rounds			@ pass rounds
1690	veor		@XMM[7], @XMM[7], @XMM[15]
1691	mov		r0, sp
1692
1693	bl		_bsaes_encrypt8
1694
1695	vld1.64		{@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1696	vld1.64		{@XMM[10]-@XMM[11]}, [r0,:128]!
1697	veor		@XMM[0], @XMM[0], @XMM[ 8]
1698	vld1.64		{@XMM[12]-@XMM[13]}, [r0,:128]!
1699	veor		@XMM[1], @XMM[1], @XMM[ 9]
1700	veor		@XMM[8], @XMM[4], @XMM[10]
1701	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
1702	veor		@XMM[9], @XMM[6], @XMM[11]
1703	vld1.64		{@XMM[14]-@XMM[15]}, [r0,:128]!
1704	veor		@XMM[10], @XMM[3], @XMM[12]
1705	vst1.8		{@XMM[8]-@XMM[9]}, [$out]!
1706	veor		@XMM[11], @XMM[7], @XMM[13]
1707	veor		@XMM[12], @XMM[2], @XMM[14]
1708	vst1.8		{@XMM[10]-@XMM[11]}, [$out]!
1709	veor		@XMM[13], @XMM[5], @XMM[15]
1710	vst1.8		{@XMM[12]-@XMM[13]}, [$out]!
1711
1712	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
1713
1714	subs		$len, #0x80
1715	bpl		.Lxts_enc_loop
1716
1717.Lxts_enc_short:
1718	adds		$len, #0x70
1719	bmi		.Lxts_enc_done
1720
1721	vldmia		$magic, {$twmask}	@ load XTS magic
1722	vshr.s64	@T[0], @XMM[8], #63
1723	mov		r0, sp
1724	vand		@T[0], @T[0], $twmask
1725___
1726for($i=9;$i<16;$i++) {
1727$code.=<<___;
1728	vadd.u64	@XMM[$i], @XMM[$i-1], @XMM[$i-1]
1729	vst1.64		{@XMM[$i-1]}, [r0,:128]!
1730	vswp		`&Dhi("@T[0]")`,`&Dlo("@T[0]")`
1731	vshr.s64	@T[1], @XMM[$i], #63
1732	veor		@XMM[$i], @XMM[$i], @T[0]
1733	vand		@T[1], @T[1], $twmask
1734___
1735	@T=reverse(@T);
1736
1737$code.=<<___ if ($i>=10);
1738	vld1.8		{@XMM[$i-10]}, [$inp]!
1739	subs		$len, #0x10
1740	bmi		.Lxts_enc_`$i-9`
1741___
1742$code.=<<___ if ($i>=11);
1743	veor		@XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
1744___
1745}
1746$code.=<<___;
1747	sub		$len, #0x10
1748	vst1.64		{@XMM[15]}, [r0,:128]		@ next round tweak
1749
1750	vld1.8		{@XMM[6]}, [$inp]!
1751	veor		@XMM[5], @XMM[5], @XMM[13]
1752#ifndef	BSAES_ASM_EXTENDED_KEY
1753	add		r4, sp, #0x90			@ pass key schedule
1754#else
1755	add		r4, $key, #248			@ pass key schedule
1756#endif
1757	veor		@XMM[6], @XMM[6], @XMM[14]
1758	mov		r5, $rounds			@ pass rounds
1759	mov		r0, sp
1760
1761	bl		_bsaes_encrypt8
1762
1763	vld1.64		{@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1764	vld1.64		{@XMM[10]-@XMM[11]}, [r0,:128]!
1765	veor		@XMM[0], @XMM[0], @XMM[ 8]
1766	vld1.64		{@XMM[12]-@XMM[13]}, [r0,:128]!
1767	veor		@XMM[1], @XMM[1], @XMM[ 9]
1768	veor		@XMM[8], @XMM[4], @XMM[10]
1769	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
1770	veor		@XMM[9], @XMM[6], @XMM[11]
1771	vld1.64		{@XMM[14]}, [r0,:128]!
1772	veor		@XMM[10], @XMM[3], @XMM[12]
1773	vst1.8		{@XMM[8]-@XMM[9]}, [$out]!
1774	veor		@XMM[11], @XMM[7], @XMM[13]
1775	veor		@XMM[12], @XMM[2], @XMM[14]
1776	vst1.8		{@XMM[10]-@XMM[11]}, [$out]!
1777	vst1.8		{@XMM[12]}, [$out]!
1778
1779	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
1780	b		.Lxts_enc_done
1781.align	4
1782.Lxts_enc_6:
1783	veor		@XMM[4], @XMM[4], @XMM[12]
1784#ifndef	BSAES_ASM_EXTENDED_KEY
1785	add		r4, sp, #0x90			@ pass key schedule
1786#else
1787	add		r4, $key, #248			@ pass key schedule
1788#endif
1789	veor		@XMM[5], @XMM[5], @XMM[13]
1790	mov		r5, $rounds			@ pass rounds
1791	mov		r0, sp
1792
1793	bl		_bsaes_encrypt8
1794
1795	vld1.64		{@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1796	vld1.64		{@XMM[10]-@XMM[11]}, [r0,:128]!
1797	veor		@XMM[0], @XMM[0], @XMM[ 8]
1798	vld1.64		{@XMM[12]-@XMM[13]}, [r0,:128]!
1799	veor		@XMM[1], @XMM[1], @XMM[ 9]
1800	veor		@XMM[8], @XMM[4], @XMM[10]
1801	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
1802	veor		@XMM[9], @XMM[6], @XMM[11]
1803	veor		@XMM[10], @XMM[3], @XMM[12]
1804	vst1.8		{@XMM[8]-@XMM[9]}, [$out]!
1805	veor		@XMM[11], @XMM[7], @XMM[13]
1806	vst1.8		{@XMM[10]-@XMM[11]}, [$out]!
1807
1808	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
1809	b		.Lxts_enc_done
1810
1811@ put this in range for both ARM and Thumb mode adr instructions
1812.align	5
1813.Lxts_magic:
1814	.quad	1, 0x87
1815
1816.align	5
1817.Lxts_enc_5:
1818	veor		@XMM[3], @XMM[3], @XMM[11]
1819#ifndef	BSAES_ASM_EXTENDED_KEY
1820	add		r4, sp, #0x90			@ pass key schedule
1821#else
1822	add		r4, $key, #248			@ pass key schedule
1823#endif
1824	veor		@XMM[4], @XMM[4], @XMM[12]
1825	mov		r5, $rounds			@ pass rounds
1826	mov		r0, sp
1827
1828	bl		_bsaes_encrypt8
1829
1830	vld1.64		{@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1831	vld1.64		{@XMM[10]-@XMM[11]}, [r0,:128]!
1832	veor		@XMM[0], @XMM[0], @XMM[ 8]
1833	vld1.64		{@XMM[12]}, [r0,:128]!
1834	veor		@XMM[1], @XMM[1], @XMM[ 9]
1835	veor		@XMM[8], @XMM[4], @XMM[10]
1836	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
1837	veor		@XMM[9], @XMM[6], @XMM[11]
1838	veor		@XMM[10], @XMM[3], @XMM[12]
1839	vst1.8		{@XMM[8]-@XMM[9]}, [$out]!
1840	vst1.8		{@XMM[10]}, [$out]!
1841
1842	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
1843	b		.Lxts_enc_done
1844.align	4
1845.Lxts_enc_4:
1846	veor		@XMM[2], @XMM[2], @XMM[10]
1847#ifndef	BSAES_ASM_EXTENDED_KEY
1848	add		r4, sp, #0x90			@ pass key schedule
1849#else
1850	add		r4, $key, #248			@ pass key schedule
1851#endif
1852	veor		@XMM[3], @XMM[3], @XMM[11]
1853	mov		r5, $rounds			@ pass rounds
1854	mov		r0, sp
1855
1856	bl		_bsaes_encrypt8
1857
1858	vld1.64		{@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
1859	vld1.64		{@XMM[10]-@XMM[11]}, [r0,:128]!
1860	veor		@XMM[0], @XMM[0], @XMM[ 8]
1861	veor		@XMM[1], @XMM[1], @XMM[ 9]
1862	veor		@XMM[8], @XMM[4], @XMM[10]
1863	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
1864	veor		@XMM[9], @XMM[6], @XMM[11]
1865	vst1.8		{@XMM[8]-@XMM[9]}, [$out]!
1866
1867	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
1868	b		.Lxts_enc_done
1869.align	4
1870.Lxts_enc_3:
1871	veor		@XMM[1], @XMM[1], @XMM[9]
1872#ifndef	BSAES_ASM_EXTENDED_KEY
1873	add		r4, sp, #0x90			@ pass key schedule
1874#else
1875	add		r4, $key, #248			@ pass key schedule
1876#endif
1877	veor		@XMM[2], @XMM[2], @XMM[10]
1878	mov		r5, $rounds			@ pass rounds
1879	mov		r0, sp
1880
1881	bl		_bsaes_encrypt8
1882
1883	vld1.64		{@XMM[8]-@XMM[9]}, [r0,:128]!
1884	vld1.64		{@XMM[10]}, [r0,:128]!
1885	veor		@XMM[0], @XMM[0], @XMM[ 8]
1886	veor		@XMM[1], @XMM[1], @XMM[ 9]
1887	veor		@XMM[8], @XMM[4], @XMM[10]
1888	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
1889	vst1.8		{@XMM[8]}, [$out]!
1890
1891	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
1892	b		.Lxts_enc_done
1893.align	4
1894.Lxts_enc_2:
1895	veor		@XMM[0], @XMM[0], @XMM[8]
1896#ifndef	BSAES_ASM_EXTENDED_KEY
1897	add		r4, sp, #0x90			@ pass key schedule
1898#else
1899	add		r4, $key, #248			@ pass key schedule
1900#endif
1901	veor		@XMM[1], @XMM[1], @XMM[9]
1902	mov		r5, $rounds			@ pass rounds
1903	mov		r0, sp
1904
1905	bl		_bsaes_encrypt8
1906
1907	vld1.64		{@XMM[8]-@XMM[9]}, [r0,:128]!
1908	veor		@XMM[0], @XMM[0], @XMM[ 8]
1909	veor		@XMM[1], @XMM[1], @XMM[ 9]
1910	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
1911
1912	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
1913	b		.Lxts_enc_done
1914.align	4
1915.Lxts_enc_1:
1916	mov		r0, sp
1917	veor		@XMM[0], @XMM[0], @XMM[8]
1918	mov		r1, sp
1919	vst1.8		{@XMM[0]}, [sp,:128]
1920	mov		r2, $key
1921	mov		r4, $fp				@ preserve fp
1922
1923	bl		aes_nohw_encrypt
1924
1925	vld1.8		{@XMM[0]}, [sp,:128]
1926	veor		@XMM[0], @XMM[0], @XMM[8]
1927	vst1.8		{@XMM[0]}, [$out]!
1928	mov		$fp, r4
1929
1930	vmov		@XMM[8], @XMM[9]		@ next round tweak
1931
1932.Lxts_enc_done:
1933#ifndef	XTS_CHAIN_TWEAK
1934	adds		$len, #0x10
1935	beq		.Lxts_enc_ret
1936	sub		r6, $out, #0x10
1937
1938.Lxts_enc_steal:
1939	ldrb		r0, [$inp], #1
1940	ldrb		r1, [$out, #-0x10]
1941	strb		r0, [$out, #-0x10]
1942	strb		r1, [$out], #1
1943
1944	subs		$len, #1
1945	bhi		.Lxts_enc_steal
1946
1947	vld1.8		{@XMM[0]}, [r6]
1948	mov		r0, sp
1949	veor		@XMM[0], @XMM[0], @XMM[8]
1950	mov		r1, sp
1951	vst1.8		{@XMM[0]}, [sp,:128]
1952	mov		r2, $key
1953	mov		r4, $fp			@ preserve fp
1954
1955	bl		aes_nohw_encrypt
1956
1957	vld1.8		{@XMM[0]}, [sp,:128]
1958	veor		@XMM[0], @XMM[0], @XMM[8]
1959	vst1.8		{@XMM[0]}, [r6]
1960	mov		$fp, r4
1961#endif
1962
1963.Lxts_enc_ret:
1964	bic		r0, $fp, #0xf
1965	vmov.i32	q0, #0
1966	vmov.i32	q1, #0
1967#ifdef	XTS_CHAIN_TWEAK
1968	ldr		r1, [$fp, #0x20+VFP_ABI_FRAME]	@ chain tweak
1969#endif
1970.Lxts_enc_bzero:				@ wipe key schedule [if any]
1971	vstmia		sp!, {q0-q1}
1972	cmp		sp, r0
1973	bne		.Lxts_enc_bzero
1974
1975	mov		sp, $fp
1976#ifdef	XTS_CHAIN_TWEAK
1977	vst1.8		{@XMM[8]}, [r1]
1978#endif
1979	VFP_ABI_POP
1980	ldmia		sp!, {r4-r10, pc}	@ return
1981
1982.size	bsaes_xts_encrypt,.-bsaes_xts_encrypt
1983
1984.globl	bsaes_xts_decrypt
1985.type	bsaes_xts_decrypt,%function
1986.align	4
1987bsaes_xts_decrypt:
1988	mov	ip, sp
1989	stmdb	sp!, {r4-r10, lr}		@ 0x20
1990	VFP_ABI_PUSH
1991	mov	r6, sp				@ future $fp
1992
1993	mov	$inp, r0
1994	mov	$out, r1
1995	mov	$len, r2
1996	mov	$key, r3
1997
1998	sub	r0, sp, #0x10			@ 0x10
1999	bic	r0, #0xf			@ align at 16 bytes
2000	mov	sp, r0
2001
2002#ifdef	XTS_CHAIN_TWEAK
2003	ldr	r0, [ip]			@ pointer to input tweak
2004#else
2005	@ generate initial tweak
2006	ldr	r0, [ip, #4]			@ iv[]
2007	mov	r1, sp
2008	ldr	r2, [ip, #0]			@ key2
2009	bl	aes_nohw_encrypt
2010	mov	r0, sp				@ pointer to initial tweak
2011#endif
2012
2013	ldr	$rounds, [$key, #240]		@ get # of rounds
2014	mov	$fp, r6
2015#ifndef	BSAES_ASM_EXTENDED_KEY
2016	@ allocate the key schedule on the stack
2017	sub	r12, sp, $rounds, lsl#7		@ 128 bytes per inner round key
2018	@ add	r12, #`128-32`			@ size of bit-sliced key schedule
2019	sub	r12, #`32+16`			@ place for tweak[9]
2020
2021	@ populate the key schedule
2022	mov	r4, $key			@ pass key
2023	mov	r5, $rounds			@ pass # of rounds
2024	mov	sp, r12
2025	add	r12, #0x90			@ pass key schedule
2026	bl	_bsaes_key_convert
2027	add	r4, sp, #0x90
2028	vldmia	r4, {@XMM[6]}
2029	vstmia	r12,  {@XMM[15]}		@ save last round key
2030	veor	@XMM[7], @XMM[7], @XMM[6]	@ fix up round 0 key
2031	vstmia	r4, {@XMM[7]}
2032#else
2033	ldr	r12, [$key, #244]
2034	eors	r12, #1
2035	beq	0f
2036
2037	str	r12, [$key, #244]
2038	mov	r4, $key			@ pass key
2039	mov	r5, $rounds			@ pass # of rounds
2040	add	r12, $key, #248			@ pass key schedule
2041	bl	_bsaes_key_convert
2042	add	r4, $key, #248
2043	vldmia	r4, {@XMM[6]}
2044	vstmia	r12,  {@XMM[15]}		@ save last round key
2045	veor	@XMM[7], @XMM[7], @XMM[6]	@ fix up round 0 key
2046	vstmia	r4, {@XMM[7]}
2047
2048.align	2
20490:	sub	sp, #0x90			@ place for tweak[9]
2050#endif
2051	vld1.8	{@XMM[8]}, [r0]			@ initial tweak
2052	adr	$magic, .Lxts_magic
2053
2054#ifndef	XTS_CHAIN_TWEAK
2055	tst	$len, #0xf			@ if not multiple of 16
2056	it	ne				@ Thumb2 thing, sanity check in ARM
2057	subne	$len, #0x10			@ subtract another 16 bytes
2058#endif
2059	subs	$len, #0x80
2060
2061	blo	.Lxts_dec_short
2062	b	.Lxts_dec_loop
2063
2064.align	4
2065.Lxts_dec_loop:
2066	vldmia		$magic, {$twmask}	@ load XTS magic
2067	vshr.s64	@T[0], @XMM[8], #63
2068	mov		r0, sp
2069	vand		@T[0], @T[0], $twmask
2070___
2071for($i=9;$i<16;$i++) {
2072$code.=<<___;
2073	vadd.u64	@XMM[$i], @XMM[$i-1], @XMM[$i-1]
2074	vst1.64		{@XMM[$i-1]}, [r0,:128]!
2075	vswp		`&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2076	vshr.s64	@T[1], @XMM[$i], #63
2077	veor		@XMM[$i], @XMM[$i], @T[0]
2078	vand		@T[1], @T[1], $twmask
2079___
2080	@T=reverse(@T);
2081
2082$code.=<<___ if ($i>=10);
2083	vld1.8		{@XMM[$i-10]}, [$inp]!
2084___
2085$code.=<<___ if ($i>=11);
2086	veor		@XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2087___
2088}
2089$code.=<<___;
2090	vadd.u64	@XMM[8], @XMM[15], @XMM[15]
2091	vst1.64		{@XMM[15]}, [r0,:128]!
2092	vswp		`&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2093	veor		@XMM[8], @XMM[8], @T[0]
2094	vst1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
2095
2096	vld1.8		{@XMM[6]-@XMM[7]}, [$inp]!
2097	veor		@XMM[5], @XMM[5], @XMM[13]
2098#ifndef	BSAES_ASM_EXTENDED_KEY
2099	add		r4, sp, #0x90			@ pass key schedule
2100#else
2101	add		r4, $key, #248			@ pass key schedule
2102#endif
2103	veor		@XMM[6], @XMM[6], @XMM[14]
2104	mov		r5, $rounds			@ pass rounds
2105	veor		@XMM[7], @XMM[7], @XMM[15]
2106	mov		r0, sp
2107
2108	bl		_bsaes_decrypt8
2109
2110	vld1.64		{@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2111	vld1.64		{@XMM[10]-@XMM[11]}, [r0,:128]!
2112	veor		@XMM[0], @XMM[0], @XMM[ 8]
2113	vld1.64		{@XMM[12]-@XMM[13]}, [r0,:128]!
2114	veor		@XMM[1], @XMM[1], @XMM[ 9]
2115	veor		@XMM[8], @XMM[6], @XMM[10]
2116	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
2117	veor		@XMM[9], @XMM[4], @XMM[11]
2118	vld1.64		{@XMM[14]-@XMM[15]}, [r0,:128]!
2119	veor		@XMM[10], @XMM[2], @XMM[12]
2120	vst1.8		{@XMM[8]-@XMM[9]}, [$out]!
2121	veor		@XMM[11], @XMM[7], @XMM[13]
2122	veor		@XMM[12], @XMM[3], @XMM[14]
2123	vst1.8		{@XMM[10]-@XMM[11]}, [$out]!
2124	veor		@XMM[13], @XMM[5], @XMM[15]
2125	vst1.8		{@XMM[12]-@XMM[13]}, [$out]!
2126
2127	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
2128
2129	subs		$len, #0x80
2130	bpl		.Lxts_dec_loop
2131
2132.Lxts_dec_short:
2133	adds		$len, #0x70
2134	bmi		.Lxts_dec_done
2135
2136	vldmia		$magic, {$twmask}	@ load XTS magic
2137	vshr.s64	@T[0], @XMM[8], #63
2138	mov		r0, sp
2139	vand		@T[0], @T[0], $twmask
2140___
2141for($i=9;$i<16;$i++) {
2142$code.=<<___;
2143	vadd.u64	@XMM[$i], @XMM[$i-1], @XMM[$i-1]
2144	vst1.64		{@XMM[$i-1]}, [r0,:128]!
2145	vswp		`&Dhi("@T[0]")`,`&Dlo("@T[0]")`
2146	vshr.s64	@T[1], @XMM[$i], #63
2147	veor		@XMM[$i], @XMM[$i], @T[0]
2148	vand		@T[1], @T[1], $twmask
2149___
2150	@T=reverse(@T);
2151
2152$code.=<<___ if ($i>=10);
2153	vld1.8		{@XMM[$i-10]}, [$inp]!
2154	subs		$len, #0x10
2155	bmi		.Lxts_dec_`$i-9`
2156___
2157$code.=<<___ if ($i>=11);
2158	veor		@XMM[$i-11], @XMM[$i-11], @XMM[$i-3]
2159___
2160}
2161$code.=<<___;
2162	sub		$len, #0x10
2163	vst1.64		{@XMM[15]}, [r0,:128]		@ next round tweak
2164
2165	vld1.8		{@XMM[6]}, [$inp]!
2166	veor		@XMM[5], @XMM[5], @XMM[13]
2167#ifndef	BSAES_ASM_EXTENDED_KEY
2168	add		r4, sp, #0x90			@ pass key schedule
2169#else
2170	add		r4, $key, #248			@ pass key schedule
2171#endif
2172	veor		@XMM[6], @XMM[6], @XMM[14]
2173	mov		r5, $rounds			@ pass rounds
2174	mov		r0, sp
2175
2176	bl		_bsaes_decrypt8
2177
2178	vld1.64		{@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2179	vld1.64		{@XMM[10]-@XMM[11]}, [r0,:128]!
2180	veor		@XMM[0], @XMM[0], @XMM[ 8]
2181	vld1.64		{@XMM[12]-@XMM[13]}, [r0,:128]!
2182	veor		@XMM[1], @XMM[1], @XMM[ 9]
2183	veor		@XMM[8], @XMM[6], @XMM[10]
2184	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
2185	veor		@XMM[9], @XMM[4], @XMM[11]
2186	vld1.64		{@XMM[14]}, [r0,:128]!
2187	veor		@XMM[10], @XMM[2], @XMM[12]
2188	vst1.8		{@XMM[8]-@XMM[9]}, [$out]!
2189	veor		@XMM[11], @XMM[7], @XMM[13]
2190	veor		@XMM[12], @XMM[3], @XMM[14]
2191	vst1.8		{@XMM[10]-@XMM[11]}, [$out]!
2192	vst1.8		{@XMM[12]}, [$out]!
2193
2194	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
2195	b		.Lxts_dec_done
2196.align	4
2197.Lxts_dec_6:
2198	vst1.64		{@XMM[14]}, [r0,:128]		@ next round tweak
2199
2200	veor		@XMM[4], @XMM[4], @XMM[12]
2201#ifndef	BSAES_ASM_EXTENDED_KEY
2202	add		r4, sp, #0x90			@ pass key schedule
2203#else
2204	add		r4, $key, #248			@ pass key schedule
2205#endif
2206	veor		@XMM[5], @XMM[5], @XMM[13]
2207	mov		r5, $rounds			@ pass rounds
2208	mov		r0, sp
2209
2210	bl		_bsaes_decrypt8
2211
2212	vld1.64		{@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2213	vld1.64		{@XMM[10]-@XMM[11]}, [r0,:128]!
2214	veor		@XMM[0], @XMM[0], @XMM[ 8]
2215	vld1.64		{@XMM[12]-@XMM[13]}, [r0,:128]!
2216	veor		@XMM[1], @XMM[1], @XMM[ 9]
2217	veor		@XMM[8], @XMM[6], @XMM[10]
2218	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
2219	veor		@XMM[9], @XMM[4], @XMM[11]
2220	veor		@XMM[10], @XMM[2], @XMM[12]
2221	vst1.8		{@XMM[8]-@XMM[9]}, [$out]!
2222	veor		@XMM[11], @XMM[7], @XMM[13]
2223	vst1.8		{@XMM[10]-@XMM[11]}, [$out]!
2224
2225	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
2226	b		.Lxts_dec_done
2227.align	4
2228.Lxts_dec_5:
2229	veor		@XMM[3], @XMM[3], @XMM[11]
2230#ifndef	BSAES_ASM_EXTENDED_KEY
2231	add		r4, sp, #0x90			@ pass key schedule
2232#else
2233	add		r4, $key, #248			@ pass key schedule
2234#endif
2235	veor		@XMM[4], @XMM[4], @XMM[12]
2236	mov		r5, $rounds			@ pass rounds
2237	mov		r0, sp
2238
2239	bl		_bsaes_decrypt8
2240
2241	vld1.64		{@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2242	vld1.64		{@XMM[10]-@XMM[11]}, [r0,:128]!
2243	veor		@XMM[0], @XMM[0], @XMM[ 8]
2244	vld1.64		{@XMM[12]}, [r0,:128]!
2245	veor		@XMM[1], @XMM[1], @XMM[ 9]
2246	veor		@XMM[8], @XMM[6], @XMM[10]
2247	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
2248	veor		@XMM[9], @XMM[4], @XMM[11]
2249	veor		@XMM[10], @XMM[2], @XMM[12]
2250	vst1.8		{@XMM[8]-@XMM[9]}, [$out]!
2251	vst1.8		{@XMM[10]}, [$out]!
2252
2253	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
2254	b		.Lxts_dec_done
2255.align	4
2256.Lxts_dec_4:
2257	veor		@XMM[2], @XMM[2], @XMM[10]
2258#ifndef	BSAES_ASM_EXTENDED_KEY
2259	add		r4, sp, #0x90			@ pass key schedule
2260#else
2261	add		r4, $key, #248			@ pass key schedule
2262#endif
2263	veor		@XMM[3], @XMM[3], @XMM[11]
2264	mov		r5, $rounds			@ pass rounds
2265	mov		r0, sp
2266
2267	bl		_bsaes_decrypt8
2268
2269	vld1.64		{@XMM[ 8]-@XMM[ 9]}, [r0,:128]!
2270	vld1.64		{@XMM[10]-@XMM[11]}, [r0,:128]!
2271	veor		@XMM[0], @XMM[0], @XMM[ 8]
2272	veor		@XMM[1], @XMM[1], @XMM[ 9]
2273	veor		@XMM[8], @XMM[6], @XMM[10]
2274	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
2275	veor		@XMM[9], @XMM[4], @XMM[11]
2276	vst1.8		{@XMM[8]-@XMM[9]}, [$out]!
2277
2278	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
2279	b		.Lxts_dec_done
2280.align	4
2281.Lxts_dec_3:
2282	veor		@XMM[1], @XMM[1], @XMM[9]
2283#ifndef	BSAES_ASM_EXTENDED_KEY
2284	add		r4, sp, #0x90			@ pass key schedule
2285#else
2286	add		r4, $key, #248			@ pass key schedule
2287#endif
2288	veor		@XMM[2], @XMM[2], @XMM[10]
2289	mov		r5, $rounds			@ pass rounds
2290	mov		r0, sp
2291
2292	bl		_bsaes_decrypt8
2293
2294	vld1.64		{@XMM[8]-@XMM[9]}, [r0,:128]!
2295	vld1.64		{@XMM[10]}, [r0,:128]!
2296	veor		@XMM[0], @XMM[0], @XMM[ 8]
2297	veor		@XMM[1], @XMM[1], @XMM[ 9]
2298	veor		@XMM[8], @XMM[6], @XMM[10]
2299	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
2300	vst1.8		{@XMM[8]}, [$out]!
2301
2302	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
2303	b		.Lxts_dec_done
2304.align	4
2305.Lxts_dec_2:
2306	veor		@XMM[0], @XMM[0], @XMM[8]
2307#ifndef	BSAES_ASM_EXTENDED_KEY
2308	add		r4, sp, #0x90			@ pass key schedule
2309#else
2310	add		r4, $key, #248			@ pass key schedule
2311#endif
2312	veor		@XMM[1], @XMM[1], @XMM[9]
2313	mov		r5, $rounds			@ pass rounds
2314	mov		r0, sp
2315
2316	bl		_bsaes_decrypt8
2317
2318	vld1.64		{@XMM[8]-@XMM[9]}, [r0,:128]!
2319	veor		@XMM[0], @XMM[0], @XMM[ 8]
2320	veor		@XMM[1], @XMM[1], @XMM[ 9]
2321	vst1.8		{@XMM[0]-@XMM[1]}, [$out]!
2322
2323	vld1.64		{@XMM[8]}, [r0,:128]		@ next round tweak
2324	b		.Lxts_dec_done
2325.align	4
2326.Lxts_dec_1:
2327	mov		r0, sp
2328	veor		@XMM[0], @XMM[0], @XMM[8]
2329	mov		r1, sp
2330	vst1.8		{@XMM[0]}, [sp,:128]
2331	mov		r5, $magic			@ preserve magic
2332	mov		r2, $key
2333	mov		r4, $fp				@ preserve fp
2334
2335	bl		aes_nohw_decrypt
2336
2337	vld1.8		{@XMM[0]}, [sp,:128]
2338	veor		@XMM[0], @XMM[0], @XMM[8]
2339	vst1.8		{@XMM[0]}, [$out]!
2340	mov		$fp, r4
2341	mov		$magic, r5
2342
2343	vmov		@XMM[8], @XMM[9]		@ next round tweak
2344
2345.Lxts_dec_done:
2346#ifndef	XTS_CHAIN_TWEAK
2347	adds		$len, #0x10
2348	beq		.Lxts_dec_ret
2349
2350	@ calculate one round of extra tweak for the stolen ciphertext
2351	vldmia		$magic, {$twmask}
2352	vshr.s64	@XMM[6], @XMM[8], #63
2353	vand		@XMM[6], @XMM[6], $twmask
2354	vadd.u64	@XMM[9], @XMM[8], @XMM[8]
2355	vswp		`&Dhi("@XMM[6]")`,`&Dlo("@XMM[6]")`
2356	veor		@XMM[9], @XMM[9], @XMM[6]
2357
2358	@ perform the final decryption with the last tweak value
2359	vld1.8		{@XMM[0]}, [$inp]!
2360	mov		r0, sp
2361	veor		@XMM[0], @XMM[0], @XMM[9]
2362	mov		r1, sp
2363	vst1.8		{@XMM[0]}, [sp,:128]
2364	mov		r2, $key
2365	mov		r4, $fp			@ preserve fp
2366
2367	bl		aes_nohw_decrypt
2368
2369	vld1.8		{@XMM[0]}, [sp,:128]
2370	veor		@XMM[0], @XMM[0], @XMM[9]
2371	vst1.8		{@XMM[0]}, [$out]
2372
2373	mov		r6, $out
2374.Lxts_dec_steal:
2375	ldrb		r1, [$out]
2376	ldrb		r0, [$inp], #1
2377	strb		r1, [$out, #0x10]
2378	strb		r0, [$out], #1
2379
2380	subs		$len, #1
2381	bhi		.Lxts_dec_steal
2382
2383	vld1.8		{@XMM[0]}, [r6]
2384	mov		r0, sp
2385	veor		@XMM[0], @XMM[8]
2386	mov		r1, sp
2387	vst1.8		{@XMM[0]}, [sp,:128]
2388	mov		r2, $key
2389
2390	bl		aes_nohw_decrypt
2391
2392	vld1.8		{@XMM[0]}, [sp,:128]
2393	veor		@XMM[0], @XMM[0], @XMM[8]
2394	vst1.8		{@XMM[0]}, [r6]
2395	mov		$fp, r4
2396#endif
2397
2398.Lxts_dec_ret:
2399	bic		r0, $fp, #0xf
2400	vmov.i32	q0, #0
2401	vmov.i32	q1, #0
2402#ifdef	XTS_CHAIN_TWEAK
2403	ldr		r1, [$fp, #0x20+VFP_ABI_FRAME]	@ chain tweak
2404#endif
2405.Lxts_dec_bzero:				@ wipe key schedule [if any]
2406	vstmia		sp!, {q0-q1}
2407	cmp		sp, r0
2408	bne		.Lxts_dec_bzero
2409
2410	mov		sp, $fp
2411#ifdef	XTS_CHAIN_TWEAK
2412	vst1.8		{@XMM[8]}, [r1]
2413#endif
2414	VFP_ABI_POP
2415	ldmia		sp!, {r4-r10, pc}	@ return
2416
2417.size	bsaes_xts_decrypt,.-bsaes_xts_decrypt
2418___
2419}
2420$code.=<<___;
2421#endif
2422___
2423
2424$code =~ s/\`([^\`]*)\`/eval($1)/gem;
2425
2426open SELF,$0;
2427while(<SELF>) {
2428	next if (/^#!/);
2429        last if (!s/^#/@/ and !/^$/);
2430        print;
2431}
2432close SELF;
2433
2434print $code;
2435
2436close STDOUT or die "error closing STDOUT";
2437