Skip to content

Commit

Permalink
Update littlefs2
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-nitrokey committed Aug 13, 2024
1 parent 947ffe6 commit 0d24dca
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ serde_cbor = { version = "0.11.2", features = ["std"] }
hex-literal = "0.4.1"

[patch.crates-io]
littlefs2 = { git = "https://github.com/sosthene-nitrokey/littlefs2.git", rev = "2b45a7559ff44260c6dd693e4cb61f54ae5efc53" }
trussed = { git = "https://github.com/Nitrokey/trussed.git", tag = "v0.1.0-nitrokey.19" }
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", branch = "core" }
trussed = { git = "https://github.com/trussed-dev/trussed.git", branch = "littlefs2" }
trussed-manage = { git = "https://github.com/trussed-dev/trussed-staging.git", tag = "manage-v0.1.0" }
apdu-dispatch = { git = "https://github.com/trussed-dev/apdu-dispatch.git", rev = "915fc237103fcecc29d0f0b73391f19abf6576de" }
ctaphid-dispatch = { git = "https://github.com/trussed-dev/ctaphid-dispatch.git", rev = "57cb3317878a8593847595319aa03ef17c29ec5b" }
Expand Down
6 changes: 3 additions & 3 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,19 @@ impl AuthBackend {
global_fs: &mut impl Filestore,
rng: &mut R,
) -> Result<Salt, Error> {
let path = PathBuf::from("salt");
let path = path!("salt");
global_fs
.read(&path, self.location)
.or_else(|_| {
if global_fs.exists(&path, self.location) {
if global_fs.exists(path, self.location) {
return Err(Error::ReadFailed);
}

let mut salt = Bytes::<SALT_LEN>::default();
salt.resize_to_capacity();
rng.fill_bytes(&mut salt);
global_fs
.write(&path, self.location, &salt)
.write(path, self.location, &salt)
.or(Err(Error::WriteFailed))
.and(Ok(salt))
})
Expand Down
21 changes: 9 additions & 12 deletions src/backend/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ use core::ops::Deref;

use chacha20poly1305::ChaCha8Poly1305;
use hmac::{Hmac, Mac};
use littlefs2::{path, path::Path};
use serde::{Deserialize, Serialize};
use serde_byte_array::ByteArray;
use sha2::{Digest as _, Sha256};
use subtle::ConstantTimeEq as _;
use trussed::{
platform::{CryptoRng, RngCore},
store::filestore::Filestore,
types::{Location, PathBuf},
types::Location,
Bytes,
};

use super::Error;
use crate::{Pin, PinId, MAX_PIN_LENGTH};

const APP_SALT_PATH: &Path = path!("application_salt");

pub(crate) const SIZE: usize = 256;
pub(crate) const CHACHA_TAG_LEN: usize = 16;
pub(crate) const SALT_LEN: usize = 16;
Expand Down Expand Up @@ -502,18 +505,12 @@ fn pin_len(pin: &Pin) -> u8 {
pin.len() as u8
}

fn app_salt_path() -> PathBuf {
const SALT_PATH: &str = "application_salt";

PathBuf::from(SALT_PATH)
}

pub(crate) fn get_app_salt<S: Filestore, R: CryptoRng + RngCore>(
fs: &mut S,
rng: &mut R,
location: Location,
) -> Result<Salt, Error> {
if !fs.exists(&app_salt_path(), location) {
if !fs.exists(APP_SALT_PATH, location) {
create_app_salt(fs, rng, location)
} else {
load_app_salt(fs, location)
Expand All @@ -524,8 +521,8 @@ pub(crate) fn delete_app_salt<S: Filestore>(
fs: &mut S,
location: Location,
) -> Result<(), trussed::Error> {
if fs.exists(&app_salt_path(), location) {
fs.remove_file(&app_salt_path(), location)
if fs.exists(APP_SALT_PATH, location) {
fs.remove_file(APP_SALT_PATH, location)
} else {
Ok(())
}
Expand All @@ -538,13 +535,13 @@ fn create_app_salt<S: Filestore, R: CryptoRng + RngCore>(
) -> Result<Salt, Error> {
let mut salt = Salt::default();
rng.fill_bytes(&mut *salt);
fs.write(&app_salt_path(), location, &*salt)
fs.write(APP_SALT_PATH, location, &*salt)
.map_err(|_| Error::WriteFailed)?;
Ok(salt)
}

fn load_app_salt<S: Filestore>(fs: &mut S, location: Location) -> Result<Salt, Error> {
fs.read(&app_salt_path(), location)
fs.read(APP_SALT_PATH, location)
.map_err(|_| Error::ReadFailed)
.and_then(|b: Bytes<SALT_LEN>| (**b).try_into().map_err(|_| Error::ReadFailed))
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl PinId {
path[0..4].copy_from_slice(b"pin.");
path[4..].copy_from_slice(&self.hex());

PathBuf::from(&path)
PathBuf::try_from(&path).unwrap()
}

/// Get the hex representation of the PIN id
Expand Down Expand Up @@ -182,7 +182,7 @@ mod tests {
for i in 0..=u8::MAX {
assert_eq!(Ok(PinId(i)), PinId::from_path(PinId(i).path().as_ref()));
let actual = PinId(i).path();
let expected = PathBuf::from(format!("pin.{i:02x}").as_str());
let expected = PathBuf::try_from(format!("pin.{i:02x}").as_str()).unwrap();
println!("id: {i}, actual: {actual}, expected: {expected}");
assert_eq!(actual, expected);
}
Expand Down
2 changes: 1 addition & 1 deletion src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn migrate_single(fs: &dyn DynFilesystem, path: &Path) -> Result<(), Error> {
});
match dir_res {
Ok(()) => fs.remove_dir(&path_dat),
Err(Error::NoSuchEntry) => Ok(()),
Err(Error::NO_SUCH_ENTRY) => Ok(()),
Err(_) => dir_res,
}
}
Expand Down
7 changes: 4 additions & 3 deletions tests/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,14 @@ mod dispatch {
}
}

use littlefs2::path;
use rand_core::{OsRng, RngCore as _};
use trussed::{
backend::BackendId,
client::{ClientImplementation, FilesystemClient, HmacSha256},
service::Service,
syscall, try_syscall,
types::{Bytes, Location, Message, PathBuf},
types::{Bytes, Location, Message},
virt::{self, Ram},
};
use trussed_auth::{AuthClient as _, PinId, MAX_HW_KEY_LEN};
Expand Down Expand Up @@ -652,7 +653,7 @@ fn delete_all_pins() {
let reply = syscall!(client.has_pin(Pin::Admin));
assert!(reply.has_pin);
assert!(try_syscall!(
client.read_file(Location::Internal, PathBuf::from("/backend-auth/pin.00"))
client.read_file(Location::Internal, path!("/backend-auth/pin.00").into())
)
.is_err());

Expand Down Expand Up @@ -741,7 +742,7 @@ fn reset_auth_data() {
let reply = syscall!(client.has_pin(Pin::Admin));
assert!(reply.has_pin);
assert!(try_syscall!(
client.read_file(Location::Internal, PathBuf::from("/backend-auth/pin.00"))
client.read_file(Location::Internal, path!("/backend-auth/pin.00").into())
)
.is_err());

Expand Down

0 comments on commit 0d24dca

Please sign in to comment.