diff --git a/tss-esapi/src/structures/buffers.rs b/tss-esapi/src/structures/buffers.rs index 0ad4b327..92eb604c 100644 --- a/tss-esapi/src/structures/buffers.rs +++ b/tss-esapi/src/structures/buffers.rs @@ -97,6 +97,7 @@ macro_rules! buffer_type { } pub mod attest; +pub mod private; pub mod public; pub mod sensitive; pub mod sensitive_create; @@ -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; diff --git a/tss-esapi/src/structures/buffers/private.rs b/tss-esapi/src/structures/buffers/private.rs new file mode 100644 index 00000000..62ee5054 --- /dev/null +++ b/tss-esapi/src/structures/buffers/private.rs @@ -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::(); + + /// 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 { + 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) + } +} diff --git a/tss-esapi/tests/integration_tests/structures_tests/buffers_tests/mod.rs b/tss-esapi/tests/integration_tests/structures_tests/buffers_tests/mod.rs index ea46ae30..b43cfb06 100644 --- a/tss-esapi/tests/integration_tests/structures_tests/buffers_tests/mod.rs +++ b/tss-esapi/tests/integration_tests/structures_tests/buffers_tests/mod.rs @@ -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; diff --git a/tss-esapi/tests/integration_tests/structures_tests/buffers_tests/private.rs b/tss-esapi/tests/integration_tests/structures_tests/buffers_tests/private.rs new file mode 100644 index 00000000..6c2f1700 --- /dev/null +++ b/tss-esapi/tests/integration_tests/structures_tests/buffers_tests/private.rs @@ -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); +}