Skip to contentSkip to conversation

Optimize PBKDF2

PATCH In review Gary Lin via Grub-devel 2 participants 5 messages Aug 14, 2025 – Oct 16, 2025

lore
Share & tools
lore
#0 lore raw

cover Optimize PBKDF2

This patch series addresses performance issues in the PBKDF2 implementation, which previously caused significant delays when unlocking encrypted partitions. The code has been optimized to eliminate redundant memory allocations and unnecessary hash operations.

These changes result in a substantial improvement: in local testing, the disk unlocking time was reduced from 15 seconds to 4 seconds.

Credit to Steffen Winterfeldt for providing a custom PBKDF2 implementation that helped identify these bottlenecks.

Gary Lin (2):
lib/crypto: Introduce new HMAC functions to reuse buffers lib/pbkdf2: Optimize PBKDF2 by reusing HMAC handle

 grub-core/disk/geli.c  |  4 +-
 grub-core/lib/crypto.c | 91 ++++++++++++++++++++++++++++++------------
 grub-core/lib/pbkdf2.c | 21 ++++++----
 include/grub/crypto.h  |  8 +++-
 4 files changed, 87 insertions(+), 37 deletions(-)
Signature · 7 lines
2.43.0


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

PATCH 1/2 lib/crypto: Introduce new HMAC functions to reuse buffers

To enable more efficient buffer reuse for HMAC operations, three new functions have been introduced. This change prevents the need to reallocate memory for each HMAC operation.

* grub_crypto_hmac_reset(): Reinitializes the hash contexts in the HMAC handle.

* grub_crypto_hmac_final(): Provides the final HMAC result without freeing the handle, allowing it to be reused immediately.

* grub_crypto_hmac_free(): Deallocates the HMAC handle and its associated memory.

To further facilitate buffer reuse, 'ctx2' is now included within the HMAC handle struct, and the initialization of 'ctx2' is moved to grub_crypto_hmac_init().

The intermediate hash states ('ctx' and 'ctx2') for the inner and outer padded keys are now cached. grub_crypto_hmac_reset() restores these cached states for new operations, which avoids redundant hashing of the keys.

---
 grub-core/disk/geli.c  |  4 +-
 grub-core/lib/crypto.c | 91 ++++++++++++++++++++++++++++++------------
 include/grub/crypto.h  |  8 +++-
 3 files changed, 74 insertions(+), 29 deletions(-)
