hal/blocking, veer: Store the DPE signature through a reference Sign::sign stores the signature in a caller-provided value and returns the next handle, so that a large signature such as ML-DSA is not copied through the return value. Assisted-by: Claude:claude-fable-5-2
diff --git a/hal/blocking/src/dpe.rs b/hal/blocking/src/dpe.rs index 5270fa7..da46a6e 100644 --- a/hal/blocking/src/dpe.rs +++ b/hal/blocking/src/dpe.rs
@@ -40,17 +40,18 @@ //! fn sign_twice<D: Sign<P384Sha384>>( //! dpe: &mut D, //! digest: &Digest<12>, -//! ) -> Result<[P384Signature; 2], ErrorKind> { -//! let first = dpe -//! .sign(&ContextHandle::DEFAULT, &LABEL, digest) +//! signatures: &mut [P384Signature; 2], +//! ) -> Result<(), ErrorKind> { +//! let [first, second] = signatures; +//! let handle = dpe +//! .sign(&ContextHandle::DEFAULT, &LABEL, digest, first) //! .map_err(|error| error.kind())?; //! // The second command uses the handle that the first one returned. -//! let second = dpe -//! .sign(&first.handle, &LABEL, digest) +//! dpe.sign(&handle, &LABEL, digest, second) //! .map_err(|error| error.kind())?; -//! Ok([first.signature, second.signature]) +//! Ok(()) //! } -//! # use openprot_hal_blocking::dpe::{ErrorType, SignOutput}; +//! # use openprot_hal_blocking::dpe::ErrorType; //! # struct Fake; //! # impl ErrorType for Fake { type Error = core::convert::Infallible; } //! # impl Sign<P384Sha384> for Fake { @@ -59,11 +60,15 @@ //! # handle: &ContextHandle, //! # _label: &[u8; 48], //! # _digest: &Digest<12>, -//! # ) -> Result<SignOutput<P384Signature>, Self::Error> { -//! # Ok(SignOutput { signature: P384Signature::new([2; 48], [3; 48]), handle: *handle }) +//! # signature: &mut P384Signature, +//! # ) -> Result<ContextHandle, Self::Error> { +//! # *signature = P384Signature::new([2; 48], [3; 48]); +//! # Ok(*handle) //! # } //! # } -//! # assert!(sign_twice(&mut Fake, &Digest::new([0; 12])).is_ok()); +//! # let empty = || P384Signature::new([0; 48], [0; 48]); +//! # let mut signatures = [empty(), empty()]; +//! # assert!(sign_twice(&mut Fake, &Digest::new([0; 12]), &mut signatures).is_ok()); //! ``` use core::fmt; @@ -185,19 +190,12 @@ pub const DEFAULT: Self = Self([0; 16]); } -/// The result of [`Sign::sign`]. -#[derive(Debug, Clone)] -pub struct SignOutput<S> { - /// The signature. - pub signature: S, - - /// The handle to use for the context in the next command. - pub handle: ContextHandle, -} - /// The DPE `Sign` command for profile `P`. pub trait Sign<P: Profile>: ErrorType { - /// Signs `digest` with a key of the context that `handle` names. + /// Signs `digest` with a key of the context that `handle` names and + /// stores the signature in `signature`. + /// + /// Returns the handle to use for the context in the next command. /// /// The DPE derives the key from the measurements of the context and from /// `label`, so the same context and label give the same key until a @@ -207,12 +205,15 @@ /// /// When the response is lost, the caller cannot tell whether the DPE /// replaced `handle`. + /// + /// After an error, the content of `signature` is unspecified. fn sign( &mut self, handle: &ContextHandle, label: &P::Label, digest: &P::Digest, - ) -> Result<SignOutput<P::Signature>, Self::Error>; + signature: &mut P::Signature, + ) -> Result<ContextHandle, Self::Error>; } impl<P: Profile, T: Sign<P> + ?Sized> Sign<P> for &mut T { @@ -221,7 +222,8 @@ handle: &ContextHandle, label: &P::Label, digest: &P::Digest, - ) -> Result<SignOutput<P::Signature>, Self::Error> { - T::sign(self, handle, label, digest) + signature: &mut P::Signature, + ) -> Result<ContextHandle, Self::Error> { + T::sign(self, handle, label, digest, signature) } }
diff --git a/target/veer/peripherals/dpe.rs b/target/veer/peripherals/dpe.rs index e7b11a1..66c3f76 100644 --- a/target/veer/peripherals/dpe.rs +++ b/target/veer/peripherals/dpe.rs
@@ -25,9 +25,7 @@ use caliptra_dpe::response::{ResponseHdr, SignP384Resp}; use caliptra_dpe::DpeProfile; use openprot_hal_blocking::digest::Digest; -use openprot_hal_blocking::dpe::{ - ContextHandle, Error, ErrorKind, ErrorType, P384Sha384, Sign, SignOutput, -}; +use openprot_hal_blocking::dpe::{ContextHandle, Error, ErrorKind, ErrorType, P384Sha384, Sign}; use openprot_hal_blocking::ecdsa::P384Signature; use veer_mailbox::{Mailbox, MailboxError}; use zerocopy::{Immutable, IntoBytes, TryFromBytes}; @@ -137,7 +135,8 @@ handle: &ContextHandle, label: &[u8; 48], digest: &Digest<12>, - ) -> Result<SignOutput<P384Signature>, Self::Error> { + signature: &mut P384Signature, + ) -> Result<ContextHandle, Self::Error> { let request = SignRequest { data_size: SIGN_COMMAND_BYTES as u32, header: CommandHdr::new(DpeProfile::P384Sha384, Command::SIGN), @@ -175,9 +174,7 @@ { return Err(CaliptraDpeError::InvalidResponse); } - Ok(SignOutput { - signature: P384Signature::new(sig_r, sig_s), - handle: ContextHandle(new_context_handle.0), - }) + *signature = P384Signature::new(sig_r, sig_s); + Ok(ContextHandle(new_context_handle.0)) } }
diff --git a/target/veer/tests/dpe/test_dpe.rs b/target/veer/tests/dpe/test_dpe.rs index fb380ca..65b6abf 100644 --- a/target/veer/tests/dpe/test_dpe.rs +++ b/target/veer/tests/dpe/test_dpe.rs
@@ -89,8 +89,9 @@ label: &[u8; 48], digest: &Digest<12>, ) -> Result<(P384Signature, ContextHandle), &'static str> { - match dpe.sign(handle, label, digest) { - Ok(output) => Ok((output.signature, output.handle)), + let mut signature = P384Signature::new([0; 48], [0; 48]); + match dpe.sign(handle, label, digest, &mut signature) { + Ok(handle) => Ok((signature, handle)), Err(CaliptraDpeError::Dpe(status)) => { pw_log::error!("Sign: DPE status {:#x}", status as u32); Err("Sign failed in the DPE") @@ -195,7 +196,8 @@ pw_log::info!("other label: ok"); let unknown = ContextHandle([0xa5; 16]); - match dpe.sign(&unknown, &LABEL, &digest) { + let mut unused = P384Signature::new([0; 48], [0; 48]); + match dpe.sign(&unknown, &LABEL, &digest, &mut unused) { Err(error) if error.kind() == ErrorKind::InvalidHandle => {} Err(_) => return Err("unknown handle gives the wrong error"), Ok(_) => return Err("unknown handle signs"),