Skip to content

Commit

Permalink
Reduce stack usage for DPE command input structures
Browse files Browse the repository at this point in the history
Use zerocopy to pass references to DPE command structures, using the
input slice as backing storage. This saves a few hundred bytes over
copying these values to structs on the stack.
  • Loading branch information
jhand2 committed Oct 18, 2024
1 parent a0a334d commit f025f93
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion dpe/src/commands/certify_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ mod tests {
.to_vec();
command.extend(TEST_CERTIFY_KEY_CMD.as_bytes());
assert_eq!(
Ok(Command::CertifyKey(TEST_CERTIFY_KEY_CMD)),
Ok(Command::CertifyKey(&TEST_CERTIFY_KEY_CMD)),
Command::deserialize(&command)
);
}
Expand Down
2 changes: 1 addition & 1 deletion dpe/src/commands/derive_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ mod tests {
.to_vec();
command.extend(TEST_DERIVE_CONTEXT_CMD.as_bytes());
assert_eq!(
Ok(Command::DeriveContext(TEST_DERIVE_CONTEXT_CMD)),
Ok(Command::DeriveContext(&TEST_DERIVE_CONTEXT_CMD)),
Command::deserialize(&command)
);
}
Expand Down
2 changes: 1 addition & 1 deletion dpe/src/commands/destroy_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ mod tests {
.to_vec();
command.extend(TEST_DESTROY_CTX_CMD.as_bytes());
assert_eq!(
Ok(Command::DestroyCtx(TEST_DESTROY_CTX_CMD)),
Ok(Command::DestroyCtx(&TEST_DESTROY_CTX_CMD)),
Command::deserialize(&command)
);
}
Expand Down
4 changes: 3 additions & 1 deletion dpe/src/commands/get_certificate_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ mod tests {
.to_vec();
command.extend(TEST_GET_CERTIFICATE_CHAIN_CMD.as_bytes());
assert_eq!(
Ok(Command::GetCertificateChain(TEST_GET_CERTIFICATE_CHAIN_CMD)),
Ok(Command::GetCertificateChain(
&TEST_GET_CERTIFICATE_CHAIN_CMD
)),
Command::deserialize(&command)
);
}
Expand Down
2 changes: 1 addition & 1 deletion dpe/src/commands/initialize_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ mod tests {
.to_vec();
command.extend(TEST_INIT_CTX_CMD.as_bytes());
assert_eq!(
Ok(Command::InitCtx(TEST_INIT_CTX_CMD)),
Ok(Command::InitCtx(&TEST_INIT_CTX_CMD)),
Command::deserialize(&command)
);
}
Expand Down
30 changes: 15 additions & 15 deletions dpe/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
DPE_PROFILE,
};
use core::mem::size_of;
use zerocopy::FromBytes;
use zerocopy::{FromBytes, Immutable, KnownLayout};

mod certify_key;
mod derive_context;
Expand All @@ -32,19 +32,19 @@ mod rotate_context;
mod sign;

#[derive(Debug, PartialEq, Eq)]
pub enum Command {
pub enum Command<'a> {
GetProfile,
InitCtx(InitCtxCmd),
DeriveContext(DeriveContextCmd),
CertifyKey(CertifyKeyCmd),
Sign(SignCmd),
InitCtx(&'a InitCtxCmd),
DeriveContext(&'a DeriveContextCmd),
CertifyKey(&'a CertifyKeyCmd),
Sign(&'a SignCmd),
#[cfg(not(feature = "disable_rotate_context"))]
RotateCtx(RotateCtxCmd),
DestroyCtx(DestroyCtxCmd),
GetCertificateChain(GetCertificateChainCmd),
RotateCtx(&'a RotateCtxCmd),
DestroyCtx(&'a DestroyCtxCmd),
GetCertificateChain(&'a GetCertificateChainCmd),
}

impl Command {
impl Command<'_> {
pub const GET_PROFILE: u32 = 0x01;
pub const INITIALIZE_CONTEXT: u32 = 0x07;
pub const DERIVE_CONTEXT: u32 = 0x08;
Expand Down Expand Up @@ -80,17 +80,17 @@ impl Command {
}
}

fn parse_command<T: FromBytes>(
build: impl FnOnce(T) -> Command,
bytes: &[u8],
fn parse_command<'a, T: FromBytes + KnownLayout + Immutable + 'a>(
build: impl FnOnce(&'a T) -> Command,
bytes: &'a [u8],
) -> Result<Command, DpeErrorCode> {
let (prefix, _remaining_bytes) =
T::read_from_prefix(bytes).map_err(|_| DpeErrorCode::InvalidArgument)?;
T::ref_from_prefix(bytes).map_err(|_| DpeErrorCode::InvalidArgument)?;
Ok(build(prefix))
}
}

impl From<Command> for u32 {
impl From<Command<'_>> for u32 {
fn from(cmd: Command) -> u32 {
match cmd {
Command::GetProfile => Command::GET_PROFILE,
Expand Down
2 changes: 1 addition & 1 deletion dpe/src/commands/rotate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ mod tests {
.to_vec();
command.extend(TEST_ROTATE_CTX_CMD.as_bytes());
assert_eq!(
Ok(Command::RotateCtx(TEST_ROTATE_CTX_CMD)),
Ok(Command::RotateCtx(&TEST_ROTATE_CTX_CMD)),
Command::deserialize(&command)
);
}
Expand Down
2 changes: 1 addition & 1 deletion dpe/src/commands/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ mod tests {
let mut command = CommandHdr::new_for_test(Command::SIGN).as_bytes().to_vec();
command.extend(TEST_SIGN_CMD.as_bytes());
assert_eq!(
Ok(Command::Sign(TEST_SIGN_CMD)),
Ok(Command::Sign(&TEST_SIGN_CMD)),
Command::deserialize(&command)
);
}
Expand Down

0 comments on commit f025f93

Please sign in to comment.