Skip to content

Commit 2e21e62

Browse files
committed
Changes conversion from slice for buffer types.
- Removes implementation of 'TryFrom<&[u8]>' for buffer types and replaces this with a new constructor function 'from_bytes'. - Removes the api 'value' for buffer types and replaces this with the api 'as_bytes'. Signed-off-by: Jesper Brynolf <[email protected]>
1 parent 87b2977 commit 2e21e62

26 files changed

+119
-128
lines changed

tss-esapi/src/abstraction/nv.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ impl std::io::Write for NvReaderWriter<'_> {
213213
let desired_size = std::cmp::min(buf.len(), self.data_size - self.offset);
214214
let size = std::cmp::min(self.buffer_size, desired_size) as u16;
215215

216-
let data = buf[0..size.into()]
217-
.try_into()
216+
let data = MaxNvBuffer::from_bytes(&buf[0..size.into()])
218217
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
219218
self.context
220219
.nv_write(self.auth_handle, self.nv_idx, data, self.offset as u16)

tss-esapi/src/abstraction/pcr/data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl From<PcrData> for Vec<TPML_DIGEST> {
125125
tpml_digest.count += 1;
126126
tpml_digest.digests[index].size = digest.len() as u16;
127127
tpml_digest.digests[index].buffer[..digest.len()]
128-
.copy_from_slice(digest.value());
128+
.copy_from_slice(digest.as_bytes());
129129
}
130130
tpml_digest
131131
})

