Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the interchange be provided by the runner #100

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ once_cell = "1.13.0"
# rand_core = { version = "0.5", features = ["getrandom"] }

[features]
default = ["default-mechanisms", "clients-5"]
default = ["default-mechanisms"]
serde-extensions = []
std = []
verbose-tests = ["littlefs2/ll-assertions"]
Expand Down Expand Up @@ -103,22 +103,7 @@ tdes = ["des"]
totp = ["sha-1"]
trng = ["sha-1"]

clients-1 = []
clients-2 = []
clients-3 = []
clients-4 = []
clients-5 = []
clients-6 = []
clients-7 = []
clients-8 = []
clients-9 = []
clients-10 = []
clients-11 = []
clients-12 = []

test-attestation-cert-ids = []
# [patch.crates-io]
# interchange = { git = "https://github.com/trussed-dev/interchange", branch = "main" }

[package.metadata.docs.rs]
features = ["serde-extensions", "virt"]
Expand Down
52 changes: 25 additions & 27 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use core::{marker::PhantomData, task::Poll};
use crate::api::*;
use crate::backend::{BackendId, CoreOnly, Dispatch};
use crate::error::*;
use crate::pipe::{TrussedRequester, TRUSSED_INTERCHANGE};
use crate::pipe::TrussedRequester;
use crate::service::Service;
use crate::types::*;

Expand All @@ -107,7 +107,7 @@ pub trait Client:
{
}

impl<S: Syscall, E> Client for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> Client for ClientImplementation<'pipe, S, E> {}

/// Lowest level interface, use one of the higher level ones.
pub trait PollClient {
Expand Down Expand Up @@ -142,12 +142,12 @@ where
}

