Skip to contentSkip to conversation

lib/crypto: sparc/aes-xts: Add optimization using the AES opcodes

PATCH Applied Stian Halseth 2 participants 2 messages Oct 4 – Oct 5

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 lib/crypto: sparc/aes-xts: Add optimization using the AES opcodes

Implement aes_xts_encrypt_arch() and aes_xts_decrypt_arch() with the AES opcodes, for AES-128 and AES-256, two blocks per iteration. AES-192, which IEEE 1619 does not specify, and data that is not 8-byte aligned are left to the generic code.

The tweak is kept as little-endian words, so that multiplying it by x is a 128-bit shift done with addcc and the VIS3 addxc. A little-endian store and an ordinary load through a stack slot give it in the byte order of the data, and the slot is cleared on return. The routines open a register window, as %g4-%g6 belong to the kernel.

With that, xts-aes-lib no longer has to be kept off SPARC. Register it there again, with the priority x86 and RISC-V use, so that it also outranks an xts(ecb-aes-sparc64) instance (priority 300).

On a T7-1 (M7), MiB/s unless noted:

xts(ecb-aes-sparc64) xts-aes-lib
AF_ALG, 64 KiB, AES-128 415 1092
AF_ALG, 64 KiB, AES-256 390 936
dm-crypt on brd, AES-256:

    sequential read                          1399         2559
    sequential write                         1472         3480
    4 KiB write latency, QD1 (us)            40.6         32.7

dm-crypt now matches aes-cbc-essiv on the same ramdisk.

On a T4-1, AF_ALG AES-256 goes from 238 to 544 MiB/s, and AES-128 from 252 to 616.

Tested with CONFIG_CRYPTO_SELFTESTS_FULL, and against OpenSSL over random keys, tweaks and lengths, on both machines.

---
v3:
- One macro for the four XTS routines, as in the RISC-V and x86 code (Eric). The object file is the same as with v2.

v2:
- Rebased on libcrypto-next, on top of 572af6872e52 ("crypto: aes - Fix undesired override of some optimized AES modes"), and register xts-aes-lib on SPARC again
- Give it the priority x86 and RISC-V use
- Drop Fixes:, Closes: -> Link:
- Commit message compares with the xts template only

QEMU does not have the SPARC crypto opcodes yet. That is being discussed on qemu-devel [1] and tracked in sparclinux#77 [2].

[1] https://lists.gnu.org/archive/html/qemu-devel/2026-09/msg09494.html
[2] https://github.com/sparclinux/issues/issues/77

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

 arch/sparc/include/asm/opcodes.h |   6 ++
 crypto/aes.c                     |   9 +-
 lib/crypto/sparc/aes.h           |  75 ++++++++++++++
 lib/crypto/sparc/aes_asm.S       | 165 +++++++++++++++++++++++++++++++
 4 files changed, 248 insertions(+), 7 deletions(-)
arch/sparc/include/asm/opcodes.h +6 -0
index ebfda6eb49b26..f1cb1a26be8fc 100644
@@ -96,5 +96,11 @@
 	.word	0xbbb02303;
 #define MOVXTOD_G7_F62		\
 	.word	0xbfb02307;
+#define MOVXTOD_L4_F56		\
+	.word	0xb3b02314;
+#define MOVXTOD_L5_F58		\
+	.word	0xb7b02315;
+#define ADDXC_L1_L1_L1		\
+	.word	0xa3b44231;
 
 #endif /* _SPARC_ASM_OPCODES_H */
crypto/aes.c +2 -7
index 73f3aa856be4e..9508a975d8eaf 100644
@@ -702,17 +702,12 @@ static struct skcipher_alg skcipher_algs[] = {
 		.decrypt = crypto_aes_xctr_crypt,
 	},
 #endif
-	/*
-	 * Don't register library-based "xts(aes)" on architectures where it
-	 * might block a "better" implementation from being instantiated via the
-	 * "xts" template.  This exclusion is temporary and will go away when
-	 * the library AES-XTS is optimized for SPARC.
-	 */
-#if IS_ENABLED(CONFIG_CRYPTO_XTS) && !IS_ENABLED(CONFIG_SPARC)
+#if IS_ENABLED(CONFIG_CRYPTO_XTS)
 	{
 		.base.cra_name = "xts(aes)",
 		.base.cra_driver_name = "xts-aes-lib",
 		.base.cra_priority = (IS_ENABLED(CONFIG_RISCV) ||
+				      IS_ENABLED(CONFIG_SPARC) ||
 				      IS_ENABLED(CONFIG_X86)) ? 500 : 110,
 		.base.cra_blocksize = AES_BLOCK_SIZE,
 		.base.cra_ctxsize = sizeof(struct aes_xts_key),
lib/crypto/sparc/aes.h +75 -0

81 lines not on this page · raw message

lib/crypto/sparc/aes_asm.S +165 -0

174 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer Applied
#1 lore raw
Quoted 27 lines · Stian Halseth

Implement aes_xts_encrypt_arch() and aes_xts_decrypt_arch() with the AES opcodes, for AES-128 and AES-256, two blocks per iteration. AES-192, which IEEE 1619 does not specify, and data that is not 8-byte aligned are left to the generic code.

The tweak is kept as little-endian words, so that multiplying it by x is a 128-bit shift done with addcc and the VIS3 addxc. A little-endian store and an ordinary load through a stack slot give it in the byte order of the data, and the slot is cleared on return. The routines open a register window, as %g4-%g6 belong to the kernel.

With that, xts-aes-lib no longer has to be kept off SPARC. Register it there again, with the priority x86 and RISC-V use, so that it also outranks an xts(ecb-aes-sparc64) instance (priority 300).

On a T7-1 (M7), MiB/s unless noted:

xts(ecb-aes-sparc64) xts-aes-lib
AF_ALG, 64 KiB, AES-128 415 1092
AF_ALG, 64 KiB, AES-256 390 936
dm-crypt on brd, AES-256:

    sequential read                          1399         2559
    sequential write                         1472         3480
    4 KiB write latency, QD1 (us)            40.6         32.7

dm-crypt now matches aes-cbc-essiv on the same ramdisk.

Quoted 8 lines

On a T4-1, AF_ALG AES-256 goes from 238 to 544 MiB/s, and AES-128 from 252 to 616.

Tested with CONFIG_CRYPTO_SELFTESTS_FULL, and against OpenSSL over random keys, tweaks and lengths, on both machines.

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git/log/?h=libcrypto-next

- Eric