public inbox for tpm-protocol@lists.linux.dev
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: tpm-protocol@lists.linux.dev
Cc: Jarkko Sakkinen <jarkko@kernel.org>
Subject: [PATCH] fix(data): add the missing null variant to TpmuHa
Date: Mon,  1 Sep 2025 21:10:37 +0300	[thread overview]
Message-ID: <20250901181037.2262746-1-jarkko@kernel.org> (raw)

Add the forgoten Null variant to `TpmuHa` and change `TpmuHa::default` to
return this variant. This fix will in effect address a data corruption bug
in `TpmtHa::default`, which triggered the analysis for the root cause:

1. `hash_alg` is set to null
2. `digest` is set to SHA-256.

With the commit applied the algorithm mismatch ceases to exist.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 src/data/tpmu.rs | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/data/tpmu.rs b/src/data/tpmu.rs
index 0fb64f7..2d7e80f 100644
--- a/src/data/tpmu.rs
+++ b/src/data/tpmu.rs
@@ -84,6 +84,7 @@ impl TpmParseTagged for TpmuCapabilities {
 
 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
 pub enum TpmuHa {
+    Null,
     Sha1([u8; 20]),
     Sha256([u8; 32]),
     Sha384([u8; 48]),
@@ -98,12 +99,19 @@ impl TpmTagged for TpmuHa {
 
 impl TpmBuild for TpmuHa {
     fn build(&self, writer: &mut TpmWriter) -> TpmResult<()> {
-        writer.write_bytes(self)
+        match self {
+            Self::Null => Ok(()),
+            _ => writer.write_bytes(self),
+        }
     }
 }
 
 impl TpmParseTagged for TpmuHa {
     fn parse_tagged(tag: TpmAlgId, buf: &[u8]) -> TpmResult<(Self, &[u8])> {
+        if tag == TpmAlgId::Null {
+            return Ok((Self::Null, buf));
+        }
+
         let digest_size = tpm_hash_size(&tag).ok_or(TpmErrorKind::InvalidValue)?;
         if buf.len() < digest_size {
             return Err(TpmErrorKind::ParseUnderflow);
@@ -126,7 +134,7 @@ impl TpmParseTagged for TpmuHa {
 
 impl Default for TpmuHa {
     fn default() -> Self {
-        Self::Sha256([0; 32])
+        Self::Null
     }
 }
 
@@ -138,6 +146,7 @@ impl TpmSized for TpmuHa {
             Self::Sha256(d) | Self::Sm3_256(d) => d.len(),
             Self::Sha384(d) => d.len(),
             Self::Sha512(d) => d.len(),
+            Self::Null => 0,
         }
     }
 }
@@ -151,6 +160,7 @@ impl Deref for TpmuHa {
             Self::Sha256(d) | Self::Sm3_256(d) => d,
             Self::Sha384(d) => d,
             Self::Sha512(d) => d,
+            Self::Null => &[],
         }
     }
 }
-- 
2.39.5


                 reply	other threads:[~2025-09-01 18:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250901181037.2262746-1-jarkko@kernel.org \
    --to=jarkko@kernel.org \
    --cc=tpm-protocol@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox