Skip to content

Commit 2e7dd7c

Browse files
Merge branch 'trussed-main'
2 parents cb7bd32 + 83cf940 commit 2e7dd7c

40 files changed

+1326
-260
lines changed

.github/workflows/ci.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,26 @@ jobs:
3434
release: "9-2020-q2"
3535

3636
- name: Build
37-
run: cargo build --verbose --target ${{ matrix.target }}
37+
run: cargo build --workspace --target ${{ matrix.target }}
3838

3939
- name: Check all targets without default features
40-
run: cargo check --all-targets --no-default-features
40+
run: cargo check --workspace --all-targets --no-default-features
4141
if: matrix.target == 'x86_64-unknown-linux-gnu'
4242

4343
- name: Check all targets with default features
44-
run: cargo check --all-targets
44+
run: cargo check --workspace --all-targets
4545
if: matrix.target == 'x86_64-unknown-linux-gnu'
4646

4747
- name: Check all features and targets
48-
run: cargo check --all-features --all-targets
48+
run: cargo check --workspace --all-features --all-targets
4949
if: matrix.target == 'x86_64-unknown-linux-gnu'
5050

5151
- name: Run tests
52-
run: cargo test --verbose --features serde-extensions,virt
52+
run: cargo test --features serde-extensions,virt
5353
if: matrix.target == 'x86_64-unknown-linux-gnu'
5454

5555
- name: Check formatting
56-
run: cargo fmt -- --check
56+
run: cargo fmt --all -- --check
5757
if: matrix.target == 'x86_64-unknown-linux-gnu'
5858

5959
- name: Check clippy lints

Cargo.toml

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1+
[workspace]
2+
members = ["derive"]
3+
4+
[workspace.package]
5+
authors = ["Nicolas Stalder <[email protected]>", "Nitrokey GmbH"]
6+
edition = "2021"
7+
homepage = "https://trussed.dev"
8+
license = "Apache-2.0 OR MIT"
9+
110
[package]
211
name = "trussed"
312
version = "0.1.0"
4-
authors = ["Nicolas Stalder <[email protected]>"]
5-
edition = "2021"
6-
homepage = "https://trussed.dev"
713
repository = "https://github.com/trussed-dev/trussed"
8-
license = "Apache-2.0 OR MIT"
914
description = "Modern Cryptographic Firmware"
1015
readme = "README.md"
1116

17+
authors.workspace = true
18+
edition.workspace = true
19+
homepage.workspace = true
20+
license.workspace = true
21+
1222
[dependencies]
1323
# general
1424
bitflags = { version = "2.1" }
@@ -41,7 +51,7 @@ sha2 = { version = "0.10", default-features = false }
4151
cosey = "0.3"
4252
delog = "0.1.0"
4353
cbor-smol = "0.4"
44-
heapless-bytes = { version = "0.3.0", features = ["cbor"] }
54+
heapless-bytes = { version = "0.3.0" }
4555
interchange = "0.3.0"
4656
littlefs2 = "0.4.0"
4757
p256-cortex-m4 = { version = "0.1.0-alpha.6", features = ["prehash", "sec1-signatures"] }
@@ -54,8 +64,7 @@ serial_test = { version = "2" }
5464
entropy = "0.4.0"
5565
once_cell = "1.13.0"
5666
serde_test = "1"
57-
# If this is not enabled, serde_test makes serde_cbor's compilation fail
58-
serde_cbor = { version = "0.11.2", features = ["std"] }
67+
trussed-derive = { path = "derive" }
5968
# Somehow, this is causing a regression.
6069
# rand_core = { version = "0.5", features = ["getrandom"] }
6170

@@ -133,4 +142,4 @@ features = ["serde-extensions", "virt"]
133142
rustdoc-args = ["--cfg", "docsrs"]
134143

135144
[patch.crates-io]
136-
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "ebd27e49ca321089d01d8c9b169c4aeb58ceeeca" }
145+
littlefs2 = { git = "https://github.com/sosthene-nitrokey/littlefs2.git", rev = "2b45a7559ff44260c6dd693e4cb61f54ae5efc53" }

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
clippy:
2-
cargo clippy --all-features --all-targets -- --deny warnings
2+
cargo clippy --workspace --all-features --all-targets -- --deny warnings
33

44
quick-test:
55
cargo test -- --nocapture

derive/Cargo.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "trussed-derive"
3+
version = "0.1.0"
4+
5+
authors.workspace = true
6+
edition.workspace = true
7+
homepage.workspace = true
8+
license.workspace = true
9+
10+
[lib]
11+
proc-macro = true
12+
13+
[dependencies]
14+
proc-macro2 = "1.0.51"
15+
quote = "1.0.23"
16+
syn = "2.0.53"
17+
18+
[dev-dependencies]
19+
serde = { version = "1.0", default-features = false }
20+
trussed = { path = "..", features = ["serde-extensions", "virt"] }

derive/examples/dispatch.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
mod backends {
2+
use trussed::backend::Backend;
3+
4+
#[derive(Default)]
5+
pub struct ABackend;
6+
7+
impl Backend for ABackend {
8+
type Context = ();
9+
}
10+
}
11+
12+
enum Backend {
13+
A,
14+
}
15+
16+
#[derive(Default, trussed_derive::Dispatch)]
17+
#[dispatch(backend_id = "Backend")]
18+
struct Dispatch {
19+
a: backends::ABackend,
20+
}
21+
22+
fn main() {
23+
use trussed::{
24+
backend::BackendId,
25+
client::CryptoClient,
26+
try_syscall,
27+
virt::{self, Ram},
28+
Error,
29+
};
30+
31+
fn run(backends: &'static [BackendId<Backend>], expected: Option<Error>) {
32+
virt::with_platform(Ram::default(), |platform| {
33+
platform.run_client_with_backends(
34+
"test",
35+
Dispatch::default(),
36+
backends,
37+
|mut client| {
38+
assert_eq!(try_syscall!(client.random_bytes(42)).err(), expected);
39+
},
40+
)
41+
});
42+
}
43+
44+
run(&[BackendId::Core], None);
45+
run(
46+
&[BackendId::Custom(Backend::A)],
47+
Some(Error::RequestNotAvailable),
48+
);
49+
}

derive/examples/extension-dispatch.rs

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
use trussed::Error;
2+
3+
mod backends {
4+
use super::extensions::{TestExtension, TestReply, TestRequest};
5+
use trussed::{
6+
backend::Backend, platform::Platform, serde_extensions::ExtensionImpl,
7+
service::ServiceResources, types::CoreContext, Error,
8+
};
9+
10+
#[derive(Default)]
11+
pub struct ABackend;
12+
13+
impl Backend for ABackend {
14+
type Context = ();
15+
}
16+
17+
impl ExtensionImpl<TestExtension> for ABackend {
18+
fn extension_request<P: Platform>(
19+
&mut self,
20+
_core_ctx: &mut CoreContext,
21+
_backend_ctx: &mut Self::Context,
22+
_request: &TestRequest,
23+
_resources: &mut ServiceResources<P>,
24+
) -> Result<TestReply, Error> {
25+
Ok(TestReply)
26+
}
27+
}
28+
29+
#[derive(Default)]
30+
pub struct BBackend;
31+
32+
impl Backend for BBackend {
33+
type Context = ();
34+
}
35+
}
36+
37+
mod extensions {
38+
use serde::{Deserialize, Serialize};
39+
use trussed::{
40+
serde_extensions::{Extension, ExtensionClient, ExtensionResult},
41+
Error,
42+
};
43+
44+
pub struct TestExtension;
45+
46+
impl Extension for TestExtension {
47+
type Request = TestRequest;
48+
type Reply = TestReply;
49+
}
50+
51+
#[derive(Deserialize, Serialize)]
52+
pub struct TestRequest;
53+
54+
#[derive(Deserialize, Serialize)]
55+
pub struct TestReply;
56+
57+
impl TryFrom<TestReply> for () {
58+
type Error = Error;
59+
60+
fn try_from(_reply: TestReply) -> Result<Self, Self::Error> {
61+
Ok(())
62+
}
63+
}
64+
65+
pub trait TestClient {
66+
fn test(&mut self) -> ExtensionResult<'_, TestExtension, (), Self>;
67+
}
68+
69+
impl<C: ExtensionClient<TestExtension>> TestClient for C {
70+
fn test(&mut self) -> ExtensionResult<'_, TestExtension, (), Self> {
71+
self.extension(TestRequest)
72+
}
73+
}
74+
75+
pub struct SampleExtension;
76+
77+
impl Extension for SampleExtension {
78+
type Request = SampleRequest;
79+
type Reply = SampleReply;
80+
}
81+
82+
#[derive(Deserialize, Serialize)]
83+
pub struct SampleRequest;
84+
85+
#[derive(Deserialize, Serialize)]
86+
pub struct SampleReply;
87+
}
88+
89+
enum Backend {
90+
A,
91+
B,
92+
}
93+
94+
#[derive(trussed_derive::ExtensionId)]
95+
enum Extension {
96+
Test = 0,
97+
Sample = 1,
98+
}
99+
100+
#[derive(Default, trussed_derive::ExtensionDispatch)]
101+
#[dispatch(backend_id = "Backend", extension_id = "Extension")]
102+
#[extensions(
103+
Test = "extensions::TestExtension",
104+
Sample = "extensions::SampleExtension"
105+
)]
106+
struct Dispatch {
107+
#[extensions("Test")]
108+
a: backends::ABackend,
109+
b: backends::BBackend,
110+
}
111+
112+
fn main() {
113+
use extensions::TestClient;
114+
use trussed::{
115+
backend::BackendId,
116+
try_syscall,
117+
virt::{self, Ram},
118+
};
119+
120+
fn run(backends: &'static [BackendId<Backend>], expected: Option<Error>) {
121+
virt::with_platform(Ram::default(), |platform| {
122+
platform.run_client_with_backends(
123+
"test",
124+
Dispatch::default(),
125+
backends,
126+
|mut client| {
127+
assert_eq!(try_syscall!(client.test()).err(), expected);
128+
},
129+
)
130+
});
131+
}
132+
133+
run(&[BackendId::Core], Some(Error::RequestNotAvailable));
134+
run(
135+
&[BackendId::Custom(Backend::B)],
136+
Some(Error::RequestNotAvailable),
137+
);
138+
run(&[BackendId::Custom(Backend::A)], None);
139+
}

0 commit comments

Comments
 (0)