grub-core/disk/geli.c +1 -3
index 722910d64..bf22ebb63 100644
@@ -464,9 +464,7 @@ geli_recover_key (grub_disk_t source, grub_cryptodisk_t dev, grub_cryptomount_ar
       grub_crypto_hmac_write (hnd, header.salt, sizeof (header.salt));
       grub_crypto_hmac_write (hnd, cargs->key_data, cargs->key_len);
 
-      gcry_err = grub_crypto_hmac_fini (hnd, geomkey);
-      if (gcry_err)
-	return grub_crypto_gcry_error (gcry_err);
+      grub_crypto_hmac_fini (hnd, geomkey);
     }
 
   gcry_err = grub_crypto_hmac_buffer (dev->hash, geomkey,
grub-core/lib/crypto.c +66 -25

156 lines not on this page · raw message

include/grub/crypto.h +7 -1
index b0d7add1d..2a7fbd31b 100644
@@ -499,8 +499,14 @@ void
 grub_crypto_hmac_write (struct grub_crypto_hmac_handle *hnd,
 			const void *data,
 			grub_size_t datalen);
-gcry_err_code_t
+void
 grub_crypto_hmac_fini (struct grub_crypto_hmac_handle *hnd, void *out);
+void
+grub_crypto_hmac_reset (struct grub_crypto_hmac_handle *hnd);
+void
+grub_crypto_hmac_final (struct grub_crypto_hmac_handle *hnd, void *out);
+void
+grub_crypto_hmac_free (struct grub_crypto_hmac_handle *hnd);
 
 gcry_err_code_t
 grub_crypto_hmac_buffer (const struct gcry_md_spec *md,
Signature · 7 lines
2.43.0


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

PATCH 2/2 lib/pbkdf2: Optimize PBKDF2 by reusing HMAC handle

In reply to Gary Lin via Grub-devel

The previous PBKDF2 implementation used grub_crypto_hmac_buffer(), which allocates and frees an HMAC handle on every call. This approach caused significant performance overhead, slowing down the boot process considerably.

This commit refactors the PBKDF2 code to use the new HMAC functions, allowing the HMAC handle and its buffers to be allocated once and reused across multiple operations. This change significantly reduces disk unlocking time.

In a QEMU/OVMF test environment, this patch reduced the time to unlock a LUKS2(*) partition from approximately 15 seconds to 4 seconds.

(*) PBKDF2 SHA256 with 3454944 iterations

---
 grub-core/lib/pbkdf2.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
grub-core/lib/pbkdf2.c +13 -8
index 28aa96c46..410eff580 100644
@@ -39,6 +39,7 @@ grub_crypto_pbkdf2 (const struct gcry_md_spec *md,
 		    unsigned int c,
 		    grub_uint8_t *DK, grub_size_t dkLen)
 {
+  struct grub_crypto_hmac_handle *hnd = NULL;
   unsigned int hLen = md->mdlen;
   grub_uint8_t U[GRUB_CRYPTO_MAX_MDLEN];
   grub_uint8_t T[GRUB_CRYPTO_MAX_MDLEN];
@@ -47,7 +48,6 @@ grub_crypto_pbkdf2 (const struct gcry_md_spec *md,
   unsigned int r;
   unsigned int i;
   unsigned int k;
-  gcry_err_code_t rc;
   grub_uint8_t *tmp;
   grub_size_t tmplen = Slen + 4;
 
@@ -72,6 +72,13 @@ grub_crypto_pbkdf2 (const struct gcry_md_spec *md,
 
   grub_memcpy (tmp, S, Slen);
 
+  hnd = grub_crypto_hmac_init (md, P, Plen);
+  if (hnd == NULL)
+    {
+      grub_free (tmp);
+      return GPG_ERR_OUT_OF_MEMORY;
+    }
+
   for (i = 1; i - 1 < l; i++)
     {
       grub_memset (T, 0, hLen);
@@ -85,16 +92,13 @@ grub_crypto_pbkdf2 (const struct gcry_md_spec *md,
 	      tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
 	      tmp[Slen + 3] = (i & 0x000000ff) >> 0;
 
-	      rc = grub_crypto_hmac_buffer (md, P, Plen, tmp, tmplen, U);
+	      grub_crypto_hmac_write (hnd, tmp, tmplen);
 	    }
 	  else
-	    rc = grub_crypto_hmac_buffer (md, P, Plen, U, hLen, U);
+	    grub_crypto_hmac_write (hnd, U, hLen);
 
-	  if (rc != GPG_ERR_NO_ERROR)
-	    {
-	      grub_free (tmp);
-	      return rc;
-	    }
+	  grub_crypto_hmac_final (hnd, U);
+	  grub_crypto_hmac_reset (hnd);
 
 	  for (k = 0; k < hLen; k++)
 	    T[k] ^= U[k];
@@ -103,6 +107,7 @@ grub_crypto_pbkdf2 (const struct gcry_md_spec *md,
       grub_memcpy (DK + (i - 1) * hLen, T, i == l ? r : hLen);
     }
 
+  grub_crypto_hmac_free (hnd);
   grub_free (tmp);
 
   return GPG_ERR_NO_ERROR;
Signature · 7 lines
2.43.0


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

This patch series addresses performance issues in the PBKDF2 implementation, which previously caused significant delays when unlocking encrypted partitions. The code has been optimized to eliminate redundant memory allocations and unnecessary hash operations.

These changes result in a substantial improvement: in local testing, the disk unlocking time was reduced from 15 seconds to 4 seconds.

Credit to Steffen Winterfeldt for providing a custom PBKDF2 implementation that helped identify these bottlenecks.

I forgot to include the Steffen's git repo as the reference.
https://github.com/wfeldt/grub2/pull/3

Gary Lin

Quoted 4 lines

Gary Lin (2):
lib/crypto: Introduce new HMAC functions to reuse buffers lib/pbkdf2: Optimize PBKDF2 by reusing HMAC handle

Quoted 9 lines
 grub-core/disk/geli.c  |  4 +-
 grub-core/lib/crypto.c | 91 ++++++++++++++++++++++++++++++------------
 grub-core/lib/pbkdf2.c | 21 ++++++----
 include/grub/crypto.h  |  8 +++-
 4 files changed, 87 insertions(+), 37 deletions(-)
Signature · 1 line
2.43.0

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

#4 lore raw
In reply to Gary Lin via Grub-devel
Quoted 8 lines · Gary Lin via Grub-devel

This patch series addresses performance issues in the PBKDF2 implementation, which previously caused significant delays when unlocking encrypted partitions. The code has been optimized to eliminate redundant memory allocations and unnecessary hash operations.

These changes result in a substantial improvement: in local testing, the disk unlocking time was reduced from 15 seconds to 4 seconds.

Quoted 6 lines

Credit to Steffen Winterfeldt for providing a custom PBKDF2 implementation that helped identify these bottlenecks.

Gary Lin (2):
lib/crypto: Introduce new HMAC functions to reuse buffers lib/pbkdf2: Optimize PBKDF2 by reusing HMAC handle

For both patches Reviewed-by: Daniel Kiper <[email protected]>...

Daniel

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