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 351a879 commit 7181fb9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,16 @@ admin-app = { version = "0.1.0", features = ["migration-tests"] }
serde_test = "1.0.176"

[patch.crates-io]
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", rev = "960e57d9fc0d209308c8e15dc26252bbe1ff6ba8" }
littlefs2 = { git = "https://github.com/trussed-dev/littlefs2.git", branch = "core" }
littlefs2-core = { git = "https://github.com/trussed-dev/littlefs2.git", branch = "core" }
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" }
trussed = { git = "https://github.com/trussed-dev/trussed.git", rev = "667d60c019d485524a276f2a4dd07aaa66e71021" }
trussed-auth = { git = "https://github.com/trussed-dev/trussed-auth.git", tag = "v0.3.0"}
trussed = { git = "https://github.com/trussed-dev/trussed.git", branch = "littlefs2" }
trussed-auth = { git = "https://github.com/trussed-dev/trussed-auth.git", branch = "littlefs2" }
trussed-manage = { git = "https://github.com/trussed-dev/trussed-staging.git", tag = "manage-v0.1.0" }
trussed-rsa-alloc = { git = "https://github.com/trussed-dev/trussed-rsa-backend.git", tag = "v0.2.1" }
trussed-wrap-key-to-file = { git = "https://github.com/trussed-dev/trussed-staging.git", tag = "wrap-key-to-file-v0.1.0" }
admin-app = { git = "https://github.com/Nitrokey/admin-app.git", tag = "v0.1.0-nitrokey.12" }
admin-app = { git = "https://github.com/Nitrokey/admin-app.git", branch = "littlefs2" }

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

Expand Down
7 changes: 5 additions & 2 deletions src/core_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use crypto_bigint::{
};
use embedded_hal::blocking::delay::DelayUs;
use hex_literal::hex;
use littlefs2::path::PathBuf;
use littlefs2::{
path,
path::{Path, PathBuf},
};
use rand::{CryptoRng, RngCore};
use se05x::{
se05x::{
Expand Down Expand Up @@ -45,7 +48,7 @@ use crate::{
};

pub(crate) const BUFFER_LEN: usize = 2048;
pub(crate) const CORE_DIR: &str = "se050-core";
pub(crate) const CORE_DIR: &Path = path!("se050-core");

#[derive(Serialize, Deserialize, Debug, Clone)]
struct VolatileKeyMaterial {
Expand Down
2 changes: 1 addition & 1 deletion src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::BACKEND_DIR;

fn migrate_single(fs: &dyn DynFilesystem, path: &Path) -> Result<(), Error> {
match fs.remove_dir_all(path) {
Err(Error::NoSuchEntry) => Ok(()),
Err(Error::NO_SUCH_ENTRY) => Ok(()),
Err(err) => Err(err),
Ok(()) => Ok(()),
}
Expand Down
6 changes: 3 additions & 3 deletions src/trussed_auth_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,19 @@ impl<Twi: I2CForT1, D: DelayUs<u32>> Se050Backend<Twi, D> {
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.metadata_location)
.or_else(|_| {
if global_fs.exists(&path, self.metadata_location) {
if global_fs.exists(path, self.metadata_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.metadata_location, &salt)
.write(path, self.metadata_location, &salt)
.or(Err(Error::WriteFailed))
.and(Ok(salt))
})
Expand Down
20 changes: 8 additions & 12 deletions src/trussed_auth_impl/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ use trussed::{
api::NotBefore,
platform::CryptoRng,
service::{Filestore, RngCore},
types::{Bytes, Location, Path, PathBuf},
types::{Bytes, Location, Path},
};
use trussed_auth::{request, PinId, MAX_PIN_LENGTH};

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

#[derive(Serialize, Deserialize, Debug, Clone)]
enum PinSeId {
Raw(PinObjectId),
Expand All @@ -49,18 +51,12 @@ impl PinSeId {
}
}

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 @@ -71,8 +67,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 @@ -85,13 +81,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

0 comments on commit 7181fb9

Please sign in to comment.