Blame SOURCES/openssl-1.1.1-arm-update.patch

0c50f5
diff -up openssl-1.1.1i/crypto/aes/asm/aesv8-armx.pl.arm-update openssl-1.1.1i/crypto/aes/asm/aesv8-armx.pl
0c50f5
--- openssl-1.1.1i/crypto/aes/asm/aesv8-armx.pl.arm-update	2020-12-08 14:20:59.000000000 +0100
0c50f5
+++ openssl-1.1.1i/crypto/aes/asm/aesv8-armx.pl	2020-12-09 10:39:50.645705385 +0100
b63792
@@ -27,44 +27,72 @@
b63792
 # CBC encrypt case. On Cortex-A57 parallelizable mode performance
b63792
 # seems to be limited by sheer amount of NEON instructions...
b63792
 #
b63792
+# April 2019
b63792
+#
b63792
+# Key to performance of parallelize-able modes is round instruction
b63792
+# interleaving. But which factor to use? There is optimal one for
b63792
+# each combination of instruction latency and issue rate, beyond
b63792
+# which increasing interleave factor doesn't pay off. While on cons
b63792
+# side we have code size increase and resource waste on platforms for
b63792
+# which interleave factor is too high. In other words you want it to
b63792
+# be just right. So far interleave factor of 3x was serving well all
b63792
+# platforms. But for ThunderX2 optimal interleave factor was measured
b63792
+# to be 5x...
b63792
+#
b63792
 # Performance in cycles per byte processed with 128-bit key:
b63792
 #
b63792
 #		CBC enc		CBC dec		CTR
b63792
 # Apple A7	2.39		1.20		1.20
b63792
-# Cortex-A53	1.32		1.29		1.46
b63792
-# Cortex-A57(*)	1.95		0.85		0.93
b63792
-# Denver	1.96		0.86		0.80
b63792
-# Mongoose	1.33		1.20		1.20
b63792
-# Kryo		1.26		0.94		1.00
b63792
+# Cortex-A53	1.32		1.17/1.29(**)	1.36/1.46
b63792
+# Cortex-A57(*)	1.95		0.82/0.85	0.89/0.93
b63792
+# Cortex-A72	1.33		0.85/0.88	0.92/0.96
b63792
+# Denver	1.96		0.65/0.86	0.76/0.80
b63792
+# Mongoose	1.33		1.23/1.20	1.30/1.20
b63792
+# Kryo		1.26		0.87/0.94	1.00/1.00
b63792
+# ThunderX2	5.95		1.25		1.30
b63792
 #
b63792
 # (*)	original 3.64/1.34/1.32 results were for r0p0 revision
b63792
 #	and are still same even for updated module;
b63792
+# (**)	numbers after slash are for 32-bit code, which is 3x-
b63792
+#	interleaved;
b63792
 
b63792
-$flavour = shift;
b63792
-$output  = shift;
b63792
+# $output is the last argument if it looks like a file (it has an extension)
b63792
+# $flavour is the first argument if it doesn't look like a file
b63792
+$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
b63792
+$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
b63792
 
b63792
 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
b63792
 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
b63792
 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
b63792
 die "can't locate arm-xlate.pl";
b63792
 
b63792
-open OUT,"| \"$^X\" $xlate $flavour $output";
b63792
+open OUT,"| \"$^X\" $xlate $flavour \"$output\""
b63792
+    or die "can't call $xlate: $!";
b63792
 *STDOUT=*OUT;
b63792
 
b63792
 $prefix="aes_v8";
b63792
 
b63792
+$_byte = ($flavour =~ /win/ ? "DCB" : ".byte");
b63792
+
b63792
 $code=<<___;
b63792
 #include "arm_arch.h"
b63792
 
b63792
 #if __ARM_MAX_ARCH__>=7
b63792
-.text
b63792
 ___
b63792
-$code.=".arch	armv8-a+crypto\n"			if ($flavour =~ /64/);
b63792
+$code.=".arch	armv8-a+crypto\n.text\n"		if ($flavour =~ /64/);
b63792
 $code.=<<___						if ($flavour !~ /64/);
b63792
 .arch	armv7-a	// don't confuse not-so-latest binutils with argv8 :-)
b63792
 .fpu	neon
b63792
+#ifdef	__thumb2__
b63792
+.syntax	unified
b63792
+.thumb
b63792
+# define INST(a,b,c,d)	$_byte	c,d|0xc,a,b
b63792
+#else
b63792
 .code	32
b63792
-#undef	__thumb2__
b63792
+# define INST(a,b,c,d)	$_byte	a,b,c,d
b63792
+#endif
b63792
+
b63792
+.text
b63792
 ___
b63792
 
b63792
 # Assembler mnemonics are an eclectic mix of 32- and 64-bit syntax,
0c50f5
@@ -361,6 +389,836 @@ ___
0c50f5
 &gen_block("en");
0c50f5
 &gen_block("de");
0c50f5
 }}}
