Skip to content

Commit

Permalink
Move manage::ManageExtension into trussed-se050-manage crate
Browse files Browse the repository at this point in the history
Similar to the changes in trussed-staging#19 [0], this patch moves the
extension that was previously defined in the backend crate into a
separate extension crate.

[0] trussed-dev/trussed-staging#19
  • Loading branch information
robin-nitrokey committed Mar 15, 2024
1 parent d2b6931 commit 1414a89
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 138 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Use extension crates `trussed-manage` and `trussed-wrap-key-to-file` instead
of backend crate `trussed-staging`, see [trussed-staging#19][].
- Move `manage::ManageExtension` into `trussed-se050-manage` crate.

[trussed-staging#19]: https://github.com/trussed-dev/trussed-staging/pull/19

Expand Down
26 changes: 22 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
[workspace]
members = ["extensions/se050-manage"]

[workspace.package]
authors = ["Nitrokey GmbH <[email protected]>"]
edition = "2021"
repository = "https://github.com/trussed-dev/trussed-staging"
license = "Apache-2.0 OR MIT"

[package]
name = "trussed-se050-backend"
version = "0.2.0"
edition = "2021"
authors.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[workspace.dependencies]
serde = { version = "1.0.185", default-features = false, features = ["derive"] }
trussed = { version = "0.1.0", features = ["serde-extensions"] }

[dependencies]
serde.workspace = true
trussed.workspace = true

se05x = { version = "0.1.1", features = ["serde", "builder"] }
trussed = { version = "0.1.0", features = ["serde-extensions"] }
trussed-auth = "0.2.2"
trussed-manage = "0.1.0"
trussed-se050-manage = "0.1.0"
trussed-wrap-key-to-file = "0.1.0"
delog = "0.1.6"
embedded-hal = "0.2.7"
Expand All @@ -19,7 +36,6 @@ hex-literal = "0.4.1"
serde-byte-array = "0.1.2"
iso7816 = "0.1.1"
hmac = "0.12.1"
serde = { version = "1.0.185", default-features = false, features = ["derive"] }
rand = { version = "0.8.5", default-features = false }
littlefs2 = "0.4.0"
cbor-smol = "0.4.0"
Expand All @@ -39,6 +55,8 @@ trussed-manage = { git = "https://github.com/trussed-dev/trussed-staging.git", t
trussed-rsa-alloc = { git = "https://github.com/Nitrokey/trussed-rsa-backend.git", rev = "2088e2f8a8d706276c1559717b4c6b6d4f270253" }
trussed-wrap-key-to-file = { git = "https://github.com/trussed-dev/trussed-staging.git", tag = "wrap-key-to-file-v0.1.0" }

trussed-se050-manage = { path = "extensions/se050-manage" }

[features]
default = ["log-all"]

Expand Down
11 changes: 11 additions & 0 deletions extensions/se050-manage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "trussed-se050-manage"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true

[dependencies]
serde.workspace = true
trussed.workspace = true
137 changes: 137 additions & 0 deletions extensions/se050-manage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
use serde::{Deserialize, Serialize};
use trussed::{
serde_extensions::{Extension, ExtensionClient, ExtensionResult},
types::Bytes,
Error,
};

#[derive(Debug, Default)]
pub struct ManageExtension;

/// Request information regarding the SE050
#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
pub struct InfoRequest;

/// Test SE050 functionality
///
/// This is now a placeholder for the previous test. It is kept to return available space on the SE050
#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
pub struct TestSe050Request;

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Deserialize, Serialize)]
pub enum ManageRequest {
Info(InfoRequest),
TestSe050(TestSe050Request),
}

impl TryFrom<ManageRequest> for InfoRequest {
type Error = Error;
fn try_from(request: ManageRequest) -> Result<Self, Self::Error> {
match request {
ManageRequest::Info(request) => Ok(request),
_ => Err(Error::InternalError),
}
}
}

impl From<InfoRequest> for ManageRequest {
fn from(request: InfoRequest) -> Self {
Self::Info(request)
}
}

impl TryFrom<ManageRequest> for TestSe050Request {
type Error = Error;
fn try_from(request: ManageRequest) -> Result<Self, Self::Error> {
match request {
ManageRequest::TestSe050(request) => Ok(request),
_ => Err(Error::InternalError),
}
}
}

impl From<TestSe050Request> for ManageRequest {
fn from(request: TestSe050Request) -> Self {
Self::TestSe050(request)
}
}

#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
pub struct InfoReply {
pub major: u8,
pub minor: u8,
pub patch: u8,
pub sb_major: u8,
pub sb_minor: u8,
pub persistent: u16,
pub transient_deselect: u16,
pub transient_reset: u16,
}

#[derive(Debug, Deserialize, Serialize)]
pub enum ManageReply {
Info(InfoReply),
TestSe050(TestSe050Reply),
}

impl TryFrom<ManageReply> for InfoReply {
type Error = Error;
fn try_from(request: ManageReply) -> Result<Self, Self::Error> {
match request {
ManageReply::Info(request) => Ok(request),
_ => Err(Error::InternalError),
}
}
}

impl From<InfoReply> for ManageReply {
fn from(request: InfoReply) -> Self {
Self::Info(request)
}
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct TestSe050Reply {
pub reply: Bytes<1024>,
}

impl TryFrom<ManageReply> for TestSe050Reply {
type Error = Error;
fn try_from(request: ManageReply) -> Result<Self, Self::Error> {
match request {
ManageReply::TestSe050(request) => Ok(request),
_ => Err(Error::InternalError),
}
}
}

impl From<TestSe050Reply> for ManageReply {
fn from(request: TestSe050Reply) -> Self {
Self::TestSe050(request)
}
}

impl Extension for ManageExtension {
type Request = ManageRequest;
type Reply = ManageReply;
}

pub type ManageResult<'a, R, C> = ExtensionResult<'a, ManageExtension, R, C>;

pub trait ManageClient: ExtensionClient<ManageExtension> {
/// Get info on the SE050
fn get_info(&mut self) -> ManageResult<'_, InfoReply, Self> {
self.extension(InfoRequest)
}

/// Test the se050 device and driver
///
/// This will fake the results of the tests from v0.1.0-test-driver for compatibility but
/// return correct metadata header to be shown in the test result
fn test_se050(&mut self) -> ManageResult<'_, TestSe050Reply, Self> {
self.extension(TestSe050Request)
}
}

