Skip to content

Commit

Permalink
Merge pull request #26 from Nitrokey/test-rng
Browse files Browse the repository at this point in the history
Add trait for StatusBytes and allow lazyly configuring the initialization of the RNG
  • Loading branch information
sosthene-nitrokey authored Jul 31, 2024
2 parents 4dd16c0 + 914da07 commit c241491
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
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

0 comments on commit c241491

Please sign in to comment.