Skip to content

Commit 96f6855

Browse files
authored
Merge pull request #550 from Superhepper/7.x.y-buffer-max-size-bug
Fixes #548 for 7.x.y branch.
2 parents e9a94a7 + f81e38c commit 96f6855

File tree

1 file changed

+75
-23
lines changed

1 file changed

+75
-23
lines changed

tss-esapi/src/structures/buffers.rs

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,30 @@ pub mod public;
9797
pub mod sensitive;
9898

9999
pub mod auth {
100-
buffer_type!(Auth, 64, TPM2B_AUTH);
100+
// Same size as TPM2B_DIGEST according to the specification.
101+
use crate::tss2_esys::TPMU_HA;
102+
use std::mem::size_of;
103+
const TPM2B_AUTH_BUFFER_SIZE: usize = size_of::<TPMU_HA>();
104+
buffer_type!(Auth, TPM2B_AUTH_BUFFER_SIZE, TPM2B_AUTH);
101105
}
102106

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

107118
pub mod digest {
108-
buffer_type!(Digest, 64, TPM2B_DIGEST);
119+
use crate::tss2_esys::TPMU_HA;
120+
use std::mem::size_of;
121+
const TPM2B_DIGEST_BUFFER_SIZE: usize = size_of::<TPMU_HA>();
122+
123+
buffer_type!(Digest, TPM2B_DIGEST_BUFFER_SIZE, TPM2B_DIGEST);
109124

110125
// Some implementations to get from Digest to [u8; N] for common values of N (sha* primarily)
111126
// This is used to work around the fact that Rust does not allow custom functions for general values of N in [T; N],
@@ -170,50 +185,75 @@ pub mod digest {
170185
}
171186

172187
pub mod ecc_parameter {
188+
use crate::tss2_esys::TPM2_MAX_ECC_KEY_BYTES;
189+
const TPM2B_ECC_PARAMETER_BUFFER_SIZE: usize = TPM2_MAX_ECC_KEY_BYTES as usize;
173190
buffer_type!(
174191
EccParameter,
175-
crate::tss2_esys::TPM2_MAX_ECC_KEY_BYTES as usize,
192+
TPM2B_ECC_PARAMETER_BUFFER_SIZE,
176193
TPM2B_ECC_PARAMETER
177194
);
178195
}
179196

180197
pub mod encrypted_secret {
181-
named_field_buffer_type!(EncryptedSecret, 256, TPM2B_ENCRYPTED_SECRET, secret);
198+
use crate::tss2_esys::TPMU_ENCRYPTED_SECRET;
199+
use std::mem::size_of;
200+
const TPM2B_ENCRYPTED_SECRET_BUFFER_SIZE: usize = size_of::<TPMU_ENCRYPTED_SECRET>();
201+
named_field_buffer_type!(
202+
EncryptedSecret,
203+
TPM2B_ENCRYPTED_SECRET_BUFFER_SIZE,
204+
TPM2B_ENCRYPTED_SECRET,
205+
secret
206+
);
182207
}
183208

184209
pub mod id_object {
185-
named_field_buffer_type!(IdObject, 256, TPM2B_ID_OBJECT, credential);
210+
use crate::tss2_esys::TPMS_ID_OBJECT;
211+
use std::mem::size_of;
212+
const TPM2B_ID_OBJECT_BUFFER_SIZE: usize = size_of::<TPMS_ID_OBJECT>();
213+
named_field_buffer_type!(
214+
IdObject,
215+
TPM2B_ID_OBJECT_BUFFER_SIZE,
216+
TPM2B_ID_OBJECT,
217+
credential
218+
);
186219
}
187220

188221
pub mod initial_value {
189-
buffer_type!(
190-
InitialValue,
191-
crate::tss2_esys::TPM2_MAX_SYM_BLOCK_SIZE as usize,
192-
TPM2B_IV
193-
);
222+
use crate::tss2_esys::TPM2_MAX_SYM_BLOCK_SIZE;
223+
const TPM2B_IV_BUFFER_SIZE: usize = TPM2_MAX_SYM_BLOCK_SIZE as usize;
224+
buffer_type!(InitialValue, TPM2B_IV_BUFFER_SIZE, TPM2B_IV);
194225
}
195226

196227
pub mod max_buffer {
197228
use crate::tss2_esys::TPM2_MAX_DIGEST_BUFFER;
198-
buffer_type!(MaxBuffer, TPM2_MAX_DIGEST_BUFFER as usize, TPM2B_MAX_BUFFER);
229+
const TPM2B_MAX_BUFFER_BUFFER_SIZE: usize = TPM2_MAX_DIGEST_BUFFER as usize;
230+
buffer_type!(MaxBuffer, TPM2B_MAX_BUFFER_BUFFER_SIZE, TPM2B_MAX_BUFFER);
199231
}
200232

201233
pub mod max_nv_buffer {
202234
use crate::tss2_esys::TPM2_MAX_NV_BUFFER_SIZE;
235+
const TPM2B_MAX_NV_BUFFER_BUFFER_SIZE: usize = TPM2_MAX_NV_BUFFER_SIZE as usize;
203236
buffer_type!(
204237
MaxNvBuffer,
205-
TPM2_MAX_NV_BUFFER_SIZE as usize,
238+
TPM2B_MAX_NV_BUFFER_BUFFER_SIZE,
206239
TPM2B_MAX_NV_BUFFER
207240
);
208241
}
209242

210243
pub mod nonce {
211-
buffer_type!(Nonce, 64, TPM2B_NONCE);
244+
// Same size as TPM2B_DIGEST according to the specification.
245+
use crate::tss2_esys::TPMU_HA;
246+
use std::mem::size_of;
247+
const TPM2B_NONCE_BUFFER_SIZE: usize = size_of::<TPMU_HA>();
248+
249+
buffer_type!(Nonce, TPM2B_NONCE_BUFFER_SIZE, TPM2B_NONCE);
212250
}
213251

214252
pub mod private {
253+
use std::mem::size_of;
215254
use tss_esapi_sys::_PRIVATE;
216-
buffer_type!(Private, ::std::mem::size_of::<_PRIVATE>(), TPM2B_PRIVATE);
255+
const TPM2B_PRIVATE_BUFFER_SIZE: usize = size_of::<_PRIVATE>();
256+
buffer_type!(Private, TPM2B_PRIVATE_BUFFER_SIZE, TPM2B_PRIVATE);
217257
}
218258

219259
pub mod private_key_rsa {
@@ -224,31 +264,34 @@ pub mod private_key_rsa {
224264
// ((MAX_RSA_KEY_BYTES * 5) ./ 2. The larger size would only apply to keys that have fixedTPM parents.
225265
// The larger size was added in revision 01.53."
226266
// The TSS stack we use only accepts the smaller of the two sizes described above (for now).
267+
const TPM2B_PRIVATE_KEY_RSA_BUFFER_SIZE: usize = (TPM2_MAX_RSA_KEY_BYTES as usize) / 2;
227268
buffer_type!(
228269
PrivateKeyRsa,
229-
(TPM2_MAX_RSA_KEY_BYTES / 2) as usize,
270+
TPM2B_PRIVATE_KEY_RSA_BUFFER_SIZE,
230271
TPM2B_PRIVATE_KEY_RSA
231272
);
232273
}
233274

234275
pub mod private_vendor_specific {
235276
use crate::tss2_esys::TPM2_PRIVATE_VENDOR_SPECIFIC_BYTES;
236-
277+
const TPM2B_PRIVATE_VENDOR_SPECIFIC_BUFFER_SIZE: usize =
278+
TPM2_PRIVATE_VENDOR_SPECIFIC_BYTES as usize;
237279
// The spec states the maximum size as:
238280
// "The value for PRIVATE_VENDOR_SPECIFIC_BYTES is determined by the vendor."
239281
// Not very helpful, but the TSS exposes a generic value that we can use.
240282
buffer_type!(
241283
PrivateVendorSpecific,
242-
TPM2_PRIVATE_VENDOR_SPECIFIC_BYTES as usize,
284+
TPM2B_PRIVATE_VENDOR_SPECIFIC_BUFFER_SIZE,
243285
TPM2B_PRIVATE_VENDOR_SPECIFIC
244286
);
245287
}
246288

247289
pub mod public_key_rsa {
248290
use crate::{interface_types::key_bits::RsaKeyBits, tss2_esys::TPM2_MAX_RSA_KEY_BYTES};
291+
const TPM2B_PUBLIC_KEY_RSA_BUFFER_SIZE: usize = TPM2_MAX_RSA_KEY_BYTES as usize;
249292
buffer_type!(
250293
PublicKeyRsa,
251-
TPM2_MAX_RSA_KEY_BYTES as usize,
294+
TPM2B_PUBLIC_KEY_RSA_BUFFER_SIZE,
252295
TPM2B_PUBLIC_KEY_RSA
253296
);
254297

@@ -321,22 +364,31 @@ pub mod public_key_rsa {
321364
}
322365

323366
pub mod sensitive_data {
367+
// This should be size_of::<TPMU_SENSITIVE_CREATE>(), but this not available
368+
// in old versions of tpm2-tss so the size calculated from sized buffer instead.
369+
use crate::tss2_esys::UINT16;
370+
use std::mem::size_of;
371+
const TPM2B_SENSITIVE_DATA_BUFFER_SIZE: usize =
372+
size_of::<TPM2B_SENSITIVE_DATA>() - size_of::<UINT16>();
324373
buffer_type!(
325374
SensitiveData,
326-
::std::mem::size_of::<TPM2B_SENSITIVE_DATA>(),
375+
TPM2B_SENSITIVE_DATA_BUFFER_SIZE,
327376
TPM2B_SENSITIVE_DATA
328377
);
329378
}
330379

331380
pub mod symmetric_key {
332381
use crate::tss2_esys::TPM2_MAX_SYM_KEY_BYTES;
333-
382+
const TPM2B_SYM_KEY_BUFFER_SIZE: usize = TPM2_MAX_SYM_KEY_BYTES as usize;
334383
// The spec states the maximum size as:
335384
// "MAX_SYM_KEY_BYTES will be the larger of the largest symmetric key supported by the TPM and the
336385
// largest digest produced by any hashing algorithm implemented on the TPM"
337-
buffer_type!(SymmetricKey, TPM2_MAX_SYM_KEY_BYTES as usize, TPM2B_SYM_KEY);
386+
buffer_type!(SymmetricKey, TPM2B_SYM_KEY_BUFFER_SIZE, TPM2B_SYM_KEY);
338387
}
339388

340389
pub mod timeout {
341-
buffer_type!(Timeout, 8, TPM2B_TIMEOUT);
390+
use crate::tss2_esys::UINT64;
391+
use std::mem::size_of;
392+
const TPM2B_TIMEOUT_BUFFER_SIZE: usize = size_of::<UINT64>();
393+
buffer_type!(Timeout, TPM2B_TIMEOUT_BUFFER_SIZE, TPM2B_TIMEOUT);
342394
}

0 commit comments

Comments
 (0)