Skip to content

Commit

Permalink
Put unused syscalls behind features
Browse files Browse the repository at this point in the history
CounterClient and CryptoClient::attest are not currently used by the
solo2 or nk3 firmwares, but due to the indirect dispatch of Trussed
requests, they cannot be optimized out by the linker.  This patch
introduces feature flags for these syscalls that are enabled by default.
  • Loading branch information
robin-nitrokey committed Dec 15, 2023
1 parent a801386 commit 278cdb5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Made `postcard_deserialize`, `postcard_serialize` and
`postcard_serialize_bytes` private.
- Changed `&PathBuf` to `&Path` where possible.
- Put `CounterClient` and `CryptoClient::attest` behind feature flags (enabled
by default).

### Fixed

Expand Down
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ serde_cbor = { feature = "0.11.2", features = ["std"] }
# rand_core = { version = "0.5", features = ["getrandom"] }

[features]
default = ["default-mechanisms", "clients-5"]
default = ["default-mechanisms", "default-syscalls", "clients-5"]
serde-extensions = []
std = []
verbose-tests = ["littlefs2/ll-assertions"]
Expand All @@ -75,7 +75,6 @@ log-warn = []
log-error = []

# mechanisms
# default-mechanisms = ["aes256-cbc", "chacha8-poly1305", "ed255", "hmac-sha256", "p256", "sha256", "trng"]
default-mechanisms = [
"aes256-cbc",
"chacha8-poly1305",
Expand Down Expand Up @@ -107,6 +106,11 @@ tdes = ["des"]
totp = ["sha-1"]
trng = ["sha-1"]

# syscalls
default-syscalls = ["counter-client", "crypto-client-attest"]
counter-client = []
crypto-client-attest = []

clients-1 = []
clients-2 = []
clients-3 = []
Expand Down
3 changes: 3 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ pub trait CryptoClient: PollClient {
})
}

#[cfg(feature = "crypto-client-attest")]
fn attest(
&mut self,
signing_mechanism: Mechanism,
Expand Down Expand Up @@ -568,13 +569,15 @@ pub trait CryptoClient: PollClient {

/// Create counters, increment existing counters.
pub trait CounterClient: PollClient {
#[cfg(feature = "counter-client")]
fn create_counter(
&mut self,
location: Location,
) -> ClientResult<'_, reply::CreateCounter, Self> {
self.request(request::CreateCounter { location })
}

#[cfg(feature = "counter-client")]
fn increment_counter(
&mut self,
id: CounterId,
Expand Down
13 changes: 13 additions & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ impl<P: Platform> ServiceResources<P> {

let keystore = once(|this, ctx| this.keystore(ctx.path.clone()));
let certstore = once(|this, ctx| this.certstore(ctx));
#[cfg(feature = "counter-client")]
let counterstore = once(|this, ctx| this.counterstore(ctx));

let filestore = &mut self.filestore(ctx.path.clone());
Expand All @@ -170,6 +171,7 @@ impl<P: Platform> ServiceResources<P> {
}.map(Reply::Agree)
},

#[cfg(feature = "crypto-client-attest")]
Request::Attest(request) => {
let mut attn_keystore: ClientKeystore<P::S> = ClientKeystore::new(
PathBuf::from("attn"),
Expand All @@ -179,6 +181,9 @@ impl<P: Platform> ServiceResources<P> {
attest::try_attest(&mut attn_keystore, &mut certstore(self, ctx)?, &mut keystore(self, ctx)?, request).map(Reply::Attest)
}

#[cfg(not(feature = "crypto-client-attest"))]
Request::Attest(_) => Err(Error::RequestNotAvailable),

Request::Decrypt(request) => {
match request.mechanism {

Expand Down Expand Up @@ -609,16 +614,24 @@ impl<P: Platform> ServiceResources<P> {
Ok(Reply::SetCustomStatus(reply::SetCustomStatus {}))
}

#[cfg(feature = "counter-client")]
Request::CreateCounter(request) => {
counterstore(self, ctx)?.create(request.location)
.map(|id| Reply::CreateCounter(reply::CreateCounter { id } ))
}

#[cfg(not(feature = "counter-client"))]
Request::CreateCounter(_) => Err(Error::RequestNotAvailable),

#[cfg(feature = "counter-client")]
Request::IncrementCounter(request) => {
counterstore(self, ctx)?.increment(request.id)
.map(|counter| Reply::IncrementCounter(reply::IncrementCounter { counter } ))
}

#[cfg(not(feature = "counter-client"))]
Request::IncrementCounter(_) => Err(Error::RequestNotAvailable),

Request::DeleteCertificate(request) => {
certstore(self, ctx)?.delete_certificate(request.id)
.map(|_| Reply::DeleteCertificate(reply::DeleteCertificate {} ))
Expand Down
1 change: 1 addition & 0 deletions tests/counter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg(feature = "virt")]
#![cfg(feature = "counter-client")]

mod client;
mod store;
Expand Down

0 comments on commit 278cdb5

Please sign in to comment.