-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #457 from brandsimon/sbr/add_public_marshalling
Add marshalling to Private (TPM2B_PRIVATE)
- Loading branch information
Showing
4 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tss-esapi/tests/integration_tests/structures_tests/buffers_tests/private.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |