Skip to contentSkip to conversation

Fixes for non-default TPM PCR banks

PATCH In review Gary Lin via Grub-devel 2 participants 6 messages Jun 13, 2025

lore
Share & tools
lore
#0 lore raw

v2 cover Fixes for non-default TPM PCR banks

There are two bugs when using the TPM PCR banks other than the default SHA256 PCR bank:

1) Key unsealing failure
2) Dumping the wrong PCR bank

This patch set fixes the above two bugs and adds the tests for SHA384 PCR bank.

v2:
- Set the SHA256 PCR bank explicitly in tpm2_key_protector_test

Gary Lin (3):
grub-protect: Fix the hash algorithm of PCR digest
tpm2_key_protector: Dump the PCR bank for key unsealing tests/tpm2_key_protector_test: Add tests for SHA384 PCR bank

 .../commands/tpm2_key_protector/module.c      | 11 ++++-
 tests/tpm2_key_protector_test.in              | 46 +++++++++++++------
 util/grub-protect.c                           |  2 +-
 3 files changed, 43 insertions(+), 16 deletions(-)

Range-diff against v1:
1: 815b486ca ! 1: fc7f713fb grub-protect: Fix the hash algorithm of PCR digest
@@ Commit message
Ref: https://github.com/lcp/grub2/issues/4

+ Reviewed-by: Stefan Berger <[email protected]>
+ Reviewed-by: Sudhakar Kuppusamy <[email protected]>

## util/grub-protect.c ##
@@ util/grub-protect.c: protect_tpm2_get_policy_digest (protect_args_t *args, TPM2B_DIGEST_t *digest)
2: 6e26ea23e ! 2: 7d3769fe2 tpm2_key_protector: Dump the PCR bank for key unsealing
@@ Commit message
necessary.

+ Reviewed-by: Stefan Berger <[email protected]>
+ Reviewed-by: Sudhakar Kuppusamy <[email protected]>

