Skip to content

Commit

Permalink
Fixes issue wrong max size in some sized buffer types.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Superhepper committed Sep 25, 2024
1 parent 97ccc11 commit 65258c9
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 200 deletions.
88 changes: 62 additions & 26 deletions tss-esapi/src/structures/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,30 @@ pub mod sensitive;
pub mod sensitive_create;

pub mod auth {
buffer_type!(Auth, 64, TPM2B_AUTH);
// Same size as TPM2B_DIGEST according to the specification.
use crate::tss2_esys::TPMU_HA;
use std::mem::size_of;
const TPM2B_AUTH_BUFFER_SIZE: usize = size_of::<TPMU_HA>();
buffer_type!(Auth, TPM2B_AUTH_BUFFER_SIZE, TPM2B_AUTH);
}

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

pub mod digest {
buffer_type!(Digest, 64, TPM2B_DIGEST);
use crate::tss2_esys::TPMU_HA;
use std::mem::size_of;
const TPM2B_DIGEST_BUFFER_SIZE: usize = size_of::<TPMU_HA>();

buffer_type!(Digest, TPM2B_DIGEST_BUFFER_SIZE, TPM2B_DIGEST);

// Some implementations to get from Digest to [u8; N] for common values of N (sha* primarily)
// This is used to work around the fact that Rust does not allow custom functions for general values of N in [T; N],
Expand Down Expand Up @@ -216,11 +231,27 @@ pub mod ecc_parameter {
}

pub mod encrypted_secret {
named_field_buffer_type!(EncryptedSecret, 256, TPM2B_ENCRYPTED_SECRET, secret);
use crate::tss2_esys::TPMU_ENCRYPTED_SECRET;
use std::mem::size_of;
const TPMU_ENCRYPTED_SECRET_MEM_SIZE: usize = size_of::<TPMU_ENCRYPTED_SECRET>();
named_field_buffer_type!(
EncryptedSecret,
TPMU_ENCRYPTED_SECRET_MEM_SIZE,
TPM2B_ENCRYPTED_SECRET,
secret
);
}

pub mod id_object {
named_field_buffer_type!(IdObject, 256, TPM2B_ID_OBJECT, credential);
use crate::tss2_esys::TPMS_ID_OBJECT;
use std::mem::size_of;
const TPMS_ID_OBJECT_MEM_SIZE: usize = size_of::<TPMS_ID_OBJECT>();
named_field_buffer_type!(
IdObject,
TPMS_ID_OBJECT_MEM_SIZE,
TPM2B_ID_OBJECT,
credential
);
}

pub mod initial_value {
Expand All @@ -246,42 +277,45 @@ pub mod max_nv_buffer {
}

pub mod nonce {
buffer_type!(Nonce, 64, TPM2B_NONCE);
// Same size as TPM2B_DIGEST according to the specification.
use crate::tss2_esys::TPMU_HA;
use std::mem::size_of;
const TPM2B_NONCE_BUFFER_SIZE: usize = size_of::<TPMU_HA>();

buffer_type!(Nonce, TPM2B_NONCE_BUFFER_SIZE, TPM2B_NONCE);
}

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

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

pub mod private_vendor_specific {
use crate::tss2_esys::TPM2_PRIVATE_VENDOR_SPECIFIC_BYTES;

const TPM2B_PRIVATE_VENDOR_SPECIFIC_BUFFER_SIZE: usize =
TPM2_PRIVATE_VENDOR_SPECIFIC_BYTES as usize;
// The spec states the maximum size as:
// "The value for PRIVATE_VENDOR_SPECIFIC_BYTES is determined by the vendor."
// Not very helpful, but the TSS exposes a generic value that we can use.
buffer_type!(
PrivateVendorSpecific,
TPM2_PRIVATE_VENDOR_SPECIFIC_BYTES as usize,
TPM2B_PRIVATE_VENDOR_SPECIFIC_BUFFER_SIZE,
TPM2B_PRIVATE_VENDOR_SPECIFIC
);
}

pub mod public_key_rsa {
use crate::{interface_types::key_bits::RsaKeyBits, tss2_esys::TPM2_MAX_RSA_KEY_BYTES};
const TPM2B_PUBLIC_KEY_RSA_BUFFER_SIZE: usize = TPM2_MAX_RSA_KEY_BYTES as usize;
buffer_type!(
PublicKeyRsa,
TPM2_MAX_RSA_KEY_BYTES as usize,
TPM2B_PUBLIC_KEY_RSA_BUFFER_SIZE,
TPM2B_PUBLIC_KEY_RSA
);

Expand Down Expand Up @@ -359,45 +393,47 @@ pub mod sensitive_data {
// versions of tpm2-tss supported by the crate so the fall back is to
// calculate the max size by removing the size of the size parameter(UINT16)
// from the total size of the buffer type.
use std::mem::size_of;
cfg_if::cfg_if! {
if #[cfg(has_tpmu_sensitive_create)] {
use crate::tss2_esys::TPMU_SENSITIVE_CREATE;
#[allow(unused_qualifications)]
const TPMU_SENSITIVE_CREATE_MEM_SIZE: usize = std::mem::size_of::<TPMU_SENSITIVE_CREATE>();
const TPM2B_SENSITIVE_DATA_BUFFER_SIZE: usize = size_of::<TPMU_SENSITIVE_CREATE>();
} else {
use crate::tss2_esys::UINT16;
#[allow(unused_qualifications)]
const TPMU_SENSITIVE_CREATE_MEM_SIZE: usize = std::mem::size_of::<TPM2B_SENSITIVE_DATA>() - std::mem::size_of::<UINT16>();
const TPM2B_SENSITIVE_DATA_BUFFER_SIZE: usize = size_of::<TPM2B_SENSITIVE_DATA>() - size_of::<UINT16>();
}
}
buffer_type!(
SensitiveData,
TPMU_SENSITIVE_CREATE_MEM_SIZE,
TPM2B_SENSITIVE_DATA_BUFFER_SIZE,
TPM2B_SENSITIVE_DATA
);
}

pub mod symmetric_key {
use crate::tss2_esys::TPM2_MAX_SYM_KEY_BYTES;

const TPM2B_SYM_KEY_BUFFER_SIZE: usize = TPM2_MAX_SYM_KEY_BYTES as usize;
// The spec states the maximum size as:
// "MAX_SYM_KEY_BYTES will be the larger of the largest symmetric key supported by the TPM and the
// largest digest produced by any hashing algorithm implemented on the TPM"
buffer_type!(SymmetricKey, TPM2_MAX_SYM_KEY_BYTES as usize, TPM2B_SYM_KEY);
buffer_type!(SymmetricKey, TPM2B_SYM_KEY_BUFFER_SIZE, TPM2B_SYM_KEY);
}

pub mod timeout {
buffer_type!(Timeout, 8, TPM2B_TIMEOUT);
use crate::tss2_esys::UINT64;
use std::mem::size_of;
const TPM2B_TIMEOUT_BUFFER_SIZE: usize = size_of::<UINT64>();
buffer_type!(Timeout, TPM2B_TIMEOUT_BUFFER_SIZE, TPM2B_TIMEOUT);
}

pub mod tpm_context_data {
use crate::tss2_esys::TPMS_CONTEXT_DATA;
use std::mem::size_of;

#[allow(unused_qualifications)]
const TPMS_CONTEXT_DATA_MEM_SIZE: usize = std::mem::size_of::<TPMS_CONTEXT_DATA>();
const TPM2B_CONTEXT_DATA_BUFFER_SIZE: usize = size_of::<TPMS_CONTEXT_DATA>();
buffer_type!(
TpmContextData,
TPMS_CONTEXT_DATA_MEM_SIZE,
TPM2B_CONTEXT_DATA_BUFFER_SIZE,
TPM2B_CONTEXT_DATA
);
}
4 changes: 2 additions & 2 deletions tss-esapi/src/structures/buffers/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
use log::error;
use std::{
convert::{TryFrom, TryInto},
mem::size_of,
ops::Deref,
};
use zeroize::{Zeroize, ZeroizeOnDrop};
Expand All @@ -24,8 +25,7 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
pub struct PublicBuffer(Vec<u8>);

impl PublicBuffer {
#[allow(unused_qualifications)]
pub const MAX_SIZE: usize = std::mem::size_of::<TPMT_PUBLIC>();
pub const MAX_SIZE: usize = size_of::<TPMT_PUBLIC>();

pub fn value(&self) -> &[u8] {
&self.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,17 @@ fn test_default() {
assert_eq!(expected, actual);
}
}

#[test]
fn test_max_sized_attest_buffer_conversions() {
let expected_attestation_data = [0xffu8; AttestBuffer::MAX_SIZE];
let native = AttestBuffer::try_from(expected_attestation_data.as_slice().to_vec()).expect(
"It should be possible to convert an array of MAX size into a AttestBuffer object.",
);
let tss = TPM2B_ATTEST::try_from(native).expect(
"It should be possible to convert a valid AttestBuffer object into a TPM2B_ATTEST.",
);
assert_eq!(AttestBuffer::MAX_SIZE, tss.size as usize);
// This will be a compiler error if the max size does not match the TSS buffer size.
assert_eq!(expected_attestation_data, tss.attestationData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,60 @@ use tss_esapi::tss2_esys::TPM2B_AUTH;
// in it being just a type alias for TPM2B_DIGEST
// in the rust code. So the same size restrictions that
// TPM2B_DIGEST have will apply here as well.
mod test_auth {
use super::*;

#[test]
fn test_max_sized_data() {
let _ = Auth::try_from([0xff; 64].to_vec()).unwrap();
}
#[test]
fn test_max_sized_data() {
let _ = Auth::try_from([0xff; 64].to_vec()).unwrap();
}

#[test]
fn test_to_large_data() {
// Removed:
// - test_handle_auth::test_set_large_handle
// - test_create::test_long_auth_create
// - test_create_primary::test_long_auth_create_primary
// from the context tests and put here instead.
#[test]
fn test_to_large_data() {
// Removed:
// - test_handle_auth::test_set_large_handle
// - test_create::test_long_auth_create
// - test_create_primary::test_long_auth_create_primary
// from the context tests and put here instead.

let _ = Auth::try_from([0xff; 100].to_vec()).unwrap_err();
}
let _ = Auth::try_from([0xff; 100].to_vec()).unwrap_err();
}

#[test]
fn test_default() {
{
let auth: Auth = Default::default();
let expected: TPM2B_AUTH = Default::default();
let actual = TPM2B_AUTH::from(auth);
assert_eq!(expected.size, actual.size);
assert_eq!(
expected.buffer.len(),
actual.buffer.len(),
"Buffers don't have the same length"
);
assert!(
expected
.buffer
.iter()
.zip(actual.buffer.iter())
.all(|(a, b)| a == b),
"Buffers are not equal"
);
}
{
let tss_auth: TPM2B_AUTH = Default::default();
let expected: Auth = Default::default();
let actual = Auth::try_from(tss_auth).unwrap();
assert_eq!(expected, actual);
}
#[test]
fn test_default() {
{
let auth: Auth = Default::default();
let expected: TPM2B_AUTH = Default::default();
let actual = TPM2B_AUTH::from(auth);
assert_eq!(expected.size, actual.size);
assert_eq!(
expected.buffer.len(),
actual.buffer.len(),
"Buffers don't have the same length"
);
assert!(
expected
.buffer
.iter()
.zip(actual.buffer.iter())
.all(|(a, b)| a == b),
"Buffers are not equal"
);
}
{
let tss_auth: TPM2B_AUTH = Default::default();
let expected: Auth = Default::default();
let actual = Auth::try_from(tss_auth).unwrap();
assert_eq!(expected, actual);
}
}

#[test]
fn test_max_sized_auth_conversions() {
let expected_buffer = [0xffu8; Auth::MAX_SIZE];
let native = Auth::try_from(expected_buffer.as_slice().to_vec())
.expect("It should be possible to convert an array of MAX size into a Auth object.");
let tss = TPM2B_AUTH::try_from(native)
.expect("It should be possible to convert a valid Auth object into a TPM2B_AUTH.");
assert_eq!(Auth::MAX_SIZE, tss.size as usize);
// This will be a compiler error if the max size does not match the TSS buffer size.
assert_eq!(expected_buffer, tss.buffer);
}
Loading

0 comments on commit 65258c9

Please sign in to comment.