tss-esapi/src/abstraction/public.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ fn public_to_decoded_key(public: &Public) -> Result<DecodedKey, Error> {
8484
}
8585
.to_be_bytes();
8686
Ok(DecodedKey::RsaPublicKey(RsaPublicKey {
87-
modulus: IntegerAsn1::from_bytes_be_unsigned(unique.value().to_vec()),
87+
modulus: IntegerAsn1::from_bytes_be_unsigned(unique.as_bytes().to_vec()),
8888
public_exponent: IntegerAsn1::from_bytes_be_signed(exponent.to_vec()),
8989
}))
9090
}
9191
Public::Ecc { unique, .. } => {
92-
let x = unique.x().value().to_vec();
93-
let y = unique.y().value().to_vec();
92+
let x = unique.x().as_bytes().to_vec();
93+
let y = unique.y().as_bytes().to_vec();
9494
Ok(DecodedKey::EcPoint(OctetStringAsn1(
9595
elliptic_curve_point_to_octet_string(x, y),
9696
)))

tss-esapi/src/abstraction/transient/key_attestation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl TransientKeyContext {
5353
/// the credential
5454
///
5555
/// **Note**: If no `key` is given, the default Endorsement Key
56-
/// will be used.
56+
/// will be used.
5757
pub fn get_make_cred_params(
5858
&mut self,
5959
object: ObjectWrapper,
@@ -131,7 +131,7 @@ impl TransientKeyContext {
131131
self.context.flush_context(key_handle.into())?;
132132
self.context
133133
.flush_context(SessionHandle::from(session_2).into())?;
134-
Ok(credential.value().to_vec())
134+
Ok(credential.as_bytes().to_vec())
135135
}
136136

137137
// No key was given, use the EK. This requires using a Policy session

tss-esapi/src/abstraction/transient/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl TransientKeyContext {
149149
let key_auth = if auth_size > 0 {
150150
self.set_session_attrs()?;
151151
let random_bytes = self.context.get_random(auth_size)?;
152-
Some(Auth::try_from(random_bytes.value().to_vec())?)
152+
Some(Auth::from_bytes(random_bytes.as_bytes())?)
153153
} else {
154154
None
155155
};
@@ -170,7 +170,7 @@ impl TransientKeyContext {
170170

171171
let key_material = KeyMaterial {
172172
public: out_public.try_into()?,
173-
private: out_private.value().to_vec(),
173+
private: out_private.as_bytes().to_vec(),
174174
};
175175
Ok((key_material, key_auth))
176176
}
@@ -390,7 +390,7 @@ impl TransientKeyContext {
390390

391391
let key_material = KeyMaterial {
392392
public: public.try_into()?,
393-
private: private.value().to_vec(),
393+
private: private.as_bytes().to_vec(),
394394
};
395395

396396
self.context.flush_context(key_handle.into())?;
@@ -670,7 +670,7 @@ impl TransientKeyContextBuilder {
670670

671671
let root_key_auth = if self.root_key_auth_size > 0 {
672672
let random = context.get_random(self.root_key_auth_size)?;
673-
Some(Auth::try_from(random.value().to_vec())?)
673+
Some(Auth::from_bytes(random.as_bytes())?)
674674
} else {
675675
None
676676
};

tss-esapi/src/context/tpm_commands/asymmetric_primitives.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl Context {
128128
/// # .expect("Failed to set attributes on session");
129129
/// # context.set_sessions((Some(session), None, None));
130130
/// # let random_digest = context.get_random(16).unwrap();
131-
/// # let key_auth = Auth::try_from(random_digest.value().to_vec()).unwrap();
131+
/// # let key_auth = Auth::from_bytes(random_digest.as_bytes()).unwrap();
132132
/// #
133133
/// // Create a key suitable for ECDH key generation
134134
/// let ecc_parms = PublicEccParametersBuilder::new()
@@ -263,7 +263,7 @@ impl Context {
263263
/// # .expect("Failed to set attributes on session");
264264
/// # context.set_sessions((Some(session), None, None));
265265
/// # let random_digest = context.get_random(16).unwrap();
266-
/// # let key_auth = Auth::try_from(random_digest.value().to_vec()).unwrap();
266+
/// # let key_auth = Auth::from_bytes(random_digest.as_bytes()).unwrap();
267267
/// #
268268
/// // Create a key suitable for ECDH key generation
269269
/// let ecc_parms = PublicEccParametersBuilder::new()
@@ -313,7 +313,7 @@ impl Context {
313313
/// // Generate ephemeral key pair and a shared secret
314314
/// let (z_point, pub_point) = context.ecdh_key_gen(key_handle).unwrap();
315315
/// let z_point_gen = context.ecdh_z_gen(key_handle, pub_point).unwrap();
316-
/// assert_eq!(z_point.x().value(), z_point_gen.x().value());
316+
/// assert_eq!(z_point.x().as_bytes(), z_point_gen.x().as_bytes());
317317
/// ```
318318
pub fn ecdh_z_gen(&mut self, key_handle: KeyHandle, in_point: EccPoint) -> Result<EccPoint> {
319319
let mut out_point_ptr = null_mut();

tss-esapi/src/context/tpm_commands/context_management.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl Context {
114114
/// context.execute_with_session(Some(session), |ctx| {
115115
/// let random_digest = ctx.get_random(16)
116116
/// .expect("Call to get_random failed");
117-
/// let key_auth = Auth::try_from(random_digest.value().to_vec())
117+
/// let key_auth = Auth::from_bytes(random_digest.as_bytes())
118118
/// .expect("Failed to create Auth");
119119
/// let key_handle = ctx
120120
/// .create_primary(

tss-esapi/src/context/tpm_commands/symmetric_primitives.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Context {
6060
/// # context
6161
/// # .get_random(16)
6262
/// # .expect("get_rand call failed")
63-
/// # .value()
63+
/// # .as_bytes()
6464
/// # .to_vec(),
6565
/// # )
6666
/// # .expect("Failed to create primary key auth");
@@ -107,7 +107,7 @@ impl Context {
107107
/// # context
108108
/// # .get_random(16)
109109
/// # .expect("get_rand call failed")
110-
/// # .value()
110+
/// # .as_bytes()
111111
/// # .to_vec(),
112112
/// # )
113113
/// # .expect("Failed to create symmetric key auth");

tss-esapi/src/structures/buffers.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@ macro_rules! named_field_buffer_type {
2323
impl $native_type {
2424
pub const MAX_SIZE: usize = $MAX;
2525

26-
pub fn value(&self) -> &[u8] {
27-
&self.0
26+
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
27+
Self::ensure_valid_buffer_size(bytes.len(), "bytes(&[u8])")?;
28+
Ok($native_type(bytes.to_vec().into()))
29+
}
30+
31+
/// Returns the content of the buffer type as
32+
/// a slice of bytes.
33+
pub fn as_bytes(&self) -> &[u8] {
34+
self.0.as_slice()
2835
}
2936

3037
/// Private function for ensuring that a buffer size is valid.
@@ -53,15 +60,6 @@ macro_rules! named_field_buffer_type {
5360
}
5461
}
5562

56-
impl TryFrom<&[u8]> for $native_type {
57-
type Error = Error;
58-
59-
fn try_from(bytes: &[u8]) -> Result<Self> {
60-
Self::ensure_valid_buffer_size(bytes.len(), "&[u8]")?;
61-
Ok($native_type(bytes.to_vec().into()))
62-
}
63-
}
64-
6563
impl TryFrom<$tss_type> for $native_type {
6664
type Error = Error;
6765

@@ -119,7 +117,7 @@ pub mod digest {
119117

120118
fn try_from(value: Digest) -> Result<Self> {
121119
value
122-
.value()
120+
.as_bytes()
123121
.try_into()
124122
.map_err(|_| Error::local_error(WrapperErrorKind::WrongParamSize))
125123
}
@@ -130,7 +128,7 @@ pub mod digest {
130128

131129
fn try_from(value: Digest) -> Result<Self> {
132130
value
133-
.value()
131+
.as_bytes()
134132
.try_into()
135133
.map_err(|_| Error::local_error(WrapperErrorKind::WrongParamSize))
136134
}
@@ -141,13 +139,13 @@ pub mod digest {
141139
type Error = Error;
142140

143141
fn try_from(value: Digest) -> Result<Self> {
144-
if value.value().len() != 48 {
142+
if value.len() != 48 {
145143
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
146144
}
147145

148146
let mut result = [0; 48];
149147

150-
result.copy_from_slice(value.value());
148+
result.copy_from_slice(value.as_bytes());
151149

152150
Ok(result)
153151
}
@@ -157,13 +155,13 @@ pub mod digest {
157155
type Error = Error;
158156

159157
fn try_from(value: Digest) -> Result<Self> {
160-
if value.value().len() != 64 {
158+
if value.len() != 64 {
161159
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
162160
}
163161

164162
let mut result = [0; 64];
165163

166-
result.copy_from_slice(value.value());
164+
result.copy_from_slice(value.as_bytes());
167165

168166
Ok(result)
169167
}
@@ -300,12 +298,12 @@ pub mod public_key_rsa {
300298
type Error = Error;
301299

302300
fn try_from(public_key_rsa: PublicKeyRsa) -> Result<Self> {
303-
if public_key_rsa.value().len() > 128 {
301+
if public_key_rsa.len() > 128 {
304302
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
305303
}
306304

307305
let mut value = [0u8; 128];
308-
value.copy_from_slice(public_key_rsa.value());
306+
value.copy_from_slice(public_key_rsa.as_bytes());
309307
Ok(value)
310308
}
311309
}
@@ -314,12 +312,12 @@ pub mod public_key_rsa {
314312
type Error = Error;
315313

316314
fn try_from(public_key_rsa: PublicKeyRsa) -> Result<Self> {
317-
if public_key_rsa.value().len() > 256 {
315+
if public_key_rsa.len() > 256 {
318316
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
319317
}
320318

321319
let mut value = [0u8; 256];
322-
value.copy_from_slice(public_key_rsa.value());
320+
value.copy_from_slice(public_key_rsa.as_bytes());
323321
Ok(value)
324322
}
325323
}
@@ -328,12 +326,12 @@ pub mod public_key_rsa {
328326
type Error = Error;
329327

330328
fn try_from(public_key_rsa: PublicKeyRsa) -> Result<Self> {
331-
if public_key_rsa.value().len() > 384 {
329+
if public_key_rsa.len() > 384 {
332330
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
333331
}
334332

335333
let mut value = [0u8; 384];
336-
value.copy_from_slice(public_key_rsa.value());
334+
value.copy_from_slice(public_key_rsa.as_bytes());
337335
Ok(value)
338336
}
339337
}
@@ -342,12 +340,12 @@ pub mod public_key_rsa {
342340
type Error = Error;
343341

344342
fn try_from(public_key_rsa: PublicKeyRsa) -> Result<Self> {
345-
if public_key_rsa.value().len() > 512 {
343+
if public_key_rsa.len() > 512 {
346344
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
347345
}
348346

349347
let mut value = [0u8; 512];
350-
value.copy_from_slice(public_key_rsa.value());
348+
value.copy_from_slice(public_key_rsa.as_bytes());
351349
Ok(value)
352350
}
353351
}

tss-esapi/src/structures/hash/agile.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,11 @@ impl TryFrom<TPMT_HA> for HashAgile {
5555
Ok(HashAgile {
5656
algorithm,
5757
digest: match algorithm {
58-
HashingAlgorithm::Sha1 => unsafe { tpmt_ha.digest.sha1 }.as_ref().try_into()?,
59-
HashingAlgorithm::Sha256 => unsafe { tpmt_ha.digest.sha256 }.as_ref().try_into()?,
60-
HashingAlgorithm::Sha384 => unsafe { tpmt_ha.digest.sha384 }.as_ref().try_into()?,
61-
HashingAlgorithm::Sha512 => unsafe { tpmt_ha.digest.sha512 }.as_ref().try_into()?,
62-
HashingAlgorithm::Sm3_256 => {
63-
unsafe { tpmt_ha.digest.sm3_256 }.as_ref().try_into()?
64-
}
58+
HashingAlgorithm::Sha1 => unsafe { tpmt_ha.digest.sha1 }.into(),
59+
HashingAlgorithm::Sha256 => unsafe { tpmt_ha.digest.sha256 }.into(),
60+
HashingAlgorithm::Sha384 => unsafe { tpmt_ha.digest.sha384 }.into(),
61+
HashingAlgorithm::Sha512 => unsafe { tpmt_ha.digest.sha512 }.into(),
62+
HashingAlgorithm::Sm3_256 => unsafe { tpmt_ha.digest.sm3_256 }.into(),
6563
_ => return Err(Error::local_error(WrapperErrorKind::WrongValueFromTpm)),
6664
},
6765
})

0 commit comments

Comments
 (0)