## grub-core/commands/tpm2_key_protector/module.c ##
@@ grub-core/commands/tpm2_key_protector/module.c: static tpm2_protector_context_t tpm2_protector_ctx = {0};
3: 8bf3b04b1 ! 3: cccf03d72 tests/tpm2_key_protector_test: Add tests for SHA384 PCR bank

    @@ Commit message
         bank instead of the default SHA256 PCR bank.
     
         Signed-off-by: Gary Lin <[email protected]>
    +    Reviewed-by: Sudhakar Kuppusamy <[email protected]>
     
      ## tests/tpm2_key_protector_test.in ##
     @@ tests/tpm2_key_protector_test.in: done
    @@ tests/tpm2_key_protector_test.in: done
          grub_srk_alg=${srk_alg}
      
     @@ tests/tpm2_key_protector_test.in: tpm2_seal_unseal() {
    - 	extra_opt="${extra_opt} --tpm2-asymmetric=${srk_alg}"
    -     fi
    - 
    -+    if [ "${pcr_bank}" = "" ]; then
    -+        pcr_bank="sha256"
    -+    fi
    -+
    -     # Seal the password with grub-protect
    -     grub-protect ${extra_opt} \
    - 	--tpm2-device="${tpm2dev}" \
      	--action=add \
      	--protector=tpm2 \
      	--tpm2key \
    @@ tests/tpm2_key_protector_test.in: EOF
          extra_opt=""
          extra_grub_opt=""
     @@ tests/tpm2_key_protector_test.in: tpm2_seal_unseal_nv() {
    - 	nv_index="0x81000000"
    -     fi
    - 
    -+    if [ "${pcr_bank}" = "" ]; then
    -+        pcr_bank="sha256"
    -+    fi
    -+
          if [ "$key_type" = "tpm2key" ]; then
      	extra_opt="--tpm2key"
          else
    @@ tests/tpm2_key_protector_test.in: tpm2_seal_unseal_nv() {
      	--tpm2-pcrs=0,1 \
      	--tpm2-keyfile="${lukskeyfile}" \
      	--tpm2-nvindex="${nv_index}" || ret=$?
    -@@ tests/tpm2_key_protector_test.in: srktests+=("RSA persistent no_fallback_srk")
    - srktests+=("ECC persistent no_fallback_srk")
    - srktests+=("RSA transient fallback_srk")
    - srktests+=("ECC transient fallback_srk")
    +@@ tests/tpm2_key_protector_test.in: EOF
    + 
    + # Testcases for SRK mode
    + declare -a srktests=()
    +-srktests+=("default transient no_fallback_srk")
    +-srktests+=("RSA transient no_fallback_srk")
    +-srktests+=("ECC transient no_fallback_srk")
    +-srktests+=("RSA persistent no_fallback_srk")
    +-srktests+=("ECC persistent no_fallback_srk")
    +-srktests+=("RSA transient fallback_srk")
    +-srktests+=("ECC transient fallback_srk")
    ++srktests+=("default transient no_fallback_srk sha256")
    ++srktests+=("RSA transient no_fallback_srk sha256")
    ++srktests+=("ECC transient no_fallback_srk sha256")
    ++srktests+=("RSA persistent no_fallback_srk sha256")
    ++srktests+=("ECC persistent no_fallback_srk sha256")
    ++srktests+=("RSA transient fallback_srk sha256")
    ++srktests+=("ECC transient fallback_srk sha256")
     +if [ "${with_sha384}" = "true" ]; then
     +    srktests+=("default transient no_fallback_srk sha384")
     +fi
      
      exit_status=0
      
    -@@ tests/tpm2_key_protector_test.in: declare -a nvtests=()
    - nvtests+=("persistent raw")
    - nvtests+=("nvindex raw")
    - nvtests+=("nvindex tpm2key")
    +@@ tests/tpm2_key_protector_test.in: done
    + 
    + # Testcases for NV index mode
    + declare -a nvtests=()
    +-nvtests+=("persistent raw")
    +-nvtests+=("nvindex raw")
    +-nvtests+=("nvindex tpm2key")
    ++nvtests+=("persistent raw sha256")
    ++nvtests+=("nvindex raw sha256")
    ++nvtests+=("nvindex tpm2key sha256")
     +if [ "${with_sha384}" = "true" ]; then
     +    nvtests+=("persistent raw sha384")
     +    nvtests+=("nvindex tpm2key sha384")
Signature · 7 lines
2.43.0


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

PATCH v2 1/3 grub-protect: Fix the hash algorithm of PCR digest

For "tpm2_key_protector" and "grub-protect", 'SHA256' is chosen as the hash algorithm for the TPM session. However, grub-protect mistakenly used the hash algorithm of the PCR bank to calculate PCR digest. If the user chose a PCR bank other than 'SHA256", "grub-protect" created a non-SHA256 PCR digest to seal the key. But, "tpm2_key_protector" expects a SHA256 PCR digest to the TPM unsealing session, so it would fail due to digest mismatch.

This commit fixes the hash algorithm of PCR digest in grub-protect to avoid the potential unsealing failure.

Ref: https://github.com/lcp/grub2/issues/4

---
 util/grub-protect.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/grub-protect.c +1 -1
index 40d4a3fc5..d53c2572d 100644
@@ -483,7 +483,7 @@ protect_tpm2_get_policy_digest (protect_args_t *args, TPM2B_DIGEST_t *digest)
     }
   pcr_concat.size = pcr_concat_len;
 
-  rc = grub_tpm2_hash (NULL, &pcr_concat, args->tpm2_bank, TPM_RH_NULL, &pcr_digest, NULL, NULL);
+  rc = grub_tpm2_hash (NULL, &pcr_concat, TPM_ALG_SHA256, TPM_RH_NULL, &pcr_digest, NULL, NULL);
   if (rc != TPM_RC_SUCCESS)
     {
       fprintf (stderr, "Failed to generate PCR digest (TPM2_Hash: 0x%x)\n", rc);
Signature · 7 lines
2.43.0


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

PATCH v2 2/3 tpm2_key_protector: Dump the PCR bank for key unsealing

In reply to Gary Lin via Grub-devel

TPM 2.0 Key File format stores the PCR selection in the parameters for 'TPM2_PolicyPCR' and it already contains the selected PCR bank. Currently, "tpm2_key_protector" dumped the PCR bank specified by the '--bank' option, and it may not be the PCR bank for key unsealing.

To dump the real PCR bank for key unsealing, this commit records the PCR bank used by 'TPM2_PolicyPCR' and dumps PCR values from that bank when necessary.

---
 grub-core/commands/tpm2_key_protector/module.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
grub-core/commands/tpm2_key_protector/module.c +9 -2
index 857f3753f..0043d8bd7 100644
@@ -162,6 +162,8 @@ static tpm2_protector_context_t tpm2_protector_ctx = {0};
 
 static grub_command_t tpm2_dump_pcr_cmd;
 
+static TPM_ALG_ID_t tpm2_dump_bank;
+
 static grub_err_t
 tpm2_protector_srk_read_file (const char *filepath, void **buffer, grub_size_t *buffer_size)
 {
@@ -654,6 +656,9 @@ tpm2_protector_policypcr (TPMI_SH_AUTH_SESSION_t session, struct grub_tpm2_buffe
   if (cmd_buf->error != 0)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "failed to unmarshal commandPolicy for TPM2_PolicyPCR");
 
+  /* Record the chosen PCR bank */
+  tpm2_dump_bank = pcr_sel.pcrSelections[0].hash;
+
   rc = grub_tpm2_policypcr (session, NULL, &pcr_digest, &pcr_sel, NULL);
   if (rc != TPM_RC_SUCCESS)
     return grub_error (GRUB_ERR_BAD_DEVICE, "failed to submit PCR policy (TPM2_PolicyPCR: 0x%x)", rc);
@@ -1091,7 +1096,7 @@ tpm2_protector_key_from_buffer (const tpm2_protector_context_t *ctx,
   if (dump_pcr == true)
     {
       grub_printf ("PCR Mismatch! Check firmware and bootloader before typing passphrase!\n");
-      tpm2_protector_dump_pcr (ctx->bank);
+      tpm2_protector_dump_pcr (tpm2_dump_bank);
     }
 
  exit2:
@@ -1151,7 +1156,7 @@ tpm2_protector_load_persistent (const tpm2_protector_context_t *ctx, TPM_HANDLE_
   if (dump_pcr == true)
     {
       grub_printf ("PCR Mismatch! Check firmware and bootloader before typing passphrase!\n");
-      tpm2_protector_dump_pcr (ctx->bank);
+      tpm2_protector_dump_pcr (tpm2_dump_bank);
     }
 
  exit:
@@ -1273,6 +1278,8 @@ tpm2_protector_check_args (tpm2_protector_context_t *ctx)
   if (ctx->bank == TPM_ALG_ERROR)
     ctx->bank = TPM_ALG_SHA256;
 
+  tpm2_dump_bank = ctx->bank;
+
   if (ctx->pcr_count == 0)
     {
       ctx->pcrs[0] = 7;
Signature · 7 lines
2.43.0


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

PATCH v2 3/3 tests/tpm2_key_protector_test: Add tests for SHA384 PCR bank

In reply to Gary Lin via Grub-devel

Add a few more tests to seal and unseal the key with the SHA384 PCR bank instead of the default SHA256 PCR bank.

---
 tests/tpm2_key_protector_test.in | 46 +++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 13 deletions(-)
tests/tpm2_key_protector_test.in +33 -13
index fae27f9e4..1d80d5d26 100644
@@ -136,16 +136,28 @@ done
 # Export the TCTI variable for tpm2-tools
 export TPM2TOOLS_TCTI="device:${tpm2dev}"
 
+# Check if the sha384 bank is available
+if [ "$(tpm2_getcap pcrs | grep sha384)" != "" ]; then
+    with_sha384=true
+fi
+
 # Extend PCR 0
 tpm2_pcrextend 0:sha256=$(echo "test0" | sha256sum | cut -d ' ' -f 1) || exit 99
+if [ "${with_sha384}" = "true" ]; then
+    tpm2_pcrextend 0:sha384=$(echo "test0" | sha384sum | cut -d ' ' -f 1) || exit 99
+fi
 
 # Extend PCR 1
 tpm2_pcrextend 1:sha256=$(echo "test1" | sha256sum | cut -d ' ' -f 1) || exit 99
+if [ "${with_sha384}" = "true" ]; then
+    tpm2_pcrextend 1:sha384=$(echo "test1" | sha384sum | cut -d ' ' -f 1) || exit 99
+fi
 
 tpm2_seal_unseal() {
     srk_alg="$1"
     handle_type="$2"
     srk_test="$3"
+    pcr_bank="$4"
 
     grub_srk_alg=${srk_alg}
 
@@ -170,7 +182,7 @@ tpm2_seal_unseal() {
 	--action=add \
 	--protector=tpm2 \
 	--tpm2key \
-	--tpm2-bank=sha256 \
+	--tpm2-bank="${pcr_bank}" \
 	--tpm2-pcrs=0,1 \
 	--tpm2-keyfile="${lukskeyfile}" \
 	--tpm2-outfile="${sealedkey}" || ret=$?
@@ -228,6 +240,7 @@ EOF
 tpm2_seal_unseal_nv() {
     handle_type="$1"
     key_type="$2"
+    pcr_bank="$3"
 
     extra_opt=""
     extra_grub_opt=""
@@ -241,7 +254,7 @@ tpm2_seal_unseal_nv() {
     if [ "$key_type" = "tpm2key" ]; then
 	extra_opt="--tpm2key"
     else
-	extra_grub_opt="--pcrs=0,1"
+	extra_grub_opt="--pcrs=0,1 -b ${pcr_bank}"
     fi
 
     grub_cfg=${tpm2testdir}/testcase.cfg
@@ -251,7 +264,7 @@ tpm2_seal_unseal_nv() {
 	--tpm2-device="${tpm2dev}" \
 	--action=add \
 	--protector=tpm2 \
-	--tpm2-bank=sha256 \
+	--tpm2-bank="${pcr_bank}" \
 	--tpm2-pcrs=0,1 \
 	--tpm2-keyfile="${lukskeyfile}" \
 	--tpm2-nvindex="${nv_index}" || ret=$?
@@ -293,13 +306,16 @@ EOF
 
 # Testcases for SRK mode
 declare -a srktests=()
-srktests+=("default transient no_fallback_srk")
-srktests+=("RSA transient no_fallback_srk")
-srktests+=("ECC transient no_fallback_srk")
-srktests+=("RSA persistent no_fallback_srk")
-srktests+=("ECC persistent no_fallback_srk")
-srktests+=("RSA transient fallback_srk")
-srktests+=("ECC transient fallback_srk")
+srktests+=("default transient no_fallback_srk sha256")
+srktests+=("RSA transient no_fallback_srk sha256")
+srktests+=("ECC transient no_fallback_srk sha256")
+srktests+=("RSA persistent no_fallback_srk sha256")
+srktests+=("ECC persistent no_fallback_srk sha256")
+srktests+=("RSA transient fallback_srk sha256")
+srktests+=("ECC transient fallback_srk sha256")
+if [ "${with_sha384}" = "true" ]; then
+    srktests+=("default transient no_fallback_srk sha384")
+fi
 
 exit_status=0
 
@@ -319,9 +335,13 @@ done
 
 # Testcases for NV index mode
 declare -a nvtests=()
-nvtests+=("persistent raw")
-nvtests+=("nvindex raw")
-nvtests+=("nvindex tpm2key")
+nvtests+=("persistent raw sha256")
+nvtests+=("nvindex raw sha256")
+nvtests+=("nvindex tpm2key sha256")
+if [ "${with_sha384}" = "true" ]; then
+    nvtests+=("persistent raw sha384")
+    nvtests+=("nvindex tpm2key sha384")
+fi
 
 for i in "${!nvtests[@]}"; do
     tpm2_seal_unseal_nv ${nvtests[$i]} || ret=$?
Signature · 7 lines
2.43.0


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

Re: [PATCH v2 3/3] tests/tpm2_key_protector_test: Add tests for SHA384 PCR bank

Quoted 5 lines · Gary Lin

Add a few more tests to seal and unseal the key with the SHA384 PCR bank instead of the default SHA256 PCR bank.

Quoted 103 lines
---
  tests/tpm2_key_protector_test.in | 46 +++++++++++++++++++++++---------
  1 file changed, 33 insertions(+), 13 deletions(-)
tests/tpm2_key_protector_test.in +26 -13
index fae27f9e4..1d80d5d26 100644
@@ -136,16 +136,28 @@ done
  # Export the TCTI variable for tpm2-tools
  export TPM2TOOLS_TCTI="device:${tpm2dev}"
 
+# Check if the sha384 bank is available
+if [ "$(tpm2_getcap pcrs | grep sha384)" != "" ]; then
+    with_sha384=true
+fi
+
  # Extend PCR 0
  tpm2_pcrextend 0:sha256=$(echo "test0" | sha256sum | cut -d ' ' -f 1) || exit 99
+if [ "${with_sha384}" = "true" ]; then
+    tpm2_pcrextend 0:sha384=$(echo "test0" | sha384sum | cut -d ' ' -f 1) || exit 99
+fi
 
  # Extend PCR 1
  tpm2_pcrextend 1:sha256=$(echo "test1" | sha256sum | cut -d ' ' -f 1) || exit 99
+if [ "${with_sha384}" = "true" ]; then
+    tpm2_pcrextend 1:sha384=$(echo "test1" | sha384sum | cut -d ' ' -f 1) || exit 99
+fi
 
  tpm2_seal_unseal() {
      srk_alg="$1"
      handle_type="$2"
      srk_test="$3"
+    pcr_bank="$4"
 
      grub_srk_alg=${srk_alg}
 
@@ -170,7 +182,7 @@ tpm2_seal_unseal() {
  	--action=add \
  	--protector=tpm2 \
  	--tpm2key \
-	--tpm2-bank=sha256 \
+	--tpm2-bank="${pcr_bank}" \
  	--tpm2-pcrs=0,1 \
  	--tpm2-keyfile="${lukskeyfile}" \
  	--tpm2-outfile="${sealedkey}" || ret=$?
@@ -228,6 +240,7 @@ EOF
  tpm2_seal_unseal_nv() {
      handle_type="$1"
      key_type="$2"
+    pcr_bank="$3"
 
      extra_opt=""
      extra_grub_opt=""
@@ -241,7 +254,7 @@ tpm2_seal_unseal_nv() {
      if [ "$key_type" = "tpm2key" ]; then
  	extra_opt="--tpm2key"
      else
-	extra_grub_opt="--pcrs=0,1"
+	extra_grub_opt="--pcrs=0,1 -b ${pcr_bank}"
      fi
 
      grub_cfg=${tpm2testdir}/testcase.cfg
@@ -251,7 +264,7 @@ tpm2_seal_unseal_nv() {
  	--tpm2-device="${tpm2dev}" \
  	--action=add \
  	--protector=tpm2 \
-	--tpm2-bank=sha256 \
+	--tpm2-bank="${pcr_bank}" \
  	--tpm2-pcrs=0,1 \
  	--tpm2-keyfile="${lukskeyfile}" \
  	--tpm2-nvindex="${nv_index}" || ret=$?
@@ -293,13 +306,16 @@ EOF
 
  # Testcases for SRK mode
  declare -a srktests=()
-srktests+=("default transient no_fallback_srk")
-srktests+=("RSA transient no_fallback_srk")
-srktests+=("ECC transient no_fallback_srk")
-srktests+=("RSA persistent no_fallback_srk")
-srktests+=("ECC persistent no_fallback_srk")
-srktests+=("RSA transient fallback_srk")
-srktests+=("ECC transient fallback_srk")
+srktests+=("default transient no_fallback_srk sha256")
+srktests+=("RSA transient no_fallback_srk sha256")
+srktests+=("ECC transient no_fallback_srk sha256")
+srktests+=("RSA persistent no_fallback_srk sha256")
+srktests+=("ECC persistent no_fallback_srk sha256")
+srktests+=("RSA transient fallback_srk sha256")
+srktests+=("ECC transient fallback_srk sha256")
+if [ "${with_sha384}" = "true" ]; then
+    srktests+=("default transient no_fallback_srk sha384")
+fi
 
  exit_status=0
 
@@ -319,9 +335,13 @@ done
 
  # Testcases for NV index mode
  declare -a nvtests=()
-nvtests+=("persistent raw")
-nvtests+=("nvindex raw")
-nvtests+=("nvindex tpm2key")

tests/tpm2_key_protector_test.in done · line 347

Quoted 10 lines
tests/tpm2_key_protector_test.in +7 -0
+nvtests+=("persistent raw sha256")
+nvtests+=("nvindex raw sha256")
+nvtests+=("nvindex tpm2key sha256")
+if [ "${with_sha384}" = "true" ]; then
+    nvtests+=("persistent raw sha384")
+    nvtests+=("nvindex tpm2key sha384")
+fi
 
  for i in "${!nvtests[@]}"; do
      tpm2_seal_unseal_nv ${nvtests[$i]} || ret=$?

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

#5 lore raw
In reply to Gary Lin via Grub-devel
Quoted 9 lines · Gary Lin

There are two bugs when using the TPM PCR banks other than the default SHA256 PCR bank:

1) Key unsealing failure
2) Dumping the wrong PCR bank

This patch set fixes the above two bugs and adds the tests for SHA384 PCR bank.

Quoted 7 lines

v2:
- Set the SHA256 PCR bank explicitly in tpm2_key_protector_test

Gary Lin (3):
grub-protect: Fix the hash algorithm of PCR digest
tpm2_key_protector: Dump the PCR bank for key unsealing tests/tpm2_key_protector_test: Add tests for SHA384 PCR bank

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

Daniel

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