From 6df7707fb22e8bd1c7d778a2041c1403f9852060 Mon Sep 17 00:00:00 2001
From: Xu Yizhou <xuyizhou1@huawei.com>
Date: Fri, 3 Feb 2023 15:59:59 +0800
Subject: [PATCH 13/13] Fix SM4-XTS build failure on Mac mini M1

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/20202)
---
 crypto/sm4/asm/vpsm4-armv8.pl    |  4 +++-
 crypto/sm4/asm/vpsm4_ex-armv8.pl | 23 ++++++++++++++++-------
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/crypto/sm4/asm/vpsm4-armv8.pl b/crypto/sm4/asm/vpsm4-armv8.pl
index e19de30901..d30e78f3ce 100755
--- a/crypto/sm4/asm/vpsm4-armv8.pl
+++ b/crypto/sm4/asm/vpsm4-armv8.pl
@@ -524,7 +524,7 @@ sub compute_tweak_vec() {
 	my $std = shift;
 	&rbit(@vtmp[2],$src,$std);
 $code.=<<___;
-	ldr  @qtmp[0], =0x01010101010101010101010101010187
+	ldr  @qtmp[0], .Lxts_magic
 	shl  $des.16b, @vtmp[2].16b, #1
 	ext  @vtmp[1].16b, @vtmp[2].16b, @vtmp[2].16b,#15
 	ushr @vtmp[1].16b, @vtmp[1].16b, #7
@@ -572,6 +572,8 @@ _vpsm4_consts:
 	.dword 0x56aa3350a3b1bac6,0xb27022dc677d9197
 .Lshuffles:
 	.dword 0x0B0A090807060504,0x030201000F0E0D0C
+.Lxts_magic:
+	.dword 0x0101010101010187,0x0101010101010101
 
 .size	_vpsm4_consts,.-_vpsm4_consts
 ___
diff --git a/crypto/sm4/asm/vpsm4_ex-armv8.pl b/crypto/sm4/asm/vpsm4_ex-armv8.pl
index 3d094aa535..f2d5b6debf 100644
--- a/crypto/sm4/asm/vpsm4_ex-armv8.pl
+++ b/crypto/sm4/asm/vpsm4_ex-armv8.pl
@@ -475,12 +475,12 @@ sub load_sbox () {
 	my $data = shift;
 
 $code.=<<___;
-	ldr $MaskQ,	   =0x0306090c0f0205080b0e0104070a0d00
-	ldr $TAHMatQ,	=0x22581a6002783a4062185a2042387a00
-	ldr $TALMatQ,	=0xc10bb67c4a803df715df62a89e54e923
-	ldr $ATAHMatQ,   =0x1407c6d56c7fbeadb9aa6b78c1d21300
-	ldr $ATALMatQ,   =0xe383c1a1fe9edcbc6404462679195b3b
-	ldr $ANDMaskQ,	=0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f
+	ldr $MaskQ, .Lsbox_magic
+	ldr $TAHMatQ, .Lsbox_magic+16
+	ldr $TALMatQ, .Lsbox_magic+32
+	ldr $ATAHMatQ, .Lsbox_magic+48
+	ldr $ATALMatQ, .Lsbox_magic+64
+	ldr $ANDMaskQ, .Lsbox_magic+80
 ___
 }
 
@@ -525,7 +525,7 @@ sub compute_tweak_vec() {
 	my $std = shift;
 	&rbit(@vtmp[2],$src,$std);
 $code.=<<___;
-	ldr  @qtmp[0], =0x01010101010101010101010101010187
+	ldr  @qtmp[0], .Lxts_magic
 	shl  $des.16b, @vtmp[2].16b, #1
 	ext  @vtmp[1].16b, @vtmp[2].16b, @vtmp[2].16b,#15
 	ushr @vtmp[1].16b, @vtmp[1].16b, #7
@@ -556,6 +556,15 @@ _${prefix}_consts:
 	.dword 0x56aa3350a3b1bac6,0xb27022dc677d9197
 .Lshuffles:
 	.dword 0x0B0A090807060504,0x030201000F0E0D0C
+.Lxts_magic:
+	.dword 0x0101010101010187,0x0101010101010101
+.Lsbox_magic:
+	.dword 0x0b0e0104070a0d00,0x0306090c0f020508
+	.dword 0x62185a2042387a00,0x22581a6002783a40
+	.dword 0x15df62a89e54e923,0xc10bb67c4a803df7
+	.dword 0xb9aa6b78c1d21300,0x1407c6d56c7fbead
+	.dword 0x6404462679195b3b,0xe383c1a1fe9edcbc
+	.dword 0x0f0f0f0f0f0f0f0f,0x0f0f0f0f0f0f0f0f
 
 .size	_${prefix}_consts,.-_${prefix}_consts
 ___
-- 
2.37.3.windows.1

