Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trait for StatusBytes and allow lazyly configuring the initialization of the RNG #26

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 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,17 @@ pub trait Reboot {
fn locked() -> bool;
}

/// Trait indicating that a value can be used as a status
pub trait StatusBytes {
type Serialized: 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;
/// Serialize the StatusBytes to raw bytes
fn serialize(&self) -> Self::Serialized;
}

pub struct App<T, R, S, C = ()> {
trussed: T,
uuid: [u8; 16],
Expand All @@ -195,7 +207,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,7 +390,13 @@ where
syscall!(self.trussed.wink(Duration::from_secs(10)));
}
Command::Status => {
response.extend_from_slice(self.status.as_ref()).ok();
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.serialize().as_ref())
.ok();
}
Command::TestSe05X => {
#[cfg(feature = "se050")]
Expand Down Expand Up @@ -508,7 +526,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 +569,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 +581,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