Skip to content

Commit

Permalink
Split backend and extension
Browse files Browse the repository at this point in the history
This patch splits the trussed-auth crate into two crates:  trussed-auth
only defines the AuthExtension and can be used by clients or other
backends implementing the extension.  trussed-auth-backend contains the
AuthBackend that implements the extension using the filesystem.
  • Loading branch information
robin-nitrokey committed Apr 2, 2024
1 parent a725ae6 commit 2e4f660
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 236 deletions.
29 changes: 9 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
# Copyright (C) Nitrokey GmbH
# SPDX-License-Identifier: CC0-1.0

[package]
name = "trussed-auth"
version = "0.3.0"
[workspace]
members = ["backend", "extension"]
resolver = "2"

[workspace.package]
authors = ["Nitrokey GmbH <[email protected]>"]
edition = "2021"
repository = "https://github.com/trussed-dev/trussed-auth"
license = "Apache-2.0 OR MIT"
description = "Authentication extension and backend for Trussed"
repository = "https://github.com/trussed-dev/trussed-auth"

[dependencies]
chacha20poly1305 = { version = "0.10.1", default-features = false, features = ["reduced-round"] }
hkdf = "0.12.3"
hmac = "0.12.1"
rand_core = "0.6.4"
[workspace.dependencies]
serde = { version = "1", default-features = false }
serde-byte-array = "0.1.2"
sha2 = { version = "0.10.6", default-features = false }
subtle = { version = "2.4.1", default-features = false }
trussed = { version = "0.1.0", features = ["serde-extensions"] }
littlefs2 = "0.4.0"

[dev-dependencies]
quickcheck = { version = "1.0.3", default-features = false }
rand_core = { version = "0.6.4", default-features = false, features = ["getrandom"] }
trussed = { version = "0.1.0", features = ["serde-extensions", "virt"] }
admin-app = { version = "0.1.0", features = ["migration-tests"] }

[patch.crates-io]
trussed-auth = { path = "extension" }

littlefs2 = { git = "https://github.com/sosthene-nitrokey/littlefs2.git", rev = "2b45a7559ff44260c6dd693e4cb61f54ae5efc53" }
trussed = { git = "https://github.com/Nitrokey/trussed.git", rev = "be04182e2c74e73599a394e814d353bc4bf79484" }
trussed-manage = { git = "https://github.com/trussed-dev/trussed-staging.git", tag = "manage-v0.1.0" }
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@

.PHONY: check
check:
RUSTLFAGS='-Dwarnings' cargo check --all-features --all-targets
RUSTLFAGS='-Dwarnings' cargo check --all-features --all-targets --workspace

.PHONY: lint
lint:
cargo clippy --all-features --all-targets -- --deny warnings
cargo fmt -- --check
RUSTDOCFLAGS='-Dwarnings' cargo doc --no-deps
cargo clippy --all-features --all-targets --workspace -- --deny warnings
cargo fmt --all -- --check
RUSTDOCFLAGS='-Dwarnings' cargo doc --no-deps --workspace
reuse lint

.PHONY: test
test:
cargo test --all-features
cargo test --all-features --workspace

.PHONY: ci
ci: check lint test
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ SPDX-License-Identifier: CC0-1.0

# trussed-auth

`trussed-auth` is an extension and custom backend for [Trussed][] that provides
basic PIN handling.
`trussed-auth` is an extension for [Trussed][] that provides basic PIN
handling. `trussed-auth-backend` is a Trussed backend implementing that
extension using the filesystem. Other implementations are provided by these
backends:
- [`trussed-se050-backend`][]

[Trussed]: https://github.com/trussed-dev/trussed
[`trussed-se050-backend`]: https://github.com/Nitrokey/trussed-se050-backend

## License

Expand Down
20 changes: 20 additions & 0 deletions backend/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--
Copyright (C) Nitrokey GmbH
SPDX-License-Identifier: CC0-1.0
-->

# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

Extracted from `trussed-auth` v0.3.0.

### Breaking Changes

- Remove the `dat` intermediary directory in file storage ([#39][])

[#39]: https://github.com/trussed-dev/trussed-auth/pull/39
31 changes: 31 additions & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (C) Nitrokey GmbH
# SPDX-License-Identifier: CC0-1.0

[package]
name = "trussed-auth-backend"
version = "0.1.0"
description = "Authentication backend for Trussed"
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true

[dependencies]
serde.workspace = true
trussed.workspace = true

chacha20poly1305 = { version = "0.10.1", default-features = false, features = ["reduced-round"] }
hkdf = "0.12.3"
hmac = "0.12.1"
rand_core = "0.6.4"
serde-byte-array = "0.1.2"
sha2 = { version = "0.10.6", default-features = false }
subtle = { version = "2.4.1", default-features = false }
trussed-auth = { version = "0.3.0" }
littlefs2 = "0.4.0"

[dev-dependencies]
quickcheck = { version = "1.0.3", default-features = false }
rand_core = { version = "0.6.4", default-features = false, features = ["getrandom"] }
trussed = { version = "0.1.0", features = ["serde-extensions", "virt"] }
admin-app = { version = "0.1.0", features = ["migration-tests"] }
2 changes: 1 addition & 1 deletion src/backend/data.rs → backend/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use trussed::{
};

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

pub(crate) const SIZE: usize = 256;
pub(crate) const CHACHA_TAG_LEN: usize = 16;
Expand Down
34 changes: 25 additions & 9 deletions src/backend.rs → backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
// Copyright (C) Nitrokey GmbH
// SPDX-License-Identifier: Apache-2.0 or MIT

#![no_std]
#![warn(
missing_debug_implementations,
missing_docs,
non_ascii_idents,
trivial_casts,
unused,
unused_qualifications,
clippy::expect_used,
clippy::unwrap_used
)]
#![deny(unsafe_code)]

//! A Trussed backend implementing the [`AuthExtension`][].
//!
//! [`AuthBackend`][] is an implementation of the [`AuthExtension`][] that stores PINs in the
//! filesystem.

mod data;

pub mod migrate;

use core::fmt;

use hkdf::Hkdf;
Expand All @@ -19,15 +39,11 @@ use trussed::{
types::{CoreContext, Location, PathBuf},
Bytes,
};
use trussed_auth::{reply, AuthExtension, AuthReply, AuthRequest};

use crate::{
backend::data::{expand_app_key, get_app_salt},
extension::{reply, AuthExtension, AuthReply, AuthRequest},
BACKEND_DIR,
};
use data::{Key, PinData, Salt, KEY_LEN, SALT_LEN};
use data::{delete_app_salt, expand_app_key, get_app_salt, Key, PinData, Salt, KEY_LEN, SALT_LEN};

use self::data::delete_app_salt;
const BACKEND_DIR: &str = "backend-auth";

/// max accepted length for the hardware initial key material
pub const MAX_HW_KEY_LEN: usize = 64;
Expand Down Expand Up @@ -115,7 +131,7 @@ impl AuthBackend {
/// Creates a new `AuthBackend` with a missing hw key
///
/// Contrary to [`new`](Self::new) which uses a default `&[]` key, this will make operations depending on the hardware key to fail:
/// - [`set_pin`](crate::AuthClient::set_pin) with `derive_key = true`
/// - [`set_pin`](trussed_auth::AuthClient::set_pin) with `derive_key = true`
/// - All operations on a pin that was created with `derive_key = true`
pub fn with_missing_hw_key(location: Location, layout: FilesystemLayout) -> Self {
Self {
Expand Down Expand Up @@ -388,7 +404,7 @@ impl ExtensionImpl<AuthExtension> for AuthBackend {
}

#[derive(Clone, Copy, Debug)]
pub(crate) enum Error {
enum Error {
NotFound,
MissingHwKey,
ReadFailed,
Expand Down
2 changes: 1 addition & 1 deletion src/migrate.rs → backend/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn migrate_single(fs: &dyn DynFilesystem, path: &Path) -> Result<(), Error> {
/// ```rust
///# use littlefs2::{fs::Filesystem, const_ram_storage, path};
///# use trussed::types::{LfsResult, LfsStorage};
///# use trussed_auth::migrate::migrate_remove_dat;
///# use trussed_auth_backend::migrate::migrate_remove_dat;
///# const_ram_storage!(Storage, 4096);
///# let mut storage = Storage::new();
///# Filesystem::format(&mut storage);
Expand Down
15 changes: 10 additions & 5 deletions tests/backend.rs → backend/tests/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ mod dispatch {
service::ServiceResources,
types::{Bytes, Context, Location},
};
use trussed_auth::{AuthBackend, AuthContext, AuthExtension, MAX_HW_KEY_LEN};
use trussed_auth::AuthExtension;
use trussed_auth_backend::{AuthBackend, AuthContext, MAX_HW_KEY_LEN};

pub const BACKENDS: &[BackendId<Backend>] =
&[BackendId::Custom(Backend::Auth), BackendId::Core];
Expand Down Expand Up @@ -55,7 +56,10 @@ mod dispatch {
impl Dispatch {
pub fn new() -> Self {
Self {
auth: AuthBackend::new(Location::Internal, trussed_auth::FilesystemLayout::V0),
auth: AuthBackend::new(
Location::Internal,
trussed_auth_backend::FilesystemLayout::V0,
),
}
}

Expand All @@ -64,15 +68,15 @@ mod dispatch {
auth: AuthBackend::with_hw_key(
Location::Internal,
hw_key,
trussed_auth::FilesystemLayout::V0,
trussed_auth_backend::FilesystemLayout::V0,
),
}
}
pub fn with_missing_hw_key() -> Self {
Self {
auth: AuthBackend::with_missing_hw_key(
Location::Internal,
trussed_auth::FilesystemLayout::V0,
trussed_auth_backend::FilesystemLayout::V0,
),
}
}
Expand Down Expand Up @@ -135,7 +139,8 @@ use trussed::{
types::{Bytes, Location, Message, PathBuf},
virt::{self, Ram},
};
use trussed_auth::{AuthClient as _, PinId, MAX_HW_KEY_LEN};
use trussed_auth::{AuthClient as _, PinId};
use trussed_auth_backend::MAX_HW_KEY_LEN;

use dispatch::{Backend, Dispatch, BACKENDS};

Expand Down
6 changes: 4 additions & 2 deletions CHANGELOG.md → extension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[Unreleased]: https://github.com/trussed-dev/trussed-auth/compare/v0.3.0...HEAD

### Breaking Changes

- Extract `AuthBackend` into `trussed-auth-backend` crate

## [0.3.0][] - 2024-03-22

[0.3.0]: https://github.com/trussed-dev/trussed-auth/releases/tag/v0.3.0

### Breaking Changes

- Remove the `dat` intermediary directory in file storage ([#39][])
- Add `delete_app_keys` and `delete_auth_keys` syscalls. ([#33][])

- `delete_all_pins` now doesn't affect application keys
Expand All @@ -37,7 +40,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#35]: https://github.com/trussed-dev/trussed-auth/pull/35
[#36]: https://github.com/trussed-dev/trussed-auth/pull/36
[#37]: https://github.com/trussed-dev/trussed-auth/pull/37
[#39]: https://github.com/trussed-dev/trussed-auth/pull/39

## [0.2.2][] - 2023-04-26

Expand Down
15 changes: 15 additions & 0 deletions extension/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-auth"
version = "0.3.0"
description = "Authentication extension for Trussed"
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true

[dependencies]
serde.workspace = true
trussed.workspace = true
Loading

0 comments on commit 2e4f660

Please sign in to comment.