Skip to contentSkip to conversation

Enable Hardware Acceleration for SHA2 Family

PATCH Superseded Gary Lin via Grub-devel 2 participants 12 messages Oct 16, 2025 – Oct 21, 2025 Kbuild

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Enable Hardware Acceleration for SHA2 Family

This patch series speeds up LUKS PBKDF2 unlocking by using assembly files from libgcrypt 1.11.0 to enable hardware-accelerated SHA-256 and SHA-512.

As an initial step, the scope is limited to x86_64 EFI targets.

Patch 1 modifies autoconf/automake files to check the availablity of special CPU instructions.
Patch 2 and 3 introduce the functions to enable/disable the hardware features.
Patch 4 and 5 copy the selected assembly files.
Patch 6 implements _gcry_get_hw_features() to control the hardware features detection in libgcrypt.
Patch 7 fixes a potential build error when compiling the Intel SHA extention function for the gcry_sha256 module.
Patch 8 and 9 build the gcry_sha256 and gcry_sha512 modules with the assembly files.
Patch 10 adds a new option to cryptomount to enable hardware acceleration.

NOTE: The libgcrypt patch numbering begins at 13 to follow the Argon2 patch set(*).

(*) https://lists.gnu.org/archive/html/grub-devel/2025-10/msg00235.html

v2:
- Fixing the coding style issues in patch 3
- Removing 'inline' from get_cpuid_ecx() and get_cpuid_ecx()
- Rebasing to the latest git master

Gary Lin (10):
Tweak autoconf/automake files to detect x86_64 features lib/hwfeatures-gcry: Introduce functions to manage hardware features lib/hwfeatures-gcry: Enable SSE and AVX for x86_64 EFI
libgcrypt: Copy sha256 x86_64 assembly files
libgcrypt: Copy sha512 x86_64 assembly files
libgcrypt: Implement _gcry_get_hw_features()
libgcrypt: Declare the sha256 shaext function
libgcrypt: Add hardware acceleration for gcry_sha256
libgcrypt: Add hardware acceleration for gcry_sha512 disk/cryptodisk: Add '--hw-accel' to enable hardware acceleration

 Makefile.util.def                             |   1 +
 autogen.sh                                    |  11 +
 conf/Makefile.common                          |   2 +
 conf/Makefile.extra-dist                      |   4 +
 configure.ac                                  | 233 +++++++++++++++++
 docs/grub.texi                                |   5 +-
 grub-core/Makefile.core.def                   |   2 +
 grub-core/disk/cryptodisk.c                   |  26 +-
 grub-core/lib/hwfeatures-gcry.c               |  52 ++++
 .../libgcrypt-patches/13_add_hwfeatures.patch |  87 +++++++
 .../14_fix_build_shaext.patch                 |  35 +++
 .../15_build_sha256_x86_64_efi_opt_code.patch |  43 +++
 .../16_build_sha512_x86_64_efi_opt_code.patch |  35 +++
 grub-core/lib/x86_64/efi/hwfeatures-gcry.c    | 246 ++++++++++++++++++
 grub-core/normal/main.c                       |   3 +-
 include/grub/crypto.h                         |   7 +-
 include/grub/hwfeatures-gcry.h                |  26 ++
 include/grub/x86_64/cpuid.h                   |   1 +
 include/grub/x86_64/efi/hwfeatures-gcry.h     |  25 ++
 19 files changed, 836 insertions(+), 8 deletions(-)
 create mode 100644 grub-core/lib/hwfeatures-gcry.c
 create mode 100644 grub-core/lib/libgcrypt-patches/13_add_hwfeatures.patch
 create mode 100644 grub-core/lib/libgcrypt-patches/14_fix_build_shaext.patch
 create mode 100644 grub-core/lib/libgcrypt-patches/15_build_sha256_x86_64_efi_opt_code.patch
 create mode 100644 grub-core/lib/libgcrypt-patches/16_build_sha512_x86_64_efi_opt_code.patch
 create mode 100644 grub-core/lib/x86_64/efi/hwfeatures-gcry.c
 create mode 100644 include/grub/hwfeatures-gcry.h
 create mode 100644 include/grub/x86_64/cpuid.h
 create mode 100644 include/grub/x86_64/efi/hwfeatures-gcry.h

Range-diff against v1:

 1:  4c2b3fec8 <  -:  --------- util/import_gcry: Import kdf.c for Argon2
 2:  210b6292b <  -:  --------- crypto: Update crypto.h for libgcrypt KDF functions
 3:  3b72e07b8 <  -:  --------- libgcrypt/kdf: Implement blake2b_512.hash_buffers()
 4:  d90a709b1 <  -:  --------- libgcrypt/kdf: Get rid of gpg_err_code_from_errno()
 5:  5980e8a1a <  -:  --------- libgcrypt/kdf: Remove unsupported KDFs
 6:  6f3a415ca <  -:  --------- libgcrypt/kdf: Fix 64-bit modulus on 32-bit platforms
 7:  8298d3139 <  -:  --------- argon2: Introduce grub_crypto_argon2()
 8:  e3ff82e7c <  -:  --------- Import Argon2 tests from libgcrypt
 9:  168a8d24b <  -:  --------- Integrate Argon2 tests into functional_test
10:  3bc593992 <  -:  --------- disk/luks2: Add Argon2 support
11:  1fb5cac29 <  -:  --------- tests/util/grub-fs-tester: Use Argon2id for LUKS2 test
12:  840cf2fb0 <  -:  --------- docs: Document argon2 and argon2_test
13:  1fbd2a278 <  -:  --------- kern/misc: Implement faster grub_memcpy() for aligned buffers
14:  3b2ccefb0 !  1:  1890cf937 Tweak autoconf/automake files to detect x86_64 features

@@ Commit message
detection is currently limited to the x86_64 EFI target.

+ Reviewed-by: Daniel Kiper <[email protected]>

## conf/Makefile.common ##
@@ conf/Makefile.common: if COND_HAVE_PCI
15: e92856ba5 ! 2: 0992871e9 lib/hwfeatures-gcry: Introduce functions to manage hardware features
@@ Commit message
functions and sets '__gcry_use_hwf' to 'false'.

+ Reviewed-by: Daniel Kiper <[email protected]>

