Skip to contentSkip to conversation

Enable Hardware Acceleration for SHA2 Family

PATCH New Gary Lin via Grub-devel 1 participant 11 messages Oct 22, 2025 Kbuild

lore
Share & tools
lore
#0 lore raw

v3 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

v3:
- Dropping 'inline' from the functions in grub-core/lib/x86_64/efi/hwfeatures-gcry.c
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 v2:

 -:  --------- >  1:  1890cf937 Tweak autoconf/automake files to detect x86_64 features
 -:  --------- >  2:  0992871e9 lib/hwfeatures-gcry: Introduce functions to manage hardware features
 1:  baf25b5ad !  3:  325befd0f lib/hwfeatures-gcry: Enable SSE and AVX for x86_64 EFI
    @@ grub-core/lib/x86_64/efi/hwfeatures-gcry.c (new)
     +static grub_uint32_t hw_features = 0;
     +static grub_uint64_t old_cr0, old_cr4, old_xcr0;
     +
    -+static inline grub_uint64_t
    ++static grub_uint64_t
     +read_cr0 (void)
     +{
     +  grub_uint64_t val;
    @@ grub-core/lib/x86_64/efi/hwfeatures-gcry.c (new)
     +  return val;
     +}
     +
    -+static inline grub_uint64_t
    ++static grub_uint64_t
     +read_cr4 (void)
     +{
     +  grub_uint64_t val;
    @@ grub-core/lib/x86_64/efi/hwfeatures-gcry.c (new)
     +  return val;
     +}
     +
    -+static inline void
    ++static void
     +write_cr0 (grub_uint64_t val)
     +{
     +  asm volatile ("mov %0,%%cr4": "+r" (val) : : "memory");
     +}
     +
    -+static inline void
    ++static void
     +write_cr4 (grub_uint64_t val)
     +{
     +  asm volatile ("mov %0,%%cr4": "+r" (val) : : "memory");
    @@ grub-core/lib/x86_64/efi/hwfeatures-gcry.c (new)
     +  return true;
     +}
     +
    -+static inline grub_uint64_t
    ++static grub_uint64_t
     +xgetbv (grub_uint32_t index)
     +{
     +  grub_uint32_t eax, edx;
    @@ grub-core/lib/x86_64/efi/hwfeatures-gcry.c (new)
     +  return eax + ((grub_uint64_t)edx << 32);
     +}
     +
    -+static inline void
    ++static void
     +xsetbv (grub_uint32_t index, grub_uint64_t value)
     +{
     +  grub_uint32_t eax = (grub_uint32_t)value;
 2:  5195b898a =  4:  46facef36 libgcrypt: Copy sha256 x86_64 assembly files
 3:  77d309e18 =  5:  ab67070fc libgcrypt: Copy sha512 x86_64 assembly files
 4:  61042e067 =  6:  4666f89c8 libgcrypt: Implement _gcry_get_hw_features()
 5:  9a5776f31 =  7:  36054eef5 libgcrypt: Declare the sha256 shaext function
 6:  2df17f531 =  8:  a38b914a0 libgcrypt: Add hardware acceleration for gcry_sha256
 7:  ba3a8ac3a =  9:  b5f6b6473 libgcrypt: Add hardware acceleration for gcry_sha512
 8:  9798849dd = 10:  ddd674688 disk/cryptodisk: Add '--hw-accel' to enable hardware acceleration
Signature · 7 lines
2.51.0


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

PATCH v3 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 v3 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 v3 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 v3 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 v3 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 v3 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 v3 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 v3 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 v3 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 v3 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