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

067bfb
diff -up openssl-1.1.1c/crypto/aes/asm/aesv8-armx.pl.arm-update openssl-1.1.1c/crypto/aes/asm/aesv8-armx.pl
067bfb
--- openssl-1.1.1c/crypto/aes/asm/aesv8-armx.pl.arm-update	2019-05-28 15:12:21.000000000 +0200
067bfb
+++ openssl-1.1.1c/crypto/aes/asm/aesv8-armx.pl	2019-11-20 11:36:22.389506155 +0100
067bfb
@@ -27,44 +27,72 @@
067bfb
 # CBC encrypt case. On Cortex-A57 parallelizable mode performance
067bfb
 # seems to be limited by sheer amount of NEON instructions...
067bfb
 #
067bfb
+# April 2019
067bfb
+#
067bfb
+# Key to performance of parallelize-able modes is round instruction
067bfb
+# interleaving. But which factor to use? There is optimal one for
067bfb
+# each combination of instruction latency and issue rate, beyond
067bfb
+# which increasing interleave factor doesn't pay off. While on cons
067bfb
+# side we have code size increase and resource waste on platforms for
067bfb
+# which interleave factor is too high. In other words you want it to
067bfb
+# be just right. So far interleave factor of 3x was serving well all
067bfb
+# platforms. But for ThunderX2 optimal interleave factor was measured
067bfb
+# to be 5x...
067bfb
+#
067bfb
 # Performance in cycles per byte processed with 128-bit key:
067bfb
 #
067bfb
 #		CBC enc		CBC dec		CTR
067bfb
 # Apple A7	2.39		1.20		1.20
067bfb
-# Cortex-A53	1.32		1.29		1.46
067bfb
-# Cortex-A57(*)	1.95		0.85		0.93
067bfb
-# Denver	1.96		0.86		0.80
067bfb
-# Mongoose	1.33		1.20		1.20
067bfb
-# Kryo		1.26		0.94		1.00
067bfb
+# Cortex-A53	1.32		1.17/1.29(**)	1.36/1.46
067bfb
+# Cortex-A57(*)	1.95		0.82/0.85	0.89/0.93
067bfb
+# Cortex-A72	1.33		0.85/0.88	0.92/0.96
067bfb
+# Denver	1.96		0.65/0.86	0.76/0.80
067bfb
+# Mongoose	1.33		1.23/1.20	1.30/1.20
067bfb
+# Kryo		1.26		0.87/0.94	1.00/1.00
067bfb
+# ThunderX2	5.95		1.25		1.30
067bfb
 #
067bfb
 # (*)	original 3.64/1.34/1.32 results were for r0p0 revision
067bfb
 #	and are still same even for updated module;
067bfb
+# (**)	numbers after slash are for 32-bit code, which is 3x-
067bfb
+#	interleaved;
067bfb
 
067bfb
-$flavour = shift;
067bfb
-$output  = shift;
067bfb
+# $output is the last argument if it looks like a file (it has an extension)
067bfb
+# $flavour is the first argument if it doesn't look like a file
067bfb
+$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
067bfb
+$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
067bfb
 
067bfb
 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
067bfb
 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
067bfb
 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
067bfb
 die "can't locate arm-xlate.pl";
067bfb
 
067bfb
-open OUT,"| \"$^X\" $xlate $flavour $output";
067bfb
+open OUT,"| \"$^X\" $xlate $flavour \"$output\""
067bfb
+    or die "can't call $xlate: $!";
067bfb
 *STDOUT=*OUT;
067bfb
 
067bfb
 $prefix="aes_v8";
067bfb
 
067bfb
+$_byte = ($flavour =~ /win/ ? "DCB" : ".byte");
067bfb
+
067bfb
 $code=<<___;
067bfb
 #include "arm_arch.h"
067bfb
 
067bfb
 #if __ARM_MAX_ARCH__>=7
067bfb
-.text
067bfb
 ___
067bfb
-$code.=".arch	armv8-a+crypto\n"			if ($flavour =~ /64/);
067bfb
+$code.=".arch	armv8-a+crypto\n.text\n"		if ($flavour =~ /64/);
067bfb
 $code.=<<___						if ($flavour !~ /64/);
067bfb
 .arch	armv7-a	// don't confuse not-so-latest binutils with argv8 :-)
067bfb
 .fpu	neon
067bfb
+#ifdef	__thumb2__
067bfb
+.syntax	unified
067bfb
+.thumb
067bfb
+# define INST(a,b,c,d)	$_byte	c,d|0xc,a,b
067bfb
+#else
067bfb
 .code	32
067bfb
-#undef	__thumb2__
067bfb
+# define INST(a,b,c,d)	$_byte	a,b,c,d
067bfb
+#endif
067bfb
+
067bfb
+.text
067bfb
 ___
067bfb
 
067bfb
 # Assembler mnemonics are an eclectic mix of 32- and 64-bit syntax,
067bfb
@@ -514,6 +542,13 @@ $code.=<<___;
067bfb
 ___
