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

Add filesystem info extension and backend implementation #27

Merged
merged 6 commits into from
Aug 1, 2024
Merged
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
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: CC0-1.0

[workspace]
members = ["extensions/chunked", "extensions/hkdf", "extensions/manage", "extensions/wrap-key-to-file"]
members = ["extensions/chunked", "extensions/fs-info", "extensions/hkdf", "extensions/manage", "extensions/wrap-key-to-file"]

[workspace.package]
authors = ["Nitrokey GmbH <info@nitrokey.com>"]
Expand Down Expand Up @@ -40,6 +40,7 @@ trussed-chunked = { version = "0.1.0", optional = true }
trussed-hkdf = { version = "0.2.0", optional = true }
trussed-manage = { version = "0.1.0", optional = true }
trussed-wrap-key-to-file = { version = "0.1.0", optional = true }
trussed-fs-info = { version = "0.1.0", optional = true }

[dev-dependencies]
hex-literal = "0.3.4"
Expand All @@ -53,6 +54,7 @@ chunked = ["trussed-chunked", "chacha20poly1305/stream"]
hkdf = ["trussed-hkdf", "dep:hkdf", "dep:sha2"]
manage = ["trussed-manage"]
wrap-key-to-file = ["chacha20poly1305", "trussed-wrap-key-to-file"]
fs-info = ["trussed-fs-info"]

virt = ["std", "trussed/virt"]
std = []
Expand All @@ -66,10 +68,11 @@ log-warn = []
log-error = []

[patch.crates-io]
trussed = { git = "https://github.com/trussed-dev/trussed.git", rev = "45ed62ba97d994aa6e05e2b61cea013ef131caa4" }
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "ebd27e49ca321089d01d8c9b169c4aeb58ceeeca" }
trussed = { git = "https://github.com/trussed-dev/trussed.git", rev = "a055e4f79a10122c8c0c882161442e6e02f0c5c6" }
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "960e57d9fc0d209308c8e15dc26252bbe1ff6ba8" }

trussed-chunked = { path = "extensions/chunked" }
trussed-hkdf = { path = "extensions/hkdf" }
trussed-manage = { path = "extensions/manage" }
trussed-wrap-key-to-file = { path = "extensions/wrap-key-to-file" }
trussed-fs-info= { path = "extensions/fs-info" }
15 changes: 15 additions & 0 deletions extensions/fs-info/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (C) Nitrokey GmbH
# SPDX-License-Identifier: CC0-1.0

[package]
name = "trussed-fs-info"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true

[dependencies]
serde.workspace = true
serde-byte-array.workspace = true
trussed.workspace = true
89 changes: 89 additions & 0 deletions extensions/fs-info/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (C) Nitrokey GmbH
// SPDX-License-Identifier: CC0-1.0

#![no_std]
#![warn(non_ascii_idents, trivial_casts, unused, unused_qualifications)]
#![deny(unsafe_code)]

use serde::{Deserialize, Serialize};
use trussed::{
serde_extensions::{Extension, ExtensionClient, ExtensionResult},
types::Location,
Error,
};

