Skip to contentSkip to conversation

lib/crypto: x86/sha: Add PHE Extensions support

PATCH Superseded AlanSong-oc 2 participants 9 messages Dec 19, 2025 – Jan 15

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover lib/crypto: x86/sha: Add PHE Extensions support

For Zhaoxin processors, the XSHA1 instruction requires the total memory allocated at %rdi register must be 32 bytes, while the XSHA1 and XSHA256 instruction doesn't perform any operation when %ecx is zero.

Due to these requirements, the current padlock-sha driver does not work correctly with Zhaoxin processors. It cannot pass the self-tests and therefore does not activate the driver on Zhaoxin processors. This issue has been reported in Debian [1]. The self-tests fail with the following messages [2]:

alg: shash: sha1-padlock-nano test failed (wrong result) on test vector 0, cfg="init+update+final aligned buffer"
alg: self-tests for sha1 using sha1-padlock-nano failed (rc=-22)

------------[ cut here ]------------

alg: shash: sha256-padlock-nano test failed (wrong result) on test vector 0, cfg="init+update+final aligned buffer"
alg: self-tests for sha256 using sha256-padlock-nano failed (rc=-22)

------------[ cut here ]------------

To enable XSHA1 and XSHA256 instruction support on Zhaoxin processors, this series adds PHE Extensions support to lib/crypto for SHA-1 and SHA-256, following the suggestion in [3].

v1 link is below:
https://lore.kernel.org/linux-crypto/[email protected]/

---
v1->v2: Add Zhaoxin support to lib/crypto instead of extending
the existing padlock-sha driver

[1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1103397
[2] https://linux-hardware.org/?probe=271fabb7a4&log=dmesg
[3] https://lore.kernel.org/linux-crypto/[email protected]/

AlanSong-oc (2):
lib/crypto: x86/sha1: PHE Extensions optimized SHA1 transform function lib/crypto: x86/sha256: PHE Extensions optimized SHA256 transform
function

 lib/crypto/Makefile             |  6 ++-
 lib/crypto/x86/sha1-phe-asm.S   | 71 +++++++++++++++++++++++++++++++++
 lib/crypto/x86/sha1.h           | 20 ++++++++++
 lib/crypto/x86/sha256-phe-asm.S | 70 ++++++++++++++++++++++++++++++++
 lib/crypto/x86/sha256.h         | 20 ++++++++++
 5 files changed, 185 insertions(+), 2 deletions(-)
 create mode 100644 lib/crypto/x86/sha1-phe-asm.S
 create mode 100644 lib/crypto/x86/sha256-phe-asm.S
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/2 lib/crypto: x86/sha1: PHE Extensions optimized SHA1 transform function

Zhaoxin CPUs have implemented the SHA(Secure Hash Algorithm) as its CPU instructions by PHE(Padlock Hash Engine) Extensions, including XSHA1, XSHA256, XSHA384 and XSHA512 instructions.

With the help of implementation of SHA in hardware instead of software, can develop applications with higher performance, more security and more flexibility.

This patch includes the XSHA1 instruction optimized implementation of SHA-1 transform function.

---
 lib/crypto/Makefile           |  3 +-
 lib/crypto/x86/sha1-phe-asm.S | 71 +++++++++++++++++++++++++++++++++++
 lib/crypto/x86/sha1.h         | 20 ++++++++++
 3 files changed, 93 insertions(+), 1 deletion(-)
 create mode 100644 lib/crypto/x86/sha1-phe-asm.S
lib/crypto/Makefile +2 -1
index d2845b214..069069377 100644
@@ -205,7 +205,8 @@ endif
 libsha1-$(CONFIG_SPARC) += sparc/sha1_asm.o
 libsha1-$(CONFIG_X86) += x86/sha1-ssse3-and-avx.o \
 			 x86/sha1-avx2-asm.o \
-			 x86/sha1-ni-asm.o
+			 x86/sha1-ni-asm.o \
+			 x86/sha1-phe-asm.o
 endif # CONFIG_CRYPTO_LIB_SHA1_ARCH
 
 ################################################################################
lib/crypto/x86/sha1-phe-asm.S new file +71 -0

71 lines not on this page · raw message

lib/crypto/x86/sha1.h +20 -0
index c48a0131f..670109c79 100644
@@ -48,6 +48,23 @@ static void sha1_blocks_avx2(struct sha1_block_state *state,
 	}
 }
 
+#define PHE_ALIGNMENT 16
+asmlinkage void sha1_transform_phe(u8 *state, const u8 *data, size_t nblocks);
+static void sha1_blocks_phe(struct sha1_block_state *state,
+			     const u8 *data, size_t nblocks)
+{
+	/*
+	 * XSHA1 requires %edi to point to a 32-byte, 16-byte-aligned
+	 * buffer on Zhaoxin processors.
+	 */
+	u8 buf[32 + PHE_ALIGNMENT - 1];
+	u8 *dst = PTR_ALIGN(&buf[0], PHE_ALIGNMENT);
+
+	memcpy(dst, (u8 *)(state), SHA1_DIGEST_SIZE);
+	sha1_transform_phe(dst, data, nblocks);
+	memcpy((u8 *)(state), dst, SHA1_DIGEST_SIZE);
+}
+
 static void sha1_blocks(struct sha1_block_state *state,
 			const u8 *data, size_t nblocks)
 {
@@ -59,6 +76,9 @@ static void sha1_mod_init_arch(void)
 {
 	if (boot_cpu_has(X86_FEATURE_SHA_NI)) {
 		static_call_update(sha1_blocks_x86, sha1_blocks_ni);
+	} else if (boot_cpu_has(X86_FEATURE_PHE) && boot_cpu_has(X86_FEATURE_PHE_EN)) {
+		if (cpu_data(0).x86 >= 0x07)
+			static_call_update(sha1_blocks_x86, sha1_blocks_phe);
 	} else if (cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
 				     NULL) &&
 		   boot_cpu_has(X86_FEATURE_AVX)) {
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v2 1/2] lib/crypto: x86/sha1: PHE Extensions optimized SHA1 transform function

[+Cc [email protected]]

Quoted 81 lines · AlanSong-oc
lib/crypto/Makefile +2 -1
index d2845b214..069069377 100644
@@ -205,7 +205,8 @@ endif
 libsha1-$(CONFIG_SPARC) += sparc/sha1_asm.o
 libsha1-$(CONFIG_X86) += x86/sha1-ssse3-and-avx.o \
 			 x86/sha1-avx2-asm.o \
-			 x86/sha1-ni-asm.o
+			 x86/sha1-ni-asm.o \
+			 x86/sha1-phe-asm.o
 endif # CONFIG_CRYPTO_LIB_SHA1_ARCH
 
 ################################################################################
lib/crypto/x86/sha1-phe-asm.S new file +61 -0
new file mode 100644
index 000000000..eff086104
@@ -0,0 +1,71 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * PHE Extensions optimized implementation of a SHA-1 update function
+ *
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 	* Redistributions of source code must retain the above copyright
+ * 	  notice, this list of conditions and the following disclaimer.
+ * 	* Redistributions in binary form must reproduce the above copyright
+ * 	  notice, this list of conditions and the following disclaimer in
+ * 	  the documentation and/or other materials provided with the
+ * 	  distribution.
+ * 	* Neither the name of Intel Corporation nor the names of its
+ * 	  contributors may be used to endorse or promote products derived
+ * 	  from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <linux/linkage.h>
+
+/*
+ * PHE Extensions optimized implementation of a SHA-1 block function
+ *
+ * This function takes a pointer to the current SHA-1 state, a pointer to the
+ * input data, and the number of 64-byte blocks to process.  The number of
+ * blocks to process is assumed to be nonzero.  Once all blocks have been
+ * processed, the state is updated with the new state.  This function only
+ * processes complete blocks.  State initialization, buffering of partial
+ * blocks, and digest finalization are expected to be handled elsewhere.
+ *
+ * void sha1_transform_phe(u8 *state, const u8 *data, size_t nblocks)

lib/crypto/x86/sha1-phe-asm.S line 71

Quoted 10 lines
lib/crypto/x86/sha1-phe-asm.S new file +10 -0
+ */
+.text
+SYM_FUNC_START(sha1_transform_phe)
+	mov		$-1, %rax
+	mov		%rdx, %rcx
+
+	.byte	0xf3,0x0f,0xa6,0xc8
+
+	RET
+SYM_FUNC_END(sha1_transform_phe)

Please make this an inline asm statement instead of using a .S file. It's just one instruction.

lib/crypto/x86/sha1-phe-asm.S

Quoted 9 lines
lib/crypto/x86/sha1-phe-asm.S new file +9 -0
+#define PHE_ALIGNMENT 16
+asmlinkage void sha1_transform_phe(u8 *state, const u8 *data, size_t nblocks);
+static void sha1_blocks_phe(struct sha1_block_state *state,
+			     const u8 *data, size_t nblocks)
+{
+	/*
+	 * XSHA1 requires %edi to point to a 32-byte, 16-byte-aligned
+	 * buffer on Zhaoxin processors.
+	 */

What is the largest 'nblocks' that the instruction supports?

What happens if the instruction is interrupted partway through? Does the CPU correctly resume it in all cases?

Is it supported in both 32-bit and 64-bit modes? Your patch doesn't check for CONFIG_64BIT. Should it? New optimized assembly code generally should be 64-bit only.

Where is this instruction specified? Please add a comment that links to the specification.

lib/crypto/x86/sha1-phe-asm.S

Quoted 7 lines
lib/crypto/x86/sha1-phe-asm.S new file +7 -0
+	u8 buf[32 + PHE_ALIGNMENT - 1];
+	u8 *dst = PTR_ALIGN(&buf[0], PHE_ALIGNMENT);
+
+	memcpy(dst, (u8 *)(state), SHA1_DIGEST_SIZE);
+	sha1_transform_phe(dst, data, nblocks);
+	memcpy((u8 *)(state), dst, SHA1_DIGEST_SIZE);
+}

The casts to 'u8 *' are unnecessary.

lib/crypto/x86/sha1-phe-asm.S static void sha1_mod_init_arch(void) · line 81

Quoted 11 lines
lib/crypto/x86/sha1-phe-asm.S new file +4 -0
+
 static void sha1_blocks(struct sha1_block_state *state,
 			const u8 *data, size_t nblocks)
 {
@@ -59,6 +76,9 @@ static void sha1_mod_init_arch(void)
 {
 	if (boot_cpu_has(X86_FEATURE_SHA_NI)) {
 		static_call_update(sha1_blocks_x86, sha1_blocks_ni);
+	} else if (boot_cpu_has(X86_FEATURE_PHE) && boot_cpu_has(X86_FEATURE_PHE_EN)) {
+		if (cpu_data(0).x86 >= 0x07)
+			static_call_update(sha1_blocks_x86, sha1_blocks_phe);

Check IS_ENABLED(CONFIG_CPU_SUP_ZHAOXIN) first, so that the code gets compiled out when support for Zhaoxin CPUs isn't included in the kernel.

There are hardly any mentions of 'cpu_data(0).x86' in the kernel. I think you mean 'boot_cpu_data.x86', which is used much more frequently.

What is the difference between X86_FEATURE_PHE and X86_FEATURE_PHE_EN, and why are both needed?

All these comments apply to the SHA-256 patch too.

- Eric

#3 lore raw

Re: [PATCH v2 1/2] lib/crypto: x86/sha1: PHE Extensions optimized SHA1 transform function

Quoted 90 lines · Eric Biggers

[+Cc [email protected]]

Quoted 86 lines · AlanSong-oc
lib/crypto/Makefile +2 -1
index d2845b214..069069377 100644
@@ -205,7 +205,8 @@ endif
 libsha1-$(CONFIG_SPARC) += sparc/sha1_asm.o
 libsha1-$(CONFIG_X86) += x86/sha1-ssse3-and-avx.o \
                       x86/sha1-avx2-asm.o \
-                      x86/sha1-ni-asm.o
+                      x86/sha1-ni-asm.o \
+                      x86/sha1-phe-asm.o
 endif # CONFIG_CRYPTO_LIB_SHA1_ARCH
 
 ################################################################################
lib/crypto/x86/sha1-phe-asm.S new file +66 -0
new file mode 100644
index 000000000..eff086104
@@ -0,0 +1,71 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * PHE Extensions optimized implementation of a SHA-1 update function
+ *
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * GPL LICENSE SUMMARY
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * BSD LICENSE
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ *   * Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   * Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in
+ *     the documentation and/or other materials provided with the
+ *     distribution.
+ *   * Neither the name of Intel Corporation nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <linux/linkage.h>
+
+/*
+ * PHE Extensions optimized implementation of a SHA-1 block function
+ *
+ * This function takes a pointer to the current SHA-1 state, a pointer to the
+ * input data, and the number of 64-byte blocks to process.  The number of
+ * blocks to process is assumed to be nonzero.  Once all blocks have been
+ * processed, the state is updated with the new state.  This function only
+ * processes complete blocks.  State initialization, buffering of partial
+ * blocks, and digest finalization are expected to be handled elsewhere.
+ *
+ * void sha1_transform_phe(u8 *state, const u8 *data, size_t nblocks)
+ */
+.text
+SYM_FUNC_START(sha1_transform_phe)
+     mov             $-1, %rax
+     mov             %rdx, %rcx
Quoted 8 lines

lib/crypto/x86/sha1-phe-asm.S line 71

Quoted 5 lines
lib/crypto/x86/sha1-phe-asm.S new file +5 -0
+
+     .byte   0xf3,0x0f,0xa6,0xc8
+
+     RET
+SYM_FUNC_END(sha1_transform_phe)

Please make this an inline asm statement instead of using a .S file. It's just one instruction.

I will implement XSHA1 and XSHA256 instructions supported by the inline asm statement in the next version of the patch.

Quoted 12 lines

lib/crypto/x86/sha1-phe-asm.S

Quoted 10 lines
lib/crypto/x86/sha1-phe-asm.S new file +9 -0
+#define PHE_ALIGNMENT 16
+asmlinkage void sha1_transform_phe(u8 *state, const u8 *data, size_t nblocks);
+static void sha1_blocks_phe(struct sha1_block_state *state,
+                          const u8 *data, size_t nblocks)
+{
+     /*
+      * XSHA1 requires %edi to point to a 32-byte, 16-byte-aligned
+      * buffer on Zhaoxin processors.
+      */

What is the largest 'nblocks' that the instruction supports?

According to the instruction specification, the maximum input data size for the XSHA1 and XSHA256 instructions is limited by the maximum value of CX, ECX, or RCX, depending on the operation mode. Accordingly, the maximum value of 'nblocks'is subject to the same limitation.

Quoted 2 lines

What happens if the instruction is interrupted partway through? Does the CPU correctly resume it in all cases?

The specification states that XSHA1 and XSHA256 instructions are interruptible. If an interrupt or exception occurs during execution, the instruction can be correctly resumed after the interrupt or exception has been handled.

Quoted 3 lines

Is it supported in both 32-bit and 64-bit modes? Your patch doesn't check for CONFIG_64BIT. Should it? New optimized assembly code generally should be 64-bit only.

The XSHA1 and XSHA256 are supported in both 32-bit and 64-bit modes. Since newly optimized assembly code is typically 64-bit only, and XSHA1 and XSHA256 fully support 64-bit mode, an explicit CONFIG_64BIT check should not required.

Quoted 2 lines

Where is this instruction specified? Please add a comment that links to the specification.

The instruction specification is available at the following link.(https://gitee.com/openzhaoxin/zhaoxin_specifications/blob/20260112/ZX_Padlock_Reference.pdf)

Quoted 10 lines

lib/crypto/x86/sha1-phe-asm.S

Quoted 8 lines
lib/crypto/x86/sha1-phe-asm.S new file +7 -0
+     u8 buf[32 + PHE_ALIGNMENT - 1];
+     u8 *dst = PTR_ALIGN(&buf[0], PHE_ALIGNMENT);
+
+     memcpy(dst, (u8 *)(state), SHA1_DIGEST_SIZE);
+     sha1_transform_phe(dst, data, nblocks);
+     memcpy((u8 *)(state), dst, SHA1_DIGEST_SIZE);
+}

The casts to 'u8 *' are unnecessary.

I will eliminate the unnecessary cast in the next version of the patch.

Quoted 15 lines

lib/crypto/x86/sha1-phe-asm.S static void sha1_mod_init_arch(void) · line 81

Quoted 11 lines
lib/crypto/x86/sha1-phe-asm.S new file +4 -0
+
 static void sha1_blocks(struct sha1_block_state *state,
                      const u8 *data, size_t nblocks)
 {
@@ -59,6 +76,9 @@ static void sha1_mod_init_arch(void)
 {
      if (boot_cpu_has(X86_FEATURE_SHA_NI)) {
              static_call_update(sha1_blocks_x86, sha1_blocks_ni);
+     } else if (boot_cpu_has(X86_FEATURE_PHE) && boot_cpu_has(X86_FEATURE_PHE_EN)) {
+             if (cpu_data(0).x86 >= 0x07)
+                     static_call_update(sha1_blocks_x86, sha1_blocks_phe);

Check IS_ENABLED(CONFIG_CPU_SUP_ZHAOXIN) first, so that the code gets compiled out when support for Zhaoxin CPUs isn't included in the kernel.

Quoted 2 lines

There are hardly any mentions of 'cpu_data(0).x86' in the kernel. I think you mean 'boot_cpu_data.x86', which is used much more frequently.

I will add CONFIG_CPU_SUP_ZHAOXIN check in the relevant code paths and use 'boot_cpu_data.x86' to identify the CPU family instead of 'cpu_data(0).x86.'

Quoted 4 lines

What is the difference between X86_FEATURE_PHE and X86_FEATURE_PHE_EN, and why are both needed?

All these comments apply to the SHA-256 patch too.

The X86_FEATURE_PHE indicates the presence of the XSHA1 and XSHA256 instructions, whereas the X86_FEATURE_PHE_EN indicates that these instructions are enabled for normal use. Surely, all of the suggestions mentioned above will also apply to the SHA-256 patch.

Please accept my apologies for the delayed response due to administrative procedures. Thank you for your review and valuable suggestions.

Best Regards
AlanSong-oc

maintainer
#4 lore raw

Re: [PATCH v2 1/2] lib/crypto: x86/sha1: PHE Extensions optimized SHA1 transform function

Quoted 8 lines · AlanSong-oc
Quoted 3 lines

Is it supported in both 32-bit and 64-bit modes? Your patch doesn't check for CONFIG_64BIT. Should it? New optimized assembly code generally should be 64-bit only.

The XSHA1 and XSHA256 are supported in both 32-bit and 64-bit modes. Since newly optimized assembly code is typically 64-bit only, and XSHA1 and XSHA256 fully support 64-bit mode, an explicit CONFIG_64BIT check should not required.

Right, all the x86-optimized SHA-1 and SHA-256 code is already 64-bit specific, due to CONFIG_CRYPTO_LIB_SHA1_ARCH and CONFIG_CRYPTO_LIB_SHA256_ARCH being enabled only when CONFIG_x86_64=y. So there's no need to check for 64-bit again.

Quoted 6 lines
Quoted 2 lines

What is the difference between X86_FEATURE_PHE and X86_FEATURE_PHE_EN, and why are both needed?

The X86_FEATURE_PHE indicates the presence of the XSHA1 and XSHA256 instructions, whereas the X86_FEATURE_PHE_EN indicates that these instructions are enabled for normal use.

I still don't understand the difference.

If you look at the other CPU feature flags, like X86_FEATURE_SHA_NI for example, there's just a single flag for the feature. We don't have X86_FEATURE_SHA_NI and X86_FEATURE_SHA_NI_EN. If the CPU supports the feature but the kernel decides it can't or shouldn't be used for whatever reason, the kernel just doesn't set the flag. There's no separate flag that tracks the CPU support independently.

Why can't the PHE flag work the same way?

- Eric

#5 lore raw

Re: [PATCH v2 1/2] lib/crypto: x86/sha1: PHE Extensions optimized SHA1 transform function

Quoted 25 lines · Eric Biggers
Quoted 8 lines · AlanSong-oc
Quoted 3 lines

Is it supported in both 32-bit and 64-bit modes? Your patch doesn't check for CONFIG_64BIT. Should it? New optimized assembly code generally should be 64-bit only.

The XSHA1 and XSHA256 are supported in both 32-bit and 64-bit modes. Since newly optimized assembly code is typically 64-bit only, and XSHA1 and XSHA256 fully support 64-bit mode, an explicit CONFIG_64BIT check should not required.

Right, all the x86-optimized SHA-1 and SHA-256 code is already 64-bit specific, due to CONFIG_CRYPTO_LIB_SHA1_ARCH and CONFIG_CRYPTO_LIB_SHA256_ARCH being enabled only when CONFIG_x86_64=y. So there's no need to check for 64-bit again.

Quoted 6 lines
Quoted 2 lines

What is the difference between X86_FEATURE_PHE and X86_FEATURE_PHE_EN, and why are both needed?

The X86_FEATURE_PHE indicates the presence of the XSHA1 and XSHA256 instructions, whereas the X86_FEATURE_PHE_EN indicates that these instructions are enabled for normal use.

I still don't understand the difference.

Quoted 8 lines

If you look at the other CPU feature flags, like X86_FEATURE_SHA_NI for example, there's just a single flag for the feature. We don't have X86_FEATURE_SHA_NI and X86_FEATURE_SHA_NI_EN. If the CPU supports the feature but the kernel decides it can't or shouldn't be used for whatever reason, the kernel just doesn't set the flag. There's no separate flag that tracks the CPU support independently.

Why can't the PHE flag work the same way?

Unlike most x86 CPU features, the PHE extension uses two bits to describe a single feature: a present bit and an enable bit. On Zhaoxin processors, these two bits are always identical. Therefore, in the next revision of this patch, I will only check X86_FEATURE_PHE_EN, as with other CPU support checks. Thanks for the suggestion.

Best Regards
AlanSong-oc

#6 lore raw

PATCH v2 2/2 lib/crypto: x86/sha256: PHE Extensions optimized SHA256 transform function

In reply to AlanSong-oc

Zhaoxin CPUs have implemented the SHA(Secure Hash Algorithm) as its CPU instructions by PHE(Padlock Hash Engine) Extensions, including XSHA1, XSHA256, XSHA384 and XSHA512 instructions.

With the help of implementation of SHA in hardware instead of software, can develop applications with higher performance, more security and more flexibility.

This patch includes the XSHA256 instruction optimized implementation of SHA-256 transform function.

---
 lib/crypto/Makefile             |  3 +-
 lib/crypto/x86/sha256-phe-asm.S | 70 +++++++++++++++++++++++++++++++++
 lib/crypto/x86/sha256.h         | 20 ++++++++++
 3 files changed, 92 insertions(+), 1 deletion(-)
 create mode 100644 lib/crypto/x86/sha256-phe-asm.S
lib/crypto/Makefile +2 -1
index 069069377..f56c6e6c7 100644
@@ -236,7 +236,8 @@ libsha256-$(CONFIG_SPARC) += sparc/sha256_asm.o
 libsha256-$(CONFIG_X86) += x86/sha256-ssse3-asm.o \
 			   x86/sha256-avx-asm.o \
 			   x86/sha256-avx2-asm.o \
-			   x86/sha256-ni-asm.o
+			   x86/sha256-ni-asm.o \
+			   x86/sha256-phe-asm.o
 endif # CONFIG_CRYPTO_LIB_SHA256_ARCH
 
 ################################################################################
lib/crypto/x86/sha256-phe-asm.S new file +70 -0

70 lines not on this page · raw message

lib/crypto/x86/sha256.h +20 -0
index 38e33b22a..072b44480 100644
@@ -31,6 +31,23 @@ DEFINE_X86_SHA256_FN(sha256_blocks_avx, sha256_transform_avx);
 DEFINE_X86_SHA256_FN(sha256_blocks_avx2, sha256_transform_rorx);
 DEFINE_X86_SHA256_FN(sha256_blocks_ni, sha256_ni_transform);
 
+#define PHE_ALIGNMENT 16
+asmlinkage void sha256_transform_phe(u8 *state, const u8 *data, size_t nblocks);
+static void sha256_blocks_phe(struct sha256_block_state *state,
+			     const u8 *data, size_t nblocks)
+{
+	/*
+	 * XSHA256 requires %edi to point to a 32-byte, 16-byte-aligned
+	 * buffer on Zhaoxin processors.
+	 */
+	u8 buf[32 + PHE_ALIGNMENT - 1];
+	u8 *dst = PTR_ALIGN(&buf[0], PHE_ALIGNMENT);
+
+	memcpy(dst, (u8 *)(state), SHA256_DIGEST_SIZE);
+	sha256_transform_phe(dst, data, nblocks);
+	memcpy((u8 *)(state), dst, SHA256_DIGEST_SIZE);
+}
+
 static void sha256_blocks(struct sha256_block_state *state,
 			  const u8 *data, size_t nblocks)
 {
@@ -79,6 +96,9 @@ static void sha256_mod_init_arch(void)
 	if (boot_cpu_has(X86_FEATURE_SHA_NI)) {
 		static_call_update(sha256_blocks_x86, sha256_blocks_ni);
 		static_branch_enable(&have_sha_ni);
+	} else if (boot_cpu_has(X86_FEATURE_PHE) && boot_cpu_has(X86_FEATURE_PHE_EN)) {
+		if (cpu_data(0).x86 >= 0x07)
+			static_call_update(sha256_blocks_x86, sha256_blocks_phe);
 	} else if (cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
 				     NULL) &&
 		   boot_cpu_has(X86_FEATURE_AVX)) {
Signature · 1 line
2.34.1
maintainer
#7 lore raw
In reply to AlanSong-oc
Quoted 10 lines · AlanSong-oc

For Zhaoxin processors, the XSHA1 instruction requires the total memory allocated at %rdi register must be 32 bytes, while the XSHA1 and XSHA256 instruction doesn't perform any operation when %ecx is zero.

Due to these requirements, the current padlock-sha driver does not work correctly with Zhaoxin processors. It cannot pass the self-tests and therefore does not activate the driver on Zhaoxin processors. This issue has been reported in Debian [1]. The self-tests fail with the following messages [2]:

Quoted 7 lines

alg: shash: sha1-padlock-nano test failed (wrong result) on test vector 0, cfg="init+update+final aligned buffer"
alg: self-tests for sha1 using sha1-padlock-nano failed (rc=-22)

------------[ cut here ]------------

alg: shash: sha256-padlock-nano test failed (wrong result) on test vector 0, cfg="init+update+final aligned buffer"
alg: self-tests for sha256 using sha256-padlock-nano failed (rc=-22)

------------[ cut here ]------------

This cover letter is misleading, as those self-test failures will still exist regardless of this patch series.

Quoted 6 lines

To enable XSHA1 and XSHA256 instruction support on Zhaoxin processors, this series adds PHE Extensions support to lib/crypto for SHA-1 and SHA-256, following the suggestion in [3].

v1 link is below:
https://lore.kernel.org/linux-crypto/[email protected]/

Please run the sha1 and sha256 KUnit test suites (CRYPTO_LIB_SHA1_KUNIT_TEST and CRYPTO_LIB_SHA256_KUNIT_TEST) before and after this series, with the benchmark enabled (CRYPTO_LIB_BENCHMARK), and show the results. For this series to be considered, the tests need to pass and there needs to be a significant performance improvement.

- Eric

#8 lore raw
Quoted 16 lines · Eric Biggers
Quoted 14 lines · AlanSong-oc

For Zhaoxin processors, the XSHA1 instruction requires the total memory allocated at %rdi register must be 32 bytes, while the XSHA1 and XSHA256 instruction doesn't perform any operation when %ecx is zero.

Due to these requirements, the current padlock-sha driver does not work correctly with Zhaoxin processors. It cannot pass the self-tests and therefore does not activate the driver on Zhaoxin processors. This issue has been reported in Debian [1]. The self-tests fail with the following messages [2]:

alg: shash: sha1-padlock-nano test failed (wrong result) on test vector 0, cfg="init+update+final aligned buffer"
alg: self-tests for sha1 using sha1-padlock-nano failed (rc=-22)

------------[ cut here ]------------
Quoted 6 lines
Quoted 3 lines

alg: shash: sha256-padlock-nano test failed (wrong result) on test vector 0, cfg="init+update+final aligned buffer"
alg: self-tests for sha256 using sha256-padlock-nano failed (rc=-22)

------------[ cut here ]------------

This cover letter is misleading, as those self-test failures will still exist regardless of this patch series.

Indeed, those self-test failures will still occur even with this patch series. I will add a separate commit in this series to restrict probing of the padlock-sha driver on Zhaoxin processors based on the CPU family, and explain the reason for those failures in that commit.

Quoted 4 lines
Quoted 4 lines

To enable XSHA1 and XSHA256 instruction support on Zhaoxin processors, this series adds PHE Extensions support to lib/crypto for SHA-1 and SHA-256, following the suggestion in [3].

Quoted 8 lines
Quoted 2 lines

v1 link is below:
https://lore.kernel.org/linux-crypto/[email protected]/

Please run the sha1 and sha256 KUnit test suites (CRYPTO_LIB_SHA1_KUNIT_TEST and CRYPTO_LIB_SHA256_KUNIT_TEST) before and after this series, with the benchmark enabled (CRYPTO_LIB_BENCHMARK), and show the results. For this series to be considered, the tests need to pass and there needs to be a significant performance improvement.

In the next revision of this patch, I will add the KUnit test suite results as well as benchmark results for SHA1 and SHA256.

Please accept my apologies for the delayed response due to administrative procedures. Thank you for your review and valuable suggestions.

Best Regards
AlanSong-oc