impl<C: ExtensionClient<ManageExtension>> ManageClient for C {}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use trussed_auth_impl::{AuthContext, HardwareKey};
mod staging;

mod core_api;
pub mod manage;
mod manage;
pub mod namespacing;

/// Need overhead for TLV + SW bytes
Expand Down
137 changes: 4 additions & 133 deletions src/manage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,129 +7,19 @@ use se05x::{
},
t1::I2CForT1,
};
use serde::{Deserialize, Serialize};
use trussed::{
serde_extensions::{Extension, ExtensionClient, ExtensionImpl, ExtensionResult},
serde_extensions::{Extension, ExtensionImpl},
service::ServiceResources,
types::Bytes,
types::CoreContext,
Error,
};
use trussed_se050_manage::{
InfoReply, InfoRequest, ManageExtension, ManageRequest, TestSe050Reply,
};

use crate::Se050Backend;

#[derive(Debug, Default)]
pub struct ManageExtension;

/// Request information regarding the SE050
#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
pub struct InfoRequest;

/// Test SE050 functionality
///
/// This is now a placeholder for the previous test. It is kept to return available space on the SE050
#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
pub struct TestSe050Request;

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Deserialize, Serialize)]
pub enum ManageRequest {
Info(InfoRequest),
TestSe050(TestSe050Request),
}

impl TryFrom<ManageRequest> for InfoRequest {
type Error = Error;
fn try_from(request: ManageRequest) -> Result<Self, Self::Error> {
match request {
ManageRequest::Info(request) => Ok(request),
_ => Err(Error::InternalError),
}
}
}

impl From<InfoRequest> for ManageRequest {
fn from(request: InfoRequest) -> Self {
Self::Info(request)
}
}

impl TryFrom<ManageRequest> for TestSe050Request {
type Error = Error;
fn try_from(request: ManageRequest) -> Result<Self, Self::Error> {
match request {
ManageRequest::TestSe050(request) => Ok(request),
_ => Err(Error::InternalError),
}
}
}

impl From<TestSe050Request> for ManageRequest {
fn from(request: TestSe050Request) -> Self {
Self::TestSe050(request)
}
}

#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
pub struct InfoReply {
pub major: u8,
pub minor: u8,
pub patch: u8,
pub sb_major: u8,
pub sb_minor: u8,
pub persistent: u16,
pub transient_deselect: u16,
pub transient_reset: u16,
}

#[derive(Debug, Deserialize, Serialize)]
pub enum ManageReply {
Info(InfoReply),
TestSe050(TestSe050Reply),
}

impl TryFrom<ManageReply> for InfoReply {
type Error = Error;
fn try_from(request: ManageReply) -> Result<Self, Self::Error> {
match request {
ManageReply::Info(request) => Ok(request),
_ => Err(Error::InternalError),
}
}
}

impl From<InfoReply> for ManageReply {
fn from(request: InfoReply) -> Self {
Self::Info(request)
}
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct TestSe050Reply {
pub reply: Bytes<1024>,
}

impl TryFrom<ManageReply> for TestSe050Reply {
type Error = Error;
fn try_from(request: ManageReply) -> Result<Self, Self::Error> {
match request {
ManageReply::TestSe050(request) => Ok(request),
_ => Err(Error::InternalError),
}
}
}

impl From<TestSe050Reply> for ManageReply {
fn from(request: TestSe050Reply) -> Self {
Self::TestSe050(request)
}
}

impl Extension for ManageExtension {
type Request = ManageRequest;
type Reply = ManageReply;
}

impl<Twi: I2CForT1, D: DelayUs<u32>> ExtensionImpl<ManageExtension> for Se050Backend<Twi, D> {
fn extension_request<P: trussed::Platform>(
&mut self,
Expand Down Expand Up @@ -264,22 +154,3 @@ impl<Twi: I2CForT1, D: DelayUs<u32>> ExtensionImpl<ManageExtension> for Se050Bac
}
}
}

type ManageResult<'a, R, C> = ExtensionResult<'a, ManageExtension, R, C>;

pub trait ManageClient: ExtensionClient<ManageExtension> {
/// Get info on the SE050
fn get_info(&mut self) -> ManageResult<'_, InfoReply, Self> {
self.extension(InfoRequest)
}

/// Test the se050 device and driver
///
/// This will fake the results of the tests from v0.1.0-test-driver for compatibility but
/// return correct metadata header to be shown in the test result
fn test_se050(&mut self) -> ManageResult<'_, TestSe050Reply, Self> {
self.extension(TestSe050Request)
}
}

impl<C: ExtensionClient<ManageExtension>> ManageClient for C {}

0 comments on commit 1414a89

Please sign in to comment.