0c50f5
+
0c50f5
+# Performance in cycles per byte.
0c50f5
+# Processed with AES-ECB different key size.
0c50f5
+# It shows the value before and after optimization as below:
0c50f5
+# (before/after):
0c50f5
+#
0c50f5
+#		AES-128-ECB		AES-192-ECB		AES-256-ECB
0c50f5
+# Cortex-A57	1.85/0.82		2.16/0.96		2.47/1.10
0c50f5
+# Cortex-A72	1.64/0.85		1.82/0.99		2.13/1.14
0c50f5
+
0c50f5
+# Optimization is implemented by loop unrolling and interleaving.
0c50f5
+# Commonly, we choose the unrolling factor as 5, if the input
0c50f5
+# data size smaller than 5 blocks, but not smaller than 3 blocks,
0c50f5
+# choose 3 as the unrolling factor.
0c50f5
+# If the input data size dsize >= 5*16 bytes, then take 5 blocks
0c50f5
+# as one iteration, every loop the left size lsize -= 5*16.
0c50f5
+# If 5*16 > lsize >= 3*16 bytes, take 3 blocks as one iteration,
0c50f5
+# every loop lsize -=3*16.
0c50f5
+# If lsize < 3*16 bytes, treat them as the tail, interleave the
0c50f5
+# two blocks AES instructions.
0c50f5
+# There is one special case, if the original input data size dsize
0c50f5
+# = 16 bytes, we will treat it seperately to improve the
0c50f5
+# performance: one independent code block without LR, FP load and
0c50f5
+# store, just looks like what the original ECB implementation does.
0c50f5
+
0c50f5
+{{{
0c50f5
+my ($inp,$out,$len,$key)=map("x$_",(0..3));
0c50f5
+my ($enc,$rounds,$cnt,$key_,$step)=("w4","w5","w6","x7","x8");
0c50f5
+my ($dat0,$dat1,$in0,$in1,$tmp0,$tmp1,$tmp2,$rndlast)=map("q$_",(0..7));
0c50f5
+
0c50f5
+my ($dat,$tmp,$rndzero_n_last)=($dat0,$tmp0,$tmp1);
0c50f5
+
0c50f5
+### q7	last round key
0c50f5
+### q10-q15	q7 Last 7 round keys
0c50f5
+### q8-q9	preloaded round keys except last 7 keys for big size
0c50f5
+### q5, q6, q8-q9	preloaded round keys except last 7 keys for only 16 byte
0c50f5
+
0c50f5
+{
0c50f5
+my ($dat2,$in2,$tmp2)=map("q$_",(10,11,9));
b63792
+
b63792
+my ($dat3,$in3,$tmp3);	# used only in 64-bit mode
b63792
+my ($dat4,$in4,$tmp4);
b63792
+if ($flavour =~ /64/) {
b63792
+    ($dat2,$dat3,$dat4,$in2,$in3,$in4,$tmp3,$tmp4)=map("q$_",(16..23));
b63792
+}
b63792
+
0c50f5
+$code.=<<___;
0c50f5
+.globl	${prefix}_ecb_encrypt
0c50f5
+.type	${prefix}_ecb_encrypt,%function
0c50f5
+.align	5
0c50f5
+${prefix}_ecb_encrypt:
0c50f5
+___
0c50f5
+$code.=<<___	if ($flavour =~ /64/);
0c50f5
+	subs	$len,$len,#16
0c50f5
+	// Original input data size bigger than 16, jump to big size processing.
0c50f5
+	b.ne    .Lecb_big_size
0c50f5
+	vld1.8	{$dat0},[$inp]
0c50f5
+	cmp	$enc,#0					// en- or decrypting?
0c50f5
+	ldr	$rounds,[$key,#240]
0c50f5
+	vld1.32	{q5-q6},[$key],#32			// load key schedule...
0c50f5
+
0c50f5
+	b.eq .Lecb_small_dec
0c50f5
+	aese	$dat0,q5
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	vld1.32	{q8-q9},[$key],#32			// load key schedule...
0c50f5
+	aese	$dat0,q6
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	subs	$rounds,$rounds,#10			// if rounds==10, jump to aes-128-ecb processing
0c50f5
+	b.eq    .Lecb_128_enc
0c50f5
+.Lecb_round_loop:
0c50f5
+	aese	$dat0,q8
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	vld1.32	{q8},[$key],#16				// load key schedule...
0c50f5
+	aese	$dat0,q9
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	vld1.32	{q9},[$key],#16				// load key schedule...
0c50f5
+	subs	$rounds,$rounds,#2			// bias
0c50f5
+	b.gt    .Lecb_round_loop
0c50f5
+.Lecb_128_enc:
0c50f5
+	vld1.32	{q10-q11},[$key],#32		// load key schedule...
0c50f5
+	aese	$dat0,q8
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat0,q9
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	vld1.32	{q12-q13},[$key],#32		// load key schedule...
0c50f5
+	aese	$dat0,q10
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat0,q11
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	vld1.32	{q14-q15},[$key],#32		// load key schedule...
0c50f5
+	aese	$dat0,q12
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat0,q13
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	vld1.32	{$rndlast},[$key]
0c50f5
+	aese	$dat0,q14
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat0,q15
0c50f5
+	veor	$dat0,$dat0,$rndlast
0c50f5
+	vst1.8	{$dat0},[$out]
0c50f5
+	b	.Lecb_Final_abort
0c50f5
+.Lecb_small_dec:
0c50f5
+	aesd	$dat0,q5
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	vld1.32	{q8-q9},[$key],#32			// load key schedule...
0c50f5
+	aesd	$dat0,q6
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	subs	$rounds,$rounds,#10			// bias
0c50f5
+	b.eq    .Lecb_128_dec
0c50f5
+.Lecb_dec_round_loop:
0c50f5
+	aesd	$dat0,q8
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	vld1.32	{q8},[$key],#16				// load key schedule...
0c50f5
+	aesd	$dat0,q9
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	vld1.32	{q9},[$key],#16				// load key schedule...
0c50f5
+	subs	$rounds,$rounds,#2			// bias
0c50f5
+	b.gt    .Lecb_dec_round_loop
0c50f5
+.Lecb_128_dec:
0c50f5
+	vld1.32	{q10-q11},[$key],#32		// load key schedule...
0c50f5
+	aesd	$dat0,q8
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat0,q9
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	vld1.32	{q12-q13},[$key],#32		// load key schedule...
0c50f5
+	aesd	$dat0,q10
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat0,q11
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	vld1.32	{q14-q15},[$key],#32		// load key schedule...
0c50f5
+	aesd	$dat0,q12
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat0,q13
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	vld1.32	{$rndlast},[$key]
0c50f5
+	aesd	$dat0,q14
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat0,q15
0c50f5
+	veor	$dat0,$dat0,$rndlast
0c50f5
+	vst1.8	{$dat0},[$out]
0c50f5
+	b	.Lecb_Final_abort
0c50f5
+.Lecb_big_size:
0c50f5
+___
0c50f5
+$code.=<<___	if ($flavour =~ /64/);
0c50f5
+	stp	x29,x30,[sp,#-16]!
0c50f5
+	add	x29,sp,#0
0c50f5
+___
0c50f5
+$code.=<<___	if ($flavour !~ /64/);
0c50f5
+	mov	ip,sp
0c50f5
+	stmdb	sp!,{r4-r8,lr}
0c50f5
+	vstmdb	sp!,{d8-d15}			@ ABI specification says so
0c50f5
+	ldmia	ip,{r4-r5}			@ load remaining args
0c50f5
+	subs	$len,$len,#16
0c50f5
+___
0c50f5
+$code.=<<___;
0c50f5
+	mov	$step,#16
0c50f5
+	b.lo	.Lecb_done
0c50f5
+	cclr	$step,eq
0c50f5
+
0c50f5
+	cmp	$enc,#0					// en- or decrypting?
0c50f5
+	ldr	$rounds,[$key,#240]
0c50f5
+	and	$len,$len,#-16
0c50f5
+	vld1.8	{$dat},[$inp],$step
0c50f5
+
0c50f5
+	vld1.32	{q8-q9},[$key]				// load key schedule...
0c50f5
+	sub	$rounds,$rounds,#6
0c50f5
+	add	$key_,$key,x5,lsl#4				// pointer to last 7 round keys
0c50f5
+	sub	$rounds,$rounds,#2
0c50f5
+	vld1.32	{q10-q11},[$key_],#32
0c50f5
+	vld1.32	{q12-q13},[$key_],#32
0c50f5
+	vld1.32	{q14-q15},[$key_],#32
0c50f5
+	vld1.32	{$rndlast},[$key_]
0c50f5
+
0c50f5
+	add	$key_,$key,#32
0c50f5
+	mov	$cnt,$rounds
0c50f5
+	b.eq	.Lecb_dec
0c50f5
+
0c50f5
+	vld1.8	{$dat1},[$inp],#16
0c50f5
+	subs	$len,$len,#32				// bias
0c50f5
+	add	$cnt,$rounds,#2
0c50f5
+	vorr	$in1,$dat1,$dat1
0c50f5
+	vorr	$dat2,$dat1,$dat1
0c50f5
+	vorr	$dat1,$dat,$dat
0c50f5
+	b.lo	.Lecb_enc_tail
0c50f5
+
0c50f5
+	vorr	$dat1,$in1,$in1
0c50f5
+	vld1.8	{$dat2},[$inp],#16
b63792
+___
b63792
+$code.=<<___	if ($flavour =~ /64/);
b63792
+	cmp	$len,#32
0c50f5
+	b.lo	.Loop3x_ecb_enc
b63792
+
b63792
+	vld1.8	{$dat3},[$inp],#16
b63792
+	vld1.8	{$dat4},[$inp],#16
0c50f5
+	sub	$len,$len,#32				// bias
b63792
+	mov	$cnt,$rounds
b63792
+
0c50f5
+.Loop5x_ecb_enc:
0c50f5
+	aese	$dat0,q8
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q8
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q8
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q8
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q8
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+	vld1.32	{q8},[$key_],#16
0c50f5
+	subs	$cnt,$cnt,#2
0c50f5
+	aese	$dat0,q9
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q9
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q9
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q9
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q9
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+	vld1.32	{q9},[$key_],#16
0c50f5
+	b.gt	.Loop5x_ecb_enc
0c50f5
+
0c50f5
+	aese	$dat0,q8
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q8
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q8
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q8
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q8
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+	cmp	$len,#0x40					// because .Lecb_enc_tail4x
0c50f5
+	sub	$len,$len,#0x50
0c50f5
+
0c50f5
+	aese	$dat0,q9
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q9
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q9
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q9
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q9
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+	csel	x6,xzr,$len,gt			// borrow x6, $cnt, "gt" is not typo
0c50f5
+	mov	$key_,$key
0c50f5
+
0c50f5
+	aese	$dat0,q10
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q10
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q10
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q10
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q10
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+	add	$inp,$inp,x6				// $inp is adjusted in such way that
0c50f5
+							// at exit from the loop $dat1-$dat4
0c50f5
+							// are loaded with last "words"
0c50f5
+	add	x6,$len,#0x60		    // because .Lecb_enc_tail4x
0c50f5
+
0c50f5
+	aese	$dat0,q11
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q11
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q11
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q11
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q11
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+
0c50f5
+	aese	$dat0,q12
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q12
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q12
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q12
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q12
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+
0c50f5
+	aese	$dat0,q13
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q13
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q13
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q13
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q13
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+
0c50f5
+	aese	$dat0,q14
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q14
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q14
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q14
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q14
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+
0c50f5
+	aese	$dat0,q15
0c50f5
+	vld1.8	{$in0},[$inp],#16
0c50f5
+	aese	$dat1,q15
0c50f5
+	vld1.8	{$in1},[$inp],#16
0c50f5
+	aese	$dat2,q15
0c50f5
+	vld1.8	{$in2},[$inp],#16
0c50f5
+	aese	$dat3,q15
0c50f5
+	vld1.8	{$in3},[$inp],#16
0c50f5
+	aese	$dat4,q15
0c50f5
+	vld1.8	{$in4},[$inp],#16
0c50f5
+	cbz	x6,.Lecb_enc_tail4x
0c50f5
+	vld1.32 {q8},[$key_],#16			// re-pre-load rndkey[0]
0c50f5
+	veor	$tmp0,$rndlast,$dat0
0c50f5
+	vorr	$dat0,$in0,$in0
0c50f5
+	veor	$tmp1,$rndlast,$dat1
0c50f5
+	vorr	$dat1,$in1,$in1
0c50f5
+	veor	$tmp2,$rndlast,$dat2
0c50f5
+	vorr	$dat2,$in2,$in2
0c50f5
+	veor	$tmp3,$rndlast,$dat3
0c50f5
+	vorr	$dat3,$in3,$in3
0c50f5
+	veor	$tmp4,$rndlast,$dat4
0c50f5
+	vst1.8	{$tmp0},[$out],#16
0c50f5
+	vorr	$dat4,$in4,$in4
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	mov	$cnt,$rounds
0c50f5
+	vst1.8	{$tmp2},[$out],#16
0c50f5
+	vld1.32 {q9},[$key_],#16			// re-pre-load rndkey[1]
0c50f5
+	vst1.8	{$tmp3},[$out],#16
0c50f5
+	vst1.8	{$tmp4},[$out],#16
0c50f5
+	b.hs	.Loop5x_ecb_enc
0c50f5
+
0c50f5
+	add	$len,$len,#0x50
0c50f5
+	cbz	$len,.Lecb_done
0c50f5
+
0c50f5
+	add	$cnt,$rounds,#2
0c50f5
+	subs	$len,$len,#0x30
0c50f5
+	vorr	$dat0,$in2,$in2
0c50f5
+	vorr	$dat1,$in3,$in3
0c50f5
+	vorr	$dat2,$in4,$in4
0c50f5
+	b.lo	.Lecb_enc_tail
0c50f5
+
0c50f5
+	b	.Loop3x_ecb_enc
0c50f5
+
0c50f5
+.align	4
0c50f5
+.Lecb_enc_tail4x:
0c50f5
+	veor	$tmp1,$rndlast,$dat1
0c50f5
+	veor	$tmp2,$rndlast,$dat2
0c50f5
+	veor	$tmp3,$rndlast,$dat3
0c50f5
+	veor	$tmp4,$rndlast,$dat4
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	vst1.8	{$tmp2},[$out],#16
0c50f5
+	vst1.8	{$tmp3},[$out],#16
0c50f5
+	vst1.8	{$tmp4},[$out],#16
0c50f5
+
0c50f5
+	b	.Lecb_done
0c50f5
+.align	4
0c50f5
+___
0c50f5
+$code.=<<___;
0c50f5
+.Loop3x_ecb_enc:
0c50f5
+	aese	$dat0,q8
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q8
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q8
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	vld1.32	{q8},[$key_],#16
0c50f5
+	subs	$cnt,$cnt,#2
0c50f5
+	aese	$dat0,q9
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q9
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q9
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	vld1.32	{q9},[$key_],#16
0c50f5
+	b.gt	.Loop3x_ecb_enc
0c50f5
+
0c50f5
+	aese	$dat0,q8
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q8
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q8
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	subs	$len,$len,#0x30
0c50f5
+	mov.lo	x6,$len				// x6, $cnt, is zero at this point
0c50f5
+	aese	$dat0,q9
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q9
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q9
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	add	$inp,$inp,x6			// $inp is adjusted in such way that
0c50f5
+						// at exit from the loop $dat1-$dat2
0c50f5
+						// are loaded with last "words"
0c50f5
+	mov	$key_,$key
0c50f5
+	aese	$dat0,q12
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q12
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q12
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	vld1.8	{$in0},[$inp],#16
0c50f5
+	aese	$dat0,q13
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q13
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q13
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	vld1.8	{$in1},[$inp],#16
0c50f5
+	aese	$dat0,q14
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q14
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q14
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	vld1.8	{$in2},[$inp],#16
0c50f5
+	aese	$dat0,q15
0c50f5
+	aese	$dat1,q15
0c50f5
+	aese	$dat2,q15
0c50f5
+	vld1.32 {q8},[$key_],#16		// re-pre-load rndkey[0]
0c50f5
+	add	$cnt,$rounds,#2
0c50f5
+	veor	$tmp0,$rndlast,$dat0
0c50f5
+	veor	$tmp1,$rndlast,$dat1
0c50f5
+	veor	$dat2,$dat2,$rndlast
0c50f5
+	vld1.32 {q9},[$key_],#16		// re-pre-load rndkey[1]
0c50f5
+	vst1.8	{$tmp0},[$out],#16
0c50f5
+	vorr	$dat0,$in0,$in0
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	vorr	$dat1,$in1,$in1
0c50f5
+	vst1.8	{$dat2},[$out],#16
0c50f5
+	vorr	$dat2,$in2,$in2
0c50f5
+	b.hs	.Loop3x_ecb_enc
0c50f5
+
0c50f5
+	cmn	$len,#0x30
0c50f5
+	b.eq	.Lecb_done
0c50f5
+	nop
0c50f5
+
0c50f5
+.Lecb_enc_tail:
0c50f5
+	aese	$dat1,q8
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q8
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	vld1.32	{q8},[$key_],#16
0c50f5
+	subs	$cnt,$cnt,#2
0c50f5
+	aese	$dat1,q9
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q9
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	vld1.32	{q9},[$key_],#16
0c50f5
+	b.gt	.Lecb_enc_tail
0c50f5
+
0c50f5
+	aese	$dat1,q8
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q8
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat1,q9
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q9
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat1,q12
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q12
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	cmn	$len,#0x20
0c50f5
+	aese	$dat1,q13
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q13
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat1,q14
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q14
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat1,q15
0c50f5
+	aese	$dat2,q15
0c50f5
+	b.eq	.Lecb_enc_one
0c50f5
+	veor	$tmp1,$rndlast,$dat1
0c50f5
+	veor	$tmp2,$rndlast,$dat2
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	vst1.8	{$tmp2},[$out],#16
0c50f5
+	b	.Lecb_done
0c50f5
+
0c50f5
+.Lecb_enc_one:
0c50f5
+	veor	$tmp1,$rndlast,$dat2
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	b	.Lecb_done
0c50f5
+___
0c50f5
+
0c50f5
+$code.=<<___;
0c50f5
+.align	5
0c50f5
+.Lecb_dec:
0c50f5
+	vld1.8	{$dat1},[$inp],#16
0c50f5
+	subs	$len,$len,#32			// bias
0c50f5
+	add	$cnt,$rounds,#2
0c50f5
+	vorr	$in1,$dat1,$dat1
0c50f5
+	vorr	$dat2,$dat1,$dat1
0c50f5
+	vorr	$dat1,$dat,$dat
0c50f5
+	b.lo	.Lecb_dec_tail
0c50f5
+
0c50f5
+	vorr	$dat1,$in1,$in1
0c50f5
+	vld1.8	{$dat2},[$inp],#16
0c50f5
+___
0c50f5
+$code.=<<___	if ($flavour =~ /64/);
0c50f5
+	cmp	$len,#32
0c50f5
+	b.lo	.Loop3x_ecb_dec
0c50f5
+
0c50f5
+	vld1.8	{$dat3},[$inp],#16
0c50f5
+	vld1.8	{$dat4},[$inp],#16
0c50f5
+	sub	$len,$len,#32				// bias
0c50f5
+	mov	$cnt,$rounds
0c50f5
+
0c50f5
+.Loop5x_ecb_dec:
b63792
+	aesd	$dat0,q8
b63792
+	aesimc	$dat0,$dat0
b63792
+	aesd	$dat1,q8
b63792
+	aesimc	$dat1,$dat1
b63792
+	aesd	$dat2,q8
b63792
+	aesimc	$dat2,$dat2
b63792
+	aesd	$dat3,q8
b63792
+	aesimc	$dat3,$dat3
b63792
+	aesd	$dat4,q8
b63792
+	aesimc	$dat4,$dat4
b63792
+	vld1.32	{q8},[$key_],#16
b63792
+	subs	$cnt,$cnt,#2
b63792
+	aesd	$dat0,q9
b63792
+	aesimc	$dat0,$dat0
b63792
+	aesd	$dat1,q9
b63792
+	aesimc	$dat1,$dat1
b63792
+	aesd	$dat2,q9
b63792
+	aesimc	$dat2,$dat2
b63792
+	aesd	$dat3,q9
b63792
+	aesimc	$dat3,$dat3
b63792
+	aesd	$dat4,q9
b63792
+	aesimc	$dat4,$dat4
b63792
+	vld1.32	{q9},[$key_],#16
0c50f5
+	b.gt	.Loop5x_ecb_dec
b63792
+
b63792
+	aesd	$dat0,q8
b63792
+	aesimc	$dat0,$dat0
b63792
+	aesd	$dat1,q8
b63792
+	aesimc	$dat1,$dat1
b63792
+	aesd	$dat2,q8
b63792
+	aesimc	$dat2,$dat2
b63792
+	aesd	$dat3,q8
b63792
+	aesimc	$dat3,$dat3
b63792
+	aesd	$dat4,q8
b63792
+	aesimc	$dat4,$dat4
0c50f5
+	cmp	$len,#0x40				// because .Lecb_tail4x
0c50f5
+	sub	$len,$len,#0x50
b63792
+
b63792
+	aesd	$dat0,q9
b63792
+	aesimc	$dat0,$dat0
b63792
+	aesd	$dat1,q9
b63792
+	aesimc	$dat1,$dat1
b63792
+	aesd	$dat2,q9
b63792
+	aesimc	$dat2,$dat2
b63792
+	aesd	$dat3,q9
b63792
+	aesimc	$dat3,$dat3
b63792
+	aesd	$dat4,q9
b63792
+	aesimc	$dat4,$dat4
0c50f5
+	csel	x6,xzr,$len,gt		// borrow x6, $cnt, "gt" is not typo
0c50f5
+	mov	$key_,$key
b63792
+
b63792
+	aesd	$dat0,q10
b63792
+	aesimc	$dat0,$dat0
b63792
+	aesd	$dat1,q10
b63792
+	aesimc	$dat1,$dat1
b63792
+	aesd	$dat2,q10
b63792
+	aesimc	$dat2,$dat2
b63792
+	aesd	$dat3,q10
b63792
+	aesimc	$dat3,$dat3
b63792
+	aesd	$dat4,q10
b63792
+	aesimc	$dat4,$dat4
0c50f5
+	add	$inp,$inp,x6				// $inp is adjusted in such way that
0c50f5
+							// at exit from the loop $dat1-$dat4
0c50f5
+							// are loaded with last "words"
0c50f5
+	add	x6,$len,#0x60			// because .Lecb_tail4x
b63792
+
b63792
+	aesd	$dat0,q11
b63792
+	aesimc	$dat0,$dat0
b63792
+	aesd	$dat1,q11
b63792
+	aesimc	$dat1,$dat1
b63792
+	aesd	$dat2,q11
b63792
+	aesimc	$dat2,$dat2
b63792
+	aesd	$dat3,q11
b63792
+	aesimc	$dat3,$dat3
b63792
+	aesd	$dat4,q11
b63792
+	aesimc	$dat4,$dat4
b63792
+
b63792
+	aesd	$dat0,q12
b63792
+	aesimc	$dat0,$dat0
b63792
+	aesd	$dat1,q12
b63792
+	aesimc	$dat1,$dat1
b63792
+	aesd	$dat2,q12
b63792
+	aesimc	$dat2,$dat2
b63792
+	aesd	$dat3,q12
b63792
+	aesimc	$dat3,$dat3
b63792
+	aesd	$dat4,q12
b63792
+	aesimc	$dat4,$dat4
b63792
+
b63792
+	aesd	$dat0,q13
b63792
+	aesimc	$dat0,$dat0
b63792
+	aesd	$dat1,q13
b63792
+	aesimc	$dat1,$dat1
b63792
+	aesd	$dat2,q13
b63792
+	aesimc	$dat2,$dat2
b63792
+	aesd	$dat3,q13
b63792
+	aesimc	$dat3,$dat3
b63792
+	aesd	$dat4,q13
b63792
+	aesimc	$dat4,$dat4
b63792
+
b63792
+	aesd	$dat0,q14
b63792
+	aesimc	$dat0,$dat0
b63792
+	aesd	$dat1,q14
b63792
+	aesimc	$dat1,$dat1
b63792
+	aesd	$dat2,q14
b63792
+	aesimc	$dat2,$dat2
b63792
+	aesd	$dat3,q14
b63792
+	aesimc	$dat3,$dat3
b63792
+	aesd	$dat4,q14
b63792
+	aesimc	$dat4,$dat4
b63792
+
b63792
+	aesd	$dat0,q15
0c50f5
+	vld1.8	{$in0},[$inp],#16
b63792
+	aesd	$dat1,q15
0c50f5
+	vld1.8	{$in1},[$inp],#16
b63792
+	aesd	$dat2,q15
0c50f5
+	vld1.8	{$in2},[$inp],#16
b63792
+	aesd	$dat3,q15
0c50f5
+	vld1.8	{$in3},[$inp],#16
b63792
+	aesd	$dat4,q15
0c50f5
+	vld1.8	{$in4},[$inp],#16
0c50f5
+	cbz	x6,.Lecb_tail4x
0c50f5
+	vld1.32 {q8},[$key_],#16			// re-pre-load rndkey[0]
0c50f5
+	veor	$tmp0,$rndlast,$dat0
0c50f5
+	vorr	$dat0,$in0,$in0
0c50f5
+	veor	$tmp1,$rndlast,$dat1
0c50f5
+	vorr	$dat1,$in1,$in1
0c50f5
+	veor	$tmp2,$rndlast,$dat2
0c50f5
+	vorr	$dat2,$in2,$in2
0c50f5
+	veor	$tmp3,$rndlast,$dat3
0c50f5
+	vorr	$dat3,$in3,$in3
0c50f5
+	veor	$tmp4,$rndlast,$dat4
b63792
+	vst1.8	{$tmp0},[$out],#16
0c50f5
+	vorr	$dat4,$in4,$in4
b63792
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	mov	$cnt,$rounds
b63792
+	vst1.8	{$tmp2},[$out],#16
0c50f5
+	vld1.32 {q9},[$key_],#16			// re-pre-load rndkey[1]
b63792
+	vst1.8	{$tmp3},[$out],#16
b63792
+	vst1.8	{$tmp4},[$out],#16
0c50f5
+	b.hs	.Loop5x_ecb_dec
b63792
+
b63792
+	add	$len,$len,#0x50
0c50f5
+	cbz	$len,.Lecb_done
b63792
+
b63792
+	add	$cnt,$rounds,#2
b63792
+	subs	$len,$len,#0x30
b63792
+	vorr	$dat0,$in2,$in2
b63792
+	vorr	$dat1,$in3,$in3
b63792
+	vorr	$dat2,$in4,$in4
0c50f5
+	b.lo	.Lecb_dec_tail
0c50f5
+
0c50f5
+	b	.Loop3x_ecb_dec
b63792
+
b63792
+.align	4
0c50f5
+.Lecb_tail4x:
0c50f5
+	veor	$tmp1,$rndlast,$dat1
0c50f5
+	veor	$tmp2,$rndlast,$dat2
0c50f5
+	veor	$tmp3,$rndlast,$dat3
0c50f5
+	veor	$tmp4,$rndlast,$dat4
b63792
+	vst1.8	{$tmp1},[$out],#16
b63792
+	vst1.8	{$tmp2},[$out],#16
b63792
+	vst1.8	{$tmp3},[$out],#16
b63792
+	vst1.8	{$tmp4},[$out],#16
b63792
+
0c50f5
+	b	.Lecb_done
b63792
+.align	4
b63792
+___
b63792
+$code.=<<___;
0c50f5
+.Loop3x_ecb_dec:
0c50f5
+	aesd	$dat0,q8
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q8
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q8
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	vld1.32	{q8},[$key_],#16
0c50f5
+	subs	$cnt,$cnt,#2
0c50f5
+	aesd	$dat0,q9
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q9
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q9
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	vld1.32	{q9},[$key_],#16
0c50f5
+	b.gt	.Loop3x_ecb_dec
b63792
+
0c50f5
+	aesd	$dat0,q8
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q8
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q8
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	subs	$len,$len,#0x30
0c50f5
+	mov.lo	x6,$len				// x6, $cnt, is zero at this point
0c50f5
+	aesd	$dat0,q9
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q9
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q9
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	add	$inp,$inp,x6 			// $inp is adjusted in such way that
0c50f5
+						// at exit from the loop $dat1-$dat2
0c50f5
+						// are loaded with last "words"
0c50f5
+	mov	$key_,$key
0c50f5
+	aesd	$dat0,q12
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q12
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q12
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	vld1.8	{$in0},[$inp],#16
0c50f5
+	aesd	$dat0,q13
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q13
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q13
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	vld1.8	{$in1},[$inp],#16
0c50f5
+	aesd	$dat0,q14
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q14
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q14
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	vld1.8	{$in2},[$inp],#16
0c50f5
+	aesd	$dat0,q15
0c50f5
+	aesd	$dat1,q15
0c50f5
+	aesd	$dat2,q15
0c50f5
+	vld1.32 {q8},[$key_],#16			// re-pre-load rndkey[0]
0c50f5
+	add	$cnt,$rounds,#2
0c50f5
+	veor	$tmp0,$rndlast,$dat0
0c50f5
+	veor	$tmp1,$rndlast,$dat1
0c50f5
+	veor	$dat2,$dat2,$rndlast
0c50f5
+	vld1.32 {q9},[$key_],#16			// re-pre-load rndkey[1]
0c50f5
+	vst1.8	{$tmp0},[$out],#16
0c50f5
+	vorr	$dat0,$in0,$in0
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	vorr	$dat1,$in1,$in1
0c50f5
+	vst1.8	{$dat2},[$out],#16
0c50f5
+	vorr	$dat2,$in2,$in2
0c50f5
+	b.hs	.Loop3x_ecb_dec
b63792
+
0c50f5
+	cmn	$len,#0x30
0c50f5
+	b.eq	.Lecb_done
0c50f5
+	nop
b63792
+
0c50f5
+.Lecb_dec_tail:
0c50f5
+	aesd	$dat1,q8
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q8
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	vld1.32	{q8},[$key_],#16
0c50f5
+	subs	$cnt,$cnt,#2
0c50f5
+	aesd	$dat1,q9
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q9
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	vld1.32	{q9},[$key_],#16
0c50f5
+	b.gt	.Lecb_dec_tail
0c50f5
+
0c50f5
+	aesd	$dat1,q8
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q8
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat1,q9
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q9
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat1,q12
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q12
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	cmn	$len,#0x20
0c50f5
+	aesd	$dat1,q13
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q13
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat1,q14
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q14
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat1,q15
0c50f5
+	aesd	$dat2,q15
0c50f5
+	b.eq	.Lecb_dec_one
0c50f5
+	veor	$tmp1,$rndlast,$dat1
0c50f5
+	veor	$tmp2,$rndlast,$dat2
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	vst1.8	{$tmp2},[$out],#16
0c50f5
+	b	.Lecb_done
0c50f5
+
0c50f5
+.Lecb_dec_one:
0c50f5
+	veor	$tmp1,$rndlast,$dat2
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+
0c50f5
+.Lecb_done:
0c50f5
+___
0c50f5
+}
0c50f5
+$code.=<<___	if ($flavour !~ /64/);
0c50f5
+	vldmia	sp!,{d8-d15}
0c50f5
+	ldmia	sp!,{r4-r8,pc}
0c50f5
+___
0c50f5
+$code.=<<___	if ($flavour =~ /64/);
0c50f5
+	ldr	x29,[sp],#16
0c50f5
+___
0c50f5
+$code.=<<___	if ($flavour =~ /64/);
0c50f5
+.Lecb_Final_abort:
0c50f5
+	ret
0c50f5
+___
0c50f5
+$code.=<<___;
0c50f5
+.size	${prefix}_ecb_encrypt,.-${prefix}_ecb_encrypt
0c50f5
+___
0c50f5
+}}}
0c50f5
 {{{
0c50f5
 my ($inp,$out,$len,$key,$ivp)=map("x$_",(0..4)); my $enc="w5";
0c50f5
 my ($rounds,$cnt,$key_,$step,$step1)=($enc,"w6","x7","x8","x12");
0c50f5
@@ -519,6 +1377,13 @@ $code.=<<___;
0c50f5
 ___
0c50f5
 {
0c50f5
 my ($dat2,$in2,$tmp2)=map("q$_",(10,11,9));
0c50f5
+
0c50f5
+my ($dat3,$in3,$tmp3);	# used only in 64-bit mode
0c50f5
+my ($dat4,$in4,$tmp4);
0c50f5
+if ($flavour =~ /64/) {
0c50f5
+    ($dat2,$dat3,$dat4,$in2,$in3,$in4,$tmp3,$tmp4)=map("q$_",(16..23));
0c50f5
+}
0c50f5
+
0c50f5
 $code.=<<___;
0c50f5
 .align	5
0c50f5
 .Lcbc_dec:
0c50f5
@@ -535,7 +1400,196 @@ $code.=<<___;
0c50f5
 	vorr	$in0,$dat,$dat
0c50f5
 	vorr	$in1,$dat1,$dat1
0c50f5
 	vorr	$in2,$dat2,$dat2
0c50f5
+___
0c50f5
+$code.=<<___	if ($flavour =~ /64/);
0c50f5
+	cmp	$len,#32
0c50f5
+	b.lo	.Loop3x_cbc_dec
0c50f5
+
0c50f5
+	vld1.8	{$dat3},[$inp],#16
0c50f5
+	vld1.8	{$dat4},[$inp],#16
0c50f5
+	sub	$len,$len,#32		// bias
0c50f5
+	mov	$cnt,$rounds
0c50f5
+	vorr	$in3,$dat3,$dat3
0c50f5
+	vorr	$in4,$dat4,$dat4
0c50f5
+
0c50f5
+.Loop5x_cbc_dec:
0c50f5
+	aesd	$dat0,q8
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q8
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q8
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q8
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q8
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+	vld1.32	{q8},[$key_],#16
0c50f5
+	subs	$cnt,$cnt,#2
0c50f5
+	aesd	$dat0,q9
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q9
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q9
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q9
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q9
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+	vld1.32	{q9},[$key_],#16
0c50f5
+	b.gt	.Loop5x_cbc_dec
0c50f5
+
0c50f5
+	aesd	$dat0,q8
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q8
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q8
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q8
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q8
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+	 cmp	$len,#0x40		// because .Lcbc_tail4x
0c50f5
+	 sub	$len,$len,#0x50
0c50f5
+
0c50f5
+	aesd	$dat0,q9
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q9
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q9
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q9
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q9
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+	 csel	x6,xzr,$len,gt		// borrow x6, $cnt, "gt" is not typo
0c50f5
+	 mov	$key_,$key
0c50f5
+
0c50f5
+	aesd	$dat0,q10
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q10
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q10
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q10
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q10
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+	 add	$inp,$inp,x6		// $inp is adjusted in such way that
0c50f5
+					// at exit from the loop $dat1-$dat4
0c50f5
+					// are loaded with last "words"
0c50f5
+	 add	x6,$len,#0x60		// because .Lcbc_tail4x
0c50f5
+
0c50f5
+	aesd	$dat0,q11
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q11
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q11
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q11
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q11
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+
0c50f5
+	aesd	$dat0,q12
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q12
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q12
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q12
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q12
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+
0c50f5
+	aesd	$dat0,q13
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q13
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q13
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q13
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q13
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+
0c50f5
+	aesd	$dat0,q14
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q14
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q14
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q14
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q14
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
 
0c50f5
+	 veor	$tmp0,$ivec,$rndlast
0c50f5
+	aesd	$dat0,q15
0c50f5
+	 veor	$tmp1,$in0,$rndlast
0c50f5
+	 vld1.8	{$in0},[$inp],#16
0c50f5
+	aesd	$dat1,q15
0c50f5
+	 veor	$tmp2,$in1,$rndlast
0c50f5
+	 vld1.8	{$in1},[$inp],#16
0c50f5
+	aesd	$dat2,q15
0c50f5
+	 veor	$tmp3,$in2,$rndlast
0c50f5
+	 vld1.8	{$in2},[$inp],#16
0c50f5
+	aesd	$dat3,q15
0c50f5
+	 veor	$tmp4,$in3,$rndlast
0c50f5
+	 vld1.8	{$in3},[$inp],#16
0c50f5
+	aesd	$dat4,q15
0c50f5
+	 vorr	$ivec,$in4,$in4
0c50f5
+	 vld1.8	{$in4},[$inp],#16
0c50f5
+	cbz	x6,.Lcbc_tail4x
0c50f5
+	 vld1.32 {q8},[$key_],#16	// re-pre-load rndkey[0]
0c50f5
+	veor	$tmp0,$tmp0,$dat0
0c50f5
+	 vorr	$dat0,$in0,$in0
0c50f5
+	veor	$tmp1,$tmp1,$dat1
0c50f5
+	 vorr	$dat1,$in1,$in1
0c50f5
+	veor	$tmp2,$tmp2,$dat2
0c50f5
+	 vorr	$dat2,$in2,$in2
0c50f5
+	veor	$tmp3,$tmp3,$dat3
0c50f5
+	 vorr	$dat3,$in3,$in3
0c50f5
+	veor	$tmp4,$tmp4,$dat4
0c50f5
+	vst1.8	{$tmp0},[$out],#16
0c50f5
+	 vorr	$dat4,$in4,$in4
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	 mov	$cnt,$rounds
0c50f5
+	vst1.8	{$tmp2},[$out],#16
0c50f5
+	 vld1.32 {q9},[$key_],#16	// re-pre-load rndkey[1]
0c50f5
+	vst1.8	{$tmp3},[$out],#16
0c50f5
+	vst1.8	{$tmp4},[$out],#16
0c50f5
+	b.hs	.Loop5x_cbc_dec
0c50f5
+
0c50f5
+	add	$len,$len,#0x50
0c50f5
+	cbz	$len,.Lcbc_done
0c50f5
+
0c50f5
+	add	$cnt,$rounds,#2
0c50f5
+	subs	$len,$len,#0x30
0c50f5
+	vorr	$dat0,$in2,$in2
0c50f5
+	vorr	$in0,$in2,$in2
0c50f5
+	vorr	$dat1,$in3,$in3
0c50f5
+	vorr	$in1,$in3,$in3
0c50f5
+	vorr	$dat2,$in4,$in4
0c50f5
+	vorr	$in2,$in4,$in4
0c50f5
+	b.lo	.Lcbc_dec_tail
0c50f5
+
0c50f5
+	b	.Loop3x_cbc_dec
0c50f5
+
0c50f5
+.align	4
0c50f5
+.Lcbc_tail4x:
0c50f5
+	veor	$tmp1,$tmp0,$dat1
0c50f5
+	veor	$tmp2,$tmp2,$dat2
0c50f5
+	veor	$tmp3,$tmp3,$dat3
0c50f5
+	veor	$tmp4,$tmp4,$dat4
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	vst1.8	{$tmp2},[$out],#16
0c50f5
+	vst1.8	{$tmp3},[$out],#16
0c50f5
+	vst1.8	{$tmp4},[$out],#16
0c50f5
+
0c50f5
+	b	.Lcbc_done
0c50f5
+.align	4
0c50f5
+___
0c50f5
+$code.=<<___;
0c50f5
 .Loop3x_cbc_dec:
0c50f5
 	aesd	$dat0,q8
0c50f5
 	aesimc	$dat0,$dat0
0c50f5
@@ -696,6 +1750,9 @@ my $step="x12";		# aliases with $tctr2
0c50f5
 my ($dat0,$dat1,$in0,$in1,$tmp0,$tmp1,$ivec,$rndlast)=map("q$_",(0..7));
0c50f5
 my ($dat2,$in2,$tmp2)=map("q$_",(10,11,9));
0c50f5
 
0c50f5
+# used only in 64-bit mode...
0c50f5
+my ($dat3,$dat4,$in3,$in4)=map("q$_",(16..23));
0c50f5
+
0c50f5
 my ($dat,$tmp)=($dat0,$tmp0);
0c50f5
 
0c50f5
 ### q8-q15	preloaded key schedule
0c50f5
@@ -751,6 +1808,175 @@ $code.=<<___;
0c50f5
 	vmov.32		${ivec}[3],$tctr2
0c50f5
 	sub		$len,$len,#3		// bias
0c50f5
 	vorr		$dat2,$ivec,$ivec
0c50f5
+___
0c50f5
+$code.=<<___	if ($flavour =~ /64/);
0c50f5
+	cmp		$len,#2
0c50f5
+	b.lo		.Loop3x_ctr32
0c50f5
+
0c50f5
+	add		w13,$ctr,#1
0c50f5
+	add		w14,$ctr,#2
0c50f5
+	vorr		$dat3,$dat0,$dat0
0c50f5
+	rev		w13,w13
0c50f5
+	vorr		$dat4,$dat0,$dat0
0c50f5
+	rev		w14,w14
0c50f5
+	vmov.32		${dat3}[3],w13
0c50f5
+	sub		$len,$len,#2		// bias
0c50f5
+	vmov.32		${dat4}[3],w14
0c50f5
+	add		$ctr,$ctr,#2
0c50f5
+	b		.Loop5x_ctr32
0c50f5
+
0c50f5
+.align	4
0c50f5
+.Loop5x_ctr32:
b63792
+	aese		$dat0,q8
b63792
+	aesmc		$dat0,$dat0
b63792
+	aese		$dat1,q8
b63792
+	aesmc		$dat1,$dat1
b63792
+	aese		$dat2,q8
b63792
+	aesmc		$dat2,$dat2
b63792
+	aese		$dat3,q8
b63792
+	aesmc		$dat3,$dat3
b63792
+	aese		$dat4,q8
b63792
+	aesmc		$dat4,$dat4
b63792
+	vld1.32		{q8},[$key_],#16
b63792
+	subs		$cnt,$cnt,#2
b63792
+	aese		$dat0,q9
b63792
+	aesmc		$dat0,$dat0
b63792
+	aese		$dat1,q9
b63792
+	aesmc		$dat1,$dat1
b63792
+	aese		$dat2,q9
b63792
+	aesmc		$dat2,$dat2
b63792
+	aese		$dat3,q9
b63792
+	aesmc		$dat3,$dat3
b63792
+	aese		$dat4,q9
b63792
+	aesmc		$dat4,$dat4
b63792
+	vld1.32		{q9},[$key_],#16
b63792
+	b.gt		.Loop5x_ctr32
b63792
+
0c50f5
+	mov		$key_,$key
0c50f5
+	aese		$dat0,q8
0c50f5
+	aesmc		$dat0,$dat0
0c50f5
+	aese		$dat1,q8
0c50f5
+	aesmc		$dat1,$dat1
0c50f5
+	aese		$dat2,q8
0c50f5
+	aesmc		$dat2,$dat2
0c50f5
+	aese		$dat3,q8
0c50f5
+	aesmc		$dat3,$dat3
0c50f5
+	aese		$dat4,q8
0c50f5
+	aesmc		$dat4,$dat4
0c50f5
+	vld1.32	 	{q8},[$key_],#16	// re-pre-load rndkey[0]
0c50f5
+
0c50f5
+	aese		$dat0,q9
0c50f5
+	aesmc		$dat0,$dat0
0c50f5
+	aese		$dat1,q9
0c50f5
+	aesmc		$dat1,$dat1
0c50f5
+	aese		$dat2,q9
0c50f5
+	aesmc		$dat2,$dat2
0c50f5
+	aese		$dat3,q9
0c50f5
+	aesmc		$dat3,$dat3
0c50f5
+	aese		$dat4,q9
0c50f5
+	aesmc		$dat4,$dat4
0c50f5
+	vld1.32	 	{q9},[$key_],#16	// re-pre-load rndkey[1]
0c50f5
+
0c50f5
+	aese		$dat0,q12
0c50f5
+	aesmc		$dat0,$dat0
0c50f5
+	 add		$tctr0,$ctr,#1
0c50f5
+	 add		$tctr1,$ctr,#2
0c50f5
+	aese		$dat1,q12
0c50f5
+	aesmc		$dat1,$dat1
0c50f5
+	 add		$tctr2,$ctr,#3
0c50f5
+	 add		w13,$ctr,#4
0c50f5
+	aese		$dat2,q12
0c50f5
+	aesmc		$dat2,$dat2
0c50f5
+	 add		w14,$ctr,#5
0c50f5
+	 rev		$tctr0,$tctr0
0c50f5
+	aese		$dat3,q12
0c50f5
+	aesmc		$dat3,$dat3
0c50f5
+	 rev		$tctr1,$tctr1
0c50f5
+	 rev		$tctr2,$tctr2
0c50f5
+	aese		$dat4,q12
0c50f5
+	aesmc		$dat4,$dat4
0c50f5
+	 rev		w13,w13
0c50f5
+	 rev		w14,w14
0c50f5
+
0c50f5
+	aese		$dat0,q13
0c50f5
+	aesmc		$dat0,$dat0
0c50f5
+	aese		$dat1,q13
0c50f5
+	aesmc		$dat1,$dat1
0c50f5
+	aese		$dat2,q13
0c50f5
+	aesmc		$dat2,$dat2
0c50f5
+	aese		$dat3,q13
0c50f5
+	aesmc		$dat3,$dat3
0c50f5
+	aese		$dat4,q13
0c50f5
+	aesmc		$dat4,$dat4
0c50f5
+
0c50f5
+	aese		$dat0,q14
0c50f5
+	aesmc		$dat0,$dat0
0c50f5
+	 vld1.8		{$in0},[$inp],#16
0c50f5
+	aese		$dat1,q14
0c50f5
+	aesmc		$dat1,$dat1
0c50f5
+	 vld1.8		{$in1},[$inp],#16
0c50f5
+	aese		$dat2,q14
0c50f5
+	aesmc		$dat2,$dat2
0c50f5
+	 vld1.8		{$in2},[$inp],#16
0c50f5
+	aese		$dat3,q14
0c50f5
+	aesmc		$dat3,$dat3
0c50f5
+	 vld1.8		{$in3},[$inp],#16
0c50f5
+	aese		$dat4,q14
0c50f5
+	aesmc		$dat4,$dat4
0c50f5
+	 vld1.8		{$in4},[$inp],#16
0c50f5
+
0c50f5
+	aese		$dat0,q15
0c50f5
+	 veor		$in0,$in0,$rndlast
0c50f5
+	aese		$dat1,q15
0c50f5
+	 veor		$in1,$in1,$rndlast
0c50f5
+	aese		$dat2,q15
0c50f5
+	 veor		$in2,$in2,$rndlast
0c50f5
+	aese		$dat3,q15
0c50f5
+	 veor		$in3,$in3,$rndlast
0c50f5
+	aese		$dat4,q15
0c50f5
+	 veor		$in4,$in4,$rndlast
0c50f5
+
0c50f5
+	veor		$in0,$in0,$dat0
0c50f5
+	 vorr		$dat0,$ivec,$ivec
0c50f5
+	veor		$in1,$in1,$dat1
0c50f5
+	 vorr		$dat1,$ivec,$ivec
0c50f5
+	veor		$in2,$in2,$dat2
0c50f5
+	 vorr		$dat2,$ivec,$ivec
0c50f5
+	veor		$in3,$in3,$dat3
0c50f5
+	 vorr		$dat3,$ivec,$ivec
0c50f5
+	veor		$in4,$in4,$dat4
0c50f5
+	 vorr		$dat4,$ivec,$ivec
0c50f5
+
0c50f5
+	vst1.8		{$in0},[$out],#16
0c50f5
+	 vmov.32	${dat0}[3],$tctr0
0c50f5
+	vst1.8		{$in1},[$out],#16
0c50f5
+	 vmov.32	${dat1}[3],$tctr1
0c50f5
+	vst1.8		{$in2},[$out],#16
0c50f5
+	 vmov.32	${dat2}[3],$tctr2
0c50f5
+	vst1.8		{$in3},[$out],#16
0c50f5
+	 vmov.32	${dat3}[3],w13
0c50f5
+	vst1.8		{$in4},[$out],#16
0c50f5
+	 vmov.32	${dat4}[3],w14
0c50f5
+
0c50f5
+	mov		$cnt,$rounds
0c50f5
+	cbz		$len,.Lctr32_done
0c50f5
+
0c50f5
+	add		$ctr,$ctr,#5
0c50f5
+	subs		$len,$len,#5
0c50f5
+	b.hs		.Loop5x_ctr32
0c50f5
+
0c50f5
+	add		$len,$len,#5
0c50f5
+	sub		$ctr,$ctr,#5
0c50f5
+
0c50f5
+	cmp		$len,#2
0c50f5
+	mov		$step,#16
0c50f5
+	cclr		$step,lo
0c50f5
+	b.ls		.Lctr32_tail
0c50f5
+
0c50f5
+	sub		$len,$len,#3		// bias
0c50f5
+	add		$ctr,$ctr,#3
0c50f5
+___
0c50f5
+$code.=<<___;
0c50f5
 	b		.Loop3x_ctr32
0c50f5
 
0c50f5
 .align	4
0c50f5
@@ -905,6 +2131,1432 @@ $code.=<<___;
0c50f5
 .size	${prefix}_ctr32_encrypt_blocks,.-${prefix}_ctr32_encrypt_blocks
0c50f5
 ___
0c50f5
 }}}
0c50f5
+# Performance in cycles per byte.
0c50f5
+# Processed with AES-XTS different key size.
0c50f5
+# It shows the value before and after optimization as below:
0c50f5
+# (before/after):
0c50f5
+#
0c50f5
+#		AES-128-XTS		AES-256-XTS
0c50f5
+# Cortex-A57	3.36/1.09		4.02/1.37
0c50f5
+# Cortex-A72	3.03/1.02		3.28/1.33
0c50f5
+
0c50f5
+# Optimization is implemented by loop unrolling and interleaving.
0c50f5
+# Commonly, we choose the unrolling factor as 5, if the input
0c50f5
+# data size smaller than 5 blocks, but not smaller than 3 blocks,
0c50f5
+# choose 3 as the unrolling factor.
0c50f5
+# If the input data size dsize >= 5*16 bytes, then take 5 blocks
0c50f5
+# as one iteration, every loop the left size lsize -= 5*16.
0c50f5
+# If lsize < 5*16 bytes, treat them as the tail. Note: left 4*16 bytes
0c50f5
+# will be processed specially, which be integrated into the 5*16 bytes
0c50f5
+# loop to improve the efficiency.
0c50f5
+# There is one special case, if the original input data size dsize
0c50f5
+# = 16 bytes, we will treat it seperately to improve the
0c50f5
+# performance: one independent code block without LR, FP load and
0c50f5
+# store.
0c50f5
+# Encryption will process the (length -tailcnt) bytes as mentioned
0c50f5
+# previously, then encrypt the composite block as last second
0c50f5
+# cipher block.
0c50f5
+# Decryption will process the (length -tailcnt -1) bytes as mentioned
0c50f5
+# previously, then decrypt the last second cipher block to get the
0c50f5
+# last plain block(tail), decrypt the composite block as last second
0c50f5
+# plain text block.
0c50f5
+
0c50f5
+{{{
0c50f5
+my ($inp,$out,$len,$key1,$key2,$ivp)=map("x$_",(0..5));
0c50f5
+my ($rounds0,$rounds,$key_,$step,$ivl,$ivh)=("w5","w6","x7","x8","x9","x10");
0c50f5
+my ($tmpoutp,$loutp,$l2outp,$tmpinp)=("x13","w14","w15","x20");
0c50f5
+my ($tailcnt,$midnum,$midnumx,$constnum,$constnumx)=("x21","w22","x22","w19","x19");
0c50f5
+my ($xoffset,$tmpmx,$tmpmw)=("x6","x11","w11");
0c50f5
+my ($dat0,$dat1,$in0,$in1,$tmp0,$tmp1,$tmp2,$rndlast)=map("q$_",(0..7));
0c50f5
+my ($iv0,$iv1,$iv2,$iv3,$iv4)=("v6.16b","v8.16b","v9.16b","v10.16b","v11.16b");
0c50f5
+my ($ivd00,$ivd01,$ivd20,$ivd21)=("d6","v6.d[1]","d9","v9.d[1]");
0c50f5
+my ($ivd10,$ivd11,$ivd30,$ivd31,$ivd40,$ivd41)=("d8","v8.d[1]","d10","v10.d[1]","d11","v11.d[1]");
0c50f5
+
0c50f5
+my ($tmpin)=("v26.16b");
0c50f5
+my ($dat,$tmp,$rndzero_n_last)=($dat0,$tmp0,$tmp1);
0c50f5
+
0c50f5
+# q7	last round key
0c50f5
+# q10-q15, q7	Last 7 round keys
0c50f5
+# q8-q9	preloaded round keys except last 7 keys for big size
0c50f5
+# q20, q21, q8-q9	preloaded round keys except last 7 keys for only 16 byte
0c50f5
+
0c50f5
+
0c50f5
+my ($dat2,$in2,$tmp2)=map("q$_",(10,11,9));
0c50f5
+
0c50f5
+my ($dat3,$in3,$tmp3);	# used only in 64-bit mode
0c50f5
+my ($dat4,$in4,$tmp4);
0c50f5
+if ($flavour =~ /64/) {
0c50f5
+    ($dat2,$dat3,$dat4,$in2,$in3,$in4,$tmp3,$tmp4)=map("q$_",(16..23));
0c50f5
+}
0c50f5
+
0c50f5
+$code.=<<___	if ($flavour =~ /64/);
0c50f5
+.globl	${prefix}_xts_encrypt
0c50f5
+.type	${prefix}_xts_encrypt,%function
0c50f5
+.align	5
0c50f5
+${prefix}_xts_encrypt:
0c50f5
+___
0c50f5
+$code.=<<___	if ($flavour =~ /64/);
0c50f5
+	cmp	$len,#16
0c50f5
+	// Original input data size bigger than 16, jump to big size processing.
0c50f5
+	b.ne	.Lxts_enc_big_size
0c50f5
+	// Encrypt the iv with key2, as the first XEX iv.
0c50f5
+	ldr	$rounds,[$key2,#240]
0c50f5
+	vld1.8	{$dat},[$key2],#16
0c50f5
+	vld1.8	{$iv0},[$ivp]
0c50f5
+	sub	$rounds,$rounds,#2
0c50f5
+	vld1.8	{$dat1},[$key2],#16
0c50f5
+
0c50f5
+.Loop_enc_iv_enc:
0c50f5
+	aese	$iv0,$dat
0c50f5
+	aesmc	$iv0,$iv0
0c50f5
+	vld1.32	{$dat},[$key2],#16
0c50f5
+	subs	$rounds,$rounds,#2
0c50f5
+	aese	$iv0,$dat1
0c50f5
+	aesmc	$iv0,$iv0
0c50f5
+	vld1.32	{$dat1},[$key2],#16
0c50f5
+	b.gt	.Loop_enc_iv_enc
0c50f5
+
0c50f5
+	aese	$iv0,$dat
0c50f5
+	aesmc	$iv0,$iv0
0c50f5
+	vld1.32	{$dat},[$key2]
0c50f5
+	aese	$iv0,$dat1
0c50f5
+	veor	$iv0,$iv0,$dat
0c50f5
+
0c50f5
+	vld1.8	{$dat0},[$inp]
0c50f5
+	veor	$dat0,$iv0,$dat0
0c50f5
+
0c50f5
+	ldr	$rounds,[$key1,#240]
0c50f5
+	vld1.32	{q20-q21},[$key1],#32		// load key schedule...
0c50f5
+
0c50f5
+	aese	$dat0,q20
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	vld1.32	{q8-q9},[$key1],#32		// load key schedule...
0c50f5
+	aese	$dat0,q21
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	subs	$rounds,$rounds,#10		// if rounds==10, jump to aes-128-xts processing
0c50f5
+	b.eq	.Lxts_128_enc
0c50f5
+.Lxts_enc_round_loop:
0c50f5
+	aese	$dat0,q8
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	vld1.32	{q8},[$key1],#16		// load key schedule...
0c50f5
+	aese	$dat0,q9
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	vld1.32	{q9},[$key1],#16		// load key schedule...
0c50f5
+	subs	$rounds,$rounds,#2		// bias
0c50f5
+	b.gt	.Lxts_enc_round_loop
0c50f5
+.Lxts_128_enc:
0c50f5
+	vld1.32	{q10-q11},[$key1],#32		// load key schedule...
0c50f5
+	aese	$dat0,q8
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat0,q9
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	vld1.32	{q12-q13},[$key1],#32		// load key schedule...
0c50f5
+	aese	$dat0,q10
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat0,q11
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	vld1.32	{q14-q15},[$key1],#32		// load key schedule...
0c50f5
+	aese	$dat0,q12
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat0,q13
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	vld1.32	{$rndlast},[$key1]
0c50f5
+	aese	$dat0,q14
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat0,q15
0c50f5
+	veor	$dat0,$dat0,$rndlast
0c50f5
+	veor	$dat0,$dat0,$iv0
0c50f5
+	vst1.8	{$dat0},[$out]
0c50f5
+	b	.Lxts_enc_final_abort
0c50f5
+
0c50f5
+.align	4
0c50f5
+.Lxts_enc_big_size:
0c50f5
+___
0c50f5
+$code.=<<___	if ($flavour =~ /64/);
0c50f5
+	stp	$constnumx,$tmpinp,[sp,#-64]!
0c50f5
+	stp	$tailcnt,$midnumx,[sp,#48]
0c50f5
+	stp	$ivd10,$ivd20,[sp,#32]
0c50f5
+	stp	$ivd30,$ivd40,[sp,#16]
0c50f5
+
0c50f5
+	// tailcnt store the tail value of length%16.
0c50f5
+	and	$tailcnt,$len,#0xf
0c50f5
+	and	$len,$len,#-16
0c50f5
+	subs	$len,$len,#16
0c50f5
+	mov	$step,#16
0c50f5
+	b.lo	.Lxts_abort
0c50f5
+	csel	$step,xzr,$step,eq
0c50f5
+
0c50f5
+	// Firstly, encrypt the iv with key2, as the first iv of XEX.
0c50f5
+	ldr	$rounds,[$key2,#240]
0c50f5
+	vld1.32	{$dat},[$key2],#16
0c50f5
+	vld1.8	{$iv0},[$ivp]
0c50f5
+	sub	$rounds,$rounds,#2
0c50f5
+	vld1.32	{$dat1},[$key2],#16
0c50f5
+
0c50f5
+.Loop_iv_enc:
0c50f5
+	aese	$iv0,$dat
0c50f5
+	aesmc	$iv0,$iv0
0c50f5
+	vld1.32	{$dat},[$key2],#16
0c50f5
+	subs	$rounds,$rounds,#2
0c50f5
+	aese	$iv0,$dat1
0c50f5
+	aesmc	$iv0,$iv0
0c50f5
+	vld1.32	{$dat1},[$key2],#16
0c50f5
+	b.gt	.Loop_iv_enc
0c50f5
+
0c50f5
+	aese	$iv0,$dat
0c50f5
+	aesmc	$iv0,$iv0
0c50f5
+	vld1.32	{$dat},[$key2]
0c50f5
+	aese	$iv0,$dat1
0c50f5
+	veor	$iv0,$iv0,$dat
0c50f5
+
0c50f5
+	// The iv for second block
0c50f5
+	// $ivl- iv(low), $ivh - iv(high)
0c50f5
+	// the five ivs stored into, $iv0,$iv1,$iv2,$iv3,$iv4
0c50f5
+	fmov	$ivl,$ivd00
0c50f5
+	fmov	$ivh,$ivd01
0c50f5
+	mov	$constnum,#0x87
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr#31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl#1
0c50f5
+	fmov	$ivd10,$ivl
0c50f5
+	fmov	$ivd11,$ivh
0c50f5
+
0c50f5
+	ldr	$rounds0,[$key1,#240]		// next starting point
0c50f5
+	vld1.8	{$dat},[$inp],$step
0c50f5
+
0c50f5
+	vld1.32	{q8-q9},[$key1]			// load key schedule...
0c50f5
+	sub	$rounds0,$rounds0,#6
0c50f5
+	add	$key_,$key1,$ivp,lsl#4		// pointer to last 7 round keys
0c50f5
+	sub	$rounds0,$rounds0,#2
0c50f5
+	vld1.32	{q10-q11},[$key_],#32
0c50f5
+	vld1.32	{q12-q13},[$key_],#32
0c50f5
+	vld1.32	{q14-q15},[$key_],#32
0c50f5
+	vld1.32	{$rndlast},[$key_]
0c50f5
+
0c50f5
+	add	$key_,$key1,#32
0c50f5
+	mov	$rounds,$rounds0
0c50f5
+
0c50f5
+	// Encryption
0c50f5
+.Lxts_enc:
0c50f5
+	vld1.8	{$dat2},[$inp],#16
0c50f5
+	subs	$len,$len,#32			// bias
0c50f5
+	add	$rounds,$rounds0,#2
0c50f5
+	vorr	$in1,$dat,$dat
0c50f5
+	vorr	$dat1,$dat,$dat
0c50f5
+	vorr	$in3,$dat,$dat
0c50f5
+	vorr	$in2,$dat2,$dat2
0c50f5
+	vorr	$in4,$dat2,$dat2
0c50f5
+	b.lo	.Lxts_inner_enc_tail
0c50f5
+	veor	$dat,$dat,$iv0			// before encryption, xor with iv
0c50f5
+	veor	$dat2,$dat2,$iv1
0c50f5
+
0c50f5
+	// The iv for third block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr#31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl#1
0c50f5
+	fmov	$ivd20,$ivl
0c50f5
+	fmov	$ivd21,$ivh
0c50f5
+
0c50f5
+
0c50f5
+	vorr	$dat1,$dat2,$dat2
0c50f5
+	vld1.8	{$dat2},[$inp],#16
0c50f5
+	vorr	$in0,$dat,$dat
0c50f5
+	vorr	$in1,$dat1,$dat1
0c50f5
+	veor	$in2,$dat2,$iv2 		// the third block
0c50f5
+	veor	$dat2,$dat2,$iv2
0c50f5
+	cmp	$len,#32
0c50f5
+	b.lo	.Lxts_outer_enc_tail
0c50f5
+
0c50f5
+	// The iv for fourth block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr#31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl#1
0c50f5
+	fmov	$ivd30,$ivl
0c50f5
+	fmov	$ivd31,$ivh
0c50f5
+
0c50f5
+	vld1.8	{$dat3},[$inp],#16
0c50f5
+	// The iv for fifth block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr#31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl#1
0c50f5
+	fmov	$ivd40,$ivl
0c50f5
+	fmov	$ivd41,$ivh
0c50f5
+
0c50f5
+	vld1.8	{$dat4},[$inp],#16
0c50f5
+	veor	$dat3,$dat3,$iv3		// the fourth block
0c50f5
+	veor	$dat4,$dat4,$iv4
0c50f5
+	sub	$len,$len,#32			// bias
0c50f5
+	mov	$rounds,$rounds0
0c50f5
+	b	.Loop5x_xts_enc
0c50f5
+
0c50f5
+.align	4
0c50f5
+.Loop5x_xts_enc:
0c50f5
+	aese	$dat0,q8
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q8
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q8
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q8
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q8
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+	vld1.32	{q8},[$key_],#16
0c50f5
+	subs	$rounds,$rounds,#2
0c50f5
+	aese	$dat0,q9
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q9
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q9
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q9
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q9
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+	vld1.32	{q9},[$key_],#16
0c50f5
+	b.gt	.Loop5x_xts_enc
0c50f5
+
0c50f5
+	aese	$dat0,q8
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q8
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q8
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q8
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q8
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+	subs	$len,$len,#0x50			// because .Lxts_enc_tail4x
0c50f5
+
0c50f5
+	aese	$dat0,q9
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q9
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q9
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q9
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q9
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+	csel	$xoffset,xzr,$len,gt		// borrow x6, w6, "gt" is not typo
0c50f5
+	mov	$key_,$key1
0c50f5
+
0c50f5
+	aese	$dat0,q10
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q10
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q10
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q10
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q10
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+	add	$inp,$inp,$xoffset		// x0 is adjusted in such way that
0c50f5
+						// at exit from the loop v1.16b-v26.16b
0c50f5
+						// are loaded with last "words"
0c50f5
+	add	$xoffset,$len,#0x60		// because .Lxts_enc_tail4x
0c50f5
+
0c50f5
+	aese	$dat0,q11
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q11
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q11
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q11
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q11
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+
0c50f5
+	aese	$dat0,q12
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q12
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q12
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q12
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q12
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+
0c50f5
+	aese	$dat0,q13
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q13
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q13
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q13
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q13
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+
0c50f5
+	aese	$dat0,q14
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q14
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q14
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat3,q14
0c50f5
+	aesmc	$dat3,$dat3
0c50f5
+	aese	$dat4,q14
0c50f5
+	aesmc	$dat4,$dat4
0c50f5
+
0c50f5
+	veor	$tmp0,$rndlast,$iv0
0c50f5
+	aese	$dat0,q15
0c50f5
+	// The iv for first block of one iteration
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr#31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl#1
0c50f5
+	fmov	$ivd00,$ivl
0c50f5
+	fmov	$ivd01,$ivh
0c50f5
+	veor	$tmp1,$rndlast,$iv1
0c50f5
+	vld1.8	{$in0},[$inp],#16
0c50f5
+	aese	$dat1,q15
0c50f5
+	// The iv for second block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr#31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl#1
0c50f5
+	fmov	$ivd10,$ivl
0c50f5
+	fmov	$ivd11,$ivh
0c50f5
+	veor	$tmp2,$rndlast,$iv2
0c50f5
+	vld1.8	{$in1},[$inp],#16
0c50f5
+	aese	$dat2,q15
0c50f5
+	// The iv for third block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr#31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl#1
0c50f5
+	fmov	$ivd20,$ivl
0c50f5
+	fmov	$ivd21,$ivh
0c50f5
+	veor	$tmp3,$rndlast,$iv3
0c50f5
+	vld1.8	{$in2},[$inp],#16
0c50f5
+	aese	$dat3,q15
0c50f5
+	// The iv for fourth block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr#31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl#1
0c50f5
+	fmov	$ivd30,$ivl
0c50f5
+	fmov	$ivd31,$ivh
0c50f5
+	veor	$tmp4,$rndlast,$iv4
0c50f5
+	vld1.8	{$in3},[$inp],#16
0c50f5
+	aese	$dat4,q15
0c50f5
+
0c50f5
+	// The iv for fifth block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd40,$ivl
0c50f5
+	fmov	$ivd41,$ivh
0c50f5
+
0c50f5
+	vld1.8	{$in4},[$inp],#16
0c50f5
+	cbz	$xoffset,.Lxts_enc_tail4x
0c50f5
+	vld1.32 {q8},[$key_],#16		// re-pre-load rndkey[0]
0c50f5
+	veor	$tmp0,$tmp0,$dat0
0c50f5
+	veor	$dat0,$in0,$iv0
0c50f5
+	veor	$tmp1,$tmp1,$dat1
0c50f5
+	veor	$dat1,$in1,$iv1
0c50f5
+	veor	$tmp2,$tmp2,$dat2
0c50f5
+	veor	$dat2,$in2,$iv2
0c50f5
+	veor	$tmp3,$tmp3,$dat3
0c50f5
+	veor	$dat3,$in3,$iv3
0c50f5
+	veor	$tmp4,$tmp4,$dat4
0c50f5
+	vst1.8	{$tmp0},[$out],#16
0c50f5
+	veor	$dat4,$in4,$iv4
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	mov	$rounds,$rounds0
0c50f5
+	vst1.8	{$tmp2},[$out],#16
0c50f5
+	vld1.32	{q9},[$key_],#16		// re-pre-load rndkey[1]
0c50f5
+	vst1.8	{$tmp3},[$out],#16
0c50f5
+	vst1.8	{$tmp4},[$out],#16
0c50f5
+	b.hs	.Loop5x_xts_enc
0c50f5
+
0c50f5
+
0c50f5
+	// If left 4 blocks, borrow the five block's processing.
0c50f5
+	cmn	$len,#0x10
0c50f5
+	b.ne	.Loop5x_enc_after
0c50f5
+	vorr	$iv4,$iv3,$iv3
0c50f5
+	vorr	$iv3,$iv2,$iv2
0c50f5
+	vorr	$iv2,$iv1,$iv1
0c50f5
+	vorr	$iv1,$iv0,$iv0
0c50f5
+	fmov	$ivl,$ivd40
0c50f5
+	fmov	$ivh,$ivd41
0c50f5
+	veor	$dat0,$iv0,$in0
0c50f5
+	veor	$dat1,$iv1,$in1
0c50f5
+	veor	$dat2,$in2,$iv2
0c50f5
+	veor	$dat3,$in3,$iv3
0c50f5
+	veor	$dat4,$in4,$iv4
0c50f5
+	b.eq	.Loop5x_xts_enc
0c50f5
+
0c50f5
+.Loop5x_enc_after:
0c50f5
+	add	$len,$len,#0x50
0c50f5
+	cbz	$len,.Lxts_enc_done
0c50f5
+
0c50f5
+	add	$rounds,$rounds0,#2
0c50f5
+	subs	$len,$len,#0x30
0c50f5
+	b.lo	.Lxts_inner_enc_tail
0c50f5
+
0c50f5
+	veor	$dat0,$iv0,$in2
0c50f5
+	veor	$dat1,$iv1,$in3
0c50f5
+	veor	$dat2,$in4,$iv2
0c50f5
+	b	.Lxts_outer_enc_tail
0c50f5
+
0c50f5
+.align	4
0c50f5
+.Lxts_enc_tail4x:
0c50f5
+	add	$inp,$inp,#16
0c50f5
+	veor	$tmp1,$dat1,$tmp1
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	veor	$tmp2,$dat2,$tmp2
0c50f5
+	vst1.8	{$tmp2},[$out],#16
0c50f5
+	veor	$tmp3,$dat3,$tmp3
0c50f5
+	veor	$tmp4,$dat4,$tmp4
0c50f5
+	vst1.8	{$tmp3-$tmp4},[$out],#32
0c50f5
+
0c50f5
+	b	.Lxts_enc_done
0c50f5
+.align	4
0c50f5
+.Lxts_outer_enc_tail:
0c50f5
+	aese	$dat0,q8
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q8
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q8
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	vld1.32	{q8},[$key_],#16
0c50f5
+	subs	$rounds,$rounds,#2
0c50f5
+	aese	$dat0,q9
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q9
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q9
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	vld1.32	{q9},[$key_],#16
0c50f5
+	b.gt	.Lxts_outer_enc_tail
0c50f5
+
0c50f5
+	aese	$dat0,q8
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q8
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q8
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	veor	$tmp0,$iv0,$rndlast
0c50f5
+	subs	$len,$len,#0x30
0c50f5
+	// The iv for first block
0c50f5
+	fmov	$ivl,$ivd20
0c50f5
+	fmov	$ivh,$ivd21
0c50f5
+	//mov	$constnum,#0x87
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr#31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl#1
0c50f5
+	fmov	$ivd00,$ivl
0c50f5
+	fmov	$ivd01,$ivh
0c50f5
+	veor	$tmp1,$iv1,$rndlast
0c50f5
+	csel	$xoffset,$len,$xoffset,lo       // x6, w6, is zero at this point
0c50f5
+	aese	$dat0,q9
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q9
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q9
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	veor	$tmp2,$iv2,$rndlast
0c50f5
+
0c50f5
+	add	$xoffset,$xoffset,#0x20
0c50f5
+	add	$inp,$inp,$xoffset
0c50f5
+	mov	$key_,$key1
0c50f5
+
0c50f5
+	aese	$dat0,q12
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q12
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q12
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat0,q13
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q13
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q13
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat0,q14
0c50f5
+	aesmc	$dat0,$dat0
0c50f5
+	aese	$dat1,q14
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q14
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat0,q15
0c50f5
+	aese	$dat1,q15
0c50f5
+	aese	$dat2,q15
0c50f5
+	vld1.8	{$in2},[$inp],#16
0c50f5
+	add	$rounds,$rounds0,#2
0c50f5
+	vld1.32	{q8},[$key_],#16                // re-pre-load rndkey[0]
0c50f5
+	veor	$tmp0,$tmp0,$dat0
0c50f5
+	veor	$tmp1,$tmp1,$dat1
0c50f5
+	veor	$dat2,$dat2,$tmp2
0c50f5
+	vld1.32	{q9},[$key_],#16                // re-pre-load rndkey[1]
0c50f5
+	vst1.8	{$tmp0},[$out],#16
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	vst1.8	{$dat2},[$out],#16
0c50f5
+	cmn	$len,#0x30
0c50f5
+	b.eq	.Lxts_enc_done
0c50f5
+.Lxts_encxor_one:
0c50f5
+	vorr	$in3,$in1,$in1
0c50f5
+	vorr	$in4,$in2,$in2
0c50f5
+	nop
0c50f5
+
0c50f5
+.Lxts_inner_enc_tail:
0c50f5
+	cmn	$len,#0x10
0c50f5
+	veor	$dat1,$in3,$iv0
0c50f5
+	veor	$dat2,$in4,$iv1
0c50f5
+	b.eq	.Lxts_enc_tail_loop
0c50f5
+	veor	$dat2,$in4,$iv0
0c50f5
+.Lxts_enc_tail_loop:
0c50f5
+	aese	$dat1,q8
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q8
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	vld1.32	{q8},[$key_],#16
0c50f5
+	subs	$rounds,$rounds,#2
0c50f5
+	aese	$dat1,q9
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q9
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	vld1.32	{q9},[$key_],#16
0c50f5
+	b.gt	.Lxts_enc_tail_loop
0c50f5
+
0c50f5
+	aese	$dat1,q8
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q8
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat1,q9
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q9
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	aese	$dat1,q12
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q12
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	cmn	$len,#0x20
0c50f5
+	aese	$dat1,q13
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q13
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	veor	$tmp1,$iv0,$rndlast
0c50f5
+	aese	$dat1,q14
0c50f5
+	aesmc	$dat1,$dat1
0c50f5
+	aese	$dat2,q14
0c50f5
+	aesmc	$dat2,$dat2
0c50f5
+	veor	$tmp2,$iv1,$rndlast
0c50f5
+	aese	$dat1,q15
0c50f5
+	aese	$dat2,q15
0c50f5
+	b.eq	.Lxts_enc_one
0c50f5
+	veor	$tmp1,$tmp1,$dat1
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	veor	$tmp2,$tmp2,$dat2
0c50f5
+	vorr	$iv0,$iv1,$iv1
0c50f5
+	vst1.8	{$tmp2},[$out],#16
0c50f5
+	fmov	$ivl,$ivd10
0c50f5
+	fmov	$ivh,$ivd11
0c50f5
+	mov	$constnum,#0x87
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd00,$ivl
0c50f5
+	fmov	$ivd01,$ivh
0c50f5
+	b	.Lxts_enc_done
0c50f5
+
0c50f5
+.Lxts_enc_one:
0c50f5
+	veor	$tmp1,$tmp1,$dat2
0c50f5
+	vorr	$iv0,$iv0,$iv0
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	fmov	$ivl,$ivd00
0c50f5
+	fmov	$ivh,$ivd01
0c50f5
+	mov	$constnum,#0x87
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd00,$ivl
0c50f5
+	fmov	$ivd01,$ivh
0c50f5
+	b	.Lxts_enc_done
0c50f5
+.align	5
0c50f5
+.Lxts_enc_done:
0c50f5
+	// Process the tail block with cipher stealing.
0c50f5
+	tst	$tailcnt,#0xf
0c50f5
+	b.eq	.Lxts_abort
0c50f5
+
0c50f5
+	mov	$tmpinp,$inp
0c50f5
+	mov	$tmpoutp,$out
0c50f5
+	sub	$out,$out,#16
0c50f5
+.composite_enc_loop:
0c50f5
+	subs	$tailcnt,$tailcnt,#1
0c50f5
+	ldrb	$l2outp,[$out,$tailcnt]
0c50f5
+	ldrb	$loutp,[$tmpinp,$tailcnt]
0c50f5
+	strb	$l2outp,[$tmpoutp,$tailcnt]
0c50f5
+	strb	$loutp,[$out,$tailcnt]
0c50f5
+	b.gt	.composite_enc_loop
0c50f5
+.Lxts_enc_load_done:
0c50f5
+	vld1.8	{$tmpin},[$out]
0c50f5
+	veor	$tmpin,$tmpin,$iv0
0c50f5
+
0c50f5
+	// Encrypt the composite block to get the last second encrypted text block
0c50f5
+	ldr	$rounds,[$key1,#240]		// load key schedule...
0c50f5
+	vld1.8	{$dat},[$key1],#16
0c50f5
+	sub	$rounds,$rounds,#2
0c50f5
+	vld1.8	{$dat1},[$key1],#16		// load key schedule...
0c50f5
+.Loop_final_enc:
0c50f5
+	aese	$tmpin,$dat0
0c50f5
+	aesmc	$tmpin,$tmpin
0c50f5
+	vld1.32	{$dat0},[$key1],#16
0c50f5
+	subs	$rounds,$rounds,#2
0c50f5
+	aese	$tmpin,$dat1
0c50f5
+	aesmc	$tmpin,$tmpin
0c50f5
+	vld1.32	{$dat1},[$key1],#16
0c50f5
+	b.gt	.Loop_final_enc
0c50f5
+
0c50f5
+	aese	$tmpin,$dat0
0c50f5
+	aesmc	$tmpin,$tmpin
0c50f5
+	vld1.32	{$dat0},[$key1]
0c50f5
+	aese	$tmpin,$dat1
0c50f5
+	veor	$tmpin,$tmpin,$dat0
0c50f5
+	veor	$tmpin,$tmpin,$iv0
0c50f5
+	vst1.8	{$tmpin},[$out]
0c50f5
+
0c50f5
+.Lxts_abort:
0c50f5
+	ldp	$tailcnt,$midnumx,[sp,#48]
0c50f5
+	ldp	$ivd10,$ivd20,[sp,#32]
0c50f5
+	ldp	$ivd30,$ivd40,[sp,#16]
0c50f5
+	ldp	$constnumx,$tmpinp,[sp],#64
0c50f5
+.Lxts_enc_final_abort:
0c50f5
+	ret
0c50f5
+.size	${prefix}_xts_encrypt,.-${prefix}_xts_encrypt
0c50f5
+___
0c50f5
+
0c50f5
+}}}
0c50f5
+{{{
0c50f5
+my ($inp,$out,$len,$key1,$key2,$ivp)=map("x$_",(0..5));
0c50f5
+my ($rounds0,$rounds,$key_,$step,$ivl,$ivh)=("w5","w6","x7","x8","x9","x10");
0c50f5
+my ($tmpoutp,$loutp,$l2outp,$tmpinp)=("x13","w14","w15","x20");
0c50f5
+my ($tailcnt,$midnum,$midnumx,$constnum,$constnumx)=("x21","w22","x22","w19","x19");
0c50f5
+my ($xoffset,$tmpmx,$tmpmw)=("x6","x11","w11");
0c50f5
+my ($dat0,$dat1,$in0,$in1,$tmp0,$tmp1,$tmp2,$rndlast)=map("q$_",(0..7));
0c50f5
+my ($iv0,$iv1,$iv2,$iv3,$iv4,$tmpin)=("v6.16b","v8.16b","v9.16b","v10.16b","v11.16b","v26.16b");
0c50f5
+my ($ivd00,$ivd01,$ivd20,$ivd21)=("d6","v6.d[1]","d9","v9.d[1]");
0c50f5
+my ($ivd10,$ivd11,$ivd30,$ivd31,$ivd40,$ivd41)=("d8","v8.d[1]","d10","v10.d[1]","d11","v11.d[1]");
0c50f5
+
0c50f5
+my ($dat,$tmp,$rndzero_n_last)=($dat0,$tmp0,$tmp1);
0c50f5
+
0c50f5
+# q7	last round key
0c50f5
+# q10-q15, q7	Last 7 round keys
0c50f5
+# q8-q9	preloaded round keys except last 7 keys for big size
0c50f5
+# q20, q21, q8-q9	preloaded round keys except last 7 keys for only 16 byte
0c50f5
+
0c50f5
+{
0c50f5
+my ($dat2,$in2,$tmp2)=map("q$_",(10,11,9));
0c50f5
+
0c50f5
+my ($dat3,$in3,$tmp3);	# used only in 64-bit mode
0c50f5
+my ($dat4,$in4,$tmp4);
0c50f5
+if ($flavour =~ /64/) {
0c50f5
+    ($dat2,$dat3,$dat4,$in2,$in3,$in4,$tmp3,$tmp4)=map("q$_",(16..23));
0c50f5
+}
0c50f5
+
0c50f5
+$code.=<<___	if ($flavour =~ /64/);
0c50f5
+.globl	${prefix}_xts_decrypt
0c50f5
+.type	${prefix}_xts_decrypt,%function
0c50f5
+.align	5
0c50f5
+${prefix}_xts_decrypt:
0c50f5
+___
0c50f5
+$code.=<<___	if ($flavour =~ /64/);
0c50f5
+	cmp	$len,#16
0c50f5
+	// Original input data size bigger than 16, jump to big size processing.
0c50f5
+	b.ne	.Lxts_dec_big_size
0c50f5
+	// Encrypt the iv with key2, as the first XEX iv.
0c50f5
+	ldr	$rounds,[$key2,#240]
0c50f5
+	vld1.8	{$dat},[$key2],#16
0c50f5
+	vld1.8	{$iv0},[$ivp]
0c50f5
+	sub	$rounds,$rounds,#2
0c50f5
+	vld1.8	{$dat1},[$key2],#16
0c50f5
+
0c50f5
+.Loop_dec_small_iv_enc:
0c50f5
+	aese	$iv0,$dat
0c50f5
+	aesmc	$iv0,$iv0
0c50f5
+	vld1.32	{$dat},[$key2],#16
0c50f5
+	subs	$rounds,$rounds,#2
0c50f5
+	aese	$iv0,$dat1
0c50f5
+	aesmc	$iv0,$iv0
0c50f5
+	vld1.32	{$dat1},[$key2],#16
0c50f5
+	b.gt	.Loop_dec_small_iv_enc
0c50f5
+
0c50f5
+	aese	$iv0,$dat
0c50f5
+	aesmc	$iv0,$iv0
0c50f5
+	vld1.32	{$dat},[$key2]
0c50f5
+	aese	$iv0,$dat1
0c50f5
+	veor	$iv0,$iv0,$dat
0c50f5
+
0c50f5
+	vld1.8	{$dat0},[$inp]
0c50f5
+	veor	$dat0,$iv0,$dat0
0c50f5
+
0c50f5
+	ldr	$rounds,[$key1,#240]
0c50f5
+	vld1.32	{q20-q21},[$key1],#32			// load key schedule...
0c50f5
+
0c50f5
+	aesd	$dat0,q20
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	vld1.32	{q8-q9},[$key1],#32			// load key schedule...
0c50f5
+	aesd	$dat0,q21
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	subs	$rounds,$rounds,#10			// bias
0c50f5
+	b.eq	.Lxts_128_dec
0c50f5
+.Lxts_dec_round_loop:
0c50f5
+	aesd	$dat0,q8
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	vld1.32	{q8},[$key1],#16			// load key schedule...
0c50f5
+	aesd	$dat0,q9
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	vld1.32	{q9},[$key1],#16			// load key schedule...
0c50f5
+	subs	$rounds,$rounds,#2			// bias
0c50f5
+	b.gt	.Lxts_dec_round_loop
0c50f5
+.Lxts_128_dec:
0c50f5
+	vld1.32	{q10-q11},[$key1],#32			// load key schedule...
0c50f5
+	aesd	$dat0,q8
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat0,q9
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	vld1.32	{q12-q13},[$key1],#32			// load key schedule...
0c50f5
+	aesd	$dat0,q10
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat0,q11
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	vld1.32	{q14-q15},[$key1],#32			// load key schedule...
0c50f5
+	aesd	$dat0,q12
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat0,q13
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	vld1.32	{$rndlast},[$key1]
0c50f5
+	aesd	$dat0,q14
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat0,q15
0c50f5
+	veor	$dat0,$dat0,$rndlast
0c50f5
+	veor	$dat0,$iv0,$dat0
0c50f5
+	vst1.8	{$dat0},[$out]
0c50f5
+	b	.Lxts_dec_final_abort
0c50f5
+.Lxts_dec_big_size:
0c50f5
+___
0c50f5
+$code.=<<___	if ($flavour =~ /64/);
0c50f5
+	stp	$constnumx,$tmpinp,[sp,#-64]!
0c50f5
+	stp	$tailcnt,$midnumx,[sp,#48]
0c50f5
+	stp	$ivd10,$ivd20,[sp,#32]
0c50f5
+	stp	$ivd30,$ivd40,[sp,#16]
0c50f5
+
0c50f5
+	and	$tailcnt,$len,#0xf
0c50f5
+	and	$len,$len,#-16
0c50f5
+	subs	$len,$len,#16
0c50f5
+	mov	$step,#16
0c50f5
+	b.lo	.Lxts_dec_abort
0c50f5
+
0c50f5
+	// Encrypt the iv with key2, as the first XEX iv
0c50f5
+	ldr	$rounds,[$key2,#240]
0c50f5
+	vld1.8	{$dat},[$key2],#16
0c50f5
+	vld1.8	{$iv0},[$ivp]
0c50f5
+	sub	$rounds,$rounds,#2
0c50f5
+	vld1.8	{$dat1},[$key2],#16
0c50f5
+
0c50f5
+.Loop_dec_iv_enc:
0c50f5
+	aese	$iv0,$dat
0c50f5
+	aesmc	$iv0,$iv0
0c50f5
+	vld1.32	{$dat},[$key2],#16
0c50f5
+	subs	$rounds,$rounds,#2
0c50f5
+	aese	$iv0,$dat1
0c50f5
+	aesmc	$iv0,$iv0
0c50f5
+	vld1.32	{$dat1},[$key2],#16
0c50f5
+	b.gt	.Loop_dec_iv_enc
0c50f5
+
0c50f5
+	aese	$iv0,$dat
0c50f5
+	aesmc	$iv0,$iv0
0c50f5
+	vld1.32	{$dat},[$key2]
0c50f5
+	aese	$iv0,$dat1
0c50f5
+	veor	$iv0,$iv0,$dat
0c50f5
+
0c50f5
+	// The iv for second block
0c50f5
+	// $ivl- iv(low), $ivh - iv(high)
0c50f5
+	// the five ivs stored into, $iv0,$iv1,$iv2,$iv3,$iv4
0c50f5
+	fmov	$ivl,$ivd00
0c50f5
+	fmov	$ivh,$ivd01
0c50f5
+	mov	$constnum,#0x87
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd10,$ivl
0c50f5
+	fmov	$ivd11,$ivh
0c50f5
+
0c50f5
+	ldr	$rounds0,[$key1,#240]		// load rounds number
0c50f5
+
0c50f5
+	// The iv for third block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd20,$ivl
0c50f5
+	fmov	$ivd21,$ivh
0c50f5
+
0c50f5
+	vld1.32	{q8-q9},[$key1]			// load key schedule...
0c50f5
+	sub	$rounds0,$rounds0,#6
0c50f5
+	add	$key_,$key1,$ivp,lsl#4		// pointer to last 7 round keys
0c50f5
+	sub	$rounds0,$rounds0,#2
0c50f5
+	vld1.32	{q10-q11},[$key_],#32		// load key schedule...
0c50f5
+	vld1.32	{q12-q13},[$key_],#32
0c50f5
+	vld1.32	{q14-q15},[$key_],#32
0c50f5
+	vld1.32	{$rndlast},[$key_]
0c50f5
+
0c50f5
+	// The iv for fourth block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd30,$ivl
0c50f5
+	fmov	$ivd31,$ivh
0c50f5
+
0c50f5
+	add	$key_,$key1,#32
0c50f5
+	mov	$rounds,$rounds0
0c50f5
+	b	.Lxts_dec
0c50f5
+
0c50f5
+	// Decryption
0c50f5
+.align	5
0c50f5
+.Lxts_dec:
0c50f5
+	tst	$tailcnt,#0xf
0c50f5
+	b.eq	.Lxts_dec_begin
0c50f5
+	subs	$len,$len,#16
0c50f5
+	csel	$step,xzr,$step,eq
0c50f5
+	vld1.8	{$dat},[$inp],#16
0c50f5
+	b.lo	.Lxts_done
0c50f5
+	sub	$inp,$inp,#16
0c50f5
+.Lxts_dec_begin:
0c50f5
+	vld1.8	{$dat},[$inp],$step
0c50f5
+	subs	$len,$len,#32			// bias
0c50f5
+	add	$rounds,$rounds0,#2
0c50f5
+	vorr	$in1,$dat,$dat
0c50f5
+	vorr	$dat1,$dat,$dat
0c50f5
+	vorr	$in3,$dat,$dat
0c50f5
+	vld1.8	{$dat2},[$inp],#16
0c50f5
+	vorr	$in2,$dat2,$dat2
0c50f5
+	vorr	$in4,$dat2,$dat2
0c50f5
+	b.lo	.Lxts_inner_dec_tail
0c50f5
+	veor	$dat,$dat,$iv0			// before decryt, xor with iv
0c50f5
+	veor	$dat2,$dat2,$iv1
0c50f5
+
0c50f5
+	vorr	$dat1,$dat2,$dat2
0c50f5
+	vld1.8	{$dat2},[$inp],#16
0c50f5
+	vorr	$in0,$dat,$dat
0c50f5
+	vorr	$in1,$dat1,$dat1
0c50f5
+	veor	$in2,$dat2,$iv2			// third block xox with third iv
0c50f5
+	veor	$dat2,$dat2,$iv2
0c50f5
+	cmp	$len,#32
0c50f5
+	b.lo	.Lxts_outer_dec_tail
0c50f5
+
0c50f5
+	vld1.8	{$dat3},[$inp],#16
0c50f5
+
0c50f5
+	// The iv for fifth block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd40,$ivl
0c50f5
+	fmov	$ivd41,$ivh
0c50f5
+
0c50f5
+	vld1.8	{$dat4},[$inp],#16
0c50f5
+	veor	$dat3,$dat3,$iv3		// the fourth block
0c50f5
+	veor	$dat4,$dat4,$iv4
0c50f5
+	sub $len,$len,#32			// bias
0c50f5
+	mov	$rounds,$rounds0
0c50f5
+	b	.Loop5x_xts_dec
0c50f5
+
0c50f5
+.align	4
0c50f5
+.Loop5x_xts_dec:
0c50f5
+	aesd	$dat0,q8
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q8
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q8
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q8
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q8
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+	vld1.32	{q8},[$key_],#16		// load key schedule...
0c50f5
+	subs	$rounds,$rounds,#2
0c50f5
+	aesd	$dat0,q9
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q9
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q9
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q9
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q9
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+	vld1.32	{q9},[$key_],#16		// load key schedule...
0c50f5
+	b.gt	.Loop5x_xts_dec
0c50f5
+
0c50f5
+	aesd	$dat0,q8
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q8
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q8
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q8
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q8
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+	subs	$len,$len,#0x50			// because .Lxts_dec_tail4x
0c50f5
+
0c50f5
+	aesd	$dat0,q9
0c50f5
+	aesimc	$dat0,$dat
0c50f5
+	aesd	$dat1,q9
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q9
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q9
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q9
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+	csel	$xoffset,xzr,$len,gt		// borrow x6, w6, "gt" is not typo
0c50f5
+	mov	$key_,$key1
0c50f5
+
0c50f5
+	aesd	$dat0,q10
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q10
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q10
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q10
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q10
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+	add	$inp,$inp,$xoffset		// x0 is adjusted in such way that
0c50f5
+						// at exit from the loop v1.16b-v26.16b
0c50f5
+						// are loaded with last "words"
0c50f5
+	add	$xoffset,$len,#0x60		// because .Lxts_dec_tail4x
0c50f5
+
0c50f5
+	aesd	$dat0,q11
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q11
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q11
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q11
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q11
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+
0c50f5
+	aesd	$dat0,q12
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q12
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q12
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q12
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q12
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+
0c50f5
+	aesd	$dat0,q13
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q13
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q13
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q13
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q13
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+
0c50f5
+	aesd	$dat0,q14
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q14
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q14
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat3,q14
0c50f5
+	aesimc	$dat3,$dat3
0c50f5
+	aesd	$dat4,q14
0c50f5
+	aesimc	$dat4,$dat4
0c50f5
+
0c50f5
+	veor	$tmp0,$rndlast,$iv0
0c50f5
+	aesd	$dat0,q15
0c50f5
+	// The iv for first block of next iteration.
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd00,$ivl
0c50f5
+	fmov	$ivd01,$ivh
0c50f5
+	veor	$tmp1,$rndlast,$iv1
0c50f5
+	vld1.8	{$in0},[$inp],#16
0c50f5
+	aesd	$dat1,q15
0c50f5
+	// The iv for second block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd10,$ivl
0c50f5
+	fmov	$ivd11,$ivh
0c50f5
+	veor	$tmp2,$rndlast,$iv2
0c50f5
+	vld1.8	{$in1},[$inp],#16
0c50f5
+	aesd	$dat2,q15
0c50f5
+	// The iv for third block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd20,$ivl
0c50f5
+	fmov	$ivd21,$ivh
0c50f5
+	veor	$tmp3,$rndlast,$iv3
0c50f5
+	vld1.8	{$in2},[$inp],#16
0c50f5
+	aesd	$dat3,q15
0c50f5
+	// The iv for fourth block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd30,$ivl
0c50f5
+	fmov	$ivd31,$ivh
0c50f5
+	veor	$tmp4,$rndlast,$iv4
0c50f5
+	vld1.8	{$in3},[$inp],#16
0c50f5
+	aesd	$dat4,q15
b63792
+
0c50f5
+	// The iv for fifth block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd40,$ivl
0c50f5
+	fmov	$ivd41,$ivh
b63792
+
0c50f5
+	vld1.8	{$in4},[$inp],#16
0c50f5
+	cbz	$xoffset,.Lxts_dec_tail4x
0c50f5
+	vld1.32	{q8},[$key_],#16		// re-pre-load rndkey[0]
0c50f5
+	veor	$tmp0,$tmp0,$dat0
0c50f5
+	veor	$dat0,$in0,$iv0
0c50f5
+	veor	$tmp1,$tmp1,$dat1
0c50f5
+	veor	$dat1,$in1,$iv1
0c50f5
+	veor	$tmp2,$tmp2,$dat2
0c50f5
+	veor	$dat2,$in2,$iv2
0c50f5
+	veor	$tmp3,$tmp3,$dat3
0c50f5
+	veor	$dat3,$in3,$iv3
0c50f5
+	veor	$tmp4,$tmp4,$dat4
0c50f5
+	vst1.8	{$tmp0},[$out],#16
0c50f5
+	veor	$dat4,$in4,$iv4
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	mov	$rounds,$rounds0
0c50f5
+	vst1.8	{$tmp2},[$out],#16
0c50f5
+	vld1.32	{q9},[$key_],#16		// re-pre-load rndkey[1]
0c50f5
+	vst1.8	{$tmp3},[$out],#16
0c50f5
+	vst1.8	{$tmp4},[$out],#16
0c50f5
+	b.hs	.Loop5x_xts_dec
b63792
+
0c50f5
+	cmn	$len,#0x10
0c50f5
+	b.ne	.Loop5x_dec_after
0c50f5
+	// If x2($len) equal to -0x10, the left blocks is 4.
0c50f5
+	// After specially processing, utilize the five blocks processing again.
0c50f5
+	// It will use the following IVs: $iv0,$iv0,$iv1,$iv2,$iv3.
0c50f5
+	vorr	$iv4,$iv3,$iv3
0c50f5
+	vorr	$iv3,$iv2,$iv2
0c50f5
+	vorr	$iv2,$iv1,$iv1
0c50f5
+	vorr	$iv1,$iv0,$iv0
0c50f5
+	fmov	$ivl,$ivd40
0c50f5
+	fmov	$ivh,$ivd41
0c50f5
+	veor	$dat0,$iv0,$in0
0c50f5
+	veor	$dat1,$iv1,$in1
0c50f5
+	veor	$dat2,$in2,$iv2
0c50f5
+	veor	$dat3,$in3,$iv3
0c50f5
+	veor	$dat4,$in4,$iv4
0c50f5
+	b.eq	.Loop5x_xts_dec
b63792
+
0c50f5
+.Loop5x_dec_after:
0c50f5
+	add	$len,$len,#0x50
0c50f5
+	cbz	$len,.Lxts_done
b63792
+
0c50f5
+	add	$rounds,$rounds0,#2
0c50f5
+	subs	$len,$len,#0x30
0c50f5
+	b.lo	.Lxts_inner_dec_tail
b63792
+
0c50f5
+	veor	$dat0,$iv0,$in2
0c50f5
+	veor	$dat1,$iv1,$in3
0c50f5
+	veor	$dat2,$in4,$iv2
0c50f5
+	b	.Lxts_outer_dec_tail
b63792
+
0c50f5
+.align	4
0c50f5
+.Lxts_dec_tail4x:
0c50f5
+	add	$inp,$inp,#16
0c50f5
+	vld1.32	{$dat0},[$inp],#16
0c50f5
+	veor	$tmp1,$dat1,$tmp0
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	veor	$tmp2,$dat2,$tmp2
0c50f5
+	vst1.8	{$tmp2},[$out],#16
0c50f5
+	veor	$tmp3,$dat3,$tmp3
0c50f5
+	veor	$tmp4,$dat4,$tmp4
0c50f5
+	vst1.8	{$tmp3-$tmp4},[$out],#32
b63792
+
0c50f5
+	b	.Lxts_done
0c50f5
+.align	4
0c50f5
+.Lxts_outer_dec_tail:
0c50f5
+	aesd	$dat0,q8
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q8
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q8
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	vld1.32	{q8},[$key_],#16
0c50f5
+	subs	$rounds,$rounds,#2
0c50f5
+	aesd	$dat0,q9
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q9
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q9
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	vld1.32	{q9},[$key_],#16
0c50f5
+	b.gt	.Lxts_outer_dec_tail
b63792
+
0c50f5
+	aesd	$dat0,q8
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q8
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q8
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	veor	$tmp0,$iv0,$rndlast
0c50f5
+	subs	$len,$len,#0x30
0c50f5
+	// The iv for first block
0c50f5
+	fmov	$ivl,$ivd20
0c50f5
+	fmov	$ivh,$ivd21
0c50f5
+	mov	$constnum,#0x87
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd00,$ivl
0c50f5
+	fmov	$ivd01,$ivh
0c50f5
+	veor	$tmp1,$iv1,$rndlast
0c50f5
+	csel	$xoffset,$len,$xoffset,lo	// x6, w6, is zero at this point
0c50f5
+	aesd	$dat0,q9
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q9
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q9
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	veor	$tmp2,$iv2,$rndlast
0c50f5
+	// The iv for second block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd10,$ivl
0c50f5
+	fmov	$ivd11,$ivh
b63792
+
0c50f5
+	add	$xoffset,$xoffset,#0x20
0c50f5
+	add	$inp,$inp,$xoffset		// $inp is adjusted to the last data
b63792
+
0c50f5
+	mov	$key_,$key1
b63792
+
0c50f5
+	// The iv for third block
0c50f5
+	extr	$midnumx,$ivh,$ivh,#32
0c50f5
+	extr	$ivh,$ivh,$ivl,#63
0c50f5
+	and	$tmpmw,$constnum,$midnum,asr #31
0c50f5
+	eor	$ivl,$tmpmx,$ivl,lsl #1
0c50f5
+	fmov	$ivd20,$ivl
0c50f5
+	fmov	$ivd21,$ivh
0c50f5
+
0c50f5
+	aesd	$dat0,q12
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q12
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q12
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat0,q13
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q13
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q13
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat0,q14
0c50f5
+	aesimc	$dat0,$dat0
0c50f5
+	aesd	$dat1,q14
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q14
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	vld1.8	{$in2},[$inp],#16
0c50f5
+	aesd	$dat0,q15
0c50f5
+	aesd	$dat1,q15
0c50f5
+	aesd	$dat2,q15
0c50f5
+	vld1.32	{q8},[$key_],#16		// re-pre-load rndkey[0]
0c50f5
+	add	$rounds,$rounds0,#2
0c50f5
+	veor	$tmp0,$tmp0,$dat0
0c50f5
+	veor	$tmp1,$tmp1,$dat1
0c50f5
+	veor	$dat2,$dat2,$tmp2
0c50f5
+	vld1.32	{q9},[$key_],#16		// re-pre-load rndkey[1]
0c50f5
+	vst1.8	{$tmp0},[$out],#16
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	vst1.8	{$dat2},[$out],#16
0c50f5
+
0c50f5
+	cmn	$len,#0x30
0c50f5
+	add	$len,$len,#0x30
0c50f5
+	b.eq	.Lxts_done
0c50f5
+	sub	$len,$len,#0x30
0c50f5
+	vorr	$in3,$in1,$in1
0c50f5
+	vorr	$in4,$in2,$in2
0c50f5
+	nop
0c50f5
+
0c50f5
+.Lxts_inner_dec_tail:
0c50f5
+	// $len == -0x10 means two blocks left.
0c50f5
+	cmn	$len,#0x10
0c50f5
+	veor	$dat1,$in3,$iv0
0c50f5
+	veor	$dat2,$in4,$iv1
0c50f5
+	b.eq	.Lxts_dec_tail_loop
0c50f5
+	veor	$dat2,$in4,$iv0
0c50f5
+.Lxts_dec_tail_loop:
0c50f5
+	aesd	$dat1,q8
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q8
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	vld1.32	{q8},[$key_],#16
0c50f5
+	subs	$rounds,$rounds,#2
0c50f5
+	aesd	$dat1,q9
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q9
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	vld1.32	{q9},[$key_],#16
0c50f5
+	b.gt	.Lxts_dec_tail_loop
0c50f5
+
0c50f5
+	aesd	$dat1,q8
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q8
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat1,q9
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q9
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	aesd	$dat1,q12
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q12
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	cmn	$len,#0x20
0c50f5
+	aesd	$dat1,q13
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q13
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	veor	$tmp1,$iv0,$rndlast
0c50f5
+	aesd	$dat1,q14
0c50f5
+	aesimc	$dat1,$dat1
0c50f5
+	aesd	$dat2,q14
0c50f5
+	aesimc	$dat2,$dat2
0c50f5
+	veor	$tmp2,$iv1,$rndlast
0c50f5
+	aesd	$dat1,q15
0c50f5
+	aesd	$dat2,q15
0c50f5
+	b.eq	.Lxts_dec_one
0c50f5
+	veor	$tmp1,$tmp1,$dat1
0c50f5
+	veor	$tmp2,$tmp2,$dat2
0c50f5
+	vorr	$iv0,$iv2,$iv2
0c50f5
+	vorr	$iv1,$iv3,$iv3
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	vst1.8	{$tmp2},[$out],#16
0c50f5
+	add	$len,$len,#16
0c50f5
+	b	.Lxts_done
0c50f5
+
0c50f5
+.Lxts_dec_one:
0c50f5
+	veor	$tmp1,$tmp1,$dat2
0c50f5
+	vorr	$iv0,$iv1,$iv1
0c50f5
+	vorr	$iv1,$iv2,$iv2
0c50f5
+	vst1.8	{$tmp1},[$out],#16
0c50f5
+	add	$len,$len,#32
0c50f5
+
0c50f5
+.Lxts_done:
0c50f5
+	tst	$tailcnt,#0xf
0c50f5
+	b.eq	.Lxts_dec_abort
0c50f5
+	// Processing the last two blocks with cipher stealing.
0c50f5
+	mov	x7,x3
0c50f5
+	cbnz	x2,.Lxts_dec_1st_done
0c50f5
+	vld1.32	{$dat0},[$inp],#16
0c50f5
+
0c50f5
+	// Decrypt the last secod block to get the last plain text block
0c50f5
+.Lxts_dec_1st_done:
0c50f5
+	eor	$tmpin,$dat0,$iv1
0c50f5
+	ldr	$rounds,[$key1,#240]
0c50f5
+	vld1.32	{$dat0},[$key1],#16
0c50f5
+	sub	$rounds,$rounds,#2
0c50f5
+	vld1.32	{$dat1},[$key1],#16
0c50f5
+.Loop_final_2nd_dec:
0c50f5
+	aesd	$tmpin,$dat0
0c50f5
+	aesimc	$tmpin,$tmpin
0c50f5
+	vld1.32	{$dat0},[$key1],#16		// load key schedule...
0c50f5
+	subs	$rounds,$rounds,#2
0c50f5
+	aesd	$tmpin,$dat1
0c50f5
+	aesimc	$tmpin,$tmpin
0c50f5
+	vld1.32	{$dat1},[$key1],#16		// load key schedule...
0c50f5
+	b.gt	.Loop_final_2nd_dec
0c50f5
+
0c50f5
+	aesd	$tmpin,$dat0
0c50f5
+	aesimc	$tmpin,$tmpin
0c50f5
+	vld1.32	{$dat0},[$key1]
0c50f5
+	aesd	$tmpin,$dat1
0c50f5
+	veor	$tmpin,$tmpin,$dat0
0c50f5
+	veor	$tmpin,$tmpin,$iv1
0c50f5
+	vst1.8	{$tmpin},[$out]
0c50f5
+
0c50f5
+	mov	$tmpinp,$inp
0c50f5
+	add	$tmpoutp,$out,#16
0c50f5
+
0c50f5
+	// Composite the tailcnt "16 byte not aligned block" into the last second plain blocks
0c50f5
+	// to get the last encrypted block.
0c50f5
+.composite_dec_loop:
0c50f5
+	subs	$tailcnt,$tailcnt,#1
0c50f5
+	ldrb	$l2outp,[$out,$tailcnt]
0c50f5
+	ldrb	$loutp,[$tmpinp,$tailcnt]
0c50f5
+	strb	$l2outp,[$tmpoutp,$tailcnt]
0c50f5
+	strb	$loutp,[$out,$tailcnt]
0c50f5
+	b.gt	.composite_dec_loop
0c50f5
+.Lxts_dec_load_done:
0c50f5
+	vld1.8	{$tmpin},[$out]
0c50f5
+	veor	$tmpin,$tmpin,$iv0
0c50f5
+
0c50f5
+	// Decrypt the composite block to get the last second plain text block
0c50f5
+	ldr	$rounds,[$key_,#240]
0c50f5
+	vld1.8	{$dat},[$key_],#16
0c50f5
+	sub	$rounds,$rounds,#2
0c50f5
+	vld1.8	{$dat1},[$key_],#16
0c50f5
+.Loop_final_dec:
0c50f5
+	aesd	$tmpin,$dat0
0c50f5
+	aesimc	$tmpin,$tmpin
0c50f5
+	vld1.32	{$dat0},[$key_],#16		// load key schedule...
0c50f5
+	subs	$rounds,$rounds,#2
0c50f5
+	aesd	$tmpin,$dat1
0c50f5
+	aesimc	$tmpin,$tmpin
0c50f5
+	vld1.32	{$dat1},[$key_],#16		// load key schedule...
0c50f5
+	b.gt	.Loop_final_dec
0c50f5
+
0c50f5
+	aesd	$tmpin,$dat0
0c50f5
+	aesimc	$tmpin,$tmpin
0c50f5
+	vld1.32	{$dat0},[$key_]
0c50f5
+	aesd	$tmpin,$dat1
0c50f5
+	veor	$tmpin,$tmpin,$dat0
0c50f5
+	veor	$tmpin,$tmpin,$iv0
0c50f5
+	vst1.8	{$tmpin},[$out]
0c50f5
+
0c50f5
+.Lxts_dec_abort:
0c50f5
+	ldp	$tailcnt,$midnumx,[sp,#48]
0c50f5
+	ldp	$ivd10,$ivd20,[sp,#32]
0c50f5
+	ldp	$ivd30,$ivd40,[sp,#16]
0c50f5
+	ldp	$constnumx,$tmpinp,[sp],#64
0c50f5
+
0c50f5
+.Lxts_dec_final_abort:
0c50f5
+	ret
0c50f5
+.size	${prefix}_xts_decrypt,.-${prefix}_xts_decrypt
b63792
+___
0c50f5
+}
0c50f5
+}}}
0c50f5
 $code.=<<___;
0c50f5
 #endif
0c50f5
 ___
0c50f5
@@ -963,7 +3615,7 @@ if ($flavour =~ /64/) {			######## 64-bi
b63792
 	    # since ARMv7 instructions are always encoded little-endian.
b63792
 	    # correct solution is to use .inst directive, but older
b63792
 	    # assemblers don't implement it:-(
b63792
-	    sprintf ".byte\t0x%02x,0x%02x,0x%02x,0x%02x\t@ %s %s",
b63792
+	    sprintf "INST(0x%02x,0x%02x,0x%02x,0x%02x)\t@ %s %s",
b63792
 			$word&0xff,($word>>8)&0xff,
b63792
 			($word>>16)&0xff,($word>>24)&0xff,
b63792
 			$mnemonic,$arg;
0c50f5
@@ -1004,14 +3656,17 @@ if ($flavour =~ /64/) {			######## 64-bi
b63792
 	s/\],#[0-9]+/]!/o;
b63792
 
b63792
 	s/[v]?(aes\w+)\s+([qv].*)/unaes($1,$2)/geo	or
b63792
-	s/cclr\s+([^,]+),\s*([a-z]+)/mov$2	$1,#0/o	or
b63792
+	s/cclr\s+([^,]+),\s*([a-z]+)/mov.$2	$1,#0/o	or
b63792
 	s/vtbl\.8\s+(.*)/unvtbl($1)/geo			or
b63792
 	s/vdup\.32\s+(.*)/unvdup32($1)/geo		or
b63792
 	s/vmov\.32\s+(.*)/unvmov32($1)/geo		or
b63792
 	s/^(\s+)b\./$1b/o				or
b63792
-	s/^(\s+)mov\./$1mov/o				or
b63792
 	s/^(\s+)ret/$1bx\tlr/o;
b63792
 
b63792
+	if (s/^(\s+)mov\.([a-z]+)/$1mov$2/) {
b63792
+	    print "	it	$2\n";
b63792
+	}
b63792
+
b63792
 	print $_,"\n";
b63792
     }
b63792
 }
0c50f5
diff -up openssl-1.1.1i/crypto/aes/asm/vpaes-armv8.pl.arm-update openssl-1.1.1i/crypto/aes/asm/vpaes-armv8.pl
0c50f5
--- openssl-1.1.1i/crypto/aes/asm/vpaes-armv8.pl.arm-update	2020-12-08 14:20:59.000000000 +0100
0c50f5
+++ openssl-1.1.1i/crypto/aes/asm/vpaes-armv8.pl	2020-12-09 10:37:38.405558929 +0100
b63792
@@ -30,6 +30,7 @@
b63792
 # Denver(***)       16.6(**)    15.1/17.8(**)    [8.80/9.93         ]
b63792
 # Apple A7(***)     22.7(**)    10.9/14.3        [8.45/10.0         ]
b63792
 # Mongoose(***)     26.3(**)    21.0/25.0(**)    [13.3/16.8         ]
b63792
+# ThunderX2(***)    39.4(**)    33.8/48.6(**)
b63792
 #
b63792
 # (*)	ECB denotes approximate result for parallelizable modes
b63792
 #	such as CBC decrypt, CTR, etc.;
0c50f5
diff -up openssl-1.1.1i/crypto/chacha/asm/chacha-armv8.pl.arm-update openssl-1.1.1i/crypto/chacha/asm/chacha-armv8.pl
0c50f5
--- openssl-1.1.1i/crypto/chacha/asm/chacha-armv8.pl.arm-update	2020-12-08 14:20:59.000000000 +0100
0c50f5
+++ openssl-1.1.1i/crypto/chacha/asm/chacha-armv8.pl	2020-12-09 10:40:57.922288627 +0100
b63792
@@ -18,32 +18,44 @@
b63792
 #
b63792
 # ChaCha20 for ARMv8.
b63792
 #
b63792
+# April 2019
b63792
+#
b63792
+# Replace 3xNEON+1xIALU code path with 4+1. 4+1 is actually fastest
b63792
+# option on most(*), but not all, processors, yet 6+2 is retained.
b63792
+# This is because penalties are considered tolerable in comparison to
b63792
+# improvement on processors where 6+2 helps. Most notably +37% on
b63792
+# ThunderX2. It's server-oriented processor which will have to serve
b63792
+# as many requests as possible. While others are mostly clients, when
b63792
+# performance doesn't have to be absolute top-notch, just fast enough,
b63792
+# as majority of time is spent "entertaining" relatively slow human.
b63792
+#
b63792
 # Performance in cycles per byte out of large buffer.
b63792
 #
b63792
-#			IALU/gcc-4.9    3xNEON+1xIALU	6xNEON+2xIALU
b63792
+#			IALU/gcc-4.9	4xNEON+1xIALU	6xNEON+2xIALU
b63792
 #
b63792
-# Apple A7		5.50/+49%       3.33            1.70
b63792
-# Cortex-A53		8.40/+80%       4.72		4.72(*)
b63792
-# Cortex-A57		8.06/+43%       4.90            4.43(**)
b63792
-# Denver		4.50/+82%       2.63		2.67(*)
b63792
-# X-Gene		9.50/+46%       8.82		8.89(*)
b63792
-# Mongoose		8.00/+44%	3.64		3.25
b63792
-# Kryo			8.17/+50%	4.83		4.65
b63792
+# Apple A7		5.50/+49%	2.72		1.60
b63792
+# Cortex-A53		8.40/+80%	4.06		4.45(*)
b63792
+# Cortex-A57		8.06/+43%	4.15		4.40(*)
b63792
+# Denver		4.50/+82%	2.30		2.70(*)
b63792
+# X-Gene		9.50/+46%	8.20		8.90(*)
b63792
+# Mongoose		8.00/+44%	2.74		3.12(*)
b63792
+# Kryo			8.17/+50%	4.47		4.65(*)
b63792
+# ThunderX2		7.22/+48%	5.64		4.10
b63792
 #
b63792
-# (*)	it's expected that doubling interleave factor doesn't help
b63792
-#	all processors, only those with higher NEON latency and
b63792
-#	higher instruction issue rate;
b63792
-# (**)	expected improvement was actually higher;
b63792
+# (*)	slower than 4+1:-(
b63792
 
b63792
-$flavour=shift;
b63792
-$output=shift;
b63792
+# $output is the last argument if it looks like a file (it has an extension)
b63792
+# $flavour is the first argument if it doesn't look like a file
b63792
+$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
b63792
+$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
b63792
 
b63792
 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
b63792
 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
b63792
 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
b63792
 die "can't locate arm-xlate.pl";
b63792
 
b63792
-open OUT,"| \"$^X\" $xlate $flavour $output";
b63792
+open OUT,"| \"$^X\" $xlate $flavour \"$output\""
b63792
+    or die "can't call $xlate: $!";
b63792
 *STDOUT=*OUT;
b63792
 
b63792
 sub AUTOLOAD()		# thunk [simplified] x86-style perlasm
0c50f5
@@ -120,42 +132,37 @@ my ($a3,$b3,$c3,$d3)=map(($_&~3)+(($_+1)
b63792
 }
b63792
 
b63792
 $code.=<<___;
b63792
-#include "arm_arch.h"
0c50f5
-
0c50f5
-.text
0c50f5
-
b63792
+#ifndef	__KERNEL__
b63792
+# include "arm_arch.h"
0c50f5
 .extern	OPENSSL_armcap_P
0c50f5
 .hidden	OPENSSL_armcap_P
b63792
+#endif
0c50f5
+
0c50f5
+.text
b63792
 
b63792
 .align	5
b63792
 .Lsigma:
b63792
 .quad	0x3320646e61707865,0x6b20657479622d32		// endian-neutral
b63792
 .Lone:
b63792
-.long	1,0,0,0
b63792
-.LOPENSSL_armcap_P:
b63792
-#ifdef	__ILP32__
b63792
-.long	OPENSSL_armcap_P-.
b63792
-#else
b63792
-.quad	OPENSSL_armcap_P-.
b63792
-#endif
b63792
-.asciz	"ChaCha20 for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
b63792
+.long	1,2,3,4
b63792
+.Lrot24:
b63792
+.long	0x02010003,0x06050407,0x0a09080b,0x0e0d0c0f
b63792
+.asciz	"ChaCha20 for ARMv8, CRYPTOGAMS by \@dot-asm"
b63792
 
b63792
 .globl	ChaCha20_ctr32
b63792
 .type	ChaCha20_ctr32,%function
b63792
 .align	5
b63792
 ChaCha20_ctr32:
b63792
 	cbz	$len,.Labort
b63792
-	adr	@x[0],.LOPENSSL_armcap_P
b63792
 	cmp	$len,#192
b63792
 	b.lo	.Lshort
b63792
-#ifdef	__ILP32__
b63792
-	ldrsw	@x[1],[@x[0]]
b63792
-#else
b63792
-	ldr	@x[1],[@x[0]]
b63792
-#endif
b63792
-	ldr	w17,[@x[1],@x[0]]
b63792
+
b63792
+#ifndef	__KERNEL__
b63792
+	adrp	x17,OPENSSL_armcap_P
b63792
+	ldr	w17,[x17,#:lo12:OPENSSL_armcap_P]
b63792
 	tst	w17,#ARMV7_NEON
b63792
-	b.ne	ChaCha20_neon
b63792
+	b.ne	.LChaCha20_neon
b63792
+#endif
b63792
 
b63792
 .Lshort:
b63792
 	.inst	0xd503233f			// paciasp
0c50f5
@@ -174,7 +181,7 @@ ChaCha20_ctr32:
b63792
 	ldp	@d[2],@d[3],[$key]		// load key
b63792
 	ldp	@d[4],@d[5],[$key,#16]
b63792
 	ldp	@d[6],@d[7],[$ctr]		// load counter
b63792
-#ifdef	__ARMEB__
b63792
+#ifdef	__AARCH64EB__
b63792
 	ror	@d[2],@d[2],#32
b63792
 	ror	@d[3],@d[3],#32
b63792
 	ror	@d[4],@d[4],#32
0c50f5
@@ -243,7 +250,7 @@ $code.=<<___;
b63792
 	add	@x[14],@x[14],@x[15],lsl#32
b63792
 	ldp	@x[13],@x[15],[$inp,#48]
b63792
 	add	$inp,$inp,#64
b63792
-#ifdef	__ARMEB__
b63792
+#ifdef	__AARCH64EB__
b63792
 	rev	@x[0],@x[0]
b63792
 	rev	@x[2],@x[2]
b63792
 	rev	@x[4],@x[4]
0c50f5
@@ -300,7 +307,7 @@ $code.=<<___;
b63792
 	add	@x[10],@x[10],@x[11],lsl#32
b63792
 	add	@x[12],@x[12],@x[13],lsl#32
b63792
 	add	@x[14],@x[14],@x[15],lsl#32
b63792
-#ifdef	__ARMEB__
b63792
+#ifdef	__AARCH64EB__
b63792
 	rev	@x[0],@x[0]
b63792
 	rev	@x[2],@x[2]
b63792
 	rev	@x[4],@x[4]
0c50f5
@@ -341,46 +348,91 @@ $code.=<<___;
b63792
 ___
b63792
 
b63792
 {{{
b63792
-my ($A0,$B0,$C0,$D0,$A1,$B1,$C1,$D1,$A2,$B2,$C2,$D2,$T0,$T1,$T2,$T3) =
b63792
-    map("v$_.4s",(0..7,16..23));
b63792
-my (@K)=map("v$_.4s",(24..30));
b63792
-my $ONE="v31.4s";
b63792
+my @K = map("v$_.4s",(0..3));
b63792
+my ($xt0,$xt1,$xt2,$xt3, $CTR,$ROT24) = map("v$_.4s",(4..9));
b63792
+my @X = map("v$_.4s",(16,20,24,28, 17,21,25,29, 18,22,26,30, 19,23,27,31));
b63792
+my ($xa0,$xa1,$xa2,$xa3, $xb0,$xb1,$xb2,$xb3,
b63792
+    $xc0,$xc1,$xc2,$xc3, $xd0,$xd1,$xd2,$xd3) = @X;
b63792
 
b63792
-sub NEONROUND {
b63792
-my $odd = pop;
b63792
-my ($a,$b,$c,$d,$t)=@_;
b63792
+sub NEON_lane_ROUND {
b63792
+my ($a0,$b0,$c0,$d0)=@_;
b63792
+my ($a1,$b1,$c1,$d1)=map(($_&~3)+(($_+1)&3),($a0,$b0,$c0,$d0));
b63792
+my ($a2,$b2,$c2,$d2)=map(($_&~3)+(($_+1)&3),($a1,$b1,$c1,$d1));
b63792
+my ($a3,$b3,$c3,$d3)=map(($_&~3)+(($_+1)&3),($a2,$b2,$c2,$d2));
b63792
+my @x=map("'$_'",@X);
b63792
 
b63792
 	(
b63792
-	"&add		('$a','$a','$b')",
b63792
-	"&eor		('$d','$d','$a')",
b63792
-	"&rev32_16	('$d','$d')",		# vrot ($d,16)
b63792
-
b63792
-	"&add		('$c','$c','$d')",
b63792
-	"&eor		('$t','$b','$c')",
b63792
-	"&ushr		('$b','$t',20)",
b63792
-	"&sli		('$b','$t',12)",
b63792
-
b63792
-	"&add		('$a','$a','$b')",
b63792
-	"&eor		('$t','$d','$a')",
b63792
-	"&ushr		('$d','$t',24)",
b63792
-	"&sli		('$d','$t',8)",
b63792
-
b63792
-	"&add		('$c','$c','$d')",
b63792
-	"&eor		('$t','$b','$c')",
b63792
-	"&ushr		('$b','$t',25)",
b63792
-	"&sli		('$b','$t',7)",
b63792
-
b63792
-	"&ext		('$c','$c','$c',8)",
b63792
-	"&ext		('$d','$d','$d',$odd?4:12)",
b63792
-	"&ext		('$b','$b','$b',$odd?12:4)"
b63792
+	"&add		(@x[$a0],@x[$a0],@x[$b0])",	# Q1
b63792
+	 "&add		(@x[$a1],@x[$a1],@x[$b1])",	# Q2
b63792
+	  "&add		(@x[$a2],@x[$a2],@x[$b2])",	# Q3
b63792
+	   "&add	(@x[$a3],@x[$a3],@x[$b3])",	# Q4
b63792
+	"&eor		(@x[$d0],@x[$d0],@x[$a0])",
b63792
+	 "&eor		(@x[$d1],@x[$d1],@x[$a1])",
b63792
+	  "&eor		(@x[$d2],@x[$d2],@x[$a2])",
b63792
+	   "&eor	(@x[$d3],@x[$d3],@x[$a3])",
b63792
+	"&rev32_16	(@x[$d0],@x[$d0])",
b63792
+	 "&rev32_16	(@x[$d1],@x[$d1])",
b63792
+	  "&rev32_16	(@x[$d2],@x[$d2])",
b63792
+	   "&rev32_16	(@x[$d3],@x[$d3])",
b63792
+
b63792
+	"&add		(@x[$c0],@x[$c0],@x[$d0])",
b63792
+	 "&add		(@x[$c1],@x[$c1],@x[$d1])",
b63792
+	  "&add		(@x[$c2],@x[$c2],@x[$d2])",
b63792
+	   "&add	(@x[$c3],@x[$c3],@x[$d3])",
b63792
+	"&eor		('$xt0',@x[$b0],@x[$c0])",
b63792
+	 "&eor		('$xt1',@x[$b1],@x[$c1])",
b63792
+	  "&eor		('$xt2',@x[$b2],@x[$c2])",
b63792
+	   "&eor	('$xt3',@x[$b3],@x[$c3])",
b63792
+	"&ushr		(@x[$b0],'$xt0',20)",
b63792
+	 "&ushr		(@x[$b1],'$xt1',20)",
b63792
+	  "&ushr	(@x[$b2],'$xt2',20)",
b63792
+	   "&ushr	(@x[$b3],'$xt3',20)",
b63792
+	"&sli		(@x[$b0],'$xt0',12)",
b63792
+	 "&sli		(@x[$b1],'$xt1',12)",
b63792
+	  "&sli		(@x[$b2],'$xt2',12)",
b63792
+	   "&sli	(@x[$b3],'$xt3',12)",
b63792
+
b63792
+	"&add		(@x[$a0],@x[$a0],@x[$b0])",
b63792
+	 "&add		(@x[$a1],@x[$a1],@x[$b1])",
b63792
+	  "&add		(@x[$a2],@x[$a2],@x[$b2])",
b63792
+	   "&add	(@x[$a3],@x[$a3],@x[$b3])",
b63792
+	"&eor		('$xt0',@x[$d0],@x[$a0])",
b63792
+	 "&eor		('$xt1',@x[$d1],@x[$a1])",
b63792
+	  "&eor		('$xt2',@x[$d2],@x[$a2])",
b63792
+	   "&eor	('$xt3',@x[$d3],@x[$a3])",
b63792
+	"&tbl		(@x[$d0],'{$xt0}','$ROT24')",
b63792
+	 "&tbl		(@x[$d1],'{$xt1}','$ROT24')",
b63792
+	  "&tbl		(@x[$d2],'{$xt2}','$ROT24')",
b63792
+	   "&tbl	(@x[$d3],'{$xt3}','$ROT24')",
b63792
+
b63792
+	"&add		(@x[$c0],@x[$c0],@x[$d0])",
b63792
+	 "&add		(@x[$c1],@x[$c1],@x[$d1])",
b63792
+	  "&add		(@x[$c2],@x[$c2],@x[$d2])",
b63792
+	   "&add	(@x[$c3],@x[$c3],@x[$d3])",
b63792
+	"&eor		('$xt0',@x[$b0],@x[$c0])",
b63792
+	 "&eor		('$xt1',@x[$b1],@x[$c1])",
b63792
+	  "&eor		('$xt2',@x[$b2],@x[$c2])",
b63792
+	   "&eor	('$xt3',@x[$b3],@x[$c3])",
b63792
+	"&ushr		(@x[$b0],'$xt0',25)",
b63792
+	 "&ushr		(@x[$b1],'$xt1',25)",
b63792
+	  "&ushr	(@x[$b2],'$xt2',25)",
b63792
+	   "&ushr	(@x[$b3],'$xt3',25)",
b63792
+	"&sli		(@x[$b0],'$xt0',7)",
b63792
+	 "&sli		(@x[$b1],'$xt1',7)",
b63792
+	  "&sli		(@x[$b2],'$xt2',7)",
b63792
+	   "&sli	(@x[$b3],'$xt3',7)"
b63792
 	);
b63792
 }
b63792
 
b63792
 $code.=<<___;
b63792
 
b63792
+#ifdef	__KERNEL__
b63792
+.globl	ChaCha20_neon
b63792
+#endif
b63792
 .type	ChaCha20_neon,%function
b63792
 .align	5
b63792
 ChaCha20_neon:
b63792
+.LChaCha20_neon:
b63792
 	.inst	0xd503233f			// paciasp
b63792
 	stp	x29,x30,[sp,#-96]!
b63792
 	add	x29,sp,#0
0c50f5
@@ -403,8 +455,9 @@ ChaCha20_neon:
b63792
 	ld1	{@K[1],@K[2]},[$key]
b63792
 	ldp	@d[6],@d[7],[$ctr]		// load counter
b63792
 	ld1	{@K[3]},[$ctr]
b63792
-	ld1	{$ONE},[@x[0]]
b63792
-#ifdef	__ARMEB__
b63792
+	stp	d8,d9,[sp]			// meet ABI requirements
b63792
+	ld1	{$CTR,$ROT24},[@x[0]]
b63792
+#ifdef	__AARCH64EB__
b63792
 	rev64	@K[0],@K[0]
b63792
 	ror	@d[2],@d[2],#32
b63792
 	ror	@d[3],@d[3],#32
0c50f5
@@ -413,115 +466,129 @@ ChaCha20_neon:
b63792
 	ror	@d[6],@d[6],#32
b63792
 	ror	@d[7],@d[7],#32
b63792
 #endif
b63792
-	add	@K[3],@K[3],$ONE		// += 1
b63792
-	add	@K[4],@K[3],$ONE
b63792
-	add	@K[5],@K[4],$ONE
b63792
-	shl	$ONE,$ONE,#2			// 1 -> 4
b63792
 
b63792
 .Loop_outer_neon:
b63792
-	mov.32	@x[0],@d[0]			// unpack key block
b63792
-	lsr	@x[1],@d[0],#32
b63792
-	 mov	$A0,@K[0]
b63792
-	mov.32	@x[2],@d[1]
b63792
-	lsr	@x[3],@d[1],#32
b63792
-	 mov	$A1,@K[0]
b63792
-	mov.32	@x[4],@d[2]
b63792
-	lsr	@x[5],@d[2],#32
b63792
-	 mov	$A2,@K[0]
b63792
-	mov.32	@x[6],@d[3]
b63792
-	 mov	$B0,@K[1]
b63792
-	lsr	@x[7],@d[3],#32
b63792
-	 mov	$B1,@K[1]
b63792
-	mov.32	@x[8],@d[4]
b63792
-	 mov	$B2,@K[1]
b63792
-	lsr	@x[9],@d[4],#32
b63792
-	 mov	$D0,@K[3]
b63792
-	mov.32	@x[10],@d[5]
b63792
-	 mov	$D1,@K[4]
b63792
-	lsr	@x[11],@d[5],#32
b63792
-	 mov	$D2,@K[5]
b63792
-	mov.32	@x[12],@d[6]
b63792
-	 mov	$C0,@K[2]
b63792
-	lsr	@x[13],@d[6],#32
b63792
-	 mov	$C1,@K[2]
b63792
-	mov.32	@x[14],@d[7]
b63792
-	 mov	$C2,@K[2]
b63792
-	lsr	@x[15],@d[7],#32
b63792
+	dup	$xa0,@{K[0]}[0]			// unpack key block
b63792
+	 mov.32	@x[0],@d[0]
b63792
+	dup	$xa1,@{K[0]}[1]
b63792
+	 lsr	@x[1],@d[0],#32
b63792
+	dup	$xa2,@{K[0]}[2]
b63792
+	 mov.32	@x[2],@d[1]
b63792
+	dup	$xa3,@{K[0]}[3]
b63792
+	 lsr	@x[3],@d[1],#32
b63792
+	dup	$xb0,@{K[1]}[0]
b63792
+	 mov.32	@x[4],@d[2]
b63792
+	dup	$xb1,@{K[1]}[1]
b63792
+	 lsr	@x[5],@d[2],#32
b63792
+	dup	$xb2,@{K[1]}[2]
b63792
+	 mov.32	@x[6],@d[3]
b63792
+	dup	$xb3,@{K[1]}[3]
b63792
+	 lsr	@x[7],@d[3],#32
b63792
+	dup	$xd0,@{K[3]}[0]
b63792
+	 mov.32	@x[8],@d[4]
b63792
+	dup	$xd1,@{K[3]}[1]
b63792
+	 lsr	@x[9],@d[4],#32
b63792
+	dup	$xd2,@{K[3]}[2]
b63792
+	 mov.32	@x[10],@d[5]
b63792
+	dup	$xd3,@{K[3]}[3]
b63792
+	 lsr	@x[11],@d[5],#32
b63792
+	add	$xd0,$xd0,$CTR
b63792
+	 mov.32	@x[12],@d[6]
b63792
+	dup	$xc0,@{K[2]}[0]
b63792
+	 lsr	@x[13],@d[6],#32
b63792
+	dup	$xc1,@{K[2]}[1]
b63792
+	 mov.32	@x[14],@d[7]
b63792
+	dup	$xc2,@{K[2]}[2]
b63792
+	 lsr	@x[15],@d[7],#32
b63792
+	dup	$xc3,@{K[2]}[3]
b63792
 
b63792
 	mov	$ctr,#10
b63792
-	subs	$len,$len,#256
b63792
+	subs	$len,$len,#320
b63792
 .Loop_neon:
b63792
 	sub	$ctr,$ctr,#1
b63792
 ___
b63792
-	my @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,0);
b63792
-	my @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,0);
b63792
-	my @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,0);
b63792
-	my @thread3=&ROUND(0,4,8,12);
b63792
-
b63792
-	foreach (@thread0) {
b63792
-		eval;			eval(shift(@thread3));
b63792
-		eval(shift(@thread1));	eval(shift(@thread3));
b63792
-		eval(shift(@thread2));	eval(shift(@thread3));
b63792
-	}
b63792
-
b63792
-	@thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,1);
b63792
-	@thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,1);
b63792
-	@thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,1);
b63792
-	@thread3=&ROUND(0,5,10,15);
b63792
+	my @plus_one=&ROUND(0,4,8,12);
b63792
+	foreach (&NEON_lane_ROUND(0,4,8,12))  { eval; eval(shift(@plus_one)); }
b63792
 
b63792
-	foreach (@thread0) {
b63792
-		eval;			eval(shift(@thread3));
b63792
-		eval(shift(@thread1));	eval(shift(@thread3));
b63792
-		eval(shift(@thread2));	eval(shift(@thread3));
b63792
-	}
b63792
+	@plus_one=&ROUND(0,5,10,15);
b63792
+	foreach (&NEON_lane_ROUND(0,5,10,15)) { eval; eval(shift(@plus_one)); }
b63792
 $code.=<<___;
b63792
 	cbnz	$ctr,.Loop_neon
b63792
 
b63792
-	add.32	@x[0],@x[0],@d[0]		// accumulate key block
b63792
-	 add	$A0,$A0,@K[0]
b63792
-	add	@x[1],@x[1],@d[0],lsr#32
b63792
-	 add	$A1,$A1,@K[0]
b63792
-	add.32	@x[2],@x[2],@d[1]
b63792
-	 add	$A2,$A2,@K[0]
b63792
-	add	@x[3],@x[3],@d[1],lsr#32
b63792
-	 add	$C0,$C0,@K[2]
b63792
-	add.32	@x[4],@x[4],@d[2]
b63792
-	 add	$C1,$C1,@K[2]
b63792
-	add	@x[5],@x[5],@d[2],lsr#32
b63792
-	 add	$C2,$C2,@K[2]
b63792
-	add.32	@x[6],@x[6],@d[3]
b63792
-	 add	$D0,$D0,@K[3]
b63792
-	add	@x[7],@x[7],@d[3],lsr#32
b63792
-	add.32	@x[8],@x[8],@d[4]
b63792
-	 add	$D1,$D1,@K[4]
b63792
-	add	@x[9],@x[9],@d[4],lsr#32
b63792
-	add.32	@x[10],@x[10],@d[5]
b63792
-	 add	$D2,$D2,@K[5]
b63792
-	add	@x[11],@x[11],@d[5],lsr#32
b63792
-	add.32	@x[12],@x[12],@d[6]
b63792
-	 add	$B0,$B0,@K[1]
b63792
-	add	@x[13],@x[13],@d[6],lsr#32
b63792
-	add.32	@x[14],@x[14],@d[7]
b63792
-	 add	$B1,$B1,@K[1]
b63792
-	add	@x[15],@x[15],@d[7],lsr#32
b63792
-	 add	$B2,$B2,@K[1]
b63792
+	add	$xd0,$xd0,$CTR
b63792
+
b63792
+	zip1	$xt0,$xa0,$xa1			// transpose data
b63792
+	zip1	$xt1,$xa2,$xa3
b63792
+	zip2	$xt2,$xa0,$xa1
b63792
+	zip2	$xt3,$xa2,$xa3
b63792
+	zip1.64	$xa0,$xt0,$xt1
b63792
+	zip2.64	$xa1,$xt0,$xt1
b63792
+	zip1.64	$xa2,$xt2,$xt3
b63792
+	zip2.64	$xa3,$xt2,$xt3
b63792
+
b63792
+	zip1	$xt0,$xb0,$xb1
b63792
+	zip1	$xt1,$xb2,$xb3
b63792
+	zip2	$xt2,$xb0,$xb1
b63792
+	zip2	$xt3,$xb2,$xb3
b63792
+	zip1.64	$xb0,$xt0,$xt1
b63792
+	zip2.64	$xb1,$xt0,$xt1
b63792
+	zip1.64	$xb2,$xt2,$xt3
b63792
+	zip2.64	$xb3,$xt2,$xt3
b63792
+
b63792
+	zip1	$xt0,$xc0,$xc1
b63792
+	 add.32	@x[0],@x[0],@d[0]		// accumulate key block
b63792
+	zip1	$xt1,$xc2,$xc3
b63792
+	 add	@x[1],@x[1],@d[0],lsr#32
b63792
+	zip2	$xt2,$xc0,$xc1
b63792
+	 add.32	@x[2],@x[2],@d[1]
b63792
+	zip2	$xt3,$xc2,$xc3
b63792
+	 add	@x[3],@x[3],@d[1],lsr#32
b63792
+	zip1.64	$xc0,$xt0,$xt1
b63792
+	 add.32	@x[4],@x[4],@d[2]
b63792
+	zip2.64	$xc1,$xt0,$xt1
b63792
+	 add	@x[5],@x[5],@d[2],lsr#32
b63792
+	zip1.64	$xc2,$xt2,$xt3
b63792
+	 add.32	@x[6],@x[6],@d[3]
b63792
+	zip2.64	$xc3,$xt2,$xt3
b63792
+	 add	@x[7],@x[7],@d[3],lsr#32
b63792
+
b63792
+	zip1	$xt0,$xd0,$xd1
b63792
+	 add.32	@x[8],@x[8],@d[4]
b63792
+	zip1	$xt1,$xd2,$xd3
b63792
+	 add	@x[9],@x[9],@d[4],lsr#32
b63792
+	zip2	$xt2,$xd0,$xd1
b63792
+	 add.32	@x[10],@x[10],@d[5]
b63792
+	zip2	$xt3,$xd2,$xd3
b63792
+	 add	@x[11],@x[11],@d[5],lsr#32
b63792
+	zip1.64	$xd0,$xt0,$xt1
b63792
+	 add.32	@x[12],@x[12],@d[6]
b63792
+	zip2.64	$xd1,$xt0,$xt1
b63792
+	 add	@x[13],@x[13],@d[6],lsr#32
b63792
+	zip1.64	$xd2,$xt2,$xt3
b63792
+	 add.32	@x[14],@x[14],@d[7]
b63792
+	zip2.64	$xd3,$xt2,$xt3
b63792
+	 add	@x[15],@x[15],@d[7],lsr#32
b63792
 
b63792
 	b.lo	.Ltail_neon
b63792
 
b63792
 	add	@x[0],@x[0],@x[1],lsl#32	// pack
b63792
 	add	@x[2],@x[2],@x[3],lsl#32
b63792
 	ldp	@x[1],@x[3],[$inp,#0]		// load input
b63792
+	 add	$xa0,$xa0,@K[0]			// accumulate key block
b63792
 	add	@x[4],@x[4],@x[5],lsl#32
b63792
 	add	@x[6],@x[6],@x[7],lsl#32
b63792
 	ldp	@x[5],@x[7],[$inp,#16]
b63792
+	 add	$xb0,$xb0,@K[1]
b63792
 	add	@x[8],@x[8],@x[9],lsl#32
b63792
 	add	@x[10],@x[10],@x[11],lsl#32
b63792
 	ldp	@x[9],@x[11],[$inp,#32]
b63792
+	 add	$xc0,$xc0,@K[2]
b63792
 	add	@x[12],@x[12],@x[13],lsl#32
b63792
 	add	@x[14],@x[14],@x[15],lsl#32
b63792
 	ldp	@x[13],@x[15],[$inp,#48]
b63792
+	 add	$xd0,$xd0,@K[3]
b63792
 	add	$inp,$inp,#64
b63792
-#ifdef	__ARMEB__
b63792
+#ifdef	__AARCH64EB__
b63792
 	rev	@x[0],@x[0]
b63792
 	rev	@x[2],@x[2]
b63792
 	rev	@x[4],@x[4]
0c50f5
@@ -531,48 +598,68 @@ $code.=<<___;
b63792
 	rev	@x[12],@x[12]
b63792
 	rev	@x[14],@x[14]
b63792
 #endif
b63792
-	ld1.8	{$T0-$T3},[$inp],#64
b63792
+	ld1.8	{$xt0-$xt3},[$inp],#64
b63792
 	eor	@x[0],@x[0],@x[1]
b63792
+	 add	$xa1,$xa1,@K[0]
b63792
 	eor	@x[2],@x[2],@x[3]
b63792
+	 add	$xb1,$xb1,@K[1]
b63792
 	eor	@x[4],@x[4],@x[5]
b63792
+	 add	$xc1,$xc1,@K[2]
b63792
 	eor	@x[6],@x[6],@x[7]
b63792
+	 add	$xd1,$xd1,@K[3]
b63792
 	eor	@x[8],@x[8],@x[9]
b63792
-	 eor	$A0,$A0,$T0
b63792
+	 eor	$xa0,$xa0,$xt0
b63792
+	 movi	$xt0,#5
b63792
 	eor	@x[10],@x[10],@x[11]
b63792
-	 eor	$B0,$B0,$T1
b63792
+	 eor	$xb0,$xb0,$xt1
b63792
 	eor	@x[12],@x[12],@x[13]
b63792
-	 eor	$C0,$C0,$T2
b63792
+	 eor	$xc0,$xc0,$xt2
b63792
 	eor	@x[14],@x[14],@x[15]
b63792
-	 eor	$D0,$D0,$T3
b63792
-	 ld1.8	{$T0-$T3},[$inp],#64
b63792
+	 eor	$xd0,$xd0,$xt3
b63792
+	 add	$CTR,$CTR,$xt0			// += 5
b63792
+	 ld1.8	{$xt0-$xt3},[$inp],#64
b63792
 
b63792
 	stp	@x[0],@x[2],[$out,#0]		// store output
b63792
-	 add	@d[6],@d[6],#4			// increment counter
b63792
+	 add	@d[6],@d[6],#5			// increment counter
b63792
 	stp	@x[4],@x[6],[$out,#16]
b63792
-	 add	@K[3],@K[3],$ONE		// += 4
b63792
 	stp	@x[8],@x[10],[$out,#32]
b63792
-	 add	@K[4],@K[4],$ONE
b63792
 	stp	@x[12],@x[14],[$out,#48]
b63792
-	 add	@K[5],@K[5],$ONE
b63792
 	add	$out,$out,#64
b63792
 
b63792
-	st1.8	{$A0-$D0},[$out],#64
b63792
-	ld1.8	{$A0-$D0},[$inp],#64
b63792
-
b63792
-	eor	$A1,$A1,$T0
b63792
-	eor	$B1,$B1,$T1
b63792
-	eor	$C1,$C1,$T2
b63792
-	eor	$D1,$D1,$T3
b63792
-	st1.8	{$A1-$D1},[$out],#64
b63792
-
b63792
-	eor	$A2,$A2,$A0
b63792
-	eor	$B2,$B2,$B0
b63792
-	eor	$C2,$C2,$C0
b63792
-	eor	$D2,$D2,$D0
b63792
-	st1.8	{$A2-$D2},[$out],#64
b63792
+	st1.8	{$xa0-$xd0},[$out],#64
b63792
+	 add	$xa2,$xa2,@K[0]
b63792
+	 add	$xb2,$xb2,@K[1]
b63792
+	 add	$xc2,$xc2,@K[2]
b63792
+	 add	$xd2,$xd2,@K[3]
b63792
+	ld1.8	{$xa0-$xd0},[$inp],#64
b63792
+
b63792
+	eor	$xa1,$xa1,$xt0
b63792
+	eor	$xb1,$xb1,$xt1
b63792
+	eor	$xc1,$xc1,$xt2
b63792
+	eor	$xd1,$xd1,$xt3
b63792
+	st1.8	{$xa1-$xd1},[$out],#64
b63792
+	 add	$xa3,$xa3,@K[0]
b63792
+	 add	$xb3,$xb3,@K[1]
b63792
+	 add	$xc3,$xc3,@K[2]
b63792
+	 add	$xd3,$xd3,@K[3]
b63792
+	ld1.8	{$xa1-$xd1},[$inp],#64
b63792
+
b63792
+	eor	$xa2,$xa2,$xa0
b63792
+	eor	$xb2,$xb2,$xb0
b63792
+	eor	$xc2,$xc2,$xc0
b63792
+	eor	$xd2,$xd2,$xd0
b63792
+	st1.8	{$xa2-$xd2},[$out],#64
b63792
+
b63792
+	eor	$xa3,$xa3,$xa1
b63792
+	eor	$xb3,$xb3,$xb1
b63792
+	eor	$xc3,$xc3,$xc1
b63792
+	eor	$xd3,$xd3,$xd1
b63792
+	st1.8	{$xa3-$xd3},[$out],#64
b63792
 
b63792
 	b.hi	.Loop_outer_neon
b63792
 
b63792
+	ldp	d8,d9,[sp]			// meet ABI requirements
b63792
+
b63792
 	ldp	x19,x20,[x29,#16]
b63792
 	add	sp,sp,#64
b63792
 	ldp	x21,x22,[x29,#32]
0c50f5
@@ -583,8 +670,10 @@ $code.=<<___;
b63792
 	.inst	0xd50323bf			// autiasp
b63792
 	ret
b63792
 
b63792
+.align	4
b63792
 .Ltail_neon:
b63792
-	add	$len,$len,#256
b63792
+	add	$len,$len,#320
b63792
+	ldp	d8,d9,[sp]			// meet ABI requirements
b63792
 	cmp	$len,#64
b63792
 	b.lo	.Less_than_64
b63792
 
0c50f5
@@ -601,7 +690,7 @@ $code.=<<___;
b63792
 	add	@x[14],@x[14],@x[15],lsl#32
b63792
 	ldp	@x[13],@x[15],[$inp,#48]
b63792
 	add	$inp,$inp,#64
b63792
-#ifdef	__ARMEB__
b63792
+#ifdef	__AARCH64EB__
b63792
 	rev	@x[0],@x[0]
b63792
 	rev	@x[2],@x[2]
b63792
 	rev	@x[4],@x[4]
0c50f5
@@ -621,48 +710,68 @@ $code.=<<___;
b63792
 	eor	@x[14],@x[14],@x[15]
b63792
 
b63792
 	stp	@x[0],@x[2],[$out,#0]		// store output
b63792
-	 add	@d[6],@d[6],#4			// increment counter
b63792
+	 add	$xa0,$xa0,@K[0]			// accumulate key block
b63792
 	stp	@x[4],@x[6],[$out,#16]
b63792
+	 add	$xb0,$xb0,@K[1]
b63792
 	stp	@x[8],@x[10],[$out,#32]
b63792
+	 add	$xc0,$xc0,@K[2]
b63792
 	stp	@x[12],@x[14],[$out,#48]
b63792
+	 add	$xd0,$xd0,@K[3]
b63792
 	add	$out,$out,#64
b63792
 	b.eq	.Ldone_neon
b63792
 	sub	$len,$len,#64
b63792
 	cmp	$len,#64
b63792
-	b.lo	.Less_than_128
b63792
+	b.lo	.Last_neon
b63792
 
b63792
-	ld1.8	{$T0-$T3},[$inp],#64
b63792
-	eor	$A0,$A0,$T0
b63792
-	eor	$B0,$B0,$T1
b63792
-	eor	$C0,$C0,$T2
b63792
-	eor	$D0,$D0,$T3
b63792
-	st1.8	{$A0-$D0},[$out],#64
b63792
+	ld1.8	{$xt0-$xt3},[$inp],#64
b63792
+	eor	$xa0,$xa0,$xt0
b63792
+	eor	$xb0,$xb0,$xt1
b63792
+	eor	$xc0,$xc0,$xt2
b63792
+	eor	$xd0,$xd0,$xt3
b63792
+	st1.8	{$xa0-$xd0},[$out],#64
b63792
 	b.eq	.Ldone_neon
b63792
+
b63792
+	add	$xa0,$xa1,@K[0]
b63792
+	add	$xb0,$xb1,@K[1]
b63792
 	sub	$len,$len,#64
b63792
+	add	$xc0,$xc1,@K[2]
b63792
 	cmp	$len,#64
b63792
-	b.lo	.Less_than_192
b63792
+	add	$xd0,$xd1,@K[3]
b63792
+	b.lo	.Last_neon
b63792
 
b63792
-	ld1.8	{$T0-$T3},[$inp],#64
b63792
-	eor	$A1,$A1,$T0
b63792
-	eor	$B1,$B1,$T1
b63792
-	eor	$C1,$C1,$T2
b63792
-	eor	$D1,$D1,$T3
b63792
-	st1.8	{$A1-$D1},[$out],#64
b63792
+	ld1.8	{$xt0-$xt3},[$inp],#64
b63792
+	eor	$xa1,$xa0,$xt0
b63792
+	eor	$xb1,$xb0,$xt1
b63792
+	eor	$xc1,$xc0,$xt2
b63792
+	eor	$xd1,$xd0,$xt3
b63792
+	st1.8	{$xa1-$xd1},[$out],#64
b63792
 	b.eq	.Ldone_neon
b63792
+
b63792
+	add	$xa0,$xa2,@K[0]
b63792
+	add	$xb0,$xb2,@K[1]
b63792
 	sub	$len,$len,#64
b63792
+	add	$xc0,$xc2,@K[2]
b63792
+	cmp	$len,#64
b63792
+	add	$xd0,$xd2,@K[3]
b63792
+	b.lo	.Last_neon
b63792
 
b63792
-	st1.8	{$A2-$D2},[sp]
b63792
-	b	.Last_neon
b63792
+	ld1.8	{$xt0-$xt3},[$inp],#64
b63792
+	eor	$xa2,$xa0,$xt0
b63792
+	eor	$xb2,$xb0,$xt1
b63792
+	eor	$xc2,$xc0,$xt2
b63792
+	eor	$xd2,$xd0,$xt3
b63792
+	st1.8	{$xa2-$xd2},[$out],#64
b63792
+	b.eq	.Ldone_neon
b63792
 
b63792
-.Less_than_128:
b63792
-	st1.8	{$A0-$D0},[sp]
b63792
-	b	.Last_neon
b63792
-.Less_than_192:
b63792
-	st1.8	{$A1-$D1},[sp]
b63792
-	b	.Last_neon
b63792
+	add	$xa0,$xa3,@K[0]
b63792
+	add	$xb0,$xb3,@K[1]
b63792
+	add	$xc0,$xc3,@K[2]
b63792
+	add	$xd0,$xd3,@K[3]
b63792
+	sub	$len,$len,#64
b63792
 
b63792
-.align	4
b63792
 .Last_neon:
b63792
+	st1.8	{$xa0-$xd0},[sp]
b63792
+
b63792
 	sub	$out,$out,#1
b63792
 	add	$inp,$inp,$len
b63792
 	add	$out,$out,$len
0c50f5
@@ -695,9 +804,41 @@ $code.=<<___;
b63792
 .size	ChaCha20_neon,.-ChaCha20_neon
b63792
 ___
b63792
 {
b63792
+my @K = map("v$_.4s",(0..6));
b63792
 my ($T0,$T1,$T2,$T3,$T4,$T5)=@K;
b63792
 my ($A0,$B0,$C0,$D0,$A1,$B1,$C1,$D1,$A2,$B2,$C2,$D2,
b63792
-    $A3,$B3,$C3,$D3,$A4,$B4,$C4,$D4,$A5,$B5,$C5,$D5) = map("v$_.4s",(0..23));
b63792
+    $A3,$B3,$C3,$D3,$A4,$B4,$C4,$D4,$A5,$B5,$C5,$D5) = map("v$_.4s",(8..31));
b63792
+my $rot24 = @K[6];
b63792
+my $ONE = "v7.4s";
b63792
+
b63792
+sub NEONROUND {
b63792
+my $odd = pop;
b63792
+my ($a,$b,$c,$d,$t)=@_;
b63792
+
b63792
+	(
b63792
+	"&add		('$a','$a','$b')",
b63792
+	"&eor		('$d','$d','$a')",
b63792
+	"&rev32_16	('$d','$d')",		# vrot ($d,16)
b63792
+
b63792
+	"&add		('$c','$c','$d')",
b63792
+	"&eor		('$t','$b','$c')",
b63792
+	"&ushr		('$b','$t',20)",
b63792
+	"&sli		('$b','$t',12)",
b63792
+
b63792
+	"&add		('$a','$a','$b')",
b63792
+	"&eor		('$d','$d','$a')",
b63792
+	"&tbl		('$d','{$d}','$rot24')",
b63792
+
b63792
+	"&add		('$c','$c','$d')",
b63792
+	"&eor		('$t','$b','$c')",
b63792
+	"&ushr		('$b','$t',25)",
b63792
+	"&sli		('$b','$t',7)",
b63792
+
b63792
+	"&ext		('$c','$c','$c',8)",
b63792
+	"&ext		('$d','$d','$d',$odd?4:12)",
b63792
+	"&ext		('$b','$b','$b',$odd?12:4)"
b63792
+	);
b63792
+}
b63792
 
b63792
 $code.=<<___;
b63792
 .type	ChaCha20_512_neon,%function
0c50f5
@@ -717,6 +858,7 @@ ChaCha20_512_neon:
b63792
 .L512_or_more_neon:
b63792
 	sub	sp,sp,#128+64
b63792
 
b63792
+	eor	$ONE,$ONE,$ONE
b63792
 	ldp	@d[0],@d[1],[@x[0]]		// load sigma
b63792
 	ld1	{@K[0]},[@x[0]],#16
b63792
 	ldp	@d[2],@d[3],[$key]		// load key
0c50f5
@@ -724,8 +866,9 @@ ChaCha20_512_neon:
b63792
 	ld1	{@K[1],@K[2]},[$key]
b63792
 	ldp	@d[6],@d[7],[$ctr]		// load counter
b63792
 	ld1	{@K[3]},[$ctr]
b63792
-	ld1	{$ONE},[@x[0]]
b63792
-#ifdef	__ARMEB__
b63792
+	ld1	{$ONE}[0],[@x[0]]
b63792
+	add	$key,@x[0],#16			// .Lrot24
b63792
+#ifdef	__AARCH64EB__
b63792
 	rev64	@K[0],@K[0]
b63792
 	ror	@d[2],@d[2],#32
b63792
 	ror	@d[3],@d[3],#32
0c50f5
@@ -792,9 +935,10 @@ ChaCha20_512_neon:
b63792
 	 mov	$C4,@K[2]
b63792
 	 stp	@K[3],@K[4],[sp,#48]		// off-load key block, variable part
b63792
 	 mov	$C5,@K[2]
b63792
-	 str	@K[5],[sp,#80]
b63792
+	 stp	@K[5],@K[6],[sp,#80]
b63792
 
b63792
 	mov	$ctr,#5
b63792
+	ld1	{$rot24},[$key]
b63792
 	subs	$len,$len,#512
b63792
 .Loop_upper_neon:
b63792
 	sub	$ctr,$ctr,#1
0c50f5
@@ -867,7 +1011,7 @@ $code.=<<___;
b63792
 	add	@x[14],@x[14],@x[15],lsl#32
b63792
 	ldp	@x[13],@x[15],[$inp,#48]
b63792
 	add	$inp,$inp,#64
b63792
-#ifdef	__ARMEB__
b63792
+#ifdef	__AARCH64EB__
b63792
 	rev	@x[0],@x[0]
b63792
 	rev	@x[2],@x[2]
b63792
 	rev	@x[4],@x[4]
0c50f5
@@ -956,6 +1100,7 @@ $code.=<<___;
b63792
 	add.32	@x[2],@x[2],@d[1]
b63792
 	 ldp	@K[4],@K[5],[sp,#64]
b63792
 	add	@x[3],@x[3],@d[1],lsr#32
b63792
+	 ldr	@K[6],[sp,#96]
b63792
 	 add	$A0,$A0,@K[0]
b63792
 	add.32	@x[4],@x[4],@d[2]
b63792
 	 add	$A1,$A1,@K[0]
0c50f5
@@ -1008,7 +1153,7 @@ $code.=<<___;
b63792
 	add	$inp,$inp,#64
b63792
 	 add	$B5,$B5,@K[1]
b63792
 
b63792
-#ifdef	__ARMEB__
b63792
+#ifdef	__AARCH64EB__
b63792
 	rev	@x[0],@x[0]
b63792
 	rev	@x[2],@x[2]
b63792
 	rev	@x[4],@x[4]
0c50f5
@@ -1086,26 +1231,26 @@ $code.=<<___;
b63792
 	b.hs	.Loop_outer_512_neon
b63792
 
b63792
 	adds	$len,$len,#512
b63792
-	ushr	$A0,$ONE,#2			// 4 -> 1
b63792
+	ushr	$ONE,$ONE,#1			// 4 -> 2
b63792
 
b63792
-	ldp	d8,d9,[sp,#128+0]		// meet ABI requirements
b63792
 	ldp	d10,d11,[sp,#128+16]
b63792
 	ldp	d12,d13,[sp,#128+32]
b63792
 	ldp	d14,d15,[sp,#128+48]
b63792
 
b63792
-	stp	@K[0],$ONE,[sp,#0]		// wipe off-load area
b63792
-	stp	@K[0],$ONE,[sp,#32]
b63792
-	stp	@K[0],$ONE,[sp,#64]
b63792
+	stp	@K[0],@K[0],[sp,#0]		// wipe off-load area
b63792
+	stp	@K[0],@K[0],[sp,#32]
b63792
+	stp	@K[0],@K[0],[sp,#64]
b63792
 
b63792
 	b.eq	.Ldone_512_neon
b63792
 
b63792
+	sub	$key,$key,#16			// .Lone
b63792
 	cmp	$len,#192
b63792
-	sub	@K[3],@K[3],$A0			// -= 1
b63792
-	sub	@K[4],@K[4],$A0
b63792
-	sub	@K[5],@K[5],$A0
b63792
 	add	sp,sp,#128
b63792
+	sub	@K[3],@K[3],$ONE		// -= 2
b63792
+	ld1	{$CTR,$ROT24},[$key]
b63792
 	b.hs	.Loop_outer_neon
b63792
 
b63792
+	ldp	d8,d9,[sp,#0]			// meet ABI requirements
b63792
 	eor	@K[1],@K[1],@K[1]
b63792
 	eor	@K[2],@K[2],@K[2]
b63792
 	eor	@K[3],@K[3],@K[3]
0c50f5
@@ -1115,6 +1260,7 @@ $code.=<<___;
b63792
 	b	.Loop_outer
b63792
 
b63792
 .Ldone_512_neon:
b63792
+	ldp	d8,d9,[sp,#128+0]		// meet ABI requirements
b63792
 	ldp	x19,x20,[x29,#16]
b63792
 	add	sp,sp,#128+64
b63792
 	ldp	x21,x22,[x29,#32]
0c50f5
@@ -1133,9 +1279,11 @@ foreach (split("\n",$code)) {
b63792
 	s/\`([^\`]*)\`/eval $1/geo;
b63792
 
b63792
 	(s/\b([a-z]+)\.32\b/$1/ and (s/x([0-9]+)/w$1/g or 1))	or
b63792
-	(m/\b(eor|ext|mov)\b/ and (s/\.4s/\.16b/g or 1))	or
b63792
+	(m/\b(eor|ext|mov|tbl)\b/ and (s/\.4s/\.16b/g or 1))	or
b63792
 	(s/\b((?:ld|st)1)\.8\b/$1/ and (s/\.4s/\.16b/g or 1))	or
b63792
 	(m/\b(ld|st)[rp]\b/ and (s/v([0-9]+)\.4s/q$1/g or 1))	or
b63792
+	(m/\b(dup|ld1)\b/ and (s/\.4(s}?\[[0-3]\])/.$1/g or 1))	or
b63792
+	(s/\b(zip[12])\.64\b/$1/ and (s/\.4s/\.2d/g or 1))	or
b63792
 	(s/\brev32\.16\b/rev32/ and (s/\.4s/\.8h/g or 1));
b63792
 
b63792
 	#s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/geo;
0c50f5
diff -up openssl-1.1.1i/crypto/modes/asm/ghashv8-armx.pl.arm-update openssl-1.1.1i/crypto/modes/asm/ghashv8-armx.pl
0c50f5
--- openssl-1.1.1i/crypto/modes/asm/ghashv8-armx.pl.arm-update	2020-12-08 14:20:59.000000000 +0100
0c50f5
+++ openssl-1.1.1i/crypto/modes/asm/ghashv8-armx.pl	2020-12-09 10:37:38.408558954 +0100
b63792
@@ -42,6 +42,7 @@
b63792
 # Denver	0.51		0.65		6.02
b63792
 # Mongoose	0.65		1.10		8.06
b63792
 # Kryo		0.76		1.16		8.00
b63792
+# ThunderX2	1.05
b63792
 #
b63792
 # (*)	presented for reference/comparison purposes;
b63792
 
0c50f5
diff -up openssl-1.1.1i/crypto/poly1305/asm/poly1305-armv8.pl.arm-update openssl-1.1.1i/crypto/poly1305/asm/poly1305-armv8.pl
0c50f5
--- openssl-1.1.1i/crypto/poly1305/asm/poly1305-armv8.pl.arm-update	2020-12-08 14:20:59.000000000 +0100
0c50f5
+++ openssl-1.1.1i/crypto/poly1305/asm/poly1305-armv8.pl	2020-12-09 10:37:38.408558954 +0100
b63792
@@ -29,6 +29,7 @@
b63792
 # X-Gene	2.13/+68%	2.27
b63792
 # Mongoose	1.77/+75%	1.12
b63792
 # Kryo		2.70/+55%	1.13
b63792
+# ThunderX2	1.17/+95%	1.36
b63792
 #
b63792
 # (*)	estimate based on resources availability is less than 1.0,
b63792
 #	i.e. measured result is worse than expected, presumably binary
0c50f5
diff -up openssl-1.1.1i/crypto/sha/asm/keccak1600-armv8.pl.arm-update openssl-1.1.1i/crypto/sha/asm/keccak1600-armv8.pl
0c50f5
--- openssl-1.1.1i/crypto/sha/asm/keccak1600-armv8.pl.arm-update	2020-12-08 14:20:59.000000000 +0100
0c50f5
+++ openssl-1.1.1i/crypto/sha/asm/keccak1600-armv8.pl	2020-12-09 10:37:38.408558954 +0100
b63792
@@ -51,6 +51,7 @@
b63792
 # Kryo		12
b63792
 # Denver	7.8
b63792
 # Apple A7	7.2
b63792
+# ThunderX2	9.7
b63792
 #
b63792
 # (*)	Corresponds to SHA3-256. No improvement coefficients are listed
b63792
 #	because they vary too much from compiler to compiler. Newer
0c50f5
diff -up openssl-1.1.1i/crypto/sha/asm/sha1-armv8.pl.arm-update openssl-1.1.1i/crypto/sha/asm/sha1-armv8.pl
0c50f5
--- openssl-1.1.1i/crypto/sha/asm/sha1-armv8.pl.arm-update	2020-12-08 14:20:59.000000000 +0100
0c50f5
+++ openssl-1.1.1i/crypto/sha/asm/sha1-armv8.pl	2020-12-09 10:37:38.408558954 +0100
b63792
@@ -27,6 +27,7 @@
b63792
 # X-Gene				8.80 (+200%)
b63792
 # Mongoose	2.05			6.50 (+160%)
b63792
 # Kryo		1.88			8.00 (+90%)
b63792
+# ThunderX2	2.64			6.36 (+150%)
b63792
 #
b63792
 # (*)	Software results are presented mostly for reference purposes.
b63792
 # (**)	Keep in mind that Denver relies on binary translation, which
0c50f5
diff -up openssl-1.1.1i/crypto/sha/asm/sha512-armv8.pl.arm-update openssl-1.1.1i/crypto/sha/asm/sha512-armv8.pl
0c50f5
--- openssl-1.1.1i/crypto/sha/asm/sha512-armv8.pl.arm-update	2020-12-08 14:20:59.000000000 +0100
0c50f5
+++ openssl-1.1.1i/crypto/sha/asm/sha512-armv8.pl	2020-12-09 10:37:38.408558954 +0100
b63792
@@ -28,6 +28,7 @@
b63792
 # X-Gene			20.0 (+100%)	12.8 (+300%(***))
b63792
 # Mongoose	2.36		13.0 (+50%)	8.36 (+33%)
b63792
 # Kryo		1.92		17.4 (+30%)	11.2 (+8%)
b63792
+# ThunderX2	2.54		13.2 (+40%)	8.40 (+18%)
b63792
 #
b63792
 # (*)	Software SHA256 results are of lesser relevance, presented
b63792
 #	mostly for informational purposes.