pub struct FsInfoExtension;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum FsInfoExtensionRequest {
FsInfo(FsInfoRequest),
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct FsInfoRequest {
pub location: Location,
}

impl From<FsInfoRequest> for FsInfoExtensionRequest {
fn from(value: FsInfoRequest) -> Self {
Self::FsInfo(value)
}
}

impl TryFrom<FsInfoExtensionRequest> for FsInfoRequest {
type Error = Error;

fn try_from(value: FsInfoExtensionRequest) -> Result<Self, Self::Error> {
match value {
FsInfoExtensionRequest::FsInfo(v) => Ok(v),
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum FsInfoExtensionReply {
FsInfo(FsInfoReply),
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct FsInfoReply {
pub block_info: Option<BlockInfo>,
pub available_space: usize,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct BlockInfo {
pub size: usize,
pub total: usize,
pub available: usize,
}

impl From<FsInfoReply> for FsInfoExtensionReply {
fn from(value: FsInfoReply) -> Self {
Self::FsInfo(value)
}
}

impl TryFrom<FsInfoExtensionReply> for FsInfoReply {
type Error = Error;

fn try_from(value: FsInfoExtensionReply) -> Result<Self, Self::Error> {
match value {
FsInfoExtensionReply::FsInfo(v) => Ok(v),
}
}
}

impl Extension for FsInfoExtension {
type Request = FsInfoExtensionRequest;
type Reply = FsInfoExtensionReply;
}

pub type FsInfoResult<'a, R, C> = ExtensionResult<'a, FsInfoExtension, R, C>;

pub trait FsInfoClient: ExtensionClient<FsInfoExtension> {
fn fs_info(&mut self, location: Location) -> FsInfoResult<'_, FsInfoReply, Self> {
self.extension(FsInfoRequest { location })
}
}
impl<C: ExtensionClient<FsInfoExtension>> FsInfoClient for C {}
37 changes: 37 additions & 0 deletions src/fs_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) Nitrokey GmbH
// SPDX-License-Identifier: CC0-1.0

use trussed::{
platform::Store, serde_extensions::ExtensionImpl, service::ServiceResources,
types::CoreContext, Error, Platform,
};
use trussed_fs_info::{
BlockInfo, FsInfoExtension, FsInfoExtensionReply, FsInfoExtensionRequest, FsInfoReply,
};

impl ExtensionImpl<FsInfoExtension> for super::StagingBackend {
fn extension_request<P: Platform>(
&mut self,
_core_ctx: &mut CoreContext,
_backend_ctx: &mut Self::Context,
request: &FsInfoExtensionRequest,
resources: &mut ServiceResources<P>,
) -> Result<FsInfoExtensionReply, Error> {
match request {
FsInfoExtensionRequest::FsInfo(req) => {
let platform = resources.platform();
let store = platform.store();
let fs = store.fs(req.location);
Ok(FsInfoReply {
block_info: Some(BlockInfo {
total: fs.total_blocks(),
available: fs.available_blocks().map_err(|_| Error::InternalError)?,
size: fs.total_space() / fs.total_blocks(),
}),
available_space: fs.available_space().map_err(|_| Error::InternalError)?,
}
.into())
}
}
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub mod virt;
#[cfg(feature = "wrap-key-to-file")]
mod wrap_key_to_file;

#[cfg(feature = "fs-info")]
mod fs_info;

#[cfg(feature = "chunked")]
mod chunked;

Expand Down
22 changes: 22 additions & 0 deletions src/virt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use trussed::types::{Location, Path};

#[cfg(feature = "chunked")]
use trussed_chunked::ChunkedExtension;
#[cfg(feature = "fs-info")]
use trussed_fs_info::FsInfoExtension;
#[cfg(feature = "hkdf")]
use trussed_hkdf::HkdfExtension;
#[cfg(feature = "manage")]
Expand Down Expand Up @@ -37,6 +39,8 @@ pub enum ExtensionIds {
Manage,
#[cfg(feature = "wrap-key-to-file")]
WrapKeyToFile,
#[cfg(feature = "fs-info")]
FsInfo,
}

#[cfg(feature = "chunked")]
Expand All @@ -63,6 +67,12 @@ impl ExtensionId<WrapKeyToFileExtension> for Dispatcher {
const ID: ExtensionIds = ExtensionIds::WrapKeyToFile;
}

#[cfg(feature = "fs-info")]
impl ExtensionId<FsInfoExtension> for Dispatcher {
type Id = ExtensionIds;
const ID: ExtensionIds = ExtensionIds::FsInfo;
}

impl From<ExtensionIds> for u8 {
fn from(value: ExtensionIds) -> Self {
match value {
Expand All @@ -74,6 +84,8 @@ impl From<ExtensionIds> for u8 {
ExtensionIds::Manage => 2,
#[cfg(feature = "wrap-key-to-file")]
ExtensionIds::WrapKeyToFile => 3,
#[cfg(feature = "fs-info")]
ExtensionIds::FsInfo => 4,
}
}
}
Expand All @@ -90,6 +102,8 @@ impl TryFrom<u8> for ExtensionIds {
2 => Ok(Self::Manage),
#[cfg(feature = "wrap-key-to-file")]
3 => Ok(Self::WrapKeyToFile),
#[cfg(feature = "fs-info")]
4 => Ok(Self::FsInfo),
_ => Err(Error::FunctionNotSupported),
}
}
Expand Down Expand Up @@ -164,6 +178,14 @@ impl ExtensionDispatch for Dispatcher {
request,
resources,
),
#[cfg(feature = "fs-info")]
ExtensionIds::FsInfo => ExtensionImpl::<FsInfoExtension>::extension_request_serialized(
&mut self.backend,
&mut ctx.core,
&mut ctx.backends,
request,
resources,
),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/wrap_key_to_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn unwrap_key_from_file(
let nonce = (&*nonce).try_into().unwrap();
let tag = (&*tag).try_into().unwrap();

let key = keystore.load_key(key::Secrecy::Secret, Some(KIND), &request.key)?;
let key = keystore.load_key(Secrecy::Secret, Some(KIND), &request.key)?;
let chachakey: [u8; KEY_LEN] = (&*key.material).try_into().unwrap();
let mut aead = ChaCha8Poly1305::new(&GenericArray::clone_from_slice(&chachakey));
if aead
Expand Down
8 changes: 6 additions & 2 deletions tests/wrap_key_to_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,28 @@ fn chacha_wrapkey() {
let key2 = syscall!(client.generate_secret_key(32, Volatile)).key;

let wrapped =
syscall!(client.wrap_key(Mechanism::Chacha8Poly1305, key, key2, &[])).wrapped_key;
syscall!(client.wrap_key(Mechanism::Chacha8Poly1305, key, key2, &[], None)).wrapped_key;
let unwrapped = syscall!(client.unwrap_key(
Mechanism::Chacha8Poly1305,
key,
wrapped,
&[],
&[],
StorageAttributes::new()
))
.key
.unwrap();
assert_key_eq(key2, unwrapped, client);

let wrapped_ad =
syscall!(client.wrap_key(Mechanism::Chacha8Poly1305, key, key2, b"abc")).wrapped_key;
syscall!(client.wrap_key(Mechanism::Chacha8Poly1305, key, key2, b"abc", None))
.wrapped_key;
assert!(syscall!(client.unwrap_key(
Mechanism::Chacha8Poly1305,
key,
wrapped_ad.clone(),
&[],
&[],
StorageAttributes::new()
))
.key
Expand All @@ -76,6 +79,7 @@ fn chacha_wrapkey() {
key,
wrapped_ad,
b"abc",
&[],
StorageAttributes::new()
))
.key
Expand Down