Skip to content

Commit

Permalink
Merge pull request #457 from brandsimon/sbr/add_public_marshalling
Browse files Browse the repository at this point in the history
Add marshalling to Private (TPM2B_PRIVATE)
  • Loading branch information
ionut-arm authored Oct 21, 2023
2 parents a3765db + 8066e05 commit d942908
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
6 changes: 1 addition & 5 deletions tss-esapi/src/structures/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ macro_rules! buffer_type {
}

pub mod attest;
pub mod private;
pub mod public;
pub mod sensitive;
pub mod sensitive_create;
Expand Down Expand Up @@ -248,11 +249,6 @@ pub mod nonce {
buffer_type!(Nonce, 64, TPM2B_NONCE);
}

pub mod private {
use tss_esapi_sys::_PRIVATE;
buffer_type!(Private, ::std::mem::size_of::<_PRIVATE>(), TPM2B_PRIVATE);
}

pub mod private_key_rsa {
use crate::tss2_esys::TPM2_MAX_RSA_KEY_BYTES;

Expand Down
69 changes: 69 additions & 0 deletions tss-esapi/src/structures/buffers/private.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2023 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0

use crate::{
traits::{Marshall, UnMarshall},
ReturnCode,
};
use std::convert::TryInto;
use tss_esapi_sys::_PRIVATE;

buffer_type!(Private, ::std::mem::size_of::<_PRIVATE>(), TPM2B_PRIVATE);

impl Marshall for Private {
const BUFFER_SIZE: usize = std::mem::size_of::<TPM2B_PRIVATE>();

/// Produce a marshalled [`TPM2B_PRIVATE`]
fn marshall_offset(
&self,
marshalled_data: &mut [u8],
offset: &mut std::os::raw::c_ulong,
) -> Result<()> {
ReturnCode::ensure_success(
unsafe {
crate::tss2_esys::Tss2_MU_TPM2B_PRIVATE_Marshal(
&self.clone().try_into().map_err(|e| {
error!("Failed to convert Private to TPM2B_PRIVATE: {}", e);
Error::local_error(WrapperErrorKind::InvalidParam)
})?,
marshalled_data.as_mut_ptr(),
marshalled_data.len().try_into().map_err(|e| {
error!("Failed to convert size of buffer to TSS size_t type: {}", e);
Error::local_error(WrapperErrorKind::InvalidParam)
})?,
offset,
)
},
|ret| {
error!("Failed to marshal Private: {}", ret);
},
)?;

Ok(())
}
}

impl UnMarshall for Private {
/// Unmarshall the structure from [`TPM2B_PRIVATE`]
fn unmarshall_offset(
marshalled_data: &[u8],
offset: &mut std::os::raw::c_ulong,
) -> Result<Self> {
let mut dest = TPM2B_PRIVATE::default();
ReturnCode::ensure_success(
unsafe {
crate::tss2_esys::Tss2_MU_TPM2B_PRIVATE_Unmarshal(
marshalled_data.as_ptr(),
marshalled_data.len().try_into().map_err(|e| {
error!("Failed to convert length of marshalled data: {}", e);
Error::local_error(WrapperErrorKind::InvalidParam)
})?,
offset,
&mut dest,
)
},
|ret| error!("Failed to unmarshal Private: {}", ret),
)?;
Private::try_from(dest)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod data_tests;
mod digest_tests;
mod max_buffer_tests;
mod nonce_tests;
mod private;
mod public;
mod sensitive;
mod sensitive_create_buffer_tests;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2023 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0

use std::convert::TryFrom;
use tss_esapi::structures::Private;

#[test]
fn marshall_unmarshall() {
crate::common::check_marshall_unmarshall(&Private::default());
let private = Private::try_from([0xff; 100].to_vec()).unwrap();
crate::common::check_marshall_unmarshall(&private);
}

#[test]
fn marshall_unmarshall_offset() {
crate::common::check_marshall_unmarshall_offset(&Private::default());
let private = Private::try_from([0xff; 100].to_vec()).unwrap();
crate::common::check_marshall_unmarshall_offset(&private);
}

0 comments on commit d942908

Please sign in to comment.