From 7922d67e9637a87e5625aaff9e5111f0d4ec0346 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 4 Mar 2025 17:40:35 +0100 Subject: [PATCH] Update trussed to use the simplified Store trait --- Cargo.toml | 2 +- src/virt.rs | 57 ++++----------------------------- tests/chunked.rs | 12 ++++--- tests/encrypted-chunked.rs | 12 +++---- tests/hkdf.rs | 3 +- tests/hpke.rs | 65 ++++++++++++++++++++------------------ tests/manage.rs | 9 ++++-- tests/wrap_key_to_file.rs | 7 ++-- 8 files changed, 68 insertions(+), 99 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 524f9ce..46ec88a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/src/virt.rs b/src/virt.rs index 60ee39a..1b40d61 100644 --- a/src/virt.rs +++ b/src/virt.rs @@ -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(store: S, client_id: &str, f: F) -> R +pub fn with_client(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( - store: S, +pub fn with_client_and_preserve( + 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( - store: S, +pub fn with_clients_and_preserve( + 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(internal: P, client_id: &str, f: F) -> R -where - F: FnOnce(Client) -> R, - P: Into, -{ - with_client(Filesystem::new(internal), client_id, f) -} - -pub fn with_ram_client(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( - 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( - 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) -} diff --git a/tests/chunked.rs b/tests/chunked.rs index 7dd222b..95c4654 100644 --- a/tests/chunked.rs +++ b/tests/chunked.rs @@ -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"))) ) diff --git a/tests/encrypted-chunked.rs b/tests/encrypted-chunked.rs index 1ecfae5..9adc294 100644 --- a/tests/encrypted-chunked.rs +++ b/tests/encrypted-chunked.rs @@ -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; diff --git a/tests/hkdf.rs b/tests/hkdf.rs index a3eacc4..2e80caf 100644 --- a/tests/hkdf.rs +++ b/tests/hkdf.rs @@ -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::::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())), diff --git a/tests/hpke.rs b/tests/hpke.rs index 59bdc8d..1cb3b14 100644 --- a/tests/hpke.rs +++ b/tests/hpke.rs @@ -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(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); + }, + ) } diff --git a/tests/manage.rs b/tests/manage.rs index 3f4d1e5..4c827a8 100644 --- a/tests/manage.rs +++ b/tests/manage.rs @@ -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]| { diff --git a/tests/wrap_key_to_file.rs b/tests/wrap_key_to_file.rs index 3e0eb0d..0612d5e 100644 --- a/tests/wrap_key_to_file.rs +++ b/tests/wrap_key_to_file.rs @@ -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(