/// The client implementation client applications actually receive.
pub struct ClientImplementation<S, D = CoreOnly> {
pub struct ClientImplementation<'pipe, S, D = CoreOnly> {
// raw: RawClient<Client<S>>,
syscall: S,

// RawClient:
pub(crate) interchange: TrussedRequester,
pub(crate) interchange: TrussedRequester<'pipe>,
// pending: Option<Discriminant<Request>>,
pending: Option<u8>,
_marker: PhantomData<D>,
Expand All @@ -161,11 +161,11 @@ pub struct ClientImplementation<S, D = CoreOnly> {
// }
// }

impl<S, E> ClientImplementation<S, E>
impl<'pipe, S, E> ClientImplementation<'pipe, S, E>
where
S: Syscall,
{
pub fn new(interchange: TrussedRequester, syscall: S) -> Self {
pub fn new(interchange: TrussedRequester<'pipe>, syscall: S) -> Self {
Self {
interchange,
pending: None,
Expand All @@ -175,7 +175,7 @@ where
}
}

impl<S, E> PollClient for ClientImplementation<S, E>
impl<'pipe, S, E> PollClient for ClientImplementation<'pipe, S, E>
where
S: Syscall,
{
Expand Down Expand Up @@ -229,12 +229,12 @@ where
}
}

impl<S: Syscall, E> CertificateClient for ClientImplementation<S, E> {}
impl<S: Syscall, E> CryptoClient for ClientImplementation<S, E> {}
impl<S: Syscall, E> CounterClient for ClientImplementation<S, E> {}
impl<S: Syscall, E> FilesystemClient for ClientImplementation<S, E> {}
impl<S: Syscall, E> ManagementClient for ClientImplementation<S, E> {}
impl<S: Syscall, E> UiClient for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> CertificateClient for ClientImplementation<'pipe, S, E> {}
impl<'pipe, S: Syscall, E> CryptoClient for ClientImplementation<'pipe, S, E> {}
impl<'pipe, S: Syscall, E> CounterClient for ClientImplementation<'pipe, S, E> {}
impl<'pipe, S: Syscall, E> FilesystemClient for ClientImplementation<'pipe, S, E> {}
impl<'pipe, S: Syscall, E> ManagementClient for ClientImplementation<'pipe, S, E> {}
impl<'pipe, S: Syscall, E> UiClient for ClientImplementation<'pipe, S, E> {}

/// Read/Write + Delete certificates
pub trait CertificateClient: PollClient {
Expand Down Expand Up @@ -730,13 +730,11 @@ impl<D: Dispatch> ClientBuilder<D> {
}
}

fn create_endpoint<P: Platform>(
fn create_endpoint<'pipe, P: Platform, const MAX_CLIENTS: usize>(
self,
service: &mut Service<P, D>,
) -> Result<TrussedRequester, Error> {
let (requester, responder) = TRUSSED_INTERCHANGE
.claim()
.ok_or(Error::ClientCountExceeded)?;
service: &mut Service<'pipe, P, MAX_CLIENTS, D>,
) -> Result<TrussedRequester<'pipe>, Error> {
let (requester, responder) = service.pipe().claim().ok_or(Error::ClientCountExceeded)?;
service.add_endpoint(responder, self.id, self.backends)?;
Ok(requester)
}
Expand All @@ -745,10 +743,10 @@ impl<D: Dispatch> ClientBuilder<D> {
///
/// This allocates a [`TrussedInterchange`][`crate::pipe::TrussedInterchange`] and a
/// [`ServiceEndpoint`][`crate::service::ServiceEndpoint`].
pub fn prepare<P: Platform>(
pub fn prepare<'pipe, P: Platform, const MAX_CLIENTS: usize>(
self,
service: &mut Service<P, D>,
) -> Result<PreparedClient<D>, Error> {
service: &mut Service<'pipe, P, MAX_CLIENTS, D>,
) -> Result<PreparedClient<'pipe, D>, Error> {
self.create_endpoint(service)
.map(|requester| PreparedClient::new(requester))
}
Expand All @@ -759,21 +757,21 @@ impl<D: Dispatch> ClientBuilder<D> {
/// This struct already has an allocated [`TrussedInterchange`][`crate::pipe::TrussedInterchange`] and
/// [`ServiceEndpoint`][`crate::service::ServiceEndpoint`] but still needs a [`Syscall`][]
/// implementation.
pub struct PreparedClient<D> {
requester: TrussedRequester,
pub struct PreparedClient<'pipe, D> {
requester: TrussedRequester<'pipe>,
_marker: PhantomData<D>,
}

impl<D> PreparedClient<D> {
fn new(requester: TrussedRequester) -> Self {
impl<'pipe, D> PreparedClient<'pipe, D> {
fn new(requester: TrussedRequester<'pipe>) -> Self {
Self {
requester,
_marker: Default::default(),
}
}

/// Builds the client using the given syscall implementation.
pub fn build<S: Syscall>(self, syscall: S) -> ClientImplementation<S, D> {
pub fn build<S: Syscall>(self, syscall: S) -> ClientImplementation<'pipe, S, D> {
ClientImplementation::new(self.requester, syscall)
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/client/mechanisms.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

#[cfg(feature = "aes256-cbc")]
impl<S: Syscall, E> Aes256Cbc for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> Aes256Cbc for ClientImplementation<'pipe, S, E> {}

pub trait Aes256Cbc: CryptoClient {
fn decrypt_aes256cbc<'c>(
Expand All @@ -22,7 +22,7 @@ pub trait Aes256Cbc: CryptoClient {
}

#[cfg(feature = "chacha8-poly1305")]
impl<S: Syscall, E> Chacha8Poly1305 for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> Chacha8Poly1305 for ClientImplementation<'pipe, S, E> {}

pub trait Chacha8Poly1305: CryptoClient {
fn decrypt_chacha8poly1305<'c>(
Expand Down Expand Up @@ -101,7 +101,7 @@ pub trait Chacha8Poly1305: CryptoClient {
}

#[cfg(feature = "hmac-blake2s")]
impl<S: Syscall, E> HmacBlake2s for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> HmacBlake2s for ClientImplementation<'pipe, S, E> {}

pub trait HmacBlake2s: CryptoClient {
fn hmacblake2s_derive_key(
Expand Down Expand Up @@ -133,7 +133,7 @@ pub trait HmacBlake2s: CryptoClient {
}

#[cfg(feature = "hmac-sha1")]
impl<S: Syscall, E> HmacSha1 for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> HmacSha1 for ClientImplementation<'pipe, S, E> {}

pub trait HmacSha1: CryptoClient {
fn hmacsha1_derive_key(
Expand Down Expand Up @@ -165,7 +165,7 @@ pub trait HmacSha1: CryptoClient {
}

#[cfg(feature = "hmac-sha256")]
impl<S: Syscall, E> HmacSha256 for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> HmacSha256 for ClientImplementation<'pipe, S, E> {}

pub trait HmacSha256: CryptoClient {
fn hmacsha256_derive_key(
Expand Down Expand Up @@ -197,7 +197,7 @@ pub trait HmacSha256: CryptoClient {
}

#[cfg(feature = "hmac-sha512")]
impl<S: Syscall, E> HmacSha512 for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> HmacSha512 for ClientImplementation<'pipe, S, E> {}

pub trait HmacSha512: CryptoClient {
fn hmacsha512_derive_key(
Expand Down Expand Up @@ -229,7 +229,7 @@ pub trait HmacSha512: CryptoClient {
}

#[cfg(feature = "ed255")]
impl<S: Syscall, E> Ed255 for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> Ed255 for ClientImplementation<'pipe, S, E> {}

pub trait Ed255: CryptoClient {
fn generate_ed255_private_key(
Expand Down Expand Up @@ -297,7 +297,7 @@ pub trait Ed255: CryptoClient {
}

#[cfg(feature = "p256")]
impl<S: Syscall, E> P256 for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> P256 for ClientImplementation<'pipe, S, E> {}

pub trait P256: CryptoClient {
fn generate_p256_private_key(
Expand Down Expand Up @@ -386,7 +386,7 @@ pub trait P256: CryptoClient {
}

#[cfg(feature = "sha256")]
impl<S: Syscall, E> Sha256 for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> Sha256 for ClientImplementation<'pipe, S, E> {}

pub trait Sha256: CryptoClient {
fn sha256_derive_key(
Expand All @@ -411,7 +411,7 @@ pub trait Sha256: CryptoClient {
}

#[cfg(feature = "tdes")]
impl<S: Syscall, E> Tdes for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> Tdes for ClientImplementation<'pipe, S, E> {}

pub trait Tdes: CryptoClient {
fn decrypt_tdes<'c>(
Expand All @@ -432,7 +432,7 @@ pub trait Tdes: CryptoClient {
}

#[cfg(feature = "totp")]
impl<S: Syscall, E> Totp for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> Totp for ClientImplementation<'pipe, S, E> {}

pub trait Totp: CryptoClient {
fn sign_totp(&mut self, key: KeyId, timestamp: u64) -> ClientResult<'_, reply::Sign, Self> {
Expand All @@ -446,7 +446,7 @@ pub trait Totp: CryptoClient {
}

#[cfg(feature = "x255")]
impl<S: Syscall, E> X255 for ClientImplementation<S, E> {}
impl<'pipe, S: Syscall, E> X255 for ClientImplementation<'pipe, S, E> {}

pub trait X255: CryptoClient {
fn generate_x255_secret_key(
Expand Down
36 changes: 1 addition & 35 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,13 @@
#![allow(non_camel_case_types)]
#![allow(clippy::upper_case_acronyms)]

use littlefs2::consts;

// TODO: this needs to be overridable.
// Should we use the "config crate that can have a replacement patched in" idea?

pub type MAX_APPLICATION_NAME_LENGTH = consts::U256;
pub const MAX_LONG_DATA_LENGTH: usize = 1024;
pub const MAX_MESSAGE_LENGTH: usize = 1024;
pub type MAX_OBJECT_HANDLES = consts::U16;
pub type MAX_LABEL_LENGTH = consts::U256;
pub const MAX_MEDIUM_DATA_LENGTH: usize = 256;
pub type MAX_PATH_LENGTH = consts::U256;
cfg_if::cfg_if! {
if #[cfg(test)] {
pub const MAX_SERVICE_CLIENTS: usize = 6;
} else if #[cfg(feature = "clients-12")] {
pub const MAX_SERVICE_CLIENTS: usize = 12;
} else if #[cfg(feature = "clients-11")] {
pub const MAX_SERVICE_CLIENTS: usize = 11;
} else if #[cfg(feature = "clients-10")] {
pub const MAX_SERVICE_CLIENTS: usize = 10;
} else if #[cfg(feature = "clients-9")] {
pub const MAX_SERVICE_CLIENTS: usize = 9;
} else if #[cfg(feature = "clients-8")] {
pub const MAX_SERVICE_CLIENTS: usize = 8;
} else if #[cfg(feature = "clients-7")] {
pub const MAX_SERVICE_CLIENTS: usize = 7;
} else if #[cfg(feature = "clients-6")] {
pub const MAX_SERVICE_CLIENTS: usize = 6;
} else if #[cfg(feature = "clients-5")] {
pub const MAX_SERVICE_CLIENTS: usize = 5;
} else if #[cfg(feature = "clients-4")] {
pub const MAX_SERVICE_CLIENTS: usize = 4;
} else if #[cfg(feature = "clients-3")] {
pub const MAX_SERVICE_CLIENTS: usize = 3;
} else if #[cfg(feature = "clients-2")] {
pub const MAX_SERVICE_CLIENTS: usize = 2;
} else if #[cfg(feature = "clients-1")] {
pub const MAX_SERVICE_CLIENTS: usize = 1;
}
}

pub const MAX_SHORT_DATA_LENGTH: usize = 128;

pub const MAX_SIGNATURE_LENGTH: usize = 512 * 2;
Expand Down
26 changes: 11 additions & 15 deletions src/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@
// Ignore lint caused by interchange! macro
#![allow(clippy::derive_partial_eq_without_eq)]

use interchange::{Interchange, InterchangeRef, Requester, Responder};
use interchange::{Interchange, Requester, Responder};

use crate::api::{Reply, Request};
use crate::backend::BackendId;
use crate::config;
use crate::error::Error;
use crate::types::Context;

type TrussedInterchangeInner =
Interchange<Request, Result<Reply, Error>, { config::MAX_SERVICE_CLIENTS }>;
static TRUSSED_INTERCHANGE_INNER: TrussedInterchangeInner = Interchange::new();
pub type TrussedInterchange<const MAX_CLIENTS: usize> =
Interchange<Request, Result<Reply, Error>, { MAX_CLIENTS }>;

pub type TrussedInterchange = InterchangeRef<'static, Request, Result<Reply, Error>>;
pub static TRUSSED_INTERCHANGE: TrussedInterchange = TRUSSED_INTERCHANGE_INNER.as_interchange_ref();

pub type TrussedResponder = Responder<'static, Request, Result<Reply, Error>>;
pub type TrussedRequester = Requester<'static, Request, Result<Reply, Error>>;
pub type TrussedResponder<'pipe> = Responder<'pipe, Request, Result<Reply, Error>>;
pub type TrussedRequester<'pipe> = Requester<'pipe, Request, Result<Reply, Error>>;

// pub use interchange::TrussedInterchange;

Expand All @@ -30,8 +25,8 @@ pub type TrussedRequester = Requester<'static, Request, Result<Reply, Error>>;
// https://xenomai.org/documentation/xenomai-2.4/html/api/group__native__queue.html
// https://doc.micrium.com/display/osiiidoc/Using+Message+Queues

pub struct ServiceEndpoint<I: 'static, C> {
pub interchange: TrussedResponder,
pub struct ServiceEndpoint<'pipe, I: 'static, C> {
pub interchange: TrussedResponder<'pipe>,
// service (trusted) has this, not client (untrusted)
// used among other things to namespace cryptographic material
pub ctx: Context<C>,
Expand All @@ -42,15 +37,14 @@ pub struct ServiceEndpoint<I: 'static, C> {

#[cfg(test)]
mod tests {
use super::TrussedInterchange;
use crate::api::{Reply, Request};
use core::mem;

// The following checks are used to ensure that we don’t accidentally increase the interchange
// size. Bumping the size is not a breaking change but should only be done if really
// necessary.

const MAX_SIZE: usize = 2416;
const MAX_SIZE: usize = 2432;

fn assert_size<T>() {
let size = mem::size_of::<T>();
Expand All @@ -69,6 +63,8 @@ mod tests {

#[test]
fn test_interchange_size() {
assert_size::<TrussedInterchange>();
use interchange::Channel;
// The real cost per-client
assert_size::<Channel<Request, Reply>>();
}
}
2 changes: 1 addition & 1 deletion src/serde_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub trait ExtensionClient<E: Extension>: PollClient {
}
}

impl<E, S, I> ExtensionClient<E> for ClientImplementation<S, I>
impl<'pipe, E, S, I> ExtensionClient<E> for ClientImplementation<'pipe, S, I>
where
E: Extension,
S: Syscall,
Expand Down
Loading