[PATCH] fix(data): add the missing null variant to TpmuHa

Jarkko Sakkinen <[email protected]> Mon, 1 Sep 2025 21:10:37 +0300
Newsgroups dev.linux.lists.tpm-protocol
Message-ID <[email protected]>
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 <[email protected]>
---
 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