## Makefile.util.def ##
@@ Makefile.util.def: library = {
16: 43f2e43de ! 3: baf25b5ad lib/hwfeatures-gcry: Enable SSE and AVX for x86_64 EFI

    @@ Commit message
         on x86_64 EFI systems when the hardware is capable.
     
         Signed-off-by: Gary Lin <[email protected]>
    +    Reviewed-by: Daniel Kiper <[email protected]>
     
      ## grub-core/Makefile.core.def ##
     @@ grub-core/Makefile.core.def: module = {
    @@ grub-core/lib/x86_64/efi/hwfeatures-gcry.c (new)
     + *
     + * Ref: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=aa5cacdc29d76a005cbbee018a47faa6e724dd2d
     + */
    -+#define __FORCE_ORDER "m"(*(unsigned int *)0x1000UL)
    ++#define __FORCE_ORDER "m"(*(unsigned int *) 0x1000UL)
     +
     +#define HW_FEATURE_X86_64_SSE (1 << 0)
     +#define HW_FEATURE_X86_64_AVX (1 << 1)
    @@ grub-core/lib/x86_64/efi/hwfeatures-gcry.c (new)
     +read_cr0 (void)
     +{
     +  grub_uint64_t val;
    ++
     +  asm volatile ("mov %%cr0, %0" : "=r" (val) : __FORCE_ORDER);
     +  return val;
     +}
    @@ grub-core/lib/x86_64/efi/hwfeatures-gcry.c (new)
     +read_cr4 (void)
     +{
     +  grub_uint64_t val;
    ++
     +  asm volatile ("mov %%cr4,%0" : "=r" (val) : __FORCE_ORDER);
     +  return val;
     +}
    @@ grub-core/lib/x86_64/efi/hwfeatures-gcry.c (new)
     +  asm volatile ("mov %0,%%cr4": "+r" (val) : : "memory");
     +}
     +
    -+static inline grub_uint32_t
    ++static grub_uint32_t
     +get_cpuid_ecx (void)
     +{
     +  grub_uint32_t eax, ebx, ecx, edx;
    @@ grub-core/lib/x86_64/efi/hwfeatures-gcry.c (new)
     +  return ecx;
     +}
     +
    -+static inline grub_uint32_t
    ++static grub_uint32_t
     +get_cpuid_edx (void)
     +{
     +  grub_uint32_t eax, ebx, ecx, edx;

17: fa139ede3 ! 4: 5195b898a libgcrypt: Copy sha256 x86_64 assembly files
@@ Commit message
acceleration for sha256.

+ Reviewed-by: Daniel Kiper <[email protected]>

## autogen.sh ##
@@ autogen.sh: for x in mpi-asm-defs.h mpih-add1.c mpih-sub1.c mpih-mul1.c mpih-mul2.c mpih-mul
18: 7cf761b87 ! 5: 77d309e18 libgcrypt: Copy sha512 x86_64 assembly files
@@ Commit message
acceleration for sha512.

+ Reviewed-by: Daniel Kiper <[email protected]>

## autogen.sh ##
@@ autogen.sh: for x in mpi-asm-defs.h mpih-add1.c mpih-sub1.c mpih-mul1.c mpih-mul2.c mpih-mul
19: 09f2baf1e ! 6: 61042e067 libgcrypt: Implement _gcry_get_hw_features()
@@ Commit message
for x86_64.

+ Reviewed-by: Daniel Kiper <[email protected]>

## autogen.sh ##
@@ autogen.sh: for x in sha256-ssse3-amd64.S sha256-avx-amd64.S sha256-avx2-bmi2-amd64.S sha256
20: 9176b4266 ! 7: 9a5776f31 libgcrypt: Declare the sha256 shaext function
@@ Commit message
Declare the prototype in sha256-intel-shaext.c to avoid the error.

+ Reviewed-by: Daniel Kiper <[email protected]>

## conf/Makefile.extra-dist ##
@@ conf/Makefile.extra-dist: EXTRA_DIST += grub-core/lib/libgcrypt-patches/10-kdf-use-GPG-errs.patch
21: 0e1265bf8 ! 8: 2df17f531 libgcrypt: Add hardware acceleration for gcry_sha256
@@ Commit message
for the x86_64 EFI target.

+ Reviewed-by: Daniel Kiper <[email protected]>

## conf/Makefile.extra-dist ##
@@ conf/Makefile.extra-dist: EXTRA_DIST += grub-core/lib/libgcrypt-patches/11-kdf-remove-unsupported-kdfs.pat
22: d6f0e1707 ! 9: ba3a8ac3a libgcrypt: Add hardware acceleration for gcry_sha512
@@ Commit message
for the x86_64 EFI target.

+ Reviewed-by: Daniel Kiper <[email protected]>

## conf/Makefile.extra-dist ##
@@ conf/Makefile.extra-dist: EXTRA_DIST += grub-core/lib/libgcrypt-patches/12-kdf-use-grub_divmod64.patch
23: ab712f1fd ! 10: 9798849dd disk/cryptodisk: Add '--hw-accel' to enable hardware acceleration
@@ Commit message
availability of the new option.

+ Reviewed-by: Daniel Kiper <[email protected]>

## docs/grub.texi ##
@@ docs/grub.texi: The option @option{--quiet} can be given to suppress the output.
@@ grub-core/normal/main.c: static const char *features[] = {
"feature_default_font_path", "feature_all_video_module", "feature_menuentry_id", "feature_menuentry_options", "feature_200_final", "feature_nativedisk_cmd", "feature_timeout_style",
-- "feature_search_cryptodisk_only"
-+ "feature_search_cryptodisk_only", "feature_gcry_hw_accel"
+- "feature_search_cryptodisk_only", "feature_tpm2_cap_pcrs"
++ "feature_search_cryptodisk_only", "feature_tpm2_cap_pcrs",
++ "feature_gcry_hw_accel"
};

GRUB_MOD_INIT(normal)

Signature · 7 lines
2.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

PATCH v2 1/10 Tweak autoconf/automake files to detect x86_64 features

To enable hardware acceleration, this commit ports the feature detection logic from libgcrypt. This allows us to check if the compiler supports specific assembly instructions, including SSSE3, Intel SHA extensions, SSE4.1, AVX, AVX2, AVX512, and BMI2.

To simplify the initial implementation, support for x86_64 feature detection is currently limited to the x86_64 EFI target.

---
 conf/Makefile.common |   2 +
 configure.ac         | 233 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 235 insertions(+)
conf/Makefile.common +2 -0
index 4d38ff034..7ef171b2b 100644
@@ -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 +233 -0

239 lines not on this page · raw message

Signature · 7 lines
2.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

PATCH v2 2/10 lib/hwfeatures-gcry: Introduce functions to manage hardware features

In reply to Gary Lin via Grub-devel

This commit introduces the generic functions to manage the hardware features in libgcrypt. These functions are stubs for future platform-specific implementations.

* grub_gcry_hwf_enabled() returns '__gcry_use_hwf' which indicates if the hardware features are enabled specifically by grub_enable_gcry_hwf().
* grub_enable_gcry_hwf() invokes the architecture specific enablement functions and sets '__gcry_use_hwf' to 'true'.
* grub_reset_gcry_hwf() invokes the architecture specific reset functions and sets '__gcry_use_hwf' to 'false'.

---
 Makefile.util.def               |  1 +
 grub-core/Makefile.core.def     |  1 +
 grub-core/lib/hwfeatures-gcry.c | 43 +++++++++++++++++++++++++++++++++
 include/grub/hwfeatures-gcry.h  | 26 ++++++++++++++++++++
 4 files changed, 71 insertions(+)
 create mode 100644 grub-core/lib/hwfeatures-gcry.c
 create mode 100644 include/grub/hwfeatures-gcry.h
Makefile.util.def +1 -0
index f8d4ae7d3..0196911e6 100644
@@ -36,6 +36,7 @@ library = {
   common = grub-core/kern/misc.c;
   common = grub-core/kern/partition.c;
   common = grub-core/lib/crypto.c;
+  common = grub-core/lib/hwfeatures-gcry.c;
   common = grub-core/lib/json/json.c;
   common = grub-core/disk/luks.c;
   common = grub-core/disk/luks2.c;
grub-core/Makefile.core.def +1 -0
index 2b84d76d2..3173e66be 100644
@@ -1720,6 +1720,7 @@ module = {
 module = {
   name = crypto;
   common = lib/crypto.c;
+  common = lib/hwfeatures-gcry.c;
 
   extra_dist = lib/libgcrypt-grub/cipher/crypto.lst;
 };
grub-core/lib/hwfeatures-gcry.c new file +43 -0

43 lines not on this page · raw message

include/grub/hwfeatures-gcry.h new file +26 -0
new file mode 100644
index 000000000..2884f054a
@@ -0,0 +1,26 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2025  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef HWF_GCRY_HEADER
+#define HWF_GCRY_HEADER 1
+
+extern bool grub_gcry_hwf_enabled (void);
+extern void grub_enable_gcry_hwf (void);
+extern void grub_reset_gcry_hwf (void);
+
+#endif /* HWF_GCRY_HEADER */
Signature · 7 lines
2.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#3 lore raw

PATCH v2 3/10 lib/hwfeatures-gcry: Enable SSE and AVX for x86_64 EFI

In reply to Gary Lin via Grub-devel

Implement the necessary functions to dynamically enable SSE and AVX on x86_64 EFI systems when the hardware is capable.

---
 grub-core/Makefile.core.def                |   1 +
 grub-core/lib/hwfeatures-gcry.c            |   9 +
 grub-core/lib/x86_64/efi/hwfeatures-gcry.c | 246 +++++++++++++++++++++
 include/grub/x86_64/cpuid.h                |   1 +
 include/grub/x86_64/efi/hwfeatures-gcry.h  |  25 +++
 5 files changed, 282 insertions(+)
 create mode 100644 grub-core/lib/x86_64/efi/hwfeatures-gcry.c
 create mode 100644 include/grub/x86_64/cpuid.h
 create mode 100644 include/grub/x86_64/efi/hwfeatures-gcry.h
grub-core/Makefile.core.def +1 -0
index 3173e66be..3d4b1a3b4 100644
@@ -1721,6 +1721,7 @@ module = {
   name = crypto;
   common = lib/crypto.c;
   common = lib/hwfeatures-gcry.c;
+  x86_64_efi = lib/x86_64/efi/hwfeatures-gcry.c;
 
   extra_dist = lib/libgcrypt-grub/cipher/crypto.lst;
 };
grub-core/lib/hwfeatures-gcry.c +9 -0
index 652e67c43..acfa7be33 100644
@@ -19,6 +19,9 @@
 #include <grub/dl.h>
 
 #include <grub/hwfeatures-gcry.h>
+#if defined (__x86_64__) && defined (GRUB_MACHINE_EFI)
+#include <grub/x86_64/efi/hwfeatures-gcry.h>
+#endif
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -33,11 +36,17 @@ grub_gcry_hwf_enabled (void)
 void
 grub_enable_gcry_hwf (void)
 {
+#if defined (__x86_64__) && defined (GRUB_MACHINE_EFI)
+  grub_enable_gcry_hwf_x86_64_efi ();
+#endif
   __gcry_use_hwf = true;
 }
 
 void
 grub_reset_gcry_hwf (void)
 {
+#if defined (__x86_64__) && defined (GRUB_MACHINE_EFI)
+  grub_reset_gcry_hwf_x86_64_efi ();
+#endif
   __gcry_use_hwf = false;
 }
grub-core/lib/x86_64/efi/hwfeatures-gcry.c new file +246 -0

246 lines not on this page · raw message

include/grub/x86_64/cpuid.h new file +1 -0
new file mode 100644
index 000000000..acd93e3a7
@@ -0,0 +1 @@
+#include <grub/i386/cpuid.h>
include/grub/x86_64/efi/hwfeatures-gcry.h new file +25 -0
new file mode 100644
index 000000000..a8e671058
@@ -0,0 +1,25 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2025  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB 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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef HWF_GCRY_X86_64_EFI_HEADER
+#define HWF_GCRY_X86_64_EFI_HEADER 1
+
+extern void grub_enable_gcry_hwf_x86_64_efi (void);
+extern void grub_reset_gcry_hwf_x86_64_efi (void);
+
+#endif /* HWF_GCRY_X86_64_EFI_HEADER */
Signature · 7 lines
2.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#4 lore raw

PATCH v2 4/10 libgcrypt: Copy sha256 x86_64 assembly files

In reply to Gary Lin via Grub-devel

Copy the selected x86_64 assembly files to support hardware acceleration for sha256.

---
 autogen.sh | 7 +++++++
 1 file changed, 7 insertions(+)
autogen.sh +7 -0
index fbdb33879..0e3c23f65 100755
@@ -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 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.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#5 lore raw

PATCH v2 5/10 libgcrypt: Copy sha512 x86_64 assembly files

In reply to Gary Lin via Grub-devel

Copy the selected x86_64 assembly files to support hardware acceleration for sha512.

---
 autogen.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
autogen.sh +1 -1
index 0e3c23f65..7ff90cb93 100755
@@ -52,7 +52,7 @@ 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 sha256-ssse3-amd64.S sha256-avx-amd64.S sha256-avx2-bmi2-amd64.S sha256-intel-shaext.c; do
+for x in sha256-ssse3-amd64.S sha256-avx-amd64.S sha256-avx2-bmi2-amd64.S sha256-intel-shaext.c sha512-ssse3-amd64.S sha512-avx-amd64.S sha512-avx2-bmi2-amd64.S sha512-avx512-amd64.S; 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
Signature · 7 lines
2.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#6 lore raw

PATCH v2 6/10 libgcrypt: Implement _gcry_get_hw_features()

In reply to Gary Lin via Grub-devel

Implement _gcry_get_hw_features() and enable hardware feature detection for x86_64.

---
 autogen.sh                                    |  4 +
 conf/Makefile.extra-dist                      |  1 +
 .../libgcrypt-patches/13_add_hwfeatures.patch | 87 +++++++++++++++++++
 include/grub/crypto.h                         |  7 +-
 4 files changed, 97 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/lib/libgcrypt-patches/13_add_hwfeatures.patch
autogen.sh +4 -0
index 7ff90cb93..7dd26cd33 100755
@@ -59,6 +59,10 @@ for x in sha256-ssse3-amd64.S sha256-avx-amd64.S sha256-avx2-bmi2-amd64.S sha256
     cp grub-core/lib/libgcrypt/cipher/"$x" grub-core/lib/libgcrypt-grub/cipher/"$x"
 done
 
+if [ -f grub-core/lib/libgcrypt-grub/src/hwfeatures.c ]; then
+  rm grub-core/lib/libgcrypt-grub/src/hwfeatures.c
+fi
+
 for x in grub-core/lib/libgcrypt-patches/*.patch; do
     patch -i $x -p1
 done
conf/Makefile.extra-dist +1 -0
index 48c2b75f8..7c5afe680 100644
@@ -47,6 +47,7 @@ EXTRA_DIST += grub-core/lib/libgcrypt-patches/09-blake2b-hash-buffers.patch
 EXTRA_DIST += grub-core/lib/libgcrypt-patches/10-kdf-use-GPG-errs.patch
 EXTRA_DIST += grub-core/lib/libgcrypt-patches/11-kdf-remove-unsupported-kdfs.patch
 EXTRA_DIST += grub-core/lib/libgcrypt-patches/12-kdf-use-grub_divmod64.patch
+EXTRA_DIST += grub-core/lib/libgcrypt-patches/13_add_hwfeatures.patch
 
 EXTRA_DIST += grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch
 EXTRA_DIST += grub-core/lib/libtasn1-patches/0002-libtasn1-replace-strcat-with-strcpy-in-_asn1_str_cat.patch
grub-core/lib/libgcrypt-patches/13_add_hwfeatures.patch new file +87 -0

87 lines not on this page · raw message

include/grub/crypto.h +5 -2
index 125502582..14ca8faa8 100644
@@ -620,11 +620,14 @@ _gcry_ct_memequal (const void *b1, const void *b2, grub_size_t len);
 unsigned int
 _gcry_ct_not_memequal (const void *b1, const void *b2, grub_size_t len);
 
-
-static inline unsigned int _gcry_get_hw_features(void)
+#if defined (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.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#7 lore raw

PATCH v2 7/10 libgcrypt: Declare the sha256 shaext function

In reply to Gary Lin via Grub-devel

There is no prototype of _gcry_sha256_transform_intel_shaext() defined in the header or libgcrypt-grub/cipher/sha256.c, and gcc may complain the missing-prototypes error when compiling sha256-intel-shaext.c.

Declare the prototype in sha256-intel-shaext.c to avoid the error.

---
 conf/Makefile.extra-dist                      |  1 +
 .../14_fix_build_shaext.patch                 | 35 +++++++++++++++++++
 2 files changed, 36 insertions(+)
 create mode 100644 grub-core/lib/libgcrypt-patches/14_fix_build_shaext.patch
conf/Makefile.extra-dist +1 -0
index 7c5afe680..cebf780b5 100644
@@ -48,6 +48,7 @@ EXTRA_DIST += grub-core/lib/libgcrypt-patches/10-kdf-use-GPG-errs.patch
 EXTRA_DIST += grub-core/lib/libgcrypt-patches/11-kdf-remove-unsupported-kdfs.patch
 EXTRA_DIST += grub-core/lib/libgcrypt-patches/12-kdf-use-grub_divmod64.patch
 EXTRA_DIST += grub-core/lib/libgcrypt-patches/13_add_hwfeatures.patch
+EXTRA_DIST += grub-core/lib/libgcrypt-patches/14_fix_build_shaext.patch
 
 EXTRA_DIST += grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch
 EXTRA_DIST += grub-core/lib/libtasn1-patches/0002-libtasn1-replace-strcat-with-strcpy-in-_asn1_str_cat.patch
grub-core/lib/libgcrypt-patches/14_fix_build_shaext.patch new file +35 -0
new file mode 100644
index 000000000..b9d311465
@@ -0,0 +1,35 @@
+From 5698f7c5055ea481d0040ea4495829e5d02781cc Mon Sep 17 00:00:00 2001
+From: Gary Lin <[email protected]>
+Date: Fri, 18 Jul 2025 15:34:21 +0800
+Subject: [PATCH 2/4] libgcrypt: Declare the sha256 shaext function
+
+There is no prototype of _gcry_sha256_transform_intel_shaext() defined
+in the header or libgcrypt-grub/cipher/sha256.c, and gcc may complain
+the missing-prototypes error when compiling sha256-intel-shaext.c.
+
+Declare the prototype in sha256-intel-shaext.c to avoid the error.
+
+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.51.0
+
Signature · 7 lines
2.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#8 lore raw

PATCH v2 8/10 libgcrypt: Add hardware acceleration for gcry_sha256

In reply to Gary Lin via Grub-devel

Enable hardware acceleration for the gcry_sha256 module when building for the x86_64 EFI target.

---
 conf/Makefile.extra-dist                      |  1 +
 .../15_build_sha256_x86_64_efi_opt_code.patch | 43 +++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 grub-core/lib/libgcrypt-patches/15_build_sha256_x86_64_efi_opt_code.patch
conf/Makefile.extra-dist +1 -0
index cebf780b5..f524f944b 100644
@@ -49,6 +49,7 @@ EXTRA_DIST += grub-core/lib/libgcrypt-patches/11-kdf-remove-unsupported-kdfs.pat
 EXTRA_DIST += grub-core/lib/libgcrypt-patches/12-kdf-use-grub_divmod64.patch
 EXTRA_DIST += grub-core/lib/libgcrypt-patches/13_add_hwfeatures.patch
 EXTRA_DIST += grub-core/lib/libgcrypt-patches/14_fix_build_shaext.patch
+EXTRA_DIST += grub-core/lib/libgcrypt-patches/15_build_sha256_x86_64_efi_opt_code.patch
 
 EXTRA_DIST += grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch
 EXTRA_DIST += grub-core/lib/libtasn1-patches/0002-libtasn1-replace-strcat-with-strcpy-in-_asn1_str_cat.patch
grub-core/lib/libgcrypt-patches/15_build_sha256_x86_64_efi_opt_code.patch new file +43 -0

43 lines not on this page · raw message

Signature · 7 lines
2.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#9 lore raw

PATCH v2 9/10 libgcrypt: Add hardware acceleration for gcry_sha512

In reply to Gary Lin via Grub-devel

Enable hardware acceleration for the gcry_sha512 module when building for the x86_64 EFI target.

---
 conf/Makefile.extra-dist                      |  1 +
 .../16_build_sha512_x86_64_efi_opt_code.patch | 35 +++++++++++++++++++
 2 files changed, 36 insertions(+)
 create mode 100644 grub-core/lib/libgcrypt-patches/16_build_sha512_x86_64_efi_opt_code.patch
conf/Makefile.extra-dist +1 -0
index f524f944b..846d45341 100644
@@ -50,6 +50,7 @@ EXTRA_DIST += grub-core/lib/libgcrypt-patches/12-kdf-use-grub_divmod64.patch
 EXTRA_DIST += grub-core/lib/libgcrypt-patches/13_add_hwfeatures.patch
 EXTRA_DIST += grub-core/lib/libgcrypt-patches/14_fix_build_shaext.patch
 EXTRA_DIST += grub-core/lib/libgcrypt-patches/15_build_sha256_x86_64_efi_opt_code.patch
+EXTRA_DIST += grub-core/lib/libgcrypt-patches/16_build_sha512_x86_64_efi_opt_code.patch
 
 EXTRA_DIST += grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch
 EXTRA_DIST += grub-core/lib/libtasn1-patches/0002-libtasn1-replace-strcat-with-strcpy-in-_asn1_str_cat.patch
grub-core/lib/libgcrypt-patches/16_build_sha512_x86_64_efi_opt_code.patch new file +35 -0
new file mode 100644
index 000000000..9cdde8830
@@ -0,0 +1,35 @@
+From f62c2c7565237bbf059220e90d3f1f8c8d6eebd5 Mon Sep 17 00:00:00 2001
+From: Gary Lin <[email protected]>
+Date: Wed, 3 Sep 2025 16:26:55 +0800
+Subject: [PATCH 4/4] libgcrypt: Add hardware acceleration for gcry_sha512
+
+Enable hardware acceleration for the gcry_sha512 module when building
+for the x86_64 EFI target.
+
+Signed-off-by: Gary Lin <[email protected]>
+---
+ grub-core/Makefile.gcry.def | 7 ++++++-
+ 1 file changed, 6 insertions(+), 1 deletion(-)
+
+diff --git a/grub-core/Makefile.gcry.def b/grub-core/Makefile.gcry.def
+index ac1d9a088..64ea5ee24 100644
+--- a/grub-core/Makefile.gcry.def
++++ b/grub-core/Makefile.gcry.def
+@@ -186,8 +186,13 @@ module = {
+   name = gcry_sha512;
+   common = lib/libgcrypt-grub/cipher/sha512.c;
+   common = lib/libgcrypt-grub/cipher/hash-common.c;
++  x86_64_efi = lib/libgcrypt-grub/cipher/sha512-ssse3-amd64.S;
++  x86_64_efi = lib/libgcrypt-grub/cipher/sha512-avx-amd64.S;
++  x86_64_efi = lib/libgcrypt-grub/cipher/sha512-avx2-bmi2-amd64.S;
++  x86_64_efi = lib/libgcrypt-grub/cipher/sha512-avx512-amd64.S;
++
+   cflags = '$(CFLAGS_GCRY) -Wno-cast-align';
+-  cppflags = '$(CPPFLAGS_GCRY)';
++  cppflags = '$(CPPFLAGS_GCRY) -DUSE_SHA512 $(CPPFLAGS_GCRY_ASM)';
+ };
+ 
+ module = {
+-- 
+2.51.0
+
Signature · 7 lines
2.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#10 lore raw

PATCH v2 10/10 disk/cryptodisk: Add '--hw-accel' to enable hardware acceleration

In reply to Gary Lin via Grub-devel

The '--hw-accel' option has been added to cryptomount to speed up decryption by temporarily enabling hardware-specific instruction sets (e.g., AVX, SSE) in libgcrypt.

A new feature, "feature_gcry_hw_accel", is also introduced to mark the availability of the new option.

---
 docs/grub.texi              |  5 +++--
 grub-core/disk/cryptodisk.c | 26 +++++++++++++++++++++++---
 grub-core/normal/main.c     |  3 ++-
 3 files changed, 28 insertions(+), 6 deletions(-)
docs/grub.texi +3 -2
index 52a98a97d..99f583f9b 100644
@@ -7134,7 +7134,7 @@ The option @option{--quiet} can be given to suppress the output.
 @node cryptomount
 @subsection cryptomount
 
-@deffn Command cryptomount [ [@option{-p} password] | [@option{-k} keyfile [@option{-O} keyoffset] [@option{-S} keysize] ] | [@option{-P} protector] ] [@option{-H} file] device|@option{-u} uuid|@option{-a}|@option{-b}
+@deffn Command cryptomount [ [@option{-p} password] | [@option{-k} keyfile [@option{-O} keyoffset] [@option{-S} keysize] ] | [@option{-P} protector] | [@option{-A}] ] [@option{-H} file] device|@option{-u} uuid|@option{-a}|@option{-b}
 Setup access to encrypted device. A passphrase will be requested interactively,
 if neither the @option{-p} nor @option{-k} options are given. The option
 @option{-p} can be used to supply a passphrase (useful for scripts).
@@ -7142,7 +7142,8 @@ Alternatively the @option{-k} option can be used to supply a keyfile with
 options @option{-O} and @option{-S} optionally supplying the offset and size,
 respectively, of the key data in the given key file. Besides the keyfile,
 the key can be stored in a key protector, and option @option{-P} configures
-specific key protector, e.g. tpm2, to retrieve the key from.
+specific key protector, e.g. tpm2, to retrieve the key from. The option @option{-A}
+enables hardware acceleration in libgcrypt to speed up decryption.
 The @option{-H} options can be used to supply cryptomount backends with an
 alternative header file (aka detached header). Not all backends have headers
 nor support alternative header files (currently only LUKS1 and LUKS2 support them).
grub-core/disk/cryptodisk.c +23 -3
index 9af665df3..290821bb6 100644
@@ -29,6 +29,7 @@
 #include <grub/partition.h>
 #include <grub/key_protector.h>
 #include <grub/safemath.h>
+#include <grub/hwfeatures-gcry.h>
 
 #ifdef GRUB_UTIL
 #include <grub/emu/hostdisk.h>
@@ -48,7 +49,8 @@ enum
     OPTION_KEYFILE_OFFSET,
     OPTION_KEYFILE_SIZE,
     OPTION_HEADER,
-    OPTION_PROTECTOR
+    OPTION_PROTECTOR,
+    OPTION_HWACCEL
   };
 
 static const struct grub_arg_option options[] =
@@ -64,6 +66,7 @@ static const struct grub_arg_option options[] =
     {"header", 'H', 0, N_("Read header from file"), 0, ARG_TYPE_STRING},
     {"protector", 'P', GRUB_ARG_OPTION_REPEATABLE,
      N_("Unlock volume(s) using key protector(s)."), 0, ARG_TYPE_STRING},
+    {"hw-accel", 'A', 0, N_("Enable hardware acceleration."), 0, 0},
     {0, 0, 0, 0, 0, 0}
   };
 
@@ -1420,7 +1423,7 @@ grub_cryptodisk_clear_key_cache (struct grub_cryptomount_args *cargs)
 }
 
 static grub_err_t
-grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
+__grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
 {
   struct grub_arg_list *state = ctxt->state;
   struct grub_cryptomount_args cargs = {0};
@@ -1629,6 +1632,23 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
     }
 }
 
+static grub_err_t
+grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
+{
+  struct grub_arg_list *state = ctxt->state;
+  grub_err_t err;
+
+  if (state[OPTION_HWACCEL].set)
+    grub_enable_gcry_hwf ();
+
+  err = __grub_cmd_cryptomount (ctxt, argc, args);
+
+  if (state[OPTION_HWACCEL].set)
+    grub_reset_gcry_hwf ();
+
+  return err;
+}
+
 static struct grub_disk_dev grub_cryptodisk_dev = {
   .name = "cryptodisk",
   .id = GRUB_DISK_DEVICE_CRYPTODISK_ID,
@@ -1898,7 +1918,7 @@ GRUB_MOD_INIT (cryptodisk)
   cmd = grub_register_extcmd ("cryptomount", grub_cmd_cryptomount, 0,
 			      N_("[ [-p password] | [-k keyfile"
 				 " [-O keyoffset] [-S keysize] ] ] [-H file]"
-				 " [-P protector [-P protector ...]]"
+				 " [-P protector [-P protector ...]] | [-A]"
 				 " <SOURCE|-u UUID|-a|-b>"),
 			      N_("Mount a crypto device."), options);
   grub_procfs_register ("luks_script", &luks_script);
grub-core/normal/main.c +2 -1
index 01b79ac32..8c2acf938 100644
@@ -518,7 +518,8 @@ static const char *features[] = {
   "feature_default_font_path", "feature_all_video_module",
   "feature_menuentry_id", "feature_menuentry_options", "feature_200_final",
   "feature_nativedisk_cmd", "feature_timeout_style",
-  "feature_search_cryptodisk_only", "feature_tpm2_cap_pcrs"
+  "feature_search_cryptodisk_only", "feature_tpm2_cap_pcrs",
+  "feature_gcry_hw_accel"
 };
 
 GRUB_MOD_INIT(normal)
Signature · 7 lines
2.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#11 lore raw
In reply to Gary Lin via Grub-devel

Gary,

Quoted 2 lines · Gary Lin via Grub-devel

This patch series speeds up LUKS PBKDF2 unlocking by using assembly files from libgcrypt 1.11.0 to enable hardware-accelerated SHA-256 and SHA-512.

Please post updated patch set as we discussed it a few days ago. I want to merge it tomorrow...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel