Skip to content

Commit d942908

Browse files
authored
Merge pull request #457 from brandsimon/sbr/add_public_marshalling
Add marshalling to Private (TPM2B_PRIVATE)
2 parents a3765db + 8066e05 commit d942908

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed

tss-esapi/src/structures/buffers.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ macro_rules! buffer_type {
9797
}
9898

9999
pub mod attest;
100+
pub mod private;
100101
pub mod public;
101102
pub mod sensitive;
102103
pub mod sensitive_create;
@@ -248,11 +249,6 @@ pub mod nonce {
248249
buffer_type!(Nonce, 64, TPM2B_NONCE);
249250
}
250251

251-
pub mod private {
252-
use tss_esapi_sys::_PRIVATE;
253-
buffer_type!(Private, ::std::mem::size_of::<_PRIVATE>(), TPM2B_PRIVATE);
254-
}
255-
256252
pub mod private_key_rsa {
257253
use crate::tss2_esys::TPM2_MAX_RSA_KEY_BYTES;
258254

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2023 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use crate::{
5+
traits::{Marshall, UnMarshall},
6+
ReturnCode,
7+
};
8+
use std::convert::TryInto;
9+
use tss_esapi_sys::_PRIVATE;
10+
11+
buffer_type!(Private, ::std::mem::size_of::<_PRIVATE>(), TPM2B_PRIVATE);
12+
13+
impl Marshall for Private {
14+
const BUFFER_SIZE: usize = std::mem::size_of::<TPM2B_PRIVATE>();
15+
16+
/// Produce a marshalled [`TPM2B_PRIVATE`]
17+
fn marshall_offset(
18+
&self,
19+
marshalled_data: &mut [u8],
20+
offset: &mut std::os::raw::c_ulong,
21+
) -> Result<()> {
22+
ReturnCode::ensure_success(
23+
unsafe {
24+
crate::tss2_esys::Tss2_MU_TPM2B_PRIVATE_Marshal(
25+
&self.clone().try_into().map_err(|e| {
26+
error!("Failed to convert Private to TPM2B_PRIVATE: {}", e);
27+
Error::local_error(WrapperErrorKind::InvalidParam)
28+
})?,
29+
marshalled_data.as_mut_ptr(),
30+
marshalled_data.len().try_into().map_err(|e| {
31+
error!("Failed to convert size of buffer to TSS size_t type: {}", e);
32+
Error::local_error(WrapperErrorKind::InvalidParam)
33+
})?,
34+
offset,
35+
)
36+
},
37+
|ret| {
38+
error!("Failed to marshal Private: {}", ret);
39+
},
40+
)?;
41+
42+
Ok(())
43+
}
44+
}
45+
46+
impl UnMarshall for Private {
47+
/// Unmarshall the structure from [`TPM2B_PRIVATE`]
48+
fn unmarshall_offset(
49+
marshalled_data: &[u8],
50+
offset: &mut std::os::raw::c_ulong,
51+
) -> Result<Self> {
52+
let mut dest = TPM2B_PRIVATE::default();
53+
ReturnCode::ensure_success(
54+
unsafe {
55+
crate::tss2_esys::Tss2_MU_TPM2B_PRIVATE_Unmarshal(
56+
marshalled_data.as_ptr(),
57+
marshalled_data.len().try_into().map_err(|e| {
58+
error!("Failed to convert length of marshalled data: {}", e);
59+
Error::local_error(WrapperErrorKind::InvalidParam)
60+
})?,
61+
offset,
62+
&mut dest,
63+
)
64+
},
65+
|ret| error!("Failed to unmarshal Private: {}", ret),
66+
)?;
67+
Private::try_from(dest)
68+
}
69+
}

tss-esapi/tests/integration_tests/structures_tests/buffers_tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod data_tests;
66
mod digest_tests;
77
mod max_buffer_tests;
88
mod nonce_tests;
9+
mod private;
910
mod public;
1011
mod sensitive;
1112
mod sensitive_create_buffer_tests;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2023 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use std::convert::TryFrom;
5+
use tss_esapi::structures::Private;
6+
7+
#[test]
8+
fn marshall_unmarshall() {
9+
crate::common::check_marshall_unmarshall(&Private::default());
10+
let private = Private::try_from([0xff; 100].to_vec()).unwrap();
11+
crate::common::check_marshall_unmarshall(&private);
12+
}
13+
14+
#[test]
15+
fn marshall_unmarshall_offset() {
16+
crate::common::check_marshall_unmarshall_offset(&Private::default());
17+
let private = Private::try_from([0xff; 100].to_vec()).unwrap();
18+
crate::common::check_marshall_unmarshall_offset(&private);
19+
}

0 commit comments

Comments
 (0)