067bfb
 {
067bfb
 my ($dat2,$in2,$tmp2)=map("q$_",(10,11,9));
067bfb
+
067bfb
+my ($dat3,$in3,$tmp3);	# used only in 64-bit mode
067bfb
+my ($dat4,$in4,$tmp4);
067bfb
+if ($flavour =~ /64/) {
067bfb
+    ($dat2,$dat3,$dat4,$in2,$in3,$in4,$tmp3,$tmp4)=map("q$_",(16..23));
067bfb
+}
067bfb
+
067bfb
 $code.=<<___;
067bfb
 .align	5
067bfb
 .Lcbc_dec:
067bfb
@@ -530,7 +565,196 @@ $code.=<<___;
067bfb
 	vorr	$in0,$dat,$dat
067bfb
 	vorr	$in1,$dat1,$dat1
067bfb
 	vorr	$in2,$dat2,$dat2
067bfb
+___
067bfb
+$code.=<<___	if ($flavour =~ /64/);
067bfb
+	cmp	$len,#32
067bfb
+	b.lo	.Loop3x_cbc_dec
067bfb
+
067bfb
+	vld1.8	{$dat3},[$inp],#16
067bfb
+	vld1.8	{$dat4},[$inp],#16
067bfb
+	sub	$len,$len,#32		// bias
067bfb
+	mov	$cnt,$rounds
067bfb
+	vorr	$in3,$dat3,$dat3
067bfb
+	vorr	$in4,$dat4,$dat4
067bfb
+
067bfb
+.Loop5x_cbc_dec:
067bfb
+	aesd	$dat0,q8
067bfb
+	aesimc	$dat0,$dat0
067bfb
+	aesd	$dat1,q8
067bfb
+	aesimc	$dat1,$dat1
067bfb
+	aesd	$dat2,q8
067bfb
+	aesimc	$dat2,$dat2
067bfb
+	aesd	$dat3,q8
067bfb
+	aesimc	$dat3,$dat3
067bfb
+	aesd	$dat4,q8
067bfb
+	aesimc	$dat4,$dat4
067bfb
+	vld1.32	{q8},[$key_],#16
067bfb
+	subs	$cnt,$cnt,#2
067bfb
+	aesd	$dat0,q9
067bfb
+	aesimc	$dat0,$dat0
067bfb
+	aesd	$dat1,q9
067bfb
+	aesimc	$dat1,$dat1
067bfb
+	aesd	$dat2,q9
067bfb
+	aesimc	$dat2,$dat2
067bfb
+	aesd	$dat3,q9
067bfb
+	aesimc	$dat3,$dat3
067bfb
+	aesd	$dat4,q9
067bfb
+	aesimc	$dat4,$dat4
067bfb
+	vld1.32	{q9},[$key_],#16
067bfb
+	b.gt	.Loop5x_cbc_dec
067bfb
+
067bfb
+	aesd	$dat0,q8
067bfb
+	aesimc	$dat0,$dat0
067bfb
+	aesd	$dat1,q8
067bfb
+	aesimc	$dat1,$dat1
067bfb
+	aesd	$dat2,q8
067bfb
+	aesimc	$dat2,$dat2
067bfb
+	aesd	$dat3,q8
067bfb
+	aesimc	$dat3,$dat3
067bfb
+	aesd	$dat4,q8
067bfb
+	aesimc	$dat4,$dat4
067bfb
+	 cmp	$len,#0x40		// because .Lcbc_tail4x
067bfb
+	 sub	$len,$len,#0x50
067bfb
+
067bfb
+	aesd	$dat0,q9
067bfb
+	aesimc	$dat0,$dat0
067bfb
+	aesd	$dat1,q9
067bfb
+	aesimc	$dat1,$dat1
067bfb
+	aesd	$dat2,q9
067bfb
+	aesimc	$dat2,$dat2
067bfb
+	aesd	$dat3,q9
067bfb
+	aesimc	$dat3,$dat3
067bfb
+	aesd	$dat4,q9
067bfb
+	aesimc	$dat4,$dat4
067bfb
+	 csel	x6,xzr,$len,gt		// borrow x6, $cnt, "gt" is not typo
067bfb
+	 mov	$key_,$key
067bfb
+
067bfb
+	aesd	$dat0,q10
067bfb
+	aesimc	$dat0,$dat0
067bfb
+	aesd	$dat1,q10
067bfb
+	aesimc	$dat1,$dat1
067bfb
+	aesd	$dat2,q10
067bfb
+	aesimc	$dat2,$dat2
067bfb
+	aesd	$dat3,q10
067bfb
+	aesimc	$dat3,$dat3
067bfb
+	aesd	$dat4,q10
067bfb
+	aesimc	$dat4,$dat4
067bfb
+	 add	$inp,$inp,x6		// $inp is adjusted in such way that
067bfb
+					// at exit from the loop $dat1-$dat4
067bfb
+					// are loaded with last "words"
067bfb
+	 add	x6,$len,#0x60		// because .Lcbc_tail4x
067bfb
+
067bfb
+	aesd	$dat0,q11
067bfb
+	aesimc	$dat0,$dat0
067bfb
+	aesd	$dat1,q11
067bfb
+	aesimc	$dat1,$dat1
067bfb
+	aesd	$dat2,q11
067bfb
+	aesimc	$dat2,$dat2
067bfb
+	aesd	$dat3,q11
067bfb
+	aesimc	$dat3,$dat3
067bfb
+	aesd	$dat4,q11
067bfb
+	aesimc	$dat4,$dat4
067bfb
+
067bfb
+	aesd	$dat0,q12
067bfb
+	aesimc	$dat0,$dat0
067bfb
+	aesd	$dat1,q12
067bfb
+	aesimc	$dat1,$dat1
067bfb
+	aesd	$dat2,q12
067bfb
+	aesimc	$dat2,$dat2
067bfb
+	aesd	$dat3,q12
067bfb
+	aesimc	$dat3,$dat3
067bfb
+	aesd	$dat4,q12
067bfb
+	aesimc	$dat4,$dat4
067bfb
+
067bfb
+	aesd	$dat0,q13
067bfb
+	aesimc	$dat0,$dat0
067bfb
+	aesd	$dat1,q13
067bfb
+	aesimc	$dat1,$dat1
067bfb
+	aesd	$dat2,q13
067bfb
+	aesimc	$dat2,$dat2
067bfb
+	aesd	$dat3,q13
067bfb
+	aesimc	$dat3,$dat3
067bfb
+	aesd	$dat4,q13
067bfb
+	aesimc	$dat4,$dat4
067bfb
+
067bfb
+	aesd	$dat0,q14
067bfb
+	aesimc	$dat0,$dat0
067bfb
+	aesd	$dat1,q14
067bfb
+	aesimc	$dat1,$dat1
067bfb
+	aesd	$dat2,q14
067bfb
+	aesimc	$dat2,$dat2
067bfb
+	aesd	$dat3,q14
067bfb
+	aesimc	$dat3,$dat3
067bfb
+	aesd	$dat4,q14
067bfb
+	aesimc	$dat4,$dat4
067bfb
+
067bfb
+	 veor	$tmp0,$ivec,$rndlast
067bfb
+	aesd	$dat0,q15
067bfb
+	 veor	$tmp1,$in0,$rndlast
067bfb
+	 vld1.8	{$in0},[$inp],#16
067bfb
+	aesd	$dat1,q15
067bfb
+	 veor	$tmp2,$in1,$rndlast
067bfb
+	 vld1.8	{$in1},[$inp],#16
067bfb
+	aesd	$dat2,q15
067bfb
+	 veor	$tmp3,$in2,$rndlast
067bfb
+	 vld1.8	{$in2},[$inp],#16
067bfb
+	aesd	$dat3,q15
067bfb
+	 veor	$tmp4,$in3,$rndlast
067bfb
+	 vld1.8	{$in3},[$inp],#16
067bfb
+	aesd	$dat4,q15
067bfb
+	 vorr	$ivec,$in4,$in4
067bfb
+	 vld1.8	{$in4},[$inp],#16
067bfb
+	cbz	x6,.Lcbc_tail4x
067bfb
+	 vld1.32 {q8},[$key_],#16	// re-pre-load rndkey[0]
067bfb
+	veor	$tmp0,$tmp0,$dat0
067bfb
+	 vorr	$dat0,$in0,$in0
067bfb
+	veor	$tmp1,$tmp1,$dat1
067bfb
+	 vorr	$dat1,$in1,$in1
067bfb
+	veor	$tmp2,$tmp2,$dat2
067bfb
+	 vorr	$dat2,$in2,$in2
067bfb
+	veor	$tmp3,$tmp3,$dat3
067bfb
+	 vorr	$dat3,$in3,$in3
067bfb
+	veor	$tmp4,$tmp4,$dat4
067bfb
+	vst1.8	{$tmp0},[$out],#16
067bfb
+	 vorr	$dat4,$in4,$in4
067bfb
+	vst1.8	{$tmp1},[$out],#16
067bfb
+	 mov	$cnt,$rounds
067bfb
+	vst1.8	{$tmp2},[$out],#16
067bfb
+	 vld1.32 {q9},[$key_],#16	// re-pre-load rndkey[1]
067bfb
+	vst1.8	{$tmp3},[$out],#16
067bfb
+	vst1.8	{$tmp4},[$out],#16
067bfb
+	b.hs	.Loop5x_cbc_dec
067bfb
+
067bfb
+	add	$len,$len,#0x50
067bfb
+	cbz	$len,.Lcbc_done
067bfb
+
067bfb
+	add	$cnt,$rounds,#2
067bfb
+	subs	$len,$len,#0x30
067bfb
+	vorr	$dat0,$in2,$in2
067bfb
+	vorr	$in0,$in2,$in2
067bfb
+	vorr	$dat1,$in3,$in3
067bfb
+	vorr	$in1,$in3,$in3
067bfb
+	vorr	$dat2,$in4,$in4
067bfb
+	vorr	$in2,$in4,$in4
067bfb
+	b.lo	.Lcbc_dec_tail
067bfb
+
067bfb
+	b	.Loop3x_cbc_dec
067bfb
 
067bfb
+.align	4
067bfb
+.Lcbc_tail4x:
067bfb
+	veor	$tmp1,$tmp0,$dat1
067bfb
+	veor	$tmp2,$tmp2,$dat2
067bfb
+	veor	$tmp3,$tmp3,$dat3
067bfb
+	veor	$tmp4,$tmp4,$dat4
067bfb
+	vst1.8	{$tmp1},[$out],#16
067bfb
+	vst1.8	{$tmp2},[$out],#16
067bfb
+	vst1.8	{$tmp3},[$out],#16
067bfb
+	vst1.8	{$tmp4},[$out],#16
067bfb
+
067bfb
+	b	.Lcbc_done
067bfb
+.align	4
067bfb
+___
067bfb
+$code.=<<___;
067bfb
 .Loop3x_cbc_dec:
067bfb
 	aesd	$dat0,q8
067bfb
 	aesimc	$dat0,$dat0
067bfb
@@ -691,6 +915,9 @@ my $step="x12";		# aliases with $tctr2
067bfb
 my ($dat0,$dat1,$in0,$in1,$tmp0,$tmp1,$ivec,$rndlast)=map("q$_",(0..7));
067bfb
 my ($dat2,$in2,$tmp2)=map("q$_",(10,11,9));
067bfb
 
067bfb
+# used only in 64-bit mode...
067bfb
+my ($dat3,$dat4,$in3,$in4)=map("q$_",(16..23));
067bfb
+
067bfb
 my ($dat,$tmp)=($dat0,$tmp0);
067bfb
 
067bfb
 ### q8-q15	preloaded key schedule
067bfb
@@ -743,6 +970,175 @@ $code.=<<___;
067bfb
 	rev		$tctr2, $ctr
067bfb
 	sub		$len,$len,#3		// bias
067bfb
 	vmov.32		${dat2}[3],$tctr2
067bfb
+___
067bfb
+$code.=<<___	if ($flavour =~ /64/);
067bfb
+	cmp		$len,#2
067bfb
+	b.lo		.Loop3x_ctr32
067bfb
+
067bfb
+	add		w13,$ctr,#1
067bfb
+	add		w14,$ctr,#2
067bfb
+	vorr		$dat3,$dat0,$dat0
067bfb
+	rev		w13,w13
067bfb
+	vorr		$dat4,$dat0,$dat0
067bfb
+	rev		w14,w14
067bfb
+	vmov.32		${dat3}[3],w13
067bfb
+	sub		$len,$len,#2		// bias
067bfb
+	vmov.32		${dat4}[3],w14
067bfb
+	add		$ctr,$ctr,#2
067bfb
+	b		.Loop5x_ctr32
067bfb
+
067bfb
+.align	4
067bfb
+.Loop5x_ctr32:
067bfb
+	aese		$dat0,q8
067bfb
+	aesmc		$dat0,$dat0
067bfb
+	aese		$dat1,q8
067bfb
+	aesmc		$dat1,$dat1
067bfb
+	aese		$dat2,q8
067bfb
+	aesmc		$dat2,$dat2
067bfb
+	aese		$dat3,q8
067bfb
+	aesmc		$dat3,$dat3
067bfb
+	aese		$dat4,q8
067bfb
+	aesmc		$dat4,$dat4
067bfb
+	vld1.32		{q8},[$key_],#16
067bfb
+	subs		$cnt,$cnt,#2
067bfb
+	aese		$dat0,q9
067bfb
+	aesmc		$dat0,$dat0
067bfb
+	aese		$dat1,q9
067bfb
+	aesmc		$dat1,$dat1
067bfb
+	aese		$dat2,q9
067bfb
+	aesmc		$dat2,$dat2
067bfb
+	aese		$dat3,q9
067bfb
+	aesmc		$dat3,$dat3
067bfb
+	aese		$dat4,q9
067bfb
+	aesmc		$dat4,$dat4
067bfb
+	vld1.32		{q9},[$key_],#16
067bfb
+	b.gt		.Loop5x_ctr32
067bfb
+
067bfb
+	mov		$key_,$key
067bfb
+	aese		$dat0,q8
067bfb
+	aesmc		$dat0,$dat0
067bfb
+	aese		$dat1,q8
067bfb
+	aesmc		$dat1,$dat1
067bfb
+	aese		$dat2,q8
067bfb
+	aesmc		$dat2,$dat2
067bfb
+	aese		$dat3,q8
067bfb
+	aesmc		$dat3,$dat3
067bfb
+	aese		$dat4,q8
067bfb
+	aesmc		$dat4,$dat4
067bfb
+	vld1.32	 	{q8},[$key_],#16	// re-pre-load rndkey[0]
067bfb
+
067bfb
+	aese		$dat0,q9
067bfb
+	aesmc		$dat0,$dat0
067bfb
+	aese		$dat1,q9
067bfb
+	aesmc		$dat1,$dat1
067bfb
+	aese		$dat2,q9
067bfb
+	aesmc		$dat2,$dat2
067bfb
+	aese		$dat3,q9
067bfb
+	aesmc		$dat3,$dat3
067bfb
+	aese		$dat4,q9
067bfb
+	aesmc		$dat4,$dat4
067bfb
+	vld1.32	 	{q9},[$key_],#16	// re-pre-load rndkey[1]
067bfb
+
067bfb
+	aese		$dat0,q12
067bfb
+	aesmc		$dat0,$dat0
067bfb
+	 add		$tctr0,$ctr,#1
067bfb
+	 add		$tctr1,$ctr,#2
067bfb
+	aese		$dat1,q12
067bfb
+	aesmc		$dat1,$dat1
067bfb
+	 add		$tctr2,$ctr,#3
067bfb
+	 add		w13,$ctr,#4
067bfb
+	aese		$dat2,q12
067bfb
+	aesmc		$dat2,$dat2
067bfb
+	 add		w14,$ctr,#5
067bfb
+	 rev		$tctr0,$tctr0
067bfb
+	aese		$dat3,q12
067bfb
+	aesmc		$dat3,$dat3
067bfb
+	 rev		$tctr1,$tctr1
067bfb
+	 rev		$tctr2,$tctr2
067bfb
+	aese		$dat4,q12
067bfb
+	aesmc		$dat4,$dat4
067bfb
+	 rev		w13,w13
067bfb
+	 rev		w14,w14
067bfb
+
067bfb
+	aese		$dat0,q13
067bfb
+	aesmc		$dat0,$dat0
067bfb
+	aese		$dat1,q13
067bfb
+	aesmc		$dat1,$dat1
067bfb
+	aese		$dat2,q13
067bfb
+	aesmc		$dat2,$dat2
067bfb
+	aese		$dat3,q13
067bfb
+	aesmc		$dat3,$dat3
067bfb
+	aese		$dat4,q13
067bfb
+	aesmc		$dat4,$dat4
067bfb
+
067bfb
+	aese		$dat0,q14
067bfb
+	aesmc		$dat0,$dat0
067bfb
+	 vld1.8		{$in0},[$inp],#16
067bfb
+	aese		$dat1,q14
067bfb
+	aesmc		$dat1,$dat1
067bfb
+	 vld1.8		{$in1},[$inp],#16
067bfb
+	aese		$dat2,q14
067bfb
+	aesmc		$dat2,$dat2
067bfb
+	 vld1.8		{$in2},[$inp],#16
067bfb
+	aese		$dat3,q14
067bfb
+	aesmc		$dat3,$dat3
067bfb
+	 vld1.8		{$in3},[$inp],#16
067bfb
+	aese		$dat4,q14
067bfb
+	aesmc		$dat4,$dat4
067bfb
+	 vld1.8		{$in4},[$inp],#16
067bfb
+
067bfb
+	aese		$dat0,q15
067bfb
+	 veor		$in0,$in0,$rndlast
067bfb
+	aese		$dat1,q15
067bfb
+	 veor		$in1,$in1,$rndlast
067bfb
+	aese		$dat2,q15
067bfb
+	 veor		$in2,$in2,$rndlast
067bfb
+	aese		$dat3,q15
067bfb
+	 veor		$in3,$in3,$rndlast
067bfb
+	aese		$dat4,q15
067bfb
+	 veor		$in4,$in4,$rndlast
067bfb
+
067bfb
+	veor		$in0,$in0,$dat0
067bfb
+	 vorr		$dat0,$ivec,$ivec
067bfb
+	veor		$in1,$in1,$dat1
067bfb
+	 vorr		$dat1,$ivec,$ivec
067bfb
+	veor		$in2,$in2,$dat2
067bfb
+	 vorr		$dat2,$ivec,$ivec
067bfb
+	veor		$in3,$in3,$dat3
067bfb
+	 vorr		$dat3,$ivec,$ivec
067bfb
+	veor		$in4,$in4,$dat4
067bfb
+	 vorr		$dat4,$ivec,$ivec
067bfb
+
067bfb
+	vst1.8		{$in0},[$out],#16
067bfb
+	 vmov.32	${dat0}[3],$tctr0
067bfb
+	vst1.8		{$in1},[$out],#16
067bfb
+	 vmov.32	${dat1}[3],$tctr1
067bfb
+	vst1.8		{$in2},[$out],#16
067bfb
+	 vmov.32	${dat2}[3],$tctr2
067bfb
+	vst1.8		{$in3},[$out],#16
067bfb
+	 vmov.32	${dat3}[3],w13
067bfb
+	vst1.8		{$in4},[$out],#16
067bfb
+	 vmov.32	${dat4}[3],w14
067bfb
+
067bfb
+	mov		$cnt,$rounds
067bfb
+	cbz		$len,.Lctr32_done
067bfb
+
067bfb
+	add		$ctr,$ctr,#5
067bfb
+	subs		$len,$len,#5
067bfb
+	b.hs		.Loop5x_ctr32
067bfb
+
067bfb
+	add		$len,$len,#5
067bfb
+	sub		$ctr,$ctr,#5
067bfb
+
067bfb
+	cmp		$len,#2
067bfb
+	mov		$step,#16
067bfb
+	cclr		$step,lo
067bfb
+	b.ls		.Lctr32_tail
067bfb
+
067bfb
+	sub		$len,$len,#3		// bias
067bfb
+	add		$ctr,$ctr,#3
067bfb
+___
067bfb
+$code.=<<___;
067bfb
 	b		.Loop3x_ctr32
067bfb
 
067bfb
 .align	4
067bfb
@@ -955,7 +1351,7 @@ if ($flavour =~ /64/) {			######## 64-bi
067bfb
 	    # since ARMv7 instructions are always encoded little-endian.
067bfb
 	    # correct solution is to use .inst directive, but older
067bfb
 	    # assemblers don't implement it:-(
067bfb
-	    sprintf ".byte\t0x%02x,0x%02x,0x%02x,0x%02x\t@ %s %s",
067bfb
+	    sprintf "INST(0x%02x,0x%02x,0x%02x,0x%02x)\t@ %s %s",
067bfb
 			$word&0xff,($word>>8)&0xff,
067bfb
 			($word>>16)&0xff,($word>>24)&0xff,
067bfb
 			$mnemonic,$arg;
067bfb
@@ -996,14 +1392,17 @@ if ($flavour =~ /64/) {			######## 64-bi
067bfb
 	s/\],#[0-9]+/]!/o;
067bfb
 
067bfb
 	s/[v]?(aes\w+)\s+([qv].*)/unaes($1,$2)/geo	or
067bfb
-	s/cclr\s+([^,]+),\s*([a-z]+)/mov$2	$1,#0/o	or
067bfb
+	s/cclr\s+([^,]+),\s*([a-z]+)/mov.$2	$1,#0/o	or
067bfb
 	s/vtbl\.8\s+(.*)/unvtbl($1)/geo			or
067bfb
 	s/vdup\.32\s+(.*)/unvdup32($1)/geo		or
067bfb
 	s/vmov\.32\s+(.*)/unvmov32($1)/geo		or
067bfb
 	s/^(\s+)b\./$1b/o				or
067bfb
-	s/^(\s+)mov\./$1mov/o				or
067bfb
 	s/^(\s+)ret/$1bx\tlr/o;
067bfb
 
067bfb
+	if (s/^(\s+)mov\.([a-z]+)/$1mov$2/) {
067bfb
+	    print "	it	$2\n";
067bfb
+	}
067bfb
+
067bfb
 	print $_,"\n";
067bfb
     }
067bfb
 }
