Skip to content

Commit eece692

Browse files
authored
Merge pull request #312 from ionut-arm/ecc-curves
Replace type in CapabilityData::EccCurves
2 parents 392b170 + b573cf0 commit eece692

File tree

11 files changed

+616
-58
lines changed

11 files changed

+616
-58
lines changed

tss-esapi/src/constants/ecc.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use num_traits::{FromPrimitive, ToPrimitive};
1414
use std::convert::TryFrom;
1515
/// Enum that contains the constants for the
1616
/// implemented elliptic curves.
17+
///
18+
/// # Details
19+
/// This corresponds to `TPM2_ECC_CURVE`
1720
#[derive(FromPrimitive, ToPrimitive, Debug, Copy, Clone, PartialEq, Eq)]
1821
#[repr(u16)]
1922
pub enum EccCurveIdentifier {

tss-esapi/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
unused_results,
2929
missing_copy_implementations
3030
)]
31-
#![allow(clippy::upper_case_acronyms)]
3231
//! # TSS 2.0 Rust Wrapper over Enhanced System API
3332
//! This crate exposes the functionality of the TCG Software Stack Enhanced System API to
3433
//! Rust developers, both directly through FFI bindings and through more Rust-tailored interfaces

tss-esapi/src/structures/capabilitydata.rs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,38 @@
22
// SPDX-License-Identifier: Apache-2.0
33
use crate::{
44
constants::tss::*,
5-
handles::TpmHandle,
65
structures::{
7-
AlgorithmPropertyList, CommandCodeList, PcrSelectionList, TaggedPcrPropertyList,
8-
TaggedTpmPropertyList,
6+
AlgorithmPropertyList, CommandCodeList, EccCurveList, HandleList, PcrSelectionList,
7+
TaggedPcrPropertyList, TaggedTpmPropertyList,
98
},
109
tss2_esys::*,
1110
Error, Result, WrapperErrorKind,
1211
};
1312
use std::convert::{TryFrom, TryInto};
1413
use std::mem::size_of;
1514

15+
/// A representation of all the capabilites that can be associated
16+
/// with a TPM.
17+
///
18+
/// # Details
19+
/// This corresponds to `TPMS_CAPABILITY_DATA`
1620
#[derive(Debug, Clone)]
1721
pub enum CapabilityData {
1822
Algorithms(AlgorithmPropertyList),
19-
Handles(Vec<TpmHandle>),
23+
Handles(HandleList),
2024
Commands(Vec<TPMA_CC>),
2125
PpCommands(CommandCodeList),
2226
AuditCommands(CommandCodeList),
23-
AssignedPCR(PcrSelectionList),
27+
AssignedPcr(PcrSelectionList),
2428
TpmProperties(TaggedTpmPropertyList),
2529
PcrProperties(TaggedPcrPropertyList),
26-
ECCCurves(Vec<TPM2_ECC_CURVE>),
30+
EccCurves(EccCurveList),
2731
// These are in the TPM TPMU_CAPABILITIES, but are not defined by esapi-2.4.1
2832
// AuthPolicies(),
2933
// ActData(),
3034
}
3135

