Skip to content

Commit 81110e6

Browse files
committed
Incorporate PR feedback
- moved the max buffer size computation in its own function - added `non_exhaustive` to the options enum - changed the name of the enum variants to be more descriptive of what they represent - other small refactoring/doc bits Signed-off-by: Ionut Mihalcea <[email protected]>
1 parent b1325a6 commit 81110e6

File tree

2 files changed

+32
-23
lines changed
  • tss-esapi

2 files changed

+32
-23
lines changed

tss-esapi/src/abstraction/nv.rs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn read_full(
2020
auth_handle: NvAuth,
2121
nv_index_handle: NvIndexTpmHandle,
2222
) -> Result<Vec<u8>> {
23-
let mut rw = NvOpenOptions::Index {
23+
let mut rw = NvOpenOptions::ExistingIndex {
2424
auth_handle,
2525
nv_index_handle,
2626
}
@@ -84,13 +84,16 @@ pub fn list(context: &mut Context) -> Result<Vec<(NvPublic, Name)>> {
8484
}
8585

8686
/// Options and flags which can be used to determine how a non-volatile storage index is opened.
87+
#[non_exhaustive]
8788
#[derive(Debug, Clone)]
8889
pub enum NvOpenOptions {
89-
Public {
90+
/// Define a new NV space with given auth
91+
NewIndex {
9092
nv_public: NvPublic,
9193
auth_handle: NvAuth,
9294
},
93-
Index {
95+
/// Open the NV space at the given handle, with the given auth
96+
ExistingIndex {
9497
nv_index_handle: NvIndexTpmHandle,
9598
auth_handle: NvAuth,
9699
},
@@ -101,18 +104,10 @@ impl NvOpenOptions {
101104
///
102105
/// The non-volatile storage index may be used for reading or writing or both.
103106
pub fn open<'a>(&self, context: &'a mut Context) -> Result<NvReaderWriter<'a>> {
104-
let buffer_size = context
105-
.get_tpm_property(PropertyTag::NvBufferMax)?
106-
.map(usize::try_from)
107-
.transpose()
108-
.map_err(|_| {
109-
log::error!("Failed to obtain valid maximum NV buffer size");
110-
Error::WrapperError(WrapperErrorKind::InternalError)
111-
})?
112-
.unwrap_or(MaxNvBuffer::MAX_SIZE);
107+
let buffer_size = max_nv_buffer_size(context)?;
113108

114109
let (data_size, nv_idx, auth_handle) = match self {
115-
NvOpenOptions::Index {
110+
NvOpenOptions::ExistingIndex {
116111
nv_index_handle,
117112
auth_handle,
118113
} => {
@@ -128,17 +123,18 @@ impl NvOpenOptions {
128123
auth_handle,
129124
)
130125
}
131-
NvOpenOptions::Public {
126+
NvOpenOptions::NewIndex {
132127
nv_public,
133128
auth_handle,
134-
} => {
135-
let auth = AuthHandle::from(*auth_handle);
136-
(
137-
nv_public.data_size(),
138-
context.nv_define_space(auth.try_into()?, None, nv_public.clone())?,
139-
auth_handle,
140-
)
141-
}
129+
} => (
130+
nv_public.data_size(),
131+
context.nv_define_space(
132+
AuthHandle::from(*auth_handle).try_into()?,
133+
None,
134+
nv_public.clone(),
135+
)?,
136+
auth_handle,
137+
),
142138
};
143139

144140
Ok(NvReaderWriter {
@@ -152,6 +148,19 @@ impl NvOpenOptions {
152148
}
153149
}
154150

151+
/// Get the maximum buffer size for an NV space.
152+
pub fn max_nv_buffer_size(ctx: &mut Context) -> Result<usize> {
153+
Ok(ctx
154+
.get_tpm_property(PropertyTag::NvBufferMax)?
155+
.map(usize::try_from)
156+
.transpose()
157+
.map_err(|_| {
158+
log::error!("Failed to obtain valid maximum NV buffer size");
159+
Error::WrapperError(WrapperErrorKind::InternalError)
160+
})?
161+
.unwrap_or(MaxNvBuffer::MAX_SIZE))
162+
}
163+
155164
/// Non-volatile storage index reader/writer
156165
///
157166
/// Provides methods and trait implementations to interact with a non-volatile storage index that has been opened.

tss-esapi/tests/integration_tests/abstraction_tests/nv_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn write() {
126126
.build()
127127
.unwrap();
128128

129-
let mut rw = nv::NvOpenOptions::Public {
129+
let mut rw = nv::NvOpenOptions::NewIndex {
130130
nv_public: owner_nv_public,
131131
auth_handle: NvAuth::Owner,
132132
}

0 commit comments

Comments
 (0)