067bfb
diff -up openssl-1.1.1c/crypto/aes/asm/vpaes-armv8.pl.arm-update openssl-1.1.1c/crypto/aes/asm/vpaes-armv8.pl
067bfb
--- openssl-1.1.1c/crypto/aes/asm/vpaes-armv8.pl.arm-update	2019-05-28 15:12:21.000000000 +0200
067bfb
+++ openssl-1.1.1c/crypto/aes/asm/vpaes-armv8.pl	2019-11-20 11:36:22.389506155 +0100
067bfb
@@ -30,6 +30,7 @@
067bfb
 # Denver(***)       16.6(**)    15.1/17.8(**)    [8.80/9.93         ]
067bfb
 # Apple A7(***)     22.7(**)    10.9/14.3        [8.45/10.0         ]
067bfb
 # Mongoose(***)     26.3(**)    21.0/25.0(**)    [13.3/16.8         ]
067bfb
+# ThunderX2(***)    39.4(**)    33.8/48.6(**)
067bfb
 #
067bfb
 # (*)	ECB denotes approximate result for parallelizable modes
067bfb
 #	such as CBC decrypt, CTR, etc.;
067bfb
diff -up openssl-1.1.1c/crypto/chacha/asm/chacha-armv8.pl.arm-update openssl-1.1.1c/crypto/chacha/asm/chacha-armv8.pl
067bfb
--- openssl-1.1.1c/crypto/chacha/asm/chacha-armv8.pl.arm-update	2019-05-28 15:12:21.000000000 +0200
067bfb
+++ openssl-1.1.1c/crypto/chacha/asm/chacha-armv8.pl	2019-11-21 16:44:50.814651553 +0100
067bfb
@@ -18,32 +18,44 @@
067bfb
 #
067bfb
 # ChaCha20 for ARMv8.
067bfb
 #
067bfb
+# April 2019
067bfb
+#
067bfb
+# Replace 3xNEON+1xIALU code path with 4+1. 4+1 is actually fastest
067bfb
+# option on most(*), but not all, processors, yet 6+2 is retained.
067bfb
+# This is because penalties are considered tolerable in comparison to
067bfb
+# improvement on processors where 6+2 helps. Most notably +37% on
067bfb
+# ThunderX2. It's server-oriented processor which will have to serve
067bfb
+# as many requests as possible. While others are mostly clients, when
067bfb
+# performance doesn't have to be absolute top-notch, just fast enough,
067bfb
+# as majority of time is spent "entertaining" relatively slow human.
067bfb
+#
067bfb
 # Performance in cycles per byte out of large buffer.
067bfb
 #
067bfb
-#			IALU/gcc-4.9    3xNEON+1xIALU	6xNEON+2xIALU
067bfb
+#			IALU/gcc-4.9	4xNEON+1xIALU	6xNEON+2xIALU
067bfb
 #
067bfb
-# Apple A7		5.50/+49%       3.33            1.70
067bfb
-# Cortex-A53		8.40/+80%       4.72		4.72(*)
067bfb
-# Cortex-A57		8.06/+43%       4.90            4.43(**)
067bfb
-# Denver		4.50/+82%       2.63		2.67(*)
067bfb
-# X-Gene		9.50/+46%       8.82		8.89(*)
067bfb
-# Mongoose		8.00/+44%	3.64		3.25
067bfb
-# Kryo			8.17/+50%	4.83		4.65
067bfb
+# Apple A7		5.50/+49%	2.72		1.60
067bfb
+# Cortex-A53		8.40/+80%	4.06		4.45(*)
067bfb
+# Cortex-A57		8.06/+43%	4.15		4.40(*)
067bfb
+# Denver		4.50/+82%	2.30		2.70(*)
067bfb
+# X-Gene		9.50/+46%	8.20		8.90(*)
067bfb
+# Mongoose		8.00/+44%	2.74		3.12(*)
067bfb
+# Kryo			8.17/+50%	4.47		4.65(*)
067bfb
+# ThunderX2		7.22/+48%	5.64		4.10
067bfb
 #
067bfb
-# (*)	it's expected that doubling interleave factor doesn't help
067bfb
-#	all processors, only those with higher NEON latency and
067bfb
-#	higher instruction issue rate;
067bfb
-# (**)	expected improvement was actually higher;
067bfb
+# (*)	slower than 4+1:-(
067bfb
 
067bfb
-$flavour=shift;
067bfb
-$output=shift;
067bfb
+# $output is the last argument if it looks like a file (it has an extension)
067bfb
+# $flavour is the first argument if it doesn't look like a file
067bfb
+$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
067bfb
+$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
067bfb
 
