Skip to content

Commit 9eda567

Browse files
committed
Addressing reviewer comments
1 parent 4a906fe commit 9eda567

File tree

2 files changed

+27
-39
lines changed

2 files changed

+27
-39
lines changed

fortanix-vme/nsm/src/lib.rs

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! The nsm_io library provides an interface to the AWS Nitro Security Module (nsm). Unfortunately,
2+
//! the interface of that crate is not very Rust friendly. To avoid clients of `nsm_io` having to
3+
//! write the same wrapper code over and over again, this crate does this once.
14
pub use nitro_attestation_verify::{AttestationDocument, Unverified, NitroError as AttestationError, Mbedtls};
25
use nsm_io::{ErrorCode, Response, Request};
36
pub use nsm_io::Digest;
@@ -22,18 +25,7 @@ pub enum Error {
2225

2326
impl std::fmt::Display for Error {
2427
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
25-
match self {
26-
Error::AttestationError(ref msg) => write!(fmt, "Attestation error: {}", msg),
27-
Error::BufferTooSmall => write!(fmt, "Buffer too small"),
28-
Error::CannotOpenDriver => write!(fmt, "CannotOpenDriver"),
29-
Error::InputTooLarge => write!(fmt, "InputTooLarge"),
30-
Error::InternalError => write!(fmt, "InternalError"),
31-
Error::InvalidArgument => write!(fmt, "InvalidArgument"),
32-
Error::InvalidOperation => write!(fmt, "InvalidOperation"),
33-
Error::InvalidPcrIndex => write!(fmt, "InvalidPcrIndex"),
34-
Error::InvalidResponse => write!(fmt, "InvalidResponse"),
35-
Error::ReadOnlyPcrIndex => write!(fmt, "ReadOnlyPcrIndex"),
36-
}
28+
std::fmt::Debug::fmt(self, fmt)
3729
}
3830
}
3931

@@ -99,35 +91,22 @@ impl Pcr {
9991
}
10092
}
10193

102-
impl TryFrom<Response> for Pcr {
103-
type Error = Error;
104-
105-
fn try_from(req: Response) -> Result<Self, Self::Error> {
106-
match req {
107-
Response::DescribePCR { lock, data } => Ok(Pcr::new(lock, data)),
108-
Response::ExtendPCR { data } => Ok(Pcr::new(false, data)) /* Only unlocked PCRs can get extended */,
109-
Response::Error(code) => Err(code.into()),
110-
_ => Err(Error::InvalidResponse),
111-
}
112-
}
113-
}
114-
11594
#[derive(Debug, PartialEq)]
11695
pub struct Description {
11796
/// Breaking API changes are denoted by `major_version`
118-
pub version_major: u16,
97+
version_major: u16,
11998
/// Minor API changes are denoted by `minor_version`. Minor versions should be backwards compatible.
120-
pub version_minor: u16,
99+
version_minor: u16,
121100
/// Patch version. These are security and stability updates and do not affect API.
122-
pub version_patch: u16,
101+
version_patch: u16,
123102
/// `module_id` is an identifier for a singular NitroSecureModule
124-
pub module_id: String,
103+
module_id: String,
125104
/// The maximum number of PCRs exposed by the NitroSecureModule.
126-
pub max_pcrs: u16,
105+
max_pcrs: u16,
127106
/// The PCRs that are read-only.
128-
pub locked_pcrs: BTreeSet<u16>,
107+
locked_pcrs: BTreeSet<u16>,
129108
/// The digest of the PCR Bank
130-
pub digest: Digest,
109+
digest: Digest,
131110
}
132111

133112
impl Description {
@@ -221,15 +200,24 @@ impl Nsm {
221200
let req = Request::DescribePCR {
222201
index: idx_pcr,
223202
};
224-
nsm_driver::nsm_process_request(self.0, req).try_into()
203+
if let Response::DescribePCR { lock, data } = nsm_driver::nsm_process_request(self.0, req) {
204+
Ok(Pcr::new(lock, data))
205+
} else {
206+
Err(Error::InvalidResponse)
207+
}
225208
}
226209

227210
pub fn extend_pcr(&mut self, idx_pcr: u16, data: Vec<u8>) -> Result<Pcr, Error> {
228211
let req = Request::ExtendPCR {
229212
index: idx_pcr,
230213
data,
231214
};
232-
nsm_driver::nsm_process_request(self.0, req).try_into()
215+
if let Response::ExtendPCR { data } = nsm_driver::nsm_process_request(self.0, req) {
216+
let locked = false; /* Only unlocked PCRs can get extended */
217+
Ok(Pcr::new(locked, data))
218+
} else {
219+
Err(Error::InvalidResponse)
220+
}
233221
}
234222

235223
pub fn lock_pcr(&mut self, idx_pcr: u16) -> Result<(), Error> {

fortanix-vme/tests/nsm-test/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ fn main() {
4747

4848
println!("# nsm description: {:#?}", nsm.describe().unwrap());
4949
let description = nsm.describe().unwrap();
50-
assert_eq!(description.version_major, 1);
51-
assert_eq!(description.version_minor, 0);
52-
assert_eq!(description.version_patch, 0);
53-
assert_eq!(description.max_pcrs, 32);
50+
assert_eq!(description.version_major(), 1);
51+
assert_eq!(description.version_minor(), 0);
52+
assert_eq!(description.version_patch(), 0);
53+
assert_eq!(description.max_pcrs(), 32);
5454
assert_eq!(description.locked_pcrs().iter().count(), 18);
55-
assert_eq!(description.digest, Digest::SHA384);
55+
assert_eq!(description.digest(), Digest::SHA384);
5656
assert!(nsm.get_random().is_ok());
5757
}

0 commit comments

Comments
 (0)