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

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