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 test function for the SE050 #5

Merged
merged 7 commits into from
Aug 24, 2023
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
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ delog = "0.1"
iso7816 = "0.1"
trussed = "0.1"

se05x = { version = "0.0.1", optional = true }
embedded-hal = { version = "0.2.7", optional = true }
hex-literal = "0.4.1"
rand_chacha = { version = "0.3.1", optional = true, default-features = false }

[features]
default = ["se050"]
se050 = ["dep:se05x", "embedded-hal", "rand_chacha"]

log-all = []
log-none = []
log-info = []
Expand All @@ -27,3 +35,5 @@ log-error = []
[patch.crates-io]
ctaphid-dispatch = { git = "https://github.com/trussed-dev/ctaphid-dispatch.git", rev = "57cb3317878a8593847595319aa03ef17c29ec5b" }
trussed = { git = "https://github.com/trussed-dev/trussed.git", rev = "51e68500d7601d04f884f5e95567d14b9018a6cb" }
iso7816 = { git = "https://github.com/sosthene-nitrokey/iso7816.git", rev = "160ca3bbd8e21ec4e4ee1e0748e1eaa53a45c97f"}
se05x = { git = "https://github.com/Nitrokey/se05x.git", rev = "db1ddea25cc382355b4292352652da656abc3005"}
78 changes: 73 additions & 5 deletions src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use apdu_dispatch::{app as apdu, command, response, Command as ApduCommand};
use core::{convert::TryInto, marker::PhantomData, time::Duration};
use ctaphid_dispatch::app::{self as hid, Command as HidCommand, Message};
use ctaphid_dispatch::command::VendorCommand;
#[cfg(feature = "se050")]
use embedded_hal::blocking::delay::DelayUs;
#[cfg(feature = "se050")]
use se05x::{se05x::Se05X, t1::I2CForT1};
use trussed::{interrupt::InterruptFlag, syscall, types::Vec, Client as TrussedClient};

pub const USER_PRESENCE_TIMEOUT_SECS: u32 = 15;
Expand All @@ -11,6 +15,7 @@ pub const USER_PRESENCE_TIMEOUT_SECS: u32 = 15;
// application). The actual application command is stored in the first byte of the packet data.
const ADMIN: VendorCommand = VendorCommand::H72;
const STATUS: u8 = 0x80;
const TEST_SE050: u8 = 0x81;

// For compatibility, old commands are also available directly as separate vendor commands.
const UPDATE: VendorCommand = VendorCommand::H51;
Expand All @@ -25,7 +30,19 @@ const WINK: HidCommand = HidCommand::Wink; // 0x08

const RNG_DATA_LEN: usize = 57;

#[derive(PartialEq)]
mod run_tests;
use run_tests::*;

/// Trait representing the possible ownership of the SE050 by the admin app.
///
/// Implemented by `()` and the `Se05X` struct
pub trait MaybeSe: RunTests {}

impl MaybeSe for () {}
#[cfg(feature = "se050")]
impl<Twi: I2CForT1, D: DelayUs<u32>> MaybeSe for Se05X<Twi, D> {}

#[derive(PartialEq, Debug)]
enum Command {
Update,
Reboot,
Expand All @@ -35,6 +52,7 @@ enum Command {
Locked,
Wink,
Status,
TestSe05X,
}

impl TryFrom<u8> for Command {
Expand All @@ -51,6 +69,7 @@ impl TryFrom<u8> for Command {
// Now check the new commands.
match command {
STATUS => Ok(Command::Status),
TEST_SE050 => Ok(Command::TestSe05X),
_ => Err(Error::UnsupportedCommand),
}
}
Expand Down Expand Up @@ -134,7 +153,7 @@ pub trait Reboot {
fn locked() -> bool;
}

pub struct App<T, R, S>
pub struct App<T, R, S, Se05X = ()>
where
T: TrussedClient,
R: Reboot,
Expand All @@ -146,6 +165,7 @@ where
full_version: &'static str,
status: S,
boot_interface: PhantomData<R>,
se050: Se05X,
}

impl<T, R, S> App<T, R, S>
Expand All @@ -168,9 +188,47 @@ where
full_version,
status,
boot_interface: PhantomData,
se050: (),
}
}
}

#[cfg(feature = "se050")]
impl<T, R, S, Twi, D> App<T, R, S, Se05X<Twi, D>>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
Twi: I2CForT1,
D: DelayUs<u32>,
{
pub fn with_se(
client: T,
uuid: [u8; 16],
version: u32,
full_version: &'static str,
status: S,
se050: Se05X<Twi, D>,
) -> Self {
Self {
trussed: client,
uuid,
version,
full_version,
status,
boot_interface: PhantomData,
se050,
}
}
}

impl<T, R, S, Se05X> App<T, R, S, Se05X>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
Se05X: MaybeSe,
{
fn user_present(&mut self) -> bool {
let user_present = syscall!(self
.trussed
Expand All @@ -185,6 +243,7 @@ where
flag: Option<u8>,
response: &mut Vec<u8, N>,
) -> Result<(), Error> {
debug_now!("Executing command: {command:?}");
match command {
Command::Reboot => R::reboot(),
Command::Locked => {
Expand Down Expand Up @@ -228,16 +287,23 @@ where
Command::Status => {
response.extend_from_slice(self.status.as_ref()).ok();
}
Command::TestSe05X => {
debug_now!("Running se050 tests");
if let Err(_err) = self.se050.run_tests(response) {
debug_now!("se050 tests failed: {_err:?}");
}
}
}
Ok(())
}
}

impl<T, R, S> hid::App<'static> for App<T, R, S>
impl<T, R, S, Se> hid::App<'static> for App<T, R, S, Se>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
Se: MaybeSe,
{
fn commands(&self) -> &'static [HidCommand] {
&[
Expand Down Expand Up @@ -276,23 +342,25 @@ where
}
}

impl<T, R, S> iso7816::App for App<T, R, S>
impl<T, R, S, Se> iso7816::App for App<T, R, S, Se>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
Se: MaybeSe,
{
// Solo management app
fn aid(&self) -> iso7816::Aid {
iso7816::Aid::new(&[0xA0, 0x00, 0x00, 0x08, 0x47, 0x00, 0x00, 0x00, 0x01])
}
}

impl<T, R, S> apdu::App<{ command::SIZE }, { response::SIZE }> for App<T, R, S>
impl<T, R, S, Se> apdu::App<{ command::SIZE }, { response::SIZE }> for App<T, R, S, Se>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
Se: MaybeSe,
{
fn select(&mut self, _apdu: &ApduCommand, _reply: &mut response::Data) -> apdu::Result {
Ok(())
Expand Down
Loading