|
1 | 1 | // Copyright 2021 Contributors to the Parsec project. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -use log::error; |
5 | 4 | use tss_esapi_sys::TPMI_ST_COMMAND_TAG; |
6 | 5 |
|
7 | 6 | use crate::{ |
8 | | - constants::StructureTag, |
9 | | - traits::{Marshall, UnMarshall}, |
10 | | - tss2_esys::TPMI_ST_ATTEST, |
11 | | - Error, Result, ReturnCode, WrapperErrorKind, |
| 7 | + constants::StructureTag, traits::impl_mu_simple, tss2_esys::TPMI_ST_ATTEST, Error, Result, |
| 8 | + WrapperErrorKind, |
12 | 9 | }; |
13 | | -use std::convert::{TryFrom, TryInto}; |
| 10 | +use std::convert::TryFrom; |
14 | 11 |
|
15 | 12 | /// Type of attestation. |
16 | 13 | /// |
@@ -75,60 +72,7 @@ impl TryFrom<TPMI_ST_ATTEST> for AttestationType { |
75 | 72 | } |
76 | 73 | } |
77 | 74 |
|
78 | | -impl Marshall for AttestationType { |
79 | | - const BUFFER_SIZE: usize = std::mem::size_of::<TPMI_ST_ATTEST>(); |
80 | | - |
81 | | - fn marshall_offset( |
82 | | - &self, |
83 | | - marshalled_data: &mut [u8], |
84 | | - offset: &mut std::os::raw::c_ulong, |
85 | | - ) -> Result<()> { |
86 | | - ReturnCode::ensure_success( |
87 | | - unsafe { |
88 | | - crate::tss2_esys::Tss2_MU_TPM2_ST_Marshal( |
89 | | - (*self).into(), |
90 | | - marshalled_data.as_mut_ptr(), |
91 | | - marshalled_data.len().try_into().map_err(|e| { |
92 | | - error!("Failed to convert size of buffer to TSS size_t type: {}", e); |
93 | | - Error::local_error(WrapperErrorKind::InvalidParam) |
94 | | - })?, |
95 | | - offset, |
96 | | - ) |
97 | | - }, |
98 | | - |ret| { |
99 | | - error!("Failed to marshal AttestationType: {}", ret); |
100 | | - }, |
101 | | - )?; |
102 | | - |
103 | | - Ok(()) |
104 | | - } |
105 | | -} |
106 | | - |
107 | | -impl UnMarshall for AttestationType { |
108 | | - fn unmarshall_offset( |
109 | | - marshalled_data: &[u8], |
110 | | - offset: &mut std::os::raw::c_ulong, |
111 | | - ) -> Result<Self> { |
112 | | - let mut dest = TPMI_ST_ATTEST::default(); |
113 | | - |
114 | | - ReturnCode::ensure_success( |
115 | | - unsafe { |
116 | | - crate::tss2_esys::Tss2_MU_TPM2_ST_Unmarshal( |
117 | | - marshalled_data.as_ptr(), |
118 | | - marshalled_data.len().try_into().map_err(|e| { |
119 | | - error!("Failed to convert length of marshalled data: {}", e); |
120 | | - Error::local_error(WrapperErrorKind::InvalidParam) |
121 | | - })?, |
122 | | - offset, |
123 | | - &mut dest, |
124 | | - ) |
125 | | - }, |
126 | | - |ret| error!("Failed to unmarshal AttestationType: {}", ret), |
127 | | - )?; |
128 | | - |
129 | | - AttestationType::try_from(dest) |
130 | | - } |
131 | | -} |
| 75 | +impl_mu_simple!(AttestationType, TPMI_ST_ATTEST, TPM2_ST); |
132 | 76 |
|
133 | 77 | /// Type of command tag. |
134 | 78 | /// |
@@ -175,57 +119,4 @@ impl TryFrom<TPMI_ST_COMMAND_TAG> for CommandTag { |
175 | 119 | } |
176 | 120 | } |
177 | 121 |
|
178 | | -impl Marshall for CommandTag { |
179 | | - const BUFFER_SIZE: usize = std::mem::size_of::<TPMI_ST_COMMAND_TAG>(); |
180 | | - |
181 | | - fn marshall_offset( |
182 | | - &self, |
183 | | - marshalled_data: &mut [u8], |
184 | | - offset: &mut std::os::raw::c_ulong, |
185 | | - ) -> Result<()> { |
186 | | - ReturnCode::ensure_success( |
187 | | - unsafe { |
188 | | - crate::tss2_esys::Tss2_MU_TPM2_ST_Marshal( |
189 | | - (*self).into(), |
190 | | - marshalled_data.as_mut_ptr(), |
191 | | - marshalled_data.len().try_into().map_err(|e| { |
192 | | - error!("Failed to convert size of buffer to TSS size_t type: {}", e); |
193 | | - Error::local_error(WrapperErrorKind::InvalidParam) |
194 | | - })?, |
195 | | - offset, |
196 | | - ) |
197 | | - }, |
198 | | - |ret| { |
199 | | - error!("Failed to marshal CommandTag: {}", ret); |
200 | | - }, |
201 | | - )?; |
202 | | - |
203 | | - Ok(()) |
204 | | - } |
205 | | -} |
206 | | - |
207 | | -impl UnMarshall for CommandTag { |
208 | | - fn unmarshall_offset( |
209 | | - marshalled_data: &[u8], |
210 | | - offset: &mut std::os::raw::c_ulong, |
211 | | - ) -> Result<Self> { |
212 | | - let mut dest = TPMI_ST_COMMAND_TAG::default(); |
213 | | - |
214 | | - ReturnCode::ensure_success( |
215 | | - unsafe { |
216 | | - crate::tss2_esys::Tss2_MU_TPM2_ST_Unmarshal( |
217 | | - marshalled_data.as_ptr(), |
218 | | - marshalled_data.len().try_into().map_err(|e| { |
219 | | - error!("Failed to convert length of marshalled data: {}", e); |
220 | | - Error::local_error(WrapperErrorKind::InvalidParam) |
221 | | - })?, |
222 | | - offset, |
223 | | - &mut dest, |
224 | | - ) |
225 | | - }, |
226 | | - |ret| error!("Failed to unmarshal CommandTag: {}", ret), |
227 | | - )?; |
228 | | - |
229 | | - CommandTag::try_from(dest) |
230 | | - } |
231 | | -} |
| 122 | +impl_mu_simple!(CommandTag, TPMI_ST_COMMAND_TAG, TPM2_ST); |
0 commit comments