public inbox for tpm-protocol@lists.linux.dev
 help / color / mirror / Atom feed
* [PATCH] fix(data): add the missing null variant to TpmuHa
@ 2025-09-01 18:10 Jarkko Sakkinen
  0 siblings, 0 replies; only message in thread
From: Jarkko Sakkinen @ 2025-09-01 18:10 UTC (permalink / raw)
  To: tpm-protocol; +Cc: Jarkko Sakkinen

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2025-09-01 18:10 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-01 18:10 [PATCH] fix(data): add the missing null variant to TpmuHa Jarkko Sakkinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox