Enable libgcrypt HW Acceleration
cover Enable libgcrypt HW Acceleration
Git repo:
https://github.com/lcp/grub2/tree/libgcrypt-1.11.0-argon2-hw-accel-upstream
This patchset is based on Vladimir's libgcrypt 1.11.0 patches.
With the newer libgcrypt code, it's possible to drop the hardware acceleration code into libgcrypt-grub. I managed to build the sha256 and blake2 modules with the available optimization code for grub-emu, and the result looks promising. For a 20MB LUKS2 block with pbkdf2 (sha256), the original decryption time is 10~11 seconds. With intel shaext, it becomes 3~4 seconds.
I also have a tentative implementation for Argon2. With AVX2 support, the decryption time is down from 26 seconds to 20 seconds.
Of course, this patchset is not for any production use yet, but I'd like to demostrate the benefit to upgrade libgcryt!
Gary Lin (6):
tweak autoconf/automake files to detect x86_64 features Copy the x86_64 optimization files
libgcrypt-grub: implement _gcry_get_hw_features()
Build blake2 and sha256 with x86_64 optimization assembly code
libgcrypt-grub: fix a typo in blake2.c
libgcrypt-grub: fix missing-prototypes error in sha256-intel-shaext.c
autogen.sh | 7 + conf/Makefile.common | 2 + configure.ac | 254 ++++++++++++++++++ .../libgcrypt-patches/08_add_hwfeatures.patch | 51 ++++ .../09_build_x86_64_opt_code.patch | 80 ++++++ .../10_fix_blake2_typo.patch | 28 ++ .../11_fix_build_shaext.patch | 30 +++ include/grub/crypto.h | 4 + 8 files changed, 456 insertions(+) create mode 100644 grub-core/lib/libgcrypt-patches/08_add_hwfeatures.patch create mode 100644 grub-core/lib/libgcrypt-patches/09_build_x86_64_opt_code.patch create mode 100644 grub-core/lib/libgcrypt-patches/10_fix_blake2_typo.patch create mode 100644 grub-core/lib/libgcrypt-patches/11_fix_build_shaext.patch
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
RFC PATCH 1/6 tweak autoconf/automake files to detect x86_64 features
Borrow the code from libgcrypt to check whether the compiler supports the assembly instructions for SSSE3, SHA extensions, SSSE4.1, AVX, AVX2, AVX512, and BMI2.
Also tweak _gcry_get_hw_features() in include/grub/crypto.h as the preparation to support the hardware feature detection function from libgcrypt.
--- conf/Makefile.common | 2 + configure.ac | 254 ++++++++++++++++++++++++++++++++++++++++++ include/grub/crypto.h | 4 + 3 files changed, 260 insertions(+)
conf/Makefile.common +2 -0
@@ -24,6 +24,8 @@ if COND_HAVE_PCI CFLAGS_PLATFORM += -DGRUB_HAS_PCI endif +CPPFLAGS_GCRY_ASM = @CPPFLAGS_GCRY_ASM@ + # Other options CPPFLAGS_DEFAULT = -DGRUB_FILE=\"$(subst $(srcdir)/,,$<)\"
configure.ac +254 -0
266 lines not on this page · raw message
include/grub/crypto.h +4 -0
@@ -585,10 +585,14 @@ unsigned int _gcry_ct_not_memequal (const void *b1, const void *b2, grub_size_t len); +#ifdef GRUB_UTIL static inline unsigned int _gcry_get_hw_features (void ) {return 0 ; } +#else +extern unsigned int _gcry_get_hw_features (void ); +#endif void *_gcry_malloc (grub_size_t n);void *_gcry_malloc_secure (grub_size_t n);
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
RFC PATCH 2/6 Copy the x86_64 optimization files
In reply to Gary Lin via Grub-devel--- autogen.sh | 7 +++++++ 1 file changed, 7 insertions(+)
autogen.sh +7 -0
@@ -52,6 +52,13 @@ for x in mpi-asm-defs.h mpih-add1.c mpih-sub1.c mpih-mul1.c mpih-mul2.c mpih-mul cp grub-core/lib/libgcrypt-grub/mpi/generic/"$x" grub-core/lib/libgcrypt-grub/mpi/"$x" done +for x in blake2b-amd64-avx2.S blake2b-amd64-avx512.S blake2s-amd64-avx.S blake2s-amd64-avx512.S sha256-ssse3-amd64.S sha256-avx-amd64.S sha256-avx2-bmi2-amd64.S sha256-intel-shaext.c; do + if [ -h grub-core/lib/libgcrypt-grub/cipher/"$x" ] || [ -f grub-core/lib/libgcrypt-grub/cipher/"$x" ]; then + rm grub-core/lib/libgcrypt-grub/cipher/"$x" + fi + cp grub-core/lib/libgcrypt/cipher/"$x" grub-core/lib/libgcrypt-grub/cipher/"$x" +done + for x in grub-core/lib/libgcrypt-patches/*.patch; do patch -i $x -p1 done
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
RFC PATCH 3/6 libgcrypt-grub: implement _gcry_get_hw_features()
In reply to Gary Lin via Grub-develImplement _gcry_get_hw_features() with the hardware features detection function from libgcryt.
--- .../libgcrypt-patches/08_add_hwfeatures.patch | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 grub-core/lib/libgcrypt-patches/08_add_hwfeatures.patch
grub-core/lib/libgcrypt-patches/08_add_hwfeatures.patch new file +51 -0
@@ -0,0 +1,51 @@ +From 146823bd16958d6b8380c79b6ec88b89b633f0ee Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Fri, 18 Jul 2025 15:21:51 +0800 +Subject: [PATCH 1/2] libgcrypt-grub: implement _gcry_get_hw_features() + +Implement _gcry_get_hw_features() with the hardware features detection +function from libgcryt. + +Signed-off-by: Gary Lin <[email protected]> +--- + grub-core/lib/libgcrypt-grub/src/hwfeatures.c | 28 +++++++++++++++++++ + 1 file changed, 28 insertions(+) + create mode 100644 grub-core/lib/libgcrypt-grub/src/hwfeatures.c + +diff --git a/grub-core/lib/libgcrypt-grub/src/hwfeatures.c b/grub-core/lib/libgcrypt-grub/src/hwfeatures.c +new file mode 100644 +index 000000000..33e754a2e +--- /dev/null ++++ b/grub-core/lib/libgcrypt-grub/src/hwfeatures.c +@@ -0,0 +1,28 @@ ++#include <config.h> ++#include <stdio.h> ++#include <ctype.h> ++#include <stdlib.h> ++#include <string.h> ++#include <stdarg.h> ++#include <unistd.h> ++ ++#include "g10lib.h" ++#include "hwf-common.h" ++ ++unsigned int ++_gcry_get_hw_features(void) ++{ ++ static bool detected; ++ static unsigned int hw_features; ++ ++ if (detected == true) ++ return hw_features; ++ ++#if defined(__x86_64__) ++ hw_features = _gcry_hwf_detect_x86(); ++#endif ++ ++ detected = true; ++ ++ return hw_features; ++} +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
RFC PATCH 4/6 Build blake2 and sha256 with x86_64 optimization assembly code
In reply to Gary Lin via Grub-develA tentative change to build blake2 and sha256 with x86_64 optimization code from libgcrypt
--- .../09_build_x86_64_opt_code.patch | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 grub-core/lib/libgcrypt-patches/09_build_x86_64_opt_code.patch
grub-core/lib/libgcrypt-patches/09_build_x86_64_opt_code.patch new file +80 -0
@@ -0,0 +1,80 @@ +From 4979228c02f1a93999645b685f99f4612edd6449 Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Fri, 18 Jul 2025 15:23:25 +0800 +Subject: [PATCH 2/2] Build blake2 and sha256 with x86_64 optimization assembly + code + +A tentative change to build blake2 and sha256 with x86_64 optimization +code from libgcrypt + +Signed-off-by: Gary Lin <[email protected]> +--- + grub-core/Makefile.gcry.def | 39 +++++++++++++++++++++++++++++++++++-- + 1 file changed, 37 insertions(+), 2 deletions(-) + +diff --git a/grub-core/Makefile.gcry.def b/grub-core/Makefile.gcry.def +index 6d9b7cb24..f2885cae3 100644 +--- a/grub-core/Makefile.gcry.def ++++ b/grub-core/Makefile.gcry.def +@@ -17,8 +17,20 @@ module = { + module = { + name = gcry_blake2; + common = lib/libgcrypt-grub/cipher/blake2.c; ++ x86_64 = lib/libgcrypt-grub/cipher/blake2b-amd64-avx2.S; ++ x86_64 = lib/libgcrypt-grub/cipher/blake2b-amd64-avx512.S; ++ x86_64 = lib/libgcrypt-grub/cipher/blake2s-amd64-avx.S; ++ x86_64 = lib/libgcrypt-grub/cipher/blake2s-amd64-avx512.S; ++ ++ emu = lib/libgcrypt-grub/cipher/blake2b-amd64-avx2.S; ++ emu = lib/libgcrypt-grub/cipher/blake2b-amd64-avx512.S; ++ emu = lib/libgcrypt-grub/cipher/blake2s-amd64-avx.S; ++ emu = lib/libgcrypt-grub/cipher/blake2s-amd64-avx512.S; ++ + cflags = '$(CFLAGS_GCRY)'; +- cppflags = '$(CPPFLAGS_GCRY)'; ++ cppflags = '$(CPPFLAGS_GCRY) $(CPPFLAGS_GCRY_ASM)'; ++ ++ emu_condition = COND_emu_hw_asm; + }; + + module = { +@@ -165,8 +177,20 @@ module = { + module = { + name = gcry_sha256; + common = lib/libgcrypt-grub/cipher/sha256.c; ++ x86_64 = lib/libgcrypt-grub/cipher/sha256-ssse3-amd64.S; ++ x86_64 = lib/libgcrypt-grub/cipher/sha256-avx-amd64.S; ++ x86_64 = lib/libgcrypt-grub/cipher/sha256-avx2-bmi2-amd64.S; ++ x86_64 = lib/libgcrypt-grub/cipher/sha256-intel-shaext.c; ++ ++ emu = lib/libgcrypt-grub/cipher/sha256-ssse3-amd64.S; ++ emu = lib/libgcrypt-grub/cipher/sha256-avx-amd64.S; ++ emu = lib/libgcrypt-grub/cipher/sha256-avx2-bmi2-amd64.S; ++ emu = lib/libgcrypt-grub/cipher/sha256-intel-shaext.c; ++ + cflags = '$(CFLAGS_GCRY) -Wno-cast-align'; +- cppflags = '$(CPPFLAGS_GCRY)'; ++ cppflags = '$(CPPFLAGS_GCRY) -DUSE_SHA256 $(CPPFLAGS_GCRY_ASM)'; ++ ++ emu_condition = COND_emu_hw_asm; + }; + + module = { +@@ -219,3 +243,14 @@ module = { + cppflags = '$(CPPFLAGS_GCRY)'; + }; + ++module = { ++ name = gcry_hwfeatures; ++ common = lib/libgcrypt-grub/src/hwfeatures.c; ++ x86_64 = lib/libgcrypt-grub/src/hwf-x86.c; ++ emu = lib/libgcrypt-grub/src/hwf-x86.c; ++ ++ cflags = '$(CFLAGS_GCRY)'; ++ cppflags = '$(CPPFLAGS_GCRY) $(CPPFLAGS_GCRY_ASM)'; ++ ++ emu_condition = COND_emu_hw_asm; ++}; +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
RFC PATCH 5/6 libgcrypt-grub: fix a typo in blake2.c
In reply to Gary Lin via Grub-develFix the typo: USE_AVX -> USE_AVX512
--- .../10_fix_blake2_typo.patch | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 grub-core/lib/libgcrypt-patches/10_fix_blake2_typo.patch
grub-core/lib/libgcrypt-patches/10_fix_blake2_typo.patch new file +28 -0
@@ -0,0 +1,28 @@ +From a7bb0c84ca7f14e25fa4781bff1cdedbc1d20510 Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Fri, 18 Jul 2025 15:32:13 +0800 +Subject: [PATCH 1/2] libgcrypt-grub: fix a typo in blake2.c + +Fix the typo: USE_AVX -> USE_AVX512 + +Signed-off-by: Gary Lin <[email protected]> +--- + grub-core/lib/libgcrypt-grub/cipher/blake2.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/grub-core/lib/libgcrypt-grub/cipher/blake2.c b/grub-core/lib/libgcrypt-grub/cipher/blake2.c +index 2829fce73..47f5b3c1f 100644 +--- a/grub-core/lib/libgcrypt-grub/cipher/blake2.c ++++ b/grub-core/lib/libgcrypt-grub/cipher/blake2.c +@@ -832,7 +832,7 @@ static gcry_err_code_t blake2s_init_ctx(void *ctx, unsigned int flags, + #ifdef USE_AVX + c->use_avx = !!(features & HWF_INTEL_AVX); + #endif +-#ifdef USE_AVX ++#ifdef USE_AVX512 + c->use_avx512 = !!(features & HWF_INTEL_AVX512); + #endif + +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
RFC PATCH 6/6 libgcrypt-grub: fix missing-prototypes error in sha256-intel-shaext.c
In reply to Gary Lin via Grub-devel--- .../11_fix_build_shaext.patch | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 grub-core/lib/libgcrypt-patches/11_fix_build_shaext.patch
grub-core/lib/libgcrypt-patches/11_fix_build_shaext.patch new file +30 -0
@@ -0,0 +1,30 @@ +From f04191c5921759df861ae1d48aeeb2733426a916 Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Fri, 18 Jul 2025 15:34:21 +0800 +Subject: [PATCH 2/2] libgcrypt-grub: fix missing-prototypes error in + sha256-intel-shaext.c + +Signed-off-by: Gary Lin <[email protected]> +--- + grub-core/lib/libgcrypt-grub/cipher/sha256-intel-shaext.c | 5 +++++ + 1 file changed, 5 insertions(+) + +diff --git a/grub-core/lib/libgcrypt-grub/cipher/sha256-intel-shaext.c b/grub-core/lib/libgcrypt-grub/cipher/sha256-intel-shaext.c +index 48c09eefe..7ec49f05e 100644 +--- a/grub-core/lib/libgcrypt-grub/cipher/sha256-intel-shaext.c ++++ b/grub-core/lib/libgcrypt-grub/cipher/sha256-intel-shaext.c +@@ -95,6 +95,11 @@ typedef struct u128_s + u32 a, b, c, d; + } u128_t; + ++ ++unsigned int ASM_FUNC_ATTR ++_gcry_sha256_transform_intel_shaext(u32 state[8], const unsigned char *data, ++ size_t nblks); ++ + /* + * Transform nblks*64 bytes (nblks*16 32-bit words) at DATA. + */ +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel