080143c
Upstream-Status: Backport [
080143c
    https://github.com/openssl/openssl/commit/f596bbe4da779b56eea34d96168b557d78e1149,
080143c
    https://github.com/openssl/openssl/commit/7e1f3ffcc5bc15fb9a12b9e3bb202f544c6ed5aa,
080143c
    hunks in crypto/ppccap.c from https://github.com/openssl/openssl/commit/f5485b97b6c9977c0d39c7669b9f97a879312447
080143c
]
080143c
diff --git a/crypto/chacha/asm/chachap10-ppc.pl b/crypto/chacha/asm/chachap10-ppc.pl
080143c
new file mode 100755
080143c
index 0000000..36e9a8d
080143c
--- /dev/null
080143c
+++ b/crypto/chacha/asm/chachap10-ppc.pl
080143c
@@ -0,0 +1,1288 @@
080143c
+#! /usr/bin/env perl
080143c
+# Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
080143c
+#
080143c
+# Licensed under the Apache License 2.0 (the "License").  You may not use
080143c
+# this file except in compliance with the License.  You can obtain a copy
080143c
+# in the file LICENSE in the source distribution or at
080143c
+# https://www.openssl.org/source/license.html
080143c
+
080143c
+#
080143c
+# ====================================================================
080143c
+# Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
080143c
+# project. The module is, however, dual licensed under OpenSSL and
080143c
+# CRYPTOGAMS licenses depending on where you obtain it. For further
080143c
+# details see http://www.openssl.org/~appro/cryptogams/.
080143c
+# ====================================================================
080143c
+#
080143c
+# October 2015
080143c
+#
080143c
+# ChaCha20 for PowerPC/AltiVec.
080143c
+#
080143c
+# June 2018
080143c
+#
080143c
+# Add VSX 2.07 code path. Original 3xAltiVec+1xIALU is well-suited for
080143c
+# processors that can't issue more than one vector instruction per
080143c
+# cycle. But POWER8 (and POWER9) can issue a pair, and vector-only 4x
080143c
+# interleave would perform better. Incidentally PowerISA 2.07 (first
080143c
+# implemented by POWER8) defined new usable instructions, hence 4xVSX
080143c
+# code path...
080143c
+#
080143c
+# Performance in cycles per byte out of large buffer.
080143c
+#
080143c
+#			IALU/gcc-4.x    3xAltiVec+1xIALU	4xVSX
080143c
+#
080143c
+# Freescale e300	13.6/+115%	-			-
080143c
+# PPC74x0/G4e		6.81/+310%	3.81			-
080143c
+# PPC970/G5		9.29/+160%	?			-
080143c
+# POWER7		8.62/+61%	3.35			-
080143c
+# POWER8		8.70/+51%	2.91			2.09
080143c
+# POWER9		8.80/+29%	4.44(*)			2.45(**)
080143c
+#
080143c
+# (*)	this is trade-off result, it's possible to improve it, but
080143c
+#	then it would negatively affect all others;
080143c
+# (**)	POWER9 seems to be "allergic" to mixing vector and integer
080143c
+#	instructions, which is why switch to vector-only code pays
080143c
+#	off that much;
080143c
+
080143c
+# $output is the last argument if it looks like a file (it has an extension)
080143c
+# $flavour is the first argument if it doesn't look like a file
080143c
+$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
080143c
+$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
080143c
+
080143c
+if ($flavour =~ /64/) {
080143c
+	$SIZE_T	=8;
080143c
+	$LRSAVE	=2*$SIZE_T;
080143c
+	$STU	="stdu";
080143c
+	$POP	="ld";
080143c
+	$PUSH	="std";
080143c
+	$UCMP	="cmpld";
080143c
+} elsif ($flavour =~ /32/) {
080143c
+	$SIZE_T	=4;
080143c
+	$LRSAVE	=$SIZE_T;
080143c
+	$STU	="stwu";
080143c
+	$POP	="lwz";
080143c
+	$PUSH	="stw";
080143c
+	$UCMP	="cmplw";
080143c
+} else { die "nonsense $flavour"; }
080143c
+
080143c
+$LITTLE_ENDIAN = ($flavour=~/le$/) ? 1 : 0;
080143c
+
080143c
+$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
080143c
+( $xlate="${dir}ppc-xlate.pl" and -f $xlate ) or
080143c
+( $xlate="${dir}../../perlasm/ppc-xlate.pl" and -f $xlate) or
080143c
+die "can't locate ppc-xlate.pl";
080143c
+
080143c
+open STDOUT,"| $^X $xlate $flavour \"$output\""
080143c
+    or die "can't call $xlate: $!";
080143c
+
080143c
+$LOCALS=6*$SIZE_T;
080143c
+$FRAME=$LOCALS+64+18*$SIZE_T;	# 64 is for local variables
080143c
+
080143c
+sub AUTOLOAD()		# thunk [simplified] x86-style perlasm
080143c
+{ my $opcode = $AUTOLOAD; $opcode =~ s/.*:://; $opcode =~ s/_/\./;
080143c
+    $code .= "\t$opcode\t".join(',',@_)."\n";
080143c
+}
080143c
+
080143c
+my $sp = "r1";
080143c
+
080143c
+my ($out,$inp,$len,$key,$ctr) = map("r$_",(3..7));
080143c
+
080143c
+
080143c
+{{{
080143c
+my ($xa0,$xa1,$xa2,$xa3, $xb0,$xb1,$xb2,$xb3,
080143c
+    $xc0,$xc1,$xc2,$xc3, $xd0,$xd1,$xd2,$xd3) = map("v$_",(0..15));
080143c
+my @K = map("v$_",(16..19));
080143c
+my $CTR = "v26";
080143c
+my ($xt0,$xt1,$xt2,$xt3) = map("v$_",(27..30));
080143c
+my ($sixteen,$twelve,$eight,$seven) = ($xt0,$xt1,$xt2,$xt3);
080143c
+my $beperm = "v31";
080143c
+
080143c
+my ($x00,$x10,$x20,$x30) = (0, map("r$_",(8..10)));
080143c
+
080143c
+my $FRAME=$LOCALS+64+7*16;	# 7*16 is for v26-v31 offload
080143c
+
080143c
+
080143c
+sub VSX_lane_ROUND_4x {
080143c
+my ($a0,$b0,$c0,$d0)=@_;
080143c
+my ($a1,$b1,$c1,$d1)=map(($_&~3)+(($_+1)&3),($a0,$b0,$c0,$d0));
080143c
+my ($a2,$b2,$c2,$d2)=map(($_&~3)+(($_+1)&3),($a1,$b1,$c1,$d1));
080143c
+my ($a3,$b3,$c3,$d3)=map(($_&~3)+(($_+1)&3),($a2,$b2,$c2,$d2));
080143c
+my @x=map("\"v$_\"",(0..15));
080143c
+
080143c
+	(
080143c
+	"&vadduwm	(@x[$a0],@x[$a0],@x[$b0])",	# Q1
080143c
+	 "&vadduwm	(@x[$a1],@x[$a1],@x[$b1])",	# Q2
080143c
+	  "&vadduwm	(@x[$a2],@x[$a2],@x[$b2])",	# Q3
080143c
+	   "&vadduwm	(@x[$a3],@x[$a3],@x[$b3])",	# Q4
080143c
+	"&vxor		(@x[$d0],@x[$d0],@x[$a0])",
080143c
+	 "&vxor		(@x[$d1],@x[$d1],@x[$a1])",
080143c
+	  "&vxor	(@x[$d2],@x[$d2],@x[$a2])",
080143c
+	   "&vxor	(@x[$d3],@x[$d3],@x[$a3])",
080143c
+	"&vrlw		(@x[$d0],@x[$d0],'$sixteen')",
080143c
+	 "&vrlw		(@x[$d1],@x[$d1],'$sixteen')",
080143c
+	  "&vrlw	(@x[$d2],@x[$d2],'$sixteen')",
080143c
+	   "&vrlw	(@x[$d3],@x[$d3],'$sixteen')",
080143c
+
080143c
+	"&vadduwm	(@x[$c0],@x[$c0],@x[$d0])",
080143c
+	 "&vadduwm	(@x[$c1],@x[$c1],@x[$d1])",
080143c
+	  "&vadduwm	(@x[$c2],@x[$c2],@x[$d2])",
080143c
+	   "&vadduwm	(@x[$c3],@x[$c3],@x[$d3])",
080143c
+	"&vxor		(@x[$b0],@x[$b0],@x[$c0])",
080143c
+	 "&vxor		(@x[$b1],@x[$b1],@x[$c1])",
080143c
+	  "&vxor	(@x[$b2],@x[$b2],@x[$c2])",
080143c
+	   "&vxor	(@x[$b3],@x[$b3],@x[$c3])",
080143c
+	"&vrlw		(@x[$b0],@x[$b0],'$twelve')",
080143c
+	 "&vrlw		(@x[$b1],@x[$b1],'$twelve')",
080143c
+	  "&vrlw	(@x[$b2],@x[$b2],'$twelve')",
080143c
+	   "&vrlw	(@x[$b3],@x[$b3],'$twelve')",
080143c
+
080143c
+	"&vadduwm	(@x[$a0],@x[$a0],@x[$b0])",
080143c
+	 "&vadduwm	(@x[$a1],@x[$a1],@x[$b1])",
080143c
+	  "&vadduwm	(@x[$a2],@x[$a2],@x[$b2])",
080143c
+	   "&vadduwm	(@x[$a3],@x[$a3],@x[$b3])",
080143c
+	"&vxor		(@x[$d0],@x[$d0],@x[$a0])",
080143c
+	 "&vxor		(@x[$d1],@x[$d1],@x[$a1])",
080143c
+	  "&vxor	(@x[$d2],@x[$d2],@x[$a2])",
080143c
+	   "&vxor	(@x[$d3],@x[$d3],@x[$a3])",
080143c
+	"&vrlw		(@x[$d0],@x[$d0],'$eight')",
080143c
+	 "&vrlw		(@x[$d1],@x[$d1],'$eight')",
080143c
+	  "&vrlw	(@x[$d2],@x[$d2],'$eight')",
080143c
+	   "&vrlw	(@x[$d3],@x[$d3],'$eight')",
080143c
+
080143c
+	"&vadduwm	(@x[$c0],@x[$c0],@x[$d0])",
080143c
+	 "&vadduwm	(@x[$c1],@x[$c1],@x[$d1])",
080143c
+	  "&vadduwm	(@x[$c2],@x[$c2],@x[$d2])",
080143c
+	   "&vadduwm	(@x[$c3],@x[$c3],@x[$d3])",
080143c
+	"&vxor		(@x[$b0],@x[$b0],@x[$c0])",
080143c
+	 "&vxor		(@x[$b1],@x[$b1],@x[$c1])",
080143c
+	  "&vxor	(@x[$b2],@x[$b2],@x[$c2])",
080143c
+	   "&vxor	(@x[$b3],@x[$b3],@x[$c3])",
080143c
+	"&vrlw		(@x[$b0],@x[$b0],'$seven')",
080143c
+	 "&vrlw		(@x[$b1],@x[$b1],'$seven')",
080143c
+	  "&vrlw	(@x[$b2],@x[$b2],'$seven')",
080143c
+	   "&vrlw	(@x[$b3],@x[$b3],'$seven')"
080143c
+	);
080143c
+}
080143c
+
080143c
+$code.=<<___;
080143c
+
080143c
+.globl	.ChaCha20_ctr32_vsx_p10
080143c
+.align	5
080143c
+.ChaCha20_ctr32_vsx_p10:
080143c
+	${UCMP}i $len,255
080143c
+	bgt 	ChaCha20_ctr32_vsx_8x
080143c
+	$STU	$sp,-$FRAME($sp)
080143c
+	mflr	r0
080143c
+	li	r10,`15+$LOCALS+64`
080143c
+	li	r11,`31+$LOCALS+64`
080143c
+	mfspr	r12,256
080143c
+	stvx	v26,r10,$sp
080143c
+	addi	r10,r10,32
080143c
+	stvx	v27,r11,$sp
080143c
+	addi	r11,r11,32
080143c
+	stvx	v28,r10,$sp
080143c
+	addi	r10,r10,32
080143c
+	stvx	v29,r11,$sp
080143c
+	addi	r11,r11,32
080143c
+	stvx	v30,r10,$sp
080143c
+	stvx	v31,r11,$sp
080143c
+	stw	r12,`$FRAME-4`($sp)		# save vrsave
080143c
+	li	r12,-4096+63
080143c
+	$PUSH	r0, `$FRAME+$LRSAVE`($sp)
080143c
+	mtspr	256,r12				# preserve 29 AltiVec registers
080143c
+
080143c
+	bl	Lconsts				# returns pointer Lsigma in r12
080143c
+	lvx_4w	@K[0],0,r12			# load sigma
080143c
+	addi	r12,r12,0x70
080143c
+	li	$x10,16
080143c
+	li	$x20,32
080143c
+	li	$x30,48
080143c
+	li	r11,64
080143c
+
080143c
+	lvx_4w	@K[1],0,$key			# load key
080143c
+	lvx_4w	@K[2],$x10,$key
080143c
+	lvx_4w	@K[3],0,$ctr			# load counter
080143c
+
080143c
+	vxor	$xt0,$xt0,$xt0
080143c
+	lvx_4w	$xt1,r11,r12
080143c
+	vspltw	$CTR,@K[3],0
080143c
+	vsldoi	@K[3],@K[3],$xt0,4
080143c
+	vsldoi	@K[3],$xt0,@K[3],12		# clear @K[3].word[0]
080143c
+	vadduwm	$CTR,$CTR,$xt1
080143c
+
080143c
+	be?lvsl	$beperm,0,$x10			# 0x00..0f
080143c
+	be?vspltisb $xt0,3			# 0x03..03
080143c
+	be?vxor	$beperm,$beperm,$xt0		# swap bytes within words
080143c
+
080143c
+	li	r0,10				# inner loop counter
080143c
+	mtctr	r0
080143c
+	b	Loop_outer_vsx
080143c
+
080143c
+.align	5
080143c
+Loop_outer_vsx:
080143c
+	lvx	$xa0,$x00,r12			# load [smashed] sigma
080143c
+	lvx	$xa1,$x10,r12
080143c
+	lvx	$xa2,$x20,r12
080143c
+	lvx	$xa3,$x30,r12
080143c
+
080143c
+	vspltw	$xb0,@K[1],0			# smash the key
080143c
+	vspltw	$xb1,@K[1],1
080143c
+	vspltw	$xb2,@K[1],2
080143c
+	vspltw	$xb3,@K[1],3
080143c
+
080143c
+	vspltw	$xc0,@K[2],0
080143c
+	vspltw	$xc1,@K[2],1
080143c
+	vspltw	$xc2,@K[2],2
080143c
+	vspltw	$xc3,@K[2],3
080143c
+
080143c
+	vmr	$xd0,$CTR			# smash the counter
080143c
+	vspltw	$xd1,@K[3],1
080143c
+	vspltw	$xd2,@K[3],2
080143c
+	vspltw	$xd3,@K[3],3
080143c
+
080143c
+	vspltisw $sixteen,-16			# synthesize constants
080143c
+	vspltisw $twelve,12
080143c
+	vspltisw $eight,8
080143c
+	vspltisw $seven,7
080143c
+
080143c
+Loop_vsx_4x:
080143c
+___
080143c
+	foreach (&VSX_lane_ROUND_4x(0, 4, 8,12)) { eval; }
080143c
+	foreach (&VSX_lane_ROUND_4x(0, 5,10,15)) { eval; }
080143c
+$code.=<<___;
080143c
+
080143c
+	bdnz	Loop_vsx_4x
080143c
+
080143c
+	vadduwm	$xd0,$xd0,$CTR
080143c
+
080143c
+	vmrgew	$xt0,$xa0,$xa1			# transpose data
080143c
+	vmrgew	$xt1,$xa2,$xa3
080143c
+	vmrgow	$xa0,$xa0,$xa1
080143c
+	vmrgow	$xa2,$xa2,$xa3
080143c
+	vmrgew	$xt2,$xb0,$xb1
080143c
+	vmrgew	$xt3,$xb2,$xb3
080143c
+	vpermdi	$xa1,$xa0,$xa2,0b00
080143c
+	vpermdi	$xa3,$xa0,$xa2,0b11
080143c
+	vpermdi	$xa0,$xt0,$xt1,0b00
080143c
+	vpermdi	$xa2,$xt0,$xt1,0b11
080143c
+
080143c
+	vmrgow	$xb0,$xb0,$xb1
080143c
+	vmrgow	$xb2,$xb2,$xb3
080143c
+	vmrgew	$xt0,$xc0,$xc1
080143c
+	vmrgew	$xt1,$xc2,$xc3
080143c
+	vpermdi	$xb1,$xb0,$xb2,0b00
080143c
+	vpermdi	$xb3,$xb0,$xb2,0b11
080143c
+	vpermdi	$xb0,$xt2,$xt3,0b00
080143c
+	vpermdi	$xb2,$xt2,$xt3,0b11
080143c
+
080143c
+	vmrgow	$xc0,$xc0,$xc1
080143c
+	vmrgow	$xc2,$xc2,$xc3
080143c
+	vmrgew	$xt2,$xd0,$xd1
080143c
+	vmrgew	$xt3,$xd2,$xd3
080143c
+	vpermdi	$xc1,$xc0,$xc2,0b00
080143c
+	vpermdi	$xc3,$xc0,$xc2,0b11
080143c
+	vpermdi	$xc0,$xt0,$xt1,0b00
080143c
+	vpermdi	$xc2,$xt0,$xt1,0b11
080143c
+
080143c
+	vmrgow	$xd0,$xd0,$xd1
080143c
+	vmrgow	$xd2,$xd2,$xd3
080143c
+	vspltisw $xt0,4
080143c
+	vadduwm  $CTR,$CTR,$xt0		# next counter value
080143c
+	vpermdi	$xd1,$xd0,$xd2,0b00
080143c
+	vpermdi	$xd3,$xd0,$xd2,0b11
080143c
+	vpermdi	$xd0,$xt2,$xt3,0b00
080143c
+	vpermdi	$xd2,$xt2,$xt3,0b11
080143c
+
080143c
+	vadduwm	$xa0,$xa0,@K[0]
080143c
+	vadduwm	$xb0,$xb0,@K[1]
080143c
+	vadduwm	$xc0,$xc0,@K[2]
080143c
+	vadduwm	$xd0,$xd0,@K[3]
080143c
+
080143c
+	be?vperm $xa0,$xa0,$xa0,$beperm
080143c
+	be?vperm $xb0,$xb0,$xb0,$beperm
080143c
+	be?vperm $xc0,$xc0,$xc0,$beperm
080143c
+	be?vperm $xd0,$xd0,$xd0,$beperm
080143c
+
080143c
+	${UCMP}i $len,0x40
080143c
+	blt	Ltail_vsx
080143c
+
080143c
+	lvx_4w	$xt0,$x00,$inp
080143c
+	lvx_4w	$xt1,$x10,$inp
080143c
+	lvx_4w	$xt2,$x20,$inp
080143c
+	lvx_4w	$xt3,$x30,$inp
080143c
+
080143c
+	vxor	$xt0,$xt0,$xa0
080143c
+	vxor	$xt1,$xt1,$xb0
080143c
+	vxor	$xt2,$xt2,$xc0
080143c
+	vxor	$xt3,$xt3,$xd0
080143c
+
080143c
+	stvx_4w	$xt0,$x00,$out
080143c
+	stvx_4w	$xt1,$x10,$out
080143c
+	addi	$inp,$inp,0x40
080143c
+	stvx_4w	$xt2,$x20,$out
080143c
+	subi	$len,$len,0x40
080143c
+	stvx_4w	$xt3,$x30,$out
080143c
+	addi	$out,$out,0x40
080143c
+	beq	Ldone_vsx
080143c
+
080143c
+	vadduwm	$xa0,$xa1,@K[0]
080143c
+	vadduwm	$xb0,$xb1,@K[1]
080143c
+	vadduwm	$xc0,$xc1,@K[2]
080143c
+	vadduwm	$xd0,$xd1,@K[3]
080143c
+
080143c
+	be?vperm $xa0,$xa0,$xa0,$beperm
080143c
+	be?vperm $xb0,$xb0,$xb0,$beperm
080143c
+	be?vperm $xc0,$xc0,$xc0,$beperm
080143c
+	be?vperm $xd0,$xd0,$xd0,$beperm
080143c
+
080143c
+	${UCMP}i $len,0x40
080143c
+	blt	Ltail_vsx
080143c
+
080143c
+	lvx_4w	$xt0,$x00,$inp
080143c
+	lvx_4w	$xt1,$x10,$inp
080143c
+	lvx_4w	$xt2,$x20,$inp
080143c
+	lvx_4w	$xt3,$x30,$inp
080143c
+
080143c
+	vxor	$xt0,$xt0,$xa0
080143c
+	vxor	$xt1,$xt1,$xb0
080143c
+	vxor	$xt2,$xt2,$xc0
080143c
+	vxor	$xt3,$xt3,$xd0
080143c
+
080143c
+	stvx_4w	$xt0,$x00,$out
080143c
+	stvx_4w	$xt1,$x10,$out
080143c
+	addi	$inp,$inp,0x40
080143c
+	stvx_4w	$xt2,$x20,$out
080143c
+	subi	$len,$len,0x40
080143c
+	stvx_4w	$xt3,$x30,$out
080143c
+	addi	$out,$out,0x40
080143c
+	beq	Ldone_vsx
080143c
+
080143c
+	vadduwm	$xa0,$xa2,@K[0]
080143c
+	vadduwm	$xb0,$xb2,@K[1]
080143c
+	vadduwm	$xc0,$xc2,@K[2]
080143c
+	vadduwm	$xd0,$xd2,@K[3]
080143c
+
080143c
+	be?vperm $xa0,$xa0,$xa0,$beperm
080143c
+	be?vperm $xb0,$xb0,$xb0,$beperm
080143c
+	be?vperm $xc0,$xc0,$xc0,$beperm
080143c
+	be?vperm $xd0,$xd0,$xd0,$beperm
080143c
+
080143c
+	${UCMP}i $len,0x40
080143c
+	blt	Ltail_vsx
080143c
+
080143c
+	lvx_4w	$xt0,$x00,$inp
080143c
+	lvx_4w	$xt1,$x10,$inp
080143c
+	lvx_4w	$xt2,$x20,$inp
080143c
+	lvx_4w	$xt3,$x30,$inp
080143c
+
080143c
+	vxor	$xt0,$xt0,$xa0
080143c
+	vxor	$xt1,$xt1,$xb0
080143c
+	vxor	$xt2,$xt2,$xc0
080143c
+	vxor	$xt3,$xt3,$xd0
080143c
+
080143c
+	stvx_4w	$xt0,$x00,$out
080143c
+	stvx_4w	$xt1,$x10,$out
080143c
+	addi	$inp,$inp,0x40
080143c
+	stvx_4w	$xt2,$x20,$out
080143c
+	subi	$len,$len,0x40
080143c
+	stvx_4w	$xt3,$x30,$out
080143c
+	addi	$out,$out,0x40
080143c
+	beq	Ldone_vsx
080143c
+
080143c
+	vadduwm	$xa0,$xa3,@K[0]
080143c
+	vadduwm	$xb0,$xb3,@K[1]
080143c
+	vadduwm	$xc0,$xc3,@K[2]
080143c
+	vadduwm	$xd0,$xd3,@K[3]
080143c
+
080143c
+	be?vperm $xa0,$xa0,$xa0,$beperm
080143c
+	be?vperm $xb0,$xb0,$xb0,$beperm
080143c
+	be?vperm $xc0,$xc0,$xc0,$beperm
080143c
+	be?vperm $xd0,$xd0,$xd0,$beperm
080143c
+
080143c
+	${UCMP}i $len,0x40
080143c
+	blt	Ltail_vsx
080143c
+
080143c
+	lvx_4w	$xt0,$x00,$inp
080143c
+	lvx_4w	$xt1,$x10,$inp
080143c
+	lvx_4w	$xt2,$x20,$inp
080143c
+	lvx_4w	$xt3,$x30,$inp
080143c
+
080143c
+	vxor	$xt0,$xt0,$xa0
080143c
+	vxor	$xt1,$xt1,$xb0
080143c
+	vxor	$xt2,$xt2,$xc0
080143c
+	vxor	$xt3,$xt3,$xd0
080143c
+
080143c
+	stvx_4w	$xt0,$x00,$out
080143c
+	stvx_4w	$xt1,$x10,$out
080143c
+	addi	$inp,$inp,0x40
080143c
+	stvx_4w	$xt2,$x20,$out
080143c
+	subi	$len,$len,0x40
080143c
+	stvx_4w	$xt3,$x30,$out
080143c
+	addi	$out,$out,0x40
080143c
+	mtctr	r0
080143c
+	bne	Loop_outer_vsx
080143c
+
080143c
+Ldone_vsx:
080143c
+	lwz	r12,`$FRAME-4`($sp)		# pull vrsave
080143c
+	li	r10,`15+$LOCALS+64`
080143c
+	li	r11,`31+$LOCALS+64`
080143c
+	$POP	r0, `$FRAME+$LRSAVE`($sp)
080143c
+	mtspr	256,r12				# restore vrsave
080143c
+	lvx	v26,r10,$sp
080143c
+	addi	r10,r10,32
080143c
+	lvx	v27,r11,$sp
080143c
+	addi	r11,r11,32
080143c
+	lvx	v28,r10,$sp
080143c
+	addi	r10,r10,32
080143c
+	lvx	v29,r11,$sp
080143c
+	addi	r11,r11,32
080143c
+	lvx	v30,r10,$sp
080143c
+	lvx	v31,r11,$sp
080143c
+	mtlr	r0
080143c
+	addi	$sp,$sp,$FRAME
080143c
+	blr
080143c
+
080143c
+.align	4
080143c
+Ltail_vsx:
080143c
+	addi	r11,$sp,$LOCALS
080143c
+	mtctr	$len
080143c
+	stvx_4w	$xa0,$x00,r11			# offload block to stack
080143c
+	stvx_4w	$xb0,$x10,r11
080143c
+	stvx_4w	$xc0,$x20,r11
080143c
+	stvx_4w	$xd0,$x30,r11
080143c
+	subi	r12,r11,1			# prepare for *++ptr
080143c
+	subi	$inp,$inp,1
080143c
+	subi	$out,$out,1
080143c
+
080143c
+Loop_tail_vsx:
080143c
+	lbzu	r6,1(r12)
080143c
+	lbzu	r7,1($inp)
080143c
+	xor	r6,r6,r7
080143c
+	stbu	r6,1($out)
080143c
+	bdnz	Loop_tail_vsx
080143c
+
080143c
+	stvx_4w	$K[0],$x00,r11			# wipe copy of the block
080143c
+	stvx_4w	$K[0],$x10,r11
080143c
+	stvx_4w	$K[0],$x20,r11
080143c
+	stvx_4w	$K[0],$x30,r11
080143c
+
080143c
+	b	Ldone_vsx
080143c
+	.long	0
080143c
+	.byte	0,12,0x04,1,0x80,0,5,0
080143c
+	.long	0
080143c
+.size	.ChaCha20_ctr32_vsx_p10,.-.ChaCha20_ctr32_vsx_p10
080143c
+___
080143c
+}}}
080143c
+
080143c
+##This is 8 block in parallel implementation. The heart of chacha round uses vector instruction that has access to
080143c
+# vsr[32+X]. To perform the 8 parallel block we tend to use all 32 register to hold the 8 block info.
080143c
+# WE need to store few register value on side, so we can use VSR{32+X} for few vector instructions used in round op and hold intermediate value.
080143c
+# WE use the VSR[0]-VSR[31] for holding intermediate value and perform 8 block in parallel.
080143c
+#
080143c
+{{{
080143c
+#### ($out,$inp,$len,$key,$ctr) = map("r$_",(3..7));
080143c
+my ($xa0,$xa1,$xa2,$xa3, $xb0,$xb1,$xb2,$xb3,
080143c
+    $xc0,$xc1,$xc2,$xc3, $xd0,$xd1,$xd2,$xd3,
080143c
+    $xa4,$xa5,$xa6,$xa7, $xb4,$xb5,$xb6,$xb7,
080143c
+    $xc4,$xc5,$xc6,$xc7, $xd4,$xd5,$xd6,$xd7) = map("v$_",(0..31));
080143c
+my ($xcn4,$xcn5,$xcn6,$xcn7, $xdn4,$xdn5,$xdn6,$xdn7) = map("v$_",(8..15));
080143c
+my ($xan0,$xbn0,$xcn0,$xdn0) = map("v$_",(0..3));
080143c
+my @K = map("v$_",27,(24..26));
080143c
+my ($xt0,$xt1,$xt2,$xt3,$xt4) = map("v$_",23,(28..31));
080143c
+my $xr0 = "v4";
080143c
+my $CTR0 = "v22";
080143c
+my $CTR1 = "v5";
080143c
+my $beperm = "v31";
080143c
+my ($x00,$x10,$x20,$x30) = (0, map("r$_",(8..10)));
080143c
+my ($xv0,$xv1,$xv2,$xv3,$xv4,$xv5,$xv6,$xv7) = map("v$_",(0..7));
080143c
+my ($xv8,$xv9,$xv10,$xv11,$xv12,$xv13,$xv14,$xv15,$xv16,$xv17) = map("v$_",(8..17));
080143c
+my ($xv18,$xv19,$xv20,$xv21) = map("v$_",(18..21));
080143c
+my ($xv22,$xv23,$xv24,$xv25,$xv26) = map("v$_",(22..26));
080143c
+
080143c
+my $FRAME=$LOCALS+64+9*16;	# 8*16 is for v24-v31 offload
080143c
+
080143c
+sub VSX_lane_ROUND_8x {
080143c
+my ($a0,$b0,$c0,$d0,$a4,$b4,$c4,$d4)=@_;
080143c
+my ($a1,$b1,$c1,$d1)=map(($_&~3)+(($_+1)&3),($a0,$b0,$c0,$d0));
080143c
+my ($a2,$b2,$c2,$d2)=map(($_&~3)+(($_+1)&3),($a1,$b1,$c1,$d1));
080143c
+my ($a3,$b3,$c3,$d3)=map(($_&~3)+(($_+1)&3),($a2,$b2,$c2,$d2));
080143c
+my ($a5,$b5,$c5,$d5)=map(($_&~3)+(($_+1)&3),($a4,$b4,$c4,$d4));
080143c
+my ($a6,$b6,$c6,$d6)=map(($_&~3)+(($_+1)&3),($a5,$b5,$c5,$d5));
080143c
+my ($a7,$b7,$c7,$d7)=map(($_&~3)+(($_+1)&3),($a6,$b6,$c6,$d6));
080143c
+my ($xv8,$xv9,$xv10,$xv11,$xv12,$xv13,$xv14,$xv15,$xv16,$xv17) = map("\"v$_\"",(8..17));
080143c
+my @x=map("\"v$_\"",(0..31));
080143c
+
080143c
+	(
080143c
+	"&vxxlor        ($xv15 ,@x[$c7],@x[$c7])",      #copy v30 to v13
080143c
+	"&vxxlorc       (@x[$c7], $xv9,$xv9)",
080143c
+
080143c
+	"&vadduwm	(@x[$a0],@x[$a0],@x[$b0])",	# Q1
080143c
+	 "&vadduwm	(@x[$a1],@x[$a1],@x[$b1])",	# Q2
080143c
+	  "&vadduwm	(@x[$a2],@x[$a2],@x[$b2])",	# Q3
080143c
+	   "&vadduwm	(@x[$a3],@x[$a3],@x[$b3])",	# Q4
080143c
+	"&vadduwm	(@x[$a4],@x[$a4],@x[$b4])",	# Q1
080143c
+	 "&vadduwm	(@x[$a5],@x[$a5],@x[$b5])",	# Q2
080143c
+	  "&vadduwm	(@x[$a6],@x[$a6],@x[$b6])",	# Q3
080143c
+	   "&vadduwm	(@x[$a7],@x[$a7],@x[$b7])",	# Q4
080143c
+
080143c
+	"&vxor		(@x[$d0],@x[$d0],@x[$a0])",
080143c
+	 "&vxor		(@x[$d1],@x[$d1],@x[$a1])",
080143c
+	  "&vxor	(@x[$d2],@x[$d2],@x[$a2])",
080143c
+	   "&vxor	(@x[$d3],@x[$d3],@x[$a3])",
080143c
+	"&vxor		(@x[$d4],@x[$d4],@x[$a4])",
080143c
+	 "&vxor		(@x[$d5],@x[$d5],@x[$a5])",
080143c
+	  "&vxor	(@x[$d6],@x[$d6],@x[$a6])",
080143c
+	   "&vxor	(@x[$d7],@x[$d7],@x[$a7])",
080143c
+
080143c
+	"&vrlw		(@x[$d0],@x[$d0],@x[$c7])",
080143c
+	 "&vrlw		(@x[$d1],@x[$d1],@x[$c7])",
080143c
+	  "&vrlw	(@x[$d2],@x[$d2],@x[$c7])",
080143c
+	   "&vrlw	(@x[$d3],@x[$d3],@x[$c7])",
080143c
+	"&vrlw		(@x[$d4],@x[$d4],@x[$c7])",
080143c
+	 "&vrlw		(@x[$d5],@x[$d5],@x[$c7])",
080143c
+	  "&vrlw	(@x[$d6],@x[$d6],@x[$c7])",
080143c
+	   "&vrlw	(@x[$d7],@x[$d7],@x[$c7])",
080143c
+
080143c
+	"&vxxlor        ($xv13 ,@x[$a7],@x[$a7])",
080143c
+	"&vxxlorc       (@x[$c7], $xv15,$xv15)",
080143c
+	"&vxxlorc       (@x[$a7], $xv10,$xv10)",
080143c
+
080143c
+	"&vadduwm	(@x[$c0],@x[$c0],@x[$d0])",
080143c
+	 "&vadduwm	(@x[$c1],@x[$c1],@x[$d1])",
080143c
+	  "&vadduwm	(@x[$c2],@x[$c2],@x[$d2])",
080143c
+	   "&vadduwm	(@x[$c3],@x[$c3],@x[$d3])",
080143c
+	"&vadduwm	(@x[$c4],@x[$c4],@x[$d4])",
080143c
+	 "&vadduwm	(@x[$c5],@x[$c5],@x[$d5])",
080143c
+	  "&vadduwm	(@x[$c6],@x[$c6],@x[$d6])",
080143c
+	   "&vadduwm	(@x[$c7],@x[$c7],@x[$d7])",
080143c
+
080143c
+	"&vxor		(@x[$b0],@x[$b0],@x[$c0])",
080143c
+	 "&vxor		(@x[$b1],@x[$b1],@x[$c1])",
080143c
+	  "&vxor	(@x[$b2],@x[$b2],@x[$c2])",
080143c
+	   "&vxor	(@x[$b3],@x[$b3],@x[$c3])",
080143c
+	"&vxor		(@x[$b4],@x[$b4],@x[$c4])",
080143c
+	 "&vxor		(@x[$b5],@x[$b5],@x[$c5])",
080143c
+	  "&vxor	(@x[$b6],@x[$b6],@x[$c6])",
080143c
+	   "&vxor	(@x[$b7],@x[$b7],@x[$c7])",
080143c
+
080143c
+	"&vrlw		(@x[$b0],@x[$b0],@x[$a7])",
080143c
+	 "&vrlw		(@x[$b1],@x[$b1],@x[$a7])",
080143c
+	  "&vrlw	(@x[$b2],@x[$b2],@x[$a7])",
080143c
+	   "&vrlw	(@x[$b3],@x[$b3],@x[$a7])",
080143c
+	"&vrlw		(@x[$b4],@x[$b4],@x[$a7])",
080143c
+	 "&vrlw		(@x[$b5],@x[$b5],@x[$a7])",
080143c
+	  "&vrlw	(@x[$b6],@x[$b6],@x[$a7])",
080143c
+	   "&vrlw	(@x[$b7],@x[$b7],@x[$a7])",
080143c
+
080143c
+	"&vxxlorc       (@x[$a7], $xv13,$xv13)",
080143c
+	"&vxxlor	($xv15 ,@x[$c7],@x[$c7])",
080143c
+	"&vxxlorc       (@x[$c7], $xv11,$xv11)",
080143c
+
080143c
+
080143c
+	"&vadduwm	(@x[$a0],@x[$a0],@x[$b0])",
080143c
+	 "&vadduwm	(@x[$a1],@x[$a1],@x[$b1])",
080143c
+	  "&vadduwm	(@x[$a2],@x[$a2],@x[$b2])",
080143c
+	   "&vadduwm	(@x[$a3],@x[$a3],@x[$b3])",
080143c
+	"&vadduwm	(@x[$a4],@x[$a4],@x[$b4])",
080143c
+	 "&vadduwm	(@x[$a5],@x[$a5],@x[$b5])",
080143c
+	  "&vadduwm	(@x[$a6],@x[$a6],@x[$b6])",
080143c
+	   "&vadduwm	(@x[$a7],@x[$a7],@x[$b7])",
080143c
+
080143c
+	"&vxor		(@x[$d0],@x[$d0],@x[$a0])",
080143c
+	 "&vxor		(@x[$d1],@x[$d1],@x[$a1])",
080143c
+	  "&vxor	(@x[$d2],@x[$d2],@x[$a2])",
080143c
+	   "&vxor	(@x[$d3],@x[$d3],@x[$a3])",
080143c
+	"&vxor		(@x[$d4],@x[$d4],@x[$a4])",
080143c
+	 "&vxor		(@x[$d5],@x[$d5],@x[$a5])",
080143c
+	  "&vxor	(@x[$d6],@x[$d6],@x[$a6])",
080143c
+	   "&vxor	(@x[$d7],@x[$d7],@x[$a7])",
080143c
+
080143c
+	"&vrlw		(@x[$d0],@x[$d0],@x[$c7])",
080143c
+	 "&vrlw		(@x[$d1],@x[$d1],@x[$c7])",
080143c
+	  "&vrlw	(@x[$d2],@x[$d2],@x[$c7])",
080143c
+	   "&vrlw	(@x[$d3],@x[$d3],@x[$c7])",
080143c
+	"&vrlw		(@x[$d4],@x[$d4],@x[$c7])",
080143c
+	 "&vrlw		(@x[$d5],@x[$d5],@x[$c7])",
080143c
+	  "&vrlw	(@x[$d6],@x[$d6],@x[$c7])",
080143c
+	   "&vrlw	(@x[$d7],@x[$d7],@x[$c7])",
080143c
+
080143c
+	"&vxxlorc       (@x[$c7], $xv15,$xv15)",
080143c
+	"&vxxlor        ($xv13 ,@x[$a7],@x[$a7])",
080143c
+	"&vxxlorc       (@x[$a7], $xv12,$xv12)",
080143c
+
080143c
+	"&vadduwm	(@x[$c0],@x[$c0],@x[$d0])",
080143c
+	 "&vadduwm	(@x[$c1],@x[$c1],@x[$d1])",
080143c
+	  "&vadduwm	(@x[$c2],@x[$c2],@x[$d2])",
080143c
+	   "&vadduwm	(@x[$c3],@x[$c3],@x[$d3])",
080143c
+	"&vadduwm	(@x[$c4],@x[$c4],@x[$d4])",
080143c
+	 "&vadduwm	(@x[$c5],@x[$c5],@x[$d5])",
080143c
+	  "&vadduwm	(@x[$c6],@x[$c6],@x[$d6])",
080143c
+	   "&vadduwm	(@x[$c7],@x[$c7],@x[$d7])",
080143c
+	"&vxor		(@x[$b0],@x[$b0],@x[$c0])",
080143c
+	 "&vxor		(@x[$b1],@x[$b1],@x[$c1])",
080143c
+	  "&vxor	(@x[$b2],@x[$b2],@x[$c2])",
080143c
+	   "&vxor	(@x[$b3],@x[$b3],@x[$c3])",
080143c
+	"&vxor		(@x[$b4],@x[$b4],@x[$c4])",
080143c
+	 "&vxor		(@x[$b5],@x[$b5],@x[$c5])",
080143c
+	  "&vxor	(@x[$b6],@x[$b6],@x[$c6])",
080143c
+	   "&vxor	(@x[$b7],@x[$b7],@x[$c7])",
080143c
+	"&vrlw		(@x[$b0],@x[$b0],@x[$a7])",
080143c
+	 "&vrlw		(@x[$b1],@x[$b1],@x[$a7])",
080143c
+	  "&vrlw	(@x[$b2],@x[$b2],@x[$a7])",
080143c
+	   "&vrlw	(@x[$b3],@x[$b3],@x[$a7])",
080143c
+	"&vrlw		(@x[$b4],@x[$b4],@x[$a7])",
080143c
+	 "&vrlw		(@x[$b5],@x[$b5],@x[$a7])",
080143c
+	  "&vrlw	(@x[$b6],@x[$b6],@x[$a7])",
080143c
+	   "&vrlw	(@x[$b7],@x[$b7],@x[$a7])",
080143c
+
080143c
+	"&vxxlorc       (@x[$a7], $xv13,$xv13)",
080143c
+	);
080143c
+}
080143c
+
080143c
+$code.=<<___;
080143c
+
080143c
+.globl	.ChaCha20_ctr32_vsx_8x
080143c
+.align	5
080143c
+.ChaCha20_ctr32_vsx_8x:
080143c
+	$STU	$sp,-$FRAME($sp)
080143c
+	mflr	r0
080143c
+	li	r10,`15+$LOCALS+64`
080143c
+	li	r11,`31+$LOCALS+64`
080143c
+	mfspr	r12,256
080143c
+	stvx	v24,r10,$sp
080143c
+	addi	r10,r10,32
080143c
+	stvx	v25,r11,$sp
080143c
+	addi	r11,r11,32
080143c
+	stvx	v26,r10,$sp
080143c
+	addi	r10,r10,32
080143c
+	stvx	v27,r11,$sp
080143c
+	addi	r11,r11,32
080143c
+	stvx	v28,r10,$sp
080143c
+	addi	r10,r10,32
080143c
+	stvx	v29,r11,$sp
080143c
+	addi	r11,r11,32
080143c
+	stvx	v30,r10,$sp
080143c
+	stvx	v31,r11,$sp
080143c
+	stw	r12,`$FRAME-4`($sp)		# save vrsave
080143c
+	li	r12,-4096+63
080143c
+	$PUSH	r0, `$FRAME+$LRSAVE`($sp)
080143c
+	mtspr	256,r12				# preserve 29 AltiVec registers
080143c
+
080143c
+	bl	Lconsts				# returns pointer Lsigma in r12
080143c
+
080143c
+	lvx_4w	@K[0],0,r12			# load sigma
080143c
+	addi	r12,r12,0x70
080143c
+	li	$x10,16
080143c
+	li	$x20,32
080143c
+	li	$x30,48
080143c
+	li	r11,64
080143c
+
080143c
+	vspltisw $xa4,-16			# synthesize constants
080143c
+	vspltisw $xb4,12			# synthesize constants
080143c
+	vspltisw $xc4,8			# synthesize constants
080143c
+	vspltisw $xd4,7			# synthesize constants
080143c
+
080143c
+	lvx	$xa0,$x00,r12			# load [smashed] sigma
080143c
+	lvx	$xa1,$x10,r12
080143c
+	lvx	$xa2,$x20,r12
080143c
+	lvx	$xa3,$x30,r12
080143c
+
080143c
+	vxxlor	$xv9   ,$xa4,$xa4               #save shift val in vr9-12
080143c
+	vxxlor	$xv10  ,$xb4,$xb4
080143c
+	vxxlor	$xv11  ,$xc4,$xc4
080143c
+	vxxlor	$xv12  ,$xd4,$xd4
080143c
+	vxxlor	$xv22  ,$xa0,$xa0               #save sigma in vr22-25
080143c
+	vxxlor	$xv23  ,$xa1,$xa1
080143c
+	vxxlor	$xv24  ,$xa2,$xa2
080143c
+	vxxlor	$xv25  ,$xa3,$xa3
080143c
+
080143c
+	lvx_4w	@K[1],0,$key			# load key
080143c
+	lvx_4w	@K[2],$x10,$key
080143c
+	lvx_4w	@K[3],0,$ctr			# load counter
080143c
+	vspltisw $xt3,4
080143c
+
080143c
+
080143c
+	vxor	$xt2,$xt2,$xt2
080143c
+	lvx_4w	$xt1,r11,r12
080143c
+	vspltw	$xa2,@K[3],0			#save the original count after spltw
080143c
+	vsldoi	@K[3],@K[3],$xt2,4
080143c
+	vsldoi	@K[3],$xt2,@K[3],12		# clear @K[3].word[0]
080143c
+	vadduwm	$xt1,$xa2,$xt1
080143c
+	vadduwm $xt3,$xt1,$xt3     		# next counter value
080143c
+	vspltw	$xa0,@K[2],2                    # save the K[2] spltw 2 and save v8.
080143c
+
080143c
+	be?lvsl	  $beperm,0,$x10			# 0x00..0f
080143c
+	be?vspltisb $xt0,3			# 0x03..03
080143c
+	be?vxor   $beperm,$beperm,$xt0		# swap bytes within words
080143c
+	be?vxxlor $xv26 ,$beperm,$beperm
080143c
+
080143c
+	vxxlor	$xv0 ,@K[0],@K[0]               # K0,k1,k2 to vr0,1,2
080143c
+	vxxlor	$xv1 ,@K[1],@K[1]
080143c
+	vxxlor	$xv2 ,@K[2],@K[2]
080143c
+	vxxlor	$xv3 ,@K[3],@K[3]
080143c
+	vxxlor	$xv4 ,$xt1,$xt1                #CTR ->4, CTR+4-> 5
080143c
+	vxxlor	$xv5 ,$xt3,$xt3
080143c
+	vxxlor	$xv8 ,$xa0,$xa0
080143c
+
080143c
+	li	r0,10				# inner loop counter
080143c
+	mtctr	r0
080143c
+	b	Loop_outer_vsx_8x
080143c
+
080143c
+.align	5
080143c
+Loop_outer_vsx_8x:
080143c
+	vxxlorc	$xa0,$xv22,$xv22	        # load [smashed] sigma
080143c
+	vxxlorc	$xa1,$xv23,$xv23
080143c
+	vxxlorc	$xa2,$xv24,$xv24
080143c
+	vxxlorc	$xa3,$xv25,$xv25
080143c
+	vxxlorc	$xa4,$xv22,$xv22
080143c
+	vxxlorc	$xa5,$xv23,$xv23
080143c
+	vxxlorc	$xa6,$xv24,$xv24
080143c
+	vxxlorc	$xa7,$xv25,$xv25
080143c
+
080143c
+	vspltw	$xb0,@K[1],0			# smash the key
080143c
+	vspltw	$xb1,@K[1],1
080143c
+	vspltw	$xb2,@K[1],2
080143c
+	vspltw	$xb3,@K[1],3
080143c
+	vspltw	$xb4,@K[1],0			# smash the key
080143c
+	vspltw	$xb5,@K[1],1
080143c
+	vspltw	$xb6,@K[1],2
080143c
+	vspltw	$xb7,@K[1],3
080143c
+
080143c
+	vspltw	$xc0,@K[2],0
080143c
+	vspltw	$xc1,@K[2],1
080143c
+	vspltw	$xc2,@K[2],2
080143c
+	vspltw	$xc3,@K[2],3
080143c
+	vspltw	$xc4,@K[2],0
080143c
+	vspltw	$xc7,@K[2],3
080143c
+	vspltw	$xc5,@K[2],1
080143c
+
080143c
+	vxxlorc	$xd0,$xv4,$xv4			# smash the counter
080143c
+	vspltw	$xd1,@K[3],1
080143c
+	vspltw	$xd2,@K[3],2
080143c
+	vspltw	$xd3,@K[3],3
080143c
+	vxxlorc	$xd4,$xv5,$xv5			# smash the counter
080143c
+	vspltw	$xd5,@K[3],1
080143c
+	vspltw	$xd6,@K[3],2
080143c
+	vspltw	$xd7,@K[3],3
080143c
+	vxxlorc	$xc6,$xv8,$xv8                  #copy of vlspt k[2],2 is in v8.v26 ->k[3] so need to wait until k3 is done
080143c
+
080143c
+Loop_vsx_8x:
080143c
+___
080143c
+	foreach (&VSX_lane_ROUND_8x(0,4, 8,12,16,20,24,28)) { eval; }
080143c
+	foreach (&VSX_lane_ROUND_8x(0,5,10,15,16,21,26,31)) { eval; }
080143c
+$code.=<<___;
080143c
+
080143c
+	bdnz	        Loop_vsx_8x
080143c
+	vxxlor	        $xv13 ,$xd4,$xd4                # save the register vr24-31
080143c
+	vxxlor	        $xv14 ,$xd5,$xd5                #
080143c
+	vxxlor	        $xv15 ,$xd6,$xd6                #
080143c
+	vxxlor	        $xv16 ,$xd7,$xd7                #
080143c
+
080143c
+	vxxlor	        $xv18 ,$xc4,$xc4                #
080143c
+	vxxlor	        $xv19 ,$xc5,$xc5                #
080143c
+	vxxlor	        $xv20 ,$xc6,$xc6                #
080143c
+	vxxlor	        $xv21 ,$xc7,$xc7                #
080143c
+
080143c
+	vxxlor	        $xv6  ,$xb6,$xb6                # save vr23, so we get 8 regs
080143c
+	vxxlor	        $xv7  ,$xb7,$xb7                # save vr23, so we get 8 regs
080143c
+	be?vxxlorc      $beperm,$xv26,$xv26             # copy back the the beperm.
080143c
+
080143c
+	vxxlorc	   @K[0],$xv0,$xv0                #27
080143c
+	vxxlorc	   @K[1],$xv1,$xv1 		  #24
080143c
+	vxxlorc	   @K[2],$xv2,$xv2		  #25
080143c
+	vxxlorc	   @K[3],$xv3,$xv3		  #26
080143c
+	vxxlorc	   $CTR0,$xv4,$xv4
080143c
+###changing to vertical
080143c
+
080143c
+	vmrgew	$xt0,$xa0,$xa1			# transpose data
080143c
+	vmrgew	$xt1,$xa2,$xa3
080143c
+	vmrgow	$xa0,$xa0,$xa1
080143c
+	vmrgow	$xa2,$xa2,$xa3
080143c
+
080143c
+	vmrgew	$xt2,$xb0,$xb1
080143c
+	vmrgew	$xt3,$xb2,$xb3
080143c
+	vmrgow	$xb0,$xb0,$xb1
080143c
+	vmrgow	$xb2,$xb2,$xb3
080143c
+
080143c
+	vadduwm	$xd0,$xd0,$CTR0
080143c
+
080143c
+	vpermdi	$xa1,$xa0,$xa2,0b00
080143c
+	vpermdi	$xa3,$xa0,$xa2,0b11
080143c
+	vpermdi	$xa0,$xt0,$xt1,0b00
080143c
+	vpermdi	$xa2,$xt0,$xt1,0b11
080143c
+	vpermdi	$xb1,$xb0,$xb2,0b00
080143c
+	vpermdi	$xb3,$xb0,$xb2,0b11
080143c
+	vpermdi	$xb0,$xt2,$xt3,0b00
080143c
+	vpermdi	$xb2,$xt2,$xt3,0b11
080143c
+
080143c
+	vmrgew	$xt0,$xc0,$xc1
080143c
+	vmrgew	$xt1,$xc2,$xc3
080143c
+	vmrgow	$xc0,$xc0,$xc1
080143c
+	vmrgow	$xc2,$xc2,$xc3
080143c
+	vmrgew	$xt2,$xd0,$xd1
080143c
+	vmrgew	$xt3,$xd2,$xd3
080143c
+	vmrgow	$xd0,$xd0,$xd1
080143c
+	vmrgow	$xd2,$xd2,$xd3
080143c
+
080143c
+	vpermdi	$xc1,$xc0,$xc2,0b00
080143c
+	vpermdi	$xc3,$xc0,$xc2,0b11
080143c
+	vpermdi	$xc0,$xt0,$xt1,0b00
080143c
+	vpermdi	$xc2,$xt0,$xt1,0b11
080143c
+	vpermdi	$xd1,$xd0,$xd2,0b00
080143c
+	vpermdi	$xd3,$xd0,$xd2,0b11
080143c
+	vpermdi	$xd0,$xt2,$xt3,0b00
080143c
+	vpermdi	$xd2,$xt2,$xt3,0b11
080143c
+
080143c
+	vspltisw $xt0,8
080143c
+	vadduwm  $CTR0,$CTR0,$xt0		# next counter value
080143c
+	vxxlor	 $xv4 ,$CTR0,$CTR0	        #CTR+4-> 5
080143c
+
080143c
+	vadduwm	$xa0,$xa0,@K[0]
080143c
+	vadduwm	$xb0,$xb0,@K[1]
080143c
+	vadduwm	$xc0,$xc0,@K[2]
080143c
+	vadduwm	$xd0,$xd0,@K[3]
080143c
+
080143c
+	be?vperm $xa0,$xa0,$xa0,$beperm
080143c
+	be?vperm $xb0,$xb0,$xb0,$beperm
080143c
+	be?vperm $xc0,$xc0,$xc0,$beperm
080143c
+	be?vperm $xd0,$xd0,$xd0,$beperm
080143c
+
080143c
+	${UCMP}i $len,0x40
080143c
+	blt	Ltail_vsx_8x
080143c
+
080143c
+	lvx_4w	$xt0,$x00,$inp
080143c
+	lvx_4w	$xt1,$x10,$inp
080143c
+	lvx_4w	$xt2,$x20,$inp
080143c
+	lvx_4w	$xt3,$x30,$inp
080143c
+
080143c
+	vxor	$xt0,$xt0,$xa0
080143c
+	vxor	$xt1,$xt1,$xb0
080143c
+	vxor	$xt2,$xt2,$xc0
080143c
+	vxor	$xt3,$xt3,$xd0
080143c
+
080143c
+	stvx_4w	$xt0,$x00,$out
080143c
+	stvx_4w	$xt1,$x10,$out
080143c
+	addi	$inp,$inp,0x40
080143c
+	stvx_4w	$xt2,$x20,$out
080143c
+	subi	$len,$len,0x40
080143c
+	stvx_4w	$xt3,$x30,$out
080143c
+	addi	$out,$out,0x40
080143c
+	beq	Ldone_vsx_8x
080143c
+
080143c
+	vadduwm	$xa0,$xa1,@K[0]
080143c
+	vadduwm	$xb0,$xb1,@K[1]
080143c
+	vadduwm	$xc0,$xc1,@K[2]
080143c
+	vadduwm	$xd0,$xd1,@K[3]
080143c
+
080143c
+	be?vperm $xa0,$xa0,$xa0,$beperm
080143c
+	be?vperm $xb0,$xb0,$xb0,$beperm
080143c
+	be?vperm $xc0,$xc0,$xc0,$beperm
080143c
+	be?vperm $xd0,$xd0,$xd0,$beperm
080143c
+
080143c
+	${UCMP}i $len,0x40
080143c
+	blt	Ltail_vsx_8x
080143c
+
080143c
+	lvx_4w	$xt0,$x00,$inp
080143c
+	lvx_4w	$xt1,$x10,$inp
080143c
+	lvx_4w	$xt2,$x20,$inp
080143c
+	lvx_4w	$xt3,$x30,$inp
080143c
+
080143c
+	vxor	$xt0,$xt0,$xa0
080143c
+	vxor	$xt1,$xt1,$xb0
080143c
+	vxor	$xt2,$xt2,$xc0
080143c
+	vxor	$xt3,$xt3,$xd0
080143c
+
080143c
+	stvx_4w	$xt0,$x00,$out
080143c
+	stvx_4w	$xt1,$x10,$out
080143c
+	addi	$inp,$inp,0x40
080143c
+	stvx_4w	$xt2,$x20,$out
080143c
+	subi	$len,$len,0x40
080143c
+	stvx_4w	$xt3,$x30,$out
080143c
+	addi	$out,$out,0x40
080143c
+	beq	Ldone_vsx_8x
080143c
+
080143c
+	vadduwm	$xa0,$xa2,@K[0]
080143c
+	vadduwm	$xb0,$xb2,@K[1]
080143c
+	vadduwm	$xc0,$xc2,@K[2]
080143c
+	vadduwm	$xd0,$xd2,@K[3]
080143c
+
080143c
+	be?vperm $xa0,$xa0,$xa0,$beperm
080143c
+	be?vperm $xb0,$xb0,$xb0,$beperm
080143c
+	be?vperm $xc0,$xc0,$xc0,$beperm
080143c
+	be?vperm $xd0,$xd0,$xd0,$beperm
080143c
+
080143c
+	${UCMP}i $len,0x40
080143c
+	blt	Ltail_vsx_8x
080143c
+
080143c
+	lvx_4w	$xt0,$x00,$inp
080143c
+	lvx_4w	$xt1,$x10,$inp
080143c
+	lvx_4w	$xt2,$x20,$inp
080143c
+	lvx_4w	$xt3,$x30,$inp
080143c
+
080143c
+	vxor	$xt0,$xt0,$xa0
080143c
+	vxor	$xt1,$xt1,$xb0
080143c
+	vxor	$xt2,$xt2,$xc0
080143c
+	vxor	$xt3,$xt3,$xd0
080143c
+
080143c
+	stvx_4w	$xt0,$x00,$out
080143c
+	stvx_4w	$xt1,$x10,$out
080143c
+	addi	$inp,$inp,0x40
080143c
+	stvx_4w	$xt2,$x20,$out
080143c
+	subi	$len,$len,0x40
080143c
+	stvx_4w	$xt3,$x30,$out
080143c
+	addi	$out,$out,0x40
080143c
+	beq	Ldone_vsx_8x
080143c
+
080143c
+	vadduwm	$xa0,$xa3,@K[0]
080143c
+	vadduwm	$xb0,$xb3,@K[1]
080143c
+	vadduwm	$xc0,$xc3,@K[2]
080143c
+	vadduwm	$xd0,$xd3,@K[3]
080143c
+
080143c
+	be?vperm $xa0,$xa0,$xa0,$beperm
080143c
+	be?vperm $xb0,$xb0,$xb0,$beperm
080143c
+	be?vperm $xc0,$xc0,$xc0,$beperm
080143c
+	be?vperm $xd0,$xd0,$xd0,$beperm
080143c
+
080143c
+	${UCMP}i $len,0x40
080143c
+	blt	Ltail_vsx_8x
080143c
+
080143c
+	lvx_4w	$xt0,$x00,$inp
080143c
+	lvx_4w	$xt1,$x10,$inp
080143c
+	lvx_4w	$xt2,$x20,$inp
080143c
+	lvx_4w	$xt3,$x30,$inp
080143c
+
080143c
+	vxor	$xt0,$xt0,$xa0
080143c
+	vxor	$xt1,$xt1,$xb0
080143c
+	vxor	$xt2,$xt2,$xc0
080143c
+	vxor	$xt3,$xt3,$xd0
080143c
+
080143c
+	stvx_4w	$xt0,$x00,$out
080143c
+	stvx_4w	$xt1,$x10,$out
080143c
+	addi	$inp,$inp,0x40
080143c
+	stvx_4w	$xt2,$x20,$out
080143c
+	subi	$len,$len,0x40
080143c
+	stvx_4w	$xt3,$x30,$out
080143c
+	addi	$out,$out,0x40
080143c
+	beq	Ldone_vsx_8x
080143c
+
080143c
+#blk4-7: 24:31 remain the same as we can use the same logic above . Reg a4-b7 remain same.Load c4,d7--> position 8-15.we can reuse vr24-31.
080143c
+#VR0-3 : are used to load temp value, vr4 --> as xr0 instead of xt0.
080143c
+
080143c
+	vxxlorc	   $CTR1 ,$xv5,$xv5
080143c
+
080143c
+	vxxlorc	   $xcn4 ,$xv18,$xv18
080143c
+	vxxlorc	   $xcn5 ,$xv19,$xv19
080143c
+	vxxlorc	   $xcn6 ,$xv20,$xv20
080143c
+	vxxlorc	   $xcn7 ,$xv21,$xv21
080143c
+
080143c
+	vxxlorc	   $xdn4 ,$xv13,$xv13
080143c
+	vxxlorc	   $xdn5 ,$xv14,$xv14
080143c
+	vxxlorc	   $xdn6 ,$xv15,$xv15
080143c
+	vxxlorc	   $xdn7 ,$xv16,$xv16
080143c
+	vadduwm	   $xdn4,$xdn4,$CTR1
080143c
+
080143c
+	vxxlorc	   $xb6 ,$xv6,$xv6
080143c
+	vxxlorc	   $xb7 ,$xv7,$xv7
080143c
+#use xa1->xr0, as xt0...in the block 4-7
080143c
+
080143c
+	vmrgew	$xr0,$xa4,$xa5			# transpose data
080143c
+	vmrgew	$xt1,$xa6,$xa7
080143c
+	vmrgow	$xa4,$xa4,$xa5
080143c
+	vmrgow	$xa6,$xa6,$xa7
080143c
+	vmrgew	$xt2,$xb4,$xb5
080143c
+	vmrgew	$xt3,$xb6,$xb7
080143c
+	vmrgow	$xb4,$xb4,$xb5
080143c
+	vmrgow	$xb6,$xb6,$xb7
080143c
+
080143c
+	vpermdi	$xa5,$xa4,$xa6,0b00
080143c
+	vpermdi	$xa7,$xa4,$xa6,0b11
080143c
+	vpermdi	$xa4,$xr0,$xt1,0b00
080143c
+	vpermdi	$xa6,$xr0,$xt1,0b11
080143c
+	vpermdi	$xb5,$xb4,$xb6,0b00
080143c
+	vpermdi	$xb7,$xb4,$xb6,0b11
080143c
+	vpermdi	$xb4,$xt2,$xt3,0b00
080143c
+	vpermdi	$xb6,$xt2,$xt3,0b11
080143c
+
080143c
+	vmrgew	$xr0,$xcn4,$xcn5
080143c
+	vmrgew	$xt1,$xcn6,$xcn7
080143c
+	vmrgow	$xcn4,$xcn4,$xcn5
080143c
+	vmrgow	$xcn6,$xcn6,$xcn7
080143c
+	vmrgew	$xt2,$xdn4,$xdn5
080143c
+	vmrgew	$xt3,$xdn6,$xdn7
080143c
+	vmrgow	$xdn4,$xdn4,$xdn5
080143c
+	vmrgow	$xdn6,$xdn6,$xdn7
080143c
+
080143c
+	vpermdi	$xcn5,$xcn4,$xcn6,0b00
080143c
+	vpermdi	$xcn7,$xcn4,$xcn6,0b11
080143c
+	vpermdi	$xcn4,$xr0,$xt1,0b00
080143c
+	vpermdi	$xcn6,$xr0,$xt1,0b11
080143c
+	vpermdi	$xdn5,$xdn4,$xdn6,0b00
080143c
+	vpermdi	$xdn7,$xdn4,$xdn6,0b11
080143c
+	vpermdi	$xdn4,$xt2,$xt3,0b00
080143c
+	vpermdi	$xdn6,$xt2,$xt3,0b11
080143c
+
080143c
+	vspltisw $xr0,8
080143c
+	vadduwm  $CTR1,$CTR1,$xr0		# next counter value
080143c
+	vxxlor	 $xv5 ,$CTR1,$CTR1	        #CTR+4-> 5
080143c
+
080143c
+	vadduwm	$xan0,$xa4,@K[0]
080143c
+	vadduwm	$xbn0,$xb4,@K[1]
080143c
+	vadduwm	$xcn0,$xcn4,@K[2]
080143c
+	vadduwm	$xdn0,$xdn4,@K[3]
080143c
+
080143c
+	be?vperm $xan0,$xa4,$xa4,$beperm
080143c
+	be?vperm $xbn0,$xb4,$xb4,$beperm
080143c
+	be?vperm $xcn0,$xcn4,$xcn4,$beperm
080143c
+	be?vperm $xdn0,$xdn4,$xdn4,$beperm
080143c
+
080143c
+	${UCMP}i $len,0x40
080143c
+	blt	Ltail_vsx_8x_1
080143c
+
080143c
+	lvx_4w	$xr0,$x00,$inp
080143c
+	lvx_4w	$xt1,$x10,$inp
080143c
+	lvx_4w	$xt2,$x20,$inp
080143c
+	lvx_4w	$xt3,$x30,$inp
080143c
+
080143c
+	vxor	$xr0,$xr0,$xan0
080143c
+	vxor	$xt1,$xt1,$xbn0
080143c
+	vxor	$xt2,$xt2,$xcn0
080143c
+	vxor	$xt3,$xt3,$xdn0
080143c
+
080143c
+	stvx_4w	$xr0,$x00,$out
080143c
+	stvx_4w	$xt1,$x10,$out
080143c
+	addi	$inp,$inp,0x40
080143c
+	stvx_4w	$xt2,$x20,$out
080143c
+	subi	$len,$len,0x40
080143c
+	stvx_4w	$xt3,$x30,$out
080143c
+	addi	$out,$out,0x40
080143c
+	beq	Ldone_vsx_8x
080143c
+
080143c
+	vadduwm	$xan0,$xa5,@K[0]
080143c
+	vadduwm	$xbn0,$xb5,@K[1]
080143c
+	vadduwm	$xcn0,$xcn5,@K[2]
080143c
+	vadduwm	$xdn0,$xdn5,@K[3]
080143c
+
080143c
+	be?vperm $xan0,$xan0,$xan0,$beperm
080143c
+	be?vperm $xbn0,$xbn0,$xbn0,$beperm
080143c
+	be?vperm $xcn0,$xcn0,$xcn0,$beperm
080143c
+	be?vperm $xdn0,$xdn0,$xdn0,$beperm
080143c
+
080143c
+	${UCMP}i $len,0x40
080143c
+	blt	Ltail_vsx_8x_1
080143c
+
080143c
+	lvx_4w	$xr0,$x00,$inp
080143c
+	lvx_4w	$xt1,$x10,$inp
080143c
+	lvx_4w	$xt2,$x20,$inp
080143c
+	lvx_4w	$xt3,$x30,$inp
080143c
+
080143c
+	vxor	$xr0,$xr0,$xan0
080143c
+	vxor	$xt1,$xt1,$xbn0
080143c
+	vxor	$xt2,$xt2,$xcn0
080143c
+	vxor	$xt3,$xt3,$xdn0
080143c
+
080143c
+	stvx_4w	$xr0,$x00,$out
080143c
+	stvx_4w	$xt1,$x10,$out
080143c
+	addi	$inp,$inp,0x40
080143c
+	stvx_4w	$xt2,$x20,$out
080143c
+	subi	$len,$len,0x40
080143c
+	stvx_4w	$xt3,$x30,$out
080143c
+	addi	$out,$out,0x40
080143c
+	beq	Ldone_vsx_8x
080143c
+
080143c
+	vadduwm	$xan0,$xa6,@K[0]
080143c
+	vadduwm	$xbn0,$xb6,@K[1]
080143c
+	vadduwm	$xcn0,$xcn6,@K[2]
080143c
+	vadduwm	$xdn0,$xdn6,@K[3]
080143c
+
080143c
+	be?vperm $xan0,$xan0,$xan0,$beperm
080143c
+	be?vperm $xbn0,$xbn0,$xbn0,$beperm
080143c
+	be?vperm $xcn0,$xcn0,$xcn0,$beperm
080143c
+	be?vperm $xdn0,$xdn0,$xdn0,$beperm
080143c
+
080143c
+	${UCMP}i $len,0x40
080143c
+	blt	Ltail_vsx_8x_1
080143c
+
080143c
+	lvx_4w	$xr0,$x00,$inp
080143c
+	lvx_4w	$xt1,$x10,$inp
080143c
+	lvx_4w	$xt2,$x20,$inp
080143c
+	lvx_4w	$xt3,$x30,$inp
080143c
+
080143c
+	vxor	$xr0,$xr0,$xan0
080143c
+	vxor	$xt1,$xt1,$xbn0
080143c
+	vxor	$xt2,$xt2,$xcn0
080143c
+	vxor	$xt3,$xt3,$xdn0
080143c
+
080143c
+	stvx_4w	$xr0,$x00,$out
080143c
+	stvx_4w	$xt1,$x10,$out
080143c
+	addi	$inp,$inp,0x40
080143c
+	stvx_4w	$xt2,$x20,$out
080143c
+	subi	$len,$len,0x40
080143c
+	stvx_4w	$xt3,$x30,$out
080143c
+	addi	$out,$out,0x40
080143c
+	beq	Ldone_vsx_8x
080143c
+
080143c
+	vadduwm	$xan0,$xa7,@K[0]
080143c
+	vadduwm	$xbn0,$xb7,@K[1]
080143c
+	vadduwm	$xcn0,$xcn7,@K[2]
080143c
+	vadduwm	$xdn0,$xdn7,@K[3]
080143c
+
080143c
+	be?vperm $xan0,$xan0,$xan0,$beperm
080143c
+	be?vperm $xbn0,$xbn0,$xbn0,$beperm
080143c
+	be?vperm $xcn0,$xcn0,$xcn0,$beperm
080143c
+	be?vperm $xdn0,$xdn0,$xdn0,$beperm
080143c
+
080143c
+	${UCMP}i $len,0x40
080143c
+	blt	Ltail_vsx_8x_1
080143c
+
080143c
+	lvx_4w	$xr0,$x00,$inp
080143c
+	lvx_4w	$xt1,$x10,$inp
080143c
+	lvx_4w	$xt2,$x20,$inp
080143c
+	lvx_4w	$xt3,$x30,$inp
080143c
+
080143c
+	vxor	$xr0,$xr0,$xan0
080143c
+	vxor	$xt1,$xt1,$xbn0
080143c
+	vxor	$xt2,$xt2,$xcn0
080143c
+	vxor	$xt3,$xt3,$xdn0
080143c
+
080143c
+	stvx_4w	$xr0,$x00,$out
080143c
+	stvx_4w	$xt1,$x10,$out
080143c
+	addi	$inp,$inp,0x40
080143c
+	stvx_4w	$xt2,$x20,$out
080143c
+	subi	$len,$len,0x40
080143c
+	stvx_4w	$xt3,$x30,$out
080143c
+	addi	$out,$out,0x40
080143c
+	beq	Ldone_vsx_8x
080143c
+
080143c
+	mtctr	r0
080143c
+	bne	Loop_outer_vsx_8x
080143c
+
080143c
+Ldone_vsx_8x:
080143c
+	lwz	r12,`$FRAME-4`($sp)		# pull vrsave
080143c
+	li	r10,`15+$LOCALS+64`
080143c
+	li	r11,`31+$LOCALS+64`
080143c
+	$POP	r0, `$FRAME+$LRSAVE`($sp)
080143c
+	mtspr	256,r12				# restore vrsave
080143c
+	lvx	v24,r10,$sp
080143c
+	addi	r10,r10,32
080143c
+	lvx	v25,r11,$sp
080143c
+	addi	r11,r11,32
080143c
+	lvx	v26,r10,$sp
080143c
+	addi	r10,r10,32
080143c
+	lvx	v27,r11,$sp
080143c
+	addi	r11,r11,32
080143c
+	lvx	v28,r10,$sp
080143c
+	addi	r10,r10,32
080143c
+	lvx	v29,r11,$sp
080143c
+	addi	r11,r11,32
080143c
+	lvx	v30,r10,$sp
080143c
+	lvx	v31,r11,$sp
080143c
+	mtlr	r0
080143c
+	addi	$sp,$sp,$FRAME
080143c
+	blr
080143c
+
080143c
+.align	4
080143c
+Ltail_vsx_8x:
080143c
+	addi	r11,$sp,$LOCALS
080143c
+	mtctr	$len
080143c
+	stvx_4w	$xa0,$x00,r11			# offload block to stack
080143c
+	stvx_4w	$xb0,$x10,r11
080143c
+	stvx_4w	$xc0,$x20,r11
080143c
+	stvx_4w	$xd0,$x30,r11
080143c
+	subi	r12,r11,1			# prepare for *++ptr
080143c
+	subi	$inp,$inp,1
080143c
+	subi	$out,$out,1
080143c
+	bl      Loop_tail_vsx_8x
080143c
+Ltail_vsx_8x_1:
080143c
+	addi	r11,$sp,$LOCALS
080143c
+	mtctr	$len
080143c
+	stvx_4w	$xan0,$x00,r11			# offload block to stack
080143c
+	stvx_4w	$xbn0,$x10,r11
080143c
+	stvx_4w	$xcn0,$x20,r11
080143c
+	stvx_4w	$xdn0,$x30,r11
080143c
+	subi	r12,r11,1			# prepare for *++ptr
080143c
+	subi	$inp,$inp,1
080143c
+	subi	$out,$out,1
080143c
+        bl      Loop_tail_vsx_8x
080143c
+
080143c
+Loop_tail_vsx_8x:
080143c
+	lbzu	r6,1(r12)
080143c
+	lbzu	r7,1($inp)
080143c
+	xor	r6,r6,r7
080143c
+	stbu	r6,1($out)
080143c
+	bdnz	Loop_tail_vsx_8x
080143c
+
080143c
+	stvx_4w	$K[0],$x00,r11			# wipe copy of the block
080143c
+	stvx_4w	$K[0],$x10,r11
080143c
+	stvx_4w	$K[0],$x20,r11
080143c
+	stvx_4w	$K[0],$x30,r11
080143c
+
080143c
+	b	Ldone_vsx_8x
080143c
+	.long	0
080143c
+	.byte	0,12,0x04,1,0x80,0,5,0
080143c
+	.long	0
080143c
+.size	.ChaCha20_ctr32_vsx_8x,.-.ChaCha20_ctr32_vsx_8x
080143c
+___
080143c
+}}}
080143c
+
080143c
+
080143c
+$code.=<<___;
080143c
+.align	5
080143c
+Lconsts:
080143c
+	mflr	r0
080143c
+	bcl	20,31,\$+4
080143c
+	mflr	r12	#vvvvv "distance between . and Lsigma
080143c
+	addi	r12,r12,`64-8`
080143c
+	mtlr	r0
080143c
+	blr
080143c
+	.long	0
080143c
+	.byte	0,12,0x14,0,0,0,0,0
080143c
+	.space	`64-9*4`
080143c
+Lsigma:
080143c
+	.long   0x61707865,0x3320646e,0x79622d32,0x6b206574
080143c
+	.long	1,0,0,0
080143c
+	.long	2,0,0,0
080143c
+	.long	3,0,0,0
080143c
+	.long	4,0,0,0
080143c
+___
080143c
+$code.=<<___ 	if ($LITTLE_ENDIAN);
080143c
+	.long	0x0e0f0c0d,0x0a0b0809,0x06070405,0x02030001
080143c
+	.long	0x0d0e0f0c,0x090a0b08,0x05060704,0x01020300
080143c
+___
080143c
+$code.=<<___ 	if (!$LITTLE_ENDIAN);	# flipped words
080143c
+	.long	0x02030001,0x06070405,0x0a0b0809,0x0e0f0c0d
080143c
+	.long	0x01020300,0x05060704,0x090a0b08,0x0d0e0f0c
080143c
+___
080143c
+$code.=<<___;
080143c
+	.long	0x61707865,0x61707865,0x61707865,0x61707865
080143c
+	.long	0x3320646e,0x3320646e,0x3320646e,0x3320646e
080143c
+	.long	0x79622d32,0x79622d32,0x79622d32,0x79622d32
080143c
+	.long	0x6b206574,0x6b206574,0x6b206574,0x6b206574
080143c
+	.long	0,1,2,3
080143c
+        .long   0x03020100,0x07060504,0x0b0a0908,0x0f0e0d0c
080143c
+.asciz  "ChaCha20 for PowerPC/AltiVec, CRYPTOGAMS by <appro\@openssl.org>"
080143c
+.align	2
080143c
+___
080143c
+
080143c
+foreach (split("\n",$code)) {
080143c
+	s/\`([^\`]*)\`/eval $1/ge;
080143c
+
080143c
+	# instructions prefixed with '?' are endian-specific and need
080143c
+	# to be adjusted accordingly...
080143c
+	if ($flavour !~ /le$/) {	# big-endian
080143c
+	    s/be\?//		or
080143c
+	    s/le\?/#le#/	or
080143c
+	    s/\?lvsr/lvsl/	or
080143c
+	    s/\?lvsl/lvsr/	or
080143c
+	    s/\?(vperm\s+v[0-9]+,\s*)(v[0-9]+,\s*)(v[0-9]+,\s*)(v[0-9]+)/$1$3$2$4/ or
080143c
+	    s/vrldoi(\s+v[0-9]+,\s*)(v[0-9]+,)\s*([0-9]+)/vsldoi$1$2$2 16-$3/;
080143c
+	} else {			# little-endian
080143c
+	    s/le\?//		or
080143c
+	    s/be\?/#be#/	or
080143c
+	    s/\?([a-z]+)/$1/	or
080143c
+	    s/vrldoi(\s+v[0-9]+,\s*)(v[0-9]+,)\s*([0-9]+)/vsldoi$1$2$2 $3/;
080143c
+	}
080143c
+
080143c
+	print $_,"\n";
080143c
+}
080143c
+
080143c
+close STDOUT or die "error closing STDOUT: $!";
080143c
diff --git a/crypto/chacha/build.info b/crypto/chacha/build.info
080143c
index c12cb9c..2a819b2 100644
080143c
--- a/crypto/chacha/build.info
080143c
+++ b/crypto/chacha/build.info
080143c
@@ -12,7 +12,7 @@ IF[{- !$disabled{asm} -}]
080143c
   $CHACHAASM_armv4=chacha-armv4.S
080143c
   $CHACHAASM_aarch64=chacha-armv8.S
080143c
 
080143c
-  $CHACHAASM_ppc32=chacha_ppc.c chacha-ppc.s
080143c
+  $CHACHAASM_ppc32=chacha_ppc.c chacha-ppc.s chachap10-ppc.s
080143c
   $CHACHAASM_ppc64=$CHACHAASM_ppc32
080143c
 
080143c
   $CHACHAASM_c64xplus=chacha-c64xplus.s
080143c
@@ -29,6 +29,7 @@ SOURCE[../../libcrypto]=$CHACHAASM
080143c
 GENERATE[chacha-x86.S]=asm/chacha-x86.pl
080143c
 GENERATE[chacha-x86_64.s]=asm/chacha-x86_64.pl
080143c
 GENERATE[chacha-ppc.s]=asm/chacha-ppc.pl
080143c
+GENERATE[chachap10-ppc.s]=asm/chachap10-ppc.pl
080143c
 GENERATE[chacha-armv4.S]=asm/chacha-armv4.pl
080143c
 INCLUDE[chacha-armv4.o]=..
080143c
 GENERATE[chacha-armv8.S]=asm/chacha-armv8.pl
080143c
diff --git a/crypto/chacha/chacha_ppc.c b/crypto/chacha/chacha_ppc.c
080143c
index 5319040..f99cca8 100644
080143c
--- a/crypto/chacha/chacha_ppc.c
080143c
+++ b/crypto/chacha/chacha_ppc.c
080143c
@@ -23,13 +23,18 @@ void ChaCha20_ctr32_vmx(unsigned char *out, const unsigned char *inp,
080143c
 void ChaCha20_ctr32_vsx(unsigned char *out, const unsigned char *inp,
080143c
                         size_t len, const unsigned int key[8],
080143c
                         const unsigned int counter[4]);
080143c
+void ChaCha20_ctr32_vsx_p10(unsigned char *out, const unsigned char *inp,
080143c
+                        size_t len, const unsigned int key[8],
080143c
+                        const unsigned int counter[4]);
080143c
 void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp,
080143c
                     size_t len, const unsigned int key[8],
080143c
                     const unsigned int counter[4])
080143c
 {
080143c
-    OPENSSL_ppccap_P & PPC_CRYPTO207
080143c
-        ? ChaCha20_ctr32_vsx(out, inp, len, key, counter)
080143c
-        : OPENSSL_ppccap_P & PPC_ALTIVEC
080143c
-            ? ChaCha20_ctr32_vmx(out, inp, len, key, counter)
080143c
-            : ChaCha20_ctr32_int(out, inp, len, key, counter);
080143c
+    OPENSSL_ppccap_P & PPC_BRD31
080143c
+        ? ChaCha20_ctr32_vsx_p10(out, inp, len, key, counter)
080143c
+        :OPENSSL_ppccap_P & PPC_CRYPTO207
080143c
+            ? ChaCha20_ctr32_vsx(out, inp, len, key, counter)
080143c
+            : OPENSSL_ppccap_P & PPC_ALTIVEC
080143c
+                 ? ChaCha20_ctr32_vmx(out, inp, len, key, counter)
080143c
+                 : ChaCha20_ctr32_int(out, inp, len, key, counter);
080143c
 }
080143c
diff --git a/crypto/perlasm/ppc-xlate.pl b/crypto/perlasm/ppc-xlate.pl
080143c
index 2ee4440..4590340 100755
080143c
--- a/crypto/perlasm/ppc-xlate.pl
080143c
+++ b/crypto/perlasm/ppc-xlate.pl
080143c
@@ -293,6 +293,14 @@ my $vpermdi	= sub {				# xxpermdi
080143c
     $dm = oct($dm) if ($dm =~ /^0/);
080143c
     "	.long	".sprintf "0x%X",(60<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|($dm<<8)|(10<<3)|7;
080143c
 };
080143c
+my $vxxlor	= sub {				# xxlor
080143c
+    my ($f, $vrt, $vra, $vrb) = @_;
080143c
+    "	.long	".sprintf "0x%X",(60<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|(146<<3)|6;
080143c
+};
080143c
+my $vxxlorc	= sub {				# xxlor
080143c
+    my ($f, $vrt, $vra, $vrb) = @_;
080143c
+    "	.long	".sprintf "0x%X",(60<<26)|($vrt<<21)|($vra<<16)|($vrb<<11)|(146<<3)|1;
080143c
+};
080143c
 
080143c
 # PowerISA 2.07 stuff
080143c
 sub vcrypto_op {
080143c
@@ -377,6 +385,15 @@ my $addex = sub {
080143c
 };
080143c
 my $vmsumudm	= sub { vfour_vsr(@_, 35); };
080143c
 
080143c
+# PowerISA 3.1 stuff
080143c
+my $brd = sub {
080143c
+    my ($f, $ra, $rs) = @_;
080143c
+    "  .long   ".sprintf "0x%X",(31<<26)|($rs<<21)|($ra<<16)|(187<<1);
080143c
+};
080143c
+my $vsrq	= sub { vcrypto_op(@_, 517); };
080143c
+
080143c
+
080143c
+
080143c
 while($line=<>) {
080143c
 
080143c
     $line =~ s|[#!;].*$||;	# get rid of asm-style comments...
080143c
diff --git a/crypto/ppccap.c b/crypto/ppccap.c
080143c
index 8bcfed2..664627c 100644
080143c
--- a/crypto/ppccap.c
080143c
+++ b/crypto/ppccap.c
080143c
@@ -45,6 +45,7 @@ void OPENSSL_ppc64_probe(void);
080143c
 void OPENSSL_altivec_probe(void);
080143c
 void OPENSSL_crypto207_probe(void);
080143c
 void OPENSSL_madd300_probe(void);
080143c
+void OPENSSL_brd31_probe(void);
080143c
 
080143c
 long OPENSSL_rdtsc_mftb(void);
080143c
 long OPENSSL_rdtsc_mfspr268(void);
080143c
@@ -117,16 +118,21 @@ static unsigned long getauxval(unsigned long key)
080143c
 #endif
080143c
 
080143c
 /* I wish <sys/auxv.h> was universally available */
080143c
-#define HWCAP                   16      /* AT_HWCAP */
080143c
+#ifndef AT_HWCAP
080143c
+# define AT_HWCAP               16      /* AT_HWCAP */
080143c
+#endif
080143c
 #define HWCAP_PPC64             (1U << 30)
080143c
 #define HWCAP_ALTIVEC           (1U << 28)
080143c
 #define HWCAP_FPU               (1U << 27)
080143c
 #define HWCAP_POWER6_EXT        (1U << 9)
080143c
 #define HWCAP_VSX               (1U << 7)
080143c
 
080143c
-#define HWCAP2                  26      /* AT_HWCAP2 */
080143c
+#ifndef AT_HWCAP2
080143c
+# define AT_HWCAP2              26      /* AT_HWCAP2 */
080143c
+#endif
080143c
 #define HWCAP_VEC_CRYPTO        (1U << 25)
080143c
 #define HWCAP_ARCH_3_00         (1U << 23)
080143c
+#define HWCAP_ARCH_3_1          (1U << 18)
080143c
 
080143c
 # if defined(__GNUC__) && __GNUC__>=2
080143c
 __attribute__ ((constructor))
080143c
@@ -187,6 +193,9 @@ void OPENSSL_cpuid_setup(void)
080143c
     if (__power_set(0xffffffffU<<17))           /* POWER9 and later */
080143c
         OPENSSL_ppccap_P |= PPC_MADD300;
080143c
 
080143c
+    if (__power_set(0xffffffffU<<18))           /* POWER10 and later */
080143c
+        OPENSSL_ppccap_P |= PPC_BRD31;
080143c
+
080143c
     return;
080143c
 # endif
080143c
 #endif
080143c
@@ -215,8 +224,8 @@ void OPENSSL_cpuid_setup(void)
080143c
 
080143c
 #ifdef OSSL_IMPLEMENT_GETAUXVAL
080143c
     {
080143c
-        unsigned long hwcap = getauxval(HWCAP);
080143c
-        unsigned long hwcap2 = getauxval(HWCAP2);
080143c
+        unsigned long hwcap = getauxval(AT_HWCAP);
080143c
+        unsigned long hwcap2 = getauxval(AT_HWCAP2);
080143c
 
080143c
         if (hwcap & HWCAP_FPU) {
080143c
             OPENSSL_ppccap_P |= PPC_FPU;
080143c
@@ -242,6 +251,10 @@ void OPENSSL_cpuid_setup(void)
080143c
         if (hwcap2 & HWCAP_ARCH_3_00) {
080143c
             OPENSSL_ppccap_P |= PPC_MADD300;
080143c
         }
080143c
+
080143c
+        if (hwcap2 & HWCAP_ARCH_3_1) {
080143c
+            OPENSSL_ppccap_P |= PPC_BRD31;
080143c
+        }
080143c
     }
080143c
 #endif
080143c
 
080143c
@@ -263,7 +276,7 @@ void OPENSSL_cpuid_setup(void)
080143c
     sigaction(SIGILL, &ill_act, &ill_oact);
080143c
 
080143c
 #ifndef OSSL_IMPLEMENT_GETAUXVAL
080143c
-    if (sigsetjmp(ill_jmp,1) == 0) {
080143c
+    if (sigsetjmp(ill_jmp, 1) == 0) {
080143c
         OPENSSL_fpu_probe();
080143c
         OPENSSL_ppccap_P |= PPC_FPU;
080143c
 
080143c
diff --git a/crypto/ppccpuid.pl b/crypto/ppccpuid.pl
080143c
index c6555df..706164a 100755
080143c
--- a/crypto/ppccpuid.pl
080143c
+++ b/crypto/ppccpuid.pl
080143c
@@ -81,6 +81,17 @@ $code=<<___;
080143c
 	.long	0
080143c
 	.byte	0,12,0x14,0,0,0,0,0
080143c
 
080143c
+.globl	.OPENSSL_brd31_probe
080143c
+.align	4
080143c
+.OPENSSL_brd31_probe:
080143c
+	xor	r0,r0,r0
080143c
+	brd	r3,r0
080143c
+	blr
080143c
+	.long	0
080143c
+	.byte	0,12,0x14,0,0,0,0,0
080143c
+.size	.OPENSSL_brd31_probe,.-.OPENSSL_brd31_probe
080143c
+
080143c
+
080143c
 .globl	.OPENSSL_wipe_cpu
080143c
 .align	4
080143c
 .OPENSSL_wipe_cpu:
080143c
diff --git a/include/crypto/ppc_arch.h b/include/crypto/ppc_arch.h
080143c
index 3b3ce4b..fcc846c 100644
080143c
--- a/include/crypto/ppc_arch.h
080143c
+++ b/include/crypto/ppc_arch.h
080143c
@@ -24,5 +24,6 @@ extern unsigned int OPENSSL_ppccap_P;
080143c
 # define PPC_MADD300     (1<<4)
080143c
 # define PPC_MFTB        (1<<5)
080143c
 # define PPC_MFSPR268    (1<<6)
080143c
+# define PPC_BRD31       (1<<7)
080143c
 
080143c
 #endif