From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org
Cc: Ard Biesheuvel <ardb@kernel.org>,
"Jason A . Donenfeld" <Jason@zx2c4.com>,
Herbert Xu <herbert@gondor.apana.org.au>,
x86@kernel.org, Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH 1/4] lib/crypto: x86/sha512-avx2: Clean up loading and byte-swapping the data
Date: Sun, 22 Nov 2026 19:13:18 -0800 [thread overview]
Message-ID: <20261123031321.403308-2-ebiggers@kernel.org> (raw)
In-Reply-To: <20261123031321.403308-1-ebiggers@kernel.org>
When used on YMM registers, the vpshufb instruction does a shuffle
independently within each 128-bit lane, using the low 4 bits of each
byte in the corresponding 128-bit shuffle mask. Yet,
sha512_transform_rorx() executed vpshufb using a mask containing indices
from 0 to 31, as if it expected the shuffle to be across the entire YMM
register. That made the code misleading, though still correct.
Make the code clearer, eliminate 16 bytes of rodata, and increase
consistency with other asm files (e.g., AES-GCM) by broadcasting the
proper 128-bit shuffle mask into both halves of the YMM register.
Also apply a few related cleanups:
- Eliminate the unhelpful COPY_YMM_AND_BSWAP and VMOVDQ macros.
- Rename the register "alias" to YBSWAP to be consistent with the
naming convention used elsewhere in this file (and sha512-ssse3-asm.S)
- Make the data label local.
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
lib/crypto/x86/sha512-avx2-asm.S | 34 +++++++++++---------------------
1 file changed, 11 insertions(+), 23 deletions(-)
diff --git a/lib/crypto/x86/sha512-avx2-asm.S b/lib/crypto/x86/sha512-avx2-asm.S
index 2256d51297e18..d5f3c0bcee653 100644
--- a/lib/crypto/x86/sha512-avx2-asm.S
+++ b/lib/crypto/x86/sha512-avx2-asm.S
@@ -65,8 +65,7 @@ YTMP2 = %ymm2
YTMP3 = %ymm3
YTMP4 = %ymm8
XFER = YTMP0
-
-BYTE_FLIP_MASK = %ymm9
+YBSWAP = %ymm9
# 1st arg is %rdi, which is saved to the stack and accessed later via %r12
CTX1 = %rdi
@@ -110,9 +109,6 @@ frame_INPEND = frame_INP + INP_SIZE
frame_CTX = frame_INPEND + INPEND_SIZE
frame_size = frame_CTX + CTX_SIZE
-## assume buffers not aligned
-#define VMOVDQ vmovdqu
-
# addm [mem], reg
# Add reg to mem using reg-mem add and store
.macro addm p1 p2
@@ -120,13 +116,6 @@ frame_size = frame_CTX + CTX_SIZE
mov \p2, \p1
.endm
-
-# COPY_YMM_AND_BSWAP ymm, [mem], byte_flip_mask
-# Load ymm with mem and byte swap each dword
-.macro COPY_YMM_AND_BSWAP p1 p2 p3
- VMOVDQ \p2, \p1
- vpshufb \p3, \p1, \p1
-.endm
# rotate_Ys
# Rotate values of symbols Y0...Y3
.macro rotate_Ys
@@ -599,16 +588,16 @@ SYM_FUNC_START(sha512_transform_rorx)
# save %rdi (CTX) before it gets clobbered
mov %rdi, frame_CTX(%rsp)
- vmovdqa PSHUFFLE_BYTE_FLIP_MASK(%rip), BYTE_FLIP_MASK
+ vbroadcasti128 .L_XMM_QWORD_BSWAP(%rip), YBSWAP
.Lloop0:
lea K512(%rip), TBL
- ## byte swap first 16 dwords
- COPY_YMM_AND_BSWAP Y_0, (INP), BYTE_FLIP_MASK
- COPY_YMM_AND_BSWAP Y_1, 1*32(INP), BYTE_FLIP_MASK
- COPY_YMM_AND_BSWAP Y_2, 2*32(INP), BYTE_FLIP_MASK
- COPY_YMM_AND_BSWAP Y_3, 3*32(INP), BYTE_FLIP_MASK
+ # Load and byte-swap W[0..15]
+.irp i, 0,1,2,3
+ vmovdqu \i*32(INP), Y_\i
+ vpshufb YBSWAP, Y_\i, Y_\i
+.endr
mov INP, frame_INP(%rsp)
@@ -735,12 +724,11 @@ K512:
.quad 0x4cc5d4becb3e42b6,0x597f299cfc657e2a
.quad 0x5fcb6fab3ad6faec,0x6c44198c4a475817
-.section .rodata.cst32.PSHUFFLE_BYTE_FLIP_MASK, "aM", @progbits, 32
-.align 32
-# Mask for byte-swapping a couple of qwords in an XMM register using (v)pshufb.
-PSHUFFLE_BYTE_FLIP_MASK:
+.section .rodata.cst16.XMM_QWORD_BSWAP, "aM", @progbits, 16
+.align 16
+# Mask for byte-swapping each qword using vpshufb
+.L_XMM_QWORD_BSWAP:
.octa 0x08090a0b0c0d0e0f0001020304050607
- .octa 0x18191a1b1c1d1e1f1011121314151617
.section .rodata.cst32.MASK_YMM_LO, "aM", @progbits, 32
.align 32
--
2.56.1
next prev parent reply other threads:[~2026-11-23 3:13 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 3:13 [PATCH 0/4] lib/crypto: Clean up and optimize sha512-avx2-asm.S Eric Biggers
2026-11-23 3:13 ` Eric Biggers [this message]
2026-11-23 3:13 ` [PATCH 2/4] lib/crypto: x86/sha512-avx2: Optimize message schedule Eric Biggers
2026-11-23 3:13 ` [PATCH 3/4] lib/crypto: x86/sha512-avx2: Factor out SHA512_Round Eric Biggers
2026-11-23 3:13 ` [PATCH 4/4] lib/crypto: x86/sha512-avx2: Optimize SHA512_Round Eric Biggers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261123031321.403308-2-ebiggers@kernel.org \
--to=ebiggers@kernel.org \
--cc=Jason@zx2c4.com \
--cc=ardb@kernel.org \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox