Skip to content

Commit

Permalink
Add trait for StatusBytes and allow lazyly configuring the initializa…
Browse files Browse the repository at this point in the history
…tion of the RNG
  • Loading branch information
sosthene-nitrokey committed Jul 29, 2024
1 parent 4dd16c0 commit 189d6a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use ctaphid_dispatch::command::VendorCommand;
use littlefs2::path::PathBuf;
use serde::Deserialize;
use trussed::store::Store;
use trussed::try_syscall;
use trussed::{interrupt::InterruptFlag, store::filestore::Filestore, syscall, types::Vec};

use crate::config::{self, Config, ConfigError};
Expand Down Expand Up @@ -180,6 +181,14 @@ pub trait Reboot {
fn locked() -> bool;
}

/// Trait indicating that a value can be used as a status
pub trait StatusBytes: AsRef<[u8]> {
// Set the flag indicating that the random generator could properly be created (`false`) or not (`true`)
fn set_random_error(&mut self, value: bool);
// Get the flag indicating that the random generator could properly be created (`false`) or not (`true`)
fn get_random_error(&self) -> bool;
}

pub struct App<T, R, S, C = ()> {
trussed: T,
uuid: [u8; 16],
Expand All @@ -195,7 +204,7 @@ impl<T, R, S, C> App<T, R, S, C>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
S: StatusBytes,
C: Config,
{
/// Create an admin app instance, loading the configuration from the filesystem.
Expand Down Expand Up @@ -378,6 +387,10 @@ where
syscall!(self.trussed.wink(Duration::from_secs(10)));
}
Command::Status => {
if !self.status.get_random_error() {
let is_random_working = try_syscall!(self.trussed.random_bytes(1)).is_ok();
self.status.set_random_error(!is_random_working);
}
response.extend_from_slice(self.status.as_ref()).ok();
}
Command::TestSe05X => {
Expand Down Expand Up @@ -508,7 +521,7 @@ impl<T, R, S, C> hid::App<'static> for App<T, R, S, C>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
S: StatusBytes,
C: Config,
{
fn commands(&self) -> &'static [HidCommand] {
Expand Down Expand Up @@ -551,7 +564,7 @@ impl<T, R, S, C> iso7816::App for App<T, R, S, C>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
S: StatusBytes,
{
// Solo management app
fn aid(&self) -> iso7816::Aid {
Expand All @@ -563,7 +576,7 @@ impl<T, R, S, C> apdu::App<{ command::SIZE }, { response::SIZE }> for App<T, R,
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
S: StatusBytes,
C: Config,
{
fn select(
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod admin;
mod config;
pub mod migrations;

pub use admin::{App, Reboot};
pub use admin::{App, Reboot, StatusBytes};
pub use config::{
Config, ConfigError, ConfigValueMut, ResetConfigResult, ResetSignal, ResetSignalAllocation,
};
Expand Down

0 comments on commit 189d6a3

Please sign in to comment.