Skip to content

Commit ac7efa4

Browse files
authored
Merge pull request #375 from Superhepper/from-slice-removal
Removes from slice conversions for buffer types.
2 parents 1d8694d + 3b12ad2 commit ac7efa4

34 files changed

+217
-207
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: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,24 @@ 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()
35+
}
36+
37+
/// Private function for ensuring that a buffer size is valid.
38+
fn ensure_valid_buffer_size(buffer_size: usize, container_name: &str) -> Result<()> {
39+
if buffer_size > Self::MAX_SIZE {
40+
error!("Invalid {} size(> {})", container_name, Self::MAX_SIZE);
41+
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
42+
}
43+
Ok(())
2844
}
2945
}
3046

@@ -39,35 +55,17 @@ macro_rules! named_field_buffer_type {
3955
type Error = Error;
4056

4157
fn try_from(bytes: Vec<u8>) -> Result<Self> {
42-
if bytes.len() > Self::MAX_SIZE {
43-
error!("Invalid Vec<u8> size(> {})", Self::MAX_SIZE);
44-
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
45-
}
58+
Self::ensure_valid_buffer_size(bytes.len(), "Vec<u8>")?;
4659
Ok($native_type(bytes.into()))
4760
}
4861
}
4962

50-
impl TryFrom<&[u8]> for $native_type {
51-
type Error = Error;
52-
53-
fn try_from(bytes: &[u8]) -> Result<Self> {
54-
if bytes.len() > Self::MAX_SIZE {
55-
error!("Invalid &[u8] size(> {})", Self::MAX_SIZE);
56-
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
57-
}
58-
Ok($native_type(bytes.to_vec().into()))
59-
}
60-
}
61-
6263
impl TryFrom<$tss_type> for $native_type {
6364
type Error = Error;
6465

6566
fn try_from(tss: $tss_type) -> Result<Self> {
6667
let size = tss.size as usize;
67-
if size > Self::MAX_SIZE {
68-
error!("Invalid buffer size(> {})", Self::MAX_SIZE);
69-
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
70-
}
68+
Self::ensure_valid_buffer_size(size, "buffer")?;
7169
Ok($native_type(tss.$buffer_field_name[..size].to_vec().into()))
7270
}
7371
}
@@ -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,17 +155,49 @@ 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
}
170168
}
169+
170+
impl From<[u8; 20]> for Digest {
171+
fn from(mut value: [u8; 20]) -> Self {
172+
let value_as_vec = value.to_vec();
173+
value.zeroize();
174+
Digest(value_as_vec.into())
175+
}
176+
}
177+
178+
impl From<[u8; 32]> for Digest {
179+
fn from(mut value: [u8; 32]) -> Self {
180+
let value_as_vec = value.to_vec();
181+
value.zeroize();
182+
Digest(value_as_vec.into())
183+
}
184+
}
185+
186+
impl From<[u8; 48]> for Digest {
187+
fn from(mut value: [u8; 48]) -> Self {
188+
let value_as_vec = value.to_vec();
189+
value.zeroize();
190+
Digest(value_as_vec.into())
191+
}
192+
}
193+
194+
impl From<[u8; 64]> for Digest {
195+
fn from(mut value: [u8; 64]) -> Self {
196+
let value_as_vec = value.to_vec();
197+
value.zeroize();
198+
Digest(value_as_vec.into())
199+
}
200+
}
171201
}
172202

173203
pub mod ecc_parameter {
@@ -268,12 +298,12 @@ pub mod public_key_rsa {
268298
type Error = Error;
269299

270300
fn try_from(public_key_rsa: PublicKeyRsa) -> Result<Self> {
271-
if public_key_rsa.value().len() > 128 {
301+
if public_key_rsa.len() > 128 {
272302
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
273303
}
274304

275305
let mut value = [0u8; 128];
276-
value.copy_from_slice(public_key_rsa.value());
306+
value.copy_from_slice(public_key_rsa.as_bytes());
277307
Ok(value)
278308
}
279309
}
@@ -282,12 +312,12 @@ pub mod public_key_rsa {
282312
type Error = Error;
283313

284314
fn try_from(public_key_rsa: PublicKeyRsa) -> Result<Self> {
285-
if public_key_rsa.value().len() > 256 {
315+
if public_key_rsa.len() > 256 {
286316
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
287317
}
288318

289319
let mut value = [0u8; 256];
290-
value.copy_from_slice(public_key_rsa.value());
320+
value.copy_from_slice(public_key_rsa.as_bytes());
291321
Ok(value)
292322
}
293323
}
@@ -296,12 +326,12 @@ pub mod public_key_rsa {
296326
type Error = Error;
297327

298328
fn try_from(public_key_rsa: PublicKeyRsa) -> Result<Self> {
299-
if public_key_rsa.value().len() > 384 {
329+
if public_key_rsa.len() > 384 {
300330
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
301331
}
302332

303333
let mut value = [0u8; 384];
304-
value.copy_from_slice(public_key_rsa.value());
334+
value.copy_from_slice(public_key_rsa.as_bytes());
305335
Ok(value)
306336
}
307337
}
@@ -310,12 +340,12 @@ pub mod public_key_rsa {
310340
type Error = Error;
311341

312342
fn try_from(public_key_rsa: PublicKeyRsa) -> Result<Self> {
313-
if public_key_rsa.value().len() > 512 {
343+
if public_key_rsa.len() > 512 {
314344
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
315345
}
316346

317347
let mut value = [0u8; 512];
318-
value.copy_from_slice(public_key_rsa.value());
348+
value.copy_from_slice(public_key_rsa.as_bytes());
319349
Ok(value)
320350
}
321351
}

0 commit comments

Comments
 (0)