32-
fn max_cap_size<T>() -> u32 {
36+
pub(crate) const fn max_cap_size<T>() -> u32 {
3337
((TPM2_MAX_CAP_BUFFER as usize - size_of::<TPM2_CAP>() - size_of::<u32>()) / size_of::<T>())
3438
as u32
3539
}
@@ -39,19 +43,7 @@ fn cd_from_alg_properties(props: TPML_ALG_PROPERTY) -> Result<CapabilityData> {
3943
}
4044

4145
fn cd_from_handles(props: TPML_HANDLE) -> Result<CapabilityData> {
42-
if props.count > max_cap_size::<TPM2_HANDLE>() {
43-
return Err(Error::WrapperError(WrapperErrorKind::InvalidParam));
44-
}
45-
46-
let mut data: Vec<TpmHandle> = Vec::new();
47-
data.reserve_exact(props.count as usize);
48-
49-
for i in 0..props.count {
50-
let handle: TPM2_HANDLE = props.handle[i as usize];
51-
data.push(handle.try_into()?);
52-
}
53-
54-
Ok(CapabilityData::Handles(data))
46+
Ok(CapabilityData::Handles(HandleList::try_from(props)?))
5547
}
5648

5749
fn cd_from_command(props: TPML_CCA) -> Result<CapabilityData> {
@@ -82,7 +74,7 @@ fn cd_from_audit_commands(props: TPML_CC) -> Result<CapabilityData> {
8274
}
8375

8476
fn cd_from_assigned_pcrs(props: TPML_PCR_SELECTION) -> Result<CapabilityData> {
85-
Ok(CapabilityData::AssignedPCR(props.try_into()?))
77+
Ok(CapabilityData::AssignedPcr(props.try_into()?))
8678
}
8779

8880
fn cd_from_tpm_properties(props: TPML_TAGGED_TPM_PROPERTY) -> Result<CapabilityData> {
@@ -94,18 +86,7 @@ fn cd_from_pcr_properties(props: TPML_TAGGED_PCR_PROPERTY) -> Result<CapabilityD
9486
}
9587

9688
fn cd_from_ecc_curves(props: TPML_ECC_CURVE) -> Result<CapabilityData> {
97-
if props.count > max_cap_size::<TPM2_ECC_CURVE>() {
98-
return Err(Error::WrapperError(WrapperErrorKind::InvalidParam));
99-
}
100-
101-
let mut data = Vec::new();
102-
data.reserve_exact(props.count as usize);
103-
104-
for i in 0..props.count {
105-
data.push(props.eccCurves[i as usize]);
106-
}
107-
108-
Ok(CapabilityData::ECCCurves(data))
89+
Ok(CapabilityData::EccCurves(EccCurveList::try_from(props)?))
10990
}
11091

11192
impl TryFrom<TPMS_CAPABILITY_DATA> for CapabilityData {
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2022 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use crate::constants::ecc::EccCurveIdentifier;
4+
use crate::tss2_esys::{TPM2_ECC_CURVE, TPML_ECC_CURVE};
5+
use crate::{Error, Result, WrapperErrorKind};
6+
use log::error;
7+
use std::convert::TryFrom;
8+
use std::ops::Deref;
9+
10+
/// A list of ECC curves
11+
///
12+
/// # Details
13+
/// This corresponds to `TPML_ECC_CURVE`.
14+
#[derive(Debug, Clone, Default, PartialEq)]
15+
pub struct EccCurveList {
16+
ecc_curves: Vec<EccCurveIdentifier>,
17+
}
18+
19+
impl EccCurveList {
20+
pub const MAX_SIZE: usize =
21+
crate::structures::capabilitydata::max_cap_size::<TPM2_ECC_CURVE>() as usize;
22+
23+
pub fn new() -> Self {
24+
EccCurveList {
25+
ecc_curves: Vec::new(),
26+
}
27+
}
28+
29+
/// Adds an ECC curve to the list of curves.
30+
pub fn add(&mut self, ecc_curve: EccCurveIdentifier) -> Result<()> {
31+
if self.ecc_curves.len() + 1 > EccCurveList::MAX_SIZE {
32+
error!(
33+
"Adding ECC curve to list will make the list exceeded its maximum count(> {})",
34+
EccCurveList::MAX_SIZE
35+
);
36+
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
37+
}
38+
self.ecc_curves.push(ecc_curve);
39+
Ok(())
40+
}
41+
42+
/// Returns the inner type.
43+
pub fn into_inner(self) -> Vec<EccCurveIdentifier> {
44+
self.ecc_curves
45+
}
46+
}
47+
48+
impl TryFrom<TPML_ECC_CURVE> for EccCurveList {
49+
type Error = Error;
50+
51+
fn try_from(ecc_curves: TPML_ECC_CURVE) -> Result<Self> {
52+
let ecc_curve_count = ecc_curves.count as usize;
53+
if ecc_curve_count > Self::MAX_SIZE {
54+
error!("Error: Invalid TPML_ECC_CURVE count(> {})", Self::MAX_SIZE);
55+
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
56+
}
57+
ecc_curves.eccCurves[..ecc_curve_count]
58+
.iter()
59+
.map(|&cc| EccCurveIdentifier::try_from(cc))
60+
.collect::<Result<Vec<EccCurveIdentifier>>>()
61+
.map(|ecc_curves| EccCurveList { ecc_curves })
62+
}
63+
}
64+
65+
impl From<EccCurveList> for TPML_ECC_CURVE {
66+
fn from(ecc_curves: EccCurveList) -> Self {
67+
let mut tss_ecc_curves: TPML_ECC_CURVE = Default::default();
68+
for ecc_curve in ecc_curves.ecc_curves {
69+
tss_ecc_curves.eccCurves[tss_ecc_curves.count as usize] = ecc_curve.into();
70+
tss_ecc_curves.count += 1;
71+
}
72+
tss_ecc_curves
73+
}
74+
}
75+
76+
impl TryFrom<Vec<EccCurveIdentifier>> for EccCurveList {
77+
type Error = Error;
78+
79+
fn try_from(ecc_curves: Vec<EccCurveIdentifier>) -> Result<Self> {
80+
if ecc_curves.len() > Self::MAX_SIZE {
81+
error!("Error: Invalid TPML_ECC_CURVE count(> {})", Self::MAX_SIZE);
82+
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
83+
}
84+
Ok(EccCurveList { ecc_curves })
85+
}
86+
}
87+
88+
impl From<EccCurveList> for Vec<EccCurveIdentifier> {
89+
fn from(ecc_curve_list: EccCurveList) -> Self {
90+
ecc_curve_list.ecc_curves
91+
}
92+
}
93+
94+
impl AsRef<[EccCurveIdentifier]> for EccCurveList {
95+
fn as_ref(&self) -> &[EccCurveIdentifier] {
96+
self.ecc_curves.as_slice()
97+
}
98+
}
99+
100+
impl Deref for EccCurveList {
101+
type Target = Vec<EccCurveIdentifier>;
102+
103+
fn deref(&self) -> &Self::Target {
104+
&self.ecc_curves
105+
}
106+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2022 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use crate::handles::TpmHandle;
4+
use crate::tss2_esys::{TPM2_HANDLE, TPML_HANDLE};
5+
use crate::{Error, Result, WrapperErrorKind};
6+
use log::error;
7+
use std::convert::TryFrom;
8+
use std::ops::Deref;
9+
10+
/// A list of TPM handles
11+
///
12+
/// # Details
13+
/// This corresponds to `TPML_HANDLE`.
14+
#[derive(Debug, Clone, Default, PartialEq)]
15+
pub struct HandleList {
16+
handles: Vec<TpmHandle>,
17+
}
18+
19+
impl HandleList {
20+
pub const MAX_SIZE: usize =
21+
crate::structures::capabilitydata::max_cap_size::<TPM2_HANDLE>() as usize;
22+
23+
pub fn new() -> Self {
24+
HandleList {
25+
handles: Vec::new(),
26+
}
27+
}
28+
29+
/// Adds a handle to the current list of handles.
30+
pub fn add(&mut self, handle: TpmHandle) -> Result<()> {
31+
if self.handles.len() + 1 > HandleList::MAX_SIZE {
32+
error!(
33+
"Adding TPM handle to list will make the list exceeded its maximum count(> {})",
34+
HandleList::MAX_SIZE
35+
);
36+
return Err(Error::local_error(WrapperErrorKind::WrongParamSize));
37+
}
38+
self.handles.push(handle);
39+
Ok(())
40+
}
41+
42+
/// Returns the inner type.
43+
pub fn into_inner(self) -> Vec<TpmHandle> {
44+
self.handles
45+
}
46+
}
47+
48+
impl TryFrom<TPML_HANDLE> for HandleList {
49+
type Error = Error;
50+
51+
fn try_from(handles: TPML_HANDLE) -> Result<Self> {
52+
let handle_count = handles.count as usize;
53+
if handle_count > Self::MAX_SIZE {
54+
error!("Error: Invalid TPML_HANDLE count(> {})", Self::MAX_SIZE);
55+
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
56+
}
57+
handles.handle[..handle_count]
58+
.iter()
59+
.map(|&cc| TpmHandle::try_from(cc))
60+
.collect::<Result<Vec<TpmHandle>>>()
61+
.map(|handles| HandleList { handles })
62+
}
63+
}
64+
65+
impl From<HandleList> for TPML_HANDLE {
66+
fn from(handles: HandleList) -> Self {
67+
let mut tss_handles: TPML_HANDLE = Default::default();
68+
for handle in handles.handles {
69+
tss_handles.handle[tss_handles.count as usize] = handle.into();
70+
tss_handles.count += 1;
71+
}
72+
tss_handles
73+
}
74+
}
75+
76+
impl TryFrom<Vec<TpmHandle>> for HandleList {
77+
type Error = Error;
78+
79+
fn try_from(handles: Vec<TpmHandle>) -> Result<Self> {
80+
if handles.len() > Self::MAX_SIZE {
81+
error!("Error: Invalid TPML_HANDLE count(> {})", Self::MAX_SIZE);
82+
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
83+
}
84+
Ok(HandleList { handles })
85+
}
86+
}
87+
88+
impl From<HandleList> for Vec<TpmHandle> {
89+
fn from(handle_list: HandleList) -> Self {
90+
handle_list.handles
91+
}
92+
}
93+
94+
impl AsRef<[TpmHandle]> for HandleList {
95+
fn as_ref(&self) -> &[TpmHandle] {
96+
self.handles.as_slice()
97+
}
98+
}
99+
100+
impl Deref for HandleList {
101+
type Target = Vec<TpmHandle>;
102+
103+
fn deref(&self) -> &Self::Target {
104+
&self.handles
105+
}
106+
}

tss-esapi/src/structures/lists/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ pub mod algorithm_property;
44
pub mod command_code;
55
pub mod digest;
66
pub mod digest_values;
7+
pub mod ecc_curves;
8+
pub mod handles;
79
pub mod pcr_selection;
810
pub mod tagged_pcr_property;
911
pub mod tagged_tpm_property;

tss-esapi/src/structures/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ pub mod digest_values {
9090
pub use super::lists::digest_values::*;
9191
}
9292

93+
pub use self::ecc_curves::EccCurveList;
94+
pub mod ecc_curves {
95+
pub use super::lists::ecc_curves::*;
96+
}
97+
98+
pub use self::handle_list::HandleList;
99+
pub mod handle_list {
100+
pub use super::lists::handles::*;
101+
}
102+
93103
pub use self::pcr_selection_list::PcrSelectionList;
94104
pub use self::pcr_selection_list::PcrSelectionListBuilder;
95105
pub mod pcr_selection_list {

0 commit comments

Comments
 (0)