Skip to content

Commit bf24797

Browse files
committed
Fixes issue wrong max size in some sized buffer types.
Some of the sized buffers had their buffer sizes set as numbers. Even though this in some cases were the correct numbers they were a little hard to determine if they actually followed the size specified in the standard. So this PR fixes #548 in the main branch by using the the calculations specified in the standard for the buffer sizes. Signed-off-by: Jesper Brynolf <jesper.brynolf@gmail.com>
1 parent 97ccc11 commit bf24797

File tree

19 files changed

+377
-223
lines changed

19 files changed

+377
-223
lines changed

tss-esapi/src/structures/buffers.rs

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,30 @@ pub mod sensitive;
103103
pub mod sensitive_create;
104104

105105
pub mod auth {
106-
buffer_type!(Auth, 64, TPM2B_AUTH);
106+
// Same size as TPM2B_DIGEST according to the specification.
107+
use crate::tss2_esys::TPMU_HA;
108+
use std::mem::size_of;
109+
const TPM2B_AUTH_BUFFER_SIZE: usize = size_of::<TPMU_HA>();
110+
buffer_type!(Auth, TPM2B_AUTH_BUFFER_SIZE, TPM2B_AUTH);
107111
}
108112

109113
pub mod data {
110-
buffer_type!(Data, 64, TPM2B_DATA);
114+
// This should, according to the specification, be
115+
// size_of::<TPMT_HA>() but due to a bug in tpm2-tss
116+
// (https://github.com/tpm2-software/tpm2-tss/issues/2888)
117+
// it is the size of TPMU_HA
118+
use crate::tss2_esys::TPMU_HA;
119+
use std::mem::size_of;
120+
const TPM2B_DATA_BUFFER_SIZE: usize = size_of::<TPMU_HA>();
121+
buffer_type!(Data, TPM2B_DATA_BUFFER_SIZE, TPM2B_DATA);
111122
}
112123