067bfb
 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
067bfb
 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
067bfb
 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
067bfb
 die "can't locate arm-xlate.pl";
067bfb
 
067bfb
-open OUT,"| \"$^X\" $xlate $flavour $output";
067bfb
+open OUT,"| \"$^X\" $xlate $flavour \"$output\""
067bfb
+    or die "can't call $xlate: $!";
067bfb
 *STDOUT=*OUT;
067bfb
 
067bfb
 sub AUTOLOAD()		# thunk [simplified] x86-style perlasm
067bfb
@@ -120,41 +132,36 @@ my ($a3,$b3,$c3,$d3)=map(($_&~3)+(($_+1)
067bfb
 }
067bfb
 
067bfb
 $code.=<<___;
067bfb
-#include "arm_arch.h"
067bfb
+#ifndef	__KERNEL__
067bfb
+# include "arm_arch.h"
067bfb
+.extern	OPENSSL_armcap_P
067bfb
+#endif
067bfb
 
067bfb
 .text
067bfb
 
067bfb
-.extern	OPENSSL_armcap_P
067bfb
-
067bfb
 .align	5
067bfb
 .Lsigma:
067bfb
 .quad	0x3320646e61707865,0x6b20657479622d32		// endian-neutral
067bfb
 .Lone:
067bfb
-.long	1,0,0,0
067bfb
-.LOPENSSL_armcap_P:
067bfb
-#ifdef	__ILP32__
067bfb
-.long	OPENSSL_armcap_P-.
067bfb
-#else
067bfb
-.quad	OPENSSL_armcap_P-.
067bfb
-#endif
067bfb
-.asciz	"ChaCha20 for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
067bfb
+.long	1,2,3,4
067bfb
+.Lrot24:
067bfb
+.long	0x02010003,0x06050407,0x0a09080b,0x0e0d0c0f
067bfb
+.asciz	"ChaCha20 for ARMv8, CRYPTOGAMS by \@dot-asm"
067bfb
 
067bfb
 .globl	ChaCha20_ctr32
067bfb
 .type	ChaCha20_ctr32,%function
067bfb
 .align	5
067bfb
 ChaCha20_ctr32:
067bfb
 	cbz	$len,.Labort
067bfb
-	adr	@x[0],.LOPENSSL_armcap_P
067bfb
 	cmp	$len,#192
067bfb
 	b.lo	.Lshort
067bfb
-#ifdef	__ILP32__
067bfb
-	ldrsw	@x[1],[@x[0]]
067bfb
-#else
067bfb
-	ldr	@x[1],[@x[0]]
067bfb
-#endif
067bfb
-	ldr	w17,[@x[1],@x[0]]
067bfb
+
067bfb
+#ifndef	__KERNEL__
067bfb
+	adrp	x17,OPENSSL_armcap_P
067bfb
+	ldr	w17,[x17,#:lo12:OPENSSL_armcap_P]
067bfb
 	tst	w17,#ARMV7_NEON
067bfb
-	b.ne	ChaCha20_neon
067bfb
+	b.ne	.LChaCha20_neon
067bfb
+#endif
067bfb
 
067bfb
 .Lshort:
067bfb
 	.inst	0xd503233f			// paciasp
067bfb
@@ -173,7 +180,7 @@ ChaCha20_ctr32:
067bfb
 	ldp	@d[2],@d[3],[$key]		// load key
067bfb
 	ldp	@d[4],@d[5],[$key,#16]
067bfb
 	ldp	@d[6],@d[7],[$ctr]		// load counter
067bfb
-#ifdef	__ARMEB__
067bfb
+#ifdef	__AARCH64EB__
067bfb
 	ror	@d[2],@d[2],#32
067bfb
 	ror	@d[3],@d[3],#32
067bfb
 	ror	@d[4],@d[4],#32
067bfb
@@ -242,7 +249,7 @@ $code.=<<___;
067bfb
 	add	@x[14],@x[14],@x[15],lsl#32
067bfb
 	ldp	@x[13],@x[15],[$inp,#48]
067bfb
 	add	$inp,$inp,#64
067bfb
-#ifdef	__ARMEB__
067bfb
+#ifdef	__AARCH64EB__
067bfb
 	rev	@x[0],@x[0]
067bfb
 	rev	@x[2],@x[2]
067bfb
 	rev	@x[4],@x[4]
067bfb
@@ -299,7 +306,7 @@ $code.=<<___;
067bfb
 	add	@x[10],@x[10],@x[11],lsl#32
067bfb
 	add	@x[12],@x[12],@x[13],lsl#32
067bfb
 	add	@x[14],@x[14],@x[15],lsl#32
067bfb
-#ifdef	__ARMEB__
067bfb
+#ifdef	__AARCH64EB__
067bfb
 	rev	@x[0],@x[0]
067bfb
 	rev	@x[2],@x[2]
067bfb
 	rev	@x[4],@x[4]
067bfb
@@ -340,46 +347,91 @@ $code.=<<___;
067bfb
 ___
067bfb
 
067bfb
 {{{
067bfb
-my ($A0,$B0,$C0,$D0,$A1,$B1,$C1,$D1,$A2,$B2,$C2,$D2,$T0,$T1,$T2,$T3) =
067bfb
-    map("v$_.4s",(0..7,16..23));
067bfb
-my (@K)=map("v$_.4s",(24..30));
067bfb
-my $ONE="v31.4s";
067bfb
+my @K = map("v$_.4s",(0..3));
067bfb
+my ($xt0,$xt1,$xt2,$xt3, $CTR,$ROT24) = map("v$_.4s",(4..9));
067bfb
+my @X = map("v$_.4s",(16,20,24,28, 17,21,25,29, 18,22,26,30, 19,23,27,31));
067bfb
+my ($xa0,$xa1,$xa2,$xa3, $xb0,$xb1,$xb2,$xb3,
067bfb
+    $xc0,$xc1,$xc2,$xc3, $xd0,$xd1,$xd2,$xd3) = @X;
067bfb
 
067bfb
-sub NEONROUND {
067bfb
-my $odd = pop;
067bfb
-my ($a,$b,$c,$d,$t)=@_;
067bfb
+sub NEON_lane_ROUND {
067bfb
+my ($a0,$b0,$c0,$d0)=@_;
067bfb
+my ($a1,$b1,$c1,$d1)=map(($_&~3)+(($_+1)&3),($a0,$b0,$c0,$d0));
067bfb
+my ($a2,$b2,$c2,$d2)=map(($_&~3)+(($_+1)&3),($a1,$b1,$c1,$d1));
067bfb
+my ($a3,$b3,$c3,$d3)=map(($_&~3)+(($_+1)&3),($a2,$b2,$c2,$d2));
067bfb
+my @x=map("'$_'",@X);
067bfb
 
067bfb
 	(
067bfb
-	"&add		('$a','$a','$b')",
067bfb
-	"&eor		('$d','$d','$a')",
067bfb
-	"&rev32_16	('$d','$d')",		# vrot ($d,16)
067bfb
-
067bfb
-	"&add		('$c','$c','$d')",
067bfb
-	"&eor		('$t','$b','$c')",
067bfb
-	"&ushr		('$b','$t',20)",
067bfb
-	"&sli		('$b','$t',12)",
067bfb
-
067bfb
-	"&add		('$a','$a','$b')",
067bfb
-	"&eor		('$t','$d','$a')",
067bfb
-	"&ushr		('$d','$t',24)",
067bfb
-	"&sli		('$d','$t',8)",
067bfb
-
067bfb
-	"&add		('$c','$c','$d')",
067bfb
-	"&eor		('$t','$b','$c')",
067bfb
-	"&ushr		('$b','$t',25)",
067bfb
-	"&sli		('$b','$t',7)",
067bfb
-
067bfb
-	"&ext		('$c','$c','$c',8)",
067bfb
-	"&ext		('$d','$d','$d',$odd?4:12)",
067bfb
-	"&ext		('$b','$b','$b',$odd?12:4)"
067bfb
+	"&add		(@x[$a0],@x[$a0],@x[$b0])",	# Q1
067bfb
+	 "&add		(@x[$a1],@x[$a1],@x[$b1])",	# Q2
067bfb
+	  "&add		(@x[$a2],@x[$a2],@x[$b2])",	# Q3
067bfb
+	   "&add	(@x[$a3],@x[$a3],@x[$b3])",	# Q4
067bfb
+	"&eor		(@x[$d0],@x[$d0],@x[$a0])",
067bfb
+	 "&eor		(@x[$d1],@x[$d1],@x[$a1])",
067bfb
+	  "&eor		(@x[$d2],@x[$d2],@x[$a2])",
067bfb
+	   "&eor	(@x[$d3],@x[$d3],@x[$a3])",
067bfb
+	"&rev32_16	(@x[$d0],@x[$d0])",
067bfb
+	 "&rev32_16	(@x[$d1],@x[$d1])",
067bfb
+	  "&rev32_16	(@x[$d2],@x[$d2])",
067bfb
+	   "&rev32_16	(@x[$d3],@x[$d3])",
067bfb
+
067bfb
+	"&add		(@x[$c0],@x[$c0],@x[$d0])",
067bfb
+	 "&add		(@x[$c1],@x[$c1],@x[$d1])",
067bfb
+	  "&add		(@x[$c2],@x[$c2],@x[$d2])",
067bfb
+	   "&add	(@x[$c3],@x[$c3],@x[$d3])",
067bfb
+	"&eor		('$xt0',@x[$b0],@x[$c0])",
067bfb
+	 "&eor		('$xt1',@x[$b1],@x[$c1])",
067bfb
+	  "&eor		('$xt2',@x[$b2],@x[$c2])",
067bfb
+	   "&eor	('$xt3',@x[$b3],@x[$c3])",
067bfb
+	"&ushr		(@x[$b0],'$xt0',20)",
067bfb
+	 "&ushr		(@x[$b1],'$xt1',20)",
067bfb
+	  "&ushr	(@x[$b2],'$xt2',20)",
067bfb
+	   "&ushr	(@x[$b3],'$xt3',20)",
067bfb
+	"&sli		(@x[$b0],'$xt0',12)",
067bfb
+	 "&sli		(@x[$b1],'$xt1',12)",
067bfb
+	  "&sli		(@x[$b2],'$xt2',12)",
067bfb
+	   "&sli	(@x[$b3],'$xt3',12)",
067bfb
+
067bfb
+	"&add		(@x[$a0],@x[$a0],@x[$b0])",
067bfb
+	 "&add		(@x[$a1],@x[$a1],@x[$b1])",
067bfb
+	  "&add		(@x[$a2],@x[$a2],@x[$b2])",
067bfb
+	   "&add	(@x[$a3],@x[$a3],@x[$b3])",
067bfb
+	"&eor		('$xt0',@x[$d0],@x[$a0])",
067bfb
+	 "&eor		('$xt1',@x[$d1],@x[$a1])",
067bfb
+	  "&eor		('$xt2',@x[$d2],@x[$a2])",
067bfb
+	   "&eor	('$xt3',@x[$d3],@x[$a3])",
067bfb
+	"&tbl		(@x[$d0],'{$xt0}','$ROT24')",
067bfb
+	 "&tbl		(@x[$d1],'{$xt1}','$ROT24')",
067bfb
+	  "&tbl		(@x[$d2],'{$xt2}','$ROT24')",
067bfb
+	   "&tbl	(@x[$d3],'{$xt3}','$ROT24')",
067bfb
+
067bfb
+	"&add		(@x[$c0],@x[$c0],@x[$d0])",
067bfb
+	 "&add		(@x[$c1],@x[$c1],@x[$d1])",
067bfb
+	  "&add		(@x[$c2],@x[$c2],@x[$d2])",
067bfb
+	   "&add	(@x[$c3],@x[$c3],@x[$d3])",
067bfb
+	"&eor		('$xt0',@x[$b0],@x[$c0])",
067bfb
+	 "&eor		('$xt1',@x[$b1],@x[$c1])",
067bfb
+	  "&eor		('$xt2',@x[$b2],@x[$c2])",
067bfb
+	   "&eor	('$xt3',@x[$b3],@x[$c3])",
067bfb
+	"&ushr		(@x[$b0],'$xt0',25)",
067bfb
+	 "&ushr		(@x[$b1],'$xt1',25)",
067bfb
+	  "&ushr	(@x[$b2],'$xt2',25)",
067bfb
+	   "&ushr	(@x[$b3],'$xt3',25)",
067bfb
+	"&sli		(@x[$b0],'$xt0',7)",
067bfb
+	 "&sli		(@x[$b1],'$xt1',7)",
067bfb
+	  "&sli		(@x[$b2],'$xt2',7)",
067bfb
+	   "&sli	(@x[$b3],'$xt3',7)"
067bfb
 	);
067bfb
 }
067bfb
 
067bfb
 $code.=<<___;
067bfb
 
067bfb
+#ifdef	__KERNEL__
067bfb
+.globl	ChaCha20_neon
067bfb
+#endif
067bfb
 .type	ChaCha20_neon,%function
067bfb
 .align	5
067bfb
 ChaCha20_neon:
067bfb
+.LChaCha20_neon:
067bfb
 	.inst	0xd503233f			// paciasp
067bfb
 	stp	x29,x30,[sp,#-96]!
067bfb
 	add	x29,sp,#0
067bfb
@@ -402,8 +454,9 @@ ChaCha20_neon:
067bfb
 	ld1	{@K[1],@K[2]},[$key]
067bfb
 	ldp	@d[6],@d[7],[$ctr]		// load counter
067bfb
 	ld1	{@K[3]},[$ctr]
067bfb
-	ld1	{$ONE},[@x[0]]
067bfb
-#ifdef	__ARMEB__
067bfb
+	stp	d8,d9,[sp]			// meet ABI requirements
067bfb
+	ld1	{$CTR,$ROT24},[@x[0]]
067bfb
+#ifdef	__AARCH64EB__
067bfb
 	rev64	@K[0],@K[0]
067bfb
 	ror	@d[2],@d[2],#32
067bfb
 	ror	@d[3],@d[3],#32
067bfb
@@ -412,115 +465,129 @@ ChaCha20_neon:
067bfb
 	ror	@d[6],@d[6],#32
067bfb
 	ror	@d[7],@d[7],#32
067bfb
 #endif
067bfb
-	add	@K[3],@K[3],$ONE		// += 1
067bfb
-	add	@K[4],@K[3],$ONE
067bfb
-	add	@K[5],@K[4],$ONE
067bfb
-	shl	$ONE,$ONE,#2			// 1 -> 4
067bfb
 
067bfb
 .Loop_outer_neon:
067bfb
-	mov.32	@x[0],@d[0]			// unpack key block
067bfb
-	lsr	@x[1],@d[0],#32
067bfb
-	 mov	$A0,@K[0]
067bfb
-	mov.32	@x[2],@d[1]
067bfb
-	lsr	@x[3],@d[1],#32
067bfb
-	 mov	$A1,@K[0]
067bfb
-	mov.32	@x[4],@d[2]
067bfb
-	lsr	@x[5],@d[2],#32
067bfb
-	 mov	$A2,@K[0]
067bfb
-	mov.32	@x[6],@d[3]
067bfb
-	 mov	$B0,@K[1]
067bfb
-	lsr	@x[7],@d[3],#32
067bfb
-	 mov	$B1,@K[1]
067bfb
-	mov.32	@x[8],@d[4]
067bfb
-	 mov	$B2,@K[1]
067bfb
-	lsr	@x[9],@d[4],#32
067bfb
-	 mov	$D0,@K[3]
067bfb
-	mov.32	@x[10],@d[5]
067bfb
-	 mov	$D1,@K[4]
067bfb
-	lsr	@x[11],@d[5],#32
067bfb
-	 mov	$D2,@K[5]
067bfb
-	mov.32	@x[12],@d[6]
067bfb
-	 mov	$C0,@K[2]
067bfb
-	lsr	@x[13],@d[6],#32
067bfb
-	 mov	$C1,@K[2]
067bfb
-	mov.32	@x[14],@d[7]
067bfb
-	 mov	$C2,@K[2]
067bfb
-	lsr	@x[15],@d[7],#32
067bfb
+	dup	$xa0,@{K[0]}[0]			// unpack key block
067bfb
+	 mov.32	@x[0],@d[0]
067bfb
+	dup	$xa1,@{K[0]}[1]
067bfb
+	 lsr	@x[1],@d[0],#32
067bfb
+	dup	$xa2,@{K[0]}[2]
067bfb
+	 mov.32	@x[2],@d[1]
067bfb
+	dup	$xa3,@{K[0]}[3]
067bfb
+	 lsr	@x[3],@d[1],#32
067bfb
+	dup	$xb0,@{K[1]}[0]
067bfb
+	 mov.32	@x[4],@d[2]
067bfb
+	dup	$xb1,@{K[1]}[1]
067bfb
+	 lsr	@x[5],@d[2],#32
067bfb
+	dup	$xb2,@{K[1]}[2]
067bfb
+	 mov.32	@x[6],@d[3]
067bfb
+	dup	$xb3,@{K[1]}[3]
067bfb
+	 lsr	@x[7],@d[3],#32
067bfb
+	dup	$xd0,@{K[3]}[0]
067bfb
+	 mov.32	@x[8],@d[4]
067bfb
+	dup	$xd1,@{K[3]}[1]
067bfb
+	 lsr	@x[9],@d[4],#32
067bfb
+	dup	$xd2,@{K[3]}[2]
067bfb
+	 mov.32	@x[10],@d[5]
067bfb
+	dup	$xd3,@{K[3]}[3]
067bfb
+	 lsr	@x[11],@d[5],#32
067bfb
+	add	$xd0,$xd0,$CTR
067bfb
+	 mov.32	@x[12],@d[6]
067bfb
+	dup	$xc0,@{K[2]}[0]
067bfb
+	 lsr	@x[13],@d[6],#32
067bfb
+	dup	$xc1,@{K[2]}[1]
067bfb
+	 mov.32	@x[14],@d[7]
067bfb
+	dup	$xc2,@{K[2]}[2]
067bfb
+	 lsr	@x[15],@d[7],#32
067bfb
+	dup	$xc3,@{K[2]}[3]
067bfb
 
067bfb
 	mov	$ctr,#10
067bfb
-	subs	$len,$len,#256
067bfb
+	subs	$len,$len,#320
067bfb
 .Loop_neon:
067bfb
 	sub	$ctr,$ctr,#1
067bfb
 ___
067bfb
-	my @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,0);
067bfb
-	my @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,0);
067bfb
-	my @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,0);
067bfb
-	my @thread3=&ROUND(0,4,8,12);
067bfb
-
067bfb
-	foreach (@thread0) {
067bfb
-		eval;			eval(shift(@thread3));
067bfb
-		eval(shift(@thread1));	eval(shift(@thread3));
067bfb
-		eval(shift(@thread2));	eval(shift(@thread3));
067bfb
-	}
067bfb
-
067bfb
-	@thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,1);
067bfb
-	@thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,1);
067bfb
-	@thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,1);
067bfb
-	@thread3=&ROUND(0,5,10,15);
067bfb
+	my @plus_one=&ROUND(0,4,8,12);
067bfb
+	foreach (&NEON_lane_ROUND(0,4,8,12))  { eval; eval(shift(@plus_one)); }
067bfb
 
067bfb
-	foreach (@thread0) {
067bfb
-		eval;			eval(shift(@thread3));
067bfb
-		eval(shift(@thread1));	eval(shift(@thread3));
067bfb
-		eval(shift(@thread2));	eval(shift(@thread3));
067bfb
-	}
067bfb
+	@plus_one=&ROUND(0,5,10,15);
067bfb
+	foreach (&NEON_lane_ROUND(0,5,10,15)) { eval; eval(shift(@plus_one)); }
067bfb
 $code.=<<___;
067bfb
 	cbnz	$ctr,.Loop_neon
067bfb
 
067bfb
-	add.32	@x[0],@x[0],@d[0]		// accumulate key block
067bfb
-	 add	$A0,$A0,@K[0]
067bfb
-	add	@x[1],@x[1],@d[0],lsr#32
067bfb
-	 add	$A1,$A1,@K[0]
067bfb
-	add.32	@x[2],@x[2],@d[1]
067bfb
-	 add	$A2,$A2,@K[0]
067bfb
-	add	@x[3],@x[3],@d[1],lsr#32
067bfb
-	 add	$C0,$C0,@K[2]
067bfb
-	add.32	@x[4],@x[4],@d[2]
067bfb
-	 add	$C1,$C1,@K[2]
067bfb
-	add	@x[5],@x[5],@d[2],lsr#32
067bfb
-	 add	$C2,$C2,@K[2]
067bfb
-	add.32	@x[6],@x[6],@d[3]
067bfb
-	 add	$D0,$D0,@K[3]
067bfb
-	add	@x[7],@x[7],@d[3],lsr#32
067bfb
-	add.32	@x[8],@x[8],@d[4]
067bfb
-	 add	$D1,$D1,@K[4]
067bfb
-	add	@x[9],@x[9],@d[4],lsr#32
067bfb
-	add.32	@x[10],@x[10],@d[5]
067bfb
-	 add	$D2,$D2,@K[5]
067bfb
-	add	@x[11],@x[11],@d[5],lsr#32
067bfb
-	add.32	@x[12],@x[12],@d[6]
067bfb
-	 add	$B0,$B0,@K[1]
067bfb
-	add	@x[13],@x[13],@d[6],lsr#32
067bfb
-	add.32	@x[14],@x[14],@d[7]
067bfb
-	 add	$B1,$B1,@K[1]
067bfb
-	add	@x[15],@x[15],@d[7],lsr#32
067bfb
-	 add	$B2,$B2,@K[1]
067bfb
+	add	$xd0,$xd0,$CTR
067bfb
+
067bfb
+	zip1	$xt0,$xa0,$xa1			// transpose data
067bfb
+	zip1	$xt1,$xa2,$xa3
067bfb
+	zip2	$xt2,$xa0,$xa1
067bfb
+	zip2	$xt3,$xa2,$xa3
067bfb
+	zip1.64	$xa0,$xt0,$xt1
067bfb
+	zip2.64	$xa1,$xt0,$xt1
067bfb
+	zip1.64	$xa2,$xt2,$xt3
067bfb
+	zip2.64	$xa3,$xt2,$xt3
067bfb
+
067bfb
+	zip1	$xt0,$xb0,$xb1
067bfb
+	zip1	$xt1,$xb2,$xb3
067bfb
+	zip2	$xt2,$xb0,$xb1
067bfb
+	zip2	$xt3,$xb2,$xb3
067bfb
+	zip1.64	$xb0,$xt0,$xt1
067bfb
+	zip2.64	$xb1,$xt0,$xt1
067bfb
+	zip1.64	$xb2,$xt2,$xt3
067bfb
+	zip2.64	$xb3,$xt2,$xt3
067bfb
+
067bfb
+	zip1	$xt0,$xc0,$xc1
067bfb
+	 add.32	@x[0],@x[0],@d[0]		// accumulate key block
067bfb
+	zip1	$xt1,$xc2,$xc3
067bfb
+	 add	@x[1],@x[1],@d[0],lsr#32
067bfb
+	zip2	$xt2,$xc0,$xc1
067bfb
+	 add.32	@x[2],@x[2],@d[1]
067bfb
+	zip2	$xt3,$xc2,$xc3
067bfb
+	 add	@x[3],@x[3],@d[1],lsr#32
067bfb
+	zip1.64	$xc0,$xt0,$xt1
067bfb
+	 add.32	@x[4],@x[4],@d[2]
067bfb
+	zip2.64	$xc1,$xt0,$xt1
067bfb
+	 add	@x[5],@x[5],@d[2],lsr#32
067bfb
+	zip1.64	$xc2,$xt2,$xt3
067bfb
+	 add.32	@x[6],@x[6],@d[3]
067bfb
+	zip2.64	$xc3,$xt2,$xt3
067bfb
+	 add	@x[7],@x[7],@d[3],lsr#32
067bfb
+
067bfb
+	zip1	$xt0,$xd0,$xd1
067bfb
+	 add.32	@x[8],@x[8],@d[4]
067bfb
+	zip1	$xt1,$xd2,$xd3
067bfb
+	 add	@x[9],@x[9],@d[4],lsr#32
067bfb
+	zip2	$xt2,$xd0,$xd1
067bfb
+	 add.32	@x[10],@x[10],@d[5]
067bfb
+	zip2	$xt3,$xd2,$xd3
067bfb
+	 add	@x[11],@x[11],@d[5],lsr#32
067bfb
+	zip1.64	$xd0,$xt0,$xt1
067bfb
+	 add.32	@x[12],@x[12],@d[6]
067bfb
+	zip2.64	$xd1,$xt0,$xt1
067bfb
+	 add	@x[13],@x[13],@d[6],lsr#32
067bfb
+	zip1.64	$xd2,$xt2,$xt3
067bfb
+	 add.32	@x[14],@x[14],@d[7]
067bfb
+	zip2.64	$xd3,$xt2,$xt3
067bfb
+	 add	@x[15],@x[15],@d[7],lsr#32
067bfb
 
067bfb
 	b.lo	.Ltail_neon
067bfb
 
067bfb
 	add	@x[0],@x[0],@x[1],lsl#32	// pack
067bfb
 	add	@x[2],@x[2],@x[3],lsl#32
067bfb
 	ldp	@x[1],@x[3],[$inp,#0]		// load input
067bfb
+	 add	$xa0,$xa0,@K[0]			// accumulate key block
067bfb
 	add	@x[4],@x[4],@x[5],lsl#32
067bfb
 	add	@x[6],@x[6],@x[7],lsl#32
067bfb
 	ldp	@x[5],@x[7],[$inp,#16]
067bfb
+	 add	$xb0,$xb0,@K[1]
067bfb
 	add	@x[8],@x[8],@x[9],lsl#32
067bfb
 	add	@x[10],@x[10],@x[11],lsl#32
067bfb
 	ldp	@x[9],@x[11],[$inp,#32]
067bfb
+	 add	$xc0,$xc0,@K[2]
067bfb
 	add	@x[12],@x[12],@x[13],lsl#32
067bfb
 	add	@x[14],@x[14],@x[15],lsl#32
067bfb
 	ldp	@x[13],@x[15],[$inp,#48]
067bfb
+	 add	$xd0,$xd0,@K[3]
067bfb
 	add	$inp,$inp,#64
067bfb
-#ifdef	__ARMEB__
067bfb
+#ifdef	__AARCH64EB__
067bfb
 	rev	@x[0],@x[0]
067bfb
 	rev	@x[2],@x[2]
067bfb
 	rev	@x[4],@x[4]
067bfb
@@ -530,48 +597,68 @@ $code.=<<___;
067bfb
 	rev	@x[12],@x[12]
067bfb
 	rev	@x[14],@x[14]
067bfb
 #endif
067bfb
-	ld1.8	{$T0-$T3},[$inp],#64
067bfb
+	ld1.8	{$xt0-$xt3},[$inp],#64
067bfb
 	eor	@x[0],@x[0],@x[1]
067bfb
+	 add	$xa1,$xa1,@K[0]
067bfb
 	eor	@x[2],@x[2],@x[3]
067bfb
+	 add	$xb1,$xb1,@K[1]
067bfb
 	eor	@x[4],@x[4],@x[5]
067bfb
+	 add	$xc1,$xc1,@K[2]
067bfb
 	eor	@x[6],@x[6],@x[7]
067bfb
+	 add	$xd1,$xd1,@K[3]
067bfb
 	eor	@x[8],@x[8],@x[9]
067bfb
-	 eor	$A0,$A0,$T0
067bfb
+	 eor	$xa0,$xa0,$xt0
067bfb
+	 movi	$xt0,#5
067bfb
 	eor	@x[10],@x[10],@x[11]
067bfb
-	 eor	$B0,$B0,$T1
067bfb
+	 eor	$xb0,$xb0,$xt1
067bfb
 	eor	@x[12],@x[12],@x[13]
067bfb
-	 eor	$C0,$C0,$T2
067bfb
+	 eor	$xc0,$xc0,$xt2
067bfb
 	eor	@x[14],@x[14],@x[15]
067bfb
-	 eor	$D0,$D0,$T3
067bfb
-	 ld1.8	{$T0-$T3},[$inp],#64
067bfb
+	 eor	$xd0,$xd0,$xt3
067bfb
+	 add	$CTR,$CTR,$xt0			// += 5
067bfb
+	 ld1.8	{$xt0-$xt3},[$inp],#64
067bfb
 
067bfb
 	stp	@x[0],@x[2],[$out,#0]		// store output
067bfb
-	 add	@d[6],@d[6],#4			// increment counter
067bfb
+	 add	@d[6],@d[6],#5			// increment counter
067bfb
 	stp	@x[4],@x[6],[$out,#16]
067bfb
-	 add	@K[3],@K[3],$ONE		// += 4
067bfb
 	stp	@x[8],@x[10],[$out,#32]
067bfb
-	 add	@K[4],@K[4],$ONE
067bfb
 	stp	@x[12],@x[14],[$out,#48]
067bfb
-	 add	@K[5],@K[5],$ONE
067bfb
 	add	$out,$out,#64
067bfb
 
067bfb
-	st1.8	{$A0-$D0},[$out],#64
067bfb
-	ld1.8	{$A0-$D0},[$inp],#64
067bfb
-
067bfb
-	eor	$A1,$A1,$T0
067bfb
-	eor	$B1,$B1,$T1
067bfb
-	eor	$C1,$C1,$T2
067bfb
-	eor	$D1,$D1,$T3
067bfb
-	st1.8	{$A1-$D1},[$out],#64
067bfb
-
067bfb
-	eor	$A2,$A2,$A0
067bfb
-	eor	$B2,$B2,$B0
067bfb
-	eor	$C2,$C2,$C0
067bfb
-	eor	$D2,$D2,$D0
067bfb
-	st1.8	{$A2-$D2},[$out],#64
067bfb
+	st1.8	{$xa0-$xd0},[$out],#64
067bfb
+	 add	$xa2,$xa2,@K[0]
067bfb
+	 add	$xb2,$xb2,@K[1]
067bfb
+	 add	$xc2,$xc2,@K[2]
067bfb
+	 add	$xd2,$xd2,@K[3]
067bfb
+	ld1.8	{$xa0-$xd0},[$inp],#64
067bfb
+
067bfb
+	eor	$xa1,$xa1,$xt0
067bfb
+	eor	$xb1,$xb1,$xt1
067bfb
+	eor	$xc1,$xc1,$xt2
067bfb
+	eor	$xd1,$xd1,$xt3
067bfb
+	st1.8	{$xa1-$xd1},[$out],#64
067bfb
+	 add	$xa3,$xa3,@K[0]
067bfb
+	 add	$xb3,$xb3,@K[1]
067bfb
+	 add	$xc3,$xc3,@K[2]
067bfb
+	 add	$xd3,$xd3,@K[3]
067bfb
+	ld1.8	{$xa1-$xd1},[$inp],#64
067bfb
+
067bfb
+	eor	$xa2,$xa2,$xa0
067bfb
+	eor	$xb2,$xb2,$xb0
067bfb
+	eor	$xc2,$xc2,$xc0
067bfb
+	eor	$xd2,$xd2,$xd0
067bfb
+	st1.8	{$xa2-$xd2},[$out],#64
067bfb
+
067bfb
+	eor	$xa3,$xa3,$xa1
067bfb
+	eor	$xb3,$xb3,$xb1
067bfb
+	eor	$xc3,$xc3,$xc1
067bfb
+	eor	$xd3,$xd3,$xd1
067bfb
+	st1.8	{$xa3-$xd3},[$out],#64
067bfb
 
067bfb
 	b.hi	.Loop_outer_neon
067bfb
 
067bfb
+	ldp	d8,d9,[sp]			// meet ABI requirements
067bfb
+
067bfb
 	ldp	x19,x20,[x29,#16]
067bfb
 	add	sp,sp,#64
067bfb
 	ldp	x21,x22,[x29,#32]
067bfb
@@ -582,8 +669,10 @@ $code.=<<___;
067bfb
 	.inst	0xd50323bf			// autiasp
067bfb
 	ret
067bfb
 
067bfb
+.align	4
067bfb
 .Ltail_neon:
067bfb
-	add	$len,$len,#256
067bfb
+	add	$len,$len,#320
067bfb
+	ldp	d8,d9,[sp]			// meet ABI requirements
067bfb
 	cmp	$len,#64
067bfb
 	b.lo	.Less_than_64
067bfb
 
067bfb
@@ -600,7 +689,7 @@ $code.=<<___;
067bfb
 	add	@x[14],@x[14],@x[15],lsl#32
067bfb
 	ldp	@x[13],@x[15],[$inp,#48]
067bfb
 	add	$inp,$inp,#64
067bfb
-#ifdef	__ARMEB__
067bfb
+#ifdef	__AARCH64EB__
067bfb
 	rev	@x[0],@x[0]
067bfb
 	rev	@x[2],@x[2]
067bfb
 	rev	@x[4],@x[4]
067bfb
@@ -620,48 +709,68 @@ $code.=<<___;
067bfb
 	eor	@x[14],@x[14],@x[15]
067bfb
 
067bfb
 	stp	@x[0],@x[2],[$out,#0]		// store output
067bfb
-	 add	@d[6],@d[6],#4			// increment counter
067bfb
+	 add	$xa0,$xa0,@K[0]			// accumulate key block
067bfb
 	stp	@x[4],@x[6],[$out,#16]
067bfb
+	 add	$xb0,$xb0,@K[1]
067bfb
 	stp	@x[8],@x[10],[$out,#32]
067bfb
+	 add	$xc0,$xc0,@K[2]
067bfb
 	stp	@x[12],@x[14],[$out,#48]
067bfb
+	 add	$xd0,$xd0,@K[3]
067bfb
 	add	$out,$out,#64
067bfb
 	b.eq	.Ldone_neon
067bfb
 	sub	$len,$len,#64
067bfb
 	cmp	$len,#64
067bfb
-	b.lo	.Less_than_128
067bfb
+	b.lo	.Last_neon
067bfb
 
067bfb
-	ld1.8	{$T0-$T3},[$inp],#64
067bfb
-	eor	$A0,$A0,$T0
067bfb
-	eor	$B0,$B0,$T1
067bfb
-	eor	$C0,$C0,$T2
067bfb
-	eor	$D0,$D0,$T3
067bfb
-	st1.8	{$A0-$D0},[$out],#64
067bfb
+	ld1.8	{$xt0-$xt3},[$inp],#64
067bfb
+	eor	$xa0,$xa0,$xt0
067bfb
+	eor	$xb0,$xb0,$xt1
067bfb
+	eor	$xc0,$xc0,$xt2
067bfb
+	eor	$xd0,$xd0,$xt3
067bfb
+	st1.8	{$xa0-$xd0},[$out],#64
067bfb
 	b.eq	.Ldone_neon
067bfb
+
067bfb
+	add	$xa0,$xa1,@K[0]
067bfb
+	add	$xb0,$xb1,@K[1]
067bfb
 	sub	$len,$len,#64
067bfb
+	add	$xc0,$xc1,@K[2]
067bfb
 	cmp	$len,#64
067bfb
-	b.lo	.Less_than_192
067bfb
+	add	$xd0,$xd1,@K[3]
067bfb
+	b.lo	.Last_neon
067bfb
 
067bfb
-	ld1.8	{$T0-$T3},[$inp],#64
067bfb
-	eor	$A1,$A1,$T0
067bfb
-	eor	$B1,$B1,$T1
067bfb
-	eor	$C1,$C1,$T2
067bfb
-	eor	$D1,$D1,$T3
067bfb
-	st1.8	{$A1-$D1},[$out],#64
067bfb
+	ld1.8	{$xt0-$xt3},[$inp],#64
067bfb
+	eor	$xa1,$xa0,$xt0
067bfb
+	eor	$xb1,$xb0,$xt1
067bfb
+	eor	$xc1,$xc0,$xt2
067bfb
+	eor	$xd1,$xd0,$xt3
067bfb
+	st1.8	{$xa1-$xd1},[$out],#64
067bfb
 	b.eq	.Ldone_neon
067bfb
+
067bfb
+	add	$xa0,$xa2,@K[0]
067bfb
+	add	$xb0,$xb2,@K[1]
067bfb
 	sub	$len,$len,#64
067bfb
+	add	$xc0,$xc2,@K[2]
067bfb
+	cmp	$len,#64
067bfb
+	add	$xd0,$xd2,@K[3]
067bfb
+	b.lo	.Last_neon
067bfb
 
067bfb
-	st1.8	{$A2-$D2},[sp]
067bfb
-	b	.Last_neon
067bfb
+	ld1.8	{$xt0-$xt3},[$inp],#64
067bfb
+	eor	$xa2,$xa0,$xt0
067bfb
+	eor	$xb2,$xb0,$xt1
067bfb
+	eor	$xc2,$xc0,$xt2
067bfb
+	eor	$xd2,$xd0,$xt3
067bfb
+	st1.8	{$xa2-$xd2},[$out],#64
067bfb
+	b.eq	.Ldone_neon
067bfb
 
067bfb
-.Less_than_128:
067bfb
-	st1.8	{$A0-$D0},[sp]
067bfb
-	b	.Last_neon
067bfb
-.Less_than_192:
067bfb
-	st1.8	{$A1-$D1},[sp]
067bfb
-	b	.Last_neon
067bfb
+	add	$xa0,$xa3,@K[0]
067bfb
+	add	$xb0,$xb3,@K[1]
067bfb
+	add	$xc0,$xc3,@K[2]
067bfb
+	add	$xd0,$xd3,@K[3]
067bfb
+	sub	$len,$len,#64
067bfb
 
067bfb
-.align	4
067bfb
 .Last_neon:
067bfb
+	st1.8	{$xa0-$xd0},[sp]
067bfb
+
067bfb
 	sub	$out,$out,#1
067bfb
 	add	$inp,$inp,$len
067bfb
 	add	$out,$out,$len
067bfb
@@ -694,9 +803,41 @@ $code.=<<___;
067bfb
 .size	ChaCha20_neon,.-ChaCha20_neon
067bfb
 ___
067bfb
 {
067bfb
+my @K = map("v$_.4s",(0..6));
067bfb
 my ($T0,$T1,$T2,$T3,$T4,$T5)=@K;
067bfb
 my ($A0,$B0,$C0,$D0,$A1,$B1,$C1,$D1,$A2,$B2,$C2,$D2,
067bfb
-    $A3,$B3,$C3,$D3,$A4,$B4,$C4,$D4,$A5,$B5,$C5,$D5) = map("v$_.4s",(0..23));
067bfb
+    $A3,$B3,$C3,$D3,$A4,$B4,$C4,$D4,$A5,$B5,$C5,$D5) = map("v$_.4s",(8..31));
067bfb
+my $rot24 = @K[6];
067bfb
+my $ONE = "v7.4s";
067bfb
+
067bfb
+sub NEONROUND {
067bfb
+my $odd = pop;
067bfb
+my ($a,$b,$c,$d,$t)=@_;
067bfb
+
067bfb
+	(
067bfb
+	"&add		('$a','$a','$b')",
067bfb
+	"&eor		('$d','$d','$a')",
067bfb
+	"&rev32_16	('$d','$d')",		# vrot ($d,16)
067bfb
+
067bfb
+	"&add		('$c','$c','$d')",
067bfb
+	"&eor		('$t','$b','$c')",
067bfb
+	"&ushr		('$b','$t',20)",
067bfb
+	"&sli		('$b','$t',12)",
067bfb
+
067bfb
+	"&add		('$a','$a','$b')",
067bfb
+	"&eor		('$d','$d','$a')",
067bfb
+	"&tbl		('$d','{$d}','$rot24')",
067bfb
+
067bfb
+	"&add		('$c','$c','$d')",
067bfb
+	"&eor		('$t','$b','$c')",
067bfb
+	"&ushr		('$b','$t',25)",
067bfb
+	"&sli		('$b','$t',7)",
067bfb
+
067bfb
+	"&ext		('$c','$c','$c',8)",
067bfb
+	"&ext		('$d','$d','$d',$odd?4:12)",
067bfb
+	"&ext		('$b','$b','$b',$odd?12:4)"
067bfb
+	);
067bfb
+}
067bfb
 
067bfb
 $code.=<<___;
067bfb
 .type	ChaCha20_512_neon,%function
067bfb
@@ -716,6 +857,7 @@ ChaCha20_512_neon:
067bfb
 .L512_or_more_neon:
067bfb
 	sub	sp,sp,#128+64
067bfb
 
067bfb
+	eor	$ONE,$ONE,$ONE
067bfb
 	ldp	@d[0],@d[1],[@x[0]]		// load sigma
067bfb
 	ld1	{@K[0]},[@x[0]],#16
067bfb
 	ldp	@d[2],@d[3],[$key]		// load key
067bfb
@@ -723,8 +865,9 @@ ChaCha20_512_neon:
067bfb
 	ld1	{@K[1],@K[2]},[$key]
067bfb
 	ldp	@d[6],@d[7],[$ctr]		// load counter
067bfb
 	ld1	{@K[3]},[$ctr]
067bfb
-	ld1	{$ONE},[@x[0]]
067bfb
-#ifdef	__ARMEB__
067bfb
+	ld1	{$ONE}[0],[@x[0]]
067bfb
+	add	$key,@x[0],#16			// .Lrot24
067bfb
+#ifdef	__AARCH64EB__
067bfb
 	rev64	@K[0],@K[0]
067bfb
 	ror	@d[2],@d[2],#32
067bfb
 	ror	@d[3],@d[3],#32
067bfb
@@ -791,9 +934,10 @@ ChaCha20_512_neon:
067bfb
 	 mov	$C4,@K[2]
067bfb
 	 stp	@K[3],@K[4],[sp,#48]		// off-load key block, variable part
067bfb
 	 mov	$C5,@K[2]
067bfb
-	 str	@K[5],[sp,#80]
067bfb
+	 stp	@K[5],@K[6],[sp,#80]
067bfb
 
067bfb
 	mov	$ctr,#5
067bfb
+	ld1	{$rot24},[$key]
067bfb
 	subs	$len,$len,#512
067bfb
 .Loop_upper_neon:
067bfb
 	sub	$ctr,$ctr,#1
067bfb
@@ -866,7 +1010,7 @@ $code.=<<___;
067bfb
 	add	@x[14],@x[14],@x[15],lsl#32
067bfb
 	ldp	@x[13],@x[15],[$inp,#48]
067bfb
 	add	$inp,$inp,#64
067bfb
-#ifdef	__ARMEB__
067bfb
+#ifdef	__AARCH64EB__
067bfb
 	rev	@x[0],@x[0]
067bfb
 	rev	@x[2],@x[2]
067bfb
 	rev	@x[4],@x[4]
067bfb
@@ -955,6 +1099,7 @@ $code.=<<___;
067bfb
 	add.32	@x[2],@x[2],@d[1]
067bfb
 	 ldp	@K[4],@K[5],[sp,#64]
067bfb
 	add	@x[3],@x[3],@d[1],lsr#32
067bfb
+	 ldr	@K[6],[sp,#96]
067bfb
 	 add	$A0,$A0,@K[0]
067bfb
 	add.32	@x[4],@x[4],@d[2]
067bfb
 	 add	$A1,$A1,@K[0]
067bfb
@@ -1007,7 +1152,7 @@ $code.=<<___;
067bfb
 	add	$inp,$inp,#64
067bfb
 	 add	$B5,$B5,@K[1]
067bfb
 
067bfb
-#ifdef	__ARMEB__
067bfb
+#ifdef	__AARCH64EB__
067bfb
 	rev	@x[0],@x[0]
067bfb
 	rev	@x[2],@x[2]
067bfb
 	rev	@x[4],@x[4]
067bfb
@@ -1085,26 +1230,26 @@ $code.=<<___;
067bfb
 	b.hs	.Loop_outer_512_neon
067bfb
 
067bfb
 	adds	$len,$len,#512
067bfb
-	ushr	$A0,$ONE,#2			// 4 -> 1
067bfb
+	ushr	$ONE,$ONE,#1			// 4 -> 2
067bfb
 
067bfb
-	ldp	d8,d9,[sp,#128+0]		// meet ABI requirements
067bfb
 	ldp	d10,d11,[sp,#128+16]
067bfb
 	ldp	d12,d13,[sp,#128+32]
067bfb
 	ldp	d14,d15,[sp,#128+48]
067bfb
 
067bfb
-	stp	@K[0],$ONE,[sp,#0]		// wipe off-load area
067bfb
-	stp	@K[0],$ONE,[sp,#32]
067bfb
-	stp	@K[0],$ONE,[sp,#64]
067bfb
+	stp	@K[0],@K[0],[sp,#0]		// wipe off-load area
067bfb
+	stp	@K[0],@K[0],[sp,#32]
067bfb
+	stp	@K[0],@K[0],[sp,#64]
067bfb
 
067bfb
 	b.eq	.Ldone_512_neon
067bfb
 
067bfb
+	sub	$key,$key,#16			// .Lone
067bfb
 	cmp	$len,#192
067bfb
-	sub	@K[3],@K[3],$A0			// -= 1
067bfb
-	sub	@K[4],@K[4],$A0
067bfb
-	sub	@K[5],@K[5],$A0
067bfb
 	add	sp,sp,#128
067bfb
+	sub	@K[3],@K[3],$ONE		// -= 2
067bfb
+	ld1	{$CTR,$ROT24},[$key]
067bfb
 	b.hs	.Loop_outer_neon
067bfb
 
067bfb
+	ldp	d8,d9,[sp,#0]			// meet ABI requirements
067bfb
 	eor	@K[1],@K[1],@K[1]
067bfb
 	eor	@K[2],@K[2],@K[2]
067bfb
 	eor	@K[3],@K[3],@K[3]
067bfb
@@ -1114,6 +1259,7 @@ $code.=<<___;
067bfb
 	b	.Loop_outer
067bfb
 
067bfb
 .Ldone_512_neon:
067bfb
+	ldp	d8,d9,[sp,#128+0]		// meet ABI requirements
067bfb
 	ldp	x19,x20,[x29,#16]
067bfb
 	add	sp,sp,#128+64
067bfb
 	ldp	x21,x22,[x29,#32]
067bfb
@@ -1132,9 +1278,11 @@ foreach (split("\n",$code)) {
067bfb
 	s/\`([^\`]*)\`/eval $1/geo;
067bfb
 
067bfb
 	(s/\b([a-z]+)\.32\b/$1/ and (s/x([0-9]+)/w$1/g or 1))	or
067bfb
-	(m/\b(eor|ext|mov)\b/ and (s/\.4s/\.16b/g or 1))	or
067bfb
+	(m/\b(eor|ext|mov|tbl)\b/ and (s/\.4s/\.16b/g or 1))	or
067bfb
 	(s/\b((?:ld|st)1)\.8\b/$1/ and (s/\.4s/\.16b/g or 1))	or
067bfb
 	(m/\b(ld|st)[rp]\b/ and (s/v([0-9]+)\.4s/q$1/g or 1))	or
067bfb
+	(m/\b(dup|ld1)\b/ and (s/\.4(s}?\[[0-3]\])/.$1/g or 1))	or
067bfb
+	(s/\b(zip[12])\.64\b/$1/ and (s/\.4s/\.2d/g or 1))	or
067bfb
 	(s/\brev32\.16\b/rev32/ and (s/\.4s/\.8h/g or 1));
067bfb
 
067bfb
 	#s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/geo;
067bfb
diff -up openssl-1.1.1c/crypto/modes/asm/ghashv8-armx.pl.arm-update openssl-1.1.1c/crypto/modes/asm/ghashv8-armx.pl
067bfb
--- openssl-1.1.1c/crypto/modes/asm/ghashv8-armx.pl.arm-update	2019-05-28 15:12:21.000000000 +0200
067bfb
+++ openssl-1.1.1c/crypto/modes/asm/ghashv8-armx.pl	2019-11-20 11:36:22.389506155 +0100
067bfb
@@ -42,6 +42,7 @@
067bfb
 # Denver	0.51		0.65		6.02
067bfb
 # Mongoose	0.65		1.10		8.06
067bfb
 # Kryo		0.76		1.16		8.00
067bfb
+# ThunderX2	1.05
067bfb
 #
067bfb
 # (*)	presented for reference/comparison purposes;
067bfb
 
067bfb
diff -up openssl-1.1.1c/crypto/poly1305/asm/poly1305-armv8.pl.arm-update openssl-1.1.1c/crypto/poly1305/asm/poly1305-armv8.pl
067bfb
--- openssl-1.1.1c/crypto/poly1305/asm/poly1305-armv8.pl.arm-update	2019-05-28 15:12:21.000000000 +0200
067bfb
+++ openssl-1.1.1c/crypto/poly1305/asm/poly1305-armv8.pl	2019-11-20 11:36:22.390506137 +0100
067bfb
@@ -29,6 +29,7 @@
067bfb
 # X-Gene	2.13/+68%	2.27
067bfb
 # Mongoose	1.77/+75%	1.12
067bfb
 # Kryo		2.70/+55%	1.13
067bfb
+# ThunderX2	1.17/+95%	1.36
067bfb
 #
067bfb
 # (*)	estimate based on resources availability is less than 1.0,
067bfb
 #	i.e. measured result is worse than expected, presumably binary
067bfb
diff -up openssl-1.1.1c/crypto/sha/asm/keccak1600-armv8.pl.arm-update openssl-1.1.1c/crypto/sha/asm/keccak1600-armv8.pl
067bfb
--- openssl-1.1.1c/crypto/sha/asm/keccak1600-armv8.pl.arm-update	2019-05-28 15:12:21.000000000 +0200
067bfb
+++ openssl-1.1.1c/crypto/sha/asm/keccak1600-armv8.pl	2019-11-20 11:36:22.390506137 +0100
067bfb
@@ -51,6 +51,7 @@
067bfb
 # Kryo		12
067bfb
 # Denver	7.8
067bfb
 # Apple A7	7.2
067bfb
+# ThunderX2	9.7
067bfb
 #
067bfb
 # (*)	Corresponds to SHA3-256. No improvement coefficients are listed
067bfb
 #	because they vary too much from compiler to compiler. Newer
067bfb
diff -up openssl-1.1.1c/crypto/sha/asm/sha1-armv8.pl.arm-update openssl-1.1.1c/crypto/sha/asm/sha1-armv8.pl
067bfb
--- openssl-1.1.1c/crypto/sha/asm/sha1-armv8.pl.arm-update	2019-05-28 15:12:21.000000000 +0200
067bfb
+++ openssl-1.1.1c/crypto/sha/asm/sha1-armv8.pl	2019-11-20 11:36:22.390506137 +0100
067bfb
@@ -27,6 +27,7 @@
067bfb
 # X-Gene				8.80 (+200%)
067bfb
 # Mongoose	2.05			6.50 (+160%)
067bfb
 # Kryo		1.88			8.00 (+90%)
067bfb
+# ThunderX2	2.64			6.36 (+150%)
067bfb
 #
067bfb
 # (*)	Software results are presented mostly for reference purposes.
067bfb
 # (**)	Keep in mind that Denver relies on binary translation, which
067bfb
diff -up openssl-1.1.1c/crypto/sha/asm/sha512-armv8.pl.arm-update openssl-1.1.1c/crypto/sha/asm/sha512-armv8.pl
067bfb
--- openssl-1.1.1c/crypto/sha/asm/sha512-armv8.pl.arm-update	2019-05-28 15:12:21.000000000 +0200
067bfb
+++ openssl-1.1.1c/crypto/sha/asm/sha512-armv8.pl	2019-11-20 11:36:22.390506137 +0100
067bfb
@@ -28,6 +28,7 @@
067bfb
 # X-Gene			20.0 (+100%)	12.8 (+300%(***))
067bfb
 # Mongoose	2.36		13.0 (+50%)	8.36 (+33%)
067bfb
 # Kryo		1.92		17.4 (+30%)	11.2 (+8%)
067bfb
+# ThunderX2	2.54		13.2 (+40%)	8.40 (+18%)
067bfb
 #
067bfb
 # (*)	Software SHA256 results are of lesser relevance, presented
067bfb
 #	mostly for informational purposes.