Skip to content

Commit 4fa6830

Browse files
committed
Describe NSM
1 parent 6a062f1 commit 4fa6830

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

fortanix-vme/nsm/src/lib.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pub use nitro_attestation_verify::{AttestationDocument, Unverified, NitroError as AttestationError};
22
use nsm_io::{ErrorCode, Response, Request};
3+
pub use nsm_io::Digest;
34
pub use serde_bytes::ByteBuf;
5+
use std::collections::BTreeSet;
46

57
pub struct Nsm(i32);
68

@@ -102,6 +104,52 @@ impl TryFrom<Response> for Pcr {
102104
}
103105
}
104106

107+
#[derive(Debug, PartialEq)]
108+
pub struct Description {
109+
/// Breaking API changes are denoted by `major_version`
110+
pub version_major: u16,
111+
/// Minor API changes are denoted by `minor_version`. Minor versions should be backwards compatible.
112+
pub version_minor: u16,
113+
/// Patch version. These are security and stability updates and do not affect API.
114+
pub version_patch: u16,
115+
/// `module_id` is an identifier for a singular NitroSecureModule
116+
pub module_id: String,
117+
/// The maximum number of PCRs exposed by the NitroSecureModule.
118+
pub max_pcrs: u16,
119+
/// The PCRs that are read-only.
120+
pub locked_pcrs: BTreeSet<u16>,
121+
/// The digest of the PCR Bank
122+
pub digest: Digest,
123+
}
124+
125+
impl TryFrom<Response> for Description {
126+
type Error = Error;
127+
128+
fn try_from(response: Response) -> Result<Self, Self::Error> {
129+
match response {
130+
Response::DescribeNSM {
131+
version_major,
132+
version_minor,
133+
version_patch,
134+
module_id,
135+
max_pcrs,
136+
locked_pcrs,
137+
digest,
138+
} => Ok(Description {
139+
version_major,
140+
version_minor,
141+
version_patch,
142+
module_id,
143+
max_pcrs,
144+
locked_pcrs,
145+
digest,
146+
}),
147+
Response::Error(code) => Err(code.into()),
148+
_ => Err(Error::InvalidResponse),
149+
}
150+
}
151+
}
152+
105153
impl Nsm {
106154
pub fn new() -> Result<Self, Error> {
107155
let fd = nsm_driver::nsm_init();
@@ -162,6 +210,10 @@ impl Nsm {
162210
_ => Err(Error::InvalidResponse),
163211
}
164212
}
213+
214+
pub fn describe(&self) -> Result<Description, Error> {
215+
nsm_driver::nsm_process_request(self.0, Request::DescribeNSM).try_into()
216+
}
165217
}
166218

167219
impl Drop for Nsm {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use nsm::{ByteBuf, Nsm};
1+
use nsm::{ByteBuf, Digest, Nsm};
22

33
fn main() {
44
let mut nsm = Nsm::new().unwrap();
@@ -44,4 +44,13 @@ fn main() {
4444
println!("#pcr{} = {:?}", pcr, nsm.describe_pcr(pcr));
4545
assert_eq!(nsm.describe_pcr(pcr).map(|val| val.locked), Ok(pcr < 18));
4646
}
47+
48+
println!("# nsm description: {:#?}", nsm.describe().unwrap());
49+
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);
54+
assert_eq!(description.locked_pcrs.iter().count(), 18);
55+
assert_eq!(description.digest, Digest::SHA384);
4756
}

0 commit comments

Comments
 (0)