113124
pub mod digest {
114-
buffer_type!(Digest, 64, TPM2B_DIGEST);
125+
use crate::tss2_esys::TPMU_HA;
126+
use std::mem::size_of;
127+
const TPM2B_DIGEST_BUFFER_SIZE: usize = size_of::<TPMU_HA>();
128+
129+
buffer_type!(Digest, TPM2B_DIGEST_BUFFER_SIZE, TPM2B_DIGEST);
115130

116131
// Some implementations to get from Digest to [u8; N] for common values of N (sha* primarily)
117132
// This is used to work around the fact that Rust does not allow custom functions for general values of N in [T; N],
@@ -208,80 +223,101 @@ pub mod digest {
208223
}
209224

210225
pub mod ecc_parameter {
226+
use crate::tss2_esys::TPM2_MAX_ECC_KEY_BYTES;
227+
const TPM2B_ECC_PARAMETER_BUFFER_SIZE: usize = TPM2_MAX_ECC_KEY_BYTES as usize;
211228
buffer_type!(
212229
EccParameter,
213-
crate::tss2_esys::TPM2_MAX_ECC_KEY_BYTES as usize,
230+
TPM2B_ECC_PARAMETER_BUFFER_SIZE,
214231
TPM2B_ECC_PARAMETER
215232
);
216233
}
217234

218235
pub mod encrypted_secret {
219-
named_field_buffer_type!(EncryptedSecret, 256, TPM2B_ENCRYPTED_SECRET, secret);
236+
use crate::tss2_esys::TPMU_ENCRYPTED_SECRET;
237+
use std::mem::size_of;
238+
const TPM2B_ENCRYPTED_SECRET_BUFFER_SIZE: usize = size_of::<TPMU_ENCRYPTED_SECRET>();
239+
named_field_buffer_type!(
240+
EncryptedSecret,
241+
TPM2B_ENCRYPTED_SECRET_BUFFER_SIZE,
242+
TPM2B_ENCRYPTED_SECRET,
243+
secret
244+
);
220245
}
221246

222247
pub mod id_object {
223-
named_field_buffer_type!(IdObject, 256, TPM2B_ID_OBJECT, credential);
248+
use crate::tss2_esys::TPMS_ID_OBJECT;
249+
use std::mem::size_of;
250+
const TPM2B_ID_OBJECT_BUFFER_SIZE: usize = size_of::<TPMS_ID_OBJECT>();
251+
named_field_buffer_type!(
252+
IdObject,
253+
TPM2B_ID_OBJECT_BUFFER_SIZE,
254+
TPM2B_ID_OBJECT,
255+
credential
256+
);
224257
}
225258

226259
pub mod initial_value {
227-
buffer_type!(
228-
InitialValue,
229-
crate::tss2_esys::TPM2_MAX_SYM_BLOCK_SIZE as usize,
230-
TPM2B_IV
231-
);
260+
use crate::tss2_esys::TPM2_MAX_SYM_BLOCK_SIZE;
261+
const TPM2B_IV_BUFFER_SIZE: usize = TPM2_MAX_SYM_BLOCK_SIZE as usize;
262+
buffer_type!(InitialValue, TPM2B_IV_BUFFER_SIZE, TPM2B_IV);
232263
}
233264

234265
pub mod max_buffer {
235266
use crate::tss2_esys::TPM2_MAX_DIGEST_BUFFER;
236-
buffer_type!(MaxBuffer, TPM2_MAX_DIGEST_BUFFER as usize, TPM2B_MAX_BUFFER);
267+
const TPM2B_MAX_BUFFER_BUFFER_SIZE: usize = TPM2_MAX_DIGEST_BUFFER as usize;
268+
buffer_type!(MaxBuffer, TPM2B_MAX_BUFFER_BUFFER_SIZE, TPM2B_MAX_BUFFER);
237269
}
238270

239271
pub mod max_nv_buffer {
240272
use crate::tss2_esys::TPM2_MAX_NV_BUFFER_SIZE;
273+
const TPM2B_MAX_NV_BUFFER_BUFFER_SIZE: usize = TPM2_MAX_NV_BUFFER_SIZE as usize;
241274
buffer_type!(
242275
MaxNvBuffer,
243-
TPM2_MAX_NV_BUFFER_SIZE as usize,
276+
TPM2B_MAX_NV_BUFFER_BUFFER_SIZE,
244277
TPM2B_MAX_NV_BUFFER
245278
);
246279
}
247280

248281
pub mod nonce {
249-
buffer_type!(Nonce, 64, TPM2B_NONCE);
282+
// Same size as TPM2B_DIGEST according to the specification.
283+
use crate::tss2_esys::TPMU_HA;
284+
use std::mem::size_of;
285+
const TPM2B_NONCE_BUFFER_SIZE: usize = size_of::<TPMU_HA>();
286+
287+
buffer_type!(Nonce, TPM2B_NONCE_BUFFER_SIZE, TPM2B_NONCE);
250288
}
251289

252290
pub mod private_key_rsa {
253291
use crate::tss2_esys::TPM2_MAX_RSA_KEY_BYTES;
292+
const TPM2B_PRIVATE_KEY_RSA_BUFFER_SIZE: usize = (TPM2_MAX_RSA_KEY_BYTES as usize) * 5 / 2;
254293

255-
// The maximum size is given in the spec as:
256-
// "RSA_PRIVATE_SIZE is a vendor specific value that can be (MAX_RSA_KEY_BYTES / 2) or
257-
// ((MAX_RSA_KEY_BYTES * 5) ./ 2. The larger size would only apply to keys that have fixedTPM parents.
258-
// The larger size was added in revision 01.53."
259-
// The TSS stack we use only accepts the smaller of the two sizes described above (for now).
260294
buffer_type!(
261295
PrivateKeyRsa,
262-
(TPM2_MAX_RSA_KEY_BYTES / 2) as usize,
296+
TPM2B_PRIVATE_KEY_RSA_BUFFER_SIZE,
263297
TPM2B_PRIVATE_KEY_RSA
264298
);
265299
}
266300

267301
pub mod private_vendor_specific {
268302
use crate::tss2_esys::TPM2_PRIVATE_VENDOR_SPECIFIC_BYTES;
269-
303+
const TPM2B_PRIVATE_VENDOR_SPECIFIC_BUFFER_SIZE: usize =
304+
TPM2_PRIVATE_VENDOR_SPECIFIC_BYTES as usize;
270305
// The spec states the maximum size as:
271306
// "The value for PRIVATE_VENDOR_SPECIFIC_BYTES is determined by the vendor."
272307
// Not very helpful, but the TSS exposes a generic value that we can use.
273308
buffer_type!(
274309
PrivateVendorSpecific,
275-
TPM2_PRIVATE_VENDOR_SPECIFIC_BYTES as usize,
310+
TPM2B_PRIVATE_VENDOR_SPECIFIC_BUFFER_SIZE,
276311
TPM2B_PRIVATE_VENDOR_SPECIFIC
277312
);
278313
}
279314

280315
pub mod public_key_rsa {
281316
use crate::{interface_types::key_bits::RsaKeyBits, tss2_esys::TPM2_MAX_RSA_KEY_BYTES};
317+
const TPM2B_PUBLIC_KEY_RSA_BUFFER_SIZE: usize = TPM2_MAX_RSA_KEY_BYTES as usize;
282318
buffer_type!(
283319
PublicKeyRsa,
284-
TPM2_MAX_RSA_KEY_BYTES as usize,
320+
TPM2B_PUBLIC_KEY_RSA_BUFFER_SIZE,
285321
TPM2B_PUBLIC_KEY_RSA
286322
);
287323

@@ -359,45 +395,47 @@ pub mod sensitive_data {
359395
// versions of tpm2-tss supported by the crate so the fall back is to
360396
// calculate the max size by removing the size of the size parameter(UINT16)
361397
// from the total size of the buffer type.
398+
use std::mem::size_of;
362399
cfg_if::cfg_if! {
363400
if #[cfg(has_tpmu_sensitive_create)] {
364401
use crate::tss2_esys::TPMU_SENSITIVE_CREATE;
365-
#[allow(unused_qualifications)]
366-
const TPMU_SENSITIVE_CREATE_MEM_SIZE: usize = std::mem::size_of::<TPMU_SENSITIVE_CREATE>();
402+
const TPM2B_SENSITIVE_DATA_BUFFER_SIZE: usize = size_of::<TPMU_SENSITIVE_CREATE>();
367403
} else {
368404
use crate::tss2_esys::UINT16;
369-
#[allow(unused_qualifications)]
370-
const TPMU_SENSITIVE_CREATE_MEM_SIZE: usize = std::mem::size_of::<TPM2B_SENSITIVE_DATA>() - std::mem::size_of::<UINT16>();
405+
const TPM2B_SENSITIVE_DATA_BUFFER_SIZE: usize = size_of::<TPM2B_SENSITIVE_DATA>() - size_of::<UINT16>();
371406
}
372407
}
373408
buffer_type!(
374409
SensitiveData,
375-
TPMU_SENSITIVE_CREATE_MEM_SIZE,
410+
TPM2B_SENSITIVE_DATA_BUFFER_SIZE,
376411
TPM2B_SENSITIVE_DATA
377412
);
378413
}
379414

380415
pub mod symmetric_key {
381416
use crate::tss2_esys::TPM2_MAX_SYM_KEY_BYTES;
382-
417+
const TPM2B_SYM_KEY_BUFFER_SIZE: usize = TPM2_MAX_SYM_KEY_BYTES as usize;
383418
// The spec states the maximum size as:
384419
// "MAX_SYM_KEY_BYTES will be the larger of the largest symmetric key supported by the TPM and the
385420
// largest digest produced by any hashing algorithm implemented on the TPM"
386-
buffer_type!(SymmetricKey, TPM2_MAX_SYM_KEY_BYTES as usize, TPM2B_SYM_KEY);
421+
buffer_type!(SymmetricKey, TPM2B_SYM_KEY_BUFFER_SIZE, TPM2B_SYM_KEY);
387422
}
388423

389424
pub mod timeout {
390-
buffer_type!(Timeout, 8, TPM2B_TIMEOUT);
425+
use crate::tss2_esys::UINT64;
426+
use std::mem::size_of;
427+
const TPM2B_TIMEOUT_BUFFER_SIZE: usize = size_of::<UINT64>();
428+
buffer_type!(Timeout, TPM2B_TIMEOUT_BUFFER_SIZE, TPM2B_TIMEOUT);
391429
}
392430

393431
pub mod tpm_context_data {
394432
use crate::tss2_esys::TPMS_CONTEXT_DATA;
433+
use std::mem::size_of;
395434

396-
#[allow(unused_qualifications)]
397-
const TPMS_CONTEXT_DATA_MEM_SIZE: usize = std::mem::size_of::<TPMS_CONTEXT_DATA>();
435+
const TPM2B_CONTEXT_DATA_BUFFER_SIZE: usize = size_of::<TPMS_CONTEXT_DATA>();
398436
buffer_type!(
399437
TpmContextData,
400-
TPMS_CONTEXT_DATA_MEM_SIZE,
438+
TPM2B_CONTEXT_DATA_BUFFER_SIZE,
401439
TPM2B_CONTEXT_DATA
402440
);
403441
}

tss-esapi/src/structures/buffers/private.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::traits::impl_mu_standard;
5+
use std::mem::size_of;
56
use tss_esapi_sys::_PRIVATE;
67

7-
buffer_type!(Private, ::std::mem::size_of::<_PRIVATE>(), TPM2B_PRIVATE);
8+
const TPM2B_PRIVATE_BUFFER_SIZE: usize = size_of::<_PRIVATE>();
9+
10+
buffer_type!(Private, TPM2B_PRIVATE_BUFFER_SIZE, TPM2B_PRIVATE);
811

912
impl_mu_standard!(Private, TPM2B_PRIVATE);
1013

tss-esapi/src/structures/buffers/public.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
use log::error;
1111
use std::{
1212
convert::{TryFrom, TryInto},
13+
mem::size_of,
1314
ops::Deref,
1415
};
1516
use zeroize::{Zeroize, ZeroizeOnDrop};
@@ -24,8 +25,7 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
2425
pub struct PublicBuffer(Vec<u8>);
2526

2627
impl PublicBuffer {
27-
#[allow(unused_qualifications)]
28-
pub const MAX_SIZE: usize = std::mem::size_of::<TPMT_PUBLIC>();
28+
pub const MAX_SIZE: usize = size_of::<TPMT_PUBLIC>();
2929

3030
pub fn value(&self) -> &[u8] {
3131
&self.0

tss-esapi/src/structures/buffers/sensitive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
use log::error;
1010
use std::{
1111
convert::{TryFrom, TryInto},
12+
mem::size_of,
1213
ops::Deref,
1314
};
1415
use zeroize::{Zeroize, ZeroizeOnDrop};
@@ -23,8 +24,7 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
2324
pub struct SensitiveBuffer(Vec<u8>);
2425

2526
impl SensitiveBuffer {
26-
#[allow(unused_qualifications)]
27-
pub const MAX_SIZE: usize = std::mem::size_of::<TPMT_SENSITIVE>();
27+
pub const MAX_SIZE: usize = size_of::<TPMT_SENSITIVE>();
2828

2929
pub fn value(&self) -> &[u8] {
3030
&self.0

tss-esapi/src/structures/buffers/sensitive_create.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
use log::error;
1010
use std::{
1111
convert::{TryFrom, TryInto},
12+
mem::size_of,
1213
ops::Deref,
1314
};
1415
use zeroize::{Zeroize, ZeroizeOnDrop};
@@ -23,8 +24,7 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
2324
pub struct SensitiveCreateBuffer(Vec<u8>);
2425

2526
impl SensitiveCreateBuffer {
26-
#[allow(unused_qualifications)]
27-
pub const MAX_SIZE: usize = std::mem::size_of::<TPMS_SENSITIVE_CREATE>();
27+
pub const MAX_SIZE: usize = size_of::<TPMS_SENSITIVE_CREATE>();
2828
pub const MIN_SIZE: usize = 4;
2929

3030
/// Returns the content of the buffer.

tss-esapi/src/structures/ecc/point.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use tss_esapi_sys::TPM2B_ECC_POINT;
33
// Copyright 2021 Contributors to the Parsec project.
44
// SPDX-License-Identifier: Apache-2.0
55
use crate::{structures::EccParameter, tss2_esys::TPMS_ECC_POINT, Error, Result};
6-
use std::convert::{TryFrom, TryInto};
6+
use std::{
7+
convert::{TryFrom, TryInto},
8+
mem::size_of,
9+
};
710

811
/// Structure holding ecc point information
912
///
@@ -49,11 +52,7 @@ impl From<EccPoint> for TPMS_ECC_POINT {
4952

5053
impl From<EccPoint> for TPM2B_ECC_POINT {
5154
fn from(ecc_point: EccPoint) -> Self {
52-
#[allow(unused_qualifications)]
53-
let size = std::mem::size_of::<u16>()
54-
+ ecc_point.x().len()
55-
+ std::mem::size_of::<u16>()
56-
+ ecc_point.y().len();
55+
let size = size_of::<u16>() + ecc_point.x().len() + size_of::<u16>() + ecc_point.y().len();
5756
TPM2B_ECC_POINT {
5857
size: size as u16,
5958
point: ecc_point.into(),

tss-esapi/src/structures/nv/storage/public.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use crate::{
1010
Error, Result, WrapperErrorKind,
1111
};
1212
use log::error;
13-
use std::convert::{TryFrom, TryInto};
13+
use std::{
14+
convert::{TryFrom, TryInto},
15+
mem::size_of,
16+
};
1417

1518
/// Representation of the public parameters of a non-volatile
1619
/// space allocation.
@@ -27,8 +30,7 @@ pub struct NvPublic {
2730
}
2831

2932
impl NvPublic {
30-
#[allow(unused_qualifications)]
31-
const MAX_SIZE: usize = std::mem::size_of::<TPMS_NV_PUBLIC>();
33+
const MAX_SIZE: usize = size_of::<TPMS_NV_PUBLIC>();
3234

3335
pub fn nv_index(&self) -> NvIndexTpmHandle {
3436
self.nv_index

tss-esapi/src/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ macro_rules! impl_marshall_trait {
4949
($native_type:ident, $tss_type:ident, $tss_mu_type:ident, $convert_expression:stmt, $( $ref_sign:tt )?) => {
5050
paste::item! {
5151
impl $crate::traits::Marshall for $native_type {
52-
const BUFFER_SIZE: usize = std::mem::size_of::<$tss_type>();
52+
const BUFFER_SIZE: usize = ::std::mem::size_of::<$tss_type>();
5353

5454
fn marshall_offset(
5555
&self,

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,15 @@ fn test_default() {
5151
assert_eq!(expected, actual);
5252
}
5353
}
54+
55+
#[test]
56+
fn test_max_sized_attest_buffer_conversions() {
57+
let expected_attestation_data = [0xffu8; AttestBuffer::MAX_SIZE];
58+
let native = AttestBuffer::try_from(expected_attestation_data.as_slice().to_vec()).expect(
59+
"It should be possible to convert an array of MAX size into a AttestBuffer object.",
60+
);
61+
let tss = TPM2B_ATTEST::from(native);
62+
assert_eq!(AttestBuffer::MAX_SIZE, tss.size as usize);
63+
// This will be a compiler error if the max size does not match the TSS buffer size.
64+
assert_eq!(expected_attestation_data, tss.attestationData);
65+
}

0 commit comments

Comments
 (0)