Update trussed to use the simplified Store trait

This commit is contained in:
Robin Krahl
2025-03-05 14:05:50 +01:00
parent 7f305b8db3
commit 7922d67e96
8 changed files with 68 additions and 99 deletions
+1 -1
View File
@@ -83,7 +83,7 @@ log-warn = []
log-error = []
[patch.crates-io]
trussed = { git = "https://github.com/trussed-dev/trussed.git", rev = "5003249c3187dca841f83551ba625921611a5ace" }
trussed = { git = "https://github.com/trussed-dev/trussed.git", rev = "ac106d63ab5e19021b0e37f0efb4313f8168a110" }
trussed-chunked = { path = "extensions/chunked" }
trussed-hkdf = { path = "extensions/hkdf" }
+7 -50
View File
@@ -212,20 +212,18 @@ impl ExtensionDispatch for Dispatcher {
}
}
use std::path::PathBuf;
use trussed::{
backend::{Backend, BackendId},
serde_extensions::*,
virt::{self, Filesystem, Ram, StoreProvider},
virt::{self, StoreConfig},
Error, Platform,
};
pub type Client<'a, D = Dispatcher> = virt::Client<'a, D>;
pub fn with_client<S, R, F>(store: S, client_id: &str, f: F) -> R
pub fn with_client<R, F>(store: StoreConfig, client_id: &str, f: F) -> R
where
F: FnOnce(Client) -> R,
S: StoreProvider,
{
virt::with_platform(store, |platform| {
platform.run_client_with_backends(
@@ -241,15 +239,14 @@ where
}
#[cfg(feature = "manage")]
pub fn with_client_and_preserve<S, R, F>(
store: S,
pub fn with_client_and_preserve<R, F>(
store: StoreConfig,
client_id: &str,
f: F,
should_preserve_file: fn(&Path, location: Location) -> bool,
) -> R
where
F: FnOnce(Client) -> R,
S: StoreProvider,
{
let mut dispatcher = Dispatcher::default();
dispatcher.backend.manage.should_preserve_file = should_preserve_file;
@@ -268,15 +265,14 @@ where
}
#[cfg(feature = "manage")]
pub fn with_clients_and_preserve<S, R, F, const N: usize>(
store: S,
pub fn with_clients_and_preserve<R, F, const N: usize>(
store: StoreConfig,
client_ids: [&str; N],
f: F,
should_preserve_file: fn(&Path, location: Location) -> bool,
f: F,
) -> R
where
F: FnOnce([Client; N]) -> R,
S: StoreProvider,
{
let mut dispatcher = Dispatcher::default();
dispatcher.backend.manage.should_preserve_file = should_preserve_file;
@@ -295,42 +291,3 @@ where
platform.run_clients_with_backends(clients_backend, dispatcher, f)
})
}
pub fn with_fs_client<P, R, F>(internal: P, client_id: &str, f: F) -> R
where
F: FnOnce(Client) -> R,
P: Into<PathBuf>,
{
with_client(Filesystem::new(internal), client_id, f)
}
pub fn with_ram_client<R, F>(client_id: &str, f: F) -> R
where
F: FnOnce(Client) -> R,
{
with_client(Ram::default(), client_id, f)
}
#[cfg(feature = "manage")]
pub fn with_ram_client_and_preserve<R, F>(
client_id: &str,
should_preserve_file: fn(&Path, location: Location) -> bool,
f: F,
) -> R
where
F: FnOnce(Client) -> R,
{
with_client_and_preserve(Ram::default(), client_id, f, should_preserve_file)
}
#[cfg(feature = "manage")]
pub fn with_ram_clients_and_preserve<R, F, const N: usize>(
client_ids: [&str; N],
should_preserve_file: fn(&Path, location: Location) -> bool,
f: F,
) -> R
where
F: FnOnce([Client; N]) -> R,
{
with_clients_and_preserve(Ram::default(), client_ids, f, should_preserve_file)
}
+7 -5
View File
@@ -4,12 +4,14 @@
#![cfg(all(feature = "virt", feature = "chunked"))]
use littlefs2_core::{path, PathBuf};
use trussed::{client::FilesystemClient, syscall, try_syscall, types::Location, Bytes};
use trussed::{
client::FilesystemClient, syscall, try_syscall, types::Location, virt::StoreConfig, Bytes,
};
use trussed_chunked::{utils, ChunkedClient};
use trussed_staging::virt::with_ram_client;
use trussed_staging::virt::with_client;
fn test_write_all(location: Location) {
with_ram_client("test chunked", |mut client| {
with_client(StoreConfig::ram(), "test chunked", |mut client| {
let path = PathBuf::from(path!("foo"));
utils::write_all(&mut client, location, path.clone(), &[48; 1234], None, None).unwrap();
@@ -21,7 +23,7 @@ fn test_write_all(location: Location) {
}
fn test_write_all_small(location: Location) {
with_ram_client("test chunked", |mut client| {
with_client(StoreConfig::ram(), "test chunked", |mut client| {
let path = PathBuf::from(path!("foo2"));
utils::write_all(&mut client, location, path.clone(), &[48; 1023], None, None).unwrap();
@@ -50,7 +52,7 @@ fn write_all_internal() {
#[test]
fn filesystem() {
with_ram_client("chunked-tests", |mut client| {
with_client(StoreConfig::ram(), "chunked-tests", |mut client| {
assert!(syscall!(
client.entry_metadata(Location::Internal, PathBuf::from(path!("test_file")))
)
+6 -6
View File
@@ -6,17 +6,17 @@
use littlefs2_core::{path, PathBuf};
use serde_byte_array::ByteArray;
use trussed::{
client::CryptoClient, client::FilesystemClient, syscall, try_syscall, types::Location, Bytes,
Error,
client::CryptoClient, client::FilesystemClient, syscall, try_syscall, types::Location,
virt::StoreConfig, Bytes, Error,
};
use trussed_chunked::{
utils::{self, EncryptionData},
ChunkedClient,
};
use trussed_staging::virt::with_ram_client;
use trussed_staging::virt::with_client;
fn test_write_all(location: Location) {
with_ram_client("test chunked", |mut client| {
with_client(StoreConfig::ram(), "test chunked", |mut client| {
let key = syscall!(client.generate_secret_key(32, Location::Volatile)).key;
let path = PathBuf::from(path!("foo"));
utils::write_all(
@@ -38,7 +38,7 @@ fn test_write_all(location: Location) {
}
fn test_write_all_small(location: Location) {
with_ram_client("test chunked", |mut client| {
with_client(StoreConfig::ram(), "test chunked", |mut client| {
let key = syscall!(client.generate_secret_key(32, Location::Volatile)).key;
let path = PathBuf::from(path!("foo2"));
utils::write_all(
@@ -77,7 +77,7 @@ fn write_all_internal() {
#[test]
fn encrypted_filesystem() {
with_ram_client("chunked-tests", |mut client| {
with_client(StoreConfig::ram(), "chunked-tests", |mut client| {
let path = PathBuf::from(path!("test_file"));
let key = syscall!(client.generate_secret_key(32, Location::Volatile)).key;
+2 -1
View File
@@ -11,6 +11,7 @@ use trussed::{
client::HmacSha256,
syscall,
types::{Bytes, Location},
virt::StoreConfig,
};
use trussed_hkdf::{HkdfClient, KeyOrData::*};
use trussed_staging::virt;
@@ -27,7 +28,7 @@ fn hkdf() {
ref_hkdf.expand(INFO, &mut okm).unwrap();
let mut mac = Hmac::<Sha256>::new_from_slice(&okm).unwrap();
mac.update(MSG);
virt::with_ram_client("hkdf_test", |mut client| {
virt::with_client(StoreConfig::ram(), "hkdf_test", |mut client| {
let prk = syscall!(client.hkdf_extract(
Data(Bytes::from_slice(IKM).unwrap()),
Some(Data(Bytes::from_slice(SALT).unwrap())),
+35 -30
View File
@@ -8,6 +8,7 @@ use trussed::client::{CryptoClient, X255};
use trussed::{
syscall,
types::{Bytes, KeyId, Location, Mechanism, SignatureSerialization},
virt::StoreConfig,
};
use trussed_hpke::HpkeClient;
@@ -35,7 +36,7 @@ fn assert_symkey_eq<C: CryptoClient>(this: KeyId, other: KeyId, client: &mut C)
#[test]
fn hpke_message() {
virt::with_ram_client("hpke_test_message", |mut client| {
virt::with_client(StoreConfig::ram(), "hpke_test_message", |mut client| {
let secret_key = syscall!(client.generate_x255_secret_key(Location::Volatile)).key;
let public_key =
syscall!(client.derive_x255_public_key(secret_key, Location::Volatile)).key;
@@ -61,7 +62,7 @@ fn hpke_message() {
#[test]
fn hpke_wrap_key() {
virt::with_ram_client("hpke_test_wrap_key", |mut client| {
virt::with_client(StoreConfig::ram(), "hpke_test_wrap_key", |mut client| {
let secret_key = syscall!(client.generate_x255_secret_key(Location::Volatile)).key;
let public_key =
syscall!(client.derive_x255_public_key(secret_key, Location::Volatile)).key;
@@ -84,36 +85,40 @@ fn hpke_wrap_key() {
#[test]
fn hpke_wrap_key_to_file() {
virt::with_ram_client("hpke_test_wrap_key_to_file", |mut client| {
let secret_key = syscall!(client.generate_x255_secret_key(Location::Volatile)).key;
let public_key =
syscall!(client.derive_x255_public_key(secret_key, Location::Volatile)).key;
virt::with_client(
StoreConfig::ram(),
"hpke_test_wrap_key_to_file",
|mut client| {
let secret_key = syscall!(client.generate_x255_secret_key(Location::Volatile)).key;
let public_key =
syscall!(client.derive_x255_public_key(secret_key, Location::Volatile)).key;
let key_to_wrap = syscall!(client.generate_secret_key(32, Location::Volatile)).key;
let key_to_wrap = syscall!(client.generate_secret_key(32, Location::Volatile)).key;
let path = path!("WRAPPED_KEY");
let aad = Bytes::from_slice(b"AAD").unwrap();
let info = Bytes::from_slice(b"INFO").unwrap();
syscall!(client.hpke_seal_key_to_file(
path.into(),
Location::Volatile,
public_key,
key_to_wrap,
aad.clone(),
info.clone()
));
let path = path!("WRAPPED_KEY");
let aad = Bytes::from_slice(b"AAD").unwrap();
let info = Bytes::from_slice(b"INFO").unwrap();
syscall!(client.hpke_seal_key_to_file(
path.into(),
Location::Volatile,
public_key,
key_to_wrap,
aad.clone(),
info.clone()
));
let unwrapped = syscall!(client.hpke_open_key_from_file(
secret_key,
path.into(),
Location::Volatile,
Location::Volatile,
aad,
info
))
.key;
assert_ne!(unwrapped, key_to_wrap);
let unwrapped = syscall!(client.hpke_open_key_from_file(
secret_key,
path.into(),
Location::Volatile,
Location::Volatile,
aad,
info
))
.key;
assert_ne!(unwrapped, key_to_wrap);
assert_symkey_eq(key_to_wrap, unwrapped, &mut client);
})
assert_symkey_eq(key_to_wrap, unwrapped, &mut client);
},
)
}
+6 -3
View File
@@ -7,8 +7,9 @@ use littlefs2_core::path;
use trussed::client::FilesystemClient;
use trussed::syscall;
use trussed::types::{Bytes, Location, Path};
use trussed::virt::StoreConfig;
use trussed_manage::ManageClient;
use trussed_staging::virt::with_ram_clients_and_preserve;
use trussed_staging::virt::with_clients_and_preserve;
fn should_preserve(path: &Path, location: Location) -> bool {
(location == Location::Internal && path == path!("/client1/dat/to_save_internal"))
@@ -18,7 +19,8 @@ fn should_preserve(path: &Path, location: Location) -> bool {
#[test]
fn device_factory_reset() {
with_ram_clients_and_preserve(
with_clients_and_preserve(
StoreConfig::ram(),
["client1", "client2"],
should_preserve,
|[mut client1, mut client2]| {
@@ -130,7 +132,8 @@ fn device_factory_reset() {
#[test]
fn client_factory_reset() {
with_ram_clients_and_preserve(
with_clients_and_preserve(
StoreConfig::ram(),
["client1", "client2"],
should_preserve,
|[mut client1, mut client2]| {
+4 -3
View File
@@ -10,8 +10,9 @@ use trussed::types::{
KeyId, KeySerialization, Location::*, Mechanism, PathBuf, SignatureSerialization,
StorageAttributes,
};
use trussed::virt::StoreConfig;
use trussed_staging::virt::with_ram_client;
use trussed_staging::virt::with_client;
use trussed_wrap_key_to_file::WrapKeyToFileClient;
@@ -35,7 +36,7 @@ fn assert_key_eq(key1: KeyId, key2: KeyId, client: &mut impl CryptoClient) {
#[test]
fn chacha_wrapkey() {
with_ram_client("staging-tests", |mut client| {
with_client(StoreConfig::ram(), "staging-tests", |mut client| {
let client = &mut client;
// Way to get a compatible Symmetric32 key
let key = syscall!(client.unsafe_inject_key(
@@ -91,7 +92,7 @@ fn chacha_wrapkey() {
#[test]
fn chacha_wraptofile() {
with_ram_client("staging-tests", |mut client| {
with_client(StoreConfig::ram(), "staging-tests", |mut client| {
let client = &mut client;
// Way to get a compatible Symmetric32 key
let key = syscall!(